Skip to contentSkip to conversation

bgmac: driver for GBit MAC core on BCMA bus

PATCH Superseded Rafał Miłecki 3 participants 10 messages Dec 24, 2012 – Jan 5, 2013

Subsystems (4)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Rafał Miłecki

author maintainer
#0 lore raw

PATCH 1/1 bgmac: driver for GBit MAC core on BCMA bus

BCMA is a Broadcom specific bus with devices AKA cores. All recent BCMA based SoCs have gigabit ethernet provided by the GBit MAC core. This patch adds driver for such a cores registering itself as a netdev. It has been tested on a BCM4706 and BCM4718 chipsets.

In the kernel tree there is already b44 driver which has some common things with bgmac, however there are many differences that has led to the decision or writing a new driver:
1) GBit MAC cores appear on BCMA bus (not SSB as in case of b44)
2) There is 64bit DMA engine which differs from 32bit one
3) There is no CAM (Content Addressable Memory) in GBit MAC
4) We have 4 TX queues on GBit MAC devices (instead of 1)
5) Many registers have different addresses/values
6) RX header flags are also different

The driver in it's state is functional how, however there is of course place for improvements:
1) Supporting more net_device_ops
2) SUpporting more ethtool_ops
3) Unaligned addressing in DMA
4) Writing separated PHY driver

---
I've implemented all planned improvements since RFC, applied some suggested changes, got rid of memory leak and filled _stop and _remove.

I've also done some testing: bringing if up, pinging and if down. After doing 1000 iterations (and waiting nearly an hour) nothing wrong has happened.

I didn't receive any extra comments since posting RFC (11 days ago), so I'm posting it normally (no RFC).

I can see (according to the Documentation/development-process/2.Process) that it's possible to make an special exception for drivers handling previously-unsupported hardware and submit them even out of merge window. I wonder if this can be applied in this situation? It's a new driver providing long-awaited support for ethernet core. It finally makes it possible to support tons of recent Broadcom routers and there is no way it can cause a regression in bcma code (it only exports one symbol).

David: what do you think about this? I'd love to see it still in 3.8, it's a kernel release that brings support for a lot of Broadcom hw parts (SoC itself, NAND, GPIO, watchdog). The only really missing part seems to be ethernet driver.

---
 drivers/bcma/driver_chipcommon_pmu.c        |    3 +-
 drivers/net/ethernet/broadcom/Kconfig       |    9 +
 drivers/net/ethernet/broadcom/Makefile      |    1 +
 drivers/net/ethernet/broadcom/bgmac.c       | 1390 +++++++++++++++++++++++++++
 drivers/net/ethernet/broadcom/bgmac.h       |  443 +++++++++
 include/linux/bcma/bcma_driver_chipcommon.h |    2 +
 6 files changed, 1847 insertions(+), 1 deletions(-)
 create mode 100644 drivers/net/ethernet/broadcom/bgmac.c
 create mode 100644 drivers/net/ethernet/broadcom/bgmac.h
drivers/bcma/driver_chipcommon_pmu.c +2 -1
index c62c788..932b101 100644
@@ -264,7 +264,7 @@ static u32 bcma_pmu_pll_clock_bcm4706(struct bcma_drv_cc *cc, u32 pll0, u32 m)
 }
 
 /* query bus clock frequency for PMU-enabled chipcommon */
-static u32 bcma_pmu_get_bus_clock(struct bcma_drv_cc *cc)
+u32 bcma_pmu_get_bus_clock(struct bcma_drv_cc *cc)
 {
 	struct bcma_bus *bus = cc->core->bus;
 
@@ -293,6 +293,7 @@ static u32 bcma_pmu_get_bus_clock(struct bcma_drv_cc *cc)
 	}
 	return BCMA_CC_PMU_HT_CLOCK;
 }
+EXPORT_SYMBOL_GPL(bcma_pmu_get_bus_clock);
 
 /* query cpu clock frequency for PMU-enabled chipcommon */
 u32 bcma_pmu_get_cpu_clock(struct bcma_drv_cc *cc)
drivers/net/ethernet/broadcom/Kconfig +9 -0
index f552673..42c0913 100644
@@ -121,4 +121,13 @@ config BNX2X
 	  To compile this driver as a module, choose M here: the module
 	  will be called bnx2x.  This is recommended.
 
+config BGMAC
+	tristate "BCMA bus GBit core support"
+	depends on BCMA_HOST_SOC && HAS_DMA
+	---help---
+	  This driver supports GBit MAC and BCM4706 GBit MAC cores on BCMA bus.
+	  They can be found on BCM47xx SoCs and provide gigabit ethernet.
+	  In case of using this driver on BCM4706 it's also requires to enable
+	  BCMA_DRIVER_GMAC_CMN to make it work.
+
 endif # NET_VENDOR_BROADCOM
drivers/net/ethernet/broadcom/Makefile +1 -0
index b789605..68efa1a 100644
@@ -9,3 +9,4 @@ obj-$(CONFIG_CNIC) += cnic.o
 obj-$(CONFIG_BNX2X) += bnx2x/
 obj-$(CONFIG_SB1250_MAC) += sb1250-mac.o
 obj-$(CONFIG_TIGON3) += tg3.o
+obj-$(CONFIG_BGMAC) += bgmac.o
drivers/net/ethernet/broadcom/bgmac.c new file +1390 -0

1,390 lines not on this page · raw message

drivers/net/ethernet/broadcom/bgmac.h new file +443 -0

443 lines not on this page · raw message

include/linux/bcma/bcma_driver_chipcommon.h +2 -0
index 9a0e3fa..ee332fa 100644
@@ -634,4 +634,6 @@ extern void bcma_chipco_regctl_maskset(struct bcma_drv_cc *cc,
 				       u32 offset, u32 mask, u32 set);
 extern void bcma_pmu_spuravoid_pllupdate(struct bcma_drv_cc *cc, int spuravoid);
 
+extern u32 bcma_pmu_get_bus_clock(struct bcma_drv_cc *cc);
+
 #endif /* LINUX_BCMA_DRIVER_CC_H_ */
Signature · 1 line
1.7.7
#1 lore raw
Quoted 4 lines · Rafał Miłecki

BCMA is a Broadcom specific bus with devices AKA cores. All recent BCMA based SoCs have gigabit ethernet provided by the GBit MAC core. This patch adds driver for such a cores registering itself as a netdev. It has been tested on a BCM4706 and BCM4718 chipsets.

Just a trivial note:

drivers/net/ethernet/broadcom/bgmac.h

Quoted 1 line
drivers/net/ethernet/broadcom/bgmac.h +0 -0

[]

Quoted 4 lines
drivers/net/ethernet/broadcom/bgmac.h +3 -0
@@ -0,0 +1,443 @@
+#ifndef _BGMAC_H
+#define _BGMAC_H
+

drivers/net/ethernet/broadcom/bgmac.h line 13

