iPXE
dwmac.c File Reference

Synopsys DesignWare MAC network driver. More...

#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/netdevice.h>
#include <ipxe/ethernet.h>
#include <ipxe/if_ether.h>
#include <ipxe/iobuf.h>
#include <ipxe/timer.h>
#include <ipxe/devtree.h>
#include <ipxe/fdt.h>
#include "dwmac.h"

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static void dwmac_dump_mac (struct dwmac *dwmac)
 Dump MAC registers (for debugging)
static void dwmac_dump_dma (struct dwmac *dwmac)
 Dump DMA registers (for debugging)
static void dwmac_dump (struct dwmac *dwmac)
 Dump all registers (for debugging)
static int dwmac_reset (struct dwmac *dwmac)
 Reset hardware.
static void dwmac_check_link (struct net_device *netdev)
 Check link state.
static int dwmac_create_ring (struct dwmac *dwmac, struct dwmac_ring *ring)
 Create descriptor ring.
static void dwmac_destroy_ring (struct dwmac *dwmac, struct dwmac_ring *ring)
 Destroy descriptor ring.
static void dwmac_refill_rx (struct dwmac *dwmac)
 Refill receive descriptor ring.
static int dwmac_open (struct net_device *netdev)
 Open network device.
static void dwmac_close (struct net_device *netdev)
 Close network device.
static int dwmac_transmit (struct net_device *netdev, struct io_buffer *iobuf)
 Transmit packet.
static void dwmac_poll_tx (struct net_device *netdev)
 Poll for completed packets.
static void dwmac_poll_rx (struct net_device *netdev)
 Poll for received packets.
static void dwmac_poll (struct net_device *netdev)
 Poll for completed and received packets.
static int dwmac_probe (struct dt_device *dt, unsigned int offset)
 Probe devicetree device.
static void dwmac_remove (struct dt_device *dt)
 Remove devicetree device.

Variables

static struct net_device_operations dwmac_operations
 DesignWare MAC network device operations.
static const char * dwmac_ids []
 DesignWare MAC compatible model identifiers.
struct dt_driver dwmac_driver __dt_driver
 DesignWare MAC devicetree driver.

Detailed Description

Synopsys DesignWare MAC network driver.

Definition in file dwmac.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ dwmac_dump_mac()

void dwmac_dump_mac ( struct dwmac * dwmac)
static

Dump MAC registers (for debugging)

Parameters
dwmacDesignWare MAC device

Definition at line 57 of file dwmac.c.

57 {
58
59 /* Do nothing unless debugging is enabled */
60 if ( ! DBG_LOG )
61 return;
62
63 /* Dump MAC registers */
64 DBGC ( dwmac, "DWMAC %s ver %08x cfg %08x flt %08x flo %08x\n",
68 readl ( dwmac->regs + DWMAC_FLOW ) );
69 DBGC ( dwmac, "DWMAC %s isr %08x dbg %08x gmi %08x\n",
72 readl ( dwmac->regs + DWMAC_GMII ) );
73}
#define DWMAC_ISR
Interrupt status register.
Definition dwmac.h:54
#define DWMAC_CFG
MAC configuration register.
Definition dwmac.h:26
#define DWMAC_DEBUG
Debug register.
Definition dwmac.h:51
#define DWMAC_GMII
SGMII/RGMII status register.
Definition dwmac.h:72
#define DWMAC_FILTER
MAC filter register.
Definition dwmac.h:33
#define DWMAC_VER
Version register.
Definition dwmac.h:40
#define DWMAC_FLOW
Flow control register.
Definition dwmac.h:37
#define DBGC(...)
Definition compiler.h:505
#define DBG_LOG
Definition compiler.h:317
A DesignWare MAC network card.
Definition dwmac.h:230
void * regs
Registers.
Definition dwmac.h:232
const char * name
Device name (for debugging)
Definition dwmac.h:236
#define readl
Definition w89c840.c:157

References DBG_LOG, DBGC, DWMAC_CFG, DWMAC_DEBUG, DWMAC_FILTER, DWMAC_FLOW, DWMAC_GMII, DWMAC_ISR, DWMAC_VER, dwmac::name, readl, and dwmac::regs.

Referenced by dwmac_dump().

◆ dwmac_dump_dma()

void dwmac_dump_dma ( struct dwmac * dwmac)
static

Dump DMA registers (for debugging)

Parameters
dwmacDesignWare MAC device

Definition at line 80 of file dwmac.c.

