Scatter/gather SPI for SJA1105 DSA
Vladimir Oltean
maintainercover Scatter/gather SPI for SJA1105 DSA
This is a small series that reduces the stack memory usage for the sja1105 driver.
Vladimir Oltean (2):
net: dsa: sja1105: Move sja1105_spi_transfer into sja1105_xfer
net: dsa: sja1105: Switch to scatter/gather API for SPI
drivers/net/dsa/sja1105/sja1105.h | 2 +- drivers/net/dsa/sja1105/sja1105_spi.c | 182 ++++++++++++-------------- 2 files changed, 85 insertions(+), 99 deletions(-)
Signature · 1 line
2.17.1
Vladimir Oltean
maintainerPATCH 1/2 net: dsa: sja1105: Move sja1105_spi_transfer into sja1105_xfer
This is a cosmetic patch that reduces some boilerplate in the SPI interaction of the driver.
--- drivers/net/dsa/sja1105/sja1105_spi.c | 48 +++++++++------------------ 1 file changed, 15 insertions(+), 33 deletions(-)
drivers/net/dsa/sja1105/sja1105_spi.c +15 -33
@@ -13,36 +13,6 @@#define SJA1105_SIZE_SPI_TRANSFER_MAX \ (SJA1105_SIZE_SPI_MSG_HEADER + SJA1105_SIZE_SPI_MSG_MAXLEN) -static int sja1105_spi_transfer (const struct sja1105_private *priv, -const void *tx,void *rx,int size) -{ -struct spi_device *spi = priv->spidev; -struct spi_transfer transfer = { - .tx_buf = tx, - .rx_buf = rx, - .len = size, - }; -struct spi_message msg; -int rc; - -if (size >SJA1105_SIZE_SPI_TRANSFER_MAX ) { -dev_err (&spi->dev,"SPI message (%d) longer than max of %d\n" , - size,SJA1105_SIZE_SPI_TRANSFER_MAX ); -return -EMSGSIZE ; - } - -spi_message_init (&msg); -spi_message_add_tail (&transfer, &msg); - - rc =spi_sync (spi, &msg); -if (rc <0 ) { -dev_err (&spi->dev,"SPI transfer failed: %d\n" , rc); -return rc; - } - -return rc; -} -static void sja1105_spi_message_pack (void *buf,const struct sja1105_spi_message *msg) {
@@ -69,10 +39,17 @@ int sja1105_xfer_buf(const struct sja1105_private *priv, sja1105_spi_rw_mode_t rw, u64 reg_addr, void *packed_buf, size_t size_bytes) { + const int msg_len = size_bytes + SJA1105_SIZE_SPI_MSG_HEADER;u8 tx_buf[SJA1105_SIZE_SPI_TRANSFER_MAX ] = {0 };u8 rx_buf[SJA1105_SIZE_SPI_TRANSFER_MAX ] = {0 }; -const int msg_len = size_bytes +SJA1105_SIZE_SPI_MSG_HEADER ; +struct spi_device *spi = priv->spidev;struct sja1105_spi_message msg = {0 }; +struct spi_transfer xfer = { + .tx_buf = tx_buf, + .rx_buf = rx_buf, + .len = msg_len, + }; +struct spi_message m;int rc;if (msg_len >SJA1105_SIZE_SPI_TRANSFER_MAX )
@@ -89,9 +66,14 @@ int sja1105_xfer_buf(const struct sja1105_private *priv,memcpy (tx_buf +SJA1105_SIZE_SPI_MSG_HEADER , packed_buf, size_bytes); - rc =sja1105_spi_transfer (priv, tx_buf, rx_buf, msg_len); -if (rc <0 ) +spi_message_init (&m); +spi_message_add_tail (&xfer, &m); + + rc =spi_sync (spi, &m); +if (rc <0 ) { +dev_err (&spi->dev,"SPI transfer failed: %d\n" , rc);return rc; + }if (rw ==SPI_READ ) memcpy(packed_buf, rx_buf + SJA1105_SIZE_SPI_MSG_HEADER,
Signature · 1 line
2.17.1
Vladimir Oltean
maintainerPATCH 2/2 net: dsa: sja1105: Switch to scatter/gather API for SPI
In reply to Vladimir OlteanThis reworks the SPI transfer implementation to make use of more of the SPI core features. The main benefit is to avoid the memcpy in sja1105_xfer_buf().
The memcpy was only needed because the function was transferring a single buffer at a time. So it needed to copy the caller-provided buffer at buf + 4, to store the SPI message header in the "headroom" area.
But the SPI core supports scatter-gather messages, comprised of multiple transfers. We can actually use those to break apart every SPI message into 2 transfers: one for the header and one for the actual payload.
To keep the behavior the same regarding the chip select signal, it is necessary to tell the SPI core to de-assert the chip select after each chunk. This was not needed before, because each spi_message contained only 1 single transfer.
The meaning of the per-transfer cs_change=1 is:
- If the transfer is the last one of the message, keep CS asserted
- Otherwise, deassert CS
We need to deassert CS in the "otherwise" case, which was implicit before.
Avoiding the memcpy creates yet another opportunity. The device can't process more than 256 bytes of SPI payload at a time, so the sja1105_xfer_long_buf() function used to exist, to split the larger caller buffer into chunks.
But these chunks couldn't be used as scatter/gather buffers for spi_message until now, because of that memcpy (we would have needed more memory for each chunk). So we can now remove the sja1105_xfer_long_buf() function and have a single implementation for long and short buffers.
Another benefit is lower usage of stack memory. Previously we had to store 2 SPI buffers for each chunk. Due to the elimination of the memcpy, we can now send pointers to the actual chunks from the caller-supplied buffer to the SPI core.
Since the patch merges two functions into a rewritten implementation, the function prototype was also changed, mainly for cosmetic consistency with the structures used within it.
--- drivers/net/dsa/sja1105/sja1105.h | 2 +- drivers/net/dsa/sja1105/sja1105_spi.c | 164 +++++++++++++------------- 2 files changed, 85 insertions(+), 81 deletions(-)
drivers/net/dsa/sja1105/sja1105.h +1 -1
@@ -129,7 +129,7 @@ int sja1105_static_config_reload(struct sja1105_private *priv);/* From sja1105_spi.c */ int sja1105_xfer_buf (const struct sja1105_private *priv,sja1105_spi_rw_mode_t rw,u64 reg_addr, -void *packed_buf,size_t size_bytes); +u8 *buf,size_t len);int sja1105_xfer_u32 (const struct sja1105_private *priv,sja1105_spi_rw_mode_t rw,u64 reg_addr,u32 *value); int sja1105_xfer_u64(const struct sja1105_private *priv,
drivers/net/dsa/sja1105/sja1105_spi.c +84 -80
204 lines not on this page · raw message
Signature · 1 line
2.17.1
David Miller
maintainerFrom: Vladimir Oltean <[email protected]>
Date: Sat, 12 Oct 2019 01:31:13 +0300
Quoted 2 lines
This is a small series that reduces the stack memory usage for the sja1105 driver.
Series applied, thanks.