xref: /linux/drivers/net/ethernet/hisilicon/hns3/hns3_enet.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
3 
4 #include <linux/dma-mapping.h>
5 #include <linux/etherdevice.h>
6 #include <linux/interrupt.h>
7 #ifdef CONFIG_RFS_ACCEL
8 #include <linux/cpu_rmap.h>
9 #endif
10 #include <linux/if_vlan.h>
11 #include <linux/irq.h>
12 #include <linux/ip.h>
13 #include <linux/ipv6.h>
14 #include <linux/iommu.h>
15 #include <linux/module.h>
16 #include <linux/pci.h>
17 #include <linux/skbuff.h>
18 #include <linux/sctp.h>
19 #include <net/gre.h>
20 #include <net/gro.h>
21 #include <net/ip6_checksum.h>
22 #include <net/page_pool/helpers.h>
23 #include <net/pkt_cls.h>
24 #include <net/pkt_sched.h>
25 #include <net/tcp.h>
26 #include <net/vxlan.h>
27 #include <net/geneve.h>
28 #include <net/netdev_queues.h>
29 
30 #include "hnae3.h"
31 #include "hns3_enet.h"
32 /* All hns3 tracepoints are defined by the include below, which
33  * must be included exactly once across the whole kernel with
34  * CREATE_TRACE_POINTS defined
35  */
36 #define CREATE_TRACE_POINTS
37 #include "hns3_trace.h"
38 
39 #define hns3_set_field(origin, shift, val)	((origin) |= (val) << (shift))
40 #define hns3_tx_bd_count(S)	DIV_ROUND_UP(S, HNS3_MAX_BD_SIZE)
41 
42 #define hns3_rl_err(fmt, ...)						\
43 	do {								\
44 		if (net_ratelimit())					\
45 			netdev_err(fmt, ##__VA_ARGS__);			\
46 	} while (0)
47 
48 static void hns3_clear_all_ring(struct hnae3_handle *h, bool force);
49 
50 static const char hns3_driver_name[] = "hns3";
51 static const char hns3_driver_string[] =
52 			"Hisilicon Ethernet Network Driver for Hip08 Family";
53 static const char hns3_copyright[] = "Copyright (c) 2017 Huawei Corporation.";
54 static struct hnae3_client client;
55 
56 static int debug = -1;
57 module_param(debug, int, 0);
58 MODULE_PARM_DESC(debug, " Network interface message level setting");
59 
60 static unsigned int tx_sgl = 1;
61 module_param(tx_sgl, uint, 0600);
62 MODULE_PARM_DESC(tx_sgl, "Minimum number of frags when using dma_map_sg() to optimize the IOMMU mapping");
63 
64 static bool page_pool_enabled = true;
65 module_param(page_pool_enabled, bool, 0400);
66 
67 #define HNS3_SGL_SIZE(nfrag)	(sizeof(struct scatterlist) * (nfrag) +	\
68 				 sizeof(struct sg_table))
69 #define HNS3_MAX_SGL_SIZE	ALIGN(HNS3_SGL_SIZE(HNS3_MAX_TSO_BD_NUM), \
70 				      dma_get_cache_alignment())
71 
72 #define DEFAULT_MSG_LEVEL (NETIF_MSG_PROBE | NETIF_MSG_LINK | \
73 			   NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
74 
75 #define HNS3_INNER_VLAN_TAG	1
76 #define HNS3_OUTER_VLAN_TAG	2
77 
78 #define HNS3_MIN_TX_LEN		33U
79 #define HNS3_MIN_TUN_PKT_LEN	65U
80 
81 /* hns3_pci_tbl - PCI Device ID Table
82  *
83  * Last entry must be all 0s
84  *
85  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
86  *   Class, Class Mask, private data (not used) }
87  */
88 static const struct pci_device_id hns3_pci_tbl[] = {
89 	{
90 		PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_GE),
91 		.driver_data = 0,
92 	}, {
93 		PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE),
94 		.driver_data = 0,
95 	}, {
96 		PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA),
97 		.driver_data = HNAE3_DEV_SUPPORT_ROCE_DCB_BITS,
98 	}, {
99 		PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA_MACSEC),
100 		.driver_data = HNAE3_DEV_SUPPORT_ROCE_DCB_BITS,
101 	}, {
102 		PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA),
103 		.driver_data = HNAE3_DEV_SUPPORT_ROCE_DCB_BITS,
104 	}, {
105 		PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA_MACSEC),
106 		.driver_data = HNAE3_DEV_SUPPORT_ROCE_DCB_BITS,
107 	}, {
108 		PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_MACSEC),
109 		.driver_data = HNAE3_DEV_SUPPORT_ROCE_DCB_BITS,
110 	}, {
111 		PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_200G_RDMA),
112 		.driver_data = HNAE3_DEV_SUPPORT_ROCE_DCB_BITS,
113 	}, {
114 		PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_VF),
115 		.driver_data = 0,
116 	}, {
117 		PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_RDMA_DCB_PFC_VF),
118 		.driver_data = HNAE3_DEV_SUPPORT_ROCE_DCB_BITS,
119 	},
120 	/* required last entry */
121 	{ }
122 };
123 MODULE_DEVICE_TABLE(pci, hns3_pci_tbl);
124 
125 #define HNS3_RX_PTYPE_ENTRY(ptype, l, s, t, h) \
126 	{	ptype, \
127 		l, \
128 		CHECKSUM_##s, \
129 		HNS3_L3_TYPE_##t, \
130 		1, \
131 		h}
132 
133 #define HNS3_RX_PTYPE_UNUSED_ENTRY(ptype) \
134 		{ ptype, 0, CHECKSUM_NONE, HNS3_L3_TYPE_PARSE_FAIL, 0, \
135 		  PKT_HASH_TYPE_NONE }
136 
137 static const struct hns3_rx_ptype hns3_rx_ptype_tbl[] = {
138 	HNS3_RX_PTYPE_UNUSED_ENTRY(0),
139 	HNS3_RX_PTYPE_ENTRY(1, 0, COMPLETE, ARP, PKT_HASH_TYPE_NONE),
140 	HNS3_RX_PTYPE_ENTRY(2, 0, COMPLETE, RARP, PKT_HASH_TYPE_NONE),
141 	HNS3_RX_PTYPE_ENTRY(3, 0, COMPLETE, LLDP, PKT_HASH_TYPE_NONE),
142 	HNS3_RX_PTYPE_ENTRY(4, 0, COMPLETE, PARSE_FAIL, PKT_HASH_TYPE_NONE),
143 	HNS3_RX_PTYPE_ENTRY(5, 0, COMPLETE, PARSE_FAIL, PKT_HASH_TYPE_NONE),
144 	HNS3_RX_PTYPE_ENTRY(6, 0, COMPLETE, PARSE_FAIL, PKT_HASH_TYPE_NONE),
145 	HNS3_RX_PTYPE_ENTRY(7, 0, COMPLETE, CNM, PKT_HASH_TYPE_NONE),
146 	HNS3_RX_PTYPE_ENTRY(8, 0, NONE, PARSE_FAIL, PKT_HASH_TYPE_NONE),
147 	HNS3_RX_PTYPE_UNUSED_ENTRY(9),
148 	HNS3_RX_PTYPE_UNUSED_ENTRY(10),
149 	HNS3_RX_PTYPE_UNUSED_ENTRY(11),
150 	HNS3_RX_PTYPE_UNUSED_ENTRY(12),
151 	HNS3_RX_PTYPE_UNUSED_ENTRY(13),
152 	HNS3_RX_PTYPE_UNUSED_ENTRY(14),
153 	HNS3_RX_PTYPE_UNUSED_ENTRY(15),
154 	HNS3_RX_PTYPE_ENTRY(16, 0, COMPLETE, PARSE_FAIL, PKT_HASH_TYPE_NONE),
155 	HNS3_RX_PTYPE_ENTRY(17, 0, COMPLETE, IPV4, PKT_HASH_TYPE_NONE),
156 	HNS3_RX_PTYPE_ENTRY(18, 0, COMPLETE, IPV4, PKT_HASH_TYPE_NONE),
157 	HNS3_RX_PTYPE_ENTRY(19, 0, UNNECESSARY, IPV4, PKT_HASH_TYPE_L4),
158 	HNS3_RX_PTYPE_ENTRY(20, 0, UNNECESSARY, IPV4, PKT_HASH_TYPE_L4),
159 	HNS3_RX_PTYPE_ENTRY(21, 0, NONE, IPV4, PKT_HASH_TYPE_NONE),
160 	HNS3_RX_PTYPE_ENTRY(22, 0, UNNECESSARY, IPV4, PKT_HASH_TYPE_L4),
161 	HNS3_RX_PTYPE_ENTRY(23, 0, NONE, IPV4, PKT_HASH_TYPE_L3),
162 	HNS3_RX_PTYPE_ENTRY(24, 0, NONE, IPV4, PKT_HASH_TYPE_L3),
163 	HNS3_RX_PTYPE_ENTRY(25, 0, UNNECESSARY, IPV4, PKT_HASH_TYPE_L4),
164 	HNS3_RX_PTYPE_UNUSED_ENTRY(26),
165 	HNS3_RX_PTYPE_UNUSED_ENTRY(27),
166 	HNS3_RX_PTYPE_UNUSED_ENTRY(28),
167 	HNS3_RX_PTYPE_ENTRY(29, 0, COMPLETE, PARSE_FAIL, PKT_HASH_TYPE_NONE),
168 	HNS3_RX_PTYPE_ENTRY(30, 0, COMPLETE, PARSE_FAIL, PKT_HASH_TYPE_NONE),
169 	HNS3_RX_PTYPE_ENTRY(31, 0, COMPLETE, IPV4, PKT_HASH_TYPE_L3),
170 	HNS3_RX_PTYPE_ENTRY(32, 0, COMPLETE, IPV4, PKT_HASH_TYPE_L3),
171 	HNS3_RX_PTYPE_ENTRY(33, 1, UNNECESSARY, IPV4, PKT_HASH_TYPE_L4),
172 	HNS3_RX_PTYPE_ENTRY(34, 1, UNNECESSARY, IPV4, PKT_HASH_TYPE_L4),
173 	HNS3_RX_PTYPE_ENTRY(35, 1, UNNECESSARY, IPV4, PKT_HASH_TYPE_L4),
174 	HNS3_RX_PTYPE_ENTRY(36, 0, COMPLETE, IPV4, PKT_HASH_TYPE_L3),
175 	HNS3_RX_PTYPE_ENTRY(37, 0, COMPLETE, IPV4, PKT_HASH_TYPE_L3),
176 	HNS3_RX_PTYPE_UNUSED_ENTRY(38),
177 	HNS3_RX_PTYPE_ENTRY(39, 0, COMPLETE, IPV6, PKT_HASH_TYPE_L3),
178 	HNS3_RX_PTYPE_ENTRY(40, 0, COMPLETE, IPV6, PKT_HASH_TYPE_L3),
179 	HNS3_RX_PTYPE_ENTRY(41, 1, UNNECESSARY, IPV6, PKT_HASH_TYPE_L4),
180 	HNS3_RX_PTYPE_ENTRY(42, 1, UNNECESSARY, IPV6, PKT_HASH_TYPE_L4),
181 	HNS3_RX_PTYPE_ENTRY(43, 1, UNNECESSARY, IPV6, PKT_HASH_TYPE_L4),
182 	HNS3_RX_PTYPE_ENTRY(44, 0, COMPLETE, IPV6, PKT_HASH_TYPE_L3),
183 	HNS3_RX_PTYPE_ENTRY(45, 0, COMPLETE, IPV6, PKT_HASH_TYPE_L3),
184 	HNS3_RX_PTYPE_UNUSED_ENTRY(46),
185 	HNS3_RX_PTYPE_UNUSED_ENTRY(47),
186 	HNS3_RX_PTYPE_UNUSED_ENTRY(48),
187 	HNS3_RX_PTYPE_UNUSED_ENTRY(49),
188 	HNS3_RX_PTYPE_UNUSED_ENTRY(50),
189 	HNS3_RX_PTYPE_UNUSED_ENTRY(51),
190 	HNS3_RX_PTYPE_UNUSED_ENTRY(52),
191 	HNS3_RX_PTYPE_UNUSED_ENTRY(53),
192 	HNS3_RX_PTYPE_UNUSED_ENTRY(54),
193 	HNS3_RX_PTYPE_UNUSED_ENTRY(55),
194 	HNS3_RX_PTYPE_UNUSED_ENTRY(56),
195 	HNS3_RX_PTYPE_UNUSED_ENTRY(57),
196 	HNS3_RX_PTYPE_UNUSED_ENTRY(58),
197 	HNS3_RX_PTYPE_UNUSED_ENTRY(59),
198 	HNS3_RX_PTYPE_UNUSED_ENTRY(60),
199 	HNS3_RX_PTYPE_UNUSED_ENTRY(61),
200 	HNS3_RX_PTYPE_UNUSED_ENTRY(62),
201 	HNS3_RX_PTYPE_UNUSED_ENTRY(63),
202 	HNS3_RX_PTYPE_UNUSED_ENTRY(64),
203 	HNS3_RX_PTYPE_UNUSED_ENTRY(65),
204 	HNS3_RX_PTYPE_UNUSED_ENTRY(66),
205 	HNS3_RX_PTYPE_UNUSED_ENTRY(67),
206 	HNS3_RX_PTYPE_UNUSED_ENTRY(68),
207 	HNS3_RX_PTYPE_UNUSED_ENTRY(69),
208 	HNS3_RX_PTYPE_UNUSED_ENTRY(70),
209 	HNS3_RX_PTYPE_UNUSED_ENTRY(71),
210 	HNS3_RX_PTYPE_UNUSED_ENTRY(72),
211 	HNS3_RX_PTYPE_UNUSED_ENTRY(73),
212 	HNS3_RX_PTYPE_UNUSED_ENTRY(74),
213 	HNS3_RX_PTYPE_UNUSED_ENTRY(75),
214 	HNS3_RX_PTYPE_UNUSED_ENTRY(76),
215 	HNS3_RX_PTYPE_UNUSED_ENTRY(77),
216 	HNS3_RX_PTYPE_UNUSED_ENTRY(78),
217 	HNS3_RX_PTYPE_UNUSED_ENTRY(79),
218 	HNS3_RX_PTYPE_UNUSED_ENTRY(80),
219 	HNS3_RX_PTYPE_UNUSED_ENTRY(81),
220 	HNS3_RX_PTYPE_UNUSED_ENTRY(82),
221 	HNS3_RX_PTYPE_UNUSED_ENTRY(83),
222 	HNS3_RX_PTYPE_UNUSED_ENTRY(84),
223 	HNS3_RX_PTYPE_UNUSED_ENTRY(85),
224 	HNS3_RX_PTYPE_UNUSED_ENTRY(86),
225 	HNS3_RX_PTYPE_UNUSED_ENTRY(87),
226 	HNS3_RX_PTYPE_UNUSED_ENTRY(88),
227 	HNS3_RX_PTYPE_UNUSED_ENTRY(89),
228 	HNS3_RX_PTYPE_UNUSED_ENTRY(90),
229 	HNS3_RX_PTYPE_UNUSED_ENTRY(91),
230 	HNS3_RX_PTYPE_UNUSED_ENTRY(92),
231 	HNS3_RX_PTYPE_UNUSED_ENTRY(93),
232 	HNS3_RX_PTYPE_UNUSED_ENTRY(94),
233 	HNS3_RX_PTYPE_UNUSED_ENTRY(95),
234 	HNS3_RX_PTYPE_UNUSED_ENTRY(96),
235 	HNS3_RX_PTYPE_UNUSED_ENTRY(97),
236 	HNS3_RX_PTYPE_UNUSED_ENTRY(98),
237 	HNS3_RX_PTYPE_UNUSED_ENTRY(99),
238 	HNS3_RX_PTYPE_UNUSED_ENTRY(100),
239 	HNS3_RX_PTYPE_UNUSED_ENTRY(101),
240 	HNS3_RX_PTYPE_UNUSED_ENTRY(102),
241 	HNS3_RX_PTYPE_UNUSED_ENTRY(103),
242 	HNS3_RX_PTYPE_UNUSED_ENTRY(104),
243 	HNS3_RX_PTYPE_UNUSED_ENTRY(105),
244 	HNS3_RX_PTYPE_UNUSED_ENTRY(106),
245 	HNS3_RX_PTYPE_UNUSED_ENTRY(107),
246 	HNS3_RX_PTYPE_UNUSED_ENTRY(108),
247 	HNS3_RX_PTYPE_UNUSED_ENTRY(109),
248 	HNS3_RX_PTYPE_UNUSED_ENTRY(110),
249 	HNS3_RX_PTYPE_ENTRY(111, 0, COMPLETE, IPV6, PKT_HASH_TYPE_L3),
250 	HNS3_RX_PTYPE_ENTRY(112, 0, COMPLETE, IPV6, PKT_HASH_TYPE_L3),
251 	HNS3_RX_PTYPE_ENTRY(113, 0, UNNECESSARY, IPV6, PKT_HASH_TYPE_L4),
252 	HNS3_RX_PTYPE_ENTRY(114, 0, UNNECESSARY, IPV6, PKT_HASH_TYPE_L4),
253 	HNS3_RX_PTYPE_ENTRY(115, 0, NONE, IPV6, PKT_HASH_TYPE_L3),
254 	HNS3_RX_PTYPE_ENTRY(116, 0, UNNECESSARY, IPV6, PKT_HASH_TYPE_L4),
255 	HNS3_RX_PTYPE_ENTRY(117, 0, NONE, IPV6, PKT_HASH_TYPE_L3),
256 	HNS3_RX_PTYPE_ENTRY(118, 0, NONE, IPV6, PKT_HASH_TYPE_L3),
257 	HNS3_RX_PTYPE_ENTRY(119, 0, UNNECESSARY, IPV6, PKT_HASH_TYPE_L4),
258 	HNS3_RX_PTYPE_UNUSED_ENTRY(120),
259 	HNS3_RX_PTYPE_UNUSED_ENTRY(121),
260 	HNS3_RX_PTYPE_UNUSED_ENTRY(122),
261 	HNS3_RX_PTYPE_ENTRY(123, 0, COMPLETE, PARSE_FAIL, PKT_HASH_TYPE_NONE),
262 	HNS3_RX_PTYPE_ENTRY(124, 0, COMPLETE, PARSE_FAIL, PKT_HASH_TYPE_NONE),
263 	HNS3_RX_PTYPE_ENTRY(125, 0, COMPLETE, IPV4, PKT_HASH_TYPE_L3),
264 	HNS3_RX_PTYPE_ENTRY(126, 0, COMPLETE, IPV4, PKT_HASH_TYPE_L3),
265 	HNS3_RX_PTYPE_ENTRY(127, 1, UNNECESSARY, IPV4, PKT_HASH_TYPE_L4),
266 	HNS3_RX_PTYPE_ENTRY(128, 1, UNNECESSARY, IPV4, PKT_HASH_TYPE_L4),
267 	HNS3_RX_PTYPE_ENTRY(129, 1, UNNECESSARY, IPV4, PKT_HASH_TYPE_L4),
268 	HNS3_RX_PTYPE_ENTRY(130, 0, COMPLETE, IPV4, PKT_HASH_TYPE_L3),
269 	HNS3_RX_PTYPE_ENTRY(131, 0, COMPLETE, IPV4, PKT_HASH_TYPE_L3),
270 	HNS3_RX_PTYPE_UNUSED_ENTRY(132),
271 	HNS3_RX_PTYPE_ENTRY(133, 0, COMPLETE, IPV6, PKT_HASH_TYPE_L3),
272 	HNS3_RX_PTYPE_ENTRY(134, 0, COMPLETE, IPV6, PKT_HASH_TYPE_L3),
273 	HNS3_RX_PTYPE_ENTRY(135, 1, UNNECESSARY, IPV6, PKT_HASH_TYPE_L4),
274 	HNS3_RX_PTYPE_ENTRY(136, 1, UNNECESSARY, IPV6, PKT_HASH_TYPE_L4),
275 	HNS3_RX_PTYPE_ENTRY(137, 1, UNNECESSARY, IPV6, PKT_HASH_TYPE_L4),
276 	HNS3_RX_PTYPE_ENTRY(138, 0, COMPLETE, IPV6, PKT_HASH_TYPE_L3),
277 	HNS3_RX_PTYPE_ENTRY(139, 0, COMPLETE, IPV6, PKT_HASH_TYPE_L3),
278 	HNS3_RX_PTYPE_UNUSED_ENTRY(140),
279 	HNS3_RX_PTYPE_UNUSED_ENTRY(141),
280 	HNS3_RX_PTYPE_UNUSED_ENTRY(142),
281 	HNS3_RX_PTYPE_UNUSED_ENTRY(143),
282 	HNS3_RX_PTYPE_UNUSED_ENTRY(144),
283 	HNS3_RX_PTYPE_UNUSED_ENTRY(145),
284 	HNS3_RX_PTYPE_UNUSED_ENTRY(146),
285 	HNS3_RX_PTYPE_UNUSED_ENTRY(147),
286 	HNS3_RX_PTYPE_UNUSED_ENTRY(148),
287 	HNS3_RX_PTYPE_UNUSED_ENTRY(149),
288 	HNS3_RX_PTYPE_UNUSED_ENTRY(150),
289 	HNS3_RX_PTYPE_UNUSED_ENTRY(151),
290 	HNS3_RX_PTYPE_UNUSED_ENTRY(152),
291 	HNS3_RX_PTYPE_UNUSED_ENTRY(153),
292 	HNS3_RX_PTYPE_UNUSED_ENTRY(154),
293 	HNS3_RX_PTYPE_UNUSED_ENTRY(155),
294 	HNS3_RX_PTYPE_UNUSED_ENTRY(156),
295 	HNS3_RX_PTYPE_UNUSED_ENTRY(157),
296 	HNS3_RX_PTYPE_UNUSED_ENTRY(158),
297 	HNS3_RX_PTYPE_UNUSED_ENTRY(159),
298 	HNS3_RX_PTYPE_UNUSED_ENTRY(160),
299 	HNS3_RX_PTYPE_UNUSED_ENTRY(161),
300 	HNS3_RX_PTYPE_UNUSED_ENTRY(162),
301 	HNS3_RX_PTYPE_UNUSED_ENTRY(163),
302 	HNS3_RX_PTYPE_UNUSED_ENTRY(164),
303 	HNS3_RX_PTYPE_UNUSED_ENTRY(165),
304 	HNS3_RX_PTYPE_UNUSED_ENTRY(166),
305 	HNS3_RX_PTYPE_UNUSED_ENTRY(167),
306 	HNS3_RX_PTYPE_UNUSED_ENTRY(168),
307 	HNS3_RX_PTYPE_UNUSED_ENTRY(169),
308 	HNS3_RX_PTYPE_UNUSED_ENTRY(170),
309 	HNS3_RX_PTYPE_UNUSED_ENTRY(171),
310 	HNS3_RX_PTYPE_UNUSED_ENTRY(172),
311 	HNS3_RX_PTYPE_UNUSED_ENTRY(173),
312 	HNS3_RX_PTYPE_UNUSED_ENTRY(174),
313 	HNS3_RX_PTYPE_UNUSED_ENTRY(175),
314 	HNS3_RX_PTYPE_UNUSED_ENTRY(176),
315 	HNS3_RX_PTYPE_UNUSED_ENTRY(177),
316 	HNS3_RX_PTYPE_UNUSED_ENTRY(178),
317 	HNS3_RX_PTYPE_UNUSED_ENTRY(179),
318 	HNS3_RX_PTYPE_UNUSED_ENTRY(180),
319 	HNS3_RX_PTYPE_UNUSED_ENTRY(181),
320 	HNS3_RX_PTYPE_UNUSED_ENTRY(182),
321 	HNS3_RX_PTYPE_UNUSED_ENTRY(183),
322 	HNS3_RX_PTYPE_UNUSED_ENTRY(184),
323 	HNS3_RX_PTYPE_UNUSED_ENTRY(185),
324 	HNS3_RX_PTYPE_UNUSED_ENTRY(186),
325 	HNS3_RX_PTYPE_UNUSED_ENTRY(187),
326 	HNS3_RX_PTYPE_UNUSED_ENTRY(188),
327 	HNS3_RX_PTYPE_UNUSED_ENTRY(189),
328 	HNS3_RX_PTYPE_UNUSED_ENTRY(190),
329 	HNS3_RX_PTYPE_UNUSED_ENTRY(191),
330 	HNS3_RX_PTYPE_UNUSED_ENTRY(192),
331 	HNS3_RX_PTYPE_UNUSED_ENTRY(193),
332 	HNS3_RX_PTYPE_UNUSED_ENTRY(194),
333 	HNS3_RX_PTYPE_UNUSED_ENTRY(195),
334 	HNS3_RX_PTYPE_UNUSED_ENTRY(196),
335 	HNS3_RX_PTYPE_UNUSED_ENTRY(197),
336 	HNS3_RX_PTYPE_UNUSED_ENTRY(198),
337 	HNS3_RX_PTYPE_UNUSED_ENTRY(199),
338 	HNS3_RX_PTYPE_UNUSED_ENTRY(200),
339 	HNS3_RX_PTYPE_UNUSED_ENTRY(201),
340 	HNS3_RX_PTYPE_UNUSED_ENTRY(202),
341 	HNS3_RX_PTYPE_UNUSED_ENTRY(203),
342 	HNS3_RX_PTYPE_UNUSED_ENTRY(204),
343 	HNS3_RX_PTYPE_UNUSED_ENTRY(205),
344 	HNS3_RX_PTYPE_UNUSED_ENTRY(206),
345 	HNS3_RX_PTYPE_UNUSED_ENTRY(207),
346 	HNS3_RX_PTYPE_UNUSED_ENTRY(208),
347 	HNS3_RX_PTYPE_UNUSED_ENTRY(209),
348 	HNS3_RX_PTYPE_UNUSED_ENTRY(210),
349 	HNS3_RX_PTYPE_UNUSED_ENTRY(211),
350 	HNS3_RX_PTYPE_UNUSED_ENTRY(212),
351 	HNS3_RX_PTYPE_UNUSED_ENTRY(213),
352 	HNS3_RX_PTYPE_UNUSED_ENTRY(214),
353 	HNS3_RX_PTYPE_UNUSED_ENTRY(215),
354 	HNS3_RX_PTYPE_UNUSED_ENTRY(216),
355 	HNS3_RX_PTYPE_UNUSED_ENTRY(217),
356 	HNS3_RX_PTYPE_UNUSED_ENTRY(218),
357 	HNS3_RX_PTYPE_UNUSED_ENTRY(219),
358 	HNS3_RX_PTYPE_UNUSED_ENTRY(220),
359 	HNS3_RX_PTYPE_UNUSED_ENTRY(221),
360 	HNS3_RX_PTYPE_UNUSED_ENTRY(222),
361 	HNS3_RX_PTYPE_UNUSED_ENTRY(223),
362 	HNS3_RX_PTYPE_UNUSED_ENTRY(224),
363 	HNS3_RX_PTYPE_UNUSED_ENTRY(225),
364 	HNS3_RX_PTYPE_UNUSED_ENTRY(226),
365 	HNS3_RX_PTYPE_UNUSED_ENTRY(227),
366 	HNS3_RX_PTYPE_UNUSED_ENTRY(228),
367 	HNS3_RX_PTYPE_UNUSED_ENTRY(229),
368 	HNS3_RX_PTYPE_UNUSED_ENTRY(230),
369 	HNS3_RX_PTYPE_UNUSED_ENTRY(231),
370 	HNS3_RX_PTYPE_UNUSED_ENTRY(232),
371 	HNS3_RX_PTYPE_UNUSED_ENTRY(233),
372 	HNS3_RX_PTYPE_UNUSED_ENTRY(234),
373 	HNS3_RX_PTYPE_UNUSED_ENTRY(235),
374 	HNS3_RX_PTYPE_UNUSED_ENTRY(236),
375 	HNS3_RX_PTYPE_UNUSED_ENTRY(237),
376 	HNS3_RX_PTYPE_UNUSED_ENTRY(238),
377 	HNS3_RX_PTYPE_UNUSED_ENTRY(239),
378 	HNS3_RX_PTYPE_UNUSED_ENTRY(240),
379 	HNS3_RX_PTYPE_UNUSED_ENTRY(241),
380 	HNS3_RX_PTYPE_UNUSED_ENTRY(242),
381 	HNS3_RX_PTYPE_UNUSED_ENTRY(243),
382 	HNS3_RX_PTYPE_UNUSED_ENTRY(244),
383 	HNS3_RX_PTYPE_UNUSED_ENTRY(245),
384 	HNS3_RX_PTYPE_UNUSED_ENTRY(246),
385 	HNS3_RX_PTYPE_UNUSED_ENTRY(247),
386 	HNS3_RX_PTYPE_UNUSED_ENTRY(248),
387 	HNS3_RX_PTYPE_UNUSED_ENTRY(249),
388 	HNS3_RX_PTYPE_UNUSED_ENTRY(250),
389 	HNS3_RX_PTYPE_UNUSED_ENTRY(251),
390 	HNS3_RX_PTYPE_UNUSED_ENTRY(252),
391 	HNS3_RX_PTYPE_UNUSED_ENTRY(253),
392 	HNS3_RX_PTYPE_UNUSED_ENTRY(254),
393 	HNS3_RX_PTYPE_UNUSED_ENTRY(255),
394 };
395 
396 #define HNS3_INVALID_PTYPE \
397 		ARRAY_SIZE(hns3_rx_ptype_tbl)
398 
399 static irqreturn_t hns3_irq_handle(int irq, void *vector)
400 {
401 	struct hns3_enet_tqp_vector *tqp_vector = vector;
402 
403 	napi_schedule_irqoff(&tqp_vector->napi);
404 	tqp_vector->event_cnt++;
405 
406 	return IRQ_HANDLED;
407 }
408 
409 static void hns3_nic_uninit_irq(struct hns3_nic_priv *priv)
410 {
411 	struct hns3_enet_tqp_vector *tqp_vectors;
412 	unsigned int i;
413 
414 	for (i = 0; i < priv->vector_num; i++) {
415 		tqp_vectors = &priv->tqp_vector[i];
416 
417 		if (tqp_vectors->irq_init_flag != HNS3_VECTOR_INITED)
418 			continue;
419 
420 		/* clear the affinity mask */
421 		irq_set_affinity_hint(tqp_vectors->vector_irq, NULL);
422 
423 		/* release the irq resource */
424 		free_irq(tqp_vectors->vector_irq, tqp_vectors);
425 		tqp_vectors->irq_init_flag = HNS3_VECTOR_NOT_INITED;
426 	}
427 }
428 
429 static int hns3_nic_init_irq(struct hns3_nic_priv *priv)
430 {
431 	struct hns3_enet_tqp_vector *tqp_vectors;
432 	int txrx_int_idx = 0;
433 	int rx_int_idx = 0;
434 	int tx_int_idx = 0;
435 	unsigned int i;
436 	int ret;
437 
438 	for (i = 0; i < priv->vector_num; i++) {
439 		tqp_vectors = &priv->tqp_vector[i];
440 
441 		if (tqp_vectors->irq_init_flag == HNS3_VECTOR_INITED)
442 			continue;
443 
444 		if (tqp_vectors->tx_group.ring && tqp_vectors->rx_group.ring) {
445 			snprintf(tqp_vectors->name, HNAE3_INT_NAME_LEN,
446 				 "%s-%s-%s-%d", hns3_driver_name,
447 				 pci_name(priv->ae_handle->pdev),
448 				 "TxRx", txrx_int_idx++);
449 			txrx_int_idx++;
450 		} else if (tqp_vectors->rx_group.ring) {
451 			snprintf(tqp_vectors->name, HNAE3_INT_NAME_LEN,
452 				 "%s-%s-%s-%d", hns3_driver_name,
453 				 pci_name(priv->ae_handle->pdev),
454 				 "Rx", rx_int_idx++);
455 		} else if (tqp_vectors->tx_group.ring) {
456 			snprintf(tqp_vectors->name, HNAE3_INT_NAME_LEN,
457 				 "%s-%s-%s-%d", hns3_driver_name,
458 				 pci_name(priv->ae_handle->pdev),
459 				 "Tx", tx_int_idx++);
460 		} else {
461 			/* Skip this unused q_vector */
462 			continue;
463 		}
464 
465 		tqp_vectors->name[HNAE3_INT_NAME_LEN - 1] = '\0';
466 
467 		irq_set_status_flags(tqp_vectors->vector_irq, IRQ_NOAUTOEN);
468 		ret = request_irq(tqp_vectors->vector_irq, hns3_irq_handle, 0,
469 				  tqp_vectors->name, tqp_vectors);
470 		if (ret) {
471 			netdev_err(priv->netdev, "request irq(%d) fail\n",
472 				   tqp_vectors->vector_irq);
473 			hns3_nic_uninit_irq(priv);
474 			return ret;
475 		}
476 
477 		irq_set_affinity_hint(tqp_vectors->vector_irq,
478 				      &tqp_vectors->affinity_mask);
479 
480 		tqp_vectors->irq_init_flag = HNS3_VECTOR_INITED;
481 	}
482 
483 	return 0;
484 }
485 
486 static void hns3_mask_vector_irq(struct hns3_enet_tqp_vector *tqp_vector,
487 				 u32 mask_en)
488 {
489 	writel(mask_en, tqp_vector->mask_addr);
490 }
491 
492 static void hns3_irq_enable(struct hns3_enet_tqp_vector *tqp_vector)
493 {
494 	napi_enable(&tqp_vector->napi);
495 	enable_irq(tqp_vector->vector_irq);
496 }
497 
498 static void hns3_irq_disable(struct hns3_enet_tqp_vector *tqp_vector)
499 {
500 	disable_irq(tqp_vector->vector_irq);
501 	napi_disable(&tqp_vector->napi);
502 	cancel_work_sync(&tqp_vector->rx_group.dim.work);
503 	cancel_work_sync(&tqp_vector->tx_group.dim.work);
504 }
505 
506 void hns3_set_vector_coalesce_rl(struct hns3_enet_tqp_vector *tqp_vector,
507 				 u32 rl_value)
508 {
509 	u32 rl_reg = hns3_rl_usec_to_reg(rl_value);
510 
511 	/* this defines the configuration for RL (Interrupt Rate Limiter).
512 	 * Rl defines rate of interrupts i.e. number of interrupts-per-second
513 	 * GL and RL(Rate Limiter) are 2 ways to acheive interrupt coalescing
514 	 */
515 	if (rl_reg > 0 && !tqp_vector->tx_group.coal.adapt_enable &&
516 	    !tqp_vector->rx_group.coal.adapt_enable)
517 		/* According to the hardware, the range of rl_reg is
518 		 * 0-59 and the unit is 4.
519 		 */
520 		rl_reg |=  HNS3_INT_RL_ENABLE_MASK;
521 
522 	writel(rl_reg, tqp_vector->mask_addr + HNS3_VECTOR_RL_OFFSET);
523 }
524 
525 void hns3_set_vector_coalesce_rx_gl(struct hns3_enet_tqp_vector *tqp_vector,
526 				    u32 gl_value)
527 {
528 	u32 new_val;
529 
530 	if (tqp_vector->rx_group.coal.unit_1us)
531 		new_val = gl_value | HNS3_INT_GL_1US;
532 	else
533 		new_val = hns3_gl_usec_to_reg(gl_value);
534 
535 	writel(new_val, tqp_vector->mask_addr + HNS3_VECTOR_GL0_OFFSET);
536 }
537 
538 void hns3_set_vector_coalesce_tx_gl(struct hns3_enet_tqp_vector *tqp_vector,
539 				    u32 gl_value)
540 {
541 	u32 new_val;
542 
543 	if (tqp_vector->tx_group.coal.unit_1us)
544 		new_val = gl_value | HNS3_INT_GL_1US;
545 	else
546 		new_val = hns3_gl_usec_to_reg(gl_value);
547 
548 	writel(new_val, tqp_vector->mask_addr + HNS3_VECTOR_GL1_OFFSET);
549 }
550 
551 void hns3_set_vector_coalesce_tx_ql(struct hns3_enet_tqp_vector *tqp_vector,
552 				    u32 ql_value)
553 {
554 	writel(ql_value, tqp_vector->mask_addr + HNS3_VECTOR_TX_QL_OFFSET);
555 }
556 
557 void hns3_set_vector_coalesce_rx_ql(struct hns3_enet_tqp_vector *tqp_vector,
558 				    u32 ql_value)
559 {
560 	writel(ql_value, tqp_vector->mask_addr + HNS3_VECTOR_RX_QL_OFFSET);
561 }
562 
563 static void hns3_vector_coalesce_init(struct hns3_enet_tqp_vector *tqp_vector,
564 				      struct hns3_nic_priv *priv)
565 {
566 	struct hns3_enet_coalesce *tx_coal = &tqp_vector->tx_group.coal;
567 	struct hns3_enet_coalesce *rx_coal = &tqp_vector->rx_group.coal;
568 	struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(priv->ae_handle);
569 	struct hns3_enet_coalesce *ptx_coal = &priv->tx_coal;
570 	struct hns3_enet_coalesce *prx_coal = &priv->rx_coal;
571 
572 	tx_coal->adapt_enable = ptx_coal->adapt_enable;
573 	rx_coal->adapt_enable = prx_coal->adapt_enable;
574 
575 	tx_coal->int_gl = ptx_coal->int_gl;
576 	rx_coal->int_gl = prx_coal->int_gl;
577 
578 	rx_coal->flow_level = prx_coal->flow_level;
579 	tx_coal->flow_level = ptx_coal->flow_level;
580 
581 	/* device version above V3(include V3), GL can configure 1us
582 	 * unit, so uses 1us unit.
583 	 */
584 	if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3) {
585 		tx_coal->unit_1us = 1;
586 		rx_coal->unit_1us = 1;
587 	}
588 
589 	if (ae_dev->dev_specs.int_ql_max) {
590 		tx_coal->ql_enable = 1;
591 		rx_coal->ql_enable = 1;
592 		tx_coal->int_ql_max = ae_dev->dev_specs.int_ql_max;
593 		rx_coal->int_ql_max = ae_dev->dev_specs.int_ql_max;
594 		tx_coal->int_ql = ptx_coal->int_ql;
595 		rx_coal->int_ql = prx_coal->int_ql;
596 	}
597 }
598 
599 static void
600 hns3_vector_coalesce_init_hw(struct hns3_enet_tqp_vector *tqp_vector,
601 			     struct hns3_nic_priv *priv)
602 {
603 	struct hns3_enet_coalesce *tx_coal = &tqp_vector->tx_group.coal;
604 	struct hns3_enet_coalesce *rx_coal = &tqp_vector->rx_group.coal;
605 	struct hnae3_handle *h = priv->ae_handle;
606 
607 	hns3_set_vector_coalesce_tx_gl(tqp_vector, tx_coal->int_gl);
608 	hns3_set_vector_coalesce_rx_gl(tqp_vector, rx_coal->int_gl);
609 	hns3_set_vector_coalesce_rl(tqp_vector, h->kinfo.int_rl_setting);
610 
611 	if (tx_coal->ql_enable)
612 		hns3_set_vector_coalesce_tx_ql(tqp_vector, tx_coal->int_ql);
613 
614 	if (rx_coal->ql_enable)
615 		hns3_set_vector_coalesce_rx_ql(tqp_vector, rx_coal->int_ql);
616 }
617 
618 static int hns3_nic_set_real_num_queue(struct net_device *netdev)
619 {
620 	struct hnae3_handle *h = hns3_get_handle(netdev);
621 	struct hnae3_knic_private_info *kinfo = &h->kinfo;
622 	struct hnae3_tc_info *tc_info = &kinfo->tc_info;
623 	unsigned int queue_size = kinfo->num_tqps;
624 	int i, ret;
625 
626 	if (tc_info->num_tc <= 1 && !tc_info->mqprio_active) {
627 		netdev_reset_tc(netdev);
628 	} else {
629 		ret = netdev_set_num_tc(netdev, tc_info->num_tc);
630 		if (ret) {
631 			netdev_err(netdev,
632 				   "netdev_set_num_tc fail, ret=%d!\n", ret);
633 			return ret;
634 		}
635 
636 		for (i = 0; i < tc_info->num_tc; i++)
637 			netdev_set_tc_queue(netdev, i, tc_info->tqp_count[i],
638 					    tc_info->tqp_offset[i]);
639 	}
640 
641 	ret = netif_set_real_num_tx_queues(netdev, queue_size);
642 	if (ret) {
643 		netdev_err(netdev,
644 			   "netif_set_real_num_tx_queues fail, ret=%d!\n", ret);
645 		return ret;
646 	}
647 
648 	ret = netif_set_real_num_rx_queues(netdev, queue_size);
649 	if (ret) {
650 		netdev_err(netdev,
651 			   "netif_set_real_num_rx_queues fail, ret=%d!\n", ret);
652 		return ret;
653 	}
654 
655 	return 0;
656 }
657 
658 u16 hns3_get_max_available_channels(struct hnae3_handle *h)
659 {
660 	u16 alloc_tqps, max_rss_size, rss_size;
661 
662 	h->ae_algo->ops->get_tqps_and_rss_info(h, &alloc_tqps, &max_rss_size);
663 	rss_size = alloc_tqps / h->kinfo.tc_info.num_tc;
664 
665 	return min_t(u16, rss_size, max_rss_size);
666 }
667 
668 static void hns3_tqp_enable(struct hnae3_queue *tqp)
669 {
670 	u32 rcb_reg;
671 
672 	rcb_reg = hns3_read_dev(tqp, HNS3_RING_EN_REG);
673 	rcb_reg |= BIT(HNS3_RING_EN_B);
674 	hns3_write_dev(tqp, HNS3_RING_EN_REG, rcb_reg);
675 }
676 
677 static void hns3_tqp_disable(struct hnae3_queue *tqp)
678 {
679 	u32 rcb_reg;
680 
681 	rcb_reg = hns3_read_dev(tqp, HNS3_RING_EN_REG);
682 	rcb_reg &= ~BIT(HNS3_RING_EN_B);
683 	hns3_write_dev(tqp, HNS3_RING_EN_REG, rcb_reg);
684 }
685 
686 static void hns3_free_rx_cpu_rmap(struct net_device *netdev)
687 {
688 #ifdef CONFIG_RFS_ACCEL
689 	free_irq_cpu_rmap(netdev->rx_cpu_rmap);
690 	netdev->rx_cpu_rmap = NULL;
691 #endif
692 }
693 
694 static int hns3_set_rx_cpu_rmap(struct net_device *netdev)
695 {
696 #ifdef CONFIG_RFS_ACCEL
697 	struct hns3_nic_priv *priv = netdev_priv(netdev);
698 	struct hns3_enet_tqp_vector *tqp_vector;
699 	int i, ret;
700 
701 	if (!netdev->rx_cpu_rmap) {
702 		netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(priv->vector_num);
703 		if (!netdev->rx_cpu_rmap)
704 			return -ENOMEM;
705 	}
706 
707 	for (i = 0; i < priv->vector_num; i++) {
708 		tqp_vector = &priv->tqp_vector[i];
709 		ret = irq_cpu_rmap_add(netdev->rx_cpu_rmap,
710 				       tqp_vector->vector_irq);
711 		if (ret) {
712 			hns3_free_rx_cpu_rmap(netdev);
713 			return ret;
714 		}
715 	}
716 #endif
717 	return 0;
718 }
719 
720 static void hns3_enable_irqs_and_tqps(struct net_device *netdev)
721 {
722 	struct hns3_nic_priv *priv = netdev_priv(netdev);
723 	struct hnae3_handle *h = priv->ae_handle;
724 	u16 i;
725 
726 	for (i = 0; i < priv->vector_num; i++)
727 		hns3_irq_enable(&priv->tqp_vector[i]);
728 
729 	for (i = 0; i < priv->vector_num; i++)
730 		hns3_mask_vector_irq(&priv->tqp_vector[i], 1);
731 
732 	for (i = 0; i < h->kinfo.num_tqps; i++)
733 		hns3_tqp_enable(h->kinfo.tqp[i]);
734 }
735 
736 static void hns3_disable_irqs_and_tqps(struct net_device *netdev)
737 {
738 	struct hns3_nic_priv *priv = netdev_priv(netdev);
739 	struct hnae3_handle *h = priv->ae_handle;
740 	u16 i;
741 
742 	for (i = 0; i < h->kinfo.num_tqps; i++)
743 		hns3_tqp_disable(h->kinfo.tqp[i]);
744 
745 	for (i = 0; i < priv->vector_num; i++)
746 		hns3_mask_vector_irq(&priv->tqp_vector[i], 0);
747 
748 	for (i = 0; i < priv->vector_num; i++)
749 		hns3_irq_disable(&priv->tqp_vector[i]);
750 }
751 
752 static int hns3_nic_net_up(struct net_device *netdev)
753 {
754 	struct hns3_nic_priv *priv = netdev_priv(netdev);
755 	struct hnae3_handle *h = priv->ae_handle;
756 	int ret;
757 
758 	ret = hns3_nic_reset_all_ring(h);
759 	if (ret)
760 		return ret;
761 
762 	clear_bit(HNS3_NIC_STATE_DOWN, &priv->state);
763 
764 	hns3_enable_irqs_and_tqps(netdev);
765 
766 	/* start the ae_dev */
767 	ret = h->ae_algo->ops->start ? h->ae_algo->ops->start(h) : 0;
768 	if (ret) {
769 		set_bit(HNS3_NIC_STATE_DOWN, &priv->state);
770 		hns3_disable_irqs_and_tqps(netdev);
771 	}
772 
773 	return ret;
774 }
775 
776 static void hns3_config_xps(struct hns3_nic_priv *priv)
777 {
778 	int i;
779 
780 	for (i = 0; i < priv->vector_num; i++) {
781 		struct hns3_enet_tqp_vector *tqp_vector = &priv->tqp_vector[i];
782 		struct hns3_enet_ring *ring = tqp_vector->tx_group.ring;
783 
784 		while (ring) {
785 			int ret;
786 
787 			ret = netif_set_xps_queue(priv->netdev,
788 						  &tqp_vector->affinity_mask,
789 						  ring->tqp->tqp_index);
790 			if (ret)
791 				netdev_warn(priv->netdev,
792 					    "set xps queue failed: %d", ret);
793 
794 			ring = ring->next;
795 		}
796 	}
797 }
798 
799 static int hns3_nic_net_open(struct net_device *netdev)
800 {
801 	struct hns3_nic_priv *priv = netdev_priv(netdev);
802 	struct hnae3_handle *h = hns3_get_handle(netdev);
803 	struct hnae3_knic_private_info *kinfo;
804 	int i, ret;
805 
806 	if (hns3_nic_resetting(netdev))
807 		return -EBUSY;
808 
809 	if (!test_bit(HNS3_NIC_STATE_DOWN, &priv->state)) {
810 		netdev_warn(netdev, "net open repeatedly!\n");
811 		return 0;
812 	}
813 
814 	netif_carrier_off(netdev);
815 
816 	ret = hns3_nic_set_real_num_queue(netdev);
817 	if (ret)
818 		return ret;
819 
820 	ret = hns3_nic_net_up(netdev);
821 	if (ret) {
822 		netdev_err(netdev, "net up fail, ret=%d!\n", ret);
823 		return ret;
824 	}
825 
826 	kinfo = &h->kinfo;
827 	for (i = 0; i < HNAE3_MAX_USER_PRIO; i++)
828 		netdev_set_prio_tc_map(netdev, i, kinfo->tc_info.prio_tc[i]);
829 
830 	if (h->ae_algo->ops->set_timer_task)
831 		h->ae_algo->ops->set_timer_task(priv->ae_handle, true);
832 
833 	hns3_config_xps(priv);
834 
835 	netif_dbg(h, drv, netdev, "net open\n");
836 
837 	return 0;
838 }
839 
840 static void hns3_reset_tx_queue(struct hnae3_handle *h)
841 {
842 	struct net_device *ndev = h->kinfo.netdev;
843 	struct hns3_nic_priv *priv = netdev_priv(ndev);
844 	struct netdev_queue *dev_queue;
845 	u32 i;
846 
847 	for (i = 0; i < h->kinfo.num_tqps; i++) {
848 		dev_queue = netdev_get_tx_queue(ndev,
849 						priv->ring[i].queue_index);
850 		netdev_tx_reset_queue(dev_queue);
851 	}
852 }
853 
854 static void hns3_nic_net_down(struct net_device *netdev)
855 {
856 	struct hns3_nic_priv *priv = netdev_priv(netdev);
857 	const struct hnae3_ae_ops *ops;
858 
859 	hns3_disable_irqs_and_tqps(netdev);
860 
861 	/* stop ae_dev */
862 	ops = priv->ae_handle->ae_algo->ops;
863 	if (ops->stop)
864 		ops->stop(priv->ae_handle);
865 
866 	/* delay ring buffer clearing to hns3_reset_notify_uninit_enet
867 	 * during reset process, because driver may not be able
868 	 * to disable the ring through firmware when downing the netdev.
869 	 */
870 	if (!hns3_nic_resetting(netdev))
871 		hns3_clear_all_ring(priv->ae_handle, false);
872 
873 	hns3_reset_tx_queue(priv->ae_handle);
874 }
875 
876 static int hns3_nic_net_stop(struct net_device *netdev)
877 {
878 	struct hns3_nic_priv *priv = netdev_priv(netdev);
879 	struct hnae3_handle *h = hns3_get_handle(netdev);
880 
881 	if (test_and_set_bit(HNS3_NIC_STATE_DOWN, &priv->state))
882 		return 0;
883 
884 	netif_dbg(h, drv, netdev, "net stop\n");
885 
886 	if (h->ae_algo->ops->set_timer_task)
887 		h->ae_algo->ops->set_timer_task(priv->ae_handle, false);
888 
889 	netif_carrier_off(netdev);
890 	netif_tx_disable(netdev);
891 
892 	hns3_nic_net_down(netdev);
893 
894 	return 0;
895 }
896 
897 static int hns3_nic_uc_sync(struct net_device *netdev,
898 			    const unsigned char *addr)
899 {
900 	struct hnae3_handle *h = hns3_get_handle(netdev);
901 
902 	if (h->ae_algo->ops->add_uc_addr)
903 		return h->ae_algo->ops->add_uc_addr(h, addr);
904 
905 	return 0;
906 }
907 
908 static int hns3_nic_uc_unsync(struct net_device *netdev,
909 			      const unsigned char *addr)
910 {
911 	struct hnae3_handle *h = hns3_get_handle(netdev);
912 
913 	/* need ignore the request of removing device address, because
914 	 * we store the device address and other addresses of uc list
915 	 * in the function's mac filter list.
916 	 */
917 	if (ether_addr_equal(addr, netdev->dev_addr))
918 		return 0;
919 
920 	if (h->ae_algo->ops->rm_uc_addr)
921 		return h->ae_algo->ops->rm_uc_addr(h, addr);
922 
923 	return 0;
924 }
925 
926 static int hns3_nic_mc_sync(struct net_device *netdev,
927 			    const unsigned char *addr)
928 {
929 	struct hnae3_handle *h = hns3_get_handle(netdev);
930 
931 	if (h->ae_algo->ops->add_mc_addr)
932 		return h->ae_algo->ops->add_mc_addr(h, addr);
933 
934 	return 0;
935 }
936 
937 static int hns3_nic_mc_unsync(struct net_device *netdev,
938 			      const unsigned char *addr)
939 {
940 	struct hnae3_handle *h = hns3_get_handle(netdev);
941 
942 	if (h->ae_algo->ops->rm_mc_addr)
943 		return h->ae_algo->ops->rm_mc_addr(h, addr);
944 
945 	return 0;
946 }
947 
948 static u8 hns3_get_netdev_flags(struct net_device *netdev)
949 {
950 	u8 flags = 0;
951 
952 	if (netdev->flags & IFF_PROMISC)
953 		flags = HNAE3_USER_UPE | HNAE3_USER_MPE | HNAE3_BPE;
954 	else if (netdev->flags & IFF_ALLMULTI)
955 		flags = HNAE3_USER_MPE;
956 
957 	return flags;
958 }
959 
960 static void hns3_nic_set_rx_mode(struct net_device *netdev)
961 {
962 	struct hnae3_handle *h = hns3_get_handle(netdev);
963 	u8 new_flags;
964 
965 	new_flags = hns3_get_netdev_flags(netdev);
966 
967 	__dev_uc_sync(netdev, hns3_nic_uc_sync, hns3_nic_uc_unsync);
968 	__dev_mc_sync(netdev, hns3_nic_mc_sync, hns3_nic_mc_unsync);
969 
970 	/* User mode Promisc mode enable and vlan filtering is disabled to
971 	 * let all packets in.
972 	 */
973 	h->netdev_flags = new_flags;
974 	hns3_request_update_promisc_mode(h);
975 }
976 
977 void hns3_request_update_promisc_mode(struct hnae3_handle *handle)
978 {
979 	const struct hnae3_ae_ops *ops = hns3_get_ops(handle);
980 
981 	if (ops->request_update_promisc_mode)
982 		ops->request_update_promisc_mode(handle);
983 }
984 
985 static u32 hns3_tx_spare_space(struct hns3_enet_ring *ring)
986 {
987 	struct hns3_tx_spare *tx_spare = ring->tx_spare;
988 	u32 ntc, ntu;
989 
990 	/* This smp_load_acquire() pairs with smp_store_release() in
991 	 * hns3_tx_spare_update() called in tx desc cleaning process.
992 	 */
993 	ntc = smp_load_acquire(&tx_spare->last_to_clean);
994 	ntu = tx_spare->next_to_use;
995 
996 	if (ntc > ntu)
997 		return ntc - ntu - 1;
998 
999 	/* The free tx buffer is divided into two part, so pick the
1000 	 * larger one.
1001 	 */
1002 	return max(ntc, tx_spare->len - ntu) - 1;
1003 }
1004 
1005 static void hns3_tx_spare_update(struct hns3_enet_ring *ring)
1006 {
1007 	struct hns3_tx_spare *tx_spare = ring->tx_spare;
1008 
1009 	if (!tx_spare ||
1010 	    tx_spare->last_to_clean == tx_spare->next_to_clean)
1011 		return;
1012 
1013 	/* This smp_store_release() pairs with smp_load_acquire() in
1014 	 * hns3_tx_spare_space() called in xmit process.
1015 	 */
1016 	smp_store_release(&tx_spare->last_to_clean,
1017 			  tx_spare->next_to_clean);
1018 }
1019 
1020 static bool hns3_can_use_tx_bounce(struct hns3_enet_ring *ring,
1021 				   struct sk_buff *skb,
1022 				   u32 space)
1023 {
1024 	u32 len = skb->len <= ring->tx_copybreak ? skb->len :
1025 				skb_headlen(skb);
1026 
1027 	if (len > ring->tx_copybreak)
1028 		return false;
1029 
1030 	if (ALIGN(len, dma_get_cache_alignment()) > space) {
1031 		hns3_ring_stats_update(ring, tx_spare_full);
1032 		return false;
1033 	}
1034 
1035 	return true;
1036 }
1037 
1038 static bool hns3_can_use_tx_sgl(struct hns3_enet_ring *ring,
1039 				struct sk_buff *skb,
1040 				u32 space)
1041 {
1042 	if (skb->len <= ring->tx_copybreak || !tx_sgl ||
1043 	    (!skb_has_frag_list(skb) &&
1044 	     skb_shinfo(skb)->nr_frags < tx_sgl))
1045 		return false;
1046 
1047 	if (space < HNS3_MAX_SGL_SIZE) {
1048 		hns3_ring_stats_update(ring, tx_spare_full);
1049 		return false;
1050 	}
1051 
1052 	return true;
1053 }
1054 
1055 static void hns3_init_tx_spare_buffer(struct hns3_enet_ring *ring)
1056 {
1057 	u32 alloc_size = ring->tqp->handle->kinfo.tx_spare_buf_size;
1058 	struct net_device *netdev = ring_to_netdev(ring);
1059 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1060 	struct hns3_tx_spare *tx_spare;
1061 	struct page *page;
1062 	dma_addr_t dma;
1063 	int order;
1064 
1065 	if (!alloc_size)
1066 		goto not_init;
1067 
1068 	order = get_order(alloc_size);
1069 	if (order > MAX_PAGE_ORDER) {
1070 		if (net_ratelimit())
1071 			dev_warn(ring_to_dev(ring), "failed to allocate tx spare buffer, exceed to max order\n");
1072 		goto not_init;
1073 	}
1074 
1075 	tx_spare = devm_kzalloc_obj(ring_to_dev(ring), *tx_spare);
1076 	if (!tx_spare) {
1077 		/* The driver still work without the tx spare buffer */
1078 		dev_warn(ring_to_dev(ring), "failed to allocate hns3_tx_spare\n");
1079 		goto devm_kzalloc_error;
1080 	}
1081 
1082 	page = alloc_pages_node(dev_to_node(ring_to_dev(ring)),
1083 				GFP_KERNEL, order);
1084 	if (!page) {
1085 		dev_warn(ring_to_dev(ring), "failed to allocate tx spare pages\n");
1086 		goto alloc_pages_error;
1087 	}
1088 
1089 	dma = dma_map_page(ring_to_dev(ring), page, 0,
1090 			   PAGE_SIZE << order, DMA_TO_DEVICE);
1091 	if (dma_mapping_error(ring_to_dev(ring), dma)) {
1092 		dev_warn(ring_to_dev(ring), "failed to map pages for tx spare\n");
1093 		goto dma_mapping_error;
1094 	}
1095 
1096 	tx_spare->dma = dma;
1097 	tx_spare->buf = page_address(page);
1098 	tx_spare->len = PAGE_SIZE << order;
1099 	ring->tx_spare = tx_spare;
1100 	ring->tx_copybreak = priv->tx_copybreak;
1101 	return;
1102 
1103 dma_mapping_error:
1104 	put_page(page);
1105 alloc_pages_error:
1106 	devm_kfree(ring_to_dev(ring), tx_spare);
1107 devm_kzalloc_error:
1108 	ring->tqp->handle->kinfo.tx_spare_buf_size = 0;
1109 not_init:
1110 	/* When driver init or reset_init, the ring->tx_spare is always NULL;
1111 	 * but when called from hns3_set_ringparam, it's usually not NULL, and
1112 	 * will be restored if hns3_init_all_ring() failed. So it's safe to set
1113 	 * ring->tx_spare to NULL here.
1114 	 */
1115 	ring->tx_spare = NULL;
1116 }
1117 
1118 /* Use hns3_tx_spare_space() to make sure there is enough buffer
1119  * before calling below function to allocate tx buffer.
1120  */
1121 static void *hns3_tx_spare_alloc(struct hns3_enet_ring *ring,
1122 				 unsigned int size, dma_addr_t *dma,
1123 				 u32 *cb_len)
1124 {
1125 	struct hns3_tx_spare *tx_spare = ring->tx_spare;
1126 	u32 ntu = tx_spare->next_to_use;
1127 
1128 	size = ALIGN(size, dma_get_cache_alignment());
1129 	*cb_len = size;
1130 
1131 	/* Tx spare buffer wraps back here because the end of
1132 	 * freed tx buffer is not enough.
1133 	 */
1134 	if (ntu + size > tx_spare->len) {
1135 		*cb_len += (tx_spare->len - ntu);
1136 		ntu = 0;
1137 	}
1138 
1139 	tx_spare->next_to_use = ntu + size;
1140 	if (tx_spare->next_to_use == tx_spare->len)
1141 		tx_spare->next_to_use = 0;
1142 
1143 	*dma = tx_spare->dma + ntu;
1144 
1145 	return tx_spare->buf + ntu;
1146 }
1147 
1148 static void hns3_tx_spare_rollback(struct hns3_enet_ring *ring, u32 len)
1149 {
1150 	struct hns3_tx_spare *tx_spare = ring->tx_spare;
1151 
1152 	if (len > tx_spare->next_to_use) {
1153 		len -= tx_spare->next_to_use;
1154 		tx_spare->next_to_use = tx_spare->len - len;
1155 	} else {
1156 		tx_spare->next_to_use -= len;
1157 	}
1158 }
1159 
1160 static void hns3_tx_spare_reclaim_cb(struct hns3_enet_ring *ring,
1161 				     struct hns3_desc_cb *cb)
1162 {
1163 	struct hns3_tx_spare *tx_spare = ring->tx_spare;
1164 	u32 ntc = tx_spare->next_to_clean;
1165 	u32 len = cb->length;
1166 
1167 	tx_spare->next_to_clean += len;
1168 
1169 	if (tx_spare->next_to_clean >= tx_spare->len) {
1170 		tx_spare->next_to_clean -= tx_spare->len;
1171 
1172 		if (tx_spare->next_to_clean) {
1173 			ntc = 0;
1174 			len = tx_spare->next_to_clean;
1175 		}
1176 	}
1177 
1178 	/* This tx spare buffer is only really reclaimed after calling
1179 	 * hns3_tx_spare_update(), so it is still safe to use the info in
1180 	 * the tx buffer to do the dma sync or sg unmapping after
1181 	 * tx_spare->next_to_clean is moved forword.
1182 	 */
1183 	if (cb->type & (DESC_TYPE_BOUNCE_HEAD | DESC_TYPE_BOUNCE_ALL)) {
1184 		dma_addr_t dma = tx_spare->dma + ntc;
1185 
1186 		dma_sync_single_for_cpu(ring_to_dev(ring), dma, len,
1187 					DMA_TO_DEVICE);
1188 	} else {
1189 		struct sg_table *sgt = tx_spare->buf + ntc;
1190 
1191 		dma_unmap_sg(ring_to_dev(ring), sgt->sgl, sgt->orig_nents,
1192 			     DMA_TO_DEVICE);
1193 	}
1194 }
1195 
1196 static int hns3_set_tso(struct sk_buff *skb, u32 *paylen_fdop_ol4cs,
1197 			u16 *mss, u32 *type_cs_vlan_tso, u32 *send_bytes)
1198 {
1199 	u32 l4_offset, hdr_len;
1200 	union l3_hdr_info l3;
1201 	union l4_hdr_info l4;
1202 	u32 l4_paylen;
1203 	int ret;
1204 
1205 	if (!skb_is_gso(skb))
1206 		return 0;
1207 
1208 	ret = skb_cow_head(skb, 0);
1209 	if (unlikely(ret < 0))
1210 		return ret;
1211 
1212 	l3.hdr = skb_network_header(skb);
1213 	l4.hdr = skb_transport_header(skb);
1214 
1215 	/* Software should clear the IPv4's checksum field when tso is
1216 	 * needed.
1217 	 */
1218 	if (l3.v4->version == 4)
1219 		l3.v4->check = 0;
1220 
1221 	/* tunnel packet */
1222 	if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
1223 					 SKB_GSO_GRE_CSUM |
1224 					 SKB_GSO_UDP_TUNNEL |
1225 					 SKB_GSO_UDP_TUNNEL_CSUM)) {
1226 		/* reset l3&l4 pointers from outer to inner headers */
1227 		l3.hdr = skb_inner_network_header(skb);
1228 		l4.hdr = skb_inner_transport_header(skb);
1229 
1230 		/* Software should clear the IPv4's checksum field when
1231 		 * tso is needed.
1232 		 */
1233 		if (l3.v4->version == 4)
1234 			l3.v4->check = 0;
1235 	}
1236 
1237 	/* normal or tunnel packet */
1238 	l4_offset = l4.hdr - skb->data;
1239 
1240 	/* remove payload length from inner pseudo checksum when tso */
1241 	l4_paylen = skb->len - l4_offset;
1242 
1243 	if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
1244 		hdr_len = sizeof(*l4.udp) + l4_offset;
1245 		csum_replace_by_diff(&l4.udp->check,
1246 				     (__force __wsum)htonl(l4_paylen));
1247 	} else {
1248 		hdr_len = (l4.tcp->doff << 2) + l4_offset;
1249 		csum_replace_by_diff(&l4.tcp->check,
1250 				     (__force __wsum)htonl(l4_paylen));
1251 	}
1252 
1253 	*send_bytes = (skb_shinfo(skb)->gso_segs - 1) * hdr_len + skb->len;
1254 
1255 	/* find the txbd field values */
1256 	*paylen_fdop_ol4cs = skb->len - hdr_len;
1257 	hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_TSO_B, 1);
1258 
1259 	/* offload outer UDP header checksum */
1260 	if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)
1261 		hns3_set_field(*paylen_fdop_ol4cs, HNS3_TXD_OL4CS_B, 1);
1262 
1263 	/* get MSS for TSO */
1264 	*mss = skb_shinfo(skb)->gso_size;
1265 
1266 	trace_hns3_tso(skb);
1267 
1268 	return 0;
1269 }
1270 
1271 static int hns3_get_l4_protocol(struct sk_buff *skb, u8 *ol4_proto,
1272 				u8 *il4_proto)
1273 {
1274 	union l3_hdr_info l3;
1275 	unsigned char *l4_hdr;
1276 	unsigned char *exthdr;
1277 	u8 l4_proto_tmp;
1278 	__be16 frag_off;
1279 
1280 	/* find outer header point */
1281 	l3.hdr = skb_network_header(skb);
1282 	l4_hdr = skb_transport_header(skb);
1283 
1284 	if (skb->protocol == htons(ETH_P_IPV6)) {
1285 		exthdr = l3.hdr + sizeof(*l3.v6);
1286 		l4_proto_tmp = l3.v6->nexthdr;
1287 		if (l4_hdr != exthdr)
1288 			ipv6_skip_exthdr(skb, exthdr - skb->data,
1289 					 &l4_proto_tmp, &frag_off);
1290 	} else if (skb->protocol == htons(ETH_P_IP)) {
1291 		l4_proto_tmp = l3.v4->protocol;
1292 	} else {
1293 		return -EINVAL;
1294 	}
1295 
1296 	*ol4_proto = l4_proto_tmp;
1297 
1298 	/* tunnel packet */
1299 	if (!skb->encapsulation) {
1300 		*il4_proto = 0;
1301 		return 0;
1302 	}
1303 
1304 	/* find inner header point */
1305 	l3.hdr = skb_inner_network_header(skb);
1306 	l4_hdr = skb_inner_transport_header(skb);
1307 
1308 	if (l3.v6->version == 6) {
1309 		exthdr = l3.hdr + sizeof(*l3.v6);
1310 		l4_proto_tmp = l3.v6->nexthdr;
1311 		if (l4_hdr != exthdr)
1312 			ipv6_skip_exthdr(skb, exthdr - skb->data,
1313 					 &l4_proto_tmp, &frag_off);
1314 	} else if (l3.v4->version == 4) {
1315 		l4_proto_tmp = l3.v4->protocol;
1316 	}
1317 
1318 	*il4_proto = l4_proto_tmp;
1319 
1320 	return 0;
1321 }
1322 
1323 /* when skb->encapsulation is 0, skb->ip_summed is CHECKSUM_PARTIAL
1324  * and it is udp packet, which has a dest port as the IANA assigned.
1325  * the hardware is expected to do the checksum offload, but the
1326  * hardware will not do the checksum offload when udp dest port is
1327  * 4789, 4790 or 6081.
1328  */
1329 static bool hns3_tunnel_csum_bug(struct sk_buff *skb)
1330 {
1331 	struct hns3_nic_priv *priv = netdev_priv(skb->dev);
1332 	struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(priv->ae_handle);
1333 	union l4_hdr_info l4;
1334 
1335 	/* device version above V3(include V3), the hardware can
1336 	 * do this checksum offload.
1337 	 */
1338 	if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1339 		return false;
1340 
1341 	l4.hdr = skb_transport_header(skb);
1342 
1343 	if (!(!skb->encapsulation &&
1344 	      (l4.udp->dest == htons(IANA_VXLAN_UDP_PORT) ||
1345 	      l4.udp->dest == htons(GENEVE_UDP_PORT) ||
1346 	      l4.udp->dest == htons(IANA_VXLAN_GPE_UDP_PORT))))
1347 		return false;
1348 
1349 	return true;
1350 }
1351 
1352 static void hns3_set_outer_l2l3l4(struct sk_buff *skb, u8 ol4_proto,
1353 				  u32 *ol_type_vlan_len_msec)
1354 {
1355 	u32 l2_len, l3_len, l4_len;
1356 	unsigned char *il2_hdr;
1357 	union l3_hdr_info l3;
1358 	union l4_hdr_info l4;
1359 
1360 	l3.hdr = skb_network_header(skb);
1361 	l4.hdr = skb_transport_header(skb);
1362 
1363 	/* compute OL2 header size, defined in 2 Bytes */
1364 	l2_len = l3.hdr - skb->data;
1365 	hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_L2LEN_S, l2_len >> 1);
1366 
1367 	/* compute OL3 header size, defined in 4 Bytes */
1368 	l3_len = l4.hdr - l3.hdr;
1369 	hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_L3LEN_S, l3_len >> 2);
1370 
1371 	il2_hdr = skb_inner_mac_header(skb);
1372 	/* compute OL4 header size, defined in 4 Bytes */
1373 	l4_len = il2_hdr - l4.hdr;
1374 	hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_L4LEN_S, l4_len >> 2);
1375 
1376 	/* define outer network header type */
1377 	if (skb->protocol == htons(ETH_P_IP)) {
1378 		if (skb_is_gso(skb))
1379 			hns3_set_field(*ol_type_vlan_len_msec,
1380 				       HNS3_TXD_OL3T_S,
1381 				       HNS3_OL3T_IPV4_CSUM);
1382 		else
1383 			hns3_set_field(*ol_type_vlan_len_msec,
1384 				       HNS3_TXD_OL3T_S,
1385 				       HNS3_OL3T_IPV4_NO_CSUM);
1386 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
1387 		hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_OL3T_S,
1388 			       HNS3_OL3T_IPV6);
1389 	}
1390 
1391 	if (ol4_proto == IPPROTO_UDP)
1392 		hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_TUNTYPE_S,
1393 			       HNS3_TUN_MAC_IN_UDP);
1394 	else if (ol4_proto == IPPROTO_GRE)
1395 		hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_TUNTYPE_S,
1396 			       HNS3_TUN_NVGRE);
1397 }
1398 
1399 static void hns3_set_l3_type(struct sk_buff *skb, union l3_hdr_info l3,
1400 			     u32 *type_cs_vlan_tso)
1401 {
1402 	if (l3.v4->version == 4) {
1403 		hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L3T_S,
1404 			       HNS3_L3T_IPV4);
1405 
1406 		/* the stack computes the IP header already, the only time we
1407 		 * need the hardware to recompute it is in the case of TSO.
1408 		 */
1409 		if (skb_is_gso(skb))
1410 			hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L3CS_B, 1);
1411 	} else if (l3.v6->version == 6) {
1412 		hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L3T_S,
1413 			       HNS3_L3T_IPV6);
1414 	}
1415 }
1416 
1417 static int hns3_set_l4_csum_length(struct sk_buff *skb, union l4_hdr_info l4,
1418 				   u32 l4_proto, u32 *type_cs_vlan_tso)
1419 {
1420 	/* compute inner(/normal) L4 header size, defined in 4 Bytes */
1421 	switch (l4_proto) {
1422 	case IPPROTO_TCP:
1423 		hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4CS_B, 1);
1424 		hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4T_S,
1425 			       HNS3_L4T_TCP);
1426 		hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4LEN_S,
1427 			       l4.tcp->doff);
1428 		break;
1429 	case IPPROTO_UDP:
1430 		if (hns3_tunnel_csum_bug(skb)) {
1431 			int ret = skb_put_padto(skb, HNS3_MIN_TUN_PKT_LEN);
1432 
1433 			return ret ? ret : skb_checksum_help(skb);
1434 		}
1435 
1436 		hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4CS_B, 1);
1437 		hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4T_S,
1438 			       HNS3_L4T_UDP);
1439 		hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4LEN_S,
1440 			       (sizeof(struct udphdr) >> 2));
1441 		break;
1442 	case IPPROTO_SCTP:
1443 		hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4CS_B, 1);
1444 		hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4T_S,
1445 			       HNS3_L4T_SCTP);
1446 		hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4LEN_S,
1447 			       (sizeof(struct sctphdr) >> 2));
1448 		break;
1449 	default:
1450 		/* drop the skb tunnel packet if hardware don't support,
1451 		 * because hardware can't calculate csum when TSO.
1452 		 */
1453 		if (skb_is_gso(skb))
1454 			return -EDOM;
1455 
1456 		/* the stack computes the IP header already,
1457 		 * driver calculate l4 checksum when not TSO.
1458 		 */
1459 		return skb_checksum_help(skb);
1460 	}
1461 
1462 	return 0;
1463 }
1464 
1465 static int hns3_set_l2l3l4(struct sk_buff *skb, u8 ol4_proto,
1466 			   u8 il4_proto, u32 *type_cs_vlan_tso,
1467 			   u32 *ol_type_vlan_len_msec)
1468 {
1469 	unsigned char *l2_hdr = skb->data;
1470 	u32 l4_proto = ol4_proto;
1471 	union l4_hdr_info l4;
1472 	union l3_hdr_info l3;
1473 	u32 l2_len, l3_len;
1474 
1475 	l4.hdr = skb_transport_header(skb);
1476 	l3.hdr = skb_network_header(skb);
1477 
1478 	/* handle encapsulation skb */
1479 	if (skb->encapsulation) {
1480 		/* If this is a not UDP/GRE encapsulation skb */
1481 		if (!(ol4_proto == IPPROTO_UDP || ol4_proto == IPPROTO_GRE)) {
1482 			/* drop the skb tunnel packet if hardware don't support,
1483 			 * because hardware can't calculate csum when TSO.
1484 			 */
1485 			if (skb_is_gso(skb))
1486 				return -EDOM;
1487 
1488 			/* the stack computes the IP header already,
1489 			 * driver calculate l4 checksum when not TSO.
1490 			 */
1491 			return skb_checksum_help(skb);
1492 		}
1493 
1494 		hns3_set_outer_l2l3l4(skb, ol4_proto, ol_type_vlan_len_msec);
1495 
1496 		/* switch to inner header */
1497 		l2_hdr = skb_inner_mac_header(skb);
1498 		l3.hdr = skb_inner_network_header(skb);
1499 		l4.hdr = skb_inner_transport_header(skb);
1500 		l4_proto = il4_proto;
1501 	}
1502 
1503 	hns3_set_l3_type(skb, l3, type_cs_vlan_tso);
1504 
1505 	/* compute inner(/normal) L2 header size, defined in 2 Bytes */
1506 	l2_len = l3.hdr - l2_hdr;
1507 	hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L2LEN_S, l2_len >> 1);
1508 
1509 	/* compute inner(/normal) L3 header size, defined in 4 Bytes */
1510 	l3_len = l4.hdr - l3.hdr;
1511 	hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L3LEN_S, l3_len >> 2);
1512 
1513 	return hns3_set_l4_csum_length(skb, l4, l4_proto, type_cs_vlan_tso);
1514 }
1515 
1516 static int hns3_handle_vtags(struct hns3_enet_ring *tx_ring,
1517 			     struct sk_buff *skb)
1518 {
1519 	struct hnae3_handle *handle = tx_ring->tqp->handle;
1520 	struct hnae3_ae_dev *ae_dev;
1521 	struct vlan_ethhdr *vhdr;
1522 	int rc;
1523 
1524 	if (!(skb->protocol == htons(ETH_P_8021Q) ||
1525 	      skb_vlan_tag_present(skb)))
1526 		return 0;
1527 
1528 	/* For HW limitation on HNAE3_DEVICE_VERSION_V2, if port based insert
1529 	 * VLAN enabled, only one VLAN header is allowed in skb, otherwise it
1530 	 * will cause RAS error.
1531 	 */
1532 	ae_dev = hns3_get_ae_dev(handle);
1533 	if (unlikely(skb_vlan_tagged_multi(skb) &&
1534 		     ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2 &&
1535 		     handle->port_base_vlan_state ==
1536 		     HNAE3_PORT_BASE_VLAN_ENABLE))
1537 		return -EINVAL;
1538 
1539 	if (skb->protocol == htons(ETH_P_8021Q) &&
1540 	    !(handle->kinfo.netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
1541 		/* When HW VLAN acceleration is turned off, and the stack
1542 		 * sets the protocol to 802.1q, the driver just need to
1543 		 * set the protocol to the encapsulated ethertype.
1544 		 */
1545 		skb->protocol = vlan_get_protocol(skb);
1546 		return 0;
1547 	}
1548 
1549 	if (skb_vlan_tag_present(skb)) {
1550 		/* Based on hw strategy, use out_vtag in two layer tag case,
1551 		 * and use inner_vtag in one tag case.
1552 		 */
1553 		if (skb->protocol == htons(ETH_P_8021Q) &&
1554 		    handle->port_base_vlan_state ==
1555 		    HNAE3_PORT_BASE_VLAN_DISABLE)
1556 			rc = HNS3_OUTER_VLAN_TAG;
1557 		else
1558 			rc = HNS3_INNER_VLAN_TAG;
1559 
1560 		skb->protocol = vlan_get_protocol(skb);
1561 		return rc;
1562 	}
1563 
1564 	rc = skb_cow_head(skb, 0);
1565 	if (unlikely(rc < 0))
1566 		return rc;
1567 
1568 	vhdr = skb_vlan_eth_hdr(skb);
1569 	vhdr->h_vlan_TCI |= cpu_to_be16((skb->priority << VLAN_PRIO_SHIFT)
1570 					 & VLAN_PRIO_MASK);
1571 
1572 	skb->protocol = vlan_get_protocol(skb);
1573 	return 0;
1574 }
1575 
1576 /* check if the hardware is capable of checksum offloading */
1577 static bool hns3_check_hw_tx_csum(struct sk_buff *skb)
1578 {
1579 	struct hns3_nic_priv *priv = netdev_priv(skb->dev);
1580 
1581 	/* Kindly note, due to backward compatibility of the TX descriptor,
1582 	 * HW checksum of the non-IP packets and GSO packets is handled at
1583 	 * different place in the following code
1584 	 */
1585 	if (skb_csum_is_sctp(skb) || skb_is_gso(skb) ||
1586 	    !test_bit(HNS3_NIC_STATE_HW_TX_CSUM_ENABLE, &priv->state))
1587 		return false;
1588 
1589 	return true;
1590 }
1591 
1592 struct hns3_desc_param {
1593 	u32 paylen_ol4cs;
1594 	u32 ol_type_vlan_len_msec;
1595 	u32 type_cs_vlan_tso;
1596 	u16 mss_hw_csum;
1597 	u16 inner_vtag;
1598 	u16 out_vtag;
1599 };
1600 
1601 static void hns3_init_desc_data(struct sk_buff *skb, struct hns3_desc_param *pa)
1602 {
1603 	pa->paylen_ol4cs = skb->len;
1604 	pa->ol_type_vlan_len_msec = 0;
1605 	pa->type_cs_vlan_tso = 0;
1606 	pa->mss_hw_csum = 0;
1607 	pa->inner_vtag = 0;
1608 	pa->out_vtag = 0;
1609 }
1610 
1611 static int hns3_handle_vlan_info(struct hns3_enet_ring *ring,
1612 				 struct sk_buff *skb,
1613 				 struct hns3_desc_param *param)
1614 {
1615 	int ret;
1616 
1617 	ret = hns3_handle_vtags(ring, skb);
1618 	if (unlikely(ret < 0)) {
1619 		hns3_ring_stats_update(ring, tx_vlan_err);
1620 		return ret;
1621 	} else if (ret == HNS3_INNER_VLAN_TAG) {
1622 		param->inner_vtag = skb_vlan_tag_get(skb);
1623 		param->inner_vtag |= (skb->priority << VLAN_PRIO_SHIFT) &
1624 				VLAN_PRIO_MASK;
1625 		hns3_set_field(param->type_cs_vlan_tso, HNS3_TXD_VLAN_B, 1);
1626 	} else if (ret == HNS3_OUTER_VLAN_TAG) {
1627 		param->out_vtag = skb_vlan_tag_get(skb);
1628 		param->out_vtag |= (skb->priority << VLAN_PRIO_SHIFT) &
1629 				VLAN_PRIO_MASK;
1630 		hns3_set_field(param->ol_type_vlan_len_msec, HNS3_TXD_OVLAN_B,
1631 			       1);
1632 	}
1633 	return 0;
1634 }
1635 
1636 static int hns3_handle_csum_partial(struct hns3_enet_ring *ring,
1637 				    struct sk_buff *skb,
1638 				    struct hns3_desc_cb *desc_cb,
1639 				    struct hns3_desc_param *param)
1640 {
1641 	u8 ol4_proto, il4_proto;
1642 	int ret;
1643 
1644 	if (hns3_check_hw_tx_csum(skb)) {
1645 		/* set checksum start and offset, defined in 2 Bytes */
1646 		hns3_set_field(param->type_cs_vlan_tso, HNS3_TXD_CSUM_START_S,
1647 			       skb_checksum_start_offset(skb) >> 1);
1648 		hns3_set_field(param->ol_type_vlan_len_msec,
1649 			       HNS3_TXD_CSUM_OFFSET_S,
1650 			       skb->csum_offset >> 1);
1651 		param->mss_hw_csum |= BIT(HNS3_TXD_HW_CS_B);
1652 		return 0;
1653 	}
1654 
1655 	skb_reset_mac_len(skb);
1656 
1657 	ret = hns3_get_l4_protocol(skb, &ol4_proto, &il4_proto);
1658 	if (unlikely(ret < 0)) {
1659 		hns3_ring_stats_update(ring, tx_l4_proto_err);
1660 		return ret;
1661 	}
1662 
1663 	ret = hns3_set_l2l3l4(skb, ol4_proto, il4_proto,
1664 			      &param->type_cs_vlan_tso,
1665 			      &param->ol_type_vlan_len_msec);
1666 	if (unlikely(ret < 0)) {
1667 		hns3_ring_stats_update(ring, tx_l2l3l4_err);
1668 		return ret;
1669 	}
1670 
1671 	ret = hns3_set_tso(skb, &param->paylen_ol4cs, &param->mss_hw_csum,
1672 			   &param->type_cs_vlan_tso, &desc_cb->send_bytes);
1673 	if (unlikely(ret < 0)) {
1674 		hns3_ring_stats_update(ring, tx_tso_err);
1675 		return ret;
1676 	}
1677 	return 0;
1678 }
1679 
1680 static int hns3_fill_skb_desc(struct hns3_enet_ring *ring,
1681 			      struct sk_buff *skb, struct hns3_desc *desc,
1682 			      struct hns3_desc_cb *desc_cb)
1683 {
1684 	struct hns3_desc_param param;
1685 	int ret;
1686 
1687 	hns3_init_desc_data(skb, &param);
1688 	ret = hns3_handle_vlan_info(ring, skb, &param);
1689 	if (unlikely(ret < 0))
1690 		return ret;
1691 
1692 	desc_cb->send_bytes = skb->len;
1693 
1694 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1695 		ret = hns3_handle_csum_partial(ring, skb, desc_cb, &param);
1696 		if (ret)
1697 			return ret;
1698 	}
1699 
1700 	/* Set txbd */
1701 	desc->tx.ol_type_vlan_len_msec =
1702 		cpu_to_le32(param.ol_type_vlan_len_msec);
1703 	desc->tx.type_cs_vlan_tso_len = cpu_to_le32(param.type_cs_vlan_tso);
1704 	desc->tx.paylen_ol4cs = cpu_to_le32(param.paylen_ol4cs);
1705 	desc->tx.mss_hw_csum = cpu_to_le16(param.mss_hw_csum);
1706 	desc->tx.vlan_tag = cpu_to_le16(param.inner_vtag);
1707 	desc->tx.outer_vlan_tag = cpu_to_le16(param.out_vtag);
1708 
1709 	return 0;
1710 }
1711 
1712 static int hns3_fill_desc(struct hns3_enet_ring *ring, dma_addr_t dma,
1713 			  unsigned int size)
1714 {
1715 #define HNS3_LIKELY_BD_NUM	1
1716 
1717 	struct hns3_desc *desc = &ring->desc[ring->next_to_use];
1718 	unsigned int frag_buf_num, k;
1719 	int sizeoflast;
1720 
1721 	if (likely(size <= HNS3_MAX_BD_SIZE)) {
1722 		desc->addr = cpu_to_le64(dma);
1723 		desc->tx.send_size = cpu_to_le16(size);
1724 		desc->tx.bdtp_fe_sc_vld_ra_ri =
1725 			cpu_to_le16(BIT(HNS3_TXD_VLD_B));
1726 
1727 		trace_hns3_tx_desc(ring, ring->next_to_use);
1728 		ring_ptr_move_fw(ring, next_to_use);
1729 		return HNS3_LIKELY_BD_NUM;
1730 	}
1731 
1732 	frag_buf_num = hns3_tx_bd_count(size);
1733 	sizeoflast = size % HNS3_MAX_BD_SIZE;
1734 	sizeoflast = sizeoflast ? sizeoflast : HNS3_MAX_BD_SIZE;
1735 
1736 	/* When frag size is bigger than hardware limit, split this frag */
1737 	for (k = 0; k < frag_buf_num; k++) {
1738 		/* now, fill the descriptor */
1739 		desc->addr = cpu_to_le64(dma + HNS3_MAX_BD_SIZE * k);
1740 		desc->tx.send_size = cpu_to_le16((k == frag_buf_num - 1) ?
1741 				     (u16)sizeoflast : (u16)HNS3_MAX_BD_SIZE);
1742 		desc->tx.bdtp_fe_sc_vld_ra_ri =
1743 				cpu_to_le16(BIT(HNS3_TXD_VLD_B));
1744 
1745 		trace_hns3_tx_desc(ring, ring->next_to_use);
1746 		/* move ring pointer to next */
1747 		ring_ptr_move_fw(ring, next_to_use);
1748 
1749 		desc = &ring->desc[ring->next_to_use];
1750 	}
1751 
1752 	return frag_buf_num;
1753 }
1754 
1755 static int hns3_map_and_fill_desc(struct hns3_enet_ring *ring, void *priv,
1756 				  unsigned int type)
1757 {
1758 	struct hns3_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_use];
1759 	struct device *dev = ring_to_dev(ring);
1760 	unsigned int size;
1761 	dma_addr_t dma;
1762 
1763 	if (type & (DESC_TYPE_FRAGLIST_SKB | DESC_TYPE_SKB)) {
1764 		struct sk_buff *skb = (struct sk_buff *)priv;
1765 
1766 		size = skb_headlen(skb);
1767 		if (!size)
1768 			return 0;
1769 
1770 		dma = dma_map_single(dev, skb->data, size, DMA_TO_DEVICE);
1771 	} else if (type & DESC_TYPE_BOUNCE_HEAD) {
1772 		/* Head data has been filled in hns3_handle_tx_bounce(),
1773 		 * just return 0 here.
1774 		 */
1775 		return 0;
1776 	} else {
1777 		skb_frag_t *frag = (skb_frag_t *)priv;
1778 
1779 		size = skb_frag_size(frag);
1780 		if (!size)
1781 			return 0;
1782 
1783 		dma = skb_frag_dma_map(dev, frag, 0, size, DMA_TO_DEVICE);
1784 	}
1785 
1786 	if (unlikely(dma_mapping_error(dev, dma))) {
1787 		hns3_ring_stats_update(ring, sw_err_cnt);
1788 		return -ENOMEM;
1789 	}
1790 
1791 	desc_cb->priv = priv;
1792 	desc_cb->length = size;
1793 	desc_cb->dma = dma;
1794 	desc_cb->type = type;
1795 
1796 	return hns3_fill_desc(ring, dma, size);
1797 }
1798 
1799 static unsigned int hns3_skb_bd_num(struct sk_buff *skb, unsigned int *bd_size,
1800 				    unsigned int bd_num)
1801 {
1802 	unsigned int size;
1803 	int i;
1804 
1805 	size = skb_headlen(skb);
1806 	while (size > HNS3_MAX_BD_SIZE) {
1807 		bd_size[bd_num++] = HNS3_MAX_BD_SIZE;
1808 		size -= HNS3_MAX_BD_SIZE;
1809 
1810 		if (bd_num > HNS3_MAX_TSO_BD_NUM)
1811 			return bd_num;
1812 	}
1813 
1814 	if (size) {
1815 		bd_size[bd_num++] = size;
1816 		if (bd_num > HNS3_MAX_TSO_BD_NUM)
1817 			return bd_num;
1818 	}
1819 
1820 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1821 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1822 		size = skb_frag_size(frag);
1823 		if (!size)
1824 			continue;
1825 
1826 		while (size > HNS3_MAX_BD_SIZE) {
1827 			bd_size[bd_num++] = HNS3_MAX_BD_SIZE;
1828 			size -= HNS3_MAX_BD_SIZE;
1829 
1830 			if (bd_num > HNS3_MAX_TSO_BD_NUM)
1831 				return bd_num;
1832 		}
1833 
1834 		bd_size[bd_num++] = size;
1835 		if (bd_num > HNS3_MAX_TSO_BD_NUM)
1836 			return bd_num;
1837 	}
1838 
1839 	return bd_num;
1840 }
1841 
1842 static unsigned int hns3_tx_bd_num(struct sk_buff *skb, unsigned int *bd_size,
1843 				   u8 max_non_tso_bd_num, unsigned int bd_num,
1844 				   unsigned int recursion_level)
1845 {
1846 #define HNS3_MAX_RECURSION_LEVEL	24
1847 
1848 	struct sk_buff *frag_skb;
1849 
1850 	/* If the total len is within the max bd limit */
1851 	if (likely(skb->len <= HNS3_MAX_BD_SIZE && !recursion_level &&
1852 		   !skb_has_frag_list(skb) &&
1853 		   skb_shinfo(skb)->nr_frags < max_non_tso_bd_num))
1854 		return skb_shinfo(skb)->nr_frags + 1U;
1855 
1856 	if (unlikely(recursion_level >= HNS3_MAX_RECURSION_LEVEL))
1857 		return UINT_MAX;
1858 
1859 	bd_num = hns3_skb_bd_num(skb, bd_size, bd_num);
1860 	if (!skb_has_frag_list(skb) || bd_num > HNS3_MAX_TSO_BD_NUM)
1861 		return bd_num;
1862 
1863 	skb_walk_frags(skb, frag_skb) {
1864 		bd_num = hns3_tx_bd_num(frag_skb, bd_size, max_non_tso_bd_num,
1865 					bd_num, recursion_level + 1);
1866 		if (bd_num > HNS3_MAX_TSO_BD_NUM)
1867 			return bd_num;
1868 	}
1869 
1870 	return bd_num;
1871 }
1872 
1873 static unsigned int hns3_gso_hdr_len(struct sk_buff *skb)
1874 {
1875 	if (!skb->encapsulation)
1876 		return skb_tcp_all_headers(skb);
1877 
1878 	return skb_inner_tcp_all_headers(skb);
1879 }
1880 
1881 /* HW need every continuous max_non_tso_bd_num buffer data to be larger
1882  * than MSS, we simplify it by ensuring skb_headlen + the first continuous
1883  * max_non_tso_bd_num - 1 frags to be larger than gso header len + mss,
1884  * and the remaining continuous max_non_tso_bd_num - 1 frags to be larger
1885  * than MSS except the last max_non_tso_bd_num - 1 frags.
1886  */
1887 static bool hns3_skb_need_linearized(struct sk_buff *skb, unsigned int *bd_size,
1888 				     unsigned int bd_num, u8 max_non_tso_bd_num)
1889 {
1890 	unsigned int tot_len = 0;
1891 	unsigned int i;
1892 
1893 	for (i = 0; i < max_non_tso_bd_num - 1U; i++)
1894 		tot_len += bd_size[i];
1895 
1896 	/* ensure the first max_non_tso_bd_num frags is greater than
1897 	 * mss + header
1898 	 */
1899 	if (tot_len + bd_size[max_non_tso_bd_num - 1U] <
1900 	    skb_shinfo(skb)->gso_size + hns3_gso_hdr_len(skb))
1901 		return true;
1902 
1903 	/* ensure every continuous max_non_tso_bd_num - 1 buffer is greater
1904 	 * than mss except the last one.
1905 	 */
1906 	for (i = 0; i < bd_num - max_non_tso_bd_num; i++) {
1907 		tot_len -= bd_size[i];
1908 		tot_len += bd_size[i + max_non_tso_bd_num - 1U];
1909 
1910 		if (tot_len < skb_shinfo(skb)->gso_size)
1911 			return true;
1912 	}
1913 
1914 	return false;
1915 }
1916 
1917 void hns3_shinfo_pack(struct skb_shared_info *shinfo, __u32 *size)
1918 {
1919 	u32 i;
1920 
1921 	for (i = 0; i < MAX_SKB_FRAGS; i++)
1922 		size[i] = skb_frag_size(&shinfo->frags[i]);
1923 }
1924 
1925 static int hns3_skb_linearize(struct hns3_enet_ring *ring,
1926 			      struct sk_buff *skb,
1927 			      unsigned int bd_num)
1928 {
1929 	/* 'bd_num == UINT_MAX' means the skb' fraglist has a
1930 	 * recursion level of over HNS3_MAX_RECURSION_LEVEL.
1931 	 */
1932 	if (bd_num == UINT_MAX) {
1933 		hns3_ring_stats_update(ring, over_max_recursion);
1934 		return -ENOMEM;
1935 	}
1936 
1937 	/* The skb->len has exceeded the hw limitation, linearization
1938 	 * will not help.
1939 	 */
1940 	if (skb->len > HNS3_MAX_TSO_SIZE ||
1941 	    (!skb_is_gso(skb) && skb->len > HNS3_MAX_NON_TSO_SIZE)) {
1942 		hns3_ring_stats_update(ring, hw_limitation);
1943 		return -ENOMEM;
1944 	}
1945 
1946 	if (__skb_linearize(skb)) {
1947 		hns3_ring_stats_update(ring, sw_err_cnt);
1948 		return -ENOMEM;
1949 	}
1950 
1951 	return 0;
1952 }
1953 
1954 static int hns3_nic_maybe_stop_tx(struct hns3_enet_ring *ring,
1955 				  struct net_device *netdev,
1956 				  struct sk_buff *skb)
1957 {
1958 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1959 	u8 max_non_tso_bd_num = priv->max_non_tso_bd_num;
1960 	unsigned int bd_size[HNS3_MAX_TSO_BD_NUM + 1U];
1961 	unsigned int bd_num;
1962 
1963 	bd_num = hns3_tx_bd_num(skb, bd_size, max_non_tso_bd_num, 0, 0);
1964 	if (unlikely(bd_num > max_non_tso_bd_num)) {
1965 		if (bd_num <= HNS3_MAX_TSO_BD_NUM && skb_is_gso(skb) &&
1966 		    !hns3_skb_need_linearized(skb, bd_size, bd_num,
1967 					      max_non_tso_bd_num)) {
1968 			trace_hns3_over_max_bd(skb);
1969 			goto out;
1970 		}
1971 
1972 		if (hns3_skb_linearize(ring, skb, bd_num))
1973 			return -ENOMEM;
1974 
1975 		bd_num = hns3_tx_bd_count(skb->len);
1976 
1977 		hns3_ring_stats_update(ring, tx_copy);
1978 	}
1979 
1980 out:
1981 	if (likely(ring_space(ring) >= bd_num))
1982 		return bd_num;
1983 
1984 	netif_stop_subqueue(netdev, ring->queue_index);
1985 	smp_mb(); /* Memory barrier before checking ring_space */
1986 
1987 	/* Start queue in case hns3_clean_tx_ring has just made room
1988 	 * available and has not seen the queue stopped state performed
1989 	 * by netif_stop_subqueue above.
1990 	 */
1991 	if (ring_space(ring) >= bd_num && netif_carrier_ok(netdev) &&
1992 	    !test_bit(HNS3_NIC_STATE_DOWN, &priv->state)) {
1993 		netif_start_subqueue(netdev, ring->queue_index);
1994 		return bd_num;
1995 	}
1996 
1997 	hns3_ring_stats_update(ring, tx_busy);
1998 
1999 	return -EBUSY;
2000 }
2001 
2002 static void hns3_clear_desc(struct hns3_enet_ring *ring, int next_to_use_orig)
2003 {
2004 	struct device *dev = ring_to_dev(ring);
2005 	unsigned int i;
2006 
2007 	for (i = 0; i < ring->desc_num; i++) {
2008 		struct hns3_desc *desc = &ring->desc[ring->next_to_use];
2009 		struct hns3_desc_cb *desc_cb;
2010 
2011 		memset(desc, 0, sizeof(*desc));
2012 
2013 		/* check if this is where we started */
2014 		if (ring->next_to_use == next_to_use_orig)
2015 			break;
2016 
2017 		/* rollback one */
2018 		ring_ptr_move_bw(ring, next_to_use);
2019 
2020 		desc_cb = &ring->desc_cb[ring->next_to_use];
2021 
2022 		if (!desc_cb->dma)
2023 			continue;
2024 
2025 		/* unmap the descriptor dma address */
2026 		if (desc_cb->type & (DESC_TYPE_SKB | DESC_TYPE_FRAGLIST_SKB))
2027 			dma_unmap_single(dev, desc_cb->dma, desc_cb->length,
2028 					 DMA_TO_DEVICE);
2029 		else if (desc_cb->type &
2030 			 (DESC_TYPE_BOUNCE_HEAD | DESC_TYPE_BOUNCE_ALL))
2031 			hns3_tx_spare_rollback(ring, desc_cb->length);
2032 		else if (desc_cb->length)
2033 			dma_unmap_page(dev, desc_cb->dma, desc_cb->length,
2034 				       DMA_TO_DEVICE);
2035 
2036 		desc_cb->length = 0;
2037 		desc_cb->dma = 0;
2038 		desc_cb->type = DESC_TYPE_UNKNOWN;
2039 	}
2040 }
2041 
2042 static int hns3_fill_skb_to_desc(struct hns3_enet_ring *ring,
2043 				 struct sk_buff *skb, unsigned int type)
2044 {
2045 	struct sk_buff *frag_skb;
2046 	int i, ret, bd_num = 0;
2047 
2048 	ret = hns3_map_and_fill_desc(ring, skb, type);
2049 	if (unlikely(ret < 0))
2050 		return ret;
2051 
2052 	bd_num += ret;
2053 
2054 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2055 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2056 
2057 		ret = hns3_map_and_fill_desc(ring, frag, DESC_TYPE_PAGE);
2058 		if (unlikely(ret < 0))
2059 			return ret;
2060 
2061 		bd_num += ret;
2062 	}
2063 
2064 	skb_walk_frags(skb, frag_skb) {
2065 		ret = hns3_fill_skb_to_desc(ring, frag_skb,
2066 					    DESC_TYPE_FRAGLIST_SKB);
2067 		if (unlikely(ret < 0))
2068 			return ret;
2069 
2070 		bd_num += ret;
2071 	}
2072 
2073 	return bd_num;
2074 }
2075 
2076 static void hns3_tx_push_bd(struct hns3_enet_ring *ring, int num)
2077 {
2078 #define HNS3_BYTES_PER_64BIT		8
2079 
2080 	struct hns3_desc desc[HNS3_MAX_PUSH_BD_NUM] = {};
2081 	int offset = 0;
2082 
2083 	/* make sure everything is visible to device before
2084 	 * excuting tx push or updating doorbell
2085 	 */
2086 	dma_wmb();
2087 
2088 	do {
2089 		int idx = (ring->next_to_use - num + ring->desc_num) %
2090 			  ring->desc_num;
2091 
2092 		u64_stats_update_begin(&ring->syncp);
2093 		ring->stats.tx_push++;
2094 		u64_stats_update_end(&ring->syncp);
2095 		memcpy(&desc[offset], &ring->desc[idx],
2096 		       sizeof(struct hns3_desc));
2097 		offset++;
2098 	} while (--num);
2099 
2100 	__iowrite64_copy(ring->tqp->mem_base, desc,
2101 			 (sizeof(struct hns3_desc) * HNS3_MAX_PUSH_BD_NUM) /
2102 			 HNS3_BYTES_PER_64BIT);
2103 }
2104 
2105 static void hns3_tx_mem_doorbell(struct hns3_enet_ring *ring)
2106 {
2107 #define HNS3_MEM_DOORBELL_OFFSET	64
2108 
2109 	__le64 bd_num = cpu_to_le64((u64)ring->pending_buf);
2110 
2111 	/* make sure everything is visible to device before
2112 	 * excuting tx push or updating doorbell
2113 	 */
2114 	dma_wmb();
2115 
2116 	__iowrite64_copy(ring->tqp->mem_base + HNS3_MEM_DOORBELL_OFFSET,
2117 			 &bd_num, 1);
2118 	u64_stats_update_begin(&ring->syncp);
2119 	ring->stats.tx_mem_doorbell += ring->pending_buf;
2120 	u64_stats_update_end(&ring->syncp);
2121 }
2122 
2123 static void hns3_tx_doorbell(struct hns3_enet_ring *ring, int num,
2124 			     bool doorbell)
2125 {
2126 	struct net_device *netdev = ring_to_netdev(ring);
2127 	struct hns3_nic_priv *priv = netdev_priv(netdev);
2128 
2129 	/* when tx push is enabled, the packet whose number of BD below
2130 	 * HNS3_MAX_PUSH_BD_NUM can be pushed directly.
2131 	 */
2132 	if (test_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state) && num &&
2133 	    !ring->pending_buf && num <= HNS3_MAX_PUSH_BD_NUM && doorbell) {
2134 		/* This smp_store_release() pairs with smp_load_acquire() in
2135 		 * hns3_nic_reclaim_desc(). Ensure that the BD valid bit
2136 		 * is updated.
2137 		 */
2138 		smp_store_release(&ring->last_to_use, ring->next_to_use);
2139 		hns3_tx_push_bd(ring, num);
2140 		return;
2141 	}
2142 
2143 	ring->pending_buf += num;
2144 
2145 	if (!doorbell) {
2146 		hns3_ring_stats_update(ring, tx_more);
2147 		return;
2148 	}
2149 
2150 	/* This smp_store_release() pairs with smp_load_acquire() in
2151 	 * hns3_nic_reclaim_desc(). Ensure that the BD valid bit is updated.
2152 	 */
2153 	smp_store_release(&ring->last_to_use, ring->next_to_use);
2154 
2155 	if (ring->tqp->mem_base)
2156 		hns3_tx_mem_doorbell(ring);
2157 	else
2158 		writel(ring->pending_buf,
2159 		       ring->tqp->io_base + HNS3_RING_TX_RING_TAIL_REG);
2160 
2161 	ring->pending_buf = 0;
2162 }
2163 
2164 static void hns3_tsyn(struct net_device *netdev, struct sk_buff *skb,
2165 		      struct hns3_desc *desc)
2166 {
2167 	struct hnae3_handle *h = hns3_get_handle(netdev);
2168 
2169 	if (!(h->ae_algo->ops->set_tx_hwts_info &&
2170 	      h->ae_algo->ops->set_tx_hwts_info(h, skb)))
2171 		return;
2172 
2173 	desc->tx.bdtp_fe_sc_vld_ra_ri |= cpu_to_le16(BIT(HNS3_TXD_TSYN_B));
2174 }
2175 
2176 static int hns3_handle_tx_bounce(struct hns3_enet_ring *ring,
2177 				 struct sk_buff *skb)
2178 {
2179 	struct hns3_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_use];
2180 	unsigned int type = DESC_TYPE_BOUNCE_HEAD;
2181 	unsigned int size = skb_headlen(skb);
2182 	dma_addr_t dma;
2183 	int bd_num = 0;
2184 	u32 cb_len;
2185 	void *buf;
2186 	int ret;
2187 
2188 	if (skb->len <= ring->tx_copybreak) {
2189 		size = skb->len;
2190 		type = DESC_TYPE_BOUNCE_ALL;
2191 	}
2192 
2193 	/* hns3_can_use_tx_bounce() is called to ensure the below
2194 	 * function can always return the tx buffer.
2195 	 */
2196 	buf = hns3_tx_spare_alloc(ring, size, &dma, &cb_len);
2197 
2198 	ret = skb_copy_bits(skb, 0, buf, size);
2199 	if (unlikely(ret < 0)) {
2200 		hns3_tx_spare_rollback(ring, cb_len);
2201 		hns3_ring_stats_update(ring, copy_bits_err);
2202 		return ret;
2203 	}
2204 
2205 	desc_cb->priv = skb;
2206 	desc_cb->length = cb_len;
2207 	desc_cb->dma = dma;
2208 	desc_cb->type = type;
2209 
2210 	bd_num += hns3_fill_desc(ring, dma, size);
2211 
2212 	if (type == DESC_TYPE_BOUNCE_HEAD) {
2213 		ret = hns3_fill_skb_to_desc(ring, skb,
2214 					    DESC_TYPE_BOUNCE_HEAD);
2215 		if (unlikely(ret < 0))
2216 			return ret;
2217 
2218 		bd_num += ret;
2219 	}
2220 
2221 	dma_sync_single_for_device(ring_to_dev(ring), dma, size,
2222 				   DMA_TO_DEVICE);
2223 
2224 	hns3_ring_stats_update(ring, tx_bounce);
2225 
2226 	return bd_num;
2227 }
2228 
2229 static int hns3_handle_tx_sgl(struct hns3_enet_ring *ring,
2230 			      struct sk_buff *skb)
2231 {
2232 	struct hns3_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_use];
2233 	u32 nfrag = skb_shinfo(skb)->nr_frags + 1;
2234 	struct sg_table *sgt;
2235 	int bd_num = 0;
2236 	dma_addr_t dma;
2237 	u32 cb_len, i;
2238 	int nents;
2239 
2240 	if (skb_has_frag_list(skb))
2241 		nfrag = HNS3_MAX_TSO_BD_NUM;
2242 
2243 	/* hns3_can_use_tx_sgl() is called to ensure the below
2244 	 * function can always return the tx buffer.
2245 	 */
2246 	sgt = hns3_tx_spare_alloc(ring, HNS3_SGL_SIZE(nfrag),
2247 				  &dma, &cb_len);
2248 
2249 	/* scatterlist follows by the sg table */
2250 	sgt->sgl = (struct scatterlist *)(sgt + 1);
2251 	sg_init_table(sgt->sgl, nfrag);
2252 	nents = skb_to_sgvec(skb, sgt->sgl, 0, skb->len);
2253 	if (unlikely(nents < 0)) {
2254 		hns3_tx_spare_rollback(ring, cb_len);
2255 		hns3_ring_stats_update(ring, skb2sgl_err);
2256 		return -ENOMEM;
2257 	}
2258 
2259 	sgt->orig_nents = nents;
2260 	sgt->nents = dma_map_sg(ring_to_dev(ring), sgt->sgl, sgt->orig_nents,
2261 				DMA_TO_DEVICE);
2262 	if (unlikely(!sgt->nents)) {
2263 		hns3_tx_spare_rollback(ring, cb_len);
2264 		hns3_ring_stats_update(ring, map_sg_err);
2265 		return -ENOMEM;
2266 	}
2267 
2268 	desc_cb->priv = skb;
2269 	desc_cb->length = cb_len;
2270 	desc_cb->dma = dma;
2271 	desc_cb->type = DESC_TYPE_SGL_SKB;
2272 
2273 	for (i = 0; i < sgt->nents; i++)
2274 		bd_num += hns3_fill_desc(ring, sg_dma_address(sgt->sgl + i),
2275 					 sg_dma_len(sgt->sgl + i));
2276 	hns3_ring_stats_update(ring, tx_sgl);
2277 
2278 	return bd_num;
2279 }
2280 
2281 static int hns3_handle_desc_filling(struct hns3_enet_ring *ring,
2282 				    struct sk_buff *skb)
2283 {
2284 	u32 space;
2285 
2286 	if (!ring->tx_spare)
2287 		goto out;
2288 
2289 	space = hns3_tx_spare_space(ring);
2290 
2291 	if (hns3_can_use_tx_sgl(ring, skb, space))
2292 		return hns3_handle_tx_sgl(ring, skb);
2293 
2294 	if (hns3_can_use_tx_bounce(ring, skb, space))
2295 		return hns3_handle_tx_bounce(ring, skb);
2296 
2297 out:
2298 	return hns3_fill_skb_to_desc(ring, skb, DESC_TYPE_SKB);
2299 }
2300 
2301 static int hns3_handle_skb_desc(struct hns3_enet_ring *ring,
2302 				struct sk_buff *skb,
2303 				struct hns3_desc_cb *desc_cb,
2304 				int next_to_use_head)
2305 {
2306 	int ret;
2307 
2308 	ret = hns3_fill_skb_desc(ring, skb, &ring->desc[ring->next_to_use],
2309 				 desc_cb);
2310 	if (unlikely(ret < 0))
2311 		goto fill_err;
2312 
2313 	/* 'ret < 0' means filling error, 'ret == 0' means skb->len is
2314 	 * zero, which is unlikely, and 'ret > 0' means how many tx desc
2315 	 * need to be notified to the hw.
2316 	 */
2317 	ret = hns3_handle_desc_filling(ring, skb);
2318 	if (likely(ret > 0))
2319 		return ret;
2320 
2321 fill_err:
2322 	hns3_clear_desc(ring, next_to_use_head);
2323 	return ret;
2324 }
2325 
2326 netdev_tx_t hns3_nic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
2327 {
2328 	struct hns3_nic_priv *priv = netdev_priv(netdev);
2329 	struct hns3_enet_ring *ring = &priv->ring[skb->queue_mapping];
2330 	struct hns3_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_use];
2331 	struct netdev_queue *dev_queue;
2332 	int pre_ntu, ret;
2333 	bool doorbell;
2334 
2335 	/* Hardware can only handle short frames above 32 bytes */
2336 	if (skb_put_padto(skb, HNS3_MIN_TX_LEN)) {
2337 		hns3_tx_doorbell(ring, 0, !netdev_xmit_more());
2338 
2339 		hns3_ring_stats_update(ring, sw_err_cnt);
2340 
2341 		return NETDEV_TX_OK;
2342 	}
2343 
2344 	/* Prefetch the data used later */
2345 	prefetch(skb->data);
2346 
2347 	ret = hns3_nic_maybe_stop_tx(ring, netdev, skb);
2348 	if (unlikely(ret <= 0)) {
2349 		if (ret == -EBUSY) {
2350 			hns3_tx_doorbell(ring, 0, true);
2351 			return NETDEV_TX_BUSY;
2352 		}
2353 
2354 		hns3_rl_err(netdev, "xmit error: %d!\n", ret);
2355 		goto out_err_tx_ok;
2356 	}
2357 
2358 	ret = hns3_handle_skb_desc(ring, skb, desc_cb, ring->next_to_use);
2359 	if (unlikely(ret <= 0))
2360 		goto out_err_tx_ok;
2361 
2362 	pre_ntu = ring->next_to_use ? (ring->next_to_use - 1) :
2363 					(ring->desc_num - 1);
2364 
2365 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
2366 		hns3_tsyn(netdev, skb, &ring->desc[pre_ntu]);
2367 
2368 	ring->desc[pre_ntu].tx.bdtp_fe_sc_vld_ra_ri |=
2369 				cpu_to_le16(BIT(HNS3_TXD_FE_B));
2370 	trace_hns3_tx_desc(ring, pre_ntu);
2371 
2372 	skb_tx_timestamp(skb);
2373 
2374 	/* Complete translate all packets */
2375 	dev_queue = netdev_get_tx_queue(netdev, ring->queue_index);
2376 	doorbell = __netdev_tx_sent_queue(dev_queue, desc_cb->send_bytes,
2377 					  netdev_xmit_more());
2378 	hns3_tx_doorbell(ring, ret, doorbell);
2379 
2380 	return NETDEV_TX_OK;
2381 
2382 out_err_tx_ok:
2383 	dev_kfree_skb_any(skb);
2384 	hns3_tx_doorbell(ring, 0, !netdev_xmit_more());
2385 	return NETDEV_TX_OK;
2386 }
2387 
2388 static int hns3_nic_net_set_mac_address(struct net_device *netdev, void *p)
2389 {
2390 	char format_mac_addr_perm[HNAE3_FORMAT_MAC_ADDR_LEN];
2391 	char format_mac_addr_sa[HNAE3_FORMAT_MAC_ADDR_LEN];
2392 	struct hnae3_handle *h = hns3_get_handle(netdev);
2393 	struct sockaddr *mac_addr = p;
2394 	int ret;
2395 
2396 	if (!mac_addr || !is_valid_ether_addr((const u8 *)mac_addr->sa_data))
2397 		return -EADDRNOTAVAIL;
2398 
2399 	if (ether_addr_equal(netdev->dev_addr, mac_addr->sa_data)) {
2400 		hnae3_format_mac_addr(format_mac_addr_sa, mac_addr->sa_data);
2401 		netdev_info(netdev, "already using mac address %s\n",
2402 			    format_mac_addr_sa);
2403 		return 0;
2404 	}
2405 
2406 	/* For VF device, if there is a perm_addr, then the user will not
2407 	 * be allowed to change the address.
2408 	 */
2409 	if (!hns3_is_phys_func(h->pdev) &&
2410 	    !is_zero_ether_addr(netdev->perm_addr)) {
2411 		hnae3_format_mac_addr(format_mac_addr_perm, netdev->perm_addr);
2412 		hnae3_format_mac_addr(format_mac_addr_sa, mac_addr->sa_data);
2413 		netdev_err(netdev, "has permanent MAC %s, user MAC %s not allow\n",
2414 			   format_mac_addr_perm, format_mac_addr_sa);
2415 		return -EPERM;
2416 	}
2417 
2418 	ret = h->ae_algo->ops->set_mac_addr(h, mac_addr->sa_data, false);
2419 	if (ret) {
2420 		netdev_err(netdev, "set_mac_address fail, ret=%d!\n", ret);
2421 		return ret;
2422 	}
2423 
2424 	eth_hw_addr_set(netdev, mac_addr->sa_data);
2425 
2426 	return 0;
2427 }
2428 
2429 static int hns3_nic_do_ioctl(struct net_device *netdev,
2430 			     struct ifreq *ifr, int cmd)
2431 {
2432 	struct hnae3_handle *h = hns3_get_handle(netdev);
2433 
2434 	if (!netif_running(netdev))
2435 		return -EINVAL;
2436 
2437 	if (!h->ae_algo->ops->do_ioctl)
2438 		return -EOPNOTSUPP;
2439 
2440 	return h->ae_algo->ops->do_ioctl(h, ifr, cmd);
2441 }
2442 
2443 static int hns3_nic_hwtstamp_get(struct net_device *netdev,
2444 				 struct kernel_hwtstamp_config *config)
2445 {
2446 	struct hnae3_handle *h = hns3_get_handle(netdev);
2447 
2448 	if (!netif_running(netdev))
2449 		return -EINVAL;
2450 
2451 	if (!h->ae_algo->ops->hwtstamp_get)
2452 		return -EOPNOTSUPP;
2453 
2454 	return h->ae_algo->ops->hwtstamp_get(h, config);
2455 }
2456 
2457 static int hns3_nic_hwtstamp_set(struct net_device *netdev,
2458 				 struct kernel_hwtstamp_config *config,
2459 				 struct netlink_ext_ack *extack)
2460 {
2461 	struct hnae3_handle *h = hns3_get_handle(netdev);
2462 
2463 	if (!netif_running(netdev))
2464 		return -EINVAL;
2465 
2466 	if (!h->ae_algo->ops->hwtstamp_set)
2467 		return -EOPNOTSUPP;
2468 
2469 	return h->ae_algo->ops->hwtstamp_set(h, config, extack);
2470 }
2471 
2472 static int hns3_nic_set_features(struct net_device *netdev,
2473 				 netdev_features_t features)
2474 {
2475 	netdev_features_t changed = netdev->features ^ features;
2476 	struct hns3_nic_priv *priv = netdev_priv(netdev);
2477 	struct hnae3_handle *h = priv->ae_handle;
2478 	bool enable;
2479 	int ret;
2480 
2481 	if (changed & (NETIF_F_GRO_HW) && h->ae_algo->ops->set_gro_en) {
2482 		enable = !!(features & NETIF_F_GRO_HW);
2483 		ret = h->ae_algo->ops->set_gro_en(h, enable);
2484 		if (ret)
2485 			return ret;
2486 	}
2487 
2488 	if ((changed & NETIF_F_HW_VLAN_CTAG_RX) &&
2489 	    h->ae_algo->ops->enable_hw_strip_rxvtag) {
2490 		enable = !!(features & NETIF_F_HW_VLAN_CTAG_RX);
2491 		ret = h->ae_algo->ops->enable_hw_strip_rxvtag(h, enable);
2492 		if (ret)
2493 			return ret;
2494 	}
2495 
2496 	if ((changed & NETIF_F_NTUPLE) && h->ae_algo->ops->enable_fd) {
2497 		enable = !!(features & NETIF_F_NTUPLE);
2498 		h->ae_algo->ops->enable_fd(h, enable);
2499 	}
2500 
2501 	if ((netdev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
2502 	    h->ae_algo->ops->cls_flower_active(h)) {
2503 		netdev_err(netdev,
2504 			   "there are offloaded TC filters active, cannot disable HW TC offload\n");
2505 		return -EINVAL;
2506 	}
2507 
2508 	if ((changed & NETIF_F_HW_VLAN_CTAG_FILTER) &&
2509 	    h->ae_algo->ops->enable_vlan_filter) {
2510 		enable = !!(features & NETIF_F_HW_VLAN_CTAG_FILTER);
2511 		ret = h->ae_algo->ops->enable_vlan_filter(h, enable);
2512 		if (ret)
2513 			return ret;
2514 	}
2515 
2516 	return 0;
2517 }
2518 
2519 static netdev_features_t hns3_features_check(struct sk_buff *skb,
2520 					     struct net_device *dev,
2521 					     netdev_features_t features)
2522 {
2523 #define HNS3_MAX_HDR_LEN	480U
2524 #define HNS3_MAX_L4_HDR_LEN	60U
2525 
2526 	size_t len;
2527 
2528 	if (skb->ip_summed != CHECKSUM_PARTIAL)
2529 		return features;
2530 
2531 	if (skb->encapsulation)
2532 		len = skb_inner_transport_offset(skb);
2533 	else
2534 		len = skb_transport_offset(skb);
2535 
2536 	/* Assume L4 is 60 byte as TCP is the only protocol with a
2537 	 * a flexible value, and it's max len is 60 bytes.
2538 	 */
2539 	len += HNS3_MAX_L4_HDR_LEN;
2540 
2541 	/* Hardware only supports checksum on the skb with a max header
2542 	 * len of 480 bytes.
2543 	 */
2544 	if (len > HNS3_MAX_HDR_LEN)
2545 		features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2546 
2547 	return features;
2548 }
2549 
2550 static void hns3_fetch_stats(struct rtnl_link_stats64 *stats,
2551 			     struct hns3_enet_ring *ring, bool is_tx)
2552 {
2553 	struct ring_stats ring_stats;
2554 	unsigned int start;
2555 
2556 	do {
2557 		start = u64_stats_fetch_begin(&ring->syncp);
2558 		ring_stats = ring->stats;
2559 	} while (u64_stats_fetch_retry(&ring->syncp, start));
2560 
2561 	if (is_tx) {
2562 		stats->tx_bytes += ring_stats.tx_bytes;
2563 		stats->tx_packets += ring_stats.tx_pkts;
2564 		stats->tx_dropped += ring_stats.sw_err_cnt;
2565 		stats->tx_dropped += ring_stats.tx_vlan_err;
2566 		stats->tx_dropped += ring_stats.tx_l4_proto_err;
2567 		stats->tx_dropped += ring_stats.tx_l2l3l4_err;
2568 		stats->tx_dropped += ring_stats.tx_tso_err;
2569 		stats->tx_dropped += ring_stats.over_max_recursion;
2570 		stats->tx_dropped += ring_stats.hw_limitation;
2571 		stats->tx_dropped += ring_stats.copy_bits_err;
2572 		stats->tx_dropped += ring_stats.skb2sgl_err;
2573 		stats->tx_dropped += ring_stats.map_sg_err;
2574 		stats->tx_errors += ring_stats.sw_err_cnt;
2575 		stats->tx_errors += ring_stats.tx_vlan_err;
2576 		stats->tx_errors += ring_stats.tx_l4_proto_err;
2577 		stats->tx_errors += ring_stats.tx_l2l3l4_err;
2578 		stats->tx_errors += ring_stats.tx_tso_err;
2579 		stats->tx_errors += ring_stats.over_max_recursion;
2580 		stats->tx_errors += ring_stats.hw_limitation;
2581 		stats->tx_errors += ring_stats.copy_bits_err;
2582 		stats->tx_errors += ring_stats.skb2sgl_err;
2583 		stats->tx_errors += ring_stats.map_sg_err;
2584 	} else {
2585 		stats->rx_bytes += ring_stats.rx_bytes;
2586 		stats->rx_packets += ring_stats.rx_pkts;
2587 		stats->rx_dropped += ring_stats.l2_err;
2588 		stats->rx_errors += ring_stats.l2_err;
2589 		stats->rx_errors += ring_stats.l3l4_csum_err;
2590 		stats->rx_crc_errors += ring_stats.l2_err;
2591 		stats->multicast += ring_stats.rx_multicast;
2592 		stats->rx_length_errors += ring_stats.err_pkt_len;
2593 	}
2594 }
2595 
2596 static void hns3_nic_get_stats64(struct net_device *netdev,
2597 				 struct rtnl_link_stats64 *stats)
2598 {
2599 	struct hns3_nic_priv *priv = netdev_priv(netdev);
2600 	int queue_num = priv->ae_handle->kinfo.num_tqps;
2601 	struct hnae3_handle *handle = priv->ae_handle;
2602 	struct rtnl_link_stats64 ring_total_stats;
2603 	struct hns3_enet_ring *ring;
2604 	int idx;
2605 
2606 	if (test_bit(HNS3_NIC_STATE_DOWN, &priv->state))
2607 		return;
2608 
2609 	handle->ae_algo->ops->update_stats(handle);
2610 
2611 	memset(&ring_total_stats, 0, sizeof(ring_total_stats));
2612 	for (idx = 0; idx < queue_num; idx++) {
2613 		/* fetch the tx stats */
2614 		ring = &priv->ring[idx];
2615 		hns3_fetch_stats(&ring_total_stats, ring, true);
2616 
2617 		/* fetch the rx stats */
2618 		ring = &priv->ring[idx + queue_num];
2619 		hns3_fetch_stats(&ring_total_stats, ring, false);
2620 	}
2621 
2622 	stats->tx_bytes = ring_total_stats.tx_bytes;
2623 	stats->tx_packets = ring_total_stats.tx_packets;
2624 	stats->rx_bytes = ring_total_stats.rx_bytes;
2625 	stats->rx_packets = ring_total_stats.rx_packets;
2626 
2627 	stats->rx_errors = ring_total_stats.rx_errors;
2628 	stats->multicast = ring_total_stats.multicast;
2629 	stats->rx_length_errors = ring_total_stats.rx_length_errors;
2630 	stats->rx_crc_errors = ring_total_stats.rx_crc_errors;
2631 	stats->rx_missed_errors = netdev->stats.rx_missed_errors;
2632 
2633 	stats->tx_errors = ring_total_stats.tx_errors;
2634 	stats->rx_dropped = ring_total_stats.rx_dropped;
2635 	stats->tx_dropped = ring_total_stats.tx_dropped;
2636 	stats->collisions = netdev->stats.collisions;
2637 	stats->rx_over_errors = netdev->stats.rx_over_errors;
2638 	stats->rx_frame_errors = netdev->stats.rx_frame_errors;
2639 	stats->rx_fifo_errors = netdev->stats.rx_fifo_errors;
2640 	stats->tx_aborted_errors = netdev->stats.tx_aborted_errors;
2641 	stats->tx_carrier_errors = netdev->stats.tx_carrier_errors;
2642 	stats->tx_fifo_errors = netdev->stats.tx_fifo_errors;
2643 	stats->tx_heartbeat_errors = netdev->stats.tx_heartbeat_errors;
2644 	stats->tx_window_errors = netdev->stats.tx_window_errors;
2645 	stats->rx_compressed = netdev->stats.rx_compressed;
2646 	stats->tx_compressed = netdev->stats.tx_compressed;
2647 }
2648 
2649 static int hns3_setup_tc(struct net_device *netdev, void *type_data)
2650 {
2651 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
2652 	struct hnae3_knic_private_info *kinfo;
2653 	u8 tc = mqprio_qopt->qopt.num_tc;
2654 	u16 mode = mqprio_qopt->mode;
2655 	u8 hw = mqprio_qopt->qopt.hw;
2656 	struct hnae3_handle *h;
2657 
2658 	if (!((hw == TC_MQPRIO_HW_OFFLOAD_TCS &&
2659 	       mode == TC_MQPRIO_MODE_CHANNEL) || (!hw && tc == 0)))
2660 		return -EOPNOTSUPP;
2661 
2662 	if (tc > HNAE3_MAX_TC)
2663 		return -EINVAL;
2664 
2665 	if (!netdev)
2666 		return -EINVAL;
2667 
2668 	h = hns3_get_handle(netdev);
2669 	kinfo = &h->kinfo;
2670 
2671 	netif_dbg(h, drv, netdev, "setup tc: num_tc=%u\n", tc);
2672 
2673 	return (kinfo->dcb_ops && kinfo->dcb_ops->setup_tc) ?
2674 		kinfo->dcb_ops->setup_tc(h, mqprio_qopt) : -EOPNOTSUPP;
2675 }
2676 
2677 static int hns3_setup_tc_cls_flower(struct hns3_nic_priv *priv,
2678 				    struct flow_cls_offload *flow)
2679 {
2680 	struct hnae3_handle *h = hns3_get_handle(priv->netdev);
2681 
2682 	switch (flow->command) {
2683 	case FLOW_CLS_REPLACE:
2684 		if (h->ae_algo->ops->add_cls_flower)
2685 			return h->ae_algo->ops->add_cls_flower(h, flow);
2686 		break;
2687 	case FLOW_CLS_DESTROY:
2688 		if (h->ae_algo->ops->del_cls_flower)
2689 			return h->ae_algo->ops->del_cls_flower(h, flow);
2690 		break;
2691 	default:
2692 		break;
2693 	}
2694 
2695 	return -EOPNOTSUPP;
2696 }
2697 
2698 static int hns3_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2699 				  void *cb_priv)
2700 {
2701 	struct hns3_nic_priv *priv = cb_priv;
2702 
2703 	if (!tc_cls_can_offload_and_chain0(priv->netdev, type_data))
2704 		return -EOPNOTSUPP;
2705 
2706 	switch (type) {
2707 	case TC_SETUP_CLSFLOWER:
2708 		return hns3_setup_tc_cls_flower(priv, type_data);
2709 	default:
2710 		return -EOPNOTSUPP;
2711 	}
2712 }
2713 
2714 static LIST_HEAD(hns3_block_cb_list);
2715 
2716 static int hns3_nic_setup_tc(struct net_device *dev, enum tc_setup_type type,
2717 			     void *type_data)
2718 {
2719 	struct hns3_nic_priv *priv = netdev_priv(dev);
2720 	int ret;
2721 
2722 	switch (type) {
2723 	case TC_SETUP_QDISC_MQPRIO:
2724 		ret = hns3_setup_tc(dev, type_data);
2725 		break;
2726 	case TC_SETUP_BLOCK:
2727 		ret = flow_block_cb_setup_simple(type_data,
2728 						 &hns3_block_cb_list,
2729 						 hns3_setup_tc_block_cb,
2730 						 priv, priv, true);
2731 		break;
2732 	default:
2733 		return -EOPNOTSUPP;
2734 	}
2735 
2736 	return ret;
2737 }
2738 
2739 static int hns3_vlan_rx_add_vid(struct net_device *netdev,
2740 				__be16 proto, u16 vid)
2741 {
2742 	struct hnae3_handle *h = hns3_get_handle(netdev);
2743 	int ret = -EIO;
2744 
2745 	if (h->ae_algo->ops->set_vlan_filter)
2746 		ret = h->ae_algo->ops->set_vlan_filter(h, proto, vid, false);
2747 
2748 	return ret;
2749 }
2750 
2751 static int hns3_vlan_rx_kill_vid(struct net_device *netdev,
2752 				 __be16 proto, u16 vid)
2753 {
2754 	struct hnae3_handle *h = hns3_get_handle(netdev);
2755 	int ret = -EIO;
2756 
2757 	if (h->ae_algo->ops->set_vlan_filter)
2758 		ret = h->ae_algo->ops->set_vlan_filter(h, proto, vid, true);
2759 
2760 	return ret;
2761 }
2762 
2763 static int hns3_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan,
2764 				u8 qos, __be16 vlan_proto)
2765 {
2766 	struct hnae3_handle *h = hns3_get_handle(netdev);
2767 	int ret = -EIO;
2768 
2769 	netif_dbg(h, drv, netdev,
2770 		  "set vf vlan: vf=%d, vlan=%u, qos=%u, vlan_proto=0x%x\n",
2771 		  vf, vlan, qos, ntohs(vlan_proto));
2772 
2773 	if (h->ae_algo->ops->set_vf_vlan_filter)
2774 		ret = h->ae_algo->ops->set_vf_vlan_filter(h, vf, vlan,
2775 							  qos, vlan_proto);
2776 
2777 	return ret;
2778 }
2779 
2780 static int hns3_set_vf_spoofchk(struct net_device *netdev, int vf, bool enable)
2781 {
2782 	struct hnae3_handle *handle = hns3_get_handle(netdev);
2783 
2784 	if (hns3_nic_resetting(netdev))
2785 		return -EBUSY;
2786 
2787 	if (!handle->ae_algo->ops->set_vf_spoofchk)
2788 		return -EOPNOTSUPP;
2789 
2790 	return handle->ae_algo->ops->set_vf_spoofchk(handle, vf, enable);
2791 }
2792 
2793 static int hns3_set_vf_trust(struct net_device *netdev, int vf, bool enable)
2794 {
2795 	struct hnae3_handle *handle = hns3_get_handle(netdev);
2796 
2797 	if (!handle->ae_algo->ops->set_vf_trust)
2798 		return -EOPNOTSUPP;
2799 
2800 	return handle->ae_algo->ops->set_vf_trust(handle, vf, enable);
2801 }
2802 
2803 static int hns3_nic_change_mtu(struct net_device *netdev, int new_mtu)
2804 {
2805 	struct hnae3_handle *h = hns3_get_handle(netdev);
2806 	int ret;
2807 
2808 	if (hns3_nic_resetting(netdev))
2809 		return -EBUSY;
2810 
2811 	if (!h->ae_algo->ops->set_mtu)
2812 		return -EOPNOTSUPP;
2813 
2814 	netif_dbg(h, drv, netdev,
2815 		  "change mtu from %u to %d\n", netdev->mtu, new_mtu);
2816 
2817 	ret = h->ae_algo->ops->set_mtu(h, new_mtu);
2818 	if (ret)
2819 		netdev_err(netdev, "failed to change MTU in hardware %d\n",
2820 			   ret);
2821 	else
2822 		WRITE_ONCE(netdev->mtu, new_mtu);
2823 
2824 	return ret;
2825 }
2826 
2827 static bool hns3_dump_timeout_queue(struct net_device *ndev,
2828 				    unsigned int txqueue)
2829 {
2830 	unsigned int timedout_ms;
2831 	struct netdev_queue *q;
2832 
2833 	q = netdev_get_tx_queue(ndev, txqueue);
2834 	timedout_ms = netif_xmit_timeout_ms(q);
2835 	if (timedout_ms) {
2836 #ifdef CONFIG_BQL
2837 		struct dql *dql = &q->dql;
2838 
2839 		netdev_info(ndev, "DQL info last_cnt: %u, queued: %u, adj_limit: %u, completed: %u\n",
2840 			    dql->last_obj_cnt, dql->num_queued,
2841 			    dql->adj_limit, dql->num_completed);
2842 #endif
2843 		netdev_info(ndev, "queue state: 0x%lx, delta msecs: %u\n",
2844 			    q->state, timedout_ms);
2845 		return true;
2846 	}
2847 
2848 	return false;
2849 }
2850 
2851 static void hns3_dump_queue_stats(struct net_device *ndev,
2852 				  struct hns3_enet_ring *tx_ring,
2853 				  int timeout_queue)
2854 {
2855 	struct napi_struct *napi = &tx_ring->tqp_vector->napi;
2856 	struct hns3_nic_priv *priv = netdev_priv(ndev);
2857 
2858 	netdev_info(ndev,
2859 		    "tx_timeout count: %llu, queue id: %d, SW_NTU: 0x%x, SW_NTC: 0x%x, napi state: %lu\n",
2860 		    priv->tx_timeout_count, timeout_queue, tx_ring->next_to_use,
2861 		    tx_ring->next_to_clean, napi->state);
2862 
2863 	netdev_info(ndev,
2864 		    "tx_pkts: %llu, tx_bytes: %llu, sw_err_cnt: %llu, tx_pending: %d\n",
2865 		    tx_ring->stats.tx_pkts, tx_ring->stats.tx_bytes,
2866 		    tx_ring->stats.sw_err_cnt, tx_ring->pending_buf);
2867 
2868 	netdev_info(ndev,
2869 		    "seg_pkt_cnt: %llu, tx_more: %llu, restart_queue: %llu, tx_busy: %llu\n",
2870 		    tx_ring->stats.seg_pkt_cnt, tx_ring->stats.tx_more,
2871 		    tx_ring->stats.restart_queue, tx_ring->stats.tx_busy);
2872 
2873 	netdev_info(ndev, "tx_push: %llu, tx_mem_doorbell: %llu\n",
2874 		    tx_ring->stats.tx_push, tx_ring->stats.tx_mem_doorbell);
2875 }
2876 
2877 static void hns3_dump_queue_reg(struct net_device *ndev,
2878 				struct hns3_enet_ring *tx_ring)
2879 {
2880 	netdev_info(ndev,
2881 		    "BD_NUM: 0x%x HW_HEAD: 0x%x, HW_TAIL: 0x%x, BD_ERR: 0x%x, INT: 0x%x\n",
2882 		    hns3_tqp_read_reg(tx_ring, HNS3_RING_TX_RING_BD_NUM_REG),
2883 		    hns3_tqp_read_reg(tx_ring, HNS3_RING_TX_RING_HEAD_REG),
2884 		    hns3_tqp_read_reg(tx_ring, HNS3_RING_TX_RING_TAIL_REG),
2885 		    hns3_tqp_read_reg(tx_ring, HNS3_RING_TX_RING_BD_ERR_REG),
2886 		    readl(tx_ring->tqp_vector->mask_addr));
2887 	netdev_info(ndev,
2888 		    "RING_EN: 0x%x, TC: 0x%x, FBD_NUM: 0x%x FBD_OFT: 0x%x, EBD_NUM: 0x%x, EBD_OFT: 0x%x\n",
2889 		    hns3_tqp_read_reg(tx_ring, HNS3_RING_EN_REG),
2890 		    hns3_tqp_read_reg(tx_ring, HNS3_RING_TX_RING_TC_REG),
2891 		    hns3_tqp_read_reg(tx_ring, HNS3_RING_TX_RING_FBDNUM_REG),
2892 		    hns3_tqp_read_reg(tx_ring, HNS3_RING_TX_RING_OFFSET_REG),
2893 		    hns3_tqp_read_reg(tx_ring, HNS3_RING_TX_RING_EBDNUM_REG),
2894 		    hns3_tqp_read_reg(tx_ring,
2895 				      HNS3_RING_TX_RING_EBD_OFFSET_REG));
2896 }
2897 
2898 static bool hns3_get_tx_timeo_queue_info(struct net_device *ndev,
2899 					 unsigned int txqueue)
2900 {
2901 	struct hns3_nic_priv *priv = netdev_priv(ndev);
2902 	struct hnae3_handle *h = hns3_get_handle(ndev);
2903 	struct hns3_enet_ring *tx_ring;
2904 
2905 	if (txqueue >= h->kinfo.num_tqps ||
2906 	    !hns3_dump_timeout_queue(ndev, txqueue)) {
2907 		netdev_info(ndev,
2908 			    "no netdev TX timeout queue found, timeout count: %llu\n",
2909 			    priv->tx_timeout_count);
2910 		return false;
2911 	}
2912 
2913 	priv->tx_timeout_count++;
2914 
2915 	tx_ring = &priv->ring[txqueue];
2916 	hns3_dump_queue_stats(ndev, tx_ring, txqueue);
2917 
2918 	/* When mac received many pause frames continuous, it's unable to send
2919 	 * packets, which may cause tx timeout
2920 	 */
2921 	if (h->ae_algo->ops->get_mac_stats) {
2922 		struct hns3_mac_stats mac_stats;
2923 
2924 		h->ae_algo->ops->get_mac_stats(h, &mac_stats);
2925 		netdev_info(ndev, "tx_pause_cnt: %llu, rx_pause_cnt: %llu\n",
2926 			    mac_stats.tx_pause_cnt, mac_stats.rx_pause_cnt);
2927 	}
2928 
2929 	hns3_dump_queue_reg(ndev, tx_ring);
2930 
2931 	return true;
2932 }
2933 
2934 static void hns3_nic_net_timeout(struct net_device *ndev, unsigned int txqueue)
2935 {
2936 	struct hns3_nic_priv *priv = netdev_priv(ndev);
2937 	struct hnae3_handle *h = priv->ae_handle;
2938 
2939 	if (!hns3_get_tx_timeo_queue_info(ndev, txqueue))
2940 		return;
2941 
2942 	/* request the reset, and let the hclge to determine
2943 	 * which reset level should be done
2944 	 */
2945 	if (h->ae_algo->ops->reset_event)
2946 		h->ae_algo->ops->reset_event(h->pdev, h);
2947 }
2948 
2949 #ifdef CONFIG_RFS_ACCEL
2950 static int hns3_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
2951 			      u16 rxq_index, u32 flow_id)
2952 {
2953 	struct hnae3_handle *h = hns3_get_handle(dev);
2954 	struct flow_keys fkeys;
2955 
2956 	if (!h->ae_algo->ops->add_arfs_entry)
2957 		return -EOPNOTSUPP;
2958 
2959 	if (skb->encapsulation)
2960 		return -EPROTONOSUPPORT;
2961 
2962 	if (!skb_flow_dissect_flow_keys(skb, &fkeys, 0))
2963 		return -EPROTONOSUPPORT;
2964 
2965 	if ((fkeys.basic.n_proto != htons(ETH_P_IP) &&
2966 	     fkeys.basic.n_proto != htons(ETH_P_IPV6)) ||
2967 	    (fkeys.basic.ip_proto != IPPROTO_TCP &&
2968 	     fkeys.basic.ip_proto != IPPROTO_UDP))
2969 		return -EPROTONOSUPPORT;
2970 
2971 	return h->ae_algo->ops->add_arfs_entry(h, rxq_index, flow_id, &fkeys);
2972 }
2973 #endif
2974 
2975 static int hns3_nic_get_vf_config(struct net_device *ndev, int vf,
2976 				  struct ifla_vf_info *ivf)
2977 {
2978 	struct hnae3_handle *h = hns3_get_handle(ndev);
2979 
2980 	if (!h->ae_algo->ops->get_vf_config)
2981 		return -EOPNOTSUPP;
2982 
2983 	return h->ae_algo->ops->get_vf_config(h, vf, ivf);
2984 }
2985 
2986 static int hns3_nic_set_vf_link_state(struct net_device *ndev, int vf,
2987 				      int link_state)
2988 {
2989 	struct hnae3_handle *h = hns3_get_handle(ndev);
2990 
2991 	if (!h->ae_algo->ops->set_vf_link_state)
2992 		return -EOPNOTSUPP;
2993 
2994 	return h->ae_algo->ops->set_vf_link_state(h, vf, link_state);
2995 }
2996 
2997 static int hns3_nic_set_vf_rate(struct net_device *ndev, int vf,
2998 				int min_tx_rate, int max_tx_rate)
2999 {
3000 	struct hnae3_handle *h = hns3_get_handle(ndev);
3001 
3002 	if (!h->ae_algo->ops->set_vf_rate)
3003 		return -EOPNOTSUPP;
3004 
3005 	return h->ae_algo->ops->set_vf_rate(h, vf, min_tx_rate, max_tx_rate,
3006 					    false);
3007 }
3008 
3009 static int hns3_nic_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
3010 {
3011 	struct hnae3_handle *h = hns3_get_handle(netdev);
3012 	char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
3013 
3014 	if (!h->ae_algo->ops->set_vf_mac)
3015 		return -EOPNOTSUPP;
3016 
3017 	if (is_multicast_ether_addr(mac)) {
3018 		hnae3_format_mac_addr(format_mac_addr, mac);
3019 		netdev_err(netdev,
3020 			   "Invalid MAC:%s specified. Could not set MAC\n",
3021 			   format_mac_addr);
3022 		return -EINVAL;
3023 	}
3024 
3025 	return h->ae_algo->ops->set_vf_mac(h, vf_id, mac);
3026 }
3027 
3028 #define HNS3_INVALID_DSCP		0xff
3029 #define HNS3_DSCP_SHIFT			2
3030 
3031 static u8 hns3_get_skb_dscp(struct sk_buff *skb)
3032 {
3033 	__be16 protocol = skb->protocol;
3034 	u8 dscp = HNS3_INVALID_DSCP;
3035 
3036 	if (protocol == htons(ETH_P_8021Q))
3037 		protocol = vlan_get_protocol(skb);
3038 
3039 	if (protocol == htons(ETH_P_IP))
3040 		dscp = ipv4_get_dsfield(ip_hdr(skb)) >> HNS3_DSCP_SHIFT;
3041 	else if (protocol == htons(ETH_P_IPV6))
3042 		dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> HNS3_DSCP_SHIFT;
3043 
3044 	return dscp;
3045 }
3046 
3047 static u16 hns3_nic_select_queue(struct net_device *netdev,
3048 				 struct sk_buff *skb,
3049 				 struct net_device *sb_dev)
3050 {
3051 	struct hnae3_handle *h = hns3_get_handle(netdev);
3052 	u8 dscp;
3053 
3054 	if (h->kinfo.tc_map_mode != HNAE3_TC_MAP_MODE_DSCP ||
3055 	    !h->ae_algo->ops->get_dscp_prio)
3056 		goto out;
3057 
3058 	dscp = hns3_get_skb_dscp(skb);
3059 	if (unlikely(dscp >= HNAE3_MAX_DSCP))
3060 		goto out;
3061 
3062 	skb->priority = h->kinfo.dscp_prio[dscp];
3063 	if (skb->priority == HNAE3_PRIO_ID_INVALID)
3064 		skb->priority = 0;
3065 
3066 out:
3067 	return netdev_pick_tx(netdev, skb, sb_dev);
3068 }
3069 
3070 static const struct net_device_ops hns3_nic_netdev_ops = {
3071 	.ndo_open		= hns3_nic_net_open,
3072 	.ndo_stop		= hns3_nic_net_stop,
3073 	.ndo_start_xmit		= hns3_nic_net_xmit,
3074 	.ndo_tx_timeout		= hns3_nic_net_timeout,
3075 	.ndo_set_mac_address	= hns3_nic_net_set_mac_address,
3076 	.ndo_eth_ioctl		= hns3_nic_do_ioctl,
3077 	.ndo_change_mtu		= hns3_nic_change_mtu,
3078 	.ndo_set_features	= hns3_nic_set_features,
3079 	.ndo_features_check	= hns3_features_check,
3080 	.ndo_get_stats64	= hns3_nic_get_stats64,
3081 	.ndo_setup_tc		= hns3_nic_setup_tc,
3082 	.ndo_set_rx_mode	= hns3_nic_set_rx_mode,
3083 	.ndo_vlan_rx_add_vid	= hns3_vlan_rx_add_vid,
3084 	.ndo_vlan_rx_kill_vid	= hns3_vlan_rx_kill_vid,
3085 	.ndo_set_vf_vlan	= hns3_ndo_set_vf_vlan,
3086 	.ndo_set_vf_spoofchk	= hns3_set_vf_spoofchk,
3087 	.ndo_set_vf_trust	= hns3_set_vf_trust,
3088 #ifdef CONFIG_RFS_ACCEL
3089 	.ndo_rx_flow_steer	= hns3_rx_flow_steer,
3090 #endif
3091 	.ndo_get_vf_config	= hns3_nic_get_vf_config,
3092 	.ndo_set_vf_link_state	= hns3_nic_set_vf_link_state,
3093 	.ndo_set_vf_rate	= hns3_nic_set_vf_rate,
3094 	.ndo_set_vf_mac		= hns3_nic_set_vf_mac,
3095 	.ndo_select_queue	= hns3_nic_select_queue,
3096 	.ndo_hwtstamp_get	= hns3_nic_hwtstamp_get,
3097 	.ndo_hwtstamp_set	= hns3_nic_hwtstamp_set,
3098 };
3099 
3100 bool hns3_is_phys_func(struct pci_dev *pdev)
3101 {
3102 	u32 dev_id = pdev->device;
3103 
3104 	switch (dev_id) {
3105 	case HNAE3_DEV_ID_GE:
3106 	case HNAE3_DEV_ID_25GE:
3107 	case HNAE3_DEV_ID_25GE_RDMA:
3108 	case HNAE3_DEV_ID_25GE_RDMA_MACSEC:
3109 	case HNAE3_DEV_ID_50GE_RDMA:
3110 	case HNAE3_DEV_ID_50GE_RDMA_MACSEC:
3111 	case HNAE3_DEV_ID_100G_RDMA_MACSEC:
3112 	case HNAE3_DEV_ID_200G_RDMA:
3113 		return true;
3114 	case HNAE3_DEV_ID_VF:
3115 	case HNAE3_DEV_ID_RDMA_DCB_PFC_VF:
3116 		return false;
3117 	default:
3118 		dev_warn(&pdev->dev, "un-recognized pci device-id %u",
3119 			 dev_id);
3120 	}
3121 
3122 	return false;
3123 }
3124 
3125 static void hns3_disable_sriov(struct pci_dev *pdev)
3126 {
3127 	/* If our VFs are assigned we cannot shut down SR-IOV
3128 	 * without causing issues, so just leave the hardware
3129 	 * available but disabled
3130 	 */
3131 	if (pci_vfs_assigned(pdev)) {
3132 		dev_warn(&pdev->dev,
3133 			 "disabling driver while VFs are assigned\n");
3134 		return;
3135 	}
3136 
3137 	pci_disable_sriov(pdev);
3138 }
3139 
3140 /* hns3_probe - Device initialization routine
3141  * @pdev: PCI device information struct
3142  * @ent: entry in hns3_pci_tbl
3143  *
3144  * hns3_probe initializes a PF identified by a pci_dev structure.
3145  * The OS initialization, configuring of the PF private structure,
3146  * and a hardware reset occur.
3147  *
3148  * Returns 0 on success, negative on failure
3149  */
3150 static int hns3_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3151 {
3152 	struct hnae3_ae_dev *ae_dev;
3153 	int ret;
3154 
3155 	ae_dev = devm_kzalloc_obj(&pdev->dev, *ae_dev);
3156 	if (!ae_dev)
3157 		return -ENOMEM;
3158 
3159 	ae_dev->pdev = pdev;
3160 	ae_dev->flag = ent->driver_data;
3161 	pci_set_drvdata(pdev, ae_dev);
3162 
3163 	ret = hnae3_register_ae_dev(ae_dev);
3164 	if (ret)
3165 		pci_set_drvdata(pdev, NULL);
3166 
3167 	return ret;
3168 }
3169 
3170 /**
3171  * hns3_clean_vf_config
3172  * @pdev: pointer to a pci_dev structure
3173  * @num_vfs: number of VFs allocated
3174  *
3175  * Clean residual vf config after disable sriov
3176  **/
3177 static void hns3_clean_vf_config(struct pci_dev *pdev, int num_vfs)
3178 {
3179 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
3180 
3181 	if (ae_dev->ops->clean_vf_config)
3182 		ae_dev->ops->clean_vf_config(ae_dev, num_vfs);
3183 }
3184 
3185 /* hns3_remove - Device removal routine
3186  * @pdev: PCI device information struct
3187  */
3188 static void hns3_remove(struct pci_dev *pdev)
3189 {
3190 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
3191 
3192 	if (hns3_is_phys_func(pdev) && IS_ENABLED(CONFIG_PCI_IOV))
3193 		hns3_disable_sriov(pdev);
3194 
3195 	hnae3_unregister_ae_dev(ae_dev);
3196 	pci_set_drvdata(pdev, NULL);
3197 }
3198 
3199 /**
3200  * hns3_pci_sriov_configure
3201  * @pdev: pointer to a pci_dev structure
3202  * @num_vfs: number of VFs to allocate
3203  *
3204  * Enable or change the number of VFs. Called when the user updates the number
3205  * of VFs in sysfs.
3206  **/
3207 static int hns3_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
3208 {
3209 	int ret;
3210 
3211 	if (!(hns3_is_phys_func(pdev) && IS_ENABLED(CONFIG_PCI_IOV))) {
3212 		dev_warn(&pdev->dev, "Can not config SRIOV\n");
3213 		return -EINVAL;
3214 	}
3215 
3216 	if (num_vfs) {
3217 		ret = pci_enable_sriov(pdev, num_vfs);
3218 		if (ret)
3219 			dev_err(&pdev->dev, "SRIOV enable failed %d\n", ret);
3220 		else
3221 			return num_vfs;
3222 	} else if (!pci_vfs_assigned(pdev)) {
3223 		int num_vfs_pre = pci_num_vf(pdev);
3224 
3225 		pci_disable_sriov(pdev);
3226 		hns3_clean_vf_config(pdev, num_vfs_pre);
3227 	} else {
3228 		dev_warn(&pdev->dev,
3229 			 "Unable to free VFs because some are assigned to VMs.\n");
3230 	}
3231 
3232 	return 0;
3233 }
3234 
3235 static void hns3_shutdown(struct pci_dev *pdev)
3236 {
3237 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
3238 
3239 	hnae3_unregister_ae_dev(ae_dev);
3240 	pci_set_drvdata(pdev, NULL);
3241 
3242 	if (system_state == SYSTEM_POWER_OFF)
3243 		pci_set_power_state(pdev, PCI_D3hot);
3244 }
3245 
3246 static int __maybe_unused hns3_suspend(struct device *dev)
3247 {
3248 	struct hnae3_ae_dev *ae_dev = dev_get_drvdata(dev);
3249 
3250 	if (ae_dev && hns3_is_phys_func(ae_dev->pdev)) {
3251 		dev_info(dev, "Begin to suspend.\n");
3252 		if (ae_dev->ops && ae_dev->ops->reset_prepare)
3253 			ae_dev->ops->reset_prepare(ae_dev, HNAE3_FUNC_RESET);
3254 	}
3255 
3256 	return 0;
3257 }
3258 
3259 static int __maybe_unused hns3_resume(struct device *dev)
3260 {
3261 	struct hnae3_ae_dev *ae_dev = dev_get_drvdata(dev);
3262 
3263 	if (ae_dev && hns3_is_phys_func(ae_dev->pdev)) {
3264 		dev_info(dev, "Begin to resume.\n");
3265 		if (ae_dev->ops && ae_dev->ops->reset_done)
3266 			ae_dev->ops->reset_done(ae_dev);
3267 	}
3268 
3269 	return 0;
3270 }
3271 
3272 static pci_ers_result_t hns3_error_detected(struct pci_dev *pdev,
3273 					    pci_channel_state_t state)
3274 {
3275 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
3276 	pci_ers_result_t ret;
3277 
3278 	dev_info(&pdev->dev, "PCI error detected, state(=%u)!!\n", state);
3279 
3280 	if (state == pci_channel_io_perm_failure)
3281 		return PCI_ERS_RESULT_DISCONNECT;
3282 
3283 	if (!ae_dev || !ae_dev->ops) {
3284 		dev_err(&pdev->dev,
3285 			"Can't recover - error happened before device initialized\n");
3286 		return PCI_ERS_RESULT_NONE;
3287 	}
3288 
3289 	if (ae_dev->ops->handle_hw_ras_error)
3290 		ret = ae_dev->ops->handle_hw_ras_error(ae_dev);
3291 	else
3292 		return PCI_ERS_RESULT_NONE;
3293 
3294 	return ret;
3295 }
3296 
3297 static pci_ers_result_t hns3_slot_reset(struct pci_dev *pdev)
3298 {
3299 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
3300 	const struct hnae3_ae_ops *ops;
3301 	enum hnae3_reset_type reset_type;
3302 	struct device *dev = &pdev->dev;
3303 
3304 	if (!ae_dev || !ae_dev->ops)
3305 		return PCI_ERS_RESULT_NONE;
3306 
3307 	ops = ae_dev->ops;
3308 	/* request the reset */
3309 	if (ops->reset_event && ops->get_reset_level &&
3310 	    ops->set_default_reset_request) {
3311 		if (ae_dev->hw_err_reset_req) {
3312 			reset_type = ops->get_reset_level(ae_dev,
3313 						&ae_dev->hw_err_reset_req);
3314 			ops->set_default_reset_request(ae_dev, reset_type);
3315 			dev_info(dev, "requesting reset due to PCI error\n");
3316 			ops->reset_event(pdev, NULL);
3317 		}
3318 
3319 		return PCI_ERS_RESULT_RECOVERED;
3320 	}
3321 
3322 	return PCI_ERS_RESULT_DISCONNECT;
3323 }
3324 
3325 static void hns3_reset_prepare(struct pci_dev *pdev)
3326 {
3327 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
3328 
3329 	dev_info(&pdev->dev, "FLR prepare\n");
3330 	if (ae_dev && ae_dev->ops && ae_dev->ops->reset_prepare)
3331 		ae_dev->ops->reset_prepare(ae_dev, HNAE3_FLR_RESET);
3332 }
3333 
3334 static void hns3_reset_done(struct pci_dev *pdev)
3335 {
3336 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
3337 
3338 	dev_info(&pdev->dev, "FLR done\n");
3339 	if (ae_dev && ae_dev->ops && ae_dev->ops->reset_done)
3340 		ae_dev->ops->reset_done(ae_dev);
3341 }
3342 
3343 static const struct pci_error_handlers hns3_err_handler = {
3344 	.error_detected = hns3_error_detected,
3345 	.slot_reset     = hns3_slot_reset,
3346 	.reset_prepare	= hns3_reset_prepare,
3347 	.reset_done	= hns3_reset_done,
3348 };
3349 
3350 static SIMPLE_DEV_PM_OPS(hns3_pm_ops, hns3_suspend, hns3_resume);
3351 
3352 static struct pci_driver hns3_driver = {
3353 	.name     = hns3_driver_name,
3354 	.id_table = hns3_pci_tbl,
3355 	.probe    = hns3_probe,
3356 	.remove   = hns3_remove,
3357 	.shutdown = hns3_shutdown,
3358 	.driver.pm  = &hns3_pm_ops,
3359 	.sriov_configure = hns3_pci_sriov_configure,
3360 	.err_handler    = &hns3_err_handler,
3361 };
3362 
3363 /* set default feature to hns3 */
3364 static void hns3_set_default_feature(struct net_device *netdev)
3365 {
3366 	struct hnae3_handle *h = hns3_get_handle(netdev);
3367 	struct pci_dev *pdev = h->pdev;
3368 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
3369 
3370 	netdev->priv_flags |= IFF_UNICAST_FLT;
3371 
3372 	netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
3373 		NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
3374 		NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
3375 		NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE |
3376 		NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL |
3377 		NETIF_F_SCTP_CRC | NETIF_F_FRAGLIST;
3378 
3379 	if (hnae3_ae_dev_gro_supported(ae_dev))
3380 		netdev->features |= NETIF_F_GRO_HW;
3381 
3382 	if (hnae3_ae_dev_fd_supported(ae_dev))
3383 		netdev->features |= NETIF_F_NTUPLE;
3384 
3385 	if (test_bit(HNAE3_DEV_SUPPORT_UDP_GSO_B, ae_dev->caps))
3386 		netdev->features |= NETIF_F_GSO_UDP_L4;
3387 
3388 	if (test_bit(HNAE3_DEV_SUPPORT_HW_TX_CSUM_B, ae_dev->caps))
3389 		netdev->features |= NETIF_F_HW_CSUM;
3390 	else
3391 		netdev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
3392 
3393 	if (test_bit(HNAE3_DEV_SUPPORT_UDP_TUNNEL_CSUM_B, ae_dev->caps))
3394 		netdev->features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
3395 
3396 	if (test_bit(HNAE3_DEV_SUPPORT_FD_FORWARD_TC_B, ae_dev->caps))
3397 		netdev->features |= NETIF_F_HW_TC;
3398 
3399 	netdev->hw_features |= netdev->features;
3400 	if (!test_bit(HNAE3_DEV_SUPPORT_VLAN_FLTR_MDF_B, ae_dev->caps))
3401 		netdev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
3402 
3403 	netdev->vlan_features |= netdev->features &
3404 		~(NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_TX |
3405 		  NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_GRO_HW | NETIF_F_NTUPLE |
3406 		  NETIF_F_HW_TC);
3407 
3408 	netdev->hw_enc_features |= netdev->vlan_features | NETIF_F_TSO_MANGLEID;
3409 
3410 	/* The device_version V3 hardware can't offload the checksum for IP in
3411 	 * GRE packets, but can do it for NvGRE. So default to disable the
3412 	 * checksum and GSO offload for GRE.
3413 	 */
3414 	if (ae_dev->dev_version > HNAE3_DEVICE_VERSION_V2) {
3415 		netdev->features &= ~NETIF_F_GSO_GRE;
3416 		netdev->features &= ~NETIF_F_GSO_GRE_CSUM;
3417 	}
3418 }
3419 
3420 static int hns3_alloc_buffer(struct hns3_enet_ring *ring,
3421 			     struct hns3_desc_cb *cb)
3422 {
3423 	unsigned int order = hns3_page_order(ring);
3424 	struct page *p;
3425 
3426 	if (ring->page_pool) {
3427 		p = page_pool_dev_alloc_frag(ring->page_pool,
3428 					     &cb->page_offset,
3429 					     hns3_buf_size(ring));
3430 		if (unlikely(!p))
3431 			return -ENOMEM;
3432 
3433 		cb->priv = p;
3434 		cb->buf = page_address(p);
3435 		cb->dma = page_pool_get_dma_addr(p);
3436 		cb->type = DESC_TYPE_PP_FRAG;
3437 		cb->reuse_flag = 0;
3438 		return 0;
3439 	}
3440 
3441 	p = dev_alloc_pages(order);
3442 	if (!p)
3443 		return -ENOMEM;
3444 
3445 	cb->priv = p;
3446 	cb->page_offset = 0;
3447 	cb->reuse_flag = 0;
3448 	cb->buf  = page_address(p);
3449 	cb->length = hns3_page_size(ring);
3450 	cb->type = DESC_TYPE_PAGE;
3451 	page_ref_add(p, USHRT_MAX - 1);
3452 	cb->pagecnt_bias = USHRT_MAX;
3453 
3454 	return 0;
3455 }
3456 
3457 static void hns3_free_buffer(struct hns3_enet_ring *ring,
3458 			     struct hns3_desc_cb *cb, int budget)
3459 {
3460 	if (cb->type & (DESC_TYPE_SKB | DESC_TYPE_BOUNCE_HEAD |
3461 			DESC_TYPE_BOUNCE_ALL | DESC_TYPE_SGL_SKB))
3462 		napi_consume_skb(cb->priv, budget);
3463 	else if (!HNAE3_IS_TX_RING(ring)) {
3464 		if (cb->type & DESC_TYPE_PAGE && cb->pagecnt_bias)
3465 			__page_frag_cache_drain(cb->priv, cb->pagecnt_bias);
3466 		else if (cb->type & DESC_TYPE_PP_FRAG)
3467 			page_pool_put_full_page(ring->page_pool, cb->priv,
3468 						false);
3469 	}
3470 	memset(cb, 0, sizeof(*cb));
3471 }
3472 
3473 static int hns3_map_buffer(struct hns3_enet_ring *ring, struct hns3_desc_cb *cb)
3474 {
3475 	cb->dma = dma_map_page(ring_to_dev(ring), cb->priv, 0,
3476 			       cb->length, ring_to_dma_dir(ring));
3477 
3478 	if (unlikely(dma_mapping_error(ring_to_dev(ring), cb->dma)))
3479 		return -EIO;
3480 
3481 	return 0;
3482 }
3483 
3484 static void hns3_unmap_buffer(struct hns3_enet_ring *ring,
3485 			      struct hns3_desc_cb *cb)
3486 {
3487 	if (cb->type & (DESC_TYPE_SKB | DESC_TYPE_FRAGLIST_SKB))
3488 		dma_unmap_single(ring_to_dev(ring), cb->dma, cb->length,
3489 				 ring_to_dma_dir(ring));
3490 	else if ((cb->type & DESC_TYPE_PAGE) && cb->length)
3491 		dma_unmap_page(ring_to_dev(ring), cb->dma, cb->length,
3492 			       ring_to_dma_dir(ring));
3493 	else if (cb->type & (DESC_TYPE_BOUNCE_ALL | DESC_TYPE_BOUNCE_HEAD |
3494 			     DESC_TYPE_SGL_SKB))
3495 		hns3_tx_spare_reclaim_cb(ring, cb);
3496 }
3497 
3498 static void hns3_buffer_detach(struct hns3_enet_ring *ring, int i)
3499 {
3500 	hns3_unmap_buffer(ring, &ring->desc_cb[i]);
3501 	ring->desc[i].addr = 0;
3502 	ring->desc_cb[i].refill = 0;
3503 }
3504 
3505 static void hns3_free_buffer_detach(struct hns3_enet_ring *ring, int i,
3506 				    int budget)
3507 {
3508 	struct hns3_desc_cb *cb = &ring->desc_cb[i];
3509 
3510 	if (!ring->desc_cb[i].dma)
3511 		return;
3512 
3513 	hns3_buffer_detach(ring, i);
3514 	hns3_free_buffer(ring, cb, budget);
3515 }
3516 
3517 static void hns3_free_buffers(struct hns3_enet_ring *ring)
3518 {
3519 	int i;
3520 
3521 	for (i = 0; i < ring->desc_num; i++)
3522 		hns3_free_buffer_detach(ring, i, 0);
3523 }
3524 
3525 /* free desc along with its attached buffer */
3526 static void hns3_free_desc(struct hns3_enet_ring *ring)
3527 {
3528 	int size = ring->desc_num * sizeof(ring->desc[0]);
3529 
3530 	hns3_free_buffers(ring);
3531 
3532 	if (ring->desc) {
3533 		dma_free_coherent(ring_to_dev(ring), size,
3534 				  ring->desc, ring->desc_dma_addr);
3535 		ring->desc = NULL;
3536 	}
3537 }
3538 
3539 static int hns3_alloc_desc(struct hns3_enet_ring *ring)
3540 {
3541 	int size = ring->desc_num * sizeof(ring->desc[0]);
3542 
3543 	ring->desc = dma_alloc_coherent(ring_to_dev(ring), size,
3544 					&ring->desc_dma_addr, GFP_KERNEL);
3545 	if (!ring->desc)
3546 		return -ENOMEM;
3547 
3548 	return 0;
3549 }
3550 
3551 static int hns3_alloc_and_map_buffer(struct hns3_enet_ring *ring,
3552 				   struct hns3_desc_cb *cb)
3553 {
3554 	int ret;
3555 
3556 	ret = hns3_alloc_buffer(ring, cb);
3557 	if (ret || ring->page_pool)
3558 		goto out;
3559 
3560 	ret = hns3_map_buffer(ring, cb);
3561 	if (ret)
3562 		goto out_with_buf;
3563 
3564 	return 0;
3565 
3566 out_with_buf:
3567 	hns3_free_buffer(ring, cb, 0);
3568 out:
3569 	return ret;
3570 }
3571 
3572 static int hns3_alloc_and_attach_buffer(struct hns3_enet_ring *ring, int i)
3573 {
3574 	int ret = hns3_alloc_and_map_buffer(ring, &ring->desc_cb[i]);
3575 
3576 	if (ret)
3577 		return ret;
3578 
3579 	ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma +
3580 					 ring->desc_cb[i].page_offset);
3581 	ring->desc_cb[i].refill = 1;
3582 
3583 	return 0;
3584 }
3585 
3586 /* Allocate memory for raw pkg, and map with dma */
3587 static int hns3_alloc_ring_buffers(struct hns3_enet_ring *ring)
3588 {
3589 	int i, j, ret;
3590 
3591 	for (i = 0; i < ring->desc_num; i++) {
3592 		ret = hns3_alloc_and_attach_buffer(ring, i);
3593 		if (ret)
3594 			goto out_buffer_fail;
3595 
3596 		if (!(i % HNS3_RESCHED_BD_NUM))
3597 			cond_resched();
3598 	}
3599 
3600 	return 0;
3601 
3602 out_buffer_fail:
3603 	for (j = i - 1; j >= 0; j--)
3604 		hns3_free_buffer_detach(ring, j, 0);
3605 	return ret;
3606 }
3607 
3608 /* detach a in-used buffer and replace with a reserved one */
3609 static void hns3_replace_buffer(struct hns3_enet_ring *ring, int i,
3610 				struct hns3_desc_cb *res_cb)
3611 {
3612 	hns3_unmap_buffer(ring, &ring->desc_cb[i]);
3613 	ring->desc_cb[i] = *res_cb;
3614 	ring->desc_cb[i].refill = 1;
3615 	ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma +
3616 					 ring->desc_cb[i].page_offset);
3617 	ring->desc[i].rx.bd_base_info = 0;
3618 }
3619 
3620 static void hns3_reuse_buffer(struct hns3_enet_ring *ring, int i)
3621 {
3622 	ring->desc_cb[i].reuse_flag = 0;
3623 	ring->desc_cb[i].refill = 1;
3624 	ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma +
3625 					 ring->desc_cb[i].page_offset);
3626 	ring->desc[i].rx.bd_base_info = 0;
3627 
3628 	dma_sync_single_for_device(ring_to_dev(ring),
3629 			ring->desc_cb[i].dma + ring->desc_cb[i].page_offset,
3630 			hns3_buf_size(ring),
3631 			DMA_FROM_DEVICE);
3632 }
3633 
3634 static bool hns3_nic_reclaim_desc(struct hns3_enet_ring *ring,
3635 				  int *bytes, int *pkts, int budget)
3636 {
3637 	/* This smp_load_acquire() pairs with smp_store_release() in
3638 	 * hns3_tx_doorbell().
3639 	 */
3640 	int ltu = smp_load_acquire(&ring->last_to_use);
3641 	int ntc = ring->next_to_clean;
3642 	struct hns3_desc_cb *desc_cb;
3643 	bool reclaimed = false;
3644 	struct hns3_desc *desc;
3645 
3646 	while (ltu != ntc) {
3647 		desc = &ring->desc[ntc];
3648 
3649 		if (le16_to_cpu(desc->tx.bdtp_fe_sc_vld_ra_ri) &
3650 				BIT(HNS3_TXD_VLD_B))
3651 			break;
3652 
3653 		desc_cb = &ring->desc_cb[ntc];
3654 
3655 		if (desc_cb->type & (DESC_TYPE_SKB | DESC_TYPE_BOUNCE_ALL |
3656 				     DESC_TYPE_BOUNCE_HEAD |
3657 				     DESC_TYPE_SGL_SKB)) {
3658 			(*pkts)++;
3659 			(*bytes) += desc_cb->send_bytes;
3660 		}
3661 
3662 		/* desc_cb will be cleaned, after hnae3_free_buffer_detach */
3663 		hns3_free_buffer_detach(ring, ntc, budget);
3664 
3665 		if (++ntc == ring->desc_num)
3666 			ntc = 0;
3667 
3668 		/* Issue prefetch for next Tx descriptor */
3669 		prefetch(&ring->desc_cb[ntc]);
3670 		reclaimed = true;
3671 	}
3672 
3673 	if (unlikely(!reclaimed))
3674 		return false;
3675 
3676 	/* This smp_store_release() pairs with smp_load_acquire() in
3677 	 * ring_space called by hns3_nic_net_xmit.
3678 	 */
3679 	smp_store_release(&ring->next_to_clean, ntc);
3680 
3681 	hns3_tx_spare_update(ring);
3682 
3683 	return true;
3684 }
3685 
3686 void hns3_clean_tx_ring(struct hns3_enet_ring *ring, int budget)
3687 {
3688 	struct net_device *netdev = ring_to_netdev(ring);
3689 	struct hns3_nic_priv *priv = netdev_priv(netdev);
3690 	struct netdev_queue *dev_queue;
3691 	int bytes, pkts;
3692 
3693 	bytes = 0;
3694 	pkts = 0;
3695 
3696 	if (unlikely(!hns3_nic_reclaim_desc(ring, &bytes, &pkts, budget)))
3697 		return;
3698 
3699 	ring->tqp_vector->tx_group.total_bytes += bytes;
3700 	ring->tqp_vector->tx_group.total_packets += pkts;
3701 
3702 	u64_stats_update_begin(&ring->syncp);
3703 	ring->stats.tx_bytes += bytes;
3704 	ring->stats.tx_pkts += pkts;
3705 	u64_stats_update_end(&ring->syncp);
3706 
3707 	dev_queue = netdev_get_tx_queue(netdev, ring->tqp->tqp_index);
3708 	netdev_tx_completed_queue(dev_queue, pkts, bytes);
3709 
3710 	if (unlikely(netif_carrier_ok(netdev) &&
3711 		     ring_space(ring) > HNS3_MAX_TSO_BD_NUM)) {
3712 		/* Make sure that anybody stopping the queue after this
3713 		 * sees the new next_to_clean.
3714 		 */
3715 		smp_mb();
3716 		if (netif_tx_queue_stopped(dev_queue) &&
3717 		    !test_bit(HNS3_NIC_STATE_DOWN, &priv->state)) {
3718 			netif_tx_wake_queue(dev_queue);
3719 			ring->stats.restart_queue++;
3720 		}
3721 	}
3722 }
3723 
3724 static int hns3_desc_unused(struct hns3_enet_ring *ring)
3725 {
3726 	int ntc = ring->next_to_clean;
3727 	int ntu = ring->next_to_use;
3728 
3729 	if (unlikely(ntc == ntu && !ring->desc_cb[ntc].refill))
3730 		return ring->desc_num;
3731 
3732 	return ((ntc >= ntu) ? 0 : ring->desc_num) + ntc - ntu;
3733 }
3734 
3735 /* Return true if there is any allocation failure */
3736 static bool hns3_nic_alloc_rx_buffers(struct hns3_enet_ring *ring,
3737 				      int cleand_count)
3738 {
3739 	struct hns3_desc_cb *desc_cb;
3740 	struct hns3_desc_cb res_cbs;
3741 	int i, ret;
3742 
3743 	for (i = 0; i < cleand_count; i++) {
3744 		desc_cb = &ring->desc_cb[ring->next_to_use];
3745 		if (desc_cb->reuse_flag) {
3746 			hns3_ring_stats_update(ring, reuse_pg_cnt);
3747 
3748 			hns3_reuse_buffer(ring, ring->next_to_use);
3749 		} else {
3750 			ret = hns3_alloc_and_map_buffer(ring, &res_cbs);
3751 			if (ret) {
3752 				hns3_ring_stats_update(ring, sw_err_cnt);
3753 
3754 				hns3_rl_err(ring_to_netdev(ring),
3755 					    "alloc rx buffer failed: %d\n",
3756 					    ret);
3757 
3758 				writel(i, ring->tqp->io_base +
3759 				       HNS3_RING_RX_RING_HEAD_REG);
3760 				return true;
3761 			}
3762 			hns3_replace_buffer(ring, ring->next_to_use, &res_cbs);
3763 
3764 			hns3_ring_stats_update(ring, non_reuse_pg);
3765 		}
3766 
3767 		ring_ptr_move_fw(ring, next_to_use);
3768 	}
3769 
3770 	writel(i, ring->tqp->io_base + HNS3_RING_RX_RING_HEAD_REG);
3771 	return false;
3772 }
3773 
3774 static bool hns3_can_reuse_page(struct hns3_desc_cb *cb)
3775 {
3776 	return page_count(cb->priv) == cb->pagecnt_bias;
3777 }
3778 
3779 static int hns3_handle_rx_copybreak(struct sk_buff *skb, int i,
3780 				    struct hns3_enet_ring *ring,
3781 				    int pull_len,
3782 				    struct hns3_desc_cb *desc_cb)
3783 {
3784 	struct hns3_desc *desc = &ring->desc[ring->next_to_clean];
3785 	u32 frag_offset = desc_cb->page_offset + pull_len;
3786 	int size = le16_to_cpu(desc->rx.size);
3787 	u32 frag_size = size - pull_len;
3788 	void *frag = napi_alloc_frag(frag_size);
3789 
3790 	if (unlikely(!frag)) {
3791 		hns3_ring_stats_update(ring, frag_alloc_err);
3792 
3793 		hns3_rl_err(ring_to_netdev(ring),
3794 			    "failed to allocate rx frag\n");
3795 		return -ENOMEM;
3796 	}
3797 
3798 	desc_cb->reuse_flag = 1;
3799 	memcpy(frag, desc_cb->buf + frag_offset, frag_size);
3800 	skb_add_rx_frag(skb, i, virt_to_page(frag),
3801 			offset_in_page(frag), frag_size, frag_size);
3802 
3803 	hns3_ring_stats_update(ring, frag_alloc);
3804 	return 0;
3805 }
3806 
3807 static void hns3_nic_reuse_page(struct sk_buff *skb, int i,
3808 				struct hns3_enet_ring *ring, int pull_len,
3809 				struct hns3_desc_cb *desc_cb)
3810 {
3811 	struct hns3_desc *desc = &ring->desc[ring->next_to_clean];
3812 	u32 frag_offset = desc_cb->page_offset + pull_len;
3813 	int size = le16_to_cpu(desc->rx.size);
3814 	u32 truesize = hns3_buf_size(ring);
3815 	u32 frag_size = size - pull_len;
3816 	int ret = 0;
3817 	bool reused;
3818 
3819 	if (ring->page_pool) {
3820 		skb_add_rx_frag(skb, i, desc_cb->priv, frag_offset,
3821 				frag_size, truesize);
3822 		return;
3823 	}
3824 
3825 	/* Avoid re-using remote or pfmem page */
3826 	if (unlikely(!dev_page_is_reusable(desc_cb->priv)))
3827 		goto out;
3828 
3829 	reused = hns3_can_reuse_page(desc_cb);
3830 
3831 	/* Rx page can be reused when:
3832 	 * 1. Rx page is only owned by the driver when page_offset
3833 	 *    is zero, which means 0 @ truesize will be used by
3834 	 *    stack after skb_add_rx_frag() is called, and the rest
3835 	 *    of rx page can be reused by driver.
3836 	 * Or
3837 	 * 2. Rx page is only owned by the driver when page_offset
3838 	 *    is non-zero, which means page_offset @ truesize will
3839 	 *    be used by stack after skb_add_rx_frag() is called,
3840 	 *    and 0 @ truesize can be reused by driver.
3841 	 */
3842 	if ((!desc_cb->page_offset && reused) ||
3843 	    ((desc_cb->page_offset + truesize + truesize) <=
3844 	     hns3_page_size(ring) && desc_cb->page_offset)) {
3845 		desc_cb->page_offset += truesize;
3846 		desc_cb->reuse_flag = 1;
3847 	} else if (desc_cb->page_offset && reused) {
3848 		desc_cb->page_offset = 0;
3849 		desc_cb->reuse_flag = 1;
3850 	} else if (frag_size <= ring->rx_copybreak) {
3851 		ret = hns3_handle_rx_copybreak(skb, i, ring, pull_len, desc_cb);
3852 		if (!ret)
3853 			return;
3854 	}
3855 
3856 out:
3857 	desc_cb->pagecnt_bias--;
3858 
3859 	if (unlikely(!desc_cb->pagecnt_bias)) {
3860 		page_ref_add(desc_cb->priv, USHRT_MAX);
3861 		desc_cb->pagecnt_bias = USHRT_MAX;
3862 	}
3863 
3864 	skb_add_rx_frag(skb, i, desc_cb->priv, frag_offset,
3865 			frag_size, truesize);
3866 
3867 	if (unlikely(!desc_cb->reuse_flag))
3868 		__page_frag_cache_drain(desc_cb->priv, desc_cb->pagecnt_bias);
3869 }
3870 
3871 static int hns3_gro_complete(struct sk_buff *skb, u32 l234info)
3872 {
3873 	__be16 type = skb->protocol;
3874 	struct tcphdr *th;
3875 	u32 depth = 0;
3876 
3877 	while (eth_type_vlan(type)) {
3878 		struct vlan_hdr *vh;
3879 
3880 		if ((depth + VLAN_HLEN) > skb_headlen(skb))
3881 			return -EFAULT;
3882 
3883 		vh = (struct vlan_hdr *)(skb->data + depth);
3884 		type = vh->h_vlan_encapsulated_proto;
3885 		depth += VLAN_HLEN;
3886 	}
3887 
3888 	skb_set_network_header(skb, depth);
3889 
3890 	if (type == htons(ETH_P_IP)) {
3891 		const struct iphdr *iph = ip_hdr(skb);
3892 
3893 		depth += sizeof(struct iphdr);
3894 		skb_set_transport_header(skb, depth);
3895 		th = tcp_hdr(skb);
3896 		th->check = ~tcp_v4_check(skb->len - depth, iph->saddr,
3897 					  iph->daddr, 0);
3898 	} else if (type == htons(ETH_P_IPV6)) {
3899 		const struct ipv6hdr *iph = ipv6_hdr(skb);
3900 
3901 		depth += sizeof(struct ipv6hdr);
3902 		skb_set_transport_header(skb, depth);
3903 		th = tcp_hdr(skb);
3904 		th->check = ~tcp_v6_check(skb->len - depth, &iph->saddr,
3905 					  &iph->daddr, 0);
3906 	} else {
3907 		hns3_rl_err(skb->dev,
3908 			    "Error: FW GRO supports only IPv4/IPv6, not 0x%04x, depth: %d\n",
3909 			    be16_to_cpu(type), depth);
3910 		return -EFAULT;
3911 	}
3912 
3913 	skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
3914 	if (th->cwr)
3915 		skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
3916 
3917 	if (l234info & BIT(HNS3_RXD_GRO_FIXID_B))
3918 		skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_FIXEDID;
3919 
3920 	skb->csum_start = (unsigned char *)th - skb->head;
3921 	skb->csum_offset = offsetof(struct tcphdr, check);
3922 	skb->ip_summed = CHECKSUM_PARTIAL;
3923 
3924 	trace_hns3_gro(skb);
3925 
3926 	return 0;
3927 }
3928 
3929 static void hns3_checksum_complete(struct hns3_enet_ring *ring,
3930 				   struct sk_buff *skb, u32 ptype, u16 csum)
3931 {
3932 	if (ptype == HNS3_INVALID_PTYPE ||
3933 	    hns3_rx_ptype_tbl[ptype].ip_summed != CHECKSUM_COMPLETE)
3934 		return;
3935 
3936 	hns3_ring_stats_update(ring, csum_complete);
3937 	skb->ip_summed = CHECKSUM_COMPLETE;
3938 	skb->csum = csum_unfold((__force __sum16)csum);
3939 }
3940 
3941 static void hns3_rx_handle_csum(struct sk_buff *skb, u32 l234info,
3942 				u32 ol_info, u32 ptype)
3943 {
3944 	int l3_type, l4_type;
3945 	int ol4_type;
3946 
3947 	if (ptype != HNS3_INVALID_PTYPE) {
3948 		skb->csum_level = hns3_rx_ptype_tbl[ptype].csum_level;
3949 		skb->ip_summed = hns3_rx_ptype_tbl[ptype].ip_summed;
3950 
3951 		return;
3952 	}
3953 
3954 	ol4_type = hnae3_get_field(ol_info, HNS3_RXD_OL4ID_M,
3955 				   HNS3_RXD_OL4ID_S);
3956 	switch (ol4_type) {
3957 	case HNS3_OL4_TYPE_MAC_IN_UDP:
3958 	case HNS3_OL4_TYPE_NVGRE:
3959 		skb->csum_level = 1;
3960 		fallthrough;
3961 	case HNS3_OL4_TYPE_NO_TUN:
3962 		l3_type = hnae3_get_field(l234info, HNS3_RXD_L3ID_M,
3963 					  HNS3_RXD_L3ID_S);
3964 		l4_type = hnae3_get_field(l234info, HNS3_RXD_L4ID_M,
3965 					  HNS3_RXD_L4ID_S);
3966 		/* Can checksum ipv4 or ipv6 + UDP/TCP/SCTP packets */
3967 		if ((l3_type == HNS3_L3_TYPE_IPV4 ||
3968 		     l3_type == HNS3_L3_TYPE_IPV6) &&
3969 		    (l4_type == HNS3_L4_TYPE_UDP ||
3970 		     l4_type == HNS3_L4_TYPE_TCP ||
3971 		     l4_type == HNS3_L4_TYPE_SCTP))
3972 			skb->ip_summed = CHECKSUM_UNNECESSARY;
3973 		break;
3974 	default:
3975 		break;
3976 	}
3977 }
3978 
3979 static void hns3_rx_checksum(struct hns3_enet_ring *ring, struct sk_buff *skb,
3980 			     u32 l234info, u32 bd_base_info, u32 ol_info,
3981 			     u16 csum)
3982 {
3983 	struct net_device *netdev = ring_to_netdev(ring);
3984 	struct hns3_nic_priv *priv = netdev_priv(netdev);
3985 	u32 ptype = HNS3_INVALID_PTYPE;
3986 
3987 	skb->ip_summed = CHECKSUM_NONE;
3988 
3989 	skb_checksum_none_assert(skb);
3990 
3991 	if (!(netdev->features & NETIF_F_RXCSUM))
3992 		return;
3993 
3994 	if (test_bit(HNS3_NIC_STATE_RXD_ADV_LAYOUT_ENABLE, &priv->state))
3995 		ptype = hnae3_get_field(ol_info, HNS3_RXD_PTYPE_M,
3996 					HNS3_RXD_PTYPE_S);
3997 
3998 	hns3_checksum_complete(ring, skb, ptype, csum);
3999 
4000 	/* check if hardware has done checksum */
4001 	if (!(bd_base_info & BIT(HNS3_RXD_L3L4P_B)))
4002 		return;
4003 
4004 	if (unlikely(l234info & (BIT(HNS3_RXD_L3E_B) | BIT(HNS3_RXD_L4E_B) |
4005 				 BIT(HNS3_RXD_OL3E_B) |
4006 				 BIT(HNS3_RXD_OL4E_B)))) {
4007 		skb->ip_summed = CHECKSUM_NONE;
4008 		hns3_ring_stats_update(ring, l3l4_csum_err);
4009 
4010 		return;
4011 	}
4012 
4013 	hns3_rx_handle_csum(skb, l234info, ol_info, ptype);
4014 }
4015 
4016 static void hns3_rx_skb(struct hns3_enet_ring *ring, struct sk_buff *skb)
4017 {
4018 	if (skb_has_frag_list(skb))
4019 		napi_gro_flush(&ring->tqp_vector->napi, false);
4020 
4021 	napi_gro_receive(&ring->tqp_vector->napi, skb);
4022 }
4023 
4024 static bool hns3_parse_vlan_tag(struct hns3_enet_ring *ring,
4025 				struct hns3_desc *desc, u32 l234info,
4026 				u16 *vlan_tag)
4027 {
4028 	struct hnae3_handle *handle = ring->tqp->handle;
4029 	struct pci_dev *pdev = ring->tqp->handle->pdev;
4030 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
4031 
4032 	if (unlikely(ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)) {
4033 		*vlan_tag = le16_to_cpu(desc->rx.ot_vlan_tag);
4034 		if (!(*vlan_tag & VLAN_VID_MASK))
4035 			*vlan_tag = le16_to_cpu(desc->rx.vlan_tag);
4036 
4037 		return (*vlan_tag != 0);
4038 	}
4039 
4040 #define HNS3_STRP_OUTER_VLAN	0x1
4041 #define HNS3_STRP_INNER_VLAN	0x2
4042 #define HNS3_STRP_BOTH		0x3
4043 
4044 	/* Hardware always insert VLAN tag into RX descriptor when
4045 	 * remove the tag from packet, driver needs to determine
4046 	 * reporting which tag to stack.
4047 	 */
4048 	switch (hnae3_get_field(l234info, HNS3_RXD_STRP_TAGP_M,
4049 				HNS3_RXD_STRP_TAGP_S)) {
4050 	case HNS3_STRP_OUTER_VLAN:
4051 		if (handle->port_base_vlan_state !=
4052 				HNAE3_PORT_BASE_VLAN_DISABLE)
4053 			return false;
4054 
4055 		*vlan_tag = le16_to_cpu(desc->rx.ot_vlan_tag);
4056 		return true;
4057 	case HNS3_STRP_INNER_VLAN:
4058 		if (handle->port_base_vlan_state !=
4059 				HNAE3_PORT_BASE_VLAN_DISABLE)
4060 			return false;
4061 
4062 		*vlan_tag = le16_to_cpu(desc->rx.vlan_tag);
4063 		return true;
4064 	case HNS3_STRP_BOTH:
4065 		if (handle->port_base_vlan_state ==
4066 				HNAE3_PORT_BASE_VLAN_DISABLE)
4067 			*vlan_tag = le16_to_cpu(desc->rx.ot_vlan_tag);
4068 		else
4069 			*vlan_tag = le16_to_cpu(desc->rx.vlan_tag);
4070 
4071 		return true;
4072 	default:
4073 		return false;
4074 	}
4075 }
4076 
4077 static void hns3_rx_ring_move_fw(struct hns3_enet_ring *ring)
4078 {
4079 	ring->desc[ring->next_to_clean].rx.bd_base_info &=
4080 		cpu_to_le32(~BIT(HNS3_RXD_VLD_B));
4081 	ring->desc_cb[ring->next_to_clean].refill = 0;
4082 	ring->next_to_clean += 1;
4083 
4084 	if (unlikely(ring->next_to_clean == ring->desc_num))
4085 		ring->next_to_clean = 0;
4086 }
4087 
4088 static int hns3_alloc_skb(struct hns3_enet_ring *ring, unsigned int length,
4089 			  unsigned char *va)
4090 {
4091 	struct hns3_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_clean];
4092 	struct net_device *netdev = ring_to_netdev(ring);
4093 	struct sk_buff *skb;
4094 
4095 	ring->skb = napi_alloc_skb(&ring->tqp_vector->napi, HNS3_RX_HEAD_SIZE);
4096 	skb = ring->skb;
4097 	if (unlikely(!skb)) {
4098 		hns3_rl_err(netdev, "alloc rx skb fail\n");
4099 		hns3_ring_stats_update(ring, sw_err_cnt);
4100 
4101 		return -ENOMEM;
4102 	}
4103 
4104 	trace_hns3_rx_desc(ring);
4105 	prefetchw(skb->data);
4106 
4107 	ring->pending_buf = 1;
4108 	ring->frag_num = 0;
4109 	ring->tail_skb = NULL;
4110 	if (length <= HNS3_RX_HEAD_SIZE) {
4111 		memcpy(__skb_put(skb, length), va, ALIGN(length, sizeof(long)));
4112 
4113 		/* We can reuse buffer as-is, just make sure it is reusable */
4114 		if (dev_page_is_reusable(desc_cb->priv))
4115 			desc_cb->reuse_flag = 1;
4116 		else if (desc_cb->type & DESC_TYPE_PP_FRAG)
4117 			page_pool_put_full_page(ring->page_pool, desc_cb->priv,
4118 						false);
4119 		else /* This page cannot be reused so discard it */
4120 			__page_frag_cache_drain(desc_cb->priv,
4121 						desc_cb->pagecnt_bias);
4122 
4123 		hns3_rx_ring_move_fw(ring);
4124 		return 0;
4125 	}
4126 
4127 	if (ring->page_pool)
4128 		skb_mark_for_recycle(skb);
4129 
4130 	hns3_ring_stats_update(ring, seg_pkt_cnt);
4131 
4132 	ring->pull_len = eth_get_headlen(netdev, va, HNS3_RX_HEAD_SIZE);
4133 	__skb_put(skb, ring->pull_len);
4134 	hns3_nic_reuse_page(skb, ring->frag_num++, ring, ring->pull_len,
4135 			    desc_cb);
4136 	hns3_rx_ring_move_fw(ring);
4137 
4138 	return 0;
4139 }
4140 
4141 static int hns3_add_frag(struct hns3_enet_ring *ring)
4142 {
4143 	struct sk_buff *skb = ring->skb;
4144 	struct sk_buff *head_skb = skb;
4145 	struct sk_buff *new_skb;
4146 	struct hns3_desc_cb *desc_cb;
4147 	struct hns3_desc *desc;
4148 	u32 bd_base_info;
4149 
4150 	do {
4151 		desc = &ring->desc[ring->next_to_clean];
4152 		desc_cb = &ring->desc_cb[ring->next_to_clean];
4153 		bd_base_info = le32_to_cpu(desc->rx.bd_base_info);
4154 		/* make sure HW write desc complete */
4155 		dma_rmb();
4156 		if (!(bd_base_info & BIT(HNS3_RXD_VLD_B)))
4157 			return -ENXIO;
4158 
4159 		if (unlikely(ring->frag_num >= MAX_SKB_FRAGS)) {
4160 			new_skb = napi_alloc_skb(&ring->tqp_vector->napi, 0);
4161 			if (unlikely(!new_skb)) {
4162 				hns3_rl_err(ring_to_netdev(ring),
4163 					    "alloc rx fraglist skb fail\n");
4164 				return -ENXIO;
4165 			}
4166 
4167 			if (ring->page_pool)
4168 				skb_mark_for_recycle(new_skb);
4169 
4170 			ring->frag_num = 0;
4171 
4172 			if (ring->tail_skb) {
4173 				ring->tail_skb->next = new_skb;
4174 				ring->tail_skb = new_skb;
4175 			} else {
4176 				skb_shinfo(skb)->frag_list = new_skb;
4177 				ring->tail_skb = new_skb;
4178 			}
4179 		}
4180 
4181 		if (ring->tail_skb) {
4182 			head_skb->truesize += hns3_buf_size(ring);
4183 			head_skb->data_len += le16_to_cpu(desc->rx.size);
4184 			head_skb->len += le16_to_cpu(desc->rx.size);
4185 			skb = ring->tail_skb;
4186 		}
4187 
4188 		dma_sync_single_for_cpu(ring_to_dev(ring),
4189 				desc_cb->dma + desc_cb->page_offset,
4190 				hns3_buf_size(ring),
4191 				DMA_FROM_DEVICE);
4192 
4193 		hns3_nic_reuse_page(skb, ring->frag_num++, ring, 0, desc_cb);
4194 		trace_hns3_rx_desc(ring);
4195 		hns3_rx_ring_move_fw(ring);
4196 		ring->pending_buf++;
4197 	} while (!(bd_base_info & BIT(HNS3_RXD_FE_B)));
4198 
4199 	return 0;
4200 }
4201 
4202 static int hns3_set_gro_and_checksum(struct hns3_enet_ring *ring,
4203 				     struct sk_buff *skb, u32 l234info,
4204 				     u32 bd_base_info, u32 ol_info, u16 csum)
4205 {
4206 	struct net_device *netdev = ring_to_netdev(ring);
4207 	struct hns3_nic_priv *priv = netdev_priv(netdev);
4208 	u32 l3_type;
4209 
4210 	skb_shinfo(skb)->gso_size = hnae3_get_field(bd_base_info,
4211 						    HNS3_RXD_GRO_SIZE_M,
4212 						    HNS3_RXD_GRO_SIZE_S);
4213 	/* if there is no HW GRO, do not set gro params */
4214 	if (!skb_shinfo(skb)->gso_size) {
4215 		hns3_rx_checksum(ring, skb, l234info, bd_base_info, ol_info,
4216 				 csum);
4217 		return 0;
4218 	}
4219 
4220 	NAPI_GRO_CB(skb)->count = hnae3_get_field(l234info,
4221 						  HNS3_RXD_GRO_COUNT_M,
4222 						  HNS3_RXD_GRO_COUNT_S);
4223 
4224 	if (test_bit(HNS3_NIC_STATE_RXD_ADV_LAYOUT_ENABLE, &priv->state)) {
4225 		u32 ptype = hnae3_get_field(ol_info, HNS3_RXD_PTYPE_M,
4226 					    HNS3_RXD_PTYPE_S);
4227 
4228 		l3_type = hns3_rx_ptype_tbl[ptype].l3_type;
4229 	} else {
4230 		l3_type = hnae3_get_field(l234info, HNS3_RXD_L3ID_M,
4231 					  HNS3_RXD_L3ID_S);
4232 	}
4233 
4234 	if (l3_type == HNS3_L3_TYPE_IPV4)
4235 		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
4236 	else if (l3_type == HNS3_L3_TYPE_IPV6)
4237 		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
4238 	else
4239 		return -EFAULT;
4240 
4241 	return  hns3_gro_complete(skb, l234info);
4242 }
4243 
4244 static void hns3_set_rx_skb_rss_type(struct hns3_enet_ring *ring,
4245 				     struct sk_buff *skb, u32 rss_hash,
4246 				     u32 l234info, u32 ol_info)
4247 {
4248 	enum pkt_hash_types rss_type = PKT_HASH_TYPE_NONE;
4249 	struct net_device *netdev = ring_to_netdev(ring);
4250 	struct hns3_nic_priv *priv = netdev_priv(netdev);
4251 
4252 	if (test_bit(HNS3_NIC_STATE_RXD_ADV_LAYOUT_ENABLE, &priv->state)) {
4253 		u32 ptype = hnae3_get_field(ol_info, HNS3_RXD_PTYPE_M,
4254 					    HNS3_RXD_PTYPE_S);
4255 
4256 		rss_type = hns3_rx_ptype_tbl[ptype].hash_type;
4257 	} else {
4258 		int l3_type = hnae3_get_field(l234info, HNS3_RXD_L3ID_M,
4259 					      HNS3_RXD_L3ID_S);
4260 		int l4_type = hnae3_get_field(l234info, HNS3_RXD_L4ID_M,
4261 					      HNS3_RXD_L4ID_S);
4262 
4263 		if (l3_type == HNS3_L3_TYPE_IPV4 ||
4264 		    l3_type == HNS3_L3_TYPE_IPV6) {
4265 			if (l4_type == HNS3_L4_TYPE_UDP ||
4266 			    l4_type == HNS3_L4_TYPE_TCP ||
4267 			    l4_type == HNS3_L4_TYPE_SCTP)
4268 				rss_type = PKT_HASH_TYPE_L4;
4269 			else if (l4_type == HNS3_L4_TYPE_IGMP ||
4270 				 l4_type == HNS3_L4_TYPE_ICMP)
4271 				rss_type = PKT_HASH_TYPE_L3;
4272 		}
4273 	}
4274 
4275 	skb_set_hash(skb, rss_hash, rss_type);
4276 }
4277 
4278 static void hns3_handle_rx_ts_info(struct net_device *netdev,
4279 				   struct hns3_desc *desc, struct sk_buff *skb,
4280 				   u32 bd_base_info)
4281 {
4282 	if (unlikely(bd_base_info & BIT(HNS3_RXD_TS_VLD_B))) {
4283 		struct hnae3_handle *h = hns3_get_handle(netdev);
4284 		u32 nsec = le32_to_cpu(desc->ts_nsec);
4285 		u32 sec = le32_to_cpu(desc->ts_sec);
4286 
4287 		if (h->ae_algo->ops->get_rx_hwts)
4288 			h->ae_algo->ops->get_rx_hwts(h, skb, nsec, sec);
4289 	}
4290 }
4291 
4292 static void hns3_handle_rx_vlan_tag(struct hns3_enet_ring *ring,
4293 				    struct hns3_desc *desc, struct sk_buff *skb,
4294 				    u32 l234info)
4295 {
4296 	struct net_device *netdev = ring_to_netdev(ring);
4297 
4298 	/* Based on hw strategy, the tag offloaded will be stored at
4299 	 * ot_vlan_tag in two layer tag case, and stored at vlan_tag
4300 	 * in one layer tag case.
4301 	 */
4302 	if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX) {
4303 		u16 vlan_tag;
4304 
4305 		if (hns3_parse_vlan_tag(ring, desc, l234info, &vlan_tag))
4306 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
4307 					       vlan_tag);
4308 	}
4309 }
4310 
4311 static int hns3_handle_bdinfo(struct hns3_enet_ring *ring, struct sk_buff *skb)
4312 {
4313 	struct net_device *netdev = ring_to_netdev(ring);
4314 	enum hns3_pkt_l2t_type l2_frame_type;
4315 	u32 bd_base_info, l234info, ol_info;
4316 	struct hns3_desc *desc;
4317 	unsigned int len;
4318 	int pre_ntc, ret;
4319 	u16 csum;
4320 
4321 	/* bdinfo handled below is only valid on the last BD of the
4322 	 * current packet, and ring->next_to_clean indicates the first
4323 	 * descriptor of next packet, so need - 1 below.
4324 	 */
4325 	pre_ntc = ring->next_to_clean ? (ring->next_to_clean - 1) :
4326 					(ring->desc_num - 1);
4327 	desc = &ring->desc[pre_ntc];
4328 	bd_base_info = le32_to_cpu(desc->rx.bd_base_info);
4329 	l234info = le32_to_cpu(desc->rx.l234_info);
4330 	ol_info = le32_to_cpu(desc->rx.ol_info);
4331 	csum = le16_to_cpu(desc->csum);
4332 
4333 	hns3_handle_rx_ts_info(netdev, desc, skb, bd_base_info);
4334 
4335 	hns3_handle_rx_vlan_tag(ring, desc, skb, l234info);
4336 
4337 	if (unlikely(!desc->rx.pkt_len || (l234info & (BIT(HNS3_RXD_TRUNCAT_B) |
4338 				  BIT(HNS3_RXD_L2E_B))))) {
4339 		u64_stats_update_begin(&ring->syncp);
4340 		if (l234info & BIT(HNS3_RXD_L2E_B))
4341 			ring->stats.l2_err++;
4342 		else
4343 			ring->stats.err_pkt_len++;
4344 		u64_stats_update_end(&ring->syncp);
4345 
4346 		return -EFAULT;
4347 	}
4348 
4349 	len = skb->len;
4350 
4351 	/* Do update ip stack process */
4352 	skb->protocol = eth_type_trans(skb, netdev);
4353 
4354 	/* This is needed in order to enable forwarding support */
4355 	ret = hns3_set_gro_and_checksum(ring, skb, l234info,
4356 					bd_base_info, ol_info, csum);
4357 	if (unlikely(ret)) {
4358 		hns3_ring_stats_update(ring, rx_err_cnt);
4359 		return ret;
4360 	}
4361 
4362 	l2_frame_type = hnae3_get_field(l234info, HNS3_RXD_DMAC_M,
4363 					HNS3_RXD_DMAC_S);
4364 
4365 	u64_stats_update_begin(&ring->syncp);
4366 	ring->stats.rx_pkts++;
4367 	ring->stats.rx_bytes += len;
4368 
4369 	if (l2_frame_type == HNS3_L2_TYPE_MULTICAST)
4370 		ring->stats.rx_multicast++;
4371 
4372 	u64_stats_update_end(&ring->syncp);
4373 
4374 	ring->tqp_vector->rx_group.total_bytes += len;
4375 
4376 	hns3_set_rx_skb_rss_type(ring, skb, le32_to_cpu(desc->rx.rss_hash),
4377 				 l234info, ol_info);
4378 	return 0;
4379 }
4380 
4381 static int hns3_handle_rx_bd(struct hns3_enet_ring *ring)
4382 {
4383 	struct sk_buff *skb = ring->skb;
4384 	struct hns3_desc_cb *desc_cb;
4385 	struct hns3_desc *desc;
4386 	unsigned int length;
4387 	u32 bd_base_info;
4388 	int ret;
4389 
4390 	desc = &ring->desc[ring->next_to_clean];
4391 	desc_cb = &ring->desc_cb[ring->next_to_clean];
4392 
4393 	prefetch(desc);
4394 
4395 	if (!skb) {
4396 		bd_base_info = le32_to_cpu(desc->rx.bd_base_info);
4397 		/* Check valid BD */
4398 		if (unlikely(!(bd_base_info & BIT(HNS3_RXD_VLD_B))))
4399 			return -ENXIO;
4400 
4401 		dma_rmb();
4402 		length = le16_to_cpu(desc->rx.size);
4403 
4404 		ring->va = desc_cb->buf + desc_cb->page_offset;
4405 
4406 		dma_sync_single_for_cpu(ring_to_dev(ring),
4407 				desc_cb->dma + desc_cb->page_offset,
4408 				hns3_buf_size(ring),
4409 				DMA_FROM_DEVICE);
4410 
4411 		/* Prefetch first cache line of first page.
4412 		 * Idea is to cache few bytes of the header of the packet.
4413 		 * Our L1 Cache line size is 64B so need to prefetch twice to make
4414 		 * it 128B. But in actual we can have greater size of caches with
4415 		 * 128B Level 1 cache lines. In such a case, single fetch would
4416 		 * suffice to cache in the relevant part of the header.
4417 		 */
4418 		net_prefetch(ring->va);
4419 
4420 		ret = hns3_alloc_skb(ring, length, ring->va);
4421 		skb = ring->skb;
4422 
4423 		if (ret < 0) /* alloc buffer fail */
4424 			return ret;
4425 		if (!(bd_base_info & BIT(HNS3_RXD_FE_B))) { /* need add frag */
4426 			ret = hns3_add_frag(ring);
4427 			if (ret)
4428 				return ret;
4429 		}
4430 	} else {
4431 		ret = hns3_add_frag(ring);
4432 		if (ret)
4433 			return ret;
4434 	}
4435 
4436 	/* As the head data may be changed when GRO enable, copy
4437 	 * the head data in after other data rx completed
4438 	 */
4439 	if (skb->len > HNS3_RX_HEAD_SIZE)
4440 		memcpy(skb->data, ring->va,
4441 		       ALIGN(ring->pull_len, sizeof(long)));
4442 
4443 	ret = hns3_handle_bdinfo(ring, skb);
4444 	if (unlikely(ret)) {
4445 		dev_kfree_skb_any(skb);
4446 		return ret;
4447 	}
4448 
4449 	skb_record_rx_queue(skb, ring->tqp->tqp_index);
4450 	return 0;
4451 }
4452 
4453 int hns3_clean_rx_ring(struct hns3_enet_ring *ring, int budget,
4454 		       void (*rx_fn)(struct hns3_enet_ring *, struct sk_buff *))
4455 {
4456 #define RCB_NOF_ALLOC_RX_BUFF_ONCE 16
4457 	int unused_count = hns3_desc_unused(ring);
4458 	bool failure = false;
4459 	int recv_pkts = 0;
4460 	int err;
4461 
4462 	unused_count -= ring->pending_buf;
4463 
4464 	while (recv_pkts < budget) {
4465 		/* Reuse or realloc buffers */
4466 		if (unused_count >= RCB_NOF_ALLOC_RX_BUFF_ONCE) {
4467 			failure = failure ||
4468 				hns3_nic_alloc_rx_buffers(ring, unused_count);
4469 			unused_count = 0;
4470 		}
4471 
4472 		/* Poll one pkt */
4473 		err = hns3_handle_rx_bd(ring);
4474 		/* Do not get FE for the packet or failed to alloc skb */
4475 		if (unlikely(!ring->skb || err == -ENXIO)) {
4476 			goto out;
4477 		} else if (likely(!err)) {
4478 			rx_fn(ring, ring->skb);
4479 			recv_pkts++;
4480 		}
4481 
4482 		unused_count += ring->pending_buf;
4483 		ring->skb = NULL;
4484 		ring->pending_buf = 0;
4485 	}
4486 
4487 out:
4488 	/* sync head pointer before exiting, since hardware will calculate
4489 	 * FBD number with head pointer
4490 	 */
4491 	if (unused_count > 0)
4492 		failure = failure ||
4493 			  hns3_nic_alloc_rx_buffers(ring, unused_count);
4494 
4495 	return failure ? budget : recv_pkts;
4496 }
4497 
4498 static void hns3_update_rx_int_coalesce(struct hns3_enet_tqp_vector *tqp_vector)
4499 {
4500 	struct hns3_enet_ring_group *rx_group = &tqp_vector->rx_group;
4501 	struct dim_sample sample = {};
4502 
4503 	if (!rx_group->coal.adapt_enable)
4504 		return;
4505 
4506 	dim_update_sample(tqp_vector->event_cnt, rx_group->total_packets,
4507 			  rx_group->total_bytes, &sample);
4508 	net_dim(&rx_group->dim, &sample);
4509 }
4510 
4511 static void hns3_update_tx_int_coalesce(struct hns3_enet_tqp_vector *tqp_vector)
4512 {
4513 	struct hns3_enet_ring_group *tx_group = &tqp_vector->tx_group;
4514 	struct dim_sample sample = {};
4515 
4516 	if (!tx_group->coal.adapt_enable)
4517 		return;
4518 
4519 	dim_update_sample(tqp_vector->event_cnt, tx_group->total_packets,
4520 			  tx_group->total_bytes, &sample);
4521 	net_dim(&tx_group->dim, &sample);
4522 }
4523 
4524 static int hns3_nic_common_poll(struct napi_struct *napi, int budget)
4525 {
4526 	struct hns3_nic_priv *priv = netdev_priv(napi->dev);
4527 	struct hns3_enet_ring *ring;
4528 	int rx_pkt_total = 0;
4529 
4530 	struct hns3_enet_tqp_vector *tqp_vector =
4531 		container_of(napi, struct hns3_enet_tqp_vector, napi);
4532 	bool clean_complete = true;
4533 	int rx_budget = budget;
4534 
4535 	if (unlikely(test_bit(HNS3_NIC_STATE_DOWN, &priv->state))) {
4536 		napi_complete(napi);
4537 		return 0;
4538 	}
4539 
4540 	/* Since the actual Tx work is minimal, we can give the Tx a larger
4541 	 * budget and be more aggressive about cleaning up the Tx descriptors.
4542 	 */
4543 	hns3_for_each_ring(ring, tqp_vector->tx_group)
4544 		hns3_clean_tx_ring(ring, budget);
4545 
4546 	/* make sure rx ring budget not smaller than 1 */
4547 	if (tqp_vector->num_tqps > 1)
4548 		rx_budget = max(budget / tqp_vector->num_tqps, 1);
4549 
4550 	hns3_for_each_ring(ring, tqp_vector->rx_group) {
4551 		int rx_cleaned = hns3_clean_rx_ring(ring, rx_budget,
4552 						    hns3_rx_skb);
4553 		if (rx_cleaned >= rx_budget)
4554 			clean_complete = false;
4555 
4556 		rx_pkt_total += rx_cleaned;
4557 	}
4558 
4559 	tqp_vector->rx_group.total_packets += rx_pkt_total;
4560 
4561 	if (!clean_complete)
4562 		return budget;
4563 
4564 	if (napi_complete(napi) &&
4565 	    likely(!test_bit(HNS3_NIC_STATE_DOWN, &priv->state))) {
4566 		hns3_update_rx_int_coalesce(tqp_vector);
4567 		hns3_update_tx_int_coalesce(tqp_vector);
4568 
4569 		hns3_mask_vector_irq(tqp_vector, 1);
4570 	}
4571 
4572 	return rx_pkt_total;
4573 }
4574 
4575 static int hns3_create_ring_chain(struct hns3_enet_tqp_vector *tqp_vector,
4576 				  struct hnae3_ring_chain_node **head,
4577 				  bool is_tx)
4578 {
4579 	u32 bit_value = is_tx ? HNAE3_RING_TYPE_TX : HNAE3_RING_TYPE_RX;
4580 	u32 field_value = is_tx ? HNAE3_RING_GL_TX : HNAE3_RING_GL_RX;
4581 	struct hnae3_ring_chain_node *cur_chain = *head;
4582 	struct pci_dev *pdev = tqp_vector->handle->pdev;
4583 	struct hnae3_ring_chain_node *chain;
4584 	struct hns3_enet_ring *ring;
4585 
4586 	ring = is_tx ? tqp_vector->tx_group.ring : tqp_vector->rx_group.ring;
4587 
4588 	if (cur_chain) {
4589 		while (cur_chain->next)
4590 			cur_chain = cur_chain->next;
4591 	}
4592 
4593 	while (ring) {
4594 		chain = devm_kzalloc_obj(&pdev->dev, *chain);
4595 		if (!chain)
4596 			return -ENOMEM;
4597 		if (cur_chain)
4598 			cur_chain->next = chain;
4599 		else
4600 			*head = chain;
4601 		chain->tqp_index = ring->tqp->tqp_index;
4602 		hnae3_set_bit(chain->flag, HNAE3_RING_TYPE_B,
4603 				bit_value);
4604 		hnae3_set_field(chain->int_gl_idx,
4605 				HNAE3_RING_GL_IDX_M,
4606 				HNAE3_RING_GL_IDX_S, field_value);
4607 
4608 		cur_chain = chain;
4609 
4610 		ring = ring->next;
4611 	}
4612 
4613 	return 0;
4614 }
4615 
4616 static struct hnae3_ring_chain_node *
4617 hns3_get_vector_ring_chain(struct hns3_enet_tqp_vector *tqp_vector)
4618 {
4619 	struct pci_dev *pdev = tqp_vector->handle->pdev;
4620 	struct hnae3_ring_chain_node *cur_chain = NULL;
4621 	struct hnae3_ring_chain_node *chain;
4622 
4623 	if (hns3_create_ring_chain(tqp_vector, &cur_chain, true))
4624 		goto err_free_chain;
4625 
4626 	if (hns3_create_ring_chain(tqp_vector, &cur_chain, false))
4627 		goto err_free_chain;
4628 
4629 	return cur_chain;
4630 
4631 err_free_chain:
4632 	while (cur_chain) {
4633 		chain = cur_chain->next;
4634 		devm_kfree(&pdev->dev, cur_chain);
4635 		cur_chain = chain;
4636 	}
4637 
4638 	return NULL;
4639 }
4640 
4641 static void hns3_free_vector_ring_chain(struct hns3_enet_tqp_vector *tqp_vector,
4642 					struct hnae3_ring_chain_node *head)
4643 {
4644 	struct pci_dev *pdev = tqp_vector->handle->pdev;
4645 	struct hnae3_ring_chain_node *chain_tmp, *chain;
4646 
4647 	chain = head;
4648 
4649 	while (chain) {
4650 		chain_tmp = chain->next;
4651 		devm_kfree(&pdev->dev, chain);
4652 		chain = chain_tmp;
4653 	}
4654 }
4655 
4656 static void hns3_add_ring_to_group(struct hns3_enet_ring_group *group,
4657 				   struct hns3_enet_ring *ring)
4658 {
4659 	ring->next = group->ring;
4660 	group->ring = ring;
4661 
4662 	group->count++;
4663 }
4664 
4665 static void hns3_nic_set_cpumask(struct hns3_nic_priv *priv)
4666 {
4667 	struct pci_dev *pdev = priv->ae_handle->pdev;
4668 	struct hns3_enet_tqp_vector *tqp_vector;
4669 	int num_vectors = priv->vector_num;
4670 	int numa_node;
4671 	int vector_i;
4672 
4673 	numa_node = dev_to_node(&pdev->dev);
4674 
4675 	for (vector_i = 0; vector_i < num_vectors; vector_i++) {
4676 		tqp_vector = &priv->tqp_vector[vector_i];
4677 		cpumask_set_cpu(cpumask_local_spread(vector_i, numa_node),
4678 				&tqp_vector->affinity_mask);
4679 	}
4680 }
4681 
4682 static void hns3_rx_dim_work(struct work_struct *work)
4683 {
4684 	struct dim *dim = container_of(work, struct dim, work);
4685 	struct hns3_enet_ring_group *group = container_of(dim,
4686 		struct hns3_enet_ring_group, dim);
4687 	struct hns3_enet_tqp_vector *tqp_vector = group->ring->tqp_vector;
4688 	struct dim_cq_moder cur_moder =
4689 		net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
4690 
4691 	hns3_set_vector_coalesce_rx_gl(group->ring->tqp_vector, cur_moder.usec);
4692 	tqp_vector->rx_group.coal.int_gl = cur_moder.usec;
4693 
4694 	if (cur_moder.pkts < tqp_vector->rx_group.coal.int_ql_max) {
4695 		hns3_set_vector_coalesce_rx_ql(tqp_vector, cur_moder.pkts);
4696 		tqp_vector->rx_group.coal.int_ql = cur_moder.pkts;
4697 	}
4698 
4699 	dim->state = DIM_START_MEASURE;
4700 }
4701 
4702 static void hns3_tx_dim_work(struct work_struct *work)
4703 {
4704 	struct dim *dim = container_of(work, struct dim, work);
4705 	struct hns3_enet_ring_group *group = container_of(dim,
4706 		struct hns3_enet_ring_group, dim);
4707 	struct hns3_enet_tqp_vector *tqp_vector = group->ring->tqp_vector;
4708 	struct dim_cq_moder cur_moder =
4709 		net_dim_get_tx_moderation(dim->mode, dim->profile_ix);
4710 
4711 	hns3_set_vector_coalesce_tx_gl(tqp_vector, cur_moder.usec);
4712 	tqp_vector->tx_group.coal.int_gl = cur_moder.usec;
4713 
4714 	if (cur_moder.pkts < tqp_vector->tx_group.coal.int_ql_max) {
4715 		hns3_set_vector_coalesce_tx_ql(tqp_vector, cur_moder.pkts);
4716 		tqp_vector->tx_group.coal.int_ql = cur_moder.pkts;
4717 	}
4718 
4719 	dim->state = DIM_START_MEASURE;
4720 }
4721 
4722 static void hns3_nic_init_dim(struct hns3_enet_tqp_vector *tqp_vector)
4723 {
4724 	INIT_WORK(&tqp_vector->rx_group.dim.work, hns3_rx_dim_work);
4725 	INIT_WORK(&tqp_vector->tx_group.dim.work, hns3_tx_dim_work);
4726 }
4727 
4728 static int hns3_nic_init_vector_data(struct hns3_nic_priv *priv)
4729 {
4730 	struct hnae3_handle *h = priv->ae_handle;
4731 	struct hns3_enet_tqp_vector *tqp_vector;
4732 	int ret;
4733 	int i;
4734 
4735 	hns3_nic_set_cpumask(priv);
4736 
4737 	for (i = 0; i < priv->vector_num; i++) {
4738 		tqp_vector = &priv->tqp_vector[i];
4739 		hns3_vector_coalesce_init_hw(tqp_vector, priv);
4740 		tqp_vector->num_tqps = 0;
4741 		hns3_nic_init_dim(tqp_vector);
4742 	}
4743 
4744 	for (i = 0; i < h->kinfo.num_tqps; i++) {
4745 		u16 vector_i = i % priv->vector_num;
4746 		u16 tqp_num = h->kinfo.num_tqps;
4747 
4748 		tqp_vector = &priv->tqp_vector[vector_i];
4749 
4750 		hns3_add_ring_to_group(&tqp_vector->tx_group,
4751 				       &priv->ring[i]);
4752 
4753 		hns3_add_ring_to_group(&tqp_vector->rx_group,
4754 				       &priv->ring[i + tqp_num]);
4755 
4756 		priv->ring[i].tqp_vector = tqp_vector;
4757 		priv->ring[i + tqp_num].tqp_vector = tqp_vector;
4758 		tqp_vector->num_tqps++;
4759 	}
4760 
4761 	for (i = 0; i < priv->vector_num; i++) {
4762 		struct hnae3_ring_chain_node *vector_ring_chain;
4763 
4764 		tqp_vector = &priv->tqp_vector[i];
4765 
4766 		tqp_vector->rx_group.total_bytes = 0;
4767 		tqp_vector->rx_group.total_packets = 0;
4768 		tqp_vector->tx_group.total_bytes = 0;
4769 		tqp_vector->tx_group.total_packets = 0;
4770 		tqp_vector->handle = h;
4771 
4772 		vector_ring_chain = hns3_get_vector_ring_chain(tqp_vector);
4773 		if (!vector_ring_chain) {
4774 			ret = -ENOMEM;
4775 			goto map_ring_fail;
4776 		}
4777 
4778 		ret = h->ae_algo->ops->map_ring_to_vector(h,
4779 			tqp_vector->vector_irq, vector_ring_chain);
4780 
4781 		hns3_free_vector_ring_chain(tqp_vector, vector_ring_chain);
4782 
4783 		if (ret)
4784 			goto map_ring_fail;
4785 
4786 		netif_napi_add(priv->netdev, &tqp_vector->napi,
4787 			       hns3_nic_common_poll);
4788 	}
4789 
4790 	return 0;
4791 
4792 map_ring_fail:
4793 	while (i--)
4794 		netif_napi_del(&priv->tqp_vector[i].napi);
4795 
4796 	return ret;
4797 }
4798 
4799 static void hns3_nic_init_coal_cfg(struct hns3_nic_priv *priv)
4800 {
4801 	struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(priv->ae_handle);
4802 	struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
4803 	struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
4804 
4805 	/* initialize the configuration for interrupt coalescing.
4806 	 * 1. GL (Interrupt Gap Limiter)
4807 	 * 2. RL (Interrupt Rate Limiter)
4808 	 * 3. QL (Interrupt Quantity Limiter)
4809 	 *
4810 	 * Default: enable interrupt coalescing self-adaptive and GL
4811 	 */
4812 	tx_coal->adapt_enable = 1;
4813 	rx_coal->adapt_enable = 1;
4814 
4815 	tx_coal->int_gl = HNS3_INT_GL_50K;
4816 	rx_coal->int_gl = HNS3_INT_GL_50K;
4817 
4818 	rx_coal->flow_level = HNS3_FLOW_LOW;
4819 	tx_coal->flow_level = HNS3_FLOW_LOW;
4820 
4821 	if (ae_dev->dev_specs.int_ql_max) {
4822 		tx_coal->int_ql = HNS3_INT_QL_DEFAULT_CFG;
4823 		rx_coal->int_ql = HNS3_INT_QL_DEFAULT_CFG;
4824 	}
4825 }
4826 
4827 static int hns3_nic_alloc_vector_data(struct hns3_nic_priv *priv)
4828 {
4829 	struct hnae3_handle *h = priv->ae_handle;
4830 	struct hns3_enet_tqp_vector *tqp_vector;
4831 	struct hnae3_vector_info *vector;
4832 	struct pci_dev *pdev = h->pdev;
4833 	u16 tqp_num = h->kinfo.num_tqps;
4834 	u16 vector_num;
4835 	int ret = 0;
4836 	u16 i;
4837 
4838 	/* RSS size, cpu online and vector_num should be the same */
4839 	/* Should consider 2p/4p later */
4840 	vector_num = min_t(u16, num_online_cpus(), tqp_num);
4841 
4842 	vector = devm_kzalloc_objs(&pdev->dev, *vector, vector_num);
4843 	if (!vector)
4844 		return -ENOMEM;
4845 
4846 	/* save the actual available vector number */
4847 	vector_num = h->ae_algo->ops->get_vector(h, vector_num, vector);
4848 
4849 	priv->vector_num = vector_num;
4850 	priv->tqp_vector = (struct hns3_enet_tqp_vector *)
4851 		devm_kzalloc_objs(&pdev->dev, *priv->tqp_vector, vector_num);
4852 	if (!priv->tqp_vector) {
4853 		ret = -ENOMEM;
4854 		goto out;
4855 	}
4856 
4857 	for (i = 0; i < priv->vector_num; i++) {
4858 		tqp_vector = &priv->tqp_vector[i];
4859 		tqp_vector->idx = i;
4860 		tqp_vector->mask_addr = vector[i].io_addr;
4861 		tqp_vector->vector_irq = vector[i].vector;
4862 		hns3_vector_coalesce_init(tqp_vector, priv);
4863 	}
4864 
4865 out:
4866 	devm_kfree(&pdev->dev, vector);
4867 	return ret;
4868 }
4869 
4870 static void hns3_clear_ring_group(struct hns3_enet_ring_group *group)
4871 {
4872 	group->ring = NULL;
4873 	group->count = 0;
4874 }
4875 
4876 static void hns3_nic_uninit_vector_data(struct hns3_nic_priv *priv)
4877 {
4878 	struct hnae3_ring_chain_node *vector_ring_chain;
4879 	struct hnae3_handle *h = priv->ae_handle;
4880 	struct hns3_enet_tqp_vector *tqp_vector;
4881 	int i;
4882 
4883 	for (i = 0; i < priv->vector_num; i++) {
4884 		tqp_vector = &priv->tqp_vector[i];
4885 
4886 		if (!tqp_vector->rx_group.ring && !tqp_vector->tx_group.ring)
4887 			continue;
4888 
4889 		/* Since the mapping can be overwritten, when fail to get the
4890 		 * chain between vector and ring, we should go on to deal with
4891 		 * the remaining options.
4892 		 */
4893 		vector_ring_chain = hns3_get_vector_ring_chain(tqp_vector);
4894 		if (!vector_ring_chain)
4895 			dev_warn(priv->dev, "failed to get ring chain\n");
4896 
4897 		h->ae_algo->ops->unmap_ring_from_vector(h,
4898 			tqp_vector->vector_irq, vector_ring_chain);
4899 
4900 		hns3_free_vector_ring_chain(tqp_vector, vector_ring_chain);
4901 
4902 		hns3_clear_ring_group(&tqp_vector->rx_group);
4903 		hns3_clear_ring_group(&tqp_vector->tx_group);
4904 		netif_napi_del(&priv->tqp_vector[i].napi);
4905 	}
4906 }
4907 
4908 static void hns3_nic_dealloc_vector_data(struct hns3_nic_priv *priv)
4909 {
4910 	struct hnae3_handle *h = priv->ae_handle;
4911 	struct pci_dev *pdev = h->pdev;
4912 	int i, ret;
4913 
4914 	for (i = 0; i < priv->vector_num; i++) {
4915 		struct hns3_enet_tqp_vector *tqp_vector;
4916 
4917 		tqp_vector = &priv->tqp_vector[i];
4918 		ret = h->ae_algo->ops->put_vector(h, tqp_vector->vector_irq);
4919 		if (ret)
4920 			return;
4921 	}
4922 
4923 	devm_kfree(&pdev->dev, priv->tqp_vector);
4924 }
4925 
4926 static void hns3_update_tx_spare_buf_config(struct hns3_nic_priv *priv)
4927 {
4928 #define HNS3_MIN_SPARE_BUF_SIZE (2 * 1024 * 1024)
4929 #define HNS3_MAX_PACKET_SIZE (64 * 1024)
4930 
4931 	struct iommu_domain *domain = iommu_get_domain_for_dev(priv->dev);
4932 	struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(priv->ae_handle);
4933 	struct hnae3_handle *handle = priv->ae_handle;
4934 
4935 	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V3)
4936 		return;
4937 
4938 	if (!(domain && iommu_is_dma_domain(domain)))
4939 		return;
4940 
4941 	priv->min_tx_copybreak = HNS3_MAX_PACKET_SIZE;
4942 	priv->min_tx_spare_buf_size = HNS3_MIN_SPARE_BUF_SIZE;
4943 
4944 	if (priv->tx_copybreak < priv->min_tx_copybreak)
4945 		priv->tx_copybreak = priv->min_tx_copybreak;
4946 	if (handle->kinfo.tx_spare_buf_size < priv->min_tx_spare_buf_size)
4947 		handle->kinfo.tx_spare_buf_size = priv->min_tx_spare_buf_size;
4948 }
4949 
4950 static void hns3_ring_get_cfg(struct hnae3_queue *q, struct hns3_nic_priv *priv,
4951 			      unsigned int ring_type)
4952 {
4953 	int queue_num = priv->ae_handle->kinfo.num_tqps;
4954 	struct hns3_enet_ring *ring;
4955 	int desc_num;
4956 
4957 	if (ring_type == HNAE3_RING_TYPE_TX) {
4958 		ring = &priv->ring[q->tqp_index];
4959 		desc_num = priv->ae_handle->kinfo.num_tx_desc;
4960 		ring->queue_index = q->tqp_index;
4961 		ring->tx_copybreak = priv->tx_copybreak;
4962 		ring->last_to_use = 0;
4963 	} else {
4964 		ring = &priv->ring[q->tqp_index + queue_num];
4965 		desc_num = priv->ae_handle->kinfo.num_rx_desc;
4966 		ring->queue_index = q->tqp_index;
4967 		ring->rx_copybreak = priv->rx_copybreak;
4968 	}
4969 
4970 	hnae3_set_bit(ring->flag, HNAE3_RING_TYPE_B, ring_type);
4971 
4972 	ring->tqp = q;
4973 	ring->desc = NULL;
4974 	ring->desc_cb = NULL;
4975 	ring->dev = priv->dev;
4976 	ring->desc_dma_addr = 0;
4977 	ring->buf_size = q->buf_size;
4978 	ring->desc_num = desc_num;
4979 	ring->next_to_use = 0;
4980 	ring->next_to_clean = 0;
4981 }
4982 
4983 static void hns3_queue_to_ring(struct hnae3_queue *tqp,
4984 			       struct hns3_nic_priv *priv)
4985 {
4986 	hns3_ring_get_cfg(tqp, priv, HNAE3_RING_TYPE_TX);
4987 	hns3_ring_get_cfg(tqp, priv, HNAE3_RING_TYPE_RX);
4988 }
4989 
4990 static int hns3_get_ring_config(struct hns3_nic_priv *priv)
4991 {
4992 	struct hnae3_handle *h = priv->ae_handle;
4993 	struct pci_dev *pdev = h->pdev;
4994 	int i;
4995 
4996 	priv->ring = devm_kzalloc(&pdev->dev,
4997 				  array3_size(h->kinfo.num_tqps,
4998 					      sizeof(*priv->ring), 2),
4999 				  GFP_KERNEL);
5000 	if (!priv->ring)
5001 		return -ENOMEM;
5002 
5003 	for (i = 0; i < h->kinfo.num_tqps; i++)
5004 		hns3_queue_to_ring(h->kinfo.tqp[i], priv);
5005 
5006 	return 0;
5007 }
5008 
5009 static void hns3_put_ring_config(struct hns3_nic_priv *priv)
5010 {
5011 	if (!priv->ring)
5012 		return;
5013 
5014 	devm_kfree(priv->dev, priv->ring);
5015 	priv->ring = NULL;
5016 }
5017 
5018 static void hns3_alloc_page_pool(struct hns3_enet_ring *ring)
5019 {
5020 	struct page_pool_params pp_params = {
5021 		.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
5022 		.order = hns3_page_order(ring),
5023 		.pool_size = ring->desc_num * hns3_buf_size(ring) /
5024 				(PAGE_SIZE << hns3_page_order(ring)),
5025 		.nid = dev_to_node(ring_to_dev(ring)),
5026 		.dev = ring_to_dev(ring),
5027 		.dma_dir = DMA_FROM_DEVICE,
5028 		.offset = 0,
5029 		.max_len = PAGE_SIZE << hns3_page_order(ring),
5030 	};
5031 
5032 	ring->page_pool = page_pool_create(&pp_params);
5033 	if (IS_ERR(ring->page_pool)) {
5034 		dev_warn(ring_to_dev(ring), "page pool creation failed: %ld\n",
5035 			 PTR_ERR(ring->page_pool));
5036 		ring->page_pool = NULL;
5037 	}
5038 }
5039 
5040 static int hns3_alloc_ring_memory(struct hns3_enet_ring *ring)
5041 {
5042 	int ret;
5043 
5044 	if (ring->desc_num <= 0 || ring->buf_size <= 0)
5045 		return -EINVAL;
5046 
5047 	ring->desc_cb = devm_kzalloc_objs(ring_to_dev(ring), ring->desc_cb[0],
5048 					  ring->desc_num);
5049 	if (!ring->desc_cb) {
5050 		ret = -ENOMEM;
5051 		goto out;
5052 	}
5053 
5054 	ret = hns3_alloc_desc(ring);
5055 	if (ret)
5056 		goto out_with_desc_cb;
5057 
5058 	if (!HNAE3_IS_TX_RING(ring)) {
5059 		if (page_pool_enabled)
5060 			hns3_alloc_page_pool(ring);
5061 
5062 		ret = hns3_alloc_ring_buffers(ring);
5063 		if (ret)
5064 			goto out_with_desc;
5065 	} else {
5066 		hns3_init_tx_spare_buffer(ring);
5067 	}
5068 
5069 	return 0;
5070 
5071 out_with_desc:
5072 	hns3_free_desc(ring);
5073 out_with_desc_cb:
5074 	devm_kfree(ring_to_dev(ring), ring->desc_cb);
5075 	ring->desc_cb = NULL;
5076 out:
5077 	return ret;
5078 }
5079 
5080 void hns3_fini_ring(struct hns3_enet_ring *ring)
5081 {
5082 	hns3_free_desc(ring);
5083 	devm_kfree(ring_to_dev(ring), ring->desc_cb);
5084 	ring->desc_cb = NULL;
5085 	ring->next_to_clean = 0;
5086 	ring->next_to_use = 0;
5087 	ring->last_to_use = 0;
5088 	ring->pending_buf = 0;
5089 	if (!HNAE3_IS_TX_RING(ring) && ring->skb) {
5090 		dev_kfree_skb_any(ring->skb);
5091 		ring->skb = NULL;
5092 	} else if (HNAE3_IS_TX_RING(ring) && ring->tx_spare) {
5093 		struct hns3_tx_spare *tx_spare = ring->tx_spare;
5094 
5095 		dma_unmap_page(ring_to_dev(ring), tx_spare->dma, tx_spare->len,
5096 			       DMA_TO_DEVICE);
5097 		free_pages((unsigned long)tx_spare->buf,
5098 			   get_order(tx_spare->len));
5099 		devm_kfree(ring_to_dev(ring), tx_spare);
5100 		ring->tx_spare = NULL;
5101 	}
5102 
5103 	if (!HNAE3_IS_TX_RING(ring) && ring->page_pool) {
5104 		page_pool_destroy(ring->page_pool);
5105 		ring->page_pool = NULL;
5106 	}
5107 }
5108 
5109 static int hns3_buf_size2type(u32 buf_size)
5110 {
5111 	int bd_size_type;
5112 
5113 	switch (buf_size) {
5114 	case 512:
5115 		bd_size_type = HNS3_BD_SIZE_512_TYPE;
5116 		break;
5117 	case 1024:
5118 		bd_size_type = HNS3_BD_SIZE_1024_TYPE;
5119 		break;
5120 	case 2048:
5121 		bd_size_type = HNS3_BD_SIZE_2048_TYPE;
5122 		break;
5123 	case 4096:
5124 		bd_size_type = HNS3_BD_SIZE_4096_TYPE;
5125 		break;
5126 	default:
5127 		bd_size_type = HNS3_BD_SIZE_2048_TYPE;
5128 	}
5129 
5130 	return bd_size_type;
5131 }
5132 
5133 static void hns3_init_ring_hw(struct hns3_enet_ring *ring)
5134 {
5135 	dma_addr_t dma = ring->desc_dma_addr;
5136 	struct hnae3_queue *q = ring->tqp;
5137 
5138 	if (!HNAE3_IS_TX_RING(ring)) {
5139 		hns3_write_dev(q, HNS3_RING_RX_RING_BASEADDR_L_REG, (u32)dma);
5140 		hns3_write_dev(q, HNS3_RING_RX_RING_BASEADDR_H_REG,
5141 			       (u32)((dma >> 31) >> 1));
5142 
5143 		hns3_write_dev(q, HNS3_RING_RX_RING_BD_LEN_REG,
5144 			       hns3_buf_size2type(ring->buf_size));
5145 		hns3_write_dev(q, HNS3_RING_RX_RING_BD_NUM_REG,
5146 			       ring->desc_num / 8 - 1);
5147 	} else {
5148 		hns3_write_dev(q, HNS3_RING_TX_RING_BASEADDR_L_REG,
5149 			       (u32)dma);
5150 		hns3_write_dev(q, HNS3_RING_TX_RING_BASEADDR_H_REG,
5151 			       (u32)((dma >> 31) >> 1));
5152 
5153 		hns3_write_dev(q, HNS3_RING_TX_RING_BD_NUM_REG,
5154 			       ring->desc_num / 8 - 1);
5155 	}
5156 }
5157 
5158 static void hns3_init_tx_ring_tc(struct hns3_nic_priv *priv)
5159 {
5160 	struct hnae3_knic_private_info *kinfo = &priv->ae_handle->kinfo;
5161 	struct hnae3_tc_info *tc_info = &kinfo->tc_info;
5162 	int i;
5163 
5164 	for (i = 0; i < tc_info->num_tc; i++) {
5165 		int j;
5166 
5167 		for (j = 0; j < tc_info->tqp_count[i]; j++) {
5168 			struct hnae3_queue *q;
5169 
5170 			q = priv->ring[tc_info->tqp_offset[i] + j].tqp;
5171 			hns3_write_dev(q, HNS3_RING_TX_RING_TC_REG, i);
5172 		}
5173 	}
5174 }
5175 
5176 int hns3_init_all_ring(struct hns3_nic_priv *priv)
5177 {
5178 	struct hnae3_handle *h = priv->ae_handle;
5179 	int ring_num = h->kinfo.num_tqps * 2;
5180 	int i, j;
5181 	int ret;
5182 
5183 	hns3_update_tx_spare_buf_config(priv);
5184 	for (i = 0; i < ring_num; i++) {
5185 		ret = hns3_alloc_ring_memory(&priv->ring[i]);
5186 		if (ret) {
5187 			dev_err(priv->dev,
5188 				"Alloc ring memory fail! ret=%d\n", ret);
5189 			goto out_when_alloc_ring_memory;
5190 		}
5191 
5192 		u64_stats_init(&priv->ring[i].syncp);
5193 		cond_resched();
5194 	}
5195 
5196 	return 0;
5197 
5198 out_when_alloc_ring_memory:
5199 	for (j = i - 1; j >= 0; j--)
5200 		hns3_fini_ring(&priv->ring[j]);
5201 
5202 	return -ENOMEM;
5203 }
5204 
5205 static void hns3_uninit_all_ring(struct hns3_nic_priv *priv)
5206 {
5207 	struct hnae3_handle *h = priv->ae_handle;
5208 	int i;
5209 
5210 	for (i = 0; i < h->kinfo.num_tqps; i++) {
5211 		hns3_fini_ring(&priv->ring[i]);
5212 		hns3_fini_ring(&priv->ring[i + h->kinfo.num_tqps]);
5213 	}
5214 }
5215 
5216 /* Set mac addr if it is configured. or leave it to the AE driver */
5217 static int hns3_init_mac_addr(struct net_device *netdev)
5218 {
5219 	struct hns3_nic_priv *priv = netdev_priv(netdev);
5220 	char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
5221 	struct hnae3_handle *h = priv->ae_handle;
5222 	u8 mac_addr_temp[ETH_ALEN] = {0};
5223 	int ret = 0;
5224 
5225 	if (h->ae_algo->ops->get_mac_addr)
5226 		h->ae_algo->ops->get_mac_addr(h, mac_addr_temp);
5227 
5228 	/* Check if the MAC address is valid, if not get a random one */
5229 	if (!is_valid_ether_addr(mac_addr_temp)) {
5230 		eth_hw_addr_random(netdev);
5231 		hnae3_format_mac_addr(format_mac_addr, netdev->dev_addr);
5232 		dev_warn(priv->dev, "using random MAC address %s\n",
5233 			 format_mac_addr);
5234 	} else if (!ether_addr_equal(netdev->dev_addr, mac_addr_temp)) {
5235 		eth_hw_addr_set(netdev, mac_addr_temp);
5236 		ether_addr_copy(netdev->perm_addr, mac_addr_temp);
5237 	} else {
5238 		return 0;
5239 	}
5240 
5241 	if (h->ae_algo->ops->set_mac_addr)
5242 		ret = h->ae_algo->ops->set_mac_addr(h, netdev->dev_addr, true);
5243 
5244 	return ret;
5245 }
5246 
5247 static int hns3_init_phy(struct net_device *netdev)
5248 {
5249 	struct hnae3_handle *h = hns3_get_handle(netdev);
5250 	int ret = 0;
5251 
5252 	if (h->ae_algo->ops->mac_connect_phy)
5253 		ret = h->ae_algo->ops->mac_connect_phy(h);
5254 
5255 	return ret;
5256 }
5257 
5258 static void hns3_uninit_phy(struct net_device *netdev)
5259 {
5260 	struct hnae3_handle *h = hns3_get_handle(netdev);
5261 
5262 	if (h->ae_algo->ops->mac_disconnect_phy)
5263 		h->ae_algo->ops->mac_disconnect_phy(h);
5264 }
5265 
5266 static int hns3_client_start(struct hnae3_handle *handle)
5267 {
5268 	if (!handle->ae_algo->ops->client_start)
5269 		return 0;
5270 
5271 	return handle->ae_algo->ops->client_start(handle);
5272 }
5273 
5274 static void hns3_client_stop(struct hnae3_handle *handle)
5275 {
5276 	if (!handle->ae_algo->ops->client_stop)
5277 		return;
5278 
5279 	handle->ae_algo->ops->client_stop(handle);
5280 }
5281 
5282 static void hns3_info_show(struct hns3_nic_priv *priv)
5283 {
5284 	struct hnae3_knic_private_info *kinfo = &priv->ae_handle->kinfo;
5285 	char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
5286 
5287 	hnae3_format_mac_addr(format_mac_addr, priv->netdev->dev_addr);
5288 	dev_info(priv->dev, "MAC address: %s\n", format_mac_addr);
5289 	dev_info(priv->dev, "Task queue pairs numbers: %u\n", kinfo->num_tqps);
5290 	dev_info(priv->dev, "RSS size: %u\n", kinfo->rss_size);
5291 	dev_info(priv->dev, "Allocated RSS size: %u\n", kinfo->req_rss_size);
5292 	dev_info(priv->dev, "RX buffer length: %u\n", kinfo->rx_buf_len);
5293 	dev_info(priv->dev, "Desc num per TX queue: %u\n", kinfo->num_tx_desc);
5294 	dev_info(priv->dev, "Desc num per RX queue: %u\n", kinfo->num_rx_desc);
5295 	dev_info(priv->dev, "Total number of enabled TCs: %u\n",
5296 		 kinfo->tc_info.num_tc);
5297 	dev_info(priv->dev, "Max mtu size: %u\n", priv->netdev->max_mtu);
5298 }
5299 
5300 static void hns3_set_cq_period_mode(struct hns3_nic_priv *priv,
5301 				    enum dim_cq_period_mode mode, bool is_tx)
5302 {
5303 	struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(priv->ae_handle);
5304 	struct hnae3_handle *handle = priv->ae_handle;
5305 	int i;
5306 
5307 	if (is_tx) {
5308 		priv->tx_cqe_mode = mode;
5309 
5310 		for (i = 0; i < priv->vector_num; i++)
5311 			priv->tqp_vector[i].tx_group.dim.mode = mode;
5312 	} else {
5313 		priv->rx_cqe_mode = mode;
5314 
5315 		for (i = 0; i < priv->vector_num; i++)
5316 			priv->tqp_vector[i].rx_group.dim.mode = mode;
5317 	}
5318 
5319 	if (hnae3_ae_dev_cq_supported(ae_dev)) {
5320 		u32 new_mode;
5321 		u64 reg;
5322 
5323 		new_mode = (mode == DIM_CQ_PERIOD_MODE_START_FROM_CQE) ?
5324 			HNS3_CQ_MODE_CQE : HNS3_CQ_MODE_EQE;
5325 		reg = is_tx ? HNS3_GL1_CQ_MODE_REG : HNS3_GL0_CQ_MODE_REG;
5326 
5327 		writel(new_mode, handle->kinfo.io_base + reg);
5328 	}
5329 }
5330 
5331 void hns3_cq_period_mode_init(struct hns3_nic_priv *priv,
5332 			      enum dim_cq_period_mode tx_mode,
5333 			      enum dim_cq_period_mode rx_mode)
5334 {
5335 	hns3_set_cq_period_mode(priv, tx_mode, true);
5336 	hns3_set_cq_period_mode(priv, rx_mode, false);
5337 }
5338 
5339 static void hns3_state_init(struct hnae3_handle *handle)
5340 {
5341 	struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle);
5342 	struct net_device *netdev = handle->kinfo.netdev;
5343 	struct hns3_nic_priv *priv = netdev_priv(netdev);
5344 
5345 	set_bit(HNS3_NIC_STATE_INITED, &priv->state);
5346 
5347 	if (test_bit(HNAE3_DEV_SUPPORT_TX_PUSH_B, ae_dev->caps))
5348 		set_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state);
5349 
5350 	if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
5351 		set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->supported_pflags);
5352 
5353 	if (test_bit(HNAE3_DEV_SUPPORT_HW_TX_CSUM_B, ae_dev->caps))
5354 		set_bit(HNS3_NIC_STATE_HW_TX_CSUM_ENABLE, &priv->state);
5355 
5356 	if (hnae3_ae_dev_rxd_adv_layout_supported(ae_dev))
5357 		set_bit(HNS3_NIC_STATE_RXD_ADV_LAYOUT_ENABLE, &priv->state);
5358 }
5359 
5360 static void hns3_state_uninit(struct hnae3_handle *handle)
5361 {
5362 	struct hns3_nic_priv *priv  = handle->priv;
5363 
5364 	clear_bit(HNS3_NIC_STATE_INITED, &priv->state);
5365 }
5366 
5367 static int hns3_client_init(struct hnae3_handle *handle)
5368 {
5369 	struct pci_dev *pdev = handle->pdev;
5370 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
5371 	u16 alloc_tqps, max_rss_size;
5372 	struct hns3_nic_priv *priv;
5373 	struct net_device *netdev;
5374 	int ret;
5375 
5376 	ae_dev->handle = handle;
5377 
5378 	handle->ae_algo->ops->get_tqps_and_rss_info(handle, &alloc_tqps,
5379 						    &max_rss_size);
5380 	netdev = alloc_etherdev_mq(sizeof(struct hns3_nic_priv), alloc_tqps);
5381 	if (!netdev)
5382 		return -ENOMEM;
5383 
5384 	priv = netdev_priv(netdev);
5385 	priv->dev = &pdev->dev;
5386 	priv->netdev = netdev;
5387 	priv->ae_handle = handle;
5388 	priv->tx_timeout_count = 0;
5389 	priv->max_non_tso_bd_num = ae_dev->dev_specs.max_non_tso_bd_num;
5390 	priv->min_tx_copybreak = 0;
5391 	priv->min_tx_spare_buf_size = 0;
5392 	set_bit(HNS3_NIC_STATE_DOWN, &priv->state);
5393 
5394 	handle->msg_enable = netif_msg_init(debug, DEFAULT_MSG_LEVEL);
5395 
5396 	handle->kinfo.netdev = netdev;
5397 	handle->priv = (void *)priv;
5398 
5399 	hns3_init_mac_addr(netdev);
5400 
5401 	hns3_set_default_feature(netdev);
5402 
5403 	netdev->watchdog_timeo = HNS3_TX_TIMEOUT;
5404 	netdev->priv_flags |= IFF_UNICAST_FLT;
5405 	netdev->netdev_ops = &hns3_nic_netdev_ops;
5406 	SET_NETDEV_DEV(netdev, &pdev->dev);
5407 	hns3_ethtool_set_ops(netdev);
5408 
5409 	/* Carrier off reporting is important to ethtool even BEFORE open */
5410 	netif_carrier_off(netdev);
5411 
5412 	ret = hns3_get_ring_config(priv);
5413 	if (ret) {
5414 		ret = -ENOMEM;
5415 		goto out_get_ring_cfg;
5416 	}
5417 
5418 	hns3_nic_init_coal_cfg(priv);
5419 
5420 	ret = hns3_nic_alloc_vector_data(priv);
5421 	if (ret) {
5422 		ret = -ENOMEM;
5423 		goto out_alloc_vector_data;
5424 	}
5425 
5426 	ret = hns3_nic_init_vector_data(priv);
5427 	if (ret) {
5428 		ret = -ENOMEM;
5429 		goto out_init_vector_data;
5430 	}
5431 
5432 	ret = hns3_init_all_ring(priv);
5433 	if (ret) {
5434 		ret = -ENOMEM;
5435 		goto out_init_ring;
5436 	}
5437 
5438 	hns3_cq_period_mode_init(priv, DIM_CQ_PERIOD_MODE_START_FROM_EQE,
5439 				 DIM_CQ_PERIOD_MODE_START_FROM_EQE);
5440 
5441 	ret = hns3_init_phy(netdev);
5442 	if (ret)
5443 		goto out_init_phy;
5444 
5445 	/* the device can work without cpu rmap, only aRFS needs it */
5446 	ret = hns3_set_rx_cpu_rmap(netdev);
5447 	if (ret)
5448 		dev_warn(priv->dev, "set rx cpu rmap fail, ret=%d\n", ret);
5449 
5450 	ret = hns3_nic_init_irq(priv);
5451 	if (ret) {
5452 		dev_err(priv->dev, "init irq failed! ret=%d\n", ret);
5453 		hns3_free_rx_cpu_rmap(netdev);
5454 		goto out_init_irq_fail;
5455 	}
5456 
5457 	ret = hns3_client_start(handle);
5458 	if (ret) {
5459 		dev_err(priv->dev, "hns3_client_start fail! ret=%d\n", ret);
5460 		goto out_client_start;
5461 	}
5462 
5463 	hns3_dcbnl_setup(handle);
5464 
5465 	ret = hns3_dbg_init(handle);
5466 	if (ret) {
5467 		dev_err(priv->dev, "failed to init debugfs, ret = %d\n",
5468 			ret);
5469 		goto out_client_start;
5470 	}
5471 
5472 	netdev->max_mtu = HNS3_MAX_MTU(ae_dev->dev_specs.max_frm_size);
5473 
5474 	hns3_state_init(handle);
5475 
5476 	ret = register_netdev(netdev);
5477 	if (ret) {
5478 		dev_err(priv->dev, "probe register netdev fail!\n");
5479 		goto out_reg_netdev_fail;
5480 	}
5481 
5482 	if (netif_msg_drv(handle))
5483 		hns3_info_show(priv);
5484 
5485 	return ret;
5486 
5487 out_reg_netdev_fail:
5488 	hns3_state_uninit(handle);
5489 	hns3_dbg_uninit(handle);
5490 	hns3_client_stop(handle);
5491 out_client_start:
5492 	hns3_free_rx_cpu_rmap(netdev);
5493 	hns3_nic_uninit_irq(priv);
5494 out_init_irq_fail:
5495 	hns3_uninit_phy(netdev);
5496 out_init_phy:
5497 	hns3_uninit_all_ring(priv);
5498 out_init_ring:
5499 	hns3_nic_uninit_vector_data(priv);
5500 out_init_vector_data:
5501 	hns3_nic_dealloc_vector_data(priv);
5502 out_alloc_vector_data:
5503 	priv->ring = NULL;
5504 out_get_ring_cfg:
5505 	priv->ae_handle = NULL;
5506 	free_netdev(netdev);
5507 	return ret;
5508 }
5509 
5510 static void hns3_client_uninit(struct hnae3_handle *handle, bool reset)
5511 {
5512 	struct net_device *netdev = handle->kinfo.netdev;
5513 	struct hns3_nic_priv *priv = netdev_priv(netdev);
5514 
5515 	if (netdev->reg_state != NETREG_UNINITIALIZED)
5516 		unregister_netdev(netdev);
5517 
5518 	hns3_client_stop(handle);
5519 
5520 	hns3_uninit_phy(netdev);
5521 
5522 	if (!test_and_clear_bit(HNS3_NIC_STATE_INITED, &priv->state)) {
5523 		netdev_warn(netdev, "already uninitialized\n");
5524 		goto out_netdev_free;
5525 	}
5526 
5527 	hns3_free_rx_cpu_rmap(netdev);
5528 
5529 	hns3_nic_uninit_irq(priv);
5530 
5531 	hns3_clear_all_ring(handle, true);
5532 
5533 	hns3_nic_uninit_vector_data(priv);
5534 
5535 	hns3_nic_dealloc_vector_data(priv);
5536 
5537 	hns3_uninit_all_ring(priv);
5538 
5539 	hns3_put_ring_config(priv);
5540 
5541 out_netdev_free:
5542 	hns3_dbg_uninit(handle);
5543 	free_netdev(netdev);
5544 }
5545 
5546 static void hns3_link_status_change(struct hnae3_handle *handle, bool linkup)
5547 {
5548 	struct net_device *netdev = handle->kinfo.netdev;
5549 
5550 	if (!netdev)
5551 		return;
5552 
5553 	if (linkup) {
5554 		netif_tx_wake_all_queues(netdev);
5555 		netif_carrier_on(netdev);
5556 		if (netif_msg_link(handle))
5557 			netdev_info(netdev, "link up\n");
5558 	} else {
5559 		netif_carrier_off(netdev);
5560 		netif_tx_stop_all_queues(netdev);
5561 		if (netif_msg_link(handle))
5562 			netdev_info(netdev, "link down\n");
5563 	}
5564 }
5565 
5566 static void hns3_clear_tx_ring(struct hns3_enet_ring *ring)
5567 {
5568 	while (ring->next_to_clean != ring->next_to_use) {
5569 		ring->desc[ring->next_to_clean].tx.bdtp_fe_sc_vld_ra_ri = 0;
5570 		hns3_free_buffer_detach(ring, ring->next_to_clean, 0);
5571 		ring_ptr_move_fw(ring, next_to_clean);
5572 	}
5573 
5574 	ring->pending_buf = 0;
5575 }
5576 
5577 static int hns3_clear_rx_ring(struct hns3_enet_ring *ring)
5578 {
5579 	struct hns3_desc_cb res_cbs;
5580 	int ret;
5581 
5582 	while (ring->next_to_use != ring->next_to_clean) {
5583 		/* When a buffer is not reused, it's memory has been
5584 		 * freed in hns3_handle_rx_bd or will be freed by
5585 		 * stack, so we need to replace the buffer here.
5586 		 */
5587 		if (!ring->desc_cb[ring->next_to_use].reuse_flag) {
5588 			ret = hns3_alloc_and_map_buffer(ring, &res_cbs);
5589 			if (ret) {
5590 				hns3_ring_stats_update(ring, sw_err_cnt);
5591 				/* if alloc new buffer fail, exit directly
5592 				 * and reclear in up flow.
5593 				 */
5594 				netdev_warn(ring_to_netdev(ring),
5595 					    "reserve buffer map failed, ret = %d\n",
5596 					    ret);
5597 				return ret;
5598 			}
5599 			hns3_replace_buffer(ring, ring->next_to_use, &res_cbs);
5600 		}
5601 		ring_ptr_move_fw(ring, next_to_use);
5602 	}
5603 
5604 	/* Free the pending skb in rx ring */
5605 	if (ring->skb) {
5606 		dev_kfree_skb_any(ring->skb);
5607 		ring->skb = NULL;
5608 		ring->pending_buf = 0;
5609 	}
5610 
5611 	return 0;
5612 }
5613 
5614 static void hns3_force_clear_rx_ring(struct hns3_enet_ring *ring)
5615 {
5616 	while (ring->next_to_use != ring->next_to_clean) {
5617 		/* When a buffer is not reused, it's memory has been
5618 		 * freed in hns3_handle_rx_bd or will be freed by
5619 		 * stack, so only need to unmap the buffer here.
5620 		 */
5621 		if (!ring->desc_cb[ring->next_to_use].reuse_flag) {
5622 			hns3_unmap_buffer(ring,
5623 					  &ring->desc_cb[ring->next_to_use]);
5624 			ring->desc_cb[ring->next_to_use].dma = 0;
5625 		}
5626 
5627 		ring_ptr_move_fw(ring, next_to_use);
5628 	}
5629 }
5630 
5631 static void hns3_clear_all_ring(struct hnae3_handle *h, bool force)
5632 {
5633 	struct net_device *ndev = h->kinfo.netdev;
5634 	struct hns3_nic_priv *priv = netdev_priv(ndev);
5635 	u32 i;
5636 
5637 	for (i = 0; i < h->kinfo.num_tqps; i++) {
5638 		struct hns3_enet_ring *ring;
5639 
5640 		ring = &priv->ring[i];
5641 		hns3_clear_tx_ring(ring);
5642 
5643 		ring = &priv->ring[i + h->kinfo.num_tqps];
5644 		/* Continue to clear other rings even if clearing some
5645 		 * rings failed.
5646 		 */
5647 		if (force)
5648 			hns3_force_clear_rx_ring(ring);
5649 		else
5650 			hns3_clear_rx_ring(ring);
5651 	}
5652 }
5653 
5654 int hns3_nic_reset_all_ring(struct hnae3_handle *h)
5655 {
5656 	struct net_device *ndev = h->kinfo.netdev;
5657 	struct hns3_nic_priv *priv = netdev_priv(ndev);
5658 	struct hns3_enet_ring *rx_ring;
5659 	int i, j;
5660 	int ret;
5661 
5662 	ret = h->ae_algo->ops->reset_queue(h);
5663 	if (ret)
5664 		return ret;
5665 
5666 	for (i = 0; i < h->kinfo.num_tqps; i++) {
5667 		hns3_init_ring_hw(&priv->ring[i]);
5668 
5669 		/* We need to clear tx ring here because self test will
5670 		 * use the ring and will not run down before up
5671 		 */
5672 		hns3_clear_tx_ring(&priv->ring[i]);
5673 		priv->ring[i].next_to_clean = 0;
5674 		priv->ring[i].next_to_use = 0;
5675 		priv->ring[i].last_to_use = 0;
5676 
5677 		rx_ring = &priv->ring[i + h->kinfo.num_tqps];
5678 		hns3_init_ring_hw(rx_ring);
5679 		ret = hns3_clear_rx_ring(rx_ring);
5680 		if (ret)
5681 			return ret;
5682 
5683 		/* We can not know the hardware head and tail when this
5684 		 * function is called in reset flow, so we reuse all desc.
5685 		 */
5686 		for (j = 0; j < rx_ring->desc_num; j++)
5687 			hns3_reuse_buffer(rx_ring, j);
5688 
5689 		rx_ring->next_to_clean = 0;
5690 		rx_ring->next_to_use = 0;
5691 	}
5692 
5693 	hns3_init_tx_ring_tc(priv);
5694 
5695 	return 0;
5696 }
5697 
5698 static int hns3_reset_notify_down_enet(struct hnae3_handle *handle)
5699 {
5700 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
5701 	struct net_device *ndev = kinfo->netdev;
5702 	struct hns3_nic_priv *priv = netdev_priv(ndev);
5703 
5704 	if (test_and_set_bit(HNS3_NIC_STATE_RESETTING, &priv->state))
5705 		return 0;
5706 
5707 	if (!netif_running(ndev))
5708 		return 0;
5709 
5710 	return hns3_nic_net_stop(ndev);
5711 }
5712 
5713 static int hns3_reset_notify_up_enet(struct hnae3_handle *handle)
5714 {
5715 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
5716 	struct hns3_nic_priv *priv = netdev_priv(kinfo->netdev);
5717 	int ret = 0;
5718 
5719 	if (!test_bit(HNS3_NIC_STATE_INITED, &priv->state)) {
5720 		netdev_err(kinfo->netdev, "device is not initialized yet\n");
5721 		return -EFAULT;
5722 	}
5723 
5724 	clear_bit(HNS3_NIC_STATE_RESETTING, &priv->state);
5725 
5726 	if (netif_running(kinfo->netdev)) {
5727 		ret = hns3_nic_net_open(kinfo->netdev);
5728 		if (ret) {
5729 			set_bit(HNS3_NIC_STATE_RESETTING, &priv->state);
5730 			netdev_err(kinfo->netdev,
5731 				   "net up fail, ret=%d!\n", ret);
5732 			return ret;
5733 		}
5734 	}
5735 
5736 	return ret;
5737 }
5738 
5739 static int hns3_reset_notify_init_enet(struct hnae3_handle *handle)
5740 {
5741 	struct net_device *netdev = handle->kinfo.netdev;
5742 	struct hns3_nic_priv *priv = netdev_priv(netdev);
5743 	int ret;
5744 
5745 	/* Carrier off reporting is important to ethtool even BEFORE open */
5746 	netif_carrier_off(netdev);
5747 
5748 	ret = hns3_get_ring_config(priv);
5749 	if (ret)
5750 		return ret;
5751 
5752 	ret = hns3_nic_alloc_vector_data(priv);
5753 	if (ret)
5754 		goto err_put_ring;
5755 
5756 	ret = hns3_nic_init_vector_data(priv);
5757 	if (ret)
5758 		goto err_dealloc_vector;
5759 
5760 	ret = hns3_init_all_ring(priv);
5761 	if (ret)
5762 		goto err_uninit_vector;
5763 
5764 	hns3_cq_period_mode_init(priv, priv->tx_cqe_mode, priv->rx_cqe_mode);
5765 
5766 	/* the device can work without cpu rmap, only aRFS needs it */
5767 	ret = hns3_set_rx_cpu_rmap(netdev);
5768 	if (ret)
5769 		dev_warn(priv->dev, "set rx cpu rmap fail, ret=%d\n", ret);
5770 
5771 	ret = hns3_nic_init_irq(priv);
5772 	if (ret) {
5773 		dev_err(priv->dev, "init irq failed! ret=%d\n", ret);
5774 		hns3_free_rx_cpu_rmap(netdev);
5775 		goto err_init_irq_fail;
5776 	}
5777 
5778 	if (!hns3_is_phys_func(handle->pdev))
5779 		hns3_init_mac_addr(netdev);
5780 
5781 	ret = hns3_client_start(handle);
5782 	if (ret) {
5783 		dev_err(priv->dev, "hns3_client_start fail! ret=%d\n", ret);
5784 		goto err_client_start_fail;
5785 	}
5786 
5787 	set_bit(HNS3_NIC_STATE_INITED, &priv->state);
5788 
5789 	return ret;
5790 
5791 err_client_start_fail:
5792 	hns3_free_rx_cpu_rmap(netdev);
5793 	hns3_nic_uninit_irq(priv);
5794 err_init_irq_fail:
5795 	hns3_uninit_all_ring(priv);
5796 err_uninit_vector:
5797 	hns3_nic_uninit_vector_data(priv);
5798 err_dealloc_vector:
5799 	hns3_nic_dealloc_vector_data(priv);
5800 err_put_ring:
5801 	hns3_put_ring_config(priv);
5802 
5803 	return ret;
5804 }
5805 
5806 static int hns3_reset_notify_uninit_enet(struct hnae3_handle *handle)
5807 {
5808 	struct net_device *netdev = handle->kinfo.netdev;
5809 	struct hns3_nic_priv *priv = netdev_priv(netdev);
5810 
5811 	if (!test_bit(HNS3_NIC_STATE_DOWN, &priv->state))
5812 		hns3_nic_net_stop(netdev);
5813 
5814 	if (!test_and_clear_bit(HNS3_NIC_STATE_INITED, &priv->state)) {
5815 		netdev_warn(netdev, "already uninitialized\n");
5816 		return 0;
5817 	}
5818 
5819 	hns3_free_rx_cpu_rmap(netdev);
5820 	hns3_nic_uninit_irq(priv);
5821 	hns3_clear_all_ring(handle, true);
5822 	hns3_reset_tx_queue(priv->ae_handle);
5823 
5824 	hns3_nic_uninit_vector_data(priv);
5825 
5826 	hns3_nic_dealloc_vector_data(priv);
5827 
5828 	hns3_uninit_all_ring(priv);
5829 
5830 	hns3_put_ring_config(priv);
5831 
5832 	return 0;
5833 }
5834 
5835 int hns3_reset_notify(struct hnae3_handle *handle,
5836 		      enum hnae3_reset_notify_type type)
5837 {
5838 	int ret = 0;
5839 
5840 	switch (type) {
5841 	case HNAE3_UP_CLIENT:
5842 		ret = hns3_reset_notify_up_enet(handle);
5843 		break;
5844 	case HNAE3_DOWN_CLIENT:
5845 		ret = hns3_reset_notify_down_enet(handle);
5846 		break;
5847 	case HNAE3_INIT_CLIENT:
5848 		ret = hns3_reset_notify_init_enet(handle);
5849 		break;
5850 	case HNAE3_UNINIT_CLIENT:
5851 		ret = hns3_reset_notify_uninit_enet(handle);
5852 		break;
5853 	default:
5854 		break;
5855 	}
5856 
5857 	return ret;
5858 }
5859 
5860 static int hns3_change_channels(struct hnae3_handle *handle, u32 new_tqp_num,
5861 				bool rxfh_configured)
5862 {
5863 	int ret;
5864 
5865 	ret = handle->ae_algo->ops->set_channels(handle, new_tqp_num,
5866 						 rxfh_configured);
5867 	if (ret) {
5868 		dev_err(&handle->pdev->dev,
5869 			"Change tqp num(%u) fail.\n", new_tqp_num);
5870 		return ret;
5871 	}
5872 
5873 	ret = hns3_reset_notify(handle, HNAE3_INIT_CLIENT);
5874 	if (ret)
5875 		return ret;
5876 
5877 	ret =  hns3_reset_notify(handle, HNAE3_UP_CLIENT);
5878 	if (ret)
5879 		hns3_reset_notify(handle, HNAE3_UNINIT_CLIENT);
5880 
5881 	return ret;
5882 }
5883 
5884 int hns3_set_channels(struct net_device *netdev,
5885 		      struct ethtool_channels *ch)
5886 {
5887 	struct hnae3_handle *h = hns3_get_handle(netdev);
5888 	struct hnae3_knic_private_info *kinfo = &h->kinfo;
5889 	bool rxfh_configured = netif_is_rxfh_configured(netdev);
5890 	u32 new_tqp_num = ch->combined_count;
5891 	u16 org_tqp_num;
5892 	int ret;
5893 
5894 	if (hns3_nic_resetting(netdev))
5895 		return -EBUSY;
5896 
5897 	if (ch->rx_count || ch->tx_count)
5898 		return -EINVAL;
5899 
5900 	if (kinfo->tc_info.mqprio_active) {
5901 		dev_err(&netdev->dev,
5902 			"it's not allowed to set channels via ethtool when MQPRIO mode is on\n");
5903 		return -EINVAL;
5904 	}
5905 
5906 	if (new_tqp_num > hns3_get_max_available_channels(h) ||
5907 	    new_tqp_num < 1) {
5908 		dev_err(&netdev->dev,
5909 			"Change tqps fail, the tqp range is from 1 to %u",
5910 			hns3_get_max_available_channels(h));
5911 		return -EINVAL;
5912 	}
5913 
5914 	if (kinfo->rss_size == new_tqp_num)
5915 		return 0;
5916 
5917 	netif_dbg(h, drv, netdev,
5918 		  "set channels: tqp_num=%u, rxfh=%d\n",
5919 		  new_tqp_num, rxfh_configured);
5920 
5921 	ret = hns3_reset_notify(h, HNAE3_DOWN_CLIENT);
5922 	if (ret)
5923 		return ret;
5924 
5925 	ret = hns3_reset_notify(h, HNAE3_UNINIT_CLIENT);
5926 	if (ret)
5927 		return ret;
5928 
5929 	org_tqp_num = h->kinfo.num_tqps;
5930 	ret = hns3_change_channels(h, new_tqp_num, rxfh_configured);
5931 	if (ret) {
5932 		int ret1;
5933 
5934 		netdev_warn(netdev,
5935 			    "Change channels fail, revert to old value\n");
5936 		ret1 = hns3_change_channels(h, org_tqp_num, rxfh_configured);
5937 		if (ret1) {
5938 			netdev_err(netdev,
5939 				   "revert to old channel fail\n");
5940 			return ret1;
5941 		}
5942 
5943 		return ret;
5944 	}
5945 
5946 	return 0;
5947 }
5948 
5949 void hns3_external_lb_prepare(struct net_device *ndev, bool if_running)
5950 {
5951 	struct hns3_nic_priv *priv = netdev_priv(ndev);
5952 
5953 	if (!if_running)
5954 		return;
5955 
5956 	if (test_and_set_bit(HNS3_NIC_STATE_DOWN, &priv->state))
5957 		return;
5958 
5959 	netif_carrier_off(ndev);
5960 	netif_tx_disable(ndev);
5961 
5962 	hns3_disable_irqs_and_tqps(ndev);
5963 
5964 	/* delay ring buffer clearing to hns3_reset_notify_uninit_enet
5965 	 * during reset process, because driver may not be able
5966 	 * to disable the ring through firmware when downing the netdev.
5967 	 */
5968 	if (!hns3_nic_resetting(ndev))
5969 		hns3_nic_reset_all_ring(priv->ae_handle);
5970 
5971 	hns3_reset_tx_queue(priv->ae_handle);
5972 }
5973 
5974 void hns3_external_lb_restore(struct net_device *ndev, bool if_running)
5975 {
5976 	struct hns3_nic_priv *priv = netdev_priv(ndev);
5977 	struct hnae3_handle *h = priv->ae_handle;
5978 
5979 	if (!if_running)
5980 		return;
5981 
5982 	if (hns3_nic_resetting(ndev))
5983 		return;
5984 
5985 	if (!test_bit(HNS3_NIC_STATE_DOWN, &priv->state))
5986 		return;
5987 
5988 	if (hns3_nic_reset_all_ring(priv->ae_handle))
5989 		return;
5990 
5991 	clear_bit(HNS3_NIC_STATE_DOWN, &priv->state);
5992 
5993 	hns3_enable_irqs_and_tqps(ndev);
5994 
5995 	netif_tx_wake_all_queues(ndev);
5996 
5997 	if (h->ae_algo->ops->get_status(h))
5998 		netif_carrier_on(ndev);
5999 }
6000 
6001 static const struct hns3_hw_error_info hns3_hw_err[] = {
6002 	{ .type = HNAE3_PPU_POISON_ERROR,
6003 	  .msg = "PPU poison" },
6004 	{ .type = HNAE3_CMDQ_ECC_ERROR,
6005 	  .msg = "IMP CMDQ error" },
6006 	{ .type = HNAE3_IMP_RD_POISON_ERROR,
6007 	  .msg = "IMP RD poison" },
6008 	{ .type = HNAE3_ROCEE_AXI_RESP_ERROR,
6009 	  .msg = "ROCEE AXI RESP error" },
6010 };
6011 
6012 static void hns3_process_hw_error(struct hnae3_handle *handle,
6013 				  enum hnae3_hw_error_type type)
6014 {
6015 	u32 i;
6016 
6017 	for (i = 0; i < ARRAY_SIZE(hns3_hw_err); i++) {
6018 		if (hns3_hw_err[i].type == type) {
6019 			dev_err(&handle->pdev->dev, "Detected %s!\n",
6020 				hns3_hw_err[i].msg);
6021 			break;
6022 		}
6023 	}
6024 }
6025 
6026 static const struct hnae3_client_ops client_ops = {
6027 	.init_instance = hns3_client_init,
6028 	.uninit_instance = hns3_client_uninit,
6029 	.link_status_change = hns3_link_status_change,
6030 	.reset_notify = hns3_reset_notify,
6031 	.process_hw_error = hns3_process_hw_error,
6032 };
6033 
6034 /* hns3_init_module - Driver registration routine
6035  * hns3_init_module is the first routine called when the driver is
6036  * loaded. All it does is register with the PCI subsystem.
6037  */
6038 static int __init hns3_init_module(void)
6039 {
6040 	int ret;
6041 
6042 	pr_debug("%s: %s - version\n", hns3_driver_name, hns3_driver_string);
6043 	pr_debug("%s: %s\n", hns3_driver_name, hns3_copyright);
6044 
6045 	client.type = HNAE3_CLIENT_KNIC;
6046 	snprintf(client.name, HNAE3_CLIENT_NAME_LENGTH, "%s",
6047 		 hns3_driver_name);
6048 
6049 	client.ops = &client_ops;
6050 
6051 	INIT_LIST_HEAD(&client.node);
6052 
6053 	hns3_dbg_register_debugfs(hns3_driver_name);
6054 
6055 	ret = hnae3_register_client(&client);
6056 	if (ret)
6057 		goto err_reg_client;
6058 
6059 	ret = pci_register_driver(&hns3_driver);
6060 	if (ret)
6061 		goto err_reg_driver;
6062 
6063 	return ret;
6064 
6065 err_reg_driver:
6066 	hnae3_unregister_client(&client);
6067 err_reg_client:
6068 	hns3_dbg_unregister_debugfs();
6069 	return ret;
6070 }
6071 module_init(hns3_init_module);
6072 
6073 /* hns3_exit_module - Driver exit cleanup routine
6074  * hns3_exit_module is called just before the driver is removed
6075  * from memory.
6076  */
6077 static void __exit hns3_exit_module(void)
6078 {
6079 	hnae3_acquire_unload_lock();
6080 	pci_unregister_driver(&hns3_driver);
6081 	hnae3_unregister_client(&client);
6082 	hns3_dbg_unregister_debugfs();
6083 	hnae3_release_unload_lock();
6084 }
6085 module_exit(hns3_exit_module);
6086 
6087 MODULE_DESCRIPTION("HNS3: Hisilicon Ethernet Driver");
6088 MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
6089 MODULE_LICENSE("GPL");
6090 MODULE_ALIAS("pci:hns-nic");
6091