80 {
82
83 /* Do nothing unless debugging is enabled */
84 if ( ! DBG_LOG )
85 return;
86
87 /* Dump DMA registers */
89 DBGC ( dwmac, "DWMAC %s bus %08x fea %08x axi %08x ahb %08x\n",
93 readl ( dwmac->regs + DWMAC_AHB ) );
94 DBGC ( dwmac, "DWMAC %s opm %08x sta %08x drp %08x\n",
97 DBGC ( dwmac, "DWMAC %s txb %08x txd %08x txb %08x\n",
100 readl ( dwmac->regs + DWMAC_TXBUF ) );
101 DBGC ( dwmac, "DWMAC %s rxb %08x rxd %08x rxb %08x\n",
104 readl ( dwmac->regs + DWMAC_RXBUF ) );
105
106 /* Clear sticky bits in status register, since nothing else will */
107 writel ( status, ( dwmac->regs + DWMAC_STATUS ) );
108}
unsigned int uint32_t
Definition stdint.h:12
#define DWMAC_STATUS
Status register.
Definition dwmac.h:104
#define DWMAC_TXBASE
Transmit descriptor list address register.
Definition dwmac.h:101
#define DWMAC_DROP
Packet drop counter register.
Definition dwmac.h:115
#define DWMAC_RXBASE
Receive descriptor list address register.
Definition dwmac.h:98
#define DWMAC_AXI
AXI bus mode register.
Definition dwmac.h:118
#define DWMAC_BUS
Bus mode register.
Definition dwmac.h:80
#define DWMAC_TXBUF
Current transmit buffer address register.
Definition dwmac.h:130
#define DWMAC_TXDESC
Current transmit descriptor register.
Definition dwmac.h:124
#define DWMAC_RXDESC
Current receive descriptor register.
Definition dwmac.h:127
#define DWMAC_OP
Operation mode register.
Definition dwmac.h:108
#define DWMAC_FEATURE
Hardware feature register.
Definition dwmac.h:136
#define DWMAC_RXBUF
Current receive buffer address register.
Definition dwmac.h:133
#define DWMAC_AHB
AHB or AXI status register.
Definition dwmac.h:121
uint8_t status
Status.
Definition ena.h:5
#define writel
Definition w89c840.c:160

References DBG_LOG, DBGC, DWMAC_AHB, DWMAC_AXI, DWMAC_BUS, DWMAC_DROP, DWMAC_FEATURE, DWMAC_OP, DWMAC_RXBASE, DWMAC_RXBUF, DWMAC_RXDESC, DWMAC_STATUS, DWMAC_TXBASE, DWMAC_TXBUF, DWMAC_TXDESC, dwmac::name, readl, dwmac::regs, status, and writel.

Referenced by dwmac_dump().

◆ dwmac_dump()

void dwmac_dump ( struct dwmac * dwmac)
static

Dump all registers (for debugging)

Parameters
dwmacDesignWare MAC device

Definition at line 115 of file dwmac.c.

115 {
116
117 /* Dump MAC and DMA registers */
120}
static void dwmac_dump_dma(struct dwmac *dwmac)
Dump DMA registers (for debugging)
Definition dwmac.c:80
static void dwmac_dump_mac(struct dwmac *dwmac)
Dump MAC registers (for debugging)
Definition dwmac.c:57

References dwmac_dump_dma(), dwmac_dump_mac(), and unused.

Referenced by dwmac_poll_rx(), and dwmac_poll_tx().

◆ dwmac_reset()

int dwmac_reset ( struct dwmac * dwmac)
static

Reset hardware.

Parameters
dwmacDesignWare MAC device
Return values
rcReturn status code

Definition at line 135 of file dwmac.c.

135 {
136 unsigned int i;
138
139 /* Trigger software reset */
141
142 /* Wait for reset to complete */
143 for ( i = 0 ; i < DWMAC_RESET_MAX_WAIT_MS ; i++ ) {
144
145 /* Delay */
146 mdelay ( 1 );
147
148 /* Check for reset completion */
149 bus = readl ( dwmac->regs + DWMAC_BUS );
150 if ( ! ( bus & DWMAC_BUS_SWR ) )
151 return 0;
152 }
153
154 DBGC ( dwmac, "DWMAC %s timed out waiting for reset\n",
155 dwmac->name );
156 return -ETIMEDOUT;
157}
#define DWMAC_BUS_SWR
Software reset.
Definition dwmac.h:86
#define DWMAC_RESET_MAX_WAIT_MS
Time to wait for software reset to complete.
Definition dwmac.h:89
uint8_t bus
Bus.
Definition edd.h:1
#define ETIMEDOUT
Connection timed out.
Definition errno.h:670
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition timer.c:79

References bus, DBGC, DWMAC_BUS, DWMAC_BUS_SWR, DWMAC_RESET_MAX_WAIT_MS, ETIMEDOUT, mdelay(), dwmac::name, readl, dwmac::regs, and writel.

Referenced by dwmac_close(), dwmac_probe(), and dwmac_remove().

◆ dwmac_check_link()

void dwmac_check_link ( struct net_device * netdev)
static

Check link state.

Parameters
netdevNetwork device

Definition at line 171 of file dwmac.c.

171 {
172 struct dwmac *dwmac = netdev->priv;
173 uint32_t gmii;
174
175 /* Read SGMII/RGMII link status */
176 gmii = readl ( dwmac->regs + DWMAC_GMII );
177 DBGC ( dwmac, "DWMAC %s GMII link status %#08x\n", dwmac->name, gmii );
178
179 /* Update network device */
180 if ( gmii & DWMAC_GMII_LINK ) {
182 } else {
184 }
185}
#define DWMAC_GMII_LINK
Link up.
Definition dwmac.h:73
static struct net_device * netdev
Definition gdbudp.c:53
void netdev_link_down(struct net_device *netdev)
Mark network device as having link down.
Definition netdevice.c:231
static void netdev_link_up(struct net_device *netdev)
Mark network device as having link up.
Definition netdevice.h:789

References DBGC, DWMAC_GMII, DWMAC_GMII_LINK, dwmac::name, netdev, netdev_link_down(), netdev_link_up(), readl, and dwmac::regs.

Referenced by dwmac_open(), dwmac_poll(), and dwmac_probe().

◆ dwmac_create_ring()

int dwmac_create_ring ( struct dwmac * dwmac,
struct dwmac_ring * ring )
static

Create descriptor ring.

Parameters
dwmacDesignWare MAC device
ringDescriptor ring
Return values
rcReturn status code

Definition at line 201 of file dwmac.c.

201 {
202 struct dwmac_descriptor *desc;
203 struct dwmac_descriptor *next;
205 unsigned int i;
206
207 /* Allocate descriptor ring (on its own size) */
208 ring->desc = dma_alloc ( dwmac->dma, &ring->map, ring->len, ring->len );
209 if ( ! ring->desc )
210 return -ENOMEM;
211
212 /* Initialise descriptor ring */
213 memset ( ring->desc, 0, ring->len );
214 for ( i = 0 ; i < ring->count ; i++ ) {
215 desc = &ring->desc[i];
216 desc->size = cpu_to_le16 ( DWMAC_RX_LEN |
218 desc->ctrl = ring->ctrl;
219 assert ( desc->ctrl & DWMAC_CTRL_CHAIN );
220 next = &ring->desc[ ( i + 1 ) & ( ring->count - 1 ) ];
221 desc->next = dma ( &ring->map, next );
222 }
223 wmb();
224
225 /* Program ring address */
226 base = dma ( &ring->map, ring->desc );
227 assert ( base == ( ( uint32_t ) base ) );
228 writel ( base, ( dwmac->regs + DWMAC_DMA + ring->qbase ) );
229
230 DBGC ( dwmac, "DWMAC %s ring %02x is at [%08lx,%08lx)\n",
231 dwmac->name, ring->qbase, virt_to_phys ( ring->desc ),
232 ( virt_to_phys ( ring->desc ) + ring->len ) );
233 return 0;
234}
unsigned long physaddr_t
Definition stdint.h:20
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
uint32_t next
Next descriptor address.
Definition dwmac.h:11
#define DWMAC_SIZE_RX_CHAIN
Buffer size.
Definition dwmac.h:171
#define DWMAC_RX_LEN
Length of receive buffers.
Definition dwmac.h:209
#define DWMAC_DMA
DMA register block.
Definition dwmac.h:76
#define DWMAC_CTRL_CHAIN
Chained descriptor.
Definition dwmac.h:176
struct ena_llq_option desc
Descriptor counts.
Definition ena.h:9
#define ENOMEM
Not enough space.
Definition errno.h:535
#define cpu_to_le16(value)
Definition byteswap.h:107
#define wmb()
Definition io.h:546
void * memset(void *dest, int character, size_t len) __nonnull
void * dma_alloc(struct dma_device *dma, struct dma_mapping *map, size_t len, size_t align)
Allocate and map DMA-coherent buffer.
physaddr_t dma(struct dma_mapping *map, void *addr)
Get DMA address from virtual address.
uint32_t base
Base.
Definition librm.h:3
A frame descriptor.
Definition dwmac.h:144
uint8_t qbase
Queue base address register (within DMA block)
Definition dwmac.h:190
uint8_t ctrl
Default control flags.
Definition dwmac.h:194
struct dma_mapping map
Descriptor ring DMA mapping.
Definition dwmac.h:183
struct dwmac_descriptor * desc
Descriptors.
Definition dwmac.h:181
size_t len
Length of descriptors.
Definition dwmac.h:196
uint8_t count
Number of descriptors.
Definition dwmac.h:192
struct dma_device * dma
DMA device.
Definition dwmac.h:234

References assert, base, dwmac_ring::count, cpu_to_le16, dwmac_ring::ctrl, DBGC, desc, dwmac_ring::desc, dma(), dwmac::dma, dma_alloc(), DWMAC_CTRL_CHAIN, DWMAC_DMA, DWMAC_RX_LEN, DWMAC_SIZE_RX_CHAIN, ENOMEM, dwmac_ring::len, dwmac_ring::map, memset(), dwmac::name, next, dwmac_ring::qbase, dwmac::regs, wmb, and writel.

Referenced by dwmac_open().

◆ dwmac_destroy_ring()

void dwmac_destroy_ring ( struct dwmac * dwmac,
struct dwmac_ring * ring )
static

Destroy descriptor ring.

Parameters
dwmacDesignWare MAC device
ringDescriptor ring

Definition at line 242 of file dwmac.c.

243 {
244
245 /* Clear ring address */
246 writel ( 0, ( dwmac->regs + DWMAC_DMA + ring->qbase ) );
247
248 /* Free descriptor ring */
249 dma_free ( &ring->map, ring->desc, ring->len );
250 ring->desc = NULL;
251 ring->prod = 0;
252 ring->cons = 0;
253}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
void dma_free(struct dma_mapping *map, void *addr, size_t len)
Unmap and free DMA-coherent buffer.
unsigned int cons
Consumer index.
Definition dwmac.h:187
unsigned int prod
Producer index.
Definition dwmac.h:185

References dwmac_ring::cons, dwmac_ring::desc, dma_free(), DWMAC_DMA, dwmac_ring::len, dwmac_ring::map, NULL, dwmac_ring::prod, dwmac_ring::qbase, dwmac::regs, and writel.

Referenced by dwmac_close(), and dwmac_open().

◆ dwmac_refill_rx()

void dwmac_refill_rx ( struct dwmac * dwmac)
static

Refill receive descriptor ring.

Parameters
dwmacDesignWare MAC device

Definition at line 260 of file dwmac.c.

260 {
261 struct dwmac_descriptor *rx;
262 struct io_buffer *iobuf;
263 unsigned int rx_idx;
264 unsigned int refilled = 0;
265
266 /* Refill ring */
267 while ( ( dwmac->rx.prod - dwmac->rx.cons ) != DWMAC_NUM_RX_DESC ) {
268
269 /* Allocate I/O buffer */
270 iobuf = alloc_rx_iob ( DWMAC_RX_LEN, dwmac->dma );
271 if ( ! iobuf ) {
272 /* Wait for next refill */
273 break;
274 }
275
276 /* Get next receive descriptor */
277 rx_idx = ( dwmac->rx.prod++ % DWMAC_NUM_RX_DESC );
278 rx = &dwmac->rx.desc[rx_idx];
279
280 /* Populate receive descriptor */
281 rx->addr = cpu_to_le32 ( iob_dma ( iobuf ) );
282 wmb();
283 rx->stat = cpu_to_le32 ( DWMAC_STAT_OWN );
284
285 /* Record I/O buffer */
286 assert ( dwmac->rx_iobuf[rx_idx] == NULL );
287 dwmac->rx_iobuf[rx_idx] = iobuf;
288
289 DBGC2 ( dwmac, "DWMAC %s RX %d is [%08lx,%08lx)\n",
290 dwmac->name, rx_idx, virt_to_phys ( iobuf->data ),
291 ( virt_to_phys ( iobuf->data ) + DWMAC_RX_LEN ) );
292 refilled++;
293 }
294
295 /* Trigger poll */
296 if ( refilled ) {
297 wmb();
298 writel ( 0, ( dwmac->regs + DWMAC_RXPOLL ) );
299 }
300}
#define DWMAC_RXPOLL
Receive poll demand register.
Definition dwmac.h:95
#define DWMAC_NUM_RX_DESC
Number of receive descriptors.
Definition dwmac.h:203
#define DWMAC_STAT_OWN
Owned by hardware.
Definition dwmac.h:160
#define DBGC2(...)
Definition compiler.h:522
#define cpu_to_le32(value)
Definition byteswap.h:108
struct io_buffer * alloc_rx_iob(size_t len, struct dma_device *dma)
Allocate and map I/O buffer for receive DMA.
Definition iobuf.c:188
static __always_inline physaddr_t iob_dma(struct io_buffer *iobuf)
Get I/O buffer DMA address.
Definition iobuf.h:268
struct io_buffer * rx_iobuf[DWMAC_NUM_RX_DESC]
Receive I/O buffers.
Definition dwmac.h:243
struct dwmac_ring rx
Receive ring.
Definition dwmac.h:241
A persistent I/O buffer.
Definition iobuf.h:38
void * data
Start of data.
Definition iobuf.h:53
u8 rx[WPA_TKIP_MIC_KEY_LEN]
MIC key for packets from the AP.
Definition wpa.h:1

References alloc_rx_iob(), assert, dwmac_ring::cons, cpu_to_le32, io_buffer::data, DBGC2, dwmac_ring::desc, dwmac::dma, DWMAC_NUM_RX_DESC, DWMAC_RX_LEN, DWMAC_RXPOLL, DWMAC_STAT_OWN, iob_dma(), dwmac::name, NULL, dwmac_ring::prod, dwmac::regs, dwmac::rx, rx, dwmac::rx_iobuf, wmb, and writel.

Referenced by dwmac_open(), and dwmac_poll().

◆ dwmac_open()

int dwmac_open ( struct net_device * netdev)
static

Open network device.

Parameters
netdevNetwork device
Return values
rcReturn status code

Definition at line 308 of file dwmac.c.

308 {
309 struct dwmac *dwmac = netdev->priv;
310 union dwmac_mac mac;
311 int rc;
312
313 /* Create transmit descriptor ring */
314 if ( ( rc = dwmac_create_ring ( dwmac, &dwmac->tx ) ) != 0 )
315 goto err_create_tx;
316
317 /* Create receive descriptor ring */
318 if ( ( rc = dwmac_create_ring ( dwmac, &dwmac->rx ) ) != 0 )
319 goto err_create_rx;
320
321 /* Set MAC address */
322 memcpy ( mac.raw, netdev->ll_addr, ETH_ALEN );
323 writel ( mac.reg.addrl, ( dwmac->regs + DWMAC_ADDRL ) );
324 writel ( mac.reg.addrh, ( dwmac->regs + DWMAC_ADDRH ) );
325
326 /* Enable promiscuous mode */
328
329 /* Enable transmit and receive */
332 ( dwmac->regs + DWMAC_OP ) );
335 ( dwmac->regs + DWMAC_CFG ) );
336
337 /* Refill receive descriptor ring */
339
340 /* Update link state */
342
343 return 0;
344
346 err_create_rx:
348 err_create_tx:
349 return rc;
350}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
static int dwmac_create_ring(struct dwmac *dwmac, struct dwmac_ring *ring)
Create descriptor ring.
Definition dwmac.c:201
static void dwmac_refill_rx(struct dwmac *dwmac)
Refill receive descriptor ring.
Definition dwmac.c:260
static void dwmac_check_link(struct net_device *netdev)
Check link state.
Definition dwmac.c:171
static void dwmac_destroy_ring(struct dwmac *dwmac, struct dwmac_ring *ring)
Destroy descriptor ring.
Definition dwmac.c:242
#define DWMAC_OP_TXEN
TX enabled.
Definition dwmac.h:111
#define DWMAC_OP_RXEN
RX enabled.
Definition dwmac.h:112
#define DWMAC_CFG_FD
Full duplex.
Definition dwmac.h:28
#define DWMAC_CFG_RXEN
RX enabled.
Definition dwmac.h:30
#define DWMAC_OP_RXSF
RX store and forward.
Definition dwmac.h:109
#define DWMAC_CFG_DO
Disable RX own frames.
Definition dwmac.h:27
#define DWMAC_CFG_TXEN
TX enabled.
Definition dwmac.h:29
#define DWMAC_FILTER_PR
Promiscuous mode.
Definition dwmac.h:34
#define DWMAC_OP_TXSF
TX store and forward.
Definition dwmac.h:110
#define DWMAC_ADDRL
MAC address low register.
Definition dwmac.h:60
#define DWMAC_ADDRH
MAC address high register.
Definition dwmac.h:57
uint8_t mac[ETH_ALEN]
MAC address.
Definition ena.h:13
#define ETH_ALEN
Definition if_ether.h:9
void * memcpy(void *dest, const void *src, size_t len) __nonnull
struct dwmac_ring tx
Transmit ring.
Definition dwmac.h:239
A DesignWare MAC address.
Definition dwmac.h:63

