Skip to contentSkip to conversation

Driver for Beckhoff CX5020 EtherCAT master module.

PATCH Superseded Darek Marcinkiewicz 2 participants 3 messages May 1, 2014 – May 2, 2014

Subsystems (2)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 Driver for Beckhoff CX5020 EtherCAT master module.

Changes since v3:
* some clarificatoins around buffer allocations

Changes since v2:
* removed all checkpatch warnings
* driver makes use of device rx fifo

Changes since v1:
* added endianess annotation to descriptors' structures
* killed checkpath warnings about string literals being split into multiple lines

Quoted 1 line

8----------------------------------------8<

This driver adds support for EtherCAT master module located on CCAT FPGA found on Beckhoff CX series industrail PCs. The driver exposes EtherCAT master as an ethernet interface.

EtherCAT is a filedbus protocol defined on top of ethernet and Beckhoff CX5020 PCs come with built-in EtherCAT master module located on a FPGA, which in turn is connected to a PCI bus.

---
 drivers/net/ethernet/Kconfig  |   11 +
 drivers/net/ethernet/Makefile |    1 +
 drivers/net/ethernet/ec_bh.c  |  785 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 797 insertions(+)
 create mode 100644 drivers/net/ethernet/ec_bh.c
drivers/net/ethernet/Kconfig +11 -0
index 39b26fe..a81611d 100644
@@ -35,6 +35,17 @@ source "drivers/net/ethernet/calxeda/Kconfig"
 source "drivers/net/ethernet/chelsio/Kconfig"
 source "drivers/net/ethernet/cirrus/Kconfig"
 source "drivers/net/ethernet/cisco/Kconfig"
+
+config CX_ECAT
+	tristate "Beckhoff CX5020 EtherCAT master support"
+	---help---
+	  Driver for EtherCAT master module located on CCAT FPGA
+	  that can be found on Beckhoff CX5020, and possibly other of CX
+	  Beckhoff CX series industrial PCs.
+
+	  To compile this driver as a module, choose M here. The module
+	  will be called ec_bh.
+
 source "drivers/net/ethernet/davicom/Kconfig"
 
 config DNET
drivers/net/ethernet/Makefile +1 -0
index 545d0b3..1712c87 100644
@@ -21,6 +21,7 @@ obj-$(CONFIG_NET_CALXEDA_XGMAC) += calxeda/
 obj-$(CONFIG_NET_VENDOR_CHELSIO) += chelsio/
 obj-$(CONFIG_NET_VENDOR_CIRRUS) += cirrus/
 obj-$(CONFIG_NET_VENDOR_CISCO) += cisco/
+obj-$(CONFIG_CX_ECAT) += ec_bh.o
 obj-$(CONFIG_DM9000) += davicom/
 obj-$(CONFIG_DNET) += dnet.o
 obj-$(CONFIG_NET_VENDOR_DEC) += dec/
drivers/net/ethernet/ec_bh.c new file +785 -0

785 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#1 lore raw

Darek Marcinkiewicz <[email protected]> :
[changes]

(you may add those after the "---" above the diffstat)

[...]

Quoted 5 lines
drivers/net/ethernet/ec_bh.c new file +0 -0
new file mode 100644
index 0000000..2ed2cee

[...]

Quoted 1 line

+#define TX_HEADER_SIZE 16

sizeof() ?

Quoted 1 line

+#define MAX_TX_BODY_SIZE 1518

s/1518/ETH_FRAME_LEN + ETH_FCS_LEN/ ?

drivers/net/ethernet/ec_bh.c line 119

