drivers/net/vsc9953: Add commands to manipulate the FDB for VSC9953

The new command:
ethsw [port <port_no>] [vlan <vid>] fdb
        { [help] | show | flush | { add | del } <mac> }

Can be used to add and delete FDB entries. Also, the command can be used
to show entries from the FDB tables. When used with [port <port_no>]
and [vlan <vid>], only the matching the FDB entries can be seen or
flushed. The command has also been added to the generic ethsw parser
from cmd_ethsw.c.

Signed-off-by: Johnson Leung <johnson.leung@freescale.com>
Signed-off-by: Codrin Ciubotariu <codrin.ciubotariu@freescale.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Reviewed-by: York Sun <yorksun@freescale.com>
diff --git a/drivers/net/vsc9953.c b/drivers/net/vsc9953.c
index 21b14e6..071906c 100644
--- a/drivers/net/vsc9953.c
+++ b/drivers/net/vsc9953.c
@@ -783,6 +783,389 @@
 	return 0;
 }
 
+/* wait for FDB to become available */
+static int vsc9953_mac_table_poll_idle(void)
+{
+	struct vsc9953_analyzer *l2ana_reg;
+	u32 timeout;
+
+	l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
+			VSC9953_ANA_OFFSET);
+
+	timeout = 50000;
+	while (((in_le32(&l2ana_reg->ana_tables.mac_access) &
+			 VSC9953_MAC_CMD_MASK) !=
+		VSC9953_MAC_CMD_IDLE) && --timeout)
+		udelay(1);
+
+	return timeout ? 0 : -EBUSY;
+}
+
+/* enum describing available commands for the MAC table */
+enum mac_table_cmd {
+	MAC_TABLE_READ,
+	MAC_TABLE_LOOKUP,
+	MAC_TABLE_WRITE,
+	MAC_TABLE_LEARN,
+	MAC_TABLE_FORGET,
+	MAC_TABLE_GET_NEXT,
+	MAC_TABLE_AGE,
+};
+
+/* Issues a command to the FDB table */
+static int vsc9953_mac_table_cmd(enum mac_table_cmd cmd)
+{
+	struct vsc9953_analyzer *l2ana_reg;
+
+	l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
+			VSC9953_ANA_OFFSET);
+
+	switch (cmd) {
+	case MAC_TABLE_READ:
+		clrsetbits_le32(&l2ana_reg->ana_tables.mac_access,
+				VSC9953_MAC_CMD_MASK | VSC9953_MAC_CMD_VALID,
+				VSC9953_MAC_CMD_READ);
+		break;
+	case MAC_TABLE_LOOKUP:
+		clrsetbits_le32(&l2ana_reg->ana_tables.mac_access,
+				VSC9953_MAC_CMD_MASK, VSC9953_MAC_CMD_READ |
+				VSC9953_MAC_CMD_VALID);
+		break;
+	case MAC_TABLE_WRITE:
+		clrsetbits_le32(&l2ana_reg->ana_tables.mac_access,
+				VSC9953_MAC_CMD_MASK |
+				VSC9953_MAC_ENTRYTYPE_MASK,
+				VSC9953_MAC_CMD_WRITE |
+				VSC9953_MAC_ENTRYTYPE_LOCKED);
+		break;
+	case MAC_TABLE_LEARN:
+		clrsetbits_le32(&l2ana_reg->ana_tables.mac_access,
+				VSC9953_MAC_CMD_MASK |
+				VSC9953_MAC_ENTRYTYPE_MASK,
+				VSC9953_MAC_CMD_LEARN |
+				VSC9953_MAC_ENTRYTYPE_LOCKED |
+				VSC9953_MAC_CMD_VALID);
+		break;
+	case MAC_TABLE_FORGET:
+		clrsetbits_le32(&l2ana_reg->ana_tables.mac_access,
+				VSC9953_MAC_CMD_MASK |
+				VSC9953_MAC_ENTRYTYPE_MASK,
+				VSC9953_MAC_CMD_FORGET);
+		break;
+	case MAC_TABLE_GET_NEXT:
+		clrsetbits_le32(&l2ana_reg->ana_tables.mac_access,
+				VSC9953_MAC_CMD_MASK |
+				VSC9953_MAC_ENTRYTYPE_MASK,
+				VSC9953_MAC_CMD_NEXT);
+		break;
+	case MAC_TABLE_AGE:
+		clrsetbits_le32(&l2ana_reg->ana_tables.mac_access,
+				VSC9953_MAC_CMD_MASK |
+				VSC9953_MAC_ENTRYTYPE_MASK,
+				VSC9953_MAC_CMD_AGE);
+		break;
+	default:
+		printf("Unknown MAC table command\n");
+	}
+
+	if (vsc9953_mac_table_poll_idle() < 0) {
+		debug("MAC table timeout\n");
+		return -1;
+	}
+
+	return 0;
+}
+
+/* show the FDB entries that correspond to a port and a VLAN */
+static void vsc9953_mac_table_show(int port_no, int vid)
+{
+	int rc[VSC9953_MAX_PORTS];
+	enum port_learn_mode mode[VSC9953_MAX_PORTS];
+	int i;
+	u32 val;
+	u32 vlan;
+	u32 mach;
+	u32 macl;
+	u32 dest_indx;
+	struct vsc9953_analyzer *l2ana_reg;
+
+	l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
+			VSC9953_ANA_OFFSET);
+
+	/* disable auto learning */
+	if (port_no == ETHSW_CMD_PORT_ALL) {
+		for (i = 0; i < VSC9953_MAX_PORTS; i++) {
+			rc[i] = vsc9953_port_learn_mode_get(i, &mode[i]);
+			if (!rc[i] && mode[i] != PORT_LEARN_NONE)
+				vsc9953_port_learn_mode_set(i, PORT_LEARN_NONE);
+		}
+	} else {
+		rc[port_no] = vsc9953_port_learn_mode_get(port_no,
+							  &mode[port_no]);
+		if (!rc[port_no] && mode[port_no] != PORT_LEARN_NONE)
+			vsc9953_port_learn_mode_set(port_no, PORT_LEARN_NONE);
+	}
+
+	/* write port and vid to get selected FDB entries */
+	val = in_le32(&l2ana_reg->ana.anag_efil);
+	if (port_no != ETHSW_CMD_PORT_ALL) {
+		val = bitfield_replace_by_mask(val, VSC9953_AGE_PORT_MASK,
+					       port_no) | VSC9953_AGE_PORT_EN;
+	}
+	if (vid != ETHSW_CMD_VLAN_ALL) {
+		val = bitfield_replace_by_mask(val, VSC9953_AGE_VID_MASK,
+					       vid) | VSC9953_AGE_VID_EN;
+	}
+	out_le32(&l2ana_reg->ana.anag_efil, val);
+
+	/* set MAC and VLAN to 0 to look from beginning */
+	clrbits_le32(&l2ana_reg->ana_tables.mach_data,
+		     VSC9953_MAC_VID_MASK | VSC9953_MAC_MACH_MASK);
+	out_le32(&l2ana_reg->ana_tables.macl_data, 0);
+
+	/* get entries */
+	printf("%10s %17s %5s %4s\n", "EntryType", "MAC", "PORT", "VID");
+	do {
+		if (vsc9953_mac_table_cmd(MAC_TABLE_GET_NEXT) < 0) {
+			debug("GET NEXT MAC table command failed\n");
+			break;
+		}
+
+		val = in_le32(&l2ana_reg->ana_tables.mac_access);
+
+		/* get out when an invalid entry is found */
+		if (!(val & VSC9953_MAC_CMD_VALID))
+			break;
+
+		switch (val & VSC9953_MAC_ENTRYTYPE_MASK) {
+		case VSC9953_MAC_ENTRYTYPE_NORMAL:
+			printf("%10s ", "Dynamic");
+			break;
+		case VSC9953_MAC_ENTRYTYPE_LOCKED:
+			printf("%10s ", "Static");
+			break;
+		case VSC9953_MAC_ENTRYTYPE_IPV4MCAST:
+			printf("%10s ", "IPv4 Mcast");
+			break;
+		case VSC9953_MAC_ENTRYTYPE_IPV6MCAST:
+			printf("%10s ", "IPv6 Mcast");
+			break;
+		default:
+			printf("%10s ", "Unknown");
+		}
+
+		dest_indx = bitfield_extract_by_mask(val,
+						     VSC9953_MAC_DESTIDX_MASK);
+
+		val = in_le32(&l2ana_reg->ana_tables.mach_data);
+		vlan = bitfield_extract_by_mask(val, VSC9953_MAC_VID_MASK);
+		mach = bitfield_extract_by_mask(val, VSC9953_MAC_MACH_MASK);
+		macl = in_le32(&l2ana_reg->ana_tables.macl_data);
+
+		printf("%02x:%02x:%02x:%02x:%02x:%02x ", (mach >> 8) & 0xff,
+		       mach & 0xff, (macl >> 24) & 0xff, (macl >> 16) & 0xff,
+		       (macl >> 8) & 0xff, macl & 0xff);
+		printf("%5d ", dest_indx);
+		printf("%4d\n", vlan);
+	} while (1);
+
+	/* set learning mode to previous value */
+	if (port_no == ETHSW_CMD_PORT_ALL) {
+		for (i = 0; i < VSC9953_MAX_PORTS; i++) {
+			if (!rc[i] && mode[i] != PORT_LEARN_NONE)
+				vsc9953_port_learn_mode_set(i, mode[i]);
+		}
+	} else {
+		/* If administrative down, skip */
+		if (!rc[port_no] && mode[port_no] != PORT_LEARN_NONE)
+			vsc9953_port_learn_mode_set(port_no, mode[port_no]);
+	}
+
+	/* reset FDB port and VLAN FDB selection */
+	clrbits_le32(&l2ana_reg->ana.anag_efil, VSC9953_AGE_PORT_EN |
+		     VSC9953_AGE_PORT_MASK | VSC9953_AGE_VID_EN |
+		     VSC9953_AGE_VID_MASK);
+}
+
+/* Add a static FDB entry */
+static int vsc9953_mac_table_add(u8 port_no, uchar mac[6], int vid)
+{
+	u32 val;
+	struct vsc9953_analyzer *l2ana_reg;
+
+	l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
+			VSC9953_ANA_OFFSET);
+
+	val = in_le32(&l2ana_reg->ana_tables.mach_data);
+	val = bitfield_replace_by_mask(val, VSC9953_MACHDATA_VID_MASK, vid) |
+	      (mac[0] << 8) | (mac[1] << 0);
+	out_le32(&l2ana_reg->ana_tables.mach_data, val);
+
+	out_le32(&l2ana_reg->ana_tables.macl_data,
+		 (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) |
+		 (mac[5] << 0));
+
+	/* set on which port is the MAC address added */
+	val = in_le32(&l2ana_reg->ana_tables.mac_access);
+	val = bitfield_replace_by_mask(val, VSC9953_MAC_DESTIDX_MASK, port_no);
+	out_le32(&l2ana_reg->ana_tables.mac_access, val);
+
+	if (vsc9953_mac_table_cmd(MAC_TABLE_LEARN) < 0)
+		return -1;
+
+	/* check if the MAC address was indeed added */
+	val = in_le32(&l2ana_reg->ana_tables.mach_data);
+	val = bitfield_replace_by_mask(val, VSC9953_MACHDATA_VID_MASK, vid) |
+	      (mac[0] << 8) | (mac[1] << 0);
+	out_le32(&l2ana_reg->ana_tables.mach_data, val);
+
+	out_le32(&l2ana_reg->ana_tables.macl_data,
+		 (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) |
+		 (mac[5] << 0));
+
+	if (vsc9953_mac_table_cmd(MAC_TABLE_READ) < 0)
+		return -1;
+
+	val = in_le32(&l2ana_reg->ana_tables.mac_access);
+
+	if ((port_no != bitfield_extract_by_mask(val,
+						 VSC9953_MAC_DESTIDX_MASK))) {
+		printf("Failed to add MAC address\n");
+		return -1;
+	}
+	return 0;
+}
+
+/* Delete a FDB entry */
+static int vsc9953_mac_table_del(uchar mac[6], u16 vid)
+{
+	u32 val;
+	struct vsc9953_analyzer *l2ana_reg;
+
+	l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
+			VSC9953_ANA_OFFSET);
+
+	/* check first if MAC entry is present */
+	val = in_le32(&l2ana_reg->ana_tables.mach_data);
+	val = bitfield_replace_by_mask(val, VSC9953_MACHDATA_VID_MASK, vid) |
+	      (mac[0] << 8) | (mac[1] << 0);
+	out_le32(&l2ana_reg->ana_tables.mach_data, val);
+
+	out_le32(&l2ana_reg->ana_tables.macl_data,
+		 (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) |
+		 (mac[5] << 0));
+
+	if (vsc9953_mac_table_cmd(MAC_TABLE_LOOKUP) < 0) {
+		debug("Lookup in the MAC table failed\n");
+		return -1;
+	}
+
+	if (!(in_le32(&l2ana_reg->ana_tables.mac_access) &
+	      VSC9953_MAC_CMD_VALID)) {
+		printf("The MAC address: %02x:%02x:%02x:%02x:%02x:%02x ",
+		       mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+		printf("VLAN: %d does not exist.\n", vid);
+		return -1;
+	}
+
+	/* FDB entry found, proceed to delete */
+	val = in_le32(&l2ana_reg->ana_tables.mach_data);
+	val = bitfield_replace_by_mask(val, VSC9953_MACHDATA_VID_MASK, vid) |
+	      (mac[0] << 8) | (mac[1] << 0);
+	out_le32(&l2ana_reg->ana_tables.mach_data, val);
+
+	out_le32(&l2ana_reg->ana_tables.macl_data, (mac[2] << 24) |
+		 (mac[3] << 16) | (mac[4] << 8) | (mac[5] << 0));
+
+	if (vsc9953_mac_table_cmd(MAC_TABLE_FORGET) < 0)
+		return -1;
+
+	/* check if the MAC entry is still in FDB */
+	val = in_le32(&l2ana_reg->ana_tables.mach_data);
+	val = bitfield_replace_by_mask(val, VSC9953_MACHDATA_VID_MASK, vid) |
+	      (mac[0] << 8) | (mac[1] << 0);
+	out_le32(&l2ana_reg->ana_tables.mach_data, val);
+
+	out_le32(&l2ana_reg->ana_tables.macl_data, (mac[2] << 24) |
+		 (mac[3] << 16) | (mac[4] << 8) | (mac[5] << 0));
+
+	if (vsc9953_mac_table_cmd(MAC_TABLE_LOOKUP) < 0) {
+		debug("Lookup in the MAC table failed\n");
+		return -1;
+	}
+	if (in_le32(&l2ana_reg->ana_tables.mac_access) &
+	    VSC9953_MAC_CMD_VALID) {
+		printf("Failed to delete MAC address\n");
+		return -1;
+	}
+
+	return 0;
+}
+
+/* age the unlocked entries in FDB */
+static void vsc9953_mac_table_age(int port_no, int vid)
+{
+	int rc[VSC9953_MAX_PORTS];
+	enum port_learn_mode mode[VSC9953_MAX_PORTS];
+	u32 val;
+	int i;
+	struct vsc9953_analyzer *l2ana_reg;
+
+	l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
+			VSC9953_ANA_OFFSET);
+
+	/* set port and VID for selective aging */
+	val = in_le32(&l2ana_reg->ana.anag_efil);
+	if (port_no != ETHSW_CMD_PORT_ALL) {
+		/* disable auto learning */
+		rc[port_no] = vsc9953_port_learn_mode_get(port_no,
+							  &mode[port_no]);
+		if (!rc[port_no] && mode[port_no] != PORT_LEARN_NONE)
+			vsc9953_port_learn_mode_set(port_no, PORT_LEARN_NONE);
+
+		val = bitfield_replace_by_mask(val, VSC9953_AGE_PORT_MASK,
+					       port_no) | VSC9953_AGE_PORT_EN;
+	} else {
+		/* disable auto learning on all ports */
+		for (i = 0; i < VSC9953_MAX_PORTS; i++) {
+			rc[i] = vsc9953_port_learn_mode_get(i, &mode[i]);
+			if (!rc[i] && mode[i] != PORT_LEARN_NONE)
+				vsc9953_port_learn_mode_set(i, PORT_LEARN_NONE);
+		}
+	}
+
+	if (vid != ETHSW_CMD_VLAN_ALL) {
+		val = bitfield_replace_by_mask(val, VSC9953_AGE_VID_MASK, vid) |
+		      VSC9953_AGE_VID_EN;
+	}
+	out_le32(&l2ana_reg->ana.anag_efil, val);
+
+	/* age the dynamic FDB entries */
+	vsc9953_mac_table_cmd(MAC_TABLE_AGE);
+
+	/* clear previously set port and VID */
+	clrbits_le32(&l2ana_reg->ana.anag_efil, VSC9953_AGE_PORT_EN |
+		     VSC9953_AGE_PORT_MASK | VSC9953_AGE_VID_EN |
+		     VSC9953_AGE_VID_MASK);
+
+	if (port_no != ETHSW_CMD_PORT_ALL) {
+		if (!rc[port_no] && mode[port_no] != PORT_LEARN_NONE)
+			vsc9953_port_learn_mode_set(port_no, mode[port_no]);
+	} else {
+		for (i = 0; i < VSC9953_MAX_PORTS; i++) {
+			if (!rc[i] && mode[i] != PORT_LEARN_NONE)
+				vsc9953_port_learn_mode_set(i, mode[i]);
+		}
+	}
+}
+
+/* Delete all the dynamic FDB entries */
+static void vsc9953_mac_table_flush(int port, int vid)
+{
+	vsc9953_mac_table_age(port, vid);
+	vsc9953_mac_table_age(port, vid);
+}
+
 static int vsc9953_port_status_key_func(struct ethsw_command_def *parsed_cmd)
 {
 	int i;
@@ -946,6 +1329,91 @@
 			vsc9953_port_learn_mode_set(i, mode);
 	}
 