References DWMAC_ADDRH, DWMAC_ADDRL, DWMAC_CFG, DWMAC_CFG_DO, DWMAC_CFG_FD, DWMAC_CFG_RXEN, DWMAC_CFG_TXEN, dwmac_check_link(), dwmac_create_ring(), dwmac_destroy_ring(), DWMAC_FILTER, DWMAC_FILTER_PR, DWMAC_OP, DWMAC_OP_RXEN, DWMAC_OP_RXSF, DWMAC_OP_TXEN, DWMAC_OP_TXSF, dwmac_refill_rx(), ETH_ALEN, mac, memcpy(), netdev, rc, dwmac::regs, dwmac::rx, dwmac::tx, and writel.

◆ dwmac_close()

void dwmac_close ( struct net_device * netdev)
static

Close network device.

Parameters
netdevNetwork device

Definition at line 357 of file dwmac.c.

357 {
358 struct dwmac *dwmac = netdev->priv;
359 unsigned int i;
360
361 /* Reset NIC */
362 dwmac_reset ( dwmac );
363
364 /* Discard unused receive buffers */
365 for ( i = 0 ; i < DWMAC_NUM_RX_DESC ; i++ ) {
366 if ( dwmac->rx_iobuf[i] )
367 free_rx_iob ( dwmac->rx_iobuf[i] );
368 dwmac->rx_iobuf[i] = NULL;
369 }
370
371 /* Destroy receive descriptor ring */
373
374 /* Destroy transmit descriptor ring */
376}
static int dwmac_reset(struct dwmac *dwmac)
Reset hardware.
Definition dwmac.c:135
void free_rx_iob(struct io_buffer *iobuf)
Unmap and free I/O buffer for receive DMA.
Definition iobuf.c:215

