blob: fe54564abdd5a297a07bf3c52f31880876530028 [file] [log] [blame]
Salil76ad4f02017-08-02 16:59:45 +01001/*
2 * Copyright (c) 2016~2017 Hisilicon Limited.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10#include <linux/dma-mapping.h>
11#include <linux/etherdevice.h>
12#include <linux/interrupt.h>
13#include <linux/if_vlan.h>
14#include <linux/ip.h>
15#include <linux/ipv6.h>
16#include <linux/module.h>
17#include <linux/pci.h>
18#include <linux/skbuff.h>
19#include <linux/sctp.h>
20#include <linux/vermagic.h>
21#include <net/gre.h>
Yunsheng Lin30d240d2017-10-17 14:51:30 +080022#include <net/pkt_cls.h>
Salil76ad4f02017-08-02 16:59:45 +010023#include <net/vxlan.h>
24
25#include "hnae3.h"
26#include "hns3_enet.h"
27
Fuyun Liang7b763f32018-05-25 19:43:04 +010028static void hns3_clear_all_ring(struct hnae3_handle *h);
29static void hns3_force_clear_all_rx_ring(struct hnae3_handle *h);
30
Yunsheng Lin1db9b1b2017-10-09 15:44:01 +080031static const char hns3_driver_name[] = "hns3";
Salil76ad4f02017-08-02 16:59:45 +010032const char hns3_driver_version[] = VERMAGIC_STRING;
33static const char hns3_driver_string[] =
34 "Hisilicon Ethernet Network Driver for Hip08 Family";
35static const char hns3_copyright[] = "Copyright (c) 2017 Huawei Corporation.";
36static struct hnae3_client client;
37
38/* hns3_pci_tbl - PCI Device ID Table
39 *
40 * Last entry must be all 0s
41 *
42 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
43 * Class, Class Mask, private data (not used) }
44 */
45static const struct pci_device_id hns3_pci_tbl[] = {
46 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_GE), 0},
47 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE), 0},
Yunsheng Line92a0842017-09-20 18:52:50 +080048 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA),
Yunsheng Lin2daf4a62017-09-20 18:52:51 +080049 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
Yunsheng Line92a0842017-09-20 18:52:50 +080050 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA_MACSEC),
Yunsheng Lin2daf4a62017-09-20 18:52:51 +080051 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
Yunsheng Line92a0842017-09-20 18:52:50 +080052 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA),
Yunsheng Lin2daf4a62017-09-20 18:52:51 +080053 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
Yunsheng Line92a0842017-09-20 18:52:50 +080054 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA_MACSEC),
Yunsheng Lin2daf4a62017-09-20 18:52:51 +080055 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
Yunsheng Line92a0842017-09-20 18:52:50 +080056 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_MACSEC),
Yunsheng Lin2daf4a62017-09-20 18:52:51 +080057 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
Salil Mehta424eb832017-12-14 18:03:06 +000058 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_VF), 0},
59 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_DCB_PFC_VF), 0},
Salil76ad4f02017-08-02 16:59:45 +010060 /* required last entry */
61 {0, }
62};
63MODULE_DEVICE_TABLE(pci, hns3_pci_tbl);
64
65static irqreturn_t hns3_irq_handle(int irq, void *dev)
66{
67 struct hns3_enet_tqp_vector *tqp_vector = dev;
68
69 napi_schedule(&tqp_vector->napi);
70
71 return IRQ_HANDLED;
72}
73
74static void hns3_nic_uninit_irq(struct hns3_nic_priv *priv)
75{
76 struct hns3_enet_tqp_vector *tqp_vectors;
77 unsigned int i;
78
79 for (i = 0; i < priv->vector_num; i++) {
80 tqp_vectors = &priv->tqp_vector[i];
81
82 if (tqp_vectors->irq_init_flag != HNS3_VECTOR_INITED)
83 continue;
84
85 /* release the irq resource */
86 free_irq(tqp_vectors->vector_irq, tqp_vectors);
87 tqp_vectors->irq_init_flag = HNS3_VECTOR_NOT_INITED;
88 }
89}
90
91static int hns3_nic_init_irq(struct hns3_nic_priv *priv)
92{
93 struct hns3_enet_tqp_vector *tqp_vectors;
94 int txrx_int_idx = 0;
95 int rx_int_idx = 0;
96 int tx_int_idx = 0;
97 unsigned int i;
98 int ret;
99
100 for (i = 0; i < priv->vector_num; i++) {
101 tqp_vectors = &priv->tqp_vector[i];
102
103 if (tqp_vectors->irq_init_flag == HNS3_VECTOR_INITED)
104 continue;
105
106 if (tqp_vectors->tx_group.ring && tqp_vectors->rx_group.ring) {
107 snprintf(tqp_vectors->name, HNAE3_INT_NAME_LEN - 1,
108 "%s-%s-%d", priv->netdev->name, "TxRx",
109 txrx_int_idx++);
110 txrx_int_idx++;
111 } else if (tqp_vectors->rx_group.ring) {
112 snprintf(tqp_vectors->name, HNAE3_INT_NAME_LEN - 1,
113 "%s-%s-%d", priv->netdev->name, "Rx",
114 rx_int_idx++);
115 } else if (tqp_vectors->tx_group.ring) {
116 snprintf(tqp_vectors->name, HNAE3_INT_NAME_LEN - 1,
117 "%s-%s-%d", priv->netdev->name, "Tx",
118 tx_int_idx++);
119 } else {
120 /* Skip this unused q_vector */
121 continue;
122 }
123
124 tqp_vectors->name[HNAE3_INT_NAME_LEN - 1] = '\0';
125
126 ret = request_irq(tqp_vectors->vector_irq, hns3_irq_handle, 0,
127 tqp_vectors->name,
128 tqp_vectors);
129 if (ret) {
130 netdev_err(priv->netdev, "request irq(%d) fail\n",
131 tqp_vectors->vector_irq);
132 return ret;
133 }
134
135 tqp_vectors->irq_init_flag = HNS3_VECTOR_INITED;
136 }
137
138 return 0;
139}
140
141static void hns3_mask_vector_irq(struct hns3_enet_tqp_vector *tqp_vector,
142 u32 mask_en)
143{
144 writel(mask_en, tqp_vector->mask_addr);
145}
146
147static void hns3_vector_enable(struct hns3_enet_tqp_vector *tqp_vector)
148{
149 napi_enable(&tqp_vector->napi);
150
151 /* enable vector */
152 hns3_mask_vector_irq(tqp_vector, 1);
153}
154
155static void hns3_vector_disable(struct hns3_enet_tqp_vector *tqp_vector)
156{
157 /* disable vector */
158 hns3_mask_vector_irq(tqp_vector, 0);
159
160 disable_irq(tqp_vector->vector_irq);
161 napi_disable(&tqp_vector->napi);
162}
163
Fuyun Liang434776a2018-01-12 16:23:10 +0800164void hns3_set_vector_coalesce_rl(struct hns3_enet_tqp_vector *tqp_vector,
165 u32 rl_value)
Salil76ad4f02017-08-02 16:59:45 +0100166{
Fuyun Liang434776a2018-01-12 16:23:10 +0800167 u32 rl_reg = hns3_rl_usec_to_reg(rl_value);
168
Salil76ad4f02017-08-02 16:59:45 +0100169 /* this defines the configuration for RL (Interrupt Rate Limiter).
170 * Rl defines rate of interrupts i.e. number of interrupts-per-second
171 * GL and RL(Rate Limiter) are 2 ways to acheive interrupt coalescing
172 */
Fuyun Liang434776a2018-01-12 16:23:10 +0800173
Yunsheng Lin9bc727a2018-03-09 10:37:03 +0800174 if (rl_reg > 0 && !tqp_vector->tx_group.coal.gl_adapt_enable &&
175 !tqp_vector->rx_group.coal.gl_adapt_enable)
Fuyun Liang434776a2018-01-12 16:23:10 +0800176 /* According to the hardware, the range of rl_reg is
177 * 0-59 and the unit is 4.
178 */
179 rl_reg |= HNS3_INT_RL_ENABLE_MASK;
180
181 writel(rl_reg, tqp_vector->mask_addr + HNS3_VECTOR_RL_OFFSET);
182}
183
184void hns3_set_vector_coalesce_rx_gl(struct hns3_enet_tqp_vector *tqp_vector,
185 u32 gl_value)
186{
187 u32 rx_gl_reg = hns3_gl_usec_to_reg(gl_value);
188
189 writel(rx_gl_reg, tqp_vector->mask_addr + HNS3_VECTOR_GL0_OFFSET);
190}
191
192void hns3_set_vector_coalesce_tx_gl(struct hns3_enet_tqp_vector *tqp_vector,
193 u32 gl_value)
194{
195 u32 tx_gl_reg = hns3_gl_usec_to_reg(gl_value);
196
197 writel(tx_gl_reg, tqp_vector->mask_addr + HNS3_VECTOR_GL1_OFFSET);
Salil76ad4f02017-08-02 16:59:45 +0100198}
199
Fuyun Liang5fd47892018-01-12 16:23:11 +0800200static void hns3_vector_gl_rl_init(struct hns3_enet_tqp_vector *tqp_vector,
201 struct hns3_nic_priv *priv)
Salil76ad4f02017-08-02 16:59:45 +0100202{
Fuyun Liang5fd47892018-01-12 16:23:11 +0800203 struct hnae3_handle *h = priv->ae_handle;
204
Salil76ad4f02017-08-02 16:59:45 +0100205 /* initialize the configuration for interrupt coalescing.
206 * 1. GL (Interrupt Gap Limiter)
207 * 2. RL (Interrupt Rate Limiter)
208 */
209
Fuyun Liang5fd47892018-01-12 16:23:11 +0800210 /* Default: enable interrupt coalescing self-adaptive and GL */
Yunsheng Lin9bc727a2018-03-09 10:37:03 +0800211 tqp_vector->tx_group.coal.gl_adapt_enable = 1;
212 tqp_vector->rx_group.coal.gl_adapt_enable = 1;
Fuyun Liang5fd47892018-01-12 16:23:11 +0800213
Yunsheng Lin9bc727a2018-03-09 10:37:03 +0800214 tqp_vector->tx_group.coal.int_gl = HNS3_INT_GL_50K;
215 tqp_vector->rx_group.coal.int_gl = HNS3_INT_GL_50K;
Fuyun Liang5fd47892018-01-12 16:23:11 +0800216
Yunsheng Lindd38c722018-03-09 10:37:02 +0800217 /* Default: disable RL */
218 h->kinfo.int_rl_setting = 0;
219
Fuyun Liangcd9d1872018-03-21 15:49:25 +0800220 tqp_vector->int_adapt_down = HNS3_INT_ADAPT_DOWN_START;
Yunsheng Lin9bc727a2018-03-09 10:37:03 +0800221 tqp_vector->rx_group.coal.flow_level = HNS3_FLOW_LOW;
222 tqp_vector->tx_group.coal.flow_level = HNS3_FLOW_LOW;
Yunsheng Lindd38c722018-03-09 10:37:02 +0800223}
224
225static void hns3_vector_gl_rl_init_hw(struct hns3_enet_tqp_vector *tqp_vector,
226 struct hns3_nic_priv *priv)
227{
228 struct hnae3_handle *h = priv->ae_handle;
229
Fuyun Liang5fd47892018-01-12 16:23:11 +0800230 hns3_set_vector_coalesce_tx_gl(tqp_vector,
Yunsheng Lin9bc727a2018-03-09 10:37:03 +0800231 tqp_vector->tx_group.coal.int_gl);
Fuyun Liang5fd47892018-01-12 16:23:11 +0800232 hns3_set_vector_coalesce_rx_gl(tqp_vector,
Yunsheng Lin9bc727a2018-03-09 10:37:03 +0800233 tqp_vector->rx_group.coal.int_gl);
Fuyun Liang5fd47892018-01-12 16:23:11 +0800234 hns3_set_vector_coalesce_rl(tqp_vector, h->kinfo.int_rl_setting);
Salil76ad4f02017-08-02 16:59:45 +0100235}
236
Yunsheng Lin9df8f792017-09-27 09:45:32 +0800237static int hns3_nic_set_real_num_queue(struct net_device *netdev)
238{
Yunsheng Lin9780cb92017-10-09 15:43:56 +0800239 struct hnae3_handle *h = hns3_get_handle(netdev);
Yunsheng Lin9df8f792017-09-27 09:45:32 +0800240 struct hnae3_knic_private_info *kinfo = &h->kinfo;
241 unsigned int queue_size = kinfo->rss_size * kinfo->num_tc;
242 int ret;
243
244 ret = netif_set_real_num_tx_queues(netdev, queue_size);
245 if (ret) {
246 netdev_err(netdev,
247 "netif_set_real_num_tx_queues fail, ret=%d!\n",
248 ret);
249 return ret;
250 }
251
252 ret = netif_set_real_num_rx_queues(netdev, queue_size);
253 if (ret) {
254 netdev_err(netdev,
255 "netif_set_real_num_rx_queues fail, ret=%d!\n", ret);
256 return ret;
257 }
258
259 return 0;
260}
261
Peng Li678335a12018-03-08 19:41:54 +0800262static u16 hns3_get_max_available_channels(struct hnae3_handle *h)
263{
264 u16 free_tqps, max_rss_size, max_tqps;
265
266 h->ae_algo->ops->get_tqps_and_rss_info(h, &free_tqps, &max_rss_size);
267 max_tqps = h->kinfo.num_tc * max_rss_size;
268
269 return min_t(u16, max_tqps, (free_tqps + h->kinfo.num_tqps));
270}
271
Salil76ad4f02017-08-02 16:59:45 +0100272static int hns3_nic_net_up(struct net_device *netdev)
273{
274 struct hns3_nic_priv *priv = netdev_priv(netdev);
275 struct hnae3_handle *h = priv->ae_handle;
276 int i, j;
277 int ret;
278
Fuyun Liang7b763f32018-05-25 19:43:04 +0100279 ret = hns3_nic_reset_all_ring(h);
280 if (ret)
281 return ret;
282
Salil76ad4f02017-08-02 16:59:45 +0100283 /* get irq resource for all vectors */
284 ret = hns3_nic_init_irq(priv);
285 if (ret) {
286 netdev_err(netdev, "hns init irq failed! ret=%d\n", ret);
287 return ret;
288 }
289
290 /* enable the vectors */
291 for (i = 0; i < priv->vector_num; i++)
292 hns3_vector_enable(&priv->tqp_vector[i]);
293
294 /* start the ae_dev */
295 ret = h->ae_algo->ops->start ? h->ae_algo->ops->start(h) : 0;
296 if (ret)
297 goto out_start_err;
298
Jian Shenb875cc32018-01-05 18:18:11 +0800299 clear_bit(HNS3_NIC_STATE_DOWN, &priv->state);
300
Salil76ad4f02017-08-02 16:59:45 +0100301 return 0;
302
303out_start_err:
304 for (j = i - 1; j >= 0; j--)
305 hns3_vector_disable(&priv->tqp_vector[j]);
306
307 hns3_nic_uninit_irq(priv);
308
309 return ret;
310}
311
312static int hns3_nic_net_open(struct net_device *netdev)
313{
Lipengf8fa222c2017-11-02 20:45:20 +0800314 struct hns3_nic_priv *priv = netdev_priv(netdev);
Salil76ad4f02017-08-02 16:59:45 +0100315 int ret;
316
317 netif_carrier_off(netdev);
318
Yunsheng Lin9df8f792017-09-27 09:45:32 +0800319 ret = hns3_nic_set_real_num_queue(netdev);
320 if (ret)
Salil76ad4f02017-08-02 16:59:45 +0100321 return ret;
Salil76ad4f02017-08-02 16:59:45 +0100322
323 ret = hns3_nic_net_up(netdev);
324 if (ret) {
325 netdev_err(netdev,
326 "hns net up fail, ret=%d!\n", ret);
327 return ret;
328 }
329
Salil Mehta6d4c3982018-03-22 14:28:52 +0000330 priv->ae_handle->last_reset_time = jiffies;
Salil76ad4f02017-08-02 16:59:45 +0100331 return 0;
332}
333
334static void hns3_nic_net_down(struct net_device *netdev)
335{
336 struct hns3_nic_priv *priv = netdev_priv(netdev);
337 const struct hnae3_ae_ops *ops;
338 int i;
339
Jian Shenb875cc32018-01-05 18:18:11 +0800340 if (test_and_set_bit(HNS3_NIC_STATE_DOWN, &priv->state))
341 return;
342
Fuyun Liang7b763f32018-05-25 19:43:04 +0100343 /* disable vectors */
344 for (i = 0; i < priv->vector_num; i++)
345 hns3_vector_disable(&priv->tqp_vector[i]);
346
Salil76ad4f02017-08-02 16:59:45 +0100347 /* stop ae_dev */
348 ops = priv->ae_handle->ae_algo->ops;
349 if (ops->stop)
350 ops->stop(priv->ae_handle);
351
Salil76ad4f02017-08-02 16:59:45 +0100352 /* free irq resources */
353 hns3_nic_uninit_irq(priv);
Fuyun Liang7b763f32018-05-25 19:43:04 +0100354
355 hns3_clear_all_ring(priv->ae_handle);
Salil76ad4f02017-08-02 16:59:45 +0100356}
357
358static int hns3_nic_net_stop(struct net_device *netdev)
359{
360 netif_tx_stop_all_queues(netdev);
361 netif_carrier_off(netdev);
362
363 hns3_nic_net_down(netdev);
364
365 return 0;
366}
367
Salil76ad4f02017-08-02 16:59:45 +0100368static int hns3_nic_uc_sync(struct net_device *netdev,
369 const unsigned char *addr)
370{
Yunsheng Lin9780cb92017-10-09 15:43:56 +0800371 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +0100372
373 if (h->ae_algo->ops->add_uc_addr)
374 return h->ae_algo->ops->add_uc_addr(h, addr);
375
376 return 0;
377}
378
379static int hns3_nic_uc_unsync(struct net_device *netdev,
380 const unsigned char *addr)
381{
Yunsheng Lin9780cb92017-10-09 15:43:56 +0800382 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +0100383
384 if (h->ae_algo->ops->rm_uc_addr)
385 return h->ae_algo->ops->rm_uc_addr(h, addr);
386
387 return 0;
388}
389
390static int hns3_nic_mc_sync(struct net_device *netdev,
391 const unsigned char *addr)
392{
Yunsheng Lin9780cb92017-10-09 15:43:56 +0800393 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +0100394
Dan Carpenter720a8472017-08-10 12:56:14 +0300395 if (h->ae_algo->ops->add_mc_addr)
Salil76ad4f02017-08-02 16:59:45 +0100396 return h->ae_algo->ops->add_mc_addr(h, addr);
397
398 return 0;
399}
400
401static int hns3_nic_mc_unsync(struct net_device *netdev,
402 const unsigned char *addr)
403{
Yunsheng Lin9780cb92017-10-09 15:43:56 +0800404 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +0100405
Dan Carpenter720a8472017-08-10 12:56:14 +0300406 if (h->ae_algo->ops->rm_mc_addr)
Salil76ad4f02017-08-02 16:59:45 +0100407 return h->ae_algo->ops->rm_mc_addr(h, addr);
408
409 return 0;
410}
411
Yunsheng Lin1db9b1b2017-10-09 15:44:01 +0800412static void hns3_nic_set_rx_mode(struct net_device *netdev)
Salil76ad4f02017-08-02 16:59:45 +0100413{
Yunsheng Lin9780cb92017-10-09 15:43:56 +0800414 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +0100415
416 if (h->ae_algo->ops->set_promisc_mode) {
417 if (netdev->flags & IFF_PROMISC)
Peng Li3b75c3d2018-06-01 17:52:02 +0100418 h->ae_algo->ops->set_promisc_mode(h, true, true);
419 else if (netdev->flags & IFF_ALLMULTI)
420 h->ae_algo->ops->set_promisc_mode(h, false, true);
Salil76ad4f02017-08-02 16:59:45 +0100421 else
Peng Li3b75c3d2018-06-01 17:52:02 +0100422 h->ae_algo->ops->set_promisc_mode(h, false, false);
Salil76ad4f02017-08-02 16:59:45 +0100423 }
424 if (__dev_uc_sync(netdev, hns3_nic_uc_sync, hns3_nic_uc_unsync))
425 netdev_err(netdev, "sync uc address fail\n");
426 if (netdev->flags & IFF_MULTICAST)
427 if (__dev_mc_sync(netdev, hns3_nic_mc_sync, hns3_nic_mc_unsync))
428 netdev_err(netdev, "sync mc address fail\n");
429}
430
431static int hns3_set_tso(struct sk_buff *skb, u32 *paylen,
432 u16 *mss, u32 *type_cs_vlan_tso)
433{
434 u32 l4_offset, hdr_len;
435 union l3_hdr_info l3;
436 union l4_hdr_info l4;
437 u32 l4_paylen;
438 int ret;
439
440 if (!skb_is_gso(skb))
441 return 0;
442
443 ret = skb_cow_head(skb, 0);
444 if (ret)
445 return ret;
446
447 l3.hdr = skb_network_header(skb);
448 l4.hdr = skb_transport_header(skb);
449
450 /* Software should clear the IPv4's checksum field when tso is
451 * needed.
452 */
453 if (l3.v4->version == 4)
454 l3.v4->check = 0;
455
456 /* tunnel packet.*/
457 if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
458 SKB_GSO_GRE_CSUM |
459 SKB_GSO_UDP_TUNNEL |
460 SKB_GSO_UDP_TUNNEL_CSUM)) {
461 if ((!(skb_shinfo(skb)->gso_type &
462 SKB_GSO_PARTIAL)) &&
463 (skb_shinfo(skb)->gso_type &
464 SKB_GSO_UDP_TUNNEL_CSUM)) {
465 /* Software should clear the udp's checksum
466 * field when tso is needed.
467 */
468 l4.udp->check = 0;
469 }
470 /* reset l3&l4 pointers from outer to inner headers */
471 l3.hdr = skb_inner_network_header(skb);
472 l4.hdr = skb_inner_transport_header(skb);
473
474 /* Software should clear the IPv4's checksum field when
475 * tso is needed.
476 */
477 if (l3.v4->version == 4)
478 l3.v4->check = 0;
479 }
480
481 /* normal or tunnel packet*/
482 l4_offset = l4.hdr - skb->data;
483 hdr_len = (l4.tcp->doff * 4) + l4_offset;
484
485 /* remove payload length from inner pseudo checksum when tso*/
486 l4_paylen = skb->len - l4_offset;
487 csum_replace_by_diff(&l4.tcp->check,
488 (__force __wsum)htonl(l4_paylen));
489
490 /* find the txbd field values */
491 *paylen = skb->len - hdr_len;
492 hnae_set_bit(*type_cs_vlan_tso,
493 HNS3_TXD_TSO_B, 1);
494
495 /* get MSS for TSO */
496 *mss = skb_shinfo(skb)->gso_size;
497
498 return 0;
499}
500
Salil1898d4e2017-08-18 12:31:39 +0100501static int hns3_get_l4_protocol(struct sk_buff *skb, u8 *ol4_proto,
502 u8 *il4_proto)
Salil76ad4f02017-08-02 16:59:45 +0100503{
504 union {
505 struct iphdr *v4;
506 struct ipv6hdr *v6;
507 unsigned char *hdr;
508 } l3;
509 unsigned char *l4_hdr;
510 unsigned char *exthdr;
511 u8 l4_proto_tmp;
512 __be16 frag_off;
513
514 /* find outer header point */
515 l3.hdr = skb_network_header(skb);
Huazhong Tan35f58fd2018-05-01 19:55:58 +0100516 l4_hdr = skb_transport_header(skb);
Salil76ad4f02017-08-02 16:59:45 +0100517
518 if (skb->protocol == htons(ETH_P_IPV6)) {
519 exthdr = l3.hdr + sizeof(*l3.v6);
520 l4_proto_tmp = l3.v6->nexthdr;
521 if (l4_hdr != exthdr)
522 ipv6_skip_exthdr(skb, exthdr - skb->data,
523 &l4_proto_tmp, &frag_off);
524 } else if (skb->protocol == htons(ETH_P_IP)) {
525 l4_proto_tmp = l3.v4->protocol;
Salil1898d4e2017-08-18 12:31:39 +0100526 } else {
527 return -EINVAL;
Salil76ad4f02017-08-02 16:59:45 +0100528 }
529
530 *ol4_proto = l4_proto_tmp;
531
532 /* tunnel packet */
533 if (!skb->encapsulation) {
534 *il4_proto = 0;
Salil1898d4e2017-08-18 12:31:39 +0100535 return 0;
Salil76ad4f02017-08-02 16:59:45 +0100536 }
537
538 /* find inner header point */
539 l3.hdr = skb_inner_network_header(skb);
540 l4_hdr = skb_inner_transport_header(skb);
541
542 if (l3.v6->version == 6) {
543 exthdr = l3.hdr + sizeof(*l3.v6);
544 l4_proto_tmp = l3.v6->nexthdr;
545 if (l4_hdr != exthdr)
546 ipv6_skip_exthdr(skb, exthdr - skb->data,
547 &l4_proto_tmp, &frag_off);
548 } else if (l3.v4->version == 4) {
549 l4_proto_tmp = l3.v4->protocol;
550 }
551
552 *il4_proto = l4_proto_tmp;
Salil1898d4e2017-08-18 12:31:39 +0100553
554 return 0;
Salil76ad4f02017-08-02 16:59:45 +0100555}
556
557static void hns3_set_l2l3l4_len(struct sk_buff *skb, u8 ol4_proto,
558 u8 il4_proto, u32 *type_cs_vlan_tso,
559 u32 *ol_type_vlan_len_msec)
560{
561 union {
562 struct iphdr *v4;
563 struct ipv6hdr *v6;
564 unsigned char *hdr;
565 } l3;
566 union {
567 struct tcphdr *tcp;
568 struct udphdr *udp;
569 struct gre_base_hdr *gre;
570 unsigned char *hdr;
571 } l4;
572 unsigned char *l2_hdr;
573 u8 l4_proto = ol4_proto;
574 u32 ol2_len;
575 u32 ol3_len;
576 u32 ol4_len;
577 u32 l2_len;
578 u32 l3_len;
579
580 l3.hdr = skb_network_header(skb);
581 l4.hdr = skb_transport_header(skb);
582
583 /* compute L2 header size for normal packet, defined in 2 Bytes */
584 l2_len = l3.hdr - skb->data;
585 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L2LEN_M,
586 HNS3_TXD_L2LEN_S, l2_len >> 1);
587
588 /* tunnel packet*/
589 if (skb->encapsulation) {
590 /* compute OL2 header size, defined in 2 Bytes */
591 ol2_len = l2_len;
592 hnae_set_field(*ol_type_vlan_len_msec,
593 HNS3_TXD_L2LEN_M,
594 HNS3_TXD_L2LEN_S, ol2_len >> 1);
595
596 /* compute OL3 header size, defined in 4 Bytes */
597 ol3_len = l4.hdr - l3.hdr;
598 hnae_set_field(*ol_type_vlan_len_msec, HNS3_TXD_L3LEN_M,
599 HNS3_TXD_L3LEN_S, ol3_len >> 2);
600
601 /* MAC in UDP, MAC in GRE (0x6558)*/
602 if ((ol4_proto == IPPROTO_UDP) || (ol4_proto == IPPROTO_GRE)) {
603 /* switch MAC header ptr from outer to inner header.*/
604 l2_hdr = skb_inner_mac_header(skb);
605
606 /* compute OL4 header size, defined in 4 Bytes. */
607 ol4_len = l2_hdr - l4.hdr;
608 hnae_set_field(*ol_type_vlan_len_msec, HNS3_TXD_L4LEN_M,
609 HNS3_TXD_L4LEN_S, ol4_len >> 2);
610
611 /* switch IP header ptr from outer to inner header */
612 l3.hdr = skb_inner_network_header(skb);
613
614 /* compute inner l2 header size, defined in 2 Bytes. */
615 l2_len = l3.hdr - l2_hdr;
616 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L2LEN_M,
617 HNS3_TXD_L2LEN_S, l2_len >> 1);
618 } else {
619 /* skb packet types not supported by hardware,
620 * txbd len fild doesn't be filled.
621 */
622 return;
623 }
624
625 /* switch L4 header pointer from outer to inner */
626 l4.hdr = skb_inner_transport_header(skb);
627
628 l4_proto = il4_proto;
629 }
630
631 /* compute inner(/normal) L3 header size, defined in 4 Bytes */
632 l3_len = l4.hdr - l3.hdr;
633 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L3LEN_M,
634 HNS3_TXD_L3LEN_S, l3_len >> 2);
635
636 /* compute inner(/normal) L4 header size, defined in 4 Bytes */
637 switch (l4_proto) {
638 case IPPROTO_TCP:
639 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L4LEN_M,
640 HNS3_TXD_L4LEN_S, l4.tcp->doff);
641 break;
642 case IPPROTO_SCTP:
643 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L4LEN_M,
644 HNS3_TXD_L4LEN_S, (sizeof(struct sctphdr) >> 2));
645 break;
646 case IPPROTO_UDP:
647 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L4LEN_M,
648 HNS3_TXD_L4LEN_S, (sizeof(struct udphdr) >> 2));
649 break;
650 default:
651 /* skb packet types not supported by hardware,
652 * txbd len fild doesn't be filled.
653 */
654 return;
655 }
656}
657
658static int hns3_set_l3l4_type_csum(struct sk_buff *skb, u8 ol4_proto,
659 u8 il4_proto, u32 *type_cs_vlan_tso,
660 u32 *ol_type_vlan_len_msec)
661{
662 union {
663 struct iphdr *v4;
664 struct ipv6hdr *v6;
665 unsigned char *hdr;
666 } l3;
667 u32 l4_proto = ol4_proto;
668
669 l3.hdr = skb_network_header(skb);
670
671 /* define OL3 type and tunnel type(OL4).*/
672 if (skb->encapsulation) {
673 /* define outer network header type.*/
674 if (skb->protocol == htons(ETH_P_IP)) {
675 if (skb_is_gso(skb))
676 hnae_set_field(*ol_type_vlan_len_msec,
677 HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
678 HNS3_OL3T_IPV4_CSUM);
679 else
680 hnae_set_field(*ol_type_vlan_len_msec,
681 HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
682 HNS3_OL3T_IPV4_NO_CSUM);
683
684 } else if (skb->protocol == htons(ETH_P_IPV6)) {
685 hnae_set_field(*ol_type_vlan_len_msec, HNS3_TXD_OL3T_M,
686 HNS3_TXD_OL3T_S, HNS3_OL3T_IPV6);
687 }
688
689 /* define tunnel type(OL4).*/
690 switch (l4_proto) {
691 case IPPROTO_UDP:
692 hnae_set_field(*ol_type_vlan_len_msec,
693 HNS3_TXD_TUNTYPE_M,
694 HNS3_TXD_TUNTYPE_S,
695 HNS3_TUN_MAC_IN_UDP);
696 break;
697 case IPPROTO_GRE:
698 hnae_set_field(*ol_type_vlan_len_msec,
699 HNS3_TXD_TUNTYPE_M,
700 HNS3_TXD_TUNTYPE_S,
701 HNS3_TUN_NVGRE);
702 break;
703 default:
704 /* drop the skb tunnel packet if hardware don't support,
705 * because hardware can't calculate csum when TSO.
706 */
707 if (skb_is_gso(skb))
708 return -EDOM;
709
710 /* the stack computes the IP header already,
711 * driver calculate l4 checksum when not TSO.
712 */
713 skb_checksum_help(skb);
714 return 0;
715 }
716
717 l3.hdr = skb_inner_network_header(skb);
718 l4_proto = il4_proto;
719 }
720
721 if (l3.v4->version == 4) {
722 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L3T_M,
723 HNS3_TXD_L3T_S, HNS3_L3T_IPV4);
724
725 /* the stack computes the IP header already, the only time we
726 * need the hardware to recompute it is in the case of TSO.
727 */
728 if (skb_is_gso(skb))
729 hnae_set_bit(*type_cs_vlan_tso, HNS3_TXD_L3CS_B, 1);
730
731 hnae_set_bit(*type_cs_vlan_tso, HNS3_TXD_L4CS_B, 1);
732 } else if (l3.v6->version == 6) {
733 hnae_set_field(*type_cs_vlan_tso, HNS3_TXD_L3T_M,
734 HNS3_TXD_L3T_S, HNS3_L3T_IPV6);
735 hnae_set_bit(*type_cs_vlan_tso, HNS3_TXD_L4CS_B, 1);
736 }
737
738 switch (l4_proto) {
739 case IPPROTO_TCP:
740 hnae_set_field(*type_cs_vlan_tso,
741 HNS3_TXD_L4T_M,
742 HNS3_TXD_L4T_S,
743 HNS3_L4T_TCP);
744 break;
745 case IPPROTO_UDP:
746 hnae_set_field(*type_cs_vlan_tso,
747 HNS3_TXD_L4T_M,
748 HNS3_TXD_L4T_S,
749 HNS3_L4T_UDP);
750 break;
751 case IPPROTO_SCTP:
752 hnae_set_field(*type_cs_vlan_tso,
753 HNS3_TXD_L4T_M,
754 HNS3_TXD_L4T_S,
755 HNS3_L4T_SCTP);
756 break;
757 default:
758 /* drop the skb tunnel packet if hardware don't support,
759 * because hardware can't calculate csum when TSO.
760 */
761 if (skb_is_gso(skb))
762 return -EDOM;
763
764 /* the stack computes the IP header already,
765 * driver calculate l4 checksum when not TSO.
766 */
767 skb_checksum_help(skb);
768 return 0;
769 }
770
771 return 0;
772}
773
774static void hns3_set_txbd_baseinfo(u16 *bdtp_fe_sc_vld_ra_ri, int frag_end)
775{
776 /* Config bd buffer end */
777 hnae_set_field(*bdtp_fe_sc_vld_ra_ri, HNS3_TXD_BDTYPE_M,
Fuyun Liang3c8f5c02018-03-24 11:32:45 +0800778 HNS3_TXD_BDTYPE_S, 0);
Salil76ad4f02017-08-02 16:59:45 +0100779 hnae_set_bit(*bdtp_fe_sc_vld_ra_ri, HNS3_TXD_FE_B, !!frag_end);
780 hnae_set_bit(*bdtp_fe_sc_vld_ra_ri, HNS3_TXD_VLD_B, 1);
Lipeng7036d262017-10-24 21:02:09 +0800781 hnae_set_field(*bdtp_fe_sc_vld_ra_ri, HNS3_TXD_SC_M, HNS3_TXD_SC_S, 0);
Salil76ad4f02017-08-02 16:59:45 +0100782}
783
Peng Li9699cff2017-12-22 12:21:48 +0800784static int hns3_fill_desc_vtags(struct sk_buff *skb,
785 struct hns3_enet_ring *tx_ring,
786 u32 *inner_vlan_flag,
787 u32 *out_vlan_flag,
788 u16 *inner_vtag,
789 u16 *out_vtag)
790{
791#define HNS3_TX_VLAN_PRIO_SHIFT 13
792
793 if (skb->protocol == htons(ETH_P_8021Q) &&
794 !(tx_ring->tqp->handle->kinfo.netdev->features &
795 NETIF_F_HW_VLAN_CTAG_TX)) {
796 /* When HW VLAN acceleration is turned off, and the stack
797 * sets the protocol to 802.1q, the driver just need to
798 * set the protocol to the encapsulated ethertype.
799 */
800 skb->protocol = vlan_get_protocol(skb);
801 return 0;
802 }
803
804 if (skb_vlan_tag_present(skb)) {
805 u16 vlan_tag;
806
807 vlan_tag = skb_vlan_tag_get(skb);
808 vlan_tag |= (skb->priority & 0x7) << HNS3_TX_VLAN_PRIO_SHIFT;
809
810 /* Based on hw strategy, use out_vtag in two layer tag case,
811 * and use inner_vtag in one tag case.
812 */
813 if (skb->protocol == htons(ETH_P_8021Q)) {
814 hnae_set_bit(*out_vlan_flag, HNS3_TXD_OVLAN_B, 1);
815 *out_vtag = vlan_tag;
816 } else {
817 hnae_set_bit(*inner_vlan_flag, HNS3_TXD_VLAN_B, 1);
818 *inner_vtag = vlan_tag;
819 }
820 } else if (skb->protocol == htons(ETH_P_8021Q)) {
821 struct vlan_ethhdr *vhdr;
822 int rc;
823
824 rc = skb_cow_head(skb, 0);
825 if (rc < 0)
826 return rc;
827 vhdr = (struct vlan_ethhdr *)skb->data;
828 vhdr->h_vlan_TCI |= cpu_to_be16((skb->priority & 0x7)
829 << HNS3_TX_VLAN_PRIO_SHIFT);
830 }
831
832 skb->protocol = vlan_get_protocol(skb);
833 return 0;
834}
835
Salil76ad4f02017-08-02 16:59:45 +0100836static int hns3_fill_desc(struct hns3_enet_ring *ring, void *priv,
837 int size, dma_addr_t dma, int frag_end,
838 enum hns_desc_type type)
839{
840 struct hns3_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_use];
841 struct hns3_desc *desc = &ring->desc[ring->next_to_use];
842 u32 ol_type_vlan_len_msec = 0;
843 u16 bdtp_fe_sc_vld_ra_ri = 0;
844 u32 type_cs_vlan_tso = 0;
845 struct sk_buff *skb;
Peng Li9699cff2017-12-22 12:21:48 +0800846 u16 inner_vtag = 0;
847 u16 out_vtag = 0;
Salil76ad4f02017-08-02 16:59:45 +0100848 u32 paylen = 0;
849 u16 mss = 0;
850 __be16 protocol;
851 u8 ol4_proto;
852 u8 il4_proto;
853 int ret;
854
855 /* The txbd's baseinfo of DESC_TYPE_PAGE & DESC_TYPE_SKB */
856 desc_cb->priv = priv;
857 desc_cb->length = size;
858 desc_cb->dma = dma;
859 desc_cb->type = type;
860
861 /* now, fill the descriptor */
862 desc->addr = cpu_to_le64(dma);
863 desc->tx.send_size = cpu_to_le16((u16)size);
864 hns3_set_txbd_baseinfo(&bdtp_fe_sc_vld_ra_ri, frag_end);
865 desc->tx.bdtp_fe_sc_vld_ra_ri = cpu_to_le16(bdtp_fe_sc_vld_ra_ri);
866
867 if (type == DESC_TYPE_SKB) {
868 skb = (struct sk_buff *)priv;
Yunsheng Lina90bb9a2017-10-09 15:44:00 +0800869 paylen = skb->len;
Salil76ad4f02017-08-02 16:59:45 +0100870
Peng Li9699cff2017-12-22 12:21:48 +0800871 ret = hns3_fill_desc_vtags(skb, ring, &type_cs_vlan_tso,
872 &ol_type_vlan_len_msec,
873 &inner_vtag, &out_vtag);
874 if (unlikely(ret))
875 return ret;
876
Salil76ad4f02017-08-02 16:59:45 +0100877 if (skb->ip_summed == CHECKSUM_PARTIAL) {
878 skb_reset_mac_len(skb);
879 protocol = skb->protocol;
880
Salil1898d4e2017-08-18 12:31:39 +0100881 ret = hns3_get_l4_protocol(skb, &ol4_proto, &il4_proto);
882 if (ret)
883 return ret;
Salil76ad4f02017-08-02 16:59:45 +0100884 hns3_set_l2l3l4_len(skb, ol4_proto, il4_proto,
885 &type_cs_vlan_tso,
886 &ol_type_vlan_len_msec);
887 ret = hns3_set_l3l4_type_csum(skb, ol4_proto, il4_proto,
888 &type_cs_vlan_tso,
889 &ol_type_vlan_len_msec);
890 if (ret)
891 return ret;
892
893 ret = hns3_set_tso(skb, &paylen, &mss,
894 &type_cs_vlan_tso);
895 if (ret)
896 return ret;
897 }
898
899 /* Set txbd */
900 desc->tx.ol_type_vlan_len_msec =
901 cpu_to_le32(ol_type_vlan_len_msec);
902 desc->tx.type_cs_vlan_tso_len =
903 cpu_to_le32(type_cs_vlan_tso);
Yunsheng Lina90bb9a2017-10-09 15:44:00 +0800904 desc->tx.paylen = cpu_to_le32(paylen);
Salil76ad4f02017-08-02 16:59:45 +0100905 desc->tx.mss = cpu_to_le16(mss);
Peng Li9699cff2017-12-22 12:21:48 +0800906 desc->tx.vlan_tag = cpu_to_le16(inner_vtag);
907 desc->tx.outer_vlan_tag = cpu_to_le16(out_vtag);
Salil76ad4f02017-08-02 16:59:45 +0100908 }
909
910 /* move ring pointer to next.*/
911 ring_ptr_move_fw(ring, next_to_use);
912
913 return 0;
914}
915
916static int hns3_fill_desc_tso(struct hns3_enet_ring *ring, void *priv,
917 int size, dma_addr_t dma, int frag_end,
918 enum hns_desc_type type)
919{
920 unsigned int frag_buf_num;
921 unsigned int k;
922 int sizeoflast;
923 int ret;
924
925 frag_buf_num = (size + HNS3_MAX_BD_SIZE - 1) / HNS3_MAX_BD_SIZE;
926 sizeoflast = size % HNS3_MAX_BD_SIZE;
927 sizeoflast = sizeoflast ? sizeoflast : HNS3_MAX_BD_SIZE;
928
929 /* When the frag size is bigger than hardware, split this frag */
930 for (k = 0; k < frag_buf_num; k++) {
931 ret = hns3_fill_desc(ring, priv,
932 (k == frag_buf_num - 1) ?
933 sizeoflast : HNS3_MAX_BD_SIZE,
934 dma + HNS3_MAX_BD_SIZE * k,
935 frag_end && (k == frag_buf_num - 1) ? 1 : 0,
936 (type == DESC_TYPE_SKB && !k) ?
937 DESC_TYPE_SKB : DESC_TYPE_PAGE);
938 if (ret)
939 return ret;
940 }
941
942 return 0;
943}
944
945static int hns3_nic_maybe_stop_tso(struct sk_buff **out_skb, int *bnum,
946 struct hns3_enet_ring *ring)
947{
948 struct sk_buff *skb = *out_skb;
949 struct skb_frag_struct *frag;
950 int bdnum_for_frag;
951 int frag_num;
952 int buf_num;
953 int size;
954 int i;
955
956 size = skb_headlen(skb);
957 buf_num = (size + HNS3_MAX_BD_SIZE - 1) / HNS3_MAX_BD_SIZE;
958
959 frag_num = skb_shinfo(skb)->nr_frags;
960 for (i = 0; i < frag_num; i++) {
961 frag = &skb_shinfo(skb)->frags[i];
962 size = skb_frag_size(frag);
963 bdnum_for_frag =
964 (size + HNS3_MAX_BD_SIZE - 1) / HNS3_MAX_BD_SIZE;
965 if (bdnum_for_frag > HNS3_MAX_BD_PER_FRAG)
966 return -ENOMEM;
967
968 buf_num += bdnum_for_frag;
969 }
970
971 if (buf_num > ring_space(ring))
972 return -EBUSY;
973
974 *bnum = buf_num;
975 return 0;
976}
977
978static int hns3_nic_maybe_stop_tx(struct sk_buff **out_skb, int *bnum,
979 struct hns3_enet_ring *ring)
980{
981 struct sk_buff *skb = *out_skb;
982 int buf_num;
983
984 /* No. of segments (plus a header) */
985 buf_num = skb_shinfo(skb)->nr_frags + 1;
986
987 if (buf_num > ring_space(ring))
988 return -EBUSY;
989
990 *bnum = buf_num;
991
992 return 0;
993}
994
995static void hns_nic_dma_unmap(struct hns3_enet_ring *ring, int next_to_use_orig)
996{
997 struct device *dev = ring_to_dev(ring);
998 unsigned int i;
999
1000 for (i = 0; i < ring->desc_num; i++) {
1001 /* check if this is where we started */
1002 if (ring->next_to_use == next_to_use_orig)
1003 break;
1004
1005 /* unmap the descriptor dma address */
1006 if (ring->desc_cb[ring->next_to_use].type == DESC_TYPE_SKB)
1007 dma_unmap_single(dev,
1008 ring->desc_cb[ring->next_to_use].dma,
1009 ring->desc_cb[ring->next_to_use].length,
1010 DMA_TO_DEVICE);
1011 else
1012 dma_unmap_page(dev,
1013 ring->desc_cb[ring->next_to_use].dma,
1014 ring->desc_cb[ring->next_to_use].length,
1015 DMA_TO_DEVICE);
1016
1017 /* rollback one */
1018 ring_ptr_move_bw(ring, next_to_use);
1019 }
1020}
1021
Yunsheng Lind43e5ac2017-10-20 10:19:21 +08001022netdev_tx_t hns3_nic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
Salil76ad4f02017-08-02 16:59:45 +01001023{
1024 struct hns3_nic_priv *priv = netdev_priv(netdev);
1025 struct hns3_nic_ring_data *ring_data =
1026 &tx_ring_data(priv, skb->queue_mapping);
1027 struct hns3_enet_ring *ring = ring_data->ring;
1028 struct device *dev = priv->dev;
1029 struct netdev_queue *dev_queue;
1030 struct skb_frag_struct *frag;
1031 int next_to_use_head;
1032 int next_to_use_frag;
1033 dma_addr_t dma;
1034 int buf_num;
1035 int seg_num;
1036 int size;
1037 int ret;
1038 int i;
1039
1040 /* Prefetch the data used later */
1041 prefetch(skb->data);
1042
1043 switch (priv->ops.maybe_stop_tx(&skb, &buf_num, ring)) {
1044 case -EBUSY:
1045 u64_stats_update_begin(&ring->syncp);
1046 ring->stats.tx_busy++;
1047 u64_stats_update_end(&ring->syncp);
1048
1049 goto out_net_tx_busy;
1050 case -ENOMEM:
1051 u64_stats_update_begin(&ring->syncp);
1052 ring->stats.sw_err_cnt++;
1053 u64_stats_update_end(&ring->syncp);
1054 netdev_err(netdev, "no memory to xmit!\n");
1055
1056 goto out_err_tx_ok;
1057 default:
1058 break;
1059 }
1060
1061 /* No. of segments (plus a header) */
1062 seg_num = skb_shinfo(skb)->nr_frags + 1;
1063 /* Fill the first part */
1064 size = skb_headlen(skb);
1065
1066 next_to_use_head = ring->next_to_use;
1067
1068 dma = dma_map_single(dev, skb->data, size, DMA_TO_DEVICE);
1069 if (dma_mapping_error(dev, dma)) {
1070 netdev_err(netdev, "TX head DMA map failed\n");
1071 ring->stats.sw_err_cnt++;
1072 goto out_err_tx_ok;
1073 }
1074
1075 ret = priv->ops.fill_desc(ring, skb, size, dma, seg_num == 1 ? 1 : 0,
1076 DESC_TYPE_SKB);
1077 if (ret)
1078 goto head_dma_map_err;
1079
1080 next_to_use_frag = ring->next_to_use;
1081 /* Fill the fragments */
1082 for (i = 1; i < seg_num; i++) {
1083 frag = &skb_shinfo(skb)->frags[i - 1];
1084 size = skb_frag_size(frag);
1085 dma = skb_frag_dma_map(dev, frag, 0, size, DMA_TO_DEVICE);
1086 if (dma_mapping_error(dev, dma)) {
1087 netdev_err(netdev, "TX frag(%d) DMA map failed\n", i);
1088 ring->stats.sw_err_cnt++;
1089 goto frag_dma_map_err;
1090 }
1091 ret = priv->ops.fill_desc(ring, skb_frag_page(frag), size, dma,
1092 seg_num - 1 == i ? 1 : 0,
1093 DESC_TYPE_PAGE);
1094
1095 if (ret)
1096 goto frag_dma_map_err;
1097 }
1098
1099 /* Complete translate all packets */
1100 dev_queue = netdev_get_tx_queue(netdev, ring_data->queue_index);
1101 netdev_tx_sent_queue(dev_queue, skb->len);
1102
1103 wmb(); /* Commit all data before submit */
1104
1105 hnae_queue_xmit(ring->tqp, buf_num);
1106
1107 return NETDEV_TX_OK;
1108
1109frag_dma_map_err:
1110 hns_nic_dma_unmap(ring, next_to_use_frag);
1111
1112head_dma_map_err:
1113 hns_nic_dma_unmap(ring, next_to_use_head);
1114
1115out_err_tx_ok:
1116 dev_kfree_skb_any(skb);
1117 return NETDEV_TX_OK;
1118
1119out_net_tx_busy:
1120 netif_stop_subqueue(netdev, ring_data->queue_index);
1121 smp_mb(); /* Commit all data before submit */
1122
1123 return NETDEV_TX_BUSY;
1124}
1125
1126static int hns3_nic_net_set_mac_address(struct net_device *netdev, void *p)
1127{
Yunsheng Lin9780cb92017-10-09 15:43:56 +08001128 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +01001129 struct sockaddr *mac_addr = p;
1130 int ret;
1131
1132 if (!mac_addr || !is_valid_ether_addr((const u8 *)mac_addr->sa_data))
1133 return -EADDRNOTAVAIL;
1134
Jian Shen5ec2a512018-06-01 17:52:03 +01001135 if (ether_addr_equal(netdev->dev_addr, mac_addr->sa_data)) {
1136 netdev_info(netdev, "already using mac address %pM\n",
1137 mac_addr->sa_data);
1138 return 0;
1139 }
1140
Fuyun Liang590980552018-03-10 11:29:22 +08001141 ret = h->ae_algo->ops->set_mac_addr(h, mac_addr->sa_data, false);
Salil76ad4f02017-08-02 16:59:45 +01001142 if (ret) {
1143 netdev_err(netdev, "set_mac_address fail, ret=%d!\n", ret);
1144 return ret;
1145 }
1146
1147 ether_addr_copy(netdev->dev_addr, mac_addr->sa_data);
1148
1149 return 0;
1150}
1151
1152static int hns3_nic_set_features(struct net_device *netdev,
1153 netdev_features_t features)
1154{
Jian Shen181d4542018-01-12 16:23:16 +08001155 netdev_features_t changed = netdev->features ^ features;
Salil76ad4f02017-08-02 16:59:45 +01001156 struct hns3_nic_priv *priv = netdev_priv(netdev);
Peng Li052ece62017-12-22 12:21:47 +08001157 struct hnae3_handle *h = priv->ae_handle;
Peng Li052ece62017-12-22 12:21:47 +08001158 int ret;
Salil76ad4f02017-08-02 16:59:45 +01001159
Jian Shen181d4542018-01-12 16:23:16 +08001160 if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
1161 if (features & (NETIF_F_TSO | NETIF_F_TSO6)) {
1162 priv->ops.fill_desc = hns3_fill_desc_tso;
1163 priv->ops.maybe_stop_tx = hns3_nic_maybe_stop_tso;
1164 } else {
1165 priv->ops.fill_desc = hns3_fill_desc;
1166 priv->ops.maybe_stop_tx = hns3_nic_maybe_stop_tx;
1167 }
Salil76ad4f02017-08-02 16:59:45 +01001168 }
1169
Jian Shenbd368412018-01-12 16:23:17 +08001170 if ((changed & NETIF_F_HW_VLAN_CTAG_FILTER) &&
1171 h->ae_algo->ops->enable_vlan_filter) {
Jian Shen181d4542018-01-12 16:23:16 +08001172 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
1173 h->ae_algo->ops->enable_vlan_filter(h, true);
1174 else
1175 h->ae_algo->ops->enable_vlan_filter(h, false);
1176 }
Jian Shen391b5e92018-01-05 18:18:05 +08001177
Jian Shenbd368412018-01-12 16:23:17 +08001178 if ((changed & NETIF_F_HW_VLAN_CTAG_RX) &&
1179 h->ae_algo->ops->enable_hw_strip_rxvtag) {
Peng Li052ece62017-12-22 12:21:47 +08001180 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1181 ret = h->ae_algo->ops->enable_hw_strip_rxvtag(h, true);
1182 else
1183 ret = h->ae_algo->ops->enable_hw_strip_rxvtag(h, false);
1184
1185 if (ret)
1186 return ret;
1187 }
1188
Salil76ad4f02017-08-02 16:59:45 +01001189 netdev->features = features;
1190 return 0;
1191}
1192
Peng Li6c88d9d2018-01-09 14:50:59 +08001193static void hns3_nic_get_stats64(struct net_device *netdev,
1194 struct rtnl_link_stats64 *stats)
Salil76ad4f02017-08-02 16:59:45 +01001195{
1196 struct hns3_nic_priv *priv = netdev_priv(netdev);
1197 int queue_num = priv->ae_handle->kinfo.num_tqps;
Jian Shenc5f65482018-01-05 18:18:10 +08001198 struct hnae3_handle *handle = priv->ae_handle;
Salil76ad4f02017-08-02 16:59:45 +01001199 struct hns3_enet_ring *ring;
1200 unsigned int start;
1201 unsigned int idx;
1202 u64 tx_bytes = 0;
1203 u64 rx_bytes = 0;
1204 u64 tx_pkts = 0;
1205 u64 rx_pkts = 0;
Jian Shend2a5dca2018-01-05 18:18:12 +08001206 u64 tx_drop = 0;
1207 u64 rx_drop = 0;
Salil76ad4f02017-08-02 16:59:45 +01001208
Jian Shenb875cc32018-01-05 18:18:11 +08001209 if (test_bit(HNS3_NIC_STATE_DOWN, &priv->state))
1210 return;
1211
Jian Shenc5f65482018-01-05 18:18:10 +08001212 handle->ae_algo->ops->update_stats(handle, &netdev->stats);
1213
Salil76ad4f02017-08-02 16:59:45 +01001214 for (idx = 0; idx < queue_num; idx++) {
1215 /* fetch the tx stats */
1216 ring = priv->ring_data[idx].ring;
1217 do {
Salild36d36c2017-08-18 12:31:37 +01001218 start = u64_stats_fetch_begin_irq(&ring->syncp);
Salil76ad4f02017-08-02 16:59:45 +01001219 tx_bytes += ring->stats.tx_bytes;
1220 tx_pkts += ring->stats.tx_pkts;
Jian Shend2a5dca2018-01-05 18:18:12 +08001221 tx_drop += ring->stats.tx_busy;
1222 tx_drop += ring->stats.sw_err_cnt;
Salil76ad4f02017-08-02 16:59:45 +01001223 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
1224
1225 /* fetch the rx stats */
1226 ring = priv->ring_data[idx + queue_num].ring;
1227 do {
Salild36d36c2017-08-18 12:31:37 +01001228 start = u64_stats_fetch_begin_irq(&ring->syncp);
Salil76ad4f02017-08-02 16:59:45 +01001229 rx_bytes += ring->stats.rx_bytes;
1230 rx_pkts += ring->stats.rx_pkts;
Jian Shend2a5dca2018-01-05 18:18:12 +08001231 rx_drop += ring->stats.non_vld_descs;
1232 rx_drop += ring->stats.err_pkt_len;
1233 rx_drop += ring->stats.l2_err;
Salil76ad4f02017-08-02 16:59:45 +01001234 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
1235 }
1236
1237 stats->tx_bytes = tx_bytes;
1238 stats->tx_packets = tx_pkts;
1239 stats->rx_bytes = rx_bytes;
1240 stats->rx_packets = rx_pkts;
1241
1242 stats->rx_errors = netdev->stats.rx_errors;
1243 stats->multicast = netdev->stats.multicast;
1244 stats->rx_length_errors = netdev->stats.rx_length_errors;
1245 stats->rx_crc_errors = netdev->stats.rx_crc_errors;
1246 stats->rx_missed_errors = netdev->stats.rx_missed_errors;
1247
1248 stats->tx_errors = netdev->stats.tx_errors;
Jian Shend2a5dca2018-01-05 18:18:12 +08001249 stats->rx_dropped = rx_drop + netdev->stats.rx_dropped;
1250 stats->tx_dropped = tx_drop + netdev->stats.tx_dropped;
Salil76ad4f02017-08-02 16:59:45 +01001251 stats->collisions = netdev->stats.collisions;
1252 stats->rx_over_errors = netdev->stats.rx_over_errors;
1253 stats->rx_frame_errors = netdev->stats.rx_frame_errors;
1254 stats->rx_fifo_errors = netdev->stats.rx_fifo_errors;
1255 stats->tx_aborted_errors = netdev->stats.tx_aborted_errors;
1256 stats->tx_carrier_errors = netdev->stats.tx_carrier_errors;
1257 stats->tx_fifo_errors = netdev->stats.tx_fifo_errors;
1258 stats->tx_heartbeat_errors = netdev->stats.tx_heartbeat_errors;
1259 stats->tx_window_errors = netdev->stats.tx_window_errors;
1260 stats->rx_compressed = netdev->stats.rx_compressed;
1261 stats->tx_compressed = netdev->stats.tx_compressed;
1262}
1263
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001264static int hns3_setup_tc(struct net_device *netdev, void *type_data)
Salil76ad4f02017-08-02 16:59:45 +01001265{
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001266 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
Yunsheng Lin9780cb92017-10-09 15:43:56 +08001267 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +01001268 struct hnae3_knic_private_info *kinfo = &h->kinfo;
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001269 u8 *prio_tc = mqprio_qopt->qopt.prio_tc_map;
1270 u8 tc = mqprio_qopt->qopt.num_tc;
1271 u16 mode = mqprio_qopt->mode;
1272 u8 hw = mqprio_qopt->qopt.hw;
1273 bool if_running;
Salil76ad4f02017-08-02 16:59:45 +01001274 unsigned int i;
1275 int ret;
1276
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001277 if (!((hw == TC_MQPRIO_HW_OFFLOAD_TCS &&
1278 mode == TC_MQPRIO_MODE_CHANNEL) || (!hw && tc == 0)))
1279 return -EOPNOTSUPP;
1280
Salil76ad4f02017-08-02 16:59:45 +01001281 if (tc > HNAE3_MAX_TC)
1282 return -EINVAL;
1283
Salil76ad4f02017-08-02 16:59:45 +01001284 if (!netdev)
1285 return -EINVAL;
1286
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001287 if_running = netif_running(netdev);
1288 if (if_running) {
1289 hns3_nic_net_stop(netdev);
1290 msleep(100);
Salil76ad4f02017-08-02 16:59:45 +01001291 }
1292
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001293 ret = (kinfo->dcb_ops && kinfo->dcb_ops->setup_tc) ?
1294 kinfo->dcb_ops->setup_tc(h, tc, prio_tc) : -EOPNOTSUPP;
Salil76ad4f02017-08-02 16:59:45 +01001295 if (ret)
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001296 goto out;
Salil76ad4f02017-08-02 16:59:45 +01001297
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001298 if (tc <= 1) {
1299 netdev_reset_tc(netdev);
1300 } else {
1301 ret = netdev_set_num_tc(netdev, tc);
1302 if (ret)
1303 goto out;
1304
1305 for (i = 0; i < HNAE3_MAX_TC; i++) {
1306 if (!kinfo->tc_info[i].enable)
1307 continue;
1308
Salil76ad4f02017-08-02 16:59:45 +01001309 netdev_set_tc_queue(netdev,
1310 kinfo->tc_info[i].tc,
1311 kinfo->tc_info[i].tqp_count,
1312 kinfo->tc_info[i].tqp_offset);
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001313 }
Salil76ad4f02017-08-02 16:59:45 +01001314 }
1315
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001316 ret = hns3_nic_set_real_num_queue(netdev);
1317
1318out:
1319 if (if_running)
1320 hns3_nic_net_open(netdev);
1321
1322 return ret;
Salil76ad4f02017-08-02 16:59:45 +01001323}
1324
Jiri Pirko2572ac52017-08-07 10:15:17 +02001325static int hns3_nic_setup_tc(struct net_device *dev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02001326 void *type_data)
Salil76ad4f02017-08-02 16:59:45 +01001327{
Nogah Frankel575ed7d2017-11-06 07:23:42 +01001328 if (type != TC_SETUP_QDISC_MQPRIO)
Jiri Pirko38cf0422017-08-07 10:15:31 +02001329 return -EOPNOTSUPP;
Salil76ad4f02017-08-02 16:59:45 +01001330
Yunsheng Lin30d240d2017-10-17 14:51:30 +08001331 return hns3_setup_tc(dev, type_data);
Salil76ad4f02017-08-02 16:59:45 +01001332}
1333
1334static int hns3_vlan_rx_add_vid(struct net_device *netdev,
1335 __be16 proto, u16 vid)
1336{
Yunsheng Lin9780cb92017-10-09 15:43:56 +08001337 struct hnae3_handle *h = hns3_get_handle(netdev);
Yunsheng Lin681ec392018-03-21 15:49:22 +08001338 struct hns3_nic_priv *priv = netdev_priv(netdev);
Salil76ad4f02017-08-02 16:59:45 +01001339 int ret = -EIO;
1340
1341 if (h->ae_algo->ops->set_vlan_filter)
1342 ret = h->ae_algo->ops->set_vlan_filter(h, proto, vid, false);
1343
Yunsheng Lin681ec392018-03-21 15:49:22 +08001344 if (!ret)
1345 set_bit(vid, priv->active_vlans);
1346
Salil76ad4f02017-08-02 16:59:45 +01001347 return ret;
1348}
1349
1350static int hns3_vlan_rx_kill_vid(struct net_device *netdev,
1351 __be16 proto, u16 vid)
1352{
Yunsheng Lin9780cb92017-10-09 15:43:56 +08001353 struct hnae3_handle *h = hns3_get_handle(netdev);
Yunsheng Lin681ec392018-03-21 15:49:22 +08001354 struct hns3_nic_priv *priv = netdev_priv(netdev);
Salil76ad4f02017-08-02 16:59:45 +01001355 int ret = -EIO;
1356
1357 if (h->ae_algo->ops->set_vlan_filter)
1358 ret = h->ae_algo->ops->set_vlan_filter(h, proto, vid, true);
1359
Yunsheng Lin681ec392018-03-21 15:49:22 +08001360 if (!ret)
1361 clear_bit(vid, priv->active_vlans);
1362
Salil76ad4f02017-08-02 16:59:45 +01001363 return ret;
1364}
1365
Yunsheng Lin681ec392018-03-21 15:49:22 +08001366static void hns3_restore_vlan(struct net_device *netdev)
1367{
1368 struct hns3_nic_priv *priv = netdev_priv(netdev);
1369 u16 vid;
1370 int ret;
1371
1372 for_each_set_bit(vid, priv->active_vlans, VLAN_N_VID) {
1373 ret = hns3_vlan_rx_add_vid(netdev, htons(ETH_P_8021Q), vid);
1374 if (ret)
1375 netdev_warn(netdev, "Restore vlan: %d filter, ret:%d\n",
1376 vid, ret);
1377 }
1378}
1379
Salil76ad4f02017-08-02 16:59:45 +01001380static int hns3_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan,
1381 u8 qos, __be16 vlan_proto)
1382{
Yunsheng Lin9780cb92017-10-09 15:43:56 +08001383 struct hnae3_handle *h = hns3_get_handle(netdev);
Salil76ad4f02017-08-02 16:59:45 +01001384 int ret = -EIO;
1385
1386 if (h->ae_algo->ops->set_vf_vlan_filter)
1387 ret = h->ae_algo->ops->set_vf_vlan_filter(h, vf, vlan,
1388 qos, vlan_proto);
1389
1390 return ret;
1391}
1392
Salila8e8b7f2017-08-21 17:05:24 +01001393static int hns3_nic_change_mtu(struct net_device *netdev, int new_mtu)
1394{
Yunsheng Lin9780cb92017-10-09 15:43:56 +08001395 struct hnae3_handle *h = hns3_get_handle(netdev);
Salila8e8b7f2017-08-21 17:05:24 +01001396 bool if_running = netif_running(netdev);
1397 int ret;
1398
1399 if (!h->ae_algo->ops->set_mtu)
1400 return -EOPNOTSUPP;
1401
1402 /* if this was called with netdev up then bring netdevice down */
1403 if (if_running) {
1404 (void)hns3_nic_net_stop(netdev);
1405 msleep(100);
1406 }
1407
1408 ret = h->ae_algo->ops->set_mtu(h, new_mtu);
1409 if (ret) {
1410 netdev_err(netdev, "failed to change MTU in hardware %d\n",
1411 ret);
1412 return ret;
1413 }
1414
Fuyun Liang5bad95a2018-01-05 18:18:20 +08001415 netdev->mtu = new_mtu;
1416
Salila8e8b7f2017-08-21 17:05:24 +01001417 /* if the netdev was running earlier, bring it up again */
1418 if (if_running && hns3_nic_net_open(netdev))
1419 ret = -EINVAL;
1420
1421 return ret;
1422}
1423
Lipengf8fa222c2017-11-02 20:45:20 +08001424static bool hns3_get_tx_timeo_queue_info(struct net_device *ndev)
1425{
1426 struct hns3_nic_priv *priv = netdev_priv(ndev);
1427 struct hns3_enet_ring *tx_ring = NULL;
1428 int timeout_queue = 0;
1429 int hw_head, hw_tail;
1430 int i;
1431
1432 /* Find the stopped queue the same way the stack does */
1433 for (i = 0; i < ndev->real_num_tx_queues; i++) {
1434 struct netdev_queue *q;
1435 unsigned long trans_start;
1436
1437 q = netdev_get_tx_queue(ndev, i);
1438 trans_start = q->trans_start;
1439 if (netif_xmit_stopped(q) &&
1440 time_after(jiffies,
1441 (trans_start + ndev->watchdog_timeo))) {
1442 timeout_queue = i;
1443 break;
1444 }
1445 }
1446
1447 if (i == ndev->num_tx_queues) {
1448 netdev_info(ndev,
1449 "no netdev TX timeout queue found, timeout count: %llu\n",
1450 priv->tx_timeout_count);
1451 return false;
1452 }
1453
1454 tx_ring = priv->ring_data[timeout_queue].ring;
1455
1456 hw_head = readl_relaxed(tx_ring->tqp->io_base +
1457 HNS3_RING_TX_RING_HEAD_REG);
1458 hw_tail = readl_relaxed(tx_ring->tqp->io_base +
1459 HNS3_RING_TX_RING_TAIL_REG);
1460 netdev_info(ndev,
1461 "tx_timeout count: %llu, queue id: %d, SW_NTU: 0x%x, SW_NTC: 0x%x, HW_HEAD: 0x%x, HW_TAIL: 0x%x, INT: 0x%x\n",
1462 priv->tx_timeout_count,
1463 timeout_queue,
1464 tx_ring->next_to_use,
1465 tx_ring->next_to_clean,
1466 hw_head,
1467 hw_tail,
1468 readl(tx_ring->tqp_vector->mask_addr));
1469
1470 return true;
1471}
1472
1473static void hns3_nic_net_timeout(struct net_device *ndev)
1474{
1475 struct hns3_nic_priv *priv = netdev_priv(ndev);
Lipengf8fa222c2017-11-02 20:45:20 +08001476 struct hnae3_handle *h = priv->ae_handle;
1477
1478 if (!hns3_get_tx_timeo_queue_info(ndev))
1479 return;
1480
1481 priv->tx_timeout_count++;
1482
Salil Mehta6d4c3982018-03-22 14:28:52 +00001483 if (time_before(jiffies, (h->last_reset_time + ndev->watchdog_timeo)))
Lipengf8fa222c2017-11-02 20:45:20 +08001484 return;
1485
Salil Mehta6d4c3982018-03-22 14:28:52 +00001486 /* request the reset */
Lipengf8fa222c2017-11-02 20:45:20 +08001487 if (h->ae_algo->ops->reset_event)
Salil Mehta6d4c3982018-03-22 14:28:52 +00001488 h->ae_algo->ops->reset_event(h);
Lipengf8fa222c2017-11-02 20:45:20 +08001489}
1490
Salil76ad4f02017-08-02 16:59:45 +01001491static const struct net_device_ops hns3_nic_netdev_ops = {
1492 .ndo_open = hns3_nic_net_open,
1493 .ndo_stop = hns3_nic_net_stop,
1494 .ndo_start_xmit = hns3_nic_net_xmit,
Lipengf8fa222c2017-11-02 20:45:20 +08001495 .ndo_tx_timeout = hns3_nic_net_timeout,
Salil76ad4f02017-08-02 16:59:45 +01001496 .ndo_set_mac_address = hns3_nic_net_set_mac_address,
Salila8e8b7f2017-08-21 17:05:24 +01001497 .ndo_change_mtu = hns3_nic_change_mtu,
Salil76ad4f02017-08-02 16:59:45 +01001498 .ndo_set_features = hns3_nic_set_features,
1499 .ndo_get_stats64 = hns3_nic_get_stats64,
1500 .ndo_setup_tc = hns3_nic_setup_tc,
1501 .ndo_set_rx_mode = hns3_nic_set_rx_mode,
Salil76ad4f02017-08-02 16:59:45 +01001502 .ndo_vlan_rx_add_vid = hns3_vlan_rx_add_vid,
1503 .ndo_vlan_rx_kill_vid = hns3_vlan_rx_kill_vid,
1504 .ndo_set_vf_vlan = hns3_ndo_set_vf_vlan,
1505};
1506
Fuyun Liang2312e052018-05-15 19:20:05 +01001507static bool hns3_is_phys_func(struct pci_dev *pdev)
1508{
1509 u32 dev_id = pdev->device;
1510
1511 switch (dev_id) {
1512 case HNAE3_DEV_ID_GE:
1513 case HNAE3_DEV_ID_25GE:
1514 case HNAE3_DEV_ID_25GE_RDMA:
1515 case HNAE3_DEV_ID_25GE_RDMA_MACSEC:
1516 case HNAE3_DEV_ID_50GE_RDMA:
1517 case HNAE3_DEV_ID_50GE_RDMA_MACSEC:
1518 case HNAE3_DEV_ID_100G_RDMA_MACSEC:
1519 return true;
1520 case HNAE3_DEV_ID_100G_VF:
1521 case HNAE3_DEV_ID_100G_RDMA_DCB_PFC_VF:
1522 return false;
1523 default:
1524 dev_warn(&pdev->dev, "un-recognized pci device-id %d",
1525 dev_id);
1526 }
1527
1528 return false;
1529}
1530
Fuyun Liang2312e052018-05-15 19:20:05 +01001531static void hns3_disable_sriov(struct pci_dev *pdev)
1532{
1533 /* If our VFs are assigned we cannot shut down SR-IOV
1534 * without causing issues, so just leave the hardware
1535 * available but disabled
1536 */
1537 if (pci_vfs_assigned(pdev)) {
1538 dev_warn(&pdev->dev,
1539 "disabling driver while VFs are assigned\n");
1540 return;
1541 }
1542
1543 pci_disable_sriov(pdev);
1544}
1545
Salil76ad4f02017-08-02 16:59:45 +01001546/* hns3_probe - Device initialization routine
1547 * @pdev: PCI device information struct
1548 * @ent: entry in hns3_pci_tbl
1549 *
1550 * hns3_probe initializes a PF identified by a pci_dev structure.
1551 * The OS initialization, configuring of the PF private structure,
1552 * and a hardware reset occur.
1553 *
1554 * Returns 0 on success, negative on failure
1555 */
1556static int hns3_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1557{
1558 struct hnae3_ae_dev *ae_dev;
1559 int ret;
1560
1561 ae_dev = devm_kzalloc(&pdev->dev, sizeof(*ae_dev),
1562 GFP_KERNEL);
1563 if (!ae_dev) {
1564 ret = -ENOMEM;
1565 return ret;
1566 }
1567
1568 ae_dev->pdev = pdev;
Yunsheng Line92a0842017-09-20 18:52:50 +08001569 ae_dev->flag = ent->driver_data;
Salil76ad4f02017-08-02 16:59:45 +01001570 ae_dev->dev_type = HNAE3_DEV_KNIC;
1571 pci_set_drvdata(pdev, ae_dev);
1572
Fuyun Liang50fbc232018-05-15 19:20:08 +01001573 hnae3_register_ae_dev(ae_dev);
Fuyun Liang2312e052018-05-15 19:20:05 +01001574
Fuyun Liang2312e052018-05-15 19:20:05 +01001575 return 0;
Salil76ad4f02017-08-02 16:59:45 +01001576}
1577
1578/* hns3_remove - Device removal routine
1579 * @pdev: PCI device information struct
1580 */
1581static void hns3_remove(struct pci_dev *pdev)
1582{
1583 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
1584
Fuyun Liang2312e052018-05-15 19:20:05 +01001585 if (hns3_is_phys_func(pdev) && IS_ENABLED(CONFIG_PCI_IOV))
1586 hns3_disable_sriov(pdev);
1587
Salil76ad4f02017-08-02 16:59:45 +01001588 hnae3_unregister_ae_dev(ae_dev);
Salil76ad4f02017-08-02 16:59:45 +01001589}
1590
Peng Lifa8d82e2018-05-15 19:20:13 +01001591/**
1592 * hns3_pci_sriov_configure
1593 * @pdev: pointer to a pci_dev structure
1594 * @num_vfs: number of VFs to allocate
1595 *
1596 * Enable or change the number of VFs. Called when the user updates the number
1597 * of VFs in sysfs.
1598 **/
Salil Mehta743e1a82018-05-19 16:53:15 +01001599static int hns3_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
Peng Lifa8d82e2018-05-15 19:20:13 +01001600{
1601 int ret;
1602
1603 if (!(hns3_is_phys_func(pdev) && IS_ENABLED(CONFIG_PCI_IOV))) {
1604 dev_warn(&pdev->dev, "Can not config SRIOV\n");
1605 return -EINVAL;
1606 }
1607
1608 if (num_vfs) {
1609 ret = pci_enable_sriov(pdev, num_vfs);
1610 if (ret)
1611 dev_err(&pdev->dev, "SRIOV enable failed %d\n", ret);
Salil Mehta743e1a82018-05-19 16:53:15 +01001612 else
1613 return num_vfs;
Peng Lifa8d82e2018-05-15 19:20:13 +01001614 } else if (!pci_vfs_assigned(pdev)) {
1615 pci_disable_sriov(pdev);
1616 } else {
1617 dev_warn(&pdev->dev,
1618 "Unable to free VFs because some are assigned to VMs.\n");
1619 }
1620
1621 return 0;
1622}
1623
Salil76ad4f02017-08-02 16:59:45 +01001624static struct pci_driver hns3_driver = {
1625 .name = hns3_driver_name,
1626 .id_table = hns3_pci_tbl,
1627 .probe = hns3_probe,
1628 .remove = hns3_remove,
Peng Lifa8d82e2018-05-15 19:20:13 +01001629 .sriov_configure = hns3_pci_sriov_configure,
Salil76ad4f02017-08-02 16:59:45 +01001630};
1631
1632/* set default feature to hns3 */
1633static void hns3_set_default_feature(struct net_device *netdev)
1634{
1635 netdev->priv_flags |= IFF_UNICAST_FLT;
1636
1637 netdev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1638 NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
1639 NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE |
1640 NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL |
1641 NETIF_F_GSO_UDP_TUNNEL_CSUM;
1642
1643 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
1644
1645 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
1646
1647 netdev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1648 NETIF_F_HW_VLAN_CTAG_FILTER |
Peng Li052ece62017-12-22 12:21:47 +08001649 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
Salil76ad4f02017-08-02 16:59:45 +01001650 NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
1651 NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE |
1652 NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL |
1653 NETIF_F_GSO_UDP_TUNNEL_CSUM;
1654
1655 netdev->vlan_features |=
1656 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM |
1657 NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO |
1658 NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE |
1659 NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL |
1660 NETIF_F_GSO_UDP_TUNNEL_CSUM;
1661
1662 netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
Yunsheng Linb2641e22018-05-03 17:28:11 +01001663 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
Salil76ad4f02017-08-02 16:59:45 +01001664 NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
1665 NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE |
1666 NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL |
1667 NETIF_F_GSO_UDP_TUNNEL_CSUM;
1668}
1669
1670static int hns3_alloc_buffer(struct hns3_enet_ring *ring,
1671 struct hns3_desc_cb *cb)
1672{
1673 unsigned int order = hnae_page_order(ring);
1674 struct page *p;
1675
1676 p = dev_alloc_pages(order);
1677 if (!p)
1678 return -ENOMEM;
1679
1680 cb->priv = p;
1681 cb->page_offset = 0;
1682 cb->reuse_flag = 0;
1683 cb->buf = page_address(p);
1684 cb->length = hnae_page_size(ring);
1685 cb->type = DESC_TYPE_PAGE;
1686
Salil76ad4f02017-08-02 16:59:45 +01001687 return 0;
1688}
1689
1690static void hns3_free_buffer(struct hns3_enet_ring *ring,
1691 struct hns3_desc_cb *cb)
1692{
1693 if (cb->type == DESC_TYPE_SKB)
1694 dev_kfree_skb_any((struct sk_buff *)cb->priv);
1695 else if (!HNAE3_IS_TX_RING(ring))
1696 put_page((struct page *)cb->priv);
1697 memset(cb, 0, sizeof(*cb));
1698}
1699
1700static int hns3_map_buffer(struct hns3_enet_ring *ring, struct hns3_desc_cb *cb)
1701{
1702 cb->dma = dma_map_page(ring_to_dev(ring), cb->priv, 0,
1703 cb->length, ring_to_dma_dir(ring));
1704
1705 if (dma_mapping_error(ring_to_dev(ring), cb->dma))
1706 return -EIO;
1707
1708 return 0;
1709}
1710
1711static void hns3_unmap_buffer(struct hns3_enet_ring *ring,
1712 struct hns3_desc_cb *cb)
1713{
1714 if (cb->type == DESC_TYPE_SKB)
1715 dma_unmap_single(ring_to_dev(ring), cb->dma, cb->length,
1716 ring_to_dma_dir(ring));
1717 else
1718 dma_unmap_page(ring_to_dev(ring), cb->dma, cb->length,
1719 ring_to_dma_dir(ring));
1720}
1721
1722static void hns3_buffer_detach(struct hns3_enet_ring *ring, int i)
1723{
1724 hns3_unmap_buffer(ring, &ring->desc_cb[i]);
1725 ring->desc[i].addr = 0;
1726}
1727
1728static void hns3_free_buffer_detach(struct hns3_enet_ring *ring, int i)
1729{
1730 struct hns3_desc_cb *cb = &ring->desc_cb[i];
1731
1732 if (!ring->desc_cb[i].dma)
1733 return;
1734
1735 hns3_buffer_detach(ring, i);
1736 hns3_free_buffer(ring, cb);
1737}
1738
1739static void hns3_free_buffers(struct hns3_enet_ring *ring)
1740{
1741 int i;
1742
1743 for (i = 0; i < ring->desc_num; i++)
1744 hns3_free_buffer_detach(ring, i);
1745}
1746
1747/* free desc along with its attached buffer */
1748static void hns3_free_desc(struct hns3_enet_ring *ring)
1749{
1750 hns3_free_buffers(ring);
1751
1752 dma_unmap_single(ring_to_dev(ring), ring->desc_dma_addr,
1753 ring->desc_num * sizeof(ring->desc[0]),
1754 DMA_BIDIRECTIONAL);
1755 ring->desc_dma_addr = 0;
1756 kfree(ring->desc);
1757 ring->desc = NULL;
1758}
1759
1760static int hns3_alloc_desc(struct hns3_enet_ring *ring)
1761{
1762 int size = ring->desc_num * sizeof(ring->desc[0]);
1763
1764 ring->desc = kzalloc(size, GFP_KERNEL);
1765 if (!ring->desc)
1766 return -ENOMEM;
1767
1768 ring->desc_dma_addr = dma_map_single(ring_to_dev(ring), ring->desc,
1769 size, DMA_BIDIRECTIONAL);
1770 if (dma_mapping_error(ring_to_dev(ring), ring->desc_dma_addr)) {
1771 ring->desc_dma_addr = 0;
1772 kfree(ring->desc);
1773 ring->desc = NULL;
1774 return -ENOMEM;
1775 }
1776
1777 return 0;
1778}
1779
1780static int hns3_reserve_buffer_map(struct hns3_enet_ring *ring,
1781 struct hns3_desc_cb *cb)
1782{
1783 int ret;
1784
1785 ret = hns3_alloc_buffer(ring, cb);
1786 if (ret)
1787 goto out;
1788
1789 ret = hns3_map_buffer(ring, cb);
1790 if (ret)
1791 goto out_with_buf;
1792
1793 return 0;
1794
1795out_with_buf:
Lipeng564883b2017-10-23 19:51:02 +08001796 hns3_free_buffer(ring, cb);
Salil76ad4f02017-08-02 16:59:45 +01001797out:
1798 return ret;
1799}
1800
1801static int hns3_alloc_buffer_attach(struct hns3_enet_ring *ring, int i)
1802{
1803 int ret = hns3_reserve_buffer_map(ring, &ring->desc_cb[i]);
1804
1805 if (ret)
1806 return ret;
1807
1808 ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma);
1809
1810 return 0;
1811}
1812
1813/* Allocate memory for raw pkg, and map with dma */
1814static int hns3_alloc_ring_buffers(struct hns3_enet_ring *ring)
1815{
1816 int i, j, ret;
1817
1818 for (i = 0; i < ring->desc_num; i++) {
1819 ret = hns3_alloc_buffer_attach(ring, i);
1820 if (ret)
1821 goto out_buffer_fail;
1822 }
1823
1824 return 0;
1825
1826out_buffer_fail:
1827 for (j = i - 1; j >= 0; j--)
1828 hns3_free_buffer_detach(ring, j);
1829 return ret;
1830}
1831
1832/* detach a in-used buffer and replace with a reserved one */
1833static void hns3_replace_buffer(struct hns3_enet_ring *ring, int i,
1834 struct hns3_desc_cb *res_cb)
1835{
Lipengb9077422017-10-23 19:51:01 +08001836 hns3_unmap_buffer(ring, &ring->desc_cb[i]);
Salil76ad4f02017-08-02 16:59:45 +01001837 ring->desc_cb[i] = *res_cb;
1838 ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma);
Fuyun Liang7d0b1302018-05-25 19:43:02 +01001839 ring->desc[i].rx.bd_base_info = 0;
Salil76ad4f02017-08-02 16:59:45 +01001840}
1841
1842static void hns3_reuse_buffer(struct hns3_enet_ring *ring, int i)
1843{
1844 ring->desc_cb[i].reuse_flag = 0;
1845 ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma
1846 + ring->desc_cb[i].page_offset);
Fuyun Liang7d0b1302018-05-25 19:43:02 +01001847 ring->desc[i].rx.bd_base_info = 0;
Salil76ad4f02017-08-02 16:59:45 +01001848}
1849
1850static void hns3_nic_reclaim_one_desc(struct hns3_enet_ring *ring, int *bytes,
1851 int *pkts)
1852{
1853 struct hns3_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_clean];
1854
1855 (*pkts) += (desc_cb->type == DESC_TYPE_SKB);
1856 (*bytes) += desc_cb->length;
1857 /* desc_cb will be cleaned, after hnae_free_buffer_detach*/
1858 hns3_free_buffer_detach(ring, ring->next_to_clean);
1859
1860 ring_ptr_move_fw(ring, next_to_clean);
1861}
1862
1863static int is_valid_clean_head(struct hns3_enet_ring *ring, int h)
1864{
1865 int u = ring->next_to_use;
1866 int c = ring->next_to_clean;
1867
1868 if (unlikely(h > ring->desc_num))
1869 return 0;
1870
1871 return u > c ? (h > c && h <= u) : (h > c || h <= u);
1872}
1873
Lipeng24e750c2017-10-23 19:51:07 +08001874bool hns3_clean_tx_ring(struct hns3_enet_ring *ring, int budget)
Salil76ad4f02017-08-02 16:59:45 +01001875{
1876 struct net_device *netdev = ring->tqp->handle->kinfo.netdev;
1877 struct netdev_queue *dev_queue;
1878 int bytes, pkts;
1879 int head;
1880
1881 head = readl_relaxed(ring->tqp->io_base + HNS3_RING_TX_RING_HEAD_REG);
1882 rmb(); /* Make sure head is ready before touch any data */
1883
1884 if (is_ring_empty(ring) || head == ring->next_to_clean)
Lipeng24e750c2017-10-23 19:51:07 +08001885 return true; /* no data to poll */
Salil76ad4f02017-08-02 16:59:45 +01001886
1887 if (!is_valid_clean_head(ring, head)) {
1888 netdev_err(netdev, "wrong head (%d, %d-%d)\n", head,
1889 ring->next_to_use, ring->next_to_clean);
1890
1891 u64_stats_update_begin(&ring->syncp);
1892 ring->stats.io_err_cnt++;
1893 u64_stats_update_end(&ring->syncp);
Lipeng24e750c2017-10-23 19:51:07 +08001894 return true;
Salil76ad4f02017-08-02 16:59:45 +01001895 }
1896
1897 bytes = 0;
1898 pkts = 0;
1899 while (head != ring->next_to_clean && budget) {
1900 hns3_nic_reclaim_one_desc(ring, &bytes, &pkts);
1901 /* Issue prefetch for next Tx descriptor */
1902 prefetch(&ring->desc_cb[ring->next_to_clean]);
1903 budget--;
1904 }
1905
1906 ring->tqp_vector->tx_group.total_bytes += bytes;
1907 ring->tqp_vector->tx_group.total_packets += pkts;
1908
1909 u64_stats_update_begin(&ring->syncp);
1910 ring->stats.tx_bytes += bytes;
1911 ring->stats.tx_pkts += pkts;
1912 u64_stats_update_end(&ring->syncp);
1913
1914 dev_queue = netdev_get_tx_queue(netdev, ring->tqp->tqp_index);
1915 netdev_tx_completed_queue(dev_queue, pkts, bytes);
1916
1917 if (unlikely(pkts && netif_carrier_ok(netdev) &&
1918 (ring_space(ring) > HNS3_MAX_BD_PER_PKT))) {
1919 /* Make sure that anybody stopping the queue after this
1920 * sees the new next_to_clean.
1921 */
1922 smp_mb();
1923 if (netif_tx_queue_stopped(dev_queue)) {
1924 netif_tx_wake_queue(dev_queue);
1925 ring->stats.restart_queue++;
1926 }
1927 }
1928
1929 return !!budget;
1930}
1931
1932static int hns3_desc_unused(struct hns3_enet_ring *ring)
1933{
1934 int ntc = ring->next_to_clean;
1935 int ntu = ring->next_to_use;
1936
1937 return ((ntc >= ntu) ? 0 : ring->desc_num) + ntc - ntu;
1938}
1939
1940static void
1941hns3_nic_alloc_rx_buffers(struct hns3_enet_ring *ring, int cleand_count)
1942{
1943 struct hns3_desc_cb *desc_cb;
1944 struct hns3_desc_cb res_cbs;
1945 int i, ret;
1946
1947 for (i = 0; i < cleand_count; i++) {
1948 desc_cb = &ring->desc_cb[ring->next_to_use];
1949 if (desc_cb->reuse_flag) {
1950 u64_stats_update_begin(&ring->syncp);
1951 ring->stats.reuse_pg_cnt++;
1952 u64_stats_update_end(&ring->syncp);
1953
1954 hns3_reuse_buffer(ring, ring->next_to_use);
1955 } else {
1956 ret = hns3_reserve_buffer_map(ring, &res_cbs);
1957 if (ret) {
1958 u64_stats_update_begin(&ring->syncp);
1959 ring->stats.sw_err_cnt++;
1960 u64_stats_update_end(&ring->syncp);
1961
1962 netdev_err(ring->tqp->handle->kinfo.netdev,
1963 "hnae reserve buffer map failed.\n");
1964 break;
1965 }
1966 hns3_replace_buffer(ring, ring->next_to_use, &res_cbs);
1967 }
1968
1969 ring_ptr_move_fw(ring, next_to_use);
1970 }
1971
1972 wmb(); /* Make all data has been write before submit */
1973 writel_relaxed(i, ring->tqp->io_base + HNS3_RING_RX_RING_HEAD_REG);
1974}
1975
Salil76ad4f02017-08-02 16:59:45 +01001976static void hns3_nic_reuse_page(struct sk_buff *skb, int i,
1977 struct hns3_enet_ring *ring, int pull_len,
1978 struct hns3_desc_cb *desc_cb)
1979{
1980 struct hns3_desc *desc;
1981 int truesize, size;
1982 int last_offset;
1983 bool twobufs;
1984
1985 twobufs = ((PAGE_SIZE < 8192) &&
1986 hnae_buf_size(ring) == HNS3_BUFFER_SIZE_2048);
1987
1988 desc = &ring->desc[ring->next_to_clean];
1989 size = le16_to_cpu(desc->rx.size);
1990
Peng Lif8d291f2018-03-10 11:29:26 +08001991 truesize = hnae_buf_size(ring);
1992
1993 if (!twobufs)
Salil76ad4f02017-08-02 16:59:45 +01001994 last_offset = hnae_page_size(ring) - hnae_buf_size(ring);
Salil76ad4f02017-08-02 16:59:45 +01001995
1996 skb_add_rx_frag(skb, i, desc_cb->priv, desc_cb->page_offset + pull_len,
Peng Lif8d291f2018-03-10 11:29:26 +08001997 size - pull_len, truesize);
Salil76ad4f02017-08-02 16:59:45 +01001998
1999 /* Avoid re-using remote pages,flag default unreuse */
2000 if (unlikely(page_to_nid(desc_cb->priv) != numa_node_id()))
2001 return;
2002
2003 if (twobufs) {
2004 /* If we are only owner of page we can reuse it */
2005 if (likely(page_count(desc_cb->priv) == 1)) {
2006 /* Flip page offset to other buffer */
2007 desc_cb->page_offset ^= truesize;
2008
2009 desc_cb->reuse_flag = 1;
2010 /* bump ref count on page before it is given*/
2011 get_page(desc_cb->priv);
2012 }
2013 return;
2014 }
2015
2016 /* Move offset up to the next cache line */
2017 desc_cb->page_offset += truesize;
2018
2019 if (desc_cb->page_offset <= last_offset) {
2020 desc_cb->reuse_flag = 1;
2021 /* Bump ref count on page before it is given*/
2022 get_page(desc_cb->priv);
2023 }
2024}
2025
2026static void hns3_rx_checksum(struct hns3_enet_ring *ring, struct sk_buff *skb,
2027 struct hns3_desc *desc)
2028{
2029 struct net_device *netdev = ring->tqp->handle->kinfo.netdev;
2030 int l3_type, l4_type;
2031 u32 bd_base_info;
2032 int ol4_type;
2033 u32 l234info;
2034
2035 bd_base_info = le32_to_cpu(desc->rx.bd_base_info);
2036 l234info = le32_to_cpu(desc->rx.l234_info);
2037
2038 skb->ip_summed = CHECKSUM_NONE;
2039
2040 skb_checksum_none_assert(skb);
2041
2042 if (!(netdev->features & NETIF_F_RXCSUM))
2043 return;
2044
2045 /* check if hardware has done checksum */
2046 if (!hnae_get_bit(bd_base_info, HNS3_RXD_L3L4P_B))
2047 return;
2048
2049 if (unlikely(hnae_get_bit(l234info, HNS3_RXD_L3E_B) ||
2050 hnae_get_bit(l234info, HNS3_RXD_L4E_B) ||
2051 hnae_get_bit(l234info, HNS3_RXD_OL3E_B) ||
2052 hnae_get_bit(l234info, HNS3_RXD_OL4E_B))) {
2053 netdev_err(netdev, "L3/L4 error pkt\n");
2054 u64_stats_update_begin(&ring->syncp);
2055 ring->stats.l3l4_csum_err++;
2056 u64_stats_update_end(&ring->syncp);
2057
2058 return;
2059 }
2060
2061 l3_type = hnae_get_field(l234info, HNS3_RXD_L3ID_M,
2062 HNS3_RXD_L3ID_S);
2063 l4_type = hnae_get_field(l234info, HNS3_RXD_L4ID_M,
2064 HNS3_RXD_L4ID_S);
2065
2066 ol4_type = hnae_get_field(l234info, HNS3_RXD_OL4ID_M, HNS3_RXD_OL4ID_S);
2067 switch (ol4_type) {
2068 case HNS3_OL4_TYPE_MAC_IN_UDP:
2069 case HNS3_OL4_TYPE_NVGRE:
2070 skb->csum_level = 1;
2071 case HNS3_OL4_TYPE_NO_TUN:
2072 /* Can checksum ipv4 or ipv6 + UDP/TCP/SCTP packets */
2073 if (l3_type == HNS3_L3_TYPE_IPV4 ||
2074 (l3_type == HNS3_L3_TYPE_IPV6 &&
2075 (l4_type == HNS3_L4_TYPE_UDP ||
2076 l4_type == HNS3_L4_TYPE_TCP ||
2077 l4_type == HNS3_L4_TYPE_SCTP)))
2078 skb->ip_summed = CHECKSUM_UNNECESSARY;
2079 break;
2080 }
2081}
2082
Yunsheng Lind43e5ac2017-10-20 10:19:21 +08002083static void hns3_rx_skb(struct hns3_enet_ring *ring, struct sk_buff *skb)
2084{
2085 napi_gro_receive(&ring->tqp_vector->napi, skb);
2086}
2087
Peng Li5b5455a2018-05-25 19:42:58 +01002088static u16 hns3_parse_vlan_tag(struct hns3_enet_ring *ring,
2089 struct hns3_desc *desc, u32 l234info)
2090{
2091 struct pci_dev *pdev = ring->tqp->handle->pdev;
2092 u16 vlan_tag;
2093
2094 if (pdev->revision == 0x20) {
2095 vlan_tag = le16_to_cpu(desc->rx.ot_vlan_tag);
2096 if (!(vlan_tag & VLAN_VID_MASK))
2097 vlan_tag = le16_to_cpu(desc->rx.vlan_tag);
2098
2099 return vlan_tag;
2100 }
2101
2102#define HNS3_STRP_OUTER_VLAN 0x1
2103#define HNS3_STRP_INNER_VLAN 0x2
2104
2105 switch (hnae_get_field(l234info, HNS3_RXD_STRP_TAGP_M,
2106 HNS3_RXD_STRP_TAGP_S)) {
2107 case HNS3_STRP_OUTER_VLAN:
2108 vlan_tag = le16_to_cpu(desc->rx.ot_vlan_tag);
2109 break;
2110 case HNS3_STRP_INNER_VLAN:
2111 vlan_tag = le16_to_cpu(desc->rx.vlan_tag);
2112 break;
2113 default:
2114 vlan_tag = 0;
2115 break;
2116 }
2117
2118 return vlan_tag;
2119}
2120
Salil76ad4f02017-08-02 16:59:45 +01002121static int hns3_handle_rx_bd(struct hns3_enet_ring *ring,
2122 struct sk_buff **out_skb, int *out_bnum)
2123{
2124 struct net_device *netdev = ring->tqp->handle->kinfo.netdev;
2125 struct hns3_desc_cb *desc_cb;
2126 struct hns3_desc *desc;
2127 struct sk_buff *skb;
2128 unsigned char *va;
2129 u32 bd_base_info;
2130 int pull_len;
2131 u32 l234info;
2132 int length;
2133 int bnum;
2134
2135 desc = &ring->desc[ring->next_to_clean];
2136 desc_cb = &ring->desc_cb[ring->next_to_clean];
2137
2138 prefetch(desc);
2139
Peng Li846fcc82018-05-25 19:42:56 +01002140 length = le16_to_cpu(desc->rx.size);
Salil76ad4f02017-08-02 16:59:45 +01002141 bd_base_info = le32_to_cpu(desc->rx.bd_base_info);
Salil76ad4f02017-08-02 16:59:45 +01002142
2143 /* Check valid BD */
2144 if (!hnae_get_bit(bd_base_info, HNS3_RXD_VLD_B))
2145 return -EFAULT;
2146
2147 va = (unsigned char *)desc_cb->buf + desc_cb->page_offset;
2148
2149 /* Prefetch first cache line of first page
2150 * Idea is to cache few bytes of the header of the packet. Our L1 Cache
2151 * line size is 64B so need to prefetch twice to make it 128B. But in
2152 * actual we can have greater size of caches with 128B Level 1 cache
2153 * lines. In such a case, single fetch would suffice to cache in the
2154 * relevant part of the header.
2155 */
2156 prefetch(va);
2157#if L1_CACHE_BYTES < 128
2158 prefetch(va + L1_CACHE_BYTES);
2159#endif
2160
2161 skb = *out_skb = napi_alloc_skb(&ring->tqp_vector->napi,
2162 HNS3_RX_HEAD_SIZE);
2163 if (unlikely(!skb)) {
2164 netdev_err(netdev, "alloc rx skb fail\n");
2165
2166 u64_stats_update_begin(&ring->syncp);
2167 ring->stats.sw_err_cnt++;
2168 u64_stats_update_end(&ring->syncp);
2169
2170 return -ENOMEM;
2171 }
2172
2173 prefetchw(skb->data);
2174
2175 bnum = 1;
2176 if (length <= HNS3_RX_HEAD_SIZE) {
2177 memcpy(__skb_put(skb, length), va, ALIGN(length, sizeof(long)));
2178
2179 /* We can reuse buffer as-is, just make sure it is local */
2180 if (likely(page_to_nid(desc_cb->priv) == numa_node_id()))
2181 desc_cb->reuse_flag = 1;
2182 else /* This page cannot be reused so discard it */
2183 put_page(desc_cb->priv);
2184
2185 ring_ptr_move_fw(ring, next_to_clean);
2186 } else {
2187 u64_stats_update_begin(&ring->syncp);
2188 ring->stats.seg_pkt_cnt++;
2189 u64_stats_update_end(&ring->syncp);
2190
Peng Lie63cd652018-05-19 16:53:16 +01002191 pull_len = eth_get_headlen(va, HNS3_RX_HEAD_SIZE);
2192
Salil76ad4f02017-08-02 16:59:45 +01002193 memcpy(__skb_put(skb, pull_len), va,
2194 ALIGN(pull_len, sizeof(long)));
2195
2196 hns3_nic_reuse_page(skb, 0, ring, pull_len, desc_cb);
2197 ring_ptr_move_fw(ring, next_to_clean);
2198
2199 while (!hnae_get_bit(bd_base_info, HNS3_RXD_FE_B)) {
2200 desc = &ring->desc[ring->next_to_clean];
2201 desc_cb = &ring->desc_cb[ring->next_to_clean];
2202 bd_base_info = le32_to_cpu(desc->rx.bd_base_info);
2203 hns3_nic_reuse_page(skb, bnum, ring, 0, desc_cb);
2204 ring_ptr_move_fw(ring, next_to_clean);
2205 bnum++;
2206 }
2207 }
2208
2209 *out_bnum = bnum;
Peng Li5b5455a2018-05-25 19:42:58 +01002210
2211 l234info = le32_to_cpu(desc->rx.l234_info);
2212
Peng Li846fcc82018-05-25 19:42:56 +01002213 /* Based on hw strategy, the tag offloaded will be stored at
2214 * ot_vlan_tag in two layer tag case, and stored at vlan_tag
2215 * in one layer tag case.
2216 */
2217 if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX) {
2218 u16 vlan_tag;
2219
Peng Li5b5455a2018-05-25 19:42:58 +01002220 vlan_tag = hns3_parse_vlan_tag(ring, desc, l234info);
Peng Li846fcc82018-05-25 19:42:56 +01002221 if (vlan_tag & VLAN_VID_MASK)
2222 __vlan_hwaccel_put_tag(skb,
2223 htons(ETH_P_8021Q),
2224 vlan_tag);
2225 }
2226
Salil76ad4f02017-08-02 16:59:45 +01002227 if (unlikely(!hnae_get_bit(bd_base_info, HNS3_RXD_VLD_B))) {
2228 netdev_err(netdev, "no valid bd,%016llx,%016llx\n",
2229 ((u64 *)desc)[0], ((u64 *)desc)[1]);
2230 u64_stats_update_begin(&ring->syncp);
2231 ring->stats.non_vld_descs++;
2232 u64_stats_update_end(&ring->syncp);
2233
2234 dev_kfree_skb_any(skb);
2235 return -EINVAL;
2236 }
2237
2238 if (unlikely((!desc->rx.pkt_len) ||
2239 hnae_get_bit(l234info, HNS3_RXD_TRUNCAT_B))) {
2240 netdev_err(netdev, "truncated pkt\n");
2241 u64_stats_update_begin(&ring->syncp);
2242 ring->stats.err_pkt_len++;
2243 u64_stats_update_end(&ring->syncp);
2244
2245 dev_kfree_skb_any(skb);
2246 return -EFAULT;
2247 }
2248
2249 if (unlikely(hnae_get_bit(l234info, HNS3_RXD_L2E_B))) {
2250 netdev_err(netdev, "L2 error pkt\n");
2251 u64_stats_update_begin(&ring->syncp);
2252 ring->stats.l2_err++;
2253 u64_stats_update_end(&ring->syncp);
2254
2255 dev_kfree_skb_any(skb);
2256 return -EFAULT;
2257 }
2258
2259 u64_stats_update_begin(&ring->syncp);
2260 ring->stats.rx_pkts++;
2261 ring->stats.rx_bytes += skb->len;
2262 u64_stats_update_end(&ring->syncp);
2263
2264 ring->tqp_vector->rx_group.total_bytes += skb->len;
2265
2266 hns3_rx_checksum(ring, skb, desc);
2267 return 0;
2268}
2269
Yunsheng Lind43e5ac2017-10-20 10:19:21 +08002270int hns3_clean_rx_ring(
2271 struct hns3_enet_ring *ring, int budget,
2272 void (*rx_fn)(struct hns3_enet_ring *, struct sk_buff *))
Salil76ad4f02017-08-02 16:59:45 +01002273{
2274#define RCB_NOF_ALLOC_RX_BUFF_ONCE 16
2275 struct net_device *netdev = ring->tqp->handle->kinfo.netdev;
2276 int recv_pkts, recv_bds, clean_count, err;
2277 int unused_count = hns3_desc_unused(ring);
2278 struct sk_buff *skb = NULL;
2279 int num, bnum = 0;
2280
2281 num = readl_relaxed(ring->tqp->io_base + HNS3_RING_RX_RING_FBDNUM_REG);
2282 rmb(); /* Make sure num taken effect before the other data is touched */
2283
2284 recv_pkts = 0, recv_bds = 0, clean_count = 0;
2285 num -= unused_count;
2286
2287 while (recv_pkts < budget && recv_bds < num) {
2288 /* Reuse or realloc buffers */
2289 if (clean_count + unused_count >= RCB_NOF_ALLOC_RX_BUFF_ONCE) {
2290 hns3_nic_alloc_rx_buffers(ring,
2291 clean_count + unused_count);
2292 clean_count = 0;
2293 unused_count = hns3_desc_unused(ring);
2294 }
2295
2296 /* Poll one pkt */
2297 err = hns3_handle_rx_bd(ring, &skb, &bnum);
2298 if (unlikely(!skb)) /* This fault cannot be repaired */
2299 goto out;
2300
2301 recv_bds += bnum;
2302 clean_count += bnum;
2303 if (unlikely(err)) { /* Do jump the err */
2304 recv_pkts++;
2305 continue;
2306 }
2307
2308 /* Do update ip stack process */
2309 skb->protocol = eth_type_trans(skb, netdev);
Yunsheng Lind43e5ac2017-10-20 10:19:21 +08002310 rx_fn(ring, skb);
Salil76ad4f02017-08-02 16:59:45 +01002311
2312 recv_pkts++;
2313 }
2314
2315out:
2316 /* Make all data has been write before submit */
2317 if (clean_count + unused_count > 0)
2318 hns3_nic_alloc_rx_buffers(ring,
2319 clean_count + unused_count);
2320
2321 return recv_pkts;
2322}
2323
2324static bool hns3_get_new_int_gl(struct hns3_enet_ring_group *ring_group)
2325{
Fuyun Lianga95e1f82018-03-21 15:49:26 +08002326 struct hns3_enet_tqp_vector *tqp_vector =
2327 ring_group->ring->tqp_vector;
Salil76ad4f02017-08-02 16:59:45 +01002328 enum hns3_flow_level_range new_flow_level;
Fuyun Lianga95e1f82018-03-21 15:49:26 +08002329 int packets_per_msecs;
2330 int bytes_per_msecs;
2331 u32 time_passed_ms;
Salil76ad4f02017-08-02 16:59:45 +01002332 u16 new_int_gl;
Salil76ad4f02017-08-02 16:59:45 +01002333
Fuyun Lianga95e1f82018-03-21 15:49:26 +08002334 if (!ring_group->coal.int_gl || !tqp_vector->last_jiffies)
Salil76ad4f02017-08-02 16:59:45 +01002335 return false;
2336
2337 if (ring_group->total_packets == 0) {
Yunsheng Lin9bc727a2018-03-09 10:37:03 +08002338 ring_group->coal.int_gl = HNS3_INT_GL_50K;
2339 ring_group->coal.flow_level = HNS3_FLOW_LOW;
Salil76ad4f02017-08-02 16:59:45 +01002340 return true;
2341 }
2342
2343 /* Simple throttlerate management
2344 * 0-10MB/s lower (50000 ints/s)
2345 * 10-20MB/s middle (20000 ints/s)
2346 * 20-1249MB/s high (18000 ints/s)
2347 * > 40000pps ultra (8000 ints/s)
2348 */
Yunsheng Lin9bc727a2018-03-09 10:37:03 +08002349 new_flow_level = ring_group->coal.flow_level;
2350 new_int_gl = ring_group->coal.int_gl;
Fuyun Lianga95e1f82018-03-21 15:49:26 +08002351 time_passed_ms =
2352 jiffies_to_msecs(jiffies - tqp_vector->last_jiffies);
2353
2354 if (!time_passed_ms)
2355 return false;
2356
2357 do_div(ring_group->total_packets, time_passed_ms);
2358 packets_per_msecs = ring_group->total_packets;
2359
2360 do_div(ring_group->total_bytes, time_passed_ms);
2361 bytes_per_msecs = ring_group->total_bytes;
2362
2363#define HNS3_RX_LOW_BYTE_RATE 10000
2364#define HNS3_RX_MID_BYTE_RATE 20000
Salil76ad4f02017-08-02 16:59:45 +01002365
2366 switch (new_flow_level) {
2367 case HNS3_FLOW_LOW:
Fuyun Lianga95e1f82018-03-21 15:49:26 +08002368 if (bytes_per_msecs > HNS3_RX_LOW_BYTE_RATE)
Salil76ad4f02017-08-02 16:59:45 +01002369 new_flow_level = HNS3_FLOW_MID;
2370 break;
2371 case HNS3_FLOW_MID:
Fuyun Lianga95e1f82018-03-21 15:49:26 +08002372 if (bytes_per_msecs > HNS3_RX_MID_BYTE_RATE)
Salil76ad4f02017-08-02 16:59:45 +01002373 new_flow_level = HNS3_FLOW_HIGH;
Fuyun Lianga95e1f82018-03-21 15:49:26 +08002374 else if (bytes_per_msecs <= HNS3_RX_LOW_BYTE_RATE)
Salil76ad4f02017-08-02 16:59:45 +01002375 new_flow_level = HNS3_FLOW_LOW;
2376 break;
2377 case HNS3_FLOW_HIGH:
2378 case HNS3_FLOW_ULTRA:
2379 default:
Fuyun Lianga95e1f82018-03-21 15:49:26 +08002380 if (bytes_per_msecs <= HNS3_RX_MID_BYTE_RATE)
Salil76ad4f02017-08-02 16:59:45 +01002381 new_flow_level = HNS3_FLOW_MID;
2382 break;
2383 }
2384
Fuyun Lianga95e1f82018-03-21 15:49:26 +08002385#define HNS3_RX_ULTRA_PACKET_RATE 40
2386
2387 if (packets_per_msecs > HNS3_RX_ULTRA_PACKET_RATE &&
2388 &tqp_vector->rx_group == ring_group)
Salil76ad4f02017-08-02 16:59:45 +01002389 new_flow_level = HNS3_FLOW_ULTRA;
2390
2391 switch (new_flow_level) {
2392 case HNS3_FLOW_LOW:
2393 new_int_gl = HNS3_INT_GL_50K;
2394 break;
2395 case HNS3_FLOW_MID:
2396 new_int_gl = HNS3_INT_GL_20K;
2397 break;
2398 case HNS3_FLOW_HIGH:
2399 new_int_gl = HNS3_INT_GL_18K;
2400 break;
2401 case HNS3_FLOW_ULTRA:
2402 new_int_gl = HNS3_INT_GL_8K;
2403 break;
2404 default:
2405 break;
2406 }
2407
2408 ring_group->total_bytes = 0;
2409 ring_group->total_packets = 0;
Yunsheng Lin9bc727a2018-03-09 10:37:03 +08002410 ring_group->coal.flow_level = new_flow_level;
2411 if (new_int_gl != ring_group->coal.int_gl) {
2412 ring_group->coal.int_gl = new_int_gl;
Salil76ad4f02017-08-02 16:59:45 +01002413 return true;
2414 }
2415 return false;
2416}
2417
2418static void hns3_update_new_int_gl(struct hns3_enet_tqp_vector *tqp_vector)
2419{
Fuyun Liang8b1ff1e2018-01-12 16:23:12 +08002420 struct hns3_enet_ring_group *rx_group = &tqp_vector->rx_group;
2421 struct hns3_enet_ring_group *tx_group = &tqp_vector->tx_group;
2422 bool rx_update, tx_update;
Salil76ad4f02017-08-02 16:59:45 +01002423
Fuyun Liangcd9d1872018-03-21 15:49:25 +08002424 if (tqp_vector->int_adapt_down > 0) {
2425 tqp_vector->int_adapt_down--;
2426 return;
2427 }
2428
Yunsheng Lin9bc727a2018-03-09 10:37:03 +08002429 if (rx_group->coal.gl_adapt_enable) {
Fuyun Liang8b1ff1e2018-01-12 16:23:12 +08002430 rx_update = hns3_get_new_int_gl(rx_group);
2431 if (rx_update)
2432 hns3_set_vector_coalesce_rx_gl(tqp_vector,
Yunsheng Lin9bc727a2018-03-09 10:37:03 +08002433 rx_group->coal.int_gl);
Fuyun Liang8b1ff1e2018-01-12 16:23:12 +08002434 }
2435
Yunsheng Lin9bc727a2018-03-09 10:37:03 +08002436 if (tx_group->coal.gl_adapt_enable) {
Fuyun Liang8b1ff1e2018-01-12 16:23:12 +08002437 tx_update = hns3_get_new_int_gl(&tqp_vector->tx_group);
2438 if (tx_update)
2439 hns3_set_vector_coalesce_tx_gl(tqp_vector,
Yunsheng Lin9bc727a2018-03-09 10:37:03 +08002440 tx_group->coal.int_gl);
Salil76ad4f02017-08-02 16:59:45 +01002441 }
Fuyun Liangcd9d1872018-03-21 15:49:25 +08002442
Fuyun Lianga95e1f82018-03-21 15:49:26 +08002443 tqp_vector->last_jiffies = jiffies;
Fuyun Liangcd9d1872018-03-21 15:49:25 +08002444 tqp_vector->int_adapt_down = HNS3_INT_ADAPT_DOWN_START;
Salil76ad4f02017-08-02 16:59:45 +01002445}
2446
2447static int hns3_nic_common_poll(struct napi_struct *napi, int budget)
2448{
2449 struct hns3_enet_ring *ring;
2450 int rx_pkt_total = 0;
2451
2452 struct hns3_enet_tqp_vector *tqp_vector =
2453 container_of(napi, struct hns3_enet_tqp_vector, napi);
2454 bool clean_complete = true;
2455 int rx_budget;
2456
2457 /* Since the actual Tx work is minimal, we can give the Tx a larger
2458 * budget and be more aggressive about cleaning up the Tx descriptors.
2459 */
2460 hns3_for_each_ring(ring, tqp_vector->tx_group) {
2461 if (!hns3_clean_tx_ring(ring, budget))
2462 clean_complete = false;
2463 }
2464
2465 /* make sure rx ring budget not smaller than 1 */
2466 rx_budget = max(budget / tqp_vector->num_tqps, 1);
2467
2468 hns3_for_each_ring(ring, tqp_vector->rx_group) {
Yunsheng Lind43e5ac2017-10-20 10:19:21 +08002469 int rx_cleaned = hns3_clean_rx_ring(ring, rx_budget,
2470 hns3_rx_skb);
Salil76ad4f02017-08-02 16:59:45 +01002471
2472 if (rx_cleaned >= rx_budget)
2473 clean_complete = false;
2474
2475 rx_pkt_total += rx_cleaned;
2476 }
2477
2478 tqp_vector->rx_group.total_packets += rx_pkt_total;
2479
2480 if (!clean_complete)
2481 return budget;
2482
2483 napi_complete(napi);
2484 hns3_update_new_int_gl(tqp_vector);
2485 hns3_mask_vector_irq(tqp_vector, 1);
2486
2487 return rx_pkt_total;
2488}
2489
2490static int hns3_get_vector_ring_chain(struct hns3_enet_tqp_vector *tqp_vector,
2491 struct hnae3_ring_chain_node *head)
2492{
2493 struct pci_dev *pdev = tqp_vector->handle->pdev;
2494 struct hnae3_ring_chain_node *cur_chain = head;
2495 struct hnae3_ring_chain_node *chain;
2496 struct hns3_enet_ring *tx_ring;
2497 struct hns3_enet_ring *rx_ring;
2498
2499 tx_ring = tqp_vector->tx_group.ring;
2500 if (tx_ring) {
2501 cur_chain->tqp_index = tx_ring->tqp->tqp_index;
2502 hnae_set_bit(cur_chain->flag, HNAE3_RING_TYPE_B,
2503 HNAE3_RING_TYPE_TX);
Fuyun Liang11af96a2018-01-12 16:23:15 +08002504 hnae_set_field(cur_chain->int_gl_idx, HNAE3_RING_GL_IDX_M,
2505 HNAE3_RING_GL_IDX_S, HNAE3_RING_GL_TX);
Salil76ad4f02017-08-02 16:59:45 +01002506
2507 cur_chain->next = NULL;
2508
2509 while (tx_ring->next) {
2510 tx_ring = tx_ring->next;
2511
2512 chain = devm_kzalloc(&pdev->dev, sizeof(*chain),
2513 GFP_KERNEL);
2514 if (!chain)
2515 return -ENOMEM;
2516
2517 cur_chain->next = chain;
2518 chain->tqp_index = tx_ring->tqp->tqp_index;
2519 hnae_set_bit(chain->flag, HNAE3_RING_TYPE_B,
2520 HNAE3_RING_TYPE_TX);
Fuyun Liang11af96a2018-01-12 16:23:15 +08002521 hnae_set_field(chain->int_gl_idx,
2522 HNAE3_RING_GL_IDX_M,
2523 HNAE3_RING_GL_IDX_S,
2524 HNAE3_RING_GL_TX);
Salil76ad4f02017-08-02 16:59:45 +01002525
2526 cur_chain = chain;
2527 }
2528 }
2529
2530 rx_ring = tqp_vector->rx_group.ring;
2531 if (!tx_ring && rx_ring) {
2532 cur_chain->next = NULL;
2533 cur_chain->tqp_index = rx_ring->tqp->tqp_index;
2534 hnae_set_bit(cur_chain->flag, HNAE3_RING_TYPE_B,
2535 HNAE3_RING_TYPE_RX);
Fuyun Liang11af96a2018-01-12 16:23:15 +08002536 hnae_set_field(cur_chain->int_gl_idx, HNAE3_RING_GL_IDX_M,
2537 HNAE3_RING_GL_IDX_S, HNAE3_RING_GL_RX);
Salil76ad4f02017-08-02 16:59:45 +01002538
2539 rx_ring = rx_ring->next;
2540 }
2541
2542 while (rx_ring) {
2543 chain = devm_kzalloc(&pdev->dev, sizeof(*chain), GFP_KERNEL);
2544 if (!chain)
2545 return -ENOMEM;
2546
2547 cur_chain->next = chain;
2548 chain->tqp_index = rx_ring->tqp->tqp_index;
2549 hnae_set_bit(chain->flag, HNAE3_RING_TYPE_B,
2550 HNAE3_RING_TYPE_RX);
Fuyun Liang11af96a2018-01-12 16:23:15 +08002551 hnae_set_field(chain->int_gl_idx, HNAE3_RING_GL_IDX_M,
2552 HNAE3_RING_GL_IDX_S, HNAE3_RING_GL_RX);
2553
Salil76ad4f02017-08-02 16:59:45 +01002554 cur_chain = chain;
2555
2556 rx_ring = rx_ring->next;
2557 }
2558
2559 return 0;
2560}
2561
2562static void hns3_free_vector_ring_chain(struct hns3_enet_tqp_vector *tqp_vector,
2563 struct hnae3_ring_chain_node *head)
2564{
2565 struct pci_dev *pdev = tqp_vector->handle->pdev;
2566 struct hnae3_ring_chain_node *chain_tmp, *chain;
2567
2568 chain = head->next;
2569
2570 while (chain) {
2571 chain_tmp = chain->next;
2572 devm_kfree(&pdev->dev, chain);
2573 chain = chain_tmp;
2574 }
2575}
2576
2577static void hns3_add_ring_to_group(struct hns3_enet_ring_group *group,
2578 struct hns3_enet_ring *ring)
2579{
2580 ring->next = group->ring;
2581 group->ring = ring;
2582
2583 group->count++;
2584}
2585
2586static int hns3_nic_init_vector_data(struct hns3_nic_priv *priv)
2587{
2588 struct hnae3_ring_chain_node vector_ring_chain;
2589 struct hnae3_handle *h = priv->ae_handle;
2590 struct hns3_enet_tqp_vector *tqp_vector;
Yunsheng Lindd38c722018-03-09 10:37:02 +08002591 int ret = 0;
2592 u16 i;
2593
2594 for (i = 0; i < priv->vector_num; i++) {
2595 tqp_vector = &priv->tqp_vector[i];
2596 hns3_vector_gl_rl_init_hw(tqp_vector, priv);
2597 tqp_vector->num_tqps = 0;
2598 }
2599
2600 for (i = 0; i < h->kinfo.num_tqps; i++) {
2601 u16 vector_i = i % priv->vector_num;
2602 u16 tqp_num = h->kinfo.num_tqps;
2603
2604 tqp_vector = &priv->tqp_vector[vector_i];
2605
2606 hns3_add_ring_to_group(&tqp_vector->tx_group,
2607 priv->ring_data[i].ring);
2608
2609 hns3_add_ring_to_group(&tqp_vector->rx_group,
2610 priv->ring_data[i + tqp_num].ring);
2611
2612 priv->ring_data[i].ring->tqp_vector = tqp_vector;
2613 priv->ring_data[i + tqp_num].ring->tqp_vector = tqp_vector;
2614 tqp_vector->num_tqps++;
2615 }
2616
2617 for (i = 0; i < priv->vector_num; i++) {
2618 tqp_vector = &priv->tqp_vector[i];
2619
2620 tqp_vector->rx_group.total_bytes = 0;
2621 tqp_vector->rx_group.total_packets = 0;
2622 tqp_vector->tx_group.total_bytes = 0;
2623 tqp_vector->tx_group.total_packets = 0;
2624 tqp_vector->handle = h;
2625
2626 ret = hns3_get_vector_ring_chain(tqp_vector,
2627 &vector_ring_chain);
2628 if (ret)
2629 return ret;
2630
2631 ret = h->ae_algo->ops->map_ring_to_vector(h,
2632 tqp_vector->vector_irq, &vector_ring_chain);
2633
2634 hns3_free_vector_ring_chain(tqp_vector, &vector_ring_chain);
2635
2636 if (ret)
2637 return ret;
2638
2639 netif_napi_add(priv->netdev, &tqp_vector->napi,
2640 hns3_nic_common_poll, NAPI_POLL_WEIGHT);
2641 }
2642
2643 return 0;
2644}
2645
2646static int hns3_nic_alloc_vector_data(struct hns3_nic_priv *priv)
2647{
2648 struct hnae3_handle *h = priv->ae_handle;
2649 struct hns3_enet_tqp_vector *tqp_vector;
Salil76ad4f02017-08-02 16:59:45 +01002650 struct hnae3_vector_info *vector;
2651 struct pci_dev *pdev = h->pdev;
2652 u16 tqp_num = h->kinfo.num_tqps;
2653 u16 vector_num;
2654 int ret = 0;
2655 u16 i;
2656
2657 /* RSS size, cpu online and vector_num should be the same */
2658 /* Should consider 2p/4p later */
2659 vector_num = min_t(u16, num_online_cpus(), tqp_num);
2660 vector = devm_kcalloc(&pdev->dev, vector_num, sizeof(*vector),
2661 GFP_KERNEL);
2662 if (!vector)
2663 return -ENOMEM;
2664
2665 vector_num = h->ae_algo->ops->get_vector(h, vector_num, vector);
2666
2667 priv->vector_num = vector_num;
2668 priv->tqp_vector = (struct hns3_enet_tqp_vector *)
2669 devm_kcalloc(&pdev->dev, vector_num, sizeof(*priv->tqp_vector),
2670 GFP_KERNEL);
Yunsheng Lindd38c722018-03-09 10:37:02 +08002671 if (!priv->tqp_vector) {
2672 ret = -ENOMEM;
2673 goto out;
Salil76ad4f02017-08-02 16:59:45 +01002674 }
2675
Yunsheng Lindd38c722018-03-09 10:37:02 +08002676 for (i = 0; i < priv->vector_num; i++) {
Salil76ad4f02017-08-02 16:59:45 +01002677 tqp_vector = &priv->tqp_vector[i];
Yunsheng Lindd38c722018-03-09 10:37:02 +08002678 tqp_vector->idx = i;
2679 tqp_vector->mask_addr = vector[i].io_addr;
2680 tqp_vector->vector_irq = vector[i].vector;
Fuyun Liang5fd47892018-01-12 16:23:11 +08002681 hns3_vector_gl_rl_init(tqp_vector, priv);
Salil76ad4f02017-08-02 16:59:45 +01002682 }
2683
2684out:
2685 devm_kfree(&pdev->dev, vector);
2686 return ret;
2687}
2688
Yunsheng Lindd38c722018-03-09 10:37:02 +08002689static void hns3_clear_ring_group(struct hns3_enet_ring_group *group)
2690{
2691 group->ring = NULL;
2692 group->count = 0;
2693}
2694
Salil76ad4f02017-08-02 16:59:45 +01002695static int hns3_nic_uninit_vector_data(struct hns3_nic_priv *priv)
2696{
2697 struct hnae3_ring_chain_node vector_ring_chain;
2698 struct hnae3_handle *h = priv->ae_handle;
2699 struct hns3_enet_tqp_vector *tqp_vector;
Salil76ad4f02017-08-02 16:59:45 +01002700 int i, ret;
2701
2702 for (i = 0; i < priv->vector_num; i++) {
2703 tqp_vector = &priv->tqp_vector[i];
2704
2705 ret = hns3_get_vector_ring_chain(tqp_vector,
2706 &vector_ring_chain);
2707 if (ret)
2708 return ret;
2709
2710 ret = h->ae_algo->ops->unmap_ring_from_vector(h,
2711 tqp_vector->vector_irq, &vector_ring_chain);
2712 if (ret)
2713 return ret;
2714
Yunsheng Lin0d3e6632018-03-09 10:37:01 +08002715 ret = h->ae_algo->ops->put_vector(h, tqp_vector->vector_irq);
2716 if (ret)
2717 return ret;
2718
Salil76ad4f02017-08-02 16:59:45 +01002719 hns3_free_vector_ring_chain(tqp_vector, &vector_ring_chain);
2720
2721 if (priv->tqp_vector[i].irq_init_flag == HNS3_VECTOR_INITED) {
2722 (void)irq_set_affinity_hint(
2723 priv->tqp_vector[i].vector_irq,
2724 NULL);
qumingguangae064e62017-11-02 20:45:22 +08002725 free_irq(priv->tqp_vector[i].vector_irq,
2726 &priv->tqp_vector[i]);
Salil76ad4f02017-08-02 16:59:45 +01002727 }
2728
2729 priv->ring_data[i].ring->irq_init_flag = HNS3_VECTOR_NOT_INITED;
Yunsheng Lindd38c722018-03-09 10:37:02 +08002730 hns3_clear_ring_group(&tqp_vector->rx_group);
2731 hns3_clear_ring_group(&tqp_vector->tx_group);
Salil76ad4f02017-08-02 16:59:45 +01002732 netif_napi_del(&priv->tqp_vector[i].napi);
2733 }
2734
Yunsheng Lindd38c722018-03-09 10:37:02 +08002735 return 0;
2736}
Salil76ad4f02017-08-02 16:59:45 +01002737
Yunsheng Lindd38c722018-03-09 10:37:02 +08002738static int hns3_nic_dealloc_vector_data(struct hns3_nic_priv *priv)
2739{
2740 struct hnae3_handle *h = priv->ae_handle;
2741 struct pci_dev *pdev = h->pdev;
2742 int i, ret;
2743
2744 for (i = 0; i < priv->vector_num; i++) {
2745 struct hns3_enet_tqp_vector *tqp_vector;
2746
2747 tqp_vector = &priv->tqp_vector[i];
2748 ret = h->ae_algo->ops->put_vector(h, tqp_vector->vector_irq);
2749 if (ret)
2750 return ret;
2751 }
2752
2753 devm_kfree(&pdev->dev, priv->tqp_vector);
Salil76ad4f02017-08-02 16:59:45 +01002754 return 0;
2755}
2756
2757static int hns3_ring_get_cfg(struct hnae3_queue *q, struct hns3_nic_priv *priv,
2758 int ring_type)
2759{
2760 struct hns3_nic_ring_data *ring_data = priv->ring_data;
2761 int queue_num = priv->ae_handle->kinfo.num_tqps;
2762 struct pci_dev *pdev = priv->ae_handle->pdev;
2763 struct hns3_enet_ring *ring;
2764
2765 ring = devm_kzalloc(&pdev->dev, sizeof(*ring), GFP_KERNEL);
2766 if (!ring)
2767 return -ENOMEM;
2768
2769 if (ring_type == HNAE3_RING_TYPE_TX) {
2770 ring_data[q->tqp_index].ring = ring;
Lipeng66b44732017-10-23 19:51:05 +08002771 ring_data[q->tqp_index].queue_index = q->tqp_index;
Salil76ad4f02017-08-02 16:59:45 +01002772 ring->io_base = (u8 __iomem *)q->io_base + HNS3_TX_REG_OFFSET;
2773 } else {
2774 ring_data[q->tqp_index + queue_num].ring = ring;
Lipeng66b44732017-10-23 19:51:05 +08002775 ring_data[q->tqp_index + queue_num].queue_index = q->tqp_index;
Salil76ad4f02017-08-02 16:59:45 +01002776 ring->io_base = q->io_base;
2777 }
2778
2779 hnae_set_bit(ring->flag, HNAE3_RING_TYPE_B, ring_type);
2780
Salil76ad4f02017-08-02 16:59:45 +01002781 ring->tqp = q;
2782 ring->desc = NULL;
2783 ring->desc_cb = NULL;
2784 ring->dev = priv->dev;
2785 ring->desc_dma_addr = 0;
2786 ring->buf_size = q->buf_size;
2787 ring->desc_num = q->desc_num;
2788 ring->next_to_use = 0;
2789 ring->next_to_clean = 0;
2790
2791 return 0;
2792}
2793
2794static int hns3_queue_to_ring(struct hnae3_queue *tqp,
2795 struct hns3_nic_priv *priv)
2796{
2797 int ret;
2798
2799 ret = hns3_ring_get_cfg(tqp, priv, HNAE3_RING_TYPE_TX);
2800 if (ret)
2801 return ret;
2802
2803 ret = hns3_ring_get_cfg(tqp, priv, HNAE3_RING_TYPE_RX);
2804 if (ret)
2805 return ret;
2806
2807 return 0;
2808}
2809
2810static int hns3_get_ring_config(struct hns3_nic_priv *priv)
2811{
2812 struct hnae3_handle *h = priv->ae_handle;
2813 struct pci_dev *pdev = h->pdev;
2814 int i, ret;
2815
2816 priv->ring_data = devm_kzalloc(&pdev->dev, h->kinfo.num_tqps *
2817 sizeof(*priv->ring_data) * 2,
2818 GFP_KERNEL);
2819 if (!priv->ring_data)
2820 return -ENOMEM;
2821
2822 for (i = 0; i < h->kinfo.num_tqps; i++) {
2823 ret = hns3_queue_to_ring(h->kinfo.tqp[i], priv);
2824 if (ret)
2825 goto err;
2826 }
2827
2828 return 0;
2829err:
2830 devm_kfree(&pdev->dev, priv->ring_data);
2831 return ret;
2832}
2833
Peng Li09f2af62017-12-22 12:21:41 +08002834static void hns3_put_ring_config(struct hns3_nic_priv *priv)
2835{
2836 struct hnae3_handle *h = priv->ae_handle;
2837 int i;
2838
2839 for (i = 0; i < h->kinfo.num_tqps; i++) {
2840 devm_kfree(priv->dev, priv->ring_data[i].ring);
2841 devm_kfree(priv->dev,
2842 priv->ring_data[i + h->kinfo.num_tqps].ring);
2843 }
2844 devm_kfree(priv->dev, priv->ring_data);
2845}
2846
Salil76ad4f02017-08-02 16:59:45 +01002847static int hns3_alloc_ring_memory(struct hns3_enet_ring *ring)
2848{
2849 int ret;
2850
2851 if (ring->desc_num <= 0 || ring->buf_size <= 0)
2852 return -EINVAL;
2853
2854 ring->desc_cb = kcalloc(ring->desc_num, sizeof(ring->desc_cb[0]),
2855 GFP_KERNEL);
2856 if (!ring->desc_cb) {
2857 ret = -ENOMEM;
2858 goto out;
2859 }
2860
2861 ret = hns3_alloc_desc(ring);
2862 if (ret)
2863 goto out_with_desc_cb;
2864
2865 if (!HNAE3_IS_TX_RING(ring)) {
2866 ret = hns3_alloc_ring_buffers(ring);
2867 if (ret)
2868 goto out_with_desc;
2869 }
2870
2871 return 0;
2872
2873out_with_desc:
2874 hns3_free_desc(ring);
2875out_with_desc_cb:
2876 kfree(ring->desc_cb);
2877 ring->desc_cb = NULL;
2878out:
2879 return ret;
2880}
2881
2882static void hns3_fini_ring(struct hns3_enet_ring *ring)
2883{
2884 hns3_free_desc(ring);
2885 kfree(ring->desc_cb);
2886 ring->desc_cb = NULL;
2887 ring->next_to_clean = 0;
2888 ring->next_to_use = 0;
2889}
2890
Yunsheng Lin1db9b1b2017-10-09 15:44:01 +08002891static int hns3_buf_size2type(u32 buf_size)
Salil76ad4f02017-08-02 16:59:45 +01002892{
2893 int bd_size_type;
2894
2895 switch (buf_size) {
2896 case 512:
2897 bd_size_type = HNS3_BD_SIZE_512_TYPE;
2898 break;
2899 case 1024:
2900 bd_size_type = HNS3_BD_SIZE_1024_TYPE;
2901 break;
2902 case 2048:
2903 bd_size_type = HNS3_BD_SIZE_2048_TYPE;
2904 break;
2905 case 4096:
2906 bd_size_type = HNS3_BD_SIZE_4096_TYPE;
2907 break;
2908 default:
2909 bd_size_type = HNS3_BD_SIZE_2048_TYPE;
2910 }
2911
2912 return bd_size_type;
2913}
2914
2915static void hns3_init_ring_hw(struct hns3_enet_ring *ring)
2916{
2917 dma_addr_t dma = ring->desc_dma_addr;
2918 struct hnae3_queue *q = ring->tqp;
2919
2920 if (!HNAE3_IS_TX_RING(ring)) {
2921 hns3_write_dev(q, HNS3_RING_RX_RING_BASEADDR_L_REG,
2922 (u32)dma);
2923 hns3_write_dev(q, HNS3_RING_RX_RING_BASEADDR_H_REG,
2924 (u32)((dma >> 31) >> 1));
2925
2926 hns3_write_dev(q, HNS3_RING_RX_RING_BD_LEN_REG,
2927 hns3_buf_size2type(ring->buf_size));
2928 hns3_write_dev(q, HNS3_RING_RX_RING_BD_NUM_REG,
2929 ring->desc_num / 8 - 1);
2930
2931 } else {
2932 hns3_write_dev(q, HNS3_RING_TX_RING_BASEADDR_L_REG,
2933 (u32)dma);
2934 hns3_write_dev(q, HNS3_RING_TX_RING_BASEADDR_H_REG,
2935 (u32)((dma >> 31) >> 1));
2936
2937 hns3_write_dev(q, HNS3_RING_TX_RING_BD_LEN_REG,
2938 hns3_buf_size2type(ring->buf_size));
2939 hns3_write_dev(q, HNS3_RING_TX_RING_BD_NUM_REG,
2940 ring->desc_num / 8 - 1);
2941 }
2942}
2943
Lipeng5668abd2017-10-10 16:42:04 +08002944int hns3_init_all_ring(struct hns3_nic_priv *priv)
Salil76ad4f02017-08-02 16:59:45 +01002945{
2946 struct hnae3_handle *h = priv->ae_handle;
2947 int ring_num = h->kinfo.num_tqps * 2;
2948 int i, j;
2949 int ret;
2950
2951 for (i = 0; i < ring_num; i++) {
2952 ret = hns3_alloc_ring_memory(priv->ring_data[i].ring);
2953 if (ret) {
2954 dev_err(priv->dev,
2955 "Alloc ring memory fail! ret=%d\n", ret);
2956 goto out_when_alloc_ring_memory;
2957 }
2958
Salil76ad4f02017-08-02 16:59:45 +01002959 u64_stats_init(&priv->ring_data[i].ring->syncp);
2960 }
2961
2962 return 0;
2963
2964out_when_alloc_ring_memory:
2965 for (j = i - 1; j >= 0; j--)
Lipengee83f772017-10-10 16:42:03 +08002966 hns3_fini_ring(priv->ring_data[j].ring);
Salil76ad4f02017-08-02 16:59:45 +01002967
2968 return -ENOMEM;
2969}
2970
Lipeng5668abd2017-10-10 16:42:04 +08002971int hns3_uninit_all_ring(struct hns3_nic_priv *priv)
Salil76ad4f02017-08-02 16:59:45 +01002972{
2973 struct hnae3_handle *h = priv->ae_handle;
2974 int i;
2975
2976 for (i = 0; i < h->kinfo.num_tqps; i++) {
2977 if (h->ae_algo->ops->reset_queue)
2978 h->ae_algo->ops->reset_queue(h, i);
2979
2980 hns3_fini_ring(priv->ring_data[i].ring);
2981 hns3_fini_ring(priv->ring_data[i + h->kinfo.num_tqps].ring);
2982 }
Salil76ad4f02017-08-02 16:59:45 +01002983 return 0;
2984}
2985
2986/* Set mac addr if it is configured. or leave it to the AE driver */
Yunsheng Linf09555f2018-05-09 17:24:38 +01002987static void hns3_init_mac_addr(struct net_device *netdev, bool init)
Salil76ad4f02017-08-02 16:59:45 +01002988{
2989 struct hns3_nic_priv *priv = netdev_priv(netdev);
2990 struct hnae3_handle *h = priv->ae_handle;
2991 u8 mac_addr_temp[ETH_ALEN];
2992
Yunsheng Linf09555f2018-05-09 17:24:38 +01002993 if (h->ae_algo->ops->get_mac_addr && init) {
Salil76ad4f02017-08-02 16:59:45 +01002994 h->ae_algo->ops->get_mac_addr(h, mac_addr_temp);
2995 ether_addr_copy(netdev->dev_addr, mac_addr_temp);
2996 }
2997
2998 /* Check if the MAC address is valid, if not get a random one */
2999 if (!is_valid_ether_addr(netdev->dev_addr)) {
3000 eth_hw_addr_random(netdev);
3001 dev_warn(priv->dev, "using random MAC address %pM\n",
3002 netdev->dev_addr);
Salil76ad4f02017-08-02 16:59:45 +01003003 }
Lipeng139e8792017-09-19 17:17:13 +01003004
3005 if (h->ae_algo->ops->set_mac_addr)
Fuyun Liang590980552018-03-10 11:29:22 +08003006 h->ae_algo->ops->set_mac_addr(h, netdev->dev_addr, true);
Lipeng139e8792017-09-19 17:17:13 +01003007
Salil76ad4f02017-08-02 16:59:45 +01003008}
3009
3010static void hns3_nic_set_priv_ops(struct net_device *netdev)
3011{
3012 struct hns3_nic_priv *priv = netdev_priv(netdev);
3013
3014 if ((netdev->features & NETIF_F_TSO) ||
3015 (netdev->features & NETIF_F_TSO6)) {
3016 priv->ops.fill_desc = hns3_fill_desc_tso;
3017 priv->ops.maybe_stop_tx = hns3_nic_maybe_stop_tso;
3018 } else {
3019 priv->ops.fill_desc = hns3_fill_desc;
3020 priv->ops.maybe_stop_tx = hns3_nic_maybe_stop_tx;
3021 }
3022}
3023
3024static int hns3_client_init(struct hnae3_handle *handle)
3025{
3026 struct pci_dev *pdev = handle->pdev;
3027 struct hns3_nic_priv *priv;
3028 struct net_device *netdev;
3029 int ret;
3030
3031 netdev = alloc_etherdev_mq(sizeof(struct hns3_nic_priv),
Peng Li678335a12018-03-08 19:41:54 +08003032 hns3_get_max_available_channels(handle));
Salil76ad4f02017-08-02 16:59:45 +01003033 if (!netdev)
3034 return -ENOMEM;
3035
3036 priv = netdev_priv(netdev);
3037 priv->dev = &pdev->dev;
3038 priv->netdev = netdev;
3039 priv->ae_handle = handle;
Salil Mehta6d4c3982018-03-22 14:28:52 +00003040 priv->ae_handle->reset_level = HNAE3_NONE_RESET;
3041 priv->ae_handle->last_reset_time = jiffies;
Lipengf8fa222c2017-11-02 20:45:20 +08003042 priv->tx_timeout_count = 0;
Salil76ad4f02017-08-02 16:59:45 +01003043
3044 handle->kinfo.netdev = netdev;
3045 handle->priv = (void *)priv;
3046
Yunsheng Linf09555f2018-05-09 17:24:38 +01003047 hns3_init_mac_addr(netdev, true);
Salil76ad4f02017-08-02 16:59:45 +01003048
3049 hns3_set_default_feature(netdev);
3050
3051 netdev->watchdog_timeo = HNS3_TX_TIMEOUT;
3052 netdev->priv_flags |= IFF_UNICAST_FLT;
3053 netdev->netdev_ops = &hns3_nic_netdev_ops;
3054 SET_NETDEV_DEV(netdev, &pdev->dev);
3055 hns3_ethtool_set_ops(netdev);
3056 hns3_nic_set_priv_ops(netdev);
3057
3058 /* Carrier off reporting is important to ethtool even BEFORE open */
3059 netif_carrier_off(netdev);
3060
3061 ret = hns3_get_ring_config(priv);
3062 if (ret) {
3063 ret = -ENOMEM;
3064 goto out_get_ring_cfg;
3065 }
3066
Yunsheng Lindd38c722018-03-09 10:37:02 +08003067 ret = hns3_nic_alloc_vector_data(priv);
3068 if (ret) {
3069 ret = -ENOMEM;
3070 goto out_alloc_vector_data;
3071 }
3072
Salil76ad4f02017-08-02 16:59:45 +01003073 ret = hns3_nic_init_vector_data(priv);
3074 if (ret) {
3075 ret = -ENOMEM;
3076 goto out_init_vector_data;
3077 }
3078
3079 ret = hns3_init_all_ring(priv);
3080 if (ret) {
3081 ret = -ENOMEM;
3082 goto out_init_ring_data;
3083 }
3084
3085 ret = register_netdev(netdev);
3086 if (ret) {
3087 dev_err(priv->dev, "probe register netdev fail!\n");
3088 goto out_reg_netdev_fail;
3089 }
3090
Yunsheng Lin986743d2017-09-27 09:45:30 +08003091 hns3_dcbnl_setup(handle);
3092
Salila8e8b7f2017-08-21 17:05:24 +01003093 /* MTU range: (ETH_MIN_MTU(kernel default) - 9706) */
3094 netdev->max_mtu = HNS3_MAX_MTU - (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
3095
Salil76ad4f02017-08-02 16:59:45 +01003096 return ret;
3097
3098out_reg_netdev_fail:
3099out_init_ring_data:
3100 (void)hns3_nic_uninit_vector_data(priv);
Salil76ad4f02017-08-02 16:59:45 +01003101out_init_vector_data:
Yunsheng Lindd38c722018-03-09 10:37:02 +08003102 hns3_nic_dealloc_vector_data(priv);
3103out_alloc_vector_data:
3104 priv->ring_data = NULL;
Salil76ad4f02017-08-02 16:59:45 +01003105out_get_ring_cfg:
3106 priv->ae_handle = NULL;
3107 free_netdev(netdev);
3108 return ret;
3109}
3110
3111static void hns3_client_uninit(struct hnae3_handle *handle, bool reset)
3112{
3113 struct net_device *netdev = handle->kinfo.netdev;
3114 struct hns3_nic_priv *priv = netdev_priv(netdev);
3115 int ret;
3116
3117 if (netdev->reg_state != NETREG_UNINITIALIZED)
3118 unregister_netdev(netdev);
3119
Fuyun Liang7b763f32018-05-25 19:43:04 +01003120 hns3_force_clear_all_rx_ring(handle);
3121
Salil76ad4f02017-08-02 16:59:45 +01003122 ret = hns3_nic_uninit_vector_data(priv);
3123 if (ret)
3124 netdev_err(netdev, "uninit vector error\n");
3125
Yunsheng Lindd38c722018-03-09 10:37:02 +08003126 ret = hns3_nic_dealloc_vector_data(priv);
3127 if (ret)
3128 netdev_err(netdev, "dealloc vector error\n");
3129
Salil76ad4f02017-08-02 16:59:45 +01003130 ret = hns3_uninit_all_ring(priv);
3131 if (ret)
3132 netdev_err(netdev, "uninit ring error\n");
3133
Yunsheng Linec777892018-03-09 10:37:00 +08003134 hns3_put_ring_config(priv);
3135
Salil76ad4f02017-08-02 16:59:45 +01003136 priv->ring_data = NULL;
3137
3138 free_netdev(netdev);
3139}
3140
3141static void hns3_link_status_change(struct hnae3_handle *handle, bool linkup)
3142{
3143 struct net_device *netdev = handle->kinfo.netdev;
3144
3145 if (!netdev)
3146 return;
3147
3148 if (linkup) {
3149 netif_carrier_on(netdev);
3150 netif_tx_wake_all_queues(netdev);
3151 netdev_info(netdev, "link up\n");
3152 } else {
3153 netif_carrier_off(netdev);
3154 netif_tx_stop_all_queues(netdev);
3155 netdev_info(netdev, "link down\n");
3156 }
3157}
3158
Yunsheng Lin9df8f792017-09-27 09:45:32 +08003159static int hns3_client_setup_tc(struct hnae3_handle *handle, u8 tc)
3160{
3161 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
3162 struct net_device *ndev = kinfo->netdev;
Colin Ian King075cfdd2017-09-29 20:51:23 +01003163 bool if_running;
Yunsheng Lin9df8f792017-09-27 09:45:32 +08003164 int ret;
3165 u8 i;
3166
3167 if (tc > HNAE3_MAX_TC)
3168 return -EINVAL;
3169
3170 if (!ndev)
3171 return -ENODEV;
3172
Colin Ian King075cfdd2017-09-29 20:51:23 +01003173 if_running = netif_running(ndev);
3174
Yunsheng Lin9df8f792017-09-27 09:45:32 +08003175 ret = netdev_set_num_tc(ndev, tc);
3176 if (ret)
3177 return ret;
3178
3179 if (if_running) {
3180 (void)hns3_nic_net_stop(ndev);
3181 msleep(100);
3182 }
3183
3184 ret = (kinfo->dcb_ops && kinfo->dcb_ops->map_update) ?
3185 kinfo->dcb_ops->map_update(handle) : -EOPNOTSUPP;
3186 if (ret)
3187 goto err_out;
3188
3189 if (tc <= 1) {
3190 netdev_reset_tc(ndev);
3191 goto out;
3192 }
3193
3194 for (i = 0; i < HNAE3_MAX_TC; i++) {
3195 struct hnae3_tc_info *tc_info = &kinfo->tc_info[i];
3196
3197 if (tc_info->enable)
3198 netdev_set_tc_queue(ndev,
3199 tc_info->tc,
3200 tc_info->tqp_count,
3201 tc_info->tqp_offset);
3202 }
3203
3204 for (i = 0; i < HNAE3_MAX_USER_PRIO; i++) {
3205 netdev_set_prio_tc_map(ndev, i,
3206 kinfo->prio_tc[i]);
3207 }
3208
3209out:
3210 ret = hns3_nic_set_real_num_queue(ndev);
3211
3212err_out:
3213 if (if_running)
3214 (void)hns3_nic_net_open(ndev);
3215
3216 return ret;
3217}
3218
Lipengbb6b94a2017-11-02 20:45:21 +08003219static void hns3_recover_hw_addr(struct net_device *ndev)
3220{
3221 struct netdev_hw_addr_list *list;
3222 struct netdev_hw_addr *ha, *tmp;
3223
3224 /* go through and sync uc_addr entries to the device */
3225 list = &ndev->uc;
3226 list_for_each_entry_safe(ha, tmp, &list->list, list)
3227 hns3_nic_uc_sync(ndev, ha->addr);
3228
3229 /* go through and sync mc_addr entries to the device */
3230 list = &ndev->mc;
3231 list_for_each_entry_safe(ha, tmp, &list->list, list)
3232 hns3_nic_mc_sync(ndev, ha->addr);
3233}
3234
Yunsheng Linbeebca3a2018-05-09 17:24:40 +01003235static void hns3_clear_tx_ring(struct hns3_enet_ring *ring)
Lipengbb6b94a2017-11-02 20:45:21 +08003236{
Yunsheng Linbeebca3a2018-05-09 17:24:40 +01003237 while (ring->next_to_clean != ring->next_to_use) {
Fuyun Liang7b763f32018-05-25 19:43:04 +01003238 ring->desc[ring->next_to_clean].tx.bdtp_fe_sc_vld_ra_ri = 0;
Yunsheng Linbeebca3a2018-05-09 17:24:40 +01003239 hns3_free_buffer_detach(ring, ring->next_to_clean);
3240 ring_ptr_move_fw(ring, next_to_clean);
3241 }
3242}
3243
Fuyun Liang7b763f32018-05-25 19:43:04 +01003244static int hns3_clear_rx_ring(struct hns3_enet_ring *ring)
3245{
3246 struct hns3_desc_cb res_cbs;
3247 int ret;
3248
3249 while (ring->next_to_use != ring->next_to_clean) {
3250 /* When a buffer is not reused, it's memory has been
3251 * freed in hns3_handle_rx_bd or will be freed by
3252 * stack, so we need to replace the buffer here.
3253 */
3254 if (!ring->desc_cb[ring->next_to_use].reuse_flag) {
3255 ret = hns3_reserve_buffer_map(ring, &res_cbs);
3256 if (ret) {
3257 u64_stats_update_begin(&ring->syncp);
3258 ring->stats.sw_err_cnt++;
3259 u64_stats_update_end(&ring->syncp);
3260 /* if alloc new buffer fail, exit directly
3261 * and reclear in up flow.
3262 */
3263 netdev_warn(ring->tqp->handle->kinfo.netdev,
3264 "reserve buffer map failed, ret = %d\n",
3265 ret);
3266 return ret;
3267 }
3268 hns3_replace_buffer(ring, ring->next_to_use,
3269 &res_cbs);
3270 }
3271 ring_ptr_move_fw(ring, next_to_use);
3272 }
3273
3274 return 0;
3275}
3276
3277static void hns3_force_clear_rx_ring(struct hns3_enet_ring *ring)
Yunsheng Linbeebca3a2018-05-09 17:24:40 +01003278{
Yunsheng Linbeebca3a2018-05-09 17:24:40 +01003279 while (ring->next_to_use != ring->next_to_clean) {
3280 /* When a buffer is not reused, it's memory has been
3281 * freed in hns3_handle_rx_bd or will be freed by
3282 * stack, so only need to unmap the buffer here.
3283 */
3284 if (!ring->desc_cb[ring->next_to_use].reuse_flag) {
3285 hns3_unmap_buffer(ring,
3286 &ring->desc_cb[ring->next_to_use]);
3287 ring->desc_cb[ring->next_to_use].dma = 0;
3288 }
3289
3290 ring_ptr_move_fw(ring, next_to_use);
3291 }
Lipengbb6b94a2017-11-02 20:45:21 +08003292}
3293
Fuyun Liang7b763f32018-05-25 19:43:04 +01003294static void hns3_force_clear_all_rx_ring(struct hnae3_handle *h)
3295{
3296 struct net_device *ndev = h->kinfo.netdev;
3297 struct hns3_nic_priv *priv = netdev_priv(ndev);
3298 struct hns3_enet_ring *ring;
3299 u32 i;
3300
3301 for (i = 0; i < h->kinfo.num_tqps; i++) {
3302 ring = priv->ring_data[i + h->kinfo.num_tqps].ring;
3303 hns3_force_clear_rx_ring(ring);
3304 }
3305}
3306
Lipengbb6b94a2017-11-02 20:45:21 +08003307static void hns3_clear_all_ring(struct hnae3_handle *h)
3308{
3309 struct net_device *ndev = h->kinfo.netdev;
3310 struct hns3_nic_priv *priv = netdev_priv(ndev);
3311 u32 i;
3312
3313 for (i = 0; i < h->kinfo.num_tqps; i++) {
3314 struct netdev_queue *dev_queue;
3315 struct hns3_enet_ring *ring;
3316
3317 ring = priv->ring_data[i].ring;
Yunsheng Linbeebca3a2018-05-09 17:24:40 +01003318 hns3_clear_tx_ring(ring);
Lipengbb6b94a2017-11-02 20:45:21 +08003319 dev_queue = netdev_get_tx_queue(ndev,
3320 priv->ring_data[i].queue_index);
3321 netdev_tx_reset_queue(dev_queue);
3322
3323 ring = priv->ring_data[i + h->kinfo.num_tqps].ring;
Fuyun Liang7b763f32018-05-25 19:43:04 +01003324 /* Continue to clear other rings even if clearing some
3325 * rings failed.
3326 */
Yunsheng Linbeebca3a2018-05-09 17:24:40 +01003327 hns3_clear_rx_ring(ring);
Lipengbb6b94a2017-11-02 20:45:21 +08003328 }
3329}
3330
Fuyun Liang7b763f32018-05-25 19:43:04 +01003331int hns3_nic_reset_all_ring(struct hnae3_handle *h)
3332{
3333 struct net_device *ndev = h->kinfo.netdev;
3334 struct hns3_nic_priv *priv = netdev_priv(ndev);
3335 struct hns3_enet_ring *rx_ring;
3336 int i, j;
3337 int ret;
3338
3339 for (i = 0; i < h->kinfo.num_tqps; i++) {
3340 h->ae_algo->ops->reset_queue(h, i);
3341 hns3_init_ring_hw(priv->ring_data[i].ring);
3342
3343 /* We need to clear tx ring here because self test will
3344 * use the ring and will not run down before up
3345 */
3346 hns3_clear_tx_ring(priv->ring_data[i].ring);
3347 priv->ring_data[i].ring->next_to_clean = 0;
3348 priv->ring_data[i].ring->next_to_use = 0;
3349
3350 rx_ring = priv->ring_data[i + h->kinfo.num_tqps].ring;
3351 hns3_init_ring_hw(rx_ring);
3352 ret = hns3_clear_rx_ring(rx_ring);
3353 if (ret)
3354 return ret;
3355
3356 /* We can not know the hardware head and tail when this
3357 * function is called in reset flow, so we reuse all desc.
3358 */
3359 for (j = 0; j < rx_ring->desc_num; j++)
3360 hns3_reuse_buffer(rx_ring, j);
3361
3362 rx_ring->next_to_clean = 0;
3363 rx_ring->next_to_use = 0;
3364 }
3365
3366 return 0;
3367}
3368
Lipengbb6b94a2017-11-02 20:45:21 +08003369static int hns3_reset_notify_down_enet(struct hnae3_handle *handle)
3370{
3371 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
3372 struct net_device *ndev = kinfo->netdev;
3373
3374 if (!netif_running(ndev))
3375 return -EIO;
3376
3377 return hns3_nic_net_stop(ndev);
3378}
3379
3380static int hns3_reset_notify_up_enet(struct hnae3_handle *handle)
3381{
3382 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
Lipengbb6b94a2017-11-02 20:45:21 +08003383 int ret = 0;
3384
3385 if (netif_running(kinfo->netdev)) {
3386 ret = hns3_nic_net_up(kinfo->netdev);
3387 if (ret) {
3388 netdev_err(kinfo->netdev,
3389 "hns net up fail, ret=%d!\n", ret);
3390 return ret;
3391 }
Salil Mehta6d4c3982018-03-22 14:28:52 +00003392 handle->last_reset_time = jiffies;
Lipengbb6b94a2017-11-02 20:45:21 +08003393 }
3394
3395 return ret;
3396}
3397
3398static int hns3_reset_notify_init_enet(struct hnae3_handle *handle)
3399{
3400 struct net_device *netdev = handle->kinfo.netdev;
3401 struct hns3_nic_priv *priv = netdev_priv(netdev);
3402 int ret;
3403
Yunsheng Linf09555f2018-05-09 17:24:38 +01003404 hns3_init_mac_addr(netdev, false);
Lipengbb6b94a2017-11-02 20:45:21 +08003405 hns3_nic_set_rx_mode(netdev);
3406 hns3_recover_hw_addr(netdev);
3407
Yunsheng Lin681ec392018-03-21 15:49:22 +08003408 /* Hardware table is only clear when pf resets */
3409 if (!(handle->flags & HNAE3_SUPPORT_VF))
3410 hns3_restore_vlan(netdev);
3411
Lipengbb6b94a2017-11-02 20:45:21 +08003412 /* Carrier off reporting is important to ethtool even BEFORE open */
3413 netif_carrier_off(netdev);
3414
3415 ret = hns3_get_ring_config(priv);
3416 if (ret)
3417 return ret;
3418
3419 ret = hns3_nic_init_vector_data(priv);
3420 if (ret)
3421 return ret;
3422
3423 ret = hns3_init_all_ring(priv);
3424 if (ret) {
3425 hns3_nic_uninit_vector_data(priv);
3426 priv->ring_data = NULL;
3427 }
3428
3429 return ret;
3430}
3431
3432static int hns3_reset_notify_uninit_enet(struct hnae3_handle *handle)
3433{
3434 struct net_device *netdev = handle->kinfo.netdev;
3435 struct hns3_nic_priv *priv = netdev_priv(netdev);
3436 int ret;
3437
Fuyun Liang7b763f32018-05-25 19:43:04 +01003438 hns3_force_clear_all_rx_ring(handle);
Lipengbb6b94a2017-11-02 20:45:21 +08003439
3440 ret = hns3_nic_uninit_vector_data(priv);
3441 if (ret) {
3442 netdev_err(netdev, "uninit vector error\n");
3443 return ret;
3444 }
3445
3446 ret = hns3_uninit_all_ring(priv);
3447 if (ret)
3448 netdev_err(netdev, "uninit ring error\n");
3449
Yunsheng Linec777892018-03-09 10:37:00 +08003450 hns3_put_ring_config(priv);
3451
Lipengbb6b94a2017-11-02 20:45:21 +08003452 priv->ring_data = NULL;
3453
3454 return ret;
3455}
3456
3457static int hns3_reset_notify(struct hnae3_handle *handle,
3458 enum hnae3_reset_notify_type type)
3459{
3460 int ret = 0;
3461
3462 switch (type) {
3463 case HNAE3_UP_CLIENT:
Salil Mehtae1586242018-01-19 15:20:53 +00003464 ret = hns3_reset_notify_up_enet(handle);
3465 break;
Lipengbb6b94a2017-11-02 20:45:21 +08003466 case HNAE3_DOWN_CLIENT:
3467 ret = hns3_reset_notify_down_enet(handle);
3468 break;
3469 case HNAE3_INIT_CLIENT:
3470 ret = hns3_reset_notify_init_enet(handle);
3471 break;
3472 case HNAE3_UNINIT_CLIENT:
3473 ret = hns3_reset_notify_uninit_enet(handle);
3474 break;
3475 default:
3476 break;
3477 }
3478
3479 return ret;
3480}
3481
Yunsheng Lin7a242b22018-03-09 10:37:04 +08003482static void hns3_restore_coal(struct hns3_nic_priv *priv,
3483 struct hns3_enet_coalesce *tx,
3484 struct hns3_enet_coalesce *rx)
3485{
3486 u16 vector_num = priv->vector_num;
3487 int i;
3488
3489 for (i = 0; i < vector_num; i++) {
3490 memcpy(&priv->tqp_vector[i].tx_group.coal, tx,
3491 sizeof(struct hns3_enet_coalesce));
3492 memcpy(&priv->tqp_vector[i].rx_group.coal, rx,
3493 sizeof(struct hns3_enet_coalesce));
3494 }
3495}
3496
3497static int hns3_modify_tqp_num(struct net_device *netdev, u16 new_tqp_num,
3498 struct hns3_enet_coalesce *tx,
3499 struct hns3_enet_coalesce *rx)
Peng Li09f2af62017-12-22 12:21:41 +08003500{
3501 struct hns3_nic_priv *priv = netdev_priv(netdev);
3502 struct hnae3_handle *h = hns3_get_handle(netdev);
3503 int ret;
3504
3505 ret = h->ae_algo->ops->set_channels(h, new_tqp_num);
3506 if (ret)
3507 return ret;
3508
3509 ret = hns3_get_ring_config(priv);
3510 if (ret)
3511 return ret;
3512
Yunsheng Lindd38c722018-03-09 10:37:02 +08003513 ret = hns3_nic_alloc_vector_data(priv);
3514 if (ret)
3515 goto err_alloc_vector;
3516
Yunsheng Lin7a242b22018-03-09 10:37:04 +08003517 hns3_restore_coal(priv, tx, rx);
3518
Peng Li09f2af62017-12-22 12:21:41 +08003519 ret = hns3_nic_init_vector_data(priv);
3520 if (ret)
3521 goto err_uninit_vector;
3522
3523 ret = hns3_init_all_ring(priv);
3524 if (ret)
3525 goto err_put_ring;
3526
3527 return 0;
3528
3529err_put_ring:
3530 hns3_put_ring_config(priv);
3531err_uninit_vector:
3532 hns3_nic_uninit_vector_data(priv);
Yunsheng Lindd38c722018-03-09 10:37:02 +08003533err_alloc_vector:
3534 hns3_nic_dealloc_vector_data(priv);
Peng Li09f2af62017-12-22 12:21:41 +08003535 return ret;
3536}
3537
3538static int hns3_adjust_tqps_num(u8 num_tc, u32 new_tqp_num)
3539{
3540 return (new_tqp_num / num_tc) * num_tc;
3541}
3542
3543int hns3_set_channels(struct net_device *netdev,
3544 struct ethtool_channels *ch)
3545{
3546 struct hns3_nic_priv *priv = netdev_priv(netdev);
3547 struct hnae3_handle *h = hns3_get_handle(netdev);
3548 struct hnae3_knic_private_info *kinfo = &h->kinfo;
Yunsheng Lin7a242b22018-03-09 10:37:04 +08003549 struct hns3_enet_coalesce tx_coal, rx_coal;
Peng Li09f2af62017-12-22 12:21:41 +08003550 bool if_running = netif_running(netdev);
3551 u32 new_tqp_num = ch->combined_count;
3552 u16 org_tqp_num;
3553 int ret;
3554
3555 if (ch->rx_count || ch->tx_count)
3556 return -EINVAL;
3557
Peng Li678335a12018-03-08 19:41:54 +08003558 if (new_tqp_num > hns3_get_max_available_channels(h) ||
Peng Li09f2af62017-12-22 12:21:41 +08003559 new_tqp_num < kinfo->num_tc) {
3560 dev_err(&netdev->dev,
3561 "Change tqps fail, the tqp range is from %d to %d",
3562 kinfo->num_tc,
Peng Li678335a12018-03-08 19:41:54 +08003563 hns3_get_max_available_channels(h));
Peng Li09f2af62017-12-22 12:21:41 +08003564 return -EINVAL;
3565 }
3566
3567 new_tqp_num = hns3_adjust_tqps_num(kinfo->num_tc, new_tqp_num);
3568 if (kinfo->num_tqps == new_tqp_num)
3569 return 0;
3570
3571 if (if_running)
Fuyun Liang20e4bf982018-03-10 11:29:24 +08003572 hns3_nic_net_stop(netdev);
Peng Li09f2af62017-12-22 12:21:41 +08003573
Peng Li09f2af62017-12-22 12:21:41 +08003574 ret = hns3_nic_uninit_vector_data(priv);
3575 if (ret) {
3576 dev_err(&netdev->dev,
3577 "Unbind vector with tqp fail, nothing is changed");
3578 goto open_netdev;
3579 }
3580
Yunsheng Lin7a242b22018-03-09 10:37:04 +08003581 /* Changing the tqp num may also change the vector num,
3582 * ethtool only support setting and querying one coal
3583 * configuation for now, so save the vector 0' coal
3584 * configuation here in order to restore it.
3585 */
3586 memcpy(&tx_coal, &priv->tqp_vector[0].tx_group.coal,
3587 sizeof(struct hns3_enet_coalesce));
3588 memcpy(&rx_coal, &priv->tqp_vector[0].rx_group.coal,
3589 sizeof(struct hns3_enet_coalesce));
3590
Yunsheng Lindd38c722018-03-09 10:37:02 +08003591 hns3_nic_dealloc_vector_data(priv);
3592
Peng Li09f2af62017-12-22 12:21:41 +08003593 hns3_uninit_all_ring(priv);
Yunsheng Linec777892018-03-09 10:37:00 +08003594 hns3_put_ring_config(priv);
Peng Li09f2af62017-12-22 12:21:41 +08003595
3596 org_tqp_num = h->kinfo.num_tqps;
Yunsheng Lin7a242b22018-03-09 10:37:04 +08003597 ret = hns3_modify_tqp_num(netdev, new_tqp_num, &tx_coal, &rx_coal);
Peng Li09f2af62017-12-22 12:21:41 +08003598 if (ret) {
Yunsheng Lin7a242b22018-03-09 10:37:04 +08003599 ret = hns3_modify_tqp_num(netdev, org_tqp_num,
3600 &tx_coal, &rx_coal);
Peng Li09f2af62017-12-22 12:21:41 +08003601 if (ret) {
3602 /* If revert to old tqp failed, fatal error occurred */
3603 dev_err(&netdev->dev,
3604 "Revert to old tqp num fail, ret=%d", ret);
3605 return ret;
3606 }
3607 dev_info(&netdev->dev,
3608 "Change tqp num fail, Revert to old tqp num");
3609 }
3610
3611open_netdev:
3612 if (if_running)
Fuyun Liang20e4bf982018-03-10 11:29:24 +08003613 hns3_nic_net_open(netdev);
Peng Li09f2af62017-12-22 12:21:41 +08003614
3615 return ret;
3616}
3617
Yunsheng Lin1db9b1b2017-10-09 15:44:01 +08003618static const struct hnae3_client_ops client_ops = {
Salil76ad4f02017-08-02 16:59:45 +01003619 .init_instance = hns3_client_init,
3620 .uninit_instance = hns3_client_uninit,
3621 .link_status_change = hns3_link_status_change,
Yunsheng Lin9df8f792017-09-27 09:45:32 +08003622 .setup_tc = hns3_client_setup_tc,
Lipengbb6b94a2017-11-02 20:45:21 +08003623 .reset_notify = hns3_reset_notify,
Salil76ad4f02017-08-02 16:59:45 +01003624};
3625
3626/* hns3_init_module - Driver registration routine
3627 * hns3_init_module is the first routine called when the driver is
3628 * loaded. All it does is register with the PCI subsystem.
3629 */
3630static int __init hns3_init_module(void)
3631{
3632 int ret;
3633
3634 pr_info("%s: %s - version\n", hns3_driver_name, hns3_driver_string);
3635 pr_info("%s: %s\n", hns3_driver_name, hns3_copyright);
3636
3637 client.type = HNAE3_CLIENT_KNIC;
3638 snprintf(client.name, HNAE3_CLIENT_NAME_LENGTH - 1, "%s",
3639 hns3_driver_name);
3640
3641 client.ops = &client_ops;
3642
Xi Wang13562d12018-05-19 16:53:18 +01003643 INIT_LIST_HEAD(&client.node);
3644
Salil76ad4f02017-08-02 16:59:45 +01003645 ret = hnae3_register_client(&client);
3646 if (ret)
3647 return ret;
3648
3649 ret = pci_register_driver(&hns3_driver);
3650 if (ret)
3651 hnae3_unregister_client(&client);
3652
3653 return ret;
3654}
3655module_init(hns3_init_module);
3656
3657/* hns3_exit_module - Driver exit cleanup routine
3658 * hns3_exit_module is called just before the driver is removed
3659 * from memory.
3660 */
3661static void __exit hns3_exit_module(void)
3662{
3663 pci_unregister_driver(&hns3_driver);
3664 hnae3_unregister_client(&client);
3665}
3666module_exit(hns3_exit_module);
3667
3668MODULE_DESCRIPTION("HNS3: Hisilicon Ethernet Driver");
3669MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
3670MODULE_LICENSE("GPL");
3671MODULE_ALIAS("pci:hns-nic");
Xi Wang3c7624d2018-05-19 16:53:19 +01003672MODULE_VERSION(HNS3_MOD_VERSION);