Commit 4b430f5c authored by David S. Miller's avatar David S. Miller
Browse files

Merge branch 'lan966x-switchdev-and-vlan'



Horatiu Vultur says:

====================
net: lan966x: Add switchdev and vlan support

This patch series extends lan966x with switchdev and vlan support.
The first patches just adds new registers and extend the MAC table to
handle the interrupts when a new address is learn/forget.

v7->v8:
- remove extra mac learn when the port leaves the bridge
- replace memcpy with ether_addr_copy
- change the order of operations in lan966x_switch_driver_init/exit
- refactor lan966x_port_bridge_flags

v6->v7:
- fix build issues when compiling as a module

v5->v6:
- fix issues with the singletones, they were not really singletons
- simplify the case where lan966x ports are added to bridges with foreign
  ports
- drop the cases NETDEV_PRE_UP and NETDEV_DOWN
- fix the change of MAC address
- drop the callbacks .ndo_set_features, .ndo_vlan_rx_add_vid,
  .ndo_vlan_rx_kill_vid
- remove duplicate code when port was added in a vlan, the MAC entries
  will be added by the fdb

v4->v5:
- make the notifier_block from lan966x to be singletones
- use switchdev_handle_port_obj_add and switchdev_handle_fdb_event_to_device
  when getting callbacks in the lan966x
- merge the two vlan patches in a single one

v3->v4:
- split the last patch in multiple patches
- replace spin_lock_irqsave/restore with spin_lock/spin_unlock
- remove lan966x_port_change_rx_flags because it was copying all the frames to
  the CPU instead of removing all RX filters.
- implement SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS
- remove calls to __dev_mc_unsync/sync as they are not needed
- replace 0/1 with false/true
- make sure that the lan966x ports are not added to bridges that have other
  interfaces except lan966x
- and allow the lan966x ports to be part of only the same bridge.

v2->v3:
- separate the PVID used when the port is in host mode or vlan unaware
- fix issue when the port was leaving the bridge

v1->v2:
- when allocating entries for the mac table use kzalloc instead of
  devm_kzalloc
- also use GFP_KERNEL instead of GFP_ATOMIC, because is never called
  in atomic context
- when deleting an mac table entry, the order of operations was wrong
- if ana irq is enabled make sure it gets disabled when the driver is
  removed
====================

Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 5f89b389 811ba277
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -37,12 +37,14 @@ properties:
    items:
      - description: register based extraction
      - description: frame dma based extraction
      - description: analyzer interrupt

  interrupt-names:
    minItems: 1
    items:
      - const: xtr
      - const: fdma
      - const: ana

  resets:
    items:
+1 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ config LAN966X_SWITCH
	tristate "Lan966x switch driver"
	depends on HAS_IOMEM
	depends on OF
	depends on NET_SWITCHDEV
	select PHYLINK
	select PACKING
	help
+2 −1
Original line number Diff line number Diff line
@@ -6,4 +6,5 @@
obj-$(CONFIG_LAN966X_SWITCH) += lan966x-switch.o

lan966x-switch-objs  := lan966x_main.o lan966x_phylink.o lan966x_port.o \
			lan966x_mac.o lan966x_ethtool.o
			lan966x_mac.o lan966x_ethtool.o lan966x_switchdev.o \
			lan966x_vlan.o lan966x_fdb.o
+244 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0+

#include <net/switchdev.h>

#include "lan966x_main.h"

struct lan966x_fdb_event_work {
	struct work_struct work;
	struct switchdev_notifier_fdb_info fdb_info;
	struct net_device *dev;
	struct lan966x *lan966x;
	unsigned long event;
};

struct lan966x_fdb_entry {
	struct list_head list;
	unsigned char mac[ETH_ALEN] __aligned(2);
	u16 vid;
	u32 references;
};

static struct lan966x_fdb_entry *
lan966x_fdb_find_entry(struct lan966x *lan966x,
		       struct switchdev_notifier_fdb_info *fdb_info)
{
	struct lan966x_fdb_entry *fdb_entry;

	list_for_each_entry(fdb_entry, &lan966x->fdb_entries, list) {
		if (fdb_entry->vid == fdb_info->vid &&
		    ether_addr_equal(fdb_entry->mac, fdb_info->addr))
			return fdb_entry;
	}