References dwmac_destroy_ring(), DWMAC_NUM_RX_DESC, dwmac_reset(), free_rx_iob(), netdev, NULL, dwmac::rx, dwmac::rx_iobuf, and dwmac::tx.

◆ dwmac_transmit()

int dwmac_transmit ( struct net_device * netdev,
struct io_buffer * iobuf )
static

Transmit packet.

Parameters
netdevNetwork device
iobufI/O buffer
Return values
rcReturn status code

Definition at line 385 of file dwmac.c.

386 {
387 struct dwmac *dwmac = netdev->priv;
388 struct dwmac_descriptor *tx;
389 unsigned int tx_idx;
390
391 /* Get next transmit descriptor */
392 if ( ( dwmac->tx.prod - dwmac->tx.cons ) >= DWMAC_NUM_TX_DESC ) {
393 DBGC ( dwmac, "DWMAC %s out of transmit descriptors\n",
394 dwmac->name );
395 return -ENOBUFS;
396 }
397 tx_idx = ( dwmac->tx.prod % DWMAC_NUM_TX_DESC );
398 tx = &dwmac->tx.desc[tx_idx];
399
400 /* Update producer index */
401 dwmac->tx.prod++;
402
403 /* Populate transmit descriptor */
404 tx->size = cpu_to_le16 ( iob_len ( iobuf ) );
405 tx->addr = cpu_to_le32 ( iob_dma ( iobuf ) );
406 wmb();
409 wmb();
410
411 /* Initiate transmission */
412 writel ( 0, ( dwmac->regs + DWMAC_TXPOLL ) );
413
414 DBGC2 ( dwmac, "DWMAC %s TX %d is [%08lx,%08lx)\n",
415 dwmac->name, tx_idx, virt_to_phys ( iobuf->data ),
416 ( virt_to_phys ( iobuf->data ) + iob_len ( iobuf ) ) );
417 return 0;
418}
#define DWMAC_STAT_TX_LAST
Last segment (TX)
Definition dwmac.h:161
#define DWMAC_STAT_TX_CHAIN
Chained descriptor (TX)
Definition dwmac.h:163
#define DWMAC_NUM_TX_DESC
Number of transmit descriptors.
Definition dwmac.h:200
#define DWMAC_TXPOLL
Transmit poll demand register.
Definition dwmac.h:92
#define DWMAC_STAT_TX_FIRST
First segment (TX)
Definition dwmac.h:162
#define ENOBUFS
No buffer space available.
Definition errno.h:499
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition iobuf.h:160
u8 tx[WPA_TKIP_MIC_KEY_LEN]
MIC key for packets to the AP.
Definition wpa.h:4

