Commit 7cef293b authored by Vladimir Oltean's avatar Vladimir Oltean Committed by David S. Miller

net: dsa: sja1105: fix multicast forwarding working only for last added mdb entry

The commit cited in Fixes: did 2 things: it refactored the read-back
polling from sja1105_dynamic_config_read() into a new function,
sja1105_dynamic_config_wait_complete(), and it called that from
sja1105_dynamic_config_write() too.

What is problematic is the refactoring.

The refactored code from sja1105_dynamic_config_poll_valid() works like
the previous one, but the problem is that it uses another packed_buf[]
SPI buffer, and there was code at the end of sja1105_dynamic_config_read()
which was relying on the read-back packed_buf[]:

	/* Don't dereference possibly NULL pointer - maybe caller
	 * only wanted to see whether the entry existed or not.
	 */
	if (entry)
		ops->entry_packing(packed_buf, entry, UNPACK);

After the change, the packed_buf[] that this code sees is no longer the
entry read back from hardware, but the original entry that the caller
passed to the sja1105_dynamic_config_read(), packed into this buffer.

This difference is the most notable with the SJA1105_SEARCH uses from
sja1105pqrs_fdb_add() - used for both fdb and mdb. There, we have logic
added by commit 728db843 ("net: dsa: sja1105: ignore the FDB entry
for unknown multicast when adding a new address") to figure out whether
the address we're trying to add matches on any existing hardware entry,
with the exception of the catch-all multicast address.

That logic was broken, because with sja1105_dynamic_config_read() not
working properly, it doesn't return us the entry read back from
hardware, but the entry that we passed to it. And, since for multicast,
a match will always exist, it will tell us that any mdb entry already
exists at index=0 L2 Address Lookup table. It is index=0 because the
caller doesn't know the index - it wants to find it out, and
sja1105_dynamic_config_read() does:

	if (index < 0) { // SJA1105_SEARCH
		/* Avoid copying a signed negative number to an u64 */
		cmd.index = 0; // <- this
		cmd.search = true;
	} else {
		cmd.index = index;
		cmd.search = false;
	}

So, to the caller of sja1105_dynamic_config_read(), the returned info
looks entirely legit, and it will add all mdb entries to FDB index 0.
There, they will always overwrite each other (not to mention,
potentially they can also overwrite a pre-existing bridge fdb entry),
and the user-visible impact will be that only the last mdb entry will be
forwarded as it should. The others won't (will be flooded or dropped,
depending on the egress flood settings).

Fixing is a bit more complicated, and involves either passing the same
packed_buf[] to sja1105_dynamic_config_wait_complete(), or moving all
the extra processing on the packed_buf[] to
sja1105_dynamic_config_wait_complete(). I've opted for the latter,
because it makes sja1105_dynamic_config_wait_complete() a bit more
self-contained.