	return NULL;
}

static void lan966x_fdb_add_entry(struct lan966x *lan966x,
				  struct switchdev_notifier_fdb_info *fdb_info)
{
	struct lan966x_fdb_entry *fdb_entry;

	fdb_entry = lan966x_fdb_find_entry(lan966x, fdb_info);
	if (fdb_entry) {
		fdb_entry->references++;
		return;
	}

	fdb_entry = kzalloc(sizeof(*fdb_entry), GFP_KERNEL);
	if (!fdb_entry)
		return;

	ether_addr_copy(fdb_entry->mac, fdb_info->addr);
	fdb_entry->vid = fdb_info->vid;
	fdb_entry->references = 1;
	list_add_tail(&fdb_entry->list, &lan966x->fdb_entries);
}

static bool lan966x_fdb_del_entry(struct lan966x *lan966x,
				  struct switchdev_notifier_fdb_info *fdb_info)
{
	struct lan966x_fdb_entry *fdb_entry, *tmp;

	list_for_each_entry_safe(fdb_entry, tmp, &lan966x->fdb_entries,
				 list) {
		if (fdb_entry->vid == fdb_info->vid &&
		    ether_addr_equal(fdb_entry->mac, fdb_info->addr)) {
			fdb_entry->references--;
			if (!fdb_entry->references) {
				list_del(&fdb_entry->list);
				kfree(fdb_entry);
				return true;
			}
			break;
		}
	}

	return false;
}

void lan966x_fdb_write_entries(struct lan966x *lan966x, u16 vid)
{
	struct lan966x_fdb_entry *fdb_entry;

	list_for_each_entry(fdb_entry, &lan966x->fdb_entries, list) {
		if (fdb_entry->vid != vid)
			continue;

		lan966x_mac_cpu_learn(lan966x, fdb_entry->mac, fdb_entry->vid);
	}
}

void lan966x_fdb_erase_entries(struct lan966x *lan966x, u16 vid)
{
	struct lan966x_fdb_entry *fdb_entry;

	list_for_each_entry(fdb_entry, &lan966x->fdb_entries, list) {
		if (fdb_entry->vid != vid)
			continue;

		lan966x_mac_cpu_forget(lan966x, fdb_entry->mac, fdb_entry->vid);
	}
}

static void lan966x_fdb_purge_entries(struct lan966x *lan966x)
{
	struct lan966x_fdb_entry *fdb_entry, *tmp;

	list_for_each_entry_safe(fdb_entry, tmp, &lan966x->fdb_entries, list) {
		list_del(&fdb_entry->list);
		kfree(fdb_entry);
	}
}

int lan966x_fdb_init(struct lan966x *lan966x)
{
	INIT_LIST_HEAD(&lan966x->fdb_entries);
	lan966x->fdb_work = alloc_ordered_workqueue("lan966x_order", 0);
	if (!lan966x->fdb_work)
		return -ENOMEM;

	return 0;
}

void lan966x_fdb_deinit(struct lan966x *lan966x)
{
	destroy_workqueue(lan966x->fdb_work);
	lan966x_fdb_purge_entries(lan966x);
}