Quoted 7 lines
drivers/net/ethernet/ec_bh.c new file +7 -0
+#define MAX_TX_PKT_SIZE		(MAX_TX_BODY_SIZE + TX_HEADER_SIZE)
+
+#define FIFO_SIZE		64
+
+static long polling_frequency = TIMER_INTERVAL_NSEC;
+
+struct bh_priv {

Nit: it would be nice to avoid "_bh_" as it is already used in a set of common kernel functions.

[...]

drivers/net/ethernet/ec_bh.c line 153

Quoted 2 lines
drivers/net/ethernet/ec_bh.c new file +2 -0
+	u8 *tx_put;
+	u8 *tx_get;

It's part u8 and part tx_header.

You may consider using a struct that adds an extra 'u8 data[0]' at the end of the struct tx_header.

skb_copy_and_csum_dev in the xmit path rings a bell but the DMA FIFO zone handling could imho save some pointer arithmetic and turn a bit more literate.

[...]

drivers/net/ethernet/ec_bh.c line 170

Quoted 4 lines
drivers/net/ethernet/ec_bh.c new file +4 -0
+static void ec_bh_print_status(struct bh_priv *priv)
+{
+	dev_info(PRIV_TO_DEV(priv),
+		 "Frame error counter:%d\n",

- Excess new line. The message always fits within the (less than) 80 columns limit. There are several occurences of it in the driver.

- You may add a local variable to save the numerous PRIV_TO_DEV(priv).

- A space between the ":" and the "%" should help reading.

[...]

drivers/net/ethernet/ec_bh.c line 218

Quoted 5 lines
drivers/net/ethernet/ec_bh.c new file +5 -0
+static void ec_bh_send_packet(struct bh_priv *priv)
+{
+	struct tx_header *header = (struct tx_header *)priv->tx_get;
+	u32 addr = priv->tx_get - priv->tx;
+	u32 len = le16_to_cpu(header->len) + sizeof(*header);

Please reorder so as to get an inverted xmas tree. You should do it wherever it can be done in the driver.

drivers/net/ethernet/ec_bh.c line 221

Quoted 3 lines
drivers/net/ethernet/ec_bh.c new file +3 -0
+
+	iowrite32((((len + 8) / 8) << 24) | addr,
+			priv->fifo_io + FIFO_TX_REG);
	iowrite32(addr | (((len + 8) / 8) << 24), priv->fifo_io + FIFO_TX_REG);

[...]

drivers/net/ethernet/ec_bh.c line 235

Quoted 10 lines
drivers/net/ethernet/ec_bh.c new file +10 -0
+static void ec_bh_process_tx(struct bh_priv *priv)
+{
+	int sent;
+	struct tx_header *header;
+	u8 *pkt_end, *next_pkt;
+
+	if (!priv->tx_in_flight)
+		return;
+
+	header = (struct tx_header *)priv->tx_get;

You may merge it with the variable declaration.

drivers/net/ethernet/ec_bh.c line 241

Quoted 6 lines
drivers/net/ethernet/ec_bh.c new file +6 -0
+	sent = le32_to_cpu(header->sent) & TX_HDR_SENT;
+	if (!sent)
+		return;
+
+	pkt_end = priv->tx_get;
+	pkt_end += (sizeof(*header) + le16_to_cpu(header->len) + 7) / 8 * 8;

ALIGN() ?

[...]

drivers/net/ethernet/ec_bh.c line 256

Quoted 3 lines
drivers/net/ethernet/ec_bh.c new file +3 -0
+	if (netif_queue_stopped(priv->net_dev) &&
+		(priv->tx_put > priv->tx_get
+			|| priv->tx_put + MAX_TX_PKT_SIZE < priv->tx_get)) {

|| should be a the end of the preceding line

[...]

drivers/net/ethernet/ec_bh.c line 280

Quoted 8 lines
drivers/net/ethernet/ec_bh.c new file +8 -0
+static void ec_bh_process_rx(struct bh_priv *priv)
+{
+	struct rx_desc *desc = priv->rx_desc;
+
+	while (ec_bh_pkt_received(desc)) {
+		int pkt_size = (le16_to_cpu(desc->header.len) & RXHDR_LEN_MASK)
+			       - sizeof(struct rx_header)
+			       - 4;

The operator should be at the end of the preceding line.

drivers/net/ethernet/ec_bh.c line 285

Quoted 5 lines
drivers/net/ethernet/ec_bh.c new file +5 -0
+		u8 *data = desc->data;
+		struct sk_buff *skb;
+
+		skb = netdev_alloc_skb_ip_align(priv->net_dev, pkt_size);
+		dev_dbg(PRIV_TO_DEV(priv),

You may use local variable instead of repeating (capitalized) macro.

[...]

drivers/net/ethernet/ec_bh.c line 328

Quoted 10 lines
drivers/net/ethernet/ec_bh.c new file +10 -0
+static enum hrtimer_restart ec_bh_timer_fun(struct hrtimer *timer)
+{
+	struct bh_priv *priv = container_of(timer,
+					struct bh_priv,
+					hrtimer);
+	unsigned long flags;
+
+	spin_lock_irqsave(&priv->lock, flags);
+
+	ec_bh_process_rx(priv);

The Rx part should not race with anything. You should thus push the locked section into ec_bh_process_tx (then narrow it).

drivers/net/ethernet/ec_bh.c line 334

Quoted 6 lines
drivers/net/ethernet/ec_bh.c new file +6 -0
+	ec_bh_process_tx(priv);
+
+	spin_unlock_irqrestore(&priv->lock, flags);
+
+	if (atomic_read(&priv->shutting_down))
+		return HRTIMER_NORESTART;

You can use 'netif_running' and remove 'shutting_down'.

[...]

Quoted 15 lines
drivers/net/ethernet/ec_bh.c new file +15 -0
+static netdev_tx_t ec_bh_start_xmit(struct sk_buff *skb,
+				    struct net_device *net_dev)
+{
+	unsigned long flags;
+	unsigned len;
+	struct bh_priv *priv = netdev_priv(net_dev);
+	struct tx_header *header = (struct tx_header *)priv->tx_put;
+	u8 *tx = priv->tx_put + sizeof(struct tx_header);
+
+	spin_lock_irqsave(&priv->lock, flags);
+
+	dev_dbg(PRIV_TO_DEV(priv), "Starting xmit\n");
+
+	/* Drop packets that are too large or have no chance
+	 * to be a ethercat packets (cause they are too small)

drivers/net/ethernet/ec_bh.c line 429

Quoted 10 lines
drivers/net/ethernet/ec_bh.c new file +10 -0
+	 */
+	if (unlikely(skb->len > priv->tx_len
+			|| skb->len < 13)) {
+		net_dev->stats.tx_dropped++;
+		dev_info(PRIV_TO_DEV(priv), "Dropping packet\n");
+		goto out_unlock;
+	}
+
+	skb_copy_and_csum_dev(skb, tx);
+	len = skb->len;

Up to this point the lock is not required (ec_bh_process_tx does not modifiy tx_put).

Bonus: 'out_unlock' turns into 'out' and you can narrow the locked section at the end of the function.

Quoted 14 lines
drivers/net/ethernet/ec_bh.c new file +14 -0
+
+	memset(header, 0, sizeof(*header));
+	header->len = cpu_to_le16(len);
+	header->port = TX_HDR_PORT_0;
+	mmiowb();
+
+	if (!priv->tx_in_flight) {
+		ec_bh_send_packet(priv);
+		priv->tx_in_flight = 1;
+	}
+
+	priv->tx_put += (TX_HEADER_SIZE + len + 7) / 8 * 8;
+	if (priv->tx_put + MAX_TX_PKT_SIZE > priv->tx + priv->tx_len)
+		priv->tx_put = priv->tx;

drivers/net/ethernet/ec_bh.c line 453

Quoted 10 lines
drivers/net/ethernet/ec_bh.c new file +10 -0
+
+	if (priv->tx_put <= priv->tx_get
+			&& priv->tx_put + MAX_TX_PKT_SIZE > priv->tx_get) {
+		dev_info(PRIV_TO_DEV(priv), "Stopping netif queue\n");
+		ec_bh_print_status(priv);
+		netif_stop_queue(net_dev);
+	}
+
+	priv->net_dev->stats.tx_bytes += len;
+	priv->net_dev->stats.tx_packets++;

ec_bh_process_tx does not care about Tx error. Can it do better or is it unable to ?

drivers/net/ethernet/ec_bh.c line 462

Quoted 9 lines
drivers/net/ethernet/ec_bh.c new file +9 -0
+out_unlock:
+	spin_unlock_irqrestore(&priv->lock, flags);
+
+	dev_kfree_skb(skb);
+
+	return NETDEV_TX_OK;
+}
+
+void *ec_bh_alloc_dma_mem(struct bh_priv *priv,

static

[...]

drivers/net/ethernet/ec_bh.c line 493

Quoted 3 lines
drivers/net/ethernet/ec_bh.c new file +3 -0
+	*buf = pci_alloc_consistent(priv->dev,
+				    *buf_len,
+				    phys_buf);

Go for dma_alloc_coherent(..., GFP_KERNEL) once you've removed the spinlock in the open method.

[...]

drivers/net/ethernet/ec_bh.c line 520

Quoted 10 lines
drivers/net/ethernet/ec_bh.c new file +10 -0
+static int ec_bh_open(struct net_device *net_dev)
+{
+	int err = 0, i;
+	unsigned long flags;
+	struct rx_desc *desc;
+	struct bh_priv *priv = netdev_priv(net_dev);
+
+	dev_info(PRIV_TO_DEV(priv), "Opening device\n");
+
+	spin_lock_irqsave(&priv->lock, flags);

What is ec_bh_open supposed to be racing with ?

drivers/net/ethernet/ec_bh.c line 527

Quoted 7 lines
drivers/net/ethernet/ec_bh.c new file +7 -0
+
+	ec_bh_reset(priv);
+
+	priv->rx = ec_bh_alloc_dma_mem(priv, priv->rx_dma_chan,
+				       &priv->rx_buf, &priv->rx_phys,
+				       &priv->rx_buf_phys, &priv->rx_len,
+				       &priv->rx_buf_len);

Yuck.

The Tx part duplicates this call. You want to introduce an extra struct that both Tx and Rx would use in bh_priv.

[...]

drivers/net/ethernet/ec_bh.c line 560

Quoted 2 lines
drivers/net/ethernet/ec_bh.c new file +2 -0
+	memset(priv->rx, 0, priv->rx_len);
+	memset(priv->tx, 0, priv->tx_len);

Useless.

Quoted 17 lines
drivers/net/ethernet/ec_bh.c new file +17 -0
+
+	iowrite8(0, priv->mii_io + MII_MAC_FILT_FLAG);
+
+	priv->rx_dcount = min_t(int, FIFO_SIZE,
+				priv->rx_len / (sizeof(struct rx_desc)));
+	desc = (struct rx_desc *)(priv->rx);
+	for (i = 0; i < priv->rx_dcount; i++) {
+		u32 next;
+
+		if (i != priv->rx_dcount - 1)
+			next = (u8 *)(desc + 1) - priv->rx;
+		else
+			next = 0;
+		next |= RXHDR_NEXT_VALID;
+		desc->header.next = cpu_to_le32(next);
+		desc->header.recv = 0;
+		ec_bh_add_rx_desc(priv, desc);

drivers/net/ethernet/ec_bh.c line 587

Quoted 10 lines
drivers/net/ethernet/ec_bh.c new file +10 -0
+
+		desc += 1;
+	}
+
+	netif_start_queue(net_dev);
+
+	hrtimer_init(&priv->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	priv->hrtimer.function = ec_bh_timer_fun;
+	hrtimer_start(&priv->hrtimer,
+		      ktime_set(0, TIMER_INTERVAL_NSEC),

Nit: s/TIMER_INTERVAL_NSEC/polling_frequency/ ?

drivers/net/ethernet/ec_bh.c line 592

Quoted 5 lines
drivers/net/ethernet/ec_bh.c new file +5 -0
+		      HRTIMER_MODE_REL);
+
+	dev_info(PRIV_TO_DEV(priv), "Device open\n");
+
+	ec_bh_print_status(priv);

(a bit too much debug messages imho)

[...]

Quoted 2 lines
drivers/net/ethernet/ec_bh.c new file +2 -0
+static int ec_bh_probe(struct pci_dev *dev, const struct pci_device_id *id)
+{

[...]

drivers/net/ethernet/ec_bh.c line 696

Quoted 3 lines
drivers/net/ethernet/ec_bh.c new file +3 -0
+	net_dev->mem_start = pci_resource_start(dev, 0);
+	net_dev->mem_end = pci_resource_end(dev, 0);
+	net_dev->irq = dev->irq;

Legacy net_device fields. Please don't use these.

[...]

drivers/net/ethernet/ec_bh.c line 714

Quoted 4 lines
drivers/net/ethernet/ec_bh.c new file +4 -0
+	if (ec_bh_setup_offsets(priv)) {
+		err = -ENODEV;
+		goto err_free_net_dev;
+	}
	err = ec_bh_setup_offsets(...)
	if (err < 0) {
		...

drivers/net/ethernet/ec_bh.c line 718

Quoted 4 lines
drivers/net/ethernet/ec_bh.c new file +4 -0
+
+	memcpy_fromio(net_dev->dev_addr, priv->mii_io + MII_MAC_ADDR, 6);
+
+	dev_info(PRIV_TO_DEV(priv),

s/PRIV_TO_DEV(priv)/&dev->dev/ ?

[...]

drivers/net/ethernet/ec_bh.c line 748

Quoted 6 lines
drivers/net/ethernet/ec_bh.c new file +6 -0
+static void ec_bh_remove(struct pci_dev *dev)
+{
+	struct net_device *net_dev = pci_get_drvdata(dev);
+	struct bh_priv *priv = netdev_priv(net_dev);
+	void * __iomem io = priv->io;
+	void * __iomem dma_io = priv->dma_io;

The void * are used only once in this function. They don't deserve local variables.

[...]

drivers/net/ethernet/ec_bh.c line 764

Quoted 5 lines
drivers/net/ethernet/ec_bh.c new file +5 -0
+static struct pci_driver pci_driver = {
+	.name = "ec_bh",
+	.id_table = ids,
+	.probe = ec_bh_probe,
+	.remove = ec_bh_remove,

Please use tabs before "=" to line things up.

Signature · 1 line
Ueimor
#2 lore raw

Hello. Thank you for your comments.

Quoted 3 lines · Francois Romieu

Darek Marcinkiewicz <[email protected]> :
[changes]

[...]

Quoted 7 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+
+struct bh_priv {

Nit: it would be nice to avoid "_bh_" as it is already used in a set of common kernel functions.

I renamed bh to bhf and as result also changed the driver name - to ec_bhf.

Quoted 6 lines

[...]

drivers/net/ethernet/ec_bh.c line 153

Quoted 2 lines
drivers/net/ethernet/ec_bh.c new file +2 -0
+	u8 *tx_put;
+	u8 *tx_get;

It's part u8 and part tx_header.

Quoted 7 lines

You may consider using a struct that adds an extra 'u8 data[0]' at the end of the struct tx_header.

skb_copy_and_csum_dev in the xmit path rings a bell but the DMA FIFO zone handling could imho save some pointer arithmetic and turn a bit more literate.

I did some rework of descriptors handling and they are now kept in a list so no pointer arithmetic at all.

As for the rest of your comments - all of them are incorporated in next revision of the patch.

Thank you.

Signature · 4 lines
DM

-- 
DM