Skip to content

Commit

Permalink
ethernet: use atomic_* to access volatile variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Sikorski committed Aug 28, 2019
1 parent a660ce0 commit 7b90e8e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
10 changes: 5 additions & 5 deletions drivers/bdring.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include LWIP_HOOK_FILENAME

#include <stdatomic.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -206,7 +207,7 @@ size_t net_reapTxFinished(net_bufdesc_ring_t *ring)

n = 0;
i = ring->tail;
head = *(volatile unsigned *)&ring->head;
head = atomic_load(&ring->head);
while (i != head) {
if (!ring->ops->nextTxDone(ring, i))
break;
Expand All @@ -221,7 +222,7 @@ size_t net_reapTxFinished(net_bufdesc_ring_t *ring)
}

if (n)
*(volatile unsigned *)&ring->tail = i;
atomic_store(&ring->tail, i);

mutexUnlock(ring->lock);
return n;
Expand Down Expand Up @@ -275,7 +276,7 @@ size_t net_transmitPacket(net_bufdesc_ring_t *ring, struct pbuf *p)

mutexLock(ring->lock);
// NOTE: 2^n ring size verified in net_initRings
n = *(volatile unsigned *)&ring->tail; // access tail once - it may be advanced by tx_done thread
n = atomic_load(&ring->tail); // access tail once - it may be advanced by tx_done thread
i = ring->head;
n = (n - i - 1) & ring->last;
if (n > MAX_TX_FRAGMENTS)
Expand All @@ -300,8 +301,7 @@ size_t net_transmitPacket(net_bufdesc_ring_t *ring, struct pbuf *p)
last = 0;
}

asm volatile ("" ::: "memory");
*(volatile unsigned *)&ring->head = ni; // sync with read in net_reapTxFinished() run in tx_done thread
atomic_store(&ring->head, ni);
mutexUnlock(ring->lock);
return frags;
}
15 changes: 6 additions & 9 deletions drivers/bdring.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define PHOENIX_NET_BDRING_H_

#include <sys/types.h>
#include <stdatomic.h>
#include "lwip/pbuf.h"


Expand Down Expand Up @@ -45,7 +46,8 @@ struct net_bufdesc_ring_
{
volatile void *ring;
struct pbuf **bufp;
unsigned head, tail, last;
volatile unsigned head, tail;
unsigned last;
addr_t phys;
const net_bufdesc_ops_t *ops;
handle_t lock;
Expand All @@ -61,15 +63,10 @@ size_t net_transmitPacket(net_bufdesc_ring_t *ring, struct pbuf *p);

static inline int net_rxFullyFilled(net_bufdesc_ring_t *ring)
{
return ((ring->head - ring->tail) & ring->last) == 1;
}

unsigned tail = atomic_load(&ring->tail);
unsigned head = atomic_load(&ring->head);

static inline int net_txEmpty(net_bufdesc_ring_t *ring)
{
// called from netport thread, races against irq/tx_done
return ring->head == *(volatile unsigned *)&ring->tail;
return ((head - tail) & ring->last) == 1;
}


#endif /* PHOENIX_NET_BDRING_H_ */
15 changes: 4 additions & 11 deletions drivers/imx6-enet.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <sys/platform.h>
#include <sys/threads.h>
#include <phoenix/arch/imx6ull.h>
#include <stdatomic.h>
#include <endian.h>
#include <errno.h>
#include <stdarg.h>
Expand Down Expand Up @@ -316,11 +317,7 @@ static void enet_fillRxDesc(const net_bufdesc_ring_t *ring, size_t i, addr_t pa,
#if USE_ENET_EXT_DESCRIPTORS
desc->yflags = ENET_RXDY_INT;
#endif
asm ("" ::: "memory");

desc->flags = ENET_DESC_OWN | wrap;

asm ("" ::: "memory");
atomic_store(&desc->flags, ENET_DESC_OWN | wrap);
}


Expand Down Expand Up @@ -355,11 +352,7 @@ static void enet_fillTxDesc(const net_bufdesc_ring_t *ring, size_t i, addr_t pa,
desc->yflags = yflags;
#endif

asm ("" ::: "memory");

desc->flags = flags;

asm ("" ::: "memory");
atomic_store(&desc->flags, flags);
}


Expand Down Expand Up @@ -404,7 +397,7 @@ static int enet_irq_handler(unsigned irq, void *arg)
state->mmio->EIMR &= ~(ENET_IRQ_RXF | ENET_IRQ_TXF);

if (events & ENET_IRQ_EBERR)
__sync_fetch_and_or(&state->drv_exit, EV_BUS_ERROR); // FIXME: atomic_set()
atomic_fetch_or(&state->drv_exit, EV_BUS_ERROR);

return 0;
}
Expand Down

0 comments on commit 7b90e8e

Please sign in to comment.