static void lan966x_fdb_event_work(struct work_struct *work)
{
	struct lan966x_fdb_event_work *fdb_work =
		container_of(work, struct lan966x_fdb_event_work, work);
	struct switchdev_notifier_fdb_info *fdb_info;
	struct net_device *dev = fdb_work->dev;
	struct lan966x_port *port;
	struct lan966x *lan966x;
	int ret;

	fdb_info = &fdb_work->fdb_info;
	lan966x = fdb_work->lan966x;

	if (lan966x_netdevice_check(dev)) {
		port = netdev_priv(dev);

		switch (fdb_work->event) {
		case SWITCHDEV_FDB_ADD_TO_DEVICE:
			if (!fdb_info->added_by_user)
				break;
			lan966x_mac_add_entry(lan966x, port, fdb_info->addr,
					      fdb_info->vid);
			break;
		case SWITCHDEV_FDB_DEL_TO_DEVICE:
			if (!fdb_info->added_by_user)
				break;
			lan966x_mac_del_entry(lan966x, fdb_info->addr,
					      fdb_info->vid);
			break;
		}
	} else {
		if (!netif_is_bridge_master(dev))
			goto out;

		/* In case the bridge is called */
		switch (fdb_work->event) {
		case SWITCHDEV_FDB_ADD_TO_DEVICE:
			/* If there is no front port in this vlan, there is no
			 * point to copy the frame to CPU because it would be
			 * just dropped at later point. So add it only if
			 * there is a port but it is required to store the fdb
			 * entry for later point when a port actually gets in
			 * the vlan.
			 */
			lan966x_fdb_add_entry(lan966x, fdb_info);
			if (!lan966x_vlan_cpu_member_cpu_vlan_mask(lan966x,
								   fdb_info->vid))
				break;

			lan966x_mac_cpu_learn(lan966x, fdb_info->addr,
					      fdb_info->vid);
			break;
		case SWITCHDEV_FDB_DEL_TO_DEVICE:
			ret = lan966x_fdb_del_entry(lan966x, fdb_info);
			if (!lan966x_vlan_cpu_member_cpu_vlan_mask(lan966x,
								   fdb_info->vid))
				break;

			if (ret)
				lan966x_mac_cpu_forget(lan966x, fdb_info->addr,
						       fdb_info->vid);
			break;
		}
	}

out:
	kfree(fdb_work->fdb_info.addr);
	kfree(fdb_work);
	dev_put(dev);
}

int lan966x_handle_fdb(struct net_device *dev,
		       struct net_device *orig_dev,
		       unsigned long event, const void *ctx,
		       const struct switchdev_notifier_fdb_info *fdb_info)
{
	struct lan966x_port *port = netdev_priv(dev);
	struct lan966x *lan966x = port->lan966x;
	struct lan966x_fdb_event_work *fdb_work;

	if (ctx && ctx != port)
		return 0;

	switch (event) {
	case SWITCHDEV_FDB_ADD_TO_DEVICE:
	case SWITCHDEV_FDB_DEL_TO_DEVICE:
		if (lan966x_netdevice_check(orig_dev) &&
		    !fdb_info->added_by_user)
			break;

		fdb_work = kzalloc(sizeof(*fdb_work), GFP_ATOMIC);
		if (!fdb_work)
			return -ENOMEM;

		fdb_work->dev = orig_dev;
		fdb_work->lan966x = lan966x;
		fdb_work->event = event;
		INIT_WORK(&fdb_work->work, lan966x_fdb_event_work);
		memcpy(&fdb_work->fdb_info, fdb_info, sizeof(fdb_work->fdb_info));
		fdb_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
		if (!fdb_work->fdb_info.addr)
			goto err_addr_alloc;

		ether_addr_copy((u8 *)fdb_work->fdb_info.addr, fdb_info->addr);
		dev_hold(orig_dev);

		queue_work(lan966x->fdb_work, &fdb_work->work);
		break;
	}

	return 0;
err_addr_alloc:
	kfree(fdb_work);
	return -ENOMEM;
}
+342 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0+

#include <net/switchdev.h>
#include "lan966x_main.h"

#define LAN966X_MAC_COLUMNS		4
@@ -13,6 +14,23 @@
#define MACACCESS_CMD_WRITE		7
#define MACACCESS_CMD_SYNC_GET_NEXT	8

#define LAN966X_MAC_INVALID_ROW		-1

struct lan966x_mac_entry {
	struct list_head list;
	unsigned char mac[ETH_ALEN] __aligned(2);
	u16 vid;
	u16 port_index;
	int row;
};

struct lan966x_mac_raw_entry {
	u32 mach;
	u32 macl;
	u32 maca;
	bool processed;
};

static int lan966x_mac_get_status(struct lan966x *lan966x)
{
	return lan_rd(lan966x, ANA_MACACCESS);
@@ -93,9 +111,333 @@ int lan966x_mac_cpu_forget(struct lan966x *lan966x, const char *addr, u16 vid)
	return lan966x_mac_forget(lan966x, addr, vid, ENTRYTYPE_LOCKED);
}