References dwmac_ring::cons, cpu_to_le16, cpu_to_le32, io_buffer::data, DBGC, DBGC2, dwmac_ring::desc, DWMAC_NUM_TX_DESC, DWMAC_STAT_OWN, DWMAC_STAT_TX_CHAIN, DWMAC_STAT_TX_FIRST, DWMAC_STAT_TX_LAST, DWMAC_TXPOLL, ENOBUFS, iob_dma(), iob_len(), dwmac::name, netdev, dwmac_ring::prod, dwmac::regs, dwmac::tx, tx, wmb, and writel.

◆ dwmac_poll_tx()

void dwmac_poll_tx ( struct net_device * netdev)
static

Poll for completed packets.

@V netdev Network device

Definition at line 425 of file dwmac.c.

425 {
426 struct dwmac *dwmac = netdev->priv;
427 struct dwmac_descriptor *tx;
428 unsigned int tx_idx;
429
430 /* Check for completed packets */
431 while ( dwmac->tx.cons != dwmac->tx.prod ) {
432
433 /* Get next transmit descriptor */
434 tx_idx = ( dwmac->tx.cons % DWMAC_NUM_TX_DESC );
435 tx = &dwmac->tx.desc[tx_idx];
436
437 /* Stop if descriptor is still owned by hardware */
438 if ( tx->stat & cpu_to_le32 ( DWMAC_STAT_OWN ) )
439 return;
440 dwmac->tx.cons++;
441
442 /* Report completion */
443 if ( tx->stat & cpu_to_le32 ( DWMAC_STAT_ERR ) ) {
444 DBGC ( dwmac, "DWMAC %s TX %d error %#08x\n",
445 dwmac->name, tx_idx, le32_to_cpu ( tx->stat ) );
446 dwmac_dump ( dwmac );
448 } else {
449 DBGC2 ( dwmac, "DWMAC %s TX %d complete\n",
450 dwmac->name, tx_idx );
452 }
453 }
454}
static void dwmac_dump(struct dwmac *dwmac)
Dump all registers (for debugging)
Definition dwmac.c:115
#define DWMAC_STAT_ERR
Error summary.
Definition dwmac.h:164
#define EIO
Input/output error.
Definition errno.h:434
#define le32_to_cpu(value)
Definition byteswap.h:114
void netdev_tx_complete_next_err(struct net_device *netdev, int rc)
Complete network transmission.
Definition netdevice.c:510
static void netdev_tx_complete_next(struct net_device *netdev)
Complete network transmission.
Definition netdevice.h:779

