Skip to content

Commit

Permalink
can: m_can: Implement transmit coalescing
Browse files Browse the repository at this point in the history
Extend the coalescing implementation for transmits.

In normal mode the chip raises an interrupt for every finished transmit.
This implementation switches to coalescing mode as soon as an interrupt
handled a transmit. For coalescing the watermark level interrupt is used
to interrupt exactly after x frames were sent. It switches back into
normal mode once there was an interrupt with no finished transmit and
the timer being inactive.

The timer is shared with receive coalescing. The time for receive and
transmit coalescing timers have to be the same for that to work. The
benefit is to have only a single running timer.

Signed-off-by: Markus Schneider-Pargmann <[email protected]>
Reviewed-by: Simon Horman <[email protected]>
Link: https://lore.kernel.org/all/[email protected]
Signed-off-by: Marc Kleine-Budde <[email protected]>
  • Loading branch information
scosu authored and marckleinebudde committed Feb 12, 2024
1 parent 07f2509 commit ec390d0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
33 changes: 20 additions & 13 deletions drivers/net/can/m_can/m_can.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ enum m_can_reg {
#define TXESC_TBDS_64B 0x7

/* Tx Event FIFO Configuration (TXEFC) */
#define TXEFC_EFWM_MASK GENMASK(29, 24)
#define TXEFC_EFS_MASK GENMASK(21, 16)

/* Tx Event FIFO Status (TXEFS) */
Expand Down Expand Up @@ -432,7 +433,7 @@ static void m_can_interrupt_enable(struct m_can_classdev *cdev, u32 interrupts)

static void m_can_coalescing_disable(struct m_can_classdev *cdev)
{
u32 new_interrupts = cdev->active_interrupts | IR_RF0N;
u32 new_interrupts = cdev->active_interrupts | IR_RF0N | IR_TEFN;

if (!cdev->net->irq)
return;
Expand Down Expand Up @@ -1115,24 +1116,29 @@ static int m_can_echo_tx_event(struct net_device *dev)
static void m_can_coalescing_update(struct m_can_classdev *cdev, u32 ir)
{
u32 new_interrupts = cdev->active_interrupts;
bool enable_timer = false;
bool enable_rx_timer = false;
bool enable_tx_timer = false;

if (!cdev->net->irq)
return;

if (cdev->rx_coalesce_usecs_irq > 0 && (ir & (IR_RF0N | IR_RF0W))) {
enable_timer = true;
enable_rx_timer = true;
new_interrupts &= ~IR_RF0N;
} else if (!hrtimer_active(&cdev->hrtimer)) {
new_interrupts |= IR_RF0N;
}
if (cdev->tx_coalesce_usecs_irq > 0 && (ir & (IR_TEFN | IR_TEFW))) {
enable_tx_timer = true;
new_interrupts &= ~IR_TEFN;
}
if (!enable_rx_timer && !hrtimer_active(&cdev->hrtimer))
new_interrupts |= IR_RF0N;
if (!enable_tx_timer && !hrtimer_active(&cdev->hrtimer))
new_interrupts |= IR_TEFN;

m_can_interrupt_enable(cdev, new_interrupts);
if (enable_timer) {
hrtimer_start(&cdev->hrtimer,
ns_to_ktime(cdev->rx_coalesce_usecs_irq * NSEC_PER_USEC),
if (enable_rx_timer | enable_tx_timer)
hrtimer_start(&cdev->hrtimer, cdev->irq_timer_wait,
HRTIMER_MODE_REL);
}
}

static irqreturn_t m_can_isr(int irq, void *dev_id)
Expand Down Expand Up @@ -1187,7 +1193,7 @@ static irqreturn_t m_can_isr(int irq, void *dev_id)
netif_wake_queue(dev);
}
} else {
if (ir & IR_TEFN) {
if (ir & (IR_TEFN | IR_TEFW)) {
/* New TX FIFO Element arrived */
if (m_can_echo_tx_event(dev) != 0)
goto out_fail;
Expand Down Expand Up @@ -1355,9 +1361,8 @@ static int m_can_chip_config(struct net_device *dev)
}

/* Disable unused interrupts */
interrupts &= ~(IR_ARA | IR_ELO | IR_DRX | IR_TEFF | IR_TEFW | IR_TFE |
IR_TCF | IR_HPM | IR_RF1F | IR_RF1W | IR_RF1N |
IR_RF0F);
interrupts &= ~(IR_ARA | IR_ELO | IR_DRX | IR_TEFF | IR_TFE | IR_TCF |
IR_HPM | IR_RF1F | IR_RF1W | IR_RF1N | IR_RF0F);

m_can_config_endisable(cdev, true);

Expand Down Expand Up @@ -1394,6 +1399,8 @@ static int m_can_chip_config(struct net_device *dev)
} else {
/* Full TX Event FIFO is used */
m_can_write(cdev, M_CAN_TXEFC,
FIELD_PREP(TXEFC_EFWM_MASK,
cdev->tx_max_coalesced_frames_irq) |
FIELD_PREP(TXEFC_EFS_MASK,
cdev->mcfg[MRAM_TXE].num) |
cdev->mcfg[MRAM_TXE].off);
Expand Down
4 changes: 4 additions & 0 deletions drivers/net/can/m_can/m_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ struct m_can_classdev {
struct sk_buff *tx_skb;
struct phy *transceiver;

ktime_t irq_timer_wait;

struct m_can_ops *ops;

int version;
Expand All @@ -96,6 +98,8 @@ struct m_can_classdev {
u32 active_interrupts;
u32 rx_max_coalesced_frames_irq;
u32 rx_coalesce_usecs_irq;
u32 tx_max_coalesced_frames_irq;
u32 tx_coalesce_usecs_irq;

struct mram_cfg mcfg[MRAM_CFG_NUM];

Expand Down

0 comments on commit ec390d0

Please sign in to comment.