void lan966x_mac_set_ageing(struct lan966x *lan966x,
			    u32 ageing)
{
	lan_rmw(ANA_AUTOAGE_AGE_PERIOD_SET(ageing / 2),
		ANA_AUTOAGE_AGE_PERIOD,
		lan966x, ANA_AUTOAGE);
}

void lan966x_mac_init(struct lan966x *lan966x)
{
	/* Clear the MAC table */
	lan_wr(MACACCESS_CMD_INIT, lan966x, ANA_MACACCESS);
	lan966x_mac_wait_for_completion(lan966x);

	spin_lock_init(&lan966x->mac_lock);
	INIT_LIST_HEAD(&lan966x->mac_entries);
}

static struct lan966x_mac_entry *lan966x_mac_alloc_entry(const unsigned char *mac,
							 u16 vid, u16 port_index)
{
	struct lan966x_mac_entry *mac_entry;

	mac_entry = kzalloc(sizeof(*mac_entry), GFP_KERNEL);
	if (!mac_entry)
		return NULL;

	memcpy(mac_entry->mac, mac, ETH_ALEN);
	mac_entry->vid = vid;
	mac_entry->port_index = port_index;
	mac_entry->row = LAN966X_MAC_INVALID_ROW;
	return mac_entry;
}

static struct lan966x_mac_entry *lan966x_mac_find_entry(struct lan966x *lan966x,
							const unsigned char *mac,
							u16 vid, u16 port_index)
{
	struct lan966x_mac_entry *res = NULL;
	struct lan966x_mac_entry *mac_entry;

	spin_lock(&lan966x->mac_lock);
	list_for_each_entry(mac_entry, &lan966x->mac_entries, list) {
		if (mac_entry->vid == vid &&
		    ether_addr_equal(mac, mac_entry->mac) &&
		    mac_entry->port_index == port_index) {
			res = mac_entry;
			break;
		}
	}
	spin_unlock(&lan966x->mac_lock);

	return res;
}

static int lan966x_mac_lookup(struct lan966x *lan966x,
			      const unsigned char mac[ETH_ALEN],
			      unsigned int vid, enum macaccess_entry_type type)
{
	int ret;

	lan966x_mac_select(lan966x, mac, vid);

	/* Issue a read command */
	lan_wr(ANA_MACACCESS_ENTRYTYPE_SET(type) |
	       ANA_MACACCESS_VALID_SET(1) |
	       ANA_MACACCESS_MAC_TABLE_CMD_SET(MACACCESS_CMD_READ),
	       lan966x, ANA_MACACCESS);

	ret = lan966x_mac_wait_for_completion(lan966x);
	if (ret)
		return ret;

	return ANA_MACACCESS_VALID_GET(lan_rd(lan966x, ANA_MACACCESS));
}

static void lan966x_fdb_call_notifiers(enum switchdev_notifier_type type,
				       const char *mac, u16 vid,
				       struct net_device *dev)
{
	struct switchdev_notifier_fdb_info info = { 0 };

	info.addr = mac;
	info.vid = vid;
	info.offloaded = true;
	call_switchdev_notifiers(type, dev, &info.info, NULL);
}

int lan966x_mac_add_entry(struct lan966x *lan966x, struct lan966x_port *port,
			  const unsigned char *addr, u16 vid)
{
	struct lan966x_mac_entry *mac_entry;

	if (lan966x_mac_lookup(lan966x, addr, vid, ENTRYTYPE_NORMAL))
		return 0;

	/* In case the entry already exists, don't add it again to SW,
	 * just update HW, but we need to look in the actual HW because
	 * it is possible for an entry to be learn by HW and before we
	 * get the interrupt the frame will reach CPU and the CPU will
	 * add the entry but without the extern_learn flag.
	 */
	mac_entry = lan966x_mac_find_entry(lan966x, addr, vid, port->chip_port);
	if (mac_entry)
		return lan966x_mac_learn(lan966x, port->chip_port,
					 addr, vid, ENTRYTYPE_LOCKED);

	mac_entry = lan966x_mac_alloc_entry(addr, vid, port->chip_port);
	if (!mac_entry)
		return -ENOMEM;

	spin_lock(&lan966x->mac_lock);
	list_add_tail(&mac_entry->list, &lan966x->mac_entries);
	spin_unlock(&lan966x->mac_lock);

	lan966x_mac_learn(lan966x, port->chip_port, addr, vid, ENTRYTYPE_LOCKED);
	lan966x_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED, addr, vid, port->dev);

	return 0;
}

