OSDN Git Service

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