References dwmac_ring::cons, cpu_to_le32, DBGC, DBGC2, dwmac_ring::desc, dwmac_dump(), DWMAC_NUM_TX_DESC, DWMAC_STAT_ERR, DWMAC_STAT_OWN, EIO, le32_to_cpu, dwmac::name, netdev, netdev_tx_complete_next(), netdev_tx_complete_next_err(), dwmac_ring::prod, dwmac::tx, and tx.

Referenced by dwmac_poll().

◆ dwmac_poll_rx()

void dwmac_poll_rx ( struct net_device * netdev)
static

Poll for received packets.

Parameters
netdevNetwork device

Definition at line 461 of file dwmac.c.

461 {
462 struct dwmac *dwmac = netdev->priv;
463 struct dwmac_descriptor *rx;
464 struct io_buffer *iobuf;
465 unsigned int rx_idx;
467 size_t len;
468
469 /* Check for received packets */
470 while ( dwmac->rx.cons != dwmac->rx.prod ) {
471
472 /* Get next receive descriptor */
473 rx_idx = ( dwmac->rx.cons % DWMAC_NUM_RX_DESC );
474 rx = &dwmac->rx.desc[rx_idx];
475
476 /* Stop if descriptor is still in use */
477 if ( rx->stat & cpu_to_le32 ( DWMAC_STAT_OWN ) )
478 return;
479 dwmac->rx.cons++;
480
481 /* Consume I/O buffer */
482 iobuf = dwmac->rx_iobuf[rx_idx];
483 assert ( iobuf != NULL );
484 dwmac->rx_iobuf[rx_idx] = NULL;
485
486 /* Hand off to network stack */
487 stat = le32_to_cpu ( rx->stat );
490 if ( stat & DWMAC_STAT_ERR ) {
491 DBGC ( dwmac, "DWMAC %s RX %d error %#08x\n",
492 dwmac->name, rx_idx, stat );
493 dwmac_dump ( dwmac );
494 netdev_rx_err ( netdev, iobuf, -EIO );
495 } else {
496 len = ( DWMAC_STAT_RX_LEN ( stat ) - 4 /* CRC */ );
497 iob_put ( iobuf, len );
498 DBGC2 ( dwmac, "DWMAC %s RX %d complete (length "
499 "%zd)\n", dwmac->name, rx_idx, len );
500 netdev_rx ( netdev, iobuf );
501 }
502 }
503}
#define DWMAC_STAT_RX_LEN(x)
Frame length (RX)
Definition dwmac.h:167
uint32_t stat
Completion status.
Definition dwmac.h:1
ring len
Length.
Definition dwmac.h:226
#define DWMAC_STAT_RX_FIRST
First segment (RX)
Definition dwmac.h:165
#define DWMAC_STAT_RX_LAST
Last segment (RX)
Definition dwmac.h:166
#define iob_put(iobuf, len)
Definition iobuf.h:125
void netdev_rx(struct net_device *netdev, struct io_buffer *iobuf)
Add packet to receive queue.
Definition netdevice.c:549
void netdev_rx_err(struct net_device *netdev, struct io_buffer *iobuf, int rc)
Discard received packet.
Definition netdevice.c:587

References assert, dwmac_ring::cons, cpu_to_le32, DBGC, DBGC2, dwmac_ring::desc, dwmac_dump(), DWMAC_NUM_RX_DESC, DWMAC_STAT_ERR, DWMAC_STAT_OWN, DWMAC_STAT_RX_FIRST, DWMAC_STAT_RX_LAST, DWMAC_STAT_RX_LEN, EIO, iob_put, le32_to_cpu, len, dwmac::name, netdev, netdev_rx(), netdev_rx_err(), NULL, dwmac_ring::prod, dwmac::rx, rx, dwmac::rx_iobuf, and stat.

