]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - drivers/net/ethernet/hisilicon/hns/hns_enet.c
net:hns: Add Hip06 "RSS(Receive Side Scaling)" support to HNS Driver
[mirror_ubuntu-kernels.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"
25
26#define NIC_MAX_Q_PER_VF 16
27#define HNS_NIC_TX_TIMEOUT (5 * HZ)
28
29#define SERVICE_TIMER_HZ (1 * HZ)
30
31#define NIC_TX_CLEAN_MAX_NUM 256
32#define NIC_RX_CLEAN_MAX_NUM 64
33
b5996f11 34#define RCB_IRQ_NOT_INITED 0
35#define RCB_IRQ_INITED 1
36
13ac695e
S
37#define BD_MAX_SEND_SIZE 8191
38#define SKB_TMP_LEN(SKB) \
39 (((SKB)->transport_header - (SKB)->mac_header) + tcp_hdrlen(SKB))
40
41static void fill_v2_desc(struct hnae_ring *ring, void *priv,
42 int size, dma_addr_t dma, int frag_end,
43 int buf_num, enum hns_desc_type type, int mtu)
44{
45 struct hnae_desc *desc = &ring->desc[ring->next_to_use];
46 struct hnae_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_use];
47 struct iphdr *iphdr;
48 struct ipv6hdr *ipv6hdr;
49 struct sk_buff *skb;
50 int skb_tmp_len;
51 __be16 protocol;
52 u8 bn_pid = 0;
53 u8 rrcfv = 0;
54 u8 ip_offset = 0;
55 u8 tvsvsn = 0;
56 u16 mss = 0;
57 u8 l4_len = 0;
58 u16 paylen = 0;
59
60 desc_cb->priv = priv;
61 desc_cb->length = size;
62 desc_cb->dma = dma;
63 desc_cb->type = type;
64
65 desc->addr = cpu_to_le64(dma);
66 desc->tx.send_size = cpu_to_le16((u16)size);
67
68 /*config bd buffer end */
69 hnae_set_bit(rrcfv, HNSV2_TXD_VLD_B, 1);
70 hnae_set_field(bn_pid, HNSV2_TXD_BUFNUM_M, 0, buf_num - 1);
71
72 if (type == DESC_TYPE_SKB) {
73 skb = (struct sk_buff *)priv;
74
75 if (skb->ip_summed == CHECKSUM_PARTIAL) {
76 skb_reset_mac_len(skb);
77 protocol = skb->protocol;
78 ip_offset = ETH_HLEN;
79
80 if (protocol == htons(ETH_P_8021Q)) {
81 ip_offset += VLAN_HLEN;
82 protocol = vlan_get_protocol(skb);
83 skb->protocol = protocol;
84 }
85
86 if (skb->protocol == htons(ETH_P_IP)) {
87 iphdr = ip_hdr(skb);
88 hnae_set_bit(rrcfv, HNSV2_TXD_L3CS_B, 1);
89 hnae_set_bit(rrcfv, HNSV2_TXD_L4CS_B, 1);
90
91 /* check for tcp/udp header */
92 if (iphdr->protocol == IPPROTO_TCP) {
93 hnae_set_bit(tvsvsn,
94 HNSV2_TXD_TSE_B, 1);
95 skb_tmp_len = SKB_TMP_LEN(skb);
96 l4_len = tcp_hdrlen(skb);
97 mss = mtu - skb_tmp_len - ETH_FCS_LEN;
98 paylen = skb->len - skb_tmp_len;
99 }
100 } else if (skb->protocol == htons(ETH_P_IPV6)) {
101 hnae_set_bit(tvsvsn, HNSV2_TXD_IPV6_B, 1);
102 ipv6hdr = ipv6_hdr(skb);
103 hnae_set_bit(rrcfv, HNSV2_TXD_L4CS_B, 1);
104
105 /* check for tcp/udp header */
106 if (ipv6hdr->nexthdr == IPPROTO_TCP) {
107 hnae_set_bit(tvsvsn,
108 HNSV2_TXD_TSE_B, 1);
109 skb_tmp_len = SKB_TMP_LEN(skb);
110 l4_len = tcp_hdrlen(skb);
111 mss = mtu - skb_tmp_len - ETH_FCS_LEN;
112 paylen = skb->len - skb_tmp_len;
113 }
114 }
115 desc->tx.ip_offset = ip_offset;
116 desc->tx.tse_vlan_snap_v6_sctp_nth = tvsvsn;
117 desc->tx.mss = cpu_to_le16(mss);
118 desc->tx.l4_len = l4_len;
119 desc->tx.paylen = cpu_to_le16(paylen);
120 }
121 }
122
123 hnae_set_bit(rrcfv, HNSV2_TXD_FE_B, frag_end);
124
125 desc->tx.bn_pid = bn_pid;
126 desc->tx.ra_ri_cs_fe_vld = rrcfv;
127
128 ring_ptr_move_fw(ring, next_to_use);
129}
130
b5996f11 131static void fill_desc(struct hnae_ring *ring, void *priv,
132 int size, dma_addr_t dma, int frag_end,
13ac695e 133 int buf_num, enum hns_desc_type type, int mtu)
b5996f11 134{
135 struct hnae_desc *desc = &ring->desc[ring->next_to_use];
136 struct hnae_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_use];
137 struct sk_buff *skb;
138 __be16 protocol;
139 u32 ip_offset;
140 u32 asid_bufnum_pid = 0;
141 u32 flag_ipoffset = 0;
142
143 desc_cb->priv = priv;
144 desc_cb->length = size;
145 desc_cb->dma = dma;
146 desc_cb->type = type;
147
148 desc->addr = cpu_to_le64(dma);
149 desc->tx.send_size = cpu_to_le16((u16)size);
150
151 /*config bd buffer end */
152 flag_ipoffset |= 1 << HNS_TXD_VLD_B;
153
154 asid_bufnum_pid |= buf_num << HNS_TXD_BUFNUM_S;
155
156 if (type == DESC_TYPE_SKB) {
157 skb = (struct sk_buff *)priv;
158
159 if (skb->ip_summed == CHECKSUM_PARTIAL) {
160 protocol = skb->protocol;
161 ip_offset = ETH_HLEN;
162
163 /*if it is a SW VLAN check the next protocol*/
164 if (protocol == htons(ETH_P_8021Q)) {
165 ip_offset += VLAN_HLEN;
166 protocol = vlan_get_protocol(skb);
167 skb->protocol = protocol;
168 }
169
170 if (skb->protocol == htons(ETH_P_IP)) {
171 flag_ipoffset |= 1 << HNS_TXD_L3CS_B;
172 /* check for tcp/udp header */
173 flag_ipoffset |= 1 << HNS_TXD_L4CS_B;
174
175 } else if (skb->protocol == htons(ETH_P_IPV6)) {
176 /* ipv6 has not l3 cs, check for L4 header */
177 flag_ipoffset |= 1 << HNS_TXD_L4CS_B;
178 }
179
180 flag_ipoffset |= ip_offset << HNS_TXD_IPOFFSET_S;
181 }
182 }
183
184 flag_ipoffset |= frag_end << HNS_TXD_FE_B;
185
186 desc->tx.asid_bufnum_pid = cpu_to_le16(asid_bufnum_pid);
187 desc->tx.flag_ipoffset = cpu_to_le32(flag_ipoffset);
188
189 ring_ptr_move_fw(ring, next_to_use);
190}
191
192static void unfill_desc(struct hnae_ring *ring)
193{
194 ring_ptr_move_bw(ring, next_to_use);
195}
196
13ac695e
S
197static int hns_nic_maybe_stop_tx(
198 struct sk_buff **out_skb, int *bnum, struct hnae_ring *ring)
b5996f11 199{
13ac695e
S
200 struct sk_buff *skb = *out_skb;
201 struct sk_buff *new_skb = NULL;
b5996f11 202 int buf_num;
b5996f11 203
204 /* no. of segments (plus a header) */
205 buf_num = skb_shinfo(skb)->nr_frags + 1;
206
207 if (unlikely(buf_num > ring->max_desc_num_per_pkt)) {
13ac695e
S
208 if (ring_space(ring) < 1)
209 return -EBUSY;
b5996f11 210
211 new_skb = skb_copy(skb, GFP_ATOMIC);
13ac695e
S
212 if (!new_skb)
213 return -ENOMEM;
b5996f11 214
215 dev_kfree_skb_any(skb);
13ac695e 216 *out_skb = new_skb;
b5996f11 217 buf_num = 1;
b5996f11 218 } else if (buf_num > ring_space(ring)) {
13ac695e
S
219 return -EBUSY;
220 }
221
222 *bnum = buf_num;
223 return 0;
224}
225
226int hns_nic_net_xmit_hw(struct net_device *ndev,
227 struct sk_buff *skb,
228 struct hns_nic_ring_data *ring_data)
229{
230 struct hns_nic_priv *priv = netdev_priv(ndev);
231 struct device *dev = priv->dev;
232 struct hnae_ring *ring = ring_data->ring;
233 struct netdev_queue *dev_queue;
234 struct skb_frag_struct *frag;
235 int buf_num;
236 int seg_num;
237 dma_addr_t dma;
238 int size, next_to_use;
239 int i;
240
241 switch (priv->ops.maybe_stop_tx(&skb, &buf_num, ring)) {
242 case -EBUSY:
b5996f11 243 ring->stats.tx_busy++;
244 goto out_net_tx_busy;
13ac695e
S
245 case -ENOMEM:
246 ring->stats.sw_err_cnt++;
247 netdev_err(ndev, "no memory to xmit!\n");
248 goto out_err_tx_ok;
249 default:
250 break;
b5996f11 251 }
13ac695e
S
252
253 /* no. of segments (plus a header) */
254 seg_num = skb_shinfo(skb)->nr_frags + 1;
b5996f11 255 next_to_use = ring->next_to_use;
256
257 /* fill the first part */
258 size = skb_headlen(skb);
259 dma = dma_map_single(dev, skb->data, size, DMA_TO_DEVICE);
260 if (dma_mapping_error(dev, dma)) {
261 netdev_err(ndev, "TX head DMA map failed\n");
262 ring->stats.sw_err_cnt++;
263 goto out_err_tx_ok;
264 }
13ac695e
S
265 priv->ops.fill_desc(ring, skb, size, dma, seg_num == 1 ? 1 : 0,
266 buf_num, DESC_TYPE_SKB, ndev->mtu);
b5996f11 267
268 /* fill the fragments */
13ac695e 269 for (i = 1; i < seg_num; i++) {
b5996f11 270 frag = &skb_shinfo(skb)->frags[i - 1];
271 size = skb_frag_size(frag);
272 dma = skb_frag_dma_map(dev, frag, 0, size, DMA_TO_DEVICE);
273 if (dma_mapping_error(dev, dma)) {
274 netdev_err(ndev, "TX frag(%d) DMA map failed\n", i);
275 ring->stats.sw_err_cnt++;
276 goto out_map_frag_fail;
277 }
13ac695e
S
278 priv->ops.fill_desc(ring, skb_frag_page(frag), size, dma,
279 seg_num - 1 == i ? 1 : 0, buf_num,
280 DESC_TYPE_PAGE, ndev->mtu);
b5996f11 281 }
282
283 /*complete translate all packets*/
284 dev_queue = netdev_get_tx_queue(ndev, skb->queue_mapping);
285 netdev_tx_sent_queue(dev_queue, skb->len);
286
287 wmb(); /* commit all data before submit */
288 assert(skb->queue_mapping < priv->ae_handle->q_num);
289 hnae_queue_xmit(priv->ae_handle->qs[skb->queue_mapping], buf_num);
290 ring->stats.tx_pkts++;
291 ring->stats.tx_bytes += skb->len;
292
293 return NETDEV_TX_OK;
294
295out_map_frag_fail:
296
13ac695e 297 while (ring->next_to_use != next_to_use) {
b5996f11 298 unfill_desc(ring);
13ac695e
S
299 if (ring->next_to_use != next_to_use)
300 dma_unmap_page(dev,
301 ring->desc_cb[ring->next_to_use].dma,
302 ring->desc_cb[ring->next_to_use].length,
303 DMA_TO_DEVICE);
304 else
305 dma_unmap_single(dev,
306 ring->desc_cb[next_to_use].dma,
307 ring->desc_cb[next_to_use].length,
308 DMA_TO_DEVICE);
b5996f11 309 }
310
b5996f11 311out_err_tx_ok:
312
313 dev_kfree_skb_any(skb);
314 return NETDEV_TX_OK;
315
316out_net_tx_busy:
317
318 netif_stop_subqueue(ndev, skb->queue_mapping);
319
320 /* Herbert's original patch had:
321 * smp_mb__after_netif_stop_queue();
322 * but since that doesn't exist yet, just open code it.
323 */
324 smp_mb();
325 return NETDEV_TX_BUSY;
326}
327
328/**
329 * hns_nic_get_headlen - determine size of header for RSC/LRO/GRO/FCOE
330 * @data: pointer to the start of the headers
331 * @max: total length of section to find headers in
332 *
333 * This function is meant to determine the length of headers that will
334 * be recognized by hardware for LRO, GRO, and RSC offloads. The main
335 * motivation of doing this is to only perform one pull for IPv4 TCP
336 * packets so that we can do basic things like calculating the gso_size
337 * based on the average data per packet.
338 **/
339static unsigned int hns_nic_get_headlen(unsigned char *data, u32 flag,
340 unsigned int max_size)
341{
342 unsigned char *network;
343 u8 hlen;
344
345 /* this should never happen, but better safe than sorry */
346 if (max_size < ETH_HLEN)
347 return max_size;
348
349 /* initialize network frame pointer */
350 network = data;
351
352 /* set first protocol and move network header forward */
353 network += ETH_HLEN;
354
355 /* handle any vlan tag if present */
356 if (hnae_get_field(flag, HNS_RXD_VLAN_M, HNS_RXD_VLAN_S)
357 == HNS_RX_FLAG_VLAN_PRESENT) {
358 if ((typeof(max_size))(network - data) > (max_size - VLAN_HLEN))
359 return max_size;
360
361 network += VLAN_HLEN;
362 }
363
364 /* handle L3 protocols */
365 if (hnae_get_field(flag, HNS_RXD_L3ID_M, HNS_RXD_L3ID_S)
366 == HNS_RX_FLAG_L3ID_IPV4) {
367 if ((typeof(max_size))(network - data) >
368 (max_size - sizeof(struct iphdr)))
369 return max_size;
370
371 /* access ihl as a u8 to avoid unaligned access on ia64 */
372 hlen = (network[0] & 0x0F) << 2;
373
374 /* verify hlen meets minimum size requirements */
375 if (hlen < sizeof(struct iphdr))
376 return network - data;
377
378 /* record next protocol if header is present */
379 } else if (hnae_get_field(flag, HNS_RXD_L3ID_M, HNS_RXD_L3ID_S)
380 == HNS_RX_FLAG_L3ID_IPV6) {
381 if ((typeof(max_size))(network - data) >
382 (max_size - sizeof(struct ipv6hdr)))
383 return max_size;
384
385 /* record next protocol */
386 hlen = sizeof(struct ipv6hdr);
387 } else {
388 return network - data;
389 }
390
391 /* relocate pointer to start of L4 header */
392 network += hlen;
393
394 /* finally sort out TCP/UDP */
395 if (hnae_get_field(flag, HNS_RXD_L4ID_M, HNS_RXD_L4ID_S)
396 == HNS_RX_FLAG_L4ID_TCP) {
397 if ((typeof(max_size))(network - data) >
398 (max_size - sizeof(struct tcphdr)))
399 return max_size;
400
401 /* access doff as a u8 to avoid unaligned access on ia64 */
402 hlen = (network[12] & 0xF0) >> 2;
403
404 /* verify hlen meets minimum size requirements */
405 if (hlen < sizeof(struct tcphdr))
406 return network - data;
407
408 network += hlen;
409 } else if (hnae_get_field(flag, HNS_RXD_L4ID_M, HNS_RXD_L4ID_S)
410 == HNS_RX_FLAG_L4ID_UDP) {
411 if ((typeof(max_size))(network - data) >
412 (max_size - sizeof(struct udphdr)))
413 return max_size;
414
415 network += sizeof(struct udphdr);
416 }
417
418 /* If everything has gone correctly network should be the
419 * data section of the packet and will be the end of the header.
420 * If not then it probably represents the end of the last recognized
421 * header.
422 */
423 if ((typeof(max_size))(network - data) < max_size)
424 return network - data;
425 else
426 return max_size;
427}
428
429static void
430hns_nic_reuse_page(struct hnae_desc_cb *desc_cb, int tsize, int last_offset)
431{
432 /* avoid re-using remote pages,flag default unreuse */
433 if (likely(page_to_nid(desc_cb->priv) == numa_node_id())) {
434 /* move offset up to the next cache line */
435 desc_cb->page_offset += tsize;
436
437 if (desc_cb->page_offset <= last_offset) {
438 desc_cb->reuse_flag = 1;
439 /* bump ref count on page before it is given*/
440 get_page(desc_cb->priv);
441 }
442 }
443}
444
13ac695e
S
445static void get_v2rx_desc_bnum(u32 bnum_flag, int *out_bnum)
446{
447 *out_bnum = hnae_get_field(bnum_flag,
448 HNS_RXD_BUFNUM_M, HNS_RXD_BUFNUM_S) + 1;
449}
450
451static void get_rx_desc_bnum(u32 bnum_flag, int *out_bnum)
452{
453 *out_bnum = hnae_get_field(bnum_flag,
454 HNS_RXD_BUFNUM_M, HNS_RXD_BUFNUM_S);
455}
456
b5996f11 457static int hns_nic_poll_rx_skb(struct hns_nic_ring_data *ring_data,
458 struct sk_buff **out_skb, int *out_bnum)
459{
460 struct hnae_ring *ring = ring_data->ring;
461 struct net_device *ndev = ring_data->napi.dev;
13ac695e 462 struct hns_nic_priv *priv = netdev_priv(ndev);
b5996f11 463 struct sk_buff *skb;
464 struct hnae_desc *desc;
465 struct hnae_desc_cb *desc_cb;
466 unsigned char *va;
467 int bnum, length, size, i, truesize, last_offset;
468 int pull_len;
469 u32 bnum_flag;
470
471 last_offset = hnae_page_size(ring) - hnae_buf_size(ring);
472 desc = &ring->desc[ring->next_to_clean];
473 desc_cb = &ring->desc_cb[ring->next_to_clean];
13ac695e
S
474
475 prefetch(desc);
476
b5996f11 477 va = (unsigned char *)desc_cb->buf + desc_cb->page_offset;
478
13ac695e
S
479 /* prefetch first cache line of first page */
480 prefetch(va);
481#if L1_CACHE_BYTES < 128
482 prefetch(va + L1_CACHE_BYTES);
483#endif
484
485 skb = *out_skb = napi_alloc_skb(&ring_data->napi,
486 HNS_RX_HEAD_SIZE);
b5996f11 487 if (unlikely(!skb)) {
488 netdev_err(ndev, "alloc rx skb fail\n");
489 ring->stats.sw_err_cnt++;
490 return -ENOMEM;
491 }
492
13ac695e
S
493 length = le16_to_cpu(desc->rx.pkt_len);
494 bnum_flag = le32_to_cpu(desc->rx.ipoff_bnum_pid_flag);
495 priv->ops.get_rxd_bnum(bnum_flag, &bnum);
496 *out_bnum = bnum;
497
498 /* we will be copying header into skb->data in
499 * pskb_may_pull so it is in our interest to prefetch
500 * it now to avoid a possible cache miss
501 */
502 prefetchw(skb->data);
503
b5996f11 504 if (length <= HNS_RX_HEAD_SIZE) {
505 memcpy(__skb_put(skb, length), va, ALIGN(length, sizeof(long)));
506
507 /* we can reuse buffer as-is, just make sure it is local */
508 if (likely(page_to_nid(desc_cb->priv) == numa_node_id()))
509 desc_cb->reuse_flag = 1;
510 else /* this page cannot be reused so discard it */
511 put_page(desc_cb->priv);
512
513 ring_ptr_move_fw(ring, next_to_clean);
514
515 if (unlikely(bnum != 1)) { /* check err*/
516 *out_bnum = 1;
517 goto out_bnum_err;
518 }
519 } else {
520 ring->stats.seg_pkt_cnt++;
521
522 pull_len = hns_nic_get_headlen(va, bnum_flag, HNS_RX_HEAD_SIZE);
523 memcpy(__skb_put(skb, pull_len), va,
524 ALIGN(pull_len, sizeof(long)));
525
526 size = le16_to_cpu(desc->rx.size);
527 truesize = ALIGN(size, L1_CACHE_BYTES);
528 skb_add_rx_frag(skb, 0, desc_cb->priv,
529 desc_cb->page_offset + pull_len,
530 size - pull_len, truesize - pull_len);
531
532 hns_nic_reuse_page(desc_cb, truesize, last_offset);
533 ring_ptr_move_fw(ring, next_to_clean);
534
535 if (unlikely(bnum >= (int)MAX_SKB_FRAGS)) { /* check err*/
536 *out_bnum = 1;
537 goto out_bnum_err;
538 }
539 for (i = 1; i < bnum; i++) {
540 desc = &ring->desc[ring->next_to_clean];
541 desc_cb = &ring->desc_cb[ring->next_to_clean];
542 size = le16_to_cpu(desc->rx.size);
543 truesize = ALIGN(size, L1_CACHE_BYTES);
544 skb_add_rx_frag(skb, i, desc_cb->priv,
545 desc_cb->page_offset,
546 size, truesize);
547
548 hns_nic_reuse_page(desc_cb, truesize, last_offset);
549 ring_ptr_move_fw(ring, next_to_clean);
550 }
551 }
552
553 /* check except process, free skb and jump the desc */
554 if (unlikely((!bnum) || (bnum > ring->max_desc_num_per_pkt))) {
555out_bnum_err:
556 *out_bnum = *out_bnum ? *out_bnum : 1; /* ntc moved,cannot 0*/
557 netdev_err(ndev, "invalid bnum(%d,%d,%d,%d),%016llx,%016llx\n",
558 bnum, ring->max_desc_num_per_pkt,
559 length, (int)MAX_SKB_FRAGS,
560 ((u64 *)desc)[0], ((u64 *)desc)[1]);
561 ring->stats.err_bd_num++;
562 dev_kfree_skb_any(skb);
563 return -EDOM;
564 }
565
566 bnum_flag = le32_to_cpu(desc->rx.ipoff_bnum_pid_flag);
567
568 if (unlikely(!hnae_get_bit(bnum_flag, HNS_RXD_VLD_B))) {
569 netdev_err(ndev, "no valid bd,%016llx,%016llx\n",
570 ((u64 *)desc)[0], ((u64 *)desc)[1]);
571 ring->stats.non_vld_descs++;
572 dev_kfree_skb_any(skb);
573 return -EINVAL;
574 }
575
576 if (unlikely((!desc->rx.pkt_len) ||
577 hnae_get_bit(bnum_flag, HNS_RXD_DROP_B))) {
b5996f11 578 ring->stats.err_pkt_len++;
579 dev_kfree_skb_any(skb);
580 return -EFAULT;
581 }
582
583 if (unlikely(hnae_get_bit(bnum_flag, HNS_RXD_L2E_B))) {
b5996f11 584 ring->stats.l2_err++;
585 dev_kfree_skb_any(skb);
586 return -EFAULT;
587 }
588
589 ring->stats.rx_pkts++;
590 ring->stats.rx_bytes += skb->len;
591
592 if (unlikely(hnae_get_bit(bnum_flag, HNS_RXD_L3E_B) ||
593 hnae_get_bit(bnum_flag, HNS_RXD_L4E_B))) {
b5996f11 594 ring->stats.l3l4_csum_err++;
595 return 0;
596 }
597
598 skb->ip_summed = CHECKSUM_UNNECESSARY;
599
600 return 0;
601}
602
603static void
604hns_nic_alloc_rx_buffers(struct hns_nic_ring_data *ring_data, int cleand_count)
605{
606 int i, ret;
607 struct hnae_desc_cb res_cbs;
608 struct hnae_desc_cb *desc_cb;
609 struct hnae_ring *ring = ring_data->ring;
610 struct net_device *ndev = ring_data->napi.dev;
611
612 for (i = 0; i < cleand_count; i++) {
613 desc_cb = &ring->desc_cb[ring->next_to_use];
614 if (desc_cb->reuse_flag) {
615 ring->stats.reuse_pg_cnt++;
616 hnae_reuse_buffer(ring, ring->next_to_use);
617 } else {
618 ret = hnae_reserve_buffer_map(ring, &res_cbs);
619 if (ret) {
620 ring->stats.sw_err_cnt++;
621 netdev_err(ndev, "hnae reserve buffer map failed.\n");
622 break;
623 }
624 hnae_replace_buffer(ring, ring->next_to_use, &res_cbs);
625 }
626
627 ring_ptr_move_fw(ring, next_to_use);
628 }
629
630 wmb(); /* make all data has been write before submit */
631 writel_relaxed(i, ring->io_base + RCB_REG_HEAD);
632}
633
634/* return error number for error or number of desc left to take
635 */
636static void hns_nic_rx_up_pro(struct hns_nic_ring_data *ring_data,
637 struct sk_buff *skb)
638{
639 struct net_device *ndev = ring_data->napi.dev;
640
641 skb->protocol = eth_type_trans(skb, ndev);
642 (void)napi_gro_receive(&ring_data->napi, skb);
643 ndev->last_rx = jiffies;
644}
645
646static int hns_nic_rx_poll_one(struct hns_nic_ring_data *ring_data,
647 int budget, void *v)
648{
649 struct hnae_ring *ring = ring_data->ring;
650 struct sk_buff *skb;
651 int num, bnum, ex_num;
652#define RCB_NOF_ALLOC_RX_BUFF_ONCE 16
653 int recv_pkts, recv_bds, clean_count, err;
654
655 num = readl_relaxed(ring->io_base + RCB_REG_FBDNUM);
656 rmb(); /* make sure num taken effect before the other data is touched */
657
658 recv_pkts = 0, recv_bds = 0, clean_count = 0;
659recv:
660 while (recv_pkts < budget && recv_bds < num) {
661 /* reuse or realloc buffers*/
662 if (clean_count >= RCB_NOF_ALLOC_RX_BUFF_ONCE) {
663 hns_nic_alloc_rx_buffers(ring_data, clean_count);
664 clean_count = 0;
665 }
666
667 /* poll one pkg*/
668 err = hns_nic_poll_rx_skb(ring_data, &skb, &bnum);
669 if (unlikely(!skb)) /* this fault cannot be repaired */
670 break;
671
672 recv_bds += bnum;
673 clean_count += bnum;
674 if (unlikely(err)) { /* do jump the err */
675 recv_pkts++;
676 continue;
677 }
678
679 /* do update ip stack process*/
680 ((void (*)(struct hns_nic_ring_data *, struct sk_buff *))v)(
681 ring_data, skb);
682 recv_pkts++;
683 }
684
685 /* make all data has been write before submit */
b5996f11 686 if (recv_pkts < budget) {
687 ex_num = readl_relaxed(ring->io_base + RCB_REG_FBDNUM);
688 rmb(); /*complete read rx ring bd number*/
13ac695e
S
689 if (ex_num > clean_count) {
690 num += ex_num - clean_count;
b5996f11 691 goto recv;
692 }
693 }
694
13ac695e
S
695 /* make all data has been write before submit */
696 if (clean_count > 0)
697 hns_nic_alloc_rx_buffers(ring_data, clean_count);
698
b5996f11 699 return recv_pkts;
700}
701
702static void hns_nic_rx_fini_pro(struct hns_nic_ring_data *ring_data)
703{
704 struct hnae_ring *ring = ring_data->ring;
705 int num = 0;
706
707 /* for hardware bug fixed */
708 num = readl_relaxed(ring->io_base + RCB_REG_FBDNUM);
709
710 if (num > 0) {
711 ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
712 ring_data->ring, 1);
713
714 napi_schedule(&ring_data->napi);
715 }
716}
717
718static inline void hns_nic_reclaim_one_desc(struct hnae_ring *ring,
719 int *bytes, int *pkts)
720{
721 struct hnae_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_clean];
722
723 (*pkts) += (desc_cb->type == DESC_TYPE_SKB);
724 (*bytes) += desc_cb->length;
725 /* desc_cb will be cleaned, after hnae_free_buffer_detach*/
726 hnae_free_buffer_detach(ring, ring->next_to_clean);
727
728 ring_ptr_move_fw(ring, next_to_clean);
729}
730
731static int is_valid_clean_head(struct hnae_ring *ring, int h)
732{
733 int u = ring->next_to_use;
734 int c = ring->next_to_clean;
735
736 if (unlikely(h > ring->desc_num))
737 return 0;
738
739 assert(u > 0 && u < ring->desc_num);
740 assert(c > 0 && c < ring->desc_num);
741 assert(u != c && h != c); /* must be checked before call this func */
742
743 return u > c ? (h > c && h <= u) : (h > c || h <= u);
744}
745
746/* netif_tx_lock will turn down the performance, set only when necessary */
747#ifdef CONFIG_NET_POLL_CONTROLLER
748#define NETIF_TX_LOCK(ndev) netif_tx_lock(ndev)
749#define NETIF_TX_UNLOCK(ndev) netif_tx_unlock(ndev)
750#else
751#define NETIF_TX_LOCK(ndev)
752#define NETIF_TX_UNLOCK(ndev)
753#endif
754/* reclaim all desc in one budget
755 * return error or number of desc left
756 */
757static int hns_nic_tx_poll_one(struct hns_nic_ring_data *ring_data,
758 int budget, void *v)
759{
760 struct hnae_ring *ring = ring_data->ring;
761 struct net_device *ndev = ring_data->napi.dev;
762 struct netdev_queue *dev_queue;
763 struct hns_nic_priv *priv = netdev_priv(ndev);
764 int head;
765 int bytes, pkts;
766
767 NETIF_TX_LOCK(ndev);
768
769 head = readl_relaxed(ring->io_base + RCB_REG_HEAD);
770 rmb(); /* make sure head is ready before touch any data */
771
772 if (is_ring_empty(ring) || head == ring->next_to_clean) {
773 NETIF_TX_UNLOCK(ndev);
774 return 0; /* no data to poll */
775 }
776
777 if (!is_valid_clean_head(ring, head)) {
778 netdev_err(ndev, "wrong head (%d, %d-%d)\n", head,
779 ring->next_to_use, ring->next_to_clean);
780 ring->stats.io_err_cnt++;
781 NETIF_TX_UNLOCK(ndev);
782 return -EIO;
783 }
784
785 bytes = 0;
786 pkts = 0;
787 while (head != ring->next_to_clean)
788 hns_nic_reclaim_one_desc(ring, &bytes, &pkts);
789
790 NETIF_TX_UNLOCK(ndev);
791
792 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
793 netdev_tx_completed_queue(dev_queue, pkts, bytes);
794
13ac695e
S
795 if (unlikely(priv->link && !netif_carrier_ok(ndev)))
796 netif_carrier_on(ndev);
797
b5996f11 798 if (unlikely(pkts && netif_carrier_ok(ndev) &&
799 (ring_space(ring) >= ring->max_desc_num_per_pkt * 2))) {
800 /* Make sure that anybody stopping the queue after this
801 * sees the new next_to_clean.
802 */
803 smp_mb();
804 if (netif_tx_queue_stopped(dev_queue) &&
805 !test_bit(NIC_STATE_DOWN, &priv->state)) {
806 netif_tx_wake_queue(dev_queue);
807 ring->stats.restart_queue++;
808 }
809 }
810 return 0;
811}
812
813static void hns_nic_tx_fini_pro(struct hns_nic_ring_data *ring_data)
814{
815 struct hnae_ring *ring = ring_data->ring;
816 int head = ring->next_to_clean;
817
818 /* for hardware bug fixed */
819 head = readl_relaxed(ring->io_base + RCB_REG_HEAD);
820
821 if (head != ring->next_to_clean) {
822 ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
823 ring_data->ring, 1);
824
825 napi_schedule(&ring_data->napi);
826 }
827}
828
829static void hns_nic_tx_clr_all_bufs(struct hns_nic_ring_data *ring_data)
830{
831 struct hnae_ring *ring = ring_data->ring;
832 struct net_device *ndev = ring_data->napi.dev;
833 struct netdev_queue *dev_queue;
834 int head;
835 int bytes, pkts;
836
837 NETIF_TX_LOCK(ndev);
838
839 head = ring->next_to_use; /* ntu :soft setted ring position*/
840 bytes = 0;
841 pkts = 0;
842 while (head != ring->next_to_clean)
843 hns_nic_reclaim_one_desc(ring, &bytes, &pkts);
844
845 NETIF_TX_UNLOCK(ndev);
846
847 dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
848 netdev_tx_reset_queue(dev_queue);
849}
850
851static int hns_nic_common_poll(struct napi_struct *napi, int budget)
852{
853 struct hns_nic_ring_data *ring_data =
854 container_of(napi, struct hns_nic_ring_data, napi);
855 int clean_complete = ring_data->poll_one(
856 ring_data, budget, ring_data->ex_process);
857
858 if (clean_complete >= 0 && clean_complete < budget) {
859 napi_complete(napi);
860 ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
861 ring_data->ring, 0);
862
863 ring_data->fini_process(ring_data);
864 }
865
866 return clean_complete;
867}
868
869static irqreturn_t hns_irq_handle(int irq, void *dev)
870{
871 struct hns_nic_ring_data *ring_data = (struct hns_nic_ring_data *)dev;
872
873 ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
874 ring_data->ring, 1);
875 napi_schedule(&ring_data->napi);
876
877 return IRQ_HANDLED;
878}
879
880/**
881 *hns_nic_adjust_link - adjust net work mode by the phy stat or new param
882 *@ndev: net device
883 */
884static void hns_nic_adjust_link(struct net_device *ndev)
885{
886 struct hns_nic_priv *priv = netdev_priv(ndev);
887 struct hnae_handle *h = priv->ae_handle;
888
889 h->dev->ops->adjust_link(h, ndev->phydev->speed, ndev->phydev->duplex);
890}
891
892/**
893 *hns_nic_init_phy - init phy
894 *@ndev: net device
895 *@h: ae handle
896 * Return 0 on success, negative on failure
897 */
898int hns_nic_init_phy(struct net_device *ndev, struct hnae_handle *h)
899{
900 struct hns_nic_priv *priv = netdev_priv(ndev);
901 struct phy_device *phy_dev = NULL;
902
903 if (!h->phy_node)
904 return 0;
905
906 if (h->phy_if != PHY_INTERFACE_MODE_XGMII)
907 phy_dev = of_phy_connect(ndev, h->phy_node,
908 hns_nic_adjust_link, 0, h->phy_if);
909 else
910 phy_dev = of_phy_attach(ndev, h->phy_node, 0, h->phy_if);
911
912 if (unlikely(!phy_dev) || IS_ERR(phy_dev))
913 return !phy_dev ? -ENODEV : PTR_ERR(phy_dev);
914
915 phy_dev->supported &= h->if_support;
916 phy_dev->advertising = phy_dev->supported;
917
918 if (h->phy_if == PHY_INTERFACE_MODE_XGMII)
919 phy_dev->autoneg = false;
920
921 priv->phy = phy_dev;
922
923 return 0;
924}
925
926static int hns_nic_ring_open(struct net_device *netdev, int idx)
927{
928 struct hns_nic_priv *priv = netdev_priv(netdev);
929 struct hnae_handle *h = priv->ae_handle;
930
931 napi_enable(&priv->ring_data[idx].napi);
932
933 enable_irq(priv->ring_data[idx].ring->irq);
934 h->dev->ops->toggle_ring_irq(priv->ring_data[idx].ring, 0);
935
936 return 0;
937}
938
939static int hns_nic_net_set_mac_address(struct net_device *ndev, void *p)
940{
941 struct hns_nic_priv *priv = netdev_priv(ndev);
942 struct hnae_handle *h = priv->ae_handle;
943 struct sockaddr *mac_addr = p;
944 int ret;
945
946 if (!mac_addr || !is_valid_ether_addr((const u8 *)mac_addr->sa_data))
947 return -EADDRNOTAVAIL;
948
949 ret = h->dev->ops->set_mac_addr(h, mac_addr->sa_data);
950 if (ret) {
951 netdev_err(ndev, "set_mac_address fail, ret=%d!\n", ret);
952 return ret;
953 }
954
955 memcpy(ndev->dev_addr, mac_addr->sa_data, ndev->addr_len);
956
957 return 0;
958}
959
960void hns_nic_update_stats(struct net_device *netdev)
961{
962 struct hns_nic_priv *priv = netdev_priv(netdev);
963 struct hnae_handle *h = priv->ae_handle;
964
965 h->dev->ops->update_stats(h, &netdev->stats);
966}
967
968/* set mac addr if it is configed. or leave it to the AE driver */
969static void hns_init_mac_addr(struct net_device *ndev)
970{
971 struct hns_nic_priv *priv = netdev_priv(ndev);
972 struct device_node *node = priv->dev->of_node;
973 const void *mac_addr_temp;
974
975 mac_addr_temp = of_get_mac_address(node);
976 if (mac_addr_temp && is_valid_ether_addr(mac_addr_temp)) {
977 memcpy(ndev->dev_addr, mac_addr_temp, ndev->addr_len);
978 } else {
979 eth_hw_addr_random(ndev);
980 dev_warn(priv->dev, "No valid mac, use random mac %pM",
981 ndev->dev_addr);
982 }
983}
984
985static void hns_nic_ring_close(struct net_device *netdev, int idx)
986{
987 struct hns_nic_priv *priv = netdev_priv(netdev);
988 struct hnae_handle *h = priv->ae_handle;
989
990 h->dev->ops->toggle_ring_irq(priv->ring_data[idx].ring, 1);
991 disable_irq(priv->ring_data[idx].ring->irq);
992
993 napi_disable(&priv->ring_data[idx].napi);
994}
995
13ac695e 996static void hns_set_irq_affinity(struct hns_nic_priv *priv)
b5996f11 997{
998 struct hnae_handle *h = priv->ae_handle;
999 struct hns_nic_ring_data *rd;
1000 int i;
b5996f11 1001 int cpu;
1002 cpumask_t mask;
1003
13ac695e
S
1004 /*diffrent irq banlance for 16core and 32core*/
1005 if (h->q_num == num_possible_cpus()) {
1006 for (i = 0; i < h->q_num * 2; i++) {
1007 rd = &priv->ring_data[i];
1008 if (cpu_online(rd->queue_index)) {
1009 cpumask_clear(&mask);
1010 cpu = rd->queue_index;
1011 cpumask_set_cpu(cpu, &mask);
1012 (void)irq_set_affinity_hint(rd->ring->irq,
1013 &mask);
1014 }
1015 }
1016 } else {
1017 for (i = 0; i < h->q_num; i++) {
1018 rd = &priv->ring_data[i];
1019 if (cpu_online(rd->queue_index * 2)) {
1020 cpumask_clear(&mask);
1021 cpu = rd->queue_index * 2;
1022 cpumask_set_cpu(cpu, &mask);
1023 (void)irq_set_affinity_hint(rd->ring->irq,
1024 &mask);
1025 }
1026 }
1027
1028 for (i = h->q_num; i < h->q_num * 2; i++) {
1029 rd = &priv->ring_data[i];
1030 if (cpu_online(rd->queue_index * 2 + 1)) {
1031 cpumask_clear(&mask);
1032 cpu = rd->queue_index * 2 + 1;
1033 cpumask_set_cpu(cpu, &mask);
1034 (void)irq_set_affinity_hint(rd->ring->irq,
1035 &mask);
1036 }
1037 }
1038 }
1039}
1040
1041static int hns_nic_init_irq(struct hns_nic_priv *priv)
1042{
1043 struct hnae_handle *h = priv->ae_handle;
1044 struct hns_nic_ring_data *rd;
1045 int i;
1046 int ret;
1047
b5996f11 1048 for (i = 0; i < h->q_num * 2; i++) {
1049 rd = &priv->ring_data[i];
1050
1051 if (rd->ring->irq_init_flag == RCB_IRQ_INITED)
1052 break;
1053
1054 snprintf(rd->ring->ring_name, RCB_RING_NAME_LEN,
1055 "%s-%s%d", priv->netdev->name,
1056 (i < h->q_num ? "tx" : "rx"), rd->queue_index);
1057
1058 rd->ring->ring_name[RCB_RING_NAME_LEN - 1] = '\0';
1059
1060 ret = request_irq(rd->ring->irq,
1061 hns_irq_handle, 0, rd->ring->ring_name, rd);
1062 if (ret) {
1063 netdev_err(priv->netdev, "request irq(%d) fail\n",
1064 rd->ring->irq);
1065 return ret;
1066 }
1067 disable_irq(rd->ring->irq);
1068 rd->ring->irq_init_flag = RCB_IRQ_INITED;
b5996f11 1069 }
1070
13ac695e
S
1071 /*set cpu affinity*/
1072 hns_set_irq_affinity(priv);
1073
b5996f11 1074 return 0;
1075}
1076
1077static int hns_nic_net_up(struct net_device *ndev)
1078{
1079 struct hns_nic_priv *priv = netdev_priv(ndev);
1080 struct hnae_handle *h = priv->ae_handle;
1081 int i, j, k;
1082 int ret;
1083
1084 ret = hns_nic_init_irq(priv);
1085 if (ret != 0) {
1086 netdev_err(ndev, "hns init irq failed! ret=%d\n", ret);
1087 return ret;
1088 }
1089
1090 for (i = 0; i < h->q_num * 2; i++) {
1091 ret = hns_nic_ring_open(ndev, i);
1092 if (ret)
1093 goto out_has_some_queues;
1094 }
1095
1096 for (k = 0; k < h->q_num; k++)
1097 h->dev->ops->toggle_queue_status(h->qs[k], 1);
1098
1099 ret = h->dev->ops->set_mac_addr(h, ndev->dev_addr);
1100 if (ret)
1101 goto out_set_mac_addr_err;
1102
1103 ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
1104 if (ret)
1105 goto out_start_err;
1106
1107 if (priv->phy)
1108 phy_start(priv->phy);
1109
1110 clear_bit(NIC_STATE_DOWN, &priv->state);
1111 (void)mod_timer(&priv->service_timer, jiffies + SERVICE_TIMER_HZ);
1112
1113 return 0;
1114
1115out_start_err:
1116 netif_stop_queue(ndev);
1117out_set_mac_addr_err:
1118 for (k = 0; k < h->q_num; k++)
1119 h->dev->ops->toggle_queue_status(h->qs[k], 0);
1120out_has_some_queues:
1121 for (j = i - 1; j >= 0; j--)
1122 hns_nic_ring_close(ndev, j);
1123
1124 set_bit(NIC_STATE_DOWN, &priv->state);
1125
1126 return ret;
1127}
1128
1129static void hns_nic_net_down(struct net_device *ndev)
1130{
1131 int i;
1132 struct hnae_ae_ops *ops;
1133 struct hns_nic_priv *priv = netdev_priv(ndev);
1134
1135 if (test_and_set_bit(NIC_STATE_DOWN, &priv->state))
1136 return;
1137
1138 (void)del_timer_sync(&priv->service_timer);
1139 netif_tx_stop_all_queues(ndev);
1140 netif_carrier_off(ndev);
1141 netif_tx_disable(ndev);
1142 priv->link = 0;
1143
1144 if (priv->phy)
1145 phy_stop(priv->phy);
1146
1147 ops = priv->ae_handle->dev->ops;
1148
1149 if (ops->stop)
1150 ops->stop(priv->ae_handle);
1151
1152 netif_tx_stop_all_queues(ndev);
1153
1154 for (i = priv->ae_handle->q_num - 1; i >= 0; i--) {
1155 hns_nic_ring_close(ndev, i);
1156 hns_nic_ring_close(ndev, i + priv->ae_handle->q_num);
1157
1158 /* clean tx buffers*/
1159 hns_nic_tx_clr_all_bufs(priv->ring_data + i);
1160 }
1161}
1162
1163void hns_nic_net_reset(struct net_device *ndev)
1164{
1165 struct hns_nic_priv *priv = netdev_priv(ndev);
1166 struct hnae_handle *handle = priv->ae_handle;
1167
1168 while (test_and_set_bit(NIC_STATE_RESETTING, &priv->state))
1169 usleep_range(1000, 2000);
1170
1171 (void)hnae_reinit_handle(handle);
1172
1173 clear_bit(NIC_STATE_RESETTING, &priv->state);
1174}
1175
1176void hns_nic_net_reinit(struct net_device *netdev)
1177{
1178 struct hns_nic_priv *priv = netdev_priv(netdev);
1179
1180 priv->netdev->trans_start = jiffies;
1181 while (test_and_set_bit(NIC_STATE_REINITING, &priv->state))
1182 usleep_range(1000, 2000);
1183
1184 hns_nic_net_down(netdev);
1185 hns_nic_net_reset(netdev);
1186 (void)hns_nic_net_up(netdev);
1187 clear_bit(NIC_STATE_REINITING, &priv->state);
1188}
1189
1190static int hns_nic_net_open(struct net_device *ndev)
1191{
1192 struct hns_nic_priv *priv = netdev_priv(ndev);
1193 struct hnae_handle *h = priv->ae_handle;
1194 int ret;
1195
1196 if (test_bit(NIC_STATE_TESTING, &priv->state))
1197 return -EBUSY;
1198
1199 priv->link = 0;
1200 netif_carrier_off(ndev);
1201
1202 ret = netif_set_real_num_tx_queues(ndev, h->q_num);
1203 if (ret < 0) {
1204 netdev_err(ndev, "netif_set_real_num_tx_queues fail, ret=%d!\n",
1205 ret);
1206 return ret;
1207 }
1208
1209 ret = netif_set_real_num_rx_queues(ndev, h->q_num);
1210 if (ret < 0) {
1211 netdev_err(ndev,
1212 "netif_set_real_num_rx_queues fail, ret=%d!\n", ret);
1213 return ret;
1214 }
1215
1216 ret = hns_nic_net_up(ndev);
1217 if (ret) {
1218 netdev_err(ndev,
1219 "hns net up fail, ret=%d!\n", ret);
1220 return ret;
1221 }
1222
1223 return 0;
1224}
1225
1226static int hns_nic_net_stop(struct net_device *ndev)
1227{
1228 hns_nic_net_down(ndev);
1229
1230 return 0;
1231}
1232
1233static void hns_tx_timeout_reset(struct hns_nic_priv *priv);
1234static void hns_nic_net_timeout(struct net_device *ndev)
1235{
1236 struct hns_nic_priv *priv = netdev_priv(ndev);
1237
1238 hns_tx_timeout_reset(priv);
1239}
1240
1241static int hns_nic_do_ioctl(struct net_device *netdev, struct ifreq *ifr,
1242 int cmd)
1243{
1244 struct hns_nic_priv *priv = netdev_priv(netdev);
1245 struct phy_device *phy_dev = priv->phy;
1246
1247 if (!netif_running(netdev))
1248 return -EINVAL;
1249
1250 if (!phy_dev)
1251 return -ENOTSUPP;
1252
1253 return phy_mii_ioctl(phy_dev, ifr, cmd);
1254}
1255
1256/* use only for netconsole to poll with the device without interrupt */
1257#ifdef CONFIG_NET_POLL_CONTROLLER
1258void hns_nic_poll_controller(struct net_device *ndev)
1259{
1260 struct hns_nic_priv *priv = netdev_priv(ndev);
1261 unsigned long flags;
1262 int i;
1263
1264 local_irq_save(flags);
1265 for (i = 0; i < priv->ae_handle->q_num * 2; i++)
1266 napi_schedule(&priv->ring_data[i].napi);
1267 local_irq_restore(flags);
1268}
1269#endif
1270
1271static netdev_tx_t hns_nic_net_xmit(struct sk_buff *skb,
1272 struct net_device *ndev)
1273{
1274 struct hns_nic_priv *priv = netdev_priv(ndev);
1275 int ret;
1276
1277 assert(skb->queue_mapping < ndev->ae_handle->q_num);
1278 ret = hns_nic_net_xmit_hw(ndev, skb,
1279 &tx_ring_data(priv, skb->queue_mapping));
1280 if (ret == NETDEV_TX_OK) {
1281 ndev->trans_start = jiffies;
1282 ndev->stats.tx_bytes += skb->len;
1283 ndev->stats.tx_packets++;
1284 }
1285 return (netdev_tx_t)ret;
1286}
1287
1288static int hns_nic_change_mtu(struct net_device *ndev, int new_mtu)
1289{
1290 struct hns_nic_priv *priv = netdev_priv(ndev);
1291 struct hnae_handle *h = priv->ae_handle;
1292 int ret;
1293
1294 /* MTU < 68 is an error and causes problems on some kernels */
1295 if (new_mtu < 68)
1296 return -EINVAL;
1297
1298 if (!h->dev->ops->set_mtu)
1299 return -ENOTSUPP;
1300
1301 if (netif_running(ndev)) {
1302 (void)hns_nic_net_stop(ndev);
1303 msleep(100);
1304
1305 ret = h->dev->ops->set_mtu(h, new_mtu);
1306 if (ret)
1307 netdev_err(ndev, "set mtu fail, return value %d\n",
1308 ret);
1309
1310 if (hns_nic_net_open(ndev))
1311 netdev_err(ndev, "hns net open fail\n");
1312 } else {
1313 ret = h->dev->ops->set_mtu(h, new_mtu);
1314 }
1315
1316 if (!ret)
1317 ndev->mtu = new_mtu;
1318
1319 return ret;
1320}
1321
1322/**
1323 * nic_set_multicast_list - set mutl mac address
1324 * @netdev: net device
1325 * @p: mac address
1326 *
1327 * return void
1328 */
1329void hns_set_multicast_list(struct net_device *ndev)
1330{
1331 struct hns_nic_priv *priv = netdev_priv(ndev);
1332 struct hnae_handle *h = priv->ae_handle;
1333 struct netdev_hw_addr *ha = NULL;
1334
1335 if (!h) {
1336 netdev_err(ndev, "hnae handle is null\n");
1337 return;
1338 }
1339
1340 if (h->dev->ops->set_mc_addr) {
1341 netdev_for_each_mc_addr(ha, ndev)
1342 if (h->dev->ops->set_mc_addr(h, ha->addr))
1343 netdev_err(ndev, "set multicast fail\n");
1344 }
1345}
1346
4568637f 1347void hns_nic_set_rx_mode(struct net_device *ndev)
1348{
1349 struct hns_nic_priv *priv = netdev_priv(ndev);
1350 struct hnae_handle *h = priv->ae_handle;
1351
1352 if (h->dev->ops->set_promisc_mode) {
1353 if (ndev->flags & IFF_PROMISC)
1354 h->dev->ops->set_promisc_mode(h, 1);
1355 else
1356 h->dev->ops->set_promisc_mode(h, 0);
1357 }
1358
1359 hns_set_multicast_list(ndev);
1360}
1361
b5996f11 1362struct rtnl_link_stats64 *hns_nic_get_stats64(struct net_device *ndev,
1363 struct rtnl_link_stats64 *stats)
1364{
1365 int idx = 0;
1366 u64 tx_bytes = 0;
1367 u64 rx_bytes = 0;
1368 u64 tx_pkts = 0;
1369 u64 rx_pkts = 0;
1370 struct hns_nic_priv *priv = netdev_priv(ndev);
1371 struct hnae_handle *h = priv->ae_handle;
1372
1373 for (idx = 0; idx < h->q_num; idx++) {
1374 tx_bytes += h->qs[idx]->tx_ring.stats.tx_bytes;
1375 tx_pkts += h->qs[idx]->tx_ring.stats.tx_pkts;
1376 rx_bytes += h->qs[idx]->rx_ring.stats.rx_bytes;
1377 rx_pkts += h->qs[idx]->rx_ring.stats.rx_pkts;
1378 }
1379
1380 stats->tx_bytes = tx_bytes;
1381 stats->tx_packets = tx_pkts;
1382 stats->rx_bytes = rx_bytes;
1383 stats->rx_packets = rx_pkts;
1384
1385 stats->rx_errors = ndev->stats.rx_errors;
1386 stats->multicast = ndev->stats.multicast;
1387 stats->rx_length_errors = ndev->stats.rx_length_errors;
1388 stats->rx_crc_errors = ndev->stats.rx_crc_errors;
1389 stats->rx_missed_errors = ndev->stats.rx_missed_errors;
1390
1391 stats->tx_errors = ndev->stats.tx_errors;
1392 stats->rx_dropped = ndev->stats.rx_dropped;
1393 stats->tx_dropped = ndev->stats.tx_dropped;
1394 stats->collisions = ndev->stats.collisions;
1395 stats->rx_over_errors = ndev->stats.rx_over_errors;
1396 stats->rx_frame_errors = ndev->stats.rx_frame_errors;
1397 stats->rx_fifo_errors = ndev->stats.rx_fifo_errors;
1398 stats->tx_aborted_errors = ndev->stats.tx_aborted_errors;
1399 stats->tx_carrier_errors = ndev->stats.tx_carrier_errors;
1400 stats->tx_fifo_errors = ndev->stats.tx_fifo_errors;
1401 stats->tx_heartbeat_errors = ndev->stats.tx_heartbeat_errors;
1402 stats->tx_window_errors = ndev->stats.tx_window_errors;
1403 stats->rx_compressed = ndev->stats.rx_compressed;
1404 stats->tx_compressed = ndev->stats.tx_compressed;
1405
1406 return stats;
1407}
1408
1409static const struct net_device_ops hns_nic_netdev_ops = {
1410 .ndo_open = hns_nic_net_open,
1411 .ndo_stop = hns_nic_net_stop,
1412 .ndo_start_xmit = hns_nic_net_xmit,
1413 .ndo_tx_timeout = hns_nic_net_timeout,
1414 .ndo_set_mac_address = hns_nic_net_set_mac_address,
1415 .ndo_change_mtu = hns_nic_change_mtu,
1416 .ndo_do_ioctl = hns_nic_do_ioctl,
1417 .ndo_get_stats64 = hns_nic_get_stats64,
1418#ifdef CONFIG_NET_POLL_CONTROLLER
1419 .ndo_poll_controller = hns_nic_poll_controller,
1420#endif
4568637f 1421 .ndo_set_rx_mode = hns_nic_set_rx_mode,
b5996f11 1422};
1423
1424static void hns_nic_update_link_status(struct net_device *netdev)
1425{
1426 struct hns_nic_priv *priv = netdev_priv(netdev);
1427
1428 struct hnae_handle *h = priv->ae_handle;
1429 int state = 1;
1430
1431 if (priv->phy) {
1432 if (!genphy_update_link(priv->phy))
1433 state = priv->phy->link;
1434 else
1435 state = 0;
1436 }
1437 state = state && h->dev->ops->get_status(h);
1438
1439 if (state != priv->link) {
1440 if (state) {
1441 netif_carrier_on(netdev);
1442 netif_tx_wake_all_queues(netdev);
1443 netdev_info(netdev, "link up\n");
1444 } else {
1445 netif_carrier_off(netdev);
1446 netdev_info(netdev, "link down\n");
1447 }
1448 priv->link = state;
1449 }
1450}
1451
1452/* for dumping key regs*/
1453static void hns_nic_dump(struct hns_nic_priv *priv)
1454{
1455 struct hnae_handle *h = priv->ae_handle;
1456 struct hnae_ae_ops *ops = h->dev->ops;
1457 u32 *data, reg_num, i;
1458
1459 if (ops->get_regs_len && ops->get_regs) {
1460 reg_num = ops->get_regs_len(priv->ae_handle);
1461 reg_num = (reg_num + 3ul) & ~3ul;
1462 data = kcalloc(reg_num, sizeof(u32), GFP_KERNEL);
1463 if (data) {
1464 ops->get_regs(priv->ae_handle, data);
1465 for (i = 0; i < reg_num; i += 4)
1466 pr_info("0x%08x: 0x%08x 0x%08x 0x%08x 0x%08x\n",
1467 i, data[i], data[i + 1],
1468 data[i + 2], data[i + 3]);
1469 kfree(data);
1470 }
1471 }
1472
1473 for (i = 0; i < h->q_num; i++) {
1474 pr_info("tx_queue%d_next_to_clean:%d\n",
1475 i, h->qs[i]->tx_ring.next_to_clean);
1476 pr_info("tx_queue%d_next_to_use:%d\n",
1477 i, h->qs[i]->tx_ring.next_to_use);
1478 pr_info("rx_queue%d_next_to_clean:%d\n",
1479 i, h->qs[i]->rx_ring.next_to_clean);
1480 pr_info("rx_queue%d_next_to_use:%d\n",
1481 i, h->qs[i]->rx_ring.next_to_use);
1482 }
1483}
1484
1485/* for resetting suntask*/
1486static void hns_nic_reset_subtask(struct hns_nic_priv *priv)
1487{
1488 enum hnae_port_type type = priv->ae_handle->port_type;
1489
1490 if (!test_bit(NIC_STATE2_RESET_REQUESTED, &priv->state))
1491 return;
1492 clear_bit(NIC_STATE2_RESET_REQUESTED, &priv->state);
1493
1494 /* If we're already down, removing or resetting, just bail */
1495 if (test_bit(NIC_STATE_DOWN, &priv->state) ||
1496 test_bit(NIC_STATE_REMOVING, &priv->state) ||
1497 test_bit(NIC_STATE_RESETTING, &priv->state))
1498 return;
1499
1500 hns_nic_dump(priv);
13ac695e
S
1501 netdev_info(priv->netdev, "try to reset %s port!\n",
1502 (type == HNAE_PORT_DEBUG ? "debug" : "service"));
b5996f11 1503
1504 rtnl_lock();
90a505b9 1505 /* put off any impending NetWatchDogTimeout */
1506 priv->netdev->trans_start = jiffies;
1507
13ac695e 1508 if (type == HNAE_PORT_DEBUG) {
b5996f11 1509 hns_nic_net_reinit(priv->netdev);
13ac695e
S
1510 } else {
1511 netif_carrier_off(priv->netdev);
1512 netif_tx_disable(priv->netdev);
1513 }
b5996f11 1514 rtnl_unlock();
1515}
1516
1517/* for doing service complete*/
1518static void hns_nic_service_event_complete(struct hns_nic_priv *priv)
1519{
13ac695e 1520 WARN_ON(!test_bit(NIC_STATE_SERVICE_SCHED, &priv->state));
b5996f11 1521
1522 smp_mb__before_atomic();
1523 clear_bit(NIC_STATE_SERVICE_SCHED, &priv->state);
1524}
1525
1526static void hns_nic_service_task(struct work_struct *work)
1527{
1528 struct hns_nic_priv *priv
1529 = container_of(work, struct hns_nic_priv, service_task);
1530 struct hnae_handle *h = priv->ae_handle;
1531
1532 hns_nic_update_link_status(priv->netdev);
1533 h->dev->ops->update_led_status(h);
1534 hns_nic_update_stats(priv->netdev);
1535
1536 hns_nic_reset_subtask(priv);
1537 hns_nic_service_event_complete(priv);
1538}
1539
1540static void hns_nic_task_schedule(struct hns_nic_priv *priv)
1541{
1542 if (!test_bit(NIC_STATE_DOWN, &priv->state) &&
1543 !test_bit(NIC_STATE_REMOVING, &priv->state) &&
1544 !test_and_set_bit(NIC_STATE_SERVICE_SCHED, &priv->state))
1545 (void)schedule_work(&priv->service_task);
1546}
1547
1548static void hns_nic_service_timer(unsigned long data)
1549{
1550 struct hns_nic_priv *priv = (struct hns_nic_priv *)data;
1551
1552 (void)mod_timer(&priv->service_timer, jiffies + SERVICE_TIMER_HZ);
1553
1554 hns_nic_task_schedule(priv);
1555}
1556
1557/**
1558 * hns_tx_timeout_reset - initiate reset due to Tx timeout
1559 * @priv: driver private struct
1560 **/
1561static void hns_tx_timeout_reset(struct hns_nic_priv *priv)
1562{
1563 /* Do the reset outside of interrupt context */
1564 if (!test_bit(NIC_STATE_DOWN, &priv->state)) {
1565 set_bit(NIC_STATE2_RESET_REQUESTED, &priv->state);
1566 netdev_warn(priv->netdev,
1567 "initiating reset due to tx timeout(%llu,0x%lx)\n",
1568 priv->tx_timeout_count, priv->state);
1569 priv->tx_timeout_count++;
1570 hns_nic_task_schedule(priv);
1571 }
1572}
1573
1574static int hns_nic_init_ring_data(struct hns_nic_priv *priv)
1575{
1576 struct hnae_handle *h = priv->ae_handle;
1577 struct hns_nic_ring_data *rd;
1578 int i;
1579
1580 if (h->q_num > NIC_MAX_Q_PER_VF) {
1581 netdev_err(priv->netdev, "too much queue (%d)\n", h->q_num);
1582 return -EINVAL;
1583 }
1584
1585 priv->ring_data = kzalloc(h->q_num * sizeof(*priv->ring_data) * 2,
1586 GFP_KERNEL);
1587 if (!priv->ring_data)
1588 return -ENOMEM;
1589
1590 for (i = 0; i < h->q_num; i++) {
1591 rd = &priv->ring_data[i];
1592 rd->queue_index = i;
1593 rd->ring = &h->qs[i]->tx_ring;
1594 rd->poll_one = hns_nic_tx_poll_one;
1595 rd->fini_process = hns_nic_tx_fini_pro;
1596
1597 netif_napi_add(priv->netdev, &rd->napi,
1598 hns_nic_common_poll, NIC_TX_CLEAN_MAX_NUM);
1599 rd->ring->irq_init_flag = RCB_IRQ_NOT_INITED;
1600 }
1601 for (i = h->q_num; i < h->q_num * 2; i++) {
1602 rd = &priv->ring_data[i];
1603 rd->queue_index = i - h->q_num;
1604 rd->ring = &h->qs[i - h->q_num]->rx_ring;
1605 rd->poll_one = hns_nic_rx_poll_one;
1606 rd->ex_process = hns_nic_rx_up_pro;
1607 rd->fini_process = hns_nic_rx_fini_pro;
1608
1609 netif_napi_add(priv->netdev, &rd->napi,
1610 hns_nic_common_poll, NIC_RX_CLEAN_MAX_NUM);
1611 rd->ring->irq_init_flag = RCB_IRQ_NOT_INITED;
1612 }
1613
1614 return 0;
1615}
1616
1617static void hns_nic_uninit_ring_data(struct hns_nic_priv *priv)
1618{
1619 struct hnae_handle *h = priv->ae_handle;
1620 int i;
1621
1622 for (i = 0; i < h->q_num * 2; i++) {
1623 netif_napi_del(&priv->ring_data[i].napi);
1624 if (priv->ring_data[i].ring->irq_init_flag == RCB_IRQ_INITED) {
13ac695e
S
1625 (void)irq_set_affinity_hint(
1626 priv->ring_data[i].ring->irq,
1627 NULL);
b5996f11 1628 free_irq(priv->ring_data[i].ring->irq,
1629 &priv->ring_data[i]);
1630 }
1631
1632 priv->ring_data[i].ring->irq_init_flag = RCB_IRQ_NOT_INITED;
1633 }
1634 kfree(priv->ring_data);
1635}
1636
13ac695e
S
1637static void hns_nic_set_priv_ops(struct net_device *netdev)
1638{
1639 struct hns_nic_priv *priv = netdev_priv(netdev);
1640
1641 if (AE_IS_VER1(priv->enet_ver)) {
1642 priv->ops.fill_desc = fill_desc;
1643 priv->ops.get_rxd_bnum = get_rx_desc_bnum;
1644 priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tx;
1645 } else {
1646 priv->ops.get_rxd_bnum = get_v2rx_desc_bnum;
1647 priv->ops.fill_desc = fill_v2_desc;
1648 priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tx;
1649 }
1650}
1651
b5996f11 1652static int hns_nic_try_get_ae(struct net_device *ndev)
1653{
1654 struct hns_nic_priv *priv = netdev_priv(ndev);
1655 struct hnae_handle *h;
1656 int ret;
1657
1658 h = hnae_get_handle(&priv->netdev->dev,
1659 priv->ae_name, priv->port_id, NULL);
1660 if (IS_ERR_OR_NULL(h)) {
1661 ret = PTR_ERR(h);
1662 dev_dbg(priv->dev, "has not handle, register notifier!\n");
1663 goto out;
1664 }
1665 priv->ae_handle = h;
1666
1667 ret = hns_nic_init_phy(ndev, h);
1668 if (ret) {
1669 dev_err(priv->dev, "probe phy device fail!\n");
1670 goto out_init_phy;
1671 }
1672
1673 ret = hns_nic_init_ring_data(priv);
1674 if (ret) {
1675 ret = -ENOMEM;
1676 goto out_init_ring_data;
1677 }
1678
13ac695e
S
1679 hns_nic_set_priv_ops(ndev);
1680
b5996f11 1681 ret = register_netdev(ndev);
1682 if (ret) {
1683 dev_err(priv->dev, "probe register netdev fail!\n");
1684 goto out_reg_ndev_fail;
1685 }
1686 return 0;
1687
1688out_reg_ndev_fail:
1689 hns_nic_uninit_ring_data(priv);
1690 priv->ring_data = NULL;
1691out_init_phy:
1692out_init_ring_data:
1693 hnae_put_handle(priv->ae_handle);
1694 priv->ae_handle = NULL;
1695out:
1696 return ret;
1697}
1698
1699static int hns_nic_notifier_action(struct notifier_block *nb,
1700 unsigned long action, void *data)
1701{
1702 struct hns_nic_priv *priv =
1703 container_of(nb, struct hns_nic_priv, notifier_block);
1704
1705 assert(action == HNAE_AE_REGISTER);
1706
1707 if (!hns_nic_try_get_ae(priv->netdev)) {
1708 hnae_unregister_notifier(&priv->notifier_block);
1709 priv->notifier_block.notifier_call = NULL;
1710 }
1711 return 0;
1712}
1713
1714static int hns_nic_dev_probe(struct platform_device *pdev)
1715{
1716 struct device *dev = &pdev->dev;
1717 struct net_device *ndev;
1718 struct hns_nic_priv *priv;
1719 struct device_node *node = dev->of_node;
1720 int ret;
1721
1722 ndev = alloc_etherdev_mq(sizeof(struct hns_nic_priv), NIC_MAX_Q_PER_VF);
1723 if (!ndev)
1724 return -ENOMEM;
1725
1726 platform_set_drvdata(pdev, ndev);
1727
1728 priv = netdev_priv(ndev);
1729 priv->dev = dev;
1730 priv->netdev = ndev;
1731
13ac695e 1732 if (of_device_is_compatible(node, "hisilicon,hns-nic-v1"))
b5996f11 1733 priv->enet_ver = AE_VERSION_1;
13ac695e
S
1734 else
1735 priv->enet_ver = AE_VERSION_2;
b5996f11 1736
1737 ret = of_property_read_string(node, "ae-name", &priv->ae_name);
1738 if (ret)
1739 goto out_read_string_fail;
1740
1741 ret = of_property_read_u32(node, "port-id", &priv->port_id);
1742 if (ret)
1743 goto out_read_string_fail;
1744
1745 hns_init_mac_addr(ndev);
1746
1747 ndev->watchdog_timeo = HNS_NIC_TX_TIMEOUT;
1748 ndev->priv_flags |= IFF_UNICAST_FLT;
1749 ndev->netdev_ops = &hns_nic_netdev_ops;
1750 hns_ethtool_set_ops(ndev);
13ac695e 1751
b5996f11 1752 ndev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1753 NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
1754 NETIF_F_GRO;
1755 ndev->vlan_features |=
1756 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
1757 ndev->vlan_features |= NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
1758
13ac695e
S
1759 switch (priv->enet_ver) {
1760 case AE_VERSION_2:
1761 ndev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1762 NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
1763 NETIF_F_GRO;
1764 break;
1765 default:
1766 break;
1767 }
1768
b5996f11 1769 SET_NETDEV_DEV(ndev, dev);
1770
1771 if (!dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
1772 dev_dbg(dev, "set mask to 64bit\n");
1773 else
1774 dev_err(dev, "set mask to 32bit fail!\n");
1775
1776 /* carrier off reporting is important to ethtool even BEFORE open */
1777 netif_carrier_off(ndev);
1778
1779 setup_timer(&priv->service_timer, hns_nic_service_timer,
1780 (unsigned long)priv);
1781 INIT_WORK(&priv->service_task, hns_nic_service_task);
1782
1783 set_bit(NIC_STATE_SERVICE_INITED, &priv->state);
1784 clear_bit(NIC_STATE_SERVICE_SCHED, &priv->state);
1785 set_bit(NIC_STATE_DOWN, &priv->state);
1786
1787 if (hns_nic_try_get_ae(priv->netdev)) {
1788 priv->notifier_block.notifier_call = hns_nic_notifier_action;
1789 ret = hnae_register_notifier(&priv->notifier_block);
1790 if (ret) {
1791 dev_err(dev, "register notifier fail!\n");
1792 goto out_notify_fail;
1793 }
1794 dev_dbg(dev, "has not handle, register notifier!\n");
1795 }
1796
1797 return 0;
1798
1799out_notify_fail:
1800 (void)cancel_work_sync(&priv->service_task);
1801out_read_string_fail:
1802 free_netdev(ndev);
1803 return ret;
1804}
1805
1806static int hns_nic_dev_remove(struct platform_device *pdev)
1807{
1808 struct net_device *ndev = platform_get_drvdata(pdev);
1809 struct hns_nic_priv *priv = netdev_priv(ndev);
1810
1811 if (ndev->reg_state != NETREG_UNINITIALIZED)
1812 unregister_netdev(ndev);
1813
1814 if (priv->ring_data)
1815 hns_nic_uninit_ring_data(priv);
1816 priv->ring_data = NULL;
1817
1818 if (priv->phy)
1819 phy_disconnect(priv->phy);
1820 priv->phy = NULL;
1821
1822 if (!IS_ERR_OR_NULL(priv->ae_handle))
1823 hnae_put_handle(priv->ae_handle);
1824 priv->ae_handle = NULL;
1825 if (priv->notifier_block.notifier_call)
1826 hnae_unregister_notifier(&priv->notifier_block);
1827 priv->notifier_block.notifier_call = NULL;
1828
1829 set_bit(NIC_STATE_REMOVING, &priv->state);
1830 (void)cancel_work_sync(&priv->service_task);
1831
1832 free_netdev(ndev);
1833 return 0;
1834}
1835
1836static const struct of_device_id hns_enet_of_match[] = {
1837 {.compatible = "hisilicon,hns-nic-v1",},
1838 {.compatible = "hisilicon,hns-nic-v2",},
1839 {},
1840};
1841
1842MODULE_DEVICE_TABLE(of, hns_enet_of_match);
1843
1844static struct platform_driver hns_nic_dev_driver = {
1845 .driver = {
1846 .name = "hns-nic",
b5996f11 1847 .of_match_table = hns_enet_of_match,
1848 },
1849 .probe = hns_nic_dev_probe,
1850 .remove = hns_nic_dev_remove,
1851};
1852
1853module_platform_driver(hns_nic_dev_driver);
1854
1855MODULE_DESCRIPTION("HISILICON HNS Ethernet driver");
1856MODULE_AUTHOR("Hisilicon, Inc.");
1857MODULE_LICENSE("GPL");
1858MODULE_ALIAS("platform:hns-nic");