Fixes: df405910 ("net: dsa: sja1105: wait for dynamic config command completion on writes too")
Reported-by: default avatarYanan Yang <yanan.yang@nxp.com>
Signed-off-by: default avatarVladimir Oltean <vladimir.oltean@nxp.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent c9567980
...@@ -1175,18 +1175,15 @@ const struct sja1105_dynamic_table_ops sja1110_dyn_ops[BLK_IDX_MAX_DYN] = { ...@@ -1175,18 +1175,15 @@ const struct sja1105_dynamic_table_ops sja1110_dyn_ops[BLK_IDX_MAX_DYN] = {
static int static int
sja1105_dynamic_config_poll_valid(struct sja1105_private *priv, sja1105_dynamic_config_poll_valid(struct sja1105_private *priv,
struct sja1105_dyn_cmd *cmd, const struct sja1105_dynamic_table_ops *ops,
const struct sja1105_dynamic_table_ops *ops) void *entry, bool check_valident,
bool check_errors)
{ {
u8 packed_buf[SJA1105_MAX_DYN_CMD_SIZE] = {}; u8 packed_buf[SJA1105_MAX_DYN_CMD_SIZE] = {};
struct sja1105_dyn_cmd cmd = {};
int rc; int rc;
/* We don't _need_ to read the full entry, just the command area which /* Read back the whole entry + command structure. */
* is a fixed SJA1105_SIZE_DYN_CMD. But our cmd_packing() API expects a
* buffer that contains the full entry too. Additionally, our API
* doesn't really know how many bytes into the buffer does the command
* area really begin. So just read back the whole entry.
*/
rc = sja1105_xfer_buf(priv, SPI_READ, ops->addr, packed_buf, rc = sja1105_xfer_buf(priv, SPI_READ, ops->addr, packed_buf,
ops->packed_size); ops->packed_size);
if (rc) if (rc)
...@@ -1195,11 +1192,25 @@ sja1105_dynamic_config_poll_valid(struct sja1105_private *priv, ...@@ -1195,11 +1192,25 @@ sja1105_dynamic_config_poll_valid(struct sja1105_private *priv,
/* Unpack the command structure, and return it to the caller in case it /* Unpack the command structure, and return it to the caller in case it
* needs to perform further checks on it (VALIDENT). * needs to perform further checks on it (VALIDENT).
*/ */
memset(cmd, 0, sizeof(*cmd)); ops->cmd_packing(packed_buf, &cmd, UNPACK);
ops->cmd_packing(packed_buf, cmd, UNPACK);
/* Hardware hasn't cleared VALID => still working on it */ /* Hardware hasn't cleared VALID => still working on it */
return cmd->valid ? -EAGAIN : 0; if (cmd.valid)
return -EAGAIN;
if (check_valident && !cmd.valident && !(ops->access & OP_VALID_ANYWAY))
return -ENOENT;
if (check_errors && cmd.errors)
return -EINVAL;
/* Don't dereference possibly NULL pointer - maybe caller
* only wanted to see whether the entry existed or not.
*/
if (entry)
ops->entry_packing(packed_buf, entry, UNPACK);
return 0;
} }
/* Poll the dynamic config entry's control area until the hardware has /* Poll the dynamic config entry's control area until the hardware has
...@@ -1208,8 +1219,9 @@ sja1105_dynamic_config_poll_valid(struct sja1105_private *priv, ...@@ -1208,8 +1219,9 @@ sja1105_dynamic_config_poll_valid(struct sja1105_private *priv,
*/ */
static int static int
sja1105_dynamic_config_wait_complete(struct sja1105_private *priv, sja1105_dynamic_config_wait_complete(struct sja1105_private *priv,
struct sja1105_dyn_cmd *cmd, const struct sja1105_dynamic_table_ops *ops,
const struct sja1105_dynamic_table_ops *ops) void *entry, bool check_valident,
bool check_errors)
{ {
int err, rc; int err, rc;
...@@ -1217,7 +1229,8 @@ sja1105_dynamic_config_wait_complete(struct sja1105_private *priv, ...@@ -1217,7 +1229,8 @@ sja1105_dynamic_config_wait_complete(struct sja1105_private *priv,
rc, rc != -EAGAIN, rc, rc != -EAGAIN,
SJA1105_DYNAMIC_CONFIG_SLEEP_US, SJA1105_DYNAMIC_CONFIG_SLEEP_US,
SJA1105_DYNAMIC_CONFIG_TIMEOUT_US, SJA1105_DYNAMIC_CONFIG_TIMEOUT_US,
false, priv, cmd, ops); false, priv, ops, entry, check_valident,
check_errors);
return err < 0 ? err : rc; return err < 0 ? err : rc;
} }
...@@ -1287,25 +1300,14 @@ int sja1105_dynamic_config_read(struct sja1105_private *priv, ...@@ -1287,25 +1300,14 @@ int sja1105_dynamic_config_read(struct sja1105_private *priv,
mutex_lock(&priv->dynamic_config_lock); mutex_lock(&priv->dynamic_config_lock);
rc = sja1105_xfer_buf(priv, SPI_WRITE, ops->addr, packed_buf, rc = sja1105_xfer_buf(priv, SPI_WRITE, ops->addr, packed_buf,
ops->packed_size); ops->packed_size);
if (rc < 0) {
mutex_unlock(&priv->dynamic_config_lock);
return rc;
}
rc = sja1105_dynamic_config_wait_complete(priv, &cmd, ops);
mutex_unlock(&priv->dynamic_config_lock);
if (rc < 0) if (rc < 0)
return rc; goto out;
if (!cmd.valident && !(ops->access & OP_VALID_ANYWAY)) rc = sja1105_dynamic_config_wait_complete(priv, ops, entry, true, false);
return -ENOENT; out:
mutex_unlock(&priv->dynamic_config_lock);
/* Don't dereference possibly NULL pointer - maybe caller return rc;
* only wanted to see whether the entry existed or not.
*/
if (entry)
ops->entry_packing(packed_buf, entry, UNPACK);
return 0;
} }
int sja1105_dynamic_config_write(struct sja1105_private *priv, int sja1105_dynamic_config_write(struct sja1105_private *priv,
...@@ -1357,22 +1359,14 @@ int sja1105_dynamic_config_write(struct sja1105_private *priv, ...@@ -1357,22 +1359,14 @@ int sja1105_dynamic_config_write(struct sja1105_private *priv,
mutex_lock(&priv->dynamic_config_lock); mutex_lock(&priv->dynamic_config_lock);
rc = sja1105_xfer_buf(priv, SPI_WRITE, ops->addr, packed_buf, rc = sja1105_xfer_buf(priv, SPI_WRITE, ops->addr, packed_buf,
ops->packed_size); ops->packed_size);
if (rc < 0) {
mutex_unlock(&priv->dynamic_config_lock);
return rc;
}
rc = sja1105_dynamic_config_wait_complete(priv, &cmd, ops);
mutex_unlock(&priv->dynamic_config_lock);
if (rc < 0) if (rc < 0)
return rc; goto out;
cmd = (struct sja1105_dyn_cmd) {0}; rc = sja1105_dynamic_config_wait_complete(priv, ops, NULL, false, true);
ops->cmd_packing(packed_buf, &cmd, UNPACK); out:
if (cmd.errors) mutex_unlock(&priv->dynamic_config_lock);
return -EINVAL;
return 0; return rc;
} }
static u8 sja1105_crc8_add(u8 crc, u8 byte, u8 poly) static u8 sja1105_crc8_add(u8 crc, u8 byte, u8 poly)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment