Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: dsa: microchip: Add partial ACL support for ksz9477 switches

This patch adds partial Access Control List (ACL) support for the
ksz9477 family of switches. ACLs enable filtering of incoming layer 2
MAC, layer 3 IP, and layer 4 TCP/UDP packets on each port. They provide
additional capabilities for filtering routed network protocols and can
take precedence over other forwarding functions.

ACLs can filter ingress traffic based on header fields such as
source/destination MAC address, EtherType, IPv4 address, IPv4 protocol,
UDP/TCP ports, and TCP flags. The ACL is an ordered list of up to 16
access control rules programmed into the ACL Table. Each entry specifies
a set of matching conditions and action rules for controlling packet
forwarding and priority.

The ACL also implements a count function, generating an interrupt
instead of a forwarding action. It can be used as a watchdog timer or an
event counter. The ACL consists of three parts: matching rules, action
rules, and processing entries. Multiple match conditions can be either
AND'ed or OR'ed together.

This patch introduces support for a subset of the available ACL
functionality, specifically layer 2 matching and prioritization of
matched packets. For example:

tc qdisc add dev lan2 clsact
tc filter add dev lan2 ingress protocol 0x88f7 flower action skbedit prio 7

tc qdisc add dev lan1 clsact
tc filter add dev lan1 ingress protocol 0x88f7 flower action skbedit prio 7

The hardware offloading implementation was benchmarked against a
configuration without hardware offloading. This latter setup relied on a
software-based Linux bridge. No noticeable differences were observed
between the two configurations. Here is an example of software-based
test:

ip l s dev enu1u1 up
ip l s dev enu1u2 up
ip l s dev enu1u4 up
ethtool -A enu1u1 autoneg off rx off tx off
ethtool -A enu1u2 autoneg off rx off tx off
ethtool -A enu1u4 autoneg off rx off tx off
ip l a name br0 type bridge
ip l s dev br0 up
ip l s enu1u1 master br0
ip l s enu1u2 master br0
ip l s enu1u4 master br0

tc qdisc add dev enu1u1 root handle 1: ets strict 4 priomap 3 3 2 2 1 1 0 0
tc qdisc add dev enu1u4 root handle 1: ets strict 4 priomap 3 3 2 2 1 1 0 0
tc qdisc add dev enu1u2 root handle 1: ets strict 4 priomap 3 3 2 2 1 1 0 0

tc qdisc add dev enu1u1 clsact
tc filter add dev enu1u1 ingress protocol ipv4 flower action skbedit prio 7

tc qdisc add dev enu1u4 clsact
tc filter add dev enu1u4 ingress protocol ipv4 flower action skbedit prio 0

On a system attached to the port enu1u2 I run two iperf3 server
instances:
iperf3 -s -p 5210 &
iperf3 -s -p 5211 &

On systems attached to enu1u4 and enu1u1 I run:
iperf3 -u -c 172.17.0.1 -p 5210 -b100M -l1472 -t100
and
iperf3 -u -c 172.17.0.1 -p 5211 -b100M -l1472 -t100

As a result, IP traffic on port enu1u1 will be prioritized and take
precedence over IP traffic on port enu1u4

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
Reviewed-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Oleksij Rempel and committed by
David S. Miller
002841be 15299227