Quoted 10 lines
drivers/net/ethernet/broadcom/bgmac.h +10 -0
+#define pr_fmt(fmt)		KBUILD_MODNAME ": " fmt
+
+#define bgmac_err(bgmac, fmt, ...) \
+	pr_err("u%d: " fmt, (bgmac)->core->core_unit, ##__VA_ARGS__)
+#define bgmac_warn(bgmac, fmt, ...) \
+	pr_warn("u%d: " fmt, (bgmac)->core->core_unit, ##__VA_ARGS__)
+#define bgmac_info(bgmac, fmt, ...) \
+	pr_info("u%d: " fmt, (bgmac)->core->core_unit, ##__VA_ARGS__)
+#define bgmac_dbg(bgmac, fmt, ...) \
+	pr_debug("u%d: " fmt, (bgmac)->core->core_unit, ##__VA_ARGS__)

Maybe all the bgmac_<level> macros should be:

	dev_<level>(&bgmac->core->dev, "u%d: " fmt,
	            (bgmac)->core->core_unit, ##__VA_ARGS)

or maybe a new function like below if you use
a lot of these to decrease object size a little.

int bgmac_printk(const char *level, const struct bgmac *bgmac,

                 const char *fmt, ...)

{

	struct va_format vaf;
	va_list args;
	int r;

	va_start(args, fmt);

	vaf.fmt = fmt;
	vaf.va = &args;

	r = dev_printk(&bgmac->core->dev, "u%d: %pV",
	               bgmac->core->core_unit, &vaf);

	va_end(args);

	return r;
}
#2 lore raw
In reply to Rafał Miłecki

Rafał Miłecki <[email protected]> :
[...]

Quoted 1 line
drivers/net/ethernet/broadcom/bgmac.c +0 -0

[...]

drivers/net/ethernet/broadcom/bgmac.c line 53

Quoted 7 lines
drivers/net/ethernet/broadcom/bgmac.c +7 -0
+static void bgmac_dma_tx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
+{
+	u32 val;
+	int i;
+
+	if (!ring->mmio_base)
+		return;

static int bgmac_probe(struct bcma_device *core)
[...]

	bgmac_chip_reset(bgmac);

	err = bgmac_dma_alloc(bgmac);

mmio_base is only set in bgmac_dma_alloc.

-> remove the useless bgmac_chip_reset() in bgmac_probe() and the driver won't need to test for !ring->mmio_base.

drivers/net/ethernet/broadcom/bgmac.c

Quoted 2 lines
drivers/net/ethernet/broadcom/bgmac.c +2 -0
+
+	/* Susend DMA TX ring first.
	/* Suspend DMA TX ring first.

[...]

Quoted 4 lines
drivers/net/ethernet/broadcom/bgmac.c +4 -0
+static netdev_tx_t bgmac_dma_tx_add(struct bgmac *bgmac,
+				    struct bgmac_dma_ring *ring,
+				    struct sk_buff *skb)
+{

drivers/net/ethernet/broadcom/bgmac.c line 114

Quoted 10 lines
drivers/net/ethernet/broadcom/bgmac.c +10 -0
+	struct device *dma_dev = bgmac->core->dma_dev;
+	struct bgmac_dma_desc *dma_desc;
+	struct bgmac_slot_info *slot;
+	u32 ctl0, ctl1;
+	int free_slots;
+
+	if (skb->len > BGMAC_DESC_CTL1_LEN) {
+		bgmac_err(bgmac, "Too long skb (%d)\n", skb->len);
+		return NETDEV_TX_BUSY;
+	}

The driver will loop if you don't stop queuing. It won't help.

drivers/net/ethernet/broadcom/bgmac.c line 124

Quoted 10 lines
drivers/net/ethernet/broadcom/bgmac.c +10 -0
+
+	if (ring->start <= ring->end)
+		free_slots = ring->start - ring->end + BGMAC_TX_RING_SLOTS;
+	else
+		free_slots = ring->start - ring->end;
+	if (free_slots <= 1) {
+		bgmac_err(bgmac, "No free slots on ring 0x%X!\n", ring->mmio_base);
+		netif_stop_queue(bgmac->net_dev);
+		return NETDEV_TX_BUSY;
+	}

The driver should stop queueing after its processing if it detects that it can't handle more requests.

drivers/net/ethernet/broadcom/bgmac.c line 132

Quoted 8 lines
drivers/net/ethernet/broadcom/bgmac.c +8 -0
+
+	slot = &ring->slots[ring->end];
+	slot->skb = skb;
+	slot->dma_addr = dma_map_single(dma_dev, skb->data, skb->len, DMA_TO_DEVICE);
+	if (dma_mapping_error(dma_dev, slot->dma_addr)) {
+		bgmac_err(bgmac, "Mapping error of skb on ring 0x%X\n", ring->mmio_base);
+		return NETDEV_TX_BUSY;
+	}

Why don't you drop the packet and return TX_OK ?

drivers/net/ethernet/broadcom/bgmac.c line 140

Quoted 8 lines
drivers/net/ethernet/broadcom/bgmac.c +8 -0
+
+	ctl0 = BGMAC_DESC_CTL0_IOC | BGMAC_DESC_CTL0_SOF | BGMAC_DESC_CTL0_EOF;
+	if (ring->end == ring->num_slots - 1)
+		ctl0 |= BGMAC_DESC_CTL0_EOT;
+	ctl1 = skb->len & BGMAC_DESC_CTL1_LEN;
+
+	dma_desc = ring->cpu_base;
+	dma_desc += ring->end;
	struct bgmac_dma_desc *dma_desc = ring->cpu_base + ring->end;

drivers/net/ethernet/broadcom/bgmac.c line 146

Quoted 6 lines
drivers/net/ethernet/broadcom/bgmac.c +6 -0
+	dma_desc->addr_low = cpu_to_le32(lower_32_bits(slot->dma_addr));
+	dma_desc->addr_high = cpu_to_le32(upper_32_bits(slot->dma_addr));
+	dma_desc->ctl0 = cpu_to_le32(ctl0);
+	dma_desc->ctl1 = cpu_to_le32(ctl1);
+
+	wmb();

Are we sure that the hardware will never read a descriptor where ctl0 is set and ctl1 is not (start_xmit, dma starts, competing start_xmit, oops) ?

drivers/net/ethernet/broadcom/bgmac.c line 149

Quoted 3 lines
drivers/net/ethernet/broadcom/bgmac.c +3 -0
+
+	/* Increase ring->end to point empty slot. We tell hardware the first
+	 * slot it shold *not* read.

* slot it should *not* read.

[...]

drivers/net/ethernet/broadcom/bgmac.c line 163

Quoted 4 lines
drivers/net/ethernet/broadcom/bgmac.c +4 -0
+static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
+{
+	struct device *dma_dev = bgmac->core->dma_dev;
+	struct bgmac_dma_desc *dma_desc;

The scope of 'slot' and 'dma_desc' can be reduced.

Quoted 12 lines
drivers/net/ethernet/broadcom/bgmac.c +12 -0
+	struct bgmac_slot_info *slot;
+	int empty_slot;
+
+	/* The last slot that hardware didn't consume yet */
+	empty_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
+	empty_slot &= BGMAC_DMA_TX_STATDPTR;
+	empty_slot /= sizeof(struct bgmac_dma_desc);
+
+	while (ring->start != empty_slot) {
+		/* Set pointers */
+		dma_desc = ring->cpu_base;
+		dma_desc += ring->start;

drivers/net/ethernet/broadcom/bgmac.c line 185

Quoted 10 lines
drivers/net/ethernet/broadcom/bgmac.c +10 -0
+		slot = &ring->slots[ring->start];
+
+		if (slot->skb) {
+			/* Unmap no longer used buffer */
+			dma_unmap_single(dma_dev, slot->dma_addr,
+					 slot->skb->len, DMA_TO_DEVICE);
+			slot->dma_addr = 0;
+
+			/* Free memory! :) */
+			dev_kfree_skb_any(slot->skb);

This is irq enabled NAPI poll context so you can dev_kfree_skb.

Quoted 4 lines
drivers/net/ethernet/broadcom/bgmac.c +4 -0
+			slot->skb = NULL;
+		} else {
+			bgmac_err(bgmac, "Hardware reported transmission for empty TX ring slot %d! End of ring: %d", ring->start, ring->end);
+		}

drivers/net/ethernet/broadcom/bgmac.c line 199

Quoted 10 lines
drivers/net/ethernet/broadcom/bgmac.c +10 -0
+
+		if (++ring->start >= BGMAC_TX_RING_SLOTS)
+			ring->start = 0;
+	}
+}
+
+static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
+{
+	if (!ring->mmio_base)
+		return;

See remark about bgmac_dma_tx_reset.

[...]

drivers/net/ethernet/broadcom/bgmac.c line 264

Quoted 11 lines
drivers/net/ethernet/broadcom/bgmac.c +11 -0
+static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
+			     int weight)
+{
+	struct device *dma_dev = bgmac->core->dma_dev;
+	struct bgmac_dma_desc *dma_desc;
+	struct bgmac_slot_info *slot;
+	struct sk_buff *skb, *new_skb;
+	struct bgmac_rx_header *rx;
+	u32 end_slot;
+	u16 len, flags;
+	int handled = 0;

The scope of several variables can be reduced as well.

Signature · 1 line
Ueimor
author maintainer
#3 lore raw

Thanks for your comments, I'll improve the driver/patch.

2012/12/27 Francois Romieu <[email protected]>:

Quoted 10 lines

drivers/net/ethernet/broadcom/bgmac.c line 264

Quoted 8 lines
drivers/net/ethernet/broadcom/bgmac.c new file +8 -0
+     struct device *dma_dev = bgmac->core->dma_dev;
+     struct bgmac_dma_desc *dma_desc;
+     struct bgmac_slot_info *slot;
+     struct sk_buff *skb, *new_skb;
+     struct bgmac_rx_header *rx;
+     u32 end_slot;
+     u16 len, flags;
+     int handled = 0;

The scope of several variables can be reduced as well.

I'm not 100% if I understand... Do you mean I should declare variables inside "while" loop when possible? I couldn't find suggestions about anywhere in Documentation, so please give me an extra work of explanation :)

Signature · 1 line
Rafał
#4 lore raw

Rafał Miłecki <[email protected]> :
[...]

Quoted 2 lines

I'm not 100% sure if I understand... Do you mean I should declare variables inside "while" loop when possible ?

Yes. Please consider the deepest inner block.

While at it:

Quoted 1 line
drivers/net/ethernet/broadcom/bgmac.c +0 -0

[...]

Quoted 1 line

+static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,

[...]

Quoted 6 lines
drivers/net/ethernet/broadcom/bgmac.c +6 -0
+		/* Check for poison and drop or pass the packet */
+		if (len == 0xdead && flags == 0xbeef) {
+			bgmac_err(bgmac, "Found poisoned packet at slot %d, DMA issue!\n", ring->start);
+		} else {
+			new_skb = netdev_alloc_skb(bgmac->net_dev, len);
+			if (new_skb) {

drivers/net/ethernet/broadcom/bgmac.c line 303

Quoted 10 lines
drivers/net/ethernet/broadcom/bgmac.c +10 -0
+				skb_put(new_skb, len);
+				skb_copy_from_linear_data_offset(skb, BGMAC_RX_FRAME_OFFSET,
+								 new_skb->data, len);
+				new_skb->protocol =
+					eth_type_trans(new_skb, bgmac->net_dev);
+				netif_receive_skb(new_skb);
+				handled++;
+			} else {
+				bgmac_err(bgmac, "Allocation of skb for copying packet failed!\n");
+			}

You should increase bgmac->net_dev->stats.rx_dropped

[...]

drivers/net/ethernet/broadcom/bgmac.c line 383

Quoted 6 lines
drivers/net/ethernet/broadcom/bgmac.c +6 -0
+static int bgmac_dma_alloc(struct bgmac *bgmac)
+{
+	struct device *dma_dev = bgmac->core->dma_dev;
+	struct bgmac_dma_ring *ring;
+	u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1, BGMAC_DMA_BASE2,
+			    BGMAC_DMA_BASE3, };

ring_base could be 'static const'.

Quoted 9 lines
drivers/net/ethernet/broadcom/bgmac.c +9 -0
+	int size; /* ring size: different for Tx and Rx */
+	int err;
+	int i;
+
+	BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base));
+	BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base));
+
+	if (!(bcma_aread32(bgmac->core, BCMA_IOST) & BCMA_IOST_DMA64)) {
+		bgmac_err(bgmac, "Core does not report 64-bit DMA\n");

drivers/net/ethernet/broadcom/bgmac.c line 402

Quoted 10 lines
drivers/net/ethernet/broadcom/bgmac.c +10 -0
+		return -ENOTSUPP;
+	}
+
+	for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
+		ring = &bgmac->tx_ring[i];
+		ring->tx = true;
+		ring->num_slots = BGMAC_TX_RING_SLOTS;
+		ring->mmio_base = ring_base[i];
+		if (bgmac_dma_unaligned(bgmac, ring))
+			bgmac_warn(bgmac, "TX on ring 0x%X supports unaligned addressing but this feature is not implemented\n", ring->mmio_base);

ring->tx is used once in bgmac_dma_unaligned.

Add bgmac_dma_unaligned an extra parameter for the BGMAC_DMA_[RT]X_RINGLO offset and ring->[rt]x can go away.

drivers/net/ethernet/broadcom/bgmac.c line 407

Quoted 5 lines
drivers/net/ethernet/broadcom/bgmac.c +5 -0
+
+		/* Alloc ring of descriptors */
+		size = ring->num_slots * sizeof(struct bgmac_dma_desc);
+		ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
+						     &(ring->dma_base),

Plese remove the useless parenthesis.

[...]

Quoted 4 lines
drivers/net/ethernet/broadcom/bgmac.c +4 -0
+static void bgmac_chip_stats_update(struct bgmac *bgmac)
+{
+	int i;
+

drivers/net/ethernet/broadcom/bgmac.c line 757

Quoted 10 lines
drivers/net/ethernet/broadcom/bgmac.c +10 -0
+	if (bgmac->core->id.id != BCMA_CORE_4706_MAC_GBIT) {
+		for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
+			bgmac->mib_tx_regs[i] =
+				bgmac_read(bgmac,
+					   BGMAC_TX_GOOD_OCTETS + (i * 4));
+		for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
+			bgmac->mib_rx_regs[i] =
+				bgmac_read(bgmac,
+					   BGMAC_RX_GOOD_OCTETS + (i * 4));
+	}

Neither of mib_[rt]x_regs is ever read.

[...]

Quoted 2 lines
drivers/net/ethernet/broadcom/bgmac.c +2 -0
+static void bgmac_chip_reset(struct bgmac *bgmac)
+{

[...]

drivers/net/ethernet/broadcom/bgmac.c line 860

Quoted 8 lines
drivers/net/ethernet/broadcom/bgmac.c +8 -0
+	if (ci->id == BCMA_CHIP_ID_BCM5357 || ci->id == BCMA_CHIP_ID_BCM4749 ||
+	    ci->id == BCMA_CHIP_ID_BCM53572) {
+		struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
+		u8 et_swtype = 0;
+		u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY |
+			     BGMAC_CHIPCTL_1_IF_TYPE_RMII;
+		char buf[2];
+		if (nvram_getenv("et_swtype", buf, 1) > 0) {

Please keep an empty line between variables declaration and body.

[...]

Quoted 6 lines
drivers/net/ethernet/broadcom/bgmac.c +6 -0
+static irqreturn_t bgmac_interrupt(int irq, void *dev_id)
+{
+	struct bgmac *bgmac = netdev_priv(dev_id);
+
+	u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS);
+	int_status &= bgmac->int_mask;

...

[...]

drivers/net/ethernet/broadcom/bgmac.c line 1095

Quoted 10 lines
drivers/net/ethernet/broadcom/bgmac.c +10 -0
+/**************************************************
+ * net_device_ops
+ **************************************************/
+
+static int bgmac_open(struct net_device *net_dev)
+{
+	struct bgmac *bgmac = netdev_priv(net_dev);
+
+	/* Reset */
+	bgmac_chip_reset(bgmac);

Please remove the useless comment(s).

drivers/net/ethernet/broadcom/bgmac.c line 1105

Quoted 10 lines
drivers/net/ethernet/broadcom/bgmac.c +10 -0
+	/* Specs say about reclaiming rings here, but we do that in DMA init */
+	bgmac_chip_init(bgmac, true);
+
+	/* Enable IRQs */
+	if (request_irq(bgmac->core->irq, bgmac_interrupt, IRQF_SHARED,
+			KBUILD_MODNAME, net_dev) < 0)
+		bgmac_err(bgmac, "IRQ request error!\n");
+	napi_enable(&bgmac->napi);
+
+	return 0;

bgmac_open should not succeed when request_irq fails. Please propagate its error status code.

[...]

Quoted 33 lines
drivers/net/ethernet/broadcom/bgmac.c +33 -0
+static int bgmac_get_settings(struct net_device *net_dev,
+			      struct ethtool_cmd *cmd)
+{
+	struct bgmac *bgmac = netdev_priv(net_dev);
+
+	cmd->supported = SUPPORTED_10baseT_Half |
+			 SUPPORTED_10baseT_Full |
+			 SUPPORTED_100baseT_Half |
+			 SUPPORTED_100baseT_Full |
+			 SUPPORTED_1000baseT_Half |
+			 SUPPORTED_1000baseT_Full |
+			 SUPPORTED_Autoneg;
+
+	if (bgmac->autoneg) {
+		WARN_ON(cmd->advertising);
+		if (bgmac->full_duplex) {
+			if (bgmac->speed & BGMAC_SPEED_10)
+				cmd->advertising |= ADVERTISED_10baseT_Full;
+			if (bgmac->speed & BGMAC_SPEED_100)
+				cmd->advertising |= ADVERTISED_100baseT_Full;
+			if (bgmac->speed & BGMAC_SPEED_1000)
+				cmd->advertising |= ADVERTISED_1000baseT_Full;
+		} else {
+			if (bgmac->speed & BGMAC_SPEED_10)
+				cmd->advertising |= ADVERTISED_10baseT_Half;
+			if (bgmac->speed & BGMAC_SPEED_100)
+				cmd->advertising |= ADVERTISED_100baseT_Half;
+			if (bgmac->speed & BGMAC_SPEED_1000)
+				cmd->advertising |= ADVERTISED_1000baseT_Half;
+		}
+	} else {
+		switch (bgmac->speed) {
+		case BGMAC_SPEED_10:

drivers/net/ethernet/broadcom/bgmac.c line 1214

Quoted 10 lines
drivers/net/ethernet/broadcom/bgmac.c +10 -0
+			ethtool_cmd_speed_set(cmd, SPEED_10);
+			break;
+		case BGMAC_SPEED_100:
+			ethtool_cmd_speed_set(cmd, SPEED_100);
+			break;
+		case BGMAC_SPEED_1000:
+			ethtool_cmd_speed_set(cmd, SPEED_1000);
+			break;
+		}
+	}

It should not take long for users to complain that the driver does not report the current link speed. A struct mii_if_info will ease your life.

Btw the driver does not manage the link (netif_carrier_{on/off}).

[...]

Quoted 1 line
drivers/net/ethernet/broadcom/bgmac.h +0 -0

[...]

drivers/net/ethernet/broadcom/bgmac.h line 372

Quoted 8 lines
drivers/net/ethernet/broadcom/bgmac.h +8 -0
+struct bgmac_dma_ring {
+	bool tx;
+	u16 num_slots;
+	u16 start;
+	u16 end;
+
+	u16 mmio_base;
+	void *cpu_base;

struct bgmac_dma_desc *cpu_base; ?

Signature · 1 line
Ueimor
author maintainer
#5 lore raw
In reply to Francois Romieu

2012/12/27 Francois Romieu <[email protected]>:

Quoted 14 lines

Rafał Miłecki <[email protected]> :
[...]

Quoted 1 line
drivers/net/ethernet/broadcom/bgmac.c +0 -0

[...]

drivers/net/ethernet/broadcom/bgmac.c line 53

Quoted 7 lines
drivers/net/ethernet/broadcom/bgmac.c +7 -0
+static void bgmac_dma_tx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
+{
+     u32 val;
+     int i;
+
+     if (!ring->mmio_base)
+             return;

static int bgmac_probe(struct bcma_device *core)
[...]

Quoted 8 lines
        bgmac_chip_reset(bgmac);

        err = bgmac_dma_alloc(bgmac);

mmio_base is only set in bgmac_dma_alloc.

-> remove the useless bgmac_chip_reset() in bgmac_probe() and the driver won't need to test for !ring->mmio_base.

bgmac_chip_reset also does different things, so I can't just remove bgmac_chip_reset from bgmac_probe. I could add some parameter to bgmac_chip_reset to let it know if DMA should be reset as well, but I'm note sure about the advantage.

Quoted 6 lines

drivers/net/ethernet/broadcom/bgmac.c line 114

Quoted 4 lines
drivers/net/ethernet/broadcom/bgmac.c +4 -0
+     if (skb->len > BGMAC_DESC_CTL1_LEN) {
+             bgmac_err(bgmac, "Too long skb (%d)\n", skb->len);
+             return NETDEV_TX_BUSY;
+     }

The driver will loop if you don't stop queuing. It won't help.

I hoped net layer will drop the packed after few failed xmit. OK, I'll add netif_stop_queue here.

Quoted 11 lines

drivers/net/ethernet/broadcom/bgmac.c line 124

Quoted 10 lines
drivers/net/ethernet/broadcom/bgmac.c +10 -0
+
+     if (ring->start <= ring->end)
+             free_slots = ring->start - ring->end + BGMAC_TX_RING_SLOTS;
+     else
+             free_slots = ring->start - ring->end;
+     if (free_slots <= 1) {
+             bgmac_err(bgmac, "No free slots on ring 0x%X!\n", ring->mmio_base);
+             netif_stop_queue(bgmac->net_dev);
+             return NETDEV_TX_BUSY;
+     }
Quoted 2 lines

The driver should stop queueing after its processing if it detects that it can't handle more requests.

I think it's what the code does... netif_stop_queue and return NETDEV_TX_BUSY. Anything wrong about it?

Quoted 9 lines

drivers/net/ethernet/broadcom/bgmac.c line 132

Quoted 7 lines
drivers/net/ethernet/broadcom/bgmac.c +7 -0
+     slot = &ring->slots[ring->end];
+     slot->skb = skb;
+     slot->dma_addr = dma_map_single(dma_dev, skb->data, skb->len, DMA_TO_DEVICE);
+     if (dma_mapping_error(dma_dev, slot->dma_addr)) {
+             bgmac_err(bgmac, "Mapping error of skb on ring 0x%X\n", ring->mmio_base);
+             return NETDEV_TX_BUSY;
+     }

Why don't you drop the packet and return TX_OK ?

Didn't know about that and there isn't any nice NAPI tutorial for developers :(

Quoted 9 lines

drivers/net/ethernet/broadcom/bgmac.c line 146

Quoted 6 lines
drivers/net/ethernet/broadcom/bgmac.c +6 -0
+     dma_desc->addr_low = cpu_to_le32(lower_32_bits(slot->dma_addr));
+     dma_desc->addr_high = cpu_to_le32(upper_32_bits(slot->dma_addr));
+     dma_desc->ctl0 = cpu_to_le32(ctl0);
+     dma_desc->ctl1 = cpu_to_le32(ctl1);
+
+     wmb();

Are we sure that the hardware will never read a descriptor where ctl0 is set and ctl1 is not (start_xmit, dma starts, competing start_xmit, oops) ?

Do you mean more that one ->ndo_start_xmit(...) can be executed at a time? So I need mutex for it?

Signature · 1 line
Rafał
#6 lore raw

Rafał Miłecki <[email protected]> :
[...]

Quoted 4 lines

bgmac_chip_reset also does different things, so I can't just remove bgmac_chip_reset from bgmac_probe. I could add some parameter to bgmac_chip_reset to let it know if DMA should be reset as well, but I'm note sure about the advantage.

Forget it. I was completely wrong.

[...]

Quoted 5 lines
Quoted 2 lines

The driver should stop queueing after its processing if it detects that it can't handle more requests.

I think it's what the code does... netif_stop_queue and return NETDEV_TX_BUSY. Anything wrong about it?

The driver does not hold its promise to send the packet. It should netif_stop_queue and return NETDEV_TX_OK after the last transmitted packet.

[...]

Quoted 5 lines
Quoted 2 lines

Are we sure that the hardware will never read a descriptor where ctl0 is set and ctl1 is not (start_xmit, dma starts, competing start_xmit, oops) ?

Do you mean more that one ->ndo_start_xmit(...) can be executed at a time ?

No. I was just wondering if a sequence of start_xmit could compete with hardware DMA but I missed the BGMAC_DMA_TX_INDEX below. You may want a mmiowb after the write to BGMAC_DMA_TX_INDEX though.

Signature · 1 line
Ueimor
author maintainer
#7 lore raw

2013/1/3 Francois Romieu <[email protected]>:

Quoted 10 lines

Rafał Miłecki <[email protected]> :

Quoted 5 lines
Quoted 2 lines

The driver should stop queueing after its processing if it detects that it can't handle more requests.

I think it's what the code does... netif_stop_queue and return NETDEV_TX_BUSY. Anything wrong about it?

The driver does not hold its promise to send the packet. It should netif_stop_queue and return NETDEV_TX_OK after the last transmitted packet.

We really need some good explanation for NETDEV_TX_BUSY somewhere (documentation!).

I've checked some drivers and:

1) b44.c
In case of:

Quoted 1 line

if (unlikely(TX_BUFFS_AVAIL(bp) < 1)) {

it does netif_stop_queue and returns NETDEV_TX_BUSY

2) sky2.c
In case of:

Quoted 1 line

if (unlikely(tx_avail(sky2) < tx_le_req(skb)))

it just returns NETDEV_TX_BUSY

3) jme.2
In case of:

Quoted 1 line

if (unlikely(idx < 0)) {

it does netif_stop_queue and returns NETDEV_TX_BUSY

4) de2104x.c
In case of:

Quoted 1 line

if (tx_free == 0) {

it does netif_stop_queue and returns NETDEV_TX_BUSY

So there are many drivers doing the same mistake I did.

Signature · 1 line
Rafał
#8 lore raw

Rafał Miłecki <[email protected]> :
[...]

Quoted 2 lines

We really need some good explanation for NETDEV_TX_BUSY somewhere (documentation!).

It is already explained in Documentation/networking/driver.txt:
<snip>
1) The ndo_start_xmit method must not return NETDEV_TX_BUSY under any normal circumstances. It is considered a hard error unless there is no way your device can tell ahead of time when it's transmit function will become busy.

Instead it must maintain the queue properly. For example, for a driver implementing scatter-gather this means:
</snip>

Please compare your V3:bgmac_dma_tx_add and the sample code after the aforementioned paragraph.

Quoted 6 lines

I've checked some drivers and:

1) b44.c
In case of:

Quoted 1 line

if (unlikely(TX_BUFFS_AVAIL(bp) < 1)) {

it does netif_stop_queue and returns NETDEV_TX_BUSY

The driver labels it as bug checking and it stops queueing when need arises at the end of the xmit function.

Quoted 4 lines

2) sky2.c
In case of:

Quoted 1 line

if (unlikely(tx_avail(sky2) < tx_le_req(skb)))

it just returns NETDEV_TX_BUSY

Same thing as above. Less documented though.

Quoted 4 lines

3) jme.2
In case of:

Quoted 1 line

if (unlikely(idx < 0)) {

it does netif_stop_queue and returns NETDEV_TX_BUSY

The driver labels it as bug checking. It stops queueing when need arises at the end of the xmit function (see jme_stop_queue_if_full).

Quoted 4 lines

4) de2104x.c
In case of:

Quoted 1 line

if (tx_free == 0) {

it does netif_stop_queue and returns NETDEV_TX_BUSY

It stops queueing at the end of the start_xmit handler as well.

Quoted 1 line

So there are many drivers doing the same mistake I did.

After a packet has been handed to hardware, each of these drivers checks if queueing should be disabled before returning from start_xmit.

V3 bgmac doesn't.

Signature · 1 line
Ueimor
author maintainer
#9 lore raw

2013/1/5 Francois Romieu <[email protected]>:

Quoted 49 lines

Rafał Miłecki <[email protected]> :
[...]

Quoted 2 lines

We really need some good explanation for NETDEV_TX_BUSY somewhere (documentation!).

It is already explained in Documentation/networking/driver.txt:
<snip>
1) The ndo_start_xmit method must not return NETDEV_TX_BUSY under any normal circumstances. It is considered a hard error unless there is no way your device can tell ahead of time when it's transmit function will become busy.

Instead it must maintain the queue properly. For example, for a driver implementing scatter-gather this means:
</snip>

Please compare your V3:bgmac_dma_tx_add and the sample code after the aforementioned paragraph.

Quoted 6 lines

I've checked some drivers and:

1) b44.c
In case of:

Quoted 1 line

if (unlikely(TX_BUFFS_AVAIL(bp) < 1)) {

it does netif_stop_queue and returns NETDEV_TX_BUSY

The driver labels it as bug checking and it stops queueing when need arises at the end of the xmit function.

Quoted 4 lines

2) sky2.c
In case of:

Quoted 1 line

if (unlikely(tx_avail(sky2) < tx_le_req(skb)))

it just returns NETDEV_TX_BUSY

Same thing as above. Less documented though.

Quoted 4 lines

3) jme.2
In case of:

Quoted 1 line

if (unlikely(idx < 0)) {

it does netif_stop_queue and returns NETDEV_TX_BUSY

The driver labels it as bug checking. It stops queueing when need arises at the end of the xmit function (see jme_stop_queue_if_full).

Quoted 4 lines

4) de2104x.c
In case of:

Quoted 1 line

if (tx_free == 0) {

it does netif_stop_queue and returns NETDEV_TX_BUSY

Quoted 8 lines

It stops queueing at the end of the start_xmit handler as well.

Quoted 1 line

So there are many drivers doing the same mistake I did.

After a packet has been handed to hardware, each of these drivers checks if queueing should be disabled before returning from start_xmit.

V3 bgmac doesn't.

How did I miss that?! Thanks for pointing that out!

Signature · 1 line
Rafał