net: hns: fix skb->truesize underestimation
[linux-2.6-block.git] / drivers / net / ethernet / hisilicon / hns / hns_enet.c
CommitLineData
b5996f11 1/*
2 * Copyright (c) 2014-2015 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/clk.h>
11#include <linux/cpumask.h>
12#include <linux/etherdevice.h>
13#include <linux/if_vlan.h>
14#include <linux/interrupt.h>
15#include <linux/io.h>
16#include <linux/ip.h>
17#include <linux/ipv6.h>
18#include <linux/module.h>
19#include <linux/phy.h>
20#include <linux/platform_device.h>
21#include <linux/skbuff.h>
22
23#include "hnae.h"
24#include "hns_enet.h"
44770e11 25#include "hns_dsaf_mac.h"
b5996f11 26
27#define NIC_MAX_Q_PER_VF 16
28#define HNS_NIC_TX_TIMEOUT (5 * HZ)
29
30#define SERVICE_TIMER_HZ (1 * HZ)
31
32#define NIC_TX_CLEAN_MAX_NUM 256
33#define NIC_RX_CLEAN_MAX_NUM 64
34
b5996f11 35#define RCB_IRQ_NOT_INITED 0
36#define RCB_IRQ_INITED 1
9cbe9fd5 37#define HNS_BUFFER_SIZE_2048 2048
b5996f11 38
13ac695e
S
39#define BD_MAX_SEND_SIZE 8191
40#define SKB_TMP_LEN(SKB) \
41 (((SKB)->transport_header - (SKB)->mac_header) + tcp_hdrlen(SKB))
42
43static void fill_v2_desc(struct hnae_ring *ring, void *priv,
44 int size, dma_addr_t dma, int frag_end,
45 int buf_num, enum hns_desc_type type, int mtu)
46{
47 struct hnae_desc *desc = &ring->desc[ring->next_to_use];
48 struct hnae_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_use];
49 struct iphdr *iphdr;
50 struct ipv6hdr *ipv6hdr;
51 struct sk_buff *skb;
13ac695e
S
52 __be16 protocol;
53 u8 bn_pid = 0;
54 u8 rrcfv = 0;
55 u8 ip_offset = 0;
56 u8 tvsvsn = 0;
57 u16 mss = 0;
58 u8 l4_len = 0;
59 u16 paylen = 0;
60
61 desc_cb->priv = priv;
62 desc_cb->length = size;
63 desc_cb->dma = dma;
64 desc_cb->type = type;
65
66 desc->addr = cpu_to_le64(dma);
67 desc->tx.send_size = cpu_to_le16((u16)size);
68
f8a1a636 69 /* config bd buffer end */
13ac695e
S
70 hnae_set_bit(rrcfv, HNSV2_TXD_VLD_B, 1);
71 hnae_set_field(bn_pid, HNSV2_TXD_BUFNUM_M, 0, buf_num - 1);
72
f8a1a636
SL
73 /* fill port_id in the tx bd for sending management pkts */
74 hnae_set_field(bn_pid, HNSV2_TXD_PORTID_M,
75 HNSV2_TXD_PORTID_S, ring->q->handle->dport_id);
76
13ac695e
S
77 if (type == DESC_TYPE_SKB) {
78 skb = (struct sk_buff *)priv;
79
80 if (skb->ip_summed == CHECKSUM_PARTIAL) {
81 skb_reset_mac_len(skb);
82 protocol = skb->protocol;
83 ip_offset = ETH_HLEN;
84
85 if (protocol == htons(ETH_P_8021Q)) {
86 ip_offset += VLAN_HLEN;
87 protocol = vlan_get_protocol(skb);
88 skb->protocol = protocol;
89 }
90
91 if (skb->protocol == htons(ETH_P_IP)) {
92 iphdr = ip_hdr(skb);
93 hnae_set_bit(rrcfv, HNSV2_TXD_L3CS_B, 1);
94 hnae_set_bit(rrcfv, HNSV2_TXD_L4CS_B, 1);
95
96 /* check for tcp/udp header */
0b51b1dc
DH
97 if (iphdr->protocol == IPPROTO_TCP &&
98 skb_is_gso(skb)) {
13ac695e
S
99 hnae_set_bit(tvsvsn,
100 HNSV2_TXD_TSE_B, 1);
13ac695e 101 l4_len = tcp_hdrlen(skb);
0b51b1dc
DH
102 mss = skb_shinfo(skb)->gso_size;
103 paylen = skb->len - SKB_TMP_LEN(skb);
13ac695e
S
104 }
105 } else if (skb->protocol == htons(ETH_P_IPV6)) {
106 hnae_set_bit(tvsvsn, HNSV2_TXD_IPV6_B, 1);
107 ipv6hdr = ipv6_hdr(skb);
108 hnae_set_bit(rrcfv, HNSV2_TXD_L4CS_B, 1);
109
110 /* check for tcp/udp header */
0b51b1dc
DH
111 if (ipv6hdr->nexthdr == IPPROTO_TCP &&
112 skb_is_gso(skb) && skb_is_gso_v6(skb)) {
13ac695e
S
113 hnae_set_bit(tvsvsn,
114 HNSV2_TXD_TSE_B, 1);
13ac695e 115 l4_len = tcp_hdrlen(skb);
0b51b1dc
DH
116 mss = skb_shinfo(skb)->gso_size;
117 paylen = skb->len - SKB_TMP_LEN(skb);
13ac695e
S
118 }
119 }
120 desc->tx.ip_offset = ip_offset;
121 desc->tx.tse_vlan_snap_v6_sctp_nth = tvsvsn;
122 desc->tx.mss = cpu_to_le16(mss);
123 desc->tx.l4_len = l4_len;
124 desc->tx.paylen = cpu_to_le16(paylen);
125 }
126 }
127
128 hnae_set_bit(rrcfv, HNSV2_TXD_FE_B, frag_end);
129
130 desc->tx.bn_pid = bn_pid;
131 desc->tx.ra_ri_cs_fe_vld = rrcfv;
132
133 ring_ptr_move_fw(ring, next_to_use);
134}
135
63434888
KY
136static const struct acpi_device_id hns_enet_acpi_match[] = {
137 { "HISI00C1", 0 },
138 { "HISI00C2", 0 },
139 { },
140};
141MODULE_DEVICE_TABLE(acpi, hns_enet_acpi_match);
142
b5996f11 143static void fill_desc(struct hnae_ring *ring, void *priv,
144 int size, dma_addr_t dma, int frag_end,
13ac695e 145 int buf_num, enum hns_desc_type type, int mtu)
b5996f11 146{
147 struct hnae_desc *desc = &ring->desc[ring->next_to_use];
148 struct hnae_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_use];
149 struct sk_buff *skb;
150 __be16 protocol;
151 u32 ip_offset;
152 u32 asid_bufnum_pid = 0;
153 u32 flag_ipoffset = 0;
154
155 desc_cb->priv = priv;
156 desc_cb->length = size;
157 desc_cb->dma = dma;
158 desc_cb->type = type;
159
160 desc->addr = cpu_to_le64(dma);
161 desc->tx.send_size = cpu_to_le16((u16)size);
162
163 /*config bd buffer end */
164 flag_ipoffset |= 1 << HNS_TXD_VLD_B;
165
166 asid_bufnum_pid |= buf_num << HNS_TXD_BUFNUM_S;
167
168 if (type == DESC_TYPE_SKB) {
169 skb = (struct sk_buff *)priv;
170
171 if (skb->ip_summed == CHECKSUM_PARTIAL) {
172 protocol = skb->protocol;
173 ip_offset = ETH_HLEN;
174
175 /*if it is a SW VLAN check the next protocol*/
176 if (protocol == htons(ETH_P_8021Q)) {
177 ip_offset += VLAN_HLEN;
178 protocol = vlan_get_protocol(skb);
179 skb->protocol = protocol;
180 }
181
182 if (skb->protocol == htons(ETH_P_IP)) {
183 flag_ipoffset |= 1 << HNS_TXD_L3CS_B;
184 /* check for tcp/udp header */
185 flag_ipoffset |= 1 << HNS_TXD_L4CS_B;
186
187 } else if (skb->protocol == htons(ETH_P_IPV6)) {
188 /* ipv6 has not l3 cs, check for L4 header */
189 flag_ipoffset |= 1 << HNS_TXD_L4CS_B;
190 }
191
192 flag_ipoffset |= ip_offset << HNS_TXD_IPOFFSET_S;
193 }
194 }
195
196 flag_ipoffset |= frag_end << HNS_TXD_FE_B;
197
198 desc->tx.asid_bufnum_pid = cpu_to_le16(asid_bufnum_pid);
199 desc->tx.flag_ipoffset = cpu_to_le32(flag_ipoffset);
200
201 ring_ptr_move_fw(ring, next_to_use);
202}
203
204static void unfill_desc(struct hnae_ring *ring)
205{
206 ring_ptr_move_bw(ring, next_to_use);
207}
208
13ac695e
S
209static int hns_nic_maybe_stop_tx(
210 struct sk_buff **out_skb, int *bnum, struct hnae_ring *ring)
b5996f11 211{
13ac695e
S
212 struct sk_buff *skb = *out_skb;
213 struct sk_buff *new_skb = NULL;
b5996f11 214 int buf_num;
b5996f11 215
216 /* no. of segments (plus a header) */
217 buf_num = skb_shinfo(skb)->nr_frags + 1;
218
219 if (unlikely(buf_num > ring->max_desc_num_per_pkt)) {
13ac695e
S
220 if (ring_space(ring) < 1)
221 return -EBUSY;
b5996f11 222
223 new_skb = skb_copy(skb, GFP_ATOMIC);
13ac695e
S
224 if (!new_skb)
225 return -ENOMEM;
b5996f11 226
227 dev_kfree_skb_any(skb);
13ac695e 228 *out_skb = new_skb;
b5996f11 229 buf_num = 1;
b5996f11 230 } else if (buf_num > ring_space(ring)) {
13ac695e
S
231 return -EBUSY;
232 }
233
234 *bnum = buf_num;
235 return 0;
236}
237
64353af6
S
238static int hns_nic_maybe_stop_tso(
239 struct sk_buff **out_skb, int *bnum, struct hnae_ring *ring)
240{
241 int i;
242 int size;
243 int buf_num;
244 int frag_num;
245 struct sk_buff *skb = *out_skb;
246 struct sk_buff *new_skb = NULL;
247 struct skb_frag_struct *frag;
248
249 size = skb_headlen(skb);
250 buf_num = (size + BD_MAX_SEND_SIZE - 1) / BD_MAX_SEND_SIZE;
251
252 frag_num = skb_shinfo(skb)->nr_frags;
253 for (i = 0; i < frag_num; i++) {
254 frag = &skb_shinfo(skb)->frags[i];
255 size = skb_frag_size(frag);
256 buf_num += (size + BD_MAX_SEND_SIZE - 1) / BD_MAX_SEND_SIZE;
257 }
258
259 if (unlikely(buf_num > ring->max_desc_num_per_pkt)) {
260 buf_num = (skb->len + BD_MAX_SEND_SIZE - 1) / BD_MAX_SEND_SIZE;
261 if (ring_space(ring) < buf_num)
262 return -EBUSY;
263 /* manual split the send packet */
264 new_skb = skb_copy(skb, GFP_ATOMIC);
265 if (!new_skb)
266 return -ENOMEM;
267 dev_kfree_skb_any(skb);
268 *out_skb = new_skb;
269
270 } else if (ring_space(ring) < buf_num) {
271 return -EBUSY;
272 }
273
274 *bnum = buf_num;
275 return 0;
276}
277
278static void fill_tso_desc(struct hnae_ring *ring, void *priv,
279 int size, dma_addr_t dma, int frag_end,
280 int buf_num, enum hns_desc_type type, int mtu)
281{
282 int frag_buf_num;
283 int sizeoflast;
284 int k;
285
286 frag_buf_num = (size + BD_MAX_SEND_SIZE - 1) / BD_MAX_SEND_SIZE;
287 sizeoflast = size % BD_MAX_SEND_SIZE;
288 sizeoflast = sizeoflast ? sizeoflast : BD_MAX_SEND_SIZE;
289
290 /* when the frag size is bigger than hardware, split this frag */
291 for (k = 0; k < frag_buf_num; k++)
292 fill_v2_desc(ring, priv,
293 (k == frag_buf_num - 1) ?
294 sizeoflast : BD_MAX_SEND_SIZE,
295 dma + BD_MAX_SEND_SIZE * k,
296 frag_end && (k == frag_buf_num - 1) ? 1 : 0,
297 buf_num,
298 (type == DESC_TYPE_SKB && !k) ?
299 DESC_TYPE_SKB : DESC_TYPE_PAGE,
300 mtu);
301}
302
27463ad9
YL
303netdev_tx_t hns_nic_net_xmit_hw(struct net_device *ndev,
304 struct sk_buff *skb,
305 struct hns_nic_ring_data *ring_data)
13ac695e
S
306{
307 struct hns_nic_priv *priv = netdev_priv(ndev);
13ac695e 308 struct hnae_ring *ring = ring_data->ring;
b85ea006 309 struct device *dev = ring_to_dev(ring);
13ac695e
S
310 struct netdev_queue *dev_queue;
311 struct skb_frag_struct *frag;
312 int buf_num;
313 int seg_num;
314 dma_addr_t dma;
315 int size, next_to_use;
316 int i;
317
318 switch (priv->ops.maybe_stop_tx(&skb, &buf_num, ring)) {
319 case -EBUSY:
b5996f11 320 ring->stats.tx_busy++;
321 goto out_net_tx_busy;
13ac695e
S
322 case -ENOMEM:
323 ring->stats.sw_err_cnt++;
324 netdev_err(ndev, "no memory to xmit!\n");
325 goto out_err_tx_ok;
326 default:
327 break;
b5996f11 328 }
13ac695e
S
329
330 /* no. of segments (plus a header) */
331 seg_num = skb_shinfo(skb)->nr_frags + 1;
b5996f11 332 next_to_use = ring->next_to_use;
333
334 /* fill the first part */
335 size = skb_headlen(skb);
336 dma = dma_map_single(dev, skb->data, size, DMA_TO_DEVICE);
337 if (dma_mapping_error(dev, dma)) {
338 netdev_err(ndev, "TX head DMA map failed\n");
339 ring->stats.sw_err_cnt++;
340 goto out_err_tx_ok;
341 }
13ac695e
S
342 priv->ops.fill_desc(ring, skb, size, dma, seg_num == 1 ? 1 : 0,
343 buf_num, DESC_TYPE_SKB, ndev->mtu);
b5996f11 344
345 /* fill the fragments */
13ac695e 346 for (i = 1; i < seg_num; i++) {
b5996f11 347 frag = &skb_shinfo(skb)->frags[i - 1];
348 size = skb_frag_size(frag);
349 dma = skb_frag_dma_map(dev, frag, 0, size, DMA_TO_DEVICE);
350 if (dma_mapping_error(dev, dma)) {
351 netdev_err(ndev, "TX frag(%d) DMA map failed\n", i);
352 ring->stats.sw_err_cnt++;
353 goto out_map_frag_fail;
354 }
13ac695e
S
355 priv->ops.fill_desc(ring, skb_frag_page(frag), size, dma,
356 seg_num - 1 == i ? 1 : 0, buf_num,
357 DESC_TYPE_PAGE, ndev->mtu);
b5996f11 358 }
359
360 /*complete translate all packets*/
361 dev_queue = netdev_get_tx_queue(ndev, skb->queue_mapping);
362 netdev_tx_sent_queue(dev_queue, skb->len);
363
27463ad9
YL
364 netif_trans_update(ndev);
365 ndev->stats.tx_bytes += skb->len;
366 ndev->stats.tx_packets++;
367
b5996f11 368 wmb(); /* commit all data before submit */
369 assert(skb->queue_mapping < priv->ae_handle->q_num);
370 hnae_queue_xmit(priv->ae_handle->qs[skb->queue_mapping], buf_num);
371 ring->stats.tx_pkts++;
372 ring->stats.tx_bytes += skb->len;
373
374 return NETDEV_TX_OK;
375
376out_map_frag_fail:
377
13ac695e 378 while (ring->next_to_use != next_to_use) {
b5996f11 379 unfill_desc(ring);
13ac695e
S
380 if (ring->next_to_use != next_to_use)
381 dma_unmap_page(dev,
382 ring->desc_cb[ring->next_to_use].dma,
383 ring->desc_cb[ring->next_to_use].length,
384 DMA_TO_DEVICE);
385 else
386 dma_unmap_single(dev,
387 ring->desc_cb[next_to_use].dma,
388 ring->desc_cb[next_to_use].length,
389 DMA_TO_DEVICE);
b5996f11 390 }
391
b5996f11 392out_err_tx_ok:
393
394 dev_kfree_skb_any(skb);
395 return NETDEV_TX_OK;
396
397out_net_tx_busy:
398
399 netif_stop_subqueue(ndev, skb->queue_mapping);
400
401 /* Herbert's original patch had:
402 * smp_mb__after_netif_stop_queue();
403 * but since that doesn't exist yet, just open code it.
404 */
405 smp_mb();
406 return NETDEV_TX_BUSY;
407}
408
409/**
410 * hns_nic_get_headlen - determine size of header for RSC/LRO/GRO/FCOE
411 * @data: pointer to the start of the headers
412 * @max: total length of section to find headers in
413 *
414 * This function is meant to determine the length of headers that will
415 * be recognized by hardware for LRO, GRO, and RSC offloads. The main
416 * motivation of doing this is to only perform one pull for IPv4 TCP
417 * packets so that we can do basic things like calculating the gso_size
418 * based on the average data per packet.
419 **/
420static unsigned int hns_nic_get_headlen(unsigned char *data, u32 flag,
421 unsigned int max_size)
422{
423 unsigned char *network;
424 u8 hlen;
425
426 /* this should never happen, but better safe than sorry */
427 if (max_size < ETH_HLEN)
428 return max_size;
429
430 /* initialize network frame pointer */
431 network = data;
432
433 /* set first protocol and move network header forward */
434 network += ETH_HLEN;
435
436 /* handle any vlan tag if present */
437 if (hnae_get_field(flag, HNS_RXD_VLAN_M, HNS_RXD_VLAN_S)
438 == HNS_RX_FLAG_VLAN_PRESENT) {
439 if ((typeof(max_size))(network - data) > (max_size - VLAN_HLEN))
440 return max_size;
441
442 network += VLAN_HLEN;
443 }
444
445 /* handle L3 protocols */
446 if (hnae_get_field(flag, HNS_RXD_L3ID_M, HNS_RXD_L3ID_S)
447 == HNS_RX_FLAG_L3ID_IPV4) {
448 if ((typeof(max_size))(network - data) >
449 (max_size - sizeof(struct iphdr)))
450 return max_size;
451
452 /* access ihl as a u8 to avoid unaligned access on ia64 */
453 hlen = (network[0] & 0x0F) << 2;
454
455 /* verify hlen meets minimum size requirements */
456 if (hlen < sizeof(struct iphdr))
457 return network - data;
458
459 /* record next protocol if header is present */
460 } else if (hnae_get_field(flag, HNS_RXD_L3ID_M, HNS_RXD_L3ID_S)
461 == HNS_RX_FLAG_L3ID_IPV6) {
462 if ((typeof(max_size))(network - data) >
463 (max_size - sizeof(struct ipv6hdr)))
464 return max_size;
465
466 /* record next protocol */
467 hlen = sizeof(struct ipv6hdr);
468 } else {
469 return network - data;
470 }
471
472 /* relocate pointer to start of L4 header */
473 network += hlen;
474
475 /* finally sort out TCP/UDP */
476 if (hnae_get_field(flag, HNS_RXD_L4ID_M, HNS_RXD_L4ID_S)
477 == HNS_RX_FLAG_L4ID_TCP) {
478 if ((typeof(max_size))(network - data) >
479 (max_size - sizeof(struct tcphdr)))
480 return max_size;
481
482 /* access doff as a u8 to avoid unaligned access on ia64 */
483 hlen = (network[12] & 0xF0) >> 2;
484
485 /* verify hlen meets minimum size requirements */
486 if (hlen < sizeof(struct tcphdr))
487 return network - data;
488
489 network += hlen;
490 } else if (hnae_get_field(flag, HNS_RXD_L4ID_M, HNS_RXD_L4ID_S)
491 == HNS_RX_FLAG_L4ID_UDP) {
492 if ((typeof(max_size))(network - data) >
493 (max_size - sizeof(struct udphdr)))
494 return max_size;
495
496 network += sizeof(struct udphdr);
497 }
498
499 /* If everything has gone correctly network should be the
500 * data section of the packet and will be the end of the header.
501 * If not then it probably represents the end of the last recognized
502 * header.
503 */
504 if ((typeof(max_size))(network - data) < max_size)
505 return network - data;
506 else
507 return max_size;
508}
509
9cbe9fd5 510static void hns_nic_reuse_page(struct sk_buff *skb, int i,
511 struct hnae_ring *ring, int pull_len,
512 struct hnae_desc_cb *desc_cb)
b5996f11 513{
9cbe9fd5 514 struct hnae_desc *desc;
ac4a5b52
HT
515 u32 truesize;
516 int size;
9cbe9fd5 517 int last_offset;
be78a690
AB
518 bool twobufs;
519
b4957ab0
S
520 twobufs = ((PAGE_SIZE < 8192) &&
521 hnae_buf_size(ring) == HNS_BUFFER_SIZE_2048);
9cbe9fd5 522
523 desc = &ring->desc[ring->next_to_clean];
524 size = le16_to_cpu(desc->rx.size);
525
be78a690 526 if (twobufs) {
9cbe9fd5 527 truesize = hnae_buf_size(ring);
528 } else {
529 truesize = ALIGN(size, L1_CACHE_BYTES);
530 last_offset = hnae_page_size(ring) - hnae_buf_size(ring);
531 }
532
9cbe9fd5 533 skb_add_rx_frag(skb, i, desc_cb->priv, desc_cb->page_offset + pull_len,
b1ccd4c0 534 size - pull_len, truesize);
9cbe9fd5 535
b5996f11 536 /* avoid re-using remote pages,flag default unreuse */
be78a690
AB
537 if (unlikely(page_to_nid(desc_cb->priv) != numa_node_id()))
538 return;
539
540 if (twobufs) {
541 /* if we are only owner of page we can reuse it */
542 if (likely(page_count(desc_cb->priv) == 1)) {
543 /* flip page offset to other buffer */
544 desc_cb->page_offset ^= truesize;
b5996f11 545
b5996f11 546 desc_cb->reuse_flag = 1;
547 /* bump ref count on page before it is given*/
548 get_page(desc_cb->priv);
549 }
be78a690
AB
550 return;
551 }
552
553 /* move offset up to the next cache line */
554 desc_cb->page_offset += truesize;
555
556 if (desc_cb->page_offset <= last_offset) {
557 desc_cb->reuse_flag = 1;
558 /* bump ref count on page before it is given*/
559 get_page(desc_cb->priv);
b5996f11 560 }
561}
562
13ac695e
S
563static void get_v2rx_desc_bnum(u32 bnum_flag, int *out_bnum)
564{
565 *out_bnum = hnae_get_field(bnum_flag,
566 HNS_RXD_BUFNUM_M, HNS_RXD_BUFNUM_S) + 1;
567}
568
569static void get_rx_desc_bnum(u32 bnum_flag, int *out_bnum)
570{
571 *out_bnum = hnae_get_field(bnum_flag,
572 HNS_RXD_BUFNUM_M, HNS_RXD_BUFNUM_S);
573}
574
862b3d20
S
575static void hns_nic_rx_checksum(struct hns_nic_ring_data *ring_data,
576 struct sk_buff *skb, u32 flag)
577{
578 struct net_device *netdev = ring_data->napi.dev;
579 u32 l3id;
580 u32 l4id;
581
582 /* check if RX checksum offload is enabled */
583 if (unlikely(!(netdev->features & NETIF_F_RXCSUM)))
584 return;
585
586 /* In hardware, we only support checksum for the following protocols:
587 * 1) IPv4,
588 * 2) TCP(over IPv4 or IPv6),
589 * 3) UDP(over IPv4 or IPv6),
590 * 4) SCTP(over IPv4 or IPv6)
591 * but we support many L3(IPv4, IPv6, MPLS, PPPoE etc) and L4(TCP,
592 * UDP, GRE, SCTP, IGMP, ICMP etc.) protocols.
593 *
594 * Hardware limitation:
595 * Our present hardware RX Descriptor lacks L3/L4 checksum "Status &
596 * Error" bit (which usually can be used to indicate whether checksum
597 * was calculated by the hardware and if there was any error encountered
598 * during checksum calculation).
599 *
600 * Software workaround:
601 * We do get info within the RX descriptor about the kind of L3/L4
602 * protocol coming in the packet and the error status. These errors
603 * might not just be checksum errors but could be related to version,
604 * length of IPv4, UDP, TCP etc.
605 * Because there is no-way of knowing if it is a L3/L4 error due to bad
606 * checksum or any other L3/L4 error, we will not (cannot) convey
607 * checksum status for such cases to upper stack and will not maintain
608 * the RX L3/L4 checksum counters as well.
609 */
610
611 l3id = hnae_get_field(flag, HNS_RXD_L3ID_M, HNS_RXD_L3ID_S);
612 l4id = hnae_get_field(flag, HNS_RXD_L4ID_M, HNS_RXD_L4ID_S);
613
614 /* check L3 protocol for which checksum is supported */
615 if ((l3id != HNS_RX_FLAG_L3ID_IPV4) && (l3id != HNS_RX_FLAG_L3ID_IPV6))
616 return;
617
618 /* check for any(not just checksum)flagged L3 protocol errors */
619 if (unlikely(hnae_get_bit(flag, HNS_RXD_L3E_B)))
620 return;
621
622 /* we do not support checksum of fragmented packets */
623 if (unlikely(hnae_get_bit(flag, HNS_RXD_FRAG_B)))
624 return;
625
626 /* check L4 protocol for which checksum is supported */
627 if ((l4id != HNS_RX_FLAG_L4ID_TCP) &&
628 (l4id != HNS_RX_FLAG_L4ID_UDP) &&
629 (l4id != HNS_RX_FLAG_L4ID_SCTP))
630 return;
631
632 /* check for any(not just checksum)flagged L4 protocol errors */
633 if (unlikely(hnae_get_bit(flag, HNS_RXD_L4E_B)))
634 return;
635
636 /* now, this has to be a packet with valid RX checksum */
637 skb->ip_summed = CHECKSUM_UNNECESSARY;
638}
639
b5996f11 640static int hns_nic_poll_rx_skb(struct hns_nic_ring_data *ring_data,
641 struct sk_buff **out_skb, int *out_bnum)
642{
643 struct hnae_ring *ring = ring_data->ring;
644 struct net_device *ndev = ring_data->napi.dev;
13ac695e 645 struct hns_nic_priv *priv = netdev_priv(ndev);
b5996f11 646 struct sk_buff *skb;
647 struct hnae_desc *desc;
648 struct hnae_desc_cb *desc_cb;
649 unsigned char *va;
9cbe9fd5 650 int bnum, length, i;
b5996f11 651 int pull_len;
652 u32 bnum_flag;
653
b5996f11 654 desc = &ring->desc[ring->next_to_clean];
655 desc_cb = &ring->desc_cb[ring->next_to_clean];
13ac695e
S
656
657 prefetch(desc);
658
b5996f11 659 va = (unsigned char *)desc_cb->buf + desc_cb->page_offset;
660
13ac695e
S
661 /* prefetch first cache line of first page */
662 prefetch(va);
663#if L1_CACHE_BYTES < 128
664 prefetch(va + L1_CACHE_BYTES);
665#endif
666
667 skb = *out_skb = napi_alloc_skb(&ring_data->napi,
668 HNS_RX_HEAD_SIZE);
b5996f11 669 if (unlikely(!skb)) {
670 netdev_err(ndev, "alloc rx skb fail\n");
671 ring->stats.sw_err_cnt++;
672 return -ENOMEM;
673 }
674
9cbe9fd5 675 prefetchw(skb->data);
13ac695e
S
676 length = le16_to_cpu(desc->rx.pkt_len);
677 bnum_flag = le32_to_cpu(desc->rx.ipoff_bnum_pid_flag);
678 priv->ops.get_rxd_bnum(bnum_flag, &bnum);
679 *out_bnum = bnum;
680
b5996f11 681 if (length <= HNS_RX_HEAD_SIZE) {
682 memcpy(__skb_put(skb, length), va, ALIGN(length, sizeof(long)));
683
684 /* we can reuse buffer as-is, just make sure it is local */
685 if (likely(page_to_nid(desc_cb->priv) == numa_node_id()))
686 desc_cb->reuse_flag = 1;
687 else /* this page cannot be reused so discard it */
688 put_page(desc_cb->priv);
689
690 ring_ptr_move_fw(ring, next_to_clean);
691
692 if (unlikely(bnum != 1)) { /* check err*/
693 *out_bnum = 1;
694 goto out_bnum_err;
695 }
696 } else {
697 ring->stats.seg_pkt_cnt++;
698
699 pull_len = hns_nic_get_headlen(va, bnum_flag, HNS_RX_HEAD_SIZE);
700 memcpy(__skb_put(skb, pull_len), va,
701 ALIGN(pull_len, sizeof(long)));
702
9cbe9fd5 703 hns_nic_reuse_page(skb, 0, ring, pull_len, desc_cb);
b5996f11 704 ring_ptr_move_fw(ring, next_to_clean);
705
706 if (unlikely(bnum >= (int)MAX_SKB_FRAGS)) { /* check err*/
707 *out_bnum = 1;
708 goto out_bnum_err;
709 }
710 for (i = 1; i < bnum; i++) {
711 desc = &ring->desc[ring->next_to_clean];
712 desc_cb = &ring->desc_cb[ring->next_to_clean];
b5996f11 713
9cbe9fd5 714 hns_nic_reuse_page(skb, i, ring, 0, desc_cb);
b5996f11 715 ring_ptr_move_fw(ring, next_to_clean);
716 }
717 }
718
719 /* check except process, free skb and jump the desc */
720 if (unlikely((!bnum) || (bnum > ring->max_desc_num_per_pkt))) {
721out_bnum_err:
722 *out_bnum = *out_bnum ? *out_bnum : 1; /* ntc moved,cannot 0*/
723 netdev_err(ndev, "invalid bnum(%d,%d,%d,%d),%016llx,%016llx\n",
724 bnum, ring->max_desc_num_per_pkt,
725 length, (int)MAX_SKB_FRAGS,
726 ((u64 *)desc)[0], ((u64 *)desc)[1]);
727 ring->stats.err_bd_num++;
728 dev_kfree_skb_any(skb);
729 return -EDOM;
730 }
731
732 bnum_flag = le32_to_cpu(desc->rx.ipoff_bnum_pid_flag);
733
734 if (unlikely(!hnae_get_bit(bnum_flag, HNS_RXD_VLD_B))) {
735 netdev_err(ndev, "no valid bd,%016llx,%016llx\n",
736 ((u64 *)desc)[0], ((u64 *)desc)[1]);
737 ring->stats.non_vld_descs++;
738 dev_kfree_skb_any(skb);
739 return -EINVAL;
740 }
741
742 if (unlikely((!desc->rx.pkt_len) ||
743 hnae_get_bit(bnum_flag, HNS_RXD_DROP_B))) {
b5996f11 744 ring->stats.err_pkt_len++;
745 dev_kfree_skb_any(skb);
746 return -EFAULT;
747 }
748
749 if (unlikely(hnae_get_bit(bnum_flag, HNS_RXD_L2E_B))) {
b5996f11 750 ring->stats.l2_err++;
751 dev_kfree_skb_any(skb);
752 return -EFAULT;
753 }
754
755 ring->stats.rx_pkts++;
756 ring->stats.rx_bytes += skb->len;
757
862b3d20
S
758 /* indicate to upper stack if our hardware has already calculated
759 * the RX checksum
760 */
761 hns_nic_rx_checksum(ring_data, skb, bnum_flag);
b5996f11 762
763 return 0;
764}
765
766static void
767hns_nic_alloc_rx_buffers(struct hns_nic_ring_data *ring_data, int cleand_count)
768{
769 int i, ret;
770 struct hnae_desc_cb res_cbs;
771 struct hnae_desc_cb *desc_cb;
772 struct hnae_ring *ring = ring_data->ring;
773 struct net_device *ndev = ring_data->napi.dev;
774
775 for (i = 0; i < cleand_count; i++) {
776 desc_cb = &ring->desc_cb[ring->next_to_use];
777 if (desc_cb->reuse_flag) {
778 ring->stats.reuse_pg_cnt++;
779 hnae_reuse_buffer(ring, ring->next_to_use);
780 } else {
781 ret = hnae_reserve_buffer_map(ring, &res_cbs);
782 if (ret) {
783 ring->stats.sw_err_cnt++;
784 netdev_err(ndev, "hnae reserve buffer map failed.\n");
785 break;
786 }
787 hnae_replace_buffer(ring, ring->next_to_use, &res_cbs);
788 }
789
790 ring_ptr_move_fw(ring, next_to_use);
791 }
792
793 wmb(); /* make all data has been write before submit */
794 writel_relaxed(i, ring->io_base + RCB_REG_HEAD);
795}
796
797/* return error number for error or number of desc left to take
798 */
799static void hns_nic_rx_up_pro(struct hns_nic_ring_data *ring_data,
800 struct sk_buff *skb)
801{
802 struct net_device *ndev = ring_data->napi.dev;
803
804 skb->protocol = eth_type_trans(skb, ndev);
805 (void)napi_gro_receive(&ring_data->napi, skb);
b5996f11 806}
807
0e97cd4e 808static int hns_desc_unused(struct hnae_ring *ring)
809{
810 int ntc = ring->next_to_clean;
811 int ntu = ring->next_to_use;
812
813 return ((ntc >= ntu) ? 0 : ring->desc_num) + ntc - ntu;
814}
815
b8c17f70
LYS
816#define HNS_LOWEST_LATENCY_RATE 27 /* 27 MB/s */
817#define HNS_LOW_LATENCY_RATE 80 /* 80 MB/s */
818
819#define HNS_COAL_BDNUM 3
820
821static u32 hns_coal_rx_bdnum(struct hnae_ring *ring)
822{
823 bool coal_enable = ring->q->handle->coal_adapt_en;
824
825 if (coal_enable &&
826 ring->coal_last_rx_bytes > HNS_LOWEST_LATENCY_RATE)
827 return HNS_COAL_BDNUM;
828 else
829 return 0;
830}
831
832static void hns_update_rx_rate(struct hnae_ring *ring)
833{
834 bool coal_enable = ring->q->handle->coal_adapt_en;
835 u32 time_passed_ms;
836 u64 total_bytes;
837
838 if (!coal_enable ||
839 time_before(jiffies, ring->coal_last_jiffies + (HZ >> 4)))
840 return;
841
842 /* ring->stats.rx_bytes overflowed */
843 if (ring->coal_last_rx_bytes > ring->stats.rx_bytes) {
844 ring->coal_last_rx_bytes = ring->stats.rx_bytes;
845 ring->coal_last_jiffies = jiffies;
846 return;
847 }
848
849 total_bytes = ring->stats.rx_bytes - ring->coal_last_rx_bytes;
850 time_passed_ms = jiffies_to_msecs(jiffies - ring->coal_last_jiffies);
967b2e2a
LYS
851 do_div(total_bytes, time_passed_ms);
852 ring->coal_rx_rate = total_bytes >> 10;
b8c17f70
LYS
853
854 ring->coal_last_rx_bytes = ring->stats.rx_bytes;
855 ring->coal_last_jiffies = jiffies;
856}
857
858/**
859 * smooth_alg - smoothing algrithm for adjusting coalesce parameter
860 **/
861static u32 smooth_alg(u32 new_param, u32 old_param)
862{
863 u32 gap = (new_param > old_param) ? new_param - old_param
864 : old_param - new_param;
865
866 if (gap > 8)
867 gap >>= 3;
868
869 if (new_param > old_param)
870 return old_param + gap;
871 else
872 return old_param - gap;
873}
874
875/**
876 * hns_nic_adp_coalesce - self adapte coalesce according to rx rate
877 * @ring_data: pointer to hns_nic_ring_data
878 **/
879static void hns_nic_adpt_coalesce(struct hns_nic_ring_data *ring_data)
880{
881 struct hnae_ring *ring = ring_data->ring;
882 struct hnae_handle *handle = ring->q->handle;
883 u32 new_coal_param, old_coal_param = ring->coal_param;
884
885 if (ring->coal_rx_rate < HNS_LOWEST_LATENCY_RATE)
886 new_coal_param = HNAE_LOWEST_LATENCY_COAL_PARAM;
887 else if (ring->coal_rx_rate < HNS_LOW_LATENCY_RATE)
888 new_coal_param = HNAE_LOW_LATENCY_COAL_PARAM;
889 else
890 new_coal_param = HNAE_BULK_LATENCY_COAL_PARAM;
891
892 if (new_coal_param == old_coal_param &&
893 new_coal_param == handle->coal_param)
894 return;
895
896 new_coal_param = smooth_alg(new_coal_param, old_coal_param);
897 ring->coal_param = new_coal_param;
898
899 /**
900 * Because all ring in one port has one coalesce param, when one ring
901 * calculate its own coalesce param, it cannot write to hardware at
902 * once. There are three conditions as follows:
903 * 1. current ring's coalesce param is larger than the hardware.
904 * 2. or ring which adapt last time can change again.
905 * 3. timeout.
906 */
907 if (new_coal_param == handle->coal_param) {
908 handle->coal_last_jiffies = jiffies;
909 handle->coal_ring_idx = ring_data->queue_index;
910 } else if (new_coal_param > handle->coal_param ||
911 handle->coal_ring_idx == ring_data->queue_index ||
912 time_after(jiffies, handle->coal_last_jiffies + (HZ >> 4))) {
913 handle->dev->ops->set_coalesce_usecs(handle,
914 new_coal_param);
915 handle->dev->ops->set_coalesce_frames(handle,
916 1, new_coal_param);
917 handle->coal_param = new_coal_param;
918 handle->coal_ring_idx = ring_data->queue_index;
919 handle->coal_last_jiffies = jiffies;
920 }
921}
922
b5996f11 923static int hns_nic_rx_poll_one(struct hns_nic_ring_data *ring_data,
924 int budget, void *v)
925{
926 struct hnae_ring *ring = ring_data->ring;
927 struct sk_buff *skb;
34447271 928 int num, bnum;
b5996f11 929#define RCB_NOF_ALLOC_RX_BUFF_ONCE 16
930 int recv_pkts, recv_bds, clean_count, err;
0e97cd4e 931 int unused_count = hns_desc_unused(ring);
b5996f11 932
933 num = readl_relaxed(ring->io_base + RCB_REG_FBDNUM);
934 rmb(); /* make sure num taken effect before the other data is touched */
935
936 recv_pkts = 0, recv_bds = 0, clean_count = 0;
0e97cd4e 937 num -= unused_count;
34447271 938
b5996f11 939 while (recv_pkts < budget && recv_bds < num) {
6ba312eb 940 /* reuse or realloc buffers */
0e97cd4e 941 if (clean_count + unused_count >= RCB_NOF_ALLOC_RX_BUFF_ONCE) {
942 hns_nic_alloc_rx_buffers(ring_data,
943 clean_count + unused_count);
b5996f11 944 clean_count = 0;
0e97cd4e 945 unused_count = hns_desc_unused(ring);
b5996f11 946 }
947
6ba312eb 948 /* poll one pkt */
b5996f11 949 err = hns_nic_poll_rx_skb(ring_data, &skb, &bnum);
950 if (unlikely(!skb)) /* this fault cannot be repaired */
3a31b64e 951 goto out;
b5996f11 952
953 recv_bds += bnum;
954 clean_count += bnum;
955 if (unlikely(err)) { /* do jump the err */
956 recv_pkts++;
957 continue;
958 }
959
960 /* do update ip stack process*/
961 ((void (*)(struct hns_nic_ring_data *, struct sk_buff *))v)(
962 ring_data, skb);
963 recv_pkts++;
964 }
965
3a31b64e 966out:
13ac695e 967 /* make all data has been write before submit */
0e97cd4e 968 if (clean_count + unused_count > 0)
969 hns_nic_alloc_rx_buffers(ring_data,
970 clean_count + unused_count);
13ac695e 971
b5996f11 972 return recv_pkts;
973}
974
36eedfde 975static bool hns_nic_rx_fini_pro(struct hns_nic_ring_data *ring_data)
b5996f11 976{
977 struct hnae_ring *ring = ring_data->ring;
978 int num = 0;
b8c17f70 979 bool rx_stopped;
b5996f11 980
b8c17f70 981 hns_update_rx_rate(ring);
cee5add4 982
b5996f11 983 /* for hardware bug fixed */
b8c17f70 984 ring_data->ring->q->handle->dev->ops->toggle_ring_irq(ring, 0);
b5996f11 985 num = readl_relaxed(ring->io_base + RCB_REG_FBDNUM);
986
b8c17f70
LYS
987 if (num <= hns_coal_rx_bdnum(ring)) {
988 if (ring->q->handle->coal_adapt_en)
989 hns_nic_adpt_coalesce(ring_data);
990
991 rx_stopped = true;
992 } else {
b5996f11 993 ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
994 ring_data->ring, 1);
995
b8c17f70 996 rx_stopped = false;
b5996f11 997 }
b8c17f70
LYS
998
999 return rx_stopped;
b5996f11 1000}
1001
36eedfde 1002static bool hns_nic_rx_fini_pro_v2(struct hns_nic_ring_data *ring_data)
cee5add4
DH
1003{
1004 struct hnae_ring *ring = ring_data->ring;
36eedfde 1005 int num;
cee5add4 1006
b8c17f70 1007 hns_update_rx_rate(ring);
cee5add4
DH
1008 num = readl_relaxed(ring->io_base + RCB_REG_FBDNUM);
1009
b8c17f70
LYS
1010 if (num <= hns_coal_rx_bdnum(ring)) {
1011 if (ring->q->handle->coal_adapt_en)
1012 hns_nic_adpt_coalesce(ring_data);
1013
36eedfde 1014 return true;
b8c17f70
LYS
1015 }
1016
1017 return false;
cee5add4
DH
1018}
1019
b5996f11 1020static inline void hns_nic_reclaim_one_desc(struct hnae_ring *ring,
1021 int *bytes, int *pkts)
1022{
1023 struct hnae_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_clean];
1024
1025 (*pkts) += (desc_cb->type == DESC_TYPE_SKB);
1026 (*bytes) += desc_cb->length;
1027 /* desc_cb will be cleaned, after hnae_free_buffer_detach*/
1028 hnae_free_buffer_detach(ring, ring->next_to_clean);
1029
1030 ring_ptr_move_fw(ring, next_to_clean);
1031}
1032
1033static int is_valid_clean_head(struct hnae_ring *ring, int h)
1034{
1035 int u = ring->next_to_use;
1036 int c = ring->next_to_clean;
1037
1038 if (unlikely(h > ring->desc_num))
1039 return 0;
1040
1041 assert(u > 0 && u < ring->desc_num);
1042 assert(c > 0 && c < ring->desc_num);
1043 assert(u != c && h != c); /* must be checked before call this func */
1044
1045 return u > c ? (h > c && h <= u) : (h > c || h <= u);
1046}
1047
1048/* netif_tx_lock will turn down the performance, set only when necessary */
1049#ifdef CONFIG_NET_POLL_CONTROLLER
b4957ab0
S
1050#define NETIF_TX_LOCK(ring) spin_lock(&(ring)->lock)
1051#define NETIF_TX_UNLOCK(ring) spin_unlock(&(ring)->lock)
b5996f11 1052#else
f2aaed55 1053#define NETIF_TX_LOCK(ring)
1054#define NETIF_TX_UNLOCK(ring)
b5996f11 1055#endif
f2aaed55 1056
b5996f11 1057/* reclaim all desc in one budget
1058 * return error or number of desc left
1059 */
1060static int hns_nic_tx_poll_one(struct hns_nic_ring_data *ring_data,
1061 int budget, void *v)
1062{
1063 struct hnae_ring *ring = ring_data->ring;
1064 struct net_device *ndev = ring_data->napi.dev;
1065 struct netdev_queue *dev_queue;
1066 struct hns_nic_priv *priv = netdev_priv(ndev);
1067 int head;
1068 int bytes, pkts;
1069
f2aaed55 1070 NETIF_TX_LOCK(ring);
b5996f11 1071
1072 head = readl_relaxed(ring->io_base + RCB_REG_HEAD);
1073 rmb(); /* make sure head is ready before touch any data */
1074
1075 if (is_ring_empty(ring) || head == ring->next_to_clean) {
f2aaed55 1076 NETIF_TX_UNLOCK(ring);
b5996f11 1077 return 0; /* no data to poll */
1078 }
1079
1080 if (!is_valid_clean_head(ring, head)) {
1081 netdev_err(ndev, "wrong head (%d, %d-%d)\n", head,
1082 ring->next_to_use, ring->next_to_clean);
1083 ring->stats.io_err_cnt++;
f2aaed55 1084 NETIF_TX_UNLOCK(ring);
b5996f11 1085 return -EIO;
1086 }
1087
1088 bytes = 0;
1089 pkts = 0;
9cbe9fd5 1090 while (head != ring->next_to_clean) {
b5996f11 1091 hns_nic_reclaim_one_desc(ring, &bytes, &pkts);
9cbe9fd5 1092 /* issue prefetch for next Tx descriptor */
1093 prefetch(&ring->desc_cb[ring->next_to_clean]);
1094 }
b5996f11 1095
f2aaed55 1096 NETIF_TX_UNLOCK(ring);
b5996f11 1097
1098 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
1099 netdev_tx_completed_queue(dev_queue, pkts, bytes);
1100
13ac695e
S
1101 if (unlikely(priv->link && !netif_carrier_ok(ndev)))
1102 netif_carrier_on(ndev);
1103
b5996f11 1104 if (unlikely(pkts && netif_carrier_ok(ndev) &&
1105 (ring_space(ring) >= ring->max_desc_num_per_pkt * 2))) {
1106 /* Make sure that anybody stopping the queue after this
1107 * sees the new next_to_clean.
1108 */
1109 smp_mb();
1110 if (netif_tx_queue_stopped(dev_queue) &&
1111 !test_bit(NIC_STATE_DOWN, &priv->state)) {
1112 netif_tx_wake_queue(dev_queue);
1113 ring->stats.restart_queue++;
1114 }
1115 }
1116 return 0;
1117}
1118
36eedfde 1119static bool hns_nic_tx_fini_pro(struct hns_nic_ring_data *ring_data)
b5996f11 1120{
1121 struct hnae_ring *ring = ring_data->ring;
cee5add4
DH
1122 int head;
1123
1124 ring_data->ring->q->handle->dev->ops->toggle_ring_irq(ring, 0);
1125
1126 head = readl_relaxed(ring->io_base + RCB_REG_HEAD);
b5996f11 1127
1128 if (head != ring->next_to_clean) {
1129 ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
1130 ring_data->ring, 1);
1131
36eedfde 1132 return false;
1133 } else {
1134 return true;
b5996f11 1135 }
1136}
1137
36eedfde 1138static bool hns_nic_tx_fini_pro_v2(struct hns_nic_ring_data *ring_data)
cee5add4
DH
1139{
1140 struct hnae_ring *ring = ring_data->ring;
1141 int head = readl_relaxed(ring->io_base + RCB_REG_HEAD);
1142
1143 if (head == ring->next_to_clean)
36eedfde 1144 return true;
cee5add4 1145 else
36eedfde 1146 return false;
cee5add4
DH
1147}
1148
b5996f11 1149static void hns_nic_tx_clr_all_bufs(struct hns_nic_ring_data *ring_data)
1150{
1151 struct hnae_ring *ring = ring_data->ring;
1152 struct net_device *ndev = ring_data->napi.dev;
1153 struct netdev_queue *dev_queue;
1154 int head;
1155 int bytes, pkts;
1156
f2aaed55 1157 NETIF_TX_LOCK(ring);
b5996f11 1158
1159 head = ring->next_to_use; /* ntu :soft setted ring position*/
1160 bytes = 0;
1161 pkts = 0;
1162 while (head != ring->next_to_clean)
1163 hns_nic_reclaim_one_desc(ring, &bytes, &pkts);
1164
f2aaed55 1165 NETIF_TX_UNLOCK(ring);
b5996f11 1166
1167 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
1168 netdev_tx_reset_queue(dev_queue);
1169}
1170
1171static int hns_nic_common_poll(struct napi_struct *napi, int budget)
1172{
36eedfde 1173 int clean_complete = 0;
b5996f11 1174 struct hns_nic_ring_data *ring_data =
1175 container_of(napi, struct hns_nic_ring_data, napi);
36eedfde 1176 struct hnae_ring *ring = ring_data->ring;
b5996f11 1177
36eedfde 1178try_again:
1179 clean_complete += ring_data->poll_one(
1180 ring_data, budget - clean_complete,
1181 ring_data->ex_process);
1182
1183 if (clean_complete < budget) {
1184 if (ring_data->fini_process(ring_data)) {
1185 napi_complete(napi);
1186 ring->q->handle->dev->ops->toggle_ring_irq(ring, 0);
1187 } else {
1188 goto try_again;
1189 }
b5996f11 1190 }
1191
1192 return clean_complete;
1193}
1194
1195static irqreturn_t hns_irq_handle(int irq, void *dev)
1196{
1197 struct hns_nic_ring_data *ring_data = (struct hns_nic_ring_data *)dev;
1198
1199 ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
1200 ring_data->ring, 1);
1201 napi_schedule(&ring_data->napi);
1202
1203 return IRQ_HANDLED;
1204}
1205
1206/**
1207 *hns_nic_adjust_link - adjust net work mode by the phy stat or new param
1208 *@ndev: net device
1209 */
1210static void hns_nic_adjust_link(struct net_device *ndev)
1211{
1212 struct hns_nic_priv *priv = netdev_priv(ndev);
1213 struct hnae_handle *h = priv->ae_handle;
bb7189dc
QX
1214 int state = 1;
1215
262b38cd 1216 if (ndev->phydev) {
bb7189dc
QX
1217 h->dev->ops->adjust_link(h, ndev->phydev->speed,
1218 ndev->phydev->duplex);
262b38cd 1219 state = ndev->phydev->link;
bb7189dc
QX
1220 }
1221 state = state && h->dev->ops->get_status(h);
b5996f11 1222
bb7189dc
QX
1223 if (state != priv->link) {
1224 if (state) {
1225 netif_carrier_on(ndev);
1226 netif_tx_wake_all_queues(ndev);
1227 netdev_info(ndev, "link up\n");
1228 } else {
1229 netif_carrier_off(ndev);
1230 netdev_info(ndev, "link down\n");
1231 }
1232 priv->link = state;
1233 }
b5996f11 1234}
1235
1236/**
1237 *hns_nic_init_phy - init phy
1238 *@ndev: net device
1239 *@h: ae handle
1240 * Return 0 on success, negative on failure
1241 */
1242int hns_nic_init_phy(struct net_device *ndev, struct hnae_handle *h)
1243{
652d39b0
KY
1244 struct phy_device *phy_dev = h->phy_dev;
1245 int ret;
b5996f11 1246
652d39b0 1247 if (!h->phy_dev)
b5996f11 1248 return 0;
1249
652d39b0
KY
1250 if (h->phy_if != PHY_INTERFACE_MODE_XGMII) {
1251 phy_dev->dev_flags = 0;
b5996f11 1252
652d39b0
KY
1253 ret = phy_connect_direct(ndev, phy_dev, hns_nic_adjust_link,
1254 h->phy_if);
1255 } else {
1256 ret = phy_attach_direct(ndev, phy_dev, 0, h->phy_if);
1257 }
1258 if (unlikely(ret))
1259 return -ENODEV;
b5996f11 1260
1261 phy_dev->supported &= h->if_support;
1262 phy_dev->advertising = phy_dev->supported;
1263
1264 if (h->phy_if == PHY_INTERFACE_MODE_XGMII)
1265 phy_dev->autoneg = false;
1266
b5996f11 1267 return 0;
1268}
1269
1270static int hns_nic_ring_open(struct net_device *netdev, int idx)
1271{
1272 struct hns_nic_priv *priv = netdev_priv(netdev);
1273 struct hnae_handle *h = priv->ae_handle;
1274
1275 napi_enable(&priv->ring_data[idx].napi);
1276
1277 enable_irq(priv->ring_data[idx].ring->irq);
1278 h->dev->ops->toggle_ring_irq(priv->ring_data[idx].ring, 0);
1279
1280 return 0;
1281}
1282
1283static int hns_nic_net_set_mac_address(struct net_device *ndev, void *p)
1284{
1285 struct hns_nic_priv *priv = netdev_priv(ndev);
1286 struct hnae_handle *h = priv->ae_handle;
1287 struct sockaddr *mac_addr = p;
1288 int ret;
1289
1290 if (!mac_addr || !is_valid_ether_addr((const u8 *)mac_addr->sa_data))
1291 return -EADDRNOTAVAIL;
1292
1293 ret = h->dev->ops->set_mac_addr(h, mac_addr->sa_data);
1294 if (ret) {
1295 netdev_err(ndev, "set_mac_address fail, ret=%d!\n", ret);
1296 return ret;
1297 }
1298
1299 memcpy(ndev->dev_addr, mac_addr->sa_data, ndev->addr_len);
1300
1301 return 0;
1302}
1303
336a443b 1304static void hns_nic_update_stats(struct net_device *netdev)
b5996f11 1305{
1306 struct hns_nic_priv *priv = netdev_priv(netdev);
1307 struct hnae_handle *h = priv->ae_handle;
1308
1309 h->dev->ops->update_stats(h, &netdev->stats);
1310}
1311
1312/* set mac addr if it is configed. or leave it to the AE driver */
1313static void hns_init_mac_addr(struct net_device *ndev)
1314{
1315 struct hns_nic_priv *priv = netdev_priv(ndev);
b5996f11 1316
6162928c 1317 if (!device_get_mac_address(priv->dev, ndev->dev_addr, ETH_ALEN)) {
b5996f11 1318 eth_hw_addr_random(ndev);
1319 dev_warn(priv->dev, "No valid mac, use random mac %pM",
1320 ndev->dev_addr);
1321 }
1322}
1323
1324static void hns_nic_ring_close(struct net_device *netdev, int idx)
1325{
1326 struct hns_nic_priv *priv = netdev_priv(netdev);
1327 struct hnae_handle *h = priv->ae_handle;
1328
1329 h->dev->ops->toggle_ring_irq(priv->ring_data[idx].ring, 1);
1330 disable_irq(priv->ring_data[idx].ring->irq);
1331
1332 napi_disable(&priv->ring_data[idx].napi);
1333}
1334
ba2d0791 1335static int hns_nic_init_affinity_mask(int q_num, int ring_idx,
1336 struct hnae_ring *ring, cpumask_t *mask)
b5996f11 1337{
b5996f11 1338 int cpu;
ff3edc9b 1339
ba2d0791 1340 /* Diffrent irq banlance between 16core and 32core.
1341 * The cpu mask set by ring index according to the ring flag
1342 * which indicate the ring is tx or rx.
1343 */
1344 if (q_num == num_possible_cpus()) {
1345 if (is_tx_ring(ring))
1346 cpu = ring_idx;
1347 else
1348 cpu = ring_idx - q_num;
13ac695e 1349 } else {
ba2d0791 1350 if (is_tx_ring(ring))
1351 cpu = ring_idx * 2;
1352 else
1353 cpu = (ring_idx - q_num) * 2 + 1;
13ac695e 1354 }
ff3edc9b 1355
ba2d0791 1356 cpumask_clear(mask);
1357 cpumask_set_cpu(cpu, mask);
1358
1359 return cpu;
13ac695e
S
1360}
1361
1362static int hns_nic_init_irq(struct hns_nic_priv *priv)
1363{
1364 struct hnae_handle *h = priv->ae_handle;
1365 struct hns_nic_ring_data *rd;
1366 int i;
1367 int ret;
ba2d0791 1368 int cpu;
13ac695e 1369
b5996f11 1370 for (i = 0; i < h->q_num * 2; i++) {
1371 rd = &priv->ring_data[i];
1372
1373 if (rd->ring->irq_init_flag == RCB_IRQ_INITED)
1374 break;
1375
1376 snprintf(rd->ring->ring_name, RCB_RING_NAME_LEN,
1377 "%s-%s%d", priv->netdev->name,
ba2d0791 1378 (is_tx_ring(rd->ring) ? "tx" : "rx"), rd->queue_index);
b5996f11 1379
1380 rd->ring->ring_name[RCB_RING_NAME_LEN - 1] = '\0';
1381
1382 ret = request_irq(rd->ring->irq,
1383 hns_irq_handle, 0, rd->ring->ring_name, rd);
1384 if (ret) {
1385 netdev_err(priv->netdev, "request irq(%d) fail\n",
1386 rd->ring->irq);
1387 return ret;
1388 }
1389 disable_irq(rd->ring->irq);
ba2d0791 1390
1391 cpu = hns_nic_init_affinity_mask(h->q_num, i,
1392 rd->ring, &rd->mask);
1393
1394 if (cpu_online(cpu))
1395 irq_set_affinity_hint(rd->ring->irq,
1396 &rd->mask);
1397
b5996f11 1398 rd->ring->irq_init_flag = RCB_IRQ_INITED;
b5996f11 1399 }
1400
1401 return 0;
1402}
1403
1404static int hns_nic_net_up(struct net_device *ndev)
1405{
1406 struct hns_nic_priv *priv = netdev_priv(ndev);
1407 struct hnae_handle *h = priv->ae_handle;
454784d8 1408 int i, j;
b5996f11 1409 int ret;
1410
1411 ret = hns_nic_init_irq(priv);
1412 if (ret != 0) {
1413 netdev_err(ndev, "hns init irq failed! ret=%d\n", ret);
1414 return ret;
1415 }
1416
1417 for (i = 0; i < h->q_num * 2; i++) {
1418 ret = hns_nic_ring_open(ndev, i);
1419 if (ret)
1420 goto out_has_some_queues;
1421 }
1422
b5996f11 1423 ret = h->dev->ops->set_mac_addr(h, ndev->dev_addr);
1424 if (ret)
1425 goto out_set_mac_addr_err;
1426
1427 ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
1428 if (ret)
1429 goto out_start_err;
1430
262b38cd
PR
1431 if (ndev->phydev)
1432 phy_start(ndev->phydev);
b5996f11 1433
1434 clear_bit(NIC_STATE_DOWN, &priv->state);
1435 (void)mod_timer(&priv->service_timer, jiffies + SERVICE_TIMER_HZ);
1436
1437 return 0;
1438
1439out_start_err:
1440 netif_stop_queue(ndev);
1441out_set_mac_addr_err:
b5996f11 1442out_has_some_queues:
1443 for (j = i - 1; j >= 0; j--)
1444 hns_nic_ring_close(ndev, j);
1445
1446 set_bit(NIC_STATE_DOWN, &priv->state);
1447
1448 return ret;
1449}
1450
1451static void hns_nic_net_down(struct net_device *ndev)
1452{
1453 int i;
1454 struct hnae_ae_ops *ops;
1455 struct hns_nic_priv *priv = netdev_priv(ndev);
1456
1457 if (test_and_set_bit(NIC_STATE_DOWN, &priv->state))
1458 return;
1459
1460 (void)del_timer_sync(&priv->service_timer);
1461 netif_tx_stop_all_queues(ndev);
1462 netif_carrier_off(ndev);
1463 netif_tx_disable(ndev);
1464 priv->link = 0;
1465
262b38cd
PR
1466 if (ndev->phydev)
1467 phy_stop(ndev->phydev);
b5996f11 1468
1469 ops = priv->ae_handle->dev->ops;
1470
1471 if (ops->stop)
1472 ops->stop(priv->ae_handle);
1473
1474 netif_tx_stop_all_queues(ndev);
1475
1476 for (i = priv->ae_handle->q_num - 1; i >= 0; i--) {
1477 hns_nic_ring_close(ndev, i);
1478 hns_nic_ring_close(ndev, i + priv->ae_handle->q_num);
1479
1480 /* clean tx buffers*/
1481 hns_nic_tx_clr_all_bufs(priv->ring_data + i);
1482 }
1483}
1484
1485void hns_nic_net_reset(struct net_device *ndev)
1486{
1487 struct hns_nic_priv *priv = netdev_priv(ndev);
1488 struct hnae_handle *handle = priv->ae_handle;
1489
1490 while (test_and_set_bit(NIC_STATE_RESETTING, &priv->state))
1491 usleep_range(1000, 2000);
1492
1493 (void)hnae_reinit_handle(handle);
1494
1495 clear_bit(NIC_STATE_RESETTING, &priv->state);
1496}
1497
1498void hns_nic_net_reinit(struct net_device *netdev)
1499{
1500 struct hns_nic_priv *priv = netdev_priv(netdev);
76b825ab 1501 enum hnae_port_type type = priv->ae_handle->port_type;
b5996f11 1502
860e9538 1503 netif_trans_update(priv->netdev);
b5996f11 1504 while (test_and_set_bit(NIC_STATE_REINITING, &priv->state))
1505 usleep_range(1000, 2000);
1506
1507 hns_nic_net_down(netdev);
76b825ab
LYS
1508
1509 /* Only do hns_nic_net_reset in debug mode
1510 * because of hardware limitation.
1511 */
1512 if (type == HNAE_PORT_DEBUG)
1513 hns_nic_net_reset(netdev);
1514
b5996f11 1515 (void)hns_nic_net_up(netdev);
1516 clear_bit(NIC_STATE_REINITING, &priv->state);
1517}
1518
1519static int hns_nic_net_open(struct net_device *ndev)
1520{
1521 struct hns_nic_priv *priv = netdev_priv(ndev);
1522 struct hnae_handle *h = priv->ae_handle;
1523 int ret;
1524
1525 if (test_bit(NIC_STATE_TESTING, &priv->state))
1526 return -EBUSY;
1527
1528 priv->link = 0;
1529 netif_carrier_off(ndev);
1530
1531 ret = netif_set_real_num_tx_queues(ndev, h->q_num);
1532 if (ret < 0) {
1533 netdev_err(ndev, "netif_set_real_num_tx_queues fail, ret=%d!\n",
1534 ret);
1535 return ret;
1536 }
1537
1538 ret = netif_set_real_num_rx_queues(ndev, h->q_num);
1539 if (ret < 0) {
1540 netdev_err(ndev,
1541 "netif_set_real_num_rx_queues fail, ret=%d!\n", ret);
1542 return ret;
1543 }
1544
1545 ret = hns_nic_net_up(ndev);
1546 if (ret) {
1547 netdev_err(ndev,
1548 "hns net up fail, ret=%d!\n", ret);
1549 return ret;
1550 }
1551
1552 return 0;
1553}
1554
1555static int hns_nic_net_stop(struct net_device *ndev)
1556{
1557 hns_nic_net_down(ndev);
1558
1559 return 0;
1560}
1561
1562static void hns_tx_timeout_reset(struct hns_nic_priv *priv);
1563static void hns_nic_net_timeout(struct net_device *ndev)
1564{
1565 struct hns_nic_priv *priv = netdev_priv(ndev);
1566
1567 hns_tx_timeout_reset(priv);
1568}
1569
1570static int hns_nic_do_ioctl(struct net_device *netdev, struct ifreq *ifr,
1571 int cmd)
1572{
262b38cd 1573 struct phy_device *phy_dev = netdev->phydev;
b5996f11 1574
1575 if (!netif_running(netdev))
1576 return -EINVAL;
1577
1578 if (!phy_dev)
1579 return -ENOTSUPP;
1580
1581 return phy_mii_ioctl(phy_dev, ifr, cmd);
1582}
1583
1584/* use only for netconsole to poll with the device without interrupt */
1585#ifdef CONFIG_NET_POLL_CONTROLLER
336a443b 1586static void hns_nic_poll_controller(struct net_device *ndev)
b5996f11 1587{
1588 struct hns_nic_priv *priv = netdev_priv(ndev);
1589 unsigned long flags;
1590 int i;
1591
1592 local_irq_save(flags);
1593 for (i = 0; i < priv->ae_handle->q_num * 2; i++)
1594 napi_schedule(&priv->ring_data[i].napi);
1595 local_irq_restore(flags);
1596}
1597#endif
1598
1599static netdev_tx_t hns_nic_net_xmit(struct sk_buff *skb,
1600 struct net_device *ndev)
1601{
1602 struct hns_nic_priv *priv = netdev_priv(ndev);
b5996f11 1603
1604 assert(skb->queue_mapping < ndev->ae_handle->q_num);
27463ad9
YL
1605
1606 return hns_nic_net_xmit_hw(ndev, skb,
1607 &tx_ring_data(priv, skb->queue_mapping));
b5996f11 1608}
1609
b29bd412 1610static void hns_nic_drop_rx_fetch(struct hns_nic_ring_data *ring_data,
1611 struct sk_buff *skb)
1612{
1613 dev_kfree_skb_any(skb);
1614}
1615
1616#define HNS_LB_TX_RING 0
1617static struct sk_buff *hns_assemble_skb(struct net_device *ndev)
1618{
1619 struct sk_buff *skb;
1620 struct ethhdr *ethhdr;
1621 int frame_len;
1622
1623 /* allocate test skb */
1624 skb = alloc_skb(64, GFP_KERNEL);
1625 if (!skb)
1626 return NULL;
1627
1628 skb_put(skb, 64);
1629 skb->dev = ndev;
1630 memset(skb->data, 0xFF, skb->len);
1631
1632 /* must be tcp/ip package */
1633 ethhdr = (struct ethhdr *)skb->data;
1634 ethhdr->h_proto = htons(ETH_P_IP);
1635
1636 frame_len = skb->len & (~1ul);
1637 memset(&skb->data[frame_len / 2], 0xAA,
1638 frame_len / 2 - 1);
1639
1640 skb->queue_mapping = HNS_LB_TX_RING;
1641
1642 return skb;
1643}
1644
1645static int hns_enable_serdes_lb(struct net_device *ndev)
1646{
1647 struct hns_nic_priv *priv = netdev_priv(ndev);
1648 struct hnae_handle *h = priv->ae_handle;
1649 struct hnae_ae_ops *ops = h->dev->ops;
1650 int speed, duplex;
1651 int ret;
1652
1653 ret = ops->set_loopback(h, MAC_INTERNALLOOP_SERDES, 1);
1654 if (ret)
1655 return ret;
1656
1657 ret = ops->start ? ops->start(h) : 0;
1658 if (ret)
1659 return ret;
1660
1661 /* link adjust duplex*/
1662 if (h->phy_if != PHY_INTERFACE_MODE_XGMII)
1663 speed = 1000;
1664 else
1665 speed = 10000;
1666 duplex = 1;
1667
1668 ops->adjust_link(h, speed, duplex);
1669
1670 /* wait h/w ready */
1671 mdelay(300);
1672
1673 return 0;
1674}
1675
1676static void hns_disable_serdes_lb(struct net_device *ndev)
1677{
1678 struct hns_nic_priv *priv = netdev_priv(ndev);
1679 struct hnae_handle *h = priv->ae_handle;
1680 struct hnae_ae_ops *ops = h->dev->ops;
1681
1682 ops->stop(h);
1683 ops->set_loopback(h, MAC_INTERNALLOOP_SERDES, 0);
1684}
1685
1686/**
1687 *hns_nic_clear_all_rx_fetch - clear the chip fetched descriptions. The
1688 *function as follows:
1689 * 1. if one rx ring has found the page_offset is not equal 0 between head
1690 * and tail, it means that the chip fetched the wrong descs for the ring
1691 * which buffer size is 4096.
1692 * 2. we set the chip serdes loopback and set rss indirection to the ring.
1693 * 3. construct 64-bytes ip broadcast packages, wait the associated rx ring
1694 * recieving all packages and it will fetch new descriptions.
1695 * 4. recover to the original state.
1696 *
1697 *@ndev: net device
1698 */
1699static int hns_nic_clear_all_rx_fetch(struct net_device *ndev)
1700{
1701 struct hns_nic_priv *priv = netdev_priv(ndev);
1702 struct hnae_handle *h = priv->ae_handle;
1703 struct hnae_ae_ops *ops = h->dev->ops;
1704 struct hns_nic_ring_data *rd;
1705 struct hnae_ring *ring;
1706 struct sk_buff *skb;
1707 u32 *org_indir;
1708 u32 *cur_indir;
1709 int indir_size;
1710 int head, tail;
1711 int fetch_num;
1712 int i, j;
1713 bool found;
1714 int retry_times;
1715 int ret = 0;
1716
1717 /* alloc indir memory */
1718 indir_size = ops->get_rss_indir_size(h) * sizeof(*org_indir);
1719 org_indir = kzalloc(indir_size, GFP_KERNEL);
1720 if (!org_indir)
1721 return -ENOMEM;
1722
1723 /* store the orginal indirection */
1724 ops->get_rss(h, org_indir, NULL, NULL);
1725
1726 cur_indir = kzalloc(indir_size, GFP_KERNEL);
1727 if (!cur_indir) {
1728 ret = -ENOMEM;
1729 goto cur_indir_alloc_err;
1730 }
1731
1732 /* set loopback */
1733 if (hns_enable_serdes_lb(ndev)) {
1734 ret = -EINVAL;
1735 goto enable_serdes_lb_err;
1736 }
1737
1738 /* foreach every rx ring to clear fetch desc */
1739 for (i = 0; i < h->q_num; i++) {
1740 ring = &h->qs[i]->rx_ring;
1741 head = readl_relaxed(ring->io_base + RCB_REG_HEAD);
1742 tail = readl_relaxed(ring->io_base + RCB_REG_TAIL);
1743 found = false;
1744 fetch_num = ring_dist(ring, head, tail);
1745
1746 while (head != tail) {
1747 if (ring->desc_cb[head].page_offset != 0) {
1748 found = true;
1749 break;
1750 }
1751
1752 head++;
1753 if (head == ring->desc_num)
1754 head = 0;
1755 }
1756
1757 if (found) {
1758 for (j = 0; j < indir_size / sizeof(*org_indir); j++)
1759 cur_indir[j] = i;
1760 ops->set_rss(h, cur_indir, NULL, 0);
1761
1762 for (j = 0; j < fetch_num; j++) {
1763 /* alloc one skb and init */
1764 skb = hns_assemble_skb(ndev);
1765 if (!skb)
1766 goto out;
1767 rd = &tx_ring_data(priv, skb->queue_mapping);
1768 hns_nic_net_xmit_hw(ndev, skb, rd);
1769
1770 retry_times = 0;
1771 while (retry_times++ < 10) {
1772 mdelay(10);
1773 /* clean rx */
1774 rd = &rx_ring_data(priv, i);
1775 if (rd->poll_one(rd, fetch_num,
1776 hns_nic_drop_rx_fetch))
1777 break;
1778 }
1779
1780 retry_times = 0;
1781 while (retry_times++ < 10) {
1782 mdelay(10);
1783 /* clean tx ring 0 send package */
1784 rd = &tx_ring_data(priv,
1785 HNS_LB_TX_RING);
1786 if (rd->poll_one(rd, fetch_num, NULL))
1787 break;
1788 }
1789 }
1790 }
1791 }
1792
1793out:
1794 /* restore everything */
1795 ops->set_rss(h, org_indir, NULL, 0);
1796 hns_disable_serdes_lb(ndev);
1797enable_serdes_lb_err:
1798 kfree(cur_indir);
1799cur_indir_alloc_err:
1800 kfree(org_indir);
1801
1802 return ret;
1803}
1804
b5996f11 1805static int hns_nic_change_mtu(struct net_device *ndev, int new_mtu)
1806{
1807 struct hns_nic_priv *priv = netdev_priv(ndev);
1808 struct hnae_handle *h = priv->ae_handle;
b29bd412 1809 bool if_running = netif_running(ndev);
b5996f11 1810 int ret;
1811
b29bd412 1812 /* MTU < 68 is an error and causes problems on some kernels */
1813 if (new_mtu < 68)
1814 return -EINVAL;
1815
1816 /* MTU no change */
1817 if (new_mtu == ndev->mtu)
1818 return 0;
1819
b5996f11 1820 if (!h->dev->ops->set_mtu)
1821 return -ENOTSUPP;
1822
b29bd412 1823 if (if_running) {
b5996f11 1824 (void)hns_nic_net_stop(ndev);
1825 msleep(100);
b29bd412 1826 }
b5996f11 1827
b29bd412 1828 if (priv->enet_ver != AE_VERSION_1 &&
1829 ndev->mtu <= BD_SIZE_2048_MAX_MTU &&
1830 new_mtu > BD_SIZE_2048_MAX_MTU) {
1831 /* update desc */
1832 hnae_reinit_all_ring_desc(h);
b5996f11 1833
b29bd412 1834 /* clear the package which the chip has fetched */
1835 ret = hns_nic_clear_all_rx_fetch(ndev);
1836
1837 /* the page offset must be consist with desc */
1838 hnae_reinit_all_ring_page_off(h);
1839
1840 if (ret) {
1841 netdev_err(ndev, "clear the fetched desc fail\n");
1842 goto out;
1843 }
1844 }
1845
1846 ret = h->dev->ops->set_mtu(h, new_mtu);
1847 if (ret) {
1848 netdev_err(ndev, "set mtu fail, return value %d\n",
1849 ret);
1850 goto out;
b5996f11 1851 }
1852
b29bd412 1853 /* finally, set new mtu to netdevice */
1854 ndev->mtu = new_mtu;
1855
1856out:
1857 if (if_running) {
1858 if (hns_nic_net_open(ndev)) {
1859 netdev_err(ndev, "hns net open fail\n");
1860 ret = -EINVAL;
1861 }
1862 }
b5996f11 1863
1864 return ret;
1865}
1866
38f616da
S
1867static int hns_nic_set_features(struct net_device *netdev,
1868 netdev_features_t features)
1869{
1870 struct hns_nic_priv *priv = netdev_priv(netdev);
38f616da
S
1871
1872 switch (priv->enet_ver) {
1873 case AE_VERSION_1:
1874 if (features & (NETIF_F_TSO | NETIF_F_TSO6))
1875 netdev_info(netdev, "enet v1 do not support tso!\n");
1876 break;
1877 default:
1878 if (features & (NETIF_F_TSO | NETIF_F_TSO6)) {
1879 priv->ops.fill_desc = fill_tso_desc;
1880 priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tso;
1881 /* The chip only support 7*4096 */
1882 netif_set_gso_max_size(netdev, 7 * 4096);
38f616da
S
1883 } else {
1884 priv->ops.fill_desc = fill_v2_desc;
1885 priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tx;
38f616da
S
1886 }
1887 break;
1888 }
1889 netdev->features = features;
1890 return 0;
1891}
1892
1893static netdev_features_t hns_nic_fix_features(
1894 struct net_device *netdev, netdev_features_t features)
1895{
1896 struct hns_nic_priv *priv = netdev_priv(netdev);
1897
1898 switch (priv->enet_ver) {
1899 case AE_VERSION_1:
1900 features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
1901 NETIF_F_HW_VLAN_CTAG_FILTER);
1902 break;
1903 default:
1904 break;
1905 }
1906 return features;
1907}
1908
66355f52
KY
1909static int hns_nic_uc_sync(struct net_device *netdev, const unsigned char *addr)
1910{
1911 struct hns_nic_priv *priv = netdev_priv(netdev);
1912 struct hnae_handle *h = priv->ae_handle;
1913
1914 if (h->dev->ops->add_uc_addr)
1915 return h->dev->ops->add_uc_addr(h, addr);
1916
1917 return 0;
1918}
1919
1920static int hns_nic_uc_unsync(struct net_device *netdev,
1921 const unsigned char *addr)
1922{
1923 struct hns_nic_priv *priv = netdev_priv(netdev);
1924 struct hnae_handle *h = priv->ae_handle;
1925
1926 if (h->dev->ops->rm_uc_addr)
1927 return h->dev->ops->rm_uc_addr(h, addr);
1928
1929 return 0;
1930}
1931
b5996f11 1932/**
1933 * nic_set_multicast_list - set mutl mac address
1934 * @netdev: net device
1935 * @p: mac address
1936 *
1937 * return void
1938 */
336a443b 1939static void hns_set_multicast_list(struct net_device *ndev)
b5996f11 1940{
1941 struct hns_nic_priv *priv = netdev_priv(ndev);
1942 struct hnae_handle *h = priv->ae_handle;
1943 struct netdev_hw_addr *ha = NULL;
1944
1945 if (!h) {
1946 netdev_err(ndev, "hnae handle is null\n");
1947 return;
1948 }
1949
ec2cafe6
KY
1950 if (h->dev->ops->clr_mc_addr)
1951 if (h->dev->ops->clr_mc_addr(h))
1952 netdev_err(ndev, "clear multicast address fail\n");
1953
b5996f11 1954 if (h->dev->ops->set_mc_addr) {
1955 netdev_for_each_mc_addr(ha, ndev)
1956 if (h->dev->ops->set_mc_addr(h, ha->addr))
1957 netdev_err(ndev, "set multicast fail\n");
1958 }
1959}
1960
336a443b 1961static void hns_nic_set_rx_mode(struct net_device *ndev)
4568637f 1962{
1963 struct hns_nic_priv *priv = netdev_priv(ndev);
1964 struct hnae_handle *h = priv->ae_handle;
1965
1966 if (h->dev->ops->set_promisc_mode) {
1967 if (ndev->flags & IFF_PROMISC)
1968 h->dev->ops->set_promisc_mode(h, 1);
1969 else
1970 h->dev->ops->set_promisc_mode(h, 0);
1971 }
1972
1973 hns_set_multicast_list(ndev);
66355f52
KY
1974
1975 if (__dev_uc_sync(ndev, hns_nic_uc_sync, hns_nic_uc_unsync))
1976 netdev_err(ndev, "sync uc address fail\n");
4568637f 1977}
1978
bc1f4470 1979static void hns_nic_get_stats64(struct net_device *ndev,
1980 struct rtnl_link_stats64 *stats)
b5996f11 1981{
1982 int idx = 0;
1983 u64 tx_bytes = 0;
1984 u64 rx_bytes = 0;
1985 u64 tx_pkts = 0;
1986 u64 rx_pkts = 0;
1987 struct hns_nic_priv *priv = netdev_priv(ndev);
1988 struct hnae_handle *h = priv->ae_handle;
1989
1990 for (idx = 0; idx < h->q_num; idx++) {
1991 tx_bytes += h->qs[idx]->tx_ring.stats.tx_bytes;
1992 tx_pkts += h->qs[idx]->tx_ring.stats.tx_pkts;
1993 rx_bytes += h->qs[idx]->rx_ring.stats.rx_bytes;
1994 rx_pkts += h->qs[idx]->rx_ring.stats.rx_pkts;
1995 }
1996
1997 stats->tx_bytes = tx_bytes;
1998 stats->tx_packets = tx_pkts;
1999 stats->rx_bytes = rx_bytes;
2000 stats->rx_packets = rx_pkts;
2001
2002 stats->rx_errors = ndev->stats.rx_errors;
2003 stats->multicast = ndev->stats.multicast;
2004 stats->rx_length_errors = ndev->stats.rx_length_errors;
2005 stats->rx_crc_errors = ndev->stats.rx_crc_errors;
2006 stats->rx_missed_errors = ndev->stats.rx_missed_errors;
2007
2008 stats->tx_errors = ndev->stats.tx_errors;
2009 stats->rx_dropped = ndev->stats.rx_dropped;
2010 stats->tx_dropped = ndev->stats.tx_dropped;
2011 stats->collisions = ndev->stats.collisions;
2012 stats->rx_over_errors = ndev->stats.rx_over_errors;
2013 stats->rx_frame_errors = ndev->stats.rx_frame_errors;
2014 stats->rx_fifo_errors = ndev->stats.rx_fifo_errors;
2015 stats->tx_aborted_errors = ndev->stats.tx_aborted_errors;
2016 stats->tx_carrier_errors = ndev->stats.tx_carrier_errors;
2017 stats->tx_fifo_errors = ndev->stats.tx_fifo_errors;
2018 stats->tx_heartbeat_errors = ndev->stats.tx_heartbeat_errors;
2019 stats->tx_window_errors = ndev->stats.tx_window_errors;
2020 stats->rx_compressed = ndev->stats.rx_compressed;
2021 stats->tx_compressed = ndev->stats.tx_compressed;
b5996f11 2022}
2023
2162a4a1
DH
2024static u16
2025hns_nic_select_queue(struct net_device *ndev, struct sk_buff *skb,
4f49dec9
AD
2026 struct net_device *sb_dev,
2027 select_queue_fallback_t fallback)
2162a4a1
DH
2028{
2029 struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
2030 struct hns_nic_priv *priv = netdev_priv(ndev);
2031
2032 /* fix hardware broadcast/multicast packets queue loopback */
2033 if (!AE_IS_VER1(priv->enet_ver) &&
2034 is_multicast_ether_addr(eth_hdr->h_dest))
2035 return 0;
2036 else
8ec56fc3 2037 return fallback(ndev, skb, NULL);
2162a4a1
DH
2038}
2039
b5996f11 2040static const struct net_device_ops hns_nic_netdev_ops = {
2041 .ndo_open = hns_nic_net_open,
2042 .ndo_stop = hns_nic_net_stop,
2043 .ndo_start_xmit = hns_nic_net_xmit,
2044 .ndo_tx_timeout = hns_nic_net_timeout,
2045 .ndo_set_mac_address = hns_nic_net_set_mac_address,
2046 .ndo_change_mtu = hns_nic_change_mtu,
2047 .ndo_do_ioctl = hns_nic_do_ioctl,
38f616da
S
2048 .ndo_set_features = hns_nic_set_features,
2049 .ndo_fix_features = hns_nic_fix_features,
b5996f11 2050 .ndo_get_stats64 = hns_nic_get_stats64,
2051#ifdef CONFIG_NET_POLL_CONTROLLER
2052 .ndo_poll_controller = hns_nic_poll_controller,
2053#endif
4568637f 2054 .ndo_set_rx_mode = hns_nic_set_rx_mode,
2162a4a1 2055 .ndo_select_queue = hns_nic_select_queue,
b5996f11 2056};
2057
2058static void hns_nic_update_link_status(struct net_device *netdev)
2059{
2060 struct hns_nic_priv *priv = netdev_priv(netdev);
2061
2062 struct hnae_handle *h = priv->ae_handle;
b5996f11 2063
bb7189dc
QX
2064 if (h->phy_dev) {
2065 if (h->phy_if != PHY_INTERFACE_MODE_XGMII)
2066 return;
b5996f11 2067
bb7189dc 2068 (void)genphy_read_status(h->phy_dev);
b5996f11 2069 }
bb7189dc 2070 hns_nic_adjust_link(netdev);
b5996f11 2071}
2072
2073/* for dumping key regs*/
2074static void hns_nic_dump(struct hns_nic_priv *priv)
2075{
2076 struct hnae_handle *h = priv->ae_handle;
2077 struct hnae_ae_ops *ops = h->dev->ops;
2078 u32 *data, reg_num, i;
2079
2080 if (ops->get_regs_len && ops->get_regs) {
2081 reg_num = ops->get_regs_len(priv->ae_handle);
2082 reg_num = (reg_num + 3ul) & ~3ul;
2083 data = kcalloc(reg_num, sizeof(u32), GFP_KERNEL);
2084 if (data) {
2085 ops->get_regs(priv->ae_handle, data);
2086 for (i = 0; i < reg_num; i += 4)
2087 pr_info("0x%08x: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2088 i, data[i], data[i + 1],
2089 data[i + 2], data[i + 3]);
2090 kfree(data);
2091 }
2092 }
2093
2094 for (i = 0; i < h->q_num; i++) {
2095 pr_info("tx_queue%d_next_to_clean:%d\n",
2096 i, h->qs[i]->tx_ring.next_to_clean);
2097 pr_info("tx_queue%d_next_to_use:%d\n",
2098 i, h->qs[i]->tx_ring.next_to_use);
2099 pr_info("rx_queue%d_next_to_clean:%d\n",
2100 i, h->qs[i]->rx_ring.next_to_clean);
2101 pr_info("rx_queue%d_next_to_use:%d\n",
2102 i, h->qs[i]->rx_ring.next_to_use);
2103 }
2104}
2105
f7211729 2106/* for resetting subtask */
b5996f11 2107static void hns_nic_reset_subtask(struct hns_nic_priv *priv)
2108{
2109 enum hnae_port_type type = priv->ae_handle->port_type;
2110
2111 if (!test_bit(NIC_STATE2_RESET_REQUESTED, &priv->state))
2112 return;
2113 clear_bit(NIC_STATE2_RESET_REQUESTED, &priv->state);
2114
2115 /* If we're already down, removing or resetting, just bail */
2116 if (test_bit(NIC_STATE_DOWN, &priv->state) ||
2117 test_bit(NIC_STATE_REMOVING, &priv->state) ||
2118 test_bit(NIC_STATE_RESETTING, &priv->state))
2119 return;
2120
2121 hns_nic_dump(priv);
13ac695e
S
2122 netdev_info(priv->netdev, "try to reset %s port!\n",
2123 (type == HNAE_PORT_DEBUG ? "debug" : "service"));
b5996f11 2124
2125 rtnl_lock();
90a505b9 2126 /* put off any impending NetWatchDogTimeout */
860e9538 2127 netif_trans_update(priv->netdev);
76b825ab 2128 hns_nic_net_reinit(priv->netdev);
90a505b9 2129
b5996f11 2130 rtnl_unlock();
2131}
2132
2133/* for doing service complete*/
2134static void hns_nic_service_event_complete(struct hns_nic_priv *priv)
2135{
13ac695e 2136 WARN_ON(!test_bit(NIC_STATE_SERVICE_SCHED, &priv->state));
b4957ab0 2137 /* make sure to commit the things */
b5996f11 2138 smp_mb__before_atomic();
2139 clear_bit(NIC_STATE_SERVICE_SCHED, &priv->state);
2140}
2141
2142static void hns_nic_service_task(struct work_struct *work)
2143{
2144 struct hns_nic_priv *priv
2145 = container_of(work, struct hns_nic_priv, service_task);
2146 struct hnae_handle *h = priv->ae_handle;
2147
2148 hns_nic_update_link_status(priv->netdev);
2149 h->dev->ops->update_led_status(h);
2150 hns_nic_update_stats(priv->netdev);
2151
2152 hns_nic_reset_subtask(priv);
2153 hns_nic_service_event_complete(priv);
2154}
2155
2156static void hns_nic_task_schedule(struct hns_nic_priv *priv)
2157{
2158 if (!test_bit(NIC_STATE_DOWN, &priv->state) &&
2159 !test_bit(NIC_STATE_REMOVING, &priv->state) &&
2160 !test_and_set_bit(NIC_STATE_SERVICE_SCHED, &priv->state))
2161 (void)schedule_work(&priv->service_task);
2162}
2163
d039ef68 2164static void hns_nic_service_timer(struct timer_list *t)
b5996f11 2165{
d039ef68 2166 struct hns_nic_priv *priv = from_timer(priv, t, service_timer);
b5996f11 2167
2168 (void)mod_timer(&priv->service_timer, jiffies + SERVICE_TIMER_HZ);
2169
2170 hns_nic_task_schedule(priv);
2171}
2172
2173/**
2174 * hns_tx_timeout_reset - initiate reset due to Tx timeout
2175 * @priv: driver private struct
2176 **/
2177static void hns_tx_timeout_reset(struct hns_nic_priv *priv)
2178{
2179 /* Do the reset outside of interrupt context */
2180 if (!test_bit(NIC_STATE_DOWN, &priv->state)) {
2181 set_bit(NIC_STATE2_RESET_REQUESTED, &priv->state);
2182 netdev_warn(priv->netdev,
2183 "initiating reset due to tx timeout(%llu,0x%lx)\n",
2184 priv->tx_timeout_count, priv->state);
2185 priv->tx_timeout_count++;
2186 hns_nic_task_schedule(priv);
2187 }
2188}
2189
2190static int hns_nic_init_ring_data(struct hns_nic_priv *priv)
2191{
2192 struct hnae_handle *h = priv->ae_handle;
2193 struct hns_nic_ring_data *rd;
4b34aa41 2194 bool is_ver1 = AE_IS_VER1(priv->enet_ver);
b5996f11 2195 int i;
2196
2197 if (h->q_num > NIC_MAX_Q_PER_VF) {
2198 netdev_err(priv->netdev, "too much queue (%d)\n", h->q_num);
2199 return -EINVAL;
2200 }
2201
6396bb22
KC
2202 priv->ring_data = kzalloc(array3_size(h->q_num,
2203 sizeof(*priv->ring_data), 2),
b5996f11 2204 GFP_KERNEL);
2205 if (!priv->ring_data)
2206 return -ENOMEM;
2207
2208 for (i = 0; i < h->q_num; i++) {
2209 rd = &priv->ring_data[i];
2210 rd->queue_index = i;
2211 rd->ring = &h->qs[i]->tx_ring;
2212 rd->poll_one = hns_nic_tx_poll_one;
cee5add4
DH
2213 rd->fini_process = is_ver1 ? hns_nic_tx_fini_pro :
2214 hns_nic_tx_fini_pro_v2;
b5996f11 2215
2216 netif_napi_add(priv->netdev, &rd->napi,
2217 hns_nic_common_poll, NIC_TX_CLEAN_MAX_NUM);
2218 rd->ring->irq_init_flag = RCB_IRQ_NOT_INITED;
2219 }
2220 for (i = h->q_num; i < h->q_num * 2; i++) {
2221 rd = &priv->ring_data[i];
2222 rd->queue_index = i - h->q_num;
2223 rd->ring = &h->qs[i - h->q_num]->rx_ring;
2224 rd->poll_one = hns_nic_rx_poll_one;
2225 rd->ex_process = hns_nic_rx_up_pro;
cee5add4
DH
2226 rd->fini_process = is_ver1 ? hns_nic_rx_fini_pro :
2227 hns_nic_rx_fini_pro_v2;
b5996f11 2228
2229 netif_napi_add(priv->netdev, &rd->napi,
2230 hns_nic_common_poll, NIC_RX_CLEAN_MAX_NUM);
2231 rd->ring->irq_init_flag = RCB_IRQ_NOT_INITED;
2232 }
2233
2234 return 0;
2235}
2236
2237static void hns_nic_uninit_ring_data(struct hns_nic_priv *priv)
2238{
2239 struct hnae_handle *h = priv->ae_handle;
2240 int i;
2241
2242 for (i = 0; i < h->q_num * 2; i++) {
2243 netif_napi_del(&priv->ring_data[i].napi);
2244 if (priv->ring_data[i].ring->irq_init_flag == RCB_IRQ_INITED) {
13ac695e
S
2245 (void)irq_set_affinity_hint(
2246 priv->ring_data[i].ring->irq,
2247 NULL);
b5996f11 2248 free_irq(priv->ring_data[i].ring->irq,
2249 &priv->ring_data[i]);
2250 }
2251
2252 priv->ring_data[i].ring->irq_init_flag = RCB_IRQ_NOT_INITED;
2253 }
2254 kfree(priv->ring_data);
2255}
2256
13ac695e
S
2257static void hns_nic_set_priv_ops(struct net_device *netdev)
2258{
2259 struct hns_nic_priv *priv = netdev_priv(netdev);
64353af6 2260 struct hnae_handle *h = priv->ae_handle;
13ac695e
S
2261
2262 if (AE_IS_VER1(priv->enet_ver)) {
2263 priv->ops.fill_desc = fill_desc;
2264 priv->ops.get_rxd_bnum = get_rx_desc_bnum;
2265 priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tx;
2266 } else {
2267 priv->ops.get_rxd_bnum = get_v2rx_desc_bnum;
64353af6
S
2268 if ((netdev->features & NETIF_F_TSO) ||
2269 (netdev->features & NETIF_F_TSO6)) {
2270 priv->ops.fill_desc = fill_tso_desc;
2271 priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tso;
2272 /* This chip only support 7*4096 */
2273 netif_set_gso_max_size(netdev, 7 * 4096);
64353af6
S
2274 } else {
2275 priv->ops.fill_desc = fill_v2_desc;
2276 priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tx;
2277 }
6fe27464
DH
2278 /* enable tso when init
2279 * control tso on/off through TSE bit in bd
2280 */
2281 h->dev->ops->set_tso_stats(h, 1);
13ac695e
S
2282 }
2283}
2284
b5996f11 2285static int hns_nic_try_get_ae(struct net_device *ndev)
2286{
2287 struct hns_nic_priv *priv = netdev_priv(ndev);
2288 struct hnae_handle *h;
2289 int ret;
2290
2291 h = hnae_get_handle(&priv->netdev->dev,
7b2acae6 2292 priv->fwnode, priv->port_id, NULL);
b5996f11 2293 if (IS_ERR_OR_NULL(h)) {
daa8cfd9 2294 ret = -ENODEV;
b5996f11 2295 dev_dbg(priv->dev, "has not handle, register notifier!\n");
2296 goto out;
2297 }
2298 priv->ae_handle = h;
2299
2300 ret = hns_nic_init_phy(ndev, h);
2301 if (ret) {
2302 dev_err(priv->dev, "probe phy device fail!\n");
2303 goto out_init_phy;
2304 }
2305
2306 ret = hns_nic_init_ring_data(priv);
2307 if (ret) {
2308 ret = -ENOMEM;
2309 goto out_init_ring_data;
2310 }
2311
13ac695e
S
2312 hns_nic_set_priv_ops(ndev);
2313
b5996f11 2314 ret = register_netdev(ndev);
2315 if (ret) {
2316 dev_err(priv->dev, "probe register netdev fail!\n");
2317 goto out_reg_ndev_fail;
2318 }
2319 return 0;
2320
2321out_reg_ndev_fail:
2322 hns_nic_uninit_ring_data(priv);
2323 priv->ring_data = NULL;
2324out_init_phy:
2325out_init_ring_data:
2326 hnae_put_handle(priv->ae_handle);
2327 priv->ae_handle = NULL;
2328out:
2329 return ret;
2330}
2331
2332static int hns_nic_notifier_action(struct notifier_block *nb,
2333 unsigned long action, void *data)
2334{
2335 struct hns_nic_priv *priv =
2336 container_of(nb, struct hns_nic_priv, notifier_block);
2337
2338 assert(action == HNAE_AE_REGISTER);
2339
2340 if (!hns_nic_try_get_ae(priv->netdev)) {
2341 hnae_unregister_notifier(&priv->notifier_block);
2342 priv->notifier_block.notifier_call = NULL;
2343 }
2344 return 0;
2345}
2346
2347static int hns_nic_dev_probe(struct platform_device *pdev)
2348{
2349 struct device *dev = &pdev->dev;
2350 struct net_device *ndev;
2351 struct hns_nic_priv *priv;
406adee9 2352 u32 port_id;
b5996f11 2353 int ret;
2354
2355 ndev = alloc_etherdev_mq(sizeof(struct hns_nic_priv), NIC_MAX_Q_PER_VF);
2356 if (!ndev)
2357 return -ENOMEM;
2358
2359 platform_set_drvdata(pdev, ndev);
2360
2361 priv = netdev_priv(ndev);
2362 priv->dev = dev;
2363 priv->netdev = ndev;
2364
63434888
KY
2365 if (dev_of_node(dev)) {
2366 struct device_node *ae_node;
b5996f11 2367
63434888
KY
2368 if (of_device_is_compatible(dev->of_node,
2369 "hisilicon,hns-nic-v1"))
2370 priv->enet_ver = AE_VERSION_1;
2371 else
2372 priv->enet_ver = AE_VERSION_2;
2373
2374 ae_node = of_parse_phandle(dev->of_node, "ae-handle", 0);
d2083d0e
PB
2375 if (!ae_node) {
2376 ret = -ENODEV;
63434888
KY
2377 dev_err(dev, "not find ae-handle\n");
2378 goto out_read_prop_fail;
2379 }
2380 priv->fwnode = &ae_node->fwnode;
2381 } else if (is_acpi_node(dev->fwnode)) {
977d5ad3 2382 struct fwnode_reference_args args;
63434888
KY
2383
2384 if (acpi_dev_found(hns_enet_acpi_match[0].id))
2385 priv->enet_ver = AE_VERSION_1;
2386 else if (acpi_dev_found(hns_enet_acpi_match[1].id))
2387 priv->enet_ver = AE_VERSION_2;
2388 else
2389 return -ENXIO;
2390
2391 /* try to find port-idx-in-ae first */
2392 ret = acpi_node_get_property_reference(dev->fwnode,
2393 "ae-handle", 0, &args);
2394 if (ret) {
2395 dev_err(dev, "not find ae-handle\n");
2396 goto out_read_prop_fail;
2397 }
977d5ad3
SA
2398 if (!is_acpi_device_node(args.fwnode)) {
2399 ret = -EINVAL;
2400 goto out_read_prop_fail;
2401 }
2402 priv->fwnode = args.fwnode;
63434888
KY
2403 } else {
2404 dev_err(dev, "cannot read cfg data from OF or acpi\n");
2405 return -ENXIO;
48189d6a 2406 }
7b2acae6 2407
6162928c 2408 ret = device_property_read_u32(dev, "port-idx-in-ae", &port_id);
406adee9
YZZ
2409 if (ret) {
2410 /* only for old code compatible */
6162928c 2411 ret = device_property_read_u32(dev, "port-id", &port_id);
406adee9
YZZ
2412 if (ret)
2413 goto out_read_prop_fail;
2414 /* for old dts, we need to caculate the port offset */
2415 port_id = port_id < HNS_SRV_OFFSET ? port_id + HNS_DEBUG_OFFSET
2416 : port_id - HNS_SRV_OFFSET;
2417 }
2418 priv->port_id = port_id;
b5996f11 2419
2420 hns_init_mac_addr(ndev);
2421
2422 ndev->watchdog_timeo = HNS_NIC_TX_TIMEOUT;
2423 ndev->priv_flags |= IFF_UNICAST_FLT;
2424 ndev->netdev_ops = &hns_nic_netdev_ops;
2425 hns_ethtool_set_ops(ndev);
13ac695e 2426
b5996f11 2427 ndev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2428 NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
2429 NETIF_F_GRO;
2430 ndev->vlan_features |=
2431 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
2432 ndev->vlan_features |= NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
2433
44770e11
JW
2434 /* MTU range: 68 - 9578 (v1) or 9706 (v2) */
2435 ndev->min_mtu = MAC_MIN_MTU;
13ac695e
S
2436 switch (priv->enet_ver) {
2437 case AE_VERSION_2:
64353af6 2438 ndev->features |= NETIF_F_TSO | NETIF_F_TSO6;
13ac695e
S
2439 ndev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2440 NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
64353af6 2441 NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6;
44770e11
JW
2442 ndev->max_mtu = MAC_MAX_MTU_V2 -
2443 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
13ac695e
S
2444 break;
2445 default:
44770e11
JW
2446 ndev->max_mtu = MAC_MAX_MTU -
2447 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
13ac695e
S
2448 break;
2449 }
2450
b5996f11 2451 SET_NETDEV_DEV(ndev, dev);
2452
2453 if (!dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
2454 dev_dbg(dev, "set mask to 64bit\n");
2455 else
39c94417 2456 dev_err(dev, "set mask to 64bit fail!\n");
b5996f11 2457
2458 /* carrier off reporting is important to ethtool even BEFORE open */
2459 netif_carrier_off(ndev);
2460
d039ef68 2461 timer_setup(&priv->service_timer, hns_nic_service_timer, 0);
b5996f11 2462 INIT_WORK(&priv->service_task, hns_nic_service_task);
2463
2464 set_bit(NIC_STATE_SERVICE_INITED, &priv->state);
2465 clear_bit(NIC_STATE_SERVICE_SCHED, &priv->state);
2466 set_bit(NIC_STATE_DOWN, &priv->state);
2467
2468 if (hns_nic_try_get_ae(priv->netdev)) {
2469 priv->notifier_block.notifier_call = hns_nic_notifier_action;
2470 ret = hnae_register_notifier(&priv->notifier_block);
2471 if (ret) {
2472 dev_err(dev, "register notifier fail!\n");
2473 goto out_notify_fail;
2474 }
2475 dev_dbg(dev, "has not handle, register notifier!\n");
2476 }
2477
2478 return 0;
2479
2480out_notify_fail:
2481 (void)cancel_work_sync(&priv->service_task);
48189d6a 2482out_read_prop_fail:
b5996f11 2483 free_netdev(ndev);
2484 return ret;
2485}
2486
2487static int hns_nic_dev_remove(struct platform_device *pdev)
2488{
2489 struct net_device *ndev = platform_get_drvdata(pdev);
2490 struct hns_nic_priv *priv = netdev_priv(ndev);
2491
2492 if (ndev->reg_state != NETREG_UNINITIALIZED)
2493 unregister_netdev(ndev);
2494
2495 if (priv->ring_data)
2496 hns_nic_uninit_ring_data(priv);
2497 priv->ring_data = NULL;
2498
262b38cd
PR
2499 if (ndev->phydev)
2500 phy_disconnect(ndev->phydev);
b5996f11 2501
2502 if (!IS_ERR_OR_NULL(priv->ae_handle))
2503 hnae_put_handle(priv->ae_handle);
2504 priv->ae_handle = NULL;
2505 if (priv->notifier_block.notifier_call)
2506 hnae_unregister_notifier(&priv->notifier_block);
2507 priv->notifier_block.notifier_call = NULL;
2508
2509 set_bit(NIC_STATE_REMOVING, &priv->state);
2510 (void)cancel_work_sync(&priv->service_task);
2511
2512 free_netdev(ndev);
2513 return 0;
2514}
2515
2516static const struct of_device_id hns_enet_of_match[] = {
2517 {.compatible = "hisilicon,hns-nic-v1",},
2518 {.compatible = "hisilicon,hns-nic-v2",},
2519 {},
2520};
2521
2522MODULE_DEVICE_TABLE(of, hns_enet_of_match);
2523
2524static struct platform_driver hns_nic_dev_driver = {
2525 .driver = {
2526 .name = "hns-nic",
b5996f11 2527 .of_match_table = hns_enet_of_match,
63434888 2528 .acpi_match_table = ACPI_PTR(hns_enet_acpi_match),
b5996f11 2529 },
2530 .probe = hns_nic_dev_probe,
2531 .remove = hns_nic_dev_remove,
2532};
2533
2534module_platform_driver(hns_nic_dev_driver);
2535
2536MODULE_DESCRIPTION("HISILICON HNS Ethernet driver");
2537MODULE_AUTHOR("Hisilicon, Inc.");
2538MODULE_LICENSE("GPL");
2539MODULE_ALIAS("platform:hns-nic");