Referenced by dwmac_poll().

◆ dwmac_poll()

void dwmac_poll ( struct net_device * netdev)
static

Poll for completed and received packets.

Parameters
netdevNetwork device

Definition at line 510 of file dwmac.c.

510 {
511 struct dwmac *dwmac = netdev->priv;
513
514 /* Check for link status changes */
518
519 /* Poll for TX competions, if applicable */
521
522 /* Poll for RX completions */
524
525 /* Refill RX ring */
527}
static void dwmac_poll_rx(struct net_device *netdev)
Poll for received packets.
Definition dwmac.c:461
static void dwmac_poll_tx(struct net_device *netdev)
Poll for completed packets.
Definition dwmac.c:425
#define DWMAC_STATUS_LINK
Link status change.
Definition dwmac.h:105

References dwmac_check_link(), dwmac_poll_rx(), dwmac_poll_tx(), dwmac_refill_rx(), DWMAC_STATUS, DWMAC_STATUS_LINK, netdev, readl, dwmac::regs, and status.

◆ dwmac_probe()

int dwmac_probe ( struct dt_device * dt,
unsigned int offset )
static

Probe devicetree device.

Parameters
dtDevicetree device
offsetStarting node offset
Return values
rcReturn status code

Definition at line 551 of file dwmac.c.

