blob: f7338cb55eca2d044a77b22040de93e24fdff7b5 [file] [log] [blame]
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001/*
2 * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33
34#include <linux/etherdevice.h>
35#include <linux/tcp.h>
36#include <linux/if_vlan.h>
37#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Amir Vadai1eb8c692012-07-18 22:33:52 +000039#include <linux/hash.h>
40#include <net/ip.h>
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070041
42#include <linux/mlx4/driver.h>
43#include <linux/mlx4/device.h>
44#include <linux/mlx4/cmd.h>
45#include <linux/mlx4/cq.h>
46
47#include "mlx4_en.h"
48#include "en_port.h"
49
Amir Vadaid3179662012-12-02 03:49:23 +000050int mlx4_en_setup_tc(struct net_device *dev, u8 up)
Amir Vadai897d7842012-04-04 21:33:27 +000051{
Amir Vadaibc6a4742012-05-17 00:58:10 +000052 struct mlx4_en_priv *priv = netdev_priv(dev);
53 int i;
Amir Vadaid3179662012-12-02 03:49:23 +000054 unsigned int offset = 0;
Amir Vadaibc6a4742012-05-17 00:58:10 +000055
56 if (up && up != MLX4_EN_NUM_UP)
Amir Vadai897d7842012-04-04 21:33:27 +000057 return -EINVAL;
58
Amir Vadaibc6a4742012-05-17 00:58:10 +000059 netdev_set_num_tc(dev, up);
60
61 /* Partition Tx queues evenly amongst UP's */
Amir Vadaibc6a4742012-05-17 00:58:10 +000062 for (i = 0; i < up; i++) {
Amir Vadaid3179662012-12-02 03:49:23 +000063 netdev_set_tc_queue(dev, i, priv->num_tx_rings_p_up, offset);
64 offset += priv->num_tx_rings_p_up;
Amir Vadaibc6a4742012-05-17 00:58:10 +000065 }
66
Amir Vadai897d7842012-04-04 21:33:27 +000067 return 0;
68}
69
Amir Vadai1eb8c692012-07-18 22:33:52 +000070#ifdef CONFIG_RFS_ACCEL
71
72struct mlx4_en_filter {
73 struct list_head next;
74 struct work_struct work;
75
76 __be32 src_ip;
77 __be32 dst_ip;
78 __be16 src_port;
79 __be16 dst_port;
80
81 int rxq_index;
82 struct mlx4_en_priv *priv;
83 u32 flow_id; /* RFS infrastructure id */
84 int id; /* mlx4_en driver id */
85 u64 reg_id; /* Flow steering API id */
86 u8 activated; /* Used to prevent expiry before filter
87 * is attached
88 */
89 struct hlist_node filter_chain;
90};
91
92static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv);
93
94static void mlx4_en_filter_work(struct work_struct *work)
95{
96 struct mlx4_en_filter *filter = container_of(work,
97 struct mlx4_en_filter,
98 work);
99 struct mlx4_en_priv *priv = filter->priv;
100 struct mlx4_spec_list spec_tcp = {
101 .id = MLX4_NET_TRANS_RULE_ID_TCP,
102 {
103 .tcp_udp = {
104 .dst_port = filter->dst_port,
105 .dst_port_msk = (__force __be16)-1,
106 .src_port = filter->src_port,
107 .src_port_msk = (__force __be16)-1,
108 },
109 },
110 };
111 struct mlx4_spec_list spec_ip = {
112 .id = MLX4_NET_TRANS_RULE_ID_IPV4,
113 {
114 .ipv4 = {
115 .dst_ip = filter->dst_ip,
116 .dst_ip_msk = (__force __be32)-1,
117 .src_ip = filter->src_ip,
118 .src_ip_msk = (__force __be32)-1,
119 },
120 },
121 };
122 struct mlx4_spec_list spec_eth = {
123 .id = MLX4_NET_TRANS_RULE_ID_ETH,
124 };
125 struct mlx4_net_trans_rule rule = {
126 .list = LIST_HEAD_INIT(rule.list),
127 .queue_mode = MLX4_NET_TRANS_Q_LIFO,
128 .exclusive = 1,
129 .allow_loopback = 1,
130 .promisc_mode = MLX4_FS_PROMISC_NONE,
131 .port = priv->port,
132 .priority = MLX4_DOMAIN_RFS,
133 };
134 int rc;
Amir Vadai1eb8c692012-07-18 22:33:52 +0000135 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
136
137 list_add_tail(&spec_eth.list, &rule.list);
138 list_add_tail(&spec_ip.list, &rule.list);
139 list_add_tail(&spec_tcp.list, &rule.list);
140
Amir Vadai1eb8c692012-07-18 22:33:52 +0000141 rule.qpn = priv->rss_map.qps[filter->rxq_index].qpn;
Yan Burman6bbb6d92013-02-07 02:25:20 +0000142 memcpy(spec_eth.eth.dst_mac, priv->dev->dev_addr, ETH_ALEN);
Amir Vadai1eb8c692012-07-18 22:33:52 +0000143 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
144
145 filter->activated = 0;
146
147 if (filter->reg_id) {
148 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
149 if (rc && rc != -ENOENT)
150 en_err(priv, "Error detaching flow. rc = %d\n", rc);
151 }
152
153 rc = mlx4_flow_attach(priv->mdev->dev, &rule, &filter->reg_id);
154 if (rc)
155 en_err(priv, "Error attaching flow. err = %d\n", rc);
156
157 mlx4_en_filter_rfs_expire(priv);
158
159 filter->activated = 1;
160}
161
162static inline struct hlist_head *
163filter_hash_bucket(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
164 __be16 src_port, __be16 dst_port)
165{
166 unsigned long l;
167 int bucket_idx;
168
169 l = (__force unsigned long)src_port |
170 ((__force unsigned long)dst_port << 2);
171 l ^= (__force unsigned long)(src_ip ^ dst_ip);
172
173 bucket_idx = hash_long(l, MLX4_EN_FILTER_HASH_SHIFT);
174
175 return &priv->filter_hash[bucket_idx];
176}
177
178static struct mlx4_en_filter *
179mlx4_en_filter_alloc(struct mlx4_en_priv *priv, int rxq_index, __be32 src_ip,
180 __be32 dst_ip, __be16 src_port, __be16 dst_port,
181 u32 flow_id)
182{
183 struct mlx4_en_filter *filter = NULL;
184
185 filter = kzalloc(sizeof(struct mlx4_en_filter), GFP_ATOMIC);
186 if (!filter)
187 return NULL;
188
189 filter->priv = priv;
190 filter->rxq_index = rxq_index;
191 INIT_WORK(&filter->work, mlx4_en_filter_work);
192
193 filter->src_ip = src_ip;
194 filter->dst_ip = dst_ip;
195 filter->src_port = src_port;
196 filter->dst_port = dst_port;
197
198 filter->flow_id = flow_id;
199
Amir Vadaiee64c0e2012-07-25 21:21:16 +0000200 filter->id = priv->last_filter_id++ % RPS_NO_FILTER;
Amir Vadai1eb8c692012-07-18 22:33:52 +0000201
202 list_add_tail(&filter->next, &priv->filters);
203 hlist_add_head(&filter->filter_chain,
204 filter_hash_bucket(priv, src_ip, dst_ip, src_port,
205 dst_port));
206
207 return filter;
208}
209
210static void mlx4_en_filter_free(struct mlx4_en_filter *filter)
211{
212 struct mlx4_en_priv *priv = filter->priv;
213 int rc;
214
215 list_del(&filter->next);
216
217 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
218 if (rc && rc != -ENOENT)
219 en_err(priv, "Error detaching flow. rc = %d\n", rc);
220
221 kfree(filter);
222}
223
224static inline struct mlx4_en_filter *
225mlx4_en_filter_find(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
226 __be16 src_port, __be16 dst_port)
227{
228 struct hlist_node *elem;
229 struct mlx4_en_filter *filter;
230 struct mlx4_en_filter *ret = NULL;
231
232 hlist_for_each_entry(filter, elem,
233 filter_hash_bucket(priv, src_ip, dst_ip,
234 src_port, dst_port),
235 filter_chain) {
236 if (filter->src_ip == src_ip &&
237 filter->dst_ip == dst_ip &&
238 filter->src_port == src_port &&
239 filter->dst_port == dst_port) {
240 ret = filter;
241 break;
242 }
243 }
244
245 return ret;
246}
247
248static int
249mlx4_en_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
250 u16 rxq_index, u32 flow_id)
251{
252 struct mlx4_en_priv *priv = netdev_priv(net_dev);
253 struct mlx4_en_filter *filter;
254 const struct iphdr *ip;
255 const __be16 *ports;
256 __be32 src_ip;
257 __be32 dst_ip;
258 __be16 src_port;
259 __be16 dst_port;
260 int nhoff = skb_network_offset(skb);
261 int ret = 0;
262
263 if (skb->protocol != htons(ETH_P_IP))
264 return -EPROTONOSUPPORT;
265
266 ip = (const struct iphdr *)(skb->data + nhoff);
267 if (ip_is_fragment(ip))
268 return -EPROTONOSUPPORT;
269
270 ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
271
272 src_ip = ip->saddr;
273 dst_ip = ip->daddr;
274 src_port = ports[0];
275 dst_port = ports[1];
276
277 if (ip->protocol != IPPROTO_TCP)
278 return -EPROTONOSUPPORT;
279
280 spin_lock_bh(&priv->filters_lock);
281 filter = mlx4_en_filter_find(priv, src_ip, dst_ip, src_port, dst_port);
282 if (filter) {
283 if (filter->rxq_index == rxq_index)
284 goto out;
285
286 filter->rxq_index = rxq_index;
287 } else {
288 filter = mlx4_en_filter_alloc(priv, rxq_index,
289 src_ip, dst_ip,
290 src_port, dst_port, flow_id);
291 if (!filter) {
292 ret = -ENOMEM;
293 goto err;
294 }
295 }
296
297 queue_work(priv->mdev->workqueue, &filter->work);
298
299out:
300 ret = filter->id;
301err:
302 spin_unlock_bh(&priv->filters_lock);
303
304 return ret;
305}
306
307void mlx4_en_cleanup_filters(struct mlx4_en_priv *priv,
308 struct mlx4_en_rx_ring *rx_ring)
309{
310 struct mlx4_en_filter *filter, *tmp;
311 LIST_HEAD(del_list);
312
313 spin_lock_bh(&priv->filters_lock);
314 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
315 list_move(&filter->next, &del_list);
316 hlist_del(&filter->filter_chain);
317 }
318 spin_unlock_bh(&priv->filters_lock);
319
320 list_for_each_entry_safe(filter, tmp, &del_list, next) {
321 cancel_work_sync(&filter->work);
322 mlx4_en_filter_free(filter);
323 }
324}
325
326static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv)
327{
328 struct mlx4_en_filter *filter = NULL, *tmp, *last_filter = NULL;
329 LIST_HEAD(del_list);
330 int i = 0;
331
332 spin_lock_bh(&priv->filters_lock);
333 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
334 if (i > MLX4_EN_FILTER_EXPIRY_QUOTA)
335 break;
336
337 if (filter->activated &&
338 !work_pending(&filter->work) &&
339 rps_may_expire_flow(priv->dev,
340 filter->rxq_index, filter->flow_id,
341 filter->id)) {
342 list_move(&filter->next, &del_list);
343 hlist_del(&filter->filter_chain);
344 } else
345 last_filter = filter;
346
347 i++;
348 }
349
350 if (last_filter && (&last_filter->next != priv->filters.next))
351 list_move(&priv->filters, &last_filter->next);
352
353 spin_unlock_bh(&priv->filters_lock);
354
355 list_for_each_entry_safe(filter, tmp, &del_list, next)
356 mlx4_en_filter_free(filter);
357}
358#endif
359
Jiri Pirko8e586132011-12-08 19:52:37 -0500360static int mlx4_en_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700361{
362 struct mlx4_en_priv *priv = netdev_priv(dev);
363 struct mlx4_en_dev *mdev = priv->mdev;
364 int err;
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300365 int idx;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700366
Jiri Pirkof1b553f2011-07-20 04:54:22 +0000367 en_dbg(HW, priv, "adding VLAN:%d\n", vid);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700368
Jiri Pirkof1b553f2011-07-20 04:54:22 +0000369 set_bit(vid, priv->active_vlans);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700370
371 /* Add VID to port VLAN filter */
372 mutex_lock(&mdev->state_lock);
373 if (mdev->device_up && priv->port_up) {
Jiri Pirkof1b553f2011-07-20 04:54:22 +0000374 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700375 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000376 en_err(priv, "Failed configuring VLAN filter\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700377 }
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300378 if (mlx4_register_vlan(mdev->dev, priv->port, vid, &idx))
379 en_err(priv, "failed adding vlan %d\n", vid);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700380 mutex_unlock(&mdev->state_lock);
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300381
Jiri Pirko8e586132011-12-08 19:52:37 -0500382 return 0;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700383}
384
Jiri Pirko8e586132011-12-08 19:52:37 -0500385static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700386{
387 struct mlx4_en_priv *priv = netdev_priv(dev);
388 struct mlx4_en_dev *mdev = priv->mdev;
389 int err;
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300390 int idx;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700391
Jiri Pirkof1b553f2011-07-20 04:54:22 +0000392 en_dbg(HW, priv, "Killing VID:%d\n", vid);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700393
Jiri Pirkof1b553f2011-07-20 04:54:22 +0000394 clear_bit(vid, priv->active_vlans);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700395
396 /* Remove VID from port VLAN filter */
397 mutex_lock(&mdev->state_lock);
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300398 if (!mlx4_find_cached_vlan(mdev->dev, priv->port, vid, &idx))
399 mlx4_unregister_vlan(mdev->dev, priv->port, idx);
400 else
401 en_err(priv, "could not find vid %d in cache\n", vid);
402
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700403 if (mdev->device_up && priv->port_up) {
Jiri Pirkof1b553f2011-07-20 04:54:22 +0000404 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700405 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000406 en_err(priv, "Failed configuring VLAN filter\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700407 }
408 mutex_unlock(&mdev->state_lock);
Jiri Pirko8e586132011-12-08 19:52:37 -0500409
410 return 0;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700411}
412
Yan Burman6bbb6d92013-02-07 02:25:20 +0000413static void mlx4_en_u64_to_mac(unsigned char dst_mac[ETH_ALEN + 2], u64 src_mac)
414{
415 unsigned int i;
416 for (i = ETH_ALEN - 1; i; --i) {
417 dst_mac[i] = src_mac & 0xff;
418 src_mac >>= 8;
419 }
420 memset(&dst_mac[ETH_ALEN], 0, 2);
421}
422
Yevgeny Petriline7c1c2c42010-08-24 03:46:18 +0000423u64 mlx4_en_mac_to_u64(u8 *addr)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700424{
425 u64 mac = 0;
426 int i;
427
428 for (i = 0; i < ETH_ALEN; i++) {
429 mac <<= 8;
430 mac |= addr[i];
431 }
432 return mac;
433}
434
435static int mlx4_en_set_mac(struct net_device *dev, void *addr)
436{
437 struct mlx4_en_priv *priv = netdev_priv(dev);
438 struct mlx4_en_dev *mdev = priv->mdev;
439 struct sockaddr *saddr = addr;
440
441 if (!is_valid_ether_addr(saddr->sa_data))
442 return -EADDRNOTAVAIL;
443
444 memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700445 queue_work(mdev->workqueue, &priv->mac_task);
446 return 0;
447}
448
449static void mlx4_en_do_set_mac(struct work_struct *work)
450{
451 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
452 mac_task);
453 struct mlx4_en_dev *mdev = priv->mdev;
454 int err = 0;
455
456 mutex_lock(&mdev->state_lock);
457 if (priv->port_up) {
458 /* Remove old MAC and insert the new one */
Yan Burman6bbb6d92013-02-07 02:25:20 +0000459 u64 mac = mlx4_en_mac_to_u64(priv->dev->dev_addr);
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000460 err = mlx4_replace_mac(mdev->dev, priv->port,
Yan Burman6bbb6d92013-02-07 02:25:20 +0000461 priv->base_qpn, mac);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700462 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000463 en_err(priv, "Failed changing HW MAC address\n");
Yan Burman6bbb6d92013-02-07 02:25:20 +0000464 memcpy(priv->prev_mac, priv->dev->dev_addr,
465 sizeof(priv->prev_mac));
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700466 } else
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000467 en_dbg(HW, priv, "Port is down while "
468 "registering mac, exiting...\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700469
470 mutex_unlock(&mdev->state_lock);
471}
472
473static void mlx4_en_clear_list(struct net_device *dev)
474{
475 struct mlx4_en_priv *priv = netdev_priv(dev);
Yevgeny Petrilin6d199932012-07-05 04:03:43 +0000476 struct mlx4_en_mc_list *tmp, *mc_to_del;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700477
Yevgeny Petrilin6d199932012-07-05 04:03:43 +0000478 list_for_each_entry_safe(mc_to_del, tmp, &priv->mc_list, list) {
479 list_del(&mc_to_del->list);
480 kfree(mc_to_del);
481 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700482}
483
484static void mlx4_en_cache_mclist(struct net_device *dev)
485{
486 struct mlx4_en_priv *priv = netdev_priv(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000487 struct netdev_hw_addr *ha;
Yevgeny Petrilin6d199932012-07-05 04:03:43 +0000488 struct mlx4_en_mc_list *tmp;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700489
Alexander Guller0e035672011-12-19 04:02:58 +0000490 mlx4_en_clear_list(dev);
Yevgeny Petrilin6d199932012-07-05 04:03:43 +0000491 netdev_for_each_mc_addr(ha, dev) {
492 tmp = kzalloc(sizeof(struct mlx4_en_mc_list), GFP_ATOMIC);
493 if (!tmp) {
494 en_err(priv, "failed to allocate multicast list\n");
495 mlx4_en_clear_list(dev);
496 return;
497 }
498 memcpy(tmp->addr, ha->addr, ETH_ALEN);
499 list_add_tail(&tmp->list, &priv->mc_list);
500 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700501}
502
Yevgeny Petrilin6d199932012-07-05 04:03:43 +0000503static void update_mclist_flags(struct mlx4_en_priv *priv,
504 struct list_head *dst,
505 struct list_head *src)
506{
507 struct mlx4_en_mc_list *dst_tmp, *src_tmp, *new_mc;
508 bool found;
509
510 /* Find all the entries that should be removed from dst,
511 * These are the entries that are not found in src
512 */
513 list_for_each_entry(dst_tmp, dst, list) {
514 found = false;
515 list_for_each_entry(src_tmp, src, list) {
516 if (!memcmp(dst_tmp->addr, src_tmp->addr, ETH_ALEN)) {
517 found = true;
518 break;
519 }
520 }
521 if (!found)
522 dst_tmp->action = MCLIST_REM;
523 }
524
525 /* Add entries that exist in src but not in dst
526 * mark them as need to add
527 */
528 list_for_each_entry(src_tmp, src, list) {
529 found = false;
530 list_for_each_entry(dst_tmp, dst, list) {
531 if (!memcmp(dst_tmp->addr, src_tmp->addr, ETH_ALEN)) {
532 dst_tmp->action = MCLIST_NONE;
533 found = true;
534 break;
535 }
536 }
537 if (!found) {
538 new_mc = kmalloc(sizeof(struct mlx4_en_mc_list),
539 GFP_KERNEL);
540 if (!new_mc) {
541 en_err(priv, "Failed to allocate current multicast list\n");
542 return;
543 }
544 memcpy(new_mc, src_tmp,
545 sizeof(struct mlx4_en_mc_list));
546 new_mc->action = MCLIST_ADD;
547 list_add_tail(&new_mc->list, dst);
548 }
549 }
550}
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700551
552static void mlx4_en_set_multicast(struct net_device *dev)
553{
554 struct mlx4_en_priv *priv = netdev_priv(dev);
555
556 if (!priv->port_up)
557 return;
558
559 queue_work(priv->mdev->workqueue, &priv->mcast_task);
560}
561
562static void mlx4_en_do_set_multicast(struct work_struct *work)
563{
564 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
565 mcast_task);
566 struct mlx4_en_dev *mdev = priv->mdev;
567 struct net_device *dev = priv->dev;
Yevgeny Petrilin6d199932012-07-05 04:03:43 +0000568 struct mlx4_en_mc_list *mclist, *tmp;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700569 u64 mcast_addr = 0;
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000570 u8 mc_list[16] = {0};
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +0000571 int err = 0;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700572
573 mutex_lock(&mdev->state_lock);
574 if (!mdev->device_up) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000575 en_dbg(HW, priv, "Card is not up, "
576 "ignoring multicast change.\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700577 goto out;
578 }
579 if (!priv->port_up) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000580 en_dbg(HW, priv, "Port is down, "
581 "ignoring multicast change.\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700582 goto out;
583 }
584
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000585 if (!netif_carrier_ok(dev)) {
586 if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
587 if (priv->port_state.link_state) {
588 priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
589 netif_carrier_on(dev);
590 en_dbg(LINK, priv, "Link Up\n");
591 }
592 }
593 }
594
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700595 /*
596 * Promsicuous mode: disable all filters
597 */
598
599 if (dev->flags & IFF_PROMISC) {
600 if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
601 if (netif_msg_rx_status(priv))
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000602 en_warn(priv, "Entering promiscuous mode\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700603 priv->flags |= MLX4_EN_FLAG_PROMISC;
604
605 /* Enable promiscouos mode */
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +0000606 switch (mdev->dev->caps.steering_mode) {
Hadar Hen Zion592e49d2012-07-05 04:03:48 +0000607 case MLX4_STEERING_MODE_DEVICE_MANAGED:
608 err = mlx4_flow_steer_promisc_add(mdev->dev,
609 priv->port,
610 priv->base_qpn,
611 MLX4_FS_PROMISC_UPLINK);
612 if (err)
613 en_err(priv, "Failed enabling promiscuous mode\n");
614 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
615 break;
616
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +0000617 case MLX4_STEERING_MODE_B0:
618 err = mlx4_unicast_promisc_add(mdev->dev,
619 priv->base_qpn,
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000620 priv->port);
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +0000621 if (err)
622 en_err(priv, "Failed enabling unicast promiscuous mode\n");
623
624 /* Add the default qp number as multicast
625 * promisc
626 */
627 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
628 err = mlx4_multicast_promisc_add(mdev->dev,
629 priv->base_qpn,
630 priv->port);
631 if (err)
632 en_err(priv, "Failed enabling multicast promiscuous mode\n");
633 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
634 }
635 break;
636
637 case MLX4_STEERING_MODE_A0:
638 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
639 priv->port,
640 priv->base_qpn,
641 1);
642 if (err)
643 en_err(priv, "Failed enabling promiscuous mode\n");
644 break;
645 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700646
647 /* Disable port multicast filter (unconditionally) */
648 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
649 0, MLX4_MCAST_DISABLE);
650 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000651 en_err(priv, "Failed disabling "
652 "multicast filter\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700653
Jiri Pirkof1b553f2011-07-20 04:54:22 +0000654 /* Disable port VLAN filter */
655 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
656 if (err)
657 en_err(priv, "Failed disabling VLAN filter\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700658 }
659 goto out;
660 }
661
662 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300663 * Not in promiscuous mode
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700664 */
665
666 if (priv->flags & MLX4_EN_FLAG_PROMISC) {
667 if (netif_msg_rx_status(priv))
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000668 en_warn(priv, "Leaving promiscuous mode\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700669 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
670
671 /* Disable promiscouos mode */
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +0000672 switch (mdev->dev->caps.steering_mode) {
Hadar Hen Zion592e49d2012-07-05 04:03:48 +0000673 case MLX4_STEERING_MODE_DEVICE_MANAGED:
674 err = mlx4_flow_steer_promisc_remove(mdev->dev,
675 priv->port,
676 MLX4_FS_PROMISC_UPLINK);
677 if (err)
678 en_err(priv, "Failed disabling promiscuous mode\n");
679 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
680 break;
681
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +0000682 case MLX4_STEERING_MODE_B0:
683 err = mlx4_unicast_promisc_remove(mdev->dev,
684 priv->base_qpn,
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000685 priv->port);
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000686 if (err)
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +0000687 en_err(priv, "Failed disabling unicast promiscuous mode\n");
688 /* Disable Multicast promisc */
689 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
690 err = mlx4_multicast_promisc_remove(mdev->dev,
691 priv->base_qpn,
692 priv->port);
693 if (err)
694 en_err(priv, "Failed disabling multicast promiscuous mode\n");
695 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
696 }
697 break;
698
699 case MLX4_STEERING_MODE_A0:
700 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
701 priv->port,
702 priv->base_qpn, 0);
703 if (err)
704 en_err(priv, "Failed disabling promiscuous mode\n");
705 break;
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000706 }
707
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700708 /* Enable port VLAN filter */
Jiri Pirkof1b553f2011-07-20 04:54:22 +0000709 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700710 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000711 en_err(priv, "Failed enabling VLAN filter\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700712 }
713
714 /* Enable/disable the multicast filter according to IFF_ALLMULTI */
715 if (dev->flags & IFF_ALLMULTI) {
716 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
717 0, MLX4_MCAST_DISABLE);
718 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000719 en_err(priv, "Failed disabling multicast filter\n");
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000720
721 /* Add the default qp number as multicast promisc */
722 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +0000723 switch (mdev->dev->caps.steering_mode) {
Hadar Hen Zion592e49d2012-07-05 04:03:48 +0000724 case MLX4_STEERING_MODE_DEVICE_MANAGED:
725 err = mlx4_flow_steer_promisc_add(mdev->dev,
726 priv->port,
727 priv->base_qpn,
728 MLX4_FS_PROMISC_ALL_MULTI);
729 break;
730
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +0000731 case MLX4_STEERING_MODE_B0:
732 err = mlx4_multicast_promisc_add(mdev->dev,
733 priv->base_qpn,
734 priv->port);
735 break;
736
737 case MLX4_STEERING_MODE_A0:
738 break;
739 }
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000740 if (err)
741 en_err(priv, "Failed entering multicast promisc mode\n");
742 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
743 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700744 } else {
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000745 /* Disable Multicast promisc */
746 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +0000747 switch (mdev->dev->caps.steering_mode) {
Hadar Hen Zion592e49d2012-07-05 04:03:48 +0000748 case MLX4_STEERING_MODE_DEVICE_MANAGED:
749 err = mlx4_flow_steer_promisc_remove(mdev->dev,
750 priv->port,
751 MLX4_FS_PROMISC_ALL_MULTI);
752 break;
753
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +0000754 case MLX4_STEERING_MODE_B0:
755 err = mlx4_multicast_promisc_remove(mdev->dev,
756 priv->base_qpn,
757 priv->port);
758 break;
759
760 case MLX4_STEERING_MODE_A0:
761 break;
762 }
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000763 if (err)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300764 en_err(priv, "Failed disabling multicast promiscuous mode\n");
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000765 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
766 }
Jiri Pirkoff6e2162010-03-01 05:09:14 +0000767
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700768 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
769 0, MLX4_MCAST_DISABLE);
770 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000771 en_err(priv, "Failed disabling multicast filter\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700772
773 /* Flush mcast filter and init it with broadcast address */
774 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
775 1, MLX4_MCAST_CONFIG);
776
777 /* Update multicast list - we cache all addresses so they won't
778 * change while HW is updated holding the command semaphor */
Eugenia Emantayevdbd501a2013-01-24 01:54:16 +0000779 netif_addr_lock_bh(dev);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700780 mlx4_en_cache_mclist(dev);
Eugenia Emantayevdbd501a2013-01-24 01:54:16 +0000781 netif_addr_unlock_bh(dev);
Yevgeny Petrilin6d199932012-07-05 04:03:43 +0000782 list_for_each_entry(mclist, &priv->mc_list, list) {
783 mcast_addr = mlx4_en_mac_to_u64(mclist->addr);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700784 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
785 mcast_addr, 0, MLX4_MCAST_CONFIG);
786 }
787 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
788 0, MLX4_MCAST_ENABLE);
789 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000790 en_err(priv, "Failed enabling multicast filter\n");
Yevgeny Petrilin6d199932012-07-05 04:03:43 +0000791
792 update_mclist_flags(priv, &priv->curr_list, &priv->mc_list);
793 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
794 if (mclist->action == MCLIST_REM) {
795 /* detach this address and delete from list */
796 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
797 mc_list[5] = priv->port;
798 err = mlx4_multicast_detach(mdev->dev,
799 &priv->rss_map.indir_qp,
800 mc_list,
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +0000801 MLX4_PROT_ETH,
802 mclist->reg_id);
Yevgeny Petrilin6d199932012-07-05 04:03:43 +0000803 if (err)
804 en_err(priv, "Fail to detach multicast address\n");
805
806 /* remove from list */
807 list_del(&mclist->list);
808 kfree(mclist);
Dan Carpenter9c645082012-07-10 20:34:07 +0000809 } else if (mclist->action == MCLIST_ADD) {
Yevgeny Petrilin6d199932012-07-05 04:03:43 +0000810 /* attach the address */
811 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +0000812 /* needed for B0 steering support */
Yevgeny Petrilin6d199932012-07-05 04:03:43 +0000813 mc_list[5] = priv->port;
814 err = mlx4_multicast_attach(mdev->dev,
815 &priv->rss_map.indir_qp,
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +0000816 mc_list,
817 priv->port, 0,
818 MLX4_PROT_ETH,
819 &mclist->reg_id);
Yevgeny Petrilin6d199932012-07-05 04:03:43 +0000820 if (err)
821 en_err(priv, "Fail to attach multicast address\n");
822
823 }
824 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700825 }
826out:
827 mutex_unlock(&mdev->state_lock);
828}
829
830#ifdef CONFIG_NET_POLL_CONTROLLER
831static void mlx4_en_netpoll(struct net_device *dev)
832{
833 struct mlx4_en_priv *priv = netdev_priv(dev);
834 struct mlx4_en_cq *cq;
835 unsigned long flags;
836 int i;
837
838 for (i = 0; i < priv->rx_ring_num; i++) {
839 cq = &priv->rx_cq[i];
840 spin_lock_irqsave(&cq->lock, flags);
841 napi_synchronize(&cq->napi);
842 mlx4_en_process_rx_cq(dev, cq, 0);
843 spin_unlock_irqrestore(&cq->lock, flags);
844 }
845}
846#endif
847
848static void mlx4_en_tx_timeout(struct net_device *dev)
849{
850 struct mlx4_en_priv *priv = netdev_priv(dev);
851 struct mlx4_en_dev *mdev = priv->mdev;
852
853 if (netif_msg_timer(priv))
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000854 en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700855
Yevgeny Petrilin1e338db2009-04-20 04:26:05 +0000856 priv->port_stats.tx_timeout++;
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000857 en_dbg(DRV, priv, "Scheduling watchdog\n");
Yevgeny Petrilin1e338db2009-04-20 04:26:05 +0000858 queue_work(mdev->workqueue, &priv->watchdog_task);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700859}
860
861
862static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
863{
864 struct mlx4_en_priv *priv = netdev_priv(dev);
865
866 spin_lock_bh(&priv->stats_lock);
867 memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
868 spin_unlock_bh(&priv->stats_lock);
869
870 return &priv->ret_stats;
871}
872
873static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
874{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700875 struct mlx4_en_cq *cq;
876 int i;
877
878 /* If we haven't received a specific coalescing setting
Martin Olsson98a17082009-04-22 18:21:29 +0200879 * (module param), we set the moderation parameters as follows:
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700880 * - moder_cnt is set to the number of mtu sized packets to
Eric Dumazetecfd2ce2012-11-05 16:20:42 +0000881 * satisfy our coalescing target.
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700882 * - moder_time is set to a fixed value.
883 */
Yevgeny Petrilin3db36fb2009-06-01 23:23:13 +0000884 priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
Yevgeny Petrilin60b9f9e2008-12-25 18:19:47 -0800885 priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
Yevgeny Petrilina19a8482012-04-23 02:18:33 +0000886 priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
887 priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000888 en_dbg(INTR, priv, "Default coalesing params for mtu:%d - "
889 "rx_frames:%d rx_usecs:%d\n",
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700890 priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
891
892 /* Setup cq moderation params */
893 for (i = 0; i < priv->rx_ring_num; i++) {
894 cq = &priv->rx_cq[i];
895 cq->moder_cnt = priv->rx_frames;
896 cq->moder_time = priv->rx_usecs;
Alexander Guller6b4d8d92011-10-09 05:38:23 +0000897 priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
898 priv->last_moder_packets[i] = 0;
899 priv->last_moder_bytes[i] = 0;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700900 }
901
902 for (i = 0; i < priv->tx_ring_num; i++) {
903 cq = &priv->tx_cq[i];
Yevgeny Petrilina19a8482012-04-23 02:18:33 +0000904 cq->moder_cnt = priv->tx_frames;
905 cq->moder_time = priv->tx_usecs;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700906 }
907
908 /* Reset auto-moderation params */
909 priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
910 priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
911 priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
912 priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
913 priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
Yevgeny Petrilin60b9f9e2008-12-25 18:19:47 -0800914 priv->adaptive_rx_coal = 1;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700915 priv->last_moder_jiffies = 0;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700916 priv->last_moder_tx_packets = 0;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700917}
918
919static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
920{
921 unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700922 struct mlx4_en_cq *cq;
923 unsigned long packets;
924 unsigned long rate;
925 unsigned long avg_pkt_size;
926 unsigned long rx_packets;
927 unsigned long rx_bytes;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700928 unsigned long rx_pkt_diff;
929 int moder_time;
Alexander Guller6b4d8d92011-10-09 05:38:23 +0000930 int ring, err;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700931
932 if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
933 return;
934
Alexander Guller6b4d8d92011-10-09 05:38:23 +0000935 for (ring = 0; ring < priv->rx_ring_num; ring++) {
936 spin_lock_bh(&priv->stats_lock);
937 rx_packets = priv->rx_ring[ring].packets;
938 rx_bytes = priv->rx_ring[ring].bytes;
939 spin_unlock_bh(&priv->stats_lock);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700940
Alexander Guller6b4d8d92011-10-09 05:38:23 +0000941 rx_pkt_diff = ((unsigned long) (rx_packets -
942 priv->last_moder_packets[ring]));
943 packets = rx_pkt_diff;
944 rate = packets * HZ / period;
945 avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
946 priv->last_moder_bytes[ring])) / packets : 0;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700947
Alexander Guller6b4d8d92011-10-09 05:38:23 +0000948 /* Apply auto-moderation only when packet rate
949 * exceeds a rate that it matters */
950 if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
951 avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700952 if (rate < priv->pkt_rate_low)
953 moder_time = priv->rx_usecs_low;
954 else if (rate > priv->pkt_rate_high)
955 moder_time = priv->rx_usecs_high;
956 else
957 moder_time = (rate - priv->pkt_rate_low) *
958 (priv->rx_usecs_high - priv->rx_usecs_low) /
959 (priv->pkt_rate_high - priv->pkt_rate_low) +
960 priv->rx_usecs_low;
Alexander Guller6b4d8d92011-10-09 05:38:23 +0000961 } else {
962 moder_time = priv->rx_usecs_low;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700963 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700964
Alexander Guller6b4d8d92011-10-09 05:38:23 +0000965 if (moder_time != priv->last_moder_time[ring]) {
966 priv->last_moder_time[ring] = moder_time;
967 cq = &priv->rx_cq[ring];
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700968 cq->moder_time = moder_time;
969 err = mlx4_en_set_cq_moder(priv, cq);
Alexander Guller6b4d8d92011-10-09 05:38:23 +0000970 if (err)
971 en_err(priv, "Failed modifying moderation "
972 "for cq:%d\n", ring);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700973 }
Alexander Guller6b4d8d92011-10-09 05:38:23 +0000974 priv->last_moder_packets[ring] = rx_packets;
975 priv->last_moder_bytes[ring] = rx_bytes;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700976 }
977
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700978 priv->last_moder_jiffies = jiffies;
979}
980
981static void mlx4_en_do_get_stats(struct work_struct *work)
982{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700983 struct delayed_work *delay = to_delayed_work(work);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700984 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
985 stats_task);
986 struct mlx4_en_dev *mdev = priv->mdev;
987 int err;
988
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700989 mutex_lock(&mdev->state_lock);
990 if (mdev->device_up) {
Eugenia Emantayev2d518372013-01-24 01:54:14 +0000991 err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
992 if (err)
993 en_dbg(HW, priv, "Could not update stats\n");
994
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700995 if (priv->port_up)
996 mlx4_en_auto_moderation(priv);
997
998 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
999 }
Yevgeny Petrilind7e1a482010-08-24 03:46:38 +00001000 if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
1001 queue_work(mdev->workqueue, &priv->mac_task);
1002 mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
1003 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001004 mutex_unlock(&mdev->state_lock);
1005}
1006
1007static void mlx4_en_linkstate(struct work_struct *work)
1008{
1009 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1010 linkstate_task);
1011 struct mlx4_en_dev *mdev = priv->mdev;
1012 int linkstate = priv->link_state;
1013
1014 mutex_lock(&mdev->state_lock);
1015 /* If observable port state changed set carrier state and
1016 * report to system log */
1017 if (priv->last_link_state != linkstate) {
1018 if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
Yevgeny Petriline5cc44b2010-08-24 03:46:01 +00001019 en_info(priv, "Link Down\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001020 netif_carrier_off(priv->dev);
1021 } else {
Yevgeny Petriline5cc44b2010-08-24 03:46:01 +00001022 en_info(priv, "Link Up\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001023 netif_carrier_on(priv->dev);
1024 }
1025 }
1026 priv->last_link_state = linkstate;
1027 mutex_unlock(&mdev->state_lock);
1028}
1029
1030
Yevgeny Petrilin18cc42a2008-12-29 18:39:20 -08001031int mlx4_en_start_port(struct net_device *dev)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001032{
1033 struct mlx4_en_priv *priv = netdev_priv(dev);
1034 struct mlx4_en_dev *mdev = priv->mdev;
1035 struct mlx4_en_cq *cq;
1036 struct mlx4_en_tx_ring *tx_ring;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001037 int rx_index = 0;
1038 int tx_index = 0;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001039 int err = 0;
1040 int i;
1041 int j;
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001042 u8 mc_list[16] = {0};
Yan Burman6bbb6d92013-02-07 02:25:20 +00001043 u64 mac = mlx4_en_mac_to_u64(dev->dev_addr);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001044
1045 if (priv->port_up) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001046 en_dbg(DRV, priv, "start port called while port already up\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001047 return 0;
1048 }
1049
Yevgeny Petrilin6d199932012-07-05 04:03:43 +00001050 INIT_LIST_HEAD(&priv->mc_list);
1051 INIT_LIST_HEAD(&priv->curr_list);
Hadar Hen Zion0d256c02013-01-30 23:07:08 +00001052 INIT_LIST_HEAD(&priv->ethtool_list);
1053 memset(&priv->ethtool_rules[0], 0,
1054 sizeof(struct ethtool_flow_id) * MAX_NUM_OF_FS_RULES);
Yevgeny Petrilin6d199932012-07-05 04:03:43 +00001055
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001056 /* Calculate Rx buf size */
1057 dev->mtu = min(dev->mtu, priv->max_mtu);
1058 mlx4_en_calc_rx_buf(dev);
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001059 en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +00001060
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001061 /* Configure rx cq's and rings */
Yevgeny Petrilin38aab072009-05-24 03:17:11 +00001062 err = mlx4_en_activate_rx_rings(priv);
1063 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001064 en_err(priv, "Failed to activate RX rings\n");
Yevgeny Petrilin38aab072009-05-24 03:17:11 +00001065 return err;
1066 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001067 for (i = 0; i < priv->rx_ring_num; i++) {
1068 cq = &priv->rx_cq[i];
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001069
Alexander Guller76532d02011-10-09 05:26:31 +00001070 err = mlx4_en_activate_cq(priv, cq, i);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001071 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001072 en_err(priv, "Failed activating Rx CQ\n");
Yevgeny Petrilina4233302009-04-26 20:41:34 +00001073 goto cq_err;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001074 }
1075 for (j = 0; j < cq->size; j++)
1076 cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
1077 err = mlx4_en_set_cq_moder(priv, cq);
1078 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001079 en_err(priv, "Failed setting cq moderation parameters");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001080 mlx4_en_deactivate_cq(priv, cq);
1081 goto cq_err;
1082 }
1083 mlx4_en_arm_cq(priv, cq);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +00001084 priv->rx_ring[i].cqn = cq->mcq.cqn;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001085 ++rx_index;
1086 }
1087
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001088 /* Set qp number */
1089 en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
1090 err = mlx4_get_eth_qp(mdev->dev, priv->port,
Yan Burman6bbb6d92013-02-07 02:25:20 +00001091 mac, &priv->base_qpn);
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001092 if (err) {
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001093 en_err(priv, "Failed getting eth qp\n");
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001094 goto cq_err;
1095 }
1096 mdev->mac_removed[priv->port] = 0;
1097
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001098 err = mlx4_en_config_rss_steer(priv);
1099 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001100 en_err(priv, "Failed configuring rss steering\n");
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001101 goto mac_err;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001102 }
1103
Hadar Hen Zioncabdc8ee2012-07-05 04:03:50 +00001104 err = mlx4_en_create_drop_qp(priv);
1105 if (err)
1106 goto rss_err;
1107
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001108 /* Configure tx cq's and rings */
1109 for (i = 0; i < priv->tx_ring_num; i++) {
1110 /* Configure cq */
1111 cq = &priv->tx_cq[i];
Alexander Guller76532d02011-10-09 05:26:31 +00001112 err = mlx4_en_activate_cq(priv, cq, i);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001113 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001114 en_err(priv, "Failed allocating Tx CQ\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001115 goto tx_err;
1116 }
1117 err = mlx4_en_set_cq_moder(priv, cq);
1118 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001119 en_err(priv, "Failed setting cq moderation parameters");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001120 mlx4_en_deactivate_cq(priv, cq);
1121 goto tx_err;
1122 }
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001123 en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001124 cq->buf->wqe_index = cpu_to_be16(0xffff);
1125
1126 /* Configure ring */
1127 tx_ring = &priv->tx_ring[i];
Amir Vadai0e98b522012-04-04 21:33:24 +00001128 err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
Amir Vadaid3179662012-12-02 03:49:23 +00001129 i / priv->num_tx_rings_p_up);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001130 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001131 en_err(priv, "Failed allocating Tx ring\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001132 mlx4_en_deactivate_cq(priv, cq);
1133 goto tx_err;
1134 }
Yevgeny Petrilin5b263f52012-04-23 02:18:50 +00001135 tx_ring->tx_queue = netdev_get_tx_queue(dev, i);
Yevgeny Petriline22979d2012-04-23 02:18:39 +00001136
1137 /* Arm CQ for TX completions */
1138 mlx4_en_arm_cq(priv, cq);
1139
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001140 /* Set initial ownership of all Tx TXBBs to SW (1) */
1141 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
1142 *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
1143 ++tx_index;
1144 }
1145
1146 /* Configure port */
1147 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
1148 priv->rx_skb_size + ETH_FCS_LEN,
Yevgeny Petrilind53b93f2008-11-05 04:48:36 +00001149 priv->prof->tx_pause,
1150 priv->prof->tx_ppp,
1151 priv->prof->rx_pause,
1152 priv->prof->rx_ppp);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001153 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001154 en_err(priv, "Failed setting port general configurations "
1155 "for port %d, with error %d\n", priv->port, err);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001156 goto tx_err;
1157 }
1158 /* Set default qp number */
1159 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
1160 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001161 en_err(priv, "Failed setting default qp numbers\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001162 goto tx_err;
1163 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001164
1165 /* Init port */
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001166 en_dbg(HW, priv, "Initializing port\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001167 err = mlx4_INIT_PORT(mdev->dev, priv->port);
1168 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001169 en_err(priv, "Failed Initializing port\n");
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001170 goto tx_err;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001171 }
1172
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001173 /* Attach rx QP to bradcast address */
1174 memset(&mc_list[10], 0xff, ETH_ALEN);
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001175 mc_list[5] = priv->port; /* needed for B0 steering support */
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001176 if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001177 priv->port, 0, MLX4_PROT_ETH,
1178 &priv->broadcast_id))
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001179 mlx4_warn(mdev, "Failed Attaching Broadcast\n");
1180
Herbert Xub5845f92011-03-27 01:01:26 +00001181 /* Must redo promiscuous mode setup. */
1182 priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
1183
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001184 /* Schedule multicast task to populate multicast list */
1185 queue_work(mdev->workqueue, &priv->mcast_task);
1186
Eugenia Emantayev93ece0c2012-01-19 09:45:05 +00001187 mlx4_set_stats_bitmap(mdev->dev, &priv->stats_bitmap);
1188
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001189 priv->port_up = true;
Yevgeny Petrilina11faac2009-06-20 22:15:46 +00001190 netif_tx_start_all_queues(dev);
Amir Vadai3484aac2013-01-30 23:07:11 +00001191 netif_device_attach(dev);
1192
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001193 return 0;
1194
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001195tx_err:
1196 while (tx_index--) {
1197 mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[tx_index]);
1198 mlx4_en_deactivate_cq(priv, &priv->tx_cq[tx_index]);
1199 }
Hadar Hen Zioncabdc8ee2012-07-05 04:03:50 +00001200 mlx4_en_destroy_drop_qp(priv);
1201rss_err:
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001202 mlx4_en_release_rss_steer(priv);
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001203mac_err:
Yan Burman6bbb6d92013-02-07 02:25:20 +00001204 mlx4_put_eth_qp(mdev->dev, priv->port, mac, priv->base_qpn);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001205cq_err:
1206 while (rx_index--)
1207 mlx4_en_deactivate_cq(priv, &priv->rx_cq[rx_index]);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +00001208 for (i = 0; i < priv->rx_ring_num; i++)
1209 mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001210
1211 return err; /* need to close devices */
1212}
1213
1214
Amir Vadai3484aac2013-01-30 23:07:11 +00001215void mlx4_en_stop_port(struct net_device *dev, int detach)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001216{
1217 struct mlx4_en_priv *priv = netdev_priv(dev);
1218 struct mlx4_en_dev *mdev = priv->mdev;
Yevgeny Petrilin6d199932012-07-05 04:03:43 +00001219 struct mlx4_en_mc_list *mclist, *tmp;
Hadar Hen Zion0d256c02013-01-30 23:07:08 +00001220 struct ethtool_flow_id *flow, *tmp_flow;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001221 int i;
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001222 u8 mc_list[16] = {0};
Yan Burman6bbb6d92013-02-07 02:25:20 +00001223 u64 mac = mlx4_en_mac_to_u64(dev->dev_addr);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001224
1225 if (!priv->port_up) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001226 en_dbg(DRV, priv, "stop port called while port already down\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001227 return;
1228 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001229
1230 /* Synchronize with tx routine */
1231 netif_tx_lock_bh(dev);
Amir Vadai3484aac2013-01-30 23:07:11 +00001232 if (detach)
1233 netif_device_detach(dev);
Yevgeny Petrilin3c05f5e2009-06-20 22:15:52 +00001234 netif_tx_stop_all_queues(dev);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001235 netif_tx_unlock_bh(dev);
1236
Amir Vadai3484aac2013-01-30 23:07:11 +00001237 netif_tx_disable(dev);
1238
Yevgeny Petrilin7c287382010-08-24 03:45:45 +00001239 /* Set port as not active */
Yevgeny Petrilin3c05f5e2009-06-20 22:15:52 +00001240 priv->port_up = false;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001241
Aviad Yehezkeldb0e7cb2013-01-24 01:54:15 +00001242 /* Promsicuous mode */
1243 if (mdev->dev->caps.steering_mode ==
1244 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1245 priv->flags &= ~(MLX4_EN_FLAG_PROMISC |
1246 MLX4_EN_FLAG_MC_PROMISC);
1247 mlx4_flow_steer_promisc_remove(mdev->dev,
1248 priv->port,
1249 MLX4_FS_PROMISC_UPLINK);
1250 mlx4_flow_steer_promisc_remove(mdev->dev,
1251 priv->port,
1252 MLX4_FS_PROMISC_ALL_MULTI);
1253 } else if (priv->flags & MLX4_EN_FLAG_PROMISC) {
1254 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
1255
1256 /* Disable promiscouos mode */
1257 mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
1258 priv->port);
1259
1260 /* Disable Multicast promisc */
1261 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1262 mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
1263 priv->port);
1264 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1265 }
1266 }
1267
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001268 /* Detach All multicasts */
1269 memset(&mc_list[10], 0xff, ETH_ALEN);
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001270 mc_list[5] = priv->port; /* needed for B0 steering support */
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001271 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001272 MLX4_PROT_ETH, priv->broadcast_id);
Yevgeny Petrilin6d199932012-07-05 04:03:43 +00001273 list_for_each_entry(mclist, &priv->curr_list, list) {
1274 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001275 mc_list[5] = priv->port;
1276 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001277 mc_list, MLX4_PROT_ETH, mclist->reg_id);
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001278 }
1279 mlx4_en_clear_list(dev);
Yevgeny Petrilin6d199932012-07-05 04:03:43 +00001280 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1281 list_del(&mclist->list);
1282 kfree(mclist);
1283 }
1284
Yevgeny Petrilin16792002011-03-22 22:38:31 +00001285 /* Flush multicast filter */
1286 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
1287
Hadar Hen Zioncabdc8ee2012-07-05 04:03:50 +00001288 mlx4_en_destroy_drop_qp(priv);
1289
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001290 /* Free TX Rings */
1291 for (i = 0; i < priv->tx_ring_num; i++) {
1292 mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[i]);
1293 mlx4_en_deactivate_cq(priv, &priv->tx_cq[i]);
1294 }
1295 msleep(10);
1296
1297 for (i = 0; i < priv->tx_ring_num; i++)
1298 mlx4_en_free_tx_buf(dev, &priv->tx_ring[i]);
1299
1300 /* Free RSS qps */
1301 mlx4_en_release_rss_steer(priv);
1302
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001303 /* Unregister Mac address for the port */
Yan Burman6bbb6d92013-02-07 02:25:20 +00001304 mlx4_put_eth_qp(mdev->dev, priv->port, mac, priv->base_qpn);
Matan Barak955154f2013-01-30 23:07:10 +00001305 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAGS2_REASSIGN_MAC_EN))
1306 mdev->mac_removed[priv->port] = 1;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001307
Hadar Hen Zion0d256c02013-01-30 23:07:08 +00001308 /* Remove flow steering rules for the port*/
1309 if (mdev->dev->caps.steering_mode ==
1310 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1311 ASSERT_RTNL();
1312 list_for_each_entry_safe(flow, tmp_flow,
1313 &priv->ethtool_list, list) {
1314 mlx4_flow_detach(mdev->dev, flow->id);
1315 list_del(&flow->list);
1316 }
1317 }
1318
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001319 /* Free RX Rings */
1320 for (i = 0; i < priv->rx_ring_num; i++) {
1321 mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
1322 while (test_bit(NAPI_STATE_SCHED, &priv->rx_cq[i].napi.state))
1323 msleep(1);
1324 mlx4_en_deactivate_cq(priv, &priv->rx_cq[i]);
1325 }
Yevgeny Petrilin7c287382010-08-24 03:45:45 +00001326
1327 /* close port*/
1328 mlx4_CLOSE_PORT(mdev->dev, priv->port);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001329}
1330
1331static void mlx4_en_restart(struct work_struct *work)
1332{
1333 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1334 watchdog_task);
1335 struct mlx4_en_dev *mdev = priv->mdev;
1336 struct net_device *dev = priv->dev;
Yevgeny Petrilin5b263f52012-04-23 02:18:50 +00001337 int i;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001338
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001339 en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
Yevgeny Petrilin1e338db2009-04-20 04:26:05 +00001340
1341 mutex_lock(&mdev->state_lock);
1342 if (priv->port_up) {
Amir Vadai3484aac2013-01-30 23:07:11 +00001343 mlx4_en_stop_port(dev, 1);
Yevgeny Petrilin5b263f52012-04-23 02:18:50 +00001344 for (i = 0; i < priv->tx_ring_num; i++)
1345 netdev_tx_reset_queue(priv->tx_ring[i].tx_queue);
Yevgeny Petrilin1e338db2009-04-20 04:26:05 +00001346 if (mlx4_en_start_port(dev))
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001347 en_err(priv, "Failed restarting port %d\n", priv->port);
Yevgeny Petrilin1e338db2009-04-20 04:26:05 +00001348 }
1349 mutex_unlock(&mdev->state_lock);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001350}
1351
Eugenia Emantayevb477ba62012-01-19 09:42:37 +00001352static void mlx4_en_clear_stats(struct net_device *dev)
1353{
1354 struct mlx4_en_priv *priv = netdev_priv(dev);
1355 struct mlx4_en_dev *mdev = priv->mdev;
1356 int i;
1357
1358 if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
1359 en_dbg(HW, priv, "Failed dumping statistics\n");
1360
1361 memset(&priv->stats, 0, sizeof(priv->stats));
1362 memset(&priv->pstats, 0, sizeof(priv->pstats));
1363 memset(&priv->pkstats, 0, sizeof(priv->pkstats));
1364 memset(&priv->port_stats, 0, sizeof(priv->port_stats));
1365
1366 for (i = 0; i < priv->tx_ring_num; i++) {
1367 priv->tx_ring[i].bytes = 0;
1368 priv->tx_ring[i].packets = 0;
1369 priv->tx_ring[i].tx_csum = 0;
1370 }
1371 for (i = 0; i < priv->rx_ring_num; i++) {
1372 priv->rx_ring[i].bytes = 0;
1373 priv->rx_ring[i].packets = 0;
1374 priv->rx_ring[i].csum_ok = 0;
1375 priv->rx_ring[i].csum_none = 0;
1376 }
1377}
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001378
1379static int mlx4_en_open(struct net_device *dev)
1380{
1381 struct mlx4_en_priv *priv = netdev_priv(dev);
1382 struct mlx4_en_dev *mdev = priv->mdev;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001383 int err = 0;
1384
1385 mutex_lock(&mdev->state_lock);
1386
1387 if (!mdev->device_up) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001388 en_err(priv, "Cannot open - device down/disabled\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001389 err = -EBUSY;
1390 goto out;
1391 }
1392
Eugenia Emantayevb477ba62012-01-19 09:42:37 +00001393 /* Reset HW statistics and SW counters */
1394 mlx4_en_clear_stats(dev);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001395
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001396 err = mlx4_en_start_port(dev);
1397 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001398 en_err(priv, "Failed starting port:%d\n", priv->port);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001399
1400out:
1401 mutex_unlock(&mdev->state_lock);
1402 return err;
1403}
1404
1405
1406static int mlx4_en_close(struct net_device *dev)
1407{
1408 struct mlx4_en_priv *priv = netdev_priv(dev);
1409 struct mlx4_en_dev *mdev = priv->mdev;
1410
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001411 en_dbg(IFDOWN, priv, "Close port called\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001412
1413 mutex_lock(&mdev->state_lock);
1414
Amir Vadai3484aac2013-01-30 23:07:11 +00001415 mlx4_en_stop_port(dev, 0);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001416 netif_carrier_off(dev);
1417
1418 mutex_unlock(&mdev->state_lock);
1419 return 0;
1420}
1421
Alexander Gullerfe0af032011-10-09 05:26:46 +00001422void mlx4_en_free_resources(struct mlx4_en_priv *priv)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001423{
1424 int i;
1425
Amir Vadai1eb8c692012-07-18 22:33:52 +00001426#ifdef CONFIG_RFS_ACCEL
1427 free_irq_cpu_rmap(priv->dev->rx_cpu_rmap);
1428 priv->dev->rx_cpu_rmap = NULL;
1429#endif
1430
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001431 for (i = 0; i < priv->tx_ring_num; i++) {
1432 if (priv->tx_ring[i].tx_info)
1433 mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
1434 if (priv->tx_cq[i].buf)
Alexander Gullerfe0af032011-10-09 05:26:46 +00001435 mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001436 }
1437
1438 for (i = 0; i < priv->rx_ring_num; i++) {
1439 if (priv->rx_ring[i].rx_info)
Thadeu Lima de Souza Cascardo68355f72012-02-06 08:39:49 +00001440 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
1441 priv->prof->rx_ring_size, priv->stride);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001442 if (priv->rx_cq[i].buf)
Alexander Gullerfe0af032011-10-09 05:26:46 +00001443 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001444 }
Yevgeny Petrilin044ca2a2012-06-25 00:24:13 +00001445
1446 if (priv->base_tx_qpn) {
1447 mlx4_qp_release_range(priv->mdev->dev, priv->base_tx_qpn, priv->tx_ring_num);
1448 priv->base_tx_qpn = 0;
1449 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001450}
1451
Yevgeny Petrilin18cc42a2008-12-29 18:39:20 -08001452int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001453{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001454 struct mlx4_en_port_profile *prof = priv->prof;
1455 int i;
Yevgeny Petrilin044ca2a2012-06-25 00:24:13 +00001456 int err;
Yevgeny Petrilin87a5c382011-03-22 22:38:52 +00001457
Yevgeny Petrilin044ca2a2012-06-25 00:24:13 +00001458 err = mlx4_qp_reserve_range(priv->mdev->dev, priv->tx_ring_num, 256, &priv->base_tx_qpn);
Yevgeny Petrilin87a5c382011-03-22 22:38:52 +00001459 if (err) {
1460 en_err(priv, "failed reserving range for TX rings\n");
1461 return err;
1462 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001463
1464 /* Create tx Rings */
1465 for (i = 0; i < priv->tx_ring_num; i++) {
1466 if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
1467 prof->tx_ring_size, i, TX))
1468 goto err;
1469
Yevgeny Petrilin044ca2a2012-06-25 00:24:13 +00001470 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i], priv->base_tx_qpn + i,
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001471 prof->tx_ring_size, TXBB_SIZE))
1472 goto err;
1473 }
1474
1475 /* Create rx Rings */
1476 for (i = 0; i < priv->rx_ring_num; i++) {
1477 if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
1478 prof->rx_ring_size, i, RX))
1479 goto err;
1480
1481 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
1482 prof->rx_ring_size, priv->stride))
1483 goto err;
1484 }
1485
Amir Vadai1eb8c692012-07-18 22:33:52 +00001486#ifdef CONFIG_RFS_ACCEL
1487 priv->dev->rx_cpu_rmap = alloc_irq_cpu_rmap(priv->rx_ring_num);
1488 if (!priv->dev->rx_cpu_rmap)
1489 goto err;
Amir Vadai1eb8c692012-07-18 22:33:52 +00001490#endif
1491
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001492 return 0;
1493
1494err:
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001495 en_err(priv, "Failed to allocate NIC resources\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001496 return -ENOMEM;
1497}
1498
1499
1500void mlx4_en_destroy_netdev(struct net_device *dev)
1501{
1502 struct mlx4_en_priv *priv = netdev_priv(dev);
1503 struct mlx4_en_dev *mdev = priv->mdev;
1504
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001505 en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001506
1507 /* Unregister device - this will close the port if it was up */
1508 if (priv->registered)
1509 unregister_netdev(dev);
1510
1511 if (priv->allocated)
1512 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
1513
1514 cancel_delayed_work(&priv->stats_task);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001515 /* flush any pending task for this netdev */
1516 flush_workqueue(mdev->workqueue);
1517
1518 /* Detach the netdev so tasks would not attempt to access it */
1519 mutex_lock(&mdev->state_lock);
1520 mdev->pndev[priv->port] = NULL;
1521 mutex_unlock(&mdev->state_lock);
1522
Alexander Gullerfe0af032011-10-09 05:26:46 +00001523 mlx4_en_free_resources(priv);
Amir Vadai564c2742012-04-04 21:33:26 +00001524
Amir Vadaibc6a4742012-05-17 00:58:10 +00001525 kfree(priv->tx_ring);
1526 kfree(priv->tx_cq);
1527
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001528 free_netdev(dev);
1529}
1530
1531static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
1532{
1533 struct mlx4_en_priv *priv = netdev_priv(dev);
1534 struct mlx4_en_dev *mdev = priv->mdev;
1535 int err = 0;
1536
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001537 en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001538 dev->mtu, new_mtu);
1539
1540 if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001541 en_err(priv, "Bad MTU size:%d.\n", new_mtu);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001542 return -EPERM;
1543 }
1544 dev->mtu = new_mtu;
1545
1546 if (netif_running(dev)) {
1547 mutex_lock(&mdev->state_lock);
1548 if (!mdev->device_up) {
1549 /* NIC is probably restarting - let watchdog task reset
1550 * the port */
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001551 en_dbg(DRV, priv, "Change MTU called with card down!?\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001552 } else {
Amir Vadai3484aac2013-01-30 23:07:11 +00001553 mlx4_en_stop_port(dev, 1);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001554 err = mlx4_en_start_port(dev);
1555 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001556 en_err(priv, "Failed restarting port:%d\n",
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001557 priv->port);
1558 queue_work(mdev->workqueue, &priv->watchdog_task);
1559 }
1560 }
1561 mutex_unlock(&mdev->state_lock);
1562 }
1563 return 0;
1564}
1565
Amir Vadai60d6fe92011-11-26 19:55:19 +00001566static int mlx4_en_set_features(struct net_device *netdev,
1567 netdev_features_t features)
1568{
1569 struct mlx4_en_priv *priv = netdev_priv(netdev);
1570
1571 if (features & NETIF_F_LOOPBACK)
1572 priv->ctrl_flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
1573 else
1574 priv->ctrl_flags &=
1575 cpu_to_be32(~MLX4_WQE_CTRL_FORCE_LOOPBACK);
1576
Yan Burman79aeacc2013-02-07 02:25:19 +00001577 mlx4_en_update_loopback_state(netdev, features);
1578
Amir Vadai60d6fe92011-11-26 19:55:19 +00001579 return 0;
1580
1581}
1582
Stephen Hemminger3addc562008-11-21 17:30:58 -08001583static const struct net_device_ops mlx4_netdev_ops = {
1584 .ndo_open = mlx4_en_open,
1585 .ndo_stop = mlx4_en_close,
1586 .ndo_start_xmit = mlx4_en_xmit,
Yevgeny Petrilinf813cad2009-06-01 23:24:07 +00001587 .ndo_select_queue = mlx4_en_select_queue,
Stephen Hemminger3addc562008-11-21 17:30:58 -08001588 .ndo_get_stats = mlx4_en_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001589 .ndo_set_rx_mode = mlx4_en_set_multicast,
Stephen Hemminger3addc562008-11-21 17:30:58 -08001590 .ndo_set_mac_address = mlx4_en_set_mac,
Stephen Hemminger52255bb2009-01-09 10:45:37 +00001591 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger3addc562008-11-21 17:30:58 -08001592 .ndo_change_mtu = mlx4_en_change_mtu,
1593 .ndo_tx_timeout = mlx4_en_tx_timeout,
Stephen Hemminger3addc562008-11-21 17:30:58 -08001594 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
1595 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
1596#ifdef CONFIG_NET_POLL_CONTROLLER
1597 .ndo_poll_controller = mlx4_en_netpoll,
1598#endif
Amir Vadai60d6fe92011-11-26 19:55:19 +00001599 .ndo_set_features = mlx4_en_set_features,
Amir Vadai897d7842012-04-04 21:33:27 +00001600 .ndo_setup_tc = mlx4_en_setup_tc,
Amir Vadai1eb8c692012-07-18 22:33:52 +00001601#ifdef CONFIG_RFS_ACCEL
1602 .ndo_rx_flow_steer = mlx4_en_filter_rfs,
1603#endif
Stephen Hemminger3addc562008-11-21 17:30:58 -08001604};
1605
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001606int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
1607 struct mlx4_en_port_profile *prof)
1608{
1609 struct net_device *dev;
1610 struct mlx4_en_priv *priv;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001611 int err;
1612
Tom Herbertf1593d22011-01-09 19:36:36 +00001613 dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
Amir Vadaid3179662012-12-02 03:49:23 +00001614 MAX_TX_RINGS, MAX_RX_RINGS);
Joe Perches41de8d42012-01-29 13:47:52 +00001615 if (dev == NULL)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001616 return -ENOMEM;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001617
Amir Vadaid3179662012-12-02 03:49:23 +00001618 netif_set_real_num_tx_queues(dev, prof->tx_ring_num);
1619 netif_set_real_num_rx_queues(dev, prof->rx_ring_num);
1620
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001621 SET_NETDEV_DEV(dev, &mdev->dev->pdev->dev);
Eli Cohen741a00b2010-05-26 19:56:24 +00001622 dev->dev_id = port - 1;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001623
1624 /*
1625 * Initialize driver private data
1626 */
1627
1628 priv = netdev_priv(dev);
1629 memset(priv, 0, sizeof(struct mlx4_en_priv));
1630 priv->dev = dev;
1631 priv->mdev = mdev;
Yevgeny Petrilinebf8c9a2012-03-06 04:03:34 +00001632 priv->ddev = &mdev->pdev->dev;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001633 priv->prof = prof;
1634 priv->port = port;
1635 priv->port_up = false;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001636 priv->flags = prof->flags;
Amir Vadai60d6fe92011-11-26 19:55:19 +00001637 priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
1638 MLX4_WQE_CTRL_SOLICITED);
Amir Vadaid3179662012-12-02 03:49:23 +00001639 priv->num_tx_rings_p_up = mdev->profile.num_tx_rings_p_up;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001640 priv->tx_ring_num = prof->tx_ring_num;
Amir Vadaid3179662012-12-02 03:49:23 +00001641
1642 priv->tx_ring = kzalloc(sizeof(struct mlx4_en_tx_ring) * MAX_TX_RINGS,
1643 GFP_KERNEL);
Amir Vadaibc6a4742012-05-17 00:58:10 +00001644 if (!priv->tx_ring) {
1645 err = -ENOMEM;
1646 goto out;
1647 }
Amir Vadaid3179662012-12-02 03:49:23 +00001648 priv->tx_cq = kzalloc(sizeof(struct mlx4_en_cq) * MAX_RX_RINGS,
1649 GFP_KERNEL);
Amir Vadaibc6a4742012-05-17 00:58:10 +00001650 if (!priv->tx_cq) {
1651 err = -ENOMEM;
1652 goto out;
1653 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001654 priv->rx_ring_num = prof->rx_ring_num;
Or Gerlitz08ff3232012-10-21 14:59:24 +00001655 priv->cqe_factor = (mdev->dev->caps.cqe_size == 64) ? 1 : 0;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001656 priv->mac_index = -1;
1657 priv->msg_enable = MLX4_EN_MSG_LEVEL;
1658 spin_lock_init(&priv->stats_lock);
1659 INIT_WORK(&priv->mcast_task, mlx4_en_do_set_multicast);
1660 INIT_WORK(&priv->mac_task, mlx4_en_do_set_mac);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001661 INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
1662 INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
1663 INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
Amir Vadai564c2742012-04-04 21:33:26 +00001664#ifdef CONFIG_MLX4_EN_DCB
1665 if (!mlx4_is_slave(priv->mdev->dev))
1666 dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
1667#endif
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001668
1669 /* Query for default mac and max mtu */
1670 priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
Yan Burman6bbb6d92013-02-07 02:25:20 +00001671
1672 /* Set default MAC */
1673 dev->addr_len = ETH_ALEN;
1674 mlx4_en_u64_to_mac(dev->dev_addr, mdev->dev->caps.def_mac[priv->port]);
1675 if (!is_valid_ether_addr(dev->dev_addr)) {
1676 en_err(priv, "Port: %d, invalid mac burned: %pM, quiting\n",
1677 priv->port, dev->dev_addr);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001678 err = -EINVAL;
1679 goto out;
1680 }
1681
Yan Burman6bbb6d92013-02-07 02:25:20 +00001682 memcpy(priv->prev_mac, dev->dev_addr, sizeof(priv->prev_mac));
1683
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001684 priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
1685 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
1686 err = mlx4_en_alloc_resources(priv);
1687 if (err)
1688 goto out;
1689
Amir Vadai78fb2de2013-01-24 01:54:19 +00001690#ifdef CONFIG_RFS_ACCEL
1691 INIT_LIST_HEAD(&priv->filters);
1692 spin_lock_init(&priv->filters_lock);
1693#endif
1694
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001695 /* Allocate page for receive rings */
1696 err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
1697 MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
1698 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001699 en_err(priv, "Failed to allocate page for rx qps\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001700 goto out;
1701 }
1702 priv->allocated = 1;
1703
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001704 /*
1705 * Initialize netdev entry points
1706 */
Stephen Hemminger3addc562008-11-21 17:30:58 -08001707 dev->netdev_ops = &mlx4_netdev_ops;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001708 dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
Ben Hutchings1eb63a22010-09-27 08:29:34 +00001709 netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
1710 netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
Stephen Hemminger3addc562008-11-21 17:30:58 -08001711
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001712 SET_ETHTOOL_OPS(dev, &mlx4_en_ethtool_ops);
1713
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001714 /*
1715 * Set driver features
1716 */
Michał Mirosławc8c64cf2011-04-15 04:50:49 +00001717 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1718 if (mdev->LSO_support)
1719 dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1720
1721 dev->vlan_features = dev->hw_features;
1722
Yevgeny Petrilinad861072011-10-18 01:51:24 +00001723 dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
Michał Mirosławc8c64cf2011-04-15 04:50:49 +00001724 dev->features = dev->hw_features | NETIF_F_HIGHDMA |
1725 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
1726 NETIF_F_HW_VLAN_FILTER;
Amir Vadai60d6fe92011-11-26 19:55:19 +00001727 dev->hw_features |= NETIF_F_LOOPBACK;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001728
Amir Vadai1eb8c692012-07-18 22:33:52 +00001729 if (mdev->dev->caps.steering_mode ==
1730 MLX4_STEERING_MODE_DEVICE_MANAGED)
1731 dev->hw_features |= NETIF_F_NTUPLE;
1732
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001733 mdev->pndev[port] = dev;
1734
1735 netif_carrier_off(dev);
1736 err = register_netdev(dev);
1737 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001738 en_err(priv, "Netdev registration failed for port %d\n", port);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001739 goto out;
1740 }
Alexander Guller42341442011-10-09 05:29:35 +00001741 priv->registered = 1;
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001742
1743 en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
1744 en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
1745
Yan Burman79aeacc2013-02-07 02:25:19 +00001746 mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
1747
Yevgeny Petrilin90822262011-03-22 22:37:41 +00001748 /* Configure port */
Yevgeny Petrilin5c8e9042012-06-25 00:24:11 +00001749 mlx4_en_calc_rx_buf(dev);
Yevgeny Petrilin90822262011-03-22 22:37:41 +00001750 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
Yevgeny Petrilin5c8e9042012-06-25 00:24:11 +00001751 priv->rx_skb_size + ETH_FCS_LEN,
1752 prof->tx_pause, prof->tx_ppp,
1753 prof->rx_pause, prof->rx_ppp);
Yevgeny Petrilin90822262011-03-22 22:37:41 +00001754 if (err) {
1755 en_err(priv, "Failed setting port general configurations "
1756 "for port %d, with error %d\n", priv->port, err);
1757 goto out;
1758 }
1759
1760 /* Init port */
1761 en_warn(priv, "Initializing port\n");
1762 err = mlx4_INIT_PORT(mdev->dev, priv->port);
1763 if (err) {
1764 en_err(priv, "Failed Initializing port\n");
1765 goto out;
1766 }
Yevgeny Petrilin39f17b42011-03-22 22:37:28 +00001767 mlx4_en_set_default_moderation(priv);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001768 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1769 return 0;
1770
1771out:
1772 mlx4_en_destroy_netdev(dev);
1773 return err;
1774}
1775