* [PATCH 0/5] net: Hisilicon Network Subsystem support
@ 2015-08-14 10:30 Kenneth Lee
2015-08-14 10:30 ` [PATCH 1/5] net: add Hisilicon Network Subsystem support (config and documents) Kenneth Lee
` (3 more replies)
0 siblings, 4 replies; 15+ messages in thread
From: Kenneth Lee @ 2015-08-14 10:30 UTC (permalink / raw)
To: linux-arm-kernel
This patchset add Hisilicon Network Subsystem support. The subsystem
provides a long term developing network accelerate engine with ring buffer
interface. The network interface can be used as standard ethernet network
interface card or be made use by a network application with decoded L2 to
L4 data.
The patchset is porting from some internal-use drivers, it is tested and
working fine with the hardware. But some detail design is not that good.
But we want to know if the community can accept the structure/arch before
refining it. Thank you.
Kenneth Lee (5):
net: add Hisilicon Network Subsystem support (config and documents)
net: add Hisilicon Network Subsystem hnae framework support
net: add Hisilicon Network Subsystem MDIO support
net: add Hisilicon Network Subsystem DSAF support
net: add Hisilicon Network Subsystem basic ethernet support
.../devicetree/bindings/net/hisilicon-hns-dsaf.txt | 40 +
.../devicetree/bindings/net/hisilicon-hns-mdio.txt | 22 +
.../devicetree/bindings/net/hisilicon-hns-nic.txt | 14 +
arch/arm64/boot/dts/hisilicon/hip05_hns.dtsi | 197 ++
drivers/net/ethernet/hisilicon/Kconfig | 33 +-
drivers/net/ethernet/hisilicon/Makefile | 1 +
drivers/net/ethernet/hisilicon/hns/Makefile | 15 +
drivers/net/ethernet/hisilicon/hns/hnae.c | 494 ++++
drivers/net/ethernet/hisilicon/hns/hnae.h | 582 +++++
drivers/net/ethernet/hisilicon/hns/hns_ae_adapt.c | 766 ++++++
drivers/net/ethernet/hisilicon/hns/hns_dsaf_gmac.c | 705 +++++
drivers/net/ethernet/hisilicon/hns/hns_dsaf_gmac.h | 45 +
drivers/net/ethernet/hisilicon/hns/hns_dsaf_mac.c | 942 +++++++
drivers/net/ethernet/hisilicon/hns/hns_dsaf_mac.h | 462 ++++
drivers/net/ethernet/hisilicon/hns/hns_dsaf_main.c | 2681 ++++++++++++++++++++
drivers/net/ethernet/hisilicon/hns/hns_dsaf_main.h | 438 ++++
drivers/net/ethernet/hisilicon/hns/hns_dsaf_misc.c | 311 +++
drivers/net/ethernet/hisilicon/hns/hns_dsaf_misc.h | 45 +
drivers/net/ethernet/hisilicon/hns/hns_dsaf_ppe.c | 582 +++++
drivers/net/ethernet/hisilicon/hns/hns_dsaf_ppe.h | 105 +
drivers/net/ethernet/hisilicon/hns/hns_dsaf_rcb.c | 972 +++++++
drivers/net/ethernet/hisilicon/hns/hns_dsaf_rcb.h | 136 +
drivers/net/ethernet/hisilicon/hns/hns_dsaf_reg.h | 958 +++++++
.../net/ethernet/hisilicon/hns/hns_dsaf_xgmac.c | 826 ++++++
.../net/ethernet/hisilicon/hns/hns_dsaf_xgmac.h | 15 +
drivers/net/ethernet/hisilicon/hns/hns_enet.c | 1552 +++++++++++
drivers/net/ethernet/hisilicon/hns/hns_enet.h | 81 +
drivers/net/ethernet/hisilicon/hns/hns_ethtool.c | 1174 +++++++++
drivers/net/ethernet/hisilicon/hns/hns_mdio_main.c | 597 +++++
29 files changed, 14790 insertions(+), 1 deletion(-)
create mode 100644 Documentation/devicetree/bindings/net/hisilicon-hns-dsaf.txt
create mode 100644 Documentation/devicetree/bindings/net/hisilicon-hns-mdio.txt
create mode 100644 Documentation/devicetree/bindings/net/hisilicon-hns-nic.txt
create mode 100644 arch/arm64/boot/dts/hisilicon/hip05_hns.dtsi
create mode 100644 drivers/net/ethernet/hisilicon/hns/Makefile
create mode 100644 drivers/net/ethernet/hisilicon/hns/hnae.c
create mode 100644 drivers/net/ethernet/hisilicon/hns/hnae.h
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_ae_adapt.c
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_dsaf_gmac.c
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_dsaf_gmac.h
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_dsaf_mac.c
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_dsaf_mac.h
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_dsaf_main.c
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_dsaf_main.h
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_dsaf_misc.c
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_dsaf_misc.h
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_dsaf_ppe.c
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_dsaf_ppe.h
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_dsaf_rcb.c
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_dsaf_rcb.h
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_dsaf_reg.h
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_dsaf_xgmac.c
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_dsaf_xgmac.h
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_enet.c
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_enet.h
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_ethtool.c
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_mdio_main.c
--
1.9.1
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 1/5] net: add Hisilicon Network Subsystem support (config and documents)
2015-08-14 10:30 [PATCH 0/5] net: Hisilicon Network Subsystem support Kenneth Lee
@ 2015-08-14 10:30 ` Kenneth Lee
2015-08-14 20:49 ` Arnd Bergmann
2015-08-14 10:30 ` [PATCH 2/5] net: add Hisilicon Network Subsystem hnae framework support Kenneth Lee
` (2 subsequent siblings)
3 siblings, 1 reply; 15+ messages in thread
From: Kenneth Lee @ 2015-08-14 10:30 UTC (permalink / raw)
To: linux-arm-kernel
The Hisilicon Network Subsystem is a long term evolution IP which is
supposed to be used in Hisilicon ICT SoC. The IP, which is called hns for
short, is a TCP/IP acceleration engine, which can directly decode TCP/IP
stream and distribute them to different ring buffers.
HNS can be configured to work on different mode for different scenario.
This patch make use only some of the mode to make it as standard ethernet
NIC. The other mode will be added soon.
The whole function has 4 kernel sub-modules:
hnae: the HNS acceleration engine framework. It provides a abstract
interface between the engine and the upper layers which make use of the
engine by ring buffer.
hns_enet_drv: a standard ethernet driver that base on the ring buffer.
hns_dsaf: one of the implementation of HNS acceleration engine, which is
applied on Hililicon P660, Hi1610 and other later-on SoCs
hns_mdio: the mdio control to the PHY, used by acceleration engine
This submit add basic config and documents
Signed-off-by: Kenneth Lee <liguozhu@huawei.com>
Signed-off-by: Yisen Zhuang <Yisen.Zhuang@huawei.com>
---
.../devicetree/bindings/net/hisilicon-hns-dsaf.txt | 40 +++++
.../devicetree/bindings/net/hisilicon-hns-mdio.txt | 22 +++
.../devicetree/bindings/net/hisilicon-hns-nic.txt | 14 ++
arch/arm64/boot/dts/hisilicon/hip05_hns.dtsi | 197 +++++++++++++++++++++
4 files changed, 273 insertions(+)
create mode 100644 Documentation/devicetree/bindings/net/hisilicon-hns-dsaf.txt
create mode 100644 Documentation/devicetree/bindings/net/hisilicon-hns-mdio.txt
create mode 100644 Documentation/devicetree/bindings/net/hisilicon-hns-nic.txt
create mode 100644 arch/arm64/boot/dts/hisilicon/hip05_hns.dtsi
diff --git a/Documentation/devicetree/bindings/net/hisilicon-hns-dsaf.txt b/Documentation/devicetree/bindings/net/hisilicon-hns-dsaf.txt
new file mode 100644
index 0000000..038c03d
--- /dev/null
+++ b/Documentation/devicetree/bindings/net/hisilicon-hns-dsaf.txt
@@ -0,0 +1,40 @@
+Hisilicon DSA Fabric device controller
+
+Required properties:
+- compatible: should be "hisilicon,dsaf".
+- dsa-name: dsa fabric name who provide this interface
+- interrupt-parent: the interrupt parent of this device.
+- interrupts: should contain the DSA Fabric and rcb interrupt.
+- reg: specifies base physical address(es) and size of the device registers.
+ The first region is external interface control register base and size.
+ The second region is SerDes base register and size.
+ The third region is the PPE register base and size.
+ The fourth region is dsa fabric base register and size.
+- phy-handle: phy handle of physicl port, 0 if not any phy device. see ethernet.txt [1].
+- buf-size: rx buffer size, should be 16-1024.
+- desc-num: number of description in TX and RX queue, should be 512, 1024, 2048 or 4096.
+
+[1] Documentation/devicetree/bindings/net/phy.txt
+
+Example:
+
+dsa: dsa at c7000000 {
+ compatible = "hisilicon,dsaf";
+ dsa_name = "soc0-n4";
+ interrupt-parent = <&mbigen_dsa>;
+ reg = <0x0 0xC0000000 0x0 0x420000
+ 0x0 0xC2000000 0x0 0x300000
+ 0x0 0xc5000000 0x0 0x890000
+ 0x0 0xc7000000 0x0 0x60000>;
+ phy-handle = <0 0 0 0 &soc0_phy4 &soc0_phy5 0 0>;
+ interrupts = <131 4>,<132 4>, <133 4>,<134 4>,
+ <135 4>,<136 4>, <137 4>,<138 4>,
+ <139 4>,<140 4>, <141 4>,<142 4>,
+ <143 4>,<144 4>, <145 4>,<146 4>,
+ <147 4>,<148 4>, <384 1>,<385 1>,
+ <386 1>,<387 1>, <388 1>,<389 1>,
+ <390 1>,<391 1>,
+ buf-size = <4096>;
+ desc-num = <1024>;
+ dma-coherent;
+};
diff --git a/Documentation/devicetree/bindings/net/hisilicon-hns-mdio.txt b/Documentation/devicetree/bindings/net/hisilicon-hns-mdio.txt
new file mode 100644
index 0000000..205e803
--- /dev/null
+++ b/Documentation/devicetree/bindings/net/hisilicon-hns-mdio.txt
@@ -0,0 +1,22 @@
+Hisilicon MDIO bus controller
+
+Properties:
+- compatible: "hisilicon,mdio"
+- reg: The base address of the MDIO bus controller register bank.
+- #address-cells: Must be <1>.
+- #size-cells: Must be <0>. MDIO addresses have no size component.
+
+Typically an MDIO bus might have several children.
+
+Example:
+ mdio at 803c0000 {
+ #address-cells = <1>;
+ #size-cells = <0>;
+ compatible = "hisilicon,mdio";
+ reg = <0x0 0x803c0000 0x0 0x10000>;
+
+ ethernet-phy at 0 {
+ ...
+ reg = <0>;
+ };
+ };
diff --git a/Documentation/devicetree/bindings/net/hisilicon-hns-nic.txt b/Documentation/devicetree/bindings/net/hisilicon-hns-nic.txt
new file mode 100644
index 0000000..5ab6969
--- /dev/null
+++ b/Documentation/devicetree/bindings/net/hisilicon-hns-nic.txt
@@ -0,0 +1,14 @@
+Hisilicon Network Subsystem NIC controller
+
+Required properties:
+- compatible: hisilicon,hns-nic
+- ae-name: accelerator name who provide this interface
+- ae-opts: options (string) to the accelerator. e.g. the index interface
+
+Example:
+
+ ethernet at 0{
+ compatible = "hisilicon,hns-nic";
+ ae-name = "soc0-n4";
+ ae-opts = "0";
+ };
diff --git a/arch/arm64/boot/dts/hisilicon/hip05_hns.dtsi b/arch/arm64/boot/dts/hisilicon/hip05_hns.dtsi
new file mode 100644
index 0000000..80fd017
--- /dev/null
+++ b/arch/arm64/boot/dts/hisilicon/hip05_hns.dtsi
@@ -0,0 +1,197 @@
+soc0: soc at 000000000 {
+ #address-cells = <2>;
+ #size-cells = <2>;
+ device_type = "soc";
+ compatible = "simple-bus";
+ ranges = <0x0 0x0 0x0 0x0 0x1 0x0>;
+ chip-id = <0>;
+
+ soc0_mdio0: mdio at 803c0000 {
+ #address-cells = <1>;
+ #size-cells = <0>;
+ compatible = "hisilicon,mdio";
+ reg = <0x0 0x803c0000 0x0 0x10000>;
+ irq-num = <9383>;
+
+ soc0_phy4: ethernet-phy at 4 {
+ reg = <0x0>;
+ device_type = "ethernet-phy";
+ compatible = "ethernet-phy-ieee802.3-c22";
+ };
+ soc0_phy5: ethernet-phy at 5 {
+ reg = <0x1>;
+ device_type = "ethernet-phy";
+ compatible = "ethernet-phy-ieee802.3-c22";
+ };
+ };
+
+ dsa: dsa at c7000000 {
+ compatible = "hisilicon,dsaf";
+ dsa_name = "soc0-n4";
+ interrupt-parent = <&mbigen_dsa>;
+
+ reg = <0x0 0xC0000000 0x0 0x420000
+ 0x0 0xC2000000 0x0 0x300000
+ 0x0 0xc5000000 0x0 0x890000
+ 0x0 0xc7000000 0x0 0x60000
+ >;
+
+ phy-handle = <0 0 0 0 &soc0_phy4 &soc0_phy5 0 0>;
+
+ interrupts = <131 4>,<132 4>, /* 0 : dsa 0 -17 */
+ <133 4>,<134 4>,
+ <135 4>,<136 4>,
+ <137 4>,<138 4>,
+ <139 4>,<140 4>,
+ <141 4>,<142 4>,
+ <143 4>,<144 4>,
+ <145 4>,<146 4>,
+ <147 4>,<148 4>, /* 17 :dsa end */
+ <384 1>,<385 1>, /* 18: rcb 0 16*2*6 */
+ <386 1>,<387 1>,
+ <388 1>,<389 1>,
+ <390 1>,<391 1>,
+ <392 1>,<393 1>,
+ <394 1>,<395 1>,
+ <396 1>,<397 1>,
+ <398 1>,<399 1>,
+ <400 1>,<401 1>,
+ <402 1>,<403 1>,
+ <404 1>,<405 1>,
+ <406 1>,<407 1>,
+ <408 1>,<409 1>,
+ <410 1>,<411 1>,
+ <412 1>,<413 1>,
+ <414 1>,<415 1>, /* */
+ <416 1>,<417 1>,
+ <418 1>,<419 1>,
+ <420 1>,<421 1>,
+ <422 1>,<423 1>,
+ <424 1>,<425 1>,
+ <426 1>,<427 1>,
+ <428 1>,<429 1>,
+ <430 1>,<431 1>,
+ <432 1>,<433 1>,
+ <434 1>,<435 1>,
+ <436 1>,<437 1>,
+ <438 1>,<439 1>,
+ <440 1>,<441 1>,
+ <442 1>,<443 1>,
+ <444 1>,<445 1>,
+ <446 1>,<447 1>,
+ <448 1>,<449 1>,
+ <450 1>,<451 1>,
+ <452 1>,<453 1>,
+ <454 1>,<455 1>,
+ <456 1>,<457 1>,
+ <458 1>,<459 1>,
+ <460 1>,<461 1>,
+ <462 1>,<463 1>,
+ <464 1>,<465 1>,
+ <466 1>,<467 1>,
+ <468 1>,<469 1>,
+ <470 1>,<471 1>,
+ <472 1>,<473 1>,
+ <474 1>,<475 1>,
+ <476 1>,<477 1>,
+ <478 1>,<479 1>,
+ <480 1>,<481 1>,
+ <482 1>,<483 1>,
+ <484 1>,<485 1>,
+ <486 1>,<487 1>,
+ <488 1>,<489 1>,
+ <490 1>,<491 1>,
+ <492 1>,<493 1>,
+ <494 1>,<495 1>,
+ <496 1>,<497 1>,
+ <498 1>,<499 1>,
+ <500 1>,<501 1>,
+ <502 1>,<503 1>,
+ <504 1>,<505 1>,
+ <506 1>,<507 1>,
+ <508 1>,<509 1>,
+ <510 1>,<511 1>,
+ <512 1>,<513 1>,
+ <514 1>,<515 1>,
+ <516 1>,<517 1>,
+ <518 1>,<519 1>,
+ <520 1>,<521 1>,
+ <522 1>,<523 1>,
+ <524 1>,<525 1>,
+ <526 1>,<527 1>,
+ <528 1>,<529 1>,
+ <530 1>,<531 1>,
+ <532 1>,<533 1>,
+ <534 1>,<535 1>,
+ <536 1>,<537 1>,
+ <538 1>,<539 1>,
+ <540 1>,<541 1>,
+ <542 1>,<543 1>,
+ <544 1>,<545 1>,
+ <546 1>,<547 1>,
+ <548 1>,<549 1>,
+ <550 1>,<551 1>,
+ <552 1>,<553 1>,
+ <554 1>,<555 1>,
+ <556 1>,<557 1>,
+ <558 1>,<559 1>,
+ <560 1>,<561 1>,
+ <562 1>,<563 1>,
+ <564 1>,<565 1>,
+ <566 1>,<567 1>,
+ <568 1>,<569 1>,
+ <570 1>,<571 1>,
+ <572 1>,<573 1>,
+ <574 1>,<575 1>, /* rcb 96*2end */
+ <14 1>,<15 1>,
+ <20 1>,<21 1>;
+ buf-size = <4096>;
+ desc-num = <1024>;
+ dma-coherent;
+ };
+
+ ethernet at 0{
+ compatible = "hisilicon,hns-nic";
+ ae-name = "soc0-n4";
+ ae-opts = "0";
+ };
+ ethernet at 1{
+ compatible = "hisilicon,hns-nic";
+ ae-name = "soc0-n4";
+ ae-opts = "1";
+ };
+ ethernet at 2{
+ compatible = "hisilicon,hns-nic";
+ ae-name = "soc0-n4";
+ ae-opts = "2";
+ };
+ ethernet at 3{
+ compatible = "hisilicon,hns-nic";
+ ae-name = "soc0-n4";
+ ae-opts = "3";
+ };
+ ethernet at 4{
+ compatible = "hisilicon,hns-nic";
+ ae-name = "soc0-n4";
+ ae-opts = "4";
+ };
+ ethernet at 5{
+ compatible = "hisilicon,hns-nic";
+ ae-name = "soc0-n4";
+ ae-opts = "5";
+ };
+ ethernet at 6{
+ compatible = "hisilicon,hns-nic";
+ ae-name = "soc0-n4";
+ ae-opts = "6";
+ };
+ ethernet at 7{
+ compatible = "hisilicon,hns-nic";
+ ae-name = "soc0-n4";
+ ae-opts = "7";
+ };
+
+
+
+
+};
--
1.9.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH 2/5] net: add Hisilicon Network Subsystem hnae framework support
2015-08-14 10:30 [PATCH 0/5] net: Hisilicon Network Subsystem support Kenneth Lee
2015-08-14 10:30 ` [PATCH 1/5] net: add Hisilicon Network Subsystem support (config and documents) Kenneth Lee
@ 2015-08-14 10:30 ` Kenneth Lee
2015-08-17 19:25 ` David Miller
2015-08-18 0:12 ` Alexey Klimov
2015-08-14 10:30 ` [PATCH 3/5] net: add Hisilicon Network Subsystem MDIO support Kenneth Lee
2015-08-14 10:30 ` [PATCH 5/5] net: add Hisilicon Network Subsystem basic ethernet support Kenneth Lee
3 siblings, 2 replies; 15+ messages in thread
From: Kenneth Lee @ 2015-08-14 10:30 UTC (permalink / raw)
To: linux-arm-kernel
HNAE (Hisilicon Network Acceleration Engine) is a framework to provide a
unified ring buffer interface for Hisilicon Network Acceleration Engines.
With the interface, upper layer can work as ethernet driver, ODP driver or
other service driver on purpose.
Signed-off-by: Kenneth Lee <liguozhu@huawei.com>
Signed-off-by: Yisen Zhuang <Yisen.Zhuang@huawei.com>
---
drivers/net/ethernet/hisilicon/Kconfig | 33 +-
drivers/net/ethernet/hisilicon/Makefile | 1 +
drivers/net/ethernet/hisilicon/hns/Makefile | 15 +
drivers/net/ethernet/hisilicon/hns/hnae.c | 494 +++++++++++++++++++++++
drivers/net/ethernet/hisilicon/hns/hnae.h | 582 ++++++++++++++++++++++++++++
5 files changed, 1124 insertions(+), 1 deletion(-)
create mode 100644 drivers/net/ethernet/hisilicon/hns/Makefile
create mode 100644 drivers/net/ethernet/hisilicon/hns/hnae.c
create mode 100644 drivers/net/ethernet/hisilicon/hns/hnae.h
diff --git a/drivers/net/ethernet/hisilicon/Kconfig b/drivers/net/ethernet/hisilicon/Kconfig
index dead17b..1e4f5a7 100644
--- a/drivers/net/ethernet/hisilicon/Kconfig
+++ b/drivers/net/ethernet/hisilicon/Kconfig
@@ -5,7 +5,7 @@
config NET_VENDOR_HISILICON
bool "Hisilicon devices"
default y
- depends on ARM
+ depends on ARM || ARM64
---help---
If you have a network (Ethernet) card belonging to this class, say Y.
@@ -31,4 +31,35 @@ config HIP04_ETH
If you wish to compile a kernel for a hardware with hisilicon p04 SoC and
want to use the internal ethernet then you should answer Y to this.
+config HNS
+ tristate "Hisilicon Network Subsystem Support (Framework)"
+ ---help---
+ This selects the framework support for Hisilicon Network Subsystem. It
+ is needed by any driver which provides HNS acceleration engine or make
+ use of the engine
+
+config HNS_DSAF
+ tristate "Hisilicon HNS DSAF device Support"
+ select HNS
+ select HNS_MDIO
+ ---help---
+ This selects the DSAF (Distributed System Area Frabric) network
+ acceleration engine support. The engine is used in Hisilicon P660,
+ Hi1610 and further ICT SoC
+
+config HNS_MDIO
+ tristate "Hisilicon HNS MDIO device Support"
+ select MDIO
+ ---help---
+ This selects the HNS MDIO support. It is needed by HNS_DSAF to access
+ the PHY
+
+config HNS_ENET
+ tristate "Hisilicon HNS Ethernet Device Support"
+ select PHYLIB
+ select HNS
+ ---help---
+ This selects the general ethernet driver for HNS. This module make
+ use of any HNS AE driver, such as HNS_DSAF
+
endif # NET_VENDOR_HISILICON
diff --git a/drivers/net/ethernet/hisilicon/Makefile b/drivers/net/ethernet/hisilicon/Makefile
index 6c14540..2503a9b 100644
--- a/drivers/net/ethernet/hisilicon/Makefile
+++ b/drivers/net/ethernet/hisilicon/Makefile
@@ -4,3 +4,4 @@
obj-$(CONFIG_HIX5HD2_GMAC) += hix5hd2_gmac.o
obj-$(CONFIG_HIP04_ETH) += hip04_mdio.o hip04_eth.o
+obj-$(CONFIG_HNS) += hns/
diff --git a/drivers/net/ethernet/hisilicon/hns/Makefile b/drivers/net/ethernet/hisilicon/hns/Makefile
new file mode 100644
index 0000000..6680602
--- /dev/null
+++ b/drivers/net/ethernet/hisilicon/hns/Makefile
@@ -0,0 +1,15 @@
+#
+# Makefile for the HISILICON network device drivers.
+#
+
+obj-$(CONFIG_HNS) += hnae.o
+
+obj-$(CONFIG_HNS_DSAF) += hns_dsaf.o
+hns_dsaf-objs = hns_ae_adapt.o hns_dsaf_gmac.o hns_dsaf_mac.o hns_dsaf_misc.o \
+ hns_dsaf_main.o hns_dsaf_ppe.o hns_dsaf_rcb.o hns_dsaf_xgmac.o
+
+obj-$(CONFIG_HNS_MDIO) += hns_mdio.o
+hns_mdio-objs = hns_mdio_main.o
+
+obj-$(CONFIG_HNS_ENET) += hns_enet_drv.o
+hns_enet_drv-objs = hns_enet.o hns_ethtool.o
diff --git a/drivers/net/ethernet/hisilicon/hns/hnae.c b/drivers/net/ethernet/hisilicon/hns/hnae.c
new file mode 100644
index 0000000..fd09768
--- /dev/null
+++ b/drivers/net/ethernet/hisilicon/hns/hnae.c
@@ -0,0 +1,494 @@
+/*
+ * Copyright (c) 2014-2015 Hisilicon Limited.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <linux/dma-mapping.h>
+#include <linux/interrupt.h>
+#include <linux/skbuff.h>
+#include <linux/slab.h>
+
+#include "hnae.h"
+
+#define cls_to_ae_dev(dev) container_of(dev, struct hnae_ae_dev, cls_dev)
+
+static struct class *hnae_class;
+
+static inline void hnae_list_add(spinlock_t *lock, struct list_head *node,
+ struct list_head *head)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(lock, flags);
+ list_add_tail_rcu(node, head);
+ spin_unlock_irqrestore(lock, flags);
+}
+
+static inline void hnae_list_del(spinlock_t *lock, struct list_head *node)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(lock, flags);
+ list_del_rcu(node);
+ spin_unlock_irqrestore(lock, flags);
+}
+
+static int hnae_alloc_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
+{
+ unsigned int order = hnae_page_order(ring);
+ struct page *p = dev_alloc_pages(order);
+
+ if (!p)
+ return -ENOMEM;
+
+ cb->priv = p;
+ cb->page_offset = 0;
+ cb->reuse_flag = 0;
+ cb->buf = page_address(p);
+ cb->length = hnae_page_size(ring);
+ cb->type = DESC_TYPE_PAGE;
+
+ return 0;
+}
+
+static void hnae_free_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
+{
+ if (cb->type == DESC_TYPE_SKB)
+ dev_kfree_skb_any((struct sk_buff *)cb->priv);
+ else if (unlikely(is_rx_ring(ring)))
+ put_page((struct page *)cb->priv);
+ memset(cb, 0, sizeof(*cb));
+}
+
+static int hnae_map_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
+{
+ cb->dma = dma_map_page(ring_to_dev(ring), cb->priv, 0,
+ cb->length, ring_to_dma_dir(ring));
+
+ if (dma_mapping_error(ring_to_dev(ring), cb->dma))
+ return -EIO;
+
+ return 0;
+}
+
+static void hnae_unmap_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
+{
+ if (cb->type == DESC_TYPE_SKB)
+ dma_unmap_single(ring_to_dev(ring), cb->dma, cb->length,
+ ring_to_dma_dir(ring));
+ else
+ dma_unmap_page(ring_to_dev(ring), cb->dma, cb->length,
+ ring_to_dma_dir(ring));
+}
+
+static struct hnae_buf_ops hnae_bops = {
+ .alloc_buffer = hnae_alloc_buffer,
+ .free_buffer = hnae_free_buffer,
+ .map_buffer = hnae_map_buffer,
+ .unmap_buffer = hnae_unmap_buffer,
+};
+
+static int __ae_match(struct device *dev, const void *data)
+{
+ struct hnae_ae_dev *hdev = cls_to_ae_dev(dev);
+ const char *ae_id = data;
+
+ if (!strncmp(ae_id, hdev->name, AE_NAME_SIZE))
+ return 1;
+
+ return 0;
+}
+
+static struct hnae_ae_dev *find_ae(const char *ae_id)
+{
+ struct device *dev;
+
+ BUG_ON(!ae_id);
+
+ dev = class_find_device(hnae_class, NULL, ae_id, __ae_match);
+
+ return cls_to_ae_dev(dev);
+}
+
+static void hnae_free_buffers(struct hnae_ring *ring)
+{
+ int i;
+
+ for (i = 0; i < ring->desc_num; i++)
+ hnae_free_buffer_detach(ring, i);
+}
+
+/* Allocate memory for raw pkg, and map with dma */
+static int hnae_alloc_buffers(struct hnae_ring *ring)
+{
+ int i, j, ret;
+
+ for (i = 0; i < ring->desc_num; i++) {
+ ret = hnae_alloc_buffer_attach(ring, i);
+ if (ret)
+ goto out_buffer_fail;
+ }
+
+ return 0;
+
+out_buffer_fail:
+ for (j = i - 1; j >= 0; j--)
+ hnae_free_buffer_detach(ring, j);
+ return ret;
+}
+
+/* free desc along with its attached buffer */
+static inline void hnae_free_desc(struct hnae_ring *ring)
+{
+ hnae_free_buffers(ring);
+ dma_unmap_single(ring_to_dev(ring), ring->desc_dma_addr,
+ ring->desc_num * sizeof(ring->desc[0]),
+ ring_to_dma_dir(ring));
+ ring->desc_dma_addr = 0;
+ kfree(ring->desc);
+ ring->desc = NULL;
+}
+
+/* alloc desc, without buffer attached */
+static inline int hnae_alloc_desc(struct hnae_ring *ring)
+{
+ int size = ring->desc_num * sizeof(ring->desc[0]);
+
+ ring->desc = kzalloc(size, GFP_KERNEL);
+ if (!ring->desc)
+ return -ENOMEM;
+
+ ring->desc_dma_addr = dma_map_single(ring_to_dev(ring),
+ ring->desc, size, ring_to_dma_dir(ring));
+ if (dma_mapping_error(ring_to_dev(ring), ring->desc_dma_addr)) {
+ ring->desc_dma_addr = 0;
+ kfree(ring->desc);
+ ring->desc = NULL;
+ return -ENOMEM;
+ }
+
+ return 0;
+}
+
+/* fini ring, also free the buffer for the ring */
+static inline void hnae_fini_ring(struct hnae_ring *ring)
+{
+ hnae_free_desc(ring);
+ kfree(ring->desc_cb);
+ ring->desc_cb = NULL;
+ ring->next_to_clean = 0;
+ ring->next_to_use = 0;
+}
+
+/* init ring, and with buffer for rx ring */
+static inline int hnae_init_ring(struct hnae_queue *q, struct hnae_ring *ring,
+ int flags)
+{
+ int ret;
+
+ if (ring->desc_num <= 0 || ring->buf_size <= 0)
+ return -EINVAL;
+
+ ring->q = q;
+ ring->flags = flags;
+ assert(!ring->desc && !ring->desc_cb && !ring->desc_dma_addr);
+
+ /* not matter for tx or rx ring, the ntc and ntc start from 0 */
+ assert(ring->next_to_use == 0);
+ assert(ring->next_to_clean == 0);
+
+ ring->desc_cb = kcalloc(ring->desc_num, sizeof(ring->desc_cb[0]),
+ GFP_KERNEL);
+ if (!ring->desc_cb) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ ret = hnae_alloc_desc(ring);
+ if (ret)
+ goto out_with_desc_cb;
+
+ if (is_rx_ring(ring)) {
+ ret = hnae_alloc_buffers(ring);
+ if (ret)
+ goto out_with_desc;
+ }
+
+ return 0;
+
+out_with_desc:
+ hnae_free_desc(ring);
+out_with_desc_cb:
+ kfree(ring->desc_cb);
+ ring->desc_cb = NULL;
+out:
+ return ret;
+}
+
+static inline int hnae_init_queue(struct hnae_handle *h, struct hnae_queue *q,
+ struct hnae_ae_dev *dev)
+{
+ int ret;
+
+ q->dev = dev;
+ q->handle = h;
+
+ ret = hnae_init_ring(q, &q->tx_ring, q->tx_ring.flags | RINGF_DIR);
+ if (ret)
+ goto out;
+
+ ret = hnae_init_ring(q, &q->rx_ring, q->rx_ring.flags & ~RINGF_DIR);
+ if (ret)
+ goto out_with_tx_ring;
+
+ if (dev->ops->init_queue)
+ dev->ops->init_queue(q);
+
+ return 0;
+
+out_with_tx_ring:
+ hnae_fini_ring(&q->tx_ring);
+out:
+ return ret;
+}
+
+static inline void hnae_fini_queue(struct hnae_queue *q)
+{
+ if (q->dev->ops->fini_queue)
+ q->dev->ops->fini_queue(q);
+
+ hnae_fini_ring(&q->tx_ring);
+ hnae_fini_ring(&q->rx_ring);
+}
+
+/**
+ * ae_chain - define ae chain head
+ */
+static RAW_NOTIFIER_HEAD(ae_chain);
+
+int hnae_register_notifier(struct notifier_block *nb)
+{
+ return raw_notifier_chain_register(&ae_chain, nb);
+}
+EXPORT_SYMBOL(hnae_register_notifier);
+
+void hnae_unregister_notifier(struct notifier_block *nb)
+{
+ if (raw_notifier_chain_unregister(&ae_chain, nb))
+ dev_err(NULL, "notifier chain unregister fail\n");
+}
+EXPORT_SYMBOL(hnae_unregister_notifier);
+
+int hnae_reinit_handle(struct hnae_handle *handle)
+{
+ int i, j;
+ int ret;
+
+ for (i = 0; i < handle->q_num; i++) /* free ring*/
+ hnae_fini_queue(handle->qs[i]);
+
+ for (i = 0; i < handle->q_num; i++) {/* reinit ring*/
+ ret = hnae_init_queue(handle, handle->qs[i], handle->dev);
+ if (ret)
+ goto out_when_init_queue;
+ }
+ return 0;
+out_when_init_queue:
+ for (j = i - 1; j >= 0; j--)
+ hnae_fini_queue(handle->qs[j]);
+ return ret;
+}
+EXPORT_SYMBOL(hnae_reinit_handle);
+
+/* hnae_get_handle - get a handle from the AE
+ * @owner_dev: the dev use this handle
+ * @ae_id: the id of the ae to be used
+ * @ae_opts: the options set for the handle
+ * @bops: the callbacks for buffer management
+ *
+ * return handle ptr or ERR_PTR
+ */
+struct hnae_handle *hnae_get_handle(struct device *owner_dev,
+ const char *ae_id, const char *ae_opts,
+ struct hnae_buf_ops *bops)
+{
+ struct hnae_ae_dev *dev;
+ struct hnae_handle *handle;
+ int i, j;
+ int ret;
+
+ dev = find_ae(ae_id);
+ if (!dev)
+ return ERR_PTR(-ENODEV);
+
+ handle = dev->ops->get_handle(dev, ae_opts);
+ if (IS_ERR(handle))
+ return handle;
+
+ handle->dev = dev;
+ handle->owner_dev = owner_dev;
+ handle->bops = bops ? bops : &hnae_bops;
+ handle->ae_opts = ae_opts;
+
+ for (i = 0; i < handle->q_num; i++) {
+ ret = hnae_init_queue(handle, handle->qs[i], dev);
+ if (ret)
+ goto out_when_init_queue;
+ }
+
+ __module_get(dev->owner);
+
+ hnae_list_add(&dev->lock, &handle->node, &dev->handle_list);
+
+ return handle;
+
+out_when_init_queue:
+ for (j = i - 1; j >= 0; j--)
+ hnae_fini_queue(handle->qs[j]);
+
+ return ERR_PTR(-ENOMEM);
+}
+EXPORT_SYMBOL(hnae_get_handle);
+
+void hnae_put_handle(struct hnae_handle *h)
+{
+ struct hnae_ae_dev *dev = h->dev;
+ int i;
+
+ for (i = 0; i < h->q_num; i++)
+ hnae_fini_queue(h->qs[i]);
+
+ hnae_list_del(&dev->lock, &h->node);
+
+ if (dev->ops->put_handle)
+ dev->ops->put_handle(h);
+
+ module_put(dev->owner);
+}
+EXPORT_SYMBOL(hnae_put_handle);
+
+static void __hnae_release(struct device *dev)
+{
+}
+
+/**
+ * hnae_ae_register - register a AE engine to hnae framework
+ * @hdev: the hnae ae engine device
+ * @owner: the module who provides this dev
+ * NOTE: the duplicated name will not be checked
+ */
+int hnae_ae_register(struct hnae_ae_dev *hdev, struct module *owner)
+{
+ static atomic_t id = ATOMIC_INIT(-1);
+ int ret;
+
+ if (!hdev->dev)
+ return -ENODEV;
+
+ if (!hdev->ops || !hdev->ops->get_handle ||
+ !hdev->ops->toggle_ring_irq ||
+ !hdev->ops->toggle_queue_status ||
+ !hdev->ops->get_status || !hdev->ops->adjust_link)
+ return -EINVAL;
+
+ hdev->owner = owner;
+ hdev->id = (int)atomic_inc_return(&id);
+ hdev->cls_dev.parent = hdev->dev;
+ hdev->cls_dev.class = hnae_class;
+ hdev->cls_dev.release = __hnae_release;
+ dev_set_name(&hdev->cls_dev, "hnae%d", hdev->id);
+ ret = device_register(&hdev->cls_dev);
+ if (ret)
+ return ret;
+
+ __module_get(THIS_MODULE);
+
+ INIT_LIST_HEAD(&hdev->handle_list);
+ spin_lock_init(&hdev->lock);
+
+ ret = raw_notifier_call_chain(&ae_chain, HNAE_AE_REGISTER, NULL);
+ if (ret)
+ dev_dbg(hdev->dev,
+ "has not notifier for AE: %s\n", hdev->name);
+
+ return 0;
+}
+EXPORT_SYMBOL(hnae_ae_register);
+
+/**
+ * hnae_ae_unregister - unregisters a HNAE AE engine
+ * @cdev: the device to unregister
+ */
+void hnae_ae_unregister(struct hnae_ae_dev *hdev)
+{
+ device_unregister(&hdev->cls_dev);
+ module_put(THIS_MODULE);
+}
+EXPORT_SYMBOL(hnae_ae_unregister);
+
+static ssize_t handles_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ ssize_t s = 0;
+ struct hnae_ae_dev *hdev = cls_to_ae_dev(dev);
+ struct hnae_handle *h;
+ int i = 0, j;
+
+ list_for_each_entry_rcu(h, &hdev->handle_list, node) {
+ s += sprintf(buf + s, "%s(%s): ", dev_name(h->owner_dev),
+ h->ae_opts);
+ for (j = 0; j < h->q_num; j++) {
+ if (h->qs[i]) {
+ s += sprintf(buf + s,
+ "(%llu,%llu; ",
+ h->qs[i]->tx_ring.stats.sw_err_cnt,
+ h->qs[i]->tx_ring.stats.io_err_cnt);
+ s += sprintf(buf + s,
+ "%llu,%llu,%llu), ",
+ h->qs[i]->rx_ring.stats.sw_err_cnt,
+ h->qs[i]->rx_ring.stats.io_err_cnt,
+ h->qs[i]->rx_ring.stats.seg_pkt_cnt);
+ } else {
+ s += sprintf(buf + s, "(null)\n");
+ }
+ }
+ s += sprintf(buf + s, "\n");
+ }
+
+ return s;
+}
+
+static DEVICE_ATTR_RO(handles);
+static struct attribute *hnae_class_attrs[] = {
+ &dev_attr_handles.attr,
+ NULL,
+};
+ATTRIBUTE_GROUPS(hnae_class);
+
+static int __init hnae_init(void)
+{
+ hnae_class = class_create(THIS_MODULE, "hnae");
+ if (IS_ERR(hnae_class))
+ return PTR_ERR(hnae_class);
+
+ hnae_class->dev_groups = hnae_class_groups;
+ return 0;
+}
+
+static void __exit hnae_exit(void)
+{
+ class_destroy(hnae_class);
+}
+
+subsys_initcall(hnae_init);
+module_exit(hnae_exit);
+
+MODULE_AUTHOR("Hisilicon, Inc.");
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Hisilicon Network Acceleration Engine Framework");
+
+/* vi: set tw=78 noet: */
diff --git a/drivers/net/ethernet/hisilicon/hns/hnae.h b/drivers/net/ethernet/hisilicon/hns/hnae.h
new file mode 100644
index 0000000..e7856cf
--- /dev/null
+++ b/drivers/net/ethernet/hisilicon/hns/hnae.h
@@ -0,0 +1,582 @@
+/*
+ * Copyright (c) 2014-2015 Hisilicon Limited.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#ifndef __HNAE_H
+#define __HNAE_H
+
+/* Names used in this framework:
+ * ae handle (handle):
+ * a set of queues provided by AE
+ * ring buffer queue (rbq):
+ * the channel between upper layer and the AE, can do tx and rx
+ * ring:
+ * a tx or rx channel within a rbq
+ * ring description (desc):
+ * an element in the ring with packet information
+ * buffer:
+ * a memory region referred by desc with the full packet payload
+ *
+ * "num" means a static number set as a parameter, "count" mean a dynamic
+ * number set while running
+ * "cb" means control block
+ */
+
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/netdevice.h>
+#include <linux/notifier.h>
+#include <linux/types.h>
+
+#define HNAE_DRIVER_VERSION "1.1.0"
+#define HNAE_DRIVER_NAME "hns"
+#define HNAE_COPYRIGHT "Copyright(c) 2015 Huawei Corporation."
+#define HNAE_DRIVER_STRING "Hisilicon Network Subsystem Driver"
+#define HNAE_DEFAULT_DEVICE_DESCR "Hisilicon Network Subsystem"
+
+#ifdef DEBUG
+
+#ifndef assert
+#define assert(expr) \
+do { \
+ if (!(expr)) { \
+ pr_err("Assertion failed! %s, %s, %s, line %d\n", \
+ #expr, __FILE__, __func__, __LINE__); \
+ } \
+} while (0)
+#endif
+
+#else
+
+#ifndef assert
+#define assert(expr)
+#endif
+
+#endif
+
+#define AE_NAME_SIZE 16
+#define AE_OPTS_SIZE 64
+
+/* some said the RX and TX RCB format should not be the same in the future. But
+ * it is the same now...
+ */
+#define RCB_REG_BASEADDR_L 0x00 /* P660 support only 32bit accessing */
+#define RCB_REG_BASEADDR_H 0x04
+#define RCB_REG_BD_NUM 0x08
+#define RCB_REG_BD_LEN 0x0C
+#define RCB_REG_PKTLINE 0x10
+#define RCB_REG_TAIL 0x18
+#define RCB_REG_HEAD 0x1C
+#define RCB_REG_FBDNUM 0x20
+#define RCB_REG_OFFSET 0x24 /* pkt num to be handled */
+#define RCB_REG_PKTNUM_RECORD 0x2C /* total pkt received */
+
+#define HNS_RX_HEAD_SIZE 256
+
+#define HNAE_AE_REGISTER 0x1
+
+#define RCB_RING_NAME_LEN 16
+
+enum hnae_led_state {
+ HNAE_LED_INACTIVE,
+ HNAE_LED_ACTIVE,
+ HNAE_LED_ON,
+ HNAE_LED_OFF
+};
+
+#define HNS_RX_FLAG_VLAN_PRESENT 0x1
+#define HNS_RX_FLAG_L3ID_IPV4 0x0
+#define HNS_RX_FLAG_L3ID_IPV6 0x1
+#define HNS_RX_FLAG_L4ID_UDP 0x0
+#define HNS_RX_FLAG_L4ID_TCP 0x1
+
+#define HNS_TXD_ASID_S 0
+#define HNS_TXD_ASID_M (0xff << HNS_TXD_ASID_S)
+#define HNS_TXD_BUFNUM_S 8
+#define HNS_TXD_BUFNUM_M (0x3 << HNS_TXD_BUFNUM_S)
+#define HNS_TXD_PORTID_S 10
+#define HNS_TXD_PORTID_M (0x7 << HNS_TXD_PORTID_S)
+
+#define HNS_TXD_RA_B 8
+#define HNS_TXD_RI_B 9
+#define HNS_TXD_L4CS_B 10
+#define HNS_TXD_L3CS_B 11
+#define HNS_TXD_FE_B 12
+#define HNS_TXD_VLD_B 13
+#define HNS_TXD_IPOFFSET_S 14
+#define HNS_TXD_IPOFFSET_M (0xff << HNS_TXD_IPOFFSET_S)
+
+#define HNS_RXD_IPOFFSET_S 0
+#define HNS_RXD_IPOFFSET_M (0xff << HNS_TXD_IPOFFSET_S)
+#define HNS_RXD_BUFNUM_S 8
+#define HNS_RXD_BUFNUM_M (0x3 << HNS_RXD_BUFNUM_S)
+#define HNS_RXD_PORTID_S 10
+#define HNS_RXD_PORTID_M (0x7 << HNS_RXD_PORTID_S)
+#define HNS_RXD_DMAC_S 13
+#define HNS_RXD_DMAC_M (0x3 << HNS_RXD_DMAC_S)
+#define HNS_RXD_VLAN_S 15
+#define HNS_RXD_VLAN_M (0x3 << HNS_RXD_VLAN_S)
+#define HNS_RXD_L3ID_S 17
+#define HNS_RXD_L3ID_M (0xf << HNS_RXD_L3ID_S)
+#define HNS_RXD_L4ID_S 21
+#define HNS_RXD_L4ID_M (0xf << HNS_RXD_L4ID_S)
+#define HNS_RXD_FE_B 25
+#define HNS_RXD_FRAG_B 26
+#define HNS_RXD_VLD_B 27
+#define HNS_RXD_L2E_B 28
+#define HNS_RXD_L3E_B 29
+#define HNS_RXD_L4E_B 30
+#define HNS_RXD_DROP_B 31
+
+#define HNS_RXD_VLANID_S 8
+#define HNS_RXD_VLANID_M (0xfff << HNS_RXD_VLANID_S)
+#define HNS_RXD_CFI_B 20
+#define HNS_RXD_PRI_S 21
+#define HNS_RXD_PRI_M (0x7 << HNS_RXD_PRI_S)
+#define HNS_RXD_ASID_S 24
+#define HNS_RXD_ASID_M (0xff << HNS_RXD_ASID_S)
+
+/* hardware spec ring buffer format */
+struct __packed hnae_desc {
+ __le64 addr;
+ union {
+ struct {
+ __le16 asid_bufnum_pid;
+ __le16 send_size;
+ __le32 flag_ipoffset;
+ __le32 reserved_3[4];
+ } tx;
+
+ struct {
+ __le32 ipoff_bnum_pid_flag;
+ __le16 pkt_len;
+ __le16 size;
+ __le32 vlan_pri_asid;
+ __le32 reserved_2[3];
+ } rx;
+ };
+};
+
+struct hnae_desc_cb {
+ dma_addr_t dma; /* dma address of this desc */
+ void *buf; /* cpu addr for a desc */
+
+ /* priv data for the desc, e.g. skb when use with ip stack*/
+ void *priv;
+ u16 page_offset;
+ u16 reuse_flag;
+
+ u16 length; /* length of the buffer */
+
+ /* desc type, used by the ring user to mark the type of the priv data */
+ u16 type;
+};
+
+#define setflags(flags, bits) ((flags) |= (bits))
+#define unsetflags(flags, bits) ((flags) &= ~(bits))
+
+/* hnae_ring->flags fields */
+#define RINGF_DIR 0x1 /* TX or RX ring, set if TX */
+#define is_tx_ring(ring) ((ring)->flags & RINGF_DIR)
+#define is_rx_ring(ring) (!is_tx_ring(ring))
+#define ring_to_dma_dir(ring) (is_tx_ring(ring) ? \
+ DMA_TO_DEVICE : DMA_FROM_DEVICE)
+
+struct ring_stats {
+ u64 io_err_cnt;
+ u64 sw_err_cnt;
+ u64 seg_pkt_cnt;
+ u64 reuse_pg_cnt;
+ u64 err_pkt_len;
+ u64 non_vld_descs;
+ u64 err_bd_num;
+ u64 csum_err;
+ union {
+ u64 tx_pkts;
+ u64 rx_pkts;
+ } pkts;
+ union {
+ u64 tx_bytes;
+ u64 rx_bytes;
+ } bytes;
+ union {
+ u64 tx_err_cnt;
+ u64 rx_err_cnt;
+ } err_cnt;
+};
+
+struct hnae_queue;
+
+struct hnae_ring {
+ u8 __iomem *io_base; /* base io address for the ring */
+ struct hnae_desc *desc; /* dma map address space */
+ struct hnae_desc_cb *desc_cb;
+ struct hnae_queue *q;
+ int irq;
+ char ring_name[RCB_RING_NAME_LEN];
+
+ /* statistic */
+ struct ring_stats stats;
+
+ dma_addr_t desc_dma_addr;
+ u32 buf_size; /* size for hnae_desc->addr, preset by AE */
+ u16 desc_num; /* total number of desc */
+ u16 max_desc_num_per_pkt;
+ u16 max_raw_data_sz_per_desc;
+ u16 max_pkt_size;
+ int next_to_use; /* idx of next spare desc */
+
+ /* idx of lastest sent desc, the ring is empty when equal to
+ * next_to_use
+ */
+ int next_to_clean;
+
+ int flags; /* ring attribute */
+};
+
+#define ring_ptr_move_fw(ring, p) \
+ ((ring)->p = ((ring)->p + 1) % (ring)->desc_num)
+#define ring_ptr_move_bw(ring, p) \
+ ((ring)->p = ((ring)->p - 1 + (ring)->desc_num) % (ring)->desc_num)
+
+enum hns_desc_type {
+ DESC_TYPE_SKB,
+ DESC_TYPE_PAGE,
+};
+
+#define assert_is_ring_idx(ring, idx) \
+ assert((idx) >= 0 && (idx) < (ring)->desc_num)
+
+/* the distance between [begin, end) in a ring buffer
+ * note: there is a unuse slot between the begin and the end
+ */
+static inline int ring_dist(struct hnae_ring *ring, int begin, int end)
+{
+ assert_is_ring_idx(ring, begin);
+ assert_is_ring_idx(ring, end);
+
+ return (end - begin + ring->desc_num) % ring->desc_num;
+}
+
+static inline int ring_space(struct hnae_ring *ring)
+{
+ return ring->desc_num -
+ ring_dist(ring, ring->next_to_clean, ring->next_to_use) - 1;
+}
+
+static inline int is_ring_empty(struct hnae_ring *ring)
+{
+ assert_is_ring_idx(ring, ring->next_to_use);
+ assert_is_ring_idx(ring, ring->next_to_clean);
+
+ return ring->next_to_use == ring->next_to_clean;
+}
+
+#define hnae_buf_size(_ring) ((_ring)->buf_size)
+#define hnae_page_order(_ring) (get_order(hnae_buf_size(_ring)))
+#define hnae_page_size(_ring) (PAGE_SIZE << hnae_page_order(_ring))
+
+struct hnae_handle;
+
+/* allocate and dma map space for hnae desc */
+struct hnae_buf_ops {
+ int (*alloc_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
+ void (*free_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
+ int (*map_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
+ void (*unmap_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
+};
+
+struct hnae_queue {
+ void __iomem *io_base;
+ struct hnae_ae_dev *dev; /* the device who use this queue */
+ struct hnae_ring rx_ring, tx_ring;
+ struct hnae_handle *handle;
+};
+
+/*hnae loop mode*/
+enum hnae_loop {
+ MAC_LOOP_NONE = 0,
+ MAC_INTERNALLOOP_MAC,
+ MAC_INTERNALLOOP_SERDES,
+ MAC_INTERNALLOOP_PHY,
+};
+
+/*hnae port type*/
+enum hnae_port_type {
+ HNAE_PORT_SERVICE = 0,
+ HNAE_PORT_DEBUG
+};
+
+/* This struct defines the operation on the handle.
+ *
+ * get_handle(): (mandatory)
+ * Get a handle from AE according to its name and options.
+ * the AE driver should manage the space used by handle and its queues while
+ * the HNAE framework will allocate desc and desc_cb for all rings in the
+ * queues.
+ * put_handle():
+ * Release the handle.
+ * start():
+ * Enable the hardware, include all queues
+ * stop():
+ * Disable the hardware
+ * set_opts(): (mandatory)
+ * Set options to the AE
+ * get_opts(): (mandatory)
+ * Get options from the AE
+ * get_status():
+ * Get the carrier state of the back channel of the handle, 1 for ok, 0 for
+ * non-ok
+ * toggle_ring_irq(): (mandatory)
+ * Set the ring irq to be enabled(0) or disable(1)
+ * toggle_queue_status(): (mandatory)
+ * Set the queue to be enabled(1) or disable(0), this will not change the
+ * ring irq state
+ * adjust_link()
+ * adjust link status
+ * set_loopback()
+ * set loopback
+ * get_ring_bdnum_limit()
+ * get ring bd number limit
+ * get_pauseparam()
+ * get tx and rx of pause frame use
+ * set_autoneg()
+ * set auto autonegotiation of pause frame use
+ * get_autoneg()
+ * get auto autonegotiation of pause frame use
+ * set_pauseparam()
+ * set tx and rx of pause frame use
+ * get_coalesce_usecs()
+ * get usecs to delay a TX interrupt after a packet is sent
+ * get_rx_max_coalesced_frames()
+ * get Maximum number of packets to be sent before a TX interrupt.
+ * set_coalesce_usecs()
+ * set usecs to delay a TX interrupt after a packet is sent
+ * set_coalesce_frames()
+ * set Maximum number of packets to be sent before a TX interrupt.
+ * get_ringnum()
+ * get RX/TX ring number
+ * get_max_ringnum()
+ * get RX/TX ring maximum number
+ * get_mac_addr()
+ * get mac address
+ * set_mac_addr()
+ * set mac address
+ * set_mc_addr()
+ * set multicast mode
+ * set_mtu()
+ * set mtu
+ * update_stats()
+ * update Old network device statistics
+ * get_ethtool_stats()
+ * get ethtool network device statistics
+ * get_strings()
+ * get a set of strings that describe the requested objects
+ * get_sset_count()
+ * get number of strings that @get_strings will write
+ * update_led_status()
+ * update the led status
+ * set_led_id()
+ * set led id
+ * get_regs()
+ * get regs dump
+ * get_regs_len()
+ * get the len of the regs dump
+ */
+struct hnae_ae_ops {
+ struct hnae_handle *(*get_handle)(struct hnae_ae_dev *dev,
+ const char *opts);
+ void (*put_handle)(struct hnae_handle *handle);
+ void (*init_queue)(struct hnae_queue *q);
+ void (*fini_queue)(struct hnae_queue *q);
+ int (*start)(struct hnae_handle *handle);
+ void (*stop)(struct hnae_handle *handle);
+ void (*reset)(struct hnae_handle *handle);
+ int (*set_opts)(struct hnae_handle *handle, int type, void *opts);
+ int (*get_opts)(struct hnae_handle *handle, int type, void **opts);
+ int (*get_status)(struct hnae_handle *handle);
+ int (*get_info)(struct hnae_handle *handle,
+ u8 *auto_neg, u16 *speed, u8 *duplex);
+ int (*set_info)(struct hnae_handle *handle,
+ u8 auto_neg, u16 speed, u8 duplex);
+ void (*toggle_ring_irq)(struct hnae_ring *ring, u32 val);
+ void (*toggle_queue_status)(struct hnae_queue *queue, u32 val);
+ void (*adjust_link)(struct hnae_handle *handle, int speed, int duplex);
+ int (*set_loopback)(struct hnae_handle *handle,
+ enum hnae_loop loop_mode, int en);
+ void (*get_ring_bdnum_limit)(struct hnae_queue *queue,
+ u32 *uplimit, u32 *lowlimit);
+ void (*get_pauseparam)(struct hnae_handle *handle,
+ u32 *auto_neg, u32 *rx_en, u32 *tx_en);
+ int (*set_autoneg)(struct hnae_handle *handle, u8 enable);
+ int (*get_autoneg)(struct hnae_handle *handle);
+ void (*set_pauseparam)(struct hnae_handle *handle,
+ u32 rx_en, u32 tx_en);
+ void (*get_coalesce_usecs)(struct hnae_handle *handle,
+ u32 *tx_usecs, u32 *rx_usecs);
+ void (*get_rx_max_coalesced_frames)(struct hnae_handle *handle,
+ u32 *tx_frames, u32 *rx_frames);
+ void (*set_coalesce_usecs)(struct hnae_handle *handle, u32 timeout);
+ int (*set_coalesce_frames)(struct hnae_handle *handle,
+ u32 coalesce_frames);
+ void (*get_ringnum)(struct hnae_handle *handle, u32 *ringnum);
+ void (*get_max_ringnum)(struct hnae_handle *handle, u32 *max_ringnum);
+ int (*get_mac_addr)(struct hnae_handle *handle, void **p);
+ int (*set_mac_addr)(struct hnae_handle *handle, void *p);
+ int (*set_mc_addr)(struct hnae_handle *handle, void *addr);
+ int (*set_mtu)(struct hnae_handle *handle, int new_mtu);
+ void (*update_stats)(struct hnae_handle *handle,
+ struct net_device_stats *net_stats);
+ void (*get_stats)(struct hnae_handle *handle, u64 *data);
+ void (*get_strings)(struct hnae_handle *handle,
+ u32 stringset, u8 *data);
+ int (*get_sset_count)(struct hnae_handle *handle, int stringset);
+ void (*update_led_status)(struct hnae_handle *handle);
+ int (*set_led_id)(struct hnae_handle *handle,
+ enum hnae_led_state status);
+ void (*get_regs)(struct hnae_handle *handle, void *data);
+ int (*get_regs_len)(struct hnae_handle *handle);
+};
+
+struct hnae_ae_dev {
+ struct device cls_dev; /* the class dev */
+ struct device *dev; /* the presented dev */
+ struct hnae_ae_ops *ops;
+ struct list_head node;
+ struct module *owner; /* the module who provides this dev */
+ int id;
+ char name[AE_NAME_SIZE];
+ struct list_head handle_list;
+ spinlock_t lock; /* lock to protect the handle_list */
+};
+
+struct hnae_handle {
+ struct device *owner_dev; /* the device which make use of this handle */
+ struct hnae_ae_dev *dev; /* the device who provides this handle */
+ struct device_node *phy_node;
+ phy_interface_t phy_if;
+ u32 if_support;
+ int q_num;
+ const char *ae_opts;
+ enum hnae_port_type port_type;
+ struct list_head node; /* list to hnae_ae_dev->handle_list */
+ struct hnae_buf_ops *bops; /* operation for the buffer */
+ struct net_device_stats net_stats;
+ struct hnae_queue **qs; /* array base of all queues */
+};
+
+#define ring_to_dev(ring) ((ring)->q->dev->dev)
+
+struct hnae_handle *hnae_get_handle(struct device *owner_dev, const char *ae_id,
+ const char *ae_opts,
+ struct hnae_buf_ops *bops);
+void hnae_put_handle(struct hnae_handle *handle);
+int hnae_ae_register(struct hnae_ae_dev *dev, struct module *owner);
+void hnae_ae_unregister(struct hnae_ae_dev *dev);
+
+int hnae_register_notifier(struct notifier_block *nb);
+void hnae_unregister_notifier(struct notifier_block *nb);
+int hnae_reinit_handle(struct hnae_handle *handle);
+
+#define hnae_queue_xmit(q, buf_num) writel_relaxed(buf_num, \
+ (q)->tx_ring.io_base + RCB_REG_TAIL)
+
+#ifndef assert
+#define assert(cond)
+#endif
+
+static inline int hnae_reserve_buffer_map(struct hnae_ring *ring,
+ struct hnae_desc_cb *cb)
+{
+ struct hnae_buf_ops *bops = ring->q->handle->bops;
+ int ret;
+
+ ret = bops->alloc_buffer(ring, cb);
+ if (ret)
+ goto out;
+
+ ret = bops->map_buffer(ring, cb);
+ if (ret)
+ goto out_with_buf;
+
+ return 0;
+
+out_with_buf:
+ bops->free_buffer(ring, cb);
+out:
+ return ret;
+}
+
+static inline int hnae_alloc_buffer_attach(struct hnae_ring *ring, int i)
+{
+ int ret = hnae_reserve_buffer_map(ring, &ring->desc_cb[i]);
+
+ if (ret)
+ return ret;
+
+ ring->desc[i].addr = (__le64)ring->desc_cb[i].dma;
+
+ return 0;
+}
+
+static inline void hnae_buffer_detach(struct hnae_ring *ring, int i)
+{
+ ring->q->handle->bops->unmap_buffer(ring, &ring->desc_cb[i]);
+ ring->desc[i].addr = 0;
+}
+
+static inline void hnae_free_buffer_detach(struct hnae_ring *ring, int i)
+{
+ struct hnae_buf_ops *bops = ring->q->handle->bops;
+ struct hnae_desc_cb *cb = &ring->desc_cb[i];
+
+ if (!ring->desc_cb[i].dma)
+ return;
+
+ hnae_buffer_detach(ring, i);
+ bops->free_buffer(ring, cb);
+}
+
+/* detach a in-used buffer and replace with a reserved one */
+static inline void hnae_replace_buffer(struct hnae_ring *ring, int i,
+ struct hnae_desc_cb *res_cb)
+{
+ struct hnae_buf_ops *bops = ring->q->handle->bops;
+ struct hnae_desc_cb tmp_cb = ring->desc_cb[i];
+
+ bops->unmap_buffer(ring, &ring->desc_cb[i]);
+ ring->desc_cb[i] = *res_cb;
+ *res_cb = tmp_cb;
+ ring->desc[i].addr = (__le64)ring->desc_cb[i].dma;
+ ring->desc[i].rx.ipoff_bnum_pid_flag = 0;
+}
+
+static inline void hnae_reuse_buffer(struct hnae_ring *ring, int i)
+{
+ ring->desc_cb[i].reuse_flag = 0;
+ ring->desc[i].addr = (__le64)(ring->desc_cb[i].dma
+ + ring->desc_cb[i].page_offset);
+ ring->desc[i].rx.ipoff_bnum_pid_flag = 0;
+}
+
+#define hnae_set_field(origin, mask, shift, val) \
+ do { \
+ (origin) &= (~(mask)); \
+ (origin) |= ((val) << (shift)) & (mask); \
+ } while (0)
+
+#define hnae_set_bit(origin, shift, val) \
+ hnae_set_field((origin), (0x1 << (shift)), (shift), (val))
+
+#define hnae_get_field(origin, mask, shift) (((origin) & (mask)) >> (shift))
+
+#define hnae_get_bit(origin, shift) \
+ hnae_get_field((origin), (0x1 << (shift)), (shift))
+
+#endif
--
1.9.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH 3/5] net: add Hisilicon Network Subsystem MDIO support
2015-08-14 10:30 [PATCH 0/5] net: Hisilicon Network Subsystem support Kenneth Lee
2015-08-14 10:30 ` [PATCH 1/5] net: add Hisilicon Network Subsystem support (config and documents) Kenneth Lee
2015-08-14 10:30 ` [PATCH 2/5] net: add Hisilicon Network Subsystem hnae framework support Kenneth Lee
@ 2015-08-14 10:30 ` Kenneth Lee
2015-08-14 20:57 ` Arnd Bergmann
2015-08-14 10:30 ` [PATCH 5/5] net: add Hisilicon Network Subsystem basic ethernet support Kenneth Lee
3 siblings, 1 reply; 15+ messages in thread
From: Kenneth Lee @ 2015-08-14 10:30 UTC (permalink / raw)
To: linux-arm-kernel
The MDIO support for Hisilicon Network Subsystem. It is used in Hislicon
P660 and Hi1610 SoC to control the external PHY
Signed-off-by: Yisen Zhuang <Yisen.Zhuang@huawei.com>
Signed-off-by: Kenneth Lee <liguozhu@huawei.com>
---
drivers/net/ethernet/hisilicon/hns/hns_mdio_main.c | 597 +++++++++++++++++++++
1 file changed, 597 insertions(+)
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_mdio_main.c
diff --git a/drivers/net/ethernet/hisilicon/hns/hns_mdio_main.c b/drivers/net/ethernet/hisilicon/hns/hns_mdio_main.c
new file mode 100644
index 0000000..7113fa8
--- /dev/null
+++ b/drivers/net/ethernet/hisilicon/hns/hns_mdio_main.c
@@ -0,0 +1,597 @@
+/*
+ * Copyright (c) 2014-2015 Hisilicon Limited.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <linux/errno.h>
+#include <linux/etherdevice.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/netdevice.h>
+#include <linux/of_address.h>
+#include <linux/of.h>
+#include <linux/of_mdio.h>
+#include <linux/of_platform.h>
+#include <linux/phy.h>
+#include <linux/platform_device.h>
+#include <linux/spinlock_types.h>
+
+#define MDIO_DRV_NAME "hi-mdio"
+#define MDIO_BUS_NAME "Hisilicon MII Bus"
+#define MDIO_DRV_VERSION "1.1.0"
+#define MDIO_COPYRIGHT "Copyright(c) 2015 Huawei Corporation."
+#define MDIO_DRV_STRING MDIO_BUS_NAME
+#define MDIO_DEFAULT_DEVICE_DESCR MDIO_BUS_NAME
+
+#define MDIO_CTL_DEV_ADDR(x) (x & 0x1f)
+#define MDIO_CTL_PORT_ADDR(x) ((x & 0x1f) << 5)
+
+#define MDIO_BASE_ADDR 0x403C0000
+#define MDIO_REG_ADDR_LEN 0x1000
+#define MDIO_PHY_GRP_LEN 0x100
+#define MDIO_REG_LEN 0x10
+#define MDIO_PHY_ADDR_NUM 5
+#define MDIO_MAX_PHY_ADDR 0x1F
+#define MDIO_MAX_PHY_REG_ADDR 0xFFFF
+
+#define MDIO_TIMEOUT 1000000
+
+struct hns_mdio_device {
+ struct device *dev;
+ void *vbase; /* mdio reg base address */
+ u8 phy_class[PHY_MAX_ADDR];
+ u8 index;
+ u8 chip_id;
+ u8 gidx; /* global index */
+};
+
+#define MDIO_COMMAND_REG 0x0
+#define MDIO_ADDR_REG 0x4
+#define MDIO_WDATA_REG 0x8
+#define MDIO_RDATA_REG 0xc
+#define MDIO_STA_REG 0x10
+
+#define MDIO_CMD_DEVAD_M 0x1f
+#define MDIO_CMD_DEVAD_S 0
+#define MDIO_CMD_PRTAD_M 0x1f
+#define MDIO_CMD_PRTAD_S 5
+#define MDIO_CMD_OP_M 0x3
+#define MDIO_CMD_OP_S 10
+#define MDIO_CMD_ST_M 0x3
+#define MDIO_CMD_ST_S 12
+#define MDIO_CMD_START_B 14
+
+#define MDIO_ADDR_DATA_M 0xffff
+#define MDIO_ADDR_DATA_S 0
+
+#define MDIO_WDATA_DATA_M 0xffff
+#define MDIO_WDATA_DATA_S 0
+
+#define MDIO_RDATA_DATA_M 0xffff
+#define MDIO_RDATA_DATA_S 0
+
+#define MDIO_STATE_STA_B 0
+
+enum mdio_st_clause {
+ MDIO_ST_CLAUSE_45 = 0,
+ MDIO_ST_CLAUSE_22
+};
+
+enum mdio_c22_op_seq {
+ MDIO_C22_WRITE = 1,
+ MDIO_C22_READ = 2
+};
+
+enum mdio_c45_op_seq {
+ MDIO_C45_WRITE_ADDR = 0,
+ MDIO_C45_WRITE_DATA,
+ MDIO_C45_READ_INCREMENT,
+ MDIO_C45_READ
+};
+
+static inline void mdio_write_reg(void *base, u32 reg, u32 value)
+{
+ u8 __iomem *reg_addr = ACCESS_ONCE(base);
+
+ writel(value, reg_addr + reg);
+}
+
+#define MDIO_WRITE_REG(a, reg, value) \
+ mdio_write_reg((a)->vbase, (reg), (value))
+
+static inline u32 mdio_read_reg(void *base, u32 reg)
+{
+ u8 __iomem *reg_addr = ACCESS_ONCE(base);
+
+ return readl(reg_addr + reg);
+}
+
+#define MDIO_READ_REG(a, reg) \
+ mdio_read_reg((a)->vbase, (reg))
+
+#define mdio_set_field(origin, mask, shift, val) \
+ do { \
+ (origin) &= (~((mask) << (shift))); \
+ (origin) |= (((val) & (mask)) << (shift)); \
+ } while (0)
+
+#define mdio_get_field(origin, mask, shift) (((origin) >> (shift)) & (mask))
+
+static void mdio_set_reg_field(void *base, u32 reg, u32 mask, u32 shift,
+ u32 val)
+{
+ u32 origin = mdio_read_reg(base, reg);
+
+ mdio_set_field(origin, mask, shift, val);
+ mdio_write_reg(base, reg, origin);
+}
+
+#define MDIO_SET_REG_FIELD(dev, reg, mask, shift, val) \
+ mdio_set_reg_field((dev)->vbase, (reg), (mask), (shift), (val))
+
+static u32 mdio_get_reg_field(void *base, u32 reg, u32 mask, u32 shift)
+{
+ u32 origin;
+
+ origin = mdio_read_reg(base, reg);
+ return mdio_get_field(origin, mask, shift);
+}
+
+#define MDIO_GET_REG_FIELD(dev, reg, mask, shift) \
+ mdio_get_reg_field((dev)->vbase, (reg), (mask), (shift))
+
+#define MDIO_SET_REG_BIT(dev, reg, bit, val) \
+ mdio_set_reg_field((dev)->vbase, (reg), 0x1ull, (bit), (val))
+
+#define MDIO_GET_REG_BIT(dev, reg, bit) \
+ mdio_get_reg_field((dev)->vbase, (reg), 0x1ull, (bit))
+
+/**
+ *hns_mdio_read_hw - read phy regs
+ *@mdio_dev: mdio device
+ *@phy_addr:phy addr
+ *@is_c45:
+ *@page:
+ *@reg: reg
+ *@data:regs data
+ *return status
+ */
+static int hns_mdio_read_hw(struct hns_mdio_device *mdio_dev, u8 phy_addr,
+ u8 is_c45, u8 page, u16 reg, u16 *data)
+{
+ u32 cmd_reg_value;
+ u32 sta_reg_value;
+ u32 time_cnt;
+
+ if (phy_addr > MDIO_MAX_PHY_ADDR) {
+ dev_err(mdio_dev->dev, "Wrong phy address: phy_addr(%x)\n",
+ phy_addr);
+ return -EINVAL;
+ }
+
+ dev_dbg(mdio_dev->dev, "mdio(%d) base is %p\n",
+ mdio_dev->gidx, mdio_dev->vbase);
+ dev_dbg(mdio_dev->dev,
+ "phy_addr=%d, is_c45=%d, devad=%d, regnum=%#x!\n",
+ phy_addr, is_c45, page, reg);
+
+ /* Step 1; waiting for MDIO_COMMAND_REG 's mdio_start==0, */
+ /* after that can do read or write*/
+ for (time_cnt = MDIO_TIMEOUT; time_cnt; time_cnt--) {
+ cmd_reg_value = MDIO_GET_REG_BIT(mdio_dev,
+ MDIO_COMMAND_REG,
+ MDIO_CMD_START_B);
+ if (!cmd_reg_value)
+ break;
+ }
+ if (cmd_reg_value) {
+ dev_err(mdio_dev->dev, "MDIO is busy!\n");
+ return -EBUSY;
+ }
+
+ if (!is_c45) {
+ cmd_reg_value = MDIO_ST_CLAUSE_22 << MDIO_CMD_ST_S;
+ cmd_reg_value |= MDIO_C22_READ << MDIO_CMD_OP_S;
+ cmd_reg_value |=
+ (phy_addr & MDIO_CMD_PRTAD_M) << MDIO_CMD_PRTAD_S;
+ cmd_reg_value |= (reg & MDIO_CMD_DEVAD_M) << MDIO_CMD_DEVAD_S;
+ cmd_reg_value |= 1 << MDIO_CMD_START_B;
+
+ MDIO_WRITE_REG(mdio_dev, MDIO_COMMAND_REG, cmd_reg_value);
+ } else {
+ MDIO_SET_REG_FIELD(mdio_dev, MDIO_ADDR_REG, MDIO_ADDR_DATA_M,
+ MDIO_ADDR_DATA_S, reg);
+
+ /* Step 2; config the cmd-reg to write addr*/
+ cmd_reg_value = MDIO_ST_CLAUSE_45 << MDIO_CMD_ST_S;
+ cmd_reg_value |= MDIO_C45_WRITE_ADDR << MDIO_CMD_OP_S;
+
+ /* mdio_st==2'b00: is configing port addr*/
+ cmd_reg_value |=
+ (phy_addr & MDIO_CMD_PRTAD_M) << MDIO_CMD_PRTAD_S;
+
+ /* mdio_st == 2'b00: is configing dev_addr*/
+ cmd_reg_value |= (page & MDIO_CMD_DEVAD_M) << MDIO_CMD_DEVAD_S;
+ cmd_reg_value |= 1 << MDIO_CMD_START_B;
+ MDIO_WRITE_REG(mdio_dev, MDIO_COMMAND_REG, cmd_reg_value);
+
+ /* Step 3; waiting for MDIO_COMMAND_REG 's mdio_start==0,*/
+ /*check for read or write opt is finished */
+ for (time_cnt = MDIO_TIMEOUT; time_cnt; time_cnt--) {
+ cmd_reg_value = MDIO_GET_REG_BIT(mdio_dev,
+ MDIO_COMMAND_REG,
+ MDIO_CMD_START_B);
+ if (!cmd_reg_value)
+ break;
+ }
+ if (cmd_reg_value) {
+ dev_err(mdio_dev->dev, "MDIO is busy\n");
+ return -EBUSY;
+ }
+
+ /* Step 4; config cmd-reg, send read opt */
+ cmd_reg_value = MDIO_ST_CLAUSE_45 << MDIO_CMD_ST_S;
+ cmd_reg_value |= MDIO_C45_READ << MDIO_CMD_OP_S;
+
+ /* mdio_st==2'b00: is configing port addr*/
+ cmd_reg_value |=
+ (phy_addr & MDIO_CMD_PRTAD_M) << MDIO_CMD_PRTAD_S;
+
+ /* mdio_st == 2'b00: is configing dev_addr*/
+ cmd_reg_value |= (page & MDIO_CMD_DEVAD_M) << MDIO_CMD_DEVAD_S;
+ cmd_reg_value |= 1 << MDIO_CMD_START_B;
+ MDIO_WRITE_REG(mdio_dev, MDIO_COMMAND_REG, cmd_reg_value);
+ }
+
+ /* Step 5; waiting for MDIO_COMMAND_REG 's mdio_start==0,*/
+ /* check for read or write opt is finished */
+ for (time_cnt = MDIO_TIMEOUT; time_cnt; time_cnt--) {
+ cmd_reg_value = MDIO_GET_REG_BIT(mdio_dev,
+ MDIO_COMMAND_REG,
+ MDIO_CMD_START_B);
+ if (!cmd_reg_value)
+ break;
+ }
+ if (cmd_reg_value) {
+ dev_err(mdio_dev->dev, "MDIO is busy\n");
+ return -EBUSY;
+ }
+
+ sta_reg_value = MDIO_GET_REG_BIT(mdio_dev,
+ MDIO_STA_REG, MDIO_STATE_STA_B);
+ if (sta_reg_value) {
+ dev_err(mdio_dev->dev, "MDIO Read failed!\n");
+ return -EBUSY;
+ }
+
+ /* Step 6; get out data*/
+ *data = (u16)MDIO_GET_REG_FIELD(mdio_dev, MDIO_RDATA_REG,
+ MDIO_RDATA_DATA_M, MDIO_RDATA_DATA_S);
+
+ return 0;
+}
+
+/**
+ *hns_mdio_write_hw - write phy regs
+ *@mdio_dev: mdio device
+ *@phy_addr: phy addr
+ *@is_c45: 0 means the clause 22, none 0 means clause 45
+ *@page: phy page addr
+ *@reg: phy reg
+ *@data: regs data
+ *return status
+ */
+static int hns_mdio_write_hw(struct hns_mdio_device *mdio_dev, u8 phy_addr,
+ u8 is_c45, u8 page, u16 reg, u16 data)
+{
+ u32 cmd_reg_value;
+ u32 time_cnt;
+
+ if (phy_addr > MDIO_MAX_PHY_ADDR) {
+ dev_err(mdio_dev->dev, "Wrong phy address: phy_addr(%x)\n",
+ phy_addr);
+ return -EINVAL;
+ }
+
+ dev_dbg(mdio_dev->dev, "mdio(%d) base is %p,write data=%d\n",
+ mdio_dev->gidx, mdio_dev->vbase, data);
+ dev_dbg(mdio_dev->dev, "phy_addr=%d, is_c45=%d, devad=%d, regnum=%#x\n",
+ phy_addr, is_c45, page, reg);
+
+ /* Step 1; waiting for MDIO_COMMAND_REG 's mdio_start==0,*/
+ /* after that can do read or wtrite*/
+ for (time_cnt = MDIO_TIMEOUT; time_cnt; time_cnt--) {
+ cmd_reg_value = MDIO_GET_REG_BIT(mdio_dev,
+ MDIO_COMMAND_REG,
+ MDIO_CMD_START_B);
+ if (!cmd_reg_value)
+ break;
+ }
+ if (cmd_reg_value) {
+ dev_err(mdio_dev->dev, "MDIO is busy\n");
+ return -EBUSY;
+ }
+
+ if (!is_c45) {
+ MDIO_SET_REG_FIELD(mdio_dev, MDIO_WDATA_REG, MDIO_WDATA_DATA_M,
+ MDIO_WDATA_DATA_S, data);
+
+ cmd_reg_value = MDIO_ST_CLAUSE_22 << MDIO_CMD_ST_S;
+ cmd_reg_value |= MDIO_C22_WRITE << MDIO_CMD_OP_S;
+ cmd_reg_value |=
+ (phy_addr & MDIO_CMD_PRTAD_M) << MDIO_CMD_PRTAD_S;
+ cmd_reg_value |= (reg & MDIO_CMD_DEVAD_M) << MDIO_CMD_DEVAD_S;
+ cmd_reg_value |= 1 << MDIO_CMD_START_B;
+
+ MDIO_WRITE_REG(mdio_dev, MDIO_COMMAND_REG, cmd_reg_value);
+ } else {
+ /* Step 2; config the cmd-reg to write addr*/
+ MDIO_SET_REG_FIELD(mdio_dev, MDIO_ADDR_REG, MDIO_ADDR_DATA_M,
+ MDIO_ADDR_DATA_S, reg);
+
+ cmd_reg_value = MDIO_ST_CLAUSE_45 << MDIO_CMD_ST_S;
+ cmd_reg_value |= MDIO_C45_WRITE_ADDR << MDIO_CMD_OP_S;
+
+ /* mdio_st==2'b00: is configing port addr*/
+ cmd_reg_value |=
+ (phy_addr & MDIO_CMD_PRTAD_M) << MDIO_CMD_PRTAD_S;
+
+ /* mdio_st == 2'b00: is configing dev_addr*/
+ cmd_reg_value |= (page & MDIO_CMD_DEVAD_M) << MDIO_CMD_DEVAD_S;
+ cmd_reg_value |= 1 << MDIO_CMD_START_B;
+ MDIO_WRITE_REG(mdio_dev, MDIO_COMMAND_REG, cmd_reg_value);
+
+ /* Step 3; waiting for MDIO_COMMAND_REG's mdio_start==0,*/
+ /* check for read or write opt is finished */
+ for (time_cnt = MDIO_TIMEOUT; time_cnt; time_cnt--) {
+ cmd_reg_value = MDIO_GET_REG_BIT(mdio_dev,
+ MDIO_COMMAND_REG,
+ MDIO_CMD_START_B);
+ if (!cmd_reg_value)
+ break;
+ }
+ if (cmd_reg_value) {
+ dev_err(mdio_dev->dev, "MDIO is busy\n");
+ return -EBUSY;
+ }
+
+ /* Step 4; config the data needed writing */
+ MDIO_SET_REG_FIELD(mdio_dev, MDIO_WDATA_REG, MDIO_WDATA_DATA_M,
+ MDIO_WDATA_DATA_S, data);
+
+ /* Step 5; config the cmd-reg for the write opt*/
+ cmd_reg_value = MDIO_ST_CLAUSE_45 << MDIO_CMD_ST_S;
+ cmd_reg_value |= MDIO_C45_WRITE_DATA << MDIO_CMD_OP_S;
+
+ /* mdio_st==2'b00: is configing port addr*/
+ cmd_reg_value |=
+ (phy_addr & MDIO_CMD_PRTAD_M) << MDIO_CMD_PRTAD_S;
+
+ /* mdio_st == 2'b00: is configing dev_addr*/
+ cmd_reg_value |= (page & MDIO_CMD_DEVAD_M) << MDIO_CMD_DEVAD_S;
+ cmd_reg_value |= 1 << MDIO_CMD_START_B;
+ MDIO_WRITE_REG(mdio_dev, MDIO_COMMAND_REG, cmd_reg_value);
+ }
+
+ return 0;
+}
+
+/**
+ * hns_mdio_get_chip_id - get parent node's chip-id attribute
+ * @dpev: mdio platform device
+ * return chip id if found the chip-id, or return 0
+ */
+static u32 hns_mdio_get_chip_id(struct platform_device *pdev)
+{
+ struct device_node *np;
+ u32 chip_id;
+ int ret;
+
+ np = of_get_parent(pdev->dev.of_node);
+ if (!np)
+ return 0;
+
+ ret = of_property_read_u32(np, "chip-id", &chip_id);
+ if (ret)
+ return 0;
+
+ return chip_id;
+}
+
+/**
+ * hns_mdio_write - access phy register
+ * @bus: mdio bus
+ * @phy_id: phy id
+ * @regnum: register num
+ * @value: register value
+ *
+ * Return 0 on success, negative on failure
+ */
+static int hns_mdio_write(struct mii_bus *bus,
+ int phy_id, int regnum, u16 value)
+{
+ int ret;
+ struct hns_mdio_device *mdio_dev = (struct hns_mdio_device *)bus->priv;
+ u8 devad = ((regnum >> 16) & 0x1f);
+ u8 is_c45 = !!(regnum & MII_ADDR_C45);
+
+ ret = hns_mdio_write_hw(mdio_dev, phy_id, is_c45, devad,
+ (u16)(regnum & 0xffff), value);
+ if (ret) {
+ dev_err(&bus->dev, "write_phy_reg fail, phy_id=%d, devad=%d,regnum=%#x, value=%#x!\n",
+ phy_id, devad, regnum, value);
+
+ return ret;
+ }
+
+ return 0;
+}
+
+/**
+ * hns_mdio_read - access phy register
+ * @bus: mdio bus
+ * @phy_id: phy id
+ * @regnum: register num
+ * @value: register value
+ *
+ * Return phy register value
+ */
+static int hns_mdio_read(struct mii_bus *bus, int phy_id, int regnum)
+{
+ int ret;
+ u16 reg_val = 0;
+ u8 devad = ((regnum >> 16) & 0x1f);
+ u8 is_c45 = !!(regnum & MII_ADDR_C45);
+ struct hns_mdio_device *mdio_dev = (struct hns_mdio_device *)bus->priv;
+
+ ret = hns_mdio_read_hw(mdio_dev, phy_id, is_c45, devad,
+ (u16)(regnum & 0xffff), ®_val);
+ if (ret) {
+ dev_err(&bus->dev, "read_phy_reg fail, mdio_idx=%d, phy_id=%d, devad=%d,regnum=%#x!\n",
+ mdio_dev->gidx, phy_id, devad, regnum);
+
+ return ret;
+ }
+
+ return reg_val;
+}
+
+/**
+ * hns_mdio_reset - reset mdio bus
+ * @bus: mdio bus
+ *
+ * Return 0 on success, negative on failure
+ */
+static int hns_mdio_reset(struct mii_bus *bus)
+{
+ return 0;
+}
+
+/**
+ * hns_mdio_bus_name - get mdio bus name
+ * @name: mdio bus name
+ * @np: mdio device node pointer
+ */
+static void hns_mdio_bus_name(char *name, struct device_node *np)
+{
+ const u32 *addr;
+ u64 taddr = OF_BAD_ADDR;
+
+ addr = of_get_address(np, 0, NULL, NULL);
+ if (addr)
+ taddr = of_translate_address(np, addr);
+
+ snprintf(name, MII_BUS_ID_SIZE, "%s@%llx", np->name,
+ (unsigned long long)taddr);
+}
+
+/**
+ * hns_mdio_probe - probe mdio device
+ * @pdev: mdio platform device
+ *
+ * Return 0 on success, negative on failure
+ */
+static int hns_mdio_probe(struct platform_device *pdev)
+{
+ struct device_node *np;
+ struct hns_mdio_device *mdio_dev;
+ struct mii_bus *new_bus;
+ struct resource *res;
+ int ret;
+
+ if (!pdev) {
+ dev_err(NULL, "pdev is NULL!\r\n");
+ return -ENODEV;
+ }
+ np = pdev->dev.of_node;
+ mdio_dev = devm_kzalloc(&pdev->dev, sizeof(*mdio_dev), GFP_KERNEL);
+ if (!mdio_dev)
+ return -ENOMEM;
+
+ new_bus = devm_mdiobus_alloc(&pdev->dev);
+ if (!new_bus) {
+ dev_err(&pdev->dev, "mdiobus_alloc fail!\n");
+ return -ENOMEM;
+ }
+
+ new_bus->name = MDIO_BUS_NAME;
+ new_bus->read = hns_mdio_read;
+ new_bus->write = hns_mdio_write;
+ new_bus->reset = hns_mdio_reset;
+ new_bus->priv = mdio_dev;
+ hns_mdio_bus_name(new_bus->id, np);
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ mdio_dev->vbase = devm_ioremap_resource(&pdev->dev, res);
+ if (IS_ERR(mdio_dev->vbase)) {
+ ret = PTR_ERR(mdio_dev->vbase);
+ return -EFAULT;
+ }
+ mdio_dev->index = 0;
+ mdio_dev->chip_id = hns_mdio_get_chip_id(pdev);
+ mdio_dev->gidx = mdio_dev->chip_id;
+ mdio_dev->dev = &pdev->dev;
+
+ new_bus->irq = devm_kcalloc(&pdev->dev, PHY_MAX_ADDR,
+ sizeof(int), GFP_KERNEL);
+ if (!new_bus->irq)
+ return -ENOMEM;
+
+ new_bus->parent = &pdev->dev;
+ platform_set_drvdata(pdev, new_bus);
+
+ ret = of_mdiobus_register(new_bus, np);
+ if (ret) {
+ dev_err(&pdev->dev, "Cannot register as MDIO bus!\n");
+ platform_set_drvdata(pdev, NULL);
+ return ret;
+ }
+
+ return 0;
+}
+
+/**
+ * hns_mdio_remove - remove mdio device
+ * @pdev: mdio platform device
+ *
+ * Return 0 on success, negative on failure
+ */
+static int hns_mdio_remove(struct platform_device *pdev)
+{
+ struct hns_mdio_device *mdio_dev;
+ struct mii_bus *bus;
+
+ bus = platform_get_drvdata(pdev);
+ mdio_dev = (struct hns_mdio_device *)bus->priv;
+
+ mdiobus_unregister(bus);
+ platform_set_drvdata(pdev, NULL);
+ return 0;
+}
+
+static const struct of_device_id hns_mdio_match[] = {
+ {.compatible = "hisilicon,mdio"},
+ {}
+};
+
+static struct platform_driver hns_mdio_driver = {
+ .probe = hns_mdio_probe,
+ .remove = hns_mdio_remove,
+ .driver = {
+ .name = MDIO_DRV_NAME,
+ .of_match_table = hns_mdio_match,
+ },
+};
+
+module_platform_driver(hns_mdio_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
+MODULE_DESCRIPTION("Hisilicon HNS MDIO driver");
+MODULE_ALIAS("platform:" MDIO_DRV_NAME);
--
1.9.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH 5/5] net: add Hisilicon Network Subsystem basic ethernet support
2015-08-14 10:30 [PATCH 0/5] net: Hisilicon Network Subsystem support Kenneth Lee
` (2 preceding siblings ...)
2015-08-14 10:30 ` [PATCH 3/5] net: add Hisilicon Network Subsystem MDIO support Kenneth Lee
@ 2015-08-14 10:30 ` Kenneth Lee
3 siblings, 0 replies; 15+ messages in thread
From: Kenneth Lee @ 2015-08-14 10:30 UTC (permalink / raw)
To: linux-arm-kernel
This is to add basic ethernet support for HNS. It is one of the way to use
the HNS acceleration engine. But most of the decoding/encoding capability
of the AE cannot be used in this way.
This submit contains the basic feature as a ethernet driver. More will be
added later.
Signed-off-by: Kenneth Lee <liguozhu@huawei.com>
Signed-off-by: Yisen Zhuang <Yisen.Zhuang@huawei.com>
---
drivers/net/ethernet/hisilicon/hns/hns_enet.c | 1552 ++++++++++++++++++++++
drivers/net/ethernet/hisilicon/hns/hns_enet.h | 81 ++
drivers/net/ethernet/hisilicon/hns/hns_ethtool.c | 1174 ++++++++++++++++
3 files changed, 2807 insertions(+)
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_enet.c
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_enet.h
create mode 100644 drivers/net/ethernet/hisilicon/hns/hns_ethtool.c
diff --git a/drivers/net/ethernet/hisilicon/hns/hns_enet.c b/drivers/net/ethernet/hisilicon/hns/hns_enet.c
new file mode 100644
index 0000000..b58d5ab
--- /dev/null
+++ b/drivers/net/ethernet/hisilicon/hns/hns_enet.c
@@ -0,0 +1,1552 @@
+/*
+ * Copyright (c) 2014-2015 Hisilicon Limited.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <linux/module.h>
+#include <linux/interrupt.h>
+#include <linux/etherdevice.h>
+#include <linux/platform_device.h>
+#include <linux/clk.h>
+#include <linux/skbuff.h>
+#include <linux/phy.h>
+#include <linux/io.h>
+#include <linux/ip.h>
+#include <linux/ipv6.h>
+#include <linux/if_vlan.h>
+#include "hnae.h"
+#include "hns_enet.h"
+
+#define NIC_MAX_Q_PER_VF 16
+#define HNS_NIC_TX_TIMEOUT (5 * HZ)
+
+#define SERVICE_TIMER_HZ (1 * HZ)
+
+#define NIC_TX_CLEAN_MAX_NUM 256
+#define NIC_RX_CLEAN_MAX_NUM 64
+
+#define RCB_ERR_PRINT_CYCLE 1000
+
+static inline void fill_desc(struct hnae_ring *ring, void *priv,
+ int size, dma_addr_t dma, int frag_end,
+ int buf_num, enum hns_desc_type type)
+{
+ struct hnae_desc *desc = &ring->desc[ring->next_to_use];
+ struct hnae_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_use];
+ struct sk_buff *skb;
+ __be16 protocol;
+ u32 ip_offset;
+ u32 asid_bufnum_pid = 0;
+ u32 flag_ipoffset = 0;
+
+ desc_cb->priv = priv;
+ desc_cb->length = size;
+ desc_cb->dma = dma;
+ desc_cb->type = type;
+
+ desc->addr = cpu_to_le64(dma);
+ desc->tx.send_size = cpu_to_le16((u16)size);
+
+ /*config bd buffer end */
+ flag_ipoffset |= 1 << HNS_TXD_VLD_B;
+
+ asid_bufnum_pid |= buf_num << HNS_TXD_BUFNUM_S;
+
+ if (type == DESC_TYPE_SKB) {
+ skb = (struct sk_buff *)priv;
+
+ if (skb->ip_summed == CHECKSUM_PARTIAL) {
+ protocol = skb->protocol;
+ ip_offset = ETH_HLEN;
+
+ /*if it is a SW VLAN check the next protocol*/
+ if (protocol == htons(ETH_P_8021Q)) {
+ ip_offset += VLAN_HLEN;
+ protocol = vlan_get_protocol(skb);
+ skb->protocol = protocol;
+ }
+
+ if (skb->protocol == ntohs(ETH_P_IP)) {
+ flag_ipoffset |= 1 << HNS_TXD_L3CS_B;
+ /* check for tcp/udp header */
+ flag_ipoffset |= 1 << HNS_TXD_L4CS_B;
+
+ } else if (skb->protocol == ntohs(ETH_P_IPV6)) {
+ /* ipv6 has not l3 cs, check for L4 header */
+ flag_ipoffset |= 1 << HNS_TXD_L4CS_B;
+ }
+
+ flag_ipoffset |= ip_offset << HNS_TXD_IPOFFSET_S;
+ }
+ }
+
+ flag_ipoffset |= frag_end << HNS_TXD_FE_B;
+
+ desc->tx.asid_bufnum_pid = cpu_to_le16(asid_bufnum_pid);
+ desc->tx.flag_ipoffset = cpu_to_le32(flag_ipoffset);
+
+ ring_ptr_move_fw(ring, next_to_use);
+}
+
+static inline void unfill_desc(struct hnae_ring *ring)
+{
+ ring_ptr_move_bw(ring, next_to_use);
+}
+
+int hns_nic_net_xmit_hw(struct net_device *ndev,
+ struct sk_buff *skb,
+ struct hns_nic_ring_data *ring_data)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct device *dev = priv->dev;
+ struct hnae_ring *ring = ring_data->ring;
+ struct netdev_queue *dev_queue;
+ struct skb_frag_struct *frag;
+ int buf_num;
+ dma_addr_t dma;
+ int size, next_to_use;
+ int i, j;
+ struct sk_buff *new_skb;
+
+ assert(ring->max_desc_num_per_pkt <= ring->desc_num);
+
+ /* no. of segments (plus a header) */
+ buf_num = skb_shinfo(skb)->nr_frags + 1;
+
+ if (unlikely(buf_num > ring->max_desc_num_per_pkt)) {
+ if (ring_space(ring) < 1)
+ return NETDEV_TX_BUSY;
+
+ new_skb = skb_copy(skb, GFP_ATOMIC);
+ if (!new_skb) {
+ ring->stats.sw_err_cnt++;
+ netdev_err(ndev, "no memory to xmit!\n");
+ dev_kfree_skb_any(skb);
+ return NETDEV_TX_OK;
+ }
+
+ dev_kfree_skb_any(skb);
+ skb = new_skb;
+ buf_num = 1;
+ assert(skb_shinfo(skb)->nr_frags == 1);
+ }
+
+ if (buf_num > ring_space(ring))
+ return NETDEV_TX_BUSY;
+
+ next_to_use = ring->next_to_use;
+
+ /* fill the first part */
+ size = skb_headlen(skb);
+ dma = dma_map_single(dev, skb->data, size, DMA_TO_DEVICE);
+ if (dma_mapping_error(dev, dma)) {
+ netdev_err(ndev, "TX head DMA map failed\n");
+ ring->stats.sw_err_cnt++;
+ goto out_map_head_fail;
+ }
+ fill_desc(ring, skb, size, dma, buf_num == 1 ? 1 : 0, buf_num,
+ DESC_TYPE_SKB);
+
+ /* fill the fragments */
+ for (i = 1; i < buf_num; i++) {
+ frag = &skb_shinfo(skb)->frags[i - 1];
+ size = skb_frag_size(frag);
+ dma = skb_frag_dma_map(dev, frag, 0, size, DMA_TO_DEVICE);
+ if (dma_mapping_error(dev, dma)) {
+ netdev_err(ndev, "TX frag(%d) DMA map failed\n", i);
+ ring->stats.sw_err_cnt++;
+ goto out_map_frag_fail;
+ }
+ fill_desc(ring, skb_frag_page(frag), size, dma,
+ buf_num - 1 == i ? 1 : 0, buf_num, DESC_TYPE_PAGE);
+ }
+
+ /*complete translate all packets*/
+ dev_queue = netdev_get_tx_queue(ndev, skb->queue_mapping);
+ netdev_tx_sent_queue(dev_queue, skb->len);
+
+ wmb(); /* commit all data before submit */
+ assert(skb->queue_mapping < priv->ae_handle->q_num);
+ hnae_queue_xmit(priv->ae_handle->qs[skb->queue_mapping], buf_num);
+ ring->stats.pkts.tx_pkts++;
+ ring->stats.bytes.tx_bytes += skb->len;
+
+ return NETDEV_TX_OK;
+
+out_map_frag_fail:
+
+ for (j = i - 1; j > 0; j--) {
+ unfill_desc(ring);
+ next_to_use = ring->next_to_use;
+ dma_unmap_page(dev, ring->desc_cb[next_to_use].dma,
+ ring->desc_cb[next_to_use].length,
+ DMA_TO_DEVICE);
+ }
+
+ unfill_desc(ring);
+ next_to_use = ring->next_to_use;
+ dma_unmap_single(dev, ring->desc_cb[next_to_use].dma,
+ ring->desc_cb[next_to_use].length, DMA_TO_DEVICE);
+
+out_map_head_fail:
+ return NETDEV_TX_BUSY;
+}
+
+/**
+ * hns_nic_get_headlen - determine size of header for RSC/LRO/GRO/FCOE
+ * @data: pointer to the start of the headers
+ * @max: total length of section to find headers in
+ *
+ * This function is meant to determine the length of headers that will
+ * be recognized by hardware for LRO, GRO, and RSC offloads. The main
+ * motivation of doing this is to only perform one pull for IPv4 TCP
+ * packets so that we can do basic things like calculating the gso_size
+ * based on the average data per packet.
+ **/
+static unsigned int hns_nic_get_headlen(unsigned char *data, u32 flag,
+ unsigned int max)
+{
+ unsigned char *network;
+ u8 hlen;
+
+ /* this should never happen, but better safe than sorry */
+ if (max < ETH_HLEN)
+ return max;
+
+ /* initialize network frame pointer */
+ network = data;
+
+ /* set first protocol and move network header forward */
+ network += ETH_HLEN;
+
+ /* handle any vlan tag if present */
+ if (hnae_get_field(flag, HNS_RXD_VLAN_M, HNS_RXD_VLAN_S)
+ == HNS_RX_FLAG_VLAN_PRESENT) {
+ if ((typeof(max))(network - data) > (max - VLAN_HLEN))
+ return max;
+
+ network += VLAN_HLEN;
+ }
+
+ /* handle L3 protocols */
+ if (hnae_get_field(flag, HNS_RXD_L3ID_M, HNS_RXD_L3ID_S)
+ == HNS_RX_FLAG_L3ID_IPV4) {
+ if ((typeof(max))(network - data) >
+ (max - sizeof(struct iphdr)))
+ return max;
+
+ /* access ihl as a u8 to avoid unaligned access on ia64 */
+ hlen = (network[0] & 0x0F) << 2;
+
+ /* verify hlen meets minimum size requirements */
+ if (hlen < sizeof(struct iphdr))
+ return network - data;
+
+ /* record next protocol if header is present */
+ } else if (hnae_get_field(flag, HNS_RXD_L3ID_M, HNS_RXD_L3ID_S)
+ == HNS_RX_FLAG_L3ID_IPV6) {
+ if ((typeof(max))(network - data) >
+ (max - sizeof(struct ipv6hdr)))
+ return max;
+
+ /* record next protocol */
+ hlen = sizeof(struct ipv6hdr);
+ } else {
+ return network - data;
+ }
+
+ /* relocate pointer to start of L4 header */
+ network += hlen;
+
+ /* finally sort out TCP/UDP */
+ if (hnae_get_field(flag, HNS_RXD_L4ID_M, HNS_RXD_L4ID_S)
+ == HNS_RX_FLAG_L4ID_TCP) {
+ if ((typeof(max))(network - data) >
+ (max - sizeof(struct tcphdr)))
+ return max;
+
+ /* access doff as a u8 to avoid unaligned access on ia64 */
+ hlen = (network[12] & 0xF0) >> 2;
+
+ /* verify hlen meets minimum size requirements */
+ if (hlen < sizeof(struct tcphdr))
+ return network - data;
+
+ network += hlen;
+ } else if (hnae_get_field(flag, HNS_RXD_L4ID_M, HNS_RXD_L4ID_S)
+ == HNS_RX_FLAG_L4ID_UDP) {
+ if ((typeof(max))(network - data) >
+ (max - sizeof(struct udphdr)))
+ return max;
+
+ network += sizeof(struct udphdr);
+ }
+
+ /* If everything has gone correctly network should be the
+ * data section of the packet and will be the end of the header.
+ * If not then it probably represents the end of the last recognized
+ * header.
+ */
+ if ((typeof(max))(network - data) < max)
+ return network - data;
+ else
+ return max;
+}
+
+static inline void hns_nic_reuse_page(struct hnae_desc_cb *desc_cb, int tsize,
+ int last_offset)
+{
+ /* avoid re-using remote pages,flag default unreuse */
+ if (likely(page_to_nid(desc_cb->priv) == numa_node_id())) {
+ /* move offset up to the next cache line */
+ desc_cb->page_offset += tsize;
+
+ if (desc_cb->page_offset <= last_offset) {
+ desc_cb->reuse_flag = 1;
+ /* bump ref count on page before it is given*/
+ get_page(desc_cb->priv);
+ }
+ }
+}
+
+static inline int hns_nic_poll_rx_skb(struct hns_nic_ring_data *ring_data,
+ struct sk_buff **out_skb, int *out_bnum)
+{
+ struct hnae_ring *ring = ring_data->ring;
+ struct net_device *ndev = ring_data->napi.dev;
+ struct sk_buff *skb;
+ struct hnae_desc *desc;
+ struct hnae_desc_cb *desc_cb;
+ unsigned char *va;
+ int bnum, length, size, i, truesize, last_offset;
+ int pull_len;
+ u32 bnum_flag;
+
+ last_offset = hnae_page_size(ring) - hnae_buf_size(ring);
+ desc = &ring->desc[ring->next_to_clean];
+ desc_cb = &ring->desc_cb[ring->next_to_clean];
+ length = le16_to_cpu(desc->rx.pkt_len);
+ bnum_flag = le32_to_cpu(desc->rx.ipoff_bnum_pid_flag);
+ bnum = hnae_get_field(bnum_flag, HNS_RXD_BUFNUM_M, HNS_RXD_BUFNUM_S);
+ *out_bnum = bnum;
+ va = (unsigned char *)desc_cb->buf + desc_cb->page_offset;
+
+ skb = *out_skb = napi_alloc_skb(&ring_data->napi, HNS_RX_HEAD_SIZE);
+ if (unlikely(!skb)) {
+ netdev_err(ndev, "alloc rx skb fail\n");
+ ring->stats.sw_err_cnt++;
+ return -ENOMEM;
+ }
+
+ if (length <= HNS_RX_HEAD_SIZE) {
+ memcpy(__skb_put(skb, length), va, ALIGN(length, sizeof(long)));
+
+ /* we can reuse buffer as-is, just make sure it is local */
+ if (likely(page_to_nid(desc_cb->priv) == numa_node_id()))
+ desc_cb->reuse_flag = 1;
+ else /* this page cannot be reused so discard it */
+ put_page(desc_cb->priv);
+
+ ring_ptr_move_fw(ring, next_to_clean);
+
+ if (unlikely(bnum != 1)) { /* check err*/
+ *out_bnum = 1;
+ goto out_bnum_err;
+ }
+ } else {
+ ring->stats.seg_pkt_cnt++;
+
+ pull_len = hns_nic_get_headlen(va, bnum_flag, HNS_RX_HEAD_SIZE);
+ memcpy(__skb_put(skb, pull_len), va,
+ ALIGN(pull_len, sizeof(long)));
+
+ size = le16_to_cpu(desc->rx.size);
+ truesize = ALIGN(size, L1_CACHE_BYTES);
+ skb_add_rx_frag(skb, 0, desc_cb->priv,
+ desc_cb->page_offset + pull_len,
+ size - pull_len, truesize - pull_len);
+
+ hns_nic_reuse_page(desc_cb, truesize, last_offset);
+ ring_ptr_move_fw(ring, next_to_clean);
+
+ if (unlikely(bnum >= (int)MAX_SKB_FRAGS)) { /* check err*/
+ *out_bnum = 1;
+ goto out_bnum_err;
+ }
+ for (i = 1; i < bnum; i++) {
+ desc = &ring->desc[ring->next_to_clean];
+ desc_cb = &ring->desc_cb[ring->next_to_clean];
+ size = le16_to_cpu(desc->rx.size);
+ truesize = ALIGN(size, L1_CACHE_BYTES);
+ skb_add_rx_frag(skb, i, desc_cb->priv,
+ desc_cb->page_offset,
+ size, truesize);
+
+ hns_nic_reuse_page(desc_cb, truesize, last_offset);
+ ring_ptr_move_fw(ring, next_to_clean);
+ }
+ }
+
+ /* check except process, free skb and jump the desc */
+ if (unlikely((!bnum) || (bnum > ring->max_desc_num_per_pkt))) {
+out_bnum_err:
+ *out_bnum = *out_bnum ? *out_bnum : 1; /* ntc moved,cannot 0*/
+ netdev_err(ndev, "invalid bnum(%d,%d,%d,%d),%016llx,%016llx\n",
+ bnum, ring->max_desc_num_per_pkt,
+ length, (int)MAX_SKB_FRAGS,
+ ((u64 *)desc)[0], ((u64 *)desc)[1]);
+ ring->stats.err_bd_num++;
+ dev_kfree_skb_any(skb);
+ return -EDOM;
+ }
+
+ bnum_flag = le32_to_cpu(desc->rx.ipoff_bnum_pid_flag);
+
+ if (unlikely(!hnae_get_bit(bnum_flag, HNS_RXD_VLD_B))) {
+ netdev_err(ndev, "no valid bd,%016llx,%016llx\n",
+ ((u64 *)desc)[0], ((u64 *)desc)[1]);
+ ring->stats.non_vld_descs++;
+ dev_kfree_skb_any(skb);
+ return -EINVAL;
+ }
+
+ if (unlikely((!desc->rx.pkt_len) ||
+ hnae_get_bit(bnum_flag, HNS_RXD_DROP_B))) {
+ if (!(ring->stats.err_pkt_len % RCB_ERR_PRINT_CYCLE))
+ netdev_dbg(ndev,
+ "pkt_len(%u),drop(%u),%#llx,%#llx\n",
+ le16_to_cpu(desc->rx.pkt_len),
+ hnae_get_bit(bnum_flag, HNS_RXD_DROP_B),
+ ((u64 *)desc)[0], ((u64 *)desc)[1]);
+ ring->stats.err_pkt_len++;
+ dev_kfree_skb_any(skb);
+ return -EFAULT;
+ }
+
+ if (unlikely(hnae_get_bit(bnum_flag, HNS_RXD_L2E_B) ||
+ hnae_get_bit(bnum_flag, HNS_RXD_L3E_B) ||
+ hnae_get_bit(bnum_flag, HNS_RXD_L4E_B))) {
+ if (!(ring->stats.csum_err % RCB_ERR_PRINT_CYCLE))
+ netdev_dbg(ndev,
+ "check err(%#x),%#llx,%#llx\n",
+ hnae_get_bit(bnum_flag, HNS_RXD_L2E_B) |
+ hnae_get_bit(bnum_flag, HNS_RXD_L3E_B) |
+ hnae_get_bit(bnum_flag, HNS_RXD_L4E_B),
+ ((u64 *)desc)[0], ((u64 *)desc)[1]);
+ ring->stats.csum_err++;
+ dev_kfree_skb_any(skb);
+ return -EFAULT;
+ }
+
+ ring->stats.pkts.rx_pkts++;
+ ring->stats.bytes.rx_bytes += skb->len;
+
+ return 0;
+}
+
+static inline void hns_nic_alloc_rx_buffers(struct hns_nic_ring_data *ring_data,
+ int cleand_count)
+{
+ int i, ret;
+ struct hnae_desc_cb res_cbs;
+ struct hnae_desc *desc;
+ struct hnae_desc_cb *desc_cb;
+ struct hnae_ring *ring = ring_data->ring;
+ struct net_device *ndev = ring_data->napi.dev;
+
+ for (i = 0; i < cleand_count; i++) {
+ desc = &ring->desc[ring->next_to_use];
+ desc_cb = &ring->desc_cb[ring->next_to_use];
+ if (desc_cb->reuse_flag) {
+ ring->stats.reuse_pg_cnt++;
+ hnae_reuse_buffer(ring, ring->next_to_use);
+ } else {
+ ret = hnae_reserve_buffer_map(ring, &res_cbs);
+ if (ret) {
+ ring->stats.sw_err_cnt++;
+ netdev_err(ndev, "hnae reserve buffer map failed.\n");
+ break;
+ }
+ hnae_replace_buffer(ring, ring->next_to_use, &res_cbs);
+ }
+
+ ring_ptr_move_fw(ring, next_to_use);
+ }
+
+ wmb(); /* make all data has been write before submit */
+ writel_relaxed(i, ring->io_base + RCB_REG_HEAD);
+}
+
+/* return error number for error or number of desc left to take
+ */
+static void hns_nic_rx_up_pro(struct hns_nic_ring_data *ring_data,
+ struct sk_buff *skb)
+{
+ struct net_device *ndev = ring_data->napi.dev;
+
+ skb->ip_summed = CHECKSUM_UNNECESSARY;
+ skb->protocol = eth_type_trans(skb, ndev);
+ (void)napi_gro_receive(&ring_data->napi, skb);
+ ndev->last_rx = jiffies;
+}
+
+static int hns_nic_rx_poll_one(struct hns_nic_ring_data *ring_data,
+ int budget, void *v)
+{
+ struct hnae_ring *ring = ring_data->ring;
+ struct sk_buff *skb;
+ int num, bnum, ex_num;
+#define RCB_NOF_ALLOC_RX_BUFF_ONCE 16
+ int recv_pkts, recv_bds, clean_count, err;
+
+ num = readl_relaxed(ring->io_base + RCB_REG_FBDNUM);
+ rmb(); /* make sure num taken effect before the other data is touched */
+
+ recv_pkts = 0, recv_bds = 0, clean_count = 0;
+recv:
+ while (recv_pkts < budget && recv_bds < num) {
+ /* reuse or realloc buffers*/
+ if (clean_count >= RCB_NOF_ALLOC_RX_BUFF_ONCE) {
+ hns_nic_alloc_rx_buffers(ring_data, clean_count);
+ clean_count = 0;
+ }
+
+ /* poll one pkg*/
+ err = hns_nic_poll_rx_skb(ring_data, &skb, &bnum);
+ if (unlikely(!skb)) /* this fault cannot be repaired */
+ break;
+
+ recv_bds += bnum;
+ clean_count += bnum;
+ if (unlikely(err)) /* do jump the err */
+ continue;
+
+ /* do update ip stack process*/
+ ((void (*)(struct hns_nic_ring_data *, struct sk_buff *))v)(
+ ring_data, skb);
+ recv_pkts++;
+ }
+
+ /* make all data has been write before submit */
+ if (clean_count > 0) {
+ hns_nic_alloc_rx_buffers(ring_data, clean_count);
+ clean_count = 0;
+ }
+
+ if (recv_pkts < budget) {
+ ex_num = readl_relaxed(ring->io_base + RCB_REG_FBDNUM);
+ rmb(); /*complete read rx ring bd number*/
+ if (ex_num > 0) {
+ num += ex_num;
+ goto recv;
+ }
+ }
+
+ return recv_pkts;
+}
+
+static inline void hns_nic_reclaim_one_desc(struct hnae_ring *ring,
+ int *bytes, int *pkts)
+{
+ struct hnae_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_clean];
+
+ (*pkts) += (desc_cb->type == DESC_TYPE_SKB);
+ (*bytes) += desc_cb->length;
+ /* desc_cb will be cleaned, after hnae_free_buffer_detach*/
+ hnae_free_buffer_detach(ring, ring->next_to_clean);
+
+ ring_ptr_move_fw(ring, next_to_clean);
+}
+
+static int is_valid_clean_head(struct hnae_ring *ring, int h)
+{
+ int u = ring->next_to_use;
+ int c = ring->next_to_clean;
+
+ if (unlikely(h > ring->desc_num))
+ return 0;
+
+ assert(u > 0 && u < ring->desc_num);
+ assert(c > 0 && c < ring->desc_num);
+ assert(u != c && h != c); /* must be checked before call this func */
+
+ return u > c ? (h > c && h <= u) : (h > c || h <= u);
+}
+
+/* netif_tx_lock will turn down the performance, set only when necessary */
+#ifdef CONFIG_NET_POLL_CONTROLLER
+#define NETIF_TX_LOCK(ndev) netif_tx_lock(ndev)
+#define NETIF_TX_UNLOCK(ndev) netif_tx_unlock(ndev)
+#else
+#define NETIF_TX_LOCK(ndev)
+#define NETIF_TX_UNLOCK(ndev)
+#endif
+/* reclaim all desc in one budget
+ * return error or number of desc left
+ */
+static int hns_nic_tx_poll_one(struct hns_nic_ring_data *ring_data,
+ int budget, void *v)
+{
+ struct hnae_ring *ring = ring_data->ring;
+ struct net_device *ndev = ring_data->napi.dev;
+ struct netdev_queue *dev_queue;
+ int head;
+ int bytes, pkts;
+
+ NETIF_TX_LOCK(ndev);
+
+ head = readl_relaxed(ring->io_base + RCB_REG_HEAD);
+ rmb(); /* make sure head is ready before touch any data */
+
+ if (is_ring_empty(ring) || head == ring->next_to_clean) {
+ NETIF_TX_UNLOCK(ndev);
+ return 0; /* no data to poll */
+ }
+
+ if (!is_valid_clean_head(ring, head)) {
+ netdev_err(ndev, "wrong head (%d, %d-%d)\n", head,
+ ring->next_to_use, ring->next_to_clean);
+ ring->stats.io_err_cnt++;
+ NETIF_TX_UNLOCK(ndev);
+ return -EIO;
+ }
+
+ bytes = 0;
+ pkts = 0;
+ while (head != ring->next_to_clean)
+ hns_nic_reclaim_one_desc(ring, &bytes, &pkts);
+
+ NETIF_TX_UNLOCK(ndev);
+
+ dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
+ netdev_tx_completed_queue(dev_queue, pkts, bytes);
+
+ if (unlikely(netif_tx_queue_stopped(dev_queue)))
+ netif_tx_wake_queue(dev_queue);
+
+ return 0;
+}
+
+static void hns_nic_tx_clr_all_bufs(struct hns_nic_ring_data *ring_data)
+{
+ struct hnae_ring *ring = ring_data->ring;
+ struct net_device *ndev = ring_data->napi.dev;
+ struct netdev_queue *dev_queue;
+ int head;
+ int bytes, pkts;
+
+ NETIF_TX_LOCK(ndev);
+
+ head = ring->next_to_use; /* ntu :soft setted ring position*/
+ bytes = 0;
+ pkts = 0;
+ while (head != ring->next_to_clean)
+ hns_nic_reclaim_one_desc(ring, &bytes, &pkts);
+
+ NETIF_TX_UNLOCK(ndev);
+
+ dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
+ netdev_tx_reset_queue(dev_queue);
+}
+
+static int hns_nic_common_poll(struct napi_struct *napi, int budget)
+{
+ struct hns_nic_ring_data *ring_data =
+ container_of(napi, struct hns_nic_ring_data, napi);
+ int clean_complete = ring_data->poll_one(
+ ring_data, budget, ring_data->ex_process);
+
+ if (clean_complete >= 0 && clean_complete < budget) {
+ napi_complete(napi);
+ ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
+ ring_data->ring, 0);
+ }
+
+ return clean_complete;
+}
+
+static irqreturn_t hns_irq_handle(int irq, void *dev)
+{
+ struct hns_nic_ring_data *ring_data = (struct hns_nic_ring_data *)dev;
+
+ ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
+ ring_data->ring, 1);
+ napi_schedule(&ring_data->napi);
+
+ return IRQ_HANDLED;
+}
+
+/**
+ *hns_nic_adjust_link - adjust net work mode by the phy stat or new param
+ *@ndev: net device
+ */
+static void hns_nic_adjust_link(struct net_device *ndev)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct hnae_handle *h = priv->ae_handle;
+
+ h->dev->ops->adjust_link(h, ndev->phydev->speed, ndev->phydev->duplex);
+}
+
+/**
+ *hns_nic_init_phy - init phy
+ *@ndev: net device
+ *@h: ae handle
+ * Return 0 on success, negative on failure
+ */
+int hns_nic_init_phy(struct net_device *ndev, struct hnae_handle *h)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct phy_device *phy_dev = NULL;
+
+ if (!h->phy_node)
+ return 0;
+
+ if (h->phy_if != PHY_INTERFACE_MODE_XGMII)
+ phy_dev = of_phy_connect(ndev, h->phy_node,
+ hns_nic_adjust_link, 0, h->phy_if);
+ else
+ phy_dev = of_phy_attach(ndev, h->phy_node, 0, h->phy_if);
+
+ if (unlikely(!phy_dev) || IS_ERR(phy_dev))
+ return !phy_dev ? -ENODEV : PTR_ERR(phy_dev);
+
+ phy_dev->supported &= h->if_support;
+ phy_dev->advertising = phy_dev->supported;
+
+ if (h->phy_if == PHY_INTERFACE_MODE_XGMII)
+ phy_dev->autoneg = false;
+
+ priv->phy = phy_dev;
+
+ return 0;
+}
+
+static int hns_nic_ring_open(struct net_device *netdev, int idx)
+{
+ struct hns_nic_priv *priv = netdev_priv(netdev);
+ struct hnae_handle *h = priv->ae_handle;
+
+ napi_enable(&priv->ring_data[idx].napi);
+
+ enable_irq(priv->ring_data[idx].ring->irq);
+ h->dev->ops->toggle_ring_irq(priv->ring_data[idx].ring, 0);
+
+ return 0;
+}
+
+static int hns_nic_net_set_mac_address(struct net_device *ndev, void *p)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct hnae_handle *h = priv->ae_handle;
+ struct sockaddr *mac_addr = p;
+ int ret;
+
+ if (!mac_addr || !is_valid_ether_addr((const u8 *)mac_addr->sa_data))
+ return -EADDRNOTAVAIL;
+
+ ret = h->dev->ops->set_mac_addr(h, mac_addr->sa_data);
+ if (ret) {
+ netdev_err(ndev, "set_mac_address fail, ret=%d!\n", ret);
+ return ret;
+ }
+
+ memcpy(ndev->dev_addr, mac_addr->sa_data, ndev->addr_len);
+
+ return 0;
+}
+
+void hns_nic_update_stats(struct net_device *netdev)
+{
+ struct hns_nic_priv *priv = netdev_priv(netdev);
+ struct hnae_handle *h = priv->ae_handle;
+
+ h->dev->ops->update_stats(h, &h->net_stats);
+
+ netdev->stats.rx_bytes = h->net_stats.rx_bytes;
+ netdev->stats.rx_packets = h->net_stats.rx_packets;
+ netdev->stats.tx_bytes = h->net_stats.tx_bytes;
+ netdev->stats.tx_packets = h->net_stats.tx_packets;
+ netdev->stats.rx_crc_errors = h->net_stats.rx_crc_errors;
+ netdev->stats.rx_frame_errors = h->net_stats.rx_frame_errors;
+ netdev->stats.rx_fifo_errors = h->net_stats.rx_fifo_errors;
+ netdev->stats.rx_length_errors = h->net_stats.rx_length_errors;
+ netdev->stats.multicast = h->net_stats.multicast;
+ netdev->stats.rx_errors = h->net_stats.rx_errors;
+ netdev->stats.rx_dropped = h->net_stats.rx_dropped;
+ netdev->stats.tx_errors = h->net_stats.tx_errors;
+ netdev->stats.tx_dropped = h->net_stats.tx_dropped;
+}
+
+/* set mac addr if it is configed. or leave it to the AE driver */
+static inline void hns_init_mac_addr(struct net_device *ndev)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct device_node *node = priv->dev->of_node;
+ const void *mac_addr_temp;
+
+ mac_addr_temp = of_get_mac_address(node);
+ if (mac_addr_temp && is_valid_ether_addr(mac_addr_temp)) {
+ memcpy(ndev->dev_addr, mac_addr_temp, ndev->addr_len);
+ } else {
+ eth_hw_addr_random(ndev);
+ dev_warn(priv->dev, "No valid mac, use random mac %pM",
+ ndev->dev_addr);
+ }
+}
+
+static void hns_nic_ring_close(struct net_device *netdev, int idx)
+{
+ struct hns_nic_priv *priv = netdev_priv(netdev);
+ struct hnae_handle *h = priv->ae_handle;
+
+ h->dev->ops->toggle_ring_irq(priv->ring_data[idx].ring, 1);
+ disable_irq(priv->ring_data[idx].ring->irq);
+
+ napi_disable(&priv->ring_data[idx].napi);
+}
+
+static int hns_nic_net_up(struct net_device *ndev)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct hnae_handle *h = priv->ae_handle;
+ int i, j, k;
+ int ret;
+
+ if (h->phy_node) {
+ ret = hns_nic_init_phy(ndev, h);
+ if (ret)
+ goto out;
+ }
+
+ for (i = 0; i < h->q_num * 2; i++) {
+ ret = hns_nic_ring_open(ndev, i);
+ if (ret)
+ goto out_has_some_queues;
+ }
+
+ for (k = 0; k < h->q_num; k++)
+ h->dev->ops->toggle_queue_status(h->qs[k], 1);
+
+ ret = h->dev->ops->set_mac_addr(h, ndev->dev_addr);
+ if (ret)
+ goto out_set_mac_addr_err;
+
+ ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
+ if (ret)
+ goto out_start_err;
+
+ if (priv->phy)
+ phy_start(priv->phy);
+
+ netif_tx_wake_all_queues(ndev);
+
+ clear_bit(NIC_STATE_DOWN, &priv->state);
+ (void)mod_timer(&priv->service_timer, jiffies + SERVICE_TIMER_HZ);
+
+ return 0;
+
+out_start_err:
+ netif_stop_queue(ndev);
+out_set_mac_addr_err:
+ for (k = 0; k < h->q_num; k++)
+ h->dev->ops->toggle_queue_status(h->qs[k], 0);
+out_has_some_queues:
+ for (j = i - 1; j >= 0; j--)
+ hns_nic_ring_close(ndev, j);
+out:
+ set_bit(NIC_STATE_DOWN, &priv->state);
+ return ret;
+}
+
+static void hns_nic_net_down(struct net_device *ndev)
+{
+ int i;
+ struct hnae_ae_ops *ops;
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+
+ if (test_and_set_bit(NIC_STATE_DOWN, &priv->state))
+ return;
+
+ (void)del_timer_sync(&priv->service_timer);
+ priv->link = 0;
+ netif_carrier_off(ndev);
+
+ if (priv->phy) {
+ phy_stop(priv->phy);
+ phy_disconnect(priv->phy);
+ priv->phy = NULL;
+ }
+ ops = priv->ae_handle->dev->ops;
+
+ if (ops->stop)
+ ops->stop(priv->ae_handle);
+
+ netif_tx_stop_all_queues(ndev);
+
+ for (i = priv->ae_handle->q_num - 1; i >= 0; i--) {
+ hns_nic_ring_close(ndev, i);
+ hns_nic_ring_close(ndev, i + priv->ae_handle->q_num);
+
+ /* clean tx buffers*/
+ hns_nic_tx_clr_all_bufs(priv->ring_data + i);
+ }
+}
+
+void hns_nic_net_reset(struct net_device *ndev)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct hnae_handle *handle = priv->ae_handle;
+
+ while (test_and_set_bit(NIC_STATE_RESETTING, &priv->state))
+ usleep_range(1000, 2000);
+
+ if (handle->dev->ops->stop)
+ handle->dev->ops->stop(handle);
+ msleep(100);
+
+ if (handle->dev->ops->reset)
+ handle->dev->ops->reset(handle);
+ (void)hnae_reinit_handle(handle);
+
+ clear_bit(NIC_STATE_RESETTING, &priv->state);
+}
+
+void hns_nic_net_reinit(struct net_device *netdev)
+{
+ struct hns_nic_priv *priv = netdev_priv(netdev);
+
+ priv->netdev->trans_start = jiffies;
+ while (test_and_set_bit(NIC_STATE_REINITING, &priv->state))
+ usleep_range(1000, 2000);
+
+ hns_nic_net_down(netdev);
+ hns_nic_net_reset(netdev);
+ (void)hns_nic_net_up(netdev);
+ clear_bit(NIC_STATE_REINITING, &priv->state);
+}
+
+static int hns_nic_net_open(struct net_device *ndev)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct hnae_handle *h = priv->ae_handle;
+ int ret;
+
+ if (test_bit(NIC_STATE_TESTING, &priv->state))
+ return -EBUSY;
+
+ priv->link = 0;
+ netif_carrier_off(ndev);
+
+ ret = netif_set_real_num_tx_queues(ndev, h->q_num);
+ if (ret < 0) {
+ netdev_err(ndev, "netif_set_real_num_tx_queues fail, ret=%d!\n",
+ ret);
+ return ret;
+ }
+
+ ret = netif_set_real_num_rx_queues(ndev, h->q_num);
+ if (ret < 0) {
+ netdev_err(ndev,
+ "netif_set_real_num_rx_queues fail, ret=%d!\n", ret);
+ return ret;
+ }
+
+ ret = hns_nic_net_up(ndev);
+ if (ret) {
+ netdev_err(ndev,
+ "hns net up fail, ret=%d!\n", ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int hns_nic_net_stop(struct net_device *ndev)
+{
+ hns_nic_net_down(ndev);
+
+ return 0;
+}
+
+static void hns_tx_timeout_reset(struct hns_nic_priv *priv);
+static void hns_nic_net_timeout(struct net_device *ndev)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+
+ hns_tx_timeout_reset(priv);
+}
+
+static int hns_nic_do_ioctl(struct net_device *netdev, struct ifreq *ifr,
+ int cmd)
+{
+ switch (cmd) {
+ default:
+ return -ENOTSUPP;
+ }
+}
+
+/* use only for netconsole to poll with the device without interrupt */
+#ifdef CONFIG_NET_POLL_CONTROLLER
+void hns_nic_poll_controller(struct net_device *ndev)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ unsigned long flags;
+ int i;
+
+ local_irq_save(flags);
+ for (i = 0; i < priv->ae_handle->q_num * 2; i++)
+ napi_schedule(&priv->ring_data[i].napi);
+ local_irq_restore(flags);
+}
+#endif
+
+static netdev_tx_t hns_nic_net_xmit(struct sk_buff *skb,
+ struct net_device *ndev)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ int ret;
+
+ assert(skb->queue_mapping < ndev->ae_handle->q_num);
+ ret = hns_nic_net_xmit_hw(ndev, skb,
+ &tx_ring_data(priv, skb->queue_mapping));
+ if (ret == NETDEV_TX_OK) {
+ ndev->trans_start = jiffies;
+ ndev->stats.tx_bytes += skb->len;
+ ndev->stats.tx_packets++;
+ }
+ return (netdev_tx_t)ret;
+}
+
+static int hns_nic_change_mtu(struct net_device *ndev, int new_mtu)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct hnae_handle *h = priv->ae_handle;
+ int ret;
+
+ /* MTU < 68 is an error and causes problems on some kernels */
+ if (new_mtu < 68)
+ return -EINVAL;
+
+ if (!h->dev->ops->set_mtu)
+ return -ENOTSUPP;
+
+ if (netif_running(ndev)) {
+ hns_nic_net_stop(ndev);
+ msleep(100);
+
+ ret = h->dev->ops->set_mtu(h, new_mtu);
+ if (ret)
+ netdev_err(ndev, "set mtu fail, return value %d\n",
+ ret);
+
+ if (hns_nic_net_open(ndev))
+ netdev_err(ndev, "hns net open fail\n");
+ } else {
+ ret = h->dev->ops->set_mtu(h, new_mtu);
+ }
+
+ if (!ret)
+ ndev->mtu = new_mtu;
+
+ return ret;
+}
+
+/**
+ * nic_set_multicast_list - set mutl mac address
+ * @netdev: net device
+ * @p: mac address
+ *
+ * return void
+ */
+void hns_set_multicast_list(struct net_device *ndev)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct hnae_handle *h = priv->ae_handle;
+ struct netdev_hw_addr *ha = NULL;
+
+ assert(priv);
+
+ if (netif_running(ndev))
+ return;
+
+ if (!h) {
+ netdev_err(ndev, "hnae handle is null\n");
+ return;
+ }
+
+ if (h->dev->ops->set_mc_addr) {
+ netdev_for_each_mc_addr(ha, ndev)
+ if (h->dev->ops->set_mc_addr(h, ha->addr))
+ netdev_err(ndev, "set multicast fail\n");
+ }
+}
+
+struct rtnl_link_stats64 *hns_nic_get_stats64(struct net_device *ndev,
+ struct rtnl_link_stats64 *stats)
+{
+ int idx = 0;
+ u64 tx_bytes = 0;
+ u64 rx_bytes = 0;
+ u64 tx_pkts = 0;
+ u64 rx_pkts = 0;
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct hnae_handle *h = priv->ae_handle;
+
+ for (idx = 0; idx < h->q_num; idx++) {
+ tx_bytes += h->qs[idx]->tx_ring.stats.bytes.tx_bytes;
+ tx_pkts += h->qs[idx]->tx_ring.stats.pkts.tx_pkts;
+ rx_bytes += h->qs[idx]->rx_ring.stats.bytes.rx_bytes;
+ rx_pkts += h->qs[idx]->rx_ring.stats.pkts.rx_pkts;
+ }
+
+ stats->tx_bytes = tx_bytes;
+ stats->tx_packets = tx_pkts;
+ stats->rx_bytes = rx_bytes;
+ stats->rx_packets = rx_pkts;
+
+ stats->rx_errors = ndev->stats.rx_errors;
+ stats->multicast = ndev->stats.multicast;
+ stats->rx_length_errors = ndev->stats.rx_length_errors;
+ stats->rx_crc_errors = ndev->stats.rx_crc_errors;
+ stats->rx_missed_errors = ndev->stats.rx_missed_errors;
+
+ stats->tx_errors = ndev->stats.tx_errors;
+ stats->rx_dropped = ndev->stats.rx_dropped;
+ stats->tx_dropped = ndev->stats.tx_dropped;
+ stats->collisions = ndev->stats.collisions;
+ stats->rx_over_errors = ndev->stats.rx_over_errors;
+ stats->rx_frame_errors = ndev->stats.rx_frame_errors;
+ stats->rx_fifo_errors = ndev->stats.rx_fifo_errors;
+ stats->tx_aborted_errors = ndev->stats.tx_aborted_errors;
+ stats->tx_carrier_errors = ndev->stats.tx_carrier_errors;
+ stats->tx_fifo_errors = ndev->stats.tx_fifo_errors;
+ stats->tx_heartbeat_errors = ndev->stats.tx_heartbeat_errors;
+ stats->tx_window_errors = ndev->stats.tx_window_errors;
+ stats->rx_compressed = ndev->stats.rx_compressed;
+ stats->tx_compressed = ndev->stats.tx_compressed;
+
+ return stats;
+}
+
+static const struct net_device_ops hns_nic_netdev_ops = {
+ .ndo_open = hns_nic_net_open,
+ .ndo_stop = hns_nic_net_stop,
+ .ndo_start_xmit = hns_nic_net_xmit,
+ .ndo_tx_timeout = hns_nic_net_timeout,
+ .ndo_set_mac_address = hns_nic_net_set_mac_address,
+ .ndo_change_mtu = hns_nic_change_mtu,
+ .ndo_do_ioctl = hns_nic_do_ioctl,
+ .ndo_get_stats64 = hns_nic_get_stats64,
+#ifdef CONFIG_NET_POLL_CONTROLLER
+ .ndo_poll_controller = hns_nic_poll_controller,
+#endif
+ .ndo_set_rx_mode = hns_set_multicast_list,
+};
+
+static void hns_nic_update_link_status(struct net_device *netdev)
+{
+ struct hns_nic_priv *priv = netdev_priv(netdev);
+
+ struct hnae_handle *h = priv->ae_handle;
+ int state = 1;
+
+ if (priv->phy) {
+ if (!genphy_update_link(priv->phy))
+ state = priv->phy->link;
+ else
+ state = 0;
+ }
+ state = state && h->dev->ops->get_status(h);
+
+ if (state != priv->link) {
+ if (state) {
+ netif_carrier_on(netdev);
+ netdev_info(netdev, "link up\n");
+ } else {
+ netif_carrier_off(netdev);
+ netdev_info(netdev, "link down\n");
+ }
+ priv->link = state;
+ }
+}
+
+/* for dumping key regs*/
+static void hns_nic_dump(struct hns_nic_priv *priv)
+{
+ struct hnae_ae_ops *ops = priv->ae_handle->dev->ops;
+ u32 *data, reg_num, i;
+
+ if (ops->get_regs_len && ops->get_regs) {
+ reg_num = ops->get_regs_len(priv->ae_handle);
+ reg_num = (reg_num + 3ul) & ~3ul;
+ data = kcalloc(reg_num, sizeof(u32), GFP_KERNEL);
+ if (data) {
+ ops->get_regs(priv->ae_handle, data);
+ for (i = 0; i < reg_num; i++)
+ pr_info("0x%08x: 0x%08x 0x%08x 0x%08x 0x%08x\n",
+ i, data[i], data[i + 1],
+ data[i + 2], data[i + 3]);
+ kfree(data);
+ }
+ }
+}
+
+/* for resetting suntask*/
+static void hns_nic_reset_subtask(struct hns_nic_priv *priv)
+{
+ enum hnae_port_type type = priv->ae_handle->port_type;
+
+ if (!test_bit(NIC_STATE2_RESET_REQUESTED, &priv->state))
+ return;
+ clear_bit(NIC_STATE2_RESET_REQUESTED, &priv->state);
+
+ /* If we're already down, removing or resetting, just bail */
+ if (test_bit(NIC_STATE_DOWN, &priv->state) ||
+ test_bit(NIC_STATE_REMOVING, &priv->state) ||
+ test_bit(NIC_STATE_RESETTING, &priv->state))
+ return;
+
+ hns_nic_dump(priv);
+ netdev_err(priv->netdev, "Reset %s port\n",
+ (type == HNAE_PORT_DEBUG ? "debug" : "business"));
+
+ rtnl_lock();
+ if (type == HNAE_PORT_DEBUG) {
+ hns_nic_net_reinit(priv->netdev);
+ } else {
+ hns_nic_net_down(priv->netdev);
+ hns_nic_net_reset(priv->netdev);
+ }
+ rtnl_unlock();
+}
+
+/* for doing service complete*/
+static void hns_nic_service_event_complete(struct hns_nic_priv *priv)
+{
+ BUG_ON(!test_bit(NIC_STATE_SERVICE_SCHED, &priv->state));
+
+ smp_mb__before_atomic();
+ clear_bit(NIC_STATE_SERVICE_SCHED, &priv->state);
+}
+
+static void hns_nic_service_task(struct work_struct *work)
+{
+ struct hns_nic_priv *priv
+ = container_of(work, struct hns_nic_priv, service_task);
+ struct hnae_handle *h = priv->ae_handle;
+
+ hns_nic_update_link_status(priv->netdev);
+ h->dev->ops->update_led_status(h);
+ hns_nic_update_stats(priv->netdev);
+
+ hns_nic_reset_subtask(priv);
+ hns_nic_service_event_complete(priv);
+}
+
+static void hns_nic_task_schedule(struct hns_nic_priv *priv)
+{
+ if (!test_bit(NIC_STATE_DOWN, &priv->state) &&
+ !test_bit(NIC_STATE_REMOVING, &priv->state) &&
+ !test_and_set_bit(NIC_STATE_SERVICE_SCHED, &priv->state))
+ (void)schedule_work(&priv->service_task);
+}
+
+static void hns_nic_service_timer(unsigned long data)
+{
+ struct hns_nic_priv *priv = (struct hns_nic_priv *)data;
+
+ (void)mod_timer(&priv->service_timer, jiffies + SERVICE_TIMER_HZ);
+
+ hns_nic_task_schedule(priv);
+}
+
+/**
+ * hns_tx_timeout_reset - initiate reset due to Tx timeout
+ * @priv: driver private struct
+ **/
+static void hns_tx_timeout_reset(struct hns_nic_priv *priv)
+{
+ /* Do the reset outside of interrupt context */
+ if (!test_bit(NIC_STATE_DOWN, &priv->state)) {
+ set_bit(NIC_STATE2_RESET_REQUESTED, &priv->state);
+ netdev_warn(priv->netdev,
+ "initiating reset due to tx timeout(%llu,0x%lx)\n",
+ priv->tx_timeout_count, priv->state);
+ priv->tx_timeout_count++;
+ hns_nic_task_schedule(priv);
+ }
+}
+
+static int hns_nic_init_ring_data(struct hns_nic_priv *priv)
+{
+ struct hnae_handle *h = priv->ae_handle;
+ struct hns_nic_ring_data *rd;
+ int i, j;
+ int ret;
+
+ if (h->q_num > NIC_MAX_Q_PER_VF) {
+ ret = -EINVAL;
+ netdev_err(priv->netdev, "too much queue (%d)\n", h->q_num);
+ goto out;
+ }
+
+ priv->ring_data = kzalloc(h->q_num * sizeof(*priv->ring_data) * 2,
+ GFP_KERNEL);
+ if (!priv->ring_data) {
+ ret = -ENOMEM;
+ netdev_err(priv->netdev, "malloc ring data fail\n");
+ goto out;
+ }
+
+ for (i = 0; i < h->q_num; i++) {
+ rd = &priv->ring_data[i];
+ rd->queue_index = i;
+ rd->ring = &h->qs[i]->tx_ring;
+ rd->poll_one = hns_nic_tx_poll_one;
+
+ snprintf(rd->ring->ring_name, RCB_RING_NAME_LEN, "eth%s-tx%d",
+ h->ae_opts, rd->queue_index);
+ rd->ring->ring_name[RCB_RING_NAME_LEN - 1] = '\0';
+
+ ret = request_irq(rd->ring->irq,
+ hns_irq_handle, 0, rd->ring->ring_name, rd);
+ if (ret) {
+ netdev_err(priv->netdev, "request irq(%d) fail\n",
+ rd->ring->irq);
+ goto out_has_some_queues;
+ }
+ disable_irq(rd->ring->irq);
+ netif_napi_add(priv->netdev, &rd->napi,
+ hns_nic_common_poll, NIC_TX_CLEAN_MAX_NUM);
+ }
+ for (i = h->q_num; i < h->q_num * 2; i++) {
+ rd = &priv->ring_data[i];
+ rd->queue_index = i - h->q_num;
+ rd->ring = &h->qs[i - h->q_num]->rx_ring;
+ rd->poll_one = hns_nic_rx_poll_one;
+ rd->ex_process = hns_nic_rx_up_pro;
+
+ snprintf(rd->ring->ring_name, RCB_RING_NAME_LEN, "eth%s-rx%d",
+ h->ae_opts, rd->queue_index);
+ rd->ring->ring_name[RCB_RING_NAME_LEN - 1] = '\0';
+
+ ret = request_irq(rd->ring->irq,
+ hns_irq_handle, 0, rd->ring->ring_name, rd);
+ if (ret) {
+ netdev_err(priv->netdev, "request irq(%d) fail\n",
+ rd->ring->irq);
+ goto out_has_some_queues;
+ }
+ disable_irq(rd->ring->irq);
+ netif_napi_add(priv->netdev, &rd->napi,
+ hns_nic_common_poll, NIC_RX_CLEAN_MAX_NUM);
+ }
+
+ return 0;
+
+out_has_some_queues:
+ for (j = i - 1; j >= 0; j--) {
+ netif_napi_del(&priv->ring_data[j].napi);
+ free_irq(priv->ring_data[j].ring->irq, &priv->ring_data[j]);
+ }
+ kfree(priv->ring_data);
+ priv->ring_data = NULL;
+out:
+ return ret;
+}
+
+static void hns_nic_uninit_ring_data(struct hns_nic_priv *priv)
+{
+ struct hnae_handle *h = priv->ae_handle;
+ int i;
+
+ for (i = 0; i < h->q_num * 2; i++) {
+ netif_napi_del(&priv->ring_data[i].napi);
+ free_irq(priv->ring_data[i].ring->irq, &priv->ring_data[i]);
+ }
+ kfree(priv->ring_data);
+}
+
+static int hns_nic_try_get_ae(struct net_device *ndev)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct hnae_handle *h;
+ int ret;
+
+ h = hnae_get_handle(&priv->netdev->dev,
+ priv->ae_name, priv->ae_opts, NULL);
+ if (IS_ERR_OR_NULL(h)) {
+ ret = PTR_ERR(h);
+ dev_dbg(priv->dev, "has not handle, register notifier!\n");
+ goto out;
+ }
+ priv->ae_handle = h;
+
+ ret = hns_nic_init_ring_data(priv);
+ if (ret) {
+ ret = -ENOMEM;
+ goto out_init_ring_data;
+ }
+
+ ret = register_netdev(ndev);
+ if (ret) {
+ dev_err(priv->dev, "probe register netdev fail!\n");
+ goto out_reg_ndev_fail;
+ }
+ return 0;
+
+out_reg_ndev_fail:
+ hns_nic_uninit_ring_data(priv);
+ priv->ring_data = NULL;
+out_init_ring_data:
+ hnae_put_handle(priv->ae_handle);
+ priv->ae_handle = NULL;
+out:
+ return ret;
+}
+
+static int hns_nic_notifier_action(struct notifier_block *nb,
+ unsigned long action, void *data)
+{
+ struct hns_nic_priv *priv =
+ container_of(nb, struct hns_nic_priv, notifier_block);
+
+ assert(action == HNAE_AE_REGISTER);
+
+ if (!hns_nic_try_get_ae(priv->netdev)) {
+ hnae_unregister_notifier(&priv->notifier_block);
+ priv->notifier_block.notifier_call = NULL;
+ }
+ return 0;
+}
+
+static int hns_nic_dev_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct net_device *ndev;
+ struct hns_nic_priv *priv;
+ struct device_node *node = dev->of_node;
+ int ret;
+
+ ndev = alloc_etherdev_mq(sizeof(struct hns_nic_priv), NIC_MAX_Q_PER_VF);
+ if (!ndev)
+ return -ENOMEM;
+
+ platform_set_drvdata(pdev, ndev);
+
+ priv = netdev_priv(ndev);
+ priv->dev = dev;
+ priv->netdev = ndev;
+
+ ret = of_property_read_string(node, "ae-name", &priv->ae_name);
+ if (ret)
+ goto out_read_string_fail;
+
+ ret = of_property_read_string(node, "ae-opts", &priv->ae_opts);
+ if (ret)
+ goto out_read_string_fail;
+
+ hns_init_mac_addr(ndev);
+
+ ndev->watchdog_timeo = HNS_NIC_TX_TIMEOUT;
+ ndev->priv_flags |= IFF_UNICAST_FLT;
+ ndev->netdev_ops = &hns_nic_netdev_ops;
+ hns_ethtool_set_ops(ndev);
+ ndev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
+ NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
+ NETIF_F_GRO;
+ ndev->vlan_features |=
+ NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
+ ndev->vlan_features |= NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
+
+ SET_NETDEV_DEV(ndev, dev);
+
+ if (!dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
+ dev_dbg(dev, "set mask to 64bit\n");
+ else
+ dev_err(dev, "set mask to 32bit fail!\n");
+
+ /* carrier off reporting is important to ethtool even BEFORE open */
+ netif_carrier_off(ndev);
+
+ setup_timer(&priv->service_timer, hns_nic_service_timer,
+ (unsigned long)priv);
+ INIT_WORK(&priv->service_task, hns_nic_service_task);
+
+ set_bit(NIC_STATE_SERVICE_INITED, &priv->state);
+ clear_bit(NIC_STATE_SERVICE_SCHED, &priv->state);
+ set_bit(NIC_STATE_DOWN, &priv->state);
+
+ if (hns_nic_try_get_ae(priv->netdev)) {
+ priv->notifier_block.notifier_call = hns_nic_notifier_action;
+ ret = hnae_register_notifier(&priv->notifier_block);
+ if (ret) {
+ dev_err(dev, "register notifier fail!\n");
+ goto out_notify_fail;
+ }
+ dev_dbg(dev, "has not handle, register notifier!\n");
+ }
+
+ return 0;
+
+out_notify_fail:
+ (void)cancel_work_sync(&priv->service_task);
+out_read_string_fail:
+ free_netdev(ndev);
+ return ret;
+}
+
+static int hns_nic_dev_remove(struct platform_device *pdev)
+{
+ struct net_device *ndev = platform_get_drvdata(pdev);
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+
+ if (ndev->reg_state != NETREG_UNINITIALIZED)
+ unregister_netdev(ndev);
+
+ if (priv->ring_data)
+ hns_nic_uninit_ring_data(priv);
+ priv->ring_data = NULL;
+
+ if (!IS_ERR_OR_NULL(priv->ae_handle))
+ hnae_put_handle(priv->ae_handle);
+ priv->ae_handle = NULL;
+ if (priv->notifier_block.notifier_call)
+ hnae_unregister_notifier(&priv->notifier_block);
+ priv->notifier_block.notifier_call = NULL;
+
+ set_bit(NIC_STATE_REMOVING, &priv->state);
+ (void)cancel_work_sync(&priv->service_task);
+
+ free_netdev(ndev);
+ return 0;
+}
+
+static const struct of_device_id hns_enet_of_match[] = {
+ {.compatible = "hisilicon,hns-nic",},
+ {},
+};
+
+MODULE_DEVICE_TABLE(of, hns_enet_of_match);
+
+static struct platform_driver hns_nic_dev_driver = {
+ .driver = {
+ .name = "hns-nic",
+ .owner = THIS_MODULE,
+ .of_match_table = hns_enet_of_match,
+ },
+ .probe = hns_nic_dev_probe,
+ .remove = hns_nic_dev_remove,
+};
+
+module_platform_driver(hns_nic_dev_driver);
+
+MODULE_DESCRIPTION("HISILICON HNS Ethernet driver");
+MODULE_AUTHOR("Hisilicon, Inc.");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:hns-nic");
diff --git a/drivers/net/ethernet/hisilicon/hns/hns_enet.h b/drivers/net/ethernet/hisilicon/hns/hns_enet.h
new file mode 100644
index 0000000..f632da4
--- /dev/null
+++ b/drivers/net/ethernet/hisilicon/hns/hns_enet.h
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2014-2015 Hisilicon Limited.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#ifndef __HNS_ENET_H
+#define __HNS_ENET_H
+
+#include <linux/timer.h>
+#include <linux/workqueue.h>
+#include <linux/of_net.h>
+#include <linux/of_mdio.h>
+#include <linux/netdevice.h>
+#include "hnae.h"
+
+enum hns_nic_state {
+ NIC_STATE_TESTING = 0,
+ NIC_STATE_RESETTING,
+ NIC_STATE_REINITING,
+ NIC_STATE_DOWN,
+ NIC_STATE_DISABLED,
+ NIC_STATE_REMOVING,
+ NIC_STATE_SERVICE_INITED,
+ NIC_STATE_SERVICE_SCHED,
+ NIC_STATE2_RESET_REQUESTED,
+ NIC_STATE_MAX
+};
+
+struct hns_nic_ring_data {
+ struct hnae_ring *ring;
+ struct napi_struct napi;
+ int queue_index;
+ int (*poll_one)(struct hns_nic_ring_data *, int, void *);
+ void (*ex_process)(struct hns_nic_ring_data *, struct sk_buff *);
+};
+
+struct hns_nic_priv {
+ const char *ae_name;
+ const char *ae_opts;
+ int phy_mode;
+ int phy_led_val;
+ struct phy_device *phy;
+ struct net_device *netdev;
+ struct device *dev;
+ struct hnae_handle *ae_handle;
+
+ /* the cb for nic to manage the ring buffer, the first half of the
+ * array is for tx_ring and vice versa for the second half
+ */
+ struct hns_nic_ring_data *ring_data;
+
+ /* The most recently read link state */
+ int link;
+ u64 tx_timeout_count;
+
+ unsigned long state;
+
+ struct timer_list service_timer;
+
+ struct work_struct service_task;
+
+ struct notifier_block notifier_block;
+};
+
+#define tx_ring_data(priv, idx) ((priv)->ring_data[idx])
+#define rx_ring_data(priv, idx) \
+ ((priv)->ring_data[(priv)->ae_handle->q_num + (idx)])
+
+void hns_ethtool_set_ops(struct net_device *ndev);
+void hns_nic_net_reset(struct net_device *ndev);
+void hns_nic_net_reinit(struct net_device *netdev);
+int hns_nic_init_phy(struct net_device *ndev, struct hnae_handle *h);
+int hns_nic_net_xmit_hw(struct net_device *ndev,
+ struct sk_buff *skb,
+ struct hns_nic_ring_data *ring_data);
+
+#endif /**__HNS_ENET_H */
diff --git a/drivers/net/ethernet/hisilicon/hns/hns_ethtool.c b/drivers/net/ethernet/hisilicon/hns/hns_ethtool.c
new file mode 100644
index 0000000..18f3fc5
--- /dev/null
+++ b/drivers/net/ethernet/hisilicon/hns/hns_ethtool.c
@@ -0,0 +1,1174 @@
+/*
+ * Copyright (c) 2014-2015 Hisilicon Limited.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <linux/module.h>
+#include <linux/interrupt.h>
+#include <linux/etherdevice.h>
+#include <linux/platform_device.h>
+#include "hns_enet.h"
+
+#define HNS_PHY_PAGE_REG 22
+#define HNS_PHY_PAGE_LED 3
+#define HNS_LED_FORCE_ON 9
+#define HNS_LED_FORCE_OFF 8
+#define HNS_PHY_PAGE_COPPER 0
+#define HNS_LED_FCR 16
+#define HNS_LED_PCR 17
+#define HNS_CHIP_VERSION 660
+
+#define HNS_NET_STATS_CNT 25
+
+/**
+ *hns_nic_get_link - get current link status
+ *@net_dev: net_device
+ *retuen 0 - success , negative --fail
+ */
+static u32 hns_nic_get_link(struct net_device *net_dev)
+{
+ struct hns_nic_priv *priv = netdev_priv(net_dev);
+ u32 link_stat = priv->link;
+ struct hnae_handle *h;
+
+ assert(priv && priv->ae_handle);
+ h = priv->ae_handle;
+
+ if (priv->phy) {
+ if (!genphy_update_link(priv->phy))
+ link_stat = priv->phy->link;
+ else
+ link_stat = 0;
+ }
+
+ if (h->dev && h->dev->ops && h->dev->ops->get_status)
+ link_stat = link_stat && h->dev->ops->get_status(h);
+ else
+ link_stat = 0;
+
+ return link_stat;
+}
+
+/**
+ *hns_nic_get_settings - implement ethtool get settings
+ *@net_dev: net_device
+ *@cmd: ethtool_cmd
+ *retuen 0 - success , negative --fail
+ */
+static int hns_nic_get_settings(struct net_device *net_dev,
+ struct ethtool_cmd *cmd)
+{
+ struct hns_nic_priv *priv = netdev_priv(net_dev);
+ struct hnae_handle *h;
+ u32 link_stat;
+ int ret;
+ u8 auto_neg, duplex;
+ u16 speed;
+
+ assert(priv && priv->ae_handle)
+ h = priv->ae_handle;
+
+ link_stat = hns_nic_get_link(net_dev);
+
+ if (h->dev->ops->get_info) {
+ ret = h->dev->ops->get_info(h, &auto_neg, &speed, &duplex);
+ if (ret < 0) {
+ netdev_err(net_dev,
+ "%s get_info error!\n", __func__);
+ return -EINVAL;
+ }
+
+ cmd->autoneg = auto_neg;
+
+ if (link_stat) {
+ ethtool_cmd_speed_set(cmd, speed);
+ cmd->duplex = duplex;
+ } else {
+ ethtool_cmd_speed_set(cmd, (u32)SPEED_UNKNOWN);
+ cmd->duplex = DUPLEX_UNKNOWN;
+ }
+
+ if (auto_neg) {
+ cmd->supported |= SUPPORTED_Autoneg;
+ cmd->advertising |= ADVERTISED_Autoneg;
+ cmd->autoneg = AUTONEG_ENABLE;
+ } else {
+ cmd->autoneg = AUTONEG_DISABLE;
+ }
+ } else {
+ ethtool_cmd_speed_set(cmd, (u32)SPEED_UNKNOWN);
+ cmd->duplex = DUPLEX_UNKNOWN;
+ }
+
+ /* Phy exists, then update data from phy standard drv. */
+ if (priv->phy)
+ (void)phy_ethtool_gset(priv->phy, cmd);
+
+ if (PHY_INTERFACE_MODE_SGMII == h->phy_if) {
+ cmd->supported |= SUPPORTED_Autoneg;
+ cmd->supported |= SUPPORTED_10baseT_Half;
+ cmd->supported |= SUPPORTED_10baseT_Full;
+ cmd->supported |= SUPPORTED_100baseT_Half;
+ cmd->supported |= SUPPORTED_100baseT_Full;
+ cmd->supported |= SUPPORTED_1000baseT_Full;
+ cmd->supported |= SUPPORTED_1000baseT_Half;
+ cmd->supported |= SUPPORTED_2500baseX_Full;
+ cmd->supported |= SUPPORTED_FIBRE;
+ cmd->supported |= SUPPORTED_TP;
+
+ cmd->advertising |= ADVERTISED_1000baseT_Full;
+ cmd->advertising |= ADVERTISED_1000baseT_Half;
+
+ /* Update port type. */
+ cmd->port = PORT_TP;
+ } else if (PHY_INTERFACE_MODE_XGMII == h->phy_if) {
+ cmd->supported |= SUPPORTED_10000baseR_FEC;
+ cmd->supported |= SUPPORTED_10000baseKR_Full;
+ cmd->supported |= SUPPORTED_FIBRE;
+
+ cmd->advertising |= ADVERTISED_10000baseKR_Full;
+
+ /* Update port type. */
+ cmd->port = PORT_FIBRE;
+
+ if (link_stat) {
+ ethtool_cmd_speed_set(cmd, SPEED_10000);
+ cmd->duplex = DUPLEX_FULL;
+ }
+ }
+
+ cmd->transceiver = XCVR_EXTERNAL;
+ cmd->mdio_support = (ETH_MDIO_SUPPORTS_C45 | ETH_MDIO_SUPPORTS_C22);
+
+ return 0;
+}
+
+/**
+ *hns_nic_set_settings - implement ethtool set settings
+ *@net_dev: net_device
+ *@cmd: ethtool_cmd
+ *retuen 0 - success , negative --fail
+ */
+static int hns_nic_set_settings(struct net_device *net_dev,
+ struct ethtool_cmd *cmd)
+{
+ struct hns_nic_priv *priv = netdev_priv(net_dev);
+ struct hnae_handle *h;
+ int ret;
+
+ if (!priv || !priv->ae_handle)
+ return -ENODEV;
+
+ h = priv->ae_handle;
+
+ if (!h->dev->ops->set_info)
+ return -ENODEV;
+
+ if (!netif_running(net_dev))
+ return -ESRCH;
+
+ ret = h->dev->ops->set_info(h, cmd->autoneg, cmd->speed, cmd->duplex);
+ if (ret) {
+ netdev_err(net_dev,
+ "set settings error!\n");
+ return ret;
+ }
+
+ if (priv->phy) {
+ (void)phy_ethtool_sset(priv->phy, cmd);
+ return 0;
+ }
+
+ /* Adjust the current link if link up. */
+ if (h->dev->ops->adjust_link && hns_nic_get_link(net_dev))
+ h->dev->ops->adjust_link(h, cmd->speed, cmd->duplex);
+ else
+ netdev_err(net_dev, "%s: not support setting!\n", __func__);
+
+ return 0;
+}
+
+static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
+ "Mac Loopback test",
+ "Serdes Loopback test",
+ "Phy Loopback test"
+};
+
+static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
+{
+#define COPPER_CONTROL_REG 0
+#define PHY_LOOP_BACK BIT(14)
+ u16 val = 0;
+
+ if (phy_dev->is_c45) /* c45 branch adding for XGE PHY */
+ return -ENOTSUPP;
+
+ if (en) {
+ /* speed : 1000M */
+ (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
+ HNS_PHY_PAGE_REG, 2);
+ (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
+ 21, 0x1046);
+ /* Force Master */
+ (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
+ 9, 0x1F00);
+ /* Soft-reset */
+ (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
+ 0, 0x9140);
+ /* If autoneg disabled,two soft-reset operations */
+ (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
+ 0, 0x9140);
+ (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
+ 22, 0xFA);
+
+ /* Default is 0x0400 */
+ (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
+ 1, 0x418);
+
+ /* Force 1000M Link, Default is 0x0200 */
+ (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
+ 7, 0x20C);
+ (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
+ 22, 0);
+
+ /* Enable MAC loop-back */
+ val = (u16)mdiobus_read(phy_dev->bus, phy_dev->addr,
+ COPPER_CONTROL_REG);
+ val |= PHY_LOOP_BACK;
+ (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
+ COPPER_CONTROL_REG, val);
+ } else {
+ (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
+ 22, 0xFA);
+ (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
+ 1, 0x400);
+ (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
+ 7, 0x200);
+ (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
+ 22, 0);
+
+ val = (u16)mdiobus_read(phy_dev->bus, phy_dev->addr,
+ COPPER_CONTROL_REG);
+ val &= ~PHY_LOOP_BACK;
+ (void)mdiobus_write(phy_dev->bus, phy_dev->addr,
+ COPPER_CONTROL_REG, val);
+ }
+ return 0;
+}
+
+static int __lb_setup(struct net_device *ndev,
+ enum hnae_loop loop)
+{
+ int ret = -ENOTSUPP;
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct phy_device *phy_dev = priv->phy;
+ struct hnae_handle *h = priv->ae_handle;
+
+ switch (loop) {
+ case MAC_INTERNALLOOP_PHY:
+ if (phy_dev)
+ ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
+ break;
+ case MAC_INTERNALLOOP_MAC:
+ case MAC_INTERNALLOOP_SERDES:
+ if (h->dev->ops->set_loopback)
+ ret = h->dev->ops->set_loopback(h, loop, 0x1);
+ break;
+ case MAC_LOOP_NONE:
+ if (phy_dev)
+ ret = hns_nic_config_phy_loopback(phy_dev, 0x0);
+
+ if (h->dev->ops->set_loopback) {
+ ret |= h->dev->ops->set_loopback(h,
+ MAC_INTERNALLOOP_MAC, 0x0);
+ ret |= h->dev->ops->set_loopback(h,
+ MAC_INTERNALLOOP_SERDES, 0x0);
+ }
+ break;
+ default:
+ break;
+ }
+
+ return ret;
+}
+
+static int __lb_up(struct net_device *ndev,
+ enum hnae_loop loop_mode)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct hnae_handle *h = priv->ae_handle;
+ int speed, duplex;
+ int ret;
+
+ hns_nic_net_reset(ndev);
+
+ if (h->phy_node) {
+ ret = hns_nic_init_phy(ndev, h);
+ if (ret)
+ return ret;
+ }
+
+ ret = __lb_setup(ndev, loop_mode);
+ if (ret)
+ return ret;
+ msleep(100);
+
+ ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
+ if (ret)
+ return ret;
+
+ if (priv->phy)
+ phy_start(priv->phy);
+
+ /* link adjust duplex*/
+ if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
+ speed = 1000;
+ else
+ speed = 10000;
+ duplex = 1;
+
+ h->dev->ops->adjust_link(h, speed, duplex);
+
+ return 0;
+}
+
+static void __lb_other_process(struct hns_nic_ring_data *ring_data,
+ struct sk_buff *skb)
+{
+ struct net_device *ndev;
+ struct hnae_ring *ring;
+ struct netdev_queue *dev_queue;
+ struct sk_buff *new_skb;
+ unsigned int frame_size;
+ int check_ok;
+ u32 i;
+ char buff[33]; /* 32B data and the last character '\0' */
+
+ if (!ring_data) { /* Just for doing create frame*/
+ frame_size = skb->len;
+ memset(skb->data, 0xFF, frame_size);
+ frame_size &= ~1ul;
+ memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
+ memset(&skb->data[frame_size / 2 + 10], 0xBE,
+ frame_size / 2 - 11);
+ memset(&skb->data[frame_size / 2 + 12], 0xAF,
+ frame_size / 2 - 13);
+ return;
+ }
+
+ ring = ring_data->ring;
+ ndev = ring_data->napi.dev;
+ if (is_tx_ring(ring)) { /* for tx queue reset*/
+ dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
+ netdev_tx_reset_queue(dev_queue);
+ return;
+ }
+
+ frame_size = skb->len;
+ frame_size &= ~1ul;
+ /* for mutl buffer*/
+ new_skb = skb_copy(skb, GFP_ATOMIC);
+ dev_kfree_skb_any(skb);
+ skb = new_skb;
+
+ check_ok = 0;
+ if (*(skb->data + 10) == 0xFF) { /* for rx check frame*/
+ if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
+ (*(skb->data + frame_size / 2 + 12) == 0xAF))
+ check_ok = 1;
+ }
+
+ if (check_ok) {
+ ndev->stats.rx_packets++;
+ ndev->stats.rx_bytes += skb->len;
+ } else {
+ ndev->stats.rx_frame_errors++;
+ for (i = 0; i < skb->len; i++) {
+ snprintf(buff + i % 16 * 2, 3, /* tailing \0*/
+ "%02x", *(skb->data + i));
+ if ((i % 16 == 15) || (i == skb->len - 1))
+ pr_info("%s\n", buff);
+ }
+ }
+ dev_kfree_skb_any(skb);
+}
+
+static int __lb_clean_rings(struct hns_nic_priv *priv,
+ int ringid0, int ringid1, int budget)
+{
+ int i, ret;
+ struct hns_nic_ring_data *ring_data;
+ struct net_device *ndev = priv->netdev;
+ unsigned long rx_packets = ndev->stats.rx_packets;
+ unsigned long rx_bytes = ndev->stats.rx_bytes;
+ unsigned long rx_frame_errors = ndev->stats.rx_frame_errors;
+
+ for (i = ringid0; i <= ringid1; i++) {
+ ring_data = &priv->ring_data[i];
+ (void)ring_data->poll_one(ring_data,
+ budget, __lb_other_process);
+ }
+ ret = (int)(ndev->stats.rx_packets - rx_packets);
+ ndev->stats.rx_packets = rx_packets;
+ ndev->stats.rx_bytes = rx_bytes;
+ ndev->stats.rx_frame_errors = rx_frame_errors;
+ return ret;
+}
+
+static const char * const pr_test[] = {
+ "none",
+ "mac",
+ "serdes",
+ "phy",
+ NULL};
+
+/**
+ * nic_run_loopback_test - run loopback test
+ * @nic_dev: net device
+ * @loopback_type: loopback type
+ */
+static int __lb_run_test(struct net_device *ndev,
+ enum hnae_loop loop_mode)
+{
+#define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
+#define NIC_LB_TEST_RING_ID 0
+#define NIC_LB_TEST_FRAME_SIZE 128
+/* nic loopback test err */
+#define NIC_LB_TEST_NO_MEM_ERR 1
+#define NIC_LB_TEST_TX_CNT_ERR 2
+#define NIC_LB_TEST_RX_CNT_ERR 3
+#define NIC_LB_TEST_RX_PKG_ERR 4
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct hnae_handle *h = priv->ae_handle;
+ int i, j, lc, good_cnt, ret_val = 0;
+ unsigned int size;
+ netdev_tx_t tx_ret_val;
+ struct sk_buff *skb;
+
+ size = NIC_LB_TEST_FRAME_SIZE;
+ /* allocate test skb */
+ skb = alloc_skb(size, GFP_KERNEL);
+ if (!skb)
+ return NIC_LB_TEST_NO_MEM_ERR;
+
+ /* place data into test skb */
+ (void)skb_put(skb, size);
+ __lb_other_process(NULL, skb);
+ skb->queue_mapping = NIC_LB_TEST_RING_ID;
+
+ lc = 1;
+ for (j = 0; j < lc; j++) {
+ /* reset count of good packets */
+ good_cnt = 0;
+ /* place 64 packets on the transmit queue*/
+ for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
+ (void)skb_get(skb);
+
+ tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
+ ndev, skb,
+ &tx_ring_data(priv, skb->queue_mapping));
+ if (tx_ret_val == NETDEV_TX_OK)
+ good_cnt++;
+ else
+ break;
+ }
+ if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
+ ret_val = NIC_LB_TEST_TX_CNT_ERR;
+ dev_err(priv->dev, "%s lb test sent fail, cnt=0x%x, budget=0x%x\n",
+ pr_test[loop_mode], good_cnt,
+ NIC_LB_TEST_PKT_NUM_PER_CYCLE);
+ break;
+ }
+
+ /* allow 100 milliseconds for packets to go from Tx to Rx */
+ msleep(100);
+
+ good_cnt = __lb_clean_rings(priv,
+ h->q_num, h->q_num * 2 - 1,
+ NIC_LB_TEST_PKT_NUM_PER_CYCLE);
+ if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
+ ret_val = NIC_LB_TEST_RX_CNT_ERR;
+ dev_err(priv->dev, "%s lb test recv fail, cnt=0x%x, budget=0x%x\n",
+ pr_test[loop_mode], good_cnt,
+ NIC_LB_TEST_PKT_NUM_PER_CYCLE);
+ break;
+ }
+ (void)__lb_clean_rings(priv,
+ NIC_LB_TEST_RING_ID, NIC_LB_TEST_RING_ID,
+ NIC_LB_TEST_PKT_NUM_PER_CYCLE);
+ }
+
+ /* free the original skb */
+ kfree_skb(skb);
+
+ return ret_val;
+}
+
+static int __lb_down(struct net_device *ndev)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ struct hnae_handle *h = priv->ae_handle;
+ int ret;
+
+ ret = __lb_setup(ndev, MAC_LOOP_NONE);
+ if ((ret) && (ret != -ENOTSUPP))
+ netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
+ __func__,
+ ret);
+
+ if (priv->phy) {
+ phy_stop(priv->phy);
+ phy_disconnect(priv->phy);
+ }
+
+ if (h->dev->ops->stop)
+ h->dev->ops->stop(h);
+
+ usleep_range(10000, 20000);
+ (void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
+
+ hns_nic_net_reset(ndev);
+
+ return 0;
+}
+
+/**
+ * hns_nic_self_test - self test
+ * @dev: net device
+ * @eth_test: test cmd
+ * @data: test result
+ */
+static void hns_nic_self_test(struct net_device *ndev,
+ struct ethtool_test *eth_test, u64 *data)
+{
+ struct hns_nic_priv *priv = netdev_priv(ndev);
+ bool if_running = netif_running(ndev);
+#define SELF_TEST_TPYE_NUM 3
+ int st_param[SELF_TEST_TPYE_NUM][2];
+ int i;
+
+ set_bit(NIC_STATE_TESTING, &priv->state);
+
+ data[0] = ENOTSUPP;
+ data[1] = ENOTSUPP;
+ data[2] = ENOTSUPP;
+
+ st_param[0][0] = MAC_INTERNALLOOP_MAC; /* XGE not supported lb */
+ st_param[0][1] = (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII);
+ st_param[1][0] = MAC_INTERNALLOOP_SERDES;
+ st_param[1][1] = 1; /*serdes must exist*/
+ st_param[2][0] = MAC_INTERNALLOOP_PHY; /* only supporte phy node*/
+ st_param[2][1] = !!(priv->ae_handle->phy_node);
+
+ if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
+ if (if_running)
+ (void)dev_close(ndev);
+
+ for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
+ if (!st_param[i][1])
+ continue; /* NEXT testing */
+
+ data[i] = __lb_up(ndev, (enum hnae_loop)st_param[i][0]);
+ if (!data[i]) {
+ data[i] = __lb_run_test(
+ ndev, (enum hnae_loop)st_param[i][0]);
+ (void)__lb_down(ndev);
+ }
+ if (data[i])
+ eth_test->flags |= ETH_TEST_FL_FAILED;
+ }
+
+ hns_nic_net_reset(priv->netdev);
+
+ if (if_running)
+ (void)dev_open(ndev);
+ }
+ /* Online tests aren't run; pass by default */
+
+ clear_bit(NIC_STATE_TESTING, &priv->state);
+
+ (void)msleep_interruptible(4 * 1000);
+}
+
+/**
+ * hns_nic_get_drvinfo - get net driver info
+ * @dev: net device
+ * @drvinfo: driver info
+ */
+static void hns_nic_get_drvinfo(struct net_device *net_dev,
+ struct ethtool_drvinfo *drvinfo)
+{
+ struct hns_nic_priv *priv = netdev_priv(net_dev);
+
+ assert(priv);
+
+ strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
+ sizeof(drvinfo->version));
+ drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
+
+ strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
+ drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
+
+ strncpy(drvinfo->bus_info, priv->dev->bus->name,
+ sizeof(drvinfo->bus_info));
+ drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
+
+ strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
+ drvinfo->eedump_len = 0;
+}
+
+/**
+ * hns_get_ringparam - get ring parameter
+ * @dev: net device
+ * @param: ethtool parameter
+ */
+void hns_get_ringparam(struct net_device *net_dev,
+ struct ethtool_ringparam *param)
+{
+ struct hns_nic_priv *priv = netdev_priv(net_dev);
+ struct hnae_ae_ops *ops;
+ struct hnae_queue *queue;
+ u32 uplimit = 0, lowlimit = 0;
+
+ queue = priv->ae_handle->qs[0];
+ ops = priv->ae_handle->dev->ops;
+
+ param->rx_max_pending = priv->ae_handle->q_num;
+ param->tx_max_pending = priv->ae_handle->q_num;
+
+ param->rx_mini_pending = priv->ae_handle->q_num;
+ param->rx_jumbo_pending = priv->ae_handle->q_num;
+ param->rx_pending = queue->rx_ring.desc_num;
+ param->tx_pending = queue->tx_ring.desc_num;
+
+ if (ops->get_ring_bdnum_limit)
+ ops->get_ring_bdnum_limit(queue, &uplimit, &lowlimit);
+
+ param->rx_mini_max_pending = lowlimit;
+ param->rx_jumbo_max_pending = uplimit;
+}
+
+/**
+ * hns_get_pauseparam - get pause parameter
+ * @dev: net device
+ * @param: pause parameter
+ */
+static void hns_get_pauseparam(struct net_device *net_dev,
+ struct ethtool_pauseparam *param)
+{
+ struct hns_nic_priv *priv = netdev_priv(net_dev);
+ struct hnae_ae_ops *ops;
+
+ ops = priv->ae_handle->dev->ops;
+
+ if (ops->get_pauseparam)
+ ops->get_pauseparam(priv->ae_handle, ¶m->autoneg,
+ ¶m->rx_pause, ¶m->tx_pause);
+}
+
+/**
+ * hns_set_pauseparam - set pause parameter
+ * @dev: net device
+ * @param: pause parameter
+ *
+ * Return 0 on success, negative on failure
+ */
+static int hns_set_pauseparam(struct net_device *net_dev,
+ struct ethtool_pauseparam *param)
+{
+ struct hns_nic_priv *priv = netdev_priv(net_dev);
+ struct hnae_ae_ops *ops;
+
+ assert(priv || priv->ae_handle);
+
+ ops = priv->ae_handle->dev->ops;
+
+ if ((priv->ae_handle->phy_node) && (ops->set_autoneg))
+ (void)ops->set_autoneg(priv->ae_handle, param->autoneg);
+
+ if (!ops->set_pauseparam)
+ return -ESRCH;
+
+ ops->set_pauseparam(priv->ae_handle,
+ param->rx_pause,
+ param->tx_pause);
+
+ return 0;
+}
+
+/**
+ * hns_get_coalesce - get coalesce info.
+ * @dev: net device
+ * @ec: coalesce info.
+ *
+ * Return 0 on success, negative on failure.
+ */
+static int hns_get_coalesce(struct net_device *net_dev,
+ struct ethtool_coalesce *ec)
+{
+ struct hns_nic_priv *priv = netdev_priv(net_dev);
+ struct hnae_ae_ops *ops;
+
+ ops = priv->ae_handle->dev->ops;
+
+ ec->use_adaptive_rx_coalesce = 1;
+ ec->use_adaptive_tx_coalesce = 1;
+
+ if ((!ops->get_coalesce_usecs) ||
+ (!ops->get_rx_max_coalesced_frames))
+ return -ESRCH;
+
+ ops->get_coalesce_usecs(priv->ae_handle,
+ &ec->tx_coalesce_usecs,
+ &ec->rx_coalesce_usecs);
+
+ ops->get_rx_max_coalesced_frames(
+ priv->ae_handle,
+ &ec->tx_max_coalesced_frames,
+ &ec->rx_max_coalesced_frames);
+
+ return 0;
+}
+
+/**
+ * hns_set_coalesce - set coalesce info.
+ * @dev: net device
+ * @ec: coalesce info.
+ *
+ * Return 0 on success, negative on failure.
+ */
+static int hns_set_coalesce(struct net_device *net_dev,
+ struct ethtool_coalesce *ec)
+{
+ struct hns_nic_priv *priv = netdev_priv(net_dev);
+ struct hnae_ae_ops *ops;
+ int ret;
+
+ assert(priv || priv->ae_handle);
+
+ ops = priv->ae_handle->dev->ops;
+
+ if (ec->tx_coalesce_usecs != ec->rx_coalesce_usecs)
+ return -EINVAL;
+
+ if (ec->rx_max_coalesced_frames != ec->tx_max_coalesced_frames)
+ return -EINVAL;
+
+ if ((!ops->set_coalesce_usecs) ||
+ (!ops->set_coalesce_frames))
+ return -ESRCH;
+
+ ops->set_coalesce_usecs(priv->ae_handle,
+ ec->rx_coalesce_usecs);
+
+ ret = ops->set_coalesce_frames(
+ priv->ae_handle,
+ ec->rx_max_coalesced_frames);
+
+ return ret;
+}
+
+/**
+ * hns_get_channels - get channel info.
+ * @dev: net device
+ * @ch: channel info.
+ */
+void hns_get_channels(struct net_device *net_dev, struct ethtool_channels *ch)
+{
+ struct hns_nic_priv *priv = netdev_priv(net_dev);
+ struct hnae_ae_ops *ops;
+ u32 max_ring_pair_num, ring_pair_num;
+
+ assert(priv || priv->ae_handle);
+
+ ops = priv->ae_handle->dev->ops;
+
+ if (!ops->get_max_ringnum) {
+ netdev_err(net_dev, "don't support get max ringnum\n");
+ return;
+ }
+
+ ops->get_max_ringnum(priv->ae_handle, &max_ring_pair_num);
+
+ ch->max_rx = max_ring_pair_num;
+ ch->max_tx = max_ring_pair_num;
+ ch->max_combined = max_ring_pair_num;
+ ch->max_other = max_ring_pair_num;
+
+ if (!ops->get_ringnum) {
+ netdev_err(net_dev, "don't support get current ringnum\n");
+ return;
+ }
+
+ ops->get_ringnum(priv->ae_handle, &ring_pair_num);
+
+ ch->rx_count = ring_pair_num;
+ ch->tx_count = ring_pair_num;
+ ch->combined_count = ring_pair_num;
+ ch->other_count = ring_pair_num;
+}
+
+/**
+ * get_ethtool_stats - get detail statistics.
+ * @dev: net device
+ * @stats: statistics info.
+ * @data: statistics data.
+ */
+void hns_get_ethtool_stats(struct net_device *netdev,
+ struct ethtool_stats *stats, u64 *data)
+{
+ u64 *p = data;
+ struct hns_nic_priv *priv = netdev_priv(netdev);
+ struct hnae_handle *h = priv->ae_handle;
+ const struct rtnl_link_stats64 *net_stats;
+ struct rtnl_link_stats64 temp;
+
+ if (!h->dev->ops->get_stats || !h->dev->ops->update_stats) {
+ netdev_err(netdev, "get_stats or update_stats is null!\n");
+ return;
+ }
+
+ h->dev->ops->update_stats(h, &netdev->stats);
+
+ net_stats = dev_get_stats(netdev, &temp);
+
+ /* get netdev statistics */
+ p[0] = net_stats->rx_packets;
+ p[1] = net_stats->tx_packets;
+ p[2] = net_stats->rx_bytes;
+ p[3] = net_stats->tx_bytes;
+ p[4] = net_stats->rx_errors;
+ p[5] = net_stats->tx_errors;
+ p[6] = net_stats->rx_dropped;
+ p[7] = net_stats->tx_dropped;
+ p[8] = net_stats->multicast;
+ p[9] = net_stats->collisions;
+ p[10] = net_stats->rx_over_errors;
+ p[11] = net_stats->rx_crc_errors;
+ p[12] = net_stats->rx_frame_errors;
+ p[13] = net_stats->rx_fifo_errors;
+ p[14] = net_stats->rx_missed_errors;
+ p[15] = net_stats->tx_aborted_errors;
+ p[16] = net_stats->tx_carrier_errors;
+ p[17] = net_stats->tx_fifo_errors;
+ p[18] = net_stats->tx_heartbeat_errors;
+ p[19] = net_stats->rx_length_errors;
+ p[20] = net_stats->tx_window_errors;
+ p[21] = net_stats->rx_compressed;
+ p[22] = net_stats->tx_compressed;
+
+ p[23] = (u64)netdev->rx_dropped.counter;
+ p[24] = (u64)netdev->tx_dropped.counter;
+
+ /* get driver statistics */
+ h->dev->ops->get_stats(h, &p[25]);
+}
+
+/**
+ * get_strings: Return a set of strings that describe the requested objects
+ * @dev: net device
+ * @stats: string set ID.
+ * @data: objects data.
+ */
+void hns_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
+{
+ struct hns_nic_priv *priv = netdev_priv(netdev);
+ struct hnae_handle *h = priv->ae_handle;
+ char *buff = (char *)data;
+
+ if (!h->dev->ops->get_strings) {
+ netdev_err(netdev, "h->dev->ops->get_strings is null!\n");
+ return;
+ }
+ if (stringset == ETH_SS_TEST) {
+ memcpy(data, *hns_nic_test_strs, sizeof(hns_nic_test_strs));
+ }
+ else {
+ snprintf(buff, ETH_GSTRING_LEN, "rx_packets");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "tx_packets");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "rx_bytes");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "tx_bytes");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "rx_errors");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "tx_errors");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "rx_dropped");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "tx_dropped");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "multicast");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "collisions");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "rx_over_errors");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "rx_crc_errors");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "rx_frame_errors");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "tx_carrier_errors");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "rx_fifo_errors");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "rx_missed_errors");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "tx_aborted_errors");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "tx_carrier_errors");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "tx_fifo_errors");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "tx_heartbeat_errors");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "tx_window_errors");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "rx_compressed");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "tx_compressed");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "netdev_rx_dropped");
+ buff = buff + ETH_GSTRING_LEN;
+ snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_dropped");
+ buff = buff + ETH_GSTRING_LEN;
+
+ h->dev->ops->get_strings(h, stringset, buff);
+ }
+}
+
+/**
+ * nic_get_sset_count - get string set count witch returned by nic_get_strings.
+ * @dev: net device
+ * @stringset: string set index, 0: self test string; 1: statistics string.
+ *
+ * Return string set count.
+ */
+int hns_get_sset_count(struct net_device *netdev, int stringset)
+{
+ struct hns_nic_priv *priv = netdev_priv(netdev);
+ struct hnae_handle *h = priv->ae_handle;
+ struct hnae_ae_ops *ops = h->dev->ops;
+
+ if (!ops->get_sset_count) {
+ netdev_err(netdev, "get_sset_count is null!\n");
+ return -EOPNOTSUPP;
+ }
+ if (stringset == ETH_SS_TEST)
+ return (sizeof(hns_nic_test_strs) / ETH_GSTRING_LEN);
+ else
+ return (HNS_NET_STATS_CNT + ops->get_sset_count(h, stringset));
+}
+
+/**
+ * hns_phy_led_set - set phy LED status.
+ * @dev: net device
+ * @value: LED state.
+ *
+ * Return 0 on success, negative on failure.
+ */
+int hns_phy_led_set(struct net_device *netdev, int value)
+{
+ int retval;
+ struct hns_nic_priv *priv = netdev_priv(netdev);
+ struct phy_device *phy_dev = priv->phy;
+
+ if (!phy_dev->bus) {
+ netdev_err(netdev, "phy_dev->bus is null!\n");
+ return -EINVAL;
+ }
+ retval = mdiobus_write(phy_dev->bus, phy_dev->addr,
+ HNS_PHY_PAGE_REG, HNS_PHY_PAGE_LED);
+ retval = mdiobus_write(phy_dev->bus, phy_dev->addr, HNS_LED_FCR, value);
+ retval = mdiobus_write(phy_dev->bus, phy_dev->addr,
+ HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
+ if (retval) {
+ netdev_err(netdev, "mdiobus_write fail !\n");
+ return retval;
+ }
+ return 0;
+}
+
+/**
+ * nic_set_phys_id - set phy identify LED.
+ * @dev: net device
+ * @state: LED state.
+ *
+ * Return 0 on success, negative on failure.
+ */
+int hns_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
+{
+ struct hns_nic_priv *priv = netdev_priv(netdev);
+ struct hnae_handle *h = priv->ae_handle;
+ struct phy_device *phy_dev = priv->phy;
+ int ret;
+
+ if (phy_dev)
+ switch (state) {
+ case ETHTOOL_ID_ACTIVE:
+ ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
+ HNS_PHY_PAGE_REG,
+ HNS_PHY_PAGE_LED);
+ if (ret)
+ return ret;
+
+ priv->phy_led_val = (u16)mdiobus_read(phy_dev->bus,
+ phy_dev->addr,
+ HNS_LED_FCR);
+
+ ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
+ HNS_PHY_PAGE_REG,
+ HNS_PHY_PAGE_COPPER);
+ if (ret)
+ return ret;
+ return 2;
+ case ETHTOOL_ID_ON:
+ ret = hns_phy_led_set(netdev, HNS_LED_FORCE_ON);
+ if (ret)
+ return ret;
+ break;
+ case ETHTOOL_ID_OFF:
+ ret = hns_phy_led_set(netdev, HNS_LED_FORCE_OFF);
+ if (ret)
+ return ret;
+ break;
+ case ETHTOOL_ID_INACTIVE:
+ ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
+ HNS_PHY_PAGE_REG,
+ HNS_PHY_PAGE_LED);
+ if (ret)
+ return ret;
+
+ ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
+ HNS_LED_FCR, priv->phy_led_val);
+ if (ret)
+ return ret;
+
+ ret = mdiobus_write(phy_dev->bus, phy_dev->addr,
+ HNS_PHY_PAGE_REG,
+ HNS_PHY_PAGE_COPPER);
+ if (ret)
+ return ret;
+ break;
+ default:
+ return -EINVAL;
+ }
+ else
+ switch (state) {
+ case ETHTOOL_ID_ACTIVE:
+ return h->dev->ops->set_led_id(h, HNAE_LED_ACTIVE);
+ case ETHTOOL_ID_ON:
+ return h->dev->ops->set_led_id(h, HNAE_LED_ON);
+ case ETHTOOL_ID_OFF:
+ return h->dev->ops->set_led_id(h, HNAE_LED_OFF);
+ case ETHTOOL_ID_INACTIVE:
+ return h->dev->ops->set_led_id(h, HNAE_LED_INACTIVE);
+ default:
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+/**
+ * hns_get_regs - get net device register
+ * @dev: net device
+ * @cmd: ethtool cmd
+ * @date: register data
+ */
+void hns_get_regs(struct net_device *net_dev, struct ethtool_regs *cmd,
+ void *data)
+{
+ struct hns_nic_priv *priv = netdev_priv(net_dev);
+ struct hnae_ae_ops *ops;
+
+ assert(priv || priv->ae_handle);
+
+ ops = priv->ae_handle->dev->ops;
+
+ cmd->version = HNS_CHIP_VERSION;
+ if (!ops->get_regs) {
+ netdev_err(net_dev, "ops->get_regs is null!\n");
+ return;
+ }
+ ops->get_regs(priv->ae_handle, data);
+}
+
+/**
+ * nic_get_regs_len - get total register len.
+ * @dev: net device
+ *
+ * Return total register len.
+ */
+static int hns_get_regs_len(struct net_device *net_dev)
+{
+ u32 reg_num;
+ struct hns_nic_priv *priv = netdev_priv(net_dev);
+ struct hnae_ae_ops *ops;
+
+ assert(priv || priv->ae_handle);
+
+ ops = priv->ae_handle->dev->ops;
+ if (!ops->get_regs_len) {
+ netdev_err(net_dev, "ops->get_regs_len is null!\n");
+ return -EOPNOTSUPP;
+ }
+
+ reg_num = ops->get_regs_len(priv->ae_handle);
+ if (reg_num > 0)
+ return reg_num * sizeof(u32);
+ else
+ return reg_num; /* error code */
+}
+
+/**
+ * hns_nic_nway_reset - nway reset
+ * @dev: net device
+ *
+ * Return 0 on success, negative on failure
+ */
+static int hns_nic_nway_reset(struct net_device *netdev)
+{
+ int ret = 0;
+ struct hns_nic_priv *priv = netdev_priv(netdev);
+ struct phy_device *phy_dev = priv->phy;
+
+ if (netif_running(netdev)) {
+ hns_nic_net_reinit(netdev);
+ if (phy_dev)
+ ret = phy_start_aneg(phy_dev);
+ }
+ return ret;
+}
+
+static struct ethtool_ops hns_ethtool_ops = {
+ .get_drvinfo = hns_nic_get_drvinfo,
+ .get_link = hns_nic_get_link,
+ .get_settings = hns_nic_get_settings,
+ .set_settings = hns_nic_set_settings,
+ .get_ringparam = hns_get_ringparam,
+ .get_pauseparam = hns_get_pauseparam,
+ .set_pauseparam = hns_set_pauseparam,
+ .get_coalesce = hns_get_coalesce,
+ .set_coalesce = hns_set_coalesce,
+ .get_channels = hns_get_channels,
+ .self_test = hns_nic_self_test,
+ .get_strings = hns_get_strings,
+ .get_sset_count = hns_get_sset_count,
+ .get_ethtool_stats = hns_get_ethtool_stats,
+ .set_phys_id = hns_set_phys_id,
+ .get_regs_len = hns_get_regs_len,
+ .get_regs = hns_get_regs,
+ .nway_reset = hns_nic_nway_reset,
+};
+
+void hns_ethtool_set_ops(struct net_device *ndev)
+{
+ ndev->ethtool_ops = &hns_ethtool_ops;
+}
--
1.9.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH 1/5] net: add Hisilicon Network Subsystem support (config and documents)
2015-08-14 10:30 ` [PATCH 1/5] net: add Hisilicon Network Subsystem support (config and documents) Kenneth Lee
@ 2015-08-14 20:49 ` Arnd Bergmann
2015-08-17 1:28 ` 答复: " Liguozhu (Kenneth)
0 siblings, 1 reply; 15+ messages in thread
From: Arnd Bergmann @ 2015-08-14 20:49 UTC (permalink / raw)
To: linux-arm-kernel
On Friday 14 August 2015 18:30:18 Kenneth Lee wrote:
> diff --git a/Documentation/devicetree/bindings/net/hisilicon-hns-nic.txt b/Documentation/devicetree/bindings/net/hisilicon-hns-nic.txt
> new file mode 100644
> index 0000000..5ab6969
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/net/hisilicon-hns-nic.txt
> @@ -0,0 +1,14 @@
> +Hisilicon Network Subsystem NIC controller
> +
> +Required properties:
> +- compatible: hisilicon,hns-nic
> +- ae-name: accelerator name who provide this interface
> +- ae-opts: options (string) to the accelerator. e.g. the index interface
> +
> +Example:
> +
> + ethernet at 0{
> + compatible = "hisilicon,hns-nic";
> + ae-name = "soc0-n4";
> + ae-opts = "0";
> + };
>
These properties look very unconventional. What are the valid strings
for ae-name and ae-opts? It looks like the latter is just a number,
so why not use an integer property?
Arnd
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 3/5] net: add Hisilicon Network Subsystem MDIO support
2015-08-14 10:30 ` [PATCH 3/5] net: add Hisilicon Network Subsystem MDIO support Kenneth Lee
@ 2015-08-14 20:57 ` Arnd Bergmann
2015-08-17 9:17 ` Kenneth Lee
0 siblings, 1 reply; 15+ messages in thread
From: Arnd Bergmann @ 2015-08-14 20:57 UTC (permalink / raw)
To: linux-arm-kernel
On Friday 14 August 2015 18:30:20 Kenneth Lee wrote:
> +#define MDIO_BASE_ADDR 0x403C0000
Does not belong in here (and is not used)
> +#define MDIO_COMMAND_REG 0x0
> +#define MDIO_ADDR_REG 0x4
> +#define MDIO_WDATA_REG 0x8
> +#define MDIO_RDATA_REG 0xc
> +#define MDIO_STA_REG 0x10
These look suspiciously similar to definitions from
drivers/net/ethernet/hisilicon/hip04_mdio.c.
Could the hardware be related? If so, please try to share
the common parts.
> +static inline void mdio_write_reg(void *base, u32 reg, u32 value)
> +{
> + u8 __iomem *reg_addr = ACCESS_ONCE(base);
> +
> + writel(value, reg_addr + reg);
> +}
> +
> +#define MDIO_WRITE_REG(a, reg, value) \
> + mdio_write_reg((a)->vbase, (reg), (value))
>
Something seems wrong here: why do you have an ACCESS_ONCE() on a
local variable? Doesn't this just make the code less efficient
without providing lockless access to shared variables?
The types are inconsistent here, you should get a warning from
running this through 'make C=1' because of the missing __iomem
annotation of the pointer.
Also, why both a macro and an inline function? Just use an inline
function.
Arnd
^ permalink raw reply [flat|nested] 15+ messages in thread
* 答复: [PATCH 1/5] net: add Hisilicon Network Subsystem support (config and documents)
2015-08-14 20:49 ` Arnd Bergmann
@ 2015-08-17 1:28 ` Liguozhu (Kenneth)
2015-08-21 14:00 ` Arnd Bergmann
0 siblings, 1 reply; 15+ messages in thread
From: Liguozhu (Kenneth) @ 2015-08-17 1:28 UTC (permalink / raw)
To: linux-arm-kernel
Thanks, Arnd.
Regarding the ae-name: it is the name of the Acceleration Engine. It is provided by the BIOS according to the position and the feature enabled of the IP. So "soc0" means it is on SoC No. 0, while "n4" means it is running on "Non-dsaf mode 4". Ideally, we should setup the rule to name it. But as I said in the patchset, the IP is original designed for a bare metal solution, it is worthless to export all modes and we are planning to add more mode for Linux itself in the IP in future version. So I think the better way is to leave it as a "name" but add more meaning in the future.
Regarding the ae-opts: it is the initial value for the AE's runtime options, Currently, we have only "port number" (there are 6XGE+2GE port for a DSAF AE) as option. But for future version, we will add other options such as "enable Spanning Tree Protocol algorithm)" and so on.
Should I add these background to somewhere?
Best Regards
-Kenneth Lee (Hisilicon)
__________________________________________________________
????????????????????????????????????????
????????????????????????????????????????
???????????????????????????????????
This e-mail and its attachments contain confidential information from HUAWEI, which
is intended only for the person or entity whose address is listed above. Any use of the
information contained herein in any way (including, but not limited to, total or partial
disclosure, reproduction, or dissemination) by persons other than the intended
recipient(s) is prohibited. If you receive this e-mail in error, please notify the sender by
phone or email immediately and delete it!
-----????-----
???: Arnd Bergmann [mailto:arnd at arndb.de]
????: 2015?8?15? 4:50
???: Liguozhu (Kenneth)
??: robh+dt at kernel.org; pawel.moll at arm.com; mark.rutland at arm.com; ijc+devicetree at hellion.org.uk; galak at codeaurora.org; catalin.marinas at arm.com; will.deacon at arm.com; Zhuangyuzeng (Yisen); davem at davemloft.net; paul.gortmaker at windriver.com; Dingtianhong; zhangfei.gao at linaro.org; devicetree at vger.kernel.org; linux-kernel at vger.kernel.org; linux-arm-kernel at lists.infradead.org; netdev at vger.kernel.org; Linuxarm; Salil Mehta; huangdaode; Liguozhu (Kenneth)
??: Re: [PATCH 1/5] net: add Hisilicon Network Subsystem support (config and documents)
On Friday 14 August 2015 18:30:18 Kenneth Lee wrote:
> diff --git a/Documentation/devicetree/bindings/net/hisilicon-hns-nic.txt b/Documentation/devicetree/bindings/net/hisilicon-hns-nic.txt
> new file mode 100644
> index 0000000..5ab6969
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/net/hisilicon-hns-nic.txt
> @@ -0,0 +1,14 @@
> +Hisilicon Network Subsystem NIC controller
> +
> +Required properties:
> +- compatible: hisilicon,hns-nic
> +- ae-name: accelerator name who provide this interface
> +- ae-opts: options (string) to the accelerator. e.g. the index interface
> +
> +Example:
> +
> + ethernet at 0{
> + compatible = "hisilicon,hns-nic";
> + ae-name = "soc0-n4";
> + ae-opts = "0";
> + };
>
These properties look very unconventional. What are the valid strings
for ae-name and ae-opts? It looks like the latter is just a number,
so why not use an integer property?
Arnd
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 3/5] net: add Hisilicon Network Subsystem MDIO support
2015-08-14 20:57 ` Arnd Bergmann
@ 2015-08-17 9:17 ` Kenneth Lee
2015-08-21 14:01 ` Arnd Bergmann
0 siblings, 1 reply; 15+ messages in thread
From: Kenneth Lee @ 2015-08-17 9:17 UTC (permalink / raw)
To: linux-arm-kernel
Thanks, Arnd,
You are right. This is the same IP as hip04_mdio.c. We just mis-understand the
hardware design. We will merge them and re-submit the patches.
On Fri, Aug 14, 2015 at 10:57:28PM +0200, Arnd Bergmann wrote:
> On Friday 14 August 2015 18:30:20 Kenneth Lee wrote:
>
> > +#define MDIO_BASE_ADDR 0x403C0000
>
> Does not belong in here (and is not used)
>
> > +#define MDIO_COMMAND_REG 0x0
> > +#define MDIO_ADDR_REG 0x4
> > +#define MDIO_WDATA_REG 0x8
> > +#define MDIO_RDATA_REG 0xc
> > +#define MDIO_STA_REG 0x10
>
> These look suspiciously similar to definitions from
> drivers/net/ethernet/hisilicon/hip04_mdio.c.
>
> Could the hardware be related? If so, please try to share
> the common parts.
>
> > +static inline void mdio_write_reg(void *base, u32 reg, u32 value)
> > +{
> > + u8 __iomem *reg_addr = ACCESS_ONCE(base);
> > +
> > + writel(value, reg_addr + reg);
> > +}
> > +
> > +#define MDIO_WRITE_REG(a, reg, value) \
> > + mdio_write_reg((a)->vbase, (reg), (value))
> >
>
> Something seems wrong here: why do you have an ACCESS_ONCE() on a
> local variable? Doesn't this just make the code less efficient
> without providing lockless access to shared variables?
>
> The types are inconsistent here, you should get a warning from
> running this through 'make C=1' because of the missing __iomem
> annotation of the pointer.
>
> Also, why both a macro and an inline function? Just use an inline
> function.
>
> Arnd
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 2/5] net: add Hisilicon Network Subsystem hnae framework support
2015-08-14 10:30 ` [PATCH 2/5] net: add Hisilicon Network Subsystem hnae framework support Kenneth Lee
@ 2015-08-17 19:25 ` David Miller
2015-08-18 0:12 ` Alexey Klimov
1 sibling, 0 replies; 15+ messages in thread
From: David Miller @ 2015-08-17 19:25 UTC (permalink / raw)
To: linux-arm-kernel
From: Kenneth Lee <liguozhu@hisilicon.com>
Date: Fri, 14 Aug 2015 18:30:19 +0800
> diff --git a/drivers/net/ethernet/hisilicon/hns/hnae.c b/drivers/net/ethernet/hisilicon/hns/hnae.c
> new file mode 100644
> index 0000000..fd09768
> --- /dev/null
> +++ b/drivers/net/ethernet/hisilicon/hns/hnae.c
...
> +static inline void hnae_list_add(spinlock_t *lock, struct list_head *node,
> + struct list_head *head)
> +{
> + unsigned long flags;
> +
> + spin_lock_irqsave(lock, flags);
> + list_add_tail_rcu(node, head);
> + spin_unlock_irqrestore(lock, flags);
> +}
Do not declare functions 'inline' in foo.c files, let the compiler decide
on it's own.
This applies to your entire patch series.
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 2/5] net: add Hisilicon Network Subsystem hnae framework support
2015-08-14 10:30 ` [PATCH 2/5] net: add Hisilicon Network Subsystem hnae framework support Kenneth Lee
2015-08-17 19:25 ` David Miller
@ 2015-08-18 0:12 ` Alexey Klimov
2015-08-21 6:36 ` Kenneth Lee
1 sibling, 1 reply; 15+ messages in thread
From: Alexey Klimov @ 2015-08-18 0:12 UTC (permalink / raw)
To: linux-arm-kernel
Hi Kenneth,
just small minor question.
On Fri, Aug 14, 2015 at 1:30 PM, Kenneth Lee <liguozhu@hisilicon.com> wrote:
> HNAE (Hisilicon Network Acceleration Engine) is a framework to provide a
> unified ring buffer interface for Hisilicon Network Acceleration Engines.
>
> With the interface, upper layer can work as ethernet driver, ODP driver or
> other service driver on purpose.
>
> Signed-off-by: Kenneth Lee <liguozhu@huawei.com>
> Signed-off-by: Yisen Zhuang <Yisen.Zhuang@huawei.com>
> ---
> drivers/net/ethernet/hisilicon/Kconfig | 33 +-
> drivers/net/ethernet/hisilicon/Makefile | 1 +
> drivers/net/ethernet/hisilicon/hns/Makefile | 15 +
> drivers/net/ethernet/hisilicon/hns/hnae.c | 494 +++++++++++++++++++++++
> drivers/net/ethernet/hisilicon/hns/hnae.h | 582 ++++++++++++++++++++++++++++
> 5 files changed, 1124 insertions(+), 1 deletion(-)
> create mode 100644 drivers/net/ethernet/hisilicon/hns/Makefile
> create mode 100644 drivers/net/ethernet/hisilicon/hns/hnae.c
> create mode 100644 drivers/net/ethernet/hisilicon/hns/hnae.h
>
> diff --git a/drivers/net/ethernet/hisilicon/Kconfig b/drivers/net/ethernet/hisilicon/Kconfig
> index dead17b..1e4f5a7 100644
> --- a/drivers/net/ethernet/hisilicon/Kconfig
> +++ b/drivers/net/ethernet/hisilicon/Kconfig
> @@ -5,7 +5,7 @@
> config NET_VENDOR_HISILICON
> bool "Hisilicon devices"
> default y
> - depends on ARM
> + depends on ARM || ARM64
> ---help---
> If you have a network (Ethernet) card belonging to this class, say Y.
>
> @@ -31,4 +31,35 @@ config HIP04_ETH
> If you wish to compile a kernel for a hardware with hisilicon p04 SoC and
> want to use the internal ethernet then you should answer Y to this.
>
> +config HNS
> + tristate "Hisilicon Network Subsystem Support (Framework)"
> + ---help---
> + This selects the framework support for Hisilicon Network Subsystem. It
> + is needed by any driver which provides HNS acceleration engine or make
> + use of the engine
> +
> +config HNS_DSAF
> + tristate "Hisilicon HNS DSAF device Support"
> + select HNS
> + select HNS_MDIO
> + ---help---
> + This selects the DSAF (Distributed System Area Frabric) network
> + acceleration engine support. The engine is used in Hisilicon P660,
> + Hi1610 and further ICT SoC
> +
> +config HNS_MDIO
> + tristate "Hisilicon HNS MDIO device Support"
> + select MDIO
> + ---help---
> + This selects the HNS MDIO support. It is needed by HNS_DSAF to access
> + the PHY
> +
> +config HNS_ENET
> + tristate "Hisilicon HNS Ethernet Device Support"
> + select PHYLIB
> + select HNS
> + ---help---
> + This selects the general ethernet driver for HNS. This module make
> + use of any HNS AE driver, such as HNS_DSAF
> +
> endif # NET_VENDOR_HISILICON
> diff --git a/drivers/net/ethernet/hisilicon/Makefile b/drivers/net/ethernet/hisilicon/Makefile
> index 6c14540..2503a9b 100644
> --- a/drivers/net/ethernet/hisilicon/Makefile
> +++ b/drivers/net/ethernet/hisilicon/Makefile
> @@ -4,3 +4,4 @@
>
> obj-$(CONFIG_HIX5HD2_GMAC) += hix5hd2_gmac.o
> obj-$(CONFIG_HIP04_ETH) += hip04_mdio.o hip04_eth.o
> +obj-$(CONFIG_HNS) += hns/
> diff --git a/drivers/net/ethernet/hisilicon/hns/Makefile b/drivers/net/ethernet/hisilicon/hns/Makefile
> new file mode 100644
> index 0000000..6680602
> --- /dev/null
> +++ b/drivers/net/ethernet/hisilicon/hns/Makefile
> @@ -0,0 +1,15 @@
> +#
> +# Makefile for the HISILICON network device drivers.
> +#
> +
> +obj-$(CONFIG_HNS) += hnae.o
> +
> +obj-$(CONFIG_HNS_DSAF) += hns_dsaf.o
> +hns_dsaf-objs = hns_ae_adapt.o hns_dsaf_gmac.o hns_dsaf_mac.o hns_dsaf_misc.o \
> + hns_dsaf_main.o hns_dsaf_ppe.o hns_dsaf_rcb.o hns_dsaf_xgmac.o
> +
> +obj-$(CONFIG_HNS_MDIO) += hns_mdio.o
> +hns_mdio-objs = hns_mdio_main.o
> +
> +obj-$(CONFIG_HNS_ENET) += hns_enet_drv.o
> +hns_enet_drv-objs = hns_enet.o hns_ethtool.o
> diff --git a/drivers/net/ethernet/hisilicon/hns/hnae.c b/drivers/net/ethernet/hisilicon/hns/hnae.c
> new file mode 100644
> index 0000000..fd09768
> --- /dev/null
> +++ b/drivers/net/ethernet/hisilicon/hns/hnae.c
> @@ -0,0 +1,494 @@
> +/*
> + * Copyright (c) 2014-2015 Hisilicon Limited.
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + */
> +
> +#include <linux/dma-mapping.h>
> +#include <linux/interrupt.h>
> +#include <linux/skbuff.h>
> +#include <linux/slab.h>
> +
> +#include "hnae.h"
> +
> +#define cls_to_ae_dev(dev) container_of(dev, struct hnae_ae_dev, cls_dev)
> +
> +static struct class *hnae_class;
> +
> +static inline void hnae_list_add(spinlock_t *lock, struct list_head *node,
> + struct list_head *head)
> +{
> + unsigned long flags;
> +
> + spin_lock_irqsave(lock, flags);
> + list_add_tail_rcu(node, head);
> + spin_unlock_irqrestore(lock, flags);
> +}
> +
> +static inline void hnae_list_del(spinlock_t *lock, struct list_head *node)
> +{
> + unsigned long flags;
> +
> + spin_lock_irqsave(lock, flags);
> + list_del_rcu(node);
> + spin_unlock_irqrestore(lock, flags);
> +}
> +
> +static int hnae_alloc_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
> +{
> + unsigned int order = hnae_page_order(ring);
> + struct page *p = dev_alloc_pages(order);
> +
> + if (!p)
> + return -ENOMEM;
> +
> + cb->priv = p;
> + cb->page_offset = 0;
> + cb->reuse_flag = 0;
> + cb->buf = page_address(p);
> + cb->length = hnae_page_size(ring);
> + cb->type = DESC_TYPE_PAGE;
> +
> + return 0;
> +}
> +
> +static void hnae_free_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
> +{
> + if (cb->type == DESC_TYPE_SKB)
> + dev_kfree_skb_any((struct sk_buff *)cb->priv);
> + else if (unlikely(is_rx_ring(ring)))
> + put_page((struct page *)cb->priv);
> + memset(cb, 0, sizeof(*cb));
> +}
> +
> +static int hnae_map_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
> +{
> + cb->dma = dma_map_page(ring_to_dev(ring), cb->priv, 0,
> + cb->length, ring_to_dma_dir(ring));
> +
> + if (dma_mapping_error(ring_to_dev(ring), cb->dma))
> + return -EIO;
> +
> + return 0;
> +}
> +
> +static void hnae_unmap_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
> +{
> + if (cb->type == DESC_TYPE_SKB)
> + dma_unmap_single(ring_to_dev(ring), cb->dma, cb->length,
> + ring_to_dma_dir(ring));
> + else
> + dma_unmap_page(ring_to_dev(ring), cb->dma, cb->length,
> + ring_to_dma_dir(ring));
> +}
> +
> +static struct hnae_buf_ops hnae_bops = {
> + .alloc_buffer = hnae_alloc_buffer,
> + .free_buffer = hnae_free_buffer,
> + .map_buffer = hnae_map_buffer,
> + .unmap_buffer = hnae_unmap_buffer,
> +};
> +
> +static int __ae_match(struct device *dev, const void *data)
> +{
> + struct hnae_ae_dev *hdev = cls_to_ae_dev(dev);
> + const char *ae_id = data;
> +
> + if (!strncmp(ae_id, hdev->name, AE_NAME_SIZE))
> + return 1;
> +
> + return 0;
> +}
> +
> +static struct hnae_ae_dev *find_ae(const char *ae_id)
> +{
> + struct device *dev;
> +
> + BUG_ON(!ae_id);
> +
> + dev = class_find_device(hnae_class, NULL, ae_id, __ae_match);
> +
> + return cls_to_ae_dev(dev);
> +}
Most entries of class_find_device() in kernel that i see perform
if-check for NULL on return value.
What do you think about such option for example:
return dev ? cls_to_ae_dev(dev) : NULL;
?
Is it possible that, someday, your dev will be NULL and creep in
container_of() that doesn't protected from ptr = NULL
and .... (let's go down a little in another function)
> +
> +static void hnae_free_buffers(struct hnae_ring *ring)
> +{
> + int i;
> +
> + for (i = 0; i < ring->desc_num; i++)
> + hnae_free_buffer_detach(ring, i);
> +}
> +
> +/* Allocate memory for raw pkg, and map with dma */
> +static int hnae_alloc_buffers(struct hnae_ring *ring)
> +{
> + int i, j, ret;
> +
> + for (i = 0; i < ring->desc_num; i++) {
> + ret = hnae_alloc_buffer_attach(ring, i);
> + if (ret)
> + goto out_buffer_fail;
> + }
> +
> + return 0;
> +
> +out_buffer_fail:
> + for (j = i - 1; j >= 0; j--)
> + hnae_free_buffer_detach(ring, j);
> + return ret;
> +}
> +
> +/* free desc along with its attached buffer */
> +static inline void hnae_free_desc(struct hnae_ring *ring)
> +{
> + hnae_free_buffers(ring);
> + dma_unmap_single(ring_to_dev(ring), ring->desc_dma_addr,
> + ring->desc_num * sizeof(ring->desc[0]),
> + ring_to_dma_dir(ring));
> + ring->desc_dma_addr = 0;
> + kfree(ring->desc);
> + ring->desc = NULL;
> +}
> +
> +/* alloc desc, without buffer attached */
> +static inline int hnae_alloc_desc(struct hnae_ring *ring)
> +{
> + int size = ring->desc_num * sizeof(ring->desc[0]);
> +
> + ring->desc = kzalloc(size, GFP_KERNEL);
> + if (!ring->desc)
> + return -ENOMEM;
> +
> + ring->desc_dma_addr = dma_map_single(ring_to_dev(ring),
> + ring->desc, size, ring_to_dma_dir(ring));
> + if (dma_mapping_error(ring_to_dev(ring), ring->desc_dma_addr)) {
> + ring->desc_dma_addr = 0;
> + kfree(ring->desc);
> + ring->desc = NULL;
> + return -ENOMEM;
> + }
> +
> + return 0;
> +}
> +
> +/* fini ring, also free the buffer for the ring */
> +static inline void hnae_fini_ring(struct hnae_ring *ring)
> +{
> + hnae_free_desc(ring);
> + kfree(ring->desc_cb);
> + ring->desc_cb = NULL;
> + ring->next_to_clean = 0;
> + ring->next_to_use = 0;
> +}
> +
> +/* init ring, and with buffer for rx ring */
> +static inline int hnae_init_ring(struct hnae_queue *q, struct hnae_ring *ring,
> + int flags)
> +{
> + int ret;
> +
> + if (ring->desc_num <= 0 || ring->buf_size <= 0)
> + return -EINVAL;
> +
> + ring->q = q;
> + ring->flags = flags;
> + assert(!ring->desc && !ring->desc_cb && !ring->desc_dma_addr);
> +
> + /* not matter for tx or rx ring, the ntc and ntc start from 0 */
> + assert(ring->next_to_use == 0);
> + assert(ring->next_to_clean == 0);
> +
> + ring->desc_cb = kcalloc(ring->desc_num, sizeof(ring->desc_cb[0]),
> + GFP_KERNEL);
> + if (!ring->desc_cb) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + ret = hnae_alloc_desc(ring);
> + if (ret)
> + goto out_with_desc_cb;
> +
> + if (is_rx_ring(ring)) {
> + ret = hnae_alloc_buffers(ring);
> + if (ret)
> + goto out_with_desc;
> + }
> +
> + return 0;
> +
> +out_with_desc:
> + hnae_free_desc(ring);
> +out_with_desc_cb:
> + kfree(ring->desc_cb);
> + ring->desc_cb = NULL;
> +out:
> + return ret;
> +}
> +
> +static inline int hnae_init_queue(struct hnae_handle *h, struct hnae_queue *q,
> + struct hnae_ae_dev *dev)
> +{
> + int ret;
> +
> + q->dev = dev;
> + q->handle = h;
> +
> + ret = hnae_init_ring(q, &q->tx_ring, q->tx_ring.flags | RINGF_DIR);
> + if (ret)
> + goto out;
> +
> + ret = hnae_init_ring(q, &q->rx_ring, q->rx_ring.flags & ~RINGF_DIR);
> + if (ret)
> + goto out_with_tx_ring;
> +
> + if (dev->ops->init_queue)
> + dev->ops->init_queue(q);
> +
> + return 0;
> +
> +out_with_tx_ring:
> + hnae_fini_ring(&q->tx_ring);
> +out:
> + return ret;
> +}
> +
> +static inline void hnae_fini_queue(struct hnae_queue *q)
> +{
> + if (q->dev->ops->fini_queue)
> + q->dev->ops->fini_queue(q);
> +
> + hnae_fini_ring(&q->tx_ring);
> + hnae_fini_ring(&q->rx_ring);
> +}
> +
> +/**
> + * ae_chain - define ae chain head
> + */
> +static RAW_NOTIFIER_HEAD(ae_chain);
> +
> +int hnae_register_notifier(struct notifier_block *nb)
> +{
> + return raw_notifier_chain_register(&ae_chain, nb);
> +}
> +EXPORT_SYMBOL(hnae_register_notifier);
> +
> +void hnae_unregister_notifier(struct notifier_block *nb)
> +{
> + if (raw_notifier_chain_unregister(&ae_chain, nb))
> + dev_err(NULL, "notifier chain unregister fail\n");
> +}
> +EXPORT_SYMBOL(hnae_unregister_notifier);
> +
> +int hnae_reinit_handle(struct hnae_handle *handle)
> +{
> + int i, j;
> + int ret;
> +
> + for (i = 0; i < handle->q_num; i++) /* free ring*/
> + hnae_fini_queue(handle->qs[i]);
> +
> + for (i = 0; i < handle->q_num; i++) {/* reinit ring*/
> + ret = hnae_init_queue(handle, handle->qs[i], handle->dev);
> + if (ret)
> + goto out_when_init_queue;
> + }
> + return 0;
> +out_when_init_queue:
> + for (j = i - 1; j >= 0; j--)
> + hnae_fini_queue(handle->qs[j]);
> + return ret;
> +}
> +EXPORT_SYMBOL(hnae_reinit_handle);
> +
> +/* hnae_get_handle - get a handle from the AE
> + * @owner_dev: the dev use this handle
> + * @ae_id: the id of the ae to be used
> + * @ae_opts: the options set for the handle
> + * @bops: the callbacks for buffer management
> + *
> + * return handle ptr or ERR_PTR
> + */
> +struct hnae_handle *hnae_get_handle(struct device *owner_dev,
> + const char *ae_id, const char *ae_opts,
> + struct hnae_buf_ops *bops)
> +{
> + struct hnae_ae_dev *dev;
> + struct hnae_handle *handle;
> + int i, j;
> + int ret;
> +
> + dev = find_ae(ae_id);
> + if (!dev)
> + return ERR_PTR(-ENODEV);
.. and here you might not receive NULL after container_of() works on
that returned NULL after class_find_device().
I guess that might happen after someone change order of fields in
struct hnae_ae_dev.
I understand that it's unlikely to happen but could you please check or comment?
> +
> + handle = dev->ops->get_handle(dev, ae_opts);
> + if (IS_ERR(handle))
> + return handle;
> +
> + handle->dev = dev;
> + handle->owner_dev = owner_dev;
> + handle->bops = bops ? bops : &hnae_bops;
> + handle->ae_opts = ae_opts;
> +
> + for (i = 0; i < handle->q_num; i++) {
> + ret = hnae_init_queue(handle, handle->qs[i], dev);
> + if (ret)
> + goto out_when_init_queue;
> + }
> +
> + __module_get(dev->owner);
> +
> + hnae_list_add(&dev->lock, &handle->node, &dev->handle_list);
> +
> + return handle;
> +
> +out_when_init_queue:
> + for (j = i - 1; j >= 0; j--)
> + hnae_fini_queue(handle->qs[j]);
> +
> + return ERR_PTR(-ENOMEM);
> +}
> +EXPORT_SYMBOL(hnae_get_handle);
> +
> +void hnae_put_handle(struct hnae_handle *h)
> +{
> + struct hnae_ae_dev *dev = h->dev;
> + int i;
> +
> + for (i = 0; i < h->q_num; i++)
> + hnae_fini_queue(h->qs[i]);
> +
> + hnae_list_del(&dev->lock, &h->node);
> +
> + if (dev->ops->put_handle)
> + dev->ops->put_handle(h);
> +
> + module_put(dev->owner);
> +}
> +EXPORT_SYMBOL(hnae_put_handle);
> +
> +static void __hnae_release(struct device *dev)
> +{
> +}
> +
> +/**
> + * hnae_ae_register - register a AE engine to hnae framework
> + * @hdev: the hnae ae engine device
> + * @owner: the module who provides this dev
> + * NOTE: the duplicated name will not be checked
> + */
> +int hnae_ae_register(struct hnae_ae_dev *hdev, struct module *owner)
> +{
> + static atomic_t id = ATOMIC_INIT(-1);
> + int ret;
> +
> + if (!hdev->dev)
> + return -ENODEV;
> +
> + if (!hdev->ops || !hdev->ops->get_handle ||
> + !hdev->ops->toggle_ring_irq ||
> + !hdev->ops->toggle_queue_status ||
> + !hdev->ops->get_status || !hdev->ops->adjust_link)
> + return -EINVAL;
> +
> + hdev->owner = owner;
> + hdev->id = (int)atomic_inc_return(&id);
> + hdev->cls_dev.parent = hdev->dev;
> + hdev->cls_dev.class = hnae_class;
> + hdev->cls_dev.release = __hnae_release;
> + dev_set_name(&hdev->cls_dev, "hnae%d", hdev->id);
> + ret = device_register(&hdev->cls_dev);
> + if (ret)
> + return ret;
> +
> + __module_get(THIS_MODULE);
> +
> + INIT_LIST_HEAD(&hdev->handle_list);
> + spin_lock_init(&hdev->lock);
> +
> + ret = raw_notifier_call_chain(&ae_chain, HNAE_AE_REGISTER, NULL);
> + if (ret)
> + dev_dbg(hdev->dev,
> + "has not notifier for AE: %s\n", hdev->name);
> +
> + return 0;
> +}
> +EXPORT_SYMBOL(hnae_ae_register);
> +
> +/**
> + * hnae_ae_unregister - unregisters a HNAE AE engine
> + * @cdev: the device to unregister
> + */
> +void hnae_ae_unregister(struct hnae_ae_dev *hdev)
> +{
> + device_unregister(&hdev->cls_dev);
> + module_put(THIS_MODULE);
> +}
> +EXPORT_SYMBOL(hnae_ae_unregister);
> +
> +static ssize_t handles_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + ssize_t s = 0;
> + struct hnae_ae_dev *hdev = cls_to_ae_dev(dev);
> + struct hnae_handle *h;
> + int i = 0, j;
> +
> + list_for_each_entry_rcu(h, &hdev->handle_list, node) {
> + s += sprintf(buf + s, "%s(%s): ", dev_name(h->owner_dev),
> + h->ae_opts);
> + for (j = 0; j < h->q_num; j++) {
> + if (h->qs[i]) {
> + s += sprintf(buf + s,
> + "(%llu,%llu; ",
> + h->qs[i]->tx_ring.stats.sw_err_cnt,
> + h->qs[i]->tx_ring.stats.io_err_cnt);
> + s += sprintf(buf + s,
> + "%llu,%llu,%llu), ",
> + h->qs[i]->rx_ring.stats.sw_err_cnt,
> + h->qs[i]->rx_ring.stats.io_err_cnt,
> + h->qs[i]->rx_ring.stats.seg_pkt_cnt);
> + } else {
> + s += sprintf(buf + s, "(null)\n");
> + }
> + }
> + s += sprintf(buf + s, "\n");
> + }
> +
> + return s;
> +}
> +
> +static DEVICE_ATTR_RO(handles);
> +static struct attribute *hnae_class_attrs[] = {
> + &dev_attr_handles.attr,
> + NULL,
> +};
> +ATTRIBUTE_GROUPS(hnae_class);
> +
> +static int __init hnae_init(void)
> +{
> + hnae_class = class_create(THIS_MODULE, "hnae");
> + if (IS_ERR(hnae_class))
> + return PTR_ERR(hnae_class);
> +
> + hnae_class->dev_groups = hnae_class_groups;
> + return 0;
> +}
> +
> +static void __exit hnae_exit(void)
> +{
> + class_destroy(hnae_class);
> +}
> +
> +subsys_initcall(hnae_init);
> +module_exit(hnae_exit);
> +
> +MODULE_AUTHOR("Hisilicon, Inc.");
> +MODULE_LICENSE("GPL");
> +MODULE_DESCRIPTION("Hisilicon Network Acceleration Engine Framework");
> +
> +/* vi: set tw=78 noet: */
> diff --git a/drivers/net/ethernet/hisilicon/hns/hnae.h b/drivers/net/ethernet/hisilicon/hns/hnae.h
> new file mode 100644
> index 0000000..e7856cf
> --- /dev/null
> +++ b/drivers/net/ethernet/hisilicon/hns/hnae.h
> @@ -0,0 +1,582 @@
> +/*
> + * Copyright (c) 2014-2015 Hisilicon Limited.
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + */
> +
> +#ifndef __HNAE_H
> +#define __HNAE_H
> +
> +/* Names used in this framework:
> + * ae handle (handle):
> + * a set of queues provided by AE
> + * ring buffer queue (rbq):
> + * the channel between upper layer and the AE, can do tx and rx
> + * ring:
> + * a tx or rx channel within a rbq
> + * ring description (desc):
> + * an element in the ring with packet information
> + * buffer:
> + * a memory region referred by desc with the full packet payload
> + *
> + * "num" means a static number set as a parameter, "count" mean a dynamic
> + * number set while running
> + * "cb" means control block
> + */
> +
> +#include <linux/delay.h>
> +#include <linux/device.h>
> +#include <linux/module.h>
> +#include <linux/netdevice.h>
> +#include <linux/notifier.h>
> +#include <linux/types.h>
> +
> +#define HNAE_DRIVER_VERSION "1.1.0"
> +#define HNAE_DRIVER_NAME "hns"
> +#define HNAE_COPYRIGHT "Copyright(c) 2015 Huawei Corporation."
> +#define HNAE_DRIVER_STRING "Hisilicon Network Subsystem Driver"
> +#define HNAE_DEFAULT_DEVICE_DESCR "Hisilicon Network Subsystem"
> +
> +#ifdef DEBUG
> +
> +#ifndef assert
> +#define assert(expr) \
> +do { \
> + if (!(expr)) { \
> + pr_err("Assertion failed! %s, %s, %s, line %d\n", \
> + #expr, __FILE__, __func__, __LINE__); \
> + } \
> +} while (0)
> +#endif
> +
> +#else
> +
> +#ifndef assert
> +#define assert(expr)
> +#endif
> +
> +#endif
> +
> +#define AE_NAME_SIZE 16
> +#define AE_OPTS_SIZE 64
> +
> +/* some said the RX and TX RCB format should not be the same in the future. But
> + * it is the same now...
> + */
> +#define RCB_REG_BASEADDR_L 0x00 /* P660 support only 32bit accessing */
> +#define RCB_REG_BASEADDR_H 0x04
> +#define RCB_REG_BD_NUM 0x08
> +#define RCB_REG_BD_LEN 0x0C
> +#define RCB_REG_PKTLINE 0x10
> +#define RCB_REG_TAIL 0x18
> +#define RCB_REG_HEAD 0x1C
> +#define RCB_REG_FBDNUM 0x20
> +#define RCB_REG_OFFSET 0x24 /* pkt num to be handled */
> +#define RCB_REG_PKTNUM_RECORD 0x2C /* total pkt received */
> +
> +#define HNS_RX_HEAD_SIZE 256
> +
> +#define HNAE_AE_REGISTER 0x1
> +
> +#define RCB_RING_NAME_LEN 16
> +
> +enum hnae_led_state {
> + HNAE_LED_INACTIVE,
> + HNAE_LED_ACTIVE,
> + HNAE_LED_ON,
> + HNAE_LED_OFF
> +};
> +
> +#define HNS_RX_FLAG_VLAN_PRESENT 0x1
> +#define HNS_RX_FLAG_L3ID_IPV4 0x0
> +#define HNS_RX_FLAG_L3ID_IPV6 0x1
> +#define HNS_RX_FLAG_L4ID_UDP 0x0
> +#define HNS_RX_FLAG_L4ID_TCP 0x1
> +
> +#define HNS_TXD_ASID_S 0
> +#define HNS_TXD_ASID_M (0xff << HNS_TXD_ASID_S)
> +#define HNS_TXD_BUFNUM_S 8
> +#define HNS_TXD_BUFNUM_M (0x3 << HNS_TXD_BUFNUM_S)
> +#define HNS_TXD_PORTID_S 10
> +#define HNS_TXD_PORTID_M (0x7 << HNS_TXD_PORTID_S)
> +
> +#define HNS_TXD_RA_B 8
> +#define HNS_TXD_RI_B 9
> +#define HNS_TXD_L4CS_B 10
> +#define HNS_TXD_L3CS_B 11
> +#define HNS_TXD_FE_B 12
> +#define HNS_TXD_VLD_B 13
> +#define HNS_TXD_IPOFFSET_S 14
> +#define HNS_TXD_IPOFFSET_M (0xff << HNS_TXD_IPOFFSET_S)
> +
> +#define HNS_RXD_IPOFFSET_S 0
> +#define HNS_RXD_IPOFFSET_M (0xff << HNS_TXD_IPOFFSET_S)
> +#define HNS_RXD_BUFNUM_S 8
> +#define HNS_RXD_BUFNUM_M (0x3 << HNS_RXD_BUFNUM_S)
> +#define HNS_RXD_PORTID_S 10
> +#define HNS_RXD_PORTID_M (0x7 << HNS_RXD_PORTID_S)
> +#define HNS_RXD_DMAC_S 13
> +#define HNS_RXD_DMAC_M (0x3 << HNS_RXD_DMAC_S)
> +#define HNS_RXD_VLAN_S 15
> +#define HNS_RXD_VLAN_M (0x3 << HNS_RXD_VLAN_S)
> +#define HNS_RXD_L3ID_S 17
> +#define HNS_RXD_L3ID_M (0xf << HNS_RXD_L3ID_S)
> +#define HNS_RXD_L4ID_S 21
> +#define HNS_RXD_L4ID_M (0xf << HNS_RXD_L4ID_S)
> +#define HNS_RXD_FE_B 25
> +#define HNS_RXD_FRAG_B 26
> +#define HNS_RXD_VLD_B 27
> +#define HNS_RXD_L2E_B 28
> +#define HNS_RXD_L3E_B 29
> +#define HNS_RXD_L4E_B 30
> +#define HNS_RXD_DROP_B 31
> +
> +#define HNS_RXD_VLANID_S 8
> +#define HNS_RXD_VLANID_M (0xfff << HNS_RXD_VLANID_S)
> +#define HNS_RXD_CFI_B 20
> +#define HNS_RXD_PRI_S 21
> +#define HNS_RXD_PRI_M (0x7 << HNS_RXD_PRI_S)
> +#define HNS_RXD_ASID_S 24
> +#define HNS_RXD_ASID_M (0xff << HNS_RXD_ASID_S)
> +
> +/* hardware spec ring buffer format */
> +struct __packed hnae_desc {
> + __le64 addr;
> + union {
> + struct {
> + __le16 asid_bufnum_pid;
> + __le16 send_size;
> + __le32 flag_ipoffset;
> + __le32 reserved_3[4];
> + } tx;
> +
> + struct {
> + __le32 ipoff_bnum_pid_flag;
> + __le16 pkt_len;
> + __le16 size;
> + __le32 vlan_pri_asid;
> + __le32 reserved_2[3];
> + } rx;
> + };
> +};
> +
> +struct hnae_desc_cb {
> + dma_addr_t dma; /* dma address of this desc */
> + void *buf; /* cpu addr for a desc */
> +
> + /* priv data for the desc, e.g. skb when use with ip stack*/
> + void *priv;
> + u16 page_offset;
> + u16 reuse_flag;
> +
> + u16 length; /* length of the buffer */
> +
> + /* desc type, used by the ring user to mark the type of the priv data */
> + u16 type;
> +};
> +
> +#define setflags(flags, bits) ((flags) |= (bits))
> +#define unsetflags(flags, bits) ((flags) &= ~(bits))
> +
> +/* hnae_ring->flags fields */
> +#define RINGF_DIR 0x1 /* TX or RX ring, set if TX */
> +#define is_tx_ring(ring) ((ring)->flags & RINGF_DIR)
> +#define is_rx_ring(ring) (!is_tx_ring(ring))
> +#define ring_to_dma_dir(ring) (is_tx_ring(ring) ? \
> + DMA_TO_DEVICE : DMA_FROM_DEVICE)
> +
> +struct ring_stats {
> + u64 io_err_cnt;
> + u64 sw_err_cnt;
> + u64 seg_pkt_cnt;
> + u64 reuse_pg_cnt;
> + u64 err_pkt_len;
> + u64 non_vld_descs;
> + u64 err_bd_num;
> + u64 csum_err;
> + union {
> + u64 tx_pkts;
> + u64 rx_pkts;
> + } pkts;
> + union {
> + u64 tx_bytes;
> + u64 rx_bytes;
> + } bytes;
> + union {
> + u64 tx_err_cnt;
> + u64 rx_err_cnt;
> + } err_cnt;
> +};
> +
> +struct hnae_queue;
> +
> +struct hnae_ring {
> + u8 __iomem *io_base; /* base io address for the ring */
> + struct hnae_desc *desc; /* dma map address space */
> + struct hnae_desc_cb *desc_cb;
> + struct hnae_queue *q;
> + int irq;
> + char ring_name[RCB_RING_NAME_LEN];
> +
> + /* statistic */
> + struct ring_stats stats;
> +
> + dma_addr_t desc_dma_addr;
> + u32 buf_size; /* size for hnae_desc->addr, preset by AE */
> + u16 desc_num; /* total number of desc */
> + u16 max_desc_num_per_pkt;
> + u16 max_raw_data_sz_per_desc;
> + u16 max_pkt_size;
> + int next_to_use; /* idx of next spare desc */
> +
> + /* idx of lastest sent desc, the ring is empty when equal to
> + * next_to_use
> + */
> + int next_to_clean;
> +
> + int flags; /* ring attribute */
> +};
> +
> +#define ring_ptr_move_fw(ring, p) \
> + ((ring)->p = ((ring)->p + 1) % (ring)->desc_num)
> +#define ring_ptr_move_bw(ring, p) \
> + ((ring)->p = ((ring)->p - 1 + (ring)->desc_num) % (ring)->desc_num)
> +
> +enum hns_desc_type {
> + DESC_TYPE_SKB,
> + DESC_TYPE_PAGE,
> +};
> +
> +#define assert_is_ring_idx(ring, idx) \
> + assert((idx) >= 0 && (idx) < (ring)->desc_num)
> +
> +/* the distance between [begin, end) in a ring buffer
> + * note: there is a unuse slot between the begin and the end
> + */
> +static inline int ring_dist(struct hnae_ring *ring, int begin, int end)
> +{
> + assert_is_ring_idx(ring, begin);
> + assert_is_ring_idx(ring, end);
> +
> + return (end - begin + ring->desc_num) % ring->desc_num;
> +}
> +
> +static inline int ring_space(struct hnae_ring *ring)
> +{
> + return ring->desc_num -
> + ring_dist(ring, ring->next_to_clean, ring->next_to_use) - 1;
> +}
> +
> +static inline int is_ring_empty(struct hnae_ring *ring)
> +{
> + assert_is_ring_idx(ring, ring->next_to_use);
> + assert_is_ring_idx(ring, ring->next_to_clean);
> +
> + return ring->next_to_use == ring->next_to_clean;
> +}
> +
> +#define hnae_buf_size(_ring) ((_ring)->buf_size)
> +#define hnae_page_order(_ring) (get_order(hnae_buf_size(_ring)))
> +#define hnae_page_size(_ring) (PAGE_SIZE << hnae_page_order(_ring))
> +
> +struct hnae_handle;
> +
> +/* allocate and dma map space for hnae desc */
> +struct hnae_buf_ops {
> + int (*alloc_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
> + void (*free_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
> + int (*map_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
> + void (*unmap_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
> +};
> +
> +struct hnae_queue {
> + void __iomem *io_base;
> + struct hnae_ae_dev *dev; /* the device who use this queue */
> + struct hnae_ring rx_ring, tx_ring;
> + struct hnae_handle *handle;
> +};
> +
> +/*hnae loop mode*/
> +enum hnae_loop {
> + MAC_LOOP_NONE = 0,
> + MAC_INTERNALLOOP_MAC,
> + MAC_INTERNALLOOP_SERDES,
> + MAC_INTERNALLOOP_PHY,
> +};
> +
> +/*hnae port type*/
> +enum hnae_port_type {
> + HNAE_PORT_SERVICE = 0,
> + HNAE_PORT_DEBUG
> +};
> +
> +/* This struct defines the operation on the handle.
> + *
> + * get_handle(): (mandatory)
> + * Get a handle from AE according to its name and options.
> + * the AE driver should manage the space used by handle and its queues while
> + * the HNAE framework will allocate desc and desc_cb for all rings in the
> + * queues.
> + * put_handle():
> + * Release the handle.
> + * start():
> + * Enable the hardware, include all queues
> + * stop():
> + * Disable the hardware
> + * set_opts(): (mandatory)
> + * Set options to the AE
> + * get_opts(): (mandatory)
> + * Get options from the AE
> + * get_status():
> + * Get the carrier state of the back channel of the handle, 1 for ok, 0 for
> + * non-ok
> + * toggle_ring_irq(): (mandatory)
> + * Set the ring irq to be enabled(0) or disable(1)
> + * toggle_queue_status(): (mandatory)
> + * Set the queue to be enabled(1) or disable(0), this will not change the
> + * ring irq state
> + * adjust_link()
> + * adjust link status
> + * set_loopback()
> + * set loopback
> + * get_ring_bdnum_limit()
> + * get ring bd number limit
> + * get_pauseparam()
> + * get tx and rx of pause frame use
> + * set_autoneg()
> + * set auto autonegotiation of pause frame use
> + * get_autoneg()
> + * get auto autonegotiation of pause frame use
> + * set_pauseparam()
> + * set tx and rx of pause frame use
> + * get_coalesce_usecs()
> + * get usecs to delay a TX interrupt after a packet is sent
> + * get_rx_max_coalesced_frames()
> + * get Maximum number of packets to be sent before a TX interrupt.
> + * set_coalesce_usecs()
> + * set usecs to delay a TX interrupt after a packet is sent
> + * set_coalesce_frames()
> + * set Maximum number of packets to be sent before a TX interrupt.
> + * get_ringnum()
> + * get RX/TX ring number
> + * get_max_ringnum()
> + * get RX/TX ring maximum number
> + * get_mac_addr()
> + * get mac address
> + * set_mac_addr()
> + * set mac address
> + * set_mc_addr()
> + * set multicast mode
> + * set_mtu()
> + * set mtu
> + * update_stats()
> + * update Old network device statistics
> + * get_ethtool_stats()
> + * get ethtool network device statistics
> + * get_strings()
> + * get a set of strings that describe the requested objects
> + * get_sset_count()
> + * get number of strings that @get_strings will write
> + * update_led_status()
> + * update the led status
> + * set_led_id()
> + * set led id
> + * get_regs()
> + * get regs dump
> + * get_regs_len()
> + * get the len of the regs dump
> + */
> +struct hnae_ae_ops {
> + struct hnae_handle *(*get_handle)(struct hnae_ae_dev *dev,
> + const char *opts);
> + void (*put_handle)(struct hnae_handle *handle);
> + void (*init_queue)(struct hnae_queue *q);
> + void (*fini_queue)(struct hnae_queue *q);
> + int (*start)(struct hnae_handle *handle);
> + void (*stop)(struct hnae_handle *handle);
> + void (*reset)(struct hnae_handle *handle);
> + int (*set_opts)(struct hnae_handle *handle, int type, void *opts);
> + int (*get_opts)(struct hnae_handle *handle, int type, void **opts);
> + int (*get_status)(struct hnae_handle *handle);
> + int (*get_info)(struct hnae_handle *handle,
> + u8 *auto_neg, u16 *speed, u8 *duplex);
> + int (*set_info)(struct hnae_handle *handle,
> + u8 auto_neg, u16 speed, u8 duplex);
> + void (*toggle_ring_irq)(struct hnae_ring *ring, u32 val);
> + void (*toggle_queue_status)(struct hnae_queue *queue, u32 val);
> + void (*adjust_link)(struct hnae_handle *handle, int speed, int duplex);
> + int (*set_loopback)(struct hnae_handle *handle,
> + enum hnae_loop loop_mode, int en);
> + void (*get_ring_bdnum_limit)(struct hnae_queue *queue,
> + u32 *uplimit, u32 *lowlimit);
> + void (*get_pauseparam)(struct hnae_handle *handle,
> + u32 *auto_neg, u32 *rx_en, u32 *tx_en);
> + int (*set_autoneg)(struct hnae_handle *handle, u8 enable);
> + int (*get_autoneg)(struct hnae_handle *handle);
> + void (*set_pauseparam)(struct hnae_handle *handle,
> + u32 rx_en, u32 tx_en);
> + void (*get_coalesce_usecs)(struct hnae_handle *handle,
> + u32 *tx_usecs, u32 *rx_usecs);
> + void (*get_rx_max_coalesced_frames)(struct hnae_handle *handle,
> + u32 *tx_frames, u32 *rx_frames);
> + void (*set_coalesce_usecs)(struct hnae_handle *handle, u32 timeout);
> + int (*set_coalesce_frames)(struct hnae_handle *handle,
> + u32 coalesce_frames);
> + void (*get_ringnum)(struct hnae_handle *handle, u32 *ringnum);
> + void (*get_max_ringnum)(struct hnae_handle *handle, u32 *max_ringnum);
> + int (*get_mac_addr)(struct hnae_handle *handle, void **p);
> + int (*set_mac_addr)(struct hnae_handle *handle, void *p);
> + int (*set_mc_addr)(struct hnae_handle *handle, void *addr);
> + int (*set_mtu)(struct hnae_handle *handle, int new_mtu);
> + void (*update_stats)(struct hnae_handle *handle,
> + struct net_device_stats *net_stats);
> + void (*get_stats)(struct hnae_handle *handle, u64 *data);
> + void (*get_strings)(struct hnae_handle *handle,
> + u32 stringset, u8 *data);
> + int (*get_sset_count)(struct hnae_handle *handle, int stringset);
> + void (*update_led_status)(struct hnae_handle *handle);
> + int (*set_led_id)(struct hnae_handle *handle,
> + enum hnae_led_state status);
> + void (*get_regs)(struct hnae_handle *handle, void *data);
> + int (*get_regs_len)(struct hnae_handle *handle);
> +};
> +
> +struct hnae_ae_dev {
> + struct device cls_dev; /* the class dev */
> + struct device *dev; /* the presented dev */
> + struct hnae_ae_ops *ops;
> + struct list_head node;
> + struct module *owner; /* the module who provides this dev */
> + int id;
> + char name[AE_NAME_SIZE];
> + struct list_head handle_list;
> + spinlock_t lock; /* lock to protect the handle_list */
> +};
> +
> +struct hnae_handle {
> + struct device *owner_dev; /* the device which make use of this handle */
> + struct hnae_ae_dev *dev; /* the device who provides this handle */
> + struct device_node *phy_node;
> + phy_interface_t phy_if;
> + u32 if_support;
> + int q_num;
> + const char *ae_opts;
> + enum hnae_port_type port_type;
> + struct list_head node; /* list to hnae_ae_dev->handle_list */
> + struct hnae_buf_ops *bops; /* operation for the buffer */
> + struct net_device_stats net_stats;
> + struct hnae_queue **qs; /* array base of all queues */
> +};
> +
> +#define ring_to_dev(ring) ((ring)->q->dev->dev)
> +
> +struct hnae_handle *hnae_get_handle(struct device *owner_dev, const char *ae_id,
> + const char *ae_opts,
> + struct hnae_buf_ops *bops);
> +void hnae_put_handle(struct hnae_handle *handle);
> +int hnae_ae_register(struct hnae_ae_dev *dev, struct module *owner);
> +void hnae_ae_unregister(struct hnae_ae_dev *dev);
> +
> +int hnae_register_notifier(struct notifier_block *nb);
> +void hnae_unregister_notifier(struct notifier_block *nb);
> +int hnae_reinit_handle(struct hnae_handle *handle);
> +
> +#define hnae_queue_xmit(q, buf_num) writel_relaxed(buf_num, \
> + (q)->tx_ring.io_base + RCB_REG_TAIL)
> +
> +#ifndef assert
> +#define assert(cond)
> +#endif
> +
> +static inline int hnae_reserve_buffer_map(struct hnae_ring *ring,
> + struct hnae_desc_cb *cb)
> +{
> + struct hnae_buf_ops *bops = ring->q->handle->bops;
> + int ret;
> +
> + ret = bops->alloc_buffer(ring, cb);
> + if (ret)
> + goto out;
> +
> + ret = bops->map_buffer(ring, cb);
> + if (ret)
> + goto out_with_buf;
> +
> + return 0;
> +
> +out_with_buf:
> + bops->free_buffer(ring, cb);
> +out:
> + return ret;
> +}
> +
> +static inline int hnae_alloc_buffer_attach(struct hnae_ring *ring, int i)
> +{
> + int ret = hnae_reserve_buffer_map(ring, &ring->desc_cb[i]);
> +
> + if (ret)
> + return ret;
> +
> + ring->desc[i].addr = (__le64)ring->desc_cb[i].dma;
> +
> + return 0;
> +}
> +
> +static inline void hnae_buffer_detach(struct hnae_ring *ring, int i)
> +{
> + ring->q->handle->bops->unmap_buffer(ring, &ring->desc_cb[i]);
> + ring->desc[i].addr = 0;
> +}
> +
> +static inline void hnae_free_buffer_detach(struct hnae_ring *ring, int i)
> +{
> + struct hnae_buf_ops *bops = ring->q->handle->bops;
> + struct hnae_desc_cb *cb = &ring->desc_cb[i];
> +
> + if (!ring->desc_cb[i].dma)
> + return;
> +
> + hnae_buffer_detach(ring, i);
> + bops->free_buffer(ring, cb);
> +}
> +
> +/* detach a in-used buffer and replace with a reserved one */
> +static inline void hnae_replace_buffer(struct hnae_ring *ring, int i,
> + struct hnae_desc_cb *res_cb)
> +{
> + struct hnae_buf_ops *bops = ring->q->handle->bops;
> + struct hnae_desc_cb tmp_cb = ring->desc_cb[i];
> +
> + bops->unmap_buffer(ring, &ring->desc_cb[i]);
> + ring->desc_cb[i] = *res_cb;
> + *res_cb = tmp_cb;
> + ring->desc[i].addr = (__le64)ring->desc_cb[i].dma;
> + ring->desc[i].rx.ipoff_bnum_pid_flag = 0;
> +}
> +
> +static inline void hnae_reuse_buffer(struct hnae_ring *ring, int i)
> +{
> + ring->desc_cb[i].reuse_flag = 0;
> + ring->desc[i].addr = (__le64)(ring->desc_cb[i].dma
> + + ring->desc_cb[i].page_offset);
> + ring->desc[i].rx.ipoff_bnum_pid_flag = 0;
> +}
> +
> +#define hnae_set_field(origin, mask, shift, val) \
> + do { \
> + (origin) &= (~(mask)); \
> + (origin) |= ((val) << (shift)) & (mask); \
> + } while (0)
> +
> +#define hnae_set_bit(origin, shift, val) \
> + hnae_set_field((origin), (0x1 << (shift)), (shift), (val))
> +
> +#define hnae_get_field(origin, mask, shift) (((origin) & (mask)) >> (shift))
> +
> +#define hnae_get_bit(origin, shift) \
> + hnae_get_field((origin), (0x1 << (shift)), (shift))
> +
> +#endif
> --
> 1.9.1
>
>
> _______________________________________________
> linux-arm-kernel mailing list
> linux-arm-kernel at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
--
Best regards, Klimov Alexey
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 2/5] net: add Hisilicon Network Subsystem hnae framework support
2015-08-18 0:12 ` Alexey Klimov
@ 2015-08-21 6:36 ` Kenneth Lee
0 siblings, 0 replies; 15+ messages in thread
From: Kenneth Lee @ 2015-08-21 6:36 UTC (permalink / raw)
To: linux-arm-kernel
Thanks, Klimov,
You are right. I will fix it in next patches.
On Tue, Aug 18, 2015 at 03:12:02AM +0300, Alexey Klimov wrote:
> Date: Tue, 18 Aug 2015 03:12:02 +0300
> From: Alexey Klimov <klimov.linux@gmail.com>
> To: Kenneth Lee <liguozhu@hisilicon.com>
> CC: robh+dt at kernel.org, pawel.moll at arm.com, Mark Rutland
> <mark.rutland@arm.com>, ijc+devicetree at hellion.org.uk, Kumar Gala
> <galak@codeaurora.org>, Catalin Marinas <catalin.marinas@arm.com>, Will
> Deacon <will.deacon@arm.com>, Yisen.Zhuang at huawei.com, "David S. Miller"
> <davem@davemloft.net>, paul.gortmaker at windriver.com,
> dingtianhong at huawei.com, zhangfei.gao at linaro.org,
> devicetree at vger.kernel.org, Linux Kernel Mailing List
> <linux-kernel@vger.kernel.org>, linux-arm-kernel at lists.infradead.org,
> netdev at vger.kernel.org, linuxarm at huawei.com, salil.mehta at huawei.com,
> huangdaode at hisilicon.com, Kenneth Lee <liguozhu@huawei.com>, Yury Norov
> <yury.norov@gmail.com>
> Subject: Re: [PATCH 2/5] net: add Hisilicon Network Subsystem hnae
> framework support
> Message-ID: <CALW4P+J8LkLshu5TuRT+8c__KRwJ8XAdMV4yA0KEnrfUg=mNow@mail.gmail.com>
>
> Hi Kenneth,
>
> just small minor question.
>
> On Fri, Aug 14, 2015 at 1:30 PM, Kenneth Lee <liguozhu@hisilicon.com> wrote:
> > HNAE (Hisilicon Network Acceleration Engine) is a framework to provide a
> > unified ring buffer interface for Hisilicon Network Acceleration Engines.
> >
> > With the interface, upper layer can work as ethernet driver, ODP driver or
> > other service driver on purpose.
> >
> > Signed-off-by: Kenneth Lee <liguozhu@huawei.com>
> > Signed-off-by: Yisen Zhuang <Yisen.Zhuang@huawei.com>
> > ---
> > drivers/net/ethernet/hisilicon/Kconfig | 33 +-
> > drivers/net/ethernet/hisilicon/Makefile | 1 +
> > drivers/net/ethernet/hisilicon/hns/Makefile | 15 +
> > drivers/net/ethernet/hisilicon/hns/hnae.c | 494 +++++++++++++++++++++++
> > drivers/net/ethernet/hisilicon/hns/hnae.h | 582 ++++++++++++++++++++++++++++
> > 5 files changed, 1124 insertions(+), 1 deletion(-)
> > create mode 100644 drivers/net/ethernet/hisilicon/hns/Makefile
> > create mode 100644 drivers/net/ethernet/hisilicon/hns/hnae.c
> > create mode 100644 drivers/net/ethernet/hisilicon/hns/hnae.h
> >
> > diff --git a/drivers/net/ethernet/hisilicon/Kconfig b/drivers/net/ethernet/hisilicon/Kconfig
> > index dead17b..1e4f5a7 100644
> > --- a/drivers/net/ethernet/hisilicon/Kconfig
> > +++ b/drivers/net/ethernet/hisilicon/Kconfig
> > @@ -5,7 +5,7 @@
> > config NET_VENDOR_HISILICON
> > bool "Hisilicon devices"
> > default y
> > - depends on ARM
> > + depends on ARM || ARM64
> > ---help---
> > If you have a network (Ethernet) card belonging to this class, say Y.
> >
> > @@ -31,4 +31,35 @@ config HIP04_ETH
> > If you wish to compile a kernel for a hardware with hisilicon p04 SoC and
> > want to use the internal ethernet then you should answer Y to this.
> >
> > +config HNS
> > + tristate "Hisilicon Network Subsystem Support (Framework)"
> > + ---help---
> > + This selects the framework support for Hisilicon Network Subsystem. It
> > + is needed by any driver which provides HNS acceleration engine or make
> > + use of the engine
> > +
> > +config HNS_DSAF
> > + tristate "Hisilicon HNS DSAF device Support"
> > + select HNS
> > + select HNS_MDIO
> > + ---help---
> > + This selects the DSAF (Distributed System Area Frabric) network
> > + acceleration engine support. The engine is used in Hisilicon P660,
> > + Hi1610 and further ICT SoC
> > +
> > +config HNS_MDIO
> > + tristate "Hisilicon HNS MDIO device Support"
> > + select MDIO
> > + ---help---
> > + This selects the HNS MDIO support. It is needed by HNS_DSAF to access
> > + the PHY
> > +
> > +config HNS_ENET
> > + tristate "Hisilicon HNS Ethernet Device Support"
> > + select PHYLIB
> > + select HNS
> > + ---help---
> > + This selects the general ethernet driver for HNS. This module make
> > + use of any HNS AE driver, such as HNS_DSAF
> > +
> > endif # NET_VENDOR_HISILICON
> > diff --git a/drivers/net/ethernet/hisilicon/Makefile b/drivers/net/ethernet/hisilicon/Makefile
> > index 6c14540..2503a9b 100644
> > --- a/drivers/net/ethernet/hisilicon/Makefile
> > +++ b/drivers/net/ethernet/hisilicon/Makefile
> > @@ -4,3 +4,4 @@
> >
> > obj-$(CONFIG_HIX5HD2_GMAC) += hix5hd2_gmac.o
> > obj-$(CONFIG_HIP04_ETH) += hip04_mdio.o hip04_eth.o
> > +obj-$(CONFIG_HNS) += hns/
> > diff --git a/drivers/net/ethernet/hisilicon/hns/Makefile b/drivers/net/ethernet/hisilicon/hns/Makefile
> > new file mode 100644
> > index 0000000..6680602
> > --- /dev/null
> > +++ b/drivers/net/ethernet/hisilicon/hns/Makefile
> > @@ -0,0 +1,15 @@
> > +#
> > +# Makefile for the HISILICON network device drivers.
> > +#
> > +
> > +obj-$(CONFIG_HNS) += hnae.o
> > +
> > +obj-$(CONFIG_HNS_DSAF) += hns_dsaf.o
> > +hns_dsaf-objs = hns_ae_adapt.o hns_dsaf_gmac.o hns_dsaf_mac.o hns_dsaf_misc.o \
> > + hns_dsaf_main.o hns_dsaf_ppe.o hns_dsaf_rcb.o hns_dsaf_xgmac.o
> > +
> > +obj-$(CONFIG_HNS_MDIO) += hns_mdio.o
> > +hns_mdio-objs = hns_mdio_main.o
> > +
> > +obj-$(CONFIG_HNS_ENET) += hns_enet_drv.o
> > +hns_enet_drv-objs = hns_enet.o hns_ethtool.o
> > diff --git a/drivers/net/ethernet/hisilicon/hns/hnae.c b/drivers/net/ethernet/hisilicon/hns/hnae.c
> > new file mode 100644
> > index 0000000..fd09768
> > --- /dev/null
> > +++ b/drivers/net/ethernet/hisilicon/hns/hnae.c
> > @@ -0,0 +1,494 @@
> > +/*
> > + * Copyright (c) 2014-2015 Hisilicon Limited.
> > + *
> > + * This program is free software; you can redistribute it and/or modify
> > + * it under the terms of the GNU General Public License as published by
> > + * the Free Software Foundation; either version 2 of the License, or
> > + * (at your option) any later version.
> > + */
> > +
> > +#include <linux/dma-mapping.h>
> > +#include <linux/interrupt.h>
> > +#include <linux/skbuff.h>
> > +#include <linux/slab.h>
> > +
> > +#include "hnae.h"
> > +
> > +#define cls_to_ae_dev(dev) container_of(dev, struct hnae_ae_dev, cls_dev)
> > +
> > +static struct class *hnae_class;
> > +
> > +static inline void hnae_list_add(spinlock_t *lock, struct list_head *node,
> > + struct list_head *head)
> > +{
> > + unsigned long flags;
> > +
> > + spin_lock_irqsave(lock, flags);
> > + list_add_tail_rcu(node, head);
> > + spin_unlock_irqrestore(lock, flags);
> > +}
> > +
> > +static inline void hnae_list_del(spinlock_t *lock, struct list_head *node)
> > +{
> > + unsigned long flags;
> > +
> > + spin_lock_irqsave(lock, flags);
> > + list_del_rcu(node);
> > + spin_unlock_irqrestore(lock, flags);
> > +}
> > +
> > +static int hnae_alloc_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
> > +{
> > + unsigned int order = hnae_page_order(ring);
> > + struct page *p = dev_alloc_pages(order);
> > +
> > + if (!p)
> > + return -ENOMEM;
> > +
> > + cb->priv = p;
> > + cb->page_offset = 0;
> > + cb->reuse_flag = 0;
> > + cb->buf = page_address(p);
> > + cb->length = hnae_page_size(ring);
> > + cb->type = DESC_TYPE_PAGE;
> > +
> > + return 0;
> > +}
> > +
> > +static void hnae_free_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
> > +{
> > + if (cb->type == DESC_TYPE_SKB)
> > + dev_kfree_skb_any((struct sk_buff *)cb->priv);
> > + else if (unlikely(is_rx_ring(ring)))
> > + put_page((struct page *)cb->priv);
> > + memset(cb, 0, sizeof(*cb));
> > +}
> > +
> > +static int hnae_map_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
> > +{
> > + cb->dma = dma_map_page(ring_to_dev(ring), cb->priv, 0,
> > + cb->length, ring_to_dma_dir(ring));
> > +
> > + if (dma_mapping_error(ring_to_dev(ring), cb->dma))
> > + return -EIO;
> > +
> > + return 0;
> > +}
> > +
> > +static void hnae_unmap_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
> > +{
> > + if (cb->type == DESC_TYPE_SKB)
> > + dma_unmap_single(ring_to_dev(ring), cb->dma, cb->length,
> > + ring_to_dma_dir(ring));
> > + else
> > + dma_unmap_page(ring_to_dev(ring), cb->dma, cb->length,
> > + ring_to_dma_dir(ring));
> > +}
> > +
> > +static struct hnae_buf_ops hnae_bops = {
> > + .alloc_buffer = hnae_alloc_buffer,
> > + .free_buffer = hnae_free_buffer,
> > + .map_buffer = hnae_map_buffer,
> > + .unmap_buffer = hnae_unmap_buffer,
> > +};
> > +
> > +static int __ae_match(struct device *dev, const void *data)
> > +{
> > + struct hnae_ae_dev *hdev = cls_to_ae_dev(dev);
> > + const char *ae_id = data;
> > +
> > + if (!strncmp(ae_id, hdev->name, AE_NAME_SIZE))
> > + return 1;
> > +
> > + return 0;
> > +}
> > +
> > +static struct hnae_ae_dev *find_ae(const char *ae_id)
> > +{
> > + struct device *dev;
> > +
> > + BUG_ON(!ae_id);
> > +
> > + dev = class_find_device(hnae_class, NULL, ae_id, __ae_match);
> > +
> > + return cls_to_ae_dev(dev);
> > +}
>
> Most entries of class_find_device() in kernel that i see perform
> if-check for NULL on return value.
> What do you think about such option for example:
> return dev ? cls_to_ae_dev(dev) : NULL;
> ?
>
> Is it possible that, someday, your dev will be NULL and creep in
> container_of() that doesn't protected from ptr = NULL
> and .... (let's go down a little in another function)
>
> > +
> > +static void hnae_free_buffers(struct hnae_ring *ring)
> > +{
> > + int i;
> > +
> > + for (i = 0; i < ring->desc_num; i++)
> > + hnae_free_buffer_detach(ring, i);
> > +}
> > +
> > +/* Allocate memory for raw pkg, and map with dma */
> > +static int hnae_alloc_buffers(struct hnae_ring *ring)
> > +{
> > + int i, j, ret;
> > +
> > + for (i = 0; i < ring->desc_num; i++) {
> > + ret = hnae_alloc_buffer_attach(ring, i);
> > + if (ret)
> > + goto out_buffer_fail;
> > + }
> > +
> > + return 0;
> > +
> > +out_buffer_fail:
> > + for (j = i - 1; j >= 0; j--)
> > + hnae_free_buffer_detach(ring, j);
> > + return ret;
> > +}
> > +
> > +/* free desc along with its attached buffer */
> > +static inline void hnae_free_desc(struct hnae_ring *ring)
> > +{
> > + hnae_free_buffers(ring);
> > + dma_unmap_single(ring_to_dev(ring), ring->desc_dma_addr,
> > + ring->desc_num * sizeof(ring->desc[0]),
> > + ring_to_dma_dir(ring));
> > + ring->desc_dma_addr = 0;
> > + kfree(ring->desc);
> > + ring->desc = NULL;
> > +}
> > +
> > +/* alloc desc, without buffer attached */
> > +static inline int hnae_alloc_desc(struct hnae_ring *ring)
> > +{
> > + int size = ring->desc_num * sizeof(ring->desc[0]);
> > +
> > + ring->desc = kzalloc(size, GFP_KERNEL);
> > + if (!ring->desc)
> > + return -ENOMEM;
> > +
> > + ring->desc_dma_addr = dma_map_single(ring_to_dev(ring),
> > + ring->desc, size, ring_to_dma_dir(ring));
> > + if (dma_mapping_error(ring_to_dev(ring), ring->desc_dma_addr)) {
> > + ring->desc_dma_addr = 0;
> > + kfree(ring->desc);
> > + ring->desc = NULL;
> > + return -ENOMEM;
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +/* fini ring, also free the buffer for the ring */
> > +static inline void hnae_fini_ring(struct hnae_ring *ring)
> > +{
> > + hnae_free_desc(ring);
> > + kfree(ring->desc_cb);
> > + ring->desc_cb = NULL;
> > + ring->next_to_clean = 0;
> > + ring->next_to_use = 0;
> > +}
> > +
> > +/* init ring, and with buffer for rx ring */
> > +static inline int hnae_init_ring(struct hnae_queue *q, struct hnae_ring *ring,
> > + int flags)
> > +{
> > + int ret;
> > +
> > + if (ring->desc_num <= 0 || ring->buf_size <= 0)
> > + return -EINVAL;
> > +
> > + ring->q = q;
> > + ring->flags = flags;
> > + assert(!ring->desc && !ring->desc_cb && !ring->desc_dma_addr);
> > +
> > + /* not matter for tx or rx ring, the ntc and ntc start from 0 */
> > + assert(ring->next_to_use == 0);
> > + assert(ring->next_to_clean == 0);
> > +
> > + ring->desc_cb = kcalloc(ring->desc_num, sizeof(ring->desc_cb[0]),
> > + GFP_KERNEL);
> > + if (!ring->desc_cb) {
> > + ret = -ENOMEM;
> > + goto out;
> > + }
> > +
> > + ret = hnae_alloc_desc(ring);
> > + if (ret)
> > + goto out_with_desc_cb;
> > +
> > + if (is_rx_ring(ring)) {
> > + ret = hnae_alloc_buffers(ring);
> > + if (ret)
> > + goto out_with_desc;
> > + }
> > +
> > + return 0;
> > +
> > +out_with_desc:
> > + hnae_free_desc(ring);
> > +out_with_desc_cb:
> > + kfree(ring->desc_cb);
> > + ring->desc_cb = NULL;
> > +out:
> > + return ret;
> > +}
> > +
> > +static inline int hnae_init_queue(struct hnae_handle *h, struct hnae_queue *q,
> > + struct hnae_ae_dev *dev)
> > +{
> > + int ret;
> > +
> > + q->dev = dev;
> > + q->handle = h;
> > +
> > + ret = hnae_init_ring(q, &q->tx_ring, q->tx_ring.flags | RINGF_DIR);
> > + if (ret)
> > + goto out;
> > +
> > + ret = hnae_init_ring(q, &q->rx_ring, q->rx_ring.flags & ~RINGF_DIR);
> > + if (ret)
> > + goto out_with_tx_ring;
> > +
> > + if (dev->ops->init_queue)
> > + dev->ops->init_queue(q);
> > +
> > + return 0;
> > +
> > +out_with_tx_ring:
> > + hnae_fini_ring(&q->tx_ring);
> > +out:
> > + return ret;
> > +}
> > +
> > +static inline void hnae_fini_queue(struct hnae_queue *q)
> > +{
> > + if (q->dev->ops->fini_queue)
> > + q->dev->ops->fini_queue(q);
> > +
> > + hnae_fini_ring(&q->tx_ring);
> > + hnae_fini_ring(&q->rx_ring);
> > +}
> > +
> > +/**
> > + * ae_chain - define ae chain head
> > + */
> > +static RAW_NOTIFIER_HEAD(ae_chain);
> > +
> > +int hnae_register_notifier(struct notifier_block *nb)
> > +{
> > + return raw_notifier_chain_register(&ae_chain, nb);
> > +}
> > +EXPORT_SYMBOL(hnae_register_notifier);
> > +
> > +void hnae_unregister_notifier(struct notifier_block *nb)
> > +{
> > + if (raw_notifier_chain_unregister(&ae_chain, nb))
> > + dev_err(NULL, "notifier chain unregister fail\n");
> > +}
> > +EXPORT_SYMBOL(hnae_unregister_notifier);
> > +
> > +int hnae_reinit_handle(struct hnae_handle *handle)
> > +{
> > + int i, j;
> > + int ret;
> > +
> > + for (i = 0; i < handle->q_num; i++) /* free ring*/
> > + hnae_fini_queue(handle->qs[i]);
> > +
> > + for (i = 0; i < handle->q_num; i++) {/* reinit ring*/
> > + ret = hnae_init_queue(handle, handle->qs[i], handle->dev);
> > + if (ret)
> > + goto out_when_init_queue;
> > + }
> > + return 0;
> > +out_when_init_queue:
> > + for (j = i - 1; j >= 0; j--)
> > + hnae_fini_queue(handle->qs[j]);
> > + return ret;
> > +}
> > +EXPORT_SYMBOL(hnae_reinit_handle);
> > +
> > +/* hnae_get_handle - get a handle from the AE
> > + * @owner_dev: the dev use this handle
> > + * @ae_id: the id of the ae to be used
> > + * @ae_opts: the options set for the handle
> > + * @bops: the callbacks for buffer management
> > + *
> > + * return handle ptr or ERR_PTR
> > + */
> > +struct hnae_handle *hnae_get_handle(struct device *owner_dev,
> > + const char *ae_id, const char *ae_opts,
> > + struct hnae_buf_ops *bops)
> > +{
> > + struct hnae_ae_dev *dev;
> > + struct hnae_handle *handle;
> > + int i, j;
> > + int ret;
> > +
> > + dev = find_ae(ae_id);
> > + if (!dev)
> > + return ERR_PTR(-ENODEV);
>
> .. and here you might not receive NULL after container_of() works on
> that returned NULL after class_find_device().
> I guess that might happen after someone change order of fields in
> struct hnae_ae_dev.
>
> I understand that it's unlikely to happen but could you please check or comment?
>
>
> > +
> > + handle = dev->ops->get_handle(dev, ae_opts);
> > + if (IS_ERR(handle))
> > + return handle;
> > +
> > + handle->dev = dev;
> > + handle->owner_dev = owner_dev;
> > + handle->bops = bops ? bops : &hnae_bops;
> > + handle->ae_opts = ae_opts;
> > +
> > + for (i = 0; i < handle->q_num; i++) {
> > + ret = hnae_init_queue(handle, handle->qs[i], dev);
> > + if (ret)
> > + goto out_when_init_queue;
> > + }
> > +
> > + __module_get(dev->owner);
> > +
> > + hnae_list_add(&dev->lock, &handle->node, &dev->handle_list);
> > +
> > + return handle;
> > +
> > +out_when_init_queue:
> > + for (j = i - 1; j >= 0; j--)
> > + hnae_fini_queue(handle->qs[j]);
> > +
> > + return ERR_PTR(-ENOMEM);
> > +}
> > +EXPORT_SYMBOL(hnae_get_handle);
> > +
> > +void hnae_put_handle(struct hnae_handle *h)
> > +{
> > + struct hnae_ae_dev *dev = h->dev;
> > + int i;
> > +
> > + for (i = 0; i < h->q_num; i++)
> > + hnae_fini_queue(h->qs[i]);
> > +
> > + hnae_list_del(&dev->lock, &h->node);
> > +
> > + if (dev->ops->put_handle)
> > + dev->ops->put_handle(h);
> > +
> > + module_put(dev->owner);
> > +}
> > +EXPORT_SYMBOL(hnae_put_handle);
> > +
> > +static void __hnae_release(struct device *dev)
> > +{
> > +}
> > +
> > +/**
> > + * hnae_ae_register - register a AE engine to hnae framework
> > + * @hdev: the hnae ae engine device
> > + * @owner: the module who provides this dev
> > + * NOTE: the duplicated name will not be checked
> > + */
> > +int hnae_ae_register(struct hnae_ae_dev *hdev, struct module *owner)
> > +{
> > + static atomic_t id = ATOMIC_INIT(-1);
> > + int ret;
> > +
> > + if (!hdev->dev)
> > + return -ENODEV;
> > +
> > + if (!hdev->ops || !hdev->ops->get_handle ||
> > + !hdev->ops->toggle_ring_irq ||
> > + !hdev->ops->toggle_queue_status ||
> > + !hdev->ops->get_status || !hdev->ops->adjust_link)
> > + return -EINVAL;
> > +
> > + hdev->owner = owner;
> > + hdev->id = (int)atomic_inc_return(&id);
> > + hdev->cls_dev.parent = hdev->dev;
> > + hdev->cls_dev.class = hnae_class;
> > + hdev->cls_dev.release = __hnae_release;
> > + dev_set_name(&hdev->cls_dev, "hnae%d", hdev->id);
> > + ret = device_register(&hdev->cls_dev);
> > + if (ret)
> > + return ret;
> > +
> > + __module_get(THIS_MODULE);
> > +
> > + INIT_LIST_HEAD(&hdev->handle_list);
> > + spin_lock_init(&hdev->lock);
> > +
> > + ret = raw_notifier_call_chain(&ae_chain, HNAE_AE_REGISTER, NULL);
> > + if (ret)
> > + dev_dbg(hdev->dev,
> > + "has not notifier for AE: %s\n", hdev->name);
> > +
> > + return 0;
> > +}
> > +EXPORT_SYMBOL(hnae_ae_register);
> > +
> > +/**
> > + * hnae_ae_unregister - unregisters a HNAE AE engine
> > + * @cdev: the device to unregister
> > + */
> > +void hnae_ae_unregister(struct hnae_ae_dev *hdev)
> > +{
> > + device_unregister(&hdev->cls_dev);
> > + module_put(THIS_MODULE);
> > +}
> > +EXPORT_SYMBOL(hnae_ae_unregister);
> > +
> > +static ssize_t handles_show(struct device *dev,
> > + struct device_attribute *attr, char *buf)
> > +{
> > + ssize_t s = 0;
> > + struct hnae_ae_dev *hdev = cls_to_ae_dev(dev);
> > + struct hnae_handle *h;
> > + int i = 0, j;
> > +
> > + list_for_each_entry_rcu(h, &hdev->handle_list, node) {
> > + s += sprintf(buf + s, "%s(%s): ", dev_name(h->owner_dev),
> > + h->ae_opts);
> > + for (j = 0; j < h->q_num; j++) {
> > + if (h->qs[i]) {
> > + s += sprintf(buf + s,
> > + "(%llu,%llu; ",
> > + h->qs[i]->tx_ring.stats.sw_err_cnt,
> > + h->qs[i]->tx_ring.stats.io_err_cnt);
> > + s += sprintf(buf + s,
> > + "%llu,%llu,%llu), ",
> > + h->qs[i]->rx_ring.stats.sw_err_cnt,
> > + h->qs[i]->rx_ring.stats.io_err_cnt,
> > + h->qs[i]->rx_ring.stats.seg_pkt_cnt);
> > + } else {
> > + s += sprintf(buf + s, "(null)\n");
> > + }
> > + }
> > + s += sprintf(buf + s, "\n");
> > + }
> > +
> > + return s;
> > +}
> > +
> > +static DEVICE_ATTR_RO(handles);
> > +static struct attribute *hnae_class_attrs[] = {
> > + &dev_attr_handles.attr,
> > + NULL,
> > +};
> > +ATTRIBUTE_GROUPS(hnae_class);
> > +
> > +static int __init hnae_init(void)
> > +{
> > + hnae_class = class_create(THIS_MODULE, "hnae");
> > + if (IS_ERR(hnae_class))
> > + return PTR_ERR(hnae_class);
> > +
> > + hnae_class->dev_groups = hnae_class_groups;
> > + return 0;
> > +}
> > +
> > +static void __exit hnae_exit(void)
> > +{
> > + class_destroy(hnae_class);
> > +}
> > +
> > +subsys_initcall(hnae_init);
> > +module_exit(hnae_exit);
> > +
> > +MODULE_AUTHOR("Hisilicon, Inc.");
> > +MODULE_LICENSE("GPL");
> > +MODULE_DESCRIPTION("Hisilicon Network Acceleration Engine Framework");
> > +
> > +/* vi: set tw=78 noet: */
> > diff --git a/drivers/net/ethernet/hisilicon/hns/hnae.h b/drivers/net/ethernet/hisilicon/hns/hnae.h
> > new file mode 100644
> > index 0000000..e7856cf
> > --- /dev/null
> > +++ b/drivers/net/ethernet/hisilicon/hns/hnae.h
> > @@ -0,0 +1,582 @@
> > +/*
> > + * Copyright (c) 2014-2015 Hisilicon Limited.
> > + *
> > + * This program is free software; you can redistribute it and/or modify
> > + * it under the terms of the GNU General Public License as published by
> > + * the Free Software Foundation; either version 2 of the License, or
> > + * (at your option) any later version.
> > + */
> > +
> > +#ifndef __HNAE_H
> > +#define __HNAE_H
> > +
> > +/* Names used in this framework:
> > + * ae handle (handle):
> > + * a set of queues provided by AE
> > + * ring buffer queue (rbq):
> > + * the channel between upper layer and the AE, can do tx and rx
> > + * ring:
> > + * a tx or rx channel within a rbq
> > + * ring description (desc):
> > + * an element in the ring with packet information
> > + * buffer:
> > + * a memory region referred by desc with the full packet payload
> > + *
> > + * "num" means a static number set as a parameter, "count" mean a dynamic
> > + * number set while running
> > + * "cb" means control block
> > + */
> > +
> > +#include <linux/delay.h>
> > +#include <linux/device.h>
> > +#include <linux/module.h>
> > +#include <linux/netdevice.h>
> > +#include <linux/notifier.h>
> > +#include <linux/types.h>
> > +
> > +#define HNAE_DRIVER_VERSION "1.1.0"
> > +#define HNAE_DRIVER_NAME "hns"
> > +#define HNAE_COPYRIGHT "Copyright(c) 2015 Huawei Corporation."
> > +#define HNAE_DRIVER_STRING "Hisilicon Network Subsystem Driver"
> > +#define HNAE_DEFAULT_DEVICE_DESCR "Hisilicon Network Subsystem"
> > +
> > +#ifdef DEBUG
> > +
> > +#ifndef assert
> > +#define assert(expr) \
> > +do { \
> > + if (!(expr)) { \
> > + pr_err("Assertion failed! %s, %s, %s, line %d\n", \
> > + #expr, __FILE__, __func__, __LINE__); \
> > + } \
> > +} while (0)
> > +#endif
> > +
> > +#else
> > +
> > +#ifndef assert
> > +#define assert(expr)
> > +#endif
> > +
> > +#endif
> > +
> > +#define AE_NAME_SIZE 16
> > +#define AE_OPTS_SIZE 64
> > +
> > +/* some said the RX and TX RCB format should not be the same in the future. But
> > + * it is the same now...
> > + */
> > +#define RCB_REG_BASEADDR_L 0x00 /* P660 support only 32bit accessing */
> > +#define RCB_REG_BASEADDR_H 0x04
> > +#define RCB_REG_BD_NUM 0x08
> > +#define RCB_REG_BD_LEN 0x0C
> > +#define RCB_REG_PKTLINE 0x10
> > +#define RCB_REG_TAIL 0x18
> > +#define RCB_REG_HEAD 0x1C
> > +#define RCB_REG_FBDNUM 0x20
> > +#define RCB_REG_OFFSET 0x24 /* pkt num to be handled */
> > +#define RCB_REG_PKTNUM_RECORD 0x2C /* total pkt received */
> > +
> > +#define HNS_RX_HEAD_SIZE 256
> > +
> > +#define HNAE_AE_REGISTER 0x1
> > +
> > +#define RCB_RING_NAME_LEN 16
> > +
> > +enum hnae_led_state {
> > + HNAE_LED_INACTIVE,
> > + HNAE_LED_ACTIVE,
> > + HNAE_LED_ON,
> > + HNAE_LED_OFF
> > +};
> > +
> > +#define HNS_RX_FLAG_VLAN_PRESENT 0x1
> > +#define HNS_RX_FLAG_L3ID_IPV4 0x0
> > +#define HNS_RX_FLAG_L3ID_IPV6 0x1
> > +#define HNS_RX_FLAG_L4ID_UDP 0x0
> > +#define HNS_RX_FLAG_L4ID_TCP 0x1
> > +
> > +#define HNS_TXD_ASID_S 0
> > +#define HNS_TXD_ASID_M (0xff << HNS_TXD_ASID_S)
> > +#define HNS_TXD_BUFNUM_S 8
> > +#define HNS_TXD_BUFNUM_M (0x3 << HNS_TXD_BUFNUM_S)
> > +#define HNS_TXD_PORTID_S 10
> > +#define HNS_TXD_PORTID_M (0x7 << HNS_TXD_PORTID_S)
> > +
> > +#define HNS_TXD_RA_B 8
> > +#define HNS_TXD_RI_B 9
> > +#define HNS_TXD_L4CS_B 10
> > +#define HNS_TXD_L3CS_B 11
> > +#define HNS_TXD_FE_B 12
> > +#define HNS_TXD_VLD_B 13
> > +#define HNS_TXD_IPOFFSET_S 14
> > +#define HNS_TXD_IPOFFSET_M (0xff << HNS_TXD_IPOFFSET_S)
> > +
> > +#define HNS_RXD_IPOFFSET_S 0
> > +#define HNS_RXD_IPOFFSET_M (0xff << HNS_TXD_IPOFFSET_S)
> > +#define HNS_RXD_BUFNUM_S 8
> > +#define HNS_RXD_BUFNUM_M (0x3 << HNS_RXD_BUFNUM_S)
> > +#define HNS_RXD_PORTID_S 10
> > +#define HNS_RXD_PORTID_M (0x7 << HNS_RXD_PORTID_S)
> > +#define HNS_RXD_DMAC_S 13
> > +#define HNS_RXD_DMAC_M (0x3 << HNS_RXD_DMAC_S)
> > +#define HNS_RXD_VLAN_S 15
> > +#define HNS_RXD_VLAN_M (0x3 << HNS_RXD_VLAN_S)
> > +#define HNS_RXD_L3ID_S 17
> > +#define HNS_RXD_L3ID_M (0xf << HNS_RXD_L3ID_S)
> > +#define HNS_RXD_L4ID_S 21
> > +#define HNS_RXD_L4ID_M (0xf << HNS_RXD_L4ID_S)
> > +#define HNS_RXD_FE_B 25
> > +#define HNS_RXD_FRAG_B 26
> > +#define HNS_RXD_VLD_B 27
> > +#define HNS_RXD_L2E_B 28
> > +#define HNS_RXD_L3E_B 29
> > +#define HNS_RXD_L4E_B 30
> > +#define HNS_RXD_DROP_B 31
> > +
> > +#define HNS_RXD_VLANID_S 8
> > +#define HNS_RXD_VLANID_M (0xfff << HNS_RXD_VLANID_S)
> > +#define HNS_RXD_CFI_B 20
> > +#define HNS_RXD_PRI_S 21
> > +#define HNS_RXD_PRI_M (0x7 << HNS_RXD_PRI_S)
> > +#define HNS_RXD_ASID_S 24
> > +#define HNS_RXD_ASID_M (0xff << HNS_RXD_ASID_S)
> > +
> > +/* hardware spec ring buffer format */
> > +struct __packed hnae_desc {
> > + __le64 addr;
> > + union {
> > + struct {
> > + __le16 asid_bufnum_pid;
> > + __le16 send_size;
> > + __le32 flag_ipoffset;
> > + __le32 reserved_3[4];
> > + } tx;
> > +
> > + struct {
> > + __le32 ipoff_bnum_pid_flag;
> > + __le16 pkt_len;
> > + __le16 size;
> > + __le32 vlan_pri_asid;
> > + __le32 reserved_2[3];
> > + } rx;
> > + };
> > +};
> > +
> > +struct hnae_desc_cb {
> > + dma_addr_t dma; /* dma address of this desc */
> > + void *buf; /* cpu addr for a desc */
> > +
> > + /* priv data for the desc, e.g. skb when use with ip stack*/
> > + void *priv;
> > + u16 page_offset;
> > + u16 reuse_flag;
> > +
> > + u16 length; /* length of the buffer */
> > +
> > + /* desc type, used by the ring user to mark the type of the priv data */
> > + u16 type;
> > +};
> > +
> > +#define setflags(flags, bits) ((flags) |= (bits))
> > +#define unsetflags(flags, bits) ((flags) &= ~(bits))
> > +
> > +/* hnae_ring->flags fields */
> > +#define RINGF_DIR 0x1 /* TX or RX ring, set if TX */
> > +#define is_tx_ring(ring) ((ring)->flags & RINGF_DIR)
> > +#define is_rx_ring(ring) (!is_tx_ring(ring))
> > +#define ring_to_dma_dir(ring) (is_tx_ring(ring) ? \
> > + DMA_TO_DEVICE : DMA_FROM_DEVICE)
> > +
> > +struct ring_stats {
> > + u64 io_err_cnt;
> > + u64 sw_err_cnt;
> > + u64 seg_pkt_cnt;
> > + u64 reuse_pg_cnt;
> > + u64 err_pkt_len;
> > + u64 non_vld_descs;
> > + u64 err_bd_num;
> > + u64 csum_err;
> > + union {
> > + u64 tx_pkts;
> > + u64 rx_pkts;
> > + } pkts;
> > + union {
> > + u64 tx_bytes;
> > + u64 rx_bytes;
> > + } bytes;
> > + union {
> > + u64 tx_err_cnt;
> > + u64 rx_err_cnt;
> > + } err_cnt;
> > +};
> > +
> > +struct hnae_queue;
> > +
> > +struct hnae_ring {
> > + u8 __iomem *io_base; /* base io address for the ring */
> > + struct hnae_desc *desc; /* dma map address space */
> > + struct hnae_desc_cb *desc_cb;
> > + struct hnae_queue *q;
> > + int irq;
> > + char ring_name[RCB_RING_NAME_LEN];
> > +
> > + /* statistic */
> > + struct ring_stats stats;
> > +
> > + dma_addr_t desc_dma_addr;
> > + u32 buf_size; /* size for hnae_desc->addr, preset by AE */
> > + u16 desc_num; /* total number of desc */
> > + u16 max_desc_num_per_pkt;
> > + u16 max_raw_data_sz_per_desc;
> > + u16 max_pkt_size;
> > + int next_to_use; /* idx of next spare desc */
> > +
> > + /* idx of lastest sent desc, the ring is empty when equal to
> > + * next_to_use
> > + */
> > + int next_to_clean;
> > +
> > + int flags; /* ring attribute */
> > +};
> > +
> > +#define ring_ptr_move_fw(ring, p) \
> > + ((ring)->p = ((ring)->p + 1) % (ring)->desc_num)
> > +#define ring_ptr_move_bw(ring, p) \
> > + ((ring)->p = ((ring)->p - 1 + (ring)->desc_num) % (ring)->desc_num)
> > +
> > +enum hns_desc_type {
> > + DESC_TYPE_SKB,
> > + DESC_TYPE_PAGE,
> > +};
> > +
> > +#define assert_is_ring_idx(ring, idx) \
> > + assert((idx) >= 0 && (idx) < (ring)->desc_num)
> > +
> > +/* the distance between [begin, end) in a ring buffer
> > + * note: there is a unuse slot between the begin and the end
> > + */
> > +static inline int ring_dist(struct hnae_ring *ring, int begin, int end)
> > +{
> > + assert_is_ring_idx(ring, begin);
> > + assert_is_ring_idx(ring, end);
> > +
> > + return (end - begin + ring->desc_num) % ring->desc_num;
> > +}
> > +
> > +static inline int ring_space(struct hnae_ring *ring)
> > +{
> > + return ring->desc_num -
> > + ring_dist(ring, ring->next_to_clean, ring->next_to_use) - 1;
> > +}
> > +
> > +static inline int is_ring_empty(struct hnae_ring *ring)
> > +{
> > + assert_is_ring_idx(ring, ring->next_to_use);
> > + assert_is_ring_idx(ring, ring->next_to_clean);
> > +
> > + return ring->next_to_use == ring->next_to_clean;
> > +}
> > +
> > +#define hnae_buf_size(_ring) ((_ring)->buf_size)
> > +#define hnae_page_order(_ring) (get_order(hnae_buf_size(_ring)))
> > +#define hnae_page_size(_ring) (PAGE_SIZE << hnae_page_order(_ring))
> > +
> > +struct hnae_handle;
> > +
> > +/* allocate and dma map space for hnae desc */
> > +struct hnae_buf_ops {
> > + int (*alloc_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
> > + void (*free_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
> > + int (*map_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
> > + void (*unmap_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
> > +};
> > +
> > +struct hnae_queue {
> > + void __iomem *io_base;
> > + struct hnae_ae_dev *dev; /* the device who use this queue */
> > + struct hnae_ring rx_ring, tx_ring;
> > + struct hnae_handle *handle;
> > +};
> > +
> > +/*hnae loop mode*/
> > +enum hnae_loop {
> > + MAC_LOOP_NONE = 0,
> > + MAC_INTERNALLOOP_MAC,
> > + MAC_INTERNALLOOP_SERDES,
> > + MAC_INTERNALLOOP_PHY,
> > +};
> > +
> > +/*hnae port type*/
> > +enum hnae_port_type {
> > + HNAE_PORT_SERVICE = 0,
> > + HNAE_PORT_DEBUG
> > +};
> > +
> > +/* This struct defines the operation on the handle.
> > + *
> > + * get_handle(): (mandatory)
> > + * Get a handle from AE according to its name and options.
> > + * the AE driver should manage the space used by handle and its queues while
> > + * the HNAE framework will allocate desc and desc_cb for all rings in the
> > + * queues.
> > + * put_handle():
> > + * Release the handle.
> > + * start():
> > + * Enable the hardware, include all queues
> > + * stop():
> > + * Disable the hardware
> > + * set_opts(): (mandatory)
> > + * Set options to the AE
> > + * get_opts(): (mandatory)
> > + * Get options from the AE
> > + * get_status():
> > + * Get the carrier state of the back channel of the handle, 1 for ok, 0 for
> > + * non-ok
> > + * toggle_ring_irq(): (mandatory)
> > + * Set the ring irq to be enabled(0) or disable(1)
> > + * toggle_queue_status(): (mandatory)
> > + * Set the queue to be enabled(1) or disable(0), this will not change the
> > + * ring irq state
> > + * adjust_link()
> > + * adjust link status
> > + * set_loopback()
> > + * set loopback
> > + * get_ring_bdnum_limit()
> > + * get ring bd number limit
> > + * get_pauseparam()
> > + * get tx and rx of pause frame use
> > + * set_autoneg()
> > + * set auto autonegotiation of pause frame use
> > + * get_autoneg()
> > + * get auto autonegotiation of pause frame use
> > + * set_pauseparam()
> > + * set tx and rx of pause frame use
> > + * get_coalesce_usecs()
> > + * get usecs to delay a TX interrupt after a packet is sent
> > + * get_rx_max_coalesced_frames()
> > + * get Maximum number of packets to be sent before a TX interrupt.
> > + * set_coalesce_usecs()
> > + * set usecs to delay a TX interrupt after a packet is sent
> > + * set_coalesce_frames()
> > + * set Maximum number of packets to be sent before a TX interrupt.
> > + * get_ringnum()
> > + * get RX/TX ring number
> > + * get_max_ringnum()
> > + * get RX/TX ring maximum number
> > + * get_mac_addr()
> > + * get mac address
> > + * set_mac_addr()
> > + * set mac address
> > + * set_mc_addr()
> > + * set multicast mode
> > + * set_mtu()
> > + * set mtu
> > + * update_stats()
> > + * update Old network device statistics
> > + * get_ethtool_stats()
> > + * get ethtool network device statistics
> > + * get_strings()
> > + * get a set of strings that describe the requested objects
> > + * get_sset_count()
> > + * get number of strings that @get_strings will write
> > + * update_led_status()
> > + * update the led status
> > + * set_led_id()
> > + * set led id
> > + * get_regs()
> > + * get regs dump
> > + * get_regs_len()
> > + * get the len of the regs dump
> > + */
> > +struct hnae_ae_ops {
> > + struct hnae_handle *(*get_handle)(struct hnae_ae_dev *dev,
> > + const char *opts);
> > + void (*put_handle)(struct hnae_handle *handle);
> > + void (*init_queue)(struct hnae_queue *q);
> > + void (*fini_queue)(struct hnae_queue *q);
> > + int (*start)(struct hnae_handle *handle);
> > + void (*stop)(struct hnae_handle *handle);
> > + void (*reset)(struct hnae_handle *handle);
> > + int (*set_opts)(struct hnae_handle *handle, int type, void *opts);
> > + int (*get_opts)(struct hnae_handle *handle, int type, void **opts);
> > + int (*get_status)(struct hnae_handle *handle);
> > + int (*get_info)(struct hnae_handle *handle,
> > + u8 *auto_neg, u16 *speed, u8 *duplex);
> > + int (*set_info)(struct hnae_handle *handle,
> > + u8 auto_neg, u16 speed, u8 duplex);
> > + void (*toggle_ring_irq)(struct hnae_ring *ring, u32 val);
> > + void (*toggle_queue_status)(struct hnae_queue *queue, u32 val);
> > + void (*adjust_link)(struct hnae_handle *handle, int speed, int duplex);
> > + int (*set_loopback)(struct hnae_handle *handle,
> > + enum hnae_loop loop_mode, int en);
> > + void (*get_ring_bdnum_limit)(struct hnae_queue *queue,
> > + u32 *uplimit, u32 *lowlimit);
> > + void (*get_pauseparam)(struct hnae_handle *handle,
> > + u32 *auto_neg, u32 *rx_en, u32 *tx_en);
> > + int (*set_autoneg)(struct hnae_handle *handle, u8 enable);
> > + int (*get_autoneg)(struct hnae_handle *handle);
> > + void (*set_pauseparam)(struct hnae_handle *handle,
> > + u32 rx_en, u32 tx_en);
> > + void (*get_coalesce_usecs)(struct hnae_handle *handle,
> > + u32 *tx_usecs, u32 *rx_usecs);
> > + void (*get_rx_max_coalesced_frames)(struct hnae_handle *handle,
> > + u32 *tx_frames, u32 *rx_frames);
> > + void (*set_coalesce_usecs)(struct hnae_handle *handle, u32 timeout);
> > + int (*set_coalesce_frames)(struct hnae_handle *handle,
> > + u32 coalesce_frames);
> > + void (*get_ringnum)(struct hnae_handle *handle, u32 *ringnum);
> > + void (*get_max_ringnum)(struct hnae_handle *handle, u32 *max_ringnum);
> > + int (*get_mac_addr)(struct hnae_handle *handle, void **p);
> > + int (*set_mac_addr)(struct hnae_handle *handle, void *p);
> > + int (*set_mc_addr)(struct hnae_handle *handle, void *addr);
> > + int (*set_mtu)(struct hnae_handle *handle, int new_mtu);
> > + void (*update_stats)(struct hnae_handle *handle,
> > + struct net_device_stats *net_stats);
> > + void (*get_stats)(struct hnae_handle *handle, u64 *data);
> > + void (*get_strings)(struct hnae_handle *handle,
> > + u32 stringset, u8 *data);
> > + int (*get_sset_count)(struct hnae_handle *handle, int stringset);
> > + void (*update_led_status)(struct hnae_handle *handle);
> > + int (*set_led_id)(struct hnae_handle *handle,
> > + enum hnae_led_state status);
> > + void (*get_regs)(struct hnae_handle *handle, void *data);
> > + int (*get_regs_len)(struct hnae_handle *handle);
> > +};
> > +
> > +struct hnae_ae_dev {
> > + struct device cls_dev; /* the class dev */
> > + struct device *dev; /* the presented dev */
> > + struct hnae_ae_ops *ops;
> > + struct list_head node;
> > + struct module *owner; /* the module who provides this dev */
> > + int id;
> > + char name[AE_NAME_SIZE];
> > + struct list_head handle_list;
> > + spinlock_t lock; /* lock to protect the handle_list */
> > +};
> > +
> > +struct hnae_handle {
> > + struct device *owner_dev; /* the device which make use of this handle */
> > + struct hnae_ae_dev *dev; /* the device who provides this handle */
> > + struct device_node *phy_node;
> > + phy_interface_t phy_if;
> > + u32 if_support;
> > + int q_num;
> > + const char *ae_opts;
> > + enum hnae_port_type port_type;
> > + struct list_head node; /* list to hnae_ae_dev->handle_list */
> > + struct hnae_buf_ops *bops; /* operation for the buffer */
> > + struct net_device_stats net_stats;
> > + struct hnae_queue **qs; /* array base of all queues */
> > +};
> > +
> > +#define ring_to_dev(ring) ((ring)->q->dev->dev)
> > +
> > +struct hnae_handle *hnae_get_handle(struct device *owner_dev, const char *ae_id,
> > + const char *ae_opts,
> > + struct hnae_buf_ops *bops);
> > +void hnae_put_handle(struct hnae_handle *handle);
> > +int hnae_ae_register(struct hnae_ae_dev *dev, struct module *owner);
> > +void hnae_ae_unregister(struct hnae_ae_dev *dev);
> > +
> > +int hnae_register_notifier(struct notifier_block *nb);
> > +void hnae_unregister_notifier(struct notifier_block *nb);
> > +int hnae_reinit_handle(struct hnae_handle *handle);
> > +
> > +#define hnae_queue_xmit(q, buf_num) writel_relaxed(buf_num, \
> > + (q)->tx_ring.io_base + RCB_REG_TAIL)
> > +
> > +#ifndef assert
> > +#define assert(cond)
> > +#endif
> > +
> > +static inline int hnae_reserve_buffer_map(struct hnae_ring *ring,
> > + struct hnae_desc_cb *cb)
> > +{
> > + struct hnae_buf_ops *bops = ring->q->handle->bops;
> > + int ret;
> > +
> > + ret = bops->alloc_buffer(ring, cb);
> > + if (ret)
> > + goto out;
> > +
> > + ret = bops->map_buffer(ring, cb);
> > + if (ret)
> > + goto out_with_buf;
> > +
> > + return 0;
> > +
> > +out_with_buf:
> > + bops->free_buffer(ring, cb);
> > +out:
> > + return ret;
> > +}
> > +
> > +static inline int hnae_alloc_buffer_attach(struct hnae_ring *ring, int i)
> > +{
> > + int ret = hnae_reserve_buffer_map(ring, &ring->desc_cb[i]);
> > +
> > + if (ret)
> > + return ret;
> > +
> > + ring->desc[i].addr = (__le64)ring->desc_cb[i].dma;
> > +
> > + return 0;
> > +}
> > +
> > +static inline void hnae_buffer_detach(struct hnae_ring *ring, int i)
> > +{
> > + ring->q->handle->bops->unmap_buffer(ring, &ring->desc_cb[i]);
> > + ring->desc[i].addr = 0;
> > +}
> > +
> > +static inline void hnae_free_buffer_detach(struct hnae_ring *ring, int i)
> > +{
> > + struct hnae_buf_ops *bops = ring->q->handle->bops;
> > + struct hnae_desc_cb *cb = &ring->desc_cb[i];
> > +
> > + if (!ring->desc_cb[i].dma)
> > + return;
> > +
> > + hnae_buffer_detach(ring, i);
> > + bops->free_buffer(ring, cb);
> > +}
> > +
> > +/* detach a in-used buffer and replace with a reserved one */
> > +static inline void hnae_replace_buffer(struct hnae_ring *ring, int i,
> > + struct hnae_desc_cb *res_cb)
> > +{
> > + struct hnae_buf_ops *bops = ring->q->handle->bops;
> > + struct hnae_desc_cb tmp_cb = ring->desc_cb[i];
> > +
> > + bops->unmap_buffer(ring, &ring->desc_cb[i]);
> > + ring->desc_cb[i] = *res_cb;
> > + *res_cb = tmp_cb;
> > + ring->desc[i].addr = (__le64)ring->desc_cb[i].dma;
> > + ring->desc[i].rx.ipoff_bnum_pid_flag = 0;
> > +}
> > +
> > +static inline void hnae_reuse_buffer(struct hnae_ring *ring, int i)
> > +{
> > + ring->desc_cb[i].reuse_flag = 0;
> > + ring->desc[i].addr = (__le64)(ring->desc_cb[i].dma
> > + + ring->desc_cb[i].page_offset);
> > + ring->desc[i].rx.ipoff_bnum_pid_flag = 0;
> > +}
> > +
> > +#define hnae_set_field(origin, mask, shift, val) \
> > + do { \
> > + (origin) &= (~(mask)); \
> > + (origin) |= ((val) << (shift)) & (mask); \
> > + } while (0)
> > +
> > +#define hnae_set_bit(origin, shift, val) \
> > + hnae_set_field((origin), (0x1 << (shift)), (shift), (val))
> > +
> > +#define hnae_get_field(origin, mask, shift) (((origin) & (mask)) >> (shift))
> > +
> > +#define hnae_get_bit(origin, shift) \
> > + hnae_get_field((origin), (0x1 << (shift)), (shift))
> > +
> > +#endif
> > --
> > 1.9.1
> >
> >
> > _______________________________________________
> > linux-arm-kernel mailing list
> > linux-arm-kernel at lists.infradead.org
> > http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
>
>
>
> --
> Best regards, Klimov Alexey
^ permalink raw reply [flat|nested] 15+ messages in thread
* 答复: [PATCH 1/5] net: add Hisilicon Network Subsystem support (config and documents)
2015-08-17 1:28 ` 答复: " Liguozhu (Kenneth)
@ 2015-08-21 14:00 ` Arnd Bergmann
2015-08-27 9:50 ` Kenneth Lee
0 siblings, 1 reply; 15+ messages in thread
From: Arnd Bergmann @ 2015-08-21 14:00 UTC (permalink / raw)
To: linux-arm-kernel
On Monday 17 August 2015 01:28:07 Liguozhu wrote:
> Thanks, Arnd.
>
> Regarding the ae-name: it is the name of the Acceleration Engine. It is provided
> by the BIOS according to the position and the feature enabled of the IP.
> So "soc0" means it is on SoC No. 0, while "n4" means it is running on
>"Non-dsaf mode 4". Ideally, we should setup the rule to name it. But as I
> said in the patchset, the IP is original designed for a bare metal solution,
> it is worthless to export all modes and we are planning to add more mode
> for Linux itself in the IP in future version. So I think the better way is
> to leave it as a "name" but add more meaning in the future.
The name property is a bit awkward. The position is normally implied by
the location of the parent device in the DT, so you should not need that
at all and instead derive it elsewhere. You can also add strings to the
compatible property instead of this, to signify differences in the programming
that are based on how the IP block is used.
> Regarding the ae-opts: it is the initial value for the AE's runtime options,
> Currently, we have only "port number" (there are 6XGE+2GE port for a DSAF AE)
> as option. But for future version, we will add other options such as "enable
> Spanning Tree Protocol algorithm)" and so on.
I think these can easily be converted into an index property and boolean
flags (present if true, absent otherwise) for additional features.
> Should I add these background to somewhere?
The binding document needs to list all supported configurations, if you
have a string property, describe specifically what strings are allowed
and what they mean, but better try to avoid strings altogether.
Arnd
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 3/5] net: add Hisilicon Network Subsystem MDIO support
2015-08-17 9:17 ` Kenneth Lee
@ 2015-08-21 14:01 ` Arnd Bergmann
0 siblings, 0 replies; 15+ messages in thread
From: Arnd Bergmann @ 2015-08-21 14:01 UTC (permalink / raw)
To: linux-arm-kernel
On Monday 17 August 2015 17:17:50 Kenneth Lee wrote:
> Thanks, Arnd,
>
> You are right. This is the same IP as hip04_mdio.c. We just mis-understand the
> hardware design. We will merge them and re-submit the patches.
Ok, great!
Arnd
^ permalink raw reply [flat|nested] 15+ messages in thread
* 答复: [PATCH 1/5] net: add Hisilicon Network Subsystem support (config and documents)
2015-08-21 14:00 ` Arnd Bergmann
@ 2015-08-27 9:50 ` Kenneth Lee
0 siblings, 0 replies; 15+ messages in thread
From: Kenneth Lee @ 2015-08-27 9:50 UTC (permalink / raw)
To: linux-arm-kernel
On Fri, Aug 21, 2015 at 04:00:35PM +0200, Arnd Bergmann wrote:
> Date: Fri, 21 Aug 2015 16:00:35 +0200
> From: Arnd Bergmann <arnd@arndb.de>
> To: "Liguozhu (Kenneth)" <liguozhu@hisilicon.com>
> CC: "mark.rutland at arm.com" <mark.rutland@arm.com>,
> "devicetree at vger.kernel.org" <devicetree@vger.kernel.org>,
> "pawel.moll at arm.com" <pawel.moll@arm.com>, "ijc+devicetree at hellion.org.uk"
> <ijc+devicetree@hellion.org.uk>, "catalin.marinas at arm.com"
> <catalin.marinas@arm.com>, "will.deacon at arm.com" <will.deacon@arm.com>,
> "linux-kernel at vger.kernel.org" <linux-kernel@vger.kernel.org>, Linuxarm
> <linuxarm@huawei.com>, "paul.gortmaker at windriver.com"
> <paul.gortmaker@windriver.com>, "robh+dt at kernel.org" <robh+dt@kernel.org>,
> "galak at codeaurora.org" <galak@codeaurora.org>, "zhangfei.gao at linaro.org"
> <zhangfei.gao@linaro.org>, "netdev at vger.kernel.org"
> <netdev@vger.kernel.org>, "davem at davemloft.net" <davem@davemloft.net>,
> "linux-arm-kernel at lists.infradead.org"
> <linux-arm-kernel@lists.infradead.org>
> Subject: Re: ??: [PATCH 1/5] net: add Hisilicon Network Subsystem
> support (config and documents)
> User-Agent: KMail/4.11.5 (Linux/3.16.0-10-generic; KDE/4.11.5; x86_64; ; )
> Message-ID: <2543796.7JthO5WCfI@wuerfel>
>
> On Monday 17 August 2015 01:28:07 Liguozhu wrote:
> > Thanks, Arnd.
> >
> > Regarding the ae-name: it is the name of the Acceleration Engine. It is provided
> > by the BIOS according to the position and the feature enabled of the IP.
> > So "soc0" means it is on SoC No. 0, while "n4" means it is running on
> >"Non-dsaf mode 4". Ideally, we should setup the rule to name it. But as I
> > said in the patchset, the IP is original designed for a bare metal solution,
> > it is worthless to export all modes and we are planning to add more mode
> > for Linux itself in the IP in future version. So I think the better way is
> > to leave it as a "name" but add more meaning in the future.
>
> The name property is a bit awkward. The position is normally implied by
> the location of the parent device in the DT, so you should not need that
> at all and instead derive it elsewhere. You can also add strings to the
> compatible property instead of this, to signify differences in the programming
> that are based on how the IP block is used.
>
> > Regarding the ae-opts: it is the initial value for the AE's runtime options,
> > Currently, we have only "port number" (there are 6XGE+2GE port for a DSAF AE)
> > as option. But for future version, we will add other options such as "enable
> > Spanning Tree Protocol algorithm)" and so on.
>
> I think these can easily be converted into an index property and boolean
> flags (present if true, absent otherwise) for additional features.
>
> > Should I add these background to somewhere?
>
> The binding document needs to list all supported configurations, if you
> have a string property, describe specifically what strings are allowed
> and what they mean, but better try to avoid strings altogether.
>
> Arnd
> _______________________________________________
> linuxarm mailing list
> linuxarm at huawei.com
> http://rnd-openeuler.huawei.com/mailman/listinfo/linuxarm
Dear Arnd,
We are working on the new PatchSet. I describe the new design here so in case
you can tell us if we make something wrong.
So now we will keep some attributes in enthernet node like this:
ethernet at 0{
compatible = "hisilicon,hns-nic";
ae-name = "dsaf1";
port-id = <0>;
};
ae-name is simply a name referring to the name of dsa_name in SAF node.
port-id is the index of port provided by DSAF (the accelerator). DSAF can
connect to 8 PHYs. Port 0 to 1 are both used for adminstration purpose. They
are called debug ports.
The remaining 6 PHYs are taken according to the mode of DSAF.
In NIC mode of DSAF, all 6 PHYs are taken as ethernet ports to the CPU. The
port-id can be 2 to 7. Here is the diagram:
+-----+---------------+
| CPU |
+-+-+-+---+-+-+-+-+-+-+
| | | | | | | |
debug service
port port
(0,1) (2-7)
In Switch mode of DSAF, all 6 PHYs are taken as physical ports connect to a
LAN Switch while the CPU side assume itself have one single NIC connect to
this switch. In this case, the port-id will be 2 only.
+-----+---------------+
| CPU |
+-+-+-+---+-+-+-+-+-+-+
| | | service port(2)
debug +------------+
port | switch |
(0,1) +-+-+-+-+-+-++
| | | | | |
external
port
--
-Kenneth(Hisilicon)
^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2015-08-27 9:50 UTC | newest]
Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-14 10:30 [PATCH 0/5] net: Hisilicon Network Subsystem support Kenneth Lee
2015-08-14 10:30 ` [PATCH 1/5] net: add Hisilicon Network Subsystem support (config and documents) Kenneth Lee
2015-08-14 20:49 ` Arnd Bergmann
2015-08-17 1:28 ` 答复: " Liguozhu (Kenneth)
2015-08-21 14:00 ` Arnd Bergmann
2015-08-27 9:50 ` Kenneth Lee
2015-08-14 10:30 ` [PATCH 2/5] net: add Hisilicon Network Subsystem hnae framework support Kenneth Lee
2015-08-17 19:25 ` David Miller
2015-08-18 0:12 ` Alexey Klimov
2015-08-21 6:36 ` Kenneth Lee
2015-08-14 10:30 ` [PATCH 3/5] net: add Hisilicon Network Subsystem MDIO support Kenneth Lee
2015-08-14 20:57 ` Arnd Bergmann
2015-08-17 9:17 ` Kenneth Lee
2015-08-21 14:01 ` Arnd Bergmann
2015-08-14 10:30 ` [PATCH 5/5] net: add Hisilicon Network Subsystem basic ethernet support Kenneth Lee
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).