+1815 -1
+1 -1
drivers/net/dsa/microchip/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 2 obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ_COMMON) += ksz_switch.o 3 3 ksz_switch-objs := ksz_common.o 4 - ksz_switch-objs += ksz9477.o 4 + ksz_switch-objs += ksz9477.o ksz9477_acl.o ksz9477_tc_flower.o 5 5 ksz_switch-objs += ksz8795.o 6 6 ksz_switch-objs += lan937x_main.o 7 7
+2
drivers/net/dsa/microchip/ksz9477.c
··· 1004 1004 /* clear pending interrupts */ 1005 1005 if (dev->info->internal_phy[port]) 1006 1006 ksz_pread16(dev, port, REG_PORT_PHY_INT_ENABLE, &data16); 1007 + 1008 + ksz9477_port_acl_init(dev, port); 1007 1009 } 1008 1010 1009 1011 void ksz9477_config_cpu_port(struct dsa_switch *ds)
+36
drivers/net/dsa/microchip/ksz9477.h
··· 57 57 void ksz9477_switch_exit(struct ksz_device *dev); 58 58 void ksz9477_port_queue_split(struct ksz_device *dev, int port); 59 59 60 + int ksz9477_port_acl_init(struct ksz_device *dev, int port); 61 + void ksz9477_port_acl_free(struct ksz_device *dev, int port); 62 + int ksz9477_cls_flower_add(struct dsa_switch *ds, int port, 63 + struct flow_cls_offload *cls, bool ingress); 64 + int ksz9477_cls_flower_del(struct dsa_switch *ds, int port, 65 + struct flow_cls_offload *cls, bool ingress); 66 + 67 + #define KSZ9477_ACL_ENTRY_SIZE 18 68 + #define KSZ9477_ACL_MAX_ENTRIES 16 69 + 70 + struct ksz9477_acl_entry { 71 + u8 entry[KSZ9477_ACL_ENTRY_SIZE]; 72 + unsigned long cookie; 73 + u32 prio; 74 + }; 75 + 76 + struct ksz9477_acl_entries { 77 + struct ksz9477_acl_entry entries[KSZ9477_ACL_MAX_ENTRIES]; 78 + int entries_count; 79 + }; 80 + 81 + struct ksz9477_acl_priv { 82 + struct ksz9477_acl_entries acles; 83 + }; 84 + 85 + void ksz9477_acl_remove_entries(struct ksz_device *dev, int port, 86 + struct ksz9477_acl_entries *acles, 87 + unsigned long cookie); 88 + int ksz9477_acl_write_list(struct ksz_device *dev, int port); 89 + int ksz9477_sort_acl_entries(struct ksz_device *dev, int port); 90 + void ksz9477_acl_action_rule_cfg(u8 *entry, bool force_prio, u8 prio_val); 91 + void ksz9477_acl_processing_rule_set_action(u8 *entry, u8 action_idx); 92 + void ksz9477_acl_match_process_l2(struct ksz_device *dev, int port, 93 + u16 ethtype, u8 *src_mac, u8 *dst_mac, 94 + unsigned long cookie, u32 prio); 95 + 60 96 #endif
+1436
drivers/net/dsa/microchip/ksz9477_acl.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + // Copyright (c) 2023 Pengutronix, Oleksij Rempel <kernel@pengutronix.de> 3 + 4 + /* Access Control List (ACL) structure: 5 + * 6 + * There are multiple groups of registers involved in ACL configuration: 7 + * 8 + * - Matching Rules: These registers define the criteria for matching incoming 9 + * packets based on their header information (Layer 2 MAC, Layer 3 IP, or 10 + * Layer 4 TCP/UDP). Different register settings are used depending on the 11 + * matching rule mode (MD) and the Enable (ENB) settings. 12 + * 13 + * - Action Rules: These registers define how the ACL should modify the packet's 14 + * priority, VLAN tag priority, and forwarding map once a matching rule has 15 + * been triggered. The settings vary depending on whether the matching rule is 16 + * in Count Mode (MD = 01 and ENB = 00) or not. 17 + * 18 + * - Processing Rules: These registers control the overall behavior of the ACL, 19 + * such as selecting which matching rule to apply first, enabling/disabling 20 + * specific rules, or specifying actions for matched packets. 21 + * 22 + * ACL Structure: 23 + * +----------------------+ 24 + * +----------------------+ | (optional) | 25 + * | Matching Rules | | Matching Rules | 26 + * | (Layer 2, 3, 4) | | (Layer 2, 3, 4) | 27 + * +----------------------+ +----------------------+ 28 + * | | 29 + * \___________________________/ 30 + * v 31 + * +----------------------+ 32 + * | Processing Rules | 33 + * | (action idx, | 34 + * | matching rule set) | 35 + * +----------------------+ 36 + * | 37 + * v 38 + * +----------------------+ 39 + * | Action Rules | 40 + * | (Modify Priority, | 41 + * | Forwarding Map, | 42 + * | VLAN tag, etc) | 43 + * +----------------------+ 44 + */ 45 + 46 + #include <linux/bitops.h> 47 + 48 + #include "ksz9477.h" 49 + #include "ksz9477_reg.h" 50 + #include "ksz_common.h" 51 + 52 + #define KSZ9477_PORT_ACL_0 0x600 53 + 54 + enum ksz9477_acl_port_access { 55 + KSZ9477_ACL_PORT_ACCESS_0 = 0x00, 56 + KSZ9477_ACL_PORT_ACCESS_1 = 0x01, 57 + KSZ9477_ACL_PORT_ACCESS_2 = 0x02, 58 + KSZ9477_ACL_PORT_ACCESS_3 = 0x03, 59 + KSZ9477_ACL_PORT_ACCESS_4 = 0x04, 60 + KSZ9477_ACL_PORT_ACCESS_5 = 0x05, 61 + KSZ9477_ACL_PORT_ACCESS_6 = 0x06, 62 + KSZ9477_ACL_PORT_ACCESS_7 = 0x07, 63 + KSZ9477_ACL_PORT_ACCESS_8 = 0x08, 64 + KSZ9477_ACL_PORT_ACCESS_9 = 0x09, 65 + KSZ9477_ACL_PORT_ACCESS_A = 0x0A, 66 + KSZ9477_ACL_PORT_ACCESS_B = 0x0B, 67 + KSZ9477_ACL_PORT_ACCESS_C = 0x0C, 68 + KSZ9477_ACL_PORT_ACCESS_D = 0x0D, 69 + KSZ9477_ACL_PORT_ACCESS_E = 0x0E, 70 + KSZ9477_ACL_PORT_ACCESS_F = 0x0F, 71 + KSZ9477_ACL_PORT_ACCESS_10 = 0x10, 72 + KSZ9477_ACL_PORT_ACCESS_11 = 0x11 73 + }; 74 + 75 + #define KSZ9477_ACL_MD_MASK GENMASK(5, 4) 76 + #define KSZ9477_ACL_MD_DISABLE 0 77 + #define KSZ9477_ACL_MD_L2_MAC 1 78 + #define KSZ9477_ACL_MD_L3_IP 2 79 + #define KSZ9477_ACL_MD_L4_TCP_UDP 3 80 + 81 + #define KSZ9477_ACL_ENB_MASK GENMASK(3, 2) 82 + #define KSZ9477_ACL_ENB_L2_COUNTER 0 83 + #define KSZ9477_ACL_ENB_L2_TYPE 1 84 + #define KSZ9477_ACL_ENB_L2_MAC 2 85 + #define KSZ9477_ACL_ENB_L2_MAC_TYPE 3 86 + 87 + /* only IPv4 src or dst can be used with mask */ 88 + #define KSZ9477_ACL_ENB_L3_IPV4_ADDR_MASK 1 89 + /* only IPv4 src and dst can be used without mask */ 90 + #define KSZ9477_ACL_ENB_L3_IPV4_ADDR_SRC_DST 2 91 + 92 + #define KSZ9477_ACL_ENB_L4_IP_PROTO 0 93 + #define KSZ9477_ACL_ENB_L4_TCP_SRC_DST_PORT 1 94 + #define KSZ9477_ACL_ENB_L4_UDP_SRC_DST_PORT 2 95 + #define KSZ9477_ACL_ENB_L4_TCP_SEQ_NUMBER 3 96 + 97 + #define KSZ9477_ACL_SD_SRC BIT(1) 98 + #define KSZ9477_ACL_SD_DST 0 99 + #define KSZ9477_ACL_EQ_EQUAL BIT(0) 100 + #define KSZ9477_ACL_EQ_NOT_EQUAL 0 101 + 102 + #define KSZ9477_ACL_PM_M GENMASK(7, 6) 103 + #define KSZ9477_ACL_PM_DISABLE 0 104 + #define KSZ9477_ACL_PM_HIGHER 1 105 + #define KSZ9477_ACL_PM_LOWER 2 106 + #define KSZ9477_ACL_PM_REPLACE 3 107 + #define KSZ9477_ACL_P_M GENMASK(5, 3) 108 + 109 + #define KSZ9477_PORT_ACL_CTRL_0 0x0612 110 + 111 + #define KSZ9477_ACL_WRITE_DONE BIT(6) 112 + #define KSZ9477_ACL_READ_DONE BIT(5) 113 + #define KSZ9477_ACL_WRITE BIT(4) 114 + #define KSZ9477_ACL_INDEX_M GENMASK(3, 0) 115 + 116 + /** 117 + * ksz9477_dump_acl_index - Print the ACL entry at the specified index 118 + * 119 + * @dev: Pointer to the ksz9477 device structure. 120 + * @acle: Pointer to the ACL entry array. 121 + * @index: The index of the ACL entry to print. 122 + * 123 + * This function prints the details of an ACL entry, located at a particular 124 + * index within the ksz9477 device's ACL table. It omits printing entries that 125 + * are empty. 126 + * 127 + * Return: 1 if the entry is non-empty and printed, 0 otherwise. 128 + */ 129 + static int ksz9477_dump_acl_index(struct ksz_device *dev, 130 + struct ksz9477_acl_entry *acle, int index) 131 + { 132 + bool empty = true; 133 + char buf[64]; 134 + u8 *entry; 135 + int i; 136 + 137 + entry = &acle[index].entry[0]; 138 + for (i = 0; i <= KSZ9477_ACL_PORT_ACCESS_11; i++) { 139 + if (entry[i]) 140 + empty = false; 141 + 142 + sprintf(buf + (i * 3), "%02x ", entry[i]); 143 + } 144 + 145 + /* no need to print empty entries */ 146 + if (empty) 147 + return 0; 148 + 149 + dev_err(dev->dev, " Entry %02d, prio: %02d : %s", index, 150 + acle[index].prio, buf); 151 + 152 + return 1; 153 + } 154 + 155 + /** 156 + * ksz9477_dump_acl - Print ACL entries 157 + * 158 + * @dev: Pointer to the device structure. 159 + * @acle: Pointer to the ACL entry array. 160 + */ 161 + static void ksz9477_dump_acl(struct ksz_device *dev, 162 + struct ksz9477_acl_entry *acle) 163 + { 164 + int count = 0; 165 + int i; 166 + 167 + for (i = 0; i < KSZ9477_ACL_MAX_ENTRIES; i++) 168 + count += ksz9477_dump_acl_index(dev, acle, i); 169 + 170 + if (count != KSZ9477_ACL_MAX_ENTRIES - 1) 171 + dev_err(dev->dev, " Empty ACL entries were skipped\n"); 172 + } 173 + 174 + /** 175 + * ksz9477_acl_is_valid_matching_rule - Check if an ACL entry contains a valid 176 + * matching rule. 177 + * 178 + * @entry: Pointer to ACL entry buffer 179 + * 180 + * This function checks if the given ACL entry buffer contains a valid 181 + * matching rule by inspecting the Mode (MD) and Enable (ENB) fields. 182 + * 183 + * Returns: True if it's a valid matching rule, false otherwise. 184 + */ 185 + static bool ksz9477_acl_is_valid_matching_rule(u8 *entry) 186 + { 187 + u8 val1, md, enb; 188 + 189 + val1 = entry[KSZ9477_ACL_PORT_ACCESS_1]; 190 + 191 + md = FIELD_GET(KSZ9477_ACL_MD_MASK, val1); 192 + if (md == KSZ9477_ACL_MD_DISABLE) 193 + return false; 194 + 195 + if (md == KSZ9477_ACL_MD_L2_MAC) { 196 + /* L2 counter is not support, so it is not valid rule for now */ 197 + enb = FIELD_GET(KSZ9477_ACL_ENB_MASK, val1); 198 + if (enb == KSZ9477_ACL_ENB_L2_COUNTER) 199 + return false; 200 + } 201 + 202 + return true; 203 + } 204 + 205 + /** 206 + * ksz9477_acl_get_cont_entr - Get count of contiguous ACL entries and validate 207 + * the matching rules. 208 + * @dev: Pointer to the KSZ9477 device structure. 209 + * @port: Port number. 210 + * @index: Index of the starting ACL entry. 211 + * 212 + * Based on the KSZ9477 switch's Access Control List (ACL) system, the RuleSet 213 + * in an ACL entry indicates which entries contain Matching rules linked to it. 214 + * This RuleSet is represented by two registers: KSZ9477_ACL_PORT_ACCESS_E and 215 + * KSZ9477_ACL_PORT_ACCESS_F. Each bit set in these registers corresponds to 216 + * an entry containing a Matching rule for this RuleSet. 217 + * 218 + * For a single Matching rule linked, only one bit is set. However, when an 219 + * entry links multiple Matching rules, forming what's termed a 'complex rule', 220 + * multiple bits are set in these registers. 221 + * 222 + * This function checks that, for complex rules, the entries containing the 223 + * linked Matching rules are contiguous in terms of their indices. It calculates 224 + * and returns the number of these contiguous entries. 225 + * 226 + * Returns: 227 + * - 0 if the entry is empty and can be safely overwritten 228 + * - 1 if the entry represents a simple rule 229 + * - The number of contiguous entries if it is the root entry of a complex 230 + * rule 231 + * - -ENOTEMPTY if the entry is part of a complex rule but not the root 232 + * entry 233 + * - -EINVAL if the validation fails 234 + */ 235 + static int ksz9477_acl_get_cont_entr(struct ksz_device *dev, int port, 236 + int index) 237 + { 238 + struct ksz9477_acl_priv *acl = dev->ports[port].acl_priv; 239 + struct ksz9477_acl_entries *acles = &acl->acles; 240 + int start_idx, end_idx, contiguous_count; 241 + unsigned long val; 242 + u8 vale, valf; 243 + u8 *entry; 244 + int i; 245 + 246 + entry = &acles->entries[index].entry[0]; 247 + vale = entry[KSZ9477_ACL_PORT_ACCESS_E]; 248 + valf = entry[KSZ9477_ACL_PORT_ACCESS_F]; 249 + 250 + val = (vale << 8) | valf; 251 + 252 + /* If no bits are set, return an appropriate value or error */ 253 + if (!val) { 254 + if (ksz9477_acl_is_valid_matching_rule(entry)) { 255 + /* Looks like we are about to corrupt some complex rule. 256 + * Do not print an error here, as this is a normal case 257 + * when we are trying to find a free or starting entry. 258 + */ 259 + dev_dbg(dev->dev, "ACL: entry %d starting with a valid matching rule, but no bits set in RuleSet\n", 260 + index); 261 + return -ENOTEMPTY; 262 + } 263 + 264 + /* This entry does not contain a valid matching rule */ 265 + return 0; 266 + } 267 + 268 + start_idx = find_first_bit((unsigned long *)&val, 16); 269 + end_idx = find_last_bit((unsigned long *)&val, 16); 270 + 271 + /* Calculate the contiguous count */ 272 + contiguous_count = end_idx - start_idx + 1; 273 + 274 + /* Check if the number of bits set in val matches our calculated count */ 275 + if (contiguous_count != hweight16(val)) { 276 + /* Probably we have a fragmented complex rule, which is not 277 + * supported by this driver. 278 + */ 279 + dev_err(dev->dev, "ACL: number of bits set in RuleSet does not match calculated count\n"); 280 + return -EINVAL; 281 + } 282 + 283 + /* loop over the contiguous entries and check for valid matching rules */ 284 + for (i = start_idx; i <= end_idx; i++) { 285 + u8 *current_entry = &acles->entries[i].entry[0]; 286 + 287 + if (!ksz9477_acl_is_valid_matching_rule(current_entry)) { 288 + /* we have something linked without a valid matching 289 + * rule. ACL table? 290 + */ 291 + dev_err(dev->dev, "ACL: entry %d does not contain a valid matching rule\n", 292 + i); 293 + return -EINVAL; 294 + } 295 + 296 + if (i > start_idx) { 297 + vale = current_entry[KSZ9477_ACL_PORT_ACCESS_E]; 298 + valf = current_entry[KSZ9477_ACL_PORT_ACCESS_F]; 299 + /* Following entry should have empty linkage list */ 300 + if (vale || valf) { 301 + dev_err(dev->dev, "ACL: entry %d has non-empty RuleSet linkage\n", 302 + i); 303 + return -EINVAL; 304 + } 305 + } 306 + } 307 + 308 + return contiguous_count; 309 + } 310 + 311 + /** 312 + * ksz9477_acl_update_linkage - Update the RuleSet linkage for an ACL entry 313 + * after a move operation. 314 + * 315 + * @dev: Pointer to the ksz_device. 316 + * @entry: Pointer to the ACL entry array. 317 + * @old_idx: The original index of the ACL entry before moving. 318 + * @new_idx: The new index of the ACL entry after moving. 319 + * 320 + * This function updates the RuleSet linkage bits for an ACL entry when 321 + * it's moved from one position to another in the ACL table. The RuleSet 322 + * linkage is represented by two 8-bit registers, which are combined 323 + * into a 16-bit value for easier manipulation. The linkage bits are shifted 324 + * based on the difference between the old and new index. If any bits are lost 325 + * during the shift operation, an error is returned. 326 + * 327 + * Note: Fragmentation within a RuleSet is not supported. Hence, entries must 328 + * be moved as complete blocks, maintaining the integrity of the RuleSet. 329 + * 330 + * Returns: 0 on success, or -EINVAL if any RuleSet linkage bits are lost 331 + * during the move. 332 + */ 333 + static int ksz9477_acl_update_linkage(struct ksz_device *dev, u8 *entry, 334 + u16 old_idx, u16 new_idx) 335 + { 336 + unsigned int original_bit_count; 337 + unsigned long rule_linkage; 338 + u8 vale, valf, val0; 339 + int shift; 340 + 341 + val0 = entry[KSZ9477_ACL_PORT_ACCESS_0]; 342 + vale = entry[KSZ9477_ACL_PORT_ACCESS_E]; 343 + valf = entry[KSZ9477_ACL_PORT_ACCESS_F]; 344 + 345 + /* Combine the two u8 values into one u16 for easier manipulation */ 346 + rule_linkage = (vale << 8) | valf; 347 + original_bit_count = hweight16(rule_linkage); 348 + 349 + /* Even if HW is able to handle fragmented RuleSet, we don't support it. 350 + * RuleSet is filled only for the first entry of the set. 351 + */ 352 + if (!rule_linkage) 353 + return 0; 354 + 355 + if (val0 != old_idx) { 356 + dev_err(dev->dev, "ACL: entry %d has unxpexted ActionRule linkage: %d\n", 357 + old_idx, val0); 358 + return -EINVAL; 359 + } 360 + 361 + val0 = new_idx; 362 + 363 + /* Calculate the number of positions to shift */ 364 + shift = new_idx - old_idx; 365 + 366 + /* Shift the RuleSet */ 367 + if (shift > 0) 368 + rule_linkage <<= shift; 369 + else 370 + rule_linkage >>= -shift; 371 + 372 + /* Check that no bits were lost in the process */ 373 + if (original_bit_count != hweight16(rule_linkage)) { 374 + dev_err(dev->dev, "ACL RuleSet linkage bits lost during move\n"); 375 + return -EINVAL; 376 + } 377 + 378 + entry[KSZ9477_ACL_PORT_ACCESS_0] = val0; 379 + 380 + /* Update the RuleSet bitfields in the entry */ 381 + entry[KSZ9477_ACL_PORT_ACCESS_E] = (rule_linkage >> 8) & 0xFF; 382 + entry[KSZ9477_ACL_PORT_ACCESS_F] = rule_linkage & 0xFF; 383 + 384 + return 0; 385 + } 386 + 387 + /** 388 + * ksz9477_validate_and_get_src_count - Validate source and destination indices 389 + * and determine the source entry count. 390 + * @dev: Pointer to the KSZ device structure. 391 + * @port: Port number on the KSZ device where the ACL entries reside. 392 + * @src_idx: Index of the starting ACL entry that needs to be validated. 393 + * @dst_idx: Index of the destination where the source entries are intended to 394 + * be moved. 395 + * @src_count: Pointer to the variable that will hold the number of contiguous 396 + * source entries if the validation passes. 397 + * @dst_count: Pointer to the variable that will hold the number of contiguous 398 + * destination entries if the validation passes. 399 + * 400 + * This function performs validation on the source and destination indices 401 + * provided for ACL entries. It checks if the indices are within the valid 402 + * range, and if the source entries are contiguous. Additionally, the function 403 + * ensures that there's adequate space at the destination for the source entries 404 + * and that the destination index isn't in the middle of a RuleSet. If all 405 + * validations pass, the function returns the number of contiguous source and 406 + * destination entries. 407 + * 408 + * Return: 0 on success, otherwise returns a negative error code if any 409 + * validation check fails. 410 + */ 411 + static int ksz9477_validate_and_get_src_count(struct ksz_device *dev, int port, 412 + int src_idx, int dst_idx, 413 + int *src_count, int *dst_count) 414 + { 415 + int ret; 416 + 417 + if (src_idx >= KSZ9477_ACL_MAX_ENTRIES || 418 + dst_idx >= KSZ9477_ACL_MAX_ENTRIES) { 419 + dev_err(dev->dev, "ACL: invalid entry index\n"); 420 + return -EINVAL; 421 + } 422 + 423 + /* Nothing to do */ 424 + if (src_idx == dst_idx) 425 + return 0; 426 + 427 + /* Validate if the source entries are contiguous */ 428 + ret = ksz9477_acl_get_cont_entr(dev, port, src_idx); 429 + if (ret < 0) 430 + return ret; 431 + *src_count = ret; 432 + 433 + if (!*src_count) { 434 + dev_err(dev->dev, "ACL: source entry is empty\n"); 435 + return -EINVAL; 436 + } 437 + 438 + if (dst_idx + *src_count >= KSZ9477_ACL_MAX_ENTRIES) { 439 + dev_err(dev->dev, "ACL: Not enough space at the destination. Move operation will fail.\n"); 440 + return -EINVAL; 441 + } 442 + 443 + /* Validate if the destination entry is empty or not in the middle of 444 + * a RuleSet. 445 + */ 446 + ret = ksz9477_acl_get_cont_entr(dev, port, dst_idx); 447 + if (ret < 0) 448 + return ret; 449 + *dst_count = ret; 450 + 451 + return 0; 452 + } 453 + 454 + /** 455 + * ksz9477_move_entries_downwards - Move a range of ACL entries downwards in 456 + * the list. 457 + * @dev: Pointer to the KSZ device structure. 458 + * @acles: Pointer to the structure encapsulating all the ACL entries. 459 + * @start_idx: Starting index of the entries to be relocated. 460 + * @num_entries_to_move: Number of consecutive entries to be relocated. 461 + * @end_idx: Destination index where the first entry should be situated post 462 + * relocation. 463 + * 464 + * This function is responsible for rearranging a specific block of ACL entries 465 + * by shifting them downwards in the list based on the supplied source and 466 + * destination indices. It ensures that the linkage between the ACL entries is 467 + * maintained accurately after the relocation. 468 + * 469 + * Return: 0 on successful relocation of entries, otherwise returns a negative 470 + * error code. 471 + */ 472 + static int ksz9477_move_entries_downwards(struct ksz_device *dev, 473 + struct ksz9477_acl_entries *acles, 474 + u16 start_idx, 475 + u16 num_entries_to_move, 476 + u16 end_idx) 477 + { 478 + struct ksz9477_acl_entry *e; 479 + int ret, i; 480 + 481 + for (i = start_idx; i < end_idx; i++) { 482 + e = &acles->entries[i]; 483 + *e = acles->entries[i + num_entries_to_move]; 484 + 485 + ret = ksz9477_acl_update_linkage(dev, &e->entry[0], 486 + i + num_entries_to_move, i); 487 + if (ret < 0) 488 + return ret; 489 + } 490 + 491 + return 0; 492 + } 493 + 494 + /** 495 + * ksz9477_move_entries_upwards - Move a range of ACL entries upwards in the 496 + * list. 497 + * @dev: Pointer to the KSZ device structure. 498 + * @acles: Pointer to the structure holding all the ACL entries. 499 + * @start_idx: The starting index of the entries to be moved. 500 + * @num_entries_to_move: Number of contiguous entries to be moved. 501 + * @target_idx: The destination index where the first entry should be placed 502 + * after moving. 503 + * 504 + * This function rearranges a chunk of ACL entries by moving them upwards 505 + * in the list based on the given source and destination indices. The reordering 506 + * process preserves the linkage between entries by updating it accordingly. 507 + * 508 + * Return: 0 if the entries were successfully moved, otherwise a negative error 509 + * code. 510 + */ 511 + static int ksz9477_move_entries_upwards(struct ksz_device *dev, 512 + struct ksz9477_acl_entries *acles, 513 + u16 start_idx, u16 num_entries_to_move, 514 + u16 target_idx) 515 + { 516 + struct ksz9477_acl_entry *e; 517 + int ret, i, b; 518 + 519 + for (i = start_idx; i > target_idx; i--) { 520 + b = i + num_entries_to_move - 1; 521 + 522 + e = &acles->entries[b]; 523 + *e = acles->entries[i - 1]; 524 + 525 + ret = ksz9477_acl_update_linkage(dev, &e->entry[0], i - 1, b); 526 + if (ret < 0) 527 + return ret; 528 + } 529 + 530 + return 0; 531 + } 532 + 533 + /** 534 + * ksz9477_acl_move_entries - Move a block of contiguous ACL entries from a 535 + * source to a destination index. 536 + * @dev: Pointer to the KSZ9477 device structure. 537 + * @port: Port number. 538 + * @src_idx: Index of the starting source ACL entry. 539 + * @dst_idx: Index of the starting destination ACL entry. 540 + * 541 + * This function aims to move a block of contiguous ACL entries from the source 542 + * index to the destination index while ensuring the integrity and validity of 543 + * the ACL table. 544 + * 545 + * In case of any errors during the adjustments or copying, the function will 546 + * restore the ACL entries to their original state from the backup. 547 + * 548 + * Return: 0 if the move operation is successful. Returns -EINVAL for validation 549 + * errors or other error codes based on specific failure conditions. 550 + */ 551 + static int ksz9477_acl_move_entries(struct ksz_device *dev, int port, 552 + u16 src_idx, u16 dst_idx) 553 + { 554 + struct ksz9477_acl_entry buffer[KSZ9477_ACL_MAX_ENTRIES]; 555 + struct ksz9477_acl_priv *acl = dev->ports[port].acl_priv; 556 + struct ksz9477_acl_entries *acles = &acl->acles; 557 + int src_count, ret, dst_count; 558 + 559 + ret = ksz9477_validate_and_get_src_count(dev, port, src_idx, dst_idx, 560 + &src_count, &dst_count); 561 + if (ret) 562 + return ret; 563 + 564 + /* In case dst_index is greater than src_index, we need to adjust the 565 + * destination index to account for the entries that will be moved 566 + * downwards and the size of the entry located at dst_idx. 567 + */ 568 + if (dst_idx > src_idx) 569 + dst_idx = dst_idx + dst_count - src_count; 570 + 571 + /* Copy source block to buffer and update its linkage */ 572 + for (int i = 0; i < src_count; i++) { 573 + buffer[i] = acles->entries[src_idx + i]; 574 + ret = ksz9477_acl_update_linkage(dev, &buffer[i].entry[0], 575 + src_idx + i, dst_idx + i); 576 + if (ret < 0) 577 + return ret; 578 + } 579 + 580 + /* Adjust other entries and their linkage based on destination */ 581 + if (dst_idx > src_idx) { 582 + ret = ksz9477_move_entries_downwards(dev, acles, src_idx, 583 + src_count, dst_idx); 584 + } else { 585 + ret = ksz9477_move_entries_upwards(dev, acles, src_idx, 586 + src_count, dst_idx); 587 + } 588 + if (ret < 0) 589 + return ret; 590 + 591 + /* Copy buffer to destination block */ 592 + for (int i = 0; i < src_count; i++) 593 + acles->entries[dst_idx + i] = buffer[i]; 594 + 595 + return 0; 596 + } 597 + 598 + /** 599 + * ksz9477_get_next_block_start - Identify the starting index of the next ACL 600 + * block. 601 + * @dev: Pointer to the device structure. 602 + * @port: The port number on which the ACL entries are being checked. 603 + * @start: The starting index from which the search begins. 604 + * 605 + * This function looks for the next valid ACL block starting from the provided 606 + * 'start' index and returns the beginning index of that block. If the block is 607 + * invalid or if it reaches the end of the ACL entries without finding another 608 + * block, it returns the maximum ACL entries count. 609 + * 610 + * Returns: 611 + * - The starting index of the next valid ACL block. 612 + * - KSZ9477_ACL_MAX_ENTRIES if no other valid blocks are found after 'start'. 613 + * - A negative error code if an error occurs while checking. 614 + */ 615 + static int ksz9477_get_next_block_start(struct ksz_device *dev, int port, 616 + int start) 617 + { 618 + int block_size; 619 + 620 + for (int i = start; i < KSZ9477_ACL_MAX_ENTRIES;) { 621 + block_size = ksz9477_acl_get_cont_entr(dev, port, i); 622 + if (block_size < 0 && block_size != -ENOTEMPTY) 623 + return block_size; 624 + 625 + if (block_size > 0) 626 + return i; 627 + 628 + i++; 629 + } 630 + return KSZ9477_ACL_MAX_ENTRIES; 631 + } 632 + 633 + /** 634 + * ksz9477_swap_acl_blocks - Swap two ACL blocks 635 + * @dev: Pointer to the device structure. 636 + * @port: The port number on which the ACL blocks are to be swapped. 637 + * @i: The starting index of the first ACL block. 638 + * @j: The starting index of the second ACL block. 639 + * 640 + * This function is used to swap two ACL blocks present at given indices. The 641 + * main purpose is to aid in the sorting and reordering of ACL blocks based on 642 + * certain criteria, e.g., priority. It checks the validity of the block at 643 + * index 'i', ensuring it's not an empty block, and then proceeds to swap it 644 + * with the block at index 'j'. 645 + * 646 + * Returns: 647 + * - 0 on successful swapping of blocks. 648 + * - -EINVAL if the block at index 'i' is empty. 649 + * - A negative error code if any other error occurs during the swap. 650 + */ 651 + static int ksz9477_swap_acl_blocks(struct ksz_device *dev, int port, int i, 652 + int j) 653 + { 654 + int ret, current_block_size; 655 + 656 + current_block_size = ksz9477_acl_get_cont_entr(dev, port, i); 657 + if (current_block_size < 0) 658 + return current_block_size; 659 + 660 + if (!current_block_size) { 661 + dev_err(dev->dev, "ACL: swapping empty entry %d\n", i); 662 + return -EINVAL; 663 + } 664 + 665 + ret = ksz9477_acl_move_entries(dev, port, i, j); 666 + if (ret) 667 + return ret; 668 + 669 + ret = ksz9477_acl_move_entries(dev, port, j - current_block_size, i); 670 + if (ret) 671 + return ret; 672 + 673 + return 0; 674 + } 675 + 676 + /** 677 + * ksz9477_sort_acl_entr_no_back - Sort ACL entries for a given port based on 678 + * priority without backing up entries. 679 + * @dev: Pointer to the device structure. 680 + * @port: The port number whose ACL entries need to be sorted. 681 + * 682 + * This function sorts ACL entries of the specified port using a variant of the 683 + * bubble sort algorithm. It operates on blocks of ACL entries rather than 684 + * individual entries. Each block's starting point is identified and then 685 + * compared with subsequent blocks based on their priority. If the current 686 + * block has a lower priority than the subsequent block, the two blocks are 687 + * swapped. 688 + * 689 + * This is done in order to maintain an organized order of ACL entries based on 690 + * priority, ensuring efficient and predictable ACL rule application. 691 + * 692 + * Returns: 693 + * - 0 on successful sorting of entries. 694 + * - A negative error code if any issue arises during sorting, e.g., 695 + * if the function is unable to get the next block start. 696 + */ 697 + static int ksz9477_sort_acl_entr_no_back(struct ksz_device *dev, int port) 698 + { 699 + struct ksz9477_acl_priv *acl = dev->ports[port].acl_priv; 700 + struct ksz9477_acl_entries *acles = &acl->acles; 701 + struct ksz9477_acl_entry *curr, *next; 702 + int i, j, ret; 703 + 704 + /* Bubble sort */ 705 + for (i = 0; i < KSZ9477_ACL_MAX_ENTRIES;) { 706 + curr = &acles->entries[i]; 707 + 708 + j = ksz9477_get_next_block_start(dev, port, i + 1); 709 + if (j < 0) 710 + return j; 711 + 712 + while (j < KSZ9477_ACL_MAX_ENTRIES) { 713 + next = &acles->entries[j]; 714 + 715 + if (curr->prio > next->prio) { 716 + ret = ksz9477_swap_acl_blocks(dev, port, i, j); 717 + if (ret) 718 + return ret; 719 + } 720 + 721 + j = ksz9477_get_next_block_start(dev, port, j + 1); 722 + if (j < 0) 723 + return j; 724 + } 725 + 726 + i = ksz9477_get_next_block_start(dev, port, i + 1); 727 + if (i < 0) 728 + return i; 729 + } 730 + 731 + return 0; 732 + } 733 + 734 + /** 735 + * ksz9477_sort_acl_entries - Sort the ACL entries for a given port. 736 + * @dev: Pointer to the KSZ device. 737 + * @port: Port number. 738 + * 739 + * This function sorts the Access Control List (ACL) entries for a specified 740 + * port. Before sorting, a backup of the original entries is created. If the 741 + * sorting process fails, the function will log error messages displaying both 742 + * the original and attempted sorted entries, and then restore the original 743 + * entries from the backup. 744 + * 745 + * Return: 0 if the sorting succeeds, otherwise a negative error code. 746 + */ 747 + int ksz9477_sort_acl_entries(struct ksz_device *dev, int port) 748 + { 749 + struct ksz9477_acl_entry backup[KSZ9477_ACL_MAX_ENTRIES]; 750 + struct ksz9477_acl_priv *acl = dev->ports[port].acl_priv; 751 + struct ksz9477_acl_entries *acles = &acl->acles; 752 + int ret; 753 + 754 + /* create a backup of the ACL entries, if something goes wrong 755 + * we can restore the ACL entries. 756 + */ 757 + memcpy(backup, acles->entries, sizeof(backup)); 758 + 759 + ret = ksz9477_sort_acl_entr_no_back(dev, port); 760 + if (ret) { 761 + dev_err(dev->dev, "ACL: failed to sort entries for port %d\n", 762 + port); 763 + dev_err(dev->dev, "ACL dump before sorting:\n"); 764 + ksz9477_dump_acl(dev, backup); 765 + dev_err(dev->dev, "ACL dump after sorting:\n"); 766 + ksz9477_dump_acl(dev, acles->entries); 767 + /* Restore the original entries */ 768 + memcpy(acles->entries, backup, sizeof(backup)); 769 + } 770 + 771 + return ret; 772 + } 773 + 774 + /** 775 + * ksz9477_acl_wait_ready - Waits for the ACL operation to complete on a given 776 + * port. 777 + * @dev: The ksz_device instance. 778 + * @port: The port number to wait for. 779 + * 780 + * This function checks if the ACL write or read operation is completed by 781 + * polling the specified register. 782 + * 783 + * Returns: 0 if the operation is successful, or a negative error code if an 784 + * error occurs. 785 + */ 786 + static int ksz9477_acl_wait_ready(struct ksz_device *dev, int port) 787 + { 788 + unsigned int wr_mask = KSZ9477_ACL_WRITE_DONE | KSZ9477_ACL_READ_DONE; 789 + unsigned int val, reg; 790 + int ret; 791 + 792 + reg = dev->dev_ops->get_port_addr(port, KSZ9477_PORT_ACL_CTRL_0); 793 + 794 + ret = regmap_read_poll_timeout(dev->regmap[0], reg, val, 795 + (val & wr_mask) == wr_mask, 1000, 10000); 796 + if (ret) 797 + dev_err(dev->dev, "Failed to read/write ACL table\n"); 798 + 799 + return ret; 800 + } 801 + 802 + /** 803 + * ksz9477_acl_entry_write - Writes an ACL entry to a given port at the 804 + * specified index. 805 + * @dev: The ksz_device instance. 806 + * @port: The port number to write the ACL entry to. 807 + * @entry: A pointer to the ACL entry data. 808 + * @idx: The index at which to write the ACL entry. 809 + * 810 + * This function writes the provided ACL entry to the specified port at the 811 + * given index. 812 + * 813 + * Returns: 0 if the operation is successful, or a negative error code if an 814 + * error occurs. 815 + */ 816 + static int ksz9477_acl_entry_write(struct ksz_device *dev, int port, u8 *entry, 817 + int idx) 818 + { 819 + int ret, i; 820 + u8 val; 821 + 822 + for (i = 0; i < KSZ9477_ACL_ENTRY_SIZE; i++) { 823 + ret = ksz_pwrite8(dev, port, KSZ9477_PORT_ACL_0 + i, entry[i]); 824 + if (ret) { 825 + dev_err(dev->dev, "Failed to write ACL entry %d\n", i); 826 + return ret; 827 + } 828 + } 829 + 830 + /* write everything down */ 831 + val = FIELD_PREP(KSZ9477_ACL_INDEX_M, idx) | KSZ9477_ACL_WRITE; 832 + ret = ksz_pwrite8(dev, port, KSZ9477_PORT_ACL_CTRL_0, val); 833 + if (ret) 834 + return ret; 835 + 836 + /* wait until everything is written */ 837 + return ksz9477_acl_wait_ready(dev, port); 838 + } 839 + 840 + /** 841 + * ksz9477_acl_port_enable - Enables ACL functionality on a given port. 842 + * @dev: The ksz_device instance. 843 + * @port: The port number on which to enable ACL functionality. 844 + * 845 + * This function enables ACL functionality on the specified port by configuring 846 + * the appropriate control registers. It returns 0 if the operation is 847 + * successful, or a negative error code if an error occurs. 848 + * 849 + * 0xn801 - KSZ9477S 5.2.8.2 Port Priority Control Register 850 + * Bit 7 - Highest Priority 851 + * Bit 6 - OR'ed Priority 852 + * Bit 4 - MAC Address Priority Classification 853 + * Bit 3 - VLAN Priority Classification 854 + * Bit 2 - 802.1p Priority Classification 855 + * Bit 1 - Diffserv Priority Classification 856 + * Bit 0 - ACL Priority Classification 857 + * 858 + * Current driver implementation sets 802.1p priority classification by default. 859 + * In this function we add ACL priority classification with OR'ed priority. 860 + * According to testing, priority set by ACL will supersede the 802.1p priority. 861 + * 862 + * 0xn803 - KSZ9477S 5.2.8.4 Port Authentication Control Register 863 + * Bit 2 - Access Control List (ACL) Enable 864 + * Bits 1:0 - Authentication Mode 865 + * 00 = Reserved 866 + * 01 = Block Mode. Authentication is enabled. When ACL is 867 + * enabled, all traffic that misses the ACL rules is 868 + * blocked; otherwise ACL actions apply. 869 + * 10 = Pass Mode. Authentication is disabled. When ACL is 870 + * enabled, all traffic that misses the ACL rules is 871 + * forwarded; otherwise ACL actions apply. 872 + * 11 = Trap Mode. Authentication is enabled. All traffic is 873 + * forwarded to the host port. When ACL is enabled, all 874 + * traffic that misses the ACL rules is blocked; otherwise 875 + * ACL actions apply. 876 + * 877 + * We are using Pass Mode int this function. 878 + * 879 + * Returns: 0 if the operation is successful, or a negative error code if an 880 + * error occurs. 881 + */ 882 + static int ksz9477_acl_port_enable(struct ksz_device *dev, int port) 883 + { 884 + int ret; 885 + 886 + ret = ksz_prmw8(dev, port, P_PRIO_CTRL, 0, PORT_ACL_PRIO_ENABLE | 887 + PORT_OR_PRIO); 888 + if (ret) 889 + return ret; 890 + 891 + return ksz_pwrite8(dev, port, REG_PORT_MRI_AUTHEN_CTRL, 892 + PORT_ACL_ENABLE | 893 + FIELD_PREP(PORT_AUTHEN_MODE, PORT_AUTHEN_PASS)); 894 + } 895 + 896 + /** 897 + * ksz9477_acl_port_disable - Disables ACL functionality on a given port. 898 + * @dev: The ksz_device instance. 899 + * @port: The port number on which to disable ACL functionality. 900 + * 901 + * This function disables ACL functionality on the specified port by writing a 902 + * value of 0 to the REG_PORT_MRI_AUTHEN_CTRL control register and remove 903 + * PORT_ACL_PRIO_ENABLE bit from P_PRIO_CTRL register. 904 + * 905 + * Returns: 0 if the operation is successful, or a negative error code if an 906 + * error occurs. 907 + */ 908 + static int ksz9477_acl_port_disable(struct ksz_device *dev, int port) 909 + { 910 + int ret; 911 + 912 + ret = ksz_prmw8(dev, port, P_PRIO_CTRL, PORT_ACL_PRIO_ENABLE, 0); 913 + if (ret) 914 + return ret; 915 + 916 + return ksz_pwrite8(dev, port, REG_PORT_MRI_AUTHEN_CTRL, 0); 917 + } 918 + 919 + /** 920 + * ksz9477_acl_write_list - Write a list of ACL entries to a given port. 921 + * @dev: The ksz_device instance. 922 + * @port: The port number on which to write ACL entries. 923 + * 924 + * This function enables ACL functionality on the specified port, writes a list 925 + * of ACL entries to the port, and disables ACL functionality if there are no 926 + * entries. 927 + * 928 + * Returns: 0 if the operation is successful, or a negative error code if an 929 + * error occurs. 930 + */ 931 + int ksz9477_acl_write_list(struct ksz_device *dev, int port) 932 + { 933 + struct ksz9477_acl_priv *acl = dev->ports[port].acl_priv; 934 + struct ksz9477_acl_entries *acles = &acl->acles; 935 + int ret, i; 936 + 937 + /* ACL should be enabled before writing entries */ 938 + ret = ksz9477_acl_port_enable(dev, port); 939 + if (ret) 940 + return ret; 941 + 942 + /* write all entries */ 943 + for (i = 0; i < ARRAY_SIZE(acles->entries); i++) { 944 + u8 *entry = acles->entries[i].entry; 945 + 946 + /* Check if entry was removed and should be zeroed. 947 + * If last fields of the entry are not zero, it means 948 + * it is removed locally but currently not synced with the HW. 949 + * So, we will write it down to the HW to remove it. 950 + */ 951 + if (i >= acles->entries_count && 952 + entry[KSZ9477_ACL_PORT_ACCESS_10] == 0 && 953 + entry[KSZ9477_ACL_PORT_ACCESS_11] == 0) 954 + continue; 955 + 956 + ret = ksz9477_acl_entry_write(dev, port, entry, i); 957 + if (ret) 958 + return ret; 959 + 960 + /* now removed entry is clean on HW side, so it can 961 + * in the cache too 962 + */ 963 + if (i >= acles->entries_count && 964 + entry[KSZ9477_ACL_PORT_ACCESS_10] != 0 && 965 + entry[KSZ9477_ACL_PORT_ACCESS_11] != 0) { 966 + entry[KSZ9477_ACL_PORT_ACCESS_10] = 0; 967 + entry[KSZ9477_ACL_PORT_ACCESS_11] = 0; 968 + } 969 + } 970 + 971 + if (!acles->entries_count) 972 + return ksz9477_acl_port_disable(dev, port); 973 + 974 + return 0; 975 + } 976 + 977 + /** 978 + * ksz9477_acl_remove_entries - Remove ACL entries with a given cookie from a 979 + * specified ksz9477_acl_entries structure. 980 + * @dev: The ksz_device instance. 981 + * @port: The port number on which to remove ACL entries. 982 + * @acles: The ksz9477_acl_entries instance. 983 + * @cookie: The cookie value to match for entry removal. 984 + * 985 + * This function iterates through the entries array, removing any entries with 986 + * a matching cookie value. The remaining entries are then shifted down to fill 987 + * the gap. 988 + */ 989 + void ksz9477_acl_remove_entries(struct ksz_device *dev, int port, 990 + struct ksz9477_acl_entries *acles, 991 + unsigned long cookie) 992 + { 993 + int entries_count = acles->entries_count; 994 + int ret, i, src_count; 995 + int src_idx = -1; 996 + 997 + if (!entries_count) 998 + return; 999 + 1000 + /* Search for the first position with the cookie */ 1001 + for (i = 0; i < entries_count; i++) { 1002 + if (acles->entries[i].cookie == cookie) { 1003 + src_idx = i; 1004 + break; 1005 + } 1006 + } 1007 + 1008 + /* No entries with the matching cookie found */ 1009 + if (src_idx == -1) 1010 + return; 1011 + 1012 + /* Get the size of the cookie entry. We may have complex entries. */ 1013 + src_count = ksz9477_acl_get_cont_entr(dev, port, src_idx); 1014 + if (src_count <= 0) 1015 + return; 1016 + 1017 + /* Move all entries down to overwrite removed entry with the cookie */ 1018 + ret = ksz9477_move_entries_downwards(dev, acles, src_idx, 1019 + src_count, 1020 + entries_count - src_count); 1021 + if (ret) { 1022 + dev_err(dev->dev, "Failed to move ACL entries down\n"); 1023 + return; 1024 + } 1025 + 1026 + /* Overwrite new empty places at the end of the list with zeros to make 1027 + * sure not unexpected things will happen or no unexplored quirks will 1028 + * come out. 1029 + */ 1030 + for (i = entries_count - src_count; i < entries_count; i++) { 1031 + struct ksz9477_acl_entry *entry = &acles->entries[i]; 1032 + 1033 + memset(entry, 0, sizeof(*entry)); 1034 + 1035 + /* Set all access bits to be able to write zeroed entry to HW */ 1036 + entry->entry[KSZ9477_ACL_PORT_ACCESS_10] = 0xff; 1037 + entry->entry[KSZ9477_ACL_PORT_ACCESS_11] = 0xff; 1038 + } 1039 + 1040 + /* Adjust the total entries count */ 1041 + acles->entries_count -= src_count; 1042 + } 1043 + 1044 + /** 1045 + * ksz9477_port_acl_init - Initialize the ACL for a specified port on a ksz 1046 + * device. 1047 + * @dev: The ksz_device instance. 1048 + * @port: The port number to initialize the ACL for. 1049 + * 1050 + * This function allocates memory for an acl structure, associates it with the 1051 + * specified port, and initializes the ACL entries to a default state. The 1052 + * entries are then written using the ksz9477_acl_write_list function, ensuring 1053 + * the ACL has a predictable initial hardware state. 1054 + * 1055 + * Returns: 0 on success, or an error code on failure. 1056 + */ 1057 + int ksz9477_port_acl_init(struct ksz_device *dev, int port) 1058 + { 1059 + struct ksz9477_acl_entries *acles; 1060 + struct ksz9477_acl_priv *acl; 1061 + int ret, i; 1062 + 1063 + acl = kzalloc(sizeof(*acl), GFP_KERNEL); 1064 + if (!acl) 1065 + return -ENOMEM; 1066 + 1067 + dev->ports[port].acl_priv = acl; 1068 + 1069 + acles = &acl->acles; 1070 + /* write all entries */ 1071 + for (i = 0; i < ARRAY_SIZE(acles->entries); i++) { 1072 + u8 *entry = acles->entries[i].entry; 1073 + 1074 + /* Set all access bits to be able to write zeroed 1075 + * entry 1076 + */ 1077 + entry[KSZ9477_ACL_PORT_ACCESS_10] = 0xff; 1078 + entry[KSZ9477_ACL_PORT_ACCESS_11] = 0xff; 1079 + } 1080 + 1081 + ret = ksz9477_acl_write_list(dev, port); 1082 + if (ret) 1083 + goto free_acl; 1084 + 1085 + return 0; 1086 + 1087 + free_acl: 1088 + kfree(dev->ports[port].acl_priv); 1089 + dev->ports[port].acl_priv = NULL; 1090 + 1091 + return ret; 1092 + } 1093 + 1094 + /** 1095 + * ksz9477_port_acl_free - Free the ACL resources for a specified port on a ksz 1096 + * device. 1097 + * @dev: The ksz_device instance. 1098 + * @port: The port number to initialize the ACL for. 1099 + * 1100 + * This disables the ACL for the specified port and frees the associated memory, 1101 + */ 1102 + void ksz9477_port_acl_free(struct ksz_device *dev, int port) 1103 + { 1104 + if (!dev->ports[port].acl_priv) 1105 + return; 1106 + 1107 + ksz9477_acl_port_disable(dev, port); 1108 + 1109 + kfree(dev->ports[port].acl_priv); 1110 + dev->ports[port].acl_priv = NULL; 1111 + } 1112 + 1113 + /** 1114 + * ksz9477_acl_set_reg - Set entry[16] and entry[17] depending on the updated 1115 + * entry[] 1116 + * @entry: An array containing the entries 1117 + * @reg: The register of the entry that needs to be updated 1118 + * @value: The value to be assigned to the updated entry 1119 + * 1120 + * This function updates the entry[] array based on the provided register and 1121 + * value. It also sets entry[0x10] and entry[0x11] according to the ACL byte 1122 + * enable rules. 1123 + * 1124 + * 0x10 - Byte Enable [15:8] 1125 + * 1126 + * Each bit enables accessing one of the ACL bytes when a read or write is 1127 + * initiated by writing to the Port ACL Byte Enable LSB Register. 1128 + * Bit 0 applies to the Port ACL Access 7 Register 1129 + * Bit 1 applies to the Port ACL Access 6 Register, etc. 1130 + * Bit 7 applies to the Port ACL Access 0 Register 1131 + * 1 = Byte is selected for read/write 1132 + * 0 = Byte is not selected 1133 + * 1134 + * 0x11 - Byte Enable [7:0] 1135 + * 1136 + * Each bit enables accessing one of the ACL bytes when a read or write is 1137 + * initiated by writing to the Port ACL Byte Enable LSB Register. 1138 + * Bit 0 applies to the Port ACL Access F Register 1139 + * Bit 1 applies to the Port ACL Access E Register, etc. 1140 + * Bit 7 applies to the Port ACL Access 8 Register 1141 + * 1 = Byte is selected for read/write 1142 + * 0 = Byte is not selected 1143 + */ 1144 + static void ksz9477_acl_set_reg(u8 *entry, enum ksz9477_acl_port_access reg, 1145 + u8 value) 1146 + { 1147 + if (reg >= KSZ9477_ACL_PORT_ACCESS_0 && 1148 + reg <= KSZ9477_ACL_PORT_ACCESS_7) { 1149 + entry[KSZ9477_ACL_PORT_ACCESS_10] |= 1150 + BIT(KSZ9477_ACL_PORT_ACCESS_7 - reg); 1151 + } else if (reg >= KSZ9477_ACL_PORT_ACCESS_8 && 1152 + reg <= KSZ9477_ACL_PORT_ACCESS_F) { 1153 + entry[KSZ9477_ACL_PORT_ACCESS_11] |= 1154 + BIT(KSZ9477_ACL_PORT_ACCESS_F - reg); 1155 + } else { 1156 + WARN_ON(1); 1157 + return; 1158 + } 1159 + 1160 + entry[reg] = value; 1161 + } 1162 + 1163 + /** 1164 + * ksz9477_acl_matching_rule_cfg_l2 - Configure an ACL filtering entry to match 1165 + * L2 types of Ethernet frames 1166 + * @entry: Pointer to ACL entry buffer 1167 + * @ethertype: Ethertype value 1168 + * @eth_addr: Pointer to Ethernet address 1169 + * @is_src: If true, match the source MAC address; if false, match the 1170 + * destination MAC address 1171 + * 1172 + * This function configures an Access Control List (ACL) filtering 1173 + * entry to match Layer 2 types of Ethernet frames based on the provided 1174 + * ethertype and Ethernet address. Additionally, it can match either the source 1175 + * or destination MAC address depending on the value of the is_src parameter. 1176 + * 1177 + * Register Descriptions for MD = 01 and ENB != 00 (Layer 2 MAC header 1178 + * filtering) 1179 + * 1180 + * 0x01 - Mode and Enable 1181 + * Bits 5:4 - MD (Mode) 1182 + * 01 = Layer 2 MAC header or counter filtering 1183 + * Bits 3:2 - ENB (Enable) 1184 + * 01 = Comparison is performed only on the TYPE value 1185 + * 10 = Comparison is performed only on the MAC Address value 1186 + * 11 = Both the MAC Address and TYPE are tested 1187 + * Bit 1 - S/D (Source / Destination) 1188 + * 0 = Destination address 1189 + * 1 = Source address 1190 + * Bit 0 - EQ (Equal / Not Equal) 1191 + * 0 = Not Equal produces true result 1192 + * 1 = Equal produces true result 1193 + * 1194 + * 0x02-0x07 - MAC Address 1195 + * 0x02 - MAC Address [47:40] 1196 + * 0x03 - MAC Address [39:32] 1197 + * 0x04 - MAC Address [31:24] 1198 + * 0x05 - MAC Address [23:16] 1199 + * 0x06 - MAC Address [15:8] 1200 + * 0x07 - MAC Address [7:0] 1201 + * 1202 + * 0x08-0x09 - EtherType 1203 + * 0x08 - EtherType [15:8] 1204 + * 0x09 - EtherType [7:0] 1205 + */ 1206 + static void ksz9477_acl_matching_rule_cfg_l2(u8 *entry, u16 ethertype, 1207 + u8 *eth_addr, bool is_src) 1208 + { 1209 + u8 enb = 0; 1210 + u8 val; 1211 + 1212 + if (ethertype) 1213 + enb |= KSZ9477_ACL_ENB_L2_TYPE; 1214 + if (eth_addr) 1215 + enb |= KSZ9477_ACL_ENB_L2_MAC; 1216 + 1217 + val = FIELD_PREP(KSZ9477_ACL_MD_MASK, KSZ9477_ACL_MD_L2_MAC) | 1218 + FIELD_PREP(KSZ9477_ACL_ENB_MASK, enb) | 1219 + FIELD_PREP(KSZ9477_ACL_SD_SRC, is_src) | KSZ9477_ACL_EQ_EQUAL; 1220 + ksz9477_acl_set_reg(entry, KSZ9477_ACL_PORT_ACCESS_1, val); 1221 + 1222 + if (eth_addr) { 1223 + int i; 1224 + 1225 + for (i = 0; i < ETH_ALEN; i++) { 1226 + ksz9477_acl_set_reg(entry, 1227 + KSZ9477_ACL_PORT_ACCESS_2 + i, 1228 + eth_addr[i]); 1229 + } 1230 + } 1231 + 1232 + ksz9477_acl_set_reg(entry, KSZ9477_ACL_PORT_ACCESS_8, ethertype >> 8); 1233 + ksz9477_acl_set_reg(entry, KSZ9477_ACL_PORT_ACCESS_9, ethertype & 0xff); 1234 + } 1235 + 1236 + /** 1237 + * ksz9477_acl_action_rule_cfg - Set action for an ACL entry 1238 + * @entry: Pointer to the ACL entry 1239 + * @force_prio: If true, force the priority value 1240 + * @prio_val: Priority value 1241 + * 1242 + * This function sets the action for the specified ACL entry. It prepares 1243 + * the priority mode and traffic class values and updates the entry's 1244 + * action registers accordingly. Currently, there is no port or VLAN PCP 1245 + * remapping. 1246 + * 1247 + * ACL Action Rule Parameters for Non-Count Modes (MD ≠ 01 or ENB ≠ 00) 1248 + * 1249 + * 0x0A - PM, P, RPE, RP[2:1] 1250 + * Bits 7:6 - PM[1:0] - Priority Mode 1251 + * 00 = ACL does not specify the packet priority. Priority is 1252 + * determined by standard QoS functions. 1253 + * 01 = Change packet priority to P[2:0] if it is greater than QoS 1254 + * result. 1255 + * 10 = Change packet priority to P[2:0] if it is smaller than the 1256 + * QoS result. 1257 + * 11 = Always change packet priority to P[2:0]. 1258 + * Bits 5:3 - P[2:0] - Priority value 1259 + * Bit 2 - RPE - Remark Priority Enable 1260 + * Bits 1:0 - RP[2:1] - Remarked Priority value (bits 2:1) 1261 + * 0 = Disable priority remarking 1262 + * 1 = Enable priority remarking. VLAN tag priority (PCP) bits are 1263 + * replaced by RP[2:0]. 1264 + * 1265 + * 0x0B - RP[0], MM 1266 + * Bit 7 - RP[0] - Remarked Priority value (bit 0) 1267 + * Bits 6:5 - MM[1:0] - Map Mode 1268 + * 00 = No forwarding remapping 1269 + * 01 = The forwarding map in FORWARD is OR'ed with the forwarding 1270 + * map from the Address Lookup Table. 1271 + * 10 = The forwarding map in FORWARD is AND'ed with the forwarding 1272 + * map from the Address Lookup Table. 1273 + * 11 = The forwarding map in FORWARD replaces the forwarding map 1274 + * from the Address Lookup Table. 1275 + * 0x0D - FORWARD[n:0] 1276 + * Bits 7:0 - FORWARD[n:0] - Forwarding map. Bit 0 = port 1, 1277 + * bit 1 = port 2, etc. 1278 + * 1 = enable forwarding to this port 1279 + * 0 = do not forward to this port 1280 + */ 1281 + void ksz9477_acl_action_rule_cfg(u8 *entry, bool force_prio, u8 prio_val) 1282 + { 1283 + u8 prio_mode, val; 1284 + 1285 + if (force_prio) 1286 + prio_mode = KSZ9477_ACL_PM_REPLACE; 1287 + else 1288 + prio_mode = KSZ9477_ACL_PM_DISABLE; 1289 + 1290 + val = FIELD_PREP(KSZ9477_ACL_PM_M, prio_mode) | 1291 + FIELD_PREP(KSZ9477_ACL_P_M, prio_val); 1292 + ksz9477_acl_set_reg(entry, KSZ9477_ACL_PORT_ACCESS_A, val); 1293 + 1294 + /* no port or VLAN PCP remapping for now */ 1295 + ksz9477_acl_set_reg(entry, KSZ9477_ACL_PORT_ACCESS_B, 0); 1296 + ksz9477_acl_set_reg(entry, KSZ9477_ACL_PORT_ACCESS_D, 0); 1297 + } 1298 + 1299 + /** 1300 + * ksz9477_acl_processing_rule_set_action - Set the action for the processing 1301 + * rule set. 1302 + * @entry: Pointer to the ACL entry 1303 + * @action_idx: Index of the action to be applied 1304 + * 1305 + * This function sets the action for the processing rule set by updating the 1306 + * appropriate register in the entry. There can be only one action per 1307 + * processing rule. 1308 + * 1309 + * Access Control List (ACL) Processing Rule Registers: 1310 + * 1311 + * 0x00 - First Rule Number (FRN) 1312 + * Bits 3:0 - First Rule Number. Pointer to an Action rule entry. 1313 + */ 1314 + void ksz9477_acl_processing_rule_set_action(u8 *entry, u8 action_idx) 1315 + { 1316 + ksz9477_acl_set_reg(entry, KSZ9477_ACL_PORT_ACCESS_0, action_idx); 1317 + } 1318 + 1319 + /** 1320 + * ksz9477_acl_processing_rule_add_match - Add a matching rule to the rule set 1321 + * @entry: Pointer to the ACL entry 1322 + * @match_idx: Index of the matching rule to be added 1323 + * 1324 + * This function adds a matching rule to the rule set by updating the 1325 + * appropriate bits in the entry's rule set registers. 1326 + * 1327 + * Access Control List (ACL) Processing Rule Registers: 1328 + * 1329 + * 0x0E - RuleSet [15:8] 1330 + * Bits 7:0 - RuleSet [15:8] Specifies a set of one or more Matching rule 1331 + * entries. RuleSet has one bit for each of the 16 Matching rule entries. 1332 + * If multiple Matching rules are selected, then all conditions will be 1333 + * AND'ed to produce a final match result. 1334 + * 0 = Matching rule not selected 1335 + * 1 = Matching rule selected 1336 + * 1337 + * 0x0F - RuleSet [7:0] 1338 + * Bits 7:0 - RuleSet [7:0] 1339 + */ 1340 + static void ksz9477_acl_processing_rule_add_match(u8 *entry, u8 match_idx) 1341 + { 1342 + u8 vale = entry[KSZ9477_ACL_PORT_ACCESS_E]; 1343 + u8 valf = entry[KSZ9477_ACL_PORT_ACCESS_F]; 1344 + 1345 + if (match_idx < 8) 1346 + valf |= BIT(match_idx); 1347 + else 1348 + vale |= BIT(match_idx - 8); 1349 + 1350 + ksz9477_acl_set_reg(entry, KSZ9477_ACL_PORT_ACCESS_E, vale); 1351 + ksz9477_acl_set_reg(entry, KSZ9477_ACL_PORT_ACCESS_F, valf); 1352 + } 1353 + 1354 + /** 1355 + * ksz9477_acl_get_init_entry - Get a new uninitialized entry for a specified 1356 + * port on a ksz_device. 1357 + * @dev: The ksz_device instance. 1358 + * @port: The port number to get the uninitialized entry for. 1359 + * @cookie: The cookie to associate with the entry. 1360 + * @prio: The priority to associate with the entry. 1361 + * 1362 + * This function retrieves the next available ACL entry for the specified port, 1363 + * clears all access flags, and associates it with the current cookie. 1364 + * 1365 + * Returns: A pointer to the new uninitialized ACL entry. 1366 + */ 1367 + static struct ksz9477_acl_entry * 1368 + ksz9477_acl_get_init_entry(struct ksz_device *dev, int port, 1369 + unsigned long cookie, u32 prio) 1370 + { 1371 + struct ksz9477_acl_priv *acl = dev->ports[port].acl_priv; 1372 + struct ksz9477_acl_entries *acles = &acl->acles; 1373 + struct ksz9477_acl_entry *entry; 1374 + 1375 + entry = &acles->entries[acles->entries_count]; 1376 + entry->cookie = cookie; 1377 + entry->prio = prio; 1378 + 1379 + /* clear all access flags */ 1380 + entry->entry[KSZ9477_ACL_PORT_ACCESS_10] = 0; 1381 + entry->entry[KSZ9477_ACL_PORT_ACCESS_11] = 0; 1382 + 1383 + return entry; 1384 + } 1385 + 1386 + /** 1387 + * ksz9477_acl_match_process_l2 - Configure Layer 2 ACL matching rules and 1388 + * processing rules. 1389 + * @dev: Pointer to the ksz_device. 1390 + * @port: Port number. 1391 + * @ethtype: Ethernet type. 1392 + * @src_mac: Source MAC address. 1393 + * @dst_mac: Destination MAC address. 1394 + * @cookie: The cookie to associate with the entry. 1395 + * @prio: The priority of the entry. 1396 + * 1397 + * This function sets up matching and processing rules for Layer 2 ACLs. 1398 + * It takes into account that only one MAC per entry is supported. 1399 + */ 1400 + void ksz9477_acl_match_process_l2(struct ksz_device *dev, int port, 1401 + u16 ethtype, u8 *src_mac, u8 *dst_mac, 1402 + unsigned long cookie, u32 prio) 1403 + { 1404 + struct ksz9477_acl_priv *acl = dev->ports[port].acl_priv; 1405 + struct ksz9477_acl_entries *acles = &acl->acles; 1406 + struct ksz9477_acl_entry *entry; 1407 + 1408 + entry = ksz9477_acl_get_init_entry(dev, port, cookie, prio); 1409 + 1410 + /* ACL supports only one MAC per entry */ 1411 + if (src_mac && dst_mac) { 1412 + ksz9477_acl_matching_rule_cfg_l2(entry->entry, ethtype, src_mac, 1413 + true); 1414 + 1415 + /* Add both match entries to first processing rule */ 1416 + ksz9477_acl_processing_rule_add_match(entry->entry, 1417 + acles->entries_count); 1418 + acles->entries_count++; 1419 + ksz9477_acl_processing_rule_add_match(entry->entry, 1420 + acles->entries_count); 1421 + 1422 + entry = ksz9477_acl_get_init_entry(dev, port, cookie, prio); 1423 + ksz9477_acl_matching_rule_cfg_l2(entry->entry, 0, dst_mac, 1424 + false); 1425 + acles->entries_count++; 1426 + } else { 1427 + u8 *mac = src_mac ? src_mac : dst_mac; 1428 + bool is_src = src_mac ? true : false; 1429 + 1430 + ksz9477_acl_matching_rule_cfg_l2(entry->entry, ethtype, mac, 1431 + is_src); 1432 + ksz9477_acl_processing_rule_add_match(entry->entry, 1433 + acles->entries_count); 1434 + acles->entries_count++; 1435 + } 1436 + }
+281
drivers/net/dsa/microchip/ksz9477_tc_flower.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + // Copyright (c) 2023 Pengutronix, Oleksij Rempel <kernel@pengutronix.de> 3 + 4 + #include "ksz9477.h" 5 + #include "ksz9477_reg.h" 6 + #include "ksz_common.h" 7 + 8 + #define ETHER_TYPE_FULL_MASK cpu_to_be16(~0) 9 + #define KSZ9477_MAX_TC 7 10 + 11 + /** 12 + * ksz9477_flower_parse_key_l2 - Parse Layer 2 key from flow rule and configure 13 + * ACL entries accordingly. 14 + * @dev: Pointer to the ksz_device. 15 + * @port: Port number. 16 + * @extack: Pointer to the netlink_ext_ack. 17 + * @rule: Pointer to the flow_rule. 18 + * @cookie: The cookie to associate with the entry. 19 + * @prio: The priority of the entry. 20 + * 21 + * This function parses the Layer 2 key from the flow rule and configures 22 + * the corresponding ACL entries. It checks for unsupported offloads and 23 + * available entries before proceeding with the configuration. 24 + * 25 + * Returns: 0 on success or a negative error code on failure. 26 + */ 27 + static int ksz9477_flower_parse_key_l2(struct ksz_device *dev, int port, 28 + struct netlink_ext_ack *extack, 29 + struct flow_rule *rule, 30 + unsigned long cookie, u32 prio) 31 + { 32 + struct ksz9477_acl_priv *acl = dev->ports[port].acl_priv; 33 + struct flow_match_eth_addrs ematch; 34 + struct ksz9477_acl_entries *acles; 35 + int required_entries; 36 + u8 *src_mac = NULL; 37 + u8 *dst_mac = NULL; 38 + u16 ethtype = 0; 39 + 40 + if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) { 41 + struct flow_match_basic match; 42 + 43 + flow_rule_match_basic(rule, &match); 44 + 45 + if (match.key->n_proto) { 46 + if (match.mask->n_proto != ETHER_TYPE_FULL_MASK) { 47 + NL_SET_ERR_MSG_MOD(extack, 48 + "ethernet type mask must be a full mask"); 49 + return -EINVAL; 50 + } 51 + 52 + ethtype = be16_to_cpu(match.key->n_proto); 53 + } 54 + } 55 + 56 + if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) { 57 + flow_rule_match_eth_addrs(rule, &ematch); 58 + 59 + if (!is_zero_ether_addr(ematch.key->src)) { 60 + if (!is_broadcast_ether_addr(ematch.mask->src)) 61 + goto not_full_mask_err; 62 + 63 + src_mac = ematch.key->src; 64 + } 65 + 66 + if (!is_zero_ether_addr(ematch.key->dst)) { 67 + if (!is_broadcast_ether_addr(ematch.mask->dst)) 68 + goto not_full_mask_err; 69 + 70 + dst_mac = ematch.key->dst; 71 + } 72 + } 73 + 74 + acles = &acl->acles; 75 + /* ACL supports only one MAC per entry */ 76 + required_entries = src_mac && dst_mac ? 2 : 1; 77 + 78 + /* Check if there are enough available entries */ 79 + if (acles->entries_count + required_entries > KSZ9477_ACL_MAX_ENTRIES) { 80 + NL_SET_ERR_MSG_MOD(extack, "ACL entry limit reached"); 81 + return -EOPNOTSUPP; 82 + } 83 + 84 + ksz9477_acl_match_process_l2(dev, port, ethtype, src_mac, dst_mac, 85 + cookie, prio); 86 + 87 + return 0; 88 + 89 + not_full_mask_err: 90 + NL_SET_ERR_MSG_MOD(extack, "MAC address mask must be a full mask"); 91 + return -EOPNOTSUPP; 92 + } 93 + 94 + /** 95 + * ksz9477_flower_parse_key - Parse flow rule keys for a specified port on a 96 + * ksz_device. 97 + * @dev: The ksz_device instance. 98 + * @port: The port number to parse the flow rule keys for. 99 + * @extack: The netlink extended ACK for reporting errors. 100 + * @rule: The flow_rule to parse. 101 + * @cookie: The cookie to associate with the entry. 102 + * @prio: The priority of the entry. 103 + * 104 + * This function checks if the used keys in the flow rule are supported by 105 + * the device and parses the L2 keys if they match. If unsupported keys are 106 + * used, an error message is set in the extended ACK. 107 + * 108 + * Returns: 0 on success or a negative error code on failure. 109 + */ 110 + static int ksz9477_flower_parse_key(struct ksz_device *dev, int port, 111 + struct netlink_ext_ack *extack, 112 + struct flow_rule *rule, 113 + unsigned long cookie, u32 prio) 114 + { 115 + struct flow_dissector *dissector = rule->match.dissector; 116 + int ret; 117 + 118 + if (dissector->used_keys & 119 + ~(BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) | 120 + BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) | 121 + BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL))) { 122 + NL_SET_ERR_MSG_MOD(extack, 123 + "Unsupported keys used"); 124 + return -EOPNOTSUPP; 125 + } 126 + 127 + if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC) || 128 + flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) { 129 + ret = ksz9477_flower_parse_key_l2(dev, port, extack, rule, 130 + cookie, prio); 131 + if (ret) 132 + return ret; 133 + } 134 + 135 + return 0; 136 + } 137 + 138 + /** 139 + * ksz9477_flower_parse_action - Parse flow rule actions for a specified port 140 + * on a ksz_device. 141 + * @dev: The ksz_device instance. 142 + * @port: The port number to parse the flow rule actions for. 143 + * @extack: The netlink extended ACK for reporting errors. 144 + * @cls: The flow_cls_offload instance containing the flow rule. 145 + * @entry_idx: The index of the ACL entry to store the action. 146 + * 147 + * This function checks if the actions in the flow rule are supported by 148 + * the device. Currently, only actions that change priorities are supported. 149 + * If unsupported actions are encountered, an error message is set in the 150 + * extended ACK. 151 + * 152 + * Returns: 0 on success or a negative error code on failure. 153 + */ 154 + static int ksz9477_flower_parse_action(struct ksz_device *dev, int port, 155 + struct netlink_ext_ack *extack, 156 + struct flow_cls_offload *cls, 157 + int entry_idx) 158 + { 159 + struct flow_rule *rule = flow_cls_offload_flow_rule(cls); 160 + struct ksz9477_acl_priv *acl = dev->ports[port].acl_priv; 161 + const struct flow_action_entry *act; 162 + struct ksz9477_acl_entry *entry; 163 + bool prio_force = false; 164 + u8 prio_val = 0; 165 + int i; 166 + 167 + if (TC_H_MIN(cls->classid)) { 168 + NL_SET_ERR_MSG_MOD(extack, "hw_tc is not supported. Use: action skbedit prio"); 169 + return -EOPNOTSUPP; 170 + } 171 + 172 + flow_action_for_each(i, act, &rule->action) { 173 + switch (act->id) { 174 + case FLOW_ACTION_PRIORITY: 175 + if (act->priority > KSZ9477_MAX_TC) { 176 + NL_SET_ERR_MSG_MOD(extack, "Priority value is too high"); 177 + return -EOPNOTSUPP; 178 + } 179 + prio_force = true; 180 + prio_val = act->priority; 181 + break; 182 + default: 183 + NL_SET_ERR_MSG_MOD(extack, "action not supported"); 184 + return -EOPNOTSUPP; 185 + } 186 + } 187 + 188 + /* pick entry to store action */ 189 + entry = &acl->acles.entries[entry_idx]; 190 + 191 + ksz9477_acl_action_rule_cfg(entry->entry, prio_force, prio_val); 192 + ksz9477_acl_processing_rule_set_action(entry->entry, entry_idx); 193 + 194 + return 0; 195 + } 196 + 197 + /** 198 + * ksz9477_cls_flower_add - Add a flow classification rule for a specified port 199 + * on a ksz_device. 200 + * @ds: The DSA switch instance. 201 + * @port: The port number to add the flow classification rule to. 202 + * @cls: The flow_cls_offload instance containing the flow rule. 203 + * @ingress: A flag indicating if the rule is applied on the ingress path. 204 + * 205 + * This function adds a flow classification rule for a specified port on a 206 + * ksz_device. It checks if the ACL offloading is supported and parses the flow 207 + * keys and actions. If the ACL is not supported, it returns an error. If there 208 + * are unprocessed entries, it parses the action for the rule. 209 + * 210 + * Returns: 0 on success or a negative error code on failure. 211 + */ 212 + int ksz9477_cls_flower_add(struct dsa_switch *ds, int port, 213 + struct flow_cls_offload *cls, bool ingress) 214 + { 215 + struct flow_rule *rule = flow_cls_offload_flow_rule(cls); 216 + struct netlink_ext_ack *extack = cls->common.extack; 217 + struct ksz_device *dev = ds->priv; 218 + struct ksz9477_acl_priv *acl; 219 + int action_entry_idx; 220 + int ret; 221 + 222 + acl = dev->ports[port].acl_priv; 223 + 224 + if (!acl) { 225 + NL_SET_ERR_MSG_MOD(extack, "ACL offloading is not supported"); 226 + return -EOPNOTSUPP; 227 + } 228 + 229 + /* A complex rule set can take multiple entries. Use first entry 230 + * to store the action. 231 + */ 232 + action_entry_idx = acl->acles.entries_count; 233 + 234 + ret = ksz9477_flower_parse_key(dev, port, extack, rule, cls->cookie, 235 + cls->common.prio); 236 + if (ret) 237 + return ret; 238 + 239 + ret = ksz9477_flower_parse_action(dev, port, extack, cls, 240 + action_entry_idx); 241 + if (ret) 242 + return ret; 243 + 244 + ret = ksz9477_sort_acl_entries(dev, port); 245 + if (ret) 246 + return ret; 247 + 248 + return ksz9477_acl_write_list(dev, port); 249 + } 250 + 251 + /** 252 + * ksz9477_cls_flower_del - Remove a flow classification rule for a specified 253 + * port on a ksz_device. 254 + * @ds: The DSA switch instance. 255 + * @port: The port number to remove the flow classification rule from. 256 + * @cls: The flow_cls_offload instance containing the flow rule. 257 + * @ingress: A flag indicating if the rule is applied on the ingress path. 258 + * 259 + * This function removes a flow classification rule for a specified port on a 260 + * ksz_device. It checks if the ACL is initialized, and if not, returns an 261 + * error. If the ACL is initialized, it removes entries with the specified 262 + * cookie and rewrites the ACL list. 263 + * 264 + * Returns: 0 on success or a negative error code on failure. 265 + */ 266 + int ksz9477_cls_flower_del(struct dsa_switch *ds, int port, 267 + struct flow_cls_offload *cls, bool ingress) 268 + { 269 + unsigned long cookie = cls->cookie; 270 + struct ksz_device *dev = ds->priv; 271 + struct ksz9477_acl_priv *acl; 272 + 273 + acl = dev->ports[port].acl_priv; 274 + 275 + if (!acl) 276 + return -EOPNOTSUPP; 277 + 278 + ksz9477_acl_remove_entries(dev, port, &acl->acles, cookie); 279 + 280 + return ksz9477_acl_write_list(dev, port); 281 + }
+58
drivers/net/dsa/microchip/ksz_common.c
··· 2627 2627 ksz_update_port_member(dev, port); 2628 2628 } 2629 2629 2630 + static void ksz_port_teardown(struct dsa_switch *ds, int port) 2631 + { 2632 + struct ksz_device *dev = ds->priv; 2633 + 2634 + switch (dev->chip_id) { 2635 + case KSZ8563_CHIP_ID: 2636 + case KSZ9477_CHIP_ID: 2637 + case KSZ9563_CHIP_ID: 2638 + case KSZ9567_CHIP_ID: 2639 + case KSZ9893_CHIP_ID: 2640 + case KSZ9896_CHIP_ID: 2641 + case KSZ9897_CHIP_ID: 2642 + if (dsa_is_user_port(ds, port)) 2643 + ksz9477_port_acl_free(dev, port); 2644 + } 2645 + } 2646 + 2630 2647 static int ksz_port_pre_bridge_flags(struct dsa_switch *ds, int port, 2631 2648 struct switchdev_brport_flags flags, 2632 2649 struct netlink_ext_ack *extack) ··· 3189 3172 return 0; 3190 3173 } 3191 3174 3175 + static int ksz_cls_flower_add(struct dsa_switch *ds, int port, 3176 + struct flow_cls_offload *cls, bool ingress) 3177 + { 3178 + struct ksz_device *dev = ds->priv; 3179 + 3180 + switch (dev->chip_id) { 3181 + case KSZ8563_CHIP_ID: 3182 + case KSZ9477_CHIP_ID: 3183 + case KSZ9563_CHIP_ID: 3184 + case KSZ9567_CHIP_ID: 3185 + case KSZ9893_CHIP_ID: 3186 + case KSZ9896_CHIP_ID: 3187 + case KSZ9897_CHIP_ID: 3188 + return ksz9477_cls_flower_add(ds, port, cls, ingress); 3189 + } 3190 + 3191 + return -EOPNOTSUPP; 3192 + } 3193 + 3194 + static int ksz_cls_flower_del(struct dsa_switch *ds, int port, 3195 + struct flow_cls_offload *cls, bool ingress) 3196 + { 3197 + struct ksz_device *dev = ds->priv; 3198 + 3199 + switch (dev->chip_id) { 3200 + case KSZ8563_CHIP_ID: 3201 + case KSZ9477_CHIP_ID: 3202 + case KSZ9563_CHIP_ID: 3203 + case KSZ9567_CHIP_ID: 3204 + case KSZ9893_CHIP_ID: 3205 + case KSZ9896_CHIP_ID: 3206 + case KSZ9897_CHIP_ID: 3207 + return ksz9477_cls_flower_del(ds, port, cls, ingress); 3208 + } 3209 + 3210 + return -EOPNOTSUPP; 3211 + } 3212 + 3192 3213 /* Bandwidth is calculated by idle slope/transmission speed. Then the Bandwidth 3193 3214 * is converted to Hex-decimal using the successive multiplication method. On 3194 3215 * every step, integer part is taken and decimal part is carry forwarded. ··· 3559 3504 .port_bridge_join = ksz_port_bridge_join, 3560 3505 .port_bridge_leave = ksz_port_bridge_leave, 3561 3506 .port_stp_state_set = ksz_port_stp_state_set, 3507 + .port_teardown = ksz_port_teardown, 3562 3508 .port_pre_bridge_flags = ksz_port_pre_bridge_flags, 3563 3509 .port_bridge_flags = ksz_port_bridge_flags, 3564 3510 .port_fast_age = ksz_port_fast_age, ··· 3582 3526 .port_hwtstamp_set = ksz_hwtstamp_set, 3583 3527 .port_txtstamp = ksz_port_txtstamp, 3584 3528 .port_rxtstamp = ksz_port_rxtstamp, 3529 + .cls_flower_add = ksz_cls_flower_add, 3530 + .cls_flower_del = ksz_cls_flower_del, 3585 3531 .port_setup_tc = ksz_setup_tc, 3586 3532 .get_mac_eee = ksz_get_mac_eee, 3587 3533 .set_mac_eee = ksz_set_mac_eee,
+1
drivers/net/dsa/microchip/ksz_common.h
··· 117 117 u32 rgmii_tx_val; 118 118 u32 rgmii_rx_val; 119 119 struct ksz_device *ksz_dev; 120 + void *acl_priv; 120 121 struct ksz_irq pirq; 121 122 u8 num; 122 123 #if IS_ENABLED(CONFIG_NET_DSA_MICROCHIP_KSZ_PTP)