Marvell Prestera 98DX326x integrates up to 24 ports of 1GbE with 8
ports of 10GbE uplinks or 2 ports of 40Gbps stacking for a largely
wireless SMB deployment.
Prestera Switchdev is a firmware based driver that operates via PCI bus. The
current implementation supports only boards designed for the Marvell Switchdev
solution and requires special firmware.
This driver implementation includes only L1, basic L2 support, and RX/TX.
The core Prestera switching logic is implemented in prestera_main.c, there is
an intermediate hw layer between core logic and firmware. It is
implemented in prestera_hw.c, the purpose of it is to encapsulate hw
related logic, in future there is a plan to support more devices with
different HW related configurations.
The following Switchdev features are supported:
- VLAN-aware bridge offloading
- VLAN-unaware bridge offloading
- FDB offloading (learning, ageing)
- Switchport configuration
The original firmware image is uploaded to the linux-firmware repository.
PATCHv3:
1) Simplify __be32 type casting in prestera_dsa.c
2) Added per-patch changelog under "---" line.
PATCHv2:
1) Use devlink_port_type_clear()
2) Add _MS prefix to timeout defines.
3) Remove not-needed packed attribute from the firmware ipc structs,
also the firmware image needs to be uploaded too (will do it soon).
4) Introduce prestera_hw_switch_fini(), to be mirrored with init and
do simple validation if the event handlers are unregistered.
5) Use kfree_rcu() for event handler unregistering.
6) Get rid of rcu-list usage when dealing with ports, not needed for
now.
7) Little spelling corrections in the error/info messages.
8) Make pci probe & remove logic mirrored.
9) Get rid of ETH_FCS_LEN in headroom setting, not needed.
PATCH:
1) Fixed W=1 warnings
2) Renamed PCI driver name to be more generic "Prestera DX" because
there will be more devices supported.
3) Changed firmware image dir path: marvell/ -> mrvl/prestera/
to be aligned with location in linux-firmware.git (if such
will be accepted).
RFC v3:
1) Fix prestera prefix in prestera_rxtx.c
2) Protect concurrent access from multiple ports on multiple CPU system
on tx path by spinlock in prestera_rxtx.c
3) Try to get base mac address from device-tree, otherwise use a random generated one.
4) Move ethtool interface support into separate prestera_ethtool.c file.
5) Add basic devlink support and get rid of physical port naming ops.
6) Add STP support in Switchdev driver.
7) Removed MODULE_AUTHOR
8) Renamed prestera.c -> prestera_main.c, and kernel module to
prestera.ko
RFC v2:
1) Use "pestera_" prefix in struct's and functions instead of mvsw_pr_
2) Original series split into additional patches for Switchdev ethtool support.
3) Use major and minor firmware version numbers in the firmware image filename.
4) Removed not needed prints.
5) Use iopoll API for waiting on register's value in prestera_pci.c
6) Use standart approach for describing PCI ID matching section instead of using
custom wrappers in prestera_pci.c
7) Add RX/TX support in prestera_rxtx.c.
8) Rewritten prestera_switchdev.c with following changes:
- handle netdev events from prestera.c
- use struct prestera_bridge for bridge objects, and get rid of
struct prestera_bridge_device which may confuse.
- use refcount_t
9) Get rid of macro usage for sending fw requests in prestera_hw.c
10) Add base_mac setting as module parameter. base_mac is required for
generation default port's mac.
Vadym Kochan (6):
net: marvell: prestera: Add driver for Prestera family ASIC devices
net: marvell: prestera: Add PCI interface support
net: marvell: prestera: Add basic devlink support
net: marvell: prestera: Add ethtool interface support
net: marvell: prestera: Add Switchdev driver implementation
dt-bindings: marvell,prestera: Add description for device-tree
bindings
.../bindings/net/marvell,prestera.txt | 34 +
drivers/net/ethernet/marvell/Kconfig | 1 +
drivers/net/ethernet/marvell/Makefile | 1 +
drivers/net/ethernet/marvell/prestera/Kconfig | 25 +
.../net/ethernet/marvell/prestera/Makefile | 7 +
.../net/ethernet/marvell/prestera/prestera.h | 208 +++
.../marvell/prestera/prestera_devlink.c | 120 ++
.../marvell/prestera/prestera_devlink.h | 26 +
.../ethernet/marvell/prestera/prestera_dsa.c | 134 ++
.../ethernet/marvell/prestera/prestera_dsa.h | 37 +
.../marvell/prestera/prestera_ethtool.c | 737 ++++++++++
.../marvell/prestera/prestera_ethtool.h | 37 +
.../ethernet/marvell/prestera/prestera_hw.c | 1231 ++++++++++++++++
.../ethernet/marvell/prestera/prestera_hw.h | 181 +++
.../ethernet/marvell/prestera/prestera_main.c | 653 +++++++++
.../ethernet/marvell/prestera/prestera_pci.c | 823 +++++++++++
.../ethernet/marvell/prestera/prestera_rxtx.c | 860 +++++++++++
.../ethernet/marvell/prestera/prestera_rxtx.h | 21 +
.../marvell/prestera/prestera_switchdev.c | 1286 +++++++++++++++++
.../marvell/prestera/prestera_switchdev.h | 16 +
20 files changed, 6438 insertions(+)
create mode 100644 drivers/net/ethernet/marvell/prestera/Kconfig
create mode 100644 drivers/net/ethernet/marvell/prestera/Makefile
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera.h
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_devlink.c
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_devlink.h
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_dsa.c
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_dsa.h
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_ethtool.c
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_ethtool.h
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_hw.c
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_hw.h
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_main.c
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_pci.c
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_rxtx.c
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_rxtx.h
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_switchdev.c
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_switchdev.h
--
2.17.1
Marvell Prestera 98DX326x integrates up to 24 ports of 1GbE with 8
ports of 10GbE uplinks or 2 ports of 40Gbps stacking for a largely
wireless SMB deployment.
The current implementation supports only boards designed for the Marvell
Switchdev solution and requires special firmware.
The core Prestera switching logic is implemented in prestera_main.c,
there is an intermediate hw layer between core logic and firmware. It is
implemented in prestera_hw.c, the purpose of it is to encapsulate hw
related logic, in future there is a plan to support more devices with
different HW related configurations.
This patch contains only basic switch initialization and RX/TX support
over SDMA mechanism.
Currently supported devices have DMA access range <= 32bit and require
ZONE_DMA to be enabled, for such cases SDMA driver checks if the skb
allocated in proper range supported by the Prestera device.
Also meanwhile there is no TX interrupt support in current firmware
version so recycling work is scheduled on each xmit.
Port's mac address is generated from the switch base mac which may be
provided via device-tree (static one or as nvme cell), or randomly
generated.
Signed-off-by: Andrii Savka <[email protected]>
Signed-off-by: Oleksandr Mazur <[email protected]>
Signed-off-by: Serhiy Boiko <[email protected]>
Signed-off-by: Serhiy Pshyk <[email protected]>
Signed-off-by: Taras Chornyi <[email protected]>
Signed-off-by: Volodymyr Mytnyk <[email protected]>
Signed-off-by: Vadym Kochan <[email protected]>
---
PATCHv3:
1) Simplify __be32 type casting in prestera_dsa.c: prestera_dsa_parse(...)
drivers/net/ethernet/marvell/Kconfig | 1 +
drivers/net/ethernet/marvell/Makefile | 1 +
drivers/net/ethernet/marvell/prestera/Kconfig | 13 +
.../net/ethernet/marvell/prestera/Makefile | 4 +
.../net/ethernet/marvell/prestera/prestera.h | 172 ++++
.../ethernet/marvell/prestera/prestera_dsa.c | 134 +++
.../ethernet/marvell/prestera/prestera_dsa.h | 37 +
.../ethernet/marvell/prestera/prestera_hw.c | 616 +++++++++++++
.../ethernet/marvell/prestera/prestera_hw.h | 72 ++
.../ethernet/marvell/prestera/prestera_main.c | 496 ++++++++++
.../ethernet/marvell/prestera/prestera_rxtx.c | 860 ++++++++++++++++++
.../ethernet/marvell/prestera/prestera_rxtx.h | 21 +
12 files changed, 2427 insertions(+)
create mode 100644 drivers/net/ethernet/marvell/prestera/Kconfig
create mode 100644 drivers/net/ethernet/marvell/prestera/Makefile
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera.h
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_dsa.c
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_dsa.h
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_hw.c
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_hw.h
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_main.c
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_rxtx.c
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_rxtx.h
diff --git a/drivers/net/ethernet/marvell/Kconfig b/drivers/net/ethernet/marvell/Kconfig
index ef4f35ba077d..892e5c8ff096 100644
--- a/drivers/net/ethernet/marvell/Kconfig
+++ b/drivers/net/ethernet/marvell/Kconfig
@@ -172,5 +172,6 @@ config SKY2_DEBUG
source "drivers/net/ethernet/marvell/octeontx2/Kconfig"
+source "drivers/net/ethernet/marvell/prestera/Kconfig"
endif # NET_VENDOR_MARVELL
diff --git a/drivers/net/ethernet/marvell/Makefile b/drivers/net/ethernet/marvell/Makefile
index 89dea7284d5b..9f88fe822555 100644
--- a/drivers/net/ethernet/marvell/Makefile
+++ b/drivers/net/ethernet/marvell/Makefile
@@ -12,3 +12,4 @@ obj-$(CONFIG_PXA168_ETH) += pxa168_eth.o
obj-$(CONFIG_SKGE) += skge.o
obj-$(CONFIG_SKY2) += sky2.o
obj-y += octeontx2/
+obj-y += prestera/
diff --git a/drivers/net/ethernet/marvell/prestera/Kconfig b/drivers/net/ethernet/marvell/prestera/Kconfig
new file mode 100644
index 000000000000..76b68613ea7a
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/Kconfig
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Marvell Prestera drivers configuration
+#
+
+config PRESTERA
+ tristate "Marvell Prestera Switch ASICs support"
+ depends on NET_SWITCHDEV && VLAN_8021Q
+ help
+ This driver supports Marvell Prestera Switch ASICs family.
+
+ To compile this driver as a module, choose M here: the
+ module will be called prestera.
diff --git a/drivers/net/ethernet/marvell/prestera/Makefile b/drivers/net/ethernet/marvell/prestera/Makefile
new file mode 100644
index 000000000000..610d75032b78
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/Makefile
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_PRESTERA) += prestera.o
+prestera-objs := prestera_main.o prestera_hw.o prestera_dsa.o \
+ prestera_rxtx.o
diff --git a/drivers/net/ethernet/marvell/prestera/prestera.h b/drivers/net/ethernet/marvell/prestera/prestera.h
new file mode 100644
index 000000000000..5079d872e18a
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/prestera.h
@@ -0,0 +1,172 @@
+/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+ *
+ * Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved.
+ *
+ */
+
+#ifndef _PRESTERA_H_
+#define _PRESTERA_H_
+
+#include <linux/skbuff.h>
+#include <linux/notifier.h>
+#include <uapi/linux/if_ether.h>
+#include <linux/workqueue.h>
+
+struct prestera_fw_rev {
+ u16 maj;
+ u16 min;
+ u16 sub;
+};
+
+struct prestera_port_stats {
+ u64 good_octets_received;
+ u64 bad_octets_received;
+ u64 mac_trans_error;
+ u64 broadcast_frames_received;
+ u64 multicast_frames_received;
+ u64 frames_64_octets;
+ u64 frames_65_to_127_octets;
+ u64 frames_128_to_255_octets;
+ u64 frames_256_to_511_octets;
+ u64 frames_512_to_1023_octets;
+ u64 frames_1024_to_max_octets;
+ u64 excessive_collision;
+ u64 multicast_frames_sent;
+ u64 broadcast_frames_sent;
+ u64 fc_sent;
+ u64 fc_received;
+ u64 buffer_overrun;
+ u64 undersize;
+ u64 fragments;
+ u64 oversize;
+ u64 jabber;
+ u64 rx_error_frame_received;
+ u64 bad_crc;
+ u64 collisions;
+ u64 late_collision;
+ u64 unicast_frames_received;
+ u64 unicast_frames_sent;
+ u64 sent_multiple;
+ u64 sent_deferred;
+ u64 frames_1024_to_1518_octets;
+ u64 frames_1519_to_max_octets;
+ u64 good_octets_sent;
+};
+
+struct prestera_port_caps {
+ u64 supp_link_modes;
+ u8 supp_fec;
+ u8 type;
+ u8 transceiver;
+};
+
+struct prestera_port {
+ struct net_device *dev;
+ struct prestera_switch *sw;
+ u32 id;
+ u32 hw_id;
+ u32 dev_id;
+ u16 fp_id;
+ bool autoneg;
+ u64 adver_link_modes;
+ u8 adver_fec;
+ struct prestera_port_caps caps;
+ struct list_head list;
+ struct {
+ struct prestera_port_stats stats;
+ struct delayed_work caching_dw;
+ } cached_hw_stats;
+};
+
+struct prestera_device {
+ struct device *dev;
+ u8 __iomem *ctl_regs;
+ u8 __iomem *pp_regs;
+ struct prestera_fw_rev fw_rev;
+ void *priv;
+
+ /* called by device driver to handle received packets */
+ void (*recv_pkt)(struct prestera_device *dev);
+
+ /* called by device driver to pass event up to the higher layer */
+ int (*recv_msg)(struct prestera_device *dev, u8 *msg, size_t size);
+
+ /* called by higher layer to send request to the firmware */
+ int (*send_req)(struct prestera_device *dev, u8 *in_msg,
+ size_t in_size, u8 *out_msg, size_t out_size,
+ unsigned int wait);
+};
+
+enum prestera_event_type {
+ PRESTERA_EVENT_TYPE_UNSPEC,
+
+ PRESTERA_EVENT_TYPE_PORT,
+ PRESTERA_EVENT_TYPE_RXTX,
+
+ PRESTERA_EVENT_TYPE_MAX,
+};
+
+enum prestera_rxtx_event_id {
+ PRESTERA_RXTX_EVENT_UNSPEC,
+ PRESTERA_RXTX_EVENT_RCV_PKT,
+};
+
+enum prestera_port_event_id {
+ PRESTERA_PORT_EVENT_UNSPEC,
+ PRESTERA_PORT_EVENT_STATE_CHANGED,
+};
+
+struct prestera_port_event {
+ u32 port_id;
+ union {
+ u32 oper_state;
+ } data;
+};
+
+struct prestera_event {
+ u16 id;
+ union {
+ struct prestera_port_event port_evt;
+ };
+};
+
+struct prestera_rxtx;
+
+struct prestera_switch {
+ struct prestera_device *dev;
+ struct prestera_rxtx *rxtx;
+ struct list_head event_handlers;
+ char base_mac[ETH_ALEN];
+ struct list_head port_list;
+ u32 port_count;
+ u32 mtu_min;
+ u32 mtu_max;
+ u8 id;
+};
+
+struct prestera_rxtx_params {
+ bool use_sdma;
+ u32 map_addr;
+};
+
+#define prestera_dev(sw) ((sw)->dev->dev)
+
+static inline void prestera_write(const struct prestera_switch *sw,
+ unsigned int reg, u32 val)
+{
+ writel(val, sw->dev->pp_regs + reg);
+}
+
+static inline u32 prestera_read(const struct prestera_switch *sw,
+ unsigned int reg)
+{
+ return readl(sw->dev->pp_regs + reg);
+}
+
+int prestera_device_register(struct prestera_device *dev);
+void prestera_device_unregister(struct prestera_device *dev);
+
+struct prestera_port *prestera_port_find_by_hwid(struct prestera_switch *sw,
+ u32 dev_id, u32 hw_id);
+
+#endif /* _PRESTERA_H_ */
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_dsa.c b/drivers/net/ethernet/marvell/prestera/prestera_dsa.c
new file mode 100644
index 000000000000..06e2a0b159b6
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/prestera_dsa.c
@@ -0,0 +1,134 @@
+// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+/* Copyright (c) 2020 Marvell International Ltd. All rights reserved */
+
+#include "prestera_dsa.h"
+
+#include <linux/string.h>
+#include <linux/bitops.h>
+#include <linux/bitfield.h>
+#include <linux/errno.h>
+
+#define PRESTERA_W0_IS_TAGGED BIT(29)
+
+/* TrgDev[4:0] = {Word0[28:24]} */
+#define PRESTERA_W0_HW_DEV_NUM GENMASK(28, 24)
+
+/* SrcPort/TrgPort extended to 8b
+ * SrcPort/TrgPort[7:0] = {Word2[20], Word1[11:10], Word0[23:19]}
+ */
+#define PRESTERA_W0_IFACE_PORT_NUM GENMASK(23, 19)
+
+/* bits 30:31 - TagCommand 1 = FROM_CPU */
+#define PRESTERA_W0_DSA_CMD GENMASK(31, 30)
+
+/* bits 13:15 -- UP */
+#define PRESTERA_W0_VPT GENMASK(15, 13)
+
+#define PRESTERA_W0_EXT_BIT BIT(12)
+
+/* bits 0:11 -- VID */
+#define PRESTERA_W0_VID GENMASK(11, 0)
+
+/* SrcPort/TrgPort extended to 8b
+ * SrcPort/TrgPort[7:0] = {Word2[20], Word1[11:10], Word0[23:19]}
+ */
+#define PRESTERA_W1_IFACE_PORT_NUM GENMASK(11, 10)
+
+#define PRESTERA_W1_EXT_BIT BIT(31)
+#define PRESTERA_W1_CFI_BIT BIT(30)
+
+/* SrcPort/TrgPort extended to 8b
+ * SrcPort/TrgPort[7:0] = {Word2[20], Word1[11:10], Word0[23:19]}
+ */
+#define PRESTERA_W2_IFACE_PORT_NUM BIT(20)
+
+#define PRESTERA_W2_EXT_BIT BIT(31)
+
+/* trgHwDev and trgPort
+ * TrgDev[11:5] = {Word3[6:0]}
+ */
+#define PRESTERA_W3_HW_DEV_NUM GENMASK(6, 0)
+
+/* VID 16b [15:0] = {Word3[30:27], Word0[11:0]} */
+#define PRESTERA_W3_VID GENMASK(30, 27)
+
+/* TRGePort[16:0] = {Word3[23:7]} */
+#define PRESTERA_W3_DST_EPORT GENMASK(23, 7)
+
+#define PRESTERA_DEV_NUM_MASK GENMASK(11, 5)
+#define PRESTERA_VID_MASK GENMASK(15, 12)
+
+int prestera_dsa_parse(struct prestera_dsa *dsa, const u8 *dsa_buf)
+{
+ __be32 *dsa_words = (__be32 *)dsa_buf;
+ enum prestera_dsa_cmd cmd;
+ u32 words[4] = { 0 };
+ u32 field;
+
+ words[0] = ntohl(dsa_words[0]);
+ words[1] = ntohl(dsa_words[1]);
+ words[2] = ntohl(dsa_words[2]);
+ words[3] = ntohl(dsa_words[3]);
+
+ /* set the common parameters */
+ cmd = (enum prestera_dsa_cmd)FIELD_GET(PRESTERA_W0_DSA_CMD, words[0]);
+
+ /* only to CPU is supported */
+ if (unlikely(cmd != PRESTERA_DSA_CMD_TO_CPU))
+ return -EINVAL;
+
+ if (FIELD_GET(PRESTERA_W0_EXT_BIT, words[0]) == 0)
+ return -EINVAL;
+ if (FIELD_GET(PRESTERA_W1_EXT_BIT, words[1]) == 0)
+ return -EINVAL;
+ if (FIELD_GET(PRESTERA_W2_EXT_BIT, words[2]) == 0)
+ return -EINVAL;
+
+ field = FIELD_GET(PRESTERA_W3_VID, words[3]);
+
+ dsa->vlan.is_tagged = (bool)FIELD_GET(PRESTERA_W0_IS_TAGGED, words[0]);
+ dsa->vlan.cfi_bit = (u8)FIELD_GET(PRESTERA_W1_CFI_BIT, words[1]);
+ dsa->vlan.vpt = (u8)FIELD_GET(PRESTERA_W0_VPT, words[0]);
+ dsa->vlan.vid = (u16)FIELD_GET(PRESTERA_W0_VID, words[0]);
+ dsa->vlan.vid &= ~PRESTERA_VID_MASK;
+ dsa->vlan.vid |= FIELD_PREP(PRESTERA_VID_MASK, field);
+
+ field = FIELD_GET(PRESTERA_W3_HW_DEV_NUM, words[3]);
+
+ dsa->hw_dev_num = FIELD_GET(PRESTERA_W0_HW_DEV_NUM, words[0]);
+ dsa->hw_dev_num &= PRESTERA_W3_HW_DEV_NUM;
+ dsa->hw_dev_num |= FIELD_PREP(PRESTERA_DEV_NUM_MASK, field);
+
+ dsa->port_num = (FIELD_GET(PRESTERA_W0_IFACE_PORT_NUM, words[0]) << 0) |
+ (FIELD_GET(PRESTERA_W1_IFACE_PORT_NUM, words[1]) << 5) |
+ (FIELD_GET(PRESTERA_W2_IFACE_PORT_NUM, words[2]) << 7);
+ return 0;
+}
+
+int prestera_dsa_build(const struct prestera_dsa *dsa, u8 *dsa_buf)
+{
+ __be32 *dsa_words = (__be32 *)dsa_buf;
+ u32 words[4] = { 0 };
+
+ if (dsa->hw_dev_num >= BIT(12))
+ return -EINVAL;
+ if (dsa->port_num >= BIT(17))
+ return -EINVAL;
+
+ words[0] |= FIELD_PREP(PRESTERA_W0_DSA_CMD, PRESTERA_DSA_CMD_FROM_CPU);
+
+ words[0] |= FIELD_PREP(PRESTERA_W0_HW_DEV_NUM, dsa->hw_dev_num);
+ words[3] |= FIELD_PREP(PRESTERA_W3_HW_DEV_NUM, (dsa->hw_dev_num >> 5));
+ words[3] |= FIELD_PREP(PRESTERA_W3_DST_EPORT, dsa->port_num);
+
+ words[0] |= FIELD_PREP(PRESTERA_W0_EXT_BIT, 1);
+ words[1] |= FIELD_PREP(PRESTERA_W1_EXT_BIT, 1);
+ words[2] |= FIELD_PREP(PRESTERA_W2_EXT_BIT, 1);
+
+ dsa_words[0] = htonl(words[0]);
+ dsa_words[1] = htonl(words[1]);
+ dsa_words[2] = htonl(words[2]);
+ dsa_words[3] = htonl(words[3]);
+
+ return 0;
+}
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_dsa.h b/drivers/net/ethernet/marvell/prestera/prestera_dsa.h
new file mode 100644
index 000000000000..795a49c26fcb
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/prestera_dsa.h
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+ *
+ * Copyright (c) 2020 Marvell International Ltd. All rights reserved.
+ *
+ */
+#ifndef __PRESTERA_DSA_H_
+#define __PRESTERA_DSA_H_
+
+#include <linux/types.h>
+
+#define PRESTERA_DSA_HLEN 16
+
+enum prestera_dsa_cmd {
+ /* DSA command is "To CPU" */
+ PRESTERA_DSA_CMD_TO_CPU = 0,
+
+ /* DSA command is "From CPU" */
+ PRESTERA_DSA_CMD_FROM_CPU,
+};
+
+struct prestera_dsa_vlan {
+ u16 vid;
+ u8 vpt;
+ u8 cfi_bit;
+ bool is_tagged;
+};
+
+struct prestera_dsa {
+ struct prestera_dsa_vlan vlan;
+ u32 hw_dev_num;
+ u32 port_num;
+};
+
+int prestera_dsa_parse(struct prestera_dsa *dsa, const u8 *dsa_buf);
+int prestera_dsa_build(const struct prestera_dsa *dsa, u8 *dsa_buf);
+
+#endif /* _PRESTERA_DSA_H_ */
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_hw.c b/drivers/net/ethernet/marvell/prestera/prestera_hw.c
new file mode 100644
index 000000000000..5899a16a493e
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/prestera_hw.c
@@ -0,0 +1,616 @@
+// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+/* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved */
+
+#include <linux/etherdevice.h>
+#include <linux/ethtool.h>
+#include <linux/netdevice.h>
+#include <linux/list.h>
+
+#include "prestera.h"
+#include "prestera_hw.h"
+
+#define PRESTERA_SWITCH_INIT_TIMEOUT_MS (30 * 1000)
+
+#define PRESTERA_MIN_MTU 64
+
+enum prestera_cmd_type_t {
+ PRESTERA_CMD_TYPE_SWITCH_INIT = 0x1,
+ PRESTERA_CMD_TYPE_SWITCH_ATTR_SET = 0x2,
+
+ PRESTERA_CMD_TYPE_PORT_ATTR_SET = 0x100,
+ PRESTERA_CMD_TYPE_PORT_ATTR_GET = 0x101,
+ PRESTERA_CMD_TYPE_PORT_INFO_GET = 0x110,
+
+ PRESTERA_CMD_TYPE_RXTX_INIT = 0x800,
+ PRESTERA_CMD_TYPE_RXTX_PORT_INIT = 0x801,
+
+ PRESTERA_CMD_TYPE_ACK = 0x10000,
+ PRESTERA_CMD_TYPE_MAX
+};
+
+enum {
+ PRESTERA_CMD_PORT_ATTR_ADMIN_STATE = 1,
+ PRESTERA_CMD_PORT_ATTR_MTU = 3,
+ PRESTERA_CMD_PORT_ATTR_MAC = 4,
+ PRESTERA_CMD_PORT_ATTR_CAPABILITY = 9,
+ PRESTERA_CMD_PORT_ATTR_AUTONEG = 15,
+ PRESTERA_CMD_PORT_ATTR_STATS = 17,
+};
+
+enum {
+ PRESTERA_CMD_SWITCH_ATTR_MAC = 1,
+};
+
+enum {
+ PRESTERA_CMD_ACK_OK,
+ PRESTERA_CMD_ACK_FAILED,
+
+ PRESTERA_CMD_ACK_MAX
+};
+
+enum {
+ PRESTERA_PORT_GOOD_OCTETS_RCV_CNT,
+ PRESTERA_PORT_BAD_OCTETS_RCV_CNT,
+ PRESTERA_PORT_MAC_TRANSMIT_ERR_CNT,
+ PRESTERA_PORT_BRDC_PKTS_RCV_CNT,
+ PRESTERA_PORT_MC_PKTS_RCV_CNT,
+ PRESTERA_PORT_PKTS_64L_CNT,
+ PRESTERA_PORT_PKTS_65TO127L_CNT,
+ PRESTERA_PORT_PKTS_128TO255L_CNT,
+ PRESTERA_PORT_PKTS_256TO511L_CNT,
+ PRESTERA_PORT_PKTS_512TO1023L_CNT,
+ PRESTERA_PORT_PKTS_1024TOMAXL_CNT,
+ PRESTERA_PORT_EXCESSIVE_COLLISIONS_CNT,
+ PRESTERA_PORT_MC_PKTS_SENT_CNT,
+ PRESTERA_PORT_BRDC_PKTS_SENT_CNT,
+ PRESTERA_PORT_FC_SENT_CNT,
+ PRESTERA_PORT_GOOD_FC_RCV_CNT,
+ PRESTERA_PORT_DROP_EVENTS_CNT,
+ PRESTERA_PORT_UNDERSIZE_PKTS_CNT,
+ PRESTERA_PORT_FRAGMENTS_PKTS_CNT,
+ PRESTERA_PORT_OVERSIZE_PKTS_CNT,
+ PRESTERA_PORT_JABBER_PKTS_CNT,
+ PRESTERA_PORT_MAC_RCV_ERROR_CNT,
+ PRESTERA_PORT_BAD_CRC_CNT,
+ PRESTERA_PORT_COLLISIONS_CNT,
+ PRESTERA_PORT_LATE_COLLISIONS_CNT,
+ PRESTERA_PORT_GOOD_UC_PKTS_RCV_CNT,
+ PRESTERA_PORT_GOOD_UC_PKTS_SENT_CNT,
+ PRESTERA_PORT_MULTIPLE_PKTS_SENT_CNT,
+ PRESTERA_PORT_DEFERRED_PKTS_SENT_CNT,
+ PRESTERA_PORT_GOOD_OCTETS_SENT_CNT,
+
+ PRESTERA_PORT_CNT_MAX,
+};
+
+struct prestera_fw_event_handler {
+ struct list_head list;
+ struct rcu_head rcu;
+ enum prestera_event_type type;
+ prestera_event_cb_t func;
+ void *arg;
+};
+
+struct prestera_msg_cmd {
+ u32 type;
+};
+
+struct prestera_msg_ret {
+ struct prestera_msg_cmd cmd;
+ u32 status;
+};
+
+struct prestera_msg_common_req {
+ struct prestera_msg_cmd cmd;
+};
+
+struct prestera_msg_common_resp {
+ struct prestera_msg_ret ret;
+};
+
+union prestera_msg_switch_param {
+ u8 mac[ETH_ALEN];
+};
+
+struct prestera_msg_switch_attr_req {
+ struct prestera_msg_cmd cmd;
+ u32 attr;
+ union prestera_msg_switch_param param;
+};
+
+struct prestera_msg_switch_init_resp {
+ struct prestera_msg_ret ret;
+ u32 port_count;
+ u32 mtu_max;
+ u8 switch_id;
+};
+
+struct prestera_msg_port_autoneg_param {
+ u64 link_mode;
+ u8 enable;
+ u8 fec;
+};
+
+struct prestera_msg_port_cap_param {
+ u64 link_mode;
+ u8 type;
+ u8 fec;
+ u8 transceiver;
+};
+
+union prestera_msg_port_param {
+ u8 admin_state;
+ u8 oper_state;
+ u32 mtu;
+ u8 mac[ETH_ALEN];
+ struct prestera_msg_port_autoneg_param autoneg;
+ struct prestera_msg_port_cap_param cap;
+};
+
+struct prestera_msg_port_attr_req {
+ struct prestera_msg_cmd cmd;
+ u32 attr;
+ u32 port;
+ u32 dev;
+ union prestera_msg_port_param param;
+};
+
+struct prestera_msg_port_attr_resp {
+ struct prestera_msg_ret ret;
+ union prestera_msg_port_param param;
+};
+
+struct prestera_msg_port_stats_resp {
+ struct prestera_msg_ret ret;
+ u64 stats[PRESTERA_PORT_CNT_MAX];
+};
+
+struct prestera_msg_port_info_req {
+ struct prestera_msg_cmd cmd;
+ u32 port;
+};
+
+struct prestera_msg_port_info_resp {
+ struct prestera_msg_ret ret;
+ u32 hw_id;
+ u32 dev_id;
+ u16 fp_id;
+};
+
+struct prestera_msg_rxtx_req {
+ struct prestera_msg_cmd cmd;
+ u8 use_sdma;
+};
+
+struct prestera_msg_rxtx_resp {
+ struct prestera_msg_ret ret;
+ u32 map_addr;
+};
+
+struct prestera_msg_rxtx_port_req {
+ struct prestera_msg_cmd cmd;
+ u32 port;
+ u32 dev;
+};
+
+struct prestera_msg_event {
+ u16 type;
+ u16 id;
+};
+
+union prestera_msg_event_port_param {
+ u32 oper_state;
+};
+
+struct prestera_msg_event_port {
+ struct prestera_msg_event id;
+ u32 port_id;
+ union prestera_msg_event_port_param param;
+};
+
+static int __prestera_cmd_ret(struct prestera_switch *sw,
+ enum prestera_cmd_type_t type,
+ struct prestera_msg_cmd *cmd, size_t clen,
+ struct prestera_msg_ret *ret, size_t rlen,
+ int waitms)
+{
+ struct prestera_device *dev = sw->dev;
+ int err;
+
+ cmd->type = type;
+
+ err = dev->send_req(dev, (u8 *)cmd, clen, (u8 *)ret, rlen, waitms);
+ if (err)
+ return err;
+
+ if (ret->cmd.type != PRESTERA_CMD_TYPE_ACK)
+ return -EBADE;
+ if (ret->status != PRESTERA_CMD_ACK_OK)
+ return -EINVAL;
+
+ return 0;
+}
+
+static int prestera_cmd_ret(struct prestera_switch *sw,
+ enum prestera_cmd_type_t type,
+ struct prestera_msg_cmd *cmd, size_t clen,
+ struct prestera_msg_ret *ret, size_t rlen)
+{
+ return __prestera_cmd_ret(sw, type, cmd, clen, ret, rlen, 0);
+}
+
+static int prestera_cmd_ret_wait(struct prestera_switch *sw,
+ enum prestera_cmd_type_t type,
+ struct prestera_msg_cmd *cmd, size_t clen,
+ struct prestera_msg_ret *ret, size_t rlen,
+ int waitms)
+{
+ return __prestera_cmd_ret(sw, type, cmd, clen, ret, rlen, waitms);
+}
+
+static int prestera_cmd(struct prestera_switch *sw,
+ enum prestera_cmd_type_t type,
+ struct prestera_msg_cmd *cmd, size_t clen)
+{
+ struct prestera_msg_common_resp resp;
+
+ return prestera_cmd_ret(sw, type, cmd, clen, &resp.ret, sizeof(resp));
+}
+
+static int prestera_fw_parse_port_evt(u8 *msg, struct prestera_event *evt)
+{
+ struct prestera_msg_event_port *hw_evt;
+
+ hw_evt = (struct prestera_msg_event_port *)msg;
+
+ evt->port_evt.port_id = hw_evt->port_id;
+
+ if (evt->id == PRESTERA_PORT_EVENT_STATE_CHANGED)
+ evt->port_evt.data.oper_state = hw_evt->param.oper_state;
+ else
+ return -EINVAL;
+
+ return 0;
+}
+
+static struct prestera_fw_evt_parser {
+ int (*func)(u8 *msg, struct prestera_event *evt);
+} fw_event_parsers[PRESTERA_EVENT_TYPE_MAX] = {
+ [PRESTERA_EVENT_TYPE_PORT] = {.func = prestera_fw_parse_port_evt},
+};
+
+static struct prestera_fw_event_handler *
+__find_event_handler(const struct prestera_switch *sw,
+ enum prestera_event_type type)
+{
+ struct prestera_fw_event_handler *eh;
+
+ list_for_each_entry_rcu(eh, &sw->event_handlers, list) {
+ if (eh->type == type)
+ return eh;
+ }
+
+ return NULL;
+}
+
+static int prestera_find_event_handler(const struct prestera_switch *sw,
+ enum prestera_event_type type,
+ struct prestera_fw_event_handler *eh)
+{
+ struct prestera_fw_event_handler *tmp;
+ int err = 0;
+
+ rcu_read_lock();
+ tmp = __find_event_handler(sw, type);
+ if (tmp)
+ *eh = *tmp;
+ else
+ err = -EEXIST;
+ rcu_read_unlock();
+
+ return err;
+}
+
+static int prestera_evt_recv(struct prestera_device *dev, u8 *buf, size_t size)
+{
+ struct prestera_msg_event *msg = (struct prestera_msg_event *)buf;
+ struct prestera_switch *sw = dev->priv;
+ struct prestera_fw_event_handler eh;
+ struct prestera_event evt;
+ int err;
+
+ if (msg->type >= PRESTERA_EVENT_TYPE_MAX)
+ return -EINVAL;
+
+ err = prestera_find_event_handler(sw, msg->type, &eh);
+
+ if (err || !fw_event_parsers[msg->type].func)
+ return 0;
+
+ evt.id = msg->id;
+
+ err = fw_event_parsers[msg->type].func(buf, &evt);
+ if (!err)
+ eh.func(sw, &evt, eh.arg);
+
+ return err;
+}
+
+static void prestera_pkt_recv(struct prestera_device *dev)
+{
+ struct prestera_switch *sw = dev->priv;
+ struct prestera_fw_event_handler eh;
+ struct prestera_event ev;
+ int err;
+
+ ev.id = PRESTERA_RXTX_EVENT_RCV_PKT;
+
+ err = prestera_find_event_handler(sw, PRESTERA_EVENT_TYPE_RXTX, &eh);
+ if (err)
+ return;
+
+ eh.func(sw, &ev, eh.arg);
+}
+
+int prestera_hw_port_info_get(const struct prestera_port *port,
+ u32 *dev_id, u32 *hw_id, u16 *fp_id)
+{
+ struct prestera_msg_port_info_resp resp;
+ struct prestera_msg_port_info_req req = {
+ .port = port->id
+ };
+ int err;
+
+ err = prestera_cmd_ret(port->sw, PRESTERA_CMD_TYPE_PORT_INFO_GET,
+ &req.cmd, sizeof(req), &resp.ret, sizeof(resp));
+ if (err)
+ return err;
+
+ *hw_id = resp.hw_id;
+ *dev_id = resp.dev_id;
+ *fp_id = resp.fp_id;
+
+ return 0;
+}
+
+int prestera_hw_switch_mac_set(struct prestera_switch *sw, char *mac)
+{
+ struct prestera_msg_switch_attr_req req = {
+ .attr = PRESTERA_CMD_SWITCH_ATTR_MAC,
+ };
+
+ memcpy(req.param.mac, mac, sizeof(req.param.mac));
+
+ return prestera_cmd(sw, PRESTERA_CMD_TYPE_SWITCH_ATTR_SET,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_switch_init(struct prestera_switch *sw)
+{
+ struct prestera_msg_switch_init_resp resp;
+ struct prestera_msg_common_req req;
+ int err;
+
+ INIT_LIST_HEAD(&sw->event_handlers);
+
+ err = prestera_cmd_ret_wait(sw, PRESTERA_CMD_TYPE_SWITCH_INIT,
+ &req.cmd, sizeof(req),
+ &resp.ret, sizeof(resp),
+ PRESTERA_SWITCH_INIT_TIMEOUT_MS);
+ if (err)
+ return err;
+
+ sw->id = resp.switch_id;
+ sw->port_count = resp.port_count;
+ sw->mtu_min = PRESTERA_MIN_MTU;
+ sw->mtu_max = resp.mtu_max;
+ sw->dev->recv_msg = prestera_evt_recv;
+ sw->dev->recv_pkt = prestera_pkt_recv;
+
+ return 0;
+}
+
+void prestera_hw_switch_fini(struct prestera_switch *sw)
+{
+ WARN_ON(!list_empty(&sw->event_handlers));
+}
+
+int prestera_hw_port_state_set(const struct prestera_port *port,
+ bool admin_state)
+{
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_ADMIN_STATE,
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ .param = {.admin_state = admin_state}
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_SET,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_port_mtu_set(const struct prestera_port *port, u32 mtu)
+{
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_MTU,
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ .param = {.mtu = mtu}
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_SET,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_port_mac_set(const struct prestera_port *port, char *mac)
+{
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_MAC,
+ .port = port->hw_id,
+ .dev = port->dev_id
+ };
+ memcpy(&req.param.mac, mac, sizeof(req.param.mac));
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_SET,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_port_cap_get(const struct prestera_port *port,
+ struct prestera_port_caps *caps)
+{
+ struct prestera_msg_port_attr_resp resp;
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_CAPABILITY,
+ .port = port->hw_id,
+ .dev = port->dev_id
+ };
+ int err;
+
+ err = prestera_cmd_ret(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_GET,
+ &req.cmd, sizeof(req), &resp.ret, sizeof(resp));
+ if (err)
+ return err;
+
+ caps->supp_link_modes = resp.param.cap.link_mode;
+ caps->supp_fec = resp.param.cap.fec;
+ caps->type = resp.param.cap.type;
+ caps->transceiver = resp.param.cap.transceiver;
+
+ return err;
+}
+
+int prestera_hw_port_autoneg_set(const struct prestera_port *port,
+ bool autoneg, u64 link_modes, u8 fec)
+{
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_AUTONEG,
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ .param = {.autoneg = {.link_mode = link_modes,
+ .enable = autoneg,
+ .fec = fec}
+ }
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_SET,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_port_stats_get(const struct prestera_port *port,
+ struct prestera_port_stats *st)
+{
+ struct prestera_msg_port_stats_resp resp;
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_STATS,
+ .port = port->hw_id,
+ .dev = port->dev_id
+ };
+ u64 *hw = resp.stats;
+ int err;
+
+ err = prestera_cmd_ret(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_GET,
+ &req.cmd, sizeof(req), &resp.ret, sizeof(resp));
+ if (err)
+ return err;
+
+ st->good_octets_received = hw[PRESTERA_PORT_GOOD_OCTETS_RCV_CNT];
+ st->bad_octets_received = hw[PRESTERA_PORT_BAD_OCTETS_RCV_CNT];
+ st->mac_trans_error = hw[PRESTERA_PORT_MAC_TRANSMIT_ERR_CNT];
+ st->broadcast_frames_received = hw[PRESTERA_PORT_BRDC_PKTS_RCV_CNT];
+ st->multicast_frames_received = hw[PRESTERA_PORT_MC_PKTS_RCV_CNT];
+ st->frames_64_octets = hw[PRESTERA_PORT_PKTS_64L_CNT];
+ st->frames_65_to_127_octets = hw[PRESTERA_PORT_PKTS_65TO127L_CNT];
+ st->frames_128_to_255_octets = hw[PRESTERA_PORT_PKTS_128TO255L_CNT];
+ st->frames_256_to_511_octets = hw[PRESTERA_PORT_PKTS_256TO511L_CNT];
+ st->frames_512_to_1023_octets = hw[PRESTERA_PORT_PKTS_512TO1023L_CNT];
+ st->frames_1024_to_max_octets = hw[PRESTERA_PORT_PKTS_1024TOMAXL_CNT];
+ st->excessive_collision = hw[PRESTERA_PORT_EXCESSIVE_COLLISIONS_CNT];
+ st->multicast_frames_sent = hw[PRESTERA_PORT_MC_PKTS_SENT_CNT];
+ st->broadcast_frames_sent = hw[PRESTERA_PORT_BRDC_PKTS_SENT_CNT];
+ st->fc_sent = hw[PRESTERA_PORT_FC_SENT_CNT];
+ st->fc_received = hw[PRESTERA_PORT_GOOD_FC_RCV_CNT];
+ st->buffer_overrun = hw[PRESTERA_PORT_DROP_EVENTS_CNT];
+ st->undersize = hw[PRESTERA_PORT_UNDERSIZE_PKTS_CNT];
+ st->fragments = hw[PRESTERA_PORT_FRAGMENTS_PKTS_CNT];
+ st->oversize = hw[PRESTERA_PORT_OVERSIZE_PKTS_CNT];
+ st->jabber = hw[PRESTERA_PORT_JABBER_PKTS_CNT];
+ st->rx_error_frame_received = hw[PRESTERA_PORT_MAC_RCV_ERROR_CNT];
+ st->bad_crc = hw[PRESTERA_PORT_BAD_CRC_CNT];
+ st->collisions = hw[PRESTERA_PORT_COLLISIONS_CNT];
+ st->late_collision = hw[PRESTERA_PORT_LATE_COLLISIONS_CNT];
+ st->unicast_frames_received = hw[PRESTERA_PORT_GOOD_UC_PKTS_RCV_CNT];
+ st->unicast_frames_sent = hw[PRESTERA_PORT_GOOD_UC_PKTS_SENT_CNT];
+ st->sent_multiple = hw[PRESTERA_PORT_MULTIPLE_PKTS_SENT_CNT];
+ st->sent_deferred = hw[PRESTERA_PORT_DEFERRED_PKTS_SENT_CNT];
+ st->good_octets_sent = hw[PRESTERA_PORT_GOOD_OCTETS_SENT_CNT];
+
+ return 0;
+}
+
+int prestera_hw_rxtx_init(struct prestera_switch *sw,
+ struct prestera_rxtx_params *params)
+{
+ struct prestera_msg_rxtx_resp resp;
+ struct prestera_msg_rxtx_req req;
+ int err;
+
+ req.use_sdma = params->use_sdma;
+
+ err = prestera_cmd_ret(sw, PRESTERA_CMD_TYPE_RXTX_INIT,
+ &req.cmd, sizeof(req), &resp.ret, sizeof(resp));
+ if (err)
+ return err;
+
+ params->map_addr = resp.map_addr;
+ return 0;
+}
+
+int prestera_hw_rxtx_port_init(struct prestera_port *port)
+{
+ struct prestera_msg_rxtx_port_req req = {
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_RXTX_PORT_INIT,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_event_handler_register(struct prestera_switch *sw,
+ enum prestera_event_type type,
+ prestera_event_cb_t fn,
+ void *arg)
+{
+ struct prestera_fw_event_handler *eh;
+
+ eh = __find_event_handler(sw, type);
+ if (eh)
+ return -EEXIST;
+ eh = kmalloc(sizeof(*eh), GFP_KERNEL);
+ if (!eh)
+ return -ENOMEM;
+
+ eh->type = type;
+ eh->func = fn;
+ eh->arg = arg;
+
+ INIT_LIST_HEAD(&eh->list);
+
+ list_add_rcu(&eh->list, &sw->event_handlers);
+
+ return 0;
+}
+
+void prestera_hw_event_handler_unregister(struct prestera_switch *sw,
+ enum prestera_event_type type,
+ prestera_event_cb_t fn)
+{
+ struct prestera_fw_event_handler *eh;
+
+ eh = __find_event_handler(sw, type);
+ if (!eh)
+ return;
+
+ list_del_rcu(&eh->list);
+ kfree_rcu(eh, rcu);
+}
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_hw.h b/drivers/net/ethernet/marvell/prestera/prestera_hw.h
new file mode 100644
index 000000000000..7f2e9c5f78dd
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/prestera_hw.h
@@ -0,0 +1,72 @@
+/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+ *
+ * Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved.
+ *
+ */
+
+#ifndef _PRESTERA_HW_H_
+#define _PRESTERA_HW_H_
+
+#include <linux/types.h>
+
+enum {
+ PRESTERA_PORT_TYPE_NONE,
+ PRESTERA_PORT_TYPE_TP,
+
+ PRESTERA_PORT_TYPE_MAX,
+};
+
+enum {
+ PRESTERA_PORT_FEC_OFF,
+
+ PRESTERA_PORT_FEC_MAX,
+};
+
+struct prestera_switch;
+struct prestera_port;
+struct prestera_port_stats;
+struct prestera_port_caps;
+enum prestera_event_type;
+struct prestera_event;
+
+typedef void (*prestera_event_cb_t)
+ (struct prestera_switch *sw, struct prestera_event *evt, void *arg);
+
+struct prestera_rxtx_params;
+
+/* Switch API */
+int prestera_hw_switch_init(struct prestera_switch *sw);
+void prestera_hw_switch_fini(struct prestera_switch *sw);
+int prestera_hw_switch_mac_set(struct prestera_switch *sw, char *mac);
+
+/* Port API */
+int prestera_hw_port_info_get(const struct prestera_port *port,
+ u32 *dev_id, u32 *hw_id, u16 *fp_id);
+int prestera_hw_port_state_set(const struct prestera_port *port,
+ bool admin_state);
+int prestera_hw_port_mtu_set(const struct prestera_port *port, u32 mtu);
+int prestera_hw_port_mtu_get(const struct prestera_port *port, u32 *mtu);
+int prestera_hw_port_mac_set(const struct prestera_port *port, char *mac);
+int prestera_hw_port_mac_get(const struct prestera_port *port, char *mac);
+int prestera_hw_port_cap_get(const struct prestera_port *port,
+ struct prestera_port_caps *caps);
+int prestera_hw_port_autoneg_set(const struct prestera_port *port,
+ bool autoneg, u64 link_modes, u8 fec);
+int prestera_hw_port_stats_get(const struct prestera_port *port,
+ struct prestera_port_stats *stats);
+
+/* Event handlers */
+int prestera_hw_event_handler_register(struct prestera_switch *sw,
+ enum prestera_event_type type,
+ prestera_event_cb_t fn,
+ void *arg);
+void prestera_hw_event_handler_unregister(struct prestera_switch *sw,
+ enum prestera_event_type type,
+ prestera_event_cb_t fn);
+
+/* RX/TX */
+int prestera_hw_rxtx_init(struct prestera_switch *sw,
+ struct prestera_rxtx_params *params);
+int prestera_hw_rxtx_port_init(struct prestera_port *port);
+
+#endif /* _PRESTERA_HW_H_ */
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_main.c b/drivers/net/ethernet/marvell/prestera/prestera_main.c
new file mode 100644
index 000000000000..dbe146163004
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/prestera_main.c
@@ -0,0 +1,496 @@
+// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+/* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/list.h>
+#include <linux/netdevice.h>
+#include <linux/netdev_features.h>
+#include <linux/etherdevice.h>
+#include <linux/jiffies.h>
+#include <linux/of.h>
+#include <linux/of_net.h>
+
+#include "prestera.h"
+#include "prestera_hw.h"
+#include "prestera_rxtx.h"
+
+#define PRESTERA_MTU_DEFAULT 1536
+
+#define PRESTERA_STATS_DELAY_MS msecs_to_jiffies(1000)
+
+static struct workqueue_struct *prestera_wq;
+
+struct prestera_port *prestera_port_find_by_hwid(struct prestera_switch *sw,
+ u32 dev_id, u32 hw_id)
+{
+ struct prestera_port *port;
+
+ list_for_each_entry(port, &sw->port_list, list) {
+ if (port->dev_id == dev_id && port->hw_id == hw_id)
+ return port;
+ }
+
+ return NULL;
+}
+
+static struct prestera_port *prestera_find_port(struct prestera_switch *sw,
+ u32 id)
+{
+ struct prestera_port *port;
+
+ list_for_each_entry(port, &sw->port_list, list) {
+ if (port->id == id)
+ break;
+ }
+
+ return port;
+}
+
+static int prestera_port_state_set(struct net_device *dev, bool is_up)
+{
+ struct prestera_port *port = netdev_priv(dev);
+ int err;
+
+ if (!is_up)
+ netif_stop_queue(dev);
+
+ err = prestera_hw_port_state_set(port, is_up);
+
+ if (is_up && !err)
+ netif_start_queue(dev);
+
+ return err;
+}
+
+static int prestera_port_open(struct net_device *dev)
+{
+ return prestera_port_state_set(dev, true);
+}
+
+static int prestera_port_close(struct net_device *dev)
+{
+ return prestera_port_state_set(dev, false);
+}
+
+static netdev_tx_t prestera_port_xmit(struct sk_buff *skb,
+ struct net_device *dev)
+{
+ return prestera_rxtx_xmit(netdev_priv(dev), skb);
+}
+
+static int prestera_is_valid_mac_addr(struct prestera_port *port, u8 *addr)
+{
+ if (!is_valid_ether_addr(addr))
+ return -EADDRNOTAVAIL;
+
+ if (memcmp(port->sw->base_mac, addr, ETH_ALEN - 1))
+ return -EINVAL;
+
+ return 0;
+}
+
+static int prestera_port_set_mac_address(struct net_device *dev, void *p)
+{
+ struct prestera_port *port = netdev_priv(dev);
+ struct sockaddr *addr = p;
+ int err;
+
+ err = prestera_is_valid_mac_addr(port, addr->sa_data);
+ if (err)
+ return err;
+
+ err = prestera_hw_port_mac_set(port, addr->sa_data);
+ if (err)
+ return err;
+
+ memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
+ return 0;
+}
+
+static int prestera_port_change_mtu(struct net_device *dev, int mtu)
+{
+ struct prestera_port *port = netdev_priv(dev);
+ int err;
+
+ err = prestera_hw_port_mtu_set(port, mtu);
+ if (err)
+ return err;
+
+ dev->mtu = mtu;
+ return 0;
+}
+
+static void prestera_port_get_stats64(struct net_device *dev,
+ struct rtnl_link_stats64 *stats)
+{
+ struct prestera_port *port = netdev_priv(dev);
+ struct prestera_port_stats *port_stats = &port->cached_hw_stats.stats;
+
+ stats->rx_packets = port_stats->broadcast_frames_received +
+ port_stats->multicast_frames_received +
+ port_stats->unicast_frames_received;
+
+ stats->tx_packets = port_stats->broadcast_frames_sent +
+ port_stats->multicast_frames_sent +
+ port_stats->unicast_frames_sent;
+
+ stats->rx_bytes = port_stats->good_octets_received;
+
+ stats->tx_bytes = port_stats->good_octets_sent;
+
+ stats->rx_errors = port_stats->rx_error_frame_received;
+ stats->tx_errors = port_stats->mac_trans_error;
+
+ stats->rx_dropped = port_stats->buffer_overrun;
+ stats->tx_dropped = 0;
+
+ stats->multicast = port_stats->multicast_frames_received;
+ stats->collisions = port_stats->excessive_collision;
+
+ stats->rx_crc_errors = port_stats->bad_crc;
+}
+
+static void prestera_port_get_hw_stats(struct prestera_port *port)
+{
+ prestera_hw_port_stats_get(port, &port->cached_hw_stats.stats);
+}
+
+static void prestera_port_stats_update(struct work_struct *work)
+{
+ struct prestera_port *port =
+ container_of(work, struct prestera_port,
+ cached_hw_stats.caching_dw.work);
+
+ prestera_port_get_hw_stats(port);
+
+ queue_delayed_work(prestera_wq, &port->cached_hw_stats.caching_dw,
+ PRESTERA_STATS_DELAY_MS);
+}
+
+static const struct net_device_ops netdev_ops = {
+ .ndo_open = prestera_port_open,
+ .ndo_stop = prestera_port_close,
+ .ndo_start_xmit = prestera_port_xmit,
+ .ndo_change_mtu = prestera_port_change_mtu,
+ .ndo_get_stats64 = prestera_port_get_stats64,
+ .ndo_set_mac_address = prestera_port_set_mac_address,
+};
+
+static int prestera_port_autoneg_set(struct prestera_port *port, bool enable,
+ u64 link_modes, u8 fec)
+{
+ bool refresh = false;
+ int err = 0;
+
+ if (port->caps.type != PRESTERA_PORT_TYPE_TP)
+ return enable ? -EINVAL : 0;
+
+ if (port->adver_link_modes != link_modes || port->adver_fec != fec) {
+ port->adver_fec = fec ?: BIT(PRESTERA_PORT_FEC_OFF);
+ port->adver_link_modes = link_modes;
+ refresh = true;
+ }
+
+ if (port->autoneg == enable && !(port->autoneg && refresh))
+ return 0;
+
+ err = prestera_hw_port_autoneg_set(port, enable, port->adver_link_modes,
+ port->adver_fec);
+ if (err)
+ return -EINVAL;
+
+ port->autoneg = enable;
+ return 0;
+}
+
+static int prestera_port_create(struct prestera_switch *sw, u32 id)
+{
+ struct prestera_port *port;
+ struct net_device *dev;
+ int err;
+
+ dev = alloc_etherdev(sizeof(*port));
+ if (!dev)
+ return -ENOMEM;
+
+ port = netdev_priv(dev);
+
+ port->dev = dev;
+ port->id = id;
+ port->sw = sw;
+
+ err = prestera_hw_port_info_get(port, &port->dev_id, &port->hw_id,
+ &port->fp_id);
+ if (err) {
+ dev_err(prestera_dev(sw), "Failed to get port(%u) info\n", id);
+ goto err_port_init;
+ }
+
+ dev->features |= NETIF_F_NETNS_LOCAL;
+ dev->netdev_ops = &netdev_ops;
+
+ netif_carrier_off(dev);
+
+ dev->mtu = min_t(unsigned int, sw->mtu_max, PRESTERA_MTU_DEFAULT);
+ dev->min_mtu = sw->mtu_min;
+ dev->max_mtu = sw->mtu_max;
+
+ err = prestera_hw_port_mtu_set(port, dev->mtu);
+ if (err) {
+ dev_err(prestera_dev(sw), "Failed to set port(%u) mtu(%d)\n",
+ id, dev->mtu);
+ goto err_port_init;
+ }
+
+ /* Only 0xFF mac addrs are supported */
+ if (port->fp_id >= 0xFF)
+ goto err_port_init;
+
+ memcpy(dev->dev_addr, sw->base_mac, dev->addr_len - 1);
+ dev->dev_addr[dev->addr_len - 1] = (char)port->fp_id;
+
+ err = prestera_hw_port_mac_set(port, dev->dev_addr);
+ if (err) {
+ dev_err(prestera_dev(sw), "Failed to set port(%u) mac addr\n", id);
+ goto err_port_init;
+ }
+
+ err = prestera_hw_port_cap_get(port, &port->caps);
+ if (err) {
+ dev_err(prestera_dev(sw), "Failed to get port(%u) caps\n", id);
+ goto err_port_init;
+ }
+
+ port->adver_fec = BIT(PRESTERA_PORT_FEC_OFF);
+ prestera_port_autoneg_set(port, true, port->caps.supp_link_modes,
+ port->caps.supp_fec);
+
+ err = prestera_hw_port_state_set(port, false);
+ if (err) {
+ dev_err(prestera_dev(sw), "Failed to set port(%u) down\n", id);
+ goto err_port_init;
+ }
+
+ err = prestera_rxtx_port_init(port);
+ if (err)
+ goto err_port_init;
+
+ INIT_DELAYED_WORK(&port->cached_hw_stats.caching_dw,
+ &prestera_port_stats_update);
+
+ list_add(&port->list, &sw->port_list);
+
+ err = register_netdev(dev);
+ if (err)
+ goto err_register_netdev;
+
+ return 0;
+
+err_register_netdev:
+ list_del(&port->list);
+err_port_init:
+ free_netdev(dev);
+ return err;
+}
+
+static void prestera_port_destroy(struct prestera_port *port)
+{
+ struct net_device *dev = port->dev;
+
+ cancel_delayed_work_sync(&port->cached_hw_stats.caching_dw);
+ unregister_netdev(dev);
+ list_del(&port->list);
+ free_netdev(dev);
+}
+
+static void prestera_destroy_ports(struct prestera_switch *sw)
+{
+ struct prestera_port *port, *tmp;
+
+ list_for_each_entry_safe(port, tmp, &sw->port_list, list)
+ prestera_port_destroy(port);
+}
+
+static int prestera_create_ports(struct prestera_switch *sw)
+{
+ struct prestera_port *port, *tmp;
+ u32 port_idx;
+ int err;
+
+ for (port_idx = 0; port_idx < sw->port_count; port_idx++) {
+ err = prestera_port_create(sw, port_idx);
+ if (err)
+ goto err_port_create;
+ }
+
+ return 0;
+
+err_port_create:
+ list_for_each_entry_safe(port, tmp, &sw->port_list, list)
+ prestera_port_destroy(port);
+
+ return err;
+}
+
+static void prestera_port_handle_event(struct prestera_switch *sw,
+ struct prestera_event *evt, void *arg)
+{
+ struct delayed_work *caching_dw;
+ struct prestera_port *port;
+
+ port = prestera_find_port(sw, evt->port_evt.port_id);
+ if (!port)
+ return;
+
+ caching_dw = &port->cached_hw_stats.caching_dw;
+
+ if (evt->id == PRESTERA_PORT_EVENT_STATE_CHANGED) {
+ if (evt->port_evt.data.oper_state) {
+ netif_carrier_on(port->dev);
+ if (!delayed_work_pending(caching_dw))
+ queue_delayed_work(prestera_wq, caching_dw, 0);
+ } else {
+ netif_carrier_off(port->dev);
+ if (delayed_work_pending(caching_dw))
+ cancel_delayed_work(caching_dw);
+ }
+ }
+}
+
+static int prestera_event_handlers_register(struct prestera_switch *sw)
+{
+ return prestera_hw_event_handler_register(sw, PRESTERA_EVENT_TYPE_PORT,
+ prestera_port_handle_event,
+ NULL);
+}
+
+static void prestera_event_handlers_unregister(struct prestera_switch *sw)
+{
+ prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_PORT,
+ prestera_port_handle_event);
+}
+
+static int prestera_switch_set_base_mac_addr(struct prestera_switch *sw)
+{
+ struct device_node *base_mac_np;
+ struct device_node *np;
+
+ np = of_find_compatible_node(NULL, NULL, "marvell,prestera");
+ if (np) {
+ base_mac_np = of_parse_phandle(np, "base-mac-provider", 0);
+ if (base_mac_np) {
+ const char *base_mac;
+
+ base_mac = of_get_mac_address(base_mac_np);
+ of_node_put(base_mac_np);
+ if (!IS_ERR(base_mac))
+ ether_addr_copy(sw->base_mac, base_mac);
+ }
+ }
+
+ if (!is_valid_ether_addr(sw->base_mac)) {
+ eth_random_addr(sw->base_mac);
+ dev_info(sw->dev->dev, "using random base mac address\n");
+ }
+
+ return prestera_hw_switch_mac_set(sw, sw->base_mac);
+}
+
+static int prestera_switch_init(struct prestera_switch *sw)
+{
+ int err;
+
+ err = prestera_hw_switch_init(sw);
+ if (err) {
+ dev_err(prestera_dev(sw), "Failed to init Switch device\n");
+ return err;
+ }
+
+ INIT_LIST_HEAD(&sw->port_list);
+
+ err = prestera_switch_set_base_mac_addr(sw);
+ if (err)
+ return err;
+
+ err = prestera_rxtx_switch_init(sw);
+ if (err)
+ return err;
+
+ err = prestera_event_handlers_register(sw);
+ if (err)
+ goto err_handlers_register;
+
+ err = prestera_create_ports(sw);
+ if (err)
+ goto err_ports_create;
+
+ return 0;
+
+err_ports_create:
+ prestera_event_handlers_unregister(sw);
+err_handlers_register:
+ prestera_rxtx_switch_fini(sw);
+ prestera_hw_switch_fini(sw);
+
+ return err;
+}
+
+static void prestera_switch_fini(struct prestera_switch *sw)
+{
+ prestera_destroy_ports(sw);
+ prestera_event_handlers_unregister(sw);
+ prestera_rxtx_switch_fini(sw);
+ prestera_hw_switch_fini(sw);
+}
+
+int prestera_device_register(struct prestera_device *dev)
+{
+ struct prestera_switch *sw;
+ int err;
+
+ sw = kzalloc(sizeof(*sw), GFP_KERNEL);
+ if (!sw)
+ return -ENOMEM;
+
+ dev->priv = sw;
+ sw->dev = dev;
+
+ err = prestera_switch_init(sw);
+ if (err) {
+ kfree(sw);
+ return err;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL(prestera_device_register);
+
+void prestera_device_unregister(struct prestera_device *dev)
+{
+ struct prestera_switch *sw = dev->priv;
+
+ prestera_switch_fini(sw);
+ kfree(sw);
+}
+EXPORT_SYMBOL(prestera_device_unregister);
+
+static int __init prestera_module_init(void)
+{
+ prestera_wq = alloc_workqueue("prestera", 0, 0);
+ if (!prestera_wq)
+ return -ENOMEM;
+
+ return 0;
+}
+
+static void __exit prestera_module_exit(void)
+{
+ destroy_workqueue(prestera_wq);
+}
+
+module_init(prestera_module_init);
+module_exit(prestera_module_exit);
+
+MODULE_LICENSE("Dual BSD/GPL");
+MODULE_DESCRIPTION("Marvell Prestera switch driver");
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_rxtx.c b/drivers/net/ethernet/marvell/prestera/prestera_rxtx.c
new file mode 100644
index 000000000000..13a6509d62c4
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/prestera_rxtx.c
@@ -0,0 +1,860 @@
+// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+/* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved */
+
+#include <linux/platform_device.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/dmapool.h>
+#include <linux/netdevice.h>
+#include <linux/etherdevice.h>
+#include <linux/if_vlan.h>
+
+#include "prestera.h"
+#include "prestera_hw.h"
+#include "prestera_dsa.h"
+#include "prestera_rxtx.h"
+
+struct prestera_sdma_desc {
+ __le32 word1;
+ __le32 word2;
+ __le32 buff;
+ __le32 next;
+} __packed __aligned(16);
+
+#define PRESTERA_SDMA_BUFF_SIZE_MAX 1544
+
+#define PRESTERA_SDMA_RX_DESC_PKT_LEN(desc) \
+ ((le32_to_cpu((desc)->word2) >> 16) & 0x3FFF)
+
+#define PRESTERA_SDMA_RX_DESC_OWNER(desc) \
+ ((le32_to_cpu((desc)->word1) & BIT(31)) >> 31)
+
+#define PRESTERA_SDMA_RX_DESC_IS_RCVD(desc) \
+ (PRESTERA_SDMA_RX_DESC_OWNER((desc)) == PRESTERA_SDMA_RX_DESC_CPU_OWN)
+
+#define PRESTERA_SDMA_RX_DESC_CPU_OWN 0
+#define PRESTERA_SDMA_RX_DESC_DMA_OWN 1
+
+#define PRESTERA_SDMA_RX_QUEUE_NUM 8
+
+#define PRESTERA_SDMA_RX_DESC_PER_Q 1000
+
+#define PRESTERA_SDMA_TX_DESC_PER_Q 1000
+#define PRESTERA_SDMA_TX_MAX_BURST 64
+
+#define PRESTERA_SDMA_TX_DESC_OWNER(desc) \
+ ((le32_to_cpu((desc)->word1) & BIT(31)) >> 31)
+
+#define PRESTERA_SDMA_TX_DESC_CPU_OWN 0
+#define PRESTERA_SDMA_TX_DESC_DMA_OWN 1
+
+#define PRESTERA_SDMA_TX_DESC_IS_SENT(desc) \
+ (PRESTERA_SDMA_TX_DESC_OWNER(desc) == PRESTERA_SDMA_TX_DESC_CPU_OWN)
+
+#define PRESTERA_SDMA_TX_DESC_LAST BIT(20)
+#define PRESTERA_SDMA_TX_DESC_FIRST BIT(21)
+#define PRESTERA_SDMA_TX_DESC_CALC_CRC BIT(12)
+
+#define PRESTERA_SDMA_TX_DESC_SINGLE \
+ (PRESTERA_SDMA_TX_DESC_FIRST | PRESTERA_SDMA_TX_DESC_LAST)
+
+#define PRESTERA_SDMA_TX_DESC_INIT \
+ (PRESTERA_SDMA_TX_DESC_SINGLE | PRESTERA_SDMA_TX_DESC_CALC_CRC)
+
+#define PRESTERA_SDMA_RX_INTR_MASK_REG 0x2814
+#define PRESTERA_SDMA_RX_QUEUE_STATUS_REG 0x2680
+#define PRESTERA_SDMA_RX_QUEUE_DESC_REG(n) (0x260C + (n) * 16)
+
+#define PRESTERA_SDMA_TX_QUEUE_DESC_REG 0x26C0
+#define PRESTERA_SDMA_TX_QUEUE_START_REG 0x2868
+
+struct prestera_sdma_buf {
+ struct prestera_sdma_desc *desc;
+ dma_addr_t desc_dma;
+ struct sk_buff *skb;
+ dma_addr_t buf_dma;
+ bool is_used;
+};
+
+struct prestera_rx_ring {
+ struct prestera_sdma_buf *bufs;
+ int next_rx;
+};
+
+struct prestera_tx_ring {
+ struct prestera_sdma_buf *bufs;
+ int next_tx;
+ int max_burst;
+ int burst;
+};
+
+struct prestera_sdma {
+ struct prestera_rx_ring rx_ring[PRESTERA_SDMA_RX_QUEUE_NUM];
+ struct prestera_tx_ring tx_ring;
+ struct prestera_switch *sw;
+ struct dma_pool *desc_pool;
+ struct work_struct tx_work;
+ struct napi_struct rx_napi;
+ struct net_device napi_dev;
+ u32 map_addr;
+ u64 dma_mask;
+ /* protect SDMA with concurrrent access from multiple CPUs */
+ spinlock_t tx_lock;
+};
+
+struct prestera_rxtx {
+ struct prestera_sdma sdma;
+};
+
+static int prestera_sdma_buf_init(struct prestera_sdma *sdma,
+ struct prestera_sdma_buf *buf)
+{
+ struct device *dma_dev = sdma->sw->dev->dev;
+ struct prestera_sdma_desc *desc;
+ dma_addr_t dma;
+
+ desc = dma_pool_alloc(sdma->desc_pool, GFP_DMA | GFP_KERNEL, &dma);
+ if (!desc)
+ return -ENOMEM;
+
+ if (dma + sizeof(struct prestera_sdma_desc) > sdma->dma_mask) {
+ dev_err(dma_dev, "failed to alloc desc\n");
+ dma_pool_free(sdma->desc_pool, desc, dma);
+ return -ENOMEM;
+ }
+
+ buf->buf_dma = DMA_MAPPING_ERROR;
+ buf->desc_dma = dma;
+ buf->desc = desc;
+ buf->skb = NULL;
+
+ return 0;
+}
+
+static u32 prestera_sdma_map(struct prestera_sdma *sdma, dma_addr_t pa)
+{
+ return sdma->map_addr + pa;
+}
+
+static void prestera_sdma_rx_desc_set_len(struct prestera_sdma_desc *desc,
+ size_t val)
+{
+ u32 word = le32_to_cpu(desc->word2);
+
+ word = (word & ~GENMASK(15, 0)) | val;
+ desc->word2 = cpu_to_le32(word);
+}
+
+static void prestera_sdma_rx_desc_init(struct prestera_sdma *sdma,
+ struct prestera_sdma_desc *desc,
+ dma_addr_t buf)
+{
+ prestera_sdma_rx_desc_set_len(desc, PRESTERA_SDMA_BUFF_SIZE_MAX);
+ desc->buff = cpu_to_le32(prestera_sdma_map(sdma, buf));
+
+ /* make sure buffer is set before reset the descriptor */
+ wmb();
+
+ desc->word1 = cpu_to_le32(0xA0000000);
+}
+
+static void prestera_sdma_rx_desc_set_next(struct prestera_sdma *sdma,
+ struct prestera_sdma_desc *desc,
+ dma_addr_t next)
+{
+ desc->next = cpu_to_le32(prestera_sdma_map(sdma, next));
+}
+
+static int prestera_sdma_rx_skb_alloc(struct prestera_sdma *sdma,
+ struct prestera_sdma_buf *buf)
+{
+ struct device *dev = sdma->sw->dev->dev;
+ struct sk_buff *skb;
+ dma_addr_t dma;
+
+ skb = alloc_skb(PRESTERA_SDMA_BUFF_SIZE_MAX, GFP_DMA | GFP_ATOMIC);
+ if (!skb)
+ return -ENOMEM;
+
+ dma = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
+
+ if (dma_mapping_error(dev, dma))
+ goto err_dma_map;
+ if (dma + skb->len > sdma->dma_mask)
+ goto err_dma_range;
+
+ if (buf->skb)
+ dma_unmap_single(dev, buf->buf_dma, buf->skb->len,
+ DMA_FROM_DEVICE);
+
+ buf->buf_dma = dma;
+ buf->skb = skb;
+ return 0;
+
+err_dma_range:
+ dma_unmap_single(dev, dma, skb->len, DMA_FROM_DEVICE);
+err_dma_map:
+ kfree_skb(skb);
+
+ return -ENOMEM;
+}
+
+static struct sk_buff *prestera_sdma_rx_skb_get(struct prestera_sdma *sdma,
+ struct prestera_sdma_buf *buf)
+{
+ dma_addr_t buf_dma = buf->buf_dma;
+ struct sk_buff *skb = buf->skb;
+ u32 len = skb->len;
+ int err;
+
+ err = prestera_sdma_rx_skb_alloc(sdma, buf);
+ if (err) {
+ buf->buf_dma = buf_dma;
+ buf->skb = skb;
+
+ skb = alloc_skb(skb->len, GFP_ATOMIC);
+ if (skb) {
+ skb_put(skb, len);
+ skb_copy_from_linear_data(buf->skb, skb->data, len);
+ }
+ }
+
+ prestera_sdma_rx_desc_init(sdma, buf->desc, buf->buf_dma);
+
+ return skb;
+}
+
+static int prestera_rxtx_process_skb(struct prestera_sdma *sdma,
+ struct sk_buff *skb)
+{
+ const struct prestera_port *port;
+ struct prestera_dsa dsa;
+ u32 hw_port, dev_id;
+ int err;
+
+ skb_pull(skb, ETH_HLEN);
+
+ /* ethertype field is part of the dsa header */
+ err = prestera_dsa_parse(&dsa, skb->data - ETH_TLEN);
+ if (err)
+ return err;
+
+ dev_id = dsa.hw_dev_num;
+ hw_port = dsa.port_num;
+
+ port = prestera_port_find_by_hwid(sdma->sw, dev_id, hw_port);
+ if (unlikely(!port)) {
+ pr_warn_ratelimited("received pkt for non-existent port(%u, %u)\n",
+ dev_id, hw_port);
+ return -EEXIST;
+ }
+
+ if (unlikely(!pskb_may_pull(skb, PRESTERA_DSA_HLEN)))
+ return -EINVAL;
+
+ /* remove DSA tag and update checksum */
+ skb_pull_rcsum(skb, PRESTERA_DSA_HLEN);
+
+ memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - PRESTERA_DSA_HLEN,
+ ETH_ALEN * 2);
+
+ skb_push(skb, ETH_HLEN);
+
+ skb->protocol = eth_type_trans(skb, port->dev);
+
+ if (dsa.vlan.is_tagged) {
+ u16 tci = dsa.vlan.vid & VLAN_VID_MASK;
+
+ tci |= dsa.vlan.vpt << VLAN_PRIO_SHIFT;
+ if (dsa.vlan.cfi_bit)
+ tci |= VLAN_CFI_MASK;
+
+ __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), tci);
+ }
+
+ return 0;
+}
+
+static int prestera_sdma_next_rx_buf_idx(int buf_idx)
+{
+ return (buf_idx + 1) % PRESTERA_SDMA_RX_DESC_PER_Q;
+}
+
+static int prestera_sdma_rx_poll(struct napi_struct *napi, int budget)
+{
+ int qnum = PRESTERA_SDMA_RX_QUEUE_NUM;
+ unsigned int rxq_done_map = 0;
+ struct prestera_sdma *sdma;
+ struct list_head rx_list;
+ unsigned int qmask;
+ int pkts_done = 0;
+ int q;
+
+ qnum = PRESTERA_SDMA_RX_QUEUE_NUM;
+ qmask = GENMASK(qnum - 1, 0);
+
+ INIT_LIST_HEAD(&rx_list);
+
+ sdma = container_of(napi, struct prestera_sdma, rx_napi);
+
+ while (pkts_done < budget && rxq_done_map != qmask) {
+ for (q = 0; q < qnum && pkts_done < budget; q++) {
+ struct prestera_rx_ring *ring = &sdma->rx_ring[q];
+ struct prestera_sdma_desc *desc;
+ struct prestera_sdma_buf *buf;
+ int buf_idx = ring->next_rx;
+ struct sk_buff *skb;
+
+ buf = &ring->bufs[buf_idx];
+ desc = buf->desc;
+
+ if (PRESTERA_SDMA_RX_DESC_IS_RCVD(desc)) {
+ rxq_done_map &= ~BIT(q);
+ } else {
+ rxq_done_map |= BIT(q);
+ continue;
+ }
+
+ pkts_done++;
+
+ __skb_trim(buf->skb, PRESTERA_SDMA_RX_DESC_PKT_LEN(desc));
+
+ skb = prestera_sdma_rx_skb_get(sdma, buf);
+ if (!skb)
+ goto rx_next_buf;
+
+ if (unlikely(prestera_rxtx_process_skb(sdma, skb)))
+ goto rx_next_buf;
+
+ list_add_tail(&skb->list, &rx_list);
+rx_next_buf:
+ ring->next_rx = prestera_sdma_next_rx_buf_idx(buf_idx);
+ }
+ }
+
+ if (pkts_done < budget && napi_complete_done(napi, pkts_done))
+ prestera_write(sdma->sw, PRESTERA_SDMA_RX_INTR_MASK_REG,
+ 0xff << 2);
+
+ netif_receive_skb_list(&rx_list);
+
+ return pkts_done;
+}
+
+static void prestera_sdma_rx_fini(struct prestera_sdma *sdma)
+{
+ int qnum = PRESTERA_SDMA_RX_QUEUE_NUM;
+ int q, b;
+
+ /* disable all rx queues */
+ prestera_write(sdma->sw, PRESTERA_SDMA_RX_QUEUE_STATUS_REG, 0xff00);
+
+ for (q = 0; q < qnum; q++) {
+ struct prestera_rx_ring *ring = &sdma->rx_ring[q];
+
+ if (!ring->bufs)
+ break;
+
+ for (b = 0; b < PRESTERA_SDMA_RX_DESC_PER_Q; b++) {
+ struct prestera_sdma_buf *buf = &ring->bufs[b];
+
+ if (buf->desc_dma)
+ dma_pool_free(sdma->desc_pool, buf->desc,
+ buf->desc_dma);
+
+ if (!buf->skb)
+ continue;
+
+ if (buf->buf_dma != DMA_MAPPING_ERROR)
+ dma_unmap_single(sdma->sw->dev->dev,
+ buf->buf_dma, buf->skb->len,
+ DMA_FROM_DEVICE);
+ kfree_skb(buf->skb);
+ }
+ }
+}
+
+static int prestera_sdma_rx_init(struct prestera_sdma *sdma)
+{
+ int bnum = PRESTERA_SDMA_RX_DESC_PER_Q;
+ int qnum = PRESTERA_SDMA_RX_QUEUE_NUM;
+ int q, b;
+ int err;
+
+ /* disable all rx queues */
+ prestera_write(sdma->sw, PRESTERA_SDMA_RX_QUEUE_STATUS_REG, 0xff00);
+
+ for (q = 0; q < qnum; q++) {
+ struct prestera_rx_ring *ring = &sdma->rx_ring[q];
+ struct prestera_sdma_buf *head;
+
+ ring->bufs = kmalloc_array(bnum, sizeof(*head), GFP_KERNEL);
+ if (!ring->bufs)
+ return -ENOMEM;
+
+ head = &ring->bufs[0];
+ ring->next_rx = 0;
+
+ for (b = 0; b < bnum; b++) {
+ struct prestera_sdma_buf *buf = &ring->bufs[b];
+
+ err = prestera_sdma_buf_init(sdma, buf);
+ if (err)
+ return err;
+
+ err = prestera_sdma_rx_skb_alloc(sdma, buf);
+ if (err)
+ return err;
+
+ prestera_sdma_rx_desc_init(sdma, buf->desc,
+ buf->buf_dma);
+
+ if (b == 0)
+ continue;
+
+ prestera_sdma_rx_desc_set_next(sdma,
+ ring->bufs[b - 1].desc,
+ buf->desc_dma);
+
+ if (b == PRESTERA_SDMA_RX_DESC_PER_Q - 1)
+ prestera_sdma_rx_desc_set_next(sdma, buf->desc,
+ head->desc_dma);
+ }
+
+ prestera_write(sdma->sw, PRESTERA_SDMA_RX_QUEUE_DESC_REG(q),
+ prestera_sdma_map(sdma, head->desc_dma));
+ }
+
+ /* make sure all rx descs are filled before enabling all rx queues */
+ wmb();
+
+ prestera_write(sdma->sw, PRESTERA_SDMA_RX_QUEUE_STATUS_REG, 0xff);
+
+ return 0;
+}
+
+static void prestera_sdma_tx_desc_init(struct prestera_sdma *sdma,
+ struct prestera_sdma_desc *desc)
+{
+ desc->word1 = cpu_to_le32(PRESTERA_SDMA_TX_DESC_INIT);
+ desc->word2 = 0;
+}
+
+static void prestera_sdma_tx_desc_set_next(struct prestera_sdma *sdma,
+ struct prestera_sdma_desc *desc,
+ dma_addr_t next)
+{
+ desc->next = cpu_to_le32(prestera_sdma_map(sdma, next));
+}
+
+static void prestera_sdma_tx_desc_set_buf(struct prestera_sdma *sdma,
+ struct prestera_sdma_desc *desc,
+ dma_addr_t buf, size_t len)
+{
+ u32 word = le32_to_cpu(desc->word2);
+
+ word = (word & ~GENMASK(30, 16)) | ((len + ETH_FCS_LEN) << 16);
+
+ desc->buff = cpu_to_le32(prestera_sdma_map(sdma, buf));
+ desc->word2 = cpu_to_le32(word);
+}
+
+static void prestera_sdma_tx_desc_xmit(struct prestera_sdma_desc *desc)
+{
+ u32 word = le32_to_cpu(desc->word1);
+
+ word |= PRESTERA_SDMA_TX_DESC_DMA_OWN << 31;
+
+ /* make sure everything is written before enable xmit */
+ wmb();
+
+ desc->word1 = cpu_to_le32(word);
+}
+
+static int prestera_sdma_tx_buf_map(struct prestera_sdma *sdma,
+ struct prestera_sdma_buf *buf,
+ struct sk_buff *skb)
+{
+ struct device *dma_dev = sdma->sw->dev->dev;
+ struct sk_buff *new_skb;
+ size_t len = skb->len;
+ dma_addr_t dma;
+
+ dma = dma_map_single(dma_dev, skb->data, len, DMA_TO_DEVICE);
+ if (!dma_mapping_error(dma_dev, dma) && dma + len <= sdma->dma_mask) {
+ buf->buf_dma = dma;
+ buf->skb = skb;
+ return 0;
+ }
+
+ if (!dma_mapping_error(dma_dev, dma))
+ dma_unmap_single(dma_dev, dma, len, DMA_TO_DEVICE);
+
+ new_skb = alloc_skb(len, GFP_ATOMIC | GFP_DMA);
+ if (!new_skb)
+ goto err_alloc_skb;
+
+ dma = dma_map_single(dma_dev, new_skb->data, len, DMA_TO_DEVICE);
+ if (dma_mapping_error(dma_dev, dma))
+ goto err_dma_map;
+ if (dma + len > sdma->dma_mask)
+ goto err_dma_range;
+
+ skb_copy_from_linear_data(skb, skb_put(new_skb, len), len);
+
+ dev_consume_skb_any(skb);
+
+ buf->skb = new_skb;
+ buf->buf_dma = dma;
+
+ return 0;
+
+err_dma_range:
+ dma_unmap_single(dma_dev, dma, len, DMA_TO_DEVICE);
+err_dma_map:
+ dev_kfree_skb(new_skb);
+err_alloc_skb:
+ dev_kfree_skb(skb);
+
+ return -ENOMEM;
+}
+
+static void prestera_sdma_tx_buf_unmap(struct prestera_sdma *sdma,
+ struct prestera_sdma_buf *buf)
+{
+ struct device *dma_dev = sdma->sw->dev->dev;
+
+ dma_unmap_single(dma_dev, buf->buf_dma, buf->skb->len, DMA_TO_DEVICE);
+}
+
+static void prestera_sdma_tx_recycle_work_fn(struct work_struct *work)
+{
+ int bnum = PRESTERA_SDMA_TX_DESC_PER_Q;
+ struct prestera_tx_ring *tx_ring;
+ struct prestera_sdma *sdma;
+ int b;
+
+ sdma = container_of(work, struct prestera_sdma, tx_work);
+
+ tx_ring = &sdma->tx_ring;
+
+ for (b = 0; b < bnum; b++) {
+ struct prestera_sdma_buf *buf = &tx_ring->bufs[b];
+
+ if (!buf->is_used)
+ continue;
+
+ if (!PRESTERA_SDMA_TX_DESC_IS_SENT(buf->desc))
+ continue;
+
+ prestera_sdma_tx_buf_unmap(sdma, buf);
+ dev_consume_skb_any(buf->skb);
+ buf->skb = NULL;
+
+ /* make sure everything is cleaned up */
+ wmb();
+
+ buf->is_used = false;
+ }
+}
+
+static int prestera_sdma_tx_init(struct prestera_sdma *sdma)
+{
+ struct prestera_tx_ring *tx_ring = &sdma->tx_ring;
+ int bnum = PRESTERA_SDMA_TX_DESC_PER_Q;
+ struct prestera_sdma_buf *head;
+ int err;
+ int b;
+
+ INIT_WORK(&sdma->tx_work, prestera_sdma_tx_recycle_work_fn);
+ spin_lock_init(&sdma->tx_lock);
+
+ tx_ring->bufs = kmalloc_array(bnum, sizeof(*head), GFP_KERNEL);
+ if (!tx_ring->bufs)
+ return -ENOMEM;
+
+ head = &tx_ring->bufs[0];
+
+ tx_ring->max_burst = PRESTERA_SDMA_TX_MAX_BURST;
+ tx_ring->burst = tx_ring->max_burst;
+ tx_ring->next_tx = 0;
+
+ for (b = 0; b < bnum; b++) {
+ struct prestera_sdma_buf *buf = &tx_ring->bufs[b];
+
+ err = prestera_sdma_buf_init(sdma, buf);
+ if (err)
+ return err;
+
+ prestera_sdma_tx_desc_init(sdma, buf->desc);
+
+ buf->is_used = false;
+
+ if (b == 0)
+ continue;
+
+ prestera_sdma_tx_desc_set_next(sdma, tx_ring->bufs[b - 1].desc,
+ buf->desc_dma);
+
+ if (b == PRESTERA_SDMA_TX_DESC_PER_Q - 1)
+ prestera_sdma_tx_desc_set_next(sdma, buf->desc,
+ head->desc_dma);
+ }
+
+ /* make sure descriptors are written */
+ wmb();
+
+ prestera_write(sdma->sw, PRESTERA_SDMA_TX_QUEUE_DESC_REG,
+ prestera_sdma_map(sdma, head->desc_dma));
+
+ return 0;
+}
+
+static void prestera_sdma_tx_fini(struct prestera_sdma *sdma)
+{
+ struct prestera_tx_ring *ring = &sdma->tx_ring;
+ int bnum = PRESTERA_SDMA_TX_DESC_PER_Q;
+ int b;
+
+ cancel_work_sync(&sdma->tx_work);
+
+ if (!ring->bufs)
+ return;
+
+ for (b = 0; b < bnum; b++) {
+ struct prestera_sdma_buf *buf = &ring->bufs[b];
+
+ if (buf->desc)
+ dma_pool_free(sdma->desc_pool, buf->desc,
+ buf->desc_dma);
+
+ if (!buf->skb)
+ continue;
+
+ dma_unmap_single(sdma->sw->dev->dev, buf->buf_dma,
+ buf->skb->len, DMA_TO_DEVICE);
+
+ dev_consume_skb_any(buf->skb);
+ }
+}
+
+static void prestera_rxtx_handle_event(struct prestera_switch *sw,
+ struct prestera_event *evt,
+ void *arg)
+{
+ struct prestera_sdma *sdma = arg;
+
+ if (evt->id != PRESTERA_RXTX_EVENT_RCV_PKT)
+ return;
+
+ prestera_write(sdma->sw, PRESTERA_SDMA_RX_INTR_MASK_REG, 0);
+ napi_schedule(&sdma->rx_napi);
+}
+
+static int prestera_sdma_switch_init(struct prestera_switch *sw)
+{
+ struct prestera_sdma *sdma = &sw->rxtx->sdma;
+ struct device *dev = sw->dev->dev;
+ struct prestera_rxtx_params p;
+ int err;
+
+ p.use_sdma = true;
+
+ err = prestera_hw_rxtx_init(sw, &p);
+ if (err) {
+ dev_err(dev, "failed to init rxtx by hw\n");
+ return err;
+ }
+
+ sdma->dma_mask = dma_get_mask(dev);
+ sdma->map_addr = p.map_addr;
+ sdma->sw = sw;
+
+ sdma->desc_pool = dma_pool_create("desc_pool", dev,
+ sizeof(struct prestera_sdma_desc),
+ 16, 0);
+ if (!sdma->desc_pool)
+ return -ENOMEM;
+
+ err = prestera_sdma_rx_init(sdma);
+ if (err) {
+ dev_err(dev, "failed to init rx ring\n");
+ goto err_rx_init;
+ }
+
+ err = prestera_sdma_tx_init(sdma);
+ if (err) {
+ dev_err(dev, "failed to init tx ring\n");
+ goto err_tx_init;
+ }
+
+ err = prestera_hw_event_handler_register(sw, PRESTERA_EVENT_TYPE_RXTX,
+ prestera_rxtx_handle_event,
+ sdma);
+ if (err)
+ goto err_evt_register;
+
+ init_dummy_netdev(&sdma->napi_dev);
+
+ netif_napi_add(&sdma->napi_dev, &sdma->rx_napi, prestera_sdma_rx_poll, 64);
+ napi_enable(&sdma->rx_napi);
+
+ return 0;
+
+err_evt_register:
+err_tx_init:
+ prestera_sdma_tx_fini(sdma);
+err_rx_init:
+ prestera_sdma_rx_fini(sdma);
+
+ dma_pool_destroy(sdma->desc_pool);
+ return err;
+}
+
+static void prestera_sdma_switch_fini(struct prestera_switch *sw)
+{
+ struct prestera_sdma *sdma = &sw->rxtx->sdma;
+
+ napi_disable(&sdma->rx_napi);
+ netif_napi_del(&sdma->rx_napi);
+ prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_RXTX,
+ prestera_rxtx_handle_event);
+ prestera_sdma_tx_fini(sdma);
+ prestera_sdma_rx_fini(sdma);
+ dma_pool_destroy(sdma->desc_pool);
+}
+
+static bool prestera_sdma_is_ready(struct prestera_sdma *sdma)
+{
+ return !(prestera_read(sdma->sw, PRESTERA_SDMA_TX_QUEUE_START_REG) & 1);
+}
+
+static int prestera_sdma_tx_wait(struct prestera_sdma *sdma,
+ struct prestera_tx_ring *tx_ring)
+{
+ int tx_retry_num = 10 * tx_ring->max_burst;
+
+ while (--tx_retry_num) {
+ if (prestera_sdma_is_ready(sdma))
+ return 0;
+
+ udelay(1);
+ }
+
+ return -EBUSY;
+}
+
+static void prestera_sdma_tx_start(struct prestera_sdma *sdma)
+{
+ prestera_write(sdma->sw, PRESTERA_SDMA_TX_QUEUE_START_REG, 1);
+ schedule_work(&sdma->tx_work);
+}
+
+static netdev_tx_t prestera_sdma_xmit(struct prestera_sdma *sdma,
+ struct sk_buff *skb)
+{
+ struct device *dma_dev = sdma->sw->dev->dev;
+ struct net_device *dev = skb->dev;
+ struct prestera_tx_ring *tx_ring;
+ struct prestera_sdma_buf *buf;
+ int err;
+
+ spin_lock(&sdma->tx_lock);
+
+ tx_ring = &sdma->tx_ring;
+
+ buf = &tx_ring->bufs[tx_ring->next_tx];
+ if (buf->is_used) {
+ schedule_work(&sdma->tx_work);
+ goto drop_skb;
+ }
+
+ if (unlikely(eth_skb_pad(skb)))
+ goto drop_skb_nofree;
+
+ err = prestera_sdma_tx_buf_map(sdma, buf, skb);
+ if (err)
+ goto drop_skb;
+
+ prestera_sdma_tx_desc_set_buf(sdma, buf->desc, buf->buf_dma, skb->len);
+
+ dma_sync_single_for_device(dma_dev, buf->buf_dma, skb->len,
+ DMA_TO_DEVICE);
+
+ if (!tx_ring->burst--) {
+ tx_ring->burst = tx_ring->max_burst;
+
+ err = prestera_sdma_tx_wait(sdma, tx_ring);
+ if (err)
+ goto drop_skb_unmap;
+ }
+
+ tx_ring->next_tx = (tx_ring->next_tx + 1) % PRESTERA_SDMA_TX_DESC_PER_Q;
+ prestera_sdma_tx_desc_xmit(buf->desc);
+ buf->is_used = true;
+
+ prestera_sdma_tx_start(sdma);
+
+ goto tx_done;
+
+drop_skb_unmap:
+ prestera_sdma_tx_buf_unmap(sdma, buf);
+drop_skb:
+ dev_consume_skb_any(skb);
+drop_skb_nofree:
+ dev->stats.tx_dropped++;
+tx_done:
+ spin_unlock(&sdma->tx_lock);
+ return NETDEV_TX_OK;
+}
+
+int prestera_rxtx_switch_init(struct prestera_switch *sw)
+{
+ struct prestera_rxtx *rxtx;
+
+ rxtx = kzalloc(sizeof(*rxtx), GFP_KERNEL);
+ if (!rxtx)
+ return -ENOMEM;
+
+ sw->rxtx = rxtx;
+
+ return prestera_sdma_switch_init(sw);
+}
+
+void prestera_rxtx_switch_fini(struct prestera_switch *sw)
+{
+ prestera_sdma_switch_fini(sw);
+ kfree(sw->rxtx);
+}
+
+int prestera_rxtx_port_init(struct prestera_port *port)
+{
+ int err;
+
+ err = prestera_hw_rxtx_port_init(port);
+ if (err)
+ return err;
+
+ port->dev->needed_headroom = PRESTERA_DSA_HLEN;
+ return 0;
+}
+
+netdev_tx_t prestera_rxtx_xmit(struct prestera_port *port, struct sk_buff *skb)
+{
+ struct prestera_dsa dsa;
+
+ dsa.hw_dev_num = port->dev_id;
+ dsa.port_num = port->hw_id;
+
+ if (skb_cow_head(skb, PRESTERA_DSA_HLEN) < 0)
+ return NET_XMIT_DROP;
+
+ skb_push(skb, PRESTERA_DSA_HLEN);
+ memmove(skb->data, skb->data + PRESTERA_DSA_HLEN, 2 * ETH_ALEN);
+
+ if (prestera_dsa_build(&dsa, skb->data + 2 * ETH_ALEN) != 0)
+ return NET_XMIT_DROP;
+
+ return prestera_sdma_xmit(&port->sw->rxtx->sdma, skb);
+}
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_rxtx.h b/drivers/net/ethernet/marvell/prestera/prestera_rxtx.h
new file mode 100644
index 000000000000..bbbadfa5accf
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/prestera_rxtx.h
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+ *
+ * Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved.
+ *
+ */
+
+#ifndef _PRESTERA_RXTX_H_
+#define _PRESTERA_RXTX_H_
+
+#include <linux/netdevice.h>
+
+#include "prestera.h"
+
+int prestera_rxtx_switch_init(struct prestera_switch *sw);
+void prestera_rxtx_switch_fini(struct prestera_switch *sw);
+
+int prestera_rxtx_port_init(struct prestera_port *port);
+
+netdev_tx_t prestera_rxtx_xmit(struct prestera_port *port, struct sk_buff *skb);
+
+#endif /* _PRESTERA_RXTX_H_ */
--
2.17.1
Add very basic support for devlink interface:
- driver name
- fw version
- devlink ports
Signed-off-by: Vadym Kochan <[email protected]>
---
drivers/net/ethernet/marvell/prestera/Kconfig | 1 +
.../net/ethernet/marvell/prestera/Makefile | 2 +-
.../net/ethernet/marvell/prestera/prestera.h | 4 +
.../marvell/prestera/prestera_devlink.c | 120 ++++++++++++++++++
.../marvell/prestera/prestera_devlink.h | 26 ++++
.../ethernet/marvell/prestera/prestera_main.c | 28 +++-
6 files changed, 176 insertions(+), 5 deletions(-)
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_devlink.c
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_devlink.h
diff --git a/drivers/net/ethernet/marvell/prestera/Kconfig b/drivers/net/ethernet/marvell/prestera/Kconfig
index d30e3e6d8b7b..7926960d1967 100644
--- a/drivers/net/ethernet/marvell/prestera/Kconfig
+++ b/drivers/net/ethernet/marvell/prestera/Kconfig
@@ -6,6 +6,7 @@
config PRESTERA
tristate "Marvell Prestera Switch ASICs support"
depends on NET_SWITCHDEV && VLAN_8021Q
+ select NET_DEVLINK
help
This driver supports Marvell Prestera Switch ASICs family.
diff --git a/drivers/net/ethernet/marvell/prestera/Makefile b/drivers/net/ethernet/marvell/prestera/Makefile
index 2146714eab21..babd71fba809 100644
--- a/drivers/net/ethernet/marvell/prestera/Makefile
+++ b/drivers/net/ethernet/marvell/prestera/Makefile
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_PRESTERA) += prestera.o
prestera-objs := prestera_main.o prestera_hw.o prestera_dsa.o \
- prestera_rxtx.o
+ prestera_rxtx.o prestera_devlink.o
obj-$(CONFIG_PRESTERA_PCI) += prestera_pci.o
diff --git a/drivers/net/ethernet/marvell/prestera/prestera.h b/drivers/net/ethernet/marvell/prestera/prestera.h
index 5079d872e18a..f8abaaff5f21 100644
--- a/drivers/net/ethernet/marvell/prestera/prestera.h
+++ b/drivers/net/ethernet/marvell/prestera/prestera.h
@@ -11,6 +11,9 @@
#include <linux/notifier.h>
#include <uapi/linux/if_ether.h>
#include <linux/workqueue.h>
+#include <net/devlink.h>
+
+#define PRESTERA_DRV_NAME "prestera"
struct prestera_fw_rev {
u16 maj;
@@ -63,6 +66,7 @@ struct prestera_port_caps {
struct prestera_port {
struct net_device *dev;
struct prestera_switch *sw;
+ struct devlink_port dl_port;
u32 id;
u32 hw_id;
u32 dev_id;
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_devlink.c b/drivers/net/ethernet/marvell/prestera/prestera_devlink.c
new file mode 100644
index 000000000000..dd09f6ee1c3e
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/prestera_devlink.c
@@ -0,0 +1,120 @@
+// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+/* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved */
+
+#include <net/devlink.h>
+
+#include "prestera.h"
+#include "prestera_devlink.h"
+
+static int prestera_dl_info_get(struct devlink *dl,
+ struct devlink_info_req *req,
+ struct netlink_ext_ack *extack)
+{
+ struct prestera_switch *sw = devlink_priv(dl);
+ char buf[16];
+ int err = 0;
+
+ err = devlink_info_driver_name_put(req, PRESTERA_DRV_NAME);
+ if (err)
+ return err;
+
+ snprintf(buf, sizeof(buf), "%d.%d.%d",
+ sw->dev->fw_rev.maj,
+ sw->dev->fw_rev.min,
+ sw->dev->fw_rev.sub);
+
+ err = devlink_info_version_running_put(req,
+ DEVLINK_INFO_VERSION_GENERIC_FW,
+ buf);
+ if (err)
+ return err;
+
+ return 0;
+}
+
+static const struct devlink_ops prestera_dl_ops = {
+ .info_get = prestera_dl_info_get,
+};
+
+struct prestera_switch *prestera_devlink_alloc(void)
+{
+ struct devlink *dl;
+
+ dl = devlink_alloc(&prestera_dl_ops, sizeof(struct prestera_switch));
+
+ return devlink_priv(dl);
+}
+
+void prestera_devlink_free(struct prestera_switch *sw)
+{
+ struct devlink *dl = priv_to_devlink(sw);
+
+ devlink_free(dl);
+}
+
+int prestera_devlink_register(struct prestera_switch *sw)
+{
+ struct devlink *dl = priv_to_devlink(sw);
+ int err;
+
+ err = devlink_register(dl, sw->dev->dev);
+ if (err) {
+ dev_warn(sw->dev->dev, "devlink_register failed: %d\n", err);
+ return err;
+ }
+
+ return 0;
+}
+
+void prestera_devlink_unregister(struct prestera_switch *sw)
+{
+ struct devlink *dl = priv_to_devlink(sw);
+
+ devlink_unregister(dl);
+}
+
+int prestera_devlink_port_register(struct prestera_port *port)
+{
+ struct devlink *dl = priv_to_devlink(port->sw);
+ struct devlink_port_attrs attrs = {};
+ struct prestera_switch *sw;
+ int err;
+
+ sw = port->sw;
+ dl = priv_to_devlink(sw);
+
+ attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
+ attrs.phys.port_number = port->fp_id;
+ attrs.switch_id.id_len = sizeof(port->sw->id);
+ memcpy(attrs.switch_id.id, &port->sw->id, attrs.switch_id.id_len);
+
+ devlink_port_attrs_set(&port->dl_port, &attrs);
+
+ err = devlink_port_register(dl, &port->dl_port, port->fp_id);
+ if (err)
+ dev_err(sw->dev->dev, "devlink_port_register failed: %d\n", err);
+
+ return 0;
+}
+
+void prestera_devlink_port_unregister(struct prestera_port *port)
+{
+ devlink_port_unregister(&port->dl_port);
+}
+
+void prestera_devlink_port_set(struct prestera_port *port)
+{
+ devlink_port_type_eth_set(&port->dl_port, port->dev);
+}
+
+void prestera_devlink_port_clear(struct prestera_port *port)
+{
+ devlink_port_type_clear(&port->dl_port);
+}
+
+struct devlink_port *prestera_devlink_get_port(struct net_device *dev)
+{
+ struct prestera_port *port = netdev_priv(dev);
+
+ return &port->dl_port;
+}
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_devlink.h b/drivers/net/ethernet/marvell/prestera/prestera_devlink.h
new file mode 100644
index 000000000000..b0793c948148
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/prestera_devlink.h
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+ *
+ * Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved.
+ *
+ */
+
+#ifndef _PRESTERA_DEVLINK_H_
+#define _PRESTERA_DEVLINK_H_
+
+#include "prestera.h"
+
+struct prestera_switch *prestera_devlink_alloc(void);
+void prestera_devlink_free(struct prestera_switch *sw);
+
+int prestera_devlink_register(struct prestera_switch *sw);
+void prestera_devlink_unregister(struct prestera_switch *sw);
+
+int prestera_devlink_port_register(struct prestera_port *port);
+void prestera_devlink_port_unregister(struct prestera_port *port);
+
+void prestera_devlink_port_set(struct prestera_port *port);
+void prestera_devlink_port_clear(struct prestera_port *port);
+
+struct devlink_port *prestera_devlink_get_port(struct net_device *dev);
+
+#endif /* _PRESTERA_DEVLINK_H_ */
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_main.c b/drivers/net/ethernet/marvell/prestera/prestera_main.c
index dbe146163004..f24a49bde7c3 100644
--- a/drivers/net/ethernet/marvell/prestera/prestera_main.c
+++ b/drivers/net/ethernet/marvell/prestera/prestera_main.c
@@ -14,6 +14,7 @@
#include "prestera.h"
#include "prestera_hw.h"
#include "prestera_rxtx.h"
+#include "prestera_devlink.h"
#define PRESTERA_MTU_DEFAULT 1536
@@ -175,6 +176,7 @@ static const struct net_device_ops netdev_ops = {
.ndo_change_mtu = prestera_port_change_mtu,
.ndo_get_stats64 = prestera_port_get_stats64,
.ndo_set_mac_address = prestera_port_set_mac_address,
+ .ndo_get_devlink_port = prestera_devlink_get_port,
};
static int prestera_port_autoneg_set(struct prestera_port *port, bool enable,
@@ -224,9 +226,13 @@ static int prestera_port_create(struct prestera_switch *sw, u32 id)
&port->fp_id);
if (err) {
dev_err(prestera_dev(sw), "Failed to get port(%u) info\n", id);
- goto err_port_init;
+ goto err_port_info_get;
}
+ err = prestera_devlink_port_register(port);
+ if (err)
+ goto err_dl_port_register;
+
dev->features |= NETIF_F_NETNS_LOCAL;
dev->netdev_ops = &netdev_ops;
@@ -285,11 +291,16 @@ static int prestera_port_create(struct prestera_switch *sw, u32 id)
if (err)
goto err_register_netdev;
+ prestera_devlink_port_set(port);
+
return 0;
err_register_netdev:
list_del(&port->list);
err_port_init:
+ prestera_devlink_port_unregister(port);
+err_dl_port_register:
+err_port_info_get:
free_netdev(dev);
return err;
}
@@ -299,8 +310,10 @@ static void prestera_port_destroy(struct prestera_port *port)
struct net_device *dev = port->dev;
cancel_delayed_work_sync(&port->cached_hw_stats.caching_dw);
+ prestera_devlink_port_clear(port);
unregister_netdev(dev);
list_del(&port->list);
+ prestera_devlink_port_unregister(port);
free_netdev(dev);
}
@@ -421,6 +434,10 @@ static int prestera_switch_init(struct prestera_switch *sw)
if (err)
goto err_handlers_register;
+ err = prestera_devlink_register(sw);
+ if (err)
+ goto err_dl_register;
+
err = prestera_create_ports(sw);
if (err)
goto err_ports_create;
@@ -428,6 +445,8 @@ static int prestera_switch_init(struct prestera_switch *sw)
return 0;
err_ports_create:
+ prestera_devlink_unregister(sw);
+err_dl_register:
prestera_event_handlers_unregister(sw);
err_handlers_register:
prestera_rxtx_switch_fini(sw);
@@ -439,6 +458,7 @@ static int prestera_switch_init(struct prestera_switch *sw)
static void prestera_switch_fini(struct prestera_switch *sw)
{
prestera_destroy_ports(sw);
+ prestera_devlink_unregister(sw);
prestera_event_handlers_unregister(sw);
prestera_rxtx_switch_fini(sw);
prestera_hw_switch_fini(sw);
@@ -449,7 +469,7 @@ int prestera_device_register(struct prestera_device *dev)
struct prestera_switch *sw;
int err;
- sw = kzalloc(sizeof(*sw), GFP_KERNEL);
+ sw = prestera_devlink_alloc();
if (!sw)
return -ENOMEM;
@@ -458,7 +478,7 @@ int prestera_device_register(struct prestera_device *dev)
err = prestera_switch_init(sw);
if (err) {
- kfree(sw);
+ prestera_devlink_free(sw);
return err;
}
@@ -471,7 +491,7 @@ void prestera_device_unregister(struct prestera_device *dev)
struct prestera_switch *sw = dev->priv;
prestera_switch_fini(sw);
- kfree(sw);
+ prestera_devlink_free(sw);
}
EXPORT_SYMBOL(prestera_device_unregister);
--
2.17.1
Add PCI interface driver for Prestera Switch ASICs family devices, which
provides:
- Firmware loading mechanism
- Requests & events handling to/from the firmware
- Access to the firmware on the bus level
The firmware has to be loaded each time device is reset. The driver is
loading it from:
/lib/firmware/marvell/prestera_fw-v{MAJOR}.{MINOR}.img
The full firmware image version is located within internal header and
consists of 3 numbers - MAJOR.MINOR.PATCH. Additionally, driver has
hard-coded minimum supported firmware version which it can work with:
MAJOR - reflects the support on ABI level between driver and loaded
firmware, this number should be the same for driver and loaded
firmware.
MINOR - this is the minimum supported version between driver and the
firmware.
PATCH - indicates only fixes, firmware ABI is not changed.
Firmware image file name contains only MAJOR and MINOR numbers to make
driver be compatible with any PATCH version.
Signed-off-by: Oleksandr Mazur <[email protected]>
Signed-off-by: Vadym Kochan <[email protected]>
---
drivers/net/ethernet/marvell/prestera/Kconfig | 11 +
.../net/ethernet/marvell/prestera/Makefile | 2 +
.../ethernet/marvell/prestera/prestera_pci.c | 823 ++++++++++++++++++
3 files changed, 836 insertions(+)
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_pci.c
diff --git a/drivers/net/ethernet/marvell/prestera/Kconfig b/drivers/net/ethernet/marvell/prestera/Kconfig
index 76b68613ea7a..d30e3e6d8b7b 100644
--- a/drivers/net/ethernet/marvell/prestera/Kconfig
+++ b/drivers/net/ethernet/marvell/prestera/Kconfig
@@ -11,3 +11,14 @@ config PRESTERA
To compile this driver as a module, choose M here: the
module will be called prestera.
+
+config PRESTERA_PCI
+ tristate "PCI interface driver for Marvell Prestera Switch ASICs family"
+ depends on PCI && HAS_IOMEM && PRESTERA
+ default m
+ help
+ This is implementation of PCI interface support for Marvell Prestera
+ Switch ASICs family.
+
+ To compile this driver as a module, choose M here: the
+ module will be called prestera_pci.
diff --git a/drivers/net/ethernet/marvell/prestera/Makefile b/drivers/net/ethernet/marvell/prestera/Makefile
index 610d75032b78..2146714eab21 100644
--- a/drivers/net/ethernet/marvell/prestera/Makefile
+++ b/drivers/net/ethernet/marvell/prestera/Makefile
@@ -2,3 +2,5 @@
obj-$(CONFIG_PRESTERA) += prestera.o
prestera-objs := prestera_main.o prestera_hw.o prestera_dsa.o \
prestera_rxtx.o
+
+obj-$(CONFIG_PRESTERA_PCI) += prestera_pci.o
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_pci.c b/drivers/net/ethernet/marvell/prestera/prestera_pci.c
new file mode 100644
index 000000000000..cc8446c6b4d9
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/prestera_pci.c
@@ -0,0 +1,823 @@
+// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+/* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/device.h>
+#include <linux/pci.h>
+#include <linux/circ_buf.h>
+#include <linux/firmware.h>
+#include <linux/iopoll.h>
+
+#include "prestera.h"
+
+#define PRESTERA_MSG_MAX_SIZE 1500
+
+#define PRESTERA_SUPP_FW_MAJ_VER 2
+#define PRESTERA_SUPP_FW_MIN_VER 0
+
+#define PRESTERA_FW_PATH \
+ "mrvl/prestera/mvsw_prestera_fw-v" \
+ __stringify(PRESTERA_SUPP_FW_MAJ_VER) \
+ "." __stringify(PRESTERA_SUPP_FW_MIN_VER) ".img"
+
+#define PRESTERA_FW_HDR_MAGIC 0x351D9D06
+#define PRESTERA_FW_DL_TIMEOUT 50000
+#define PRESTERA_FW_BLK_SZ 1024
+
+#define PRESTERA_FW_VER_MAJ_MUL 1000000
+#define PRESTERA_FW_VER_MIN_MUL 1000
+
+#define PRESTERA_FW_VER_MAJ(v) ((v) / PRESTERA_FW_VER_MAJ_MUL)
+
+#define PRESTERA_FW_VER_MIN(v) \
+ (((v) - (PRESTERA_FW_VER_MAJ(v) * PRESTERA_FW_VER_MAJ_MUL)) / \
+ PRESTERA_FW_VER_MIN_MUL)
+
+#define PRESTERA_FW_VER_PATCH(v) \
+ ((v) - (PRESTERA_FW_VER_MAJ(v) * PRESTERA_FW_VER_MAJ_MUL) - \
+ (PRESTERA_FW_VER_MIN(v) * PRESTERA_FW_VER_MIN_MUL))
+
+struct prestera_fw_header {
+ __be32 magic_number;
+ __be32 version_value;
+ u8 reserved[8];
+} __packed;
+
+struct prestera_ldr_regs {
+ u32 ldr_ready;
+ u32 pad1;
+
+ u32 ldr_img_size;
+ u32 ldr_ctl_flags;
+
+ u32 ldr_buf_offs;
+ u32 ldr_buf_size;
+
+ u32 ldr_buf_rd;
+ u32 pad2;
+ u32 ldr_buf_wr;
+
+ u32 ldr_status;
+} __packed __aligned(4);
+
+#define PRESTERA_LDR_REG_OFFSET(f) offsetof(struct prestera_ldr_regs, f)
+
+#define PRESTERA_LDR_READY_MAGIC 0xf00dfeed
+
+#define PRESTERA_LDR_STATUS_IMG_DL BIT(0)
+#define PRESTERA_LDR_STATUS_START_FW BIT(1)
+#define PRESTERA_LDR_STATUS_INVALID_IMG BIT(2)
+#define PRESTERA_LDR_STATUS_NOMEM BIT(3)
+
+#define PRESTERA_LDR_REG_BASE(fw) ((fw)->ldr_regs)
+#define PRESTERA_LDR_REG_ADDR(fw, reg) (PRESTERA_LDR_REG_BASE(fw) + (reg))
+
+#define prestera_ldr_write(fw, reg, val) \
+ writel(val, PRESTERA_LDR_REG_ADDR(fw, reg))
+#define prestera_ldr_read(fw, reg) \
+ readl(PRESTERA_LDR_REG_ADDR(fw, reg))
+
+/* fw loader registers */
+#define PRESTERA_LDR_READY_REG PRESTERA_LDR_REG_OFFSET(ldr_ready)
+#define PRESTERA_LDR_IMG_SIZE_REG PRESTERA_LDR_REG_OFFSET(ldr_img_size)
+#define PRESTERA_LDR_CTL_REG PRESTERA_LDR_REG_OFFSET(ldr_ctl_flags)
+#define PRESTERA_LDR_BUF_SIZE_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_size)
+#define PRESTERA_LDR_BUF_OFFS_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_offs)
+#define PRESTERA_LDR_BUF_RD_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_rd)
+#define PRESTERA_LDR_BUF_WR_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_wr)
+#define PRESTERA_LDR_STATUS_REG PRESTERA_LDR_REG_OFFSET(ldr_status)
+
+#define PRESTERA_LDR_CTL_DL_START BIT(0)
+
+#define PRESTERA_EVT_QNUM_MAX 4
+
+struct prestera_fw_evtq_regs {
+ u32 rd_idx;
+ u32 pad1;
+ u32 wr_idx;
+ u32 pad2;
+ u32 offs;
+ u32 len;
+};
+
+struct prestera_fw_regs {
+ u32 fw_ready;
+ u32 pad;
+ u32 cmd_offs;
+ u32 cmd_len;
+ u32 evt_offs;
+ u32 evt_qnum;
+
+ u32 cmd_req_ctl;
+ u32 cmd_req_len;
+ u32 cmd_rcv_ctl;
+ u32 cmd_rcv_len;
+
+ u32 fw_status;
+ u32 rx_status;
+
+ struct prestera_fw_evtq_regs evtq_list[PRESTERA_EVT_QNUM_MAX];
+};
+
+#define PRESTERA_FW_REG_OFFSET(f) offsetof(struct prestera_fw_regs, f)
+
+#define PRESTERA_FW_READY_MAGIC 0xcafebabe
+
+/* fw registers */
+#define PRESTERA_FW_READY_REG PRESTERA_FW_REG_OFFSET(fw_ready)
+
+#define PRESTERA_CMD_BUF_OFFS_REG PRESTERA_FW_REG_OFFSET(cmd_offs)
+#define PRESTERA_CMD_BUF_LEN_REG PRESTERA_FW_REG_OFFSET(cmd_len)
+#define PRESTERA_EVT_BUF_OFFS_REG PRESTERA_FW_REG_OFFSET(evt_offs)
+#define PRESTERA_EVT_QNUM_REG PRESTERA_FW_REG_OFFSET(evt_qnum)
+
+#define PRESTERA_CMD_REQ_CTL_REG PRESTERA_FW_REG_OFFSET(cmd_req_ctl)
+#define PRESTERA_CMD_REQ_LEN_REG PRESTERA_FW_REG_OFFSET(cmd_req_len)
+
+#define PRESTERA_CMD_RCV_CTL_REG PRESTERA_FW_REG_OFFSET(cmd_rcv_ctl)
+#define PRESTERA_CMD_RCV_LEN_REG PRESTERA_FW_REG_OFFSET(cmd_rcv_len)
+#define PRESTERA_FW_STATUS_REG PRESTERA_FW_REG_OFFSET(fw_status)
+#define PRESTERA_RX_STATUS_REG PRESTERA_FW_REG_OFFSET(rx_status)
+
+/* PRESTERA_CMD_REQ_CTL_REG flags */
+#define PRESTERA_CMD_F_REQ_SENT BIT(0)
+#define PRESTERA_CMD_F_REPL_RCVD BIT(1)
+
+/* PRESTERA_CMD_RCV_CTL_REG flags */
+#define PRESTERA_CMD_F_REPL_SENT BIT(0)
+
+#define PRESTERA_EVTQ_REG_OFFSET(q, f) \
+ (PRESTERA_FW_REG_OFFSET(evtq_list) + \
+ (q) * sizeof(struct prestera_fw_evtq_regs) + \
+ offsetof(struct prestera_fw_evtq_regs, f))
+
+#define PRESTERA_EVTQ_RD_IDX_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, rd_idx)
+#define PRESTERA_EVTQ_WR_IDX_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, wr_idx)
+#define PRESTERA_EVTQ_OFFS_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, offs)
+#define PRESTERA_EVTQ_LEN_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, len)
+
+#define PRESTERA_FW_REG_BASE(fw) ((fw)->dev.ctl_regs)
+#define PRESTERA_FW_REG_ADDR(fw, reg) PRESTERA_FW_REG_BASE((fw)) + (reg)
+
+#define prestera_fw_write(fw, reg, val) \
+ writel(val, PRESTERA_FW_REG_ADDR(fw, reg))
+#define prestera_fw_read(fw, reg) \
+ readl(PRESTERA_FW_REG_ADDR(fw, reg))
+
+#define PRESTERA_FW_CMD_DEFAULT_WAITMS 30000
+#define PRESTERA_FW_READY_WAITMS 20000
+
+struct prestera_fw_evtq {
+ u8 __iomem *addr;
+ size_t len;
+};
+
+struct prestera_fw {
+ struct workqueue_struct *wq;
+ struct prestera_device dev;
+ struct pci_dev *pci_dev;
+ u8 __iomem *ldr_regs;
+ u8 __iomem *ldr_ring_buf;
+ u32 ldr_buf_len;
+ u32 ldr_wr_idx;
+ struct mutex cmd_mtx; /* serialize access to dev->send_req */
+ size_t cmd_mbox_len;
+ u8 __iomem *cmd_mbox;
+ struct prestera_fw_evtq evt_queue[PRESTERA_EVT_QNUM_MAX];
+ u8 evt_qnum;
+ struct work_struct evt_work;
+ u8 __iomem *evt_buf;
+ u8 *evt_msg;
+};
+
+static int prestera_fw_load(struct prestera_fw *fw);
+
+static u32 prestera_fw_evtq_len(struct prestera_fw *fw, u8 qid)
+{
+ return fw->evt_queue[qid].len;
+}
+
+static u32 prestera_fw_evtq_avail(struct prestera_fw *fw, u8 qid)
+{
+ u32 wr_idx = prestera_fw_read(fw, PRESTERA_EVTQ_WR_IDX_REG(qid));
+ u32 rd_idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
+
+ return CIRC_CNT(wr_idx, rd_idx, prestera_fw_evtq_len(fw, qid));
+}
+
+static void prestera_fw_evtq_rd_set(struct prestera_fw *fw,
+ u8 qid, u32 idx)
+{
+ u32 rd_idx = idx & (prestera_fw_evtq_len(fw, qid) - 1);
+
+ prestera_fw_write(fw, PRESTERA_EVTQ_RD_IDX_REG(qid), rd_idx);
+}
+
+static u8 __iomem *prestera_fw_evtq_buf(struct prestera_fw *fw, u8 qid)
+{
+ return fw->evt_queue[qid].addr;
+}
+
+static u32 prestera_fw_evtq_read32(struct prestera_fw *fw, u8 qid)
+{
+ u32 rd_idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
+ u32 val;
+
+ val = readl(prestera_fw_evtq_buf(fw, qid) + rd_idx);
+ prestera_fw_evtq_rd_set(fw, qid, rd_idx + 4);
+ return val;
+}
+
+static ssize_t prestera_fw_evtq_read_buf(struct prestera_fw *fw,
+ u8 qid, u8 *buf, size_t len)
+{
+ u32 idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
+ u8 __iomem *evtq_addr = prestera_fw_evtq_buf(fw, qid);
+ u32 *buf32 = (u32 *)buf;
+ int i;
+
+ for (i = 0; i < len / 4; buf32++, i++) {
+ *buf32 = readl_relaxed(evtq_addr + idx);
+ idx = (idx + 4) & (prestera_fw_evtq_len(fw, qid) - 1);
+ }
+
+ prestera_fw_evtq_rd_set(fw, qid, idx);
+
+ return i;
+}
+
+static u8 prestera_fw_evtq_pick(struct prestera_fw *fw)
+{
+ int qid;
+
+ for (qid = 0; qid < fw->evt_qnum; qid++) {
+ if (prestera_fw_evtq_avail(fw, qid) >= 4)
+ return qid;
+ }
+
+ return PRESTERA_EVT_QNUM_MAX;
+}
+
+static void prestera_fw_evt_work_fn(struct work_struct *work)
+{
+ struct prestera_fw *fw;
+ u8 *msg;
+ u8 qid;
+
+ fw = container_of(work, struct prestera_fw, evt_work);
+ msg = fw->evt_msg;
+
+ while ((qid = prestera_fw_evtq_pick(fw)) < PRESTERA_EVT_QNUM_MAX) {
+ u32 idx;
+ u32 len;
+
+ len = prestera_fw_evtq_read32(fw, qid);
+ idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
+
+ WARN_ON(prestera_fw_evtq_avail(fw, qid) < len);
+
+ if (WARN_ON(len > PRESTERA_MSG_MAX_SIZE)) {
+ prestera_fw_evtq_rd_set(fw, qid, idx + len);
+ continue;
+ }
+
+ prestera_fw_evtq_read_buf(fw, qid, msg, len);
+
+ if (fw->dev.recv_msg)
+ fw->dev.recv_msg(&fw->dev, msg, len);
+ }
+}
+
+static int prestera_fw_wait_reg32(struct prestera_fw *fw, u32 reg, u32 cmp,
+ unsigned int waitms)
+{
+ u8 __iomem *addr = PRESTERA_FW_REG_ADDR(fw, reg);
+ u32 val = 0;
+
+ return readl_poll_timeout(addr, val, cmp == val, 1000 * 10, waitms * 1000);
+}
+
+static void prestera_pci_copy_to(u8 __iomem *dst, u8 *src, size_t len)
+{
+ u32 __iomem *dst32 = (u32 __iomem *)dst;
+ u32 *src32 = (u32 *)src;
+ int i;
+
+ for (i = 0; i < (len / 4); dst32++, src32++, i++)
+ writel_relaxed(*src32, dst32);
+}
+
+static void prestera_pci_copy_from(u8 *dst, u8 __iomem *src, size_t len)
+{
+ u32 __iomem *src32 = (u32 __iomem *)src;
+ u32 *dst32 = (u32 *)dst;
+ int i;
+
+ for (i = 0; i < (len / 4); dst32++, src32++, i++)
+ *dst32 = readl_relaxed(src32);
+}
+
+static int prestera_fw_cmd_send(struct prestera_fw *fw,
+ u8 *in_msg, size_t in_size,
+ u8 *out_msg, size_t out_size,
+ unsigned int waitms)
+{
+ u32 ret_size = 0;
+ int err = 0;
+
+ if (!waitms)
+ waitms = PRESTERA_FW_CMD_DEFAULT_WAITMS;
+
+ if (ALIGN(in_size, 4) > fw->cmd_mbox_len)
+ return -EMSGSIZE;
+
+ /* wait for finish previous reply from FW */
+ err = prestera_fw_wait_reg32(fw, PRESTERA_CMD_RCV_CTL_REG, 0, 30);
+ if (err) {
+ dev_err(fw->dev.dev, "finish reply from FW is timed out\n");
+ return err;
+ }
+
+ prestera_fw_write(fw, PRESTERA_CMD_REQ_LEN_REG, in_size);
+ prestera_pci_copy_to(fw->cmd_mbox, in_msg, in_size);
+
+ prestera_fw_write(fw, PRESTERA_CMD_REQ_CTL_REG, PRESTERA_CMD_F_REQ_SENT);
+
+ /* wait for reply from FW */
+ err = prestera_fw_wait_reg32(fw, PRESTERA_CMD_RCV_CTL_REG,
+ PRESTERA_CMD_F_REPL_SENT, waitms);
+ if (err) {
+ dev_err(fw->dev.dev, "reply from FW is timed out\n");
+ goto cmd_exit;
+ }
+
+ ret_size = prestera_fw_read(fw, PRESTERA_CMD_RCV_LEN_REG);
+ if (ret_size > out_size) {
+ dev_err(fw->dev.dev, "ret_size (%u) > out_len(%zu)\n",
+ ret_size, out_size);
+ err = -EMSGSIZE;
+ goto cmd_exit;
+ }
+
+ prestera_pci_copy_from(out_msg, fw->cmd_mbox + in_size, ret_size);
+
+cmd_exit:
+ prestera_fw_write(fw, PRESTERA_CMD_REQ_CTL_REG, PRESTERA_CMD_F_REPL_RCVD);
+ return err;
+}
+
+static int prestera_fw_send_req(struct prestera_device *dev,
+ u8 *in_msg, size_t in_size, u8 *out_msg,
+ size_t out_size, unsigned int waitms)
+{
+ struct prestera_fw *fw;
+ ssize_t ret;
+
+ fw = container_of(dev, struct prestera_fw, dev);
+
+ mutex_lock(&fw->cmd_mtx);
+ ret = prestera_fw_cmd_send(fw, in_msg, in_size, out_msg, out_size, waitms);
+ mutex_unlock(&fw->cmd_mtx);
+
+ return ret;
+}
+
+static int prestera_fw_init(struct prestera_fw *fw)
+{
+ u8 __iomem *base;
+ int err;
+ u8 qid;
+
+ fw->dev.send_req = prestera_fw_send_req;
+ fw->ldr_regs = fw->dev.ctl_regs;
+
+ err = prestera_fw_load(fw);
+ if (err && err != -ETIMEDOUT)
+ return err;
+
+ err = prestera_fw_wait_reg32(fw, PRESTERA_FW_READY_REG,
+ PRESTERA_FW_READY_MAGIC,
+ PRESTERA_FW_READY_WAITMS);
+ if (err) {
+ dev_err(fw->dev.dev, "FW failed to start\n");
+ return err;
+ }
+
+ base = fw->dev.ctl_regs;
+
+ fw->cmd_mbox = base + prestera_fw_read(fw, PRESTERA_CMD_BUF_OFFS_REG);
+ fw->cmd_mbox_len = prestera_fw_read(fw, PRESTERA_CMD_BUF_LEN_REG);
+ mutex_init(&fw->cmd_mtx);
+
+ fw->evt_buf = base + prestera_fw_read(fw, PRESTERA_EVT_BUF_OFFS_REG);
+ fw->evt_qnum = prestera_fw_read(fw, PRESTERA_EVT_QNUM_REG);
+ fw->evt_msg = kmalloc(PRESTERA_MSG_MAX_SIZE, GFP_KERNEL);
+ if (!fw->evt_msg)
+ return -ENOMEM;
+
+ for (qid = 0; qid < fw->evt_qnum; qid++) {
+ u32 offs = prestera_fw_read(fw, PRESTERA_EVTQ_OFFS_REG(qid));
+ struct prestera_fw_evtq *evtq = &fw->evt_queue[qid];
+
+ evtq->len = prestera_fw_read(fw, PRESTERA_EVTQ_LEN_REG(qid));
+ evtq->addr = fw->evt_buf + offs;
+ }
+
+ return 0;
+}
+
+static void prestera_fw_uninit(struct prestera_fw *fw)
+{
+ kfree(fw->evt_msg);
+}
+
+static irqreturn_t prestera_pci_irq_handler(int irq, void *dev_id)
+{
+ struct prestera_fw *fw = dev_id;
+
+ if (prestera_fw_read(fw, PRESTERA_RX_STATUS_REG)) {
+ prestera_fw_write(fw, PRESTERA_RX_STATUS_REG, 0);
+
+ if (fw->dev.recv_pkt)
+ fw->dev.recv_pkt(&fw->dev);
+ }
+
+ queue_work(fw->wq, &fw->evt_work);
+
+ return IRQ_HANDLED;
+}
+
+static int prestera_ldr_wait_reg32(struct prestera_fw *fw,
+ u32 reg, u32 cmp, unsigned int waitms)
+{
+ u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, reg);
+ u32 val = 0;
+
+ return readl_poll_timeout(addr, val, cmp == val, 1000 * 10, waitms * 1000);
+}
+
+static u32 prestera_ldr_wait_buf(struct prestera_fw *fw, size_t len)
+{
+ u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, PRESTERA_LDR_BUF_RD_REG);
+ u32 buf_len = fw->ldr_buf_len;
+ u32 wr_idx = fw->ldr_wr_idx;
+ u32 rd_idx = 0;
+
+ return readl_poll_timeout(addr, rd_idx,
+ CIRC_SPACE(wr_idx, rd_idx, buf_len) >= len,
+ 1000, 100 * 1000);
+ return 0;
+}
+
+static int prestera_ldr_wait_dl_finish(struct prestera_fw *fw)
+{
+ u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, PRESTERA_LDR_STATUS_REG);
+ unsigned int waitus = PRESTERA_FW_DL_TIMEOUT * 1000;
+ unsigned long mask = ~(PRESTERA_LDR_STATUS_IMG_DL);
+ u32 val = 0;
+ int err;
+
+ err = readl_poll_timeout(addr, val, val & mask, 1000 * 10, waitus);
+ if (err) {
+ dev_err(fw->dev.dev, "Timeout to load FW img [state=%d]",
+ prestera_ldr_read(fw, PRESTERA_LDR_STATUS_REG));
+ return err;
+ }
+
+ return 0;
+}
+
+static void prestera_ldr_wr_idx_move(struct prestera_fw *fw, unsigned int n)
+{
+ fw->ldr_wr_idx = (fw->ldr_wr_idx + (n)) & (fw->ldr_buf_len - 1);
+}
+
+static void prestera_ldr_wr_idx_commit(struct prestera_fw *fw)
+{
+ prestera_ldr_write(fw, PRESTERA_LDR_BUF_WR_REG, fw->ldr_wr_idx);
+}
+
+static u8 __iomem *prestera_ldr_wr_ptr(struct prestera_fw *fw)
+{
+ return fw->ldr_ring_buf + fw->ldr_wr_idx;
+}
+
+static int prestera_ldr_send(struct prestera_fw *fw, const u8 *buf, size_t len)
+{
+ int err;
+ int i;
+
+ err = prestera_ldr_wait_buf(fw, len);
+ if (err) {
+ dev_err(fw->dev.dev, "failed wait for sending firmware\n");
+ return err;
+ }
+
+ for (i = 0; i < len; i += 4) {
+ writel_relaxed(*(u32 *)(buf + i), prestera_ldr_wr_ptr(fw));
+ prestera_ldr_wr_idx_move(fw, 4);
+ }
+
+ prestera_ldr_wr_idx_commit(fw);
+ return 0;
+}
+
+static int prestera_ldr_fw_send(struct prestera_fw *fw,
+ const char *img, u32 fw_size)
+{
+ u32 status;
+ u32 pos;
+ int err;
+
+ err = prestera_ldr_wait_reg32(fw, PRESTERA_LDR_STATUS_REG,
+ PRESTERA_LDR_STATUS_IMG_DL, 5 * 1000);
+ if (err) {
+ dev_err(fw->dev.dev, "Loader is not ready to load image\n");
+ return err;
+ }
+
+ for (pos = 0; pos < fw_size; pos += PRESTERA_FW_BLK_SZ) {
+ if (pos + PRESTERA_FW_BLK_SZ > fw_size)
+ break;
+
+ err = prestera_ldr_send(fw, img + pos, PRESTERA_FW_BLK_SZ);
+ if (err)
+ return err;
+ }
+
+ if (pos < fw_size) {
+ err = prestera_ldr_send(fw, img + pos, fw_size - pos);
+ if (err)
+ return err;
+ }
+
+ err = prestera_ldr_wait_dl_finish(fw);
+ if (err)
+ return err;
+
+ status = prestera_ldr_read(fw, PRESTERA_LDR_STATUS_REG);
+ if (status != PRESTERA_LDR_STATUS_START_FW) {
+ switch (status) {
+ case PRESTERA_LDR_STATUS_INVALID_IMG:
+ dev_err(fw->dev.dev, "FW img has bad CRC\n");
+ return -EINVAL;
+ case PRESTERA_LDR_STATUS_NOMEM:
+ dev_err(fw->dev.dev, "Loader has no enough mem\n");
+ return -ENOMEM;
+ default:
+ break;
+ }
+ }
+
+ return 0;
+}
+
+static void prestera_fw_rev_parse(const struct prestera_fw_header *hdr,
+ struct prestera_fw_rev *rev)
+{
+ u32 version = be32_to_cpu(hdr->version_value);
+
+ rev->maj = PRESTERA_FW_VER_MAJ(version);
+ rev->min = PRESTERA_FW_VER_MIN(version);
+ rev->sub = PRESTERA_FW_VER_PATCH(version);
+}
+
+static int prestera_fw_rev_check(struct prestera_fw *fw)
+{
+ struct prestera_fw_rev *rev = &fw->dev.fw_rev;
+ u16 maj_supp = PRESTERA_SUPP_FW_MAJ_VER;
+ u16 min_supp = PRESTERA_SUPP_FW_MIN_VER;
+
+ if (rev->maj == maj_supp && rev->min >= min_supp)
+ return 0;
+
+ dev_err(fw->dev.dev, "Driver supports FW version only '%u.%u.x'",
+ PRESTERA_SUPP_FW_MAJ_VER, PRESTERA_SUPP_FW_MIN_VER);
+
+ return -EINVAL;
+}
+
+static int prestera_fw_hdr_parse(struct prestera_fw *fw,
+ const struct firmware *img)
+{
+ struct prestera_fw_header *hdr = (struct prestera_fw_header *)img->data;
+ struct prestera_fw_rev *rev = &fw->dev.fw_rev;
+ u32 magic;
+
+ magic = be32_to_cpu(hdr->magic_number);
+ if (magic != PRESTERA_FW_HDR_MAGIC) {
+ dev_err(fw->dev.dev, "FW img hdr magic is invalid");
+ return -EINVAL;
+ }
+
+ prestera_fw_rev_parse(hdr, rev);
+
+ dev_info(fw->dev.dev, "FW version '%u.%u.%u'\n",
+ rev->maj, rev->min, rev->sub);
+
+ return prestera_fw_rev_check(fw);
+}
+
+static int prestera_fw_load(struct prestera_fw *fw)
+{
+ size_t hlen = sizeof(struct prestera_fw_header);
+ const struct firmware *f;
+ int err;
+
+ err = prestera_ldr_wait_reg32(fw, PRESTERA_LDR_READY_REG,
+ PRESTERA_LDR_READY_MAGIC, 5 * 1000);
+ if (err) {
+ dev_err(fw->dev.dev, "waiting for FW loader is timed out");
+ return err;
+ }
+
+ fw->ldr_ring_buf = fw->ldr_regs +
+ prestera_ldr_read(fw, PRESTERA_LDR_BUF_OFFS_REG);
+
+ fw->ldr_buf_len =
+ prestera_ldr_read(fw, PRESTERA_LDR_BUF_SIZE_REG);
+
+ fw->ldr_wr_idx = 0;
+
+ err = request_firmware_direct(&f, PRESTERA_FW_PATH, &fw->pci_dev->dev);
+ if (err) {
+ dev_err(fw->dev.dev, "failed to request firmware file\n");
+ return err;
+ }
+
+ if (!IS_ALIGNED(f->size, 4)) {
+ dev_err(fw->dev.dev, "FW image file is not aligned");
+ release_firmware(f);
+ return -EINVAL;
+ }
+
+ err = prestera_fw_hdr_parse(fw, f);
+ if (err) {
+ dev_err(fw->dev.dev, "FW image header is invalid\n");
+ release_firmware(f);
+ return err;
+ }
+
+ prestera_ldr_write(fw, PRESTERA_LDR_IMG_SIZE_REG, f->size - hlen);
+ prestera_ldr_write(fw, PRESTERA_LDR_CTL_REG, PRESTERA_LDR_CTL_DL_START);
+
+ dev_info(fw->dev.dev, "Loading prestera FW image ...");
+
+ err = prestera_ldr_fw_send(fw, f->data + hlen, f->size - hlen);
+
+ release_firmware(f);
+ return err;
+}
+
+static int prestera_pci_probe(struct pci_dev *pdev,
+ const struct pci_device_id *id)
+{
+ const char *driver_name = pdev->driver->name;
+ u8 __iomem *ctl_addr, *pp_addr;
+ struct prestera_fw *fw;
+ int err;
+
+ err = pci_enable_device(pdev);
+ if (err)
+ return err;
+
+ err = pci_request_regions(pdev, driver_name);
+ if (err) {
+ dev_err(&pdev->dev, "pci_request_regions failed\n");
+ goto err_pci_request_regions;
+ }
+
+ if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(30))) {
+ dev_err(&pdev->dev, "fail to set DMA mask\n");
+ goto err_dma_mask;
+ }
+
+ ctl_addr = pci_ioremap_bar(pdev, 2);
+ if (!ctl_addr) {
+ dev_err(&pdev->dev, "ioremap failed\n");
+ err = -EIO;
+ goto err_ctl_ioremap;
+ }
+
+ pp_addr = pci_ioremap_bar(pdev, 4);
+ if (!pp_addr) {
+ dev_err(&pdev->dev, "ioremap failed\n");
+ err = -EIO;
+ goto err_pp_ioremap;
+ }
+
+ pci_set_master(pdev);
+
+ fw = kzalloc(sizeof(*fw), GFP_KERNEL);
+ if (!fw) {
+ err = -ENOMEM;
+ goto err_pci_dev_alloc;
+ }
+
+ fw->pci_dev = pdev;
+ fw->dev.dev = &pdev->dev;
+ fw->dev.ctl_regs = ctl_addr;
+ fw->dev.pp_regs = pp_addr;
+
+ pci_set_drvdata(pdev, fw);
+
+ err = prestera_fw_init(fw);
+ if (err)
+ goto err_prestera_fw_init;
+
+ dev_info(fw->dev.dev, "Switch FW is ready\n");
+
+ fw->wq = alloc_workqueue("prestera_fw_wq", WQ_HIGHPRI, 1);
+ if (!fw->wq)
+ goto err_wq_alloc;
+
+ INIT_WORK(&fw->evt_work, prestera_fw_evt_work_fn);
+
+ err = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
+ if (err < 0) {
+ dev_err(&pdev->dev, "MSI IRQ init failed\n");
+ goto err_irq_alloc;
+ }
+
+ err = request_irq(pci_irq_vector(pdev, 0), prestera_pci_irq_handler,
+ 0, driver_name, fw);
+ if (err) {
+ dev_err(&pdev->dev, "fail to request IRQ\n");
+ goto err_request_irq;
+ }
+
+ err = prestera_device_register(&fw->dev);
+ if (err)
+ goto err_prestera_dev_register;
+
+ return 0;
+
+err_prestera_dev_register:
+ free_irq(pci_irq_vector(pdev, 0), fw);
+err_request_irq:
+ pci_free_irq_vectors(pdev);
+err_irq_alloc:
+ destroy_workqueue(fw->wq);
+err_wq_alloc:
+ prestera_fw_uninit(fw);
+err_prestera_fw_init:
+ kfree(fw);
+err_pci_dev_alloc:
+ iounmap(pp_addr);
+err_pp_ioremap:
+ iounmap(ctl_addr);
+err_ctl_ioremap:
+err_dma_mask:
+ pci_release_regions(pdev);
+err_pci_request_regions:
+ pci_disable_device(pdev);
+ return err;
+}
+
+static void prestera_pci_remove(struct pci_dev *pdev)
+{
+ struct prestera_fw *fw = pci_get_drvdata(pdev);
+ u8 __iomem *ctl_addr = fw->dev.ctl_regs;
+ u8 __iomem *pp_addr = fw->dev.pp_regs;
+
+ prestera_device_unregister(&fw->dev);
+ free_irq(pci_irq_vector(pdev, 0), fw);
+ pci_free_irq_vectors(pdev);
+ destroy_workqueue(fw->wq);
+ prestera_fw_uninit(fw);
+ kfree(fw);
+ iounmap(pp_addr);
+ iounmap(ctl_addr);
+ pci_release_regions(pdev);
+ pci_disable_device(pdev);
+}
+
+static const struct pci_device_id prestera_pci_devices[] = {
+ { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0xC804) },
+ { }
+};
+MODULE_DEVICE_TABLE(pci, prestera_pci_devices);
+
+static struct pci_driver prestera_pci_driver = {
+ .name = "Prestera DX",
+ .id_table = prestera_pci_devices,
+ .probe = prestera_pci_probe,
+ .remove = prestera_pci_remove,
+};
+
+static int __init prestera_pci_init(void)
+{
+ return pci_register_driver(&prestera_pci_driver);
+}
+
+static void __exit prestera_pci_exit(void)
+{
+ pci_unregister_driver(&prestera_pci_driver);
+}
+
+module_init(prestera_pci_init);
+module_exit(prestera_pci_exit);
+
+MODULE_LICENSE("Dual BSD/GPL");
+MODULE_DESCRIPTION("Marvell Prestera switch PCI interface");
--
2.17.1
The following features are supported:
- VLAN-aware bridge offloading
- VLAN-unaware bridge offloading
- FDB offloading (learning, ageing)
- Switchport configuration
Currently there are some limitations like:
- Only 1 VLAN-aware bridge instance supported
- FDB ageing timeout parameter is set globally per device
Signed-off-by: Serhiy Boiko <[email protected]>
Signed-off-by: Serhiy Pshyk <[email protected]>
Signed-off-by: Taras Chornyi <[email protected]>
Signed-off-by: Vadym Kochan <[email protected]>
---
.../net/ethernet/marvell/prestera/Makefile | 3 +-
.../net/ethernet/marvell/prestera/prestera.h | 33 +-
.../ethernet/marvell/prestera/prestera_hw.c | 322 +++++
.../ethernet/marvell/prestera/prestera_hw.h | 48 +
.../ethernet/marvell/prestera/prestera_main.c | 111 +-
.../marvell/prestera/prestera_switchdev.c | 1286 +++++++++++++++++
.../marvell/prestera/prestera_switchdev.h | 16 +
7 files changed, 1813 insertions(+), 6 deletions(-)
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_switchdev.c
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_switchdev.h
diff --git a/drivers/net/ethernet/marvell/prestera/Makefile b/drivers/net/ethernet/marvell/prestera/Makefile
index 7684e7047562..93129e32ebc5 100644
--- a/drivers/net/ethernet/marvell/prestera/Makefile
+++ b/drivers/net/ethernet/marvell/prestera/Makefile
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_PRESTERA) += prestera.o
prestera-objs := prestera_main.o prestera_hw.o prestera_dsa.o \
- prestera_rxtx.o prestera_devlink.o prestera_ethtool.o
+ prestera_rxtx.o prestera_devlink.o prestera_ethtool.o \
+ prestera_switchdev.o
obj-$(CONFIG_PRESTERA_PCI) += prestera_pci.o
diff --git a/drivers/net/ethernet/marvell/prestera/prestera.h b/drivers/net/ethernet/marvell/prestera/prestera.h
index ee834e824521..dabe18c84fa6 100644
--- a/drivers/net/ethernet/marvell/prestera/prestera.h
+++ b/drivers/net/ethernet/marvell/prestera/prestera.h
@@ -15,6 +15,8 @@
#define PRESTERA_DRV_NAME "prestera"
+#define PRESTERA_DEFAULT_VID 1
+
struct prestera_fw_rev {
u16 maj;
u16 min;
@@ -51,8 +53,6 @@ struct prestera_port_stats {
u64 unicast_frames_sent;
u64 sent_multiple;
u64 sent_deferred;
- u64 frames_1024_to_1518_octets;
- u64 frames_1519_to_max_octets;
u64 good_octets_sent;
};
@@ -71,11 +71,13 @@ struct prestera_port {
u32 hw_id;
u32 dev_id;
u16 fp_id;
+ u16 pvid;
bool autoneg;
u64 adver_link_modes;
u8 adver_fec;
struct prestera_port_caps caps;
struct list_head list;
+ struct list_head vlans_list;
struct {
struct prestera_port_stats stats;
struct delayed_work caching_dw;
@@ -105,6 +107,7 @@ enum prestera_event_type {
PRESTERA_EVENT_TYPE_UNSPEC,
PRESTERA_EVENT_TYPE_PORT,
+ PRESTERA_EVENT_TYPE_FDB,
PRESTERA_EVENT_TYPE_RXTX,
PRESTERA_EVENT_TYPE_MAX,
@@ -127,19 +130,37 @@ struct prestera_port_event {
} data;
};
+enum prestera_fdb_event_id {
+ PRESTERA_FDB_EVENT_UNSPEC,
+ PRESTERA_FDB_EVENT_LEARNED,
+ PRESTERA_FDB_EVENT_AGED,
+};
+
+struct prestera_fdb_event {
+ u32 port_id;
+ u32 vid;
+ union {
+ u8 mac[ETH_ALEN];
+ } data;
+};
+
struct prestera_event {
u16 id;
union {
struct prestera_port_event port_evt;
+ struct prestera_fdb_event fdb_evt;
};
};
+struct prestera_switchdev;
struct prestera_rxtx;
struct prestera_switch {
struct prestera_device *dev;
+ struct prestera_switchdev *swdev;
struct prestera_rxtx *rxtx;
struct list_head event_handlers;
+ struct notifier_block netdev_nb;
char base_mac[ETH_ALEN];
struct list_head port_list;
u32 port_count;
@@ -176,4 +197,12 @@ struct prestera_port *prestera_port_find_by_hwid(struct prestera_switch *sw,
int prestera_port_autoneg_set(struct prestera_port *port, bool enable,
u64 adver_link_modes, u8 adver_fec);
+struct prestera_port *prestera_find_port(struct prestera_switch *sw, u32 id);
+
+struct prestera_port *prestera_port_dev_lower_find(struct net_device *dev);
+
+int prestera_port_pvid_set(struct prestera_port *port, u16 vid);
+
+bool prestera_netdev_check(const struct net_device *dev);
+
#endif /* _PRESTERA_H_ */
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_hw.c b/drivers/net/ethernet/marvell/prestera/prestera_hw.c
index c54435b0f786..cc7ddbace454 100644
--- a/drivers/net/ethernet/marvell/prestera/prestera_hw.c
+++ b/drivers/net/ethernet/marvell/prestera/prestera_hw.c
@@ -21,9 +21,27 @@ enum prestera_cmd_type_t {
PRESTERA_CMD_TYPE_PORT_ATTR_GET = 0x101,
PRESTERA_CMD_TYPE_PORT_INFO_GET = 0x110,
+ PRESTERA_CMD_TYPE_VLAN_CREATE = 0x200,
+ PRESTERA_CMD_TYPE_VLAN_DELETE = 0x201,
+ PRESTERA_CMD_TYPE_VLAN_PORT_SET = 0x202,
+ PRESTERA_CMD_TYPE_VLAN_PVID_SET = 0x203,
+
+ PRESTERA_CMD_TYPE_FDB_ADD = 0x300,
+ PRESTERA_CMD_TYPE_FDB_DELETE = 0x301,
+ PRESTERA_CMD_TYPE_FDB_FLUSH_PORT = 0x310,
+ PRESTERA_CMD_TYPE_FDB_FLUSH_VLAN = 0x311,
+ PRESTERA_CMD_TYPE_FDB_FLUSH_PORT_VLAN = 0x312,
+
+ PRESTERA_CMD_TYPE_BRIDGE_CREATE = 0x400,
+ PRESTERA_CMD_TYPE_BRIDGE_DELETE = 0x401,
+ PRESTERA_CMD_TYPE_BRIDGE_PORT_ADD = 0x402,
+ PRESTERA_CMD_TYPE_BRIDGE_PORT_DELETE = 0x403,
+
PRESTERA_CMD_TYPE_RXTX_INIT = 0x800,
PRESTERA_CMD_TYPE_RXTX_PORT_INIT = 0x801,
+ PRESTERA_CMD_TYPE_STP_PORT_SET = 0x1000,
+
PRESTERA_CMD_TYPE_ACK = 0x10000,
PRESTERA_CMD_TYPE_MAX
};
@@ -33,6 +51,9 @@ enum {
PRESTERA_CMD_PORT_ATTR_MTU = 3,
PRESTERA_CMD_PORT_ATTR_MAC = 4,
PRESTERA_CMD_PORT_ATTR_SPEED = 5,
+ PRESTERA_CMD_PORT_ATTR_ACCEPT_FRAME_TYPE = 6,
+ PRESTERA_CMD_PORT_ATTR_LEARNING = 7,
+ PRESTERA_CMD_PORT_ATTR_FLOOD = 8,
PRESTERA_CMD_PORT_ATTR_CAPABILITY = 9,
PRESTERA_CMD_PORT_ATTR_REMOTE_CAPABILITY = 10,
PRESTERA_CMD_PORT_ATTR_REMOTE_FC = 11,
@@ -48,6 +69,7 @@ enum {
enum {
PRESTERA_CMD_SWITCH_ATTR_MAC = 1,
+ PRESTERA_CMD_SWITCH_ATTR_AGEING = 2,
};
enum {
@@ -133,6 +155,7 @@ struct prestera_msg_common_resp {
union prestera_msg_switch_param {
u8 mac[ETH_ALEN];
+ u32 ageing_timeout;
};
struct prestera_msg_switch_attr_req {
@@ -171,7 +194,10 @@ union prestera_msg_port_param {
u8 oper_state;
u32 mtu;
u8 mac[ETH_ALEN];
+ u8 accept_frm_type;
u32 speed;
+ u8 learning;
+ u8 flood;
u32 link_mode;
u8 type;
u8 duplex;
@@ -212,6 +238,46 @@ struct prestera_msg_port_info_resp {
u16 fp_id;
};
+struct prestera_msg_vlan_req {
+ struct prestera_msg_cmd cmd;
+ u32 port;
+ u32 dev;
+ u16 vid;
+ u8 is_member;
+ u8 is_tagged;
+};
+
+struct prestera_msg_fdb_req {
+ struct prestera_msg_cmd cmd;
+ u8 dest_type;
+ u32 port;
+ u32 dev;
+ u8 mac[ETH_ALEN];
+ u16 vid;
+ u8 dynamic;
+ u32 flush_mode;
+};
+
+struct prestera_msg_bridge_req {
+ struct prestera_msg_cmd cmd;
+ u32 port;
+ u32 dev;
+ u16 bridge;
+};
+
+struct prestera_msg_bridge_resp {
+ struct prestera_msg_ret ret;
+ u16 bridge;
+};
+
+struct prestera_msg_stp_req {
+ struct prestera_msg_cmd cmd;
+ u32 port;
+ u32 dev;
+ u16 vid;
+ u8 state;
+};
+
struct prestera_msg_rxtx_req {
struct prestera_msg_cmd cmd;
u8 use_sdma;
@@ -243,6 +309,18 @@ struct prestera_msg_event_port {
union prestera_msg_event_port_param param;
};
+union prestera_msg_event_fdb_param {
+ u8 mac[ETH_ALEN];
+};
+
+struct prestera_msg_event_fdb {
+ struct prestera_msg_event id;
+ u8 dest_type;
+ u32 port_id;
+ u32 vid;
+ union prestera_msg_event_fdb_param param;
+};
+
static int __prestera_cmd_ret(struct prestera_switch *sw,
enum prestera_cmd_type_t type,
struct prestera_msg_cmd *cmd, size_t clen,
@@ -308,10 +386,25 @@ static int prestera_fw_parse_port_evt(u8 *msg, struct prestera_event *evt)
return 0;
}
+static int prestera_fw_parse_fdb_evt(u8 *msg, struct prestera_event *evt)
+{
+ struct prestera_msg_event_fdb *hw_evt;
+
+ hw_evt = (struct prestera_msg_event_fdb *)msg;
+
+ evt->fdb_evt.port_id = hw_evt->port_id;
+ evt->fdb_evt.vid = hw_evt->vid;
+
+ memcpy(&evt->fdb_evt.data, &hw_evt->param, sizeof(u8) * ETH_ALEN);
+
+ return 0;
+}
+
static struct prestera_fw_evt_parser {
int (*func)(u8 *msg, struct prestera_event *evt);
} fw_event_parsers[PRESTERA_EVENT_TYPE_MAX] = {
[PRESTERA_EVENT_TYPE_PORT] = {.func = prestera_fw_parse_port_evt},
+ [PRESTERA_EVENT_TYPE_FDB] = {.func = prestera_fw_parse_fdb_evt},
};
static struct prestera_fw_event_handler *
@@ -450,6 +543,17 @@ void prestera_hw_switch_fini(struct prestera_switch *sw)
WARN_ON(!list_empty(&sw->event_handlers));
}
+int prestera_hw_switch_ageing_set(struct prestera_switch *sw, u32 ageing)
+{
+ struct prestera_msg_switch_attr_req req = {
+ .param = {.ageing_timeout = ageing},
+ .attr = PRESTERA_CMD_SWITCH_ATTR_AGEING,
+ };
+
+ return prestera_cmd(sw, PRESTERA_CMD_TYPE_SWITCH_ATTR_SET,
+ &req.cmd, sizeof(req));
+}
+
int prestera_hw_port_state_set(const struct prestera_port *port,
bool admin_state)
{
@@ -490,6 +594,20 @@ int prestera_hw_port_mac_set(const struct prestera_port *port, char *mac)
&req.cmd, sizeof(req));
}
+int prestera_hw_port_accept_frm_type(struct prestera_port *port,
+ enum prestera_accept_frm_type type)
+{
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_ACCEPT_FRAME_TYPE,
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ .param = {.accept_frm_type = type}
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_SET,
+ &req.cmd, sizeof(req));
+}
+
int prestera_hw_port_cap_get(const struct prestera_port *port,
struct prestera_port_caps *caps)
{
@@ -840,6 +958,210 @@ int prestera_hw_port_stats_get(const struct prestera_port *port,
return 0;
}
+int prestera_hw_port_learning_set(struct prestera_port *port, bool enable)
+{
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_LEARNING,
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ .param = {.learning = enable}
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_SET,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_port_flood_set(struct prestera_port *port, bool flood)
+{
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_FLOOD,
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ .param = {.flood = flood}
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_SET,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_vlan_create(struct prestera_switch *sw, u16 vid)
+{
+ struct prestera_msg_vlan_req req = {
+ .vid = vid,
+ };
+
+ return prestera_cmd(sw, PRESTERA_CMD_TYPE_VLAN_CREATE,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_vlan_delete(struct prestera_switch *sw, u16 vid)
+{
+ struct prestera_msg_vlan_req req = {
+ .vid = vid,
+ };
+
+ return prestera_cmd(sw, PRESTERA_CMD_TYPE_VLAN_DELETE,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_vlan_port_set(struct prestera_port *port, u16 vid,
+ bool is_member, bool untagged)
+{
+ struct prestera_msg_vlan_req req = {
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ .vid = vid,
+ .is_member = is_member,
+ .is_tagged = !untagged
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_VLAN_PORT_SET,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_vlan_port_vid_set(struct prestera_port *port, u16 vid)
+{
+ struct prestera_msg_vlan_req req = {
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ .vid = vid
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_VLAN_PVID_SET,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_vlan_port_stp_set(struct prestera_port *port, u16 vid, u8 state)
+{
+ struct prestera_msg_stp_req req = {
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ .vid = vid,
+ .state = state
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_STP_PORT_SET,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_fdb_add(struct prestera_port *port, const unsigned char *mac,
+ u16 vid, bool dynamic)
+{
+ struct prestera_msg_fdb_req req = {
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ .vid = vid,
+ .dynamic = dynamic
+ };
+
+ memcpy(req.mac, mac, sizeof(req.mac));
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_FDB_ADD,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_fdb_del(struct prestera_port *port, const unsigned char *mac,
+ u16 vid)
+{
+ struct prestera_msg_fdb_req req = {
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ .vid = vid
+ };
+
+ memcpy(req.mac, mac, sizeof(req.mac));
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_FDB_DELETE,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_fdb_flush_port(struct prestera_port *port, u32 mode)
+{
+ struct prestera_msg_fdb_req req = {
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ .flush_mode = mode,
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_FDB_FLUSH_PORT,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_fdb_flush_vlan(struct prestera_switch *sw, u16 vid, u32 mode)
+{
+ struct prestera_msg_fdb_req req = {
+ .vid = vid,
+ .flush_mode = mode,
+ };
+
+ return prestera_cmd(sw, PRESTERA_CMD_TYPE_FDB_FLUSH_VLAN,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_fdb_flush_port_vlan(struct prestera_port *port, u16 vid,
+ u32 mode)
+{
+ struct prestera_msg_fdb_req req = {
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ .vid = vid,
+ .flush_mode = mode,
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_FDB_FLUSH_PORT_VLAN,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_bridge_create(struct prestera_switch *sw, u16 *bridge_id)
+{
+ struct prestera_msg_bridge_resp resp;
+ struct prestera_msg_bridge_req req;
+ int err;
+
+ err = prestera_cmd_ret(sw, PRESTERA_CMD_TYPE_BRIDGE_CREATE,
+ &req.cmd, sizeof(req),
+ &resp.ret, sizeof(resp));
+ if (err)
+ return err;
+
+ *bridge_id = resp.bridge;
+ return err;
+}
+
+int prestera_hw_bridge_delete(struct prestera_switch *sw, u16 bridge_id)
+{
+ struct prestera_msg_bridge_req req = {
+ .bridge = bridge_id
+ };
+
+ return prestera_cmd(sw, PRESTERA_CMD_TYPE_BRIDGE_DELETE,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_bridge_port_add(struct prestera_port *port, u16 bridge_id)
+{
+ struct prestera_msg_bridge_req req = {
+ .bridge = bridge_id,
+ .port = port->hw_id,
+ .dev = port->dev_id
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_BRIDGE_PORT_ADD,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_bridge_port_delete(struct prestera_port *port, u16 bridge_id)
+{
+ struct prestera_msg_bridge_req req = {
+ .bridge = bridge_id,
+ .port = port->hw_id,
+ .dev = port->dev_id
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_BRIDGE_PORT_DELETE,
+ &req.cmd, sizeof(req));
+}
+
int prestera_hw_rxtx_init(struct prestera_switch *sw,
struct prestera_rxtx_params *params)
{
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_hw.h b/drivers/net/ethernet/marvell/prestera/prestera_hw.h
index bcb04848894b..e1d7bf515eba 100644
--- a/drivers/net/ethernet/marvell/prestera/prestera_hw.h
+++ b/drivers/net/ethernet/marvell/prestera/prestera_hw.h
@@ -9,6 +9,19 @@
#include <linux/types.h>
+enum prestera_accept_frm_type {
+ PRESTERA_ACCEPT_FRAME_TYPE_TAGGED,
+ PRESTERA_ACCEPT_FRAME_TYPE_UNTAGGED,
+ PRESTERA_ACCEPT_FRAME_TYPE_ALL
+};
+
+enum prestera_fdb_flush_mode {
+ PRESTERA_FDB_FLUSH_MODE_DYNAMIC = BIT(0),
+ PRESTERA_FDB_FLUSH_MODE_STATIC = BIT(1),
+ PRESTERA_FDB_FLUSH_MODE_ALL = PRESTERA_FDB_FLUSH_MODE_DYNAMIC
+ | PRESTERA_FDB_FLUSH_MODE_STATIC,
+};
+
enum {
PRESTERA_LINK_MODE_10baseT_Half,
PRESTERA_LINK_MODE_10baseT_Full,
@@ -68,6 +81,13 @@ enum {
PRESTERA_PORT_DUPLEX_FULL
};
+enum {
+ PRESTERA_STP_DISABLED,
+ PRESTERA_STP_BLOCK_LISTEN,
+ PRESTERA_STP_LEARN,
+ PRESTERA_STP_FORWARD
+};
+
struct prestera_switch;
struct prestera_port;
struct prestera_port_stats;
@@ -83,6 +103,7 @@ struct prestera_rxtx_params;
/* Switch API */
int prestera_hw_switch_init(struct prestera_switch *sw);
void prestera_hw_switch_fini(struct prestera_switch *sw);
+int prestera_hw_switch_ageing_set(struct prestera_switch *sw, u32 ageing);
int prestera_hw_switch_mac_set(struct prestera_switch *sw, char *mac);
/* Port API */
@@ -115,6 +136,33 @@ int prestera_hw_port_mdix_get(const struct prestera_port *port, u8 *status,
u8 *admin_mode);
int prestera_hw_port_mdix_set(const struct prestera_port *port, u8 mode);
int prestera_hw_port_speed_get(const struct prestera_port *port, u32 *speed);
+int prestera_hw_port_learning_set(struct prestera_port *port, bool enable);
+int prestera_hw_port_flood_set(struct prestera_port *port, bool flood);
+int prestera_hw_port_accept_frm_type(struct prestera_port *port,
+ enum prestera_accept_frm_type type);
+/* Vlan API */
+int prestera_hw_vlan_create(struct prestera_switch *sw, u16 vid);
+int prestera_hw_vlan_delete(struct prestera_switch *sw, u16 vid);
+int prestera_hw_vlan_port_set(struct prestera_port *port, u16 vid,
+ bool is_member, bool untagged);
+int prestera_hw_vlan_port_vid_set(struct prestera_port *port, u16 vid);
+int prestera_hw_vlan_port_stp_set(struct prestera_port *port, u16 vid, u8 state);
+
+/* FDB API */
+int prestera_hw_fdb_add(struct prestera_port *port, const unsigned char *mac,
+ u16 vid, bool dynamic);
+int prestera_hw_fdb_del(struct prestera_port *port, const unsigned char *mac,
+ u16 vid);
+int prestera_hw_fdb_flush_port(struct prestera_port *port, u32 mode);
+int prestera_hw_fdb_flush_vlan(struct prestera_switch *sw, u16 vid, u32 mode);
+int prestera_hw_fdb_flush_port_vlan(struct prestera_port *port, u16 vid,
+ u32 mode);
+
+/* Bridge API */
+int prestera_hw_bridge_create(struct prestera_switch *sw, u16 *bridge_id);
+int prestera_hw_bridge_delete(struct prestera_switch *sw, u16 bridge_id);
+int prestera_hw_bridge_port_add(struct prestera_port *port, u16 bridge_id);
+int prestera_hw_bridge_port_delete(struct prestera_port *port, u16 bridge_id);
/* Event handlers */
int prestera_hw_event_handler_register(struct prestera_switch *sw,
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_main.c b/drivers/net/ethernet/marvell/prestera/prestera_main.c
index 25f1310da4cf..075ad9b9fabf 100644
--- a/drivers/net/ethernet/marvell/prestera/prestera_main.c
+++ b/drivers/net/ethernet/marvell/prestera/prestera_main.c
@@ -17,6 +17,7 @@
#include "prestera_rxtx.h"
#include "prestera_devlink.h"
#include "prestera_ethtool.h"
+#include "prestera_switchdev.h"
#define PRESTERA_MTU_DEFAULT 1536
@@ -24,6 +25,29 @@
static struct workqueue_struct *prestera_wq;
+int prestera_port_pvid_set(struct prestera_port *port, u16 vid)
+{
+ enum prestera_accept_frm_type frm_type;
+ int err;
+
+ frm_type = PRESTERA_ACCEPT_FRAME_TYPE_TAGGED;
+
+ if (vid) {
+ err = prestera_hw_vlan_port_vid_set(port, vid);
+ if (err)
+ return err;
+
+ frm_type = PRESTERA_ACCEPT_FRAME_TYPE_ALL;
+ }
+
+ err = prestera_hw_port_accept_frm_type(port, frm_type);
+ if (err && frm_type == PRESTERA_ACCEPT_FRAME_TYPE_ALL)
+ prestera_hw_vlan_port_vid_set(port, port->pvid);
+
+ port->pvid = vid;
+ return 0;
+}
+
struct prestera_port *prestera_port_find_by_hwid(struct prestera_switch *sw,
u32 dev_id, u32 hw_id)
{
@@ -37,8 +61,7 @@ struct prestera_port *prestera_port_find_by_hwid(struct prestera_switch *sw,
return NULL;
}
-static struct prestera_port *prestera_find_port(struct prestera_switch *sw,
- u32 id)
+struct prestera_port *prestera_find_port(struct prestera_switch *sw, u32 id)
{
struct prestera_port *port;
@@ -249,6 +272,8 @@ static int prestera_port_create(struct prestera_switch *sw, u32 id)
port = netdev_priv(dev);
+ INIT_LIST_HEAD(&port->vlans_list);
+ port->pvid = PRESTERA_DEFAULT_VID;
port->dev = dev;
port->id = id;
port->sw = sw;
@@ -442,6 +467,72 @@ static int prestera_switch_set_base_mac_addr(struct prestera_switch *sw)
return prestera_hw_switch_mac_set(sw, sw->base_mac);
}
+bool prestera_netdev_check(const struct net_device *dev)
+{
+ return dev->netdev_ops == &netdev_ops;
+}
+
+static int prestera_lower_dev_walk(struct net_device *dev, void *data)
+{
+ struct prestera_port **pport = data;
+
+ if (prestera_netdev_check(dev)) {
+ *pport = netdev_priv(dev);
+ return 1;
+ }
+
+ return 0;
+}
+
+struct prestera_port *prestera_port_dev_lower_find(struct net_device *dev)
+{
+ struct prestera_port *port;
+
+ if (prestera_netdev_check(dev))
+ return netdev_priv(dev);
+
+ port = NULL;
+ netdev_walk_all_lower_dev(dev, prestera_lower_dev_walk, &port);
+
+ return port;
+}
+
+static int prestera_netdev_port_event(struct net_device *dev,
+ unsigned long event, void *ptr)
+{
+ switch (event) {
+ case NETDEV_PRECHANGEUPPER:
+ case NETDEV_CHANGEUPPER:
+ return prestera_bridge_port_event(dev, event, ptr);
+ }
+
+ return 0;
+}
+
+static int prestera_netdev_event_handler(struct notifier_block *nb,
+ unsigned long event, void *ptr)
+{
+ struct net_device *dev = netdev_notifier_info_to_dev(ptr);
+ int err = 0;
+
+ if (prestera_netdev_check(dev))
+ err = prestera_netdev_port_event(dev, event, ptr);
+
+ return notifier_from_errno(err);
+}
+
+static int prestera_netdev_event_handler_register(struct prestera_switch *sw)
+{
+ sw->netdev_nb.notifier_call = prestera_netdev_event_handler;
+
+ return register_netdevice_notifier(&sw->netdev_nb);
+}
+
+static void prestera_netdev_event_handler_unregister(struct prestera_switch *sw)
+{
+ unregister_netdevice_notifier(&sw->netdev_nb);
+}
+
static int prestera_switch_init(struct prestera_switch *sw)
{
int err;
@@ -458,10 +549,18 @@ static int prestera_switch_init(struct prestera_switch *sw)
if (err)
return err;
- err = prestera_rxtx_switch_init(sw);
+ err = prestera_netdev_event_handler_register(sw);
if (err)
return err;
+ err = prestera_switchdev_init(sw);
+ if (err)
+ goto err_swdev_register;
+
+ err = prestera_rxtx_switch_init(sw);
+ if (err)
+ goto err_rxtx_register;
+
err = prestera_event_handlers_register(sw);
if (err)
goto err_handlers_register;
@@ -482,6 +581,10 @@ static int prestera_switch_init(struct prestera_switch *sw)
prestera_event_handlers_unregister(sw);
err_handlers_register:
prestera_rxtx_switch_fini(sw);
+err_rxtx_register:
+ prestera_switchdev_fini(sw);
+err_swdev_register:
+ prestera_netdev_event_handler_unregister(sw);
prestera_hw_switch_fini(sw);
return err;
@@ -493,6 +596,8 @@ static void prestera_switch_fini(struct prestera_switch *sw)
prestera_devlink_unregister(sw);
prestera_event_handlers_unregister(sw);
prestera_rxtx_switch_fini(sw);
+ prestera_switchdev_fini(sw);
+ prestera_netdev_event_handler_unregister(sw);
prestera_hw_switch_fini(sw);
}
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_switchdev.c b/drivers/net/ethernet/marvell/prestera/prestera_switchdev.c
new file mode 100644
index 000000000000..40619cf3a343
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/prestera_switchdev.c
@@ -0,0 +1,1286 @@
+// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+/* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/if_vlan.h>
+#include <linux/if_bridge.h>
+#include <linux/notifier.h>
+#include <net/switchdev.h>
+#include <net/netevent.h>
+
+#include "prestera.h"
+#include "prestera_hw.h"
+#include "prestera_switchdev.h"
+
+#define PRESTERA_VID_ALL (0xffff)
+
+#define PRESTERA_DEFAULT_AGEING_TIME_MS 300
+#define PRESTERA_MAX_AGEING_TIME_MS 1000000
+#define PRESTERA_MIN_AGEING_TIME_MS 10
+
+struct prestera_fdb_event_work {
+ struct work_struct work;
+ struct switchdev_notifier_fdb_info fdb_info;
+ struct net_device *dev;
+ unsigned long event;
+};
+
+struct prestera_switchdev {
+ struct prestera_switch *sw;
+ u32 ageing_time;
+ struct list_head bridge_list;
+ bool bridge_8021q_exists;
+ struct notifier_block swdev_nb_blk;
+ struct notifier_block swdev_nb;
+};
+
+struct prestera_bridge {
+ struct list_head head;
+ struct net_device *dev;
+ struct prestera_switchdev *swdev;
+ struct list_head port_list;
+ bool vlan_enabled;
+ u16 bridge_id;
+};
+
+struct prestera_bridge_port {
+ struct list_head head;
+ struct net_device *dev;
+ struct prestera_bridge *bridge;
+ struct list_head vlan_list;
+ refcount_t ref_count;
+ unsigned long flags;
+ u8 stp_state;
+};
+
+struct prestera_bridge_vlan {
+ struct list_head head;
+ struct list_head port_vlan_list;
+ u16 vid;
+};
+
+struct prestera_port_vlan {
+ struct list_head br_vlan_head;
+ struct list_head port_head;
+ struct prestera_port *port;
+ struct prestera_bridge_port *br_port;
+ u16 vid;
+};
+
+static struct workqueue_struct *swdev_wq;
+
+static void prestera_bridge_port_put(struct prestera_bridge_port *br_port);
+
+static int prestera_port_vid_stp_set(struct prestera_port *port, u16 vid,
+ u8 state);
+
+static struct prestera_bridge_vlan *
+prestera_bridge_vlan_create(struct prestera_bridge_port *br_port, u16 vid)
+{
+ struct prestera_bridge_vlan *br_vlan;
+
+ br_vlan = kzalloc(sizeof(*br_vlan), GFP_KERNEL);
+ if (!br_vlan)
+ return NULL;
+
+ INIT_LIST_HEAD(&br_vlan->port_vlan_list);
+ br_vlan->vid = vid;
+ list_add(&br_vlan->head, &br_port->vlan_list);
+
+ return br_vlan;
+}
+
+static void prestera_bridge_vlan_destroy(struct prestera_bridge_vlan *br_vlan)
+{
+ list_del(&br_vlan->head);
+ WARN_ON(!list_empty(&br_vlan->port_vlan_list));
+ kfree(br_vlan);
+}
+
+static struct prestera_bridge_vlan *
+prestera_bridge_vlan_by_vid(struct prestera_bridge_port *br_port, u16 vid)
+{
+ struct prestera_bridge_vlan *br_vlan;
+
+ list_for_each_entry(br_vlan, &br_port->vlan_list, head) {
+ if (br_vlan->vid == vid)
+ return br_vlan;
+ }
+
+ return NULL;
+}
+
+static int prestera_bridge_vlan_port_count(struct prestera_bridge *bridge,
+ u16 vid)
+{
+ struct prestera_bridge_port *br_port;
+ struct prestera_bridge_vlan *br_vlan;
+ int count = 0;
+
+ list_for_each_entry(br_port, &bridge->port_list, head) {
+ list_for_each_entry(br_vlan, &br_port->vlan_list, head) {
+ if (br_vlan->vid == vid) {
+ count += 1;
+ break;
+ }
+ }
+ }
+
+ return count;
+}
+
+static void prestera_bridge_vlan_put(struct prestera_bridge_vlan *br_vlan)
+{
+ if (list_empty(&br_vlan->port_vlan_list))
+ prestera_bridge_vlan_destroy(br_vlan);
+}
+
+static struct prestera_port_vlan *
+prestera_port_vlan_by_vid(struct prestera_port *port, u16 vid)
+{
+ struct prestera_port_vlan *port_vlan;
+
+ list_for_each_entry(port_vlan, &port->vlans_list, port_head) {
+ if (port_vlan->vid == vid)
+ return port_vlan;
+ }
+
+ return NULL;
+}
+
+static struct prestera_port_vlan *
+prestera_port_vlan_create(struct prestera_port *port, u16 vid, bool untagged)
+{
+ struct prestera_port_vlan *port_vlan;
+ int err;
+
+ port_vlan = prestera_port_vlan_by_vid(port, vid);
+ if (port_vlan)
+ return ERR_PTR(-EEXIST);
+
+ err = prestera_hw_vlan_port_set(port, vid, true, untagged);
+ if (err)
+ return ERR_PTR(err);
+
+ port_vlan = kzalloc(sizeof(*port_vlan), GFP_KERNEL);
+ if (!port_vlan) {
+ err = -ENOMEM;
+ goto err_port_vlan_alloc;
+ }
+
+ port_vlan->port = port;
+ port_vlan->vid = vid;
+
+ list_add(&port_vlan->port_head, &port->vlans_list);
+
+ return port_vlan;
+
+err_port_vlan_alloc:
+ prestera_hw_vlan_port_set(port, vid, false, false);
+ return ERR_PTR(err);
+}
+
+static void
+prestera_port_vlan_bridge_leave(struct prestera_port_vlan *port_vlan)
+{
+ u32 fdb_flush_mode = PRESTERA_FDB_FLUSH_MODE_DYNAMIC;
+ struct prestera_port *port = port_vlan->port;
+ struct prestera_bridge_vlan *br_vlan;
+ struct prestera_bridge_port *br_port;
+ u16 vid = port_vlan->vid;
+ bool last_port, last_vlan;
+ int port_count;
+
+ br_port = port_vlan->br_port;
+ port_count = prestera_bridge_vlan_port_count(br_port->bridge, vid);
+ br_vlan = prestera_bridge_vlan_by_vid(br_port, vid);
+
+ last_vlan = list_is_singular(&br_port->vlan_list);
+ last_port = port_count == 1;
+
+ if (last_vlan)
+ prestera_hw_fdb_flush_port(port, fdb_flush_mode);
+ else if (last_port)
+ prestera_hw_fdb_flush_vlan(port->sw, vid, fdb_flush_mode);
+ else
+ prestera_hw_fdb_flush_port_vlan(port, vid, fdb_flush_mode);
+
+ list_del(&port_vlan->br_vlan_head);
+ prestera_bridge_vlan_put(br_vlan);
+ prestera_bridge_port_put(br_port);
+ port_vlan->br_port = NULL;
+}
+
+static void prestera_port_vlan_destroy(struct prestera_port_vlan *port_vlan)
+{
+ struct prestera_port *port = port_vlan->port;
+ u16 vid = port_vlan->vid;
+
+ if (port_vlan->br_port)
+ prestera_port_vlan_bridge_leave(port_vlan);
+
+ list_del(&port_vlan->port_head);
+ kfree(port_vlan);
+ prestera_hw_vlan_port_set(port, vid, false, false);
+}
+
+static struct prestera_bridge *
+prestera_bridge_create(struct prestera_switchdev *swdev, struct net_device *dev)
+{
+ bool vlan_enabled = br_vlan_enabled(dev);
+ struct prestera_bridge *bridge;
+ u16 bridge_id;
+ int err;
+
+ if (vlan_enabled && swdev->bridge_8021q_exists) {
+ netdev_err(dev, "Only one VLAN-aware bridge is supported\n");
+ return ERR_PTR(-EINVAL);
+ }
+
+ bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
+ if (!bridge)
+ return ERR_PTR(-ENOMEM);
+
+ if (vlan_enabled) {
+ swdev->bridge_8021q_exists = true;
+ } else {
+ err = prestera_hw_bridge_create(swdev->sw, &bridge_id);
+ if (err) {
+ kfree(bridge);
+ return ERR_PTR(err);
+ }
+
+ bridge->bridge_id = bridge_id;
+ }
+
+ bridge->vlan_enabled = vlan_enabled;
+ bridge->swdev = swdev;
+ bridge->dev = dev;
+
+ INIT_LIST_HEAD(&bridge->port_list);
+
+ list_add(&bridge->head, &swdev->bridge_list);
+
+ return bridge;
+}
+
+static void prestera_bridge_destroy(struct prestera_bridge *bridge)
+{
+ struct prestera_switchdev *swdev = bridge->swdev;
+
+ list_del(&bridge->head);
+
+ if (bridge->vlan_enabled)
+ swdev->bridge_8021q_exists = false;
+ else
+ prestera_hw_bridge_delete(swdev->sw, bridge->bridge_id);
+
+ WARN_ON(!list_empty(&bridge->port_list));
+ kfree(bridge);
+}
+
+static void prestera_bridge_put(struct prestera_bridge *bridge)
+{
+ if (list_empty(&bridge->port_list))
+ prestera_bridge_destroy(bridge);
+}
+
+static
+struct prestera_bridge *prestera_bridge_by_dev(struct prestera_switchdev *swdev,
+ const struct net_device *dev)
+{
+ struct prestera_bridge *bridge;
+
+ list_for_each_entry(bridge, &swdev->bridge_list, head)
+ if (bridge->dev == dev)
+ return bridge;
+
+ return NULL;
+}
+
+static struct prestera_bridge_port *
+__prestera_bridge_port_by_dev(struct prestera_bridge *bridge,
+ struct net_device *dev)
+{
+ struct prestera_bridge_port *br_port;
+
+ list_for_each_entry(br_port, &bridge->port_list, head) {
+ if (br_port->dev == dev)
+ return br_port;
+ }
+
+ return NULL;
+}
+
+static struct prestera_bridge_port *
+prestera_bridge_port_by_dev(struct prestera_switchdev *swdev,
+ struct net_device *dev)
+{
+ struct net_device *br_dev = netdev_master_upper_dev_get(dev);
+ struct prestera_bridge *bridge;
+
+ if (!br_dev)
+ return NULL;
+
+ bridge = prestera_bridge_by_dev(swdev, br_dev);
+ if (!bridge)
+ return NULL;
+
+ return __prestera_bridge_port_by_dev(bridge, dev);
+}
+
+static struct prestera_bridge_port *
+prestera_bridge_port_create(struct prestera_bridge *bridge,
+ struct net_device *dev)
+{
+ struct prestera_bridge_port *br_port;
+
+ br_port = kzalloc(sizeof(*br_port), GFP_KERNEL);
+ if (!br_port)
+ return NULL;
+
+ br_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC |
+ BR_MCAST_FLOOD;
+ br_port->stp_state = BR_STATE_DISABLED;
+ refcount_set(&br_port->ref_count, 1);
+ br_port->bridge = bridge;
+ br_port->dev = dev;
+
+ INIT_LIST_HEAD(&br_port->vlan_list);
+ list_add(&br_port->head, &bridge->port_list);
+
+ return br_port;
+}
+
+static void
+prestera_bridge_port_destroy(struct prestera_bridge_port *br_port)
+{
+ list_del(&br_port->head);
+ WARN_ON(!list_empty(&br_port->vlan_list));
+ kfree(br_port);
+}
+
+static void prestera_bridge_port_get(struct prestera_bridge_port *br_port)
+{
+ refcount_inc(&br_port->ref_count);
+}
+
+static void prestera_bridge_port_put(struct prestera_bridge_port *br_port)
+{
+ struct prestera_bridge *bridge = br_port->bridge;
+
+ if (refcount_dec_and_test(&br_port->ref_count)) {
+ prestera_bridge_port_destroy(br_port);
+ prestera_bridge_put(bridge);
+ }
+}
+
+static struct prestera_bridge_port *
+prestera_bridge_port_add(struct prestera_bridge *bridge, struct net_device *dev)
+{
+ struct prestera_bridge_port *br_port;
+
+ br_port = __prestera_bridge_port_by_dev(bridge, dev);
+ if (br_port) {
+ prestera_bridge_port_get(br_port);
+ return br_port;
+ }
+
+ br_port = prestera_bridge_port_create(bridge, dev);
+ if (!br_port)
+ return ERR_PTR(-ENOMEM);
+
+ return br_port;
+}
+
+static int
+prestera_bridge_1d_port_join(struct prestera_bridge_port *br_port)
+{
+ struct prestera_port *port = netdev_priv(br_port->dev);
+ struct prestera_bridge *bridge = br_port->bridge;
+ int err;
+
+ err = prestera_hw_bridge_port_add(port, bridge->bridge_id);
+ if (err)
+ return err;
+
+ err = prestera_hw_port_flood_set(port, br_port->flags & BR_FLOOD);
+ if (err)
+ goto err_port_flood_set;
+
+ err = prestera_hw_port_learning_set(port, br_port->flags & BR_LEARNING);
+ if (err)
+ goto err_port_learning_set;
+
+ return err;
+
+err_port_learning_set:
+ prestera_hw_port_flood_set(port, false);
+err_port_flood_set:
+ prestera_hw_bridge_port_delete(port, bridge->bridge_id);
+
+ return err;
+}
+
+static int prestera_port_bridge_join(struct prestera_port *port,
+ struct net_device *upper)
+{
+ struct prestera_bridge_port *br_port;
+ struct prestera_switchdev *swdev;
+ struct prestera_bridge *bridge;
+ int err;
+
+ swdev = port->sw->swdev;
+
+ bridge = prestera_bridge_by_dev(swdev, upper);
+ if (!bridge) {
+ bridge = prestera_bridge_create(swdev, upper);
+ if (IS_ERR(bridge))
+ return PTR_ERR(bridge);
+ }
+
+ br_port = prestera_bridge_port_add(bridge, port->dev);
+ if (IS_ERR(br_port)) {
+ err = PTR_ERR(br_port);
+ goto err_brport_create;
+ }
+
+ if (bridge->vlan_enabled)
+ return 0;
+
+ err = prestera_bridge_1d_port_join(br_port);
+ if (err)
+ goto err_port_join;
+
+ return 0;
+
+err_port_join:
+ prestera_bridge_port_put(br_port);
+err_brport_create:
+ prestera_bridge_put(bridge);
+ return err;
+}
+
+static void prestera_bridge_1q_port_leave(struct prestera_bridge_port *br_port)
+{
+ struct prestera_port *port = netdev_priv(br_port->dev);
+
+ prestera_hw_fdb_flush_port(port, PRESTERA_FDB_FLUSH_MODE_ALL);
+ prestera_port_pvid_set(port, PRESTERA_DEFAULT_VID);
+}
+
+static void prestera_bridge_1d_port_leave(struct prestera_bridge_port *br_port)
+{
+ struct prestera_port *port = netdev_priv(br_port->dev);
+
+ prestera_hw_fdb_flush_port(port, PRESTERA_FDB_FLUSH_MODE_ALL);
+ prestera_hw_bridge_port_delete(port, br_port->bridge->bridge_id);
+}
+
+static int prestera_port_vid_stp_set(struct prestera_port *port, u16 vid,
+ u8 state)
+{
+ u8 hw_state = state;
+
+ switch (state) {
+ case BR_STATE_DISABLED:
+ hw_state = PRESTERA_STP_DISABLED;
+ break;
+
+ case BR_STATE_BLOCKING:
+ case BR_STATE_LISTENING:
+ hw_state = PRESTERA_STP_BLOCK_LISTEN;
+ break;
+
+ case BR_STATE_LEARNING:
+ hw_state = PRESTERA_STP_LEARN;
+ break;
+
+ case BR_STATE_FORWARDING:
+ hw_state = PRESTERA_STP_FORWARD;
+ break;
+
+ default:
+ return -EINVAL;
+ }
+
+ return prestera_hw_vlan_port_stp_set(port, vid, hw_state);
+}
+
+static void prestera_port_bridge_leave(struct prestera_port *port,
+ struct net_device *upper)
+{
+ struct prestera_switchdev *swdev = port->sw->swdev;
+ struct prestera_bridge_port *br_port;
+ struct prestera_bridge *bridge;
+
+ bridge = prestera_bridge_by_dev(swdev, upper);
+ if (!bridge)
+ return;
+
+ br_port = __prestera_bridge_port_by_dev(bridge, port->dev);
+ if (!br_port)
+ return;
+
+ bridge = br_port->bridge;
+
+ if (bridge->vlan_enabled)
+ prestera_bridge_1q_port_leave(br_port);
+ else
+ prestera_bridge_1d_port_leave(br_port);
+
+ prestera_hw_port_learning_set(port, false);
+ prestera_hw_port_flood_set(port, false);
+ prestera_port_vid_stp_set(port, PRESTERA_VID_ALL, BR_STATE_FORWARDING);
+ prestera_bridge_port_put(br_port);
+}
+
+int prestera_bridge_port_event(struct net_device *dev, unsigned long event,
+ void *ptr)
+{
+ struct netdev_notifier_changeupper_info *info = ptr;
+ struct netlink_ext_ack *extack;
+ struct prestera_port *port;
+ struct net_device *upper;
+ int err = 0;
+
+ extack = netdev_notifier_info_to_extack(&info->info);
+ port = netdev_priv(dev);
+ upper = info->upper_dev;
+
+ switch (event) {
+ case NETDEV_PRECHANGEUPPER:
+ if (!netif_is_bridge_master(upper)) {
+ NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
+ return -EINVAL;
+ }
+
+ if (!info->linking)
+ break;
+
+ if (netdev_has_any_upper_dev(upper)) {
+ NL_SET_ERR_MSG_MOD(extack, "Upper device is already enslaved");
+ return -EINVAL;
+ }
+ break;
+
+ case NETDEV_CHANGEUPPER:
+ if (!netif_is_bridge_master(upper))
+ break;
+
+ if (info->linking)
+ err = prestera_port_bridge_join(port, upper);
+ else
+ prestera_port_bridge_leave(port, upper);
+ break;
+ }
+
+ return err;
+}
+
+static int prestera_port_attr_br_flags_set(struct prestera_port *port,
+ struct switchdev_trans *trans,
+ struct net_device *dev,
+ unsigned long flags)
+{
+ struct prestera_bridge_port *br_port;
+ int err;
+
+ if (switchdev_trans_ph_prepare(trans))
+ return 0;
+
+ br_port = prestera_bridge_port_by_dev(port->sw->swdev, dev);
+ if (!br_port)
+ return 0;
+
+ err = prestera_hw_port_flood_set(port, flags & BR_FLOOD);
+ if (err)
+ return err;
+
+ err = prestera_hw_port_learning_set(port, flags & BR_LEARNING);
+ if (err)
+ return err;
+
+ memcpy(&br_port->flags, &flags, sizeof(flags));
+ return 0;
+}
+
+static int prestera_port_attr_br_ageing_set(struct prestera_port *port,
+ struct switchdev_trans *trans,
+ unsigned long ageing_clock_t)
+{
+ unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
+ u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
+ struct prestera_switch *sw = port->sw;
+ int err;
+
+ if (switchdev_trans_ph_prepare(trans)) {
+ if (ageing_time < PRESTERA_MIN_AGEING_TIME_MS ||
+ ageing_time > PRESTERA_MAX_AGEING_TIME_MS)
+ return -ERANGE;
+ else
+ return 0;
+ }
+
+ err = prestera_hw_switch_ageing_set(sw, ageing_time);
+ if (!err)
+ sw->swdev->ageing_time = ageing_time;
+
+ return err;
+}
+
+static int prestera_port_attr_br_vlan_set(struct prestera_port *port,
+ struct switchdev_trans *trans,
+ struct net_device *dev,
+ bool vlan_enabled)
+{
+ struct prestera_switch *sw = port->sw;
+ struct prestera_bridge *bridge;
+
+ if (!switchdev_trans_ph_prepare(trans))
+ return 0;
+
+ bridge = prestera_bridge_by_dev(sw->swdev, dev);
+ if (WARN_ON(!bridge))
+ return -EINVAL;
+
+ if (bridge->vlan_enabled == vlan_enabled)
+ return 0;
+
+ netdev_err(bridge->dev, "VLAN filtering can't be changed for existing bridge\n");
+
+ return -EINVAL;
+}
+
+static int prestera_port_bridge_vlan_stp_set(struct prestera_port *port,
+ struct prestera_bridge_vlan *br_vlan,
+ u8 state)
+{
+ struct prestera_port_vlan *port_vlan;
+
+ list_for_each_entry(port_vlan, &br_vlan->port_vlan_list, br_vlan_head) {
+ if (port_vlan->port != port)
+ continue;
+
+ return prestera_port_vid_stp_set(port, br_vlan->vid, state);
+ }
+
+ return 0;
+}
+
+static int presterar_port_attr_stp_state_set(struct prestera_port *port,
+ struct switchdev_trans *trans,
+ struct net_device *dev,
+ u8 state)
+{
+ struct prestera_bridge_port *br_port;
+ struct prestera_bridge_vlan *br_vlan;
+ int err;
+ u16 vid;
+
+ if (switchdev_trans_ph_prepare(trans))
+ return 0;
+
+ br_port = prestera_bridge_port_by_dev(port->sw->swdev, dev);
+ if (!br_port)
+ return 0;
+
+ if (!br_port->bridge->vlan_enabled) {
+ vid = br_port->bridge->bridge_id;
+ err = prestera_port_vid_stp_set(port, vid, state);
+ if (err)
+ goto err_port_stp_set;
+ } else {
+ list_for_each_entry(br_vlan, &br_port->vlan_list, head) {
+ err = prestera_port_bridge_vlan_stp_set(port, br_vlan,
+ state);
+ if (err)
+ goto err_port_vlan_stp_set;
+ }
+ }
+
+ br_port->stp_state = state;
+
+ return 0;
+
+err_port_vlan_stp_set:
+ list_for_each_entry_continue_reverse(br_vlan, &br_port->vlan_list, head)
+ prestera_port_bridge_vlan_stp_set(port, br_vlan, br_port->stp_state);
+ return err;
+
+err_port_stp_set:
+ prestera_port_vid_stp_set(port, vid, br_port->stp_state);
+
+ return err;
+}
+
+static int prestera_port_obj_attr_set(struct net_device *dev,
+ const struct switchdev_attr *attr,
+ struct switchdev_trans *trans)
+{
+ struct prestera_port *port = netdev_priv(dev);
+ int err = 0;
+
+ switch (attr->id) {
+ case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
+ err = presterar_port_attr_stp_state_set(port, trans,
+ attr->orig_dev,
+ attr->u.stp_state);
+ break;
+ case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
+ if (attr->u.brport_flags &
+ ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD))
+ err = -EINVAL;
+ break;
+ case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
+ err = prestera_port_attr_br_flags_set(port, trans,
+ attr->orig_dev,
+ attr->u.brport_flags);
+ break;
+ case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
+ err = prestera_port_attr_br_ageing_set(port, trans,
+ attr->u.ageing_time);
+ break;
+ case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
+ err = prestera_port_attr_br_vlan_set(port, trans,
+ attr->orig_dev,
+ attr->u.vlan_filtering);
+ break;
+ default:
+ err = -EOPNOTSUPP;
+ }
+
+ return err;
+}
+
+static void
+prestera_fdb_offload_notify(struct prestera_port *port,
+ struct switchdev_notifier_fdb_info *info)
+{
+ struct switchdev_notifier_fdb_info send_info;
+
+ send_info.addr = info->addr;
+ send_info.vid = info->vid;
+ send_info.offloaded = true;
+
+ call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, port->dev,
+ &send_info.info, NULL);
+}
+
+static int prestera_port_fdb_set(struct prestera_port *port,
+ struct switchdev_notifier_fdb_info *fdb_info,
+ bool adding)
+{
+ struct prestera_switch *sw = port->sw;
+ struct prestera_bridge_port *br_port;
+ struct prestera_bridge *bridge;
+ int err;
+ u16 vid;
+
+ br_port = prestera_bridge_port_by_dev(sw->swdev, port->dev);
+ if (!br_port)
+ return -EINVAL;
+
+ bridge = br_port->bridge;
+
+ if (bridge->vlan_enabled)
+ vid = fdb_info->vid;
+ else
+ vid = bridge->bridge_id;
+
+ if (adding)
+ err = prestera_hw_fdb_add(port, fdb_info->addr, vid, false);
+ else
+ err = prestera_hw_fdb_del(port, fdb_info->addr, vid);
+
+ return err;
+}
+
+static void prestera_fdb_event_work(struct work_struct *work)
+{
+ struct switchdev_notifier_fdb_info *fdb_info;
+ struct prestera_fdb_event_work *swdev_work;
+ struct prestera_port *port;
+ struct net_device *dev;
+ int err = 0;
+
+ swdev_work = container_of(work, struct prestera_fdb_event_work, work);
+ dev = swdev_work->dev;
+
+ rtnl_lock();
+
+ port = prestera_port_dev_lower_find(dev);
+ if (!port)
+ goto out;
+
+ switch (swdev_work->event) {
+ case SWITCHDEV_FDB_ADD_TO_DEVICE:
+ fdb_info = &swdev_work->fdb_info;
+ if (!fdb_info->added_by_user)
+ break;
+
+ err = prestera_port_fdb_set(port, fdb_info, true);
+ if (err)
+ break;
+
+ prestera_fdb_offload_notify(port, fdb_info);
+ break;
+
+ case SWITCHDEV_FDB_DEL_TO_DEVICE:
+ fdb_info = &swdev_work->fdb_info;
+ prestera_port_fdb_set(port, fdb_info, false);
+ break;
+ }
+
+out:
+ rtnl_unlock();
+
+ kfree(swdev_work->fdb_info.addr);
+ kfree(swdev_work);
+ dev_put(dev);
+}
+
+static int prestera_switchdev_event(struct notifier_block *unused,
+ unsigned long event, void *ptr)
+{
+ struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
+ struct switchdev_notifier_fdb_info *fdb_info;
+ struct switchdev_notifier_info *info = ptr;
+ struct prestera_fdb_event_work *swdev_work;
+ struct net_device *upper;
+ int err = 0;
+
+ if (event == SWITCHDEV_PORT_ATTR_SET) {
+ err = switchdev_handle_port_attr_set(dev, ptr,
+ prestera_netdev_check,
+ prestera_port_obj_attr_set);
+ return notifier_from_errno(err);
+ }
+
+ upper = netdev_master_upper_dev_get_rcu(dev);
+ if (!upper)
+ return NOTIFY_DONE;
+
+ if (!netif_is_bridge_master(upper))
+ return NOTIFY_DONE;
+
+ swdev_work = kzalloc(sizeof(*swdev_work), GFP_ATOMIC);
+ if (!swdev_work)
+ return NOTIFY_BAD;
+
+ swdev_work->event = event;
+ swdev_work->dev = dev;
+
+ switch (event) {
+ case SWITCHDEV_FDB_ADD_TO_DEVICE:
+ case SWITCHDEV_FDB_DEL_TO_DEVICE:
+ fdb_info = container_of(info,
+ struct switchdev_notifier_fdb_info,
+ info);
+
+ INIT_WORK(&swdev_work->work, prestera_fdb_event_work);
+ memcpy(&swdev_work->fdb_info, ptr,
+ sizeof(swdev_work->fdb_info));
+
+ swdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
+ if (!swdev_work->fdb_info.addr)
+ goto out;
+
+ ether_addr_copy((u8 *)swdev_work->fdb_info.addr,
+ fdb_info->addr);
+ dev_hold(dev);
+
+ break;
+
+ default:
+ kfree(swdev_work);
+ return NOTIFY_DONE;
+ }
+
+ queue_work(swdev_wq, &swdev_work->work);
+ return NOTIFY_DONE;
+out:
+ kfree(swdev_work);
+ return NOTIFY_BAD;
+}
+
+static int
+prestera_port_vlan_bridge_join(struct prestera_port_vlan *port_vlan,
+ struct prestera_bridge_port *br_port)
+{
+ struct prestera_port *port = port_vlan->port;
+ struct prestera_bridge_vlan *br_vlan;
+ u16 vid = port_vlan->vid;
+ int err;
+
+ if (port_vlan->br_port)
+ return 0;
+
+ err = prestera_hw_port_flood_set(port, br_port->flags & BR_FLOOD);
+ if (err)
+ return err;
+
+ err = prestera_hw_port_learning_set(port, br_port->flags & BR_LEARNING);
+ if (err)
+ goto err_port_learning_set;
+
+ err = prestera_port_vid_stp_set(port, vid, br_port->stp_state);
+ if (err)
+ goto err_port_vid_stp_set;
+
+ br_vlan = prestera_bridge_vlan_by_vid(br_port, vid);
+ if (!br_vlan) {
+ br_vlan = prestera_bridge_vlan_create(br_port, vid);
+ if (!br_vlan) {
+ err = -ENOMEM;
+ goto err_bridge_vlan_get;
+ }
+ }
+
+ list_add(&port_vlan->br_vlan_head, &br_vlan->port_vlan_list);
+
+ prestera_bridge_port_get(br_port);
+ port_vlan->br_port = br_port;
+
+ return 0;
+
+err_bridge_vlan_get:
+ prestera_port_vid_stp_set(port, vid, BR_STATE_FORWARDING);
+err_port_vid_stp_set:
+ prestera_hw_port_learning_set(port, false);
+err_port_learning_set:
+ return err;
+}
+
+static int
+prestera_bridge_port_vlan_add(struct prestera_port *port,
+ struct prestera_bridge_port *br_port,
+ u16 vid, bool is_untagged, bool is_pvid,
+ struct netlink_ext_ack *extack)
+{
+ struct prestera_port_vlan *port_vlan;
+ u16 old_pvid = port->pvid;
+ u16 pvid;
+ int err;
+
+ if (is_pvid)
+ pvid = vid;
+ else
+ pvid = port->pvid == vid ? 0 : port->pvid;
+
+ port_vlan = prestera_port_vlan_by_vid(port, vid);
+ if (port_vlan && port_vlan->br_port != br_port)
+ return -EEXIST;
+
+ if (!port_vlan) {
+ port_vlan = prestera_port_vlan_create(port, vid, is_untagged);
+ if (IS_ERR(port_vlan))
+ return PTR_ERR(port_vlan);
+ } else {
+ err = prestera_hw_vlan_port_set(port, vid, true, is_untagged);
+ if (err)
+ goto err_port_vlan_set;
+ }
+
+ err = prestera_port_pvid_set(port, pvid);
+ if (err)
+ goto err_port_pvid_set;
+
+ err = prestera_port_vlan_bridge_join(port_vlan, br_port);
+ if (err)
+ goto err_port_vlan_bridge_join;
+
+ return 0;
+
+err_port_vlan_bridge_join:
+ prestera_port_pvid_set(port, old_pvid);
+err_port_pvid_set:
+ prestera_hw_vlan_port_set(port, vid, false, false);
+err_port_vlan_set:
+ prestera_port_vlan_destroy(port_vlan);
+
+ return err;
+}
+
+static void
+prestera_bridge_port_vlan_del(struct prestera_port *port,
+ struct prestera_bridge_port *br_port, u16 vid)
+{
+ u16 pvid = port->pvid == vid ? 0 : port->pvid;
+ struct prestera_port_vlan *port_vlan;
+
+ port_vlan = prestera_port_vlan_by_vid(port, vid);
+ if (WARN_ON(!port_vlan))
+ return;
+
+ prestera_port_vlan_bridge_leave(port_vlan);
+ prestera_port_pvid_set(port, pvid);
+ prestera_port_vlan_destroy(port_vlan);
+}
+
+static int prestera_port_vlans_add(struct prestera_port *port,
+ const struct switchdev_obj_port_vlan *vlan,
+ struct switchdev_trans *trans,
+ struct netlink_ext_ack *extack)
+{
+ bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
+ bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
+ struct net_device *dev = vlan->obj.orig_dev;
+ struct prestera_bridge_port *br_port;
+ struct prestera_switch *sw = port->sw;
+ struct prestera_bridge *bridge;
+ u16 vid;
+
+ if (netif_is_bridge_master(dev))
+ return 0;
+
+ if (switchdev_trans_ph_commit(trans))
+ return 0;
+
+ br_port = prestera_bridge_port_by_dev(sw->swdev, dev);
+ if (WARN_ON(!br_port))
+ return -EINVAL;
+
+ bridge = br_port->bridge;
+ if (!bridge->vlan_enabled)
+ return 0;
+
+ for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
+ int err;
+
+ err = prestera_bridge_port_vlan_add(port, br_port,
+ vid, flag_untagged,
+ flag_pvid, extack);
+ if (err)
+ return err;
+ }
+
+ return 0;
+}
+
+static int prestera_port_obj_add(struct net_device *dev,
+ const struct switchdev_obj *obj,
+ struct switchdev_trans *trans,
+ struct netlink_ext_ack *extack)
+{
+ struct prestera_port *port = netdev_priv(dev);
+ const struct switchdev_obj_port_vlan *vlan;
+ int err = 0;
+
+ switch (obj->id) {
+ case SWITCHDEV_OBJ_ID_PORT_VLAN:
+ vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
+ err = prestera_port_vlans_add(port, vlan, trans, extack);
+ break;
+ default:
+ err = -EOPNOTSUPP;
+ }
+
+ return err;
+}
+
+static int prestera_port_vlans_del(struct prestera_port *port,
+ const struct switchdev_obj_port_vlan *vlan)
+{
+ struct net_device *dev = vlan->obj.orig_dev;
+ struct prestera_bridge_port *br_port;
+ struct prestera_switch *sw = port->sw;
+ u16 vid;
+
+ if (netif_is_bridge_master(dev))
+ return -EOPNOTSUPP;
+
+ br_port = prestera_bridge_port_by_dev(sw->swdev, dev);
+ if (WARN_ON(!br_port))
+ return -EINVAL;
+
+ if (!br_port->bridge->vlan_enabled)
+ return 0;
+
+ for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
+ prestera_bridge_port_vlan_del(port, br_port, vid);
+
+ return 0;
+}
+
+static int prestera_port_obj_del(struct net_device *dev,
+ const struct switchdev_obj *obj)
+{
+ struct prestera_port *port = netdev_priv(dev);
+ int err = 0;
+
+ switch (obj->id) {
+ case SWITCHDEV_OBJ_ID_PORT_VLAN:
+ err = prestera_port_vlans_del(port, SWITCHDEV_OBJ_PORT_VLAN(obj));
+ break;
+ default:
+ err = -EOPNOTSUPP;
+ break;
+ }
+
+ return err;
+}
+
+static int prestera_switchdev_blk_event(struct notifier_block *unused,
+ unsigned long event, void *ptr)
+{
+ struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
+ int err = 0;
+
+ switch (event) {
+ case SWITCHDEV_PORT_OBJ_ADD:
+ err = switchdev_handle_port_obj_add(dev, ptr,
+ prestera_netdev_check,
+ prestera_port_obj_add);
+ break;
+ case SWITCHDEV_PORT_OBJ_DEL:
+ err = switchdev_handle_port_obj_del(dev, ptr,
+ prestera_netdev_check,
+ prestera_port_obj_del);
+ break;
+ case SWITCHDEV_PORT_ATTR_SET:
+ err = switchdev_handle_port_attr_set(dev, ptr,
+ prestera_netdev_check,
+ prestera_port_obj_attr_set);
+ break;
+ default:
+ err = -EOPNOTSUPP;
+ }
+
+ return notifier_from_errno(err);
+}
+
+static void prestera_fdb_event(struct prestera_switch *sw,
+ struct prestera_event *evt, void *arg)
+{
+ struct switchdev_notifier_fdb_info info;
+ struct prestera_port *port;
+
+ port = prestera_find_port(sw, evt->fdb_evt.port_id);
+ if (!port)
+ return;
+
+ info.addr = evt->fdb_evt.data.mac;
+ info.vid = evt->fdb_evt.vid;
+ info.offloaded = true;
+
+ rtnl_lock();
+
+ switch (evt->id) {
+ case PRESTERA_FDB_EVENT_LEARNED:
+ call_switchdev_notifiers(SWITCHDEV_FDB_ADD_TO_BRIDGE,
+ port->dev, &info.info, NULL);
+ break;
+ case PRESTERA_FDB_EVENT_AGED:
+ call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE,
+ port->dev, &info.info, NULL);
+ break;
+ }
+
+ rtnl_unlock();
+}
+
+static int prestera_fdb_init(struct prestera_switch *sw)
+{
+ int err;
+
+ err = prestera_hw_event_handler_register(sw, PRESTERA_EVENT_TYPE_FDB,
+ prestera_fdb_event, NULL);
+ if (err)
+ return err;
+
+ err = prestera_hw_switch_ageing_set(sw, PRESTERA_DEFAULT_AGEING_TIME_MS);
+ if (err)
+ goto err_ageing_set;
+
+ return 0;
+
+err_ageing_set:
+ prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_FDB,
+ prestera_fdb_event);
+ return err;
+}
+
+static void prestera_fdb_fini(struct prestera_switch *sw)
+{
+ prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_FDB,
+ prestera_fdb_event);
+}
+
+static int prestera_switchdev_handler_init(struct prestera_switchdev *swdev)
+{
+ int err;
+
+ swdev->swdev_nb.notifier_call = prestera_switchdev_event;
+ err = register_switchdev_notifier(&swdev->swdev_nb);
+ if (err)
+ goto err_register_swdev_notifier;
+
+ swdev->swdev_nb_blk.notifier_call = prestera_switchdev_blk_event;
+ err = register_switchdev_blocking_notifier(&swdev->swdev_nb_blk);
+ if (err)
+ goto err_register_blk_swdev_notifier;
+
+ return 0;
+
+err_register_blk_swdev_notifier:
+ unregister_switchdev_notifier(&swdev->swdev_nb);
+err_register_swdev_notifier:
+ destroy_workqueue(swdev_wq);
+ return err;
+}
+
+static void prestera_switchdev_handler_fini(struct prestera_switchdev *swdev)
+{
+ unregister_switchdev_blocking_notifier(&swdev->swdev_nb_blk);
+ unregister_switchdev_notifier(&swdev->swdev_nb);
+}
+
+int prestera_switchdev_init(struct prestera_switch *sw)
+{
+ struct prestera_switchdev *swdev;
+ int err;
+
+ swdev = kzalloc(sizeof(*swdev), GFP_KERNEL);
+ if (!swdev)
+ return -ENOMEM;
+
+ sw->swdev = swdev;
+ swdev->sw = sw;
+
+ INIT_LIST_HEAD(&swdev->bridge_list);
+
+ swdev_wq = alloc_ordered_workqueue("%s_ordered", 0, "prestera_br");
+ if (!swdev_wq) {
+ err = -ENOMEM;
+ goto err_alloc_wq;
+ }
+
+ err = prestera_switchdev_handler_init(swdev);
+ if (err)
+ goto err_swdev_init;
+
+ err = prestera_fdb_init(sw);
+ if (err)
+ goto err_fdb_init;
+
+ return 0;
+
+err_fdb_init:
+err_swdev_init:
+err_alloc_wq:
+ kfree(swdev);
+
+ return err;
+}
+
+void prestera_switchdev_fini(struct prestera_switch *sw)
+{
+ struct prestera_switchdev *swdev = sw->swdev;
+
+ prestera_fdb_fini(sw);
+ prestera_switchdev_handler_fini(swdev);
+ destroy_workqueue(swdev_wq);
+ kfree(swdev);
+}
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_switchdev.h b/drivers/net/ethernet/marvell/prestera/prestera_switchdev.h
new file mode 100644
index 000000000000..7b7aa2c12d9f
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/prestera_switchdev.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+ *
+ * Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved.
+ *
+ */
+
+#ifndef _PRESTERA_SWITCHDEV_H_
+#define _PRESTERA_SWITCHDEV_H_
+
+int prestera_switchdev_init(struct prestera_switch *sw);
+void prestera_switchdev_fini(struct prestera_switch *sw);
+
+int prestera_bridge_port_event(struct net_device *dev, unsigned long event,
+ void *ptr);
+
+#endif /* _PRESTERA_SWITCHDEV_H_ */
--
2.17.1
Add brief description how to configure base mac address binding in
device-tree.
Describe requirement for the PCI port which is connected to the ASIC, to
allow access to the firmware related registers.
Signed-off-by: Vadym Kochan <[email protected]>
---
.../bindings/net/marvell,prestera.txt | 34 +++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/Documentation/devicetree/bindings/net/marvell,prestera.txt b/Documentation/devicetree/bindings/net/marvell,prestera.txt
index 83370ebf5b89..e28938ddfdf5 100644
--- a/Documentation/devicetree/bindings/net/marvell,prestera.txt
+++ b/Documentation/devicetree/bindings/net/marvell,prestera.txt
@@ -45,3 +45,37 @@ dfx-server {
ranges = <0 MBUS_ID(0x08, 0x00) 0 0x100000>;
reg = <MBUS_ID(0x08, 0x00) 0 0x100000>;
};
+
+Marvell Prestera SwitchDev bindings
+-----------------------------------
+Optional properties:
+- compatible: must be "marvell,prestera"
+- base-mac-provider: describes handle to node which provides base mac address,
+ might be a static base mac address or nvme cell provider.
+
+Example:
+
+eeprom_mac_addr: eeprom-mac-addr {
+ compatible = "eeprom,mac-addr-cell";
+ status = "okay";
+
+ nvmem = <&eeprom_at24>;
+};
+
+prestera {
+ compatible = "marvell,prestera";
+ status = "okay";
+
+ base-mac-provider = <&eeprom_mac_addr>;
+};
+
+The current implementation of Prestera Switchdev PCI interface driver requires
+that BAR2 is assigned to 0xf6000000 as base address from the PCI IO range:
+
+&cp0_pcie0 {
+ ranges = <0x81000000 0x0 0xfb000000 0x0 0xfb000000 0x0 0xf0000
+ 0x82000000 0x0 0xf6000000 0x0 0xf6000000 0x0 0x2000000
+ 0x82000000 0x0 0xf9000000 0x0 0xf9000000 0x0 0x100000>;
+ phys = <&cp0_comphy0 0>;
+ status = "okay";
+};
--
2.17.1
The ethtool API provides support for the configuration of the following
features: speed and duplex, auto-negotiation, MDI-x, forward error
correction, port media type. The API also provides information about the
port status, hardware and software statistic. The following limitation
exists:
- port media type should be configured before speed setting
- ethtool -m option is not supported
- ethtool -p option is not supported
- ethtool -r option is supported for RJ45 port only
- the following combination of parameters is not supported:
ethtool -s sw1pX port XX autoneg on
- forward error correction feature is supported only on SFP ports, 10G
speed
- auto-negotiation and MDI-x features are not supported on
Copper-to-Fiber SFP module
Signed-off-by: Andrii Savka <[email protected]>
Signed-off-by: Serhiy Boiko <[email protected]>
Signed-off-by: Vadym Kochan <[email protected]>
---
.../net/ethernet/marvell/prestera/Makefile | 2 +-
.../net/ethernet/marvell/prestera/prestera.h | 3 +
.../marvell/prestera/prestera_ethtool.c | 737 ++++++++++++++++++
.../marvell/prestera/prestera_ethtool.h | 37 +
.../ethernet/marvell/prestera/prestera_hw.c | 293 +++++++
.../ethernet/marvell/prestera/prestera_hw.h | 65 +-
.../ethernet/marvell/prestera/prestera_main.c | 42 +-
7 files changed, 1171 insertions(+), 8 deletions(-)
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_ethtool.c
create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_ethtool.h
diff --git a/drivers/net/ethernet/marvell/prestera/Makefile b/drivers/net/ethernet/marvell/prestera/Makefile
index babd71fba809..7684e7047562 100644
--- a/drivers/net/ethernet/marvell/prestera/Makefile
+++ b/drivers/net/ethernet/marvell/prestera/Makefile
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_PRESTERA) += prestera.o
prestera-objs := prestera_main.o prestera_hw.o prestera_dsa.o \
- prestera_rxtx.o prestera_devlink.o
+ prestera_rxtx.o prestera_devlink.o prestera_ethtool.o
obj-$(CONFIG_PRESTERA_PCI) += prestera_pci.o
diff --git a/drivers/net/ethernet/marvell/prestera/prestera.h b/drivers/net/ethernet/marvell/prestera/prestera.h
index f8abaaff5f21..ee834e824521 100644
--- a/drivers/net/ethernet/marvell/prestera/prestera.h
+++ b/drivers/net/ethernet/marvell/prestera/prestera.h
@@ -173,4 +173,7 @@ void prestera_device_unregister(struct prestera_device *dev);
struct prestera_port *prestera_port_find_by_hwid(struct prestera_switch *sw,
u32 dev_id, u32 hw_id);
+int prestera_port_autoneg_set(struct prestera_port *port, bool enable,
+ u64 adver_link_modes, u8 adver_fec);
+
#endif /* _PRESTERA_H_ */
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_ethtool.c b/drivers/net/ethernet/marvell/prestera/prestera_ethtool.c
new file mode 100644
index 000000000000..3351ecfc8eea
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/prestera_ethtool.c
@@ -0,0 +1,737 @@
+// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+/* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved */
+
+#include <linux/kernel.h>
+#include <linux/netdevice.h>
+#include <linux/ethtool.h>
+
+#include "prestera.h"
+#include "prestera_hw.h"
+#include "prestera_ethtool.h"
+
+#define PRESTERA_STATS_CNT \
+ (sizeof(struct prestera_port_stats) / sizeof(u64))
+#define PRESTERA_STATS_IDX(name) \
+ (offsetof(struct prestera_port_stats, name) / sizeof(u64))
+#define PRESTERA_STATS_FIELD(name) \
+ [PRESTERA_STATS_IDX(name)] = __stringify(name)
+
+static const char driver_kind[] = "prestera";
+
+static const struct prestera_link_mode {
+ enum ethtool_link_mode_bit_indices eth_mode;
+ u32 speed;
+ u64 pr_mask;
+ u8 duplex;
+ u8 port_type;
+} port_link_modes[PRESTERA_LINK_MODE_MAX] = {
+ [PRESTERA_LINK_MODE_10baseT_Half] = {
+ .eth_mode = ETHTOOL_LINK_MODE_10baseT_Half_BIT,
+ .speed = 10,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_10baseT_Half,
+ .duplex = PRESTERA_PORT_DUPLEX_HALF,
+ .port_type = PRESTERA_PORT_TYPE_TP,
+ },
+ [PRESTERA_LINK_MODE_10baseT_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_10baseT_Full_BIT,
+ .speed = 10,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_10baseT_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_TP,
+ },
+ [PRESTERA_LINK_MODE_100baseT_Half] = {
+ .eth_mode = ETHTOOL_LINK_MODE_100baseT_Half_BIT,
+ .speed = 100,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_100baseT_Half,
+ .duplex = PRESTERA_PORT_DUPLEX_HALF,
+ .port_type = PRESTERA_PORT_TYPE_TP,
+ },
+ [PRESTERA_LINK_MODE_100baseT_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_100baseT_Full_BIT,
+ .speed = 100,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_100baseT_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_TP,
+ },
+ [PRESTERA_LINK_MODE_1000baseT_Half] = {
+ .eth_mode = ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
+ .speed = 1000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_1000baseT_Half,
+ .duplex = PRESTERA_PORT_DUPLEX_HALF,
+ .port_type = PRESTERA_PORT_TYPE_TP,
+ },
+ [PRESTERA_LINK_MODE_1000baseT_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
+ .speed = 1000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_1000baseT_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_TP,
+ },
+ [PRESTERA_LINK_MODE_1000baseX_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
+ .speed = 1000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_1000baseX_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_FIBRE,
+ },
+ [PRESTERA_LINK_MODE_1000baseKX_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
+ .speed = 1000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_1000baseKX_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_TP,
+ },
+ [PRESTERA_LINK_MODE_2500baseX_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_2500baseX_Full_BIT,
+ .speed = 2500,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_2500baseX_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ },
+ [PRESTERA_LINK_MODE_10GbaseKR_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
+ .speed = 10000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_10GbaseKR_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_TP,
+ },
+ [PRESTERA_LINK_MODE_10GbaseSR_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_10000baseSR_Full_BIT,
+ .speed = 10000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_10GbaseSR_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_FIBRE,
+ },
+ [PRESTERA_LINK_MODE_10GbaseLR_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_10000baseLR_Full_BIT,
+ .speed = 10000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_10GbaseLR_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_FIBRE,
+ },
+ [PRESTERA_LINK_MODE_20GbaseKR2_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT,
+ .speed = 20000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_20GbaseKR2_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_TP,
+ },
+ [PRESTERA_LINK_MODE_25GbaseCR_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
+ .speed = 25000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_25GbaseCR_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_DA,
+ },
+ [PRESTERA_LINK_MODE_25GbaseKR_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
+ .speed = 25000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_25GbaseKR_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_TP,
+ },
+ [PRESTERA_LINK_MODE_25GbaseSR_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
+ .speed = 25000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_25GbaseSR_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_FIBRE,
+ },
+ [PRESTERA_LINK_MODE_40GbaseKR4_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT,
+ .speed = 40000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_40GbaseKR4_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_TP,
+ },
+ [PRESTERA_LINK_MODE_40GbaseCR4_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT,
+ .speed = 40000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_40GbaseCR4_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_DA,
+ },
+ [PRESTERA_LINK_MODE_40GbaseSR4_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT,
+ .speed = 40000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_40GbaseSR4_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_FIBRE,
+ },
+ [PRESTERA_LINK_MODE_50GbaseCR2_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT,
+ .speed = 50000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_50GbaseCR2_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_DA,
+ },
+ [PRESTERA_LINK_MODE_50GbaseKR2_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT,
+ .speed = 50000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_50GbaseKR2_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_TP,
+ },
+ [PRESTERA_LINK_MODE_50GbaseSR2_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT,
+ .speed = 50000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_50GbaseSR2_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_FIBRE,
+ },
+ [PRESTERA_LINK_MODE_100GbaseKR4_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
+ .speed = 100000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_100GbaseKR4_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_TP,
+ },
+ [PRESTERA_LINK_MODE_100GbaseSR4_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT,
+ .speed = 100000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_100GbaseSR4_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_FIBRE,
+ },
+ [PRESTERA_LINK_MODE_100GbaseCR4_Full] = {
+ .eth_mode = ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
+ .speed = 100000,
+ .pr_mask = 1 << PRESTERA_LINK_MODE_100GbaseCR4_Full,
+ .duplex = PRESTERA_PORT_DUPLEX_FULL,
+ .port_type = PRESTERA_PORT_TYPE_DA,
+ }
+};
+
+static const struct prestera_fec {
+ u32 eth_fec;
+ enum ethtool_link_mode_bit_indices eth_mode;
+ u8 pr_fec;
+} port_fec_caps[PRESTERA_PORT_FEC_MAX] = {
+ [PRESTERA_PORT_FEC_OFF] = {
+ .eth_fec = ETHTOOL_FEC_OFF,
+ .eth_mode = ETHTOOL_LINK_MODE_FEC_NONE_BIT,
+ .pr_fec = 1 << PRESTERA_PORT_FEC_OFF,
+ },
+ [PRESTERA_PORT_FEC_BASER] = {
+ .eth_fec = ETHTOOL_FEC_BASER,
+ .eth_mode = ETHTOOL_LINK_MODE_FEC_BASER_BIT,
+ .pr_fec = 1 << PRESTERA_PORT_FEC_BASER,
+ },
+ [PRESTERA_PORT_FEC_RS] = {
+ .eth_fec = ETHTOOL_FEC_RS,
+ .eth_mode = ETHTOOL_LINK_MODE_FEC_RS_BIT,
+ .pr_fec = 1 << PRESTERA_PORT_FEC_RS,
+ }
+};
+
+static const struct prestera_port_type {
+ enum ethtool_link_mode_bit_indices eth_mode;
+ u8 eth_type;
+} port_types[PRESTERA_PORT_TYPE_MAX] = {
+ [PRESTERA_PORT_TYPE_NONE] = {
+ .eth_mode = __ETHTOOL_LINK_MODE_MASK_NBITS,
+ .eth_type = PORT_NONE,
+ },
+ [PRESTERA_PORT_TYPE_TP] = {
+ .eth_mode = ETHTOOL_LINK_MODE_TP_BIT,
+ .eth_type = PORT_TP,
+ },
+ [PRESTERA_PORT_TYPE_AUI] = {
+ .eth_mode = ETHTOOL_LINK_MODE_AUI_BIT,
+ .eth_type = PORT_AUI,
+ },
+ [PRESTERA_PORT_TYPE_MII] = {
+ .eth_mode = ETHTOOL_LINK_MODE_MII_BIT,
+ .eth_type = PORT_MII,
+ },
+ [PRESTERA_PORT_TYPE_FIBRE] = {
+ .eth_mode = ETHTOOL_LINK_MODE_FIBRE_BIT,
+ .eth_type = PORT_FIBRE,
+ },
+ [PRESTERA_PORT_TYPE_BNC] = {
+ .eth_mode = ETHTOOL_LINK_MODE_BNC_BIT,
+ .eth_type = PORT_BNC,
+ },
+ [PRESTERA_PORT_TYPE_DA] = {
+ .eth_mode = ETHTOOL_LINK_MODE_TP_BIT,
+ .eth_type = PORT_TP,
+ },
+ [PRESTERA_PORT_TYPE_OTHER] = {
+ .eth_mode = __ETHTOOL_LINK_MODE_MASK_NBITS,
+ .eth_type = PORT_OTHER,
+ }
+};
+
+static const char prestera_cnt_name[PRESTERA_STATS_CNT][ETH_GSTRING_LEN] = {
+ PRESTERA_STATS_FIELD(good_octets_received),
+ PRESTERA_STATS_FIELD(bad_octets_received),
+ PRESTERA_STATS_FIELD(mac_trans_error),
+ PRESTERA_STATS_FIELD(broadcast_frames_received),
+ PRESTERA_STATS_FIELD(multicast_frames_received),
+ PRESTERA_STATS_FIELD(frames_64_octets),
+ PRESTERA_STATS_FIELD(frames_65_to_127_octets),
+ PRESTERA_STATS_FIELD(frames_128_to_255_octets),
+ PRESTERA_STATS_FIELD(frames_256_to_511_octets),
+ PRESTERA_STATS_FIELD(frames_512_to_1023_octets),
+ PRESTERA_STATS_FIELD(frames_1024_to_max_octets),
+ PRESTERA_STATS_FIELD(excessive_collision),
+ PRESTERA_STATS_FIELD(multicast_frames_sent),
+ PRESTERA_STATS_FIELD(broadcast_frames_sent),
+ PRESTERA_STATS_FIELD(fc_sent),
+ PRESTERA_STATS_FIELD(fc_received),
+ PRESTERA_STATS_FIELD(buffer_overrun),
+ PRESTERA_STATS_FIELD(undersize),
+ PRESTERA_STATS_FIELD(fragments),
+ PRESTERA_STATS_FIELD(oversize),
+ PRESTERA_STATS_FIELD(jabber),
+ PRESTERA_STATS_FIELD(rx_error_frame_received),
+ PRESTERA_STATS_FIELD(bad_crc),
+ PRESTERA_STATS_FIELD(collisions),
+ PRESTERA_STATS_FIELD(late_collision),
+ PRESTERA_STATS_FIELD(unicast_frames_received),
+ PRESTERA_STATS_FIELD(unicast_frames_sent),
+ PRESTERA_STATS_FIELD(sent_multiple),
+ PRESTERA_STATS_FIELD(sent_deferred),
+ PRESTERA_STATS_FIELD(good_octets_sent),
+};
+
+void prestera_ethtool_get_drvinfo(struct net_device *dev,
+ struct ethtool_drvinfo *drvinfo)
+{
+ struct prestera_port *port = netdev_priv(dev);
+ struct prestera_switch *sw = port->sw;
+
+ strlcpy(drvinfo->driver, driver_kind, sizeof(drvinfo->driver));
+ strlcpy(drvinfo->bus_info, dev_name(prestera_dev(sw)),
+ sizeof(drvinfo->bus_info));
+ snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
+ "%d.%d.%d",
+ sw->dev->fw_rev.maj,
+ sw->dev->fw_rev.min,
+ sw->dev->fw_rev.sub);
+}
+
+static u8 prestera_port_type_get(struct prestera_port *port)
+{
+ if (port->caps.type < PRESTERA_PORT_TYPE_MAX)
+ return port_types[port->caps.type].eth_type;
+ return PORT_OTHER;
+}
+
+static int prestera_port_type_set(const struct ethtool_link_ksettings *ecmd,
+ struct prestera_port *port)
+{
+ u32 new_mode = PRESTERA_LINK_MODE_MAX;
+ u32 type, mode;
+ int err;
+
+ for (type = 0; type < PRESTERA_PORT_TYPE_MAX; type++) {
+ if (port_types[type].eth_type == ecmd->base.port &&
+ test_bit(port_types[type].eth_mode,
+ ecmd->link_modes.supported)) {
+ break;
+ }
+ }
+
+ if (type == port->caps.type)
+ return 0;
+ if (type != port->caps.type && ecmd->base.autoneg == AUTONEG_ENABLE)
+ return -EINVAL;
+ if (type == PRESTERA_PORT_TYPE_MAX)
+ return -EOPNOTSUPP;
+
+ for (mode = 0; mode < PRESTERA_LINK_MODE_MAX; mode++) {
+ if ((port_link_modes[mode].pr_mask &
+ port->caps.supp_link_modes) &&
+ type == port_link_modes[mode].port_type) {
+ new_mode = mode;
+ }
+ }
+
+ if (new_mode < PRESTERA_LINK_MODE_MAX)
+ err = prestera_hw_port_link_mode_set(port, new_mode);
+ else
+ err = -EINVAL;
+
+ if (!err) {
+ port->caps.type = type;
+ port->autoneg = false;
+ }
+
+ return err;
+}
+
+static void prestera_modes_to_eth(unsigned long *eth_modes, u64 link_modes,
+ u8 fec, u8 type)
+{
+ u32 mode;
+
+ for (mode = 0; mode < PRESTERA_LINK_MODE_MAX; mode++) {
+ if ((port_link_modes[mode].pr_mask & link_modes) == 0)
+ continue;
+ if (type != PRESTERA_PORT_TYPE_NONE &&
+ port_link_modes[mode].port_type != type)
+ continue;
+ __set_bit(port_link_modes[mode].eth_mode, eth_modes);
+ }
+
+ for (mode = 0; mode < PRESTERA_PORT_FEC_MAX; mode++) {
+ if ((port_fec_caps[mode].pr_fec & fec) == 0)
+ continue;
+ __set_bit(port_fec_caps[mode].eth_mode, eth_modes);
+ }
+}
+
+static void prestera_modes_from_eth(const unsigned long *eth_modes,
+ u64 *link_modes, u8 *fec, u8 type)
+{
+ u32 mode;
+
+ for (mode = 0; mode < PRESTERA_LINK_MODE_MAX; mode++) {
+ if (!test_bit(port_link_modes[mode].eth_mode, eth_modes))
+ continue;
+ if (port_link_modes[mode].port_type != type)
+ continue;
+ *link_modes |= port_link_modes[mode].pr_mask;
+ }
+
+ for (mode = 0; mode < PRESTERA_PORT_FEC_MAX; mode++) {
+ if (!test_bit(port_fec_caps[mode].eth_mode, eth_modes))
+ continue;
+ *fec |= port_fec_caps[mode].pr_fec;
+ }
+}
+
+static void prestera_port_supp_types_get(struct ethtool_link_ksettings *ecmd,
+ struct prestera_port *port)
+{
+ u32 mode;
+ u8 ptype;
+
+ for (mode = 0; mode < PRESTERA_LINK_MODE_MAX; mode++) {
+ if ((port_link_modes[mode].pr_mask &
+ port->caps.supp_link_modes) == 0)
+ continue;
+ ptype = port_link_modes[mode].port_type;
+ __set_bit(port_types[ptype].eth_mode,
+ ecmd->link_modes.supported);
+ }
+}
+
+static void prestera_port_remote_cap_get(struct ethtool_link_ksettings *ecmd,
+ struct prestera_port *port)
+{
+ bool asym_pause;
+ bool pause;
+ u64 bitmap;
+
+ if (!prestera_hw_port_remote_cap_get(port, &bitmap)) {
+ prestera_modes_to_eth(ecmd->link_modes.lp_advertising,
+ bitmap, 0, PRESTERA_PORT_TYPE_NONE);
+
+ if (!bitmap_empty(ecmd->link_modes.lp_advertising,
+ __ETHTOOL_LINK_MODE_MASK_NBITS)) {
+ ethtool_link_ksettings_add_link_mode(ecmd,
+ lp_advertising,
+ Autoneg);
+ }
+ }
+
+ if (prestera_hw_port_remote_fc_get(port, &pause, &asym_pause))
+ return;
+ if (pause)
+ ethtool_link_ksettings_add_link_mode(ecmd,
+ lp_advertising,
+ Pause);
+ if (asym_pause)
+ ethtool_link_ksettings_add_link_mode(ecmd,
+ lp_advertising,
+ Asym_Pause);
+}
+
+static void prestera_port_speed_get(struct ethtool_link_ksettings *ecmd,
+ struct prestera_port *port)
+{
+ u32 speed;
+ int err;
+
+ err = prestera_hw_port_speed_get(port, &speed);
+ ecmd->base.speed = !err ? speed : SPEED_UNKNOWN;
+}
+
+static void prestera_port_duplex_get(struct ethtool_link_ksettings *ecmd,
+ struct prestera_port *port)
+{
+ u8 duplex;
+
+ if (!prestera_hw_port_duplex_get(port, &duplex)) {
+ ecmd->base.duplex = duplex == PRESTERA_PORT_DUPLEX_FULL ?
+ DUPLEX_FULL : DUPLEX_HALF;
+ } else {
+ ecmd->base.duplex = DUPLEX_UNKNOWN;
+ }
+}
+
+int prestera_ethtool_get_link_ksettings(struct net_device *dev,
+ struct ethtool_link_ksettings *ecmd)
+{
+ struct prestera_port *port = netdev_priv(dev);
+
+ ethtool_link_ksettings_zero_link_mode(ecmd, supported);
+ ethtool_link_ksettings_zero_link_mode(ecmd, advertising);
+ ethtool_link_ksettings_zero_link_mode(ecmd, lp_advertising);
+
+ ecmd->base.autoneg = port->autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
+
+ if (port->caps.type == PRESTERA_PORT_TYPE_TP) {
+ ethtool_link_ksettings_add_link_mode(ecmd, supported, Autoneg);
+
+ if (netif_running(dev) &&
+ (port->autoneg ||
+ port->caps.transceiver == PRESTERA_PORT_TCVR_COPPER))
+ ethtool_link_ksettings_add_link_mode(ecmd, advertising,
+ Autoneg);
+ }
+
+ prestera_modes_to_eth(ecmd->link_modes.supported,
+ port->caps.supp_link_modes,
+ port->caps.supp_fec,
+ port->caps.type);
+
+ prestera_port_supp_types_get(ecmd, port);
+
+ if (netif_carrier_ok(dev)) {
+ prestera_port_speed_get(ecmd, port);
+ prestera_port_duplex_get(ecmd, port);
+ } else {
+ ecmd->base.speed = SPEED_UNKNOWN;
+ ecmd->base.duplex = DUPLEX_UNKNOWN;
+ }
+
+ ecmd->base.port = prestera_port_type_get(port);
+
+ if (port->autoneg) {
+ if (netif_running(dev))
+ prestera_modes_to_eth(ecmd->link_modes.advertising,
+ port->adver_link_modes,
+ port->adver_fec,
+ port->caps.type);
+
+ if (netif_carrier_ok(dev) &&
+ port->caps.transceiver == PRESTERA_PORT_TCVR_COPPER)
+ prestera_port_remote_cap_get(ecmd, port);
+ }
+
+ if (port->caps.type == PRESTERA_PORT_TYPE_TP &&
+ port->caps.transceiver == PRESTERA_PORT_TCVR_COPPER)
+ prestera_hw_port_mdix_get(port, &ecmd->base.eth_tp_mdix,
+ &ecmd->base.eth_tp_mdix_ctrl);
+
+ return 0;
+}
+
+static int prestera_port_mdix_set(const struct ethtool_link_ksettings *ecmd,
+ struct prestera_port *port)
+{
+ if (ecmd->base.eth_tp_mdix_ctrl != ETH_TP_MDI_INVALID &&
+ port->caps.transceiver == PRESTERA_PORT_TCVR_COPPER &&
+ port->caps.type == PRESTERA_PORT_TYPE_TP)
+ return prestera_hw_port_mdix_set(port,
+ ecmd->base.eth_tp_mdix_ctrl);
+
+ return 0;
+}
+
+static int prestera_port_link_mode_set(struct prestera_port *port,
+ u32 speed, u8 duplex, u8 type)
+{
+ u32 new_mode = PRESTERA_LINK_MODE_MAX;
+ u32 mode;
+
+ for (mode = 0; mode < PRESTERA_LINK_MODE_MAX; mode++) {
+ if (speed != port_link_modes[mode].speed)
+ continue;
+ if (duplex != port_link_modes[mode].duplex)
+ continue;
+ if (!(port_link_modes[mode].pr_mask &
+ port->caps.supp_link_modes))
+ continue;
+ if (type != port_link_modes[mode].port_type)
+ continue;
+
+ new_mode = mode;
+ break;
+ }
+
+ if (new_mode == PRESTERA_LINK_MODE_MAX)
+ return -EOPNOTSUPP;
+
+ return prestera_hw_port_link_mode_set(port, new_mode);
+}
+
+static int
+prestera_port_speed_duplex_set(const struct ethtool_link_ksettings *ecmd,
+ struct prestera_port *port)
+{
+ u32 curr_mode;
+ u8 duplex;
+ u32 speed;
+ int err;
+
+ err = prestera_hw_port_link_mode_get(port, &curr_mode);
+ if (err || curr_mode >= PRESTERA_LINK_MODE_MAX)
+ return -EINVAL;
+
+ if (ecmd->base.duplex != DUPLEX_UNKNOWN)
+ duplex = ecmd->base.duplex == DUPLEX_FULL ?
+ PRESTERA_PORT_DUPLEX_FULL : PRESTERA_PORT_DUPLEX_HALF;
+ else
+ duplex = port_link_modes[curr_mode].duplex;
+
+ if (ecmd->base.speed != SPEED_UNKNOWN)
+ speed = ecmd->base.speed;
+ else
+ speed = port_link_modes[curr_mode].speed;
+
+ return prestera_port_link_mode_set(port, speed, duplex,
+ port->caps.type);
+}
+
+int prestera_ethtool_set_link_ksettings(struct net_device *dev,
+ const struct ethtool_link_ksettings *ecmd)
+{
+ struct prestera_port *port = netdev_priv(dev);
+ u64 adver_modes = 0;
+ u8 adver_fec = 0;
+ int err;
+
+ err = prestera_port_type_set(ecmd, port);
+ if (err)
+ return err;
+
+ if (port->caps.transceiver == PRESTERA_PORT_TCVR_COPPER) {
+ err = prestera_port_mdix_set(ecmd, port);
+ if (err)
+ return err;
+ }
+
+ prestera_modes_from_eth(ecmd->link_modes.advertising, &adver_modes,
+ &adver_fec, port->caps.type);
+
+ err = prestera_port_autoneg_set(port,
+ ecmd->base.autoneg == AUTONEG_ENABLE,
+ adver_modes, adver_fec);
+ if (err)
+ return err;
+
+ if (ecmd->base.autoneg == AUTONEG_DISABLE) {
+ err = prestera_port_speed_duplex_set(ecmd, port);
+ if (err)
+ return err;
+ }
+
+ return 0;
+}
+
+int prestera_ethtool_get_fecparam(struct net_device *dev,
+ struct ethtool_fecparam *fecparam)
+{
+ struct prestera_port *port = netdev_priv(dev);
+ u32 mode;
+ u8 active;
+ int err;
+
+ err = prestera_hw_port_fec_get(port, &active);
+ if (err)
+ return err;
+
+ fecparam->fec = 0;
+ for (mode = 0; mode < PRESTERA_PORT_FEC_MAX; mode++) {
+ if ((port_fec_caps[mode].pr_fec & port->caps.supp_fec) == 0)
+ continue;
+ fecparam->fec |= port_fec_caps[mode].eth_fec;
+ }
+
+ if (active < PRESTERA_PORT_FEC_MAX)
+ fecparam->active_fec = port_fec_caps[active].eth_fec;
+ else
+ fecparam->active_fec = ETHTOOL_FEC_AUTO;
+
+ return 0;
+}
+
+int prestera_ethtool_set_fecparam(struct net_device *dev,
+ struct ethtool_fecparam *fecparam)
+{
+ struct prestera_port *port = netdev_priv(dev);
+ u8 fec, active;
+ u32 mode;
+ int err;
+
+ if (port->autoneg) {
+ netdev_err(dev, "FEC set is not allowed while autoneg is on\n");
+ return -EINVAL;
+ }
+
+ err = prestera_hw_port_fec_get(port, &active);
+ if (err)
+ return err;
+
+ fec = PRESTERA_PORT_FEC_MAX;
+ for (mode = 0; mode < PRESTERA_PORT_FEC_MAX; mode++) {
+ if ((port_fec_caps[mode].eth_fec & fecparam->fec) &&
+ (port_fec_caps[mode].pr_fec & port->caps.supp_fec)) {
+ fec = mode;
+ break;
+ }
+ }
+
+ if (fec == active)
+ return 0;
+
+ if (fec == PRESTERA_PORT_FEC_MAX)
+ return -EOPNOTSUPP;
+
+ return prestera_hw_port_fec_set(port, fec);
+}
+
+int prestera_ethtool_get_sset_count(struct net_device *dev, int sset)
+{
+ switch (sset) {
+ case ETH_SS_STATS:
+ return PRESTERA_STATS_CNT;
+ default:
+ return -EOPNOTSUPP;
+ }
+}
+
+void prestera_ethtool_get_strings(struct net_device *dev,
+ u32 stringset, u8 *data)
+{
+ if (stringset != ETH_SS_STATS)
+ return;
+
+ memcpy(data, prestera_cnt_name, sizeof(prestera_cnt_name));
+}
+
+void prestera_ethtool_get_stats(struct net_device *dev,
+ struct ethtool_stats *stats, u64 *data)
+{
+ struct prestera_port *port = netdev_priv(dev);
+ struct prestera_port_stats *port_stats;
+
+ port_stats = &port->cached_hw_stats.stats;
+
+ memcpy((u8 *)data, port_stats, sizeof(*port_stats));
+}
+
+int prestera_ethtool_nway_reset(struct net_device *dev)
+{
+ struct prestera_port *port = netdev_priv(dev);
+
+ if (netif_running(dev) &&
+ port->caps.transceiver == PRESTERA_PORT_TCVR_COPPER &&
+ port->caps.type == PRESTERA_PORT_TYPE_TP)
+ return prestera_hw_port_autoneg_restart(port);
+
+ return -EINVAL;
+}
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_ethtool.h b/drivers/net/ethernet/marvell/prestera/prestera_ethtool.h
new file mode 100644
index 000000000000..e6a51e9020a4
--- /dev/null
+++ b/drivers/net/ethernet/marvell/prestera/prestera_ethtool.h
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+ *
+ * Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved.
+ *
+ */
+#ifndef __PRESTERA_ETHTOOL_H_
+#define __PRESTERA_ETHTOOL_H_
+
+#include <linux/netdevice.h>
+#include <linux/ethtool.h>
+
+void prestera_ethtool_get_drvinfo(struct net_device *dev,
+ struct ethtool_drvinfo *drvinfo);
+
+int prestera_ethtool_get_link_ksettings(struct net_device *dev,
+ struct ethtool_link_ksettings *ecmd);
+
+int prestera_ethtool_set_link_ksettings(struct net_device *dev,
+ const struct ethtool_link_ksettings *ecmd);
+
+int prestera_ethtool_get_fecparam(struct net_device *dev,
+ struct ethtool_fecparam *fecparam);
+
+int prestera_ethtool_set_fecparam(struct net_device *dev,
+ struct ethtool_fecparam *fecparam);
+
+int prestera_ethtool_get_sset_count(struct net_device *dev, int sset);
+
+void prestera_ethtool_get_strings(struct net_device *dev, u32 stringset,
+ u8 *data);
+
+void prestera_ethtool_get_stats(struct net_device *dev,
+ struct ethtool_stats *stats, u64 *data);
+
+int prestera_ethtool_nway_reset(struct net_device *dev);
+
+#endif /* _PRESTERA_ETHTOOL_H_ */
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_hw.c b/drivers/net/ethernet/marvell/prestera/prestera_hw.c
index 5899a16a493e..c54435b0f786 100644
--- a/drivers/net/ethernet/marvell/prestera/prestera_hw.c
+++ b/drivers/net/ethernet/marvell/prestera/prestera_hw.c
@@ -32,9 +32,18 @@ enum {
PRESTERA_CMD_PORT_ATTR_ADMIN_STATE = 1,
PRESTERA_CMD_PORT_ATTR_MTU = 3,
PRESTERA_CMD_PORT_ATTR_MAC = 4,
+ PRESTERA_CMD_PORT_ATTR_SPEED = 5,
PRESTERA_CMD_PORT_ATTR_CAPABILITY = 9,
+ PRESTERA_CMD_PORT_ATTR_REMOTE_CAPABILITY = 10,
+ PRESTERA_CMD_PORT_ATTR_REMOTE_FC = 11,
+ PRESTERA_CMD_PORT_ATTR_LINK_MODE = 12,
+ PRESTERA_CMD_PORT_ATTR_TYPE = 13,
+ PRESTERA_CMD_PORT_ATTR_FEC = 14,
PRESTERA_CMD_PORT_ATTR_AUTONEG = 15,
+ PRESTERA_CMD_PORT_ATTR_DUPLEX = 16,
PRESTERA_CMD_PORT_ATTR_STATS = 17,
+ PRESTERA_CMD_PORT_ATTR_MDIX = 18,
+ PRESTERA_CMD_PORT_ATTR_AUTONEG_RESTART = 19,
};
enum {
@@ -48,6 +57,13 @@ enum {
PRESTERA_CMD_ACK_MAX
};
+enum {
+ PRESTERA_PORT_TP_NA,
+ PRESTERA_PORT_TP_MDI,
+ PRESTERA_PORT_TP_MDIX,
+ PRESTERA_PORT_TP_AUTO
+};
+
enum {
PRESTERA_PORT_GOOD_OCTETS_RCV_CNT,
PRESTERA_PORT_BAD_OCTETS_RCV_CNT,
@@ -83,6 +99,13 @@ enum {
PRESTERA_PORT_CNT_MAX,
};
+enum {
+ PRESTERA_FC_NONE,
+ PRESTERA_FC_SYMMETRIC,
+ PRESTERA_FC_ASYMMETRIC,
+ PRESTERA_FC_SYMM_ASYMM,
+};
+
struct prestera_fw_event_handler {
struct list_head list;
struct rcu_head rcu;
@@ -138,11 +161,23 @@ struct prestera_msg_port_cap_param {
u8 transceiver;
};
+struct prestera_msg_port_mdix_param {
+ u8 status;
+ u8 admin_mode;
+};
+
union prestera_msg_port_param {
u8 admin_state;
u8 oper_state;
u32 mtu;
u8 mac[ETH_ALEN];
+ u32 speed;
+ u32 link_mode;
+ u8 type;
+ u8 duplex;
+ u8 fec;
+ u8 fc;
+ struct prestera_msg_port_mdix_param mdix;
struct prestera_msg_port_autoneg_param autoneg;
struct prestera_msg_port_cap_param cap;
};
@@ -479,6 +514,232 @@ int prestera_hw_port_cap_get(const struct prestera_port *port,
return err;
}
+int prestera_hw_port_remote_cap_get(const struct prestera_port *port,
+ u64 *link_mode_bitmap)
+{
+ struct prestera_msg_port_attr_resp resp;
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_REMOTE_CAPABILITY,
+ .port = port->hw_id,
+ .dev = port->dev_id
+ };
+ int err;
+
+ err = prestera_cmd_ret(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_GET,
+ &req.cmd, sizeof(req), &resp.ret, sizeof(resp));
+ if (err)
+ return err;
+
+ *link_mode_bitmap = resp.param.cap.link_mode;
+
+ return err;
+}
+
+int prestera_hw_port_remote_fc_get(const struct prestera_port *port,
+ bool *pause, bool *asym_pause)
+{
+ struct prestera_msg_port_attr_resp resp;
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_REMOTE_FC,
+ .port = port->hw_id,
+ .dev = port->dev_id
+ };
+ int err;
+
+ err = prestera_cmd_ret(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_GET,
+ &req.cmd, sizeof(req), &resp.ret, sizeof(resp));
+ if (err)
+ return err;
+
+ switch (resp.param.fc) {
+ case PRESTERA_FC_SYMMETRIC:
+ *pause = true;
+ *asym_pause = false;
+ break;
+ case PRESTERA_FC_ASYMMETRIC:
+ *pause = false;
+ *asym_pause = true;
+ break;
+ case PRESTERA_FC_SYMM_ASYMM:
+ *pause = true;
+ *asym_pause = true;
+ break;
+ default:
+ *pause = false;
+ *asym_pause = false;
+ }
+
+ return err;
+}
+
+int prestera_hw_port_type_get(const struct prestera_port *port, u8 *type)
+{
+ struct prestera_msg_port_attr_resp resp;
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_TYPE,
+ .port = port->hw_id,
+ .dev = port->dev_id
+ };
+ int err;
+
+ err = prestera_cmd_ret(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_GET,
+ &req.cmd, sizeof(req), &resp.ret, sizeof(resp));
+ if (err)
+ return err;
+
+ *type = resp.param.type;
+
+ return err;
+}
+
+int prestera_hw_port_fec_get(const struct prestera_port *port, u8 *fec)
+{
+ struct prestera_msg_port_attr_resp resp;
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_FEC,
+ .port = port->hw_id,
+ .dev = port->dev_id
+ };
+ int err;
+
+ err = prestera_cmd_ret(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_GET,
+ &req.cmd, sizeof(req), &resp.ret, sizeof(resp));
+ if (err)
+ return err;
+
+ *fec = resp.param.fec;
+
+ return err;
+}
+
+int prestera_hw_port_fec_set(const struct prestera_port *port, u8 fec)
+{
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_FEC,
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ .param = {.fec = fec}
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_SET,
+ &req.cmd, sizeof(req));
+}
+
+static u8 prestera_hw_mdix_to_eth(u8 mode)
+{
+ switch (mode) {
+ case PRESTERA_PORT_TP_MDI:
+ return ETH_TP_MDI;
+ case PRESTERA_PORT_TP_MDIX:
+ return ETH_TP_MDI_X;
+ case PRESTERA_PORT_TP_AUTO:
+ return ETH_TP_MDI_AUTO;
+ }
+
+ return ETH_TP_MDI_INVALID;
+}
+
+static u8 prestera_hw_mdix_from_eth(u8 mode)
+{
+ switch (mode) {
+ case ETH_TP_MDI:
+ return PRESTERA_PORT_TP_MDI;
+ case ETH_TP_MDI_X:
+ return PRESTERA_PORT_TP_MDIX;
+ case ETH_TP_MDI_AUTO:
+ return PRESTERA_PORT_TP_AUTO;
+ }
+
+ return PRESTERA_PORT_TP_NA;
+}
+
+int prestera_hw_port_mdix_get(const struct prestera_port *port, u8 *status,
+ u8 *admin_mode)
+{
+ struct prestera_msg_port_attr_resp resp;
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_MDIX,
+ .port = port->hw_id,
+ .dev = port->dev_id
+ };
+ int err;
+
+ err = prestera_cmd_ret(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_GET,
+ &req.cmd, sizeof(req), &resp.ret, sizeof(resp));
+ if (err)
+ return err;
+
+ *status = prestera_hw_mdix_to_eth(resp.param.mdix.status);
+ *admin_mode = prestera_hw_mdix_to_eth(resp.param.mdix.admin_mode);
+
+ return 0;
+}
+
+int prestera_hw_port_mdix_set(const struct prestera_port *port, u8 mode)
+{
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_MDIX,
+ .port = port->hw_id,
+ .dev = port->dev_id
+ };
+
+ req.param.mdix.admin_mode = prestera_hw_mdix_from_eth(mode);
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_SET,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_port_link_mode_set(const struct prestera_port *port, u32 mode)
+{
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_LINK_MODE,
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ .param = {.link_mode = mode}
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_SET,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_port_link_mode_get(const struct prestera_port *port, u32 *mode)
+{
+ struct prestera_msg_port_attr_resp resp;
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_LINK_MODE,
+ .port = port->hw_id,
+ .dev = port->dev_id
+ };
+ int err;
+
+ err = prestera_cmd_ret(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_GET,
+ &req.cmd, sizeof(req), &resp.ret, sizeof(resp));
+
+ *mode = resp.param.link_mode;
+
+ return err;
+}
+
+int prestera_hw_port_speed_get(const struct prestera_port *port, u32 *speed)
+{
+ struct prestera_msg_port_attr_resp resp;
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_SPEED,
+ .port = port->hw_id,
+ .dev = port->dev_id
+ };
+ int err;
+
+ err = prestera_cmd_ret(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_GET,
+ &req.cmd, sizeof(req), &resp.ret, sizeof(resp));
+ if (err)
+ return err;
+
+ *speed = resp.param.speed;
+
+ return err;
+}
+
int prestera_hw_port_autoneg_set(const struct prestera_port *port,
bool autoneg, u64 link_modes, u8 fec)
{
@@ -496,6 +757,38 @@ int prestera_hw_port_autoneg_set(const struct prestera_port *port,
&req.cmd, sizeof(req));
}
+int prestera_hw_port_autoneg_restart(struct prestera_port *port)
+{
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_AUTONEG_RESTART,
+ .port = port->hw_id,
+ .dev = port->dev_id,
+ };
+
+ return prestera_cmd(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_SET,
+ &req.cmd, sizeof(req));
+}
+
+int prestera_hw_port_duplex_get(const struct prestera_port *port, u8 *duplex)
+{
+ struct prestera_msg_port_attr_resp resp;
+ struct prestera_msg_port_attr_req req = {
+ .attr = PRESTERA_CMD_PORT_ATTR_DUPLEX,
+ .port = port->hw_id,
+ .dev = port->dev_id
+ };
+ int err;
+
+ err = prestera_cmd_ret(port->sw, PRESTERA_CMD_TYPE_PORT_ATTR_GET,
+ &req.cmd, sizeof(req), &resp.ret, sizeof(resp));
+ if (err)
+ return err;
+
+ *duplex = resp.param.duplex;
+
+ return err;
+}
+
int prestera_hw_port_stats_get(const struct prestera_port *port,
struct prestera_port_stats *st)
{
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_hw.h b/drivers/net/ethernet/marvell/prestera/prestera_hw.h
index 7f2e9c5f78dd..bcb04848894b 100644
--- a/drivers/net/ethernet/marvell/prestera/prestera_hw.h
+++ b/drivers/net/ethernet/marvell/prestera/prestera_hw.h
@@ -9,19 +9,65 @@
#include <linux/types.h>
+enum {
+ PRESTERA_LINK_MODE_10baseT_Half,
+ PRESTERA_LINK_MODE_10baseT_Full,
+ PRESTERA_LINK_MODE_100baseT_Half,
+ PRESTERA_LINK_MODE_100baseT_Full,
+ PRESTERA_LINK_MODE_1000baseT_Half,
+ PRESTERA_LINK_MODE_1000baseT_Full,
+ PRESTERA_LINK_MODE_1000baseX_Full,
+ PRESTERA_LINK_MODE_1000baseKX_Full,
+ PRESTERA_LINK_MODE_2500baseX_Full,
+ PRESTERA_LINK_MODE_10GbaseKR_Full,
+ PRESTERA_LINK_MODE_10GbaseSR_Full,
+ PRESTERA_LINK_MODE_10GbaseLR_Full,
+ PRESTERA_LINK_MODE_20GbaseKR2_Full,
+ PRESTERA_LINK_MODE_25GbaseCR_Full,
+ PRESTERA_LINK_MODE_25GbaseKR_Full,
+ PRESTERA_LINK_MODE_25GbaseSR_Full,
+ PRESTERA_LINK_MODE_40GbaseKR4_Full,
+ PRESTERA_LINK_MODE_40GbaseCR4_Full,
+ PRESTERA_LINK_MODE_40GbaseSR4_Full,
+ PRESTERA_LINK_MODE_50GbaseCR2_Full,
+ PRESTERA_LINK_MODE_50GbaseKR2_Full,
+ PRESTERA_LINK_MODE_50GbaseSR2_Full,
+ PRESTERA_LINK_MODE_100GbaseKR4_Full,
+ PRESTERA_LINK_MODE_100GbaseSR4_Full,
+ PRESTERA_LINK_MODE_100GbaseCR4_Full,
+ PRESTERA_LINK_MODE_MAX,
+};
+
enum {
PRESTERA_PORT_TYPE_NONE,
PRESTERA_PORT_TYPE_TP,
-
+ PRESTERA_PORT_TYPE_AUI,
+ PRESTERA_PORT_TYPE_MII,
+ PRESTERA_PORT_TYPE_FIBRE,
+ PRESTERA_PORT_TYPE_BNC,
+ PRESTERA_PORT_TYPE_DA,
+ PRESTERA_PORT_TYPE_OTHER,
PRESTERA_PORT_TYPE_MAX,
};
enum {
- PRESTERA_PORT_FEC_OFF,
+ PRESTERA_PORT_TCVR_COPPER,
+ PRESTERA_PORT_TCVR_SFP,
+ PRESTERA_PORT_TCVR_MAX,
+};
+enum {
+ PRESTERA_PORT_FEC_OFF,
+ PRESTERA_PORT_FEC_BASER,
+ PRESTERA_PORT_FEC_RS,
PRESTERA_PORT_FEC_MAX,
};
+enum {
+ PRESTERA_PORT_DUPLEX_HALF,
+ PRESTERA_PORT_DUPLEX_FULL
+};
+
struct prestera_switch;
struct prestera_port;
struct prestera_port_stats;
@@ -50,10 +96,25 @@ int prestera_hw_port_mac_set(const struct prestera_port *port, char *mac);
int prestera_hw_port_mac_get(const struct prestera_port *port, char *mac);
int prestera_hw_port_cap_get(const struct prestera_port *port,
struct prestera_port_caps *caps);
+int prestera_hw_port_remote_cap_get(const struct prestera_port *port,
+ u64 *link_mode_bitmap);
+int prestera_hw_port_remote_fc_get(const struct prestera_port *port,
+ bool *pause, bool *asym_pause);
+int prestera_hw_port_type_get(const struct prestera_port *port, u8 *type);
+int prestera_hw_port_fec_get(const struct prestera_port *port, u8 *fec);
+int prestera_hw_port_fec_set(const struct prestera_port *port, u8 fec);
int prestera_hw_port_autoneg_set(const struct prestera_port *port,
bool autoneg, u64 link_modes, u8 fec);
+int prestera_hw_port_autoneg_restart(struct prestera_port *port);
+int prestera_hw_port_duplex_get(const struct prestera_port *port, u8 *duplex);
int prestera_hw_port_stats_get(const struct prestera_port *port,
struct prestera_port_stats *stats);
+int prestera_hw_port_link_mode_set(const struct prestera_port *port, u32 mode);
+int prestera_hw_port_link_mode_get(const struct prestera_port *port, u32 *mode);
+int prestera_hw_port_mdix_get(const struct prestera_port *port, u8 *status,
+ u8 *admin_mode);
+int prestera_hw_port_mdix_set(const struct prestera_port *port, u8 mode);
+int prestera_hw_port_speed_get(const struct prestera_port *port, u32 *speed);
/* Event handlers */
int prestera_hw_event_handler_register(struct prestera_switch *sw,
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_main.c b/drivers/net/ethernet/marvell/prestera/prestera_main.c
index f24a49bde7c3..25f1310da4cf 100644
--- a/drivers/net/ethernet/marvell/prestera/prestera_main.c
+++ b/drivers/net/ethernet/marvell/prestera/prestera_main.c
@@ -5,6 +5,7 @@
#include <linux/module.h>
#include <linux/list.h>
#include <linux/netdevice.h>
+#include <linux/ethtool.h>
#include <linux/netdev_features.h>
#include <linux/etherdevice.h>
#include <linux/jiffies.h>
@@ -15,6 +16,7 @@
#include "prestera_hw.h"
#include "prestera_rxtx.h"
#include "prestera_devlink.h"
+#include "prestera_ethtool.h"
#define PRESTERA_MTU_DEFAULT 1536
@@ -179,22 +181,38 @@ static const struct net_device_ops netdev_ops = {
.ndo_get_devlink_port = prestera_devlink_get_port,
};
-static int prestera_port_autoneg_set(struct prestera_port *port, bool enable,
- u64 link_modes, u8 fec)
+int prestera_port_autoneg_set(struct prestera_port *port, bool enable,
+ u64 adver_link_modes, u8 adver_fec)
{
bool refresh = false;
+ u64 link_modes;
int err = 0;
+ u8 fec;
if (port->caps.type != PRESTERA_PORT_TYPE_TP)
return enable ? -EINVAL : 0;
- if (port->adver_link_modes != link_modes || port->adver_fec != fec) {
- port->adver_fec = fec ?: BIT(PRESTERA_PORT_FEC_OFF);
+ if (!enable)
+ goto set_autoneg;
+
+ link_modes = port->caps.supp_link_modes & adver_link_modes;
+ fec = port->caps.supp_fec & adver_fec;
+
+ if (!link_modes && !fec)
+ return -EOPNOTSUPP;
+
+ if (link_modes && port->adver_link_modes != link_modes) {
port->adver_link_modes = link_modes;
refresh = true;
}
- if (port->autoneg == enable && !(port->autoneg && refresh))
+ if (fec && port->adver_fec != fec) {
+ port->adver_fec = fec;
+ refresh = true;
+ }
+
+set_autoneg:
+ if (port->autoneg == enable && !refresh)
return 0;
err = prestera_hw_port_autoneg_set(port, enable, port->adver_link_modes,
@@ -206,6 +224,19 @@ static int prestera_port_autoneg_set(struct prestera_port *port, bool enable,
return 0;
}
+static const struct ethtool_ops ethtool_ops = {
+ .get_drvinfo = prestera_ethtool_get_drvinfo,
+ .get_link_ksettings = prestera_ethtool_get_link_ksettings,
+ .set_link_ksettings = prestera_ethtool_set_link_ksettings,
+ .get_fecparam = prestera_ethtool_get_fecparam,
+ .set_fecparam = prestera_ethtool_set_fecparam,
+ .get_sset_count = prestera_ethtool_get_sset_count,
+ .get_strings = prestera_ethtool_get_strings,
+ .get_ethtool_stats = prestera_ethtool_get_stats,
+ .get_link = ethtool_op_get_link,
+ .nway_reset = prestera_ethtool_nway_reset
+};
+
static int prestera_port_create(struct prestera_switch *sw, u32 id)
{
struct prestera_port *port;
@@ -235,6 +266,7 @@ static int prestera_port_create(struct prestera_switch *sw, u32 id)
dev->features |= NETIF_F_NETNS_LOCAL;
dev->netdev_ops = &netdev_ops;
+ dev->ethtool_ops = ðtool_ops;
netif_carrier_off(dev);
--
2.17.1
Sat, Jul 25, 2020 at 05:06:50PM CEST, [email protected] wrote:
>The following features are supported:
>
> - VLAN-aware bridge offloading
> - VLAN-unaware bridge offloading
> - FDB offloading (learning, ageing)
> - Switchport configuration
>
>Currently there are some limitations like:
>
> - Only 1 VLAN-aware bridge instance supported
> - FDB ageing timeout parameter is set globally per device
>
>Signed-off-by: Serhiy Boiko <[email protected]>
>Signed-off-by: Serhiy Pshyk <[email protected]>
>Signed-off-by: Taras Chornyi <[email protected]>
>Signed-off-by: Vadym Kochan <[email protected]>
>---
[...]
>+static void prestera_fdb_event_work(struct work_struct *work)
>+{
>+ struct switchdev_notifier_fdb_info *fdb_info;
>+ struct prestera_fdb_event_work *swdev_work;
>+ struct prestera_port *port;
>+ struct net_device *dev;
>+ int err = 0;
>+
>+ swdev_work = container_of(work, struct prestera_fdb_event_work, work);
>+ dev = swdev_work->dev;
>+
>+ rtnl_lock();
>+
>+ port = prestera_port_dev_lower_find(dev);
>+ if (!port)
>+ goto out;
>+
>+ switch (swdev_work->event) {
>+ case SWITCHDEV_FDB_ADD_TO_DEVICE:
>+ fdb_info = &swdev_work->fdb_info;
>+ if (!fdb_info->added_by_user)
>+ break;
>+
>+ err = prestera_port_fdb_set(port, fdb_info, true);
>+ if (err)
>+ break;
>+
>+ prestera_fdb_offload_notify(port, fdb_info);
>+ break;
>+
>+ case SWITCHDEV_FDB_DEL_TO_DEVICE:
>+ fdb_info = &swdev_work->fdb_info;
>+ prestera_port_fdb_set(port, fdb_info, false);
>+ break;
>+ }
>+
>+out:
>+ rtnl_unlock();
>+
>+ kfree(swdev_work->fdb_info.addr);
>+ kfree(swdev_work);
>+ dev_put(dev);
>+}
>+
>+static int prestera_switchdev_event(struct notifier_block *unused,
>+ unsigned long event, void *ptr)
>+{
>+ struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
>+ struct switchdev_notifier_fdb_info *fdb_info;
>+ struct switchdev_notifier_info *info = ptr;
>+ struct prestera_fdb_event_work *swdev_work;
>+ struct net_device *upper;
>+ int err = 0;
>+
>+ if (event == SWITCHDEV_PORT_ATTR_SET) {
>+ err = switchdev_handle_port_attr_set(dev, ptr,
>+ prestera_netdev_check,
>+ prestera_port_obj_attr_set);
>+ return notifier_from_errno(err);
>+ }
>+
>+ upper = netdev_master_upper_dev_get_rcu(dev);
>+ if (!upper)
>+ return NOTIFY_DONE;
>+
>+ if (!netif_is_bridge_master(upper))
>+ return NOTIFY_DONE;
Okay, you support upper bridge. Of which interface? I believe you should
put prestera_netdev_check(dev) check here and avoid the lookup in the
work. Otherwise any chain of intermediate lower devices would be
supported, which is wrong.
>+
>+ swdev_work = kzalloc(sizeof(*swdev_work), GFP_ATOMIC);
>+ if (!swdev_work)
>+ return NOTIFY_BAD;
>+
>+ swdev_work->event = event;
>+ swdev_work->dev = dev;
>+
>+ switch (event) {
>+ case SWITCHDEV_FDB_ADD_TO_DEVICE:
>+ case SWITCHDEV_FDB_DEL_TO_DEVICE:
>+ fdb_info = container_of(info,
>+ struct switchdev_notifier_fdb_info,
>+ info);
>+
>+ INIT_WORK(&swdev_work->work, prestera_fdb_event_work);
>+ memcpy(&swdev_work->fdb_info, ptr,
>+ sizeof(swdev_work->fdb_info));
>+
>+ swdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
>+ if (!swdev_work->fdb_info.addr)
>+ goto out;
>+
>+ ether_addr_copy((u8 *)swdev_work->fdb_info.addr,
>+ fdb_info->addr);
>+ dev_hold(dev);
>+
>+ break;
>+
>+ default:
>+ kfree(swdev_work);
>+ return NOTIFY_DONE;
>+ }
>+
>+ queue_work(swdev_wq, &swdev_work->work);
>+ return NOTIFY_DONE;
>+out:
>+ kfree(swdev_work);
>+ return NOTIFY_BAD;
>+}
[...]
Sat, Jul 25, 2020 at 05:06:46PM CEST, [email protected] wrote:
>Marvell Prestera 98DX326x integrates up to 24 ports of 1GbE with 8
>ports of 10GbE uplinks or 2 ports of 40Gbps stacking for a largely
>wireless SMB deployment.
>
>The current implementation supports only boards designed for the Marvell
>Switchdev solution and requires special firmware.
>
>The core Prestera switching logic is implemented in prestera_main.c,
>there is an intermediate hw layer between core logic and firmware. It is
>implemented in prestera_hw.c, the purpose of it is to encapsulate hw
>related logic, in future there is a plan to support more devices with
>different HW related configurations.
>
>This patch contains only basic switch initialization and RX/TX support
>over SDMA mechanism.
>
>Currently supported devices have DMA access range <= 32bit and require
>ZONE_DMA to be enabled, for such cases SDMA driver checks if the skb
>allocated in proper range supported by the Prestera device.
>
>Also meanwhile there is no TX interrupt support in current firmware
>version so recycling work is scheduled on each xmit.
>
>Port's mac address is generated from the switch base mac which may be
>provided via device-tree (static one or as nvme cell), or randomly
>generated.
>
>Signed-off-by: Andrii Savka <[email protected]>
>Signed-off-by: Oleksandr Mazur <[email protected]>
>Signed-off-by: Serhiy Boiko <[email protected]>
>Signed-off-by: Serhiy Pshyk <[email protected]>
>Signed-off-by: Taras Chornyi <[email protected]>
>Signed-off-by: Volodymyr Mytnyk <[email protected]>
>Signed-off-by: Vadym Kochan <[email protected]>
>---
[...]
>+static const struct net_device_ops netdev_ops = {
Prefix, please:
prestera_netdev_ops.
>+ .ndo_open = prestera_port_open,
>+ .ndo_stop = prestera_port_close,
>+ .ndo_start_xmit = prestera_port_xmit,
>+ .ndo_change_mtu = prestera_port_change_mtu,
>+ .ndo_get_stats64 = prestera_port_get_stats64,
>+ .ndo_set_mac_address = prestera_port_set_mac_address,
>+};
[...]
Sat, Jul 25, 2020 at 05:06:48PM CEST, [email protected] wrote:
>Add very basic support for devlink interface:
>
> - driver name
> - fw version
> - devlink ports
>
>Signed-off-by: Vadym Kochan <[email protected]>
Reviewed-by: Jiri Pirko <[email protected]>
Sat, Jul 25, 2020 at 05:06:49PM CEST, [email protected] wrote:
>The ethtool API provides support for the configuration of the following
>features: speed and duplex, auto-negotiation, MDI-x, forward error
>correction, port media type. The API also provides information about the
>port status, hardware and software statistic. The following limitation
>exists:
>
> - port media type should be configured before speed setting
> - ethtool -m option is not supported
> - ethtool -p option is not supported
> - ethtool -r option is supported for RJ45 port only
> - the following combination of parameters is not supported:
>
> ethtool -s sw1pX port XX autoneg on
>
> - forward error correction feature is supported only on SFP ports, 10G
> speed
>
> - auto-negotiation and MDI-x features are not supported on
> Copper-to-Fiber SFP module
>
>Signed-off-by: Andrii Savka <[email protected]>
>Signed-off-by: Serhiy Boiko <[email protected]>
>Signed-off-by: Vadym Kochan <[email protected]>
>---
[...]
>+
>+void prestera_ethtool_get_stats(struct net_device *dev,
>+ struct ethtool_stats *stats, u64 *data)
>+{
>+ struct prestera_port *port = netdev_priv(dev);
>+ struct prestera_port_stats *port_stats;
>+
>+ port_stats = &port->cached_hw_stats.stats;
>+
>+ memcpy((u8 *)data, port_stats, sizeof(*port_stats));
Avoid the needless cast.
[...]
>+static const struct ethtool_ops ethtool_ops = {
>+ .get_drvinfo = prestera_ethtool_get_drvinfo,
>+ .get_link_ksettings = prestera_ethtool_get_link_ksettings,
>+ .set_link_ksettings = prestera_ethtool_set_link_ksettings,
>+ .get_fecparam = prestera_ethtool_get_fecparam,
>+ .set_fecparam = prestera_ethtool_set_fecparam,
>+ .get_sset_count = prestera_ethtool_get_sset_count,
>+ .get_strings = prestera_ethtool_get_strings,
>+ .get_ethtool_stats = prestera_ethtool_get_stats,
>+ .get_link = ethtool_op_get_link,
>+ .nway_reset = prestera_ethtool_nway_reset
>+};
Have the struct in prestera_ethtool.c and export just the struct instead
of all the callbacks.
>+
> static int prestera_port_create(struct prestera_switch *sw, u32 id)
> {
> struct prestera_port *port;
>@@ -235,6 +266,7 @@ static int prestera_port_create(struct prestera_switch *sw, u32 id)
>
> dev->features |= NETIF_F_NETNS_LOCAL;
> dev->netdev_ops = &netdev_ops;
>+ dev->ethtool_ops = ðtool_ops;
>
> netif_carrier_off(dev);
>
>--
>2.17.1
>
Sat, Jul 25, 2020 at 05:06:47PM CEST, [email protected] wrote:
>Add PCI interface driver for Prestera Switch ASICs family devices, which
>provides:
>
> - Firmware loading mechanism
> - Requests & events handling to/from the firmware
> - Access to the firmware on the bus level
>
>The firmware has to be loaded each time device is reset. The driver is
>loading it from:
>
> /lib/firmware/marvell/prestera_fw-v{MAJOR}.{MINOR}.img
>
>The full firmware image version is located within internal header and
>consists of 3 numbers - MAJOR.MINOR.PATCH. Additionally, driver has
>hard-coded minimum supported firmware version which it can work with:
>
> MAJOR - reflects the support on ABI level between driver and loaded
> firmware, this number should be the same for driver and loaded
> firmware.
>
> MINOR - this is the minimum supported version between driver and the
> firmware.
>
> PATCH - indicates only fixes, firmware ABI is not changed.
>
>Firmware image file name contains only MAJOR and MINOR numbers to make
>driver be compatible with any PATCH version.
>
>Signed-off-by: Oleksandr Mazur <[email protected]>
>Signed-off-by: Vadym Kochan <[email protected]>
Looks fine.
Acked-by: Jiri Pirko <[email protected]>
On Sat, Jul 25, 2020 at 6:10 PM Vadym Kochan <[email protected]> wrote:
>
> Add PCI interface driver for Prestera Switch ASICs family devices, which
> provides:
>
> - Firmware loading mechanism
> - Requests & events handling to/from the firmware
> - Access to the firmware on the bus level
>
> The firmware has to be loaded each time device is reset. The driver is
the device
> loading it from:
>
> /lib/firmware/marvell/prestera_fw-v{MAJOR}.{MINOR}.img
>
> The full firmware image version is located within internal header and
the internal
> consists of 3 numbers - MAJOR.MINOR.PATCH. Additionally, driver has
> hard-coded minimum supported firmware version which it can work with:
>
> MAJOR - reflects the support on ABI level between driver and loaded
> firmware, this number should be the same for driver and loaded
> firmware.
>
> MINOR - this is the minimum supported version between driver and the
> firmware.
>
> PATCH - indicates only fixes, firmware ABI is not changed.
>
> Firmware image file name contains only MAJOR and MINOR numbers to make
> driver be compatible with any PATCH version.
>
> Signed-off-by: Oleksandr Mazur <[email protected]>
> Signed-off-by: Vadym Kochan <[email protected]>
> ---
> drivers/net/ethernet/marvell/prestera/Kconfig | 11 +
> .../net/ethernet/marvell/prestera/Makefile | 2 +
> .../ethernet/marvell/prestera/prestera_pci.c | 823 ++++++++++++++++++
> 3 files changed, 836 insertions(+)
> create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_pci.c
>
> diff --git a/drivers/net/ethernet/marvell/prestera/Kconfig b/drivers/net/ethernet/marvell/prestera/Kconfig
> index 76b68613ea7a..d30e3e6d8b7b 100644
> --- a/drivers/net/ethernet/marvell/prestera/Kconfig
> +++ b/drivers/net/ethernet/marvell/prestera/Kconfig
> @@ -11,3 +11,14 @@ config PRESTERA
>
> To compile this driver as a module, choose M here: the
> module will be called prestera.
> +
> +config PRESTERA_PCI
> + tristate "PCI interface driver for Marvell Prestera Switch ASICs family"
> + depends on PCI && HAS_IOMEM && PRESTERA
> + default m
Even if I have CONFIG_PRESTERA=y, why as a user I must have this as a module?
If it's a crucial feature, shouldn't it be rather
default CONFIG_PRESTERA
?
> + help
> + This is implementation of PCI interface support for Marvell Prestera
> + Switch ASICs family.
> +
> + To compile this driver as a module, choose M here: the
> + module will be called prestera_pci.
> diff --git a/drivers/net/ethernet/marvell/prestera/Makefile b/drivers/net/ethernet/marvell/prestera/Makefile
> index 610d75032b78..2146714eab21 100644
> --- a/drivers/net/ethernet/marvell/prestera/Makefile
> +++ b/drivers/net/ethernet/marvell/prestera/Makefile
> @@ -2,3 +2,5 @@
> obj-$(CONFIG_PRESTERA) += prestera.o
> prestera-objs := prestera_main.o prestera_hw.o prestera_dsa.o \
> prestera_rxtx.o
> +
> +obj-$(CONFIG_PRESTERA_PCI) += prestera_pci.o
> diff --git a/drivers/net/ethernet/marvell/prestera/prestera_pci.c b/drivers/net/ethernet/marvell/prestera/prestera_pci.c
> new file mode 100644
> index 000000000000..cc8446c6b4d9
> --- /dev/null
> +++ b/drivers/net/ethernet/marvell/prestera/prestera_pci.c
> @@ -0,0 +1,823 @@
> +// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
> +/* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved */
> +
> +#include <linux/module.h>
> +#include <linux/kernel.h>
> +#include <linux/device.h>
> +#include <linux/pci.h>
> +#include <linux/circ_buf.h>
> +#include <linux/firmware.h>
> +#include <linux/iopoll.h>
Perhaps keep them in order?
> +
> +#include "prestera.h"
> +
> +#define PRESTERA_MSG_MAX_SIZE 1500
> +
> +#define PRESTERA_SUPP_FW_MAJ_VER 2
> +#define PRESTERA_SUPP_FW_MIN_VER 0
> +
> +#define PRESTERA_FW_PATH \
> + "mrvl/prestera/mvsw_prestera_fw-v" \
> + __stringify(PRESTERA_SUPP_FW_MAJ_VER) \
> + "." __stringify(PRESTERA_SUPP_FW_MIN_VER) ".img"
Wouldn't it be better to see this in the C code?
Also this is a PATH and a filename.
> +
> +#define PRESTERA_FW_HDR_MAGIC 0x351D9D06
> +#define PRESTERA_FW_DL_TIMEOUT 50000
Use the suffix, are they "us" or "ns" or "s" or what?
> +#define PRESTERA_FW_BLK_SZ 1024
> +
> +#define PRESTERA_FW_VER_MAJ_MUL 1000000
> +#define PRESTERA_FW_VER_MIN_MUL 1000
> +
> +#define PRESTERA_FW_VER_MAJ(v) ((v) / PRESTERA_FW_VER_MAJ_MUL)
> +
> +#define PRESTERA_FW_VER_MIN(v) \
> + (((v) - (PRESTERA_FW_VER_MAJ(v) * PRESTERA_FW_VER_MAJ_MUL)) / \
> + PRESTERA_FW_VER_MIN_MUL)
> +
> +#define PRESTERA_FW_VER_PATCH(v) \
> + ((v) - (PRESTERA_FW_VER_MAJ(v) * PRESTERA_FW_VER_MAJ_MUL) - \
> + (PRESTERA_FW_VER_MIN(v) * PRESTERA_FW_VER_MIN_MUL))
> +
> +struct prestera_fw_header {
> + __be32 magic_number;
> + __be32 version_value;
> + u8 reserved[8];
> +} __packed;
Hmm... How __packed make a difference here?
> +struct prestera_ldr_regs {
> + u32 ldr_ready;
> + u32 pad1;
> +
> + u32 ldr_img_size;
> + u32 ldr_ctl_flags;
> +
> + u32 ldr_buf_offs;
> + u32 ldr_buf_size;
> +
> + u32 ldr_buf_rd;
> + u32 pad2;
> + u32 ldr_buf_wr;
> +
> + u32 ldr_status;
> +} __packed __aligned(4);
> +
> +#define PRESTERA_LDR_REG_OFFSET(f) offsetof(struct prestera_ldr_regs, f)
> +
> +#define PRESTERA_LDR_READY_MAGIC 0xf00dfeed
> +
> +#define PRESTERA_LDR_STATUS_IMG_DL BIT(0)
> +#define PRESTERA_LDR_STATUS_START_FW BIT(1)
> +#define PRESTERA_LDR_STATUS_INVALID_IMG BIT(2)
> +#define PRESTERA_LDR_STATUS_NOMEM BIT(3)
> +
> +#define PRESTERA_LDR_REG_BASE(fw) ((fw)->ldr_regs)
> +#define PRESTERA_LDR_REG_ADDR(fw, reg) (PRESTERA_LDR_REG_BASE(fw) + (reg))
> +
> +#define prestera_ldr_write(fw, reg, val) \
> + writel(val, PRESTERA_LDR_REG_ADDR(fw, reg))
> +#define prestera_ldr_read(fw, reg) \
> + readl(PRESTERA_LDR_REG_ADDR(fw, reg))
> +
> +/* fw loader registers */
> +#define PRESTERA_LDR_READY_REG PRESTERA_LDR_REG_OFFSET(ldr_ready)
> +#define PRESTERA_LDR_IMG_SIZE_REG PRESTERA_LDR_REG_OFFSET(ldr_img_size)
> +#define PRESTERA_LDR_CTL_REG PRESTERA_LDR_REG_OFFSET(ldr_ctl_flags)
> +#define PRESTERA_LDR_BUF_SIZE_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_size)
> +#define PRESTERA_LDR_BUF_OFFS_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_offs)
> +#define PRESTERA_LDR_BUF_RD_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_rd)
> +#define PRESTERA_LDR_BUF_WR_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_wr)
> +#define PRESTERA_LDR_STATUS_REG PRESTERA_LDR_REG_OFFSET(ldr_status)
> +
> +#define PRESTERA_LDR_CTL_DL_START BIT(0)
> +
> +#define PRESTERA_EVT_QNUM_MAX 4
> +
> +struct prestera_fw_evtq_regs {
> + u32 rd_idx;
> + u32 pad1;
> + u32 wr_idx;
> + u32 pad2;
> + u32 offs;
> + u32 len;
> +};
> +
> +struct prestera_fw_regs {
> + u32 fw_ready;
> + u32 pad;
> + u32 cmd_offs;
> + u32 cmd_len;
> + u32 evt_offs;
> + u32 evt_qnum;
> +
> + u32 cmd_req_ctl;
> + u32 cmd_req_len;
> + u32 cmd_rcv_ctl;
> + u32 cmd_rcv_len;
> +
> + u32 fw_status;
> + u32 rx_status;
> +
> + struct prestera_fw_evtq_regs evtq_list[PRESTERA_EVT_QNUM_MAX];
> +};
> +#define PRESTERA_FW_REG_OFFSET(f) offsetof(struct prestera_fw_regs, f)
> +#define PRESTERA_FW_READY_MAGIC 0xcafebabe
> +
> +/* fw registers */
> +#define PRESTERA_FW_READY_REG PRESTERA_FW_REG_OFFSET(fw_ready)
> +
> +#define PRESTERA_CMD_BUF_OFFS_REG PRESTERA_FW_REG_OFFSET(cmd_offs)
> +#define PRESTERA_CMD_BUF_LEN_REG PRESTERA_FW_REG_OFFSET(cmd_len)
> +#define PRESTERA_EVT_BUF_OFFS_REG PRESTERA_FW_REG_OFFSET(evt_offs)
> +#define PRESTERA_EVT_QNUM_REG PRESTERA_FW_REG_OFFSET(evt_qnum)
> +
> +#define PRESTERA_CMD_REQ_CTL_REG PRESTERA_FW_REG_OFFSET(cmd_req_ctl)
> +#define PRESTERA_CMD_REQ_LEN_REG PRESTERA_FW_REG_OFFSET(cmd_req_len)
> +
> +#define PRESTERA_CMD_RCV_CTL_REG PRESTERA_FW_REG_OFFSET(cmd_rcv_ctl)
> +#define PRESTERA_CMD_RCV_LEN_REG PRESTERA_FW_REG_OFFSET(cmd_rcv_len)
> +#define PRESTERA_FW_STATUS_REG PRESTERA_FW_REG_OFFSET(fw_status)
> +#define PRESTERA_RX_STATUS_REG PRESTERA_FW_REG_OFFSET(rx_status)
> +
> +/* PRESTERA_CMD_REQ_CTL_REG flags */
> +#define PRESTERA_CMD_F_REQ_SENT BIT(0)
> +#define PRESTERA_CMD_F_REPL_RCVD BIT(1)
> +
> +/* PRESTERA_CMD_RCV_CTL_REG flags */
> +#define PRESTERA_CMD_F_REPL_SENT BIT(0)
> +
> +#define PRESTERA_EVTQ_REG_OFFSET(q, f) \
> + (PRESTERA_FW_REG_OFFSET(evtq_list) + \
> + (q) * sizeof(struct prestera_fw_evtq_regs) + \
> + offsetof(struct prestera_fw_evtq_regs, f))
> +
> +#define PRESTERA_EVTQ_RD_IDX_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, rd_idx)
> +#define PRESTERA_EVTQ_WR_IDX_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, wr_idx)
> +#define PRESTERA_EVTQ_OFFS_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, offs)
> +#define PRESTERA_EVTQ_LEN_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, len)
> +
> +#define PRESTERA_FW_REG_BASE(fw) ((fw)->dev.ctl_regs)
> +#define PRESTERA_FW_REG_ADDR(fw, reg) PRESTERA_FW_REG_BASE((fw)) + (reg)
> +#define prestera_fw_write(fw, reg, val) \
> + writel(val, PRESTERA_FW_REG_ADDR(fw, reg))
> +#define prestera_fw_read(fw, reg) \
> + readl(PRESTERA_FW_REG_ADDR(fw, reg))
Why macros? Can't be inline functions? Why?
> +#define PRESTERA_FW_CMD_DEFAULT_WAITMS 30000
> +#define PRESTERA_FW_READY_WAITMS 20000
_MS in both cases
> +struct prestera_fw_evtq {
> + u8 __iomem *addr;
> + size_t len;
> +};
> +
> +struct prestera_fw {
> + struct workqueue_struct *wq;
> + struct prestera_device dev;
> + struct pci_dev *pci_dev;
> + u8 __iomem *ldr_regs;
> + u8 __iomem *ldr_ring_buf;
> + u32 ldr_buf_len;
> + u32 ldr_wr_idx;
> + struct mutex cmd_mtx; /* serialize access to dev->send_req */
> + size_t cmd_mbox_len;
> + u8 __iomem *cmd_mbox;
> + struct prestera_fw_evtq evt_queue[PRESTERA_EVT_QNUM_MAX];
> + u8 evt_qnum;
> + struct work_struct evt_work;
> + u8 __iomem *evt_buf;
> + u8 *evt_msg;
> +};
> +
> +static int prestera_fw_load(struct prestera_fw *fw);
> +
> +static u32 prestera_fw_evtq_len(struct prestera_fw *fw, u8 qid)
> +{
> + return fw->evt_queue[qid].len;
> +}
> +
> +static u32 prestera_fw_evtq_avail(struct prestera_fw *fw, u8 qid)
> +{
> + u32 wr_idx = prestera_fw_read(fw, PRESTERA_EVTQ_WR_IDX_REG(qid));
> + u32 rd_idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
> +
> + return CIRC_CNT(wr_idx, rd_idx, prestera_fw_evtq_len(fw, qid));
> +}
> +
> +static void prestera_fw_evtq_rd_set(struct prestera_fw *fw,
> + u8 qid, u32 idx)
> +{
> + u32 rd_idx = idx & (prestera_fw_evtq_len(fw, qid) - 1);
> +
> + prestera_fw_write(fw, PRESTERA_EVTQ_RD_IDX_REG(qid), rd_idx);
> +}
> +
> +static u8 __iomem *prestera_fw_evtq_buf(struct prestera_fw *fw, u8 qid)
> +{
> + return fw->evt_queue[qid].addr;
> +}
> +
> +static u32 prestera_fw_evtq_read32(struct prestera_fw *fw, u8 qid)
> +{
> + u32 rd_idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
> + u32 val;
> +
> + val = readl(prestera_fw_evtq_buf(fw, qid) + rd_idx);
> + prestera_fw_evtq_rd_set(fw, qid, rd_idx + 4);
> + return val;
> +}
> +
> +static ssize_t prestera_fw_evtq_read_buf(struct prestera_fw *fw,
> + u8 qid, u8 *buf, size_t len)
> +{
> + u32 idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
> + u8 __iomem *evtq_addr = prestera_fw_evtq_buf(fw, qid);
> + u32 *buf32 = (u32 *)buf;
> + int i;
> + for (i = 0; i < len / 4; buf32++, i++) {
> + *buf32 = readl_relaxed(evtq_addr + idx);
> + idx = (idx + 4) & (prestera_fw_evtq_len(fw, qid) - 1);
> + }
> +
> + prestera_fw_evtq_rd_set(fw, qid, idx);
> +
> + return i;
> +}
> +
> +static u8 prestera_fw_evtq_pick(struct prestera_fw *fw)
> +{
> + int qid;
> +
> + for (qid = 0; qid < fw->evt_qnum; qid++) {
> + if (prestera_fw_evtq_avail(fw, qid) >= 4)
> + return qid;
> + }
> +
> + return PRESTERA_EVT_QNUM_MAX;
> +}
> +
> +static void prestera_fw_evt_work_fn(struct work_struct *work)
> +{
> + struct prestera_fw *fw;
> + u8 *msg;
> + u8 qid;
> +
> + fw = container_of(work, struct prestera_fw, evt_work);
> + msg = fw->evt_msg;
> +
> + while ((qid = prestera_fw_evtq_pick(fw)) < PRESTERA_EVT_QNUM_MAX) {
> + u32 idx;
> + u32 len;
> +
> + len = prestera_fw_evtq_read32(fw, qid);
> + idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
> +
> + WARN_ON(prestera_fw_evtq_avail(fw, qid) < len);
> +
> + if (WARN_ON(len > PRESTERA_MSG_MAX_SIZE)) {
> + prestera_fw_evtq_rd_set(fw, qid, idx + len);
> + continue;
> + }
> +
> + prestera_fw_evtq_read_buf(fw, qid, msg, len);
> +
> + if (fw->dev.recv_msg)
> + fw->dev.recv_msg(&fw->dev, msg, len);
> + }
> +}
> +
> +static int prestera_fw_wait_reg32(struct prestera_fw *fw, u32 reg, u32 cmp,
> + unsigned int waitms)
> +{
> + u8 __iomem *addr = PRESTERA_FW_REG_ADDR(fw, reg);
> + u32 val = 0;
Redundant assignment.
> +
> + return readl_poll_timeout(addr, val, cmp == val, 1000 * 10, waitms * 1000);
> +}
> +static void prestera_pci_copy_to(u8 __iomem *dst, u8 *src, size_t len)
> +{
> + u32 __iomem *dst32 = (u32 __iomem *)dst;
> + u32 *src32 = (u32 *)src;
> + int i;
> +
> + for (i = 0; i < (len / 4); dst32++, src32++, i++)
> + writel_relaxed(*src32, dst32);
> +}
> +
> +static void prestera_pci_copy_from(u8 *dst, u8 __iomem *src, size_t len)
> +{
> + u32 __iomem *src32 = (u32 __iomem *)src;
> + u32 *dst32 = (u32 *)dst;
> + int i;
> +
> + for (i = 0; i < (len / 4); dst32++, src32++, i++)
> + *dst32 = readl_relaxed(src32);
> +}
NIH of memcpy_fromio() / memcpy_toio() ?
> +static int prestera_fw_cmd_send(struct prestera_fw *fw,
> + u8 *in_msg, size_t in_size,
> + u8 *out_msg, size_t out_size,
> + unsigned int waitms)
> +{
> + u32 ret_size = 0;
> + int err = 0;
Redundant assignment. Check your entire code.
> + if (!waitms)
> + waitms = PRESTERA_FW_CMD_DEFAULT_WAITMS;
> +
> + if (ALIGN(in_size, 4) > fw->cmd_mbox_len)
> + return -EMSGSIZE;
> +
> + /* wait for finish previous reply from FW */
> + err = prestera_fw_wait_reg32(fw, PRESTERA_CMD_RCV_CTL_REG, 0, 30);
> + if (err) {
> + dev_err(fw->dev.dev, "finish reply from FW is timed out\n");
> + return err;
> + }
> +
> + prestera_fw_write(fw, PRESTERA_CMD_REQ_LEN_REG, in_size);
> + prestera_pci_copy_to(fw->cmd_mbox, in_msg, in_size);
> +
> + prestera_fw_write(fw, PRESTERA_CMD_REQ_CTL_REG, PRESTERA_CMD_F_REQ_SENT);
> +
> + /* wait for reply from FW */
> + err = prestera_fw_wait_reg32(fw, PRESTERA_CMD_RCV_CTL_REG,
> + PRESTERA_CMD_F_REPL_SENT, waitms);
> + if (err) {
> + dev_err(fw->dev.dev, "reply from FW is timed out\n");
> + goto cmd_exit;
> + }
> +
> + ret_size = prestera_fw_read(fw, PRESTERA_CMD_RCV_LEN_REG);
> + if (ret_size > out_size) {
> + dev_err(fw->dev.dev, "ret_size (%u) > out_len(%zu)\n",
> + ret_size, out_size);
> + err = -EMSGSIZE;
> + goto cmd_exit;
> + }
> +
> + prestera_pci_copy_from(out_msg, fw->cmd_mbox + in_size, ret_size);
> +
> +cmd_exit:
> + prestera_fw_write(fw, PRESTERA_CMD_REQ_CTL_REG, PRESTERA_CMD_F_REPL_RCVD);
> + return err;
> +}
> +
> +static int prestera_fw_send_req(struct prestera_device *dev,
> + u8 *in_msg, size_t in_size, u8 *out_msg,
> + size_t out_size, unsigned int waitms)
> +{
> + struct prestera_fw *fw;
> + ssize_t ret;
> +
> + fw = container_of(dev, struct prestera_fw, dev);
> +
> + mutex_lock(&fw->cmd_mtx);
> + ret = prestera_fw_cmd_send(fw, in_msg, in_size, out_msg, out_size, waitms);
> + mutex_unlock(&fw->cmd_mtx);
> +
> + return ret;
> +}
> +
> +static int prestera_fw_init(struct prestera_fw *fw)
> +{
> + u8 __iomem *base;
> + int err;
> + u8 qid;
> +
> + fw->dev.send_req = prestera_fw_send_req;
> + fw->ldr_regs = fw->dev.ctl_regs;
> +
> + err = prestera_fw_load(fw);
> + if (err && err != -ETIMEDOUT)
> + return err;
> +
> + err = prestera_fw_wait_reg32(fw, PRESTERA_FW_READY_REG,
> + PRESTERA_FW_READY_MAGIC,
> + PRESTERA_FW_READY_WAITMS);
> + if (err) {
> + dev_err(fw->dev.dev, "FW failed to start\n");
> + return err;
> + }
> +
> + base = fw->dev.ctl_regs;
> +
> + fw->cmd_mbox = base + prestera_fw_read(fw, PRESTERA_CMD_BUF_OFFS_REG);
> + fw->cmd_mbox_len = prestera_fw_read(fw, PRESTERA_CMD_BUF_LEN_REG);
> + mutex_init(&fw->cmd_mtx);
> +
> + fw->evt_buf = base + prestera_fw_read(fw, PRESTERA_EVT_BUF_OFFS_REG);
> + fw->evt_qnum = prestera_fw_read(fw, PRESTERA_EVT_QNUM_REG);
> + fw->evt_msg = kmalloc(PRESTERA_MSG_MAX_SIZE, GFP_KERNEL);
> + if (!fw->evt_msg)
> + return -ENOMEM;
> +
> + for (qid = 0; qid < fw->evt_qnum; qid++) {
> + u32 offs = prestera_fw_read(fw, PRESTERA_EVTQ_OFFS_REG(qid));
> + struct prestera_fw_evtq *evtq = &fw->evt_queue[qid];
> +
> + evtq->len = prestera_fw_read(fw, PRESTERA_EVTQ_LEN_REG(qid));
> + evtq->addr = fw->evt_buf + offs;
> + }
> +
> + return 0;
> +}
> +
> +static void prestera_fw_uninit(struct prestera_fw *fw)
> +{
> + kfree(fw->evt_msg);
> +}
> +
> +static irqreturn_t prestera_pci_irq_handler(int irq, void *dev_id)
> +{
> + struct prestera_fw *fw = dev_id;
> +
> + if (prestera_fw_read(fw, PRESTERA_RX_STATUS_REG)) {
> + prestera_fw_write(fw, PRESTERA_RX_STATUS_REG, 0);
> +
> + if (fw->dev.recv_pkt)
> + fw->dev.recv_pkt(&fw->dev);
> + }
> +
> + queue_work(fw->wq, &fw->evt_work);
> +
> + return IRQ_HANDLED;
> +}
> +
> +static int prestera_ldr_wait_reg32(struct prestera_fw *fw,
> + u32 reg, u32 cmp, unsigned int waitms)
> +{
> + u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, reg);
> + u32 val = 0;
> +
> + return readl_poll_timeout(addr, val, cmp == val, 1000 * 10, waitms * 1000);
> +}
> +
> +static u32 prestera_ldr_wait_buf(struct prestera_fw *fw, size_t len)
> +{
> + u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, PRESTERA_LDR_BUF_RD_REG);
> + u32 buf_len = fw->ldr_buf_len;
> + u32 wr_idx = fw->ldr_wr_idx;
> + u32 rd_idx = 0;
> +
> + return readl_poll_timeout(addr, rd_idx,
> + CIRC_SPACE(wr_idx, rd_idx, buf_len) >= len,
> + 1000, 100 * 1000);
> + return 0;
> +}
> +
> +static int prestera_ldr_wait_dl_finish(struct prestera_fw *fw)
> +{
> + u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, PRESTERA_LDR_STATUS_REG);
> + unsigned int waitus = PRESTERA_FW_DL_TIMEOUT * 1000;
> + unsigned long mask = ~(PRESTERA_LDR_STATUS_IMG_DL);
> + u32 val = 0;
> + int err;
> +
> + err = readl_poll_timeout(addr, val, val & mask, 1000 * 10, waitus);
> + if (err) {
> + dev_err(fw->dev.dev, "Timeout to load FW img [state=%d]",
> + prestera_ldr_read(fw, PRESTERA_LDR_STATUS_REG));
> + return err;
> + }
> +
> + return 0;
> +}
> +
> +static void prestera_ldr_wr_idx_move(struct prestera_fw *fw, unsigned int n)
> +{
> + fw->ldr_wr_idx = (fw->ldr_wr_idx + (n)) & (fw->ldr_buf_len - 1);
> +}
> +
> +static void prestera_ldr_wr_idx_commit(struct prestera_fw *fw)
> +{
> + prestera_ldr_write(fw, PRESTERA_LDR_BUF_WR_REG, fw->ldr_wr_idx);
> +}
> +
> +static u8 __iomem *prestera_ldr_wr_ptr(struct prestera_fw *fw)
> +{
> + return fw->ldr_ring_buf + fw->ldr_wr_idx;
> +}
> +
> +static int prestera_ldr_send(struct prestera_fw *fw, const u8 *buf, size_t len)
> +{
> + int err;
> + int i;
> +
> + err = prestera_ldr_wait_buf(fw, len);
> + if (err) {
> + dev_err(fw->dev.dev, "failed wait for sending firmware\n");
> + return err;
> + }
> +
> + for (i = 0; i < len; i += 4) {
> + writel_relaxed(*(u32 *)(buf + i), prestera_ldr_wr_ptr(fw));
> + prestera_ldr_wr_idx_move(fw, 4);
> + }
> +
> + prestera_ldr_wr_idx_commit(fw);
> + return 0;
> +}
> +
> +static int prestera_ldr_fw_send(struct prestera_fw *fw,
> + const char *img, u32 fw_size)
> +{
> + u32 status;
> + u32 pos;
> + int err;
> +
> + err = prestera_ldr_wait_reg32(fw, PRESTERA_LDR_STATUS_REG,
> + PRESTERA_LDR_STATUS_IMG_DL, 5 * 1000);
> + if (err) {
> + dev_err(fw->dev.dev, "Loader is not ready to load image\n");
> + return err;
> + }
> +
> + for (pos = 0; pos < fw_size; pos += PRESTERA_FW_BLK_SZ) {
> + if (pos + PRESTERA_FW_BLK_SZ > fw_size)
> + break;
> +
> + err = prestera_ldr_send(fw, img + pos, PRESTERA_FW_BLK_SZ);
> + if (err)
> + return err;
> + }
> +
> + if (pos < fw_size) {
> + err = prestera_ldr_send(fw, img + pos, fw_size - pos);
> + if (err)
> + return err;
> + }
> +
> + err = prestera_ldr_wait_dl_finish(fw);
> + if (err)
> + return err;
> +
> + status = prestera_ldr_read(fw, PRESTERA_LDR_STATUS_REG);
> + if (status != PRESTERA_LDR_STATUS_START_FW) {
What is the point?
> + switch (status) {
> + case PRESTERA_LDR_STATUS_INVALID_IMG:
> + dev_err(fw->dev.dev, "FW img has bad CRC\n");
> + return -EINVAL;
> + case PRESTERA_LDR_STATUS_NOMEM:
> + dev_err(fw->dev.dev, "Loader has no enough mem\n");
> + return -ENOMEM;
> + default:
> + break;
> + }
> + }
> +
> + return 0;
> +}
> +
> +static void prestera_fw_rev_parse(const struct prestera_fw_header *hdr,
> + struct prestera_fw_rev *rev)
> +{
> + u32 version = be32_to_cpu(hdr->version_value);
> +
> + rev->maj = PRESTERA_FW_VER_MAJ(version);
> + rev->min = PRESTERA_FW_VER_MIN(version);
> + rev->sub = PRESTERA_FW_VER_PATCH(version);
> +}
> +
> +static int prestera_fw_rev_check(struct prestera_fw *fw)
> +{
> + struct prestera_fw_rev *rev = &fw->dev.fw_rev;
> + u16 maj_supp = PRESTERA_SUPP_FW_MAJ_VER;
> + u16 min_supp = PRESTERA_SUPP_FW_MIN_VER;
> +
> + if (rev->maj == maj_supp && rev->min >= min_supp)
> + return 0;
Why not traditional pattern
if (err) {
...
}
...
return 0;
?
> + dev_err(fw->dev.dev, "Driver supports FW version only '%u.%u.x'",
> + PRESTERA_SUPP_FW_MAJ_VER, PRESTERA_SUPP_FW_MIN_VER);
> +
> + return -EINVAL;
> +}
> +
> +static int prestera_fw_hdr_parse(struct prestera_fw *fw,
> + const struct firmware *img)
> +{
> + struct prestera_fw_header *hdr = (struct prestera_fw_header *)img->data;
> + struct prestera_fw_rev *rev = &fw->dev.fw_rev;
> + u32 magic;
> +
> + magic = be32_to_cpu(hdr->magic_number);
> + if (magic != PRESTERA_FW_HDR_MAGIC) {
> + dev_err(fw->dev.dev, "FW img hdr magic is invalid");
> + return -EINVAL;
> + }
> +
> + prestera_fw_rev_parse(hdr, rev);
> +
> + dev_info(fw->dev.dev, "FW version '%u.%u.%u'\n",
> + rev->maj, rev->min, rev->sub);
> +
> + return prestera_fw_rev_check(fw);
> +}
> +
> +static int prestera_fw_load(struct prestera_fw *fw)
> +{
> + size_t hlen = sizeof(struct prestera_fw_header);
> + const struct firmware *f;
> + int err;
> +
> + err = prestera_ldr_wait_reg32(fw, PRESTERA_LDR_READY_REG,
> + PRESTERA_LDR_READY_MAGIC, 5 * 1000);
> + if (err) {
> + dev_err(fw->dev.dev, "waiting for FW loader is timed out");
> + return err;
> + }
> +
> + fw->ldr_ring_buf = fw->ldr_regs +
> + prestera_ldr_read(fw, PRESTERA_LDR_BUF_OFFS_REG);
> +
> + fw->ldr_buf_len =
> + prestera_ldr_read(fw, PRESTERA_LDR_BUF_SIZE_REG);
> +
> + fw->ldr_wr_idx = 0;
> +
> + err = request_firmware_direct(&f, PRESTERA_FW_PATH, &fw->pci_dev->dev);
> + if (err) {
> + dev_err(fw->dev.dev, "failed to request firmware file\n");
> + return err;
> + }
> +
> + if (!IS_ALIGNED(f->size, 4)) {
> + dev_err(fw->dev.dev, "FW image file is not aligned");
> + release_firmware(f);
> + return -EINVAL;
> + }
> +
> + err = prestera_fw_hdr_parse(fw, f);
> + if (err) {
> + dev_err(fw->dev.dev, "FW image header is invalid\n");
> + release_firmware(f);
> + return err;
> + }
> +
> + prestera_ldr_write(fw, PRESTERA_LDR_IMG_SIZE_REG, f->size - hlen);
> + prestera_ldr_write(fw, PRESTERA_LDR_CTL_REG, PRESTERA_LDR_CTL_DL_START);
> +
> + dev_info(fw->dev.dev, "Loading prestera FW image ...");
Makes sense to see the file which is being used as a FW image.
> + err = prestera_ldr_fw_send(fw, f->data + hlen, f->size - hlen);
> +
> + release_firmware(f);
> + return err;
> +}
> +
> +static int prestera_pci_probe(struct pci_dev *pdev,
> + const struct pci_device_id *id)
> +{
> + const char *driver_name = pdev->driver->name;
> + u8 __iomem *ctl_addr, *pp_addr;
> + struct prestera_fw *fw;
> + int err;
> +
> + err = pci_enable_device(pdev);
pcim_enable_device()
> + if (err)
> + return err;
> +
> + err = pci_request_regions(pdev, driver_name);
> + if (err) {
> + dev_err(&pdev->dev, "pci_request_regions failed\n");
> + goto err_pci_request_regions;
> + }
(1)
> +
> + if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(30))) {
> + dev_err(&pdev->dev, "fail to set DMA mask\n");
> + goto err_dma_mask;
> + }
> +
> + ctl_addr = pci_ioremap_bar(pdev, 2);
> + if (!ctl_addr) {
> + dev_err(&pdev->dev, "ioremap failed\n");
> + err = -EIO;
> + goto err_ctl_ioremap;
> + }
> +
> + pp_addr = pci_ioremap_bar(pdev, 4);
> + if (!pp_addr) {
> + dev_err(&pdev->dev, "ioremap failed\n");
> + err = -EIO;
> + goto err_pp_ioremap;
> + }
(2)
(1) and (2) can be replaced with
pcim_ioremap_regions()
followed by pcim_iomap_table[2] / [4]
And magic 2 & 4 should be defined.
> + pci_set_master(pdev);
> + fw = kzalloc(sizeof(*fw), GFP_KERNEL);
devm_kzalloc() ?
> + if (!fw) {
> + err = -ENOMEM;
> + goto err_pci_dev_alloc;
> + }
> +
> + fw->pci_dev = pdev;
> + fw->dev.dev = &pdev->dev;
> + fw->dev.ctl_regs = ctl_addr;
> + fw->dev.pp_regs = pp_addr;
> +
> + pci_set_drvdata(pdev, fw);
> +
> + err = prestera_fw_init(fw);
> + if (err)
> + goto err_prestera_fw_init;
> +
> + dev_info(fw->dev.dev, "Switch FW is ready\n");
> +
> + fw->wq = alloc_workqueue("prestera_fw_wq", WQ_HIGHPRI, 1);
> + if (!fw->wq)
> + goto err_wq_alloc;
> +
> + INIT_WORK(&fw->evt_work, prestera_fw_evt_work_fn);
> +
> + err = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
> + if (err < 0) {
> + dev_err(&pdev->dev, "MSI IRQ init failed\n");
> + goto err_irq_alloc;
> + }
> +
> + err = request_irq(pci_irq_vector(pdev, 0), prestera_pci_irq_handler,
> + 0, driver_name, fw);
> + if (err) {
> + dev_err(&pdev->dev, "fail to request IRQ\n");
> + goto err_request_irq;
> + }
> +
> + err = prestera_device_register(&fw->dev);
> + if (err)
> + goto err_prestera_dev_register;
> +
> + return 0;
> +
> +err_prestera_dev_register:
> + free_irq(pci_irq_vector(pdev, 0), fw);
> +err_request_irq:
> + pci_free_irq_vectors(pdev);
> +err_irq_alloc:
> + destroy_workqueue(fw->wq);
> +err_wq_alloc:
> + prestera_fw_uninit(fw);
> +err_prestera_fw_init:
> + kfree(fw);
> +err_pci_dev_alloc:
> + iounmap(pp_addr);
> +err_pp_ioremap:
> + iounmap(ctl_addr);
> +err_ctl_ioremap:
> +err_dma_mask:
> + pci_release_regions(pdev);
> +err_pci_request_regions:
> + pci_disable_device(pdev);
> + return err;
> +}
> +
> +static void prestera_pci_remove(struct pci_dev *pdev)
> +{
> + struct prestera_fw *fw = pci_get_drvdata(pdev);
> + u8 __iomem *ctl_addr = fw->dev.ctl_regs;
> + u8 __iomem *pp_addr = fw->dev.pp_regs;
> +
> + prestera_device_unregister(&fw->dev);
> + free_irq(pci_irq_vector(pdev, 0), fw);
> + pci_free_irq_vectors(pdev);
> + destroy_workqueue(fw->wq);
> + prestera_fw_uninit(fw);
> + kfree(fw);
> + iounmap(pp_addr);
> + iounmap(ctl_addr);
> + pci_release_regions(pdev);
> + pci_disable_device(pdev);
> +}
> +
> +static const struct pci_device_id prestera_pci_devices[] = {
> + { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0xC804) },
> + { }
> +};
> +MODULE_DEVICE_TABLE(pci, prestera_pci_devices);
> +
> +static struct pci_driver prestera_pci_driver = {
> + .name = "Prestera DX",
> + .id_table = prestera_pci_devices,
> + .probe = prestera_pci_probe,
> + .remove = prestera_pci_remove,
> +};
> +
> +static int __init prestera_pci_init(void)
> +{
> + return pci_register_driver(&prestera_pci_driver);
> +}
> +
> +static void __exit prestera_pci_exit(void)
> +{
> + pci_unregister_driver(&prestera_pci_driver);
> +}
> +
> +module_init(prestera_pci_init);
> +module_exit(prestera_pci_exit);
module_pci_driver()
> +MODULE_LICENSE("Dual BSD/GPL");
> +MODULE_DESCRIPTION("Marvell Prestera switch PCI interface");
--
With Best Regards,
Andy Shevchenko
Hi Andy,
On Sun, Jul 26, 2020 at 01:32:19PM +0300, Andy Shevchenko wrote:
> On Sat, Jul 25, 2020 at 6:10 PM Vadym Kochan <[email protected]> wrote:
> >
> > Add PCI interface driver for Prestera Switch ASICs family devices, which
> > provides:
> >
> > - Firmware loading mechanism
> > - Requests & events handling to/from the firmware
> > - Access to the firmware on the bus level
> >
> > The firmware has to be loaded each time device is reset. The driver is
>
> the device
>
> > loading it from:
> >
> > /lib/firmware/marvell/prestera_fw-v{MAJOR}.{MINOR}.img
> >
> > The full firmware image version is located within internal header and
>
> the internal
>
> > consists of 3 numbers - MAJOR.MINOR.PATCH. Additionally, driver has
> > hard-coded minimum supported firmware version which it can work with:
> >
> > MAJOR - reflects the support on ABI level between driver and loaded
> > firmware, this number should be the same for driver and loaded
> > firmware.
> >
> > MINOR - this is the minimum supported version between driver and the
> > firmware.
> >
> > PATCH - indicates only fixes, firmware ABI is not changed.
> >
> > Firmware image file name contains only MAJOR and MINOR numbers to make
> > driver be compatible with any PATCH version.
> >
> > Signed-off-by: Oleksandr Mazur <[email protected]>
> > Signed-off-by: Vadym Kochan <[email protected]>
> > ---
> > drivers/net/ethernet/marvell/prestera/Kconfig | 11 +
> > .../net/ethernet/marvell/prestera/Makefile | 2 +
> > .../ethernet/marvell/prestera/prestera_pci.c | 823 ++++++++++++++++++
> > 3 files changed, 836 insertions(+)
> > create mode 100644 drivers/net/ethernet/marvell/prestera/prestera_pci.c
> >
> > diff --git a/drivers/net/ethernet/marvell/prestera/Kconfig b/drivers/net/ethernet/marvell/prestera/Kconfig
> > index 76b68613ea7a..d30e3e6d8b7b 100644
> > --- a/drivers/net/ethernet/marvell/prestera/Kconfig
> > +++ b/drivers/net/ethernet/marvell/prestera/Kconfig
> > @@ -11,3 +11,14 @@ config PRESTERA
> >
> > To compile this driver as a module, choose M here: the
> > module will be called prestera.
> > +
> > +config PRESTERA_PCI
> > + tristate "PCI interface driver for Marvell Prestera Switch ASICs family"
> > + depends on PCI && HAS_IOMEM && PRESTERA
>
> > + default m
>
> Even if I have CONFIG_PRESTERA=y, why as a user I must have this as a module?
> If it's a crucial feature, shouldn't it be rather
> default CONFIG_PRESTERA
> ?
The firmware image should be located on rootfs, and in case the rootfs
should be mounted later the pci driver can't pick this up when
statically compiled so I left it as 'm' by default.
>
> > + help
> > + This is implementation of PCI interface support for Marvell Prestera
> > + Switch ASICs family.
> > +
> > + To compile this driver as a module, choose M here: the
> > + module will be called prestera_pci.
> > diff --git a/drivers/net/ethernet/marvell/prestera/Makefile b/drivers/net/ethernet/marvell/prestera/Makefile
> > index 610d75032b78..2146714eab21 100644
> > --- a/drivers/net/ethernet/marvell/prestera/Makefile
> > +++ b/drivers/net/ethernet/marvell/prestera/Makefile
> > @@ -2,3 +2,5 @@
> > obj-$(CONFIG_PRESTERA) += prestera.o
> > prestera-objs := prestera_main.o prestera_hw.o prestera_dsa.o \
> > prestera_rxtx.o
> > +
> > +obj-$(CONFIG_PRESTERA_PCI) += prestera_pci.o
> > diff --git a/drivers/net/ethernet/marvell/prestera/prestera_pci.c b/drivers/net/ethernet/marvell/prestera/prestera_pci.c
> > new file mode 100644
> > index 000000000000..cc8446c6b4d9
> > --- /dev/null
> > +++ b/drivers/net/ethernet/marvell/prestera/prestera_pci.c
> > @@ -0,0 +1,823 @@
> > +// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
> > +/* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved */
> > +
> > +#include <linux/module.h>
> > +#include <linux/kernel.h>
> > +#include <linux/device.h>
> > +#include <linux/pci.h>
> > +#include <linux/circ_buf.h>
> > +#include <linux/firmware.h>
> > +#include <linux/iopoll.h>
>
> Perhaps keep them in order?
>
> > +
> > +#include "prestera.h"
> > +
> > +#define PRESTERA_MSG_MAX_SIZE 1500
> > +
> > +#define PRESTERA_SUPP_FW_MAJ_VER 2
> > +#define PRESTERA_SUPP_FW_MIN_VER 0
> > +
> > +#define PRESTERA_FW_PATH \
> > + "mrvl/prestera/mvsw_prestera_fw-v" \
> > + __stringify(PRESTERA_SUPP_FW_MAJ_VER) \
> > + "." __stringify(PRESTERA_SUPP_FW_MIN_VER) ".img"
>
> Wouldn't it be better to see this in the C code?
I have no strong opinion on this, but looks like macro is enough for
this statically defined versioning.
>
> Also this is a PATH and a filename.
>
> > +
> > +#define PRESTERA_FW_HDR_MAGIC 0x351D9D06
>
> > +#define PRESTERA_FW_DL_TIMEOUT 50000
>
> Use the suffix, are they "us" or "ns" or "s" or what?
>
> > +#define PRESTERA_FW_BLK_SZ 1024
> > +
> > +#define PRESTERA_FW_VER_MAJ_MUL 1000000
> > +#define PRESTERA_FW_VER_MIN_MUL 1000
> > +
> > +#define PRESTERA_FW_VER_MAJ(v) ((v) / PRESTERA_FW_VER_MAJ_MUL)
> > +
> > +#define PRESTERA_FW_VER_MIN(v) \
> > + (((v) - (PRESTERA_FW_VER_MAJ(v) * PRESTERA_FW_VER_MAJ_MUL)) / \
> > + PRESTERA_FW_VER_MIN_MUL)
> > +
> > +#define PRESTERA_FW_VER_PATCH(v) \
> > + ((v) - (PRESTERA_FW_VER_MAJ(v) * PRESTERA_FW_VER_MAJ_MUL) - \
> > + (PRESTERA_FW_VER_MIN(v) * PRESTERA_FW_VER_MIN_MUL))
> > +
> > +struct prestera_fw_header {
> > + __be32 magic_number;
> > + __be32 version_value;
> > + u8 reserved[8];
>
> > +} __packed;
>
> Hmm... How __packed make a difference here?
>
> > +struct prestera_ldr_regs {
> > + u32 ldr_ready;
> > + u32 pad1;
> > +
> > + u32 ldr_img_size;
> > + u32 ldr_ctl_flags;
> > +
> > + u32 ldr_buf_offs;
> > + u32 ldr_buf_size;
> > +
> > + u32 ldr_buf_rd;
> > + u32 pad2;
> > + u32 ldr_buf_wr;
> > +
> > + u32 ldr_status;
> > +} __packed __aligned(4);
> > +
> > +#define PRESTERA_LDR_REG_OFFSET(f) offsetof(struct prestera_ldr_regs, f)
> > +
> > +#define PRESTERA_LDR_READY_MAGIC 0xf00dfeed
> > +
> > +#define PRESTERA_LDR_STATUS_IMG_DL BIT(0)
> > +#define PRESTERA_LDR_STATUS_START_FW BIT(1)
> > +#define PRESTERA_LDR_STATUS_INVALID_IMG BIT(2)
> > +#define PRESTERA_LDR_STATUS_NOMEM BIT(3)
> > +
> > +#define PRESTERA_LDR_REG_BASE(fw) ((fw)->ldr_regs)
> > +#define PRESTERA_LDR_REG_ADDR(fw, reg) (PRESTERA_LDR_REG_BASE(fw) + (reg))
> > +
> > +#define prestera_ldr_write(fw, reg, val) \
> > + writel(val, PRESTERA_LDR_REG_ADDR(fw, reg))
> > +#define prestera_ldr_read(fw, reg) \
> > + readl(PRESTERA_LDR_REG_ADDR(fw, reg))
> > +
> > +/* fw loader registers */
> > +#define PRESTERA_LDR_READY_REG PRESTERA_LDR_REG_OFFSET(ldr_ready)
> > +#define PRESTERA_LDR_IMG_SIZE_REG PRESTERA_LDR_REG_OFFSET(ldr_img_size)
> > +#define PRESTERA_LDR_CTL_REG PRESTERA_LDR_REG_OFFSET(ldr_ctl_flags)
> > +#define PRESTERA_LDR_BUF_SIZE_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_size)
> > +#define PRESTERA_LDR_BUF_OFFS_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_offs)
> > +#define PRESTERA_LDR_BUF_RD_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_rd)
> > +#define PRESTERA_LDR_BUF_WR_REG PRESTERA_LDR_REG_OFFSET(ldr_buf_wr)
> > +#define PRESTERA_LDR_STATUS_REG PRESTERA_LDR_REG_OFFSET(ldr_status)
> > +
> > +#define PRESTERA_LDR_CTL_DL_START BIT(0)
> > +
> > +#define PRESTERA_EVT_QNUM_MAX 4
> > +
> > +struct prestera_fw_evtq_regs {
> > + u32 rd_idx;
> > + u32 pad1;
> > + u32 wr_idx;
> > + u32 pad2;
> > + u32 offs;
> > + u32 len;
> > +};
> > +
> > +struct prestera_fw_regs {
> > + u32 fw_ready;
> > + u32 pad;
> > + u32 cmd_offs;
> > + u32 cmd_len;
> > + u32 evt_offs;
> > + u32 evt_qnum;
> > +
> > + u32 cmd_req_ctl;
> > + u32 cmd_req_len;
> > + u32 cmd_rcv_ctl;
> > + u32 cmd_rcv_len;
> > +
> > + u32 fw_status;
> > + u32 rx_status;
> > +
> > + struct prestera_fw_evtq_regs evtq_list[PRESTERA_EVT_QNUM_MAX];
> > +};
>
> > +#define PRESTERA_FW_REG_OFFSET(f) offsetof(struct prestera_fw_regs, f)
>
> > +#define PRESTERA_FW_READY_MAGIC 0xcafebabe
> > +
> > +/* fw registers */
> > +#define PRESTERA_FW_READY_REG PRESTERA_FW_REG_OFFSET(fw_ready)
> > +
> > +#define PRESTERA_CMD_BUF_OFFS_REG PRESTERA_FW_REG_OFFSET(cmd_offs)
> > +#define PRESTERA_CMD_BUF_LEN_REG PRESTERA_FW_REG_OFFSET(cmd_len)
> > +#define PRESTERA_EVT_BUF_OFFS_REG PRESTERA_FW_REG_OFFSET(evt_offs)
> > +#define PRESTERA_EVT_QNUM_REG PRESTERA_FW_REG_OFFSET(evt_qnum)
> > +
> > +#define PRESTERA_CMD_REQ_CTL_REG PRESTERA_FW_REG_OFFSET(cmd_req_ctl)
> > +#define PRESTERA_CMD_REQ_LEN_REG PRESTERA_FW_REG_OFFSET(cmd_req_len)
> > +
> > +#define PRESTERA_CMD_RCV_CTL_REG PRESTERA_FW_REG_OFFSET(cmd_rcv_ctl)
> > +#define PRESTERA_CMD_RCV_LEN_REG PRESTERA_FW_REG_OFFSET(cmd_rcv_len)
> > +#define PRESTERA_FW_STATUS_REG PRESTERA_FW_REG_OFFSET(fw_status)
> > +#define PRESTERA_RX_STATUS_REG PRESTERA_FW_REG_OFFSET(rx_status)
> > +
> > +/* PRESTERA_CMD_REQ_CTL_REG flags */
> > +#define PRESTERA_CMD_F_REQ_SENT BIT(0)
> > +#define PRESTERA_CMD_F_REPL_RCVD BIT(1)
> > +
> > +/* PRESTERA_CMD_RCV_CTL_REG flags */
> > +#define PRESTERA_CMD_F_REPL_SENT BIT(0)
> > +
> > +#define PRESTERA_EVTQ_REG_OFFSET(q, f) \
> > + (PRESTERA_FW_REG_OFFSET(evtq_list) + \
> > + (q) * sizeof(struct prestera_fw_evtq_regs) + \
> > + offsetof(struct prestera_fw_evtq_regs, f))
> > +
> > +#define PRESTERA_EVTQ_RD_IDX_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, rd_idx)
> > +#define PRESTERA_EVTQ_WR_IDX_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, wr_idx)
> > +#define PRESTERA_EVTQ_OFFS_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, offs)
> > +#define PRESTERA_EVTQ_LEN_REG(q) PRESTERA_EVTQ_REG_OFFSET(q, len)
> > +
> > +#define PRESTERA_FW_REG_BASE(fw) ((fw)->dev.ctl_regs)
> > +#define PRESTERA_FW_REG_ADDR(fw, reg) PRESTERA_FW_REG_BASE((fw)) + (reg)
>
> > +#define prestera_fw_write(fw, reg, val) \
> > + writel(val, PRESTERA_FW_REG_ADDR(fw, reg))
> > +#define prestera_fw_read(fw, reg) \
> > + readl(PRESTERA_FW_REG_ADDR(fw, reg))
>
> Why macros? Can't be inline functions? Why?
>
> > +#define PRESTERA_FW_CMD_DEFAULT_WAITMS 30000
> > +#define PRESTERA_FW_READY_WAITMS 20000
>
> _MS in both cases
>
> > +struct prestera_fw_evtq {
> > + u8 __iomem *addr;
> > + size_t len;
> > +};
> > +
> > +struct prestera_fw {
> > + struct workqueue_struct *wq;
> > + struct prestera_device dev;
> > + struct pci_dev *pci_dev;
> > + u8 __iomem *ldr_regs;
> > + u8 __iomem *ldr_ring_buf;
> > + u32 ldr_buf_len;
> > + u32 ldr_wr_idx;
> > + struct mutex cmd_mtx; /* serialize access to dev->send_req */
> > + size_t cmd_mbox_len;
> > + u8 __iomem *cmd_mbox;
> > + struct prestera_fw_evtq evt_queue[PRESTERA_EVT_QNUM_MAX];
> > + u8 evt_qnum;
> > + struct work_struct evt_work;
> > + u8 __iomem *evt_buf;
> > + u8 *evt_msg;
> > +};
> > +
> > +static int prestera_fw_load(struct prestera_fw *fw);
> > +
> > +static u32 prestera_fw_evtq_len(struct prestera_fw *fw, u8 qid)
> > +{
> > + return fw->evt_queue[qid].len;
> > +}
> > +
> > +static u32 prestera_fw_evtq_avail(struct prestera_fw *fw, u8 qid)
> > +{
> > + u32 wr_idx = prestera_fw_read(fw, PRESTERA_EVTQ_WR_IDX_REG(qid));
> > + u32 rd_idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
> > +
> > + return CIRC_CNT(wr_idx, rd_idx, prestera_fw_evtq_len(fw, qid));
> > +}
> > +
> > +static void prestera_fw_evtq_rd_set(struct prestera_fw *fw,
> > + u8 qid, u32 idx)
> > +{
> > + u32 rd_idx = idx & (prestera_fw_evtq_len(fw, qid) - 1);
> > +
> > + prestera_fw_write(fw, PRESTERA_EVTQ_RD_IDX_REG(qid), rd_idx);
> > +}
> > +
> > +static u8 __iomem *prestera_fw_evtq_buf(struct prestera_fw *fw, u8 qid)
> > +{
> > + return fw->evt_queue[qid].addr;
> > +}
> > +
> > +static u32 prestera_fw_evtq_read32(struct prestera_fw *fw, u8 qid)
> > +{
> > + u32 rd_idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
> > + u32 val;
> > +
> > + val = readl(prestera_fw_evtq_buf(fw, qid) + rd_idx);
> > + prestera_fw_evtq_rd_set(fw, qid, rd_idx + 4);
> > + return val;
> > +}
> > +
> > +static ssize_t prestera_fw_evtq_read_buf(struct prestera_fw *fw,
> > + u8 qid, u8 *buf, size_t len)
> > +{
> > + u32 idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
> > + u8 __iomem *evtq_addr = prestera_fw_evtq_buf(fw, qid);
> > + u32 *buf32 = (u32 *)buf;
> > + int i;
>
> > + for (i = 0; i < len / 4; buf32++, i++) {
> > + *buf32 = readl_relaxed(evtq_addr + idx);
> > + idx = (idx + 4) & (prestera_fw_evtq_len(fw, qid) - 1);
> > + }
> > +
> > + prestera_fw_evtq_rd_set(fw, qid, idx);
> > +
> > + return i;
> > +}
> > +
> > +static u8 prestera_fw_evtq_pick(struct prestera_fw *fw)
> > +{
> > + int qid;
> > +
> > + for (qid = 0; qid < fw->evt_qnum; qid++) {
> > + if (prestera_fw_evtq_avail(fw, qid) >= 4)
> > + return qid;
> > + }
> > +
> > + return PRESTERA_EVT_QNUM_MAX;
> > +}
> > +
> > +static void prestera_fw_evt_work_fn(struct work_struct *work)
> > +{
> > + struct prestera_fw *fw;
> > + u8 *msg;
> > + u8 qid;
> > +
> > + fw = container_of(work, struct prestera_fw, evt_work);
> > + msg = fw->evt_msg;
> > +
> > + while ((qid = prestera_fw_evtq_pick(fw)) < PRESTERA_EVT_QNUM_MAX) {
> > + u32 idx;
> > + u32 len;
> > +
> > + len = prestera_fw_evtq_read32(fw, qid);
> > + idx = prestera_fw_read(fw, PRESTERA_EVTQ_RD_IDX_REG(qid));
> > +
> > + WARN_ON(prestera_fw_evtq_avail(fw, qid) < len);
> > +
> > + if (WARN_ON(len > PRESTERA_MSG_MAX_SIZE)) {
> > + prestera_fw_evtq_rd_set(fw, qid, idx + len);
> > + continue;
> > + }
> > +
> > + prestera_fw_evtq_read_buf(fw, qid, msg, len);
> > +
> > + if (fw->dev.recv_msg)
> > + fw->dev.recv_msg(&fw->dev, msg, len);
> > + }
> > +}
> > +
> > +static int prestera_fw_wait_reg32(struct prestera_fw *fw, u32 reg, u32 cmp,
> > + unsigned int waitms)
> > +{
> > + u8 __iomem *addr = PRESTERA_FW_REG_ADDR(fw, reg);
>
> > + u32 val = 0;
>
> Redundant assignment.
>
> > +
> > + return readl_poll_timeout(addr, val, cmp == val, 1000 * 10, waitms * 1000);
> > +}
>
>
> > +static void prestera_pci_copy_to(u8 __iomem *dst, u8 *src, size_t len)
> > +{
> > + u32 __iomem *dst32 = (u32 __iomem *)dst;
> > + u32 *src32 = (u32 *)src;
> > + int i;
> > +
> > + for (i = 0; i < (len / 4); dst32++, src32++, i++)
> > + writel_relaxed(*src32, dst32);
> > +}
> > +
> > +static void prestera_pci_copy_from(u8 *dst, u8 __iomem *src, size_t len)
> > +{
> > + u32 __iomem *src32 = (u32 __iomem *)src;
> > + u32 *dst32 = (u32 *)dst;
> > + int i;
> > +
> > + for (i = 0; i < (len / 4); dst32++, src32++, i++)
> > + *dst32 = readl_relaxed(src32);
> > +}
>
> NIH of memcpy_fromio() / memcpy_toio() ?
>
I am not sure if there will be no issue with < 4 bytes transactions over
PCI bus. I need to check it.
> > +static int prestera_fw_cmd_send(struct prestera_fw *fw,
> > + u8 *in_msg, size_t in_size,
> > + u8 *out_msg, size_t out_size,
> > + unsigned int waitms)
> > +{
> > + u32 ret_size = 0;
>
> > + int err = 0;
>
> Redundant assignment. Check your entire code.
>
> > + if (!waitms)
> > + waitms = PRESTERA_FW_CMD_DEFAULT_WAITMS;
> > +
> > + if (ALIGN(in_size, 4) > fw->cmd_mbox_len)
> > + return -EMSGSIZE;
> > +
> > + /* wait for finish previous reply from FW */
> > + err = prestera_fw_wait_reg32(fw, PRESTERA_CMD_RCV_CTL_REG, 0, 30);
> > + if (err) {
> > + dev_err(fw->dev.dev, "finish reply from FW is timed out\n");
> > + return err;
> > + }
> > +
> > + prestera_fw_write(fw, PRESTERA_CMD_REQ_LEN_REG, in_size);
> > + prestera_pci_copy_to(fw->cmd_mbox, in_msg, in_size);
> > +
> > + prestera_fw_write(fw, PRESTERA_CMD_REQ_CTL_REG, PRESTERA_CMD_F_REQ_SENT);
> > +
> > + /* wait for reply from FW */
> > + err = prestera_fw_wait_reg32(fw, PRESTERA_CMD_RCV_CTL_REG,
> > + PRESTERA_CMD_F_REPL_SENT, waitms);
> > + if (err) {
> > + dev_err(fw->dev.dev, "reply from FW is timed out\n");
> > + goto cmd_exit;
> > + }
> > +
> > + ret_size = prestera_fw_read(fw, PRESTERA_CMD_RCV_LEN_REG);
> > + if (ret_size > out_size) {
> > + dev_err(fw->dev.dev, "ret_size (%u) > out_len(%zu)\n",
> > + ret_size, out_size);
> > + err = -EMSGSIZE;
> > + goto cmd_exit;
> > + }
> > +
> > + prestera_pci_copy_from(out_msg, fw->cmd_mbox + in_size, ret_size);
> > +
> > +cmd_exit:
> > + prestera_fw_write(fw, PRESTERA_CMD_REQ_CTL_REG, PRESTERA_CMD_F_REPL_RCVD);
> > + return err;
> > +}
> > +
> > +static int prestera_fw_send_req(struct prestera_device *dev,
> > + u8 *in_msg, size_t in_size, u8 *out_msg,
> > + size_t out_size, unsigned int waitms)
> > +{
> > + struct prestera_fw *fw;
> > + ssize_t ret;
> > +
> > + fw = container_of(dev, struct prestera_fw, dev);
> > +
> > + mutex_lock(&fw->cmd_mtx);
> > + ret = prestera_fw_cmd_send(fw, in_msg, in_size, out_msg, out_size, waitms);
> > + mutex_unlock(&fw->cmd_mtx);
> > +
> > + return ret;
> > +}
> > +
> > +static int prestera_fw_init(struct prestera_fw *fw)
> > +{
> > + u8 __iomem *base;
> > + int err;
> > + u8 qid;
> > +
> > + fw->dev.send_req = prestera_fw_send_req;
> > + fw->ldr_regs = fw->dev.ctl_regs;
> > +
> > + err = prestera_fw_load(fw);
> > + if (err && err != -ETIMEDOUT)
> > + return err;
> > +
> > + err = prestera_fw_wait_reg32(fw, PRESTERA_FW_READY_REG,
> > + PRESTERA_FW_READY_MAGIC,
> > + PRESTERA_FW_READY_WAITMS);
> > + if (err) {
> > + dev_err(fw->dev.dev, "FW failed to start\n");
> > + return err;
> > + }
> > +
> > + base = fw->dev.ctl_regs;
> > +
> > + fw->cmd_mbox = base + prestera_fw_read(fw, PRESTERA_CMD_BUF_OFFS_REG);
> > + fw->cmd_mbox_len = prestera_fw_read(fw, PRESTERA_CMD_BUF_LEN_REG);
> > + mutex_init(&fw->cmd_mtx);
> > +
> > + fw->evt_buf = base + prestera_fw_read(fw, PRESTERA_EVT_BUF_OFFS_REG);
> > + fw->evt_qnum = prestera_fw_read(fw, PRESTERA_EVT_QNUM_REG);
> > + fw->evt_msg = kmalloc(PRESTERA_MSG_MAX_SIZE, GFP_KERNEL);
> > + if (!fw->evt_msg)
> > + return -ENOMEM;
> > +
> > + for (qid = 0; qid < fw->evt_qnum; qid++) {
> > + u32 offs = prestera_fw_read(fw, PRESTERA_EVTQ_OFFS_REG(qid));
> > + struct prestera_fw_evtq *evtq = &fw->evt_queue[qid];
> > +
> > + evtq->len = prestera_fw_read(fw, PRESTERA_EVTQ_LEN_REG(qid));
> > + evtq->addr = fw->evt_buf + offs;
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +static void prestera_fw_uninit(struct prestera_fw *fw)
> > +{
> > + kfree(fw->evt_msg);
> > +}
> > +
> > +static irqreturn_t prestera_pci_irq_handler(int irq, void *dev_id)
> > +{
> > + struct prestera_fw *fw = dev_id;
> > +
> > + if (prestera_fw_read(fw, PRESTERA_RX_STATUS_REG)) {
> > + prestera_fw_write(fw, PRESTERA_RX_STATUS_REG, 0);
> > +
> > + if (fw->dev.recv_pkt)
> > + fw->dev.recv_pkt(&fw->dev);
> > + }
> > +
> > + queue_work(fw->wq, &fw->evt_work);
> > +
> > + return IRQ_HANDLED;
> > +}
> > +
> > +static int prestera_ldr_wait_reg32(struct prestera_fw *fw,
> > + u32 reg, u32 cmp, unsigned int waitms)
> > +{
> > + u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, reg);
> > + u32 val = 0;
> > +
> > + return readl_poll_timeout(addr, val, cmp == val, 1000 * 10, waitms * 1000);
> > +}
> > +
> > +static u32 prestera_ldr_wait_buf(struct prestera_fw *fw, size_t len)
> > +{
> > + u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, PRESTERA_LDR_BUF_RD_REG);
> > + u32 buf_len = fw->ldr_buf_len;
> > + u32 wr_idx = fw->ldr_wr_idx;
> > + u32 rd_idx = 0;
> > +
> > + return readl_poll_timeout(addr, rd_idx,
> > + CIRC_SPACE(wr_idx, rd_idx, buf_len) >= len,
> > + 1000, 100 * 1000);
> > + return 0;
> > +}
> > +
> > +static int prestera_ldr_wait_dl_finish(struct prestera_fw *fw)
> > +{
> > + u8 __iomem *addr = PRESTERA_LDR_REG_ADDR(fw, PRESTERA_LDR_STATUS_REG);
> > + unsigned int waitus = PRESTERA_FW_DL_TIMEOUT * 1000;
> > + unsigned long mask = ~(PRESTERA_LDR_STATUS_IMG_DL);
> > + u32 val = 0;
> > + int err;
> > +
> > + err = readl_poll_timeout(addr, val, val & mask, 1000 * 10, waitus);
> > + if (err) {
> > + dev_err(fw->dev.dev, "Timeout to load FW img [state=%d]",
> > + prestera_ldr_read(fw, PRESTERA_LDR_STATUS_REG));
> > + return err;
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +static void prestera_ldr_wr_idx_move(struct prestera_fw *fw, unsigned int n)
> > +{
> > + fw->ldr_wr_idx = (fw->ldr_wr_idx + (n)) & (fw->ldr_buf_len - 1);
> > +}
> > +
> > +static void prestera_ldr_wr_idx_commit(struct prestera_fw *fw)
> > +{
> > + prestera_ldr_write(fw, PRESTERA_LDR_BUF_WR_REG, fw->ldr_wr_idx);
> > +}
> > +
> > +static u8 __iomem *prestera_ldr_wr_ptr(struct prestera_fw *fw)
> > +{
> > + return fw->ldr_ring_buf + fw->ldr_wr_idx;
> > +}
> > +
> > +static int prestera_ldr_send(struct prestera_fw *fw, const u8 *buf, size_t len)
> > +{
> > + int err;
> > + int i;
> > +
> > + err = prestera_ldr_wait_buf(fw, len);
> > + if (err) {
> > + dev_err(fw->dev.dev, "failed wait for sending firmware\n");
> > + return err;
> > + }
> > +
> > + for (i = 0; i < len; i += 4) {
> > + writel_relaxed(*(u32 *)(buf + i), prestera_ldr_wr_ptr(fw));
> > + prestera_ldr_wr_idx_move(fw, 4);
> > + }
> > +
> > + prestera_ldr_wr_idx_commit(fw);
> > + return 0;
> > +}
> > +
> > +static int prestera_ldr_fw_send(struct prestera_fw *fw,
> > + const char *img, u32 fw_size)
> > +{
> > + u32 status;
> > + u32 pos;
> > + int err;
> > +
> > + err = prestera_ldr_wait_reg32(fw, PRESTERA_LDR_STATUS_REG,
> > + PRESTERA_LDR_STATUS_IMG_DL, 5 * 1000);
> > + if (err) {
> > + dev_err(fw->dev.dev, "Loader is not ready to load image\n");
> > + return err;
> > + }
> > +
> > + for (pos = 0; pos < fw_size; pos += PRESTERA_FW_BLK_SZ) {
> > + if (pos + PRESTERA_FW_BLK_SZ > fw_size)
> > + break;
> > +
> > + err = prestera_ldr_send(fw, img + pos, PRESTERA_FW_BLK_SZ);
> > + if (err)
> > + return err;
> > + }
> > +
> > + if (pos < fw_size) {
> > + err = prestera_ldr_send(fw, img + pos, fw_size - pos);
> > + if (err)
> > + return err;
> > + }
> > +
> > + err = prestera_ldr_wait_dl_finish(fw);
> > + if (err)
> > + return err;
> > +
> > + status = prestera_ldr_read(fw, PRESTERA_LDR_STATUS_REG);
>
> > + if (status != PRESTERA_LDR_STATUS_START_FW) {
>
> What is the point?
>
> > + switch (status) {
> > + case PRESTERA_LDR_STATUS_INVALID_IMG:
> > + dev_err(fw->dev.dev, "FW img has bad CRC\n");
> > + return -EINVAL;
> > + case PRESTERA_LDR_STATUS_NOMEM:
> > + dev_err(fw->dev.dev, "Loader has no enough mem\n");
> > + return -ENOMEM;
> > + default:
> > + break;
> > + }
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +static void prestera_fw_rev_parse(const struct prestera_fw_header *hdr,
> > + struct prestera_fw_rev *rev)
> > +{
> > + u32 version = be32_to_cpu(hdr->version_value);
> > +
> > + rev->maj = PRESTERA_FW_VER_MAJ(version);
> > + rev->min = PRESTERA_FW_VER_MIN(version);
> > + rev->sub = PRESTERA_FW_VER_PATCH(version);
> > +}
> > +
> > +static int prestera_fw_rev_check(struct prestera_fw *fw)
> > +{
> > + struct prestera_fw_rev *rev = &fw->dev.fw_rev;
> > + u16 maj_supp = PRESTERA_SUPP_FW_MAJ_VER;
> > + u16 min_supp = PRESTERA_SUPP_FW_MIN_VER;
> > +
>
> > + if (rev->maj == maj_supp && rev->min >= min_supp)
> > + return 0;
>
> Why not traditional pattern
>
> if (err) {
> ...
> }
At least for me it looks simpler when to check which version is
correct.
>
> ...
> return 0;
>
> ?
>
> > + dev_err(fw->dev.dev, "Driver supports FW version only '%u.%u.x'",
> > + PRESTERA_SUPP_FW_MAJ_VER, PRESTERA_SUPP_FW_MIN_VER);
> > +
> > + return -EINVAL;
> > +}
> > +
> > +static int prestera_fw_hdr_parse(struct prestera_fw *fw,
> > + const struct firmware *img)
> > +{
> > + struct prestera_fw_header *hdr = (struct prestera_fw_header *)img->data;
> > + struct prestera_fw_rev *rev = &fw->dev.fw_rev;
> > + u32 magic;
> > +
> > + magic = be32_to_cpu(hdr->magic_number);
> > + if (magic != PRESTERA_FW_HDR_MAGIC) {
> > + dev_err(fw->dev.dev, "FW img hdr magic is invalid");
> > + return -EINVAL;
> > + }
> > +
> > + prestera_fw_rev_parse(hdr, rev);
> > +
> > + dev_info(fw->dev.dev, "FW version '%u.%u.%u'\n",
> > + rev->maj, rev->min, rev->sub);
> > +
> > + return prestera_fw_rev_check(fw);
> > +}
> > +
> > +static int prestera_fw_load(struct prestera_fw *fw)
> > +{
> > + size_t hlen = sizeof(struct prestera_fw_header);
> > + const struct firmware *f;
> > + int err;
> > +
> > + err = prestera_ldr_wait_reg32(fw, PRESTERA_LDR_READY_REG,
> > + PRESTERA_LDR_READY_MAGIC, 5 * 1000);
> > + if (err) {
> > + dev_err(fw->dev.dev, "waiting for FW loader is timed out");
> > + return err;
> > + }
> > +
> > + fw->ldr_ring_buf = fw->ldr_regs +
> > + prestera_ldr_read(fw, PRESTERA_LDR_BUF_OFFS_REG);
> > +
> > + fw->ldr_buf_len =
> > + prestera_ldr_read(fw, PRESTERA_LDR_BUF_SIZE_REG);
> > +
> > + fw->ldr_wr_idx = 0;
> > +
> > + err = request_firmware_direct(&f, PRESTERA_FW_PATH, &fw->pci_dev->dev);
> > + if (err) {
> > + dev_err(fw->dev.dev, "failed to request firmware file\n");
> > + return err;
> > + }
> > +
> > + if (!IS_ALIGNED(f->size, 4)) {
> > + dev_err(fw->dev.dev, "FW image file is not aligned");
> > + release_firmware(f);
> > + return -EINVAL;
> > + }
> > +
> > + err = prestera_fw_hdr_parse(fw, f);
> > + if (err) {
> > + dev_err(fw->dev.dev, "FW image header is invalid\n");
> > + release_firmware(f);
> > + return err;
> > + }
> > +
> > + prestera_ldr_write(fw, PRESTERA_LDR_IMG_SIZE_REG, f->size - hlen);
> > + prestera_ldr_write(fw, PRESTERA_LDR_CTL_REG, PRESTERA_LDR_CTL_DL_START);
> > +
>
> > + dev_info(fw->dev.dev, "Loading prestera FW image ...");
>
> Makes sense to see the file which is being used as a FW image.
>
> > + err = prestera_ldr_fw_send(fw, f->data + hlen, f->size - hlen);
> > +
> > + release_firmware(f);
> > + return err;
> > +}
> > +
> > +static int prestera_pci_probe(struct pci_dev *pdev,
> > + const struct pci_device_id *id)
> > +{
> > + const char *driver_name = pdev->driver->name;
> > + u8 __iomem *ctl_addr, *pp_addr;
> > + struct prestera_fw *fw;
> > + int err;
> > +
> > + err = pci_enable_device(pdev);
>
> pcim_enable_device()
>
> > + if (err)
> > + return err;
> > +
>
> > + err = pci_request_regions(pdev, driver_name);
> > + if (err) {
> > + dev_err(&pdev->dev, "pci_request_regions failed\n");
> > + goto err_pci_request_regions;
> > + }
>
> (1)
>
> > +
> > + if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(30))) {
> > + dev_err(&pdev->dev, "fail to set DMA mask\n");
> > + goto err_dma_mask;
> > + }
> > +
> > + ctl_addr = pci_ioremap_bar(pdev, 2);
> > + if (!ctl_addr) {
> > + dev_err(&pdev->dev, "ioremap failed\n");
> > + err = -EIO;
> > + goto err_ctl_ioremap;
> > + }
> > +
> > + pp_addr = pci_ioremap_bar(pdev, 4);
> > + if (!pp_addr) {
> > + dev_err(&pdev->dev, "ioremap failed\n");
> > + err = -EIO;
> > + goto err_pp_ioremap;
> > + }
>
> (2)
>
> (1) and (2) can be replaced with
> pcim_ioremap_regions()
> followed by pcim_iomap_table[2] / [4]
>
> And magic 2 & 4 should be defined.
>
> > + pci_set_master(pdev);
> > + fw = kzalloc(sizeof(*fw), GFP_KERNEL);
>
> devm_kzalloc() ?
>
> > + if (!fw) {
> > + err = -ENOMEM;
> > + goto err_pci_dev_alloc;
> > + }
> > +
> > + fw->pci_dev = pdev;
> > + fw->dev.dev = &pdev->dev;
> > + fw->dev.ctl_regs = ctl_addr;
> > + fw->dev.pp_regs = pp_addr;
> > +
> > + pci_set_drvdata(pdev, fw);
> > +
> > + err = prestera_fw_init(fw);
> > + if (err)
> > + goto err_prestera_fw_init;
> > +
> > + dev_info(fw->dev.dev, "Switch FW is ready\n");
> > +
> > + fw->wq = alloc_workqueue("prestera_fw_wq", WQ_HIGHPRI, 1);
> > + if (!fw->wq)
> > + goto err_wq_alloc;
> > +
> > + INIT_WORK(&fw->evt_work, prestera_fw_evt_work_fn);
> > +
> > + err = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
> > + if (err < 0) {
> > + dev_err(&pdev->dev, "MSI IRQ init failed\n");
> > + goto err_irq_alloc;
> > + }
> > +
> > + err = request_irq(pci_irq_vector(pdev, 0), prestera_pci_irq_handler,
> > + 0, driver_name, fw);
> > + if (err) {
> > + dev_err(&pdev->dev, "fail to request IRQ\n");
> > + goto err_request_irq;
> > + }
> > +
> > + err = prestera_device_register(&fw->dev);
> > + if (err)
> > + goto err_prestera_dev_register;
> > +
> > + return 0;
> > +
> > +err_prestera_dev_register:
> > + free_irq(pci_irq_vector(pdev, 0), fw);
> > +err_request_irq:
> > + pci_free_irq_vectors(pdev);
> > +err_irq_alloc:
> > + destroy_workqueue(fw->wq);
> > +err_wq_alloc:
> > + prestera_fw_uninit(fw);
> > +err_prestera_fw_init:
> > + kfree(fw);
> > +err_pci_dev_alloc:
> > + iounmap(pp_addr);
> > +err_pp_ioremap:
> > + iounmap(ctl_addr);
> > +err_ctl_ioremap:
> > +err_dma_mask:
> > + pci_release_regions(pdev);
> > +err_pci_request_regions:
> > + pci_disable_device(pdev);
> > + return err;
> > +}
> > +
> > +static void prestera_pci_remove(struct pci_dev *pdev)
> > +{
> > + struct prestera_fw *fw = pci_get_drvdata(pdev);
> > + u8 __iomem *ctl_addr = fw->dev.ctl_regs;
> > + u8 __iomem *pp_addr = fw->dev.pp_regs;
> > +
> > + prestera_device_unregister(&fw->dev);
> > + free_irq(pci_irq_vector(pdev, 0), fw);
> > + pci_free_irq_vectors(pdev);
> > + destroy_workqueue(fw->wq);
> > + prestera_fw_uninit(fw);
> > + kfree(fw);
> > + iounmap(pp_addr);
> > + iounmap(ctl_addr);
> > + pci_release_regions(pdev);
> > + pci_disable_device(pdev);
> > +}
> > +
> > +static const struct pci_device_id prestera_pci_devices[] = {
> > + { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0xC804) },
> > + { }
> > +};
> > +MODULE_DEVICE_TABLE(pci, prestera_pci_devices);
> > +
> > +static struct pci_driver prestera_pci_driver = {
> > + .name = "Prestera DX",
> > + .id_table = prestera_pci_devices,
> > + .probe = prestera_pci_probe,
> > + .remove = prestera_pci_remove,
> > +};
> > +
> > +static int __init prestera_pci_init(void)
> > +{
> > + return pci_register_driver(&prestera_pci_driver);
> > +}
> > +
> > +static void __exit prestera_pci_exit(void)
> > +{
> > + pci_unregister_driver(&prestera_pci_driver);
> > +}
> > +
> > +module_init(prestera_pci_init);
> > +module_exit(prestera_pci_exit);
>
> module_pci_driver()
>
> > +MODULE_LICENSE("Dual BSD/GPL");
> > +MODULE_DESCRIPTION("Marvell Prestera switch PCI interface");
>
> --
> With Best Regards,
> Andy Shevchenko
Thanks Andy for the comments, especially for pcim_ helpers.
Regards,
Vadym Kochan
On Mon, Jul 27, 2020 at 1:55 AM Vadym Kochan <[email protected]> wrote:
> On Sun, Jul 26, 2020 at 01:32:19PM +0300, Andy Shevchenko wrote:
> > On Sat, Jul 25, 2020 at 6:10 PM Vadym Kochan <[email protected]> wrote:
...
For the non-commented I assume you are agree with. Correct?
...
> > > +config PRESTERA_PCI
> > > + tristate "PCI interface driver for Marvell Prestera Switch ASICs family"
> > > + depends on PCI && HAS_IOMEM && PRESTERA
> >
> > > + default m
> >
> > Even if I have CONFIG_PRESTERA=y, why as a user I must have this as a module?
> > If it's a crucial feature, shouldn't it be rather
> > default CONFIG_PRESTERA
> > ?
>
> The firmware image should be located on rootfs, and in case the rootfs
> should be mounted later the pci driver can't pick this up when
> statically compiled so I left it as 'm' by default.
We have for a long time to catch firmware blobs from initrd (initramfs).
default m is very unusual.
...
> > > +#define PRESTERA_FW_PATH \
> > > + "mrvl/prestera/mvsw_prestera_fw-v" \
> > > + __stringify(PRESTERA_SUPP_FW_MAJ_VER) \
> > > + "." __stringify(PRESTERA_SUPP_FW_MIN_VER) ".img"
> >
> > Wouldn't it be better to see this in the C code?
>
> I have no strong opinion on this, but looks like macro is enough for
> this statically defined versioning.
The problem is that you have to bounce your editor to C code then to
macro then to another macro...
(in case you are looking for the code responsible for that)
In many drivers I saw either it's one static line (without those
__stringify(), etc) or done in C code dynamically near to
request_firmware() call.
Maybe you may replace __stringify by explicit characters / strings and
comment how the name was constructed?
#define FW_NAME "patch/to/it/fileX.Y.img"
...
> > > +static void prestera_pci_copy_to(u8 __iomem *dst, u8 *src, size_t len)
> > > +{
> > > + u32 __iomem *dst32 = (u32 __iomem *)dst;
> > > + u32 *src32 = (u32 *)src;
> > > + int i;
> > > +
> > > + for (i = 0; i < (len / 4); dst32++, src32++, i++)
> > > + writel_relaxed(*src32, dst32);
> > > +}
> > > +
> > > +static void prestera_pci_copy_from(u8 *dst, u8 __iomem *src, size_t len)
> > > +{
> > > + u32 __iomem *src32 = (u32 __iomem *)src;
> > > + u32 *dst32 = (u32 *)dst;
> > > + int i;
> > > +
> > > + for (i = 0; i < (len / 4); dst32++, src32++, i++)
> > > + *dst32 = readl_relaxed(src32);
> > > +}
> >
> > NIH of memcpy_fromio() / memcpy_toio() ?
> >
> I am not sure if there will be no issue with < 4 bytes transactions over
> PCI bus. I need to check it.
I didn't get it. You always do 4 byte chunks, so, supply aligned
length to memcpy and you will have the same.
...
> > > +static int prestera_fw_rev_check(struct prestera_fw *fw)
> > > +{
> > > + struct prestera_fw_rev *rev = &fw->dev.fw_rev;
> > > + u16 maj_supp = PRESTERA_SUPP_FW_MAJ_VER;
> > > + u16 min_supp = PRESTERA_SUPP_FW_MIN_VER;
> > > +
> >
> > > + if (rev->maj == maj_supp && rev->min >= min_supp)
> > > + return 0;
> >
> > Why not traditional pattern
> >
> > if (err) {
> > ...
> > }
>
> At least for me it looks simpler when to check which version is
> correct.
OK.
> > ...
> > return 0;
> >
> > ?
> >
> > > + dev_err(fw->dev.dev, "Driver supports FW version only '%u.%u.x'",
> > > + PRESTERA_SUPP_FW_MAJ_VER, PRESTERA_SUPP_FW_MIN_VER);
> > > +
> > > + return -EINVAL;
> > > +}
...
> Thanks Andy for the comments, especially for pcim_ helpers.
You are welcome!
--
With Best Regards,
Andy Shevchenko
Hi Andy,
On Mon, Jul 27, 2020 at 11:04:56AM +0300, Andy Shevchenko wrote:
> On Mon, Jul 27, 2020 at 1:55 AM Vadym Kochan <[email protected]> wrote:
> > On Sun, Jul 26, 2020 at 01:32:19PM +0300, Andy Shevchenko wrote:
> > > On Sat, Jul 25, 2020 at 6:10 PM Vadym Kochan <[email protected]> wrote:
>
> ...
>
> For the non-commented I assume you are agree with. Correct?
>
Yes
> ...
>
> > > > +config PRESTERA_PCI
> > > > + tristate "PCI interface driver for Marvell Prestera Switch ASICs family"
> > > > + depends on PCI && HAS_IOMEM && PRESTERA
> > >
> > > > + default m
> > >
> > > Even if I have CONFIG_PRESTERA=y, why as a user I must have this as a module?
> > > If it's a crucial feature, shouldn't it be rather
> > > default CONFIG_PRESTERA
> > > ?
> >
> > The firmware image should be located on rootfs, and in case the rootfs
> > should be mounted later the pci driver can't pick this up when
> > statically compiled so I left it as 'm' by default.
>
> We have for a long time to catch firmware blobs from initrd (initramfs).
> default m is very unusual.
>
For example drivers/net/ethernet/mellanox/mlxsw/pci.c also uses 'm' as
default, but may be in that case the reason is that there are several
bus implementations - i2c, pci.
> ...
>
> > > > +#define PRESTERA_FW_PATH \
> > > > + "mrvl/prestera/mvsw_prestera_fw-v" \
> > > > + __stringify(PRESTERA_SUPP_FW_MAJ_VER) \
> > > > + "." __stringify(PRESTERA_SUPP_FW_MIN_VER) ".img"
> > >
> > > Wouldn't it be better to see this in the C code?
> >
> > I have no strong opinion on this, but looks like macro is enough for
> > this statically defined versioning.
>
> The problem is that you have to bounce your editor to C code then to
> macro then to another macro...
> (in case you are looking for the code responsible for that)
> In many drivers I saw either it's one static line (without those
> __stringify(), etc) or done in C code dynamically near to
> request_firmware() call.
>
> Maybe you may replace __stringify by explicit characters / strings and
> comment how the name was constructed?
>
> #define FW_NAME "patch/to/it/fileX.Y.img"
>
I used snprintf, and now it looks simpler.
> ...
>
> > > > +static void prestera_pci_copy_to(u8 __iomem *dst, u8 *src, size_t len)
> > > > +{
> > > > + u32 __iomem *dst32 = (u32 __iomem *)dst;
> > > > + u32 *src32 = (u32 *)src;
> > > > + int i;
> > > > +
> > > > + for (i = 0; i < (len / 4); dst32++, src32++, i++)
> > > > + writel_relaxed(*src32, dst32);
> > > > +}
> > > > +
> > > > +static void prestera_pci_copy_from(u8 *dst, u8 __iomem *src, size_t len)
> > > > +{
> > > > + u32 __iomem *src32 = (u32 __iomem *)src;
> > > > + u32 *dst32 = (u32 *)dst;
> > > > + int i;
> > > > +
> > > > + for (i = 0; i < (len / 4); dst32++, src32++, i++)
> > > > + *dst32 = readl_relaxed(src32);
> > > > +}
> > >
> > > NIH of memcpy_fromio() / memcpy_toio() ?
> > >
> > I am not sure if there will be no issue with < 4 bytes transactions over
> > PCI bus. I need to check it.
>
> I didn't get it. You always do 4 byte chunks, so, supply aligned
> length to memcpy and you will have the same.
>
> ...
Yes, I converted code to use these helpers.
>
> > > > +static int prestera_fw_rev_check(struct prestera_fw *fw)
> > > > +{
> > > > + struct prestera_fw_rev *rev = &fw->dev.fw_rev;
> > > > + u16 maj_supp = PRESTERA_SUPP_FW_MAJ_VER;
> > > > + u16 min_supp = PRESTERA_SUPP_FW_MIN_VER;
> > > > +
> > >
> > > > + if (rev->maj == maj_supp && rev->min >= min_supp)
> > > > + return 0;
> > >
> > > Why not traditional pattern
> > >
> > > if (err) {
> > > ...
> > > }
> >
> > At least for me it looks simpler when to check which version is
> > correct.
>
> OK.
>
> > > ...
> > > return 0;
> > >
> > > ?
> > >
> > > > + dev_err(fw->dev.dev, "Driver supports FW version only '%u.%u.x'",
> > > > + PRESTERA_SUPP_FW_MAJ_VER, PRESTERA_SUPP_FW_MIN_VER);
> > > > +
> > > > + return -EINVAL;
> > > > +}
>
> ...
>
> > Thanks Andy for the comments, especially for pcim_ helpers.
>
> You are welcome!
>
> --
> With Best Regards,
> Andy Shevchenko
Thanks!
On Mon, Jul 27, 2020 at 12:34 PM Vadym Kochan <[email protected]> wrote:
> On Mon, Jul 27, 2020 at 11:04:56AM +0300, Andy Shevchenko wrote:
> > On Mon, Jul 27, 2020 at 1:55 AM Vadym Kochan <[email protected]> wrote:
> > > On Sun, Jul 26, 2020 at 01:32:19PM +0300, Andy Shevchenko wrote:
> > > > On Sat, Jul 25, 2020 at 6:10 PM Vadym Kochan <[email protected]> wrote:
...
> > > > > +config PRESTERA_PCI
> > > > > + tristate "PCI interface driver for Marvell Prestera Switch ASICs family"
> > > > > + depends on PCI && HAS_IOMEM && PRESTERA
> > > >
> > > > > + default m
> > > >
> > > > Even if I have CONFIG_PRESTERA=y, why as a user I must have this as a module?
> > > > If it's a crucial feature, shouldn't it be rather
> > > > default CONFIG_PRESTERA
> > > > ?
> > >
> > > The firmware image should be located on rootfs, and in case the rootfs
> > > should be mounted later the pci driver can't pick this up when
> > > statically compiled so I left it as 'm' by default.
> >
> > We have for a long time to catch firmware blobs from initrd (initramfs).
> > default m is very unusual.
> >
> For example drivers/net/ethernet/mellanox/mlxsw/pci.c also uses 'm' as
> default, but may be in that case the reason is that there are several
> bus implementations - i2c, pci.
% git grep -n -w 'default m if' -- $(git ls-files | grep Kconfig) | wc -l
240
% git grep -n -w 'default m' -- $(git ls-files | grep Kconfig) | wc -l
293
% git grep -n -w 'default' -- $(git ls-files | grep Kconfig) | wc -l
5226
So, basically 'default m' cases are ~1% (53) of all default cases
(5226) in the tree.
...
> > Maybe you may replace __stringify by explicit characters / strings and
> > comment how the name was constructed?
> >
> > #define FW_NAME "patch/to/it/fileX.Y.img"
> >
> I used snprintf, and now it looks simpler.
Works for me!
--
With Best Regards,
Andy Shevchenko