551 {
552 struct net_device *netdev;
553 struct dwmac *dwmac;
554 union dwmac_mac mac;
556 int rc;
557
558 /* Allocate and initialise net device */
559 netdev = alloc_etherdev ( sizeof ( *dwmac ) );
560 if ( ! netdev ) {
561 rc = -ENOMEM;
562 goto err_alloc;
563 }
565 dwmac = netdev->priv;
566 dt_set_drvdata ( dt, netdev );
567 netdev->dev = &dt->dev;
568 netdev->dma = &dt->dma;
569 memset ( dwmac, 0, sizeof ( *dwmac ) );
570 dwmac->dma = &dt->dma;
571 dwmac->name = netdev->dev->name;
572 dwmac_init_ring ( &dwmac->tx, DWMAC_NUM_TX_DESC, DWMAC_TXBASE,
575 dwmac_init_ring ( &dwmac->rx, DWMAC_NUM_RX_DESC, DWMAC_RXBASE,
577
578 /* Map registers */
580 if ( ! dwmac->regs ) {
581 rc = -ENODEV;
582 goto err_ioremap;
583 }
585 DBGC ( dwmac, "DWMAC %s version %x.%x (user %x.%x)\n", dwmac->name,
590
591 /* Fetch devicetree MAC address */
592 if ( ( rc = fdt_mac ( &sysfdt, offset, netdev ) ) != 0 ) {
593 DBGC ( dwmac, "DWMAC %s could not fetch MAC: %s\n",
594 dwmac->name, strerror ( rc ) );
595 goto err_mac;
596 }
597
598 /* Fetch current MAC address, if set */
599 mac.reg.addrl = readl ( dwmac->regs + DWMAC_ADDRL );
600 mac.reg.addrh = readl ( dwmac->regs + DWMAC_ADDRH );
601 memcpy ( netdev->ll_addr, mac.raw, ETH_ALEN );
602
603 /* Reset the NIC */
604 if ( ( rc = dwmac_reset ( dwmac ) ) != 0 )
605 goto err_reset;
606
607 /* Register network device */
608 if ( ( rc = register_netdev ( netdev ) ) != 0 )
609 goto err_register_netdev;
610
611 /* Update link state */
613
614 return 0;
615
617 err_register_netdev:
618 dwmac_reset ( dwmac );
619 err_reset:
620 err_mac:
621 iounmap ( dwmac->regs );
622 err_ioremap:
624 netdev_put ( netdev );
625 err_alloc:
626 return rc;
627}
u32 version
Driver version.
Definition ath9k_hw.c:1985
uint16_t offset
Offset to command line.
Definition bzimage.h:3
void * dt_ioremap(struct dt_device *dt, unsigned int offset, unsigned int index, size_t len)
Map devicetree range.
Definition devtree.c:52
static void dt_set_drvdata(struct dt_device *dt, void *priv)
Set devicetree driver-private data.
Definition devtree.h:72
static struct net_device_operations dwmac_operations
DesignWare MAC network device operations.
Definition dwmac.c:530
#define DWMAC_VER_USER_MAJOR(x)
User major version.
Definition dwmac.h:41
#define DWMAC_VER_CORE_MAJOR(x)
Core major version.
Definition dwmac.h:45
#define DWMAC_REG_LEN
I/O region length.
Definition dwmac.h:19
#define DWMAC_REG_IDX
I/O region index.
Definition dwmac.h:16
#define DWMAC_VER_CORE_MINOR(x)
Core minor version.
Definition dwmac.h:47
#define DWMAC_CTRL_TX_LAST
Last segment (TX)
Definition dwmac.h:174
#define DWMAC_VER_USER_MINOR(x)
User minor version.
Definition dwmac.h:43
#define DWMAC_CTRL_TX_FIRST
First segment (TX)
Definition dwmac.h:175
struct net_device * alloc_etherdev(size_t priv_size)
Allocate Ethernet device.
Definition ethernet.c:265
int fdt_mac(struct fdt *fdt, unsigned int offset, struct net_device *netdev)
Get MAC address from property.
Definition fdt.c:867
struct fdt sysfdt
The system flattened device tree (if present)
Definition fdt.c:45
#define ENODEV
No such device.
Definition errno.h:510
void iounmap(volatile const void *io_addr)
Unmap I/O address.
void unregister_netdev(struct net_device *netdev)
Unregister network device.
Definition netdevice.c:942
int register_netdev(struct net_device *netdev)
Register network device.
Definition netdevice.c:760
static void netdev_init(struct net_device *netdev, struct net_device_operations *op)
Initialise a network device.
Definition netdevice.h:519
static void netdev_nullify(struct net_device *netdev)
Stop using a network device.
Definition netdevice.h:532
static void netdev_put(struct net_device *netdev)
Drop reference to network device.
Definition netdevice.h:576
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
struct dma_device dma
DMA device.
Definition devtree.h:23
struct device dev
Generic device.
Definition devtree.h:21
A network device.
Definition netdevice.h:353

References alloc_etherdev(), DBGC, dt_device::dev, dt_device::dma, dwmac::dma, dt_ioremap(), dt_set_drvdata(), DWMAC_ADDRH, DWMAC_ADDRL, dwmac_check_link(), DWMAC_CTRL_CHAIN, DWMAC_CTRL_TX_FIRST, DWMAC_CTRL_TX_LAST, DWMAC_NUM_RX_DESC, DWMAC_NUM_TX_DESC, dwmac_operations, DWMAC_REG_IDX, DWMAC_REG_LEN, dwmac_reset(), DWMAC_RXBASE, DWMAC_TXBASE, DWMAC_VER, DWMAC_VER_CORE_MAJOR, DWMAC_VER_CORE_MINOR, DWMAC_VER_USER_MAJOR, DWMAC_VER_USER_MINOR, ENODEV, ENOMEM, ETH_ALEN, fdt_mac(), iounmap(), mac, memcpy(), memset(), dwmac::name, netdev, netdev_init(), netdev_nullify(), netdev_put(), offset, rc, readl, register_netdev(), dwmac::regs, dwmac::rx, strerror(), sysfdt, dwmac::tx, unregister_netdev(), and version.

◆ dwmac_remove()

void dwmac_remove ( struct dt_device * dt)
static

Remove devicetree device.

Parameters
dtDevicetree device

Definition at line 634 of file dwmac.c.

634 {
635 struct net_device *netdev = dt_get_drvdata ( dt );
636 struct dwmac *dwmac = netdev->priv;
637
638 /* Unregister network device */
640
641 /* Reset card */
642 dwmac_reset ( dwmac );
643
644 /* Free network device */
645 iounmap ( dwmac->regs );
647 netdev_put ( netdev );
648}
static void * dt_get_drvdata(struct dt_device *dt)
Get devicetree driver-private data.
Definition devtree.h:82

References dt_get_drvdata(), dwmac_reset(), iounmap(), netdev, netdev_nullify(), netdev_put(), dwmac::regs, and unregister_netdev().

Variable Documentation

◆ dwmac_operations

struct net_device_operations dwmac_operations
static
Initial value:
= {
.open = dwmac_open,
.close = dwmac_close,
.transmit = dwmac_transmit,
.poll = dwmac_poll,
}
static void dwmac_poll(struct net_device *netdev)
Poll for completed and received packets.
Definition dwmac.c:510
static int dwmac_open(struct net_device *netdev)
Open network device.
Definition dwmac.c:308
static void dwmac_close(struct net_device *netdev)
Close network device.
Definition dwmac.c:357
static int dwmac_transmit(struct net_device *netdev, struct io_buffer *iobuf)
Transmit packet.
Definition dwmac.c:385

DesignWare MAC network device operations.

Definition at line 530 of file dwmac.c.

530 {
531 .open = dwmac_open,
532 .close = dwmac_close,
533 .transmit = dwmac_transmit,
534 .poll = dwmac_poll,
535};

Referenced by dwmac_probe().

◆ dwmac_ids

const char* dwmac_ids[]
static
Initial value:
= {
DT_ROM ( "thead,light-dwmac", "T-Head MAC" ),
DT_ROM ( "snps,dwmac", "DesignWare MAC" ),
}
#define DT_ROM(_name, _desc)
Definition devtree.h:34

DesignWare MAC compatible model identifiers.

Definition at line 651 of file dwmac.c.

651 {
652 DT_ROM ( "thead,light-dwmac", "T-Head MAC" ),
653 DT_ROM ( "snps,dwmac", "DesignWare MAC" ),
654};

◆ __dt_driver

struct dt_driver dwmac_driver __dt_driver
Initial value:
= {
.name = "dwmac",
.ids = dwmac_ids,
.id_count = ( sizeof ( dwmac_ids ) / sizeof ( dwmac_ids[0] ) ),
.probe = dwmac_probe,
}
static int dwmac_probe(struct dt_device *dt, unsigned int offset)
Probe devicetree device.
Definition dwmac.c:551
static void dwmac_remove(struct dt_device *dt)
Remove devicetree device.
Definition dwmac.c:634
static const char * dwmac_ids[]
DesignWare MAC compatible model identifiers.
Definition dwmac.c:651
static struct xen_remove_from_physmap * remove
Definition xenmem.h:40

DesignWare MAC devicetree driver.

Definition at line 657 of file dwmac.c.

657 {
658 .name = "dwmac",
659 .ids = dwmac_ids,
660 .id_count = ( sizeof ( dwmac_ids ) / sizeof ( dwmac_ids[0] ) ),
661 .probe = dwmac_probe,
663};