+	return CMD_RET_SUCCESS;
+}
+
+static int vsc9953_fdb_show_key_func(struct ethsw_command_def *parsed_cmd)
+{
+	if (parsed_cmd->port != ETHSW_CMD_PORT_ALL &&
+	    !VSC9953_PORT_CHECK(parsed_cmd->port)) {
+		printf("Invalid port number: %d\n", parsed_cmd->port);
+		return CMD_RET_FAILURE;
+	}
+
+	if (parsed_cmd->vid != ETHSW_CMD_VLAN_ALL &&
+	    !VSC9953_VLAN_CHECK(parsed_cmd->vid)) {
+		printf("Invalid VID number: %d\n", parsed_cmd->vid);
+		return CMD_RET_FAILURE;
+	}
+
+	vsc9953_mac_table_show(parsed_cmd->port, parsed_cmd->vid);
+
+	return CMD_RET_SUCCESS;
+}
+
+static int vsc9953_fdb_flush_key_func(struct ethsw_command_def *parsed_cmd)
+{
+	if (parsed_cmd->port != ETHSW_CMD_PORT_ALL &&
+	    !VSC9953_PORT_CHECK(parsed_cmd->port)) {
+		printf("Invalid port number: %d\n", parsed_cmd->port);
+		return CMD_RET_FAILURE;
+	}
+
+	if (parsed_cmd->vid != ETHSW_CMD_VLAN_ALL &&
+	    !VSC9953_VLAN_CHECK(parsed_cmd->vid)) {
+		printf("Invalid VID number: %d\n", parsed_cmd->vid);
+		return CMD_RET_FAILURE;
+	}
+
+	vsc9953_mac_table_flush(parsed_cmd->port, parsed_cmd->vid);
+
+	return CMD_RET_SUCCESS;
+}
+
+static int vsc9953_fdb_entry_add_key_func(struct ethsw_command_def *parsed_cmd)
+{
+	int vid;
+
+	/* a port number must be present */
+	if (parsed_cmd->port == ETHSW_CMD_PORT_ALL) {
+		printf("Please specify a port\n");
+		return CMD_RET_FAILURE;
+	}
+
+	if (!VSC9953_PORT_CHECK(parsed_cmd->port)) {
+		printf("Invalid port number: %d\n", parsed_cmd->port);
+		return CMD_RET_FAILURE;
+	}
+
+	/* Use VLAN 1 if VID is not set */
+	vid = (parsed_cmd->vid == ETHSW_CMD_VLAN_ALL ? 1 : parsed_cmd->vid);
+
+	if (!VSC9953_VLAN_CHECK(vid)) {
+		printf("Invalid VID number: %d\n", vid);
+		return CMD_RET_FAILURE;
+	}
+
+	if (vsc9953_mac_table_add(parsed_cmd->port, parsed_cmd->ethaddr, vid))
+		return CMD_RET_FAILURE;
+
+	return CMD_RET_SUCCESS;
+}
+
+static int vsc9953_fdb_entry_del_key_func(struct ethsw_command_def *parsed_cmd)
+{
+	int vid;
+
+	/* Use VLAN 1 if VID is not set */
+	vid = (parsed_cmd->vid == ETHSW_CMD_VLAN_ALL ? 1 : parsed_cmd->vid);
+
+	if (!VSC9953_VLAN_CHECK(vid)) {
+		printf("Invalid VID number: %d\n", vid);
+		return CMD_RET_FAILURE;
+	}
+
+	if (vsc9953_mac_table_del(parsed_cmd->ethaddr, vid))
+		return CMD_RET_FAILURE;
+
 	return CMD_RET_SUCCESS;
 }
 
@@ -958,6 +1426,10 @@
 		.port_stats_clear = &vsc9953_port_stats_clear_key_func,
 		.port_learn = &vsc9953_learn_set_key_func,
 		.port_learn_show = &vsc9953_learn_show_key_func,
+		.fdb_show = &vsc9953_fdb_show_key_func,
+		.fdb_flush = &vsc9953_fdb_flush_key_func,
+		.fdb_entry_add = &vsc9953_fdb_entry_add_key_func,
+		.fdb_entry_del = &vsc9953_fdb_entry_del_key_func,
 };
 
 #endif /* CONFIG_CMD_ETHSW */