int lan966x_mac_del_entry(struct lan966x *lan966x, const unsigned char *addr,
			  u16 vid)
{
	struct lan966x_mac_entry *mac_entry, *tmp;

	spin_lock(&lan966x->mac_lock);
	list_for_each_entry_safe(mac_entry, tmp, &lan966x->mac_entries,
				 list) {
		if (mac_entry->vid == vid &&
		    ether_addr_equal(addr, mac_entry->mac)) {
			lan966x_mac_forget(lan966x, mac_entry->mac, mac_entry->vid,
					   ENTRYTYPE_LOCKED);

			list_del(&mac_entry->list);
			kfree(mac_entry);
		}
	}
	spin_unlock(&lan966x->mac_lock);

	return 0;
}

void lan966x_mac_purge_entries(struct lan966x *lan966x)
{
	struct lan966x_mac_entry *mac_entry, *tmp;

	spin_lock(&lan966x->mac_lock);
	list_for_each_entry_safe(mac_entry, tmp, &lan966x->mac_entries,
				 list) {
		lan966x_mac_forget(lan966x, mac_entry->mac, mac_entry->vid,
				   ENTRYTYPE_LOCKED);

		list_del(&mac_entry->list);
		kfree(mac_entry);
	}
	spin_unlock(&lan966x->mac_lock);
}

static void lan966x_mac_notifiers(enum switchdev_notifier_type type,
				  unsigned char *mac, u32 vid,
				  struct net_device *dev)
{
	rtnl_lock();
	lan966x_fdb_call_notifiers(type, mac, vid, dev);
	rtnl_unlock();
}

static void lan966x_mac_process_raw_entry(struct lan966x_mac_raw_entry *raw_entry,
					  u8 *mac, u16 *vid, u32 *dest_idx)
{
	mac[0] = (raw_entry->mach >> 8)  & 0xff;
	mac[1] = (raw_entry->mach >> 0)  & 0xff;
	mac[2] = (raw_entry->macl >> 24) & 0xff;
	mac[3] = (raw_entry->macl >> 16) & 0xff;
	mac[4] = (raw_entry->macl >> 8)  & 0xff;
	mac[5] = (raw_entry->macl >> 0)  & 0xff;

	*vid = (raw_entry->mach >> 16) & 0xfff;
	*dest_idx = ANA_MACACCESS_DEST_IDX_GET(raw_entry->maca);
}

static void lan966x_mac_irq_process(struct lan966x *lan966x, u32 row,
				    struct lan966x_mac_raw_entry *raw_entries)
{
	struct lan966x_mac_entry *mac_entry, *tmp;
	unsigned char mac[ETH_ALEN] __aligned(2);
	u32 dest_idx;
	u32 column;
	u16 vid;

