blob: 93f4abd990a923c715e98daa19b99662afbf71cb [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>
38
39#include <linux/mlx4/driver.h>
40#include <linux/mlx4/device.h>
41#include <linux/mlx4/cmd.h>
42#include <linux/mlx4/cq.h>
43
44#include "mlx4_en.h"
45#include "en_port.h"
46
47
48static void mlx4_en_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
49{
50 struct mlx4_en_priv *priv = netdev_priv(dev);
51 struct mlx4_en_dev *mdev = priv->mdev;
52 int err;
53
Yevgeny Petrilin453a6082009-06-01 20:27:13 +000054 en_dbg(HW, priv, "Registering VLAN group:%p\n", grp);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070055 priv->vlgrp = grp;
56
57 mutex_lock(&mdev->state_lock);
58 if (mdev->device_up && priv->port_up) {
59 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv->port, grp);
60 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +000061 en_err(priv, "Failed configuring VLAN filter\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070062 }
63 mutex_unlock(&mdev->state_lock);
64}
65
66static void mlx4_en_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
67{
68 struct mlx4_en_priv *priv = netdev_priv(dev);
69 struct mlx4_en_dev *mdev = priv->mdev;
70 int err;
71
72 if (!priv->vlgrp)
73 return;
74
Yevgeny Petrilin453a6082009-06-01 20:27:13 +000075 en_dbg(HW, priv, "adding VLAN:%d (vlgrp entry:%p)\n",
76 vid, vlan_group_get_device(priv->vlgrp, vid));
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070077
78 /* Add VID to port VLAN filter */
79 mutex_lock(&mdev->state_lock);
80 if (mdev->device_up && priv->port_up) {
81 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv->port, priv->vlgrp);
82 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +000083 en_err(priv, "Failed configuring VLAN filter\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070084 }
85 mutex_unlock(&mdev->state_lock);
86}
87
88static void mlx4_en_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
89{
90 struct mlx4_en_priv *priv = netdev_priv(dev);
91 struct mlx4_en_dev *mdev = priv->mdev;
92 int err;
93
94 if (!priv->vlgrp)
95 return;
96
Yevgeny Petrilin453a6082009-06-01 20:27:13 +000097 en_dbg(HW, priv, "Killing VID:%d (vlgrp:%p vlgrp entry:%p)\n",
98 vid, priv->vlgrp, vlan_group_get_device(priv->vlgrp, vid));
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -070099 vlan_group_set_device(priv->vlgrp, vid, NULL);
100
101 /* Remove VID from port VLAN filter */
102 mutex_lock(&mdev->state_lock);
103 if (mdev->device_up && priv->port_up) {
104 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv->port, priv->vlgrp);
105 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000106 en_err(priv, "Failed configuring VLAN filter\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700107 }
108 mutex_unlock(&mdev->state_lock);
109}
110
111static u64 mlx4_en_mac_to_u64(u8 *addr)
112{
113 u64 mac = 0;
114 int i;
115
116 for (i = 0; i < ETH_ALEN; i++) {
117 mac <<= 8;
118 mac |= addr[i];
119 }
120 return mac;
121}
122
123static int mlx4_en_set_mac(struct net_device *dev, void *addr)
124{
125 struct mlx4_en_priv *priv = netdev_priv(dev);
126 struct mlx4_en_dev *mdev = priv->mdev;
127 struct sockaddr *saddr = addr;
128
129 if (!is_valid_ether_addr(saddr->sa_data))
130 return -EADDRNOTAVAIL;
131
132 memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
133 priv->mac = mlx4_en_mac_to_u64(dev->dev_addr);
134 queue_work(mdev->workqueue, &priv->mac_task);
135 return 0;
136}
137
138static void mlx4_en_do_set_mac(struct work_struct *work)
139{
140 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
141 mac_task);
142 struct mlx4_en_dev *mdev = priv->mdev;
143 int err = 0;
144
145 mutex_lock(&mdev->state_lock);
146 if (priv->port_up) {
147 /* Remove old MAC and insert the new one */
148 mlx4_unregister_mac(mdev->dev, priv->port, priv->mac_index);
149 err = mlx4_register_mac(mdev->dev, priv->port,
150 priv->mac, &priv->mac_index);
151 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000152 en_err(priv, "Failed changing HW MAC address\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700153 } else
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000154 en_dbg(HW, priv, "Port is down while "
155 "registering mac, exiting...\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700156
157 mutex_unlock(&mdev->state_lock);
158}
159
160static void mlx4_en_clear_list(struct net_device *dev)
161{
162 struct mlx4_en_priv *priv = netdev_priv(dev);
163 struct dev_mc_list *plist = priv->mc_list;
164 struct dev_mc_list *next;
165
166 while (plist) {
167 next = plist->next;
168 kfree(plist);
169 plist = next;
170 }
171 priv->mc_list = NULL;
172}
173
174static void mlx4_en_cache_mclist(struct net_device *dev)
175{
176 struct mlx4_en_priv *priv = netdev_priv(dev);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700177 struct dev_mc_list *mclist;
178 struct dev_mc_list *tmp;
179 struct dev_mc_list *plist = NULL;
180
181 for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
182 tmp = kmalloc(sizeof(struct dev_mc_list), GFP_ATOMIC);
183 if (!tmp) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000184 en_err(priv, "failed to allocate multicast list\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700185 mlx4_en_clear_list(dev);
186 return;
187 }
188 memcpy(tmp, mclist, sizeof(struct dev_mc_list));
189 tmp->next = NULL;
190 if (plist)
191 plist->next = tmp;
192 else
193 priv->mc_list = tmp;
194 plist = tmp;
195 }
196}
197
198
199static void mlx4_en_set_multicast(struct net_device *dev)
200{
201 struct mlx4_en_priv *priv = netdev_priv(dev);
202
203 if (!priv->port_up)
204 return;
205
206 queue_work(priv->mdev->workqueue, &priv->mcast_task);
207}
208
209static void mlx4_en_do_set_multicast(struct work_struct *work)
210{
211 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
212 mcast_task);
213 struct mlx4_en_dev *mdev = priv->mdev;
214 struct net_device *dev = priv->dev;
215 struct dev_mc_list *mclist;
216 u64 mcast_addr = 0;
217 int err;
218
219 mutex_lock(&mdev->state_lock);
220 if (!mdev->device_up) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000221 en_dbg(HW, priv, "Card is not up, "
222 "ignoring multicast change.\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700223 goto out;
224 }
225 if (!priv->port_up) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000226 en_dbg(HW, priv, "Port is down, "
227 "ignoring multicast change.\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700228 goto out;
229 }
230
231 /*
232 * Promsicuous mode: disable all filters
233 */
234
235 if (dev->flags & IFF_PROMISC) {
236 if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
237 if (netif_msg_rx_status(priv))
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000238 en_warn(priv, "Entering promiscuous mode\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700239 priv->flags |= MLX4_EN_FLAG_PROMISC;
240
241 /* Enable promiscouos mode */
242 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port,
243 priv->base_qpn, 1);
244 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000245 en_err(priv, "Failed enabling "
246 "promiscous mode\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700247
248 /* Disable port multicast filter (unconditionally) */
249 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
250 0, MLX4_MCAST_DISABLE);
251 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000252 en_err(priv, "Failed disabling "
253 "multicast filter\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700254
255 /* Disable port VLAN filter */
256 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv->port, NULL);
257 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000258 en_err(priv, "Failed disabling VLAN filter\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700259 }
260 goto out;
261 }
262
263 /*
264 * Not in promiscous mode
265 */
266
267 if (priv->flags & MLX4_EN_FLAG_PROMISC) {
268 if (netif_msg_rx_status(priv))
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000269 en_warn(priv, "Leaving promiscuous mode\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700270 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
271
272 /* Disable promiscouos mode */
273 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port,
274 priv->base_qpn, 0);
275 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000276 en_err(priv, "Failed disabling promiscous mode\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700277
278 /* Enable port VLAN filter */
279 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv->port, priv->vlgrp);
280 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000281 en_err(priv, "Failed enabling VLAN filter\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700282 }
283
284 /* Enable/disable the multicast filter according to IFF_ALLMULTI */
285 if (dev->flags & IFF_ALLMULTI) {
286 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
287 0, MLX4_MCAST_DISABLE);
288 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000289 en_err(priv, "Failed disabling multicast filter\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700290 } else {
291 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
292 0, MLX4_MCAST_DISABLE);
293 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000294 en_err(priv, "Failed disabling multicast filter\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700295
296 /* Flush mcast filter and init it with broadcast address */
297 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
298 1, MLX4_MCAST_CONFIG);
299
300 /* Update multicast list - we cache all addresses so they won't
301 * change while HW is updated holding the command semaphor */
302 netif_tx_lock_bh(dev);
303 mlx4_en_cache_mclist(dev);
304 netif_tx_unlock_bh(dev);
305 for (mclist = priv->mc_list; mclist; mclist = mclist->next) {
306 mcast_addr = mlx4_en_mac_to_u64(mclist->dmi_addr);
307 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
308 mcast_addr, 0, MLX4_MCAST_CONFIG);
309 }
310 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
311 0, MLX4_MCAST_ENABLE);
312 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000313 en_err(priv, "Failed enabling multicast filter\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700314
315 mlx4_en_clear_list(dev);
316 }
317out:
318 mutex_unlock(&mdev->state_lock);
319}
320
321#ifdef CONFIG_NET_POLL_CONTROLLER
322static void mlx4_en_netpoll(struct net_device *dev)
323{
324 struct mlx4_en_priv *priv = netdev_priv(dev);
325 struct mlx4_en_cq *cq;
326 unsigned long flags;
327 int i;
328
329 for (i = 0; i < priv->rx_ring_num; i++) {
330 cq = &priv->rx_cq[i];
331 spin_lock_irqsave(&cq->lock, flags);
332 napi_synchronize(&cq->napi);
333 mlx4_en_process_rx_cq(dev, cq, 0);
334 spin_unlock_irqrestore(&cq->lock, flags);
335 }
336}
337#endif
338
339static void mlx4_en_tx_timeout(struct net_device *dev)
340{
341 struct mlx4_en_priv *priv = netdev_priv(dev);
342 struct mlx4_en_dev *mdev = priv->mdev;
343
344 if (netif_msg_timer(priv))
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000345 en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700346
Yevgeny Petrilin1e338db2009-04-20 04:26:05 +0000347 priv->port_stats.tx_timeout++;
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000348 en_dbg(DRV, priv, "Scheduling watchdog\n");
Yevgeny Petrilin1e338db2009-04-20 04:26:05 +0000349 queue_work(mdev->workqueue, &priv->watchdog_task);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700350}
351
352
353static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
354{
355 struct mlx4_en_priv *priv = netdev_priv(dev);
356
357 spin_lock_bh(&priv->stats_lock);
358 memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
359 spin_unlock_bh(&priv->stats_lock);
360
361 return &priv->ret_stats;
362}
363
364static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
365{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700366 struct mlx4_en_cq *cq;
367 int i;
368
369 /* If we haven't received a specific coalescing setting
Martin Olsson98a17082009-04-22 18:21:29 +0200370 * (module param), we set the moderation parameters as follows:
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700371 * - moder_cnt is set to the number of mtu sized packets to
372 * satisfy our coelsing target.
373 * - moder_time is set to a fixed value.
374 */
Yevgeny Petrilin3db36fb2009-06-01 23:23:13 +0000375 priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
Yevgeny Petrilin60b9f9e2008-12-25 18:19:47 -0800376 priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000377 en_dbg(INTR, priv, "Default coalesing params for mtu:%d - "
378 "rx_frames:%d rx_usecs:%d\n",
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700379 priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
380
381 /* Setup cq moderation params */
382 for (i = 0; i < priv->rx_ring_num; i++) {
383 cq = &priv->rx_cq[i];
384 cq->moder_cnt = priv->rx_frames;
385 cq->moder_time = priv->rx_usecs;
386 }
387
388 for (i = 0; i < priv->tx_ring_num; i++) {
389 cq = &priv->tx_cq[i];
390 cq->moder_cnt = MLX4_EN_TX_COAL_PKTS;
391 cq->moder_time = MLX4_EN_TX_COAL_TIME;
392 }
393
394 /* Reset auto-moderation params */
395 priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
396 priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
397 priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
398 priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
399 priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
Yevgeny Petrilin60b9f9e2008-12-25 18:19:47 -0800400 priv->adaptive_rx_coal = 1;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700401 priv->last_moder_time = MLX4_EN_AUTO_CONF;
402 priv->last_moder_jiffies = 0;
403 priv->last_moder_packets = 0;
404 priv->last_moder_tx_packets = 0;
405 priv->last_moder_bytes = 0;
406}
407
408static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
409{
410 unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700411 struct mlx4_en_cq *cq;
412 unsigned long packets;
413 unsigned long rate;
414 unsigned long avg_pkt_size;
415 unsigned long rx_packets;
416 unsigned long rx_bytes;
417 unsigned long tx_packets;
418 unsigned long tx_pkt_diff;
419 unsigned long rx_pkt_diff;
420 int moder_time;
421 int i, err;
422
423 if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
424 return;
425
426 spin_lock_bh(&priv->stats_lock);
427 rx_packets = priv->stats.rx_packets;
428 rx_bytes = priv->stats.rx_bytes;
429 tx_packets = priv->stats.tx_packets;
430 spin_unlock_bh(&priv->stats_lock);
431
432 if (!priv->last_moder_jiffies || !period)
433 goto out;
434
435 tx_pkt_diff = ((unsigned long) (tx_packets -
436 priv->last_moder_tx_packets));
437 rx_pkt_diff = ((unsigned long) (rx_packets -
438 priv->last_moder_packets));
439 packets = max(tx_pkt_diff, rx_pkt_diff);
440 rate = packets * HZ / period;
441 avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
442 priv->last_moder_bytes)) / packets : 0;
443
444 /* Apply auto-moderation only when packet rate exceeds a rate that
445 * it matters */
446 if (rate > MLX4_EN_RX_RATE_THRESH) {
447 /* If tx and rx packet rates are not balanced, assume that
448 * traffic is mainly BW bound and apply maximum moderation.
449 * Otherwise, moderate according to packet rate */
450 if (2 * tx_pkt_diff > 3 * rx_pkt_diff ||
451 2 * rx_pkt_diff > 3 * tx_pkt_diff) {
452 moder_time = priv->rx_usecs_high;
453 } else {
454 if (rate < priv->pkt_rate_low)
455 moder_time = priv->rx_usecs_low;
456 else if (rate > priv->pkt_rate_high)
457 moder_time = priv->rx_usecs_high;
458 else
459 moder_time = (rate - priv->pkt_rate_low) *
460 (priv->rx_usecs_high - priv->rx_usecs_low) /
461 (priv->pkt_rate_high - priv->pkt_rate_low) +
462 priv->rx_usecs_low;
463 }
464 } else {
465 /* When packet rate is low, use default moderation rather than
466 * 0 to prevent interrupt storms if traffic suddenly increases */
467 moder_time = priv->rx_usecs;
468 }
469
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000470 en_dbg(INTR, priv, "tx rate:%lu rx_rate:%lu\n",
471 tx_pkt_diff * HZ / period, rx_pkt_diff * HZ / period);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700472
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000473 en_dbg(INTR, priv, "Rx moder_time changed from:%d to %d period:%lu "
474 "[jiff] packets:%lu avg_pkt_size:%lu rate:%lu [p/s])\n",
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700475 priv->last_moder_time, moder_time, period, packets,
476 avg_pkt_size, rate);
477
478 if (moder_time != priv->last_moder_time) {
479 priv->last_moder_time = moder_time;
480 for (i = 0; i < priv->rx_ring_num; i++) {
481 cq = &priv->rx_cq[i];
482 cq->moder_time = moder_time;
483 err = mlx4_en_set_cq_moder(priv, cq);
484 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000485 en_err(priv, "Failed modifying moderation for cq:%d\n", i);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700486 break;
487 }
488 }
489 }
490
491out:
492 priv->last_moder_packets = rx_packets;
493 priv->last_moder_tx_packets = tx_packets;
494 priv->last_moder_bytes = rx_bytes;
495 priv->last_moder_jiffies = jiffies;
496}
497
498static void mlx4_en_do_get_stats(struct work_struct *work)
499{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700500 struct delayed_work *delay = to_delayed_work(work);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700501 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
502 stats_task);
503 struct mlx4_en_dev *mdev = priv->mdev;
504 int err;
505
506 err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
507 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000508 en_dbg(HW, priv, "Could not update stats \n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700509
510 mutex_lock(&mdev->state_lock);
511 if (mdev->device_up) {
512 if (priv->port_up)
513 mlx4_en_auto_moderation(priv);
514
515 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
516 }
517 mutex_unlock(&mdev->state_lock);
518}
519
520static void mlx4_en_linkstate(struct work_struct *work)
521{
522 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
523 linkstate_task);
524 struct mlx4_en_dev *mdev = priv->mdev;
525 int linkstate = priv->link_state;
526
527 mutex_lock(&mdev->state_lock);
528 /* If observable port state changed set carrier state and
529 * report to system log */
530 if (priv->last_link_state != linkstate) {
531 if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000532 en_dbg(LINK, priv, "Link Down\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700533 netif_carrier_off(priv->dev);
534 } else {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000535 en_dbg(LINK, priv, "Link Up\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700536 netif_carrier_on(priv->dev);
537 }
538 }
539 priv->last_link_state = linkstate;
540 mutex_unlock(&mdev->state_lock);
541}
542
543
Yevgeny Petrilin18cc42a2008-12-29 18:39:20 -0800544int mlx4_en_start_port(struct net_device *dev)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700545{
546 struct mlx4_en_priv *priv = netdev_priv(dev);
547 struct mlx4_en_dev *mdev = priv->mdev;
548 struct mlx4_en_cq *cq;
549 struct mlx4_en_tx_ring *tx_ring;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700550 int rx_index = 0;
551 int tx_index = 0;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700552 int err = 0;
553 int i;
554 int j;
555
556 if (priv->port_up) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000557 en_dbg(DRV, priv, "start port called while port already up\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700558 return 0;
559 }
560
561 /* Calculate Rx buf size */
562 dev->mtu = min(dev->mtu, priv->max_mtu);
563 mlx4_en_calc_rx_buf(dev);
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000564 en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000565
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700566 /* Configure rx cq's and rings */
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000567 err = mlx4_en_activate_rx_rings(priv);
568 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000569 en_err(priv, "Failed to activate RX rings\n");
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000570 return err;
571 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700572 for (i = 0; i < priv->rx_ring_num; i++) {
573 cq = &priv->rx_cq[i];
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700574
575 err = mlx4_en_activate_cq(priv, cq);
576 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000577 en_err(priv, "Failed activating Rx CQ\n");
Yevgeny Petrilina4233302009-04-26 20:41:34 +0000578 goto cq_err;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700579 }
580 for (j = 0; j < cq->size; j++)
581 cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
582 err = mlx4_en_set_cq_moder(priv, cq);
583 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000584 en_err(priv, "Failed setting cq moderation parameters");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700585 mlx4_en_deactivate_cq(priv, cq);
586 goto cq_err;
587 }
588 mlx4_en_arm_cq(priv, cq);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000589 priv->rx_ring[i].cqn = cq->mcq.cqn;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700590 ++rx_index;
591 }
592
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700593 err = mlx4_en_config_rss_steer(priv);
594 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000595 en_err(priv, "Failed configuring rss steering\n");
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000596 goto cq_err;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700597 }
598
599 /* Configure tx cq's and rings */
600 for (i = 0; i < priv->tx_ring_num; i++) {
601 /* Configure cq */
602 cq = &priv->tx_cq[i];
603 err = mlx4_en_activate_cq(priv, cq);
604 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000605 en_err(priv, "Failed allocating Tx CQ\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700606 goto tx_err;
607 }
608 err = mlx4_en_set_cq_moder(priv, cq);
609 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000610 en_err(priv, "Failed setting cq moderation parameters");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700611 mlx4_en_deactivate_cq(priv, cq);
612 goto tx_err;
613 }
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000614 en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700615 cq->buf->wqe_index = cpu_to_be16(0xffff);
616
617 /* Configure ring */
618 tx_ring = &priv->tx_ring[i];
619 err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
620 priv->rx_ring[0].srq.srqn);
621 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000622 en_err(priv, "Failed allocating Tx ring\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700623 mlx4_en_deactivate_cq(priv, cq);
624 goto tx_err;
625 }
626 /* Set initial ownership of all Tx TXBBs to SW (1) */
627 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
628 *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
629 ++tx_index;
630 }
631
632 /* Configure port */
633 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
634 priv->rx_skb_size + ETH_FCS_LEN,
Yevgeny Petrilind53b93f2008-11-05 04:48:36 +0000635 priv->prof->tx_pause,
636 priv->prof->tx_ppp,
637 priv->prof->rx_pause,
638 priv->prof->rx_ppp);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700639 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000640 en_err(priv, "Failed setting port general configurations "
641 "for port %d, with error %d\n", priv->port, err);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700642 goto tx_err;
643 }
644 /* Set default qp number */
645 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
646 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000647 en_err(priv, "Failed setting default qp numbers\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700648 goto tx_err;
649 }
650 /* Set port mac number */
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000651 en_dbg(DRV, priv, "Setting mac for port %d\n", priv->port);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700652 err = mlx4_register_mac(mdev->dev, priv->port,
653 priv->mac, &priv->mac_index);
654 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000655 en_err(priv, "Failed setting port mac\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700656 goto tx_err;
657 }
658
659 /* Init port */
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000660 en_dbg(HW, priv, "Initializing port\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700661 err = mlx4_INIT_PORT(mdev->dev, priv->port);
662 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000663 en_err(priv, "Failed Initializing port\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700664 goto mac_err;
665 }
666
667 /* Schedule multicast task to populate multicast list */
668 queue_work(mdev->workqueue, &priv->mcast_task);
669
670 priv->port_up = true;
Yevgeny Petrilina11faac2009-06-20 22:15:46 +0000671 netif_tx_start_all_queues(dev);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700672 return 0;
673
674mac_err:
675 mlx4_unregister_mac(mdev->dev, priv->port, priv->mac_index);
676tx_err:
677 while (tx_index--) {
678 mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[tx_index]);
679 mlx4_en_deactivate_cq(priv, &priv->tx_cq[tx_index]);
680 }
681
682 mlx4_en_release_rss_steer(priv);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700683cq_err:
684 while (rx_index--)
685 mlx4_en_deactivate_cq(priv, &priv->rx_cq[rx_index]);
Yevgeny Petrilin38aab072009-05-24 03:17:11 +0000686 for (i = 0; i < priv->rx_ring_num; i++)
687 mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700688
689 return err; /* need to close devices */
690}
691
692
Yevgeny Petrilin18cc42a2008-12-29 18:39:20 -0800693void mlx4_en_stop_port(struct net_device *dev)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700694{
695 struct mlx4_en_priv *priv = netdev_priv(dev);
696 struct mlx4_en_dev *mdev = priv->mdev;
697 int i;
698
699 if (!priv->port_up) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000700 en_dbg(DRV, priv, "stop port called while port already down\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700701 return;
702 }
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700703
704 /* Synchronize with tx routine */
705 netif_tx_lock_bh(dev);
Yevgeny Petrilin3c05f5e2009-06-20 22:15:52 +0000706 netif_tx_stop_all_queues(dev);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700707 netif_tx_unlock_bh(dev);
708
709 /* close port*/
Yevgeny Petrilin3c05f5e2009-06-20 22:15:52 +0000710 priv->port_up = false;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700711 mlx4_CLOSE_PORT(mdev->dev, priv->port);
712
713 /* Unregister Mac address for the port */
714 mlx4_unregister_mac(mdev->dev, priv->port, priv->mac_index);
715
716 /* Free TX Rings */
717 for (i = 0; i < priv->tx_ring_num; i++) {
718 mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[i]);
719 mlx4_en_deactivate_cq(priv, &priv->tx_cq[i]);
720 }
721 msleep(10);
722
723 for (i = 0; i < priv->tx_ring_num; i++)
724 mlx4_en_free_tx_buf(dev, &priv->tx_ring[i]);
725
726 /* Free RSS qps */
727 mlx4_en_release_rss_steer(priv);
728
729 /* Free RX Rings */
730 for (i = 0; i < priv->rx_ring_num; i++) {
731 mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
732 while (test_bit(NAPI_STATE_SCHED, &priv->rx_cq[i].napi.state))
733 msleep(1);
734 mlx4_en_deactivate_cq(priv, &priv->rx_cq[i]);
735 }
736}
737
738static void mlx4_en_restart(struct work_struct *work)
739{
740 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
741 watchdog_task);
742 struct mlx4_en_dev *mdev = priv->mdev;
743 struct net_device *dev = priv->dev;
744
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000745 en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
Yevgeny Petrilin1e338db2009-04-20 04:26:05 +0000746
747 mutex_lock(&mdev->state_lock);
748 if (priv->port_up) {
749 mlx4_en_stop_port(dev);
750 if (mlx4_en_start_port(dev))
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000751 en_err(priv, "Failed restarting port %d\n", priv->port);
Yevgeny Petrilin1e338db2009-04-20 04:26:05 +0000752 }
753 mutex_unlock(&mdev->state_lock);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700754}
755
756
757static int mlx4_en_open(struct net_device *dev)
758{
759 struct mlx4_en_priv *priv = netdev_priv(dev);
760 struct mlx4_en_dev *mdev = priv->mdev;
761 int i;
762 int err = 0;
763
764 mutex_lock(&mdev->state_lock);
765
766 if (!mdev->device_up) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000767 en_err(priv, "Cannot open - device down/disabled\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700768 err = -EBUSY;
769 goto out;
770 }
771
772 /* Reset HW statistics and performance counters */
773 if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000774 en_dbg(HW, priv, "Failed dumping statistics\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700775
776 memset(&priv->stats, 0, sizeof(priv->stats));
777 memset(&priv->pstats, 0, sizeof(priv->pstats));
778
779 for (i = 0; i < priv->tx_ring_num; i++) {
780 priv->tx_ring[i].bytes = 0;
781 priv->tx_ring[i].packets = 0;
782 }
783 for (i = 0; i < priv->rx_ring_num; i++) {
784 priv->rx_ring[i].bytes = 0;
785 priv->rx_ring[i].packets = 0;
786 }
787
788 mlx4_en_set_default_moderation(priv);
789 err = mlx4_en_start_port(dev);
790 if (err)
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000791 en_err(priv, "Failed starting port:%d\n", priv->port);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700792
793out:
794 mutex_unlock(&mdev->state_lock);
795 return err;
796}
797
798
799static int mlx4_en_close(struct net_device *dev)
800{
801 struct mlx4_en_priv *priv = netdev_priv(dev);
802 struct mlx4_en_dev *mdev = priv->mdev;
803
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000804 en_dbg(IFDOWN, priv, "Close port called\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700805
806 mutex_lock(&mdev->state_lock);
807
808 mlx4_en_stop_port(dev);
809 netif_carrier_off(dev);
810
811 mutex_unlock(&mdev->state_lock);
812 return 0;
813}
814
Yevgeny Petrilin18cc42a2008-12-29 18:39:20 -0800815void mlx4_en_free_resources(struct mlx4_en_priv *priv)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700816{
817 int i;
818
819 for (i = 0; i < priv->tx_ring_num; i++) {
820 if (priv->tx_ring[i].tx_info)
821 mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
822 if (priv->tx_cq[i].buf)
823 mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
824 }
825
826 for (i = 0; i < priv->rx_ring_num; i++) {
827 if (priv->rx_ring[i].rx_info)
828 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i]);
829 if (priv->rx_cq[i].buf)
830 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
831 }
832}
833
Yevgeny Petrilin18cc42a2008-12-29 18:39:20 -0800834int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700835{
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700836 struct mlx4_en_port_profile *prof = priv->prof;
837 int i;
838
839 /* Create tx Rings */
840 for (i = 0; i < priv->tx_ring_num; i++) {
841 if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
842 prof->tx_ring_size, i, TX))
843 goto err;
844
845 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i],
846 prof->tx_ring_size, TXBB_SIZE))
847 goto err;
848 }
849
850 /* Create rx Rings */
851 for (i = 0; i < priv->rx_ring_num; i++) {
852 if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
853 prof->rx_ring_size, i, RX))
854 goto err;
855
856 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
857 prof->rx_ring_size, priv->stride))
858 goto err;
859 }
860
861 return 0;
862
863err:
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000864 en_err(priv, "Failed to allocate NIC resources\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700865 return -ENOMEM;
866}
867
868
869void mlx4_en_destroy_netdev(struct net_device *dev)
870{
871 struct mlx4_en_priv *priv = netdev_priv(dev);
872 struct mlx4_en_dev *mdev = priv->mdev;
873
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000874 en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700875
876 /* Unregister device - this will close the port if it was up */
877 if (priv->registered)
878 unregister_netdev(dev);
879
880 if (priv->allocated)
881 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
882
883 cancel_delayed_work(&priv->stats_task);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700884 /* flush any pending task for this netdev */
885 flush_workqueue(mdev->workqueue);
886
887 /* Detach the netdev so tasks would not attempt to access it */
888 mutex_lock(&mdev->state_lock);
889 mdev->pndev[priv->port] = NULL;
890 mutex_unlock(&mdev->state_lock);
891
892 mlx4_en_free_resources(priv);
893 free_netdev(dev);
894}
895
896static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
897{
898 struct mlx4_en_priv *priv = netdev_priv(dev);
899 struct mlx4_en_dev *mdev = priv->mdev;
900 int err = 0;
901
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000902 en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700903 dev->mtu, new_mtu);
904
905 if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000906 en_err(priv, "Bad MTU size:%d.\n", new_mtu);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700907 return -EPERM;
908 }
909 dev->mtu = new_mtu;
910
911 if (netif_running(dev)) {
912 mutex_lock(&mdev->state_lock);
913 if (!mdev->device_up) {
914 /* NIC is probably restarting - let watchdog task reset
915 * the port */
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000916 en_dbg(DRV, priv, "Change MTU called with card down!?\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700917 } else {
918 mlx4_en_stop_port(dev);
919 mlx4_en_set_default_moderation(priv);
920 err = mlx4_en_start_port(dev);
921 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000922 en_err(priv, "Failed restarting port:%d\n",
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700923 priv->port);
924 queue_work(mdev->workqueue, &priv->watchdog_task);
925 }
926 }
927 mutex_unlock(&mdev->state_lock);
928 }
929 return 0;
930}
931
Stephen Hemminger3addc562008-11-21 17:30:58 -0800932static const struct net_device_ops mlx4_netdev_ops = {
933 .ndo_open = mlx4_en_open,
934 .ndo_stop = mlx4_en_close,
935 .ndo_start_xmit = mlx4_en_xmit,
Yevgeny Petrilinf813cad2009-06-01 23:24:07 +0000936 .ndo_select_queue = mlx4_en_select_queue,
Stephen Hemminger3addc562008-11-21 17:30:58 -0800937 .ndo_get_stats = mlx4_en_get_stats,
938 .ndo_set_multicast_list = mlx4_en_set_multicast,
939 .ndo_set_mac_address = mlx4_en_set_mac,
Stephen Hemminger52255bb2009-01-09 10:45:37 +0000940 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger3addc562008-11-21 17:30:58 -0800941 .ndo_change_mtu = mlx4_en_change_mtu,
942 .ndo_tx_timeout = mlx4_en_tx_timeout,
943 .ndo_vlan_rx_register = mlx4_en_vlan_rx_register,
944 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
945 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
946#ifdef CONFIG_NET_POLL_CONTROLLER
947 .ndo_poll_controller = mlx4_en_netpoll,
948#endif
949};
950
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700951int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
952 struct mlx4_en_port_profile *prof)
953{
954 struct net_device *dev;
955 struct mlx4_en_priv *priv;
956 int i;
957 int err;
958
Yevgeny Petrilinf813cad2009-06-01 23:24:07 +0000959 dev = alloc_etherdev_mq(sizeof(struct mlx4_en_priv), prof->tx_ring_num);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700960 if (dev == NULL) {
961 mlx4_err(mdev, "Net device allocation failed\n");
962 return -ENOMEM;
963 }
964
965 SET_NETDEV_DEV(dev, &mdev->dev->pdev->dev);
966
967 /*
968 * Initialize driver private data
969 */
970
971 priv = netdev_priv(dev);
972 memset(priv, 0, sizeof(struct mlx4_en_priv));
973 priv->dev = dev;
974 priv->mdev = mdev;
975 priv->prof = prof;
976 priv->port = port;
977 priv->port_up = false;
978 priv->rx_csum = 1;
979 priv->flags = prof->flags;
980 priv->tx_ring_num = prof->tx_ring_num;
981 priv->rx_ring_num = prof->rx_ring_num;
982 priv->mc_list = NULL;
983 priv->mac_index = -1;
984 priv->msg_enable = MLX4_EN_MSG_LEVEL;
985 spin_lock_init(&priv->stats_lock);
986 INIT_WORK(&priv->mcast_task, mlx4_en_do_set_multicast);
987 INIT_WORK(&priv->mac_task, mlx4_en_do_set_mac);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700988 INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
989 INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
990 INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
991
992 /* Query for default mac and max mtu */
993 priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
994 priv->mac = mdev->dev->caps.def_mac[priv->port];
995 if (ILLEGAL_MAC(priv->mac)) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +0000996 en_err(priv, "Port: %d, invalid mac burned: 0x%llx, quiting\n",
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -0700997 priv->port, priv->mac);
998 err = -EINVAL;
999 goto out;
1000 }
1001
1002 priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
1003 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
1004 err = mlx4_en_alloc_resources(priv);
1005 if (err)
1006 goto out;
1007
1008 /* Populate Rx default RSS mappings */
1009 mlx4_en_set_default_rss_map(priv, &priv->rss_map, priv->rx_ring_num *
1010 RSS_FACTOR, priv->rx_ring_num);
1011 /* Allocate page for receive rings */
1012 err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
1013 MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
1014 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001015 en_err(priv, "Failed to allocate page for rx qps\n");
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001016 goto out;
1017 }
1018 priv->allocated = 1;
1019
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001020 /*
1021 * Initialize netdev entry points
1022 */
Stephen Hemminger3addc562008-11-21 17:30:58 -08001023 dev->netdev_ops = &mlx4_netdev_ops;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001024 dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
Yevgeny Petrilinf813cad2009-06-01 23:24:07 +00001025 dev->real_num_tx_queues = MLX4_EN_NUM_TX_RINGS;
Stephen Hemminger3addc562008-11-21 17:30:58 -08001026
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001027 SET_ETHTOOL_OPS(dev, &mlx4_en_ethtool_ops);
1028
1029 /* Set defualt MAC */
1030 dev->addr_len = ETH_ALEN;
1031 for (i = 0; i < ETH_ALEN; i++)
1032 dev->dev_addr[ETH_ALEN - 1 - i] =
1033 (u8) (priv->mac >> (8 * i));
1034
1035 /*
1036 * Set driver features
1037 */
1038 dev->features |= NETIF_F_SG;
Yevgeny Petriline4869732009-06-01 23:28:29 +00001039 dev->vlan_features |= NETIF_F_SG;
Yevgeny Petrilin45b4d662009-04-20 04:33:15 +00001040 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
Yevgeny Petriline4869732009-06-01 23:28:29 +00001041 dev->vlan_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001042 dev->features |= NETIF_F_HIGHDMA;
1043 dev->features |= NETIF_F_HW_VLAN_TX |
1044 NETIF_F_HW_VLAN_RX |
1045 NETIF_F_HW_VLAN_FILTER;
1046 if (mdev->profile.num_lro)
1047 dev->features |= NETIF_F_LRO;
1048 if (mdev->LSO_support) {
1049 dev->features |= NETIF_F_TSO;
1050 dev->features |= NETIF_F_TSO6;
Yevgeny Petriline4869732009-06-01 23:28:29 +00001051 dev->vlan_features |= NETIF_F_TSO;
1052 dev->vlan_features |= NETIF_F_TSO6;
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001053 }
1054
1055 mdev->pndev[port] = dev;
1056
1057 netif_carrier_off(dev);
1058 err = register_netdev(dev);
1059 if (err) {
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001060 en_err(priv, "Netdev registration failed for port %d\n", port);
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001061 goto out;
1062 }
Yevgeny Petrilin453a6082009-06-01 20:27:13 +00001063
1064 en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
1065 en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
1066
Yevgeny Petrilinc27a02c2008-10-22 15:47:49 -07001067 priv->registered = 1;
1068 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1069 return 0;
1070
1071out:
1072 mlx4_en_destroy_netdev(dev);
1073 return err;
1074}
1075