blob: 565d85db30dfe2838b8a3e917b04ad11f2452950 [file] [log] [blame]
Salil76ad4f02017-08-02 16:59:45 +01001/*
2 * Copyright (c) 2016~2017 Hisilicon Limited.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10#include <linux/dma-mapping.h>
11#include <linux/etherdevice.h>
12#include <linux/interrupt.h>
13#include <linux/if_vlan.h>
14#include <linux/ip.h>
15#include <linux/ipv6.h>
16#include <linux/module.h>
17#include <linux/pci.h>
18#include <linux/skbuff.h>
19#include <linux/sctp.h>
20#include <linux/vermagic.h>
21#include <net/gre.h>
Yunsheng Lin30d240d2017-10-17 14:51:30 +080022#include <net/pkt_cls.h>
Salil76ad4f02017-08-02 16:59:45 +010023#include <net/vxlan.h>
24
25#include "hnae3.h"
26#include "hns3_enet.h"
27
Yunsheng Lin1db9b1b2017-10-09 15:44:01 +080028static const char hns3_driver_name[] = "hns3";
Salil76ad4f02017-08-02 16:59:45 +010029const char hns3_driver_version[] = VERMAGIC_STRING;
30static const char hns3_driver_string[] =
31 "Hisilicon Ethernet Network Driver for Hip08 Family";
32static const char hns3_copyright[] = "Copyright (c) 2017 Huawei Corporation.";
33static struct hnae3_client client;
34
35/* hns3_pci_tbl - PCI Device ID Table
36 *
37 * Last entry must be all 0s
38 *
39 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
40 * Class, Class Mask, private data (not used) }
41 */
42static const struct pci_device_id hns3_pci_tbl[] = {
43 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_GE), 0},
44 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE), 0},
Yunsheng Line92a0842017-09-20 18:52:50 +080045 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA),
Yunsheng Lin2daf4a62017-09-20 18:52:51 +080046 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
Yunsheng Line92a0842017-09-20 18:52:50 +080047 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA_MACSEC),
Yunsheng Lin2daf4a62017-09-20 18:52:51 +080048 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
Yunsheng Line92a0842017-09-20 18:52:50 +080049 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA),
Yunsheng Lin2daf4a62017-09-20 18:52:51 +080050 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
Yunsheng Line92a0842017-09-20 18:52:50 +080051 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA_MACSEC),
Yunsheng Lin2daf4a62017-09-20 18:52:51 +080052 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
Yunsheng Line92a0842017-09-20 18:52:50 +080053 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_MACSEC),
Yunsheng Lin2daf4a62017-09-20 18:52:51 +080054 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
Salil Mehta424eb832017-12-14 18:03:06 +000055 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_VF), 0},
56 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_DCB_PFC_VF), 0},
Salil76ad4f02017-08-02 16:59:45 +010057 /* required last entry */
58 {0, }
59};
60MODULE_DEVICE_TABLE(pci, hns3_pci_tbl);
61
62static irqreturn_t hns3_irq_handle(int irq, void *dev)
63{
64 struct hns3_enet_tqp_vector *tqp_vector = dev;
65
66 napi_schedule(&tqp_vector->napi);
67
68 return IRQ_HANDLED;
69}
70
71static void hns3_nic_uninit_irq(struct hns3_nic_priv *priv)
72{
73 struct hns3_enet_tqp_vector *tqp_vectors;
74 unsigned int i;
75
76 for (i = 0; i < priv->vector_num; i++) {
77 tqp_vectors = &priv->tqp_vector[i];
78
79 if (tqp_vectors->irq_init_flag != HNS3_VECTOR_INITED)
80 continue;
81
82 /* release the irq resource */
83 free_irq(tqp_vectors->vector_irq, tqp_vectors);
84 tqp_vectors->irq_init_flag = HNS3_VECTOR_NOT_INITED;
85 }
86}
87
88static int hns3_nic_init_irq(struct hns3_nic_priv *priv)
89{
90 struct hns3_enet_tqp_vector *tqp_vectors;
91 int txrx_int_idx = 0;
92 int rx_int_idx = 0;
93 int tx_int_idx = 0;
94 unsigned int i;
95 int ret;
96
97 for (i = 0; i < priv->vector_num; i++) {
98 tqp_vectors = &priv->tqp_vector[i];
99
100 if (tqp_vectors->irq_init_flag == HNS3_VECTOR_INITED)
101 continue;
102
103 if (tqp_vectors->tx_group.ring && tqp_vectors->rx_group.ring) {
104 snprintf(tqp_vectors->name, HNAE3_INT_NAME_LEN - 1,
105 "%s-%s-%d", priv->netdev->name, "TxRx",
106 txrx_int_idx++);
107 txrx_int_idx++;
108 } else if (tqp_vectors->rx_group.ring) {
109 snprintf(tqp_vectors->name, HNAE3_INT_NAME_LEN - 1,
110 "%s-%s-%d", priv->netdev->name, "Rx",
111 rx_int_idx++);
112 } else if (tqp_vectors->tx_group.ring) {
113 snprintf(tqp_vectors->name, HNAE3_INT_NAME_LEN - 1,
114 "%s-%s-%d", priv->netdev->name, "Tx",
115 tx_int_idx++);
116 } else {
117 /* Skip this unused q_vector */
118 continue;
119 }
120
121 tqp_vectors->name[HNAE3_INT_NAME_LEN - 1] = '\0';
122
123 ret = request_irq(tqp_vectors->vector_irq, hns3_irq_handle, 0,
124 tqp_vectors->name,
125 tqp_vectors);
126 if (ret) {
127 netdev_err(priv->netdev, "request irq(%d) fail\n",
128 tqp_vectors->vector_irq);
129 return ret;
130 }
131
132 tqp_vectors->irq_init_flag = HNS3_VECTOR_INITED;
133 }
134
135 return 0;
136}
137
138static void hns3_mask_vector_irq(struct hns3_enet_tqp_vector *tqp_vector,
139 u32 mask_en)
140{
141 writel(mask_en, tqp_vector->mask_addr);
142}
143
144static void hns3_vector_enable(struct hns3_enet_tqp_vector *tqp_vector)
145{
146 napi_enable(&tqp_vector->napi);
147
148 /* enable vector */
149 hns3_mask_vector_irq(tqp_vector, 1);
150}
151
152static void hns3_vector_disable(struct hns3_enet_tqp_vector *tqp_vector)
153{
154 /* disable vector */
155 hns3_mask_vector_irq(tqp_vector, 0);
156
157 disable_irq(tqp_vector->vector_irq);
158 napi_disable(&tqp_vector->napi);
159}
160
161static void hns3_set_vector_coalesc_gl(struct hns3_enet_tqp_vector *tqp_vector,
162 u32 gl_value)
163{
164 /* this defines the configuration for GL (Interrupt Gap Limiter)
165 * GL defines inter interrupt gap.
166 * GL and RL(Rate Limiter) are 2 ways to acheive interrupt coalescing
167 */
168 writel(gl_value, tqp_vector->mask_addr + HNS3_VECTOR_GL0_OFFSET);
169 writel(gl_value, tqp_vector->mask_addr + HNS3_VECTOR_GL1_OFFSET);
170 writel(gl_value, tqp_vector->mask_addr + HNS3_VECTOR_GL2_OFFSET);
171}
172
173static void hns3_set_vector_coalesc_rl(struct hns3_enet_tqp_vector *tqp_vector,
174 u32 rl_value)
175{
176 /* this defines the configuration for RL (Interrupt Rate Limiter).
177 * Rl defines rate of interrupts i.e. number of interrupts-per-second
178 * GL and RL(Rate Limiter) are 2 ways to acheive interrupt coalescing
179 */
180 writel(rl_value, tqp_vector->mask_addr + HNS3_VECTOR_RL_OFFSET);
181}
182
183static void hns3_vector_gl_rl_init(struct hns3_enet_tqp_vector *tqp_vector)
184{
185 /* initialize the configuration for interrupt coalescing.
186 * 1. GL (Interrupt Gap Limiter)
187 * 2. RL (Interrupt Rate Limiter)
188 */
189
190 /* Default :enable interrupt coalesce */
191 tqp_vector->rx_group.int_gl = HNS3_INT_GL_50K;
192 tqp_vector->tx_group.int_gl = HNS3_INT_GL_50K;
193 hns3_set_vector_coalesc_gl(tqp_vector, HNS3_INT_GL_50K);
194 /* for now we are disabling Interrupt RL - we
195 * will re-enable later
196 */
197 hns3_set_vector_coalesc_rl(tqp_vector, 0);
198 tqp_vector->rx_group.flow_level = HNS3_FLOW_LOW;
199 tqp_vector->tx_group.flow_level = HNS3_FLOW_LOW;
200}
201
Yunsheng Lin9df8f792017-09-27 09:45:32 +0800202static int hns3_nic_set_real_num_queue(struct net_device *netdev)
203{
Yunsheng Lin9780cb92017-10-09 15:43:56 +0800204 struct hnae3_handle *h = hns3_get_handle(netdev);
Yunsheng Lin9df8f792017-09-27 09:45:32 +0800205 struct hnae3_knic_private_info *kinfo = &h->kinfo;
206 unsigned int queue_size = kinfo->rss_size * kinfo->num_tc;
207 int ret;
208
209 ret = netif_set_real_num_tx_queues(netdev, queue_size);
210 if (ret) {
211 netdev_err(netdev,
212 "netif_set_real_num_tx_queues fail, ret=%d!\n",
213 ret);
214 return ret;
215 }
216
217 ret = netif_set_real_num_rx_queues(netdev, queue_size);
218 if (ret) {
219 netdev_err(netdev,
220 "netif_set_real_num_rx_queues fail, ret=%d!\n", ret);
221 return ret;
222 }
223
224 return 0;
225}
226
Salil76ad4f02017-08-02 16:59:45 +0100227static int hns3_nic_net_up(struct net_device *netdev)
228{
229 struct hns3_nic_priv *priv = netdev_priv(netdev);
230 struct hnae3_handle *h = priv->ae_handle;
231 int i, j;
232 int ret;
233
234 /* get irq resource for all vectors */
235 ret = hns3_nic_init_irq(priv);
236 if (ret) {
237 netdev_err(netdev, "hns init irq failed! ret=%d\n", ret);
238 return ret;
239 }
240
241 /* enable the vectors */
242 for (i = 0; i < priv->vector_num; i++)
243 hns3_vector_enable(&priv->tqp_vector[i]);
244
245 /* start the ae_dev */
246 ret = h->ae_algo->ops->start ? h->ae_algo->ops->start(h) : 0;
247 if (ret)
248 goto out_start_err;
249
250 return 0;
251
252out_start_err:
253 for (j = i - 1; j >= 0; j--)
254 hns3_vector_disable(&priv->tqp_vector[j]);
255
256 hns3_nic_uninit_irq(priv);
257
258 return ret;
259}
260
261static int hns3_nic_net_open(struct net_device *netdev)
262{
Lipengf8fa222c2017-11-02 20:45:20 +0800263 struct hns3_nic_priv *priv = netdev_priv(netdev);
Salil76ad4f02017-08-02 16:59:45 +0100264 int ret;
265
266 netif_carrier_off(netdev);
267
Yunsheng Lin9df8f792017-09-27 09:45:32 +0800268 ret = hns3_nic_set_real_num_queue(netdev);
269 if (ret)
Salil76ad4f02017-08-02 16:59:45 +0100270 return ret;
Salil76ad4f02017-08-02 16:59:45 +0100271
272 ret = hns3_nic_net_up(netdev);
273 if (ret) {
274 netdev_err(netdev,
275 "hns net up fail, ret=%d!\n", ret);
276 return ret;
277 }
278
Lipengf8fa222c2017-11-02 20:45:20 +0800279 priv->last_reset_time = jiffies;
Salil76ad4f02017-08-02 16:59:45 +0100280 return 0;
281}
282
283static void hns3_nic_net_down(struct net_device *netdev)
284{
285 struct hns3_nic_priv *priv = netdev_priv(netdev);
286 const struct hnae3_ae_ops *ops;
287 int i;
288
289 /* stop ae_dev */
290 ops = priv->ae_handle->ae_algo->ops;
291 if (ops->stop)
292 ops->stop(priv->ae_handle);
293
294 /* disable vectors */
295 for (i = 0; i < priv->vector_num; i++)
296 hns3_vector_disable(&priv->tqp_vector[i]);
297
298 /* free irq resources */
299 hns3_nic_uninit_irq(priv);
300}
301
302static int hns3_nic_net_stop(struct net_device *netdev)
303{
304 netif_tx_stop_all_queues(netdev);
305 netif_carrier_off(netdev);
306
307 hns3_nic_net_down(netdev);
308
309 return 0;
310}
311
Salil76ad4f02017-08-02 16:59:45 +0100312static int hns3_nic_uc_sync(struct net_device *netdev,
313 const unsigned char *addr)
314{
Yunsheng Lin9780cb92017-10-09 15:43:56 +0800315 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +0100316
317 if (h->ae_algo->ops->add_uc_addr)
318 return h->ae_algo->ops->add_uc_addr(h, addr);
319
320 return 0;
321}
322
323static int hns3_nic_uc_unsync(struct net_device *netdev,
324 const unsigned char *addr)
325{
Yunsheng Lin9780cb92017-10-09 15:43:56 +0800326 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +0100327
328 if (h->ae_algo->ops->rm_uc_addr)
329 return h->ae_algo->ops->rm_uc_addr(h, addr);
330
331 return 0;
332}
333
334static int hns3_nic_mc_sync(struct net_device *netdev,
335 const unsigned char *addr)
336{
Yunsheng Lin9780cb92017-10-09 15:43:56 +0800337 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +0100338
Dan Carpenter720a8472017-08-10 12:56:14 +0300339 if (h->ae_algo->ops->add_mc_addr)
Salil76ad4f02017-08-02 16:59:45 +0100340 return h->ae_algo->ops->add_mc_addr(h, addr);
341
342 return 0;
343}
344
345static int hns3_nic_mc_unsync(struct net_device *netdev,
346 const unsigned char *addr)
347{
Yunsheng Lin9780cb92017-10-09 15:43:56 +0800348 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +0100349
Dan Carpenter720a8472017-08-10 12:56:14 +0300350 if (h->ae_algo->ops->rm_mc_addr)
Salil76ad4f02017-08-02 16:59:45 +0100351 return h->ae_algo->ops->rm_mc_addr(h, addr);
352
353 return 0;
354}
355
Yunsheng Lin1db9b1b2017-10-09 15:44:01 +0800356static void hns3_nic_set_rx_mode(struct net_device *netdev)
Salil76ad4f02017-08-02 16:59:45 +0100357{
Yunsheng Lin9780cb92017-10-09 15:43:56 +0800358 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +0100359
360 if (h->ae_algo->ops->set_promisc_mode) {
361 if (netdev->flags & IFF_PROMISC)
362 h->ae_algo->ops->set_promisc_mode(h, 1);
363 else
364 h->ae_algo->ops->set_promisc_mode(h, 0);
365 }
366 if (__dev_uc_sync(netdev, hns3_nic_uc_sync, hns3_nic_uc_unsync))
367 netdev_err(netdev, "sync uc address fail\n");
368 if (netdev->flags & IFF_MULTICAST)
369 if (__dev_mc_sync(netdev, hns3_nic_mc_sync, hns3_nic_mc_unsync))
370 netdev_err(netdev, "sync mc address fail\n");
371}
372
373static int hns3_set_tso(struct sk_buff *skb, u32 *paylen,
374 u16 *mss, u32 *type_cs_vlan_tso)
375{
376 u32 l4_offset, hdr_len;
377 union l3_hdr_info l3;
378 union l4_hdr_info l4;
379 u32 l4_paylen;
380 int ret;
381
382 if (!skb_is_gso(skb))
383 return 0;
384
385 ret = skb_cow_head(skb, 0);
386 if (ret)
387 return ret;
388
389 l3.hdr = skb_network_header(skb);
390 l4.hdr = skb_transport_header(skb);
391
392 /* Software should clear the IPv4's checksum field when tso is
393 * needed.
394 */
395 if (l3.v4->version == 4)
396 l3.v4->check = 0;
397
398 /* tunnel packet.*/
399 if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
400 SKB_GSO_GRE_CSUM |
401 SKB_GSO_UDP_TUNNEL |
402 SKB_GSO_UDP_TUNNEL_CSUM)) {
403 if ((!(skb_shinfo(skb)->gso_type &
404 SKB_GSO_PARTIAL)) &&
405 (skb_shinfo(skb)->gso_type &
406 SKB_GSO_UDP_TUNNEL_CSUM)) {
407 /* Software should clear the udp's checksum
408 * field when tso is needed.
409 */
410 l4.udp->check = 0;
411 }
412 /* reset l3&l4 pointers from outer to inner headers */
413 l3.hdr = skb_inner_network_header(skb);
414 l4.hdr = skb_inner_transport_header(skb);
415
416 /* Software should clear the IPv4's checksum field when
417 * tso is needed.
418 */
419 if (l3.v4->version == 4)
420 l3.v4->check = 0;
421 }
422
423 /* normal or tunnel packet*/
424 l4_offset = l4.hdr - skb->data;
425 hdr_len = (l4.tcp->doff * 4) + l4_offset;
426
427 /* remove payload length from inner pseudo checksum when tso*/
428 l4_paylen = skb->len - l4_offset;
429 csum_replace_by_diff(&l4.tcp->check,
430 (__force __wsum)htonl(l4_paylen));
431
432 /* find the txbd field values */
433 *paylen = skb->len - hdr_len;
434 hnae_set_bit(*type_cs_vlan_tso,
435 HNS3_TXD_TSO_B, 1);
436
437 /* get MSS for TSO */
438 *mss = skb_shinfo(skb)->gso_size;
439
440 return 0;
441}
442
Salil1898d4e2017-08-18 12:31:39 +0100443static int hns3_get_l4_protocol(struct sk_buff *skb, u8 *ol4_proto,
444 u8 *il4_proto)
Salil76ad4f02017-08-02 16:59:45 +0100445{
446 union {
447 struct iphdr *v4;
448 struct ipv6hdr *v6;
449 unsigned char *hdr;
450 } l3;
451 unsigned char *l4_hdr;
452 unsigned char *exthdr;
453 u8 l4_proto_tmp;
454 __be16 frag_off;
455
456 /* find outer header point */
457 l3.hdr = skb_network_header(skb);
458 l4_hdr = skb_inner_transport_header(skb);
459
460 if (skb->protocol == htons(ETH_P_IPV6)) {
461 exthdr = l3.hdr + sizeof(*l3.v6);
462 l4_proto_tmp = l3.v6->nexthdr;
463 if (l4_hdr != exthdr)
464 ipv6_skip_exthdr(skb, exthdr - skb->data,
465 &l4_proto_tmp, &frag_off);
466 } else if (skb->protocol == htons(ETH_P_IP)) {
467 l4_proto_tmp = l3.v4->protocol;
Salil1898d4e2017-08-18 12:31:39 +0100468 } else {
469 return -EINVAL;
Salil76ad4f02017-08-02 16:59:45 +0100470 }
471
472 *ol4_proto = l4_proto_tmp;
473
474 /* tunnel packet */
475 if (!skb->encapsulation) {
476 *il4_proto = 0;
Salil1898d4e2017-08-18 12:31:39 +0100477 return 0;
Salil76ad4f02017-08-02 16:59:45 +0100478 }
479
480 /* find inner header point */
481 l3.hdr = skb_inner_network_header(skb);
482 l4_hdr = skb_inner_transport_header(skb);
483
484 if (l3.v6->version == 6) {
485 exthdr = l3.hdr + sizeof(*l3.v6);
486 l4_proto_tmp = l3.v6->nexthdr;
487 if (l4_hdr != exthdr)
488 ipv6_skip_exthdr(skb, exthdr - skb->data,
489 &l4_proto_tmp, &frag_off);
490 } else if (l3.v4->version == 4) {
491 l4_proto_tmp = l3.v4->protocol;
492 }
493
494 *il4_proto = l4_proto_tmp;
Salil1898d4e2017-08-18 12:31:39 +0100495
496 return 0;
Salil76ad4f02017-08-02 16:59:45 +0100497}
498
499static void hns3_set_l2l3l4_len(struct sk_buff *skb, u8 ol4_proto,
500 u8 il4_proto, u32 *type_cs_vlan_tso,
501 u32 *ol_type_vlan_len_msec)
502{
503 union {
504 struct iphdr *v4;
505 struct ipv6hdr *v6;
506 unsigned char *hdr;
507 } l3;
508 union {
509 struct tcphdr *tcp;
510 struct udphdr *udp;
511 struct gre_base_hdr *gre;
512 unsigned char *hdr;
513 } l4;
514 unsigned char *l2_hdr;
515 u8 l4_proto = ol4_proto;
516 u32 ol2_len;
517 u32 ol3_len;
518 u32 ol4_len;
519 u32 l2_len;
520 u32 l3_len;
521
522 l3.hdr = skb_network_header(skb);
523 l4.hdr = skb_transport_header(skb);
524
525 /* compute L2 header size for normal packet, defined in 2 Bytes */
526 l2_len = l3.hdr - skb->data;
527 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L2LEN_M,
528 HNS3_TXD_L2LEN_S, l2_len >> 1);
529
530 /* tunnel packet*/
531 if (skb->encapsulation) {
532 /* compute OL2 header size, defined in 2 Bytes */
533 ol2_len = l2_len;
534 hnae_set_field(*ol_type_vlan_len_msec,
535 HNS3_TXD_L2LEN_M,
536 HNS3_TXD_L2LEN_S, ol2_len >> 1);
537
538 /* compute OL3 header size, defined in 4 Bytes */
539 ol3_len = l4.hdr - l3.hdr;
540 hnae_set_field(*ol_type_vlan_len_msec, HNS3_TXD_L3LEN_M,
541 HNS3_TXD_L3LEN_S, ol3_len >> 2);
542
543 /* MAC in UDP, MAC in GRE (0x6558)*/
544 if ((ol4_proto == IPPROTO_UDP) || (ol4_proto == IPPROTO_GRE)) {
545 /* switch MAC header ptr from outer to inner header.*/
546 l2_hdr = skb_inner_mac_header(skb);
547
548 /* compute OL4 header size, defined in 4 Bytes. */
549 ol4_len = l2_hdr - l4.hdr;
550 hnae_set_field(*ol_type_vlan_len_msec, HNS3_TXD_L4LEN_M,
551 HNS3_TXD_L4LEN_S, ol4_len >> 2);
552
553 /* switch IP header ptr from outer to inner header */
554 l3.hdr = skb_inner_network_header(skb);
555
556 /* compute inner l2 header size, defined in 2 Bytes. */
557 l2_len = l3.hdr - l2_hdr;
558 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L2LEN_M,
559 HNS3_TXD_L2LEN_S, l2_len >> 1);
560 } else {
561 /* skb packet types not supported by hardware,
562 * txbd len fild doesn't be filled.
563 */
564 return;
565 }
566
567 /* switch L4 header pointer from outer to inner */
568 l4.hdr = skb_inner_transport_header(skb);
569
570 l4_proto = il4_proto;
571 }
572
573 /* compute inner(/normal) L3 header size, defined in 4 Bytes */
574 l3_len = l4.hdr - l3.hdr;
575 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L3LEN_M,
576 HNS3_TXD_L3LEN_S, l3_len >> 2);
577
578 /* compute inner(/normal) L4 header size, defined in 4 Bytes */
579 switch (l4_proto) {
580 case IPPROTO_TCP:
581 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L4LEN_M,
582 HNS3_TXD_L4LEN_S, l4.tcp->doff);
583 break;
584 case IPPROTO_SCTP:
585 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L4LEN_M,
586 HNS3_TXD_L4LEN_S, (sizeof(struct sctphdr) >> 2));
587 break;
588 case IPPROTO_UDP:
589 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L4LEN_M,
590 HNS3_TXD_L4LEN_S, (sizeof(struct udphdr) >> 2));
591 break;
592 default:
593 /* skb packet types not supported by hardware,
594 * txbd len fild doesn't be filled.
595 */
596 return;
597 }
598}
599
600static int hns3_set_l3l4_type_csum(struct sk_buff *skb, u8 ol4_proto,
601 u8 il4_proto, u32 *type_cs_vlan_tso,
602 u32 *ol_type_vlan_len_msec)
603{
604 union {
605 struct iphdr *v4;
606 struct ipv6hdr *v6;
607 unsigned char *hdr;
608 } l3;
609 u32 l4_proto = ol4_proto;
610
611 l3.hdr = skb_network_header(skb);
612
613 /* define OL3 type and tunnel type(OL4).*/
614 if (skb->encapsulation) {
615 /* define outer network header type.*/
616 if (skb->protocol == htons(ETH_P_IP)) {
617 if (skb_is_gso(skb))
618 hnae_set_field(*ol_type_vlan_len_msec,
619 HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
620 HNS3_OL3T_IPV4_CSUM);
621 else
622 hnae_set_field(*ol_type_vlan_len_msec,
623 HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
624 HNS3_OL3T_IPV4_NO_CSUM);
625
626 } else if (skb->protocol == htons(ETH_P_IPV6)) {
627 hnae_set_field(*ol_type_vlan_len_msec, HNS3_TXD_OL3T_M,
628 HNS3_TXD_OL3T_S, HNS3_OL3T_IPV6);
629 }
630
631 /* define tunnel type(OL4).*/
632 switch (l4_proto) {
633 case IPPROTO_UDP:
634 hnae_set_field(*ol_type_vlan_len_msec,
635 HNS3_TXD_TUNTYPE_M,
636 HNS3_TXD_TUNTYPE_S,
637 HNS3_TUN_MAC_IN_UDP);
638 break;
639 case IPPROTO_GRE:
640 hnae_set_field(*ol_type_vlan_len_msec,
641 HNS3_TXD_TUNTYPE_M,
642 HNS3_TXD_TUNTYPE_S,
643 HNS3_TUN_NVGRE);
644 break;
645 default:
646 /* drop the skb tunnel packet if hardware don't support,
647 * because hardware can't calculate csum when TSO.
648 */
649 if (skb_is_gso(skb))
650 return -EDOM;
651
652 /* the stack computes the IP header already,
653 * driver calculate l4 checksum when not TSO.
654 */
655 skb_checksum_help(skb);
656 return 0;
657 }
658
659 l3.hdr = skb_inner_network_header(skb);
660 l4_proto = il4_proto;
661 }
662
663 if (l3.v4->version == 4) {
664 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L3T_M,
665 HNS3_TXD_L3T_S, HNS3_L3T_IPV4);
666
667 /* the stack computes the IP header already, the only time we
668 * need the hardware to recompute it is in the case of TSO.
669 */
670 if (skb_is_gso(skb))
671 hnae_set_bit(*type_cs_vlan_tso, HNS3_TXD_L3CS_B, 1);
672
673 hnae_set_bit(*type_cs_vlan_tso, HNS3_TXD_L4CS_B, 1);
674 } else if (l3.v6->version == 6) {
675 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L3T_M,
676 HNS3_TXD_L3T_S, HNS3_L3T_IPV6);
677 hnae_set_bit(*type_cs_vlan_tso, HNS3_TXD_L4CS_B, 1);
678 }
679
680 switch (l4_proto) {
681 case IPPROTO_TCP:
682 hnae_set_field(*type_cs_vlan_tso,
683 HNS3_TXD_L4T_M,
684 HNS3_TXD_L4T_S,
685 HNS3_L4T_TCP);
686 break;
687 case IPPROTO_UDP:
688 hnae_set_field(*type_cs_vlan_tso,
689 HNS3_TXD_L4T_M,
690 HNS3_TXD_L4T_S,
691 HNS3_L4T_UDP);
692 break;
693 case IPPROTO_SCTP:
694 hnae_set_field(*type_cs_vlan_tso,
695 HNS3_TXD_L4T_M,
696 HNS3_TXD_L4T_S,
697 HNS3_L4T_SCTP);
698 break;
699 default:
700 /* drop the skb tunnel packet if hardware don't support,
701 * because hardware can't calculate csum when TSO.
702 */
703 if (skb_is_gso(skb))
704 return -EDOM;
705
706 /* the stack computes the IP header already,
707 * driver calculate l4 checksum when not TSO.
708 */
709 skb_checksum_help(skb);
710 return 0;
711 }
712
713 return 0;
714}
715
716static void hns3_set_txbd_baseinfo(u16 *bdtp_fe_sc_vld_ra_ri, int frag_end)
717{
718 /* Config bd buffer end */
719 hnae_set_field(*bdtp_fe_sc_vld_ra_ri, HNS3_TXD_BDTYPE_M,
720 HNS3_TXD_BDTYPE_M, 0);
721 hnae_set_bit(*bdtp_fe_sc_vld_ra_ri, HNS3_TXD_FE_B, !!frag_end);
722 hnae_set_bit(*bdtp_fe_sc_vld_ra_ri, HNS3_TXD_VLD_B, 1);
Lipeng7036d262017-10-24 21:02:09 +0800723 hnae_set_field(*bdtp_fe_sc_vld_ra_ri, HNS3_TXD_SC_M, HNS3_TXD_SC_S, 0);
Salil76ad4f02017-08-02 16:59:45 +0100724}
725
Peng Li9699cff2017-12-22 12:21:48 +0800726static int hns3_fill_desc_vtags(struct sk_buff *skb,
727 struct hns3_enet_ring *tx_ring,
728 u32 *inner_vlan_flag,
729 u32 *out_vlan_flag,
730 u16 *inner_vtag,
731 u16 *out_vtag)
732{
733#define HNS3_TX_VLAN_PRIO_SHIFT 13
734
735 if (skb->protocol == htons(ETH_P_8021Q) &&
736 !(tx_ring->tqp->handle->kinfo.netdev->features &
737 NETIF_F_HW_VLAN_CTAG_TX)) {
738 /* When HW VLAN acceleration is turned off, and the stack
739 * sets the protocol to 802.1q, the driver just need to
740 * set the protocol to the encapsulated ethertype.
741 */
742 skb->protocol = vlan_get_protocol(skb);
743 return 0;
744 }
745
746 if (skb_vlan_tag_present(skb)) {
747 u16 vlan_tag;
748
749 vlan_tag = skb_vlan_tag_get(skb);
750 vlan_tag |= (skb->priority & 0x7) << HNS3_TX_VLAN_PRIO_SHIFT;
751
752 /* Based on hw strategy, use out_vtag in two layer tag case,
753 * and use inner_vtag in one tag case.
754 */
755 if (skb->protocol == htons(ETH_P_8021Q)) {
756 hnae_set_bit(*out_vlan_flag, HNS3_TXD_OVLAN_B, 1);
757 *out_vtag = vlan_tag;
758 } else {
759 hnae_set_bit(*inner_vlan_flag, HNS3_TXD_VLAN_B, 1);
760 *inner_vtag = vlan_tag;
761 }
762 } else if (skb->protocol == htons(ETH_P_8021Q)) {
763 struct vlan_ethhdr *vhdr;
764 int rc;
765
766 rc = skb_cow_head(skb, 0);
767 if (rc < 0)
768 return rc;
769 vhdr = (struct vlan_ethhdr *)skb->data;
770 vhdr->h_vlan_TCI |= cpu_to_be16((skb->priority & 0x7)
771 << HNS3_TX_VLAN_PRIO_SHIFT);
772 }
773
774 skb->protocol = vlan_get_protocol(skb);
775 return 0;
776}
777
Salil76ad4f02017-08-02 16:59:45 +0100778static int hns3_fill_desc(struct hns3_enet_ring *ring, void *priv,
779 int size, dma_addr_t dma, int frag_end,
780 enum hns_desc_type type)
781{
782 struct hns3_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_use];
783 struct hns3_desc *desc = &ring->desc[ring->next_to_use];
784 u32 ol_type_vlan_len_msec = 0;
785 u16 bdtp_fe_sc_vld_ra_ri = 0;
786 u32 type_cs_vlan_tso = 0;
787 struct sk_buff *skb;
Peng Li9699cff2017-12-22 12:21:48 +0800788 u16 inner_vtag = 0;
789 u16 out_vtag = 0;
Salil76ad4f02017-08-02 16:59:45 +0100790 u32 paylen = 0;
791 u16 mss = 0;
792 __be16 protocol;
793 u8 ol4_proto;
794 u8 il4_proto;
795 int ret;
796
797 /* The txbd's baseinfo of DESC_TYPE_PAGE & DESC_TYPE_SKB */
798 desc_cb->priv = priv;
799 desc_cb->length = size;
800 desc_cb->dma = dma;
801 desc_cb->type = type;
802
803 /* now, fill the descriptor */
804 desc->addr = cpu_to_le64(dma);
805 desc->tx.send_size = cpu_to_le16((u16)size);
806 hns3_set_txbd_baseinfo(&bdtp_fe_sc_vld_ra_ri, frag_end);
807 desc->tx.bdtp_fe_sc_vld_ra_ri = cpu_to_le16(bdtp_fe_sc_vld_ra_ri);
808
809 if (type == DESC_TYPE_SKB) {
810 skb = (struct sk_buff *)priv;
Yunsheng Lina90bb9a2017-10-09 15:44:00 +0800811 paylen = skb->len;
Salil76ad4f02017-08-02 16:59:45 +0100812
Peng Li9699cff2017-12-22 12:21:48 +0800813 ret = hns3_fill_desc_vtags(skb, ring, &type_cs_vlan_tso,
814 &ol_type_vlan_len_msec,
815 &inner_vtag, &out_vtag);
816 if (unlikely(ret))
817 return ret;
818
Salil76ad4f02017-08-02 16:59:45 +0100819 if (skb->ip_summed == CHECKSUM_PARTIAL) {
820 skb_reset_mac_len(skb);
821 protocol = skb->protocol;
822
Salil1898d4e2017-08-18 12:31:39 +0100823 ret = hns3_get_l4_protocol(skb, &ol4_proto, &il4_proto);
824 if (ret)
825 return ret;
Salil76ad4f02017-08-02 16:59:45 +0100826 hns3_set_l2l3l4_len(skb, ol4_proto, il4_proto,
827 &type_cs_vlan_tso,
828 &ol_type_vlan_len_msec);
829 ret = hns3_set_l3l4_type_csum(skb, ol4_proto, il4_proto,
830 &type_cs_vlan_tso,
831 &ol_type_vlan_len_msec);
832 if (ret)
833 return ret;
834
835 ret = hns3_set_tso(skb, &paylen, &mss,
836 &type_cs_vlan_tso);
837 if (ret)
838 return ret;
839 }
840
841 /* Set txbd */
842 desc->tx.ol_type_vlan_len_msec =
843 cpu_to_le32(ol_type_vlan_len_msec);
844 desc->tx.type_cs_vlan_tso_len =
845 cpu_to_le32(type_cs_vlan_tso);
Yunsheng Lina90bb9a2017-10-09 15:44:00 +0800846 desc->tx.paylen = cpu_to_le32(paylen);
Salil76ad4f02017-08-02 16:59:45 +0100847 desc->tx.mss = cpu_to_le16(mss);
Peng Li9699cff2017-12-22 12:21:48 +0800848 desc->tx.vlan_tag = cpu_to_le16(inner_vtag);
849 desc->tx.outer_vlan_tag = cpu_to_le16(out_vtag);
Salil76ad4f02017-08-02 16:59:45 +0100850 }
851
852 /* move ring pointer to next.*/
853 ring_ptr_move_fw(ring, next_to_use);
854
855 return 0;
856}
857
858static int hns3_fill_desc_tso(struct hns3_enet_ring *ring, void *priv,
859 int size, dma_addr_t dma, int frag_end,
860 enum hns_desc_type type)
861{
862 unsigned int frag_buf_num;
863 unsigned int k;
864 int sizeoflast;
865 int ret;
866
867 frag_buf_num = (size + HNS3_MAX_BD_SIZE - 1) / HNS3_MAX_BD_SIZE;
868 sizeoflast = size % HNS3_MAX_BD_SIZE;
869 sizeoflast = sizeoflast ? sizeoflast : HNS3_MAX_BD_SIZE;
870
871 /* When the frag size is bigger than hardware, split this frag */
872 for (k = 0; k < frag_buf_num; k++) {
873 ret = hns3_fill_desc(ring, priv,
874 (k == frag_buf_num - 1) ?
875 sizeoflast : HNS3_MAX_BD_SIZE,
876 dma + HNS3_MAX_BD_SIZE * k,
877 frag_end && (k == frag_buf_num - 1) ? 1 : 0,
878 (type == DESC_TYPE_SKB && !k) ?
879 DESC_TYPE_SKB : DESC_TYPE_PAGE);
880 if (ret)
881 return ret;
882 }
883
884 return 0;
885}
886
887static int hns3_nic_maybe_stop_tso(struct sk_buff **out_skb, int *bnum,
888 struct hns3_enet_ring *ring)
889{
890 struct sk_buff *skb = *out_skb;
891 struct skb_frag_struct *frag;
892 int bdnum_for_frag;
893 int frag_num;
894 int buf_num;
895 int size;
896 int i;
897
898 size = skb_headlen(skb);
899 buf_num = (size + HNS3_MAX_BD_SIZE - 1) / HNS3_MAX_BD_SIZE;
900
901 frag_num = skb_shinfo(skb)->nr_frags;
902 for (i = 0; i < frag_num; i++) {
903 frag = &skb_shinfo(skb)->frags[i];
904 size = skb_frag_size(frag);
905 bdnum_for_frag =
906 (size + HNS3_MAX_BD_SIZE - 1) / HNS3_MAX_BD_SIZE;
907 if (bdnum_for_frag > HNS3_MAX_BD_PER_FRAG)
908 return -ENOMEM;
909
910 buf_num += bdnum_for_frag;
911 }
912
913 if (buf_num > ring_space(ring))
914 return -EBUSY;
915
916 *bnum = buf_num;
917 return 0;
918}
919
920static int hns3_nic_maybe_stop_tx(struct sk_buff **out_skb, int *bnum,
921 struct hns3_enet_ring *ring)
922{
923 struct sk_buff *skb = *out_skb;
924 int buf_num;
925
926 /* No. of segments (plus a header) */
927 buf_num = skb_shinfo(skb)->nr_frags + 1;
928
929 if (buf_num > ring_space(ring))
930 return -EBUSY;
931
932 *bnum = buf_num;
933
934 return 0;
935}
936
937static void hns_nic_dma_unmap(struct hns3_enet_ring *ring, int next_to_use_orig)
938{
939 struct device *dev = ring_to_dev(ring);
940 unsigned int i;
941
942 for (i = 0; i < ring->desc_num; i++) {
943 /* check if this is where we started */
944 if (ring->next_to_use == next_to_use_orig)
945 break;
946
947 /* unmap the descriptor dma address */
948 if (ring->desc_cb[ring->next_to_use].type == DESC_TYPE_SKB)
949 dma_unmap_single(dev,
950 ring->desc_cb[ring->next_to_use].dma,
951 ring->desc_cb[ring->next_to_use].length,
952 DMA_TO_DEVICE);
953 else
954 dma_unmap_page(dev,
955 ring->desc_cb[ring->next_to_use].dma,
956 ring->desc_cb[ring->next_to_use].length,
957 DMA_TO_DEVICE);
958
959 /* rollback one */
960 ring_ptr_move_bw(ring, next_to_use);
961 }
962}
963
Yunsheng Lind43e5ac2017-10-20 10:19:21 +0800964netdev_tx_t hns3_nic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
Salil76ad4f02017-08-02 16:59:45 +0100965{
966 struct hns3_nic_priv *priv = netdev_priv(netdev);
967 struct hns3_nic_ring_data *ring_data =
968 &tx_ring_data(priv, skb->queue_mapping);
969 struct hns3_enet_ring *ring = ring_data->ring;
970 struct device *dev = priv->dev;
971 struct netdev_queue *dev_queue;
972 struct skb_frag_struct *frag;
973 int next_to_use_head;
974 int next_to_use_frag;
975 dma_addr_t dma;
976 int buf_num;
977 int seg_num;
978 int size;
979 int ret;
980 int i;
981
982 /* Prefetch the data used later */
983 prefetch(skb->data);
984
985 switch (priv->ops.maybe_stop_tx(&skb, &buf_num, ring)) {
986 case -EBUSY:
987 u64_stats_update_begin(&ring->syncp);
988 ring->stats.tx_busy++;
989 u64_stats_update_end(&ring->syncp);
990
991 goto out_net_tx_busy;
992 case -ENOMEM:
993 u64_stats_update_begin(&ring->syncp);
994 ring->stats.sw_err_cnt++;
995 u64_stats_update_end(&ring->syncp);
996 netdev_err(netdev, "no memory to xmit!\n");
997
998 goto out_err_tx_ok;
999 default:
1000 break;
1001 }
1002
1003 /* No. of segments (plus a header) */
1004 seg_num = skb_shinfo(skb)->nr_frags + 1;
1005 /* Fill the first part */
1006 size = skb_headlen(skb);
1007
1008 next_to_use_head = ring->next_to_use;
1009
1010 dma = dma_map_single(dev, skb->data, size, DMA_TO_DEVICE);
1011 if (dma_mapping_error(dev, dma)) {
1012 netdev_err(netdev, "TX head DMA map failed\n");
1013 ring->stats.sw_err_cnt++;
1014 goto out_err_tx_ok;
1015 }
1016
1017 ret = priv->ops.fill_desc(ring, skb, size, dma, seg_num == 1 ? 1 : 0,
1018 DESC_TYPE_SKB);
1019 if (ret)
1020 goto head_dma_map_err;
1021
1022 next_to_use_frag = ring->next_to_use;
1023 /* Fill the fragments */
1024 for (i = 1; i < seg_num; i++) {
1025 frag = &skb_shinfo(skb)->frags[i - 1];
1026 size = skb_frag_size(frag);
1027 dma = skb_frag_dma_map(dev, frag, 0, size, DMA_TO_DEVICE);
1028 if (dma_mapping_error(dev, dma)) {
1029 netdev_err(netdev, "TX frag(%d) DMA map failed\n", i);
1030 ring->stats.sw_err_cnt++;
1031 goto frag_dma_map_err;
1032 }
1033 ret = priv->ops.fill_desc(ring, skb_frag_page(frag), size, dma,
1034 seg_num - 1 == i ? 1 : 0,
1035 DESC_TYPE_PAGE);
1036
1037 if (ret)
1038 goto frag_dma_map_err;
1039 }
1040
1041 /* Complete translate all packets */
1042 dev_queue = netdev_get_tx_queue(netdev, ring_data->queue_index);
1043 netdev_tx_sent_queue(dev_queue, skb->len);
1044
1045 wmb(); /* Commit all data before submit */
1046
1047 hnae_queue_xmit(ring->tqp, buf_num);
1048
1049 return NETDEV_TX_OK;
1050
1051frag_dma_map_err:
1052 hns_nic_dma_unmap(ring, next_to_use_frag);
1053
1054head_dma_map_err:
1055 hns_nic_dma_unmap(ring, next_to_use_head);
1056
1057out_err_tx_ok:
1058 dev_kfree_skb_any(skb);
1059 return NETDEV_TX_OK;
1060
1061out_net_tx_busy:
1062 netif_stop_subqueue(netdev, ring_data->queue_index);
1063 smp_mb(); /* Commit all data before submit */
1064
1065 return NETDEV_TX_BUSY;
1066}
1067
1068static int hns3_nic_net_set_mac_address(struct net_device *netdev, void *p)
1069{
Yunsheng Lin9780cb92017-10-09 15:43:56 +08001070 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +01001071 struct sockaddr *mac_addr = p;
1072 int ret;
1073
1074 if (!mac_addr || !is_valid_ether_addr((const u8 *)mac_addr->sa_data))
1075 return -EADDRNOTAVAIL;
1076
1077 ret = h->ae_algo->ops->set_mac_addr(h, mac_addr->sa_data);
1078 if (ret) {
1079 netdev_err(netdev, "set_mac_address fail, ret=%d!\n", ret);
1080 return ret;
1081 }
1082
1083 ether_addr_copy(netdev->dev_addr, mac_addr->sa_data);
1084
1085 return 0;
1086}
1087
1088static int hns3_nic_set_features(struct net_device *netdev,
1089 netdev_features_t features)
1090{
1091 struct hns3_nic_priv *priv = netdev_priv(netdev);
Peng Li052ece62017-12-22 12:21:47 +08001092 struct hnae3_handle *h = priv->ae_handle;
1093 netdev_features_t changed;
1094 int ret;
Salil76ad4f02017-08-02 16:59:45 +01001095
1096 if (features & (NETIF_F_TSO | NETIF_F_TSO6)) {
1097 priv->ops.fill_desc = hns3_fill_desc_tso;
1098 priv->ops.maybe_stop_tx = hns3_nic_maybe_stop_tso;
1099 } else {
1100 priv->ops.fill_desc = hns3_fill_desc;
1101 priv->ops.maybe_stop_tx = hns3_nic_maybe_stop_tx;
1102 }
1103
Jian Shen391b5e92018-01-05 18:18:05 +08001104 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
1105 h->ae_algo->ops->enable_vlan_filter(h, true);
1106 else
1107 h->ae_algo->ops->enable_vlan_filter(h, false);
1108
Peng Li052ece62017-12-22 12:21:47 +08001109 changed = netdev->features ^ features;
1110 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
1111 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1112 ret = h->ae_algo->ops->enable_hw_strip_rxvtag(h, true);
1113 else
1114 ret = h->ae_algo->ops->enable_hw_strip_rxvtag(h, false);
1115
1116 if (ret)
1117 return ret;
1118 }
1119
Salil76ad4f02017-08-02 16:59:45 +01001120 netdev->features = features;
1121 return 0;
1122}
1123
1124static void
1125hns3_nic_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
1126{
1127 struct hns3_nic_priv *priv = netdev_priv(netdev);
1128 int queue_num = priv->ae_handle->kinfo.num_tqps;
1129 struct hns3_enet_ring *ring;
1130 unsigned int start;
1131 unsigned int idx;
1132 u64 tx_bytes = 0;
1133 u64 rx_bytes = 0;
1134 u64 tx_pkts = 0;
1135 u64 rx_pkts = 0;
1136
1137 for (idx = 0; idx < queue_num; idx++) {
1138 /* fetch the tx stats */
1139 ring = priv->ring_data[idx].ring;
1140 do {
Salild36d36c2017-08-18 12:31:37 +01001141 start = u64_stats_fetch_begin_irq(&ring->syncp);
Salil76ad4f02017-08-02 16:59:45 +01001142 tx_bytes += ring->stats.tx_bytes;
1143 tx_pkts += ring->stats.tx_pkts;
1144 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
1145
1146 /* fetch the rx stats */
1147 ring = priv->ring_data[idx + queue_num].ring;
1148 do {
Salild36d36c2017-08-18 12:31:37 +01001149 start = u64_stats_fetch_begin_irq(&ring->syncp);
Salil76ad4f02017-08-02 16:59:45 +01001150 rx_bytes += ring->stats.rx_bytes;
1151 rx_pkts += ring->stats.rx_pkts;
1152 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
1153 }
1154
1155 stats->tx_bytes = tx_bytes;
1156 stats->tx_packets = tx_pkts;
1157 stats->rx_bytes = rx_bytes;
1158 stats->rx_packets = rx_pkts;
1159
1160 stats->rx_errors = netdev->stats.rx_errors;
1161 stats->multicast = netdev->stats.multicast;
1162 stats->rx_length_errors = netdev->stats.rx_length_errors;
1163 stats->rx_crc_errors = netdev->stats.rx_crc_errors;
1164 stats->rx_missed_errors = netdev->stats.rx_missed_errors;
1165
1166 stats->tx_errors = netdev->stats.tx_errors;
1167 stats->rx_dropped = netdev->stats.rx_dropped;
1168 stats->tx_dropped = netdev->stats.tx_dropped;
1169 stats->collisions = netdev->stats.collisions;
1170 stats->rx_over_errors = netdev->stats.rx_over_errors;
1171 stats->rx_frame_errors = netdev->stats.rx_frame_errors;
1172 stats->rx_fifo_errors = netdev->stats.rx_fifo_errors;
1173 stats->tx_aborted_errors = netdev->stats.tx_aborted_errors;
1174 stats->tx_carrier_errors = netdev->stats.tx_carrier_errors;
1175 stats->tx_fifo_errors = netdev->stats.tx_fifo_errors;
1176 stats->tx_heartbeat_errors = netdev->stats.tx_heartbeat_errors;
1177 stats->tx_window_errors = netdev->stats.tx_window_errors;
1178 stats->rx_compressed = netdev->stats.rx_compressed;
1179 stats->tx_compressed = netdev->stats.tx_compressed;
1180}
1181
1182static void hns3_add_tunnel_port(struct net_device *netdev, u16 port,
1183 enum hns3_udp_tnl_type type)
1184{
1185 struct hns3_nic_priv *priv = netdev_priv(netdev);
1186 struct hns3_udp_tunnel *udp_tnl = &priv->udp_tnl[type];
1187 struct hnae3_handle *h = priv->ae_handle;
1188
1189 if (udp_tnl->used && udp_tnl->dst_port == port) {
1190 udp_tnl->used++;
1191 return;
1192 }
1193
1194 if (udp_tnl->used) {
1195 netdev_warn(netdev,
1196 "UDP tunnel [%d], port [%d] offload\n", type, port);
1197 return;
1198 }
1199
1200 udp_tnl->dst_port = port;
1201 udp_tnl->used = 1;
1202 /* TBD send command to hardware to add port */
1203 if (h->ae_algo->ops->add_tunnel_udp)
1204 h->ae_algo->ops->add_tunnel_udp(h, port);
1205}
1206
1207static void hns3_del_tunnel_port(struct net_device *netdev, u16 port,
1208 enum hns3_udp_tnl_type type)
1209{
1210 struct hns3_nic_priv *priv = netdev_priv(netdev);
1211 struct hns3_udp_tunnel *udp_tnl = &priv->udp_tnl[type];
1212 struct hnae3_handle *h = priv->ae_handle;
1213
1214 if (!udp_tnl->used || udp_tnl->dst_port != port) {
1215 netdev_warn(netdev,
1216 "Invalid UDP tunnel port %d\n", port);
1217 return;
1218 }
1219
1220 udp_tnl->used--;
1221 if (udp_tnl->used)
1222 return;
1223
1224 udp_tnl->dst_port = 0;
1225 /* TBD send command to hardware to del port */
1226 if (h->ae_algo->ops->del_tunnel_udp)
Dan Carpenter9537e7c2017-08-10 12:54:59 +03001227 h->ae_algo->ops->del_tunnel_udp(h, port);
Salil76ad4f02017-08-02 16:59:45 +01001228}
1229
1230/* hns3_nic_udp_tunnel_add - Get notifiacetion about UDP tunnel ports
1231 * @netdev: This physical ports's netdev
1232 * @ti: Tunnel information
1233 */
1234static void hns3_nic_udp_tunnel_add(struct net_device *netdev,
1235 struct udp_tunnel_info *ti)
1236{
1237 u16 port_n = ntohs(ti->port);
1238
1239 switch (ti->type) {
1240 case UDP_TUNNEL_TYPE_VXLAN:
1241 hns3_add_tunnel_port(netdev, port_n, HNS3_UDP_TNL_VXLAN);
1242 break;
1243 case UDP_TUNNEL_TYPE_GENEVE:
1244 hns3_add_tunnel_port(netdev, port_n, HNS3_UDP_TNL_GENEVE);
1245 break;
1246 default:
1247 netdev_err(netdev, "unsupported tunnel type %d\n", ti->type);
1248 break;
1249 }
1250}
1251
1252static void hns3_nic_udp_tunnel_del(struct net_device *netdev,
1253 struct udp_tunnel_info *ti)
1254{
1255 u16 port_n = ntohs(ti->port);
1256
1257 switch (ti->type) {
1258 case UDP_TUNNEL_TYPE_VXLAN:
1259 hns3_del_tunnel_port(netdev, port_n, HNS3_UDP_TNL_VXLAN);
1260 break;
1261 case UDP_TUNNEL_TYPE_GENEVE:
1262 hns3_del_tunnel_port(netdev, port_n, HNS3_UDP_TNL_GENEVE);
1263 break;
1264 default:
1265 break;
1266 }
1267}
1268
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001269static int hns3_setup_tc(struct net_device *netdev, void *type_data)
Salil76ad4f02017-08-02 16:59:45 +01001270{
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001271 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
Yunsheng Lin9780cb92017-10-09 15:43:56 +08001272 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +01001273 struct hnae3_knic_private_info *kinfo = &h->kinfo;
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001274 u8 *prio_tc = mqprio_qopt->qopt.prio_tc_map;
1275 u8 tc = mqprio_qopt->qopt.num_tc;
1276 u16 mode = mqprio_qopt->mode;
1277 u8 hw = mqprio_qopt->qopt.hw;
1278 bool if_running;
Salil76ad4f02017-08-02 16:59:45 +01001279 unsigned int i;
1280 int ret;
1281
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001282 if (!((hw == TC_MQPRIO_HW_OFFLOAD_TCS &&
1283 mode == TC_MQPRIO_MODE_CHANNEL) || (!hw && tc == 0)))
1284 return -EOPNOTSUPP;
1285
Salil76ad4f02017-08-02 16:59:45 +01001286 if (tc > HNAE3_MAX_TC)
1287 return -EINVAL;
1288
Salil76ad4f02017-08-02 16:59:45 +01001289 if (!netdev)
1290 return -EINVAL;
1291
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001292 if_running = netif_running(netdev);
1293 if (if_running) {
1294 hns3_nic_net_stop(netdev);
1295 msleep(100);
Salil76ad4f02017-08-02 16:59:45 +01001296 }
1297
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001298 ret = (kinfo->dcb_ops && kinfo->dcb_ops->setup_tc) ?
1299 kinfo->dcb_ops->setup_tc(h, tc, prio_tc) : -EOPNOTSUPP;
Salil76ad4f02017-08-02 16:59:45 +01001300 if (ret)
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001301 goto out;
Salil76ad4f02017-08-02 16:59:45 +01001302
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001303 if (tc <= 1) {
1304 netdev_reset_tc(netdev);
1305 } else {
1306 ret = netdev_set_num_tc(netdev, tc);
1307 if (ret)
1308 goto out;
1309
1310 for (i = 0; i < HNAE3_MAX_TC; i++) {
1311 if (!kinfo->tc_info[i].enable)
1312 continue;
1313
Salil76ad4f02017-08-02 16:59:45 +01001314 netdev_set_tc_queue(netdev,
1315 kinfo->tc_info[i].tc,
1316 kinfo->tc_info[i].tqp_count,
1317 kinfo->tc_info[i].tqp_offset);
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001318 }
Salil76ad4f02017-08-02 16:59:45 +01001319 }
1320
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001321 ret = hns3_nic_set_real_num_queue(netdev);
1322
1323out:
1324 if (if_running)
1325 hns3_nic_net_open(netdev);
1326
1327 return ret;
Salil76ad4f02017-08-02 16:59:45 +01001328}
1329
Jiri Pirko2572ac52017-08-07 10:15:17 +02001330static int hns3_nic_setup_tc(struct net_device *dev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02001331 void *type_data)
Salil76ad4f02017-08-02 16:59:45 +01001332{
Nogah Frankel575ed7d2017-11-06 07:23:42 +01001333 if (type != TC_SETUP_QDISC_MQPRIO)
Jiri Pirko38cf0422017-08-07 10:15:31 +02001334 return -EOPNOTSUPP;
Salil76ad4f02017-08-02 16:59:45 +01001335
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001336 return hns3_setup_tc(dev, type_data);
Salil76ad4f02017-08-02 16:59:45 +01001337}
1338
1339static int hns3_vlan_rx_add_vid(struct net_device *netdev,
1340 __be16 proto, u16 vid)
1341{
Yunsheng Lin9780cb92017-10-09 15:43:56 +08001342 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +01001343 int ret = -EIO;
1344
1345 if (h->ae_algo->ops->set_vlan_filter)
1346 ret = h->ae_algo->ops->set_vlan_filter(h, proto, vid, false);
1347
1348 return ret;
1349}
1350
1351static int hns3_vlan_rx_kill_vid(struct net_device *netdev,
1352 __be16 proto, u16 vid)
1353{
Yunsheng Lin9780cb92017-10-09 15:43:56 +08001354 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +01001355 int ret = -EIO;
1356
1357 if (h->ae_algo->ops->set_vlan_filter)
1358 ret = h->ae_algo->ops->set_vlan_filter(h, proto, vid, true);
1359
1360 return ret;
1361}
1362
1363static int hns3_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan,
1364 u8 qos, __be16 vlan_proto)
1365{
Yunsheng Lin9780cb92017-10-09 15:43:56 +08001366 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +01001367 int ret = -EIO;
1368
1369 if (h->ae_algo->ops->set_vf_vlan_filter)
1370 ret = h->ae_algo->ops->set_vf_vlan_filter(h, vf, vlan,
1371 qos, vlan_proto);
1372
1373 return ret;
1374}
1375
Salila8e8b7f2017-08-21 17:05:24 +01001376static int hns3_nic_change_mtu(struct net_device *netdev, int new_mtu)
1377{
Yunsheng Lin9780cb92017-10-09 15:43:56 +08001378 struct hnae3_handle *h = hns3_get_handle(netdev);
Salila8e8b7f2017-08-21 17:05:24 +01001379 bool if_running = netif_running(netdev);
1380 int ret;
1381
1382 if (!h->ae_algo->ops->set_mtu)
1383 return -EOPNOTSUPP;
1384
1385 /* if this was called with netdev up then bring netdevice down */
1386 if (if_running) {
1387 (void)hns3_nic_net_stop(netdev);
1388 msleep(100);
1389 }
1390
1391 ret = h->ae_algo->ops->set_mtu(h, new_mtu);
1392 if (ret) {
1393 netdev_err(netdev, "failed to change MTU in hardware %d\n",
1394 ret);
1395 return ret;
1396 }
1397
1398 /* if the netdev was running earlier, bring it up again */
1399 if (if_running && hns3_nic_net_open(netdev))
1400 ret = -EINVAL;
1401
1402 return ret;
1403}
1404
Lipengf8fa222c2017-11-02 20:45:20 +08001405static bool hns3_get_tx_timeo_queue_info(struct net_device *ndev)
1406{
1407 struct hns3_nic_priv *priv = netdev_priv(ndev);
1408 struct hns3_enet_ring *tx_ring = NULL;
1409 int timeout_queue = 0;
1410 int hw_head, hw_tail;
1411 int i;
1412
1413 /* Find the stopped queue the same way the stack does */
1414 for (i = 0; i < ndev->real_num_tx_queues; i++) {
1415 struct netdev_queue *q;
1416 unsigned long trans_start;
1417
1418 q = netdev_get_tx_queue(ndev, i);
1419 trans_start = q->trans_start;
1420 if (netif_xmit_stopped(q) &&
1421 time_after(jiffies,
1422 (trans_start + ndev->watchdog_timeo))) {
1423 timeout_queue = i;
1424 break;
1425 }
1426 }
1427
1428 if (i == ndev->num_tx_queues) {
1429 netdev_info(ndev,
1430 "no netdev TX timeout queue found, timeout count: %llu\n",
1431 priv->tx_timeout_count);
1432 return false;
1433 }
1434
1435 tx_ring = priv->ring_data[timeout_queue].ring;
1436
1437 hw_head = readl_relaxed(tx_ring->tqp->io_base +
1438 HNS3_RING_TX_RING_HEAD_REG);
1439 hw_tail = readl_relaxed(tx_ring->tqp->io_base +
1440 HNS3_RING_TX_RING_TAIL_REG);
1441 netdev_info(ndev,
1442 "tx_timeout count: %llu, queue id: %d, SW_NTU: 0x%x, SW_NTC: 0x%x, HW_HEAD: 0x%x, HW_TAIL: 0x%x, INT: 0x%x\n",
1443 priv->tx_timeout_count,
1444 timeout_queue,
1445 tx_ring->next_to_use,
1446 tx_ring->next_to_clean,
1447 hw_head,
1448 hw_tail,
1449 readl(tx_ring->tqp_vector->mask_addr));
1450
1451 return true;
1452}
1453
1454static void hns3_nic_net_timeout(struct net_device *ndev)
1455{
1456 struct hns3_nic_priv *priv = netdev_priv(ndev);
1457 unsigned long last_reset_time = priv->last_reset_time;
1458 struct hnae3_handle *h = priv->ae_handle;
1459
1460 if (!hns3_get_tx_timeo_queue_info(ndev))
1461 return;
1462
1463 priv->tx_timeout_count++;
1464
1465 /* This timeout is far away enough from last timeout,
1466 * if timeout again,set the reset type to PF reset
1467 */
1468 if (time_after(jiffies, (last_reset_time + 20 * HZ)))
1469 priv->reset_level = HNAE3_FUNC_RESET;
1470
1471 /* Don't do any new action before the next timeout */
1472 else if (time_before(jiffies, (last_reset_time + ndev->watchdog_timeo)))
1473 return;
1474
1475 priv->last_reset_time = jiffies;
1476
1477 if (h->ae_algo->ops->reset_event)
1478 h->ae_algo->ops->reset_event(h, priv->reset_level);
1479
1480 priv->reset_level++;
1481 if (priv->reset_level > HNAE3_GLOBAL_RESET)
1482 priv->reset_level = HNAE3_GLOBAL_RESET;
1483}
1484
Salil76ad4f02017-08-02 16:59:45 +01001485static const struct net_device_ops hns3_nic_netdev_ops = {
1486 .ndo_open = hns3_nic_net_open,
1487 .ndo_stop = hns3_nic_net_stop,
1488 .ndo_start_xmit = hns3_nic_net_xmit,
Lipengf8fa222c2017-11-02 20:45:20 +08001489 .ndo_tx_timeout = hns3_nic_net_timeout,
Salil76ad4f02017-08-02 16:59:45 +01001490 .ndo_set_mac_address = hns3_nic_net_set_mac_address,
Salila8e8b7f2017-08-21 17:05:24 +01001491 .ndo_change_mtu = hns3_nic_change_mtu,
Salil76ad4f02017-08-02 16:59:45 +01001492 .ndo_set_features = hns3_nic_set_features,
1493 .ndo_get_stats64 = hns3_nic_get_stats64,
1494 .ndo_setup_tc = hns3_nic_setup_tc,
1495 .ndo_set_rx_mode = hns3_nic_set_rx_mode,
1496 .ndo_udp_tunnel_add = hns3_nic_udp_tunnel_add,
1497 .ndo_udp_tunnel_del = hns3_nic_udp_tunnel_del,
1498 .ndo_vlan_rx_add_vid = hns3_vlan_rx_add_vid,
1499 .ndo_vlan_rx_kill_vid = hns3_vlan_rx_kill_vid,
1500 .ndo_set_vf_vlan = hns3_ndo_set_vf_vlan,
1501};
1502
1503/* hns3_probe - Device initialization routine
1504 * @pdev: PCI device information struct
1505 * @ent: entry in hns3_pci_tbl
1506 *
1507 * hns3_probe initializes a PF identified by a pci_dev structure.
1508 * The OS initialization, configuring of the PF private structure,
1509 * and a hardware reset occur.
1510 *
1511 * Returns 0 on success, negative on failure
1512 */
1513static int hns3_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1514{
1515 struct hnae3_ae_dev *ae_dev;
1516 int ret;
1517
1518 ae_dev = devm_kzalloc(&pdev->dev, sizeof(*ae_dev),
1519 GFP_KERNEL);
1520 if (!ae_dev) {
1521 ret = -ENOMEM;
1522 return ret;
1523 }
1524
1525 ae_dev->pdev = pdev;
Yunsheng Line92a0842017-09-20 18:52:50 +08001526 ae_dev->flag = ent->driver_data;
Salil76ad4f02017-08-02 16:59:45 +01001527 ae_dev->dev_type = HNAE3_DEV_KNIC;
1528 pci_set_drvdata(pdev, ae_dev);
1529
1530 return hnae3_register_ae_dev(ae_dev);
1531}
1532
1533/* hns3_remove - Device removal routine
1534 * @pdev: PCI device information struct
1535 */
1536static void hns3_remove(struct pci_dev *pdev)
1537{
1538 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
1539
1540 hnae3_unregister_ae_dev(ae_dev);
1541
1542 devm_kfree(&pdev->dev, ae_dev);
1543
1544 pci_set_drvdata(pdev, NULL);
1545}
1546
1547static struct pci_driver hns3_driver = {
1548 .name = hns3_driver_name,
1549 .id_table = hns3_pci_tbl,
1550 .probe = hns3_probe,
1551 .remove = hns3_remove,
1552};
1553
1554/* set default feature to hns3 */
1555static void hns3_set_default_feature(struct net_device *netdev)
1556{
Jian Shen391b5e92018-01-05 18:18:05 +08001557 struct hnae3_handle *h = hns3_get_handle(netdev);
1558
Salil76ad4f02017-08-02 16:59:45 +01001559 netdev->priv_flags |= IFF_UNICAST_FLT;
1560
1561 netdev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1562 NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
1563 NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE |
1564 NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL |
1565 NETIF_F_GSO_UDP_TUNNEL_CSUM;
1566
1567 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
1568
1569 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
1570
1571 netdev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1572 NETIF_F_HW_VLAN_CTAG_FILTER |
Peng Li052ece62017-12-22 12:21:47 +08001573 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
Salil76ad4f02017-08-02 16:59:45 +01001574 NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
1575 NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE |
1576 NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL |
1577 NETIF_F_GSO_UDP_TUNNEL_CSUM;
1578
1579 netdev->vlan_features |=
1580 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM |
1581 NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO |
1582 NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE |
1583 NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL |
1584 NETIF_F_GSO_UDP_TUNNEL_CSUM;
1585
1586 netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
Jian Shen30ba2ab2018-01-05 18:18:06 +08001587 NETIF_F_HW_VLAN_CTAG_TX |
Salil76ad4f02017-08-02 16:59:45 +01001588 NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
1589 NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE |
1590 NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL |
1591 NETIF_F_GSO_UDP_TUNNEL_CSUM;
Jian Shen391b5e92018-01-05 18:18:05 +08001592
1593 if (!(h->flags & HNAE3_SUPPORT_VF))
Jian Shen30ba2ab2018-01-05 18:18:06 +08001594 netdev->hw_features |=
1595 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
Salil76ad4f02017-08-02 16:59:45 +01001596}
1597
1598static int hns3_alloc_buffer(struct hns3_enet_ring *ring,
1599 struct hns3_desc_cb *cb)
1600{
1601 unsigned int order = hnae_page_order(ring);
1602 struct page *p;
1603
1604 p = dev_alloc_pages(order);
1605 if (!p)
1606 return -ENOMEM;
1607
1608 cb->priv = p;
1609 cb->page_offset = 0;
1610 cb->reuse_flag = 0;
1611 cb->buf = page_address(p);
1612 cb->length = hnae_page_size(ring);
1613 cb->type = DESC_TYPE_PAGE;
1614
Salil76ad4f02017-08-02 16:59:45 +01001615 return 0;
1616}
1617
1618static void hns3_free_buffer(struct hns3_enet_ring *ring,
1619 struct hns3_desc_cb *cb)
1620{
1621 if (cb->type == DESC_TYPE_SKB)
1622 dev_kfree_skb_any((struct sk_buff *)cb->priv);
1623 else if (!HNAE3_IS_TX_RING(ring))
1624 put_page((struct page *)cb->priv);
1625 memset(cb, 0, sizeof(*cb));
1626}
1627
1628static int hns3_map_buffer(struct hns3_enet_ring *ring, struct hns3_desc_cb *cb)
1629{
1630 cb->dma = dma_map_page(ring_to_dev(ring), cb->priv, 0,
1631 cb->length, ring_to_dma_dir(ring));
1632
1633 if (dma_mapping_error(ring_to_dev(ring), cb->dma))
1634 return -EIO;
1635
1636 return 0;
1637}
1638
1639static void hns3_unmap_buffer(struct hns3_enet_ring *ring,
1640 struct hns3_desc_cb *cb)
1641{
1642 if (cb->type == DESC_TYPE_SKB)
1643 dma_unmap_single(ring_to_dev(ring), cb->dma, cb->length,
1644 ring_to_dma_dir(ring));
1645 else
1646 dma_unmap_page(ring_to_dev(ring), cb->dma, cb->length,
1647 ring_to_dma_dir(ring));
1648}
1649
1650static void hns3_buffer_detach(struct hns3_enet_ring *ring, int i)
1651{
1652 hns3_unmap_buffer(ring, &ring->desc_cb[i]);
1653 ring->desc[i].addr = 0;
1654}
1655
1656static void hns3_free_buffer_detach(struct hns3_enet_ring *ring, int i)
1657{
1658 struct hns3_desc_cb *cb = &ring->desc_cb[i];
1659
1660 if (!ring->desc_cb[i].dma)
1661 return;
1662
1663 hns3_buffer_detach(ring, i);
1664 hns3_free_buffer(ring, cb);
1665}
1666
1667static void hns3_free_buffers(struct hns3_enet_ring *ring)
1668{
1669 int i;
1670
1671 for (i = 0; i < ring->desc_num; i++)
1672 hns3_free_buffer_detach(ring, i);
1673}
1674
1675/* free desc along with its attached buffer */
1676static void hns3_free_desc(struct hns3_enet_ring *ring)
1677{
1678 hns3_free_buffers(ring);
1679
1680 dma_unmap_single(ring_to_dev(ring), ring->desc_dma_addr,
1681 ring->desc_num * sizeof(ring->desc[0]),
1682 DMA_BIDIRECTIONAL);
1683 ring->desc_dma_addr = 0;
1684 kfree(ring->desc);
1685 ring->desc = NULL;
1686}
1687
1688static int hns3_alloc_desc(struct hns3_enet_ring *ring)
1689{
1690 int size = ring->desc_num * sizeof(ring->desc[0]);
1691
1692 ring->desc = kzalloc(size, GFP_KERNEL);
1693 if (!ring->desc)
1694 return -ENOMEM;
1695
1696 ring->desc_dma_addr = dma_map_single(ring_to_dev(ring), ring->desc,
1697 size, DMA_BIDIRECTIONAL);
1698 if (dma_mapping_error(ring_to_dev(ring), ring->desc_dma_addr)) {
1699 ring->desc_dma_addr = 0;
1700 kfree(ring->desc);
1701 ring->desc = NULL;
1702 return -ENOMEM;
1703 }
1704
1705 return 0;
1706}
1707
1708static int hns3_reserve_buffer_map(struct hns3_enet_ring *ring,
1709 struct hns3_desc_cb *cb)
1710{
1711 int ret;
1712
1713 ret = hns3_alloc_buffer(ring, cb);
1714 if (ret)
1715 goto out;
1716
1717 ret = hns3_map_buffer(ring, cb);
1718 if (ret)
1719 goto out_with_buf;
1720
1721 return 0;
1722
1723out_with_buf:
Lipeng564883b2017-10-23 19:51:02 +08001724 hns3_free_buffer(ring, cb);
Salil76ad4f02017-08-02 16:59:45 +01001725out:
1726 return ret;
1727}
1728
1729static int hns3_alloc_buffer_attach(struct hns3_enet_ring *ring, int i)
1730{
1731 int ret = hns3_reserve_buffer_map(ring, &ring->desc_cb[i]);
1732
1733 if (ret)
1734 return ret;
1735
1736 ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma);
1737
1738 return 0;
1739}
1740
1741/* Allocate memory for raw pkg, and map with dma */
1742static int hns3_alloc_ring_buffers(struct hns3_enet_ring *ring)
1743{
1744 int i, j, ret;
1745
1746 for (i = 0; i < ring->desc_num; i++) {
1747 ret = hns3_alloc_buffer_attach(ring, i);
1748 if (ret)
1749 goto out_buffer_fail;
1750 }
1751
1752 return 0;
1753
1754out_buffer_fail:
1755 for (j = i - 1; j >= 0; j--)
1756 hns3_free_buffer_detach(ring, j);
1757 return ret;
1758}
1759
1760/* detach a in-used buffer and replace with a reserved one */
1761static void hns3_replace_buffer(struct hns3_enet_ring *ring, int i,
1762 struct hns3_desc_cb *res_cb)
1763{
Lipengb9077422017-10-23 19:51:01 +08001764 hns3_unmap_buffer(ring, &ring->desc_cb[i]);
Salil76ad4f02017-08-02 16:59:45 +01001765 ring->desc_cb[i] = *res_cb;
1766 ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma);
1767}
1768
1769static void hns3_reuse_buffer(struct hns3_enet_ring *ring, int i)
1770{
1771 ring->desc_cb[i].reuse_flag = 0;
1772 ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma
1773 + ring->desc_cb[i].page_offset);
1774}
1775
1776static void hns3_nic_reclaim_one_desc(struct hns3_enet_ring *ring, int *bytes,
1777 int *pkts)
1778{
1779 struct hns3_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_clean];
1780
1781 (*pkts) += (desc_cb->type == DESC_TYPE_SKB);
1782 (*bytes) += desc_cb->length;
1783 /* desc_cb will be cleaned, after hnae_free_buffer_detach*/
1784 hns3_free_buffer_detach(ring, ring->next_to_clean);
1785
1786 ring_ptr_move_fw(ring, next_to_clean);
1787}
1788
1789static int is_valid_clean_head(struct hns3_enet_ring *ring, int h)
1790{
1791 int u = ring->next_to_use;
1792 int c = ring->next_to_clean;
1793
1794 if (unlikely(h > ring->desc_num))
1795 return 0;
1796
1797 return u > c ? (h > c && h <= u) : (h > c || h <= u);
1798}
1799
Lipeng24e750c2017-10-23 19:51:07 +08001800bool hns3_clean_tx_ring(struct hns3_enet_ring *ring, int budget)
Salil76ad4f02017-08-02 16:59:45 +01001801{
1802 struct net_device *netdev = ring->tqp->handle->kinfo.netdev;
1803 struct netdev_queue *dev_queue;
1804 int bytes, pkts;
1805 int head;
1806
1807 head = readl_relaxed(ring->tqp->io_base + HNS3_RING_TX_RING_HEAD_REG);
1808 rmb(); /* Make sure head is ready before touch any data */
1809
1810 if (is_ring_empty(ring) || head == ring->next_to_clean)
Lipeng24e750c2017-10-23 19:51:07 +08001811 return true; /* no data to poll */
Salil76ad4f02017-08-02 16:59:45 +01001812
1813 if (!is_valid_clean_head(ring, head)) {
1814 netdev_err(netdev, "wrong head (%d, %d-%d)\n", head,
1815 ring->next_to_use, ring->next_to_clean);
1816
1817 u64_stats_update_begin(&ring->syncp);
1818 ring->stats.io_err_cnt++;
1819 u64_stats_update_end(&ring->syncp);
Lipeng24e750c2017-10-23 19:51:07 +08001820 return true;
Salil76ad4f02017-08-02 16:59:45 +01001821 }
1822
1823 bytes = 0;
1824 pkts = 0;
1825 while (head != ring->next_to_clean && budget) {
1826 hns3_nic_reclaim_one_desc(ring, &bytes, &pkts);
1827 /* Issue prefetch for next Tx descriptor */
1828 prefetch(&ring->desc_cb[ring->next_to_clean]);
1829 budget--;
1830 }
1831
1832 ring->tqp_vector->tx_group.total_bytes += bytes;
1833 ring->tqp_vector->tx_group.total_packets += pkts;
1834
1835 u64_stats_update_begin(&ring->syncp);
1836 ring->stats.tx_bytes += bytes;
1837 ring->stats.tx_pkts += pkts;
1838 u64_stats_update_end(&ring->syncp);
1839
1840 dev_queue = netdev_get_tx_queue(netdev, ring->tqp->tqp_index);
1841 netdev_tx_completed_queue(dev_queue, pkts, bytes);
1842
1843 if (unlikely(pkts && netif_carrier_ok(netdev) &&
1844 (ring_space(ring) > HNS3_MAX_BD_PER_PKT))) {
1845 /* Make sure that anybody stopping the queue after this
1846 * sees the new next_to_clean.
1847 */
1848 smp_mb();
1849 if (netif_tx_queue_stopped(dev_queue)) {
1850 netif_tx_wake_queue(dev_queue);
1851 ring->stats.restart_queue++;
1852 }
1853 }
1854
1855 return !!budget;
1856}
1857
1858static int hns3_desc_unused(struct hns3_enet_ring *ring)
1859{
1860 int ntc = ring->next_to_clean;
1861 int ntu = ring->next_to_use;
1862
1863 return ((ntc >= ntu) ? 0 : ring->desc_num) + ntc - ntu;
1864}
1865
1866static void
1867hns3_nic_alloc_rx_buffers(struct hns3_enet_ring *ring, int cleand_count)
1868{
1869 struct hns3_desc_cb *desc_cb;
1870 struct hns3_desc_cb res_cbs;
1871 int i, ret;
1872
1873 for (i = 0; i < cleand_count; i++) {
1874 desc_cb = &ring->desc_cb[ring->next_to_use];
1875 if (desc_cb->reuse_flag) {
1876 u64_stats_update_begin(&ring->syncp);
1877 ring->stats.reuse_pg_cnt++;
1878 u64_stats_update_end(&ring->syncp);
1879
1880 hns3_reuse_buffer(ring, ring->next_to_use);
1881 } else {
1882 ret = hns3_reserve_buffer_map(ring, &res_cbs);
1883 if (ret) {
1884 u64_stats_update_begin(&ring->syncp);
1885 ring->stats.sw_err_cnt++;
1886 u64_stats_update_end(&ring->syncp);
1887
1888 netdev_err(ring->tqp->handle->kinfo.netdev,
1889 "hnae reserve buffer map failed.\n");
1890 break;
1891 }
1892 hns3_replace_buffer(ring, ring->next_to_use, &res_cbs);
1893 }
1894
1895 ring_ptr_move_fw(ring, next_to_use);
1896 }
1897
1898 wmb(); /* Make all data has been write before submit */
1899 writel_relaxed(i, ring->tqp->io_base + HNS3_RING_RX_RING_HEAD_REG);
1900}
1901
1902/* hns3_nic_get_headlen - determine size of header for LRO/GRO
1903 * @data: pointer to the start of the headers
1904 * @max: total length of section to find headers in
1905 *
1906 * This function is meant to determine the length of headers that will
1907 * be recognized by hardware for LRO, GRO, and RSC offloads. The main
1908 * motivation of doing this is to only perform one pull for IPv4 TCP
1909 * packets so that we can do basic things like calculating the gso_size
1910 * based on the average data per packet.
1911 */
1912static unsigned int hns3_nic_get_headlen(unsigned char *data, u32 flag,
1913 unsigned int max_size)
1914{
1915 unsigned char *network;
1916 u8 hlen;
1917
1918 /* This should never happen, but better safe than sorry */
1919 if (max_size < ETH_HLEN)
1920 return max_size;
1921
1922 /* Initialize network frame pointer */
1923 network = data;
1924
1925 /* Set first protocol and move network header forward */
1926 network += ETH_HLEN;
1927
1928 /* Handle any vlan tag if present */
1929 if (hnae_get_field(flag, HNS3_RXD_VLAN_M, HNS3_RXD_VLAN_S)
1930 == HNS3_RX_FLAG_VLAN_PRESENT) {
1931 if ((typeof(max_size))(network - data) > (max_size - VLAN_HLEN))
1932 return max_size;
1933
1934 network += VLAN_HLEN;
1935 }
1936
1937 /* Handle L3 protocols */
1938 if (hnae_get_field(flag, HNS3_RXD_L3ID_M, HNS3_RXD_L3ID_S)
1939 == HNS3_RX_FLAG_L3ID_IPV4) {
1940 if ((typeof(max_size))(network - data) >
1941 (max_size - sizeof(struct iphdr)))
1942 return max_size;
1943
1944 /* Access ihl as a u8 to avoid unaligned access on ia64 */
1945 hlen = (network[0] & 0x0F) << 2;
1946
1947 /* Verify hlen meets minimum size requirements */
1948 if (hlen < sizeof(struct iphdr))
1949 return network - data;
1950
1951 /* Record next protocol if header is present */
1952 } else if (hnae_get_field(flag, HNS3_RXD_L3ID_M, HNS3_RXD_L3ID_S)
1953 == HNS3_RX_FLAG_L3ID_IPV6) {
1954 if ((typeof(max_size))(network - data) >
1955 (max_size - sizeof(struct ipv6hdr)))
1956 return max_size;
1957
1958 /* Record next protocol */
1959 hlen = sizeof(struct ipv6hdr);
1960 } else {
1961 return network - data;
1962 }
1963
1964 /* Relocate pointer to start of L4 header */
1965 network += hlen;
1966
1967 /* Finally sort out TCP/UDP */
1968 if (hnae_get_field(flag, HNS3_RXD_L4ID_M, HNS3_RXD_L4ID_S)
1969 == HNS3_RX_FLAG_L4ID_TCP) {
1970 if ((typeof(max_size))(network - data) >
1971 (max_size - sizeof(struct tcphdr)))
1972 return max_size;
1973
1974 /* Access doff as a u8 to avoid unaligned access on ia64 */
1975 hlen = (network[12] & 0xF0) >> 2;
1976
1977 /* Verify hlen meets minimum size requirements */
1978 if (hlen < sizeof(struct tcphdr))
1979 return network - data;
1980
1981 network += hlen;
1982 } else if (hnae_get_field(flag, HNS3_RXD_L4ID_M, HNS3_RXD_L4ID_S)
1983 == HNS3_RX_FLAG_L4ID_UDP) {
1984 if ((typeof(max_size))(network - data) >
1985 (max_size - sizeof(struct udphdr)))
1986 return max_size;
1987
1988 network += sizeof(struct udphdr);
1989 }
1990
1991 /* If everything has gone correctly network should be the
1992 * data section of the packet and will be the end of the header.
1993 * If not then it probably represents the end of the last recognized
1994 * header.
1995 */
1996 if ((typeof(max_size))(network - data) < max_size)
1997 return network - data;
1998 else
1999 return max_size;
2000}
2001
2002static void hns3_nic_reuse_page(struct sk_buff *skb, int i,
2003 struct hns3_enet_ring *ring, int pull_len,
2004 struct hns3_desc_cb *desc_cb)
2005{
2006 struct hns3_desc *desc;
2007 int truesize, size;
2008 int last_offset;
2009 bool twobufs;
2010
2011 twobufs = ((PAGE_SIZE < 8192) &&
2012 hnae_buf_size(ring) == HNS3_BUFFER_SIZE_2048);
2013
2014 desc = &ring->desc[ring->next_to_clean];
2015 size = le16_to_cpu(desc->rx.size);
2016
2017 if (twobufs) {
2018 truesize = hnae_buf_size(ring);
2019 } else {
2020 truesize = ALIGN(size, L1_CACHE_BYTES);
2021 last_offset = hnae_page_size(ring) - hnae_buf_size(ring);
2022 }
2023
2024 skb_add_rx_frag(skb, i, desc_cb->priv, desc_cb->page_offset + pull_len,
2025 size - pull_len, truesize - pull_len);
2026
2027 /* Avoid re-using remote pages,flag default unreuse */
2028 if (unlikely(page_to_nid(desc_cb->priv) != numa_node_id()))
2029 return;
2030
2031 if (twobufs) {
2032 /* If we are only owner of page we can reuse it */
2033 if (likely(page_count(desc_cb->priv) == 1)) {
2034 /* Flip page offset to other buffer */
2035 desc_cb->page_offset ^= truesize;
2036
2037 desc_cb->reuse_flag = 1;
2038 /* bump ref count on page before it is given*/
2039 get_page(desc_cb->priv);
2040 }
2041 return;
2042 }
2043
2044 /* Move offset up to the next cache line */
2045 desc_cb->page_offset += truesize;
2046
2047 if (desc_cb->page_offset <= last_offset) {
2048 desc_cb->reuse_flag = 1;
2049 /* Bump ref count on page before it is given*/
2050 get_page(desc_cb->priv);
2051 }
2052}
2053
2054static void hns3_rx_checksum(struct hns3_enet_ring *ring, struct sk_buff *skb,
2055 struct hns3_desc *desc)
2056{
2057 struct net_device *netdev = ring->tqp->handle->kinfo.netdev;
2058 int l3_type, l4_type;
2059 u32 bd_base_info;
2060 int ol4_type;
2061 u32 l234info;
2062
2063 bd_base_info = le32_to_cpu(desc->rx.bd_base_info);
2064 l234info = le32_to_cpu(desc->rx.l234_info);
2065
2066 skb->ip_summed = CHECKSUM_NONE;
2067
2068 skb_checksum_none_assert(skb);
2069
2070 if (!(netdev->features & NETIF_F_RXCSUM))
2071 return;
2072
2073 /* check if hardware has done checksum */
2074 if (!hnae_get_bit(bd_base_info, HNS3_RXD_L3L4P_B))
2075 return;
2076
2077 if (unlikely(hnae_get_bit(l234info, HNS3_RXD_L3E_B) ||
2078 hnae_get_bit(l234info, HNS3_RXD_L4E_B) ||
2079 hnae_get_bit(l234info, HNS3_RXD_OL3E_B) ||
2080 hnae_get_bit(l234info, HNS3_RXD_OL4E_B))) {
2081 netdev_err(netdev, "L3/L4 error pkt\n");
2082 u64_stats_update_begin(&ring->syncp);
2083 ring->stats.l3l4_csum_err++;
2084 u64_stats_update_end(&ring->syncp);
2085
2086 return;
2087 }
2088
2089 l3_type = hnae_get_field(l234info, HNS3_RXD_L3ID_M,
2090 HNS3_RXD_L3ID_S);
2091 l4_type = hnae_get_field(l234info, HNS3_RXD_L4ID_M,
2092 HNS3_RXD_L4ID_S);
2093
2094 ol4_type = hnae_get_field(l234info, HNS3_RXD_OL4ID_M, HNS3_RXD_OL4ID_S);
2095 switch (ol4_type) {
2096 case HNS3_OL4_TYPE_MAC_IN_UDP:
2097 case HNS3_OL4_TYPE_NVGRE:
2098 skb->csum_level = 1;
2099 case HNS3_OL4_TYPE_NO_TUN:
2100 /* Can checksum ipv4 or ipv6 + UDP/TCP/SCTP packets */
2101 if (l3_type == HNS3_L3_TYPE_IPV4 ||
2102 (l3_type == HNS3_L3_TYPE_IPV6 &&
2103 (l4_type == HNS3_L4_TYPE_UDP ||
2104 l4_type == HNS3_L4_TYPE_TCP ||
2105 l4_type == HNS3_L4_TYPE_SCTP)))
2106 skb->ip_summed = CHECKSUM_UNNECESSARY;
2107 break;
2108 }
2109}
2110
Yunsheng Lind43e5ac2017-10-20 10:19:21 +08002111static void hns3_rx_skb(struct hns3_enet_ring *ring, struct sk_buff *skb)
2112{
2113 napi_gro_receive(&ring->tqp_vector->napi, skb);
2114}
2115
Salil76ad4f02017-08-02 16:59:45 +01002116static int hns3_handle_rx_bd(struct hns3_enet_ring *ring,
2117 struct sk_buff **out_skb, int *out_bnum)
2118{
2119 struct net_device *netdev = ring->tqp->handle->kinfo.netdev;
2120 struct hns3_desc_cb *desc_cb;
2121 struct hns3_desc *desc;
2122 struct sk_buff *skb;
2123 unsigned char *va;
2124 u32 bd_base_info;
2125 int pull_len;
2126 u32 l234info;
2127 int length;
2128 int bnum;
2129
2130 desc = &ring->desc[ring->next_to_clean];
2131 desc_cb = &ring->desc_cb[ring->next_to_clean];
2132
2133 prefetch(desc);
2134
2135 length = le16_to_cpu(desc->rx.pkt_len);
2136 bd_base_info = le32_to_cpu(desc->rx.bd_base_info);
2137 l234info = le32_to_cpu(desc->rx.l234_info);
2138
2139 /* Check valid BD */
2140 if (!hnae_get_bit(bd_base_info, HNS3_RXD_VLD_B))
2141 return -EFAULT;
2142
2143 va = (unsigned char *)desc_cb->buf + desc_cb->page_offset;
2144
2145 /* Prefetch first cache line of first page
2146 * Idea is to cache few bytes of the header of the packet. Our L1 Cache
2147 * line size is 64B so need to prefetch twice to make it 128B. But in
2148 * actual we can have greater size of caches with 128B Level 1 cache
2149 * lines. In such a case, single fetch would suffice to cache in the
2150 * relevant part of the header.
2151 */
2152 prefetch(va);
2153#if L1_CACHE_BYTES < 128
2154 prefetch(va + L1_CACHE_BYTES);
2155#endif
2156
2157 skb = *out_skb = napi_alloc_skb(&ring->tqp_vector->napi,
2158 HNS3_RX_HEAD_SIZE);
2159 if (unlikely(!skb)) {
2160 netdev_err(netdev, "alloc rx skb fail\n");
2161
2162 u64_stats_update_begin(&ring->syncp);
2163 ring->stats.sw_err_cnt++;
2164 u64_stats_update_end(&ring->syncp);
2165
2166 return -ENOMEM;
2167 }
2168
2169 prefetchw(skb->data);
2170
Peng Li9699cff2017-12-22 12:21:48 +08002171 /* Based on hw strategy, the tag offloaded will be stored at
2172 * ot_vlan_tag in two layer tag case, and stored at vlan_tag
2173 * in one layer tag case.
2174 */
2175 if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX) {
2176 u16 vlan_tag;
2177
2178 vlan_tag = le16_to_cpu(desc->rx.ot_vlan_tag);
2179 if (!(vlan_tag & VLAN_VID_MASK))
2180 vlan_tag = le16_to_cpu(desc->rx.vlan_tag);
2181 if (vlan_tag & VLAN_VID_MASK)
2182 __vlan_hwaccel_put_tag(skb,
2183 htons(ETH_P_8021Q),
2184 vlan_tag);
2185 }
2186
Salil76ad4f02017-08-02 16:59:45 +01002187 bnum = 1;
2188 if (length <= HNS3_RX_HEAD_SIZE) {
2189 memcpy(__skb_put(skb, length), va, ALIGN(length, sizeof(long)));
2190
2191 /* We can reuse buffer as-is, just make sure it is local */
2192 if (likely(page_to_nid(desc_cb->priv) == numa_node_id()))
2193 desc_cb->reuse_flag = 1;
2194 else /* This page cannot be reused so discard it */
2195 put_page(desc_cb->priv);
2196
2197 ring_ptr_move_fw(ring, next_to_clean);
2198 } else {
2199 u64_stats_update_begin(&ring->syncp);
2200 ring->stats.seg_pkt_cnt++;
2201 u64_stats_update_end(&ring->syncp);
2202
2203 pull_len = hns3_nic_get_headlen(va, l234info,
2204 HNS3_RX_HEAD_SIZE);
2205 memcpy(__skb_put(skb, pull_len), va,
2206 ALIGN(pull_len, sizeof(long)));
2207
2208 hns3_nic_reuse_page(skb, 0, ring, pull_len, desc_cb);
2209 ring_ptr_move_fw(ring, next_to_clean);
2210
2211 while (!hnae_get_bit(bd_base_info, HNS3_RXD_FE_B)) {
2212 desc = &ring->desc[ring->next_to_clean];
2213 desc_cb = &ring->desc_cb[ring->next_to_clean];
2214 bd_base_info = le32_to_cpu(desc->rx.bd_base_info);
2215 hns3_nic_reuse_page(skb, bnum, ring, 0, desc_cb);
2216 ring_ptr_move_fw(ring, next_to_clean);
2217 bnum++;
2218 }
2219 }
2220
2221 *out_bnum = bnum;
2222
2223 if (unlikely(!hnae_get_bit(bd_base_info, HNS3_RXD_VLD_B))) {
2224 netdev_err(netdev, "no valid bd,%016llx,%016llx\n",
2225 ((u64 *)desc)[0], ((u64 *)desc)[1]);
2226 u64_stats_update_begin(&ring->syncp);
2227 ring->stats.non_vld_descs++;
2228 u64_stats_update_end(&ring->syncp);
2229
2230 dev_kfree_skb_any(skb);
2231 return -EINVAL;
2232 }
2233
2234 if (unlikely((!desc->rx.pkt_len) ||
2235 hnae_get_bit(l234info, HNS3_RXD_TRUNCAT_B))) {
2236 netdev_err(netdev, "truncated pkt\n");
2237 u64_stats_update_begin(&ring->syncp);
2238 ring->stats.err_pkt_len++;
2239 u64_stats_update_end(&ring->syncp);
2240
2241 dev_kfree_skb_any(skb);
2242 return -EFAULT;
2243 }
2244
2245 if (unlikely(hnae_get_bit(l234info, HNS3_RXD_L2E_B))) {
2246 netdev_err(netdev, "L2 error pkt\n");
2247 u64_stats_update_begin(&ring->syncp);
2248 ring->stats.l2_err++;
2249 u64_stats_update_end(&ring->syncp);
2250
2251 dev_kfree_skb_any(skb);
2252 return -EFAULT;
2253 }
2254
2255 u64_stats_update_begin(&ring->syncp);
2256 ring->stats.rx_pkts++;
2257 ring->stats.rx_bytes += skb->len;
2258 u64_stats_update_end(&ring->syncp);
2259
2260 ring->tqp_vector->rx_group.total_bytes += skb->len;
2261
2262 hns3_rx_checksum(ring, skb, desc);
2263 return 0;
2264}
2265
Yunsheng Lind43e5ac2017-10-20 10:19:21 +08002266int hns3_clean_rx_ring(
2267 struct hns3_enet_ring *ring, int budget,
2268 void (*rx_fn)(struct hns3_enet_ring *, struct sk_buff *))
Salil76ad4f02017-08-02 16:59:45 +01002269{
2270#define RCB_NOF_ALLOC_RX_BUFF_ONCE 16
2271 struct net_device *netdev = ring->tqp->handle->kinfo.netdev;
2272 int recv_pkts, recv_bds, clean_count, err;
2273 int unused_count = hns3_desc_unused(ring);
2274 struct sk_buff *skb = NULL;
2275 int num, bnum = 0;
2276
2277 num = readl_relaxed(ring->tqp->io_base + HNS3_RING_RX_RING_FBDNUM_REG);
2278 rmb(); /* Make sure num taken effect before the other data is touched */
2279
2280 recv_pkts = 0, recv_bds = 0, clean_count = 0;
2281 num -= unused_count;
2282
2283 while (recv_pkts < budget && recv_bds < num) {
2284 /* Reuse or realloc buffers */
2285 if (clean_count + unused_count >= RCB_NOF_ALLOC_RX_BUFF_ONCE) {
2286 hns3_nic_alloc_rx_buffers(ring,
2287 clean_count + unused_count);
2288 clean_count = 0;
2289 unused_count = hns3_desc_unused(ring);
2290 }
2291
2292 /* Poll one pkt */
2293 err = hns3_handle_rx_bd(ring, &skb, &bnum);
2294 if (unlikely(!skb)) /* This fault cannot be repaired */
2295 goto out;
2296
2297 recv_bds += bnum;
2298 clean_count += bnum;
2299 if (unlikely(err)) { /* Do jump the err */
2300 recv_pkts++;
2301 continue;
2302 }
2303
2304 /* Do update ip stack process */
2305 skb->protocol = eth_type_trans(skb, netdev);
Yunsheng Lind43e5ac2017-10-20 10:19:21 +08002306 rx_fn(ring, skb);
Salil76ad4f02017-08-02 16:59:45 +01002307
2308 recv_pkts++;
2309 }
2310
2311out:
2312 /* Make all data has been write before submit */
2313 if (clean_count + unused_count > 0)
2314 hns3_nic_alloc_rx_buffers(ring,
2315 clean_count + unused_count);
2316
2317 return recv_pkts;
2318}
2319
2320static bool hns3_get_new_int_gl(struct hns3_enet_ring_group *ring_group)
2321{
2322#define HNS3_RX_ULTRA_PACKET_RATE 40000
2323 enum hns3_flow_level_range new_flow_level;
2324 struct hns3_enet_tqp_vector *tqp_vector;
2325 int packets_per_secs;
2326 int bytes_per_usecs;
2327 u16 new_int_gl;
2328 int usecs;
2329
2330 if (!ring_group->int_gl)
2331 return false;
2332
2333 if (ring_group->total_packets == 0) {
2334 ring_group->int_gl = HNS3_INT_GL_50K;
2335 ring_group->flow_level = HNS3_FLOW_LOW;
2336 return true;
2337 }
2338
2339 /* Simple throttlerate management
2340 * 0-10MB/s lower (50000 ints/s)
2341 * 10-20MB/s middle (20000 ints/s)
2342 * 20-1249MB/s high (18000 ints/s)
2343 * > 40000pps ultra (8000 ints/s)
2344 */
2345 new_flow_level = ring_group->flow_level;
2346 new_int_gl = ring_group->int_gl;
2347 tqp_vector = ring_group->ring->tqp_vector;
2348 usecs = (ring_group->int_gl << 1);
2349 bytes_per_usecs = ring_group->total_bytes / usecs;
2350 /* 1000000 microseconds */
2351 packets_per_secs = ring_group->total_packets * 1000000 / usecs;
2352
2353 switch (new_flow_level) {
2354 case HNS3_FLOW_LOW:
2355 if (bytes_per_usecs > 10)
2356 new_flow_level = HNS3_FLOW_MID;
2357 break;
2358 case HNS3_FLOW_MID:
2359 if (bytes_per_usecs > 20)
2360 new_flow_level = HNS3_FLOW_HIGH;
2361 else if (bytes_per_usecs <= 10)
2362 new_flow_level = HNS3_FLOW_LOW;
2363 break;
2364 case HNS3_FLOW_HIGH:
2365 case HNS3_FLOW_ULTRA:
2366 default:
2367 if (bytes_per_usecs <= 20)
2368 new_flow_level = HNS3_FLOW_MID;
2369 break;
2370 }
2371
2372 if ((packets_per_secs > HNS3_RX_ULTRA_PACKET_RATE) &&
2373 (&tqp_vector->rx_group == ring_group))
2374 new_flow_level = HNS3_FLOW_ULTRA;
2375
2376 switch (new_flow_level) {
2377 case HNS3_FLOW_LOW:
2378 new_int_gl = HNS3_INT_GL_50K;
2379 break;
2380 case HNS3_FLOW_MID:
2381 new_int_gl = HNS3_INT_GL_20K;
2382 break;
2383 case HNS3_FLOW_HIGH:
2384 new_int_gl = HNS3_INT_GL_18K;
2385 break;
2386 case HNS3_FLOW_ULTRA:
2387 new_int_gl = HNS3_INT_GL_8K;
2388 break;
2389 default:
2390 break;
2391 }
2392
2393 ring_group->total_bytes = 0;
2394 ring_group->total_packets = 0;
2395 ring_group->flow_level = new_flow_level;
2396 if (new_int_gl != ring_group->int_gl) {
2397 ring_group->int_gl = new_int_gl;
2398 return true;
2399 }
2400 return false;
2401}
2402
2403static void hns3_update_new_int_gl(struct hns3_enet_tqp_vector *tqp_vector)
2404{
2405 u16 rx_int_gl, tx_int_gl;
2406 bool rx, tx;
2407
2408 rx = hns3_get_new_int_gl(&tqp_vector->rx_group);
2409 tx = hns3_get_new_int_gl(&tqp_vector->tx_group);
2410 rx_int_gl = tqp_vector->rx_group.int_gl;
2411 tx_int_gl = tqp_vector->tx_group.int_gl;
2412 if (rx && tx) {
2413 if (rx_int_gl > tx_int_gl) {
2414 tqp_vector->tx_group.int_gl = rx_int_gl;
2415 tqp_vector->tx_group.flow_level =
2416 tqp_vector->rx_group.flow_level;
2417 hns3_set_vector_coalesc_gl(tqp_vector, rx_int_gl);
2418 } else {
2419 tqp_vector->rx_group.int_gl = tx_int_gl;
2420 tqp_vector->rx_group.flow_level =
2421 tqp_vector->tx_group.flow_level;
2422 hns3_set_vector_coalesc_gl(tqp_vector, tx_int_gl);
2423 }
2424 }
2425}
2426
2427static int hns3_nic_common_poll(struct napi_struct *napi, int budget)
2428{
2429 struct hns3_enet_ring *ring;
2430 int rx_pkt_total = 0;
2431
2432 struct hns3_enet_tqp_vector *tqp_vector =
2433 container_of(napi, struct hns3_enet_tqp_vector, napi);
2434 bool clean_complete = true;
2435 int rx_budget;
2436
2437 /* Since the actual Tx work is minimal, we can give the Tx a larger
2438 * budget and be more aggressive about cleaning up the Tx descriptors.
2439 */
2440 hns3_for_each_ring(ring, tqp_vector->tx_group) {
2441 if (!hns3_clean_tx_ring(ring, budget))
2442 clean_complete = false;
2443 }
2444
2445 /* make sure rx ring budget not smaller than 1 */
2446 rx_budget = max(budget / tqp_vector->num_tqps, 1);
2447
2448 hns3_for_each_ring(ring, tqp_vector->rx_group) {
Yunsheng Lind43e5ac2017-10-20 10:19:21 +08002449 int rx_cleaned = hns3_clean_rx_ring(ring, rx_budget,
2450 hns3_rx_skb);
Salil76ad4f02017-08-02 16:59:45 +01002451
2452 if (rx_cleaned >= rx_budget)
2453 clean_complete = false;
2454
2455 rx_pkt_total += rx_cleaned;
2456 }
2457
2458 tqp_vector->rx_group.total_packets += rx_pkt_total;
2459
2460 if (!clean_complete)
2461 return budget;
2462
2463 napi_complete(napi);
2464 hns3_update_new_int_gl(tqp_vector);
2465 hns3_mask_vector_irq(tqp_vector, 1);
2466
2467 return rx_pkt_total;
2468}
2469
2470static int hns3_get_vector_ring_chain(struct hns3_enet_tqp_vector *tqp_vector,
2471 struct hnae3_ring_chain_node *head)
2472{
2473 struct pci_dev *pdev = tqp_vector->handle->pdev;
2474 struct hnae3_ring_chain_node *cur_chain = head;
2475 struct hnae3_ring_chain_node *chain;
2476 struct hns3_enet_ring *tx_ring;
2477 struct hns3_enet_ring *rx_ring;
2478
2479 tx_ring = tqp_vector->tx_group.ring;
2480 if (tx_ring) {
2481 cur_chain->tqp_index = tx_ring->tqp->tqp_index;
2482 hnae_set_bit(cur_chain->flag, HNAE3_RING_TYPE_B,
2483 HNAE3_RING_TYPE_TX);
2484
2485 cur_chain->next = NULL;
2486
2487 while (tx_ring->next) {
2488 tx_ring = tx_ring->next;
2489
2490 chain = devm_kzalloc(&pdev->dev, sizeof(*chain),
2491 GFP_KERNEL);
2492 if (!chain)
2493 return -ENOMEM;
2494
2495 cur_chain->next = chain;
2496 chain->tqp_index = tx_ring->tqp->tqp_index;
2497 hnae_set_bit(chain->flag, HNAE3_RING_TYPE_B,
2498 HNAE3_RING_TYPE_TX);
2499
2500 cur_chain = chain;
2501 }
2502 }
2503
2504 rx_ring = tqp_vector->rx_group.ring;
2505 if (!tx_ring && rx_ring) {
2506 cur_chain->next = NULL;
2507 cur_chain->tqp_index = rx_ring->tqp->tqp_index;
2508 hnae_set_bit(cur_chain->flag, HNAE3_RING_TYPE_B,
2509 HNAE3_RING_TYPE_RX);
2510
2511 rx_ring = rx_ring->next;
2512 }
2513
2514 while (rx_ring) {
2515 chain = devm_kzalloc(&pdev->dev, sizeof(*chain), GFP_KERNEL);
2516 if (!chain)
2517 return -ENOMEM;
2518
2519 cur_chain->next = chain;
2520 chain->tqp_index = rx_ring->tqp->tqp_index;
2521 hnae_set_bit(chain->flag, HNAE3_RING_TYPE_B,
2522 HNAE3_RING_TYPE_RX);
2523 cur_chain = chain;
2524
2525 rx_ring = rx_ring->next;
2526 }
2527
2528 return 0;
2529}
2530
2531static void hns3_free_vector_ring_chain(struct hns3_enet_tqp_vector *tqp_vector,
2532 struct hnae3_ring_chain_node *head)
2533{
2534 struct pci_dev *pdev = tqp_vector->handle->pdev;
2535 struct hnae3_ring_chain_node *chain_tmp, *chain;
2536
2537 chain = head->next;
2538
2539 while (chain) {
2540 chain_tmp = chain->next;
2541 devm_kfree(&pdev->dev, chain);
2542 chain = chain_tmp;
2543 }
2544}
2545
2546static void hns3_add_ring_to_group(struct hns3_enet_ring_group *group,
2547 struct hns3_enet_ring *ring)
2548{
2549 ring->next = group->ring;
2550 group->ring = ring;
2551
2552 group->count++;
2553}
2554
2555static int hns3_nic_init_vector_data(struct hns3_nic_priv *priv)
2556{
2557 struct hnae3_ring_chain_node vector_ring_chain;
2558 struct hnae3_handle *h = priv->ae_handle;
2559 struct hns3_enet_tqp_vector *tqp_vector;
2560 struct hnae3_vector_info *vector;
2561 struct pci_dev *pdev = h->pdev;
2562 u16 tqp_num = h->kinfo.num_tqps;
2563 u16 vector_num;
2564 int ret = 0;
2565 u16 i;
2566
2567 /* RSS size, cpu online and vector_num should be the same */
2568 /* Should consider 2p/4p later */
2569 vector_num = min_t(u16, num_online_cpus(), tqp_num);
2570 vector = devm_kcalloc(&pdev->dev, vector_num, sizeof(*vector),
2571 GFP_KERNEL);
2572 if (!vector)
2573 return -ENOMEM;
2574
2575 vector_num = h->ae_algo->ops->get_vector(h, vector_num, vector);
2576
2577 priv->vector_num = vector_num;
2578 priv->tqp_vector = (struct hns3_enet_tqp_vector *)
2579 devm_kcalloc(&pdev->dev, vector_num, sizeof(*priv->tqp_vector),
2580 GFP_KERNEL);
2581 if (!priv->tqp_vector)
2582 return -ENOMEM;
2583
2584 for (i = 0; i < tqp_num; i++) {
2585 u16 vector_i = i % vector_num;
2586
2587 tqp_vector = &priv->tqp_vector[vector_i];
2588
2589 hns3_add_ring_to_group(&tqp_vector->tx_group,
2590 priv->ring_data[i].ring);
2591
2592 hns3_add_ring_to_group(&tqp_vector->rx_group,
2593 priv->ring_data[i + tqp_num].ring);
2594
2595 tqp_vector->idx = vector_i;
2596 tqp_vector->mask_addr = vector[vector_i].io_addr;
2597 tqp_vector->vector_irq = vector[vector_i].vector;
2598 tqp_vector->num_tqps++;
2599
2600 priv->ring_data[i].ring->tqp_vector = tqp_vector;
2601 priv->ring_data[i + tqp_num].ring->tqp_vector = tqp_vector;
2602 }
2603
2604 for (i = 0; i < vector_num; i++) {
2605 tqp_vector = &priv->tqp_vector[i];
2606
2607 tqp_vector->rx_group.total_bytes = 0;
2608 tqp_vector->rx_group.total_packets = 0;
2609 tqp_vector->tx_group.total_bytes = 0;
2610 tqp_vector->tx_group.total_packets = 0;
2611 hns3_vector_gl_rl_init(tqp_vector);
2612 tqp_vector->handle = h;
2613
2614 ret = hns3_get_vector_ring_chain(tqp_vector,
2615 &vector_ring_chain);
2616 if (ret)
2617 goto out;
2618
2619 ret = h->ae_algo->ops->map_ring_to_vector(h,
2620 tqp_vector->vector_irq, &vector_ring_chain);
2621 if (ret)
2622 goto out;
2623
2624 hns3_free_vector_ring_chain(tqp_vector, &vector_ring_chain);
2625
2626 netif_napi_add(priv->netdev, &tqp_vector->napi,
2627 hns3_nic_common_poll, NAPI_POLL_WEIGHT);
2628 }
2629
2630out:
2631 devm_kfree(&pdev->dev, vector);
2632 return ret;
2633}
2634
2635static int hns3_nic_uninit_vector_data(struct hns3_nic_priv *priv)
2636{
2637 struct hnae3_ring_chain_node vector_ring_chain;
2638 struct hnae3_handle *h = priv->ae_handle;
2639 struct hns3_enet_tqp_vector *tqp_vector;
2640 struct pci_dev *pdev = h->pdev;
2641 int i, ret;
2642
2643 for (i = 0; i < priv->vector_num; i++) {
2644 tqp_vector = &priv->tqp_vector[i];
2645
2646 ret = hns3_get_vector_ring_chain(tqp_vector,
2647 &vector_ring_chain);
2648 if (ret)
2649 return ret;
2650
2651 ret = h->ae_algo->ops->unmap_ring_from_vector(h,
2652 tqp_vector->vector_irq, &vector_ring_chain);
2653 if (ret)
2654 return ret;
2655
2656 hns3_free_vector_ring_chain(tqp_vector, &vector_ring_chain);
2657
2658 if (priv->tqp_vector[i].irq_init_flag == HNS3_VECTOR_INITED) {
2659 (void)irq_set_affinity_hint(
2660 priv->tqp_vector[i].vector_irq,
2661 NULL);
qumingguangae064e62017-11-02 20:45:22 +08002662 free_irq(priv->tqp_vector[i].vector_irq,
2663 &priv->tqp_vector[i]);
Salil76ad4f02017-08-02 16:59:45 +01002664 }
2665
2666 priv->ring_data[i].ring->irq_init_flag = HNS3_VECTOR_NOT_INITED;
2667
2668 netif_napi_del(&priv->tqp_vector[i].napi);
2669 }
2670
2671 devm_kfree(&pdev->dev, priv->tqp_vector);
2672
2673 return 0;
2674}
2675
2676static int hns3_ring_get_cfg(struct hnae3_queue *q, struct hns3_nic_priv *priv,
2677 int ring_type)
2678{
2679 struct hns3_nic_ring_data *ring_data = priv->ring_data;
2680 int queue_num = priv->ae_handle->kinfo.num_tqps;
2681 struct pci_dev *pdev = priv->ae_handle->pdev;
2682 struct hns3_enet_ring *ring;
2683
2684 ring = devm_kzalloc(&pdev->dev, sizeof(*ring), GFP_KERNEL);
2685 if (!ring)
2686 return -ENOMEM;
2687
2688 if (ring_type == HNAE3_RING_TYPE_TX) {
2689 ring_data[q->tqp_index].ring = ring;
Lipeng66b44732017-10-23 19:51:05 +08002690 ring_data[q->tqp_index].queue_index = q->tqp_index;
Salil76ad4f02017-08-02 16:59:45 +01002691 ring->io_base = (u8 __iomem *)q->io_base + HNS3_TX_REG_OFFSET;
2692 } else {
2693 ring_data[q->tqp_index + queue_num].ring = ring;
Lipeng66b44732017-10-23 19:51:05 +08002694 ring_data[q->tqp_index + queue_num].queue_index = q->tqp_index;
Salil76ad4f02017-08-02 16:59:45 +01002695 ring->io_base = q->io_base;
2696 }
2697
2698 hnae_set_bit(ring->flag, HNAE3_RING_TYPE_B, ring_type);
2699
Salil76ad4f02017-08-02 16:59:45 +01002700 ring->tqp = q;
2701 ring->desc = NULL;
2702 ring->desc_cb = NULL;
2703 ring->dev = priv->dev;
2704 ring->desc_dma_addr = 0;
2705 ring->buf_size = q->buf_size;
2706 ring->desc_num = q->desc_num;
2707 ring->next_to_use = 0;
2708 ring->next_to_clean = 0;
2709
2710 return 0;
2711}
2712
2713static int hns3_queue_to_ring(struct hnae3_queue *tqp,
2714 struct hns3_nic_priv *priv)
2715{
2716 int ret;
2717
2718 ret = hns3_ring_get_cfg(tqp, priv, HNAE3_RING_TYPE_TX);
2719 if (ret)
2720 return ret;
2721
2722 ret = hns3_ring_get_cfg(tqp, priv, HNAE3_RING_TYPE_RX);
2723 if (ret)
2724 return ret;
2725
2726 return 0;
2727}
2728
2729static int hns3_get_ring_config(struct hns3_nic_priv *priv)
2730{
2731 struct hnae3_handle *h = priv->ae_handle;
2732 struct pci_dev *pdev = h->pdev;
2733 int i, ret;
2734
2735 priv->ring_data = devm_kzalloc(&pdev->dev, h->kinfo.num_tqps *
2736 sizeof(*priv->ring_data) * 2,
2737 GFP_KERNEL);
2738 if (!priv->ring_data)
2739 return -ENOMEM;
2740
2741 for (i = 0; i < h->kinfo.num_tqps; i++) {
2742 ret = hns3_queue_to_ring(h->kinfo.tqp[i], priv);
2743 if (ret)
2744 goto err;
2745 }
2746
2747 return 0;
2748err:
2749 devm_kfree(&pdev->dev, priv->ring_data);
2750 return ret;
2751}
2752
Peng Li09f2af62017-12-22 12:21:41 +08002753static void hns3_put_ring_config(struct hns3_nic_priv *priv)
2754{
2755 struct hnae3_handle *h = priv->ae_handle;
2756 int i;
2757
2758 for (i = 0; i < h->kinfo.num_tqps; i++) {
2759 devm_kfree(priv->dev, priv->ring_data[i].ring);
2760 devm_kfree(priv->dev,
2761 priv->ring_data[i + h->kinfo.num_tqps].ring);
2762 }
2763 devm_kfree(priv->dev, priv->ring_data);
2764}
2765
Salil76ad4f02017-08-02 16:59:45 +01002766static int hns3_alloc_ring_memory(struct hns3_enet_ring *ring)
2767{
2768 int ret;
2769
2770 if (ring->desc_num <= 0 || ring->buf_size <= 0)
2771 return -EINVAL;
2772
2773 ring->desc_cb = kcalloc(ring->desc_num, sizeof(ring->desc_cb[0]),
2774 GFP_KERNEL);
2775 if (!ring->desc_cb) {
2776 ret = -ENOMEM;
2777 goto out;
2778 }
2779
2780 ret = hns3_alloc_desc(ring);
2781 if (ret)
2782 goto out_with_desc_cb;
2783
2784 if (!HNAE3_IS_TX_RING(ring)) {
2785 ret = hns3_alloc_ring_buffers(ring);
2786 if (ret)
2787 goto out_with_desc;
2788 }
2789
2790 return 0;
2791
2792out_with_desc:
2793 hns3_free_desc(ring);
2794out_with_desc_cb:
2795 kfree(ring->desc_cb);
2796 ring->desc_cb = NULL;
2797out:
2798 return ret;
2799}
2800
2801static void hns3_fini_ring(struct hns3_enet_ring *ring)
2802{
2803 hns3_free_desc(ring);
2804 kfree(ring->desc_cb);
2805 ring->desc_cb = NULL;
2806 ring->next_to_clean = 0;
2807 ring->next_to_use = 0;
2808}
2809
Yunsheng Lin1db9b1b2017-10-09 15:44:01 +08002810static int hns3_buf_size2type(u32 buf_size)
Salil76ad4f02017-08-02 16:59:45 +01002811{
2812 int bd_size_type;
2813
2814 switch (buf_size) {
2815 case 512:
2816 bd_size_type = HNS3_BD_SIZE_512_TYPE;
2817 break;
2818 case 1024:
2819 bd_size_type = HNS3_BD_SIZE_1024_TYPE;
2820 break;
2821 case 2048:
2822 bd_size_type = HNS3_BD_SIZE_2048_TYPE;
2823 break;
2824 case 4096:
2825 bd_size_type = HNS3_BD_SIZE_4096_TYPE;
2826 break;
2827 default:
2828 bd_size_type = HNS3_BD_SIZE_2048_TYPE;
2829 }
2830
2831 return bd_size_type;
2832}
2833
2834static void hns3_init_ring_hw(struct hns3_enet_ring *ring)
2835{
2836 dma_addr_t dma = ring->desc_dma_addr;
2837 struct hnae3_queue *q = ring->tqp;
2838
2839 if (!HNAE3_IS_TX_RING(ring)) {
2840 hns3_write_dev(q, HNS3_RING_RX_RING_BASEADDR_L_REG,
2841 (u32)dma);
2842 hns3_write_dev(q, HNS3_RING_RX_RING_BASEADDR_H_REG,
2843 (u32)((dma >> 31) >> 1));
2844
2845 hns3_write_dev(q, HNS3_RING_RX_RING_BD_LEN_REG,
2846 hns3_buf_size2type(ring->buf_size));
2847 hns3_write_dev(q, HNS3_RING_RX_RING_BD_NUM_REG,
2848 ring->desc_num / 8 - 1);
2849
2850 } else {
2851 hns3_write_dev(q, HNS3_RING_TX_RING_BASEADDR_L_REG,
2852 (u32)dma);
2853 hns3_write_dev(q, HNS3_RING_TX_RING_BASEADDR_H_REG,
2854 (u32)((dma >> 31) >> 1));
2855
2856 hns3_write_dev(q, HNS3_RING_TX_RING_BD_LEN_REG,
2857 hns3_buf_size2type(ring->buf_size));
2858 hns3_write_dev(q, HNS3_RING_TX_RING_BD_NUM_REG,
2859 ring->desc_num / 8 - 1);
2860 }
2861}
2862
Lipeng5668abd2017-10-10 16:42:04 +08002863int hns3_init_all_ring(struct hns3_nic_priv *priv)
Salil76ad4f02017-08-02 16:59:45 +01002864{
2865 struct hnae3_handle *h = priv->ae_handle;
2866 int ring_num = h->kinfo.num_tqps * 2;
2867 int i, j;
2868 int ret;
2869
2870 for (i = 0; i < ring_num; i++) {
2871 ret = hns3_alloc_ring_memory(priv->ring_data[i].ring);
2872 if (ret) {
2873 dev_err(priv->dev,
2874 "Alloc ring memory fail! ret=%d\n", ret);
2875 goto out_when_alloc_ring_memory;
2876 }
2877
2878 hns3_init_ring_hw(priv->ring_data[i].ring);
2879
2880 u64_stats_init(&priv->ring_data[i].ring->syncp);
2881 }
2882
2883 return 0;
2884
2885out_when_alloc_ring_memory:
2886 for (j = i - 1; j >= 0; j--)
Lipengee83f772017-10-10 16:42:03 +08002887 hns3_fini_ring(priv->ring_data[j].ring);
Salil76ad4f02017-08-02 16:59:45 +01002888
2889 return -ENOMEM;
2890}
2891
Lipeng5668abd2017-10-10 16:42:04 +08002892int hns3_uninit_all_ring(struct hns3_nic_priv *priv)
Salil76ad4f02017-08-02 16:59:45 +01002893{
2894 struct hnae3_handle *h = priv->ae_handle;
2895 int i;
2896
2897 for (i = 0; i < h->kinfo.num_tqps; i++) {
2898 if (h->ae_algo->ops->reset_queue)
2899 h->ae_algo->ops->reset_queue(h, i);
2900
2901 hns3_fini_ring(priv->ring_data[i].ring);
Peng Li99fdf6b2017-12-22 12:21:43 +08002902 devm_kfree(priv->dev, priv->ring_data[i].ring);
Salil76ad4f02017-08-02 16:59:45 +01002903 hns3_fini_ring(priv->ring_data[i + h->kinfo.num_tqps].ring);
Peng Li99fdf6b2017-12-22 12:21:43 +08002904 devm_kfree(priv->dev,
2905 priv->ring_data[i + h->kinfo.num_tqps].ring);
Salil76ad4f02017-08-02 16:59:45 +01002906 }
Peng Li99fdf6b2017-12-22 12:21:43 +08002907 devm_kfree(priv->dev, priv->ring_data);
Salil76ad4f02017-08-02 16:59:45 +01002908
2909 return 0;
2910}
2911
2912/* Set mac addr if it is configured. or leave it to the AE driver */
2913static void hns3_init_mac_addr(struct net_device *netdev)
2914{
2915 struct hns3_nic_priv *priv = netdev_priv(netdev);
2916 struct hnae3_handle *h = priv->ae_handle;
2917 u8 mac_addr_temp[ETH_ALEN];
2918
2919 if (h->ae_algo->ops->get_mac_addr) {
2920 h->ae_algo->ops->get_mac_addr(h, mac_addr_temp);
2921 ether_addr_copy(netdev->dev_addr, mac_addr_temp);
2922 }
2923
2924 /* Check if the MAC address is valid, if not get a random one */
2925 if (!is_valid_ether_addr(netdev->dev_addr)) {
2926 eth_hw_addr_random(netdev);
2927 dev_warn(priv->dev, "using random MAC address %pM\n",
2928 netdev->dev_addr);
Salil76ad4f02017-08-02 16:59:45 +01002929 }
Lipeng139e8792017-09-19 17:17:13 +01002930
2931 if (h->ae_algo->ops->set_mac_addr)
2932 h->ae_algo->ops->set_mac_addr(h, netdev->dev_addr);
2933
Salil76ad4f02017-08-02 16:59:45 +01002934}
2935
2936static void hns3_nic_set_priv_ops(struct net_device *netdev)
2937{
2938 struct hns3_nic_priv *priv = netdev_priv(netdev);
2939
2940 if ((netdev->features & NETIF_F_TSO) ||
2941 (netdev->features & NETIF_F_TSO6)) {
2942 priv->ops.fill_desc = hns3_fill_desc_tso;
2943 priv->ops.maybe_stop_tx = hns3_nic_maybe_stop_tso;
2944 } else {
2945 priv->ops.fill_desc = hns3_fill_desc;
2946 priv->ops.maybe_stop_tx = hns3_nic_maybe_stop_tx;
2947 }
2948}
2949
2950static int hns3_client_init(struct hnae3_handle *handle)
2951{
2952 struct pci_dev *pdev = handle->pdev;
2953 struct hns3_nic_priv *priv;
2954 struct net_device *netdev;
2955 int ret;
2956
2957 netdev = alloc_etherdev_mq(sizeof(struct hns3_nic_priv),
2958 handle->kinfo.num_tqps);
2959 if (!netdev)
2960 return -ENOMEM;
2961
2962 priv = netdev_priv(netdev);
2963 priv->dev = &pdev->dev;
2964 priv->netdev = netdev;
2965 priv->ae_handle = handle;
Lipengf8fa222c2017-11-02 20:45:20 +08002966 priv->last_reset_time = jiffies;
2967 priv->reset_level = HNAE3_FUNC_RESET;
2968 priv->tx_timeout_count = 0;
Salil76ad4f02017-08-02 16:59:45 +01002969
2970 handle->kinfo.netdev = netdev;
2971 handle->priv = (void *)priv;
2972
2973 hns3_init_mac_addr(netdev);
2974
2975 hns3_set_default_feature(netdev);
2976
2977 netdev->watchdog_timeo = HNS3_TX_TIMEOUT;
2978 netdev->priv_flags |= IFF_UNICAST_FLT;
2979 netdev->netdev_ops = &hns3_nic_netdev_ops;
2980 SET_NETDEV_DEV(netdev, &pdev->dev);
2981 hns3_ethtool_set_ops(netdev);
2982 hns3_nic_set_priv_ops(netdev);
2983
2984 /* Carrier off reporting is important to ethtool even BEFORE open */
2985 netif_carrier_off(netdev);
2986
2987 ret = hns3_get_ring_config(priv);
2988 if (ret) {
2989 ret = -ENOMEM;
2990 goto out_get_ring_cfg;
2991 }
2992
2993 ret = hns3_nic_init_vector_data(priv);
2994 if (ret) {
2995 ret = -ENOMEM;
2996 goto out_init_vector_data;
2997 }
2998
2999 ret = hns3_init_all_ring(priv);
3000 if (ret) {
3001 ret = -ENOMEM;
3002 goto out_init_ring_data;
3003 }
3004
3005 ret = register_netdev(netdev);
3006 if (ret) {
3007 dev_err(priv->dev, "probe register netdev fail!\n");
3008 goto out_reg_netdev_fail;
3009 }
3010
Yunsheng Lin986743d2017-09-27 09:45:30 +08003011 hns3_dcbnl_setup(handle);
3012
Salila8e8b7f2017-08-21 17:05:24 +01003013 /* MTU range: (ETH_MIN_MTU(kernel default) - 9706) */
3014 netdev->max_mtu = HNS3_MAX_MTU - (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
3015
Salil76ad4f02017-08-02 16:59:45 +01003016 return ret;
3017
3018out_reg_netdev_fail:
3019out_init_ring_data:
3020 (void)hns3_nic_uninit_vector_data(priv);
3021 priv->ring_data = NULL;
3022out_init_vector_data:
3023out_get_ring_cfg:
3024 priv->ae_handle = NULL;
3025 free_netdev(netdev);
3026 return ret;
3027}
3028
3029static void hns3_client_uninit(struct hnae3_handle *handle, bool reset)
3030{
3031 struct net_device *netdev = handle->kinfo.netdev;
3032 struct hns3_nic_priv *priv = netdev_priv(netdev);
3033 int ret;
3034
3035 if (netdev->reg_state != NETREG_UNINITIALIZED)
3036 unregister_netdev(netdev);
3037
3038 ret = hns3_nic_uninit_vector_data(priv);
3039 if (ret)
3040 netdev_err(netdev, "uninit vector error\n");
3041
3042 ret = hns3_uninit_all_ring(priv);
3043 if (ret)
3044 netdev_err(netdev, "uninit ring error\n");
3045
3046 priv->ring_data = NULL;
3047
3048 free_netdev(netdev);
3049}
3050
3051static void hns3_link_status_change(struct hnae3_handle *handle, bool linkup)
3052{
3053 struct net_device *netdev = handle->kinfo.netdev;
3054
3055 if (!netdev)
3056 return;
3057
3058 if (linkup) {
3059 netif_carrier_on(netdev);
3060 netif_tx_wake_all_queues(netdev);
3061 netdev_info(netdev, "link up\n");
3062 } else {
3063 netif_carrier_off(netdev);
3064 netif_tx_stop_all_queues(netdev);
3065 netdev_info(netdev, "link down\n");
3066 }
3067}
3068
Yunsheng Lin9df8f792017-09-27 09:45:32 +08003069static int hns3_client_setup_tc(struct hnae3_handle *handle, u8 tc)
3070{
3071 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
3072 struct net_device *ndev = kinfo->netdev;
Colin Ian King075cfdd2017-09-29 20:51:23 +01003073 bool if_running;
Yunsheng Lin9df8f792017-09-27 09:45:32 +08003074 int ret;
3075 u8 i;
3076
3077 if (tc > HNAE3_MAX_TC)
3078 return -EINVAL;
3079
3080 if (!ndev)
3081 return -ENODEV;
3082
Colin Ian King075cfdd2017-09-29 20:51:23 +01003083 if_running = netif_running(ndev);
3084
Yunsheng Lin9df8f792017-09-27 09:45:32 +08003085 ret = netdev_set_num_tc(ndev, tc);
3086 if (ret)
3087 return ret;
3088
3089 if (if_running) {
3090 (void)hns3_nic_net_stop(ndev);
3091 msleep(100);
3092 }
3093
3094 ret = (kinfo->dcb_ops && kinfo->dcb_ops->map_update) ?
3095 kinfo->dcb_ops->map_update(handle) : -EOPNOTSUPP;
3096 if (ret)
3097 goto err_out;
3098
3099 if (tc <= 1) {
3100 netdev_reset_tc(ndev);
3101 goto out;
3102 }
3103
3104 for (i = 0; i < HNAE3_MAX_TC; i++) {
3105 struct hnae3_tc_info *tc_info = &kinfo->tc_info[i];
3106
3107 if (tc_info->enable)
3108 netdev_set_tc_queue(ndev,
3109 tc_info->tc,
3110 tc_info->tqp_count,
3111 tc_info->tqp_offset);
3112 }
3113
3114 for (i = 0; i < HNAE3_MAX_USER_PRIO; i++) {
3115 netdev_set_prio_tc_map(ndev, i,
3116 kinfo->prio_tc[i]);
3117 }
3118
3119out:
3120 ret = hns3_nic_set_real_num_queue(ndev);
3121
3122err_out:
3123 if (if_running)
3124 (void)hns3_nic_net_open(ndev);
3125
3126 return ret;
3127}
3128
Lipengbb6b94a2017-11-02 20:45:21 +08003129static void hns3_recover_hw_addr(struct net_device *ndev)
3130{
3131 struct netdev_hw_addr_list *list;
3132 struct netdev_hw_addr *ha, *tmp;
3133
3134 /* go through and sync uc_addr entries to the device */
3135 list = &ndev->uc;
3136 list_for_each_entry_safe(ha, tmp, &list->list, list)
3137 hns3_nic_uc_sync(ndev, ha->addr);
3138
3139 /* go through and sync mc_addr entries to the device */
3140 list = &ndev->mc;
3141 list_for_each_entry_safe(ha, tmp, &list->list, list)
3142 hns3_nic_mc_sync(ndev, ha->addr);
3143}
3144
3145static void hns3_drop_skb_data(struct hns3_enet_ring *ring, struct sk_buff *skb)
3146{
3147 dev_kfree_skb_any(skb);
3148}
3149
3150static void hns3_clear_all_ring(struct hnae3_handle *h)
3151{
3152 struct net_device *ndev = h->kinfo.netdev;
3153 struct hns3_nic_priv *priv = netdev_priv(ndev);
3154 u32 i;
3155
3156 for (i = 0; i < h->kinfo.num_tqps; i++) {
3157 struct netdev_queue *dev_queue;
3158 struct hns3_enet_ring *ring;
3159
3160 ring = priv->ring_data[i].ring;
3161 hns3_clean_tx_ring(ring, ring->desc_num);
3162 dev_queue = netdev_get_tx_queue(ndev,
3163 priv->ring_data[i].queue_index);
3164 netdev_tx_reset_queue(dev_queue);
3165
3166 ring = priv->ring_data[i + h->kinfo.num_tqps].ring;
3167 hns3_clean_rx_ring(ring, ring->desc_num, hns3_drop_skb_data);
3168 }
3169}
3170
3171static int hns3_reset_notify_down_enet(struct hnae3_handle *handle)
3172{
3173 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
3174 struct net_device *ndev = kinfo->netdev;
3175
3176 if (!netif_running(ndev))
3177 return -EIO;
3178
3179 return hns3_nic_net_stop(ndev);
3180}
3181
3182static int hns3_reset_notify_up_enet(struct hnae3_handle *handle)
3183{
3184 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
3185 struct hns3_nic_priv *priv = netdev_priv(kinfo->netdev);
3186 int ret = 0;
3187
3188 if (netif_running(kinfo->netdev)) {
3189 ret = hns3_nic_net_up(kinfo->netdev);
3190 if (ret) {
3191 netdev_err(kinfo->netdev,
3192 "hns net up fail, ret=%d!\n", ret);
3193 return ret;
3194 }
3195
3196 priv->last_reset_time = jiffies;
3197 }
3198
3199 return ret;
3200}
3201
3202static int hns3_reset_notify_init_enet(struct hnae3_handle *handle)
3203{
3204 struct net_device *netdev = handle->kinfo.netdev;
3205 struct hns3_nic_priv *priv = netdev_priv(netdev);
3206 int ret;
3207
3208 priv->reset_level = 1;
3209 hns3_init_mac_addr(netdev);
3210 hns3_nic_set_rx_mode(netdev);
3211 hns3_recover_hw_addr(netdev);
3212
3213 /* Carrier off reporting is important to ethtool even BEFORE open */
3214 netif_carrier_off(netdev);
3215
3216 ret = hns3_get_ring_config(priv);
3217 if (ret)
3218 return ret;
3219
3220 ret = hns3_nic_init_vector_data(priv);
3221 if (ret)
3222 return ret;
3223
3224 ret = hns3_init_all_ring(priv);
3225 if (ret) {
3226 hns3_nic_uninit_vector_data(priv);
3227 priv->ring_data = NULL;
3228 }
3229
3230 return ret;
3231}
3232
3233static int hns3_reset_notify_uninit_enet(struct hnae3_handle *handle)
3234{
3235 struct net_device *netdev = handle->kinfo.netdev;
3236 struct hns3_nic_priv *priv = netdev_priv(netdev);
3237 int ret;
3238
3239 hns3_clear_all_ring(handle);
3240
3241 ret = hns3_nic_uninit_vector_data(priv);
3242 if (ret) {
3243 netdev_err(netdev, "uninit vector error\n");
3244 return ret;
3245 }
3246
3247 ret = hns3_uninit_all_ring(priv);
3248 if (ret)
3249 netdev_err(netdev, "uninit ring error\n");
3250
3251 priv->ring_data = NULL;
3252
3253 return ret;
3254}
3255
3256static int hns3_reset_notify(struct hnae3_handle *handle,
3257 enum hnae3_reset_notify_type type)
3258{
3259 int ret = 0;
3260
3261 switch (type) {
3262 case HNAE3_UP_CLIENT:
3263 ret = hns3_reset_notify_up_enet(handle);
3264 break;
3265 case HNAE3_DOWN_CLIENT:
3266 ret = hns3_reset_notify_down_enet(handle);
3267 break;
3268 case HNAE3_INIT_CLIENT:
3269 ret = hns3_reset_notify_init_enet(handle);
3270 break;
3271 case HNAE3_UNINIT_CLIENT:
3272 ret = hns3_reset_notify_uninit_enet(handle);
3273 break;
3274 default:
3275 break;
3276 }
3277
3278 return ret;
3279}
3280
Peng Li09f2af62017-12-22 12:21:41 +08003281static u16 hns3_get_max_available_channels(struct net_device *netdev)
3282{
3283 struct hnae3_handle *h = hns3_get_handle(netdev);
3284 u16 free_tqps, max_rss_size, max_tqps;
3285
3286 h->ae_algo->ops->get_tqps_and_rss_info(h, &free_tqps, &max_rss_size);
3287 max_tqps = h->kinfo.num_tc * max_rss_size;
3288
3289 return min_t(u16, max_tqps, (free_tqps + h->kinfo.num_tqps));
3290}
3291
3292static int hns3_modify_tqp_num(struct net_device *netdev, u16 new_tqp_num)
3293{
3294 struct hns3_nic_priv *priv = netdev_priv(netdev);
3295 struct hnae3_handle *h = hns3_get_handle(netdev);
3296 int ret;
3297
3298 ret = h->ae_algo->ops->set_channels(h, new_tqp_num);
3299 if (ret)
3300 return ret;
3301
3302 ret = hns3_get_ring_config(priv);
3303 if (ret)
3304 return ret;
3305
3306 ret = hns3_nic_init_vector_data(priv);
3307 if (ret)
3308 goto err_uninit_vector;
3309
3310 ret = hns3_init_all_ring(priv);
3311 if (ret)
3312 goto err_put_ring;
3313
3314 return 0;
3315
3316err_put_ring:
3317 hns3_put_ring_config(priv);
3318err_uninit_vector:
3319 hns3_nic_uninit_vector_data(priv);
3320 return ret;
3321}
3322
3323static int hns3_adjust_tqps_num(u8 num_tc, u32 new_tqp_num)
3324{
3325 return (new_tqp_num / num_tc) * num_tc;
3326}
3327
3328int hns3_set_channels(struct net_device *netdev,
3329 struct ethtool_channels *ch)
3330{
3331 struct hns3_nic_priv *priv = netdev_priv(netdev);
3332 struct hnae3_handle *h = hns3_get_handle(netdev);
3333 struct hnae3_knic_private_info *kinfo = &h->kinfo;
3334 bool if_running = netif_running(netdev);
3335 u32 new_tqp_num = ch->combined_count;
3336 u16 org_tqp_num;
3337 int ret;
3338
3339 if (ch->rx_count || ch->tx_count)
3340 return -EINVAL;
3341
3342 if (new_tqp_num > hns3_get_max_available_channels(netdev) ||
3343 new_tqp_num < kinfo->num_tc) {
3344 dev_err(&netdev->dev,
3345 "Change tqps fail, the tqp range is from %d to %d",
3346 kinfo->num_tc,
3347 hns3_get_max_available_channels(netdev));
3348 return -EINVAL;
3349 }
3350
3351 new_tqp_num = hns3_adjust_tqps_num(kinfo->num_tc, new_tqp_num);
3352 if (kinfo->num_tqps == new_tqp_num)
3353 return 0;
3354
3355 if (if_running)
3356 dev_close(netdev);
3357
3358 hns3_clear_all_ring(h);
3359
3360 ret = hns3_nic_uninit_vector_data(priv);
3361 if (ret) {
3362 dev_err(&netdev->dev,
3363 "Unbind vector with tqp fail, nothing is changed");
3364 goto open_netdev;
3365 }
3366
3367 hns3_uninit_all_ring(priv);
3368
3369 org_tqp_num = h->kinfo.num_tqps;
3370 ret = hns3_modify_tqp_num(netdev, new_tqp_num);
3371 if (ret) {
3372 ret = hns3_modify_tqp_num(netdev, org_tqp_num);
3373 if (ret) {
3374 /* If revert to old tqp failed, fatal error occurred */
3375 dev_err(&netdev->dev,
3376 "Revert to old tqp num fail, ret=%d", ret);
3377 return ret;
3378 }
3379 dev_info(&netdev->dev,
3380 "Change tqp num fail, Revert to old tqp num");
3381 }
3382
3383open_netdev:
3384 if (if_running)
3385 dev_open(netdev);
3386
3387 return ret;
3388}
3389
Yunsheng Lin1db9b1b2017-10-09 15:44:01 +08003390static const struct hnae3_client_ops client_ops = {
Salil76ad4f02017-08-02 16:59:45 +01003391 .init_instance = hns3_client_init,
3392 .uninit_instance = hns3_client_uninit,
3393 .link_status_change = hns3_link_status_change,
Yunsheng Lin9df8f792017-09-27 09:45:32 +08003394 .setup_tc = hns3_client_setup_tc,
Lipengbb6b94a2017-11-02 20:45:21 +08003395 .reset_notify = hns3_reset_notify,
Salil76ad4f02017-08-02 16:59:45 +01003396};
3397
3398/* hns3_init_module - Driver registration routine
3399 * hns3_init_module is the first routine called when the driver is
3400 * loaded. All it does is register with the PCI subsystem.
3401 */
3402static int __init hns3_init_module(void)
3403{
3404 int ret;
3405
3406 pr_info("%s: %s - version\n", hns3_driver_name, hns3_driver_string);
3407 pr_info("%s: %s\n", hns3_driver_name, hns3_copyright);
3408
3409 client.type = HNAE3_CLIENT_KNIC;
3410 snprintf(client.name, HNAE3_CLIENT_NAME_LENGTH - 1, "%s",
3411 hns3_driver_name);
3412
3413 client.ops = &client_ops;
3414
3415 ret = hnae3_register_client(&client);
3416 if (ret)
3417 return ret;
3418
3419 ret = pci_register_driver(&hns3_driver);
3420 if (ret)
3421 hnae3_unregister_client(&client);
3422
3423 return ret;
3424}
3425module_init(hns3_init_module);
3426
3427/* hns3_exit_module - Driver exit cleanup routine
3428 * hns3_exit_module is called just before the driver is removed
3429 * from memory.
3430 */
3431static void __exit hns3_exit_module(void)
3432{
3433 pci_unregister_driver(&hns3_driver);
3434 hnae3_unregister_client(&client);
3435}
3436module_exit(hns3_exit_module);
3437
3438MODULE_DESCRIPTION("HNS3: Hisilicon Ethernet Driver");
3439MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
3440MODULE_LICENSE("GPL");
3441MODULE_ALIAS("pci:hns-nic");