	spin_lock(&lan966x->mac_lock);
	list_for_each_entry_safe(mac_entry, tmp, &lan966x->mac_entries, list) {
		bool found = false;

		if (mac_entry->row != row)
			continue;

		for (column = 0; column < LAN966X_MAC_COLUMNS; ++column) {
			/* All the valid entries are at the start of the row,
			 * so when get one invalid entry it can just skip the
			 * rest of the columns
			 */
			if (!ANA_MACACCESS_VALID_GET(raw_entries[column].maca))
				break;

			lan966x_mac_process_raw_entry(&raw_entries[column],
						      mac, &vid, &dest_idx);
			WARN_ON(dest_idx > lan966x->num_phys_ports);

			/* If the entry in SW is found, then there is nothing
			 * to do
			 */
			if (mac_entry->vid == vid &&
			    ether_addr_equal(mac_entry->mac, mac) &&
			    mac_entry->port_index == dest_idx) {
				raw_entries[column].processed = true;
				found = true;
				break;
			}
		}

		if (!found) {
			/* Notify the bridge that the entry doesn't exist
			 * anymore in the HW and remove the entry from the SW
			 * list
			 */
			lan966x_mac_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE,
					      mac_entry->mac, mac_entry->vid,
					      lan966x->ports[mac_entry->port_index]->dev);

			list_del(&mac_entry->list);
			kfree(mac_entry);
		}
	}
	spin_unlock(&lan966x->mac_lock);

	/* Now go to the list of columns and see if any entry was not in the SW
	 * list, then that means that the entry is new so it needs to notify the
	 * bridge.
	 */
	for (column = 0; column < LAN966X_MAC_COLUMNS; ++column) {
		/* All the valid entries are at the start of the row, so when
		 * get one invalid entry it can just skip the rest of the columns
		 */
		if (!ANA_MACACCESS_VALID_GET(raw_entries[column].maca))
			break;

		/* If the entry already exists then don't do anything */
		if (raw_entries[column].processed)
			continue;

		lan966x_mac_process_raw_entry(&raw_entries[column],
					      mac, &vid, &dest_idx);
		WARN_ON(dest_idx > lan966x->num_phys_ports);

		mac_entry = lan966x_mac_alloc_entry(mac, vid, dest_idx);
		if (!mac_entry)
			return;

		mac_entry->row = row;

		spin_lock(&lan966x->mac_lock);
		list_add_tail(&mac_entry->list, &lan966x->mac_entries);
		spin_unlock(&lan966x->mac_lock);

		lan966x_mac_notifiers(SWITCHDEV_FDB_ADD_TO_BRIDGE,
				      mac, vid, lan966x->ports[dest_idx]->dev);
	}
}

irqreturn_t lan966x_mac_irq_handler(struct lan966x *lan966x)
{
	struct lan966x_mac_raw_entry entry[LAN966X_MAC_COLUMNS] = { 0 };
	u32 index, column;
	bool stop = true;
	u32 val;

	/* Start the scan from 0, 0 */
	lan_wr(ANA_MACTINDX_M_INDEX_SET(0) |
	       ANA_MACTINDX_BUCKET_SET(0),
	       lan966x, ANA_MACTINDX);

	while (1) {
		lan_rmw(ANA_MACACCESS_MAC_TABLE_CMD_SET(MACACCESS_CMD_SYNC_GET_NEXT),
			ANA_MACACCESS_MAC_TABLE_CMD,
			lan966x, ANA_MACACCESS);
		lan966x_mac_wait_for_completion(lan966x);

		val = lan_rd(lan966x, ANA_MACTINDX);
		index = ANA_MACTINDX_M_INDEX_GET(val);
		column = ANA_MACTINDX_BUCKET_GET(val);

		/* The SYNC-GET-NEXT returns all the entries(4) in a row in
		 * which is suffered a change. By change it means that new entry
		 * was added or an entry was removed because of ageing.
		 * It would return all the columns for that row. And after that
		 * it would return the next row The stop conditions of the
		 * SYNC-GET-NEXT is when it reaches 'directly' to row 0
		 * column 3. So if SYNC-GET-NEXT returns row 0 and column 0
		 * then it is required to continue to read more even if it
		 * reaches row 0 and column 3.
		 */
		if (index == 0 && column == 0)
			stop = false;

		if (column == LAN966X_MAC_COLUMNS - 1 &&
		    index == 0 && stop)
			break;

		entry[column].mach = lan_rd(lan966x, ANA_MACHDATA);
		entry[column].macl = lan_rd(lan966x, ANA_MACLDATA);
		entry[column].maca = lan_rd(lan966x, ANA_MACACCESS);

		/* Once all the columns are read process them */
		if (column == LAN966X_MAC_COLUMNS - 1) {
			lan966x_mac_irq_process(lan966x, index, entry);
			/* A row was processed so it is safe to assume that the
			 * next row/column can be the stop condition
			 */
			stop = true;
		}
	}

	lan_rmw(ANA_ANAINTR_INTR_SET(0),
		ANA_ANAINTR_INTR,
		lan966x, ANA_ANAINTR);

	return IRQ_HANDLED;
}
Loading