net/mlx4_en: DCB QoS support
[linux-2.6.git] / drivers / net / ethernet / mellanox / mlx4 / en_netdev.c
blob107f00553bd3e3267dec530d0702dc4d4d9a802f
1 /*
2 * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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.
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.
34 #include <linux/etherdevice.h>
35 #include <linux/tcp.h>
36 #include <linux/if_vlan.h>
37 #include <linux/delay.h>
38 #include <linux/slab.h>
40 #include <linux/mlx4/driver.h>
41 #include <linux/mlx4/device.h>
42 #include <linux/mlx4/cmd.h>
43 #include <linux/mlx4/cq.h>
45 #include "mlx4_en.h"
46 #include "en_port.h"
48 static int mlx4_en_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
50 struct mlx4_en_priv *priv = netdev_priv(dev);
51 struct mlx4_en_dev *mdev = priv->mdev;
52 int err;
53 int idx;
55 en_dbg(HW, priv, "adding VLAN:%d\n", vid);
57 set_bit(vid, priv->active_vlans);
59 /* Add VID to port VLAN filter */
60 mutex_lock(&mdev->state_lock);
61 if (mdev->device_up && priv->port_up) {
62 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
63 if (err)
64 en_err(priv, "Failed configuring VLAN filter\n");
66 if (mlx4_register_vlan(mdev->dev, priv->port, vid, &idx))
67 en_err(priv, "failed adding vlan %d\n", vid);
68 mutex_unlock(&mdev->state_lock);
70 return 0;
73 static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
75 struct mlx4_en_priv *priv = netdev_priv(dev);
76 struct mlx4_en_dev *mdev = priv->mdev;
77 int err;
78 int idx;
80 en_dbg(HW, priv, "Killing VID:%d\n", vid);
82 clear_bit(vid, priv->active_vlans);
84 /* Remove VID from port VLAN filter */
85 mutex_lock(&mdev->state_lock);
86 if (!mlx4_find_cached_vlan(mdev->dev, priv->port, vid, &idx))
87 mlx4_unregister_vlan(mdev->dev, priv->port, idx);
88 else
89 en_err(priv, "could not find vid %d in cache\n", vid);
91 if (mdev->device_up && priv->port_up) {
92 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
93 if (err)
94 en_err(priv, "Failed configuring VLAN filter\n");
96 mutex_unlock(&mdev->state_lock);
98 return 0;
101 u64 mlx4_en_mac_to_u64(u8 *addr)
103 u64 mac = 0;
104 int i;
106 for (i = 0; i < ETH_ALEN; i++) {
107 mac <<= 8;
108 mac |= addr[i];
110 return mac;
113 static int mlx4_en_set_mac(struct net_device *dev, void *addr)
115 struct mlx4_en_priv *priv = netdev_priv(dev);
116 struct mlx4_en_dev *mdev = priv->mdev;
117 struct sockaddr *saddr = addr;
119 if (!is_valid_ether_addr(saddr->sa_data))
120 return -EADDRNOTAVAIL;
122 memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
123 priv->mac = mlx4_en_mac_to_u64(dev->dev_addr);
124 queue_work(mdev->workqueue, &priv->mac_task);
125 return 0;
128 static void mlx4_en_do_set_mac(struct work_struct *work)
130 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
131 mac_task);
132 struct mlx4_en_dev *mdev = priv->mdev;
133 int err = 0;
135 mutex_lock(&mdev->state_lock);
136 if (priv->port_up) {
137 /* Remove old MAC and insert the new one */
138 err = mlx4_replace_mac(mdev->dev, priv->port,
139 priv->base_qpn, priv->mac);
140 if (err)
141 en_err(priv, "Failed changing HW MAC address\n");
142 } else
143 en_dbg(HW, priv, "Port is down while "
144 "registering mac, exiting...\n");
146 mutex_unlock(&mdev->state_lock);
149 static void mlx4_en_clear_list(struct net_device *dev)
151 struct mlx4_en_priv *priv = netdev_priv(dev);
153 kfree(priv->mc_addrs);
154 priv->mc_addrs = NULL;
155 priv->mc_addrs_cnt = 0;
158 static void mlx4_en_cache_mclist(struct net_device *dev)
160 struct mlx4_en_priv *priv = netdev_priv(dev);
161 struct netdev_hw_addr *ha;
162 char *mc_addrs;
163 int mc_addrs_cnt = netdev_mc_count(dev);
164 int i;
166 mc_addrs = kmalloc(mc_addrs_cnt * ETH_ALEN, GFP_ATOMIC);
167 if (!mc_addrs) {
168 en_err(priv, "failed to allocate multicast list\n");
169 return;
171 i = 0;
172 netdev_for_each_mc_addr(ha, dev)
173 memcpy(mc_addrs + i++ * ETH_ALEN, ha->addr, ETH_ALEN);
174 mlx4_en_clear_list(dev);
175 priv->mc_addrs = mc_addrs;
176 priv->mc_addrs_cnt = mc_addrs_cnt;
180 static void mlx4_en_set_multicast(struct net_device *dev)
182 struct mlx4_en_priv *priv = netdev_priv(dev);
184 if (!priv->port_up)
185 return;
187 queue_work(priv->mdev->workqueue, &priv->mcast_task);
190 static void mlx4_en_do_set_multicast(struct work_struct *work)
192 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
193 mcast_task);
194 struct mlx4_en_dev *mdev = priv->mdev;
195 struct net_device *dev = priv->dev;
196 u64 mcast_addr = 0;
197 u8 mc_list[16] = {0};
198 int err;
200 mutex_lock(&mdev->state_lock);
201 if (!mdev->device_up) {
202 en_dbg(HW, priv, "Card is not up, "
203 "ignoring multicast change.\n");
204 goto out;
206 if (!priv->port_up) {
207 en_dbg(HW, priv, "Port is down, "
208 "ignoring multicast change.\n");
209 goto out;
212 if (!netif_carrier_ok(dev)) {
213 if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
214 if (priv->port_state.link_state) {
215 priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
216 netif_carrier_on(dev);
217 en_dbg(LINK, priv, "Link Up\n");
223 * Promsicuous mode: disable all filters
226 if (dev->flags & IFF_PROMISC) {
227 if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
228 if (netif_msg_rx_status(priv))
229 en_warn(priv, "Entering promiscuous mode\n");
230 priv->flags |= MLX4_EN_FLAG_PROMISC;
232 /* Enable promiscouos mode */
233 if (!(mdev->dev->caps.flags &
234 MLX4_DEV_CAP_FLAG_VEP_UC_STEER))
235 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port,
236 priv->base_qpn, 1);
237 else
238 err = mlx4_unicast_promisc_add(mdev->dev, priv->base_qpn,
239 priv->port);
240 if (err)
241 en_err(priv, "Failed enabling "
242 "promiscuous mode\n");
244 /* Disable port multicast filter (unconditionally) */
245 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
246 0, MLX4_MCAST_DISABLE);
247 if (err)
248 en_err(priv, "Failed disabling "
249 "multicast filter\n");
251 /* Add the default qp number as multicast promisc */
252 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
253 err = mlx4_multicast_promisc_add(mdev->dev, priv->base_qpn,
254 priv->port);
255 if (err)
256 en_err(priv, "Failed entering multicast promisc mode\n");
257 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
260 /* Disable port VLAN filter */
261 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
262 if (err)
263 en_err(priv, "Failed disabling VLAN filter\n");
265 goto out;
269 * Not in promiscuous mode
272 if (priv->flags & MLX4_EN_FLAG_PROMISC) {
273 if (netif_msg_rx_status(priv))
274 en_warn(priv, "Leaving promiscuous mode\n");
275 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
277 /* Disable promiscouos mode */
278 if (!(mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER))
279 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port,
280 priv->base_qpn, 0);
281 else
282 err = mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
283 priv->port);
284 if (err)
285 en_err(priv, "Failed disabling promiscuous mode\n");
287 /* Disable Multicast promisc */
288 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
289 err = mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
290 priv->port);
291 if (err)
292 en_err(priv, "Failed disabling multicast promiscuous mode\n");
293 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
296 /* Enable port VLAN filter */
297 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
298 if (err)
299 en_err(priv, "Failed enabling VLAN filter\n");
302 /* Enable/disable the multicast filter according to IFF_ALLMULTI */
303 if (dev->flags & IFF_ALLMULTI) {
304 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
305 0, MLX4_MCAST_DISABLE);
306 if (err)
307 en_err(priv, "Failed disabling multicast filter\n");
309 /* Add the default qp number as multicast promisc */
310 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
311 err = mlx4_multicast_promisc_add(mdev->dev, priv->base_qpn,
312 priv->port);
313 if (err)
314 en_err(priv, "Failed entering multicast promisc mode\n");
315 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
317 } else {
318 int i;
319 /* Disable Multicast promisc */
320 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
321 err = mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
322 priv->port);
323 if (err)
324 en_err(priv, "Failed disabling multicast promiscuous mode\n");
325 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
328 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
329 0, MLX4_MCAST_DISABLE);
330 if (err)
331 en_err(priv, "Failed disabling multicast filter\n");
333 /* Detach our qp from all the multicast addresses */
334 for (i = 0; i < priv->mc_addrs_cnt; i++) {
335 memcpy(&mc_list[10], priv->mc_addrs + i * ETH_ALEN, ETH_ALEN);
336 mc_list[5] = priv->port;
337 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
338 mc_list, MLX4_PROT_ETH);
340 /* Flush mcast filter and init it with broadcast address */
341 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
342 1, MLX4_MCAST_CONFIG);
344 /* Update multicast list - we cache all addresses so they won't
345 * change while HW is updated holding the command semaphor */
346 netif_tx_lock_bh(dev);
347 mlx4_en_cache_mclist(dev);
348 netif_tx_unlock_bh(dev);
349 for (i = 0; i < priv->mc_addrs_cnt; i++) {
350 mcast_addr =
351 mlx4_en_mac_to_u64(priv->mc_addrs + i * ETH_ALEN);
352 memcpy(&mc_list[10], priv->mc_addrs + i * ETH_ALEN, ETH_ALEN);
353 mc_list[5] = priv->port;
354 mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp,
355 mc_list, 0, MLX4_PROT_ETH);
356 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
357 mcast_addr, 0, MLX4_MCAST_CONFIG);
359 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
360 0, MLX4_MCAST_ENABLE);
361 if (err)
362 en_err(priv, "Failed enabling multicast filter\n");
364 out:
365 mutex_unlock(&mdev->state_lock);
368 #ifdef CONFIG_NET_POLL_CONTROLLER
369 static void mlx4_en_netpoll(struct net_device *dev)
371 struct mlx4_en_priv *priv = netdev_priv(dev);
372 struct mlx4_en_cq *cq;
373 unsigned long flags;
374 int i;
376 for (i = 0; i < priv->rx_ring_num; i++) {
377 cq = &priv->rx_cq[i];
378 spin_lock_irqsave(&cq->lock, flags);
379 napi_synchronize(&cq->napi);
380 mlx4_en_process_rx_cq(dev, cq, 0);
381 spin_unlock_irqrestore(&cq->lock, flags);
384 #endif
386 static void mlx4_en_tx_timeout(struct net_device *dev)
388 struct mlx4_en_priv *priv = netdev_priv(dev);
389 struct mlx4_en_dev *mdev = priv->mdev;
391 if (netif_msg_timer(priv))
392 en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
394 priv->port_stats.tx_timeout++;
395 en_dbg(DRV, priv, "Scheduling watchdog\n");
396 queue_work(mdev->workqueue, &priv->watchdog_task);
400 static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
402 struct mlx4_en_priv *priv = netdev_priv(dev);
404 spin_lock_bh(&priv->stats_lock);
405 memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
406 spin_unlock_bh(&priv->stats_lock);
408 return &priv->ret_stats;
411 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
413 struct mlx4_en_cq *cq;
414 int i;
416 /* If we haven't received a specific coalescing setting
417 * (module param), we set the moderation parameters as follows:
418 * - moder_cnt is set to the number of mtu sized packets to
419 * satisfy our coelsing target.
420 * - moder_time is set to a fixed value.
422 priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
423 priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
424 en_dbg(INTR, priv, "Default coalesing params for mtu:%d - "
425 "rx_frames:%d rx_usecs:%d\n",
426 priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
428 /* Setup cq moderation params */
429 for (i = 0; i < priv->rx_ring_num; i++) {
430 cq = &priv->rx_cq[i];
431 cq->moder_cnt = priv->rx_frames;
432 cq->moder_time = priv->rx_usecs;
433 priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
434 priv->last_moder_packets[i] = 0;
435 priv->last_moder_bytes[i] = 0;
438 for (i = 0; i < priv->tx_ring_num; i++) {
439 cq = &priv->tx_cq[i];
440 cq->moder_cnt = MLX4_EN_TX_COAL_PKTS;
441 cq->moder_time = MLX4_EN_TX_COAL_TIME;
444 /* Reset auto-moderation params */
445 priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
446 priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
447 priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
448 priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
449 priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
450 priv->adaptive_rx_coal = 1;
451 priv->last_moder_jiffies = 0;
452 priv->last_moder_tx_packets = 0;
455 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
457 unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
458 struct mlx4_en_cq *cq;
459 unsigned long packets;
460 unsigned long rate;
461 unsigned long avg_pkt_size;
462 unsigned long rx_packets;
463 unsigned long rx_bytes;
464 unsigned long rx_pkt_diff;
465 int moder_time;
466 int ring, err;
468 if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
469 return;
471 for (ring = 0; ring < priv->rx_ring_num; ring++) {
472 spin_lock_bh(&priv->stats_lock);
473 rx_packets = priv->rx_ring[ring].packets;
474 rx_bytes = priv->rx_ring[ring].bytes;
475 spin_unlock_bh(&priv->stats_lock);
477 rx_pkt_diff = ((unsigned long) (rx_packets -
478 priv->last_moder_packets[ring]));
479 packets = rx_pkt_diff;
480 rate = packets * HZ / period;
481 avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
482 priv->last_moder_bytes[ring])) / packets : 0;
484 /* Apply auto-moderation only when packet rate
485 * exceeds a rate that it matters */
486 if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
487 avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
488 if (rate < priv->pkt_rate_low)
489 moder_time = priv->rx_usecs_low;
490 else if (rate > priv->pkt_rate_high)
491 moder_time = priv->rx_usecs_high;
492 else
493 moder_time = (rate - priv->pkt_rate_low) *
494 (priv->rx_usecs_high - priv->rx_usecs_low) /
495 (priv->pkt_rate_high - priv->pkt_rate_low) +
496 priv->rx_usecs_low;
497 } else {
498 moder_time = priv->rx_usecs_low;
501 if (moder_time != priv->last_moder_time[ring]) {
502 priv->last_moder_time[ring] = moder_time;
503 cq = &priv->rx_cq[ring];
504 cq->moder_time = moder_time;
505 err = mlx4_en_set_cq_moder(priv, cq);
506 if (err)
507 en_err(priv, "Failed modifying moderation "
508 "for cq:%d\n", ring);
510 priv->last_moder_packets[ring] = rx_packets;
511 priv->last_moder_bytes[ring] = rx_bytes;
514 priv->last_moder_jiffies = jiffies;
517 static void mlx4_en_do_get_stats(struct work_struct *work)
519 struct delayed_work *delay = to_delayed_work(work);
520 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
521 stats_task);
522 struct mlx4_en_dev *mdev = priv->mdev;
523 int err;
525 err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
526 if (err)
527 en_dbg(HW, priv, "Could not update stats\n");
529 mutex_lock(&mdev->state_lock);
530 if (mdev->device_up) {
531 if (priv->port_up)
532 mlx4_en_auto_moderation(priv);
534 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
536 if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
537 queue_work(mdev->workqueue, &priv->mac_task);
538 mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
540 mutex_unlock(&mdev->state_lock);
543 static void mlx4_en_linkstate(struct work_struct *work)
545 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
546 linkstate_task);
547 struct mlx4_en_dev *mdev = priv->mdev;
548 int linkstate = priv->link_state;
550 mutex_lock(&mdev->state_lock);
551 /* If observable port state changed set carrier state and
552 * report to system log */
553 if (priv->last_link_state != linkstate) {
554 if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
555 en_info(priv, "Link Down\n");
556 netif_carrier_off(priv->dev);
557 } else {
558 en_info(priv, "Link Up\n");
559 netif_carrier_on(priv->dev);
562 priv->last_link_state = linkstate;
563 mutex_unlock(&mdev->state_lock);
567 int mlx4_en_start_port(struct net_device *dev)
569 struct mlx4_en_priv *priv = netdev_priv(dev);
570 struct mlx4_en_dev *mdev = priv->mdev;
571 struct mlx4_en_cq *cq;
572 struct mlx4_en_tx_ring *tx_ring;
573 int rx_index = 0;
574 int tx_index = 0;
575 int err = 0;
576 int i;
577 int j;
578 u8 mc_list[16] = {0};
580 if (priv->port_up) {
581 en_dbg(DRV, priv, "start port called while port already up\n");
582 return 0;
585 /* Calculate Rx buf size */
586 dev->mtu = min(dev->mtu, priv->max_mtu);
587 mlx4_en_calc_rx_buf(dev);
588 en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
590 /* Configure rx cq's and rings */
591 err = mlx4_en_activate_rx_rings(priv);
592 if (err) {
593 en_err(priv, "Failed to activate RX rings\n");
594 return err;
596 for (i = 0; i < priv->rx_ring_num; i++) {
597 cq = &priv->rx_cq[i];
599 err = mlx4_en_activate_cq(priv, cq, i);
600 if (err) {
601 en_err(priv, "Failed activating Rx CQ\n");
602 goto cq_err;
604 for (j = 0; j < cq->size; j++)
605 cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
606 err = mlx4_en_set_cq_moder(priv, cq);
607 if (err) {
608 en_err(priv, "Failed setting cq moderation parameters");
609 mlx4_en_deactivate_cq(priv, cq);
610 goto cq_err;
612 mlx4_en_arm_cq(priv, cq);
613 priv->rx_ring[i].cqn = cq->mcq.cqn;
614 ++rx_index;
617 /* Set qp number */
618 en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
619 err = mlx4_get_eth_qp(mdev->dev, priv->port,
620 priv->mac, &priv->base_qpn);
621 if (err) {
622 en_err(priv, "Failed getting eth qp\n");
623 goto cq_err;
625 mdev->mac_removed[priv->port] = 0;
627 err = mlx4_en_config_rss_steer(priv);
628 if (err) {
629 en_err(priv, "Failed configuring rss steering\n");
630 goto mac_err;
633 /* Configure tx cq's and rings */
634 for (i = 0; i < priv->tx_ring_num; i++) {
635 /* Configure cq */
636 cq = &priv->tx_cq[i];
637 err = mlx4_en_activate_cq(priv, cq, i);
638 if (err) {
639 en_err(priv, "Failed allocating Tx CQ\n");
640 goto tx_err;
642 err = mlx4_en_set_cq_moder(priv, cq);
643 if (err) {
644 en_err(priv, "Failed setting cq moderation parameters");
645 mlx4_en_deactivate_cq(priv, cq);
646 goto tx_err;
648 en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
649 cq->buf->wqe_index = cpu_to_be16(0xffff);
651 /* Configure ring */
652 tx_ring = &priv->tx_ring[i];
653 err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
654 max(0, i - MLX4_EN_NUM_TX_RINGS));
655 if (err) {
656 en_err(priv, "Failed allocating Tx ring\n");
657 mlx4_en_deactivate_cq(priv, cq);
658 goto tx_err;
660 /* Set initial ownership of all Tx TXBBs to SW (1) */
661 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
662 *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
663 ++tx_index;
666 /* Configure port */
667 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
668 priv->rx_skb_size + ETH_FCS_LEN,
669 priv->prof->tx_pause,
670 priv->prof->tx_ppp,
671 priv->prof->rx_pause,
672 priv->prof->rx_ppp);
673 if (err) {
674 en_err(priv, "Failed setting port general configurations "
675 "for port %d, with error %d\n", priv->port, err);
676 goto tx_err;
678 /* Set default qp number */
679 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
680 if (err) {
681 en_err(priv, "Failed setting default qp numbers\n");
682 goto tx_err;
685 /* Init port */
686 en_dbg(HW, priv, "Initializing port\n");
687 err = mlx4_INIT_PORT(mdev->dev, priv->port);
688 if (err) {
689 en_err(priv, "Failed Initializing port\n");
690 goto tx_err;
693 /* Attach rx QP to bradcast address */
694 memset(&mc_list[10], 0xff, ETH_ALEN);
695 mc_list[5] = priv->port;
696 if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
697 0, MLX4_PROT_ETH))
698 mlx4_warn(mdev, "Failed Attaching Broadcast\n");
700 /* Must redo promiscuous mode setup. */
701 priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
703 /* Schedule multicast task to populate multicast list */
704 queue_work(mdev->workqueue, &priv->mcast_task);
706 mlx4_set_stats_bitmap(mdev->dev, &priv->stats_bitmap);
708 priv->port_up = true;
709 netif_tx_start_all_queues(dev);
710 return 0;
712 tx_err:
713 while (tx_index--) {
714 mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[tx_index]);
715 mlx4_en_deactivate_cq(priv, &priv->tx_cq[tx_index]);
718 mlx4_en_release_rss_steer(priv);
719 mac_err:
720 mlx4_put_eth_qp(mdev->dev, priv->port, priv->mac, priv->base_qpn);
721 cq_err:
722 while (rx_index--)
723 mlx4_en_deactivate_cq(priv, &priv->rx_cq[rx_index]);
724 for (i = 0; i < priv->rx_ring_num; i++)
725 mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
727 return err; /* need to close devices */
731 void mlx4_en_stop_port(struct net_device *dev)
733 struct mlx4_en_priv *priv = netdev_priv(dev);
734 struct mlx4_en_dev *mdev = priv->mdev;
735 int i;
736 u8 mc_list[16] = {0};
738 if (!priv->port_up) {
739 en_dbg(DRV, priv, "stop port called while port already down\n");
740 return;
743 /* Synchronize with tx routine */
744 netif_tx_lock_bh(dev);
745 netif_tx_stop_all_queues(dev);
746 netif_tx_unlock_bh(dev);
748 /* Set port as not active */
749 priv->port_up = false;
751 /* Detach All multicasts */
752 memset(&mc_list[10], 0xff, ETH_ALEN);
753 mc_list[5] = priv->port;
754 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
755 MLX4_PROT_ETH);
756 for (i = 0; i < priv->mc_addrs_cnt; i++) {
757 memcpy(&mc_list[10], priv->mc_addrs + i * ETH_ALEN, ETH_ALEN);
758 mc_list[5] = priv->port;
759 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
760 mc_list, MLX4_PROT_ETH);
762 mlx4_en_clear_list(dev);
763 /* Flush multicast filter */
764 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
766 /* Free TX Rings */
767 for (i = 0; i < priv->tx_ring_num; i++) {
768 mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[i]);
769 mlx4_en_deactivate_cq(priv, &priv->tx_cq[i]);
771 msleep(10);
773 for (i = 0; i < priv->tx_ring_num; i++)
774 mlx4_en_free_tx_buf(dev, &priv->tx_ring[i]);
776 /* Free RSS qps */
777 mlx4_en_release_rss_steer(priv);
779 /* Unregister Mac address for the port */
780 mlx4_put_eth_qp(mdev->dev, priv->port, priv->mac, priv->base_qpn);
781 mdev->mac_removed[priv->port] = 1;
783 /* Free RX Rings */
784 for (i = 0; i < priv->rx_ring_num; i++) {
785 mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
786 while (test_bit(NAPI_STATE_SCHED, &priv->rx_cq[i].napi.state))
787 msleep(1);
788 mlx4_en_deactivate_cq(priv, &priv->rx_cq[i]);
791 /* close port*/
792 mlx4_CLOSE_PORT(mdev->dev, priv->port);
795 static void mlx4_en_restart(struct work_struct *work)
797 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
798 watchdog_task);
799 struct mlx4_en_dev *mdev = priv->mdev;
800 struct net_device *dev = priv->dev;
802 en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
804 mutex_lock(&mdev->state_lock);
805 if (priv->port_up) {
806 mlx4_en_stop_port(dev);
807 if (mlx4_en_start_port(dev))
808 en_err(priv, "Failed restarting port %d\n", priv->port);
810 mutex_unlock(&mdev->state_lock);
813 static void mlx4_en_clear_stats(struct net_device *dev)
815 struct mlx4_en_priv *priv = netdev_priv(dev);
816 struct mlx4_en_dev *mdev = priv->mdev;
817 int i;
819 if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
820 en_dbg(HW, priv, "Failed dumping statistics\n");
822 memset(&priv->stats, 0, sizeof(priv->stats));
823 memset(&priv->pstats, 0, sizeof(priv->pstats));
824 memset(&priv->pkstats, 0, sizeof(priv->pkstats));
825 memset(&priv->port_stats, 0, sizeof(priv->port_stats));
827 for (i = 0; i < priv->tx_ring_num; i++) {
828 priv->tx_ring[i].bytes = 0;
829 priv->tx_ring[i].packets = 0;
830 priv->tx_ring[i].tx_csum = 0;
832 for (i = 0; i < priv->rx_ring_num; i++) {
833 priv->rx_ring[i].bytes = 0;
834 priv->rx_ring[i].packets = 0;
835 priv->rx_ring[i].csum_ok = 0;
836 priv->rx_ring[i].csum_none = 0;
840 static int mlx4_en_open(struct net_device *dev)
842 struct mlx4_en_priv *priv = netdev_priv(dev);
843 struct mlx4_en_dev *mdev = priv->mdev;
844 int err = 0;
846 mutex_lock(&mdev->state_lock);
848 if (!mdev->device_up) {
849 en_err(priv, "Cannot open - device down/disabled\n");
850 err = -EBUSY;
851 goto out;
854 /* Reset HW statistics and SW counters */
855 mlx4_en_clear_stats(dev);
857 err = mlx4_en_start_port(dev);
858 if (err)
859 en_err(priv, "Failed starting port:%d\n", priv->port);
861 out:
862 mutex_unlock(&mdev->state_lock);
863 return err;
867 static int mlx4_en_close(struct net_device *dev)
869 struct mlx4_en_priv *priv = netdev_priv(dev);
870 struct mlx4_en_dev *mdev = priv->mdev;
872 en_dbg(IFDOWN, priv, "Close port called\n");
874 mutex_lock(&mdev->state_lock);
876 mlx4_en_stop_port(dev);
877 netif_carrier_off(dev);
879 mutex_unlock(&mdev->state_lock);
880 return 0;
883 void mlx4_en_free_resources(struct mlx4_en_priv *priv)
885 int i;
887 for (i = 0; i < priv->tx_ring_num; i++) {
888 if (priv->tx_ring[i].tx_info)
889 mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
890 if (priv->tx_cq[i].buf)
891 mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
894 for (i = 0; i < priv->rx_ring_num; i++) {
895 if (priv->rx_ring[i].rx_info)
896 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
897 priv->prof->rx_ring_size, priv->stride);
898 if (priv->rx_cq[i].buf)
899 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
903 int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
905 struct mlx4_en_port_profile *prof = priv->prof;
906 int i;
907 int base_tx_qpn, err;
909 err = mlx4_qp_reserve_range(priv->mdev->dev, priv->tx_ring_num, 256, &base_tx_qpn);
910 if (err) {
911 en_err(priv, "failed reserving range for TX rings\n");
912 return err;
915 /* Create tx Rings */
916 for (i = 0; i < priv->tx_ring_num; i++) {
917 if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
918 prof->tx_ring_size, i, TX))
919 goto err;
921 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i], base_tx_qpn + i,
922 prof->tx_ring_size, TXBB_SIZE))
923 goto err;
926 /* Create rx Rings */
927 for (i = 0; i < priv->rx_ring_num; i++) {
928 if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
929 prof->rx_ring_size, i, RX))
930 goto err;
932 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
933 prof->rx_ring_size, priv->stride))
934 goto err;
937 return 0;
939 err:
940 en_err(priv, "Failed to allocate NIC resources\n");
941 mlx4_qp_release_range(priv->mdev->dev, base_tx_qpn, priv->tx_ring_num);
942 return -ENOMEM;
946 void mlx4_en_destroy_netdev(struct net_device *dev)
948 struct mlx4_en_priv *priv = netdev_priv(dev);
949 struct mlx4_en_dev *mdev = priv->mdev;
951 en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
953 /* Unregister device - this will close the port if it was up */
954 if (priv->registered)
955 unregister_netdev(dev);
957 if (priv->allocated)
958 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
960 cancel_delayed_work(&priv->stats_task);
961 /* flush any pending task for this netdev */
962 flush_workqueue(mdev->workqueue);
964 /* Detach the netdev so tasks would not attempt to access it */
965 mutex_lock(&mdev->state_lock);
966 mdev->pndev[priv->port] = NULL;
967 mutex_unlock(&mdev->state_lock);
969 mlx4_en_free_resources(priv);
971 free_netdev(dev);
974 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
976 struct mlx4_en_priv *priv = netdev_priv(dev);
977 struct mlx4_en_dev *mdev = priv->mdev;
978 int err = 0;
980 en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
981 dev->mtu, new_mtu);
983 if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
984 en_err(priv, "Bad MTU size:%d.\n", new_mtu);
985 return -EPERM;
987 dev->mtu = new_mtu;
989 if (netif_running(dev)) {
990 mutex_lock(&mdev->state_lock);
991 if (!mdev->device_up) {
992 /* NIC is probably restarting - let watchdog task reset
993 * the port */
994 en_dbg(DRV, priv, "Change MTU called with card down!?\n");
995 } else {
996 mlx4_en_stop_port(dev);
997 err = mlx4_en_start_port(dev);
998 if (err) {
999 en_err(priv, "Failed restarting port:%d\n",
1000 priv->port);
1001 queue_work(mdev->workqueue, &priv->watchdog_task);
1004 mutex_unlock(&mdev->state_lock);
1006 return 0;
1009 static int mlx4_en_set_features(struct net_device *netdev,
1010 netdev_features_t features)
1012 struct mlx4_en_priv *priv = netdev_priv(netdev);
1014 if (features & NETIF_F_LOOPBACK)
1015 priv->ctrl_flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
1016 else
1017 priv->ctrl_flags &=
1018 cpu_to_be32(~MLX4_WQE_CTRL_FORCE_LOOPBACK);
1020 return 0;
1024 static const struct net_device_ops mlx4_netdev_ops = {
1025 .ndo_open = mlx4_en_open,
1026 .ndo_stop = mlx4_en_close,
1027 .ndo_start_xmit = mlx4_en_xmit,
1028 .ndo_select_queue = mlx4_en_select_queue,
1029 .ndo_get_stats = mlx4_en_get_stats,
1030 .ndo_set_rx_mode = mlx4_en_set_multicast,
1031 .ndo_set_mac_address = mlx4_en_set_mac,
1032 .ndo_validate_addr = eth_validate_addr,
1033 .ndo_change_mtu = mlx4_en_change_mtu,
1034 .ndo_tx_timeout = mlx4_en_tx_timeout,
1035 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
1036 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
1037 #ifdef CONFIG_NET_POLL_CONTROLLER
1038 .ndo_poll_controller = mlx4_en_netpoll,
1039 #endif
1040 .ndo_set_features = mlx4_en_set_features,
1043 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
1044 struct mlx4_en_port_profile *prof)
1046 struct net_device *dev;
1047 struct mlx4_en_priv *priv;
1048 int i;
1049 int err;
1051 dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
1052 prof->tx_ring_num, prof->rx_ring_num);
1053 if (dev == NULL)
1054 return -ENOMEM;
1056 SET_NETDEV_DEV(dev, &mdev->dev->pdev->dev);
1057 dev->dev_id = port - 1;
1060 * Initialize driver private data
1063 priv = netdev_priv(dev);
1064 memset(priv, 0, sizeof(struct mlx4_en_priv));
1065 priv->dev = dev;
1066 priv->mdev = mdev;
1067 priv->ddev = &mdev->pdev->dev;
1068 priv->prof = prof;
1069 priv->port = port;
1070 priv->port_up = false;
1071 priv->flags = prof->flags;
1072 priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
1073 MLX4_WQE_CTRL_SOLICITED);
1074 priv->tx_ring_num = prof->tx_ring_num;
1075 priv->rx_ring_num = prof->rx_ring_num;
1076 priv->mac_index = -1;
1077 priv->msg_enable = MLX4_EN_MSG_LEVEL;
1078 spin_lock_init(&priv->stats_lock);
1079 INIT_WORK(&priv->mcast_task, mlx4_en_do_set_multicast);
1080 INIT_WORK(&priv->mac_task, mlx4_en_do_set_mac);
1081 INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
1082 INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
1083 INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
1084 #ifdef CONFIG_MLX4_EN_DCB
1085 if (!mlx4_is_slave(priv->mdev->dev))
1086 dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
1087 #endif
1089 /* Query for default mac and max mtu */
1090 priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
1091 priv->mac = mdev->dev->caps.def_mac[priv->port];
1092 if (ILLEGAL_MAC(priv->mac)) {
1093 en_err(priv, "Port: %d, invalid mac burned: 0x%llx, quiting\n",
1094 priv->port, priv->mac);
1095 err = -EINVAL;
1096 goto out;
1099 priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
1100 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
1101 err = mlx4_en_alloc_resources(priv);
1102 if (err)
1103 goto out;
1105 /* Allocate page for receive rings */
1106 err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
1107 MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
1108 if (err) {
1109 en_err(priv, "Failed to allocate page for rx qps\n");
1110 goto out;
1112 priv->allocated = 1;
1115 * Initialize netdev entry points
1117 dev->netdev_ops = &mlx4_netdev_ops;
1118 dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
1119 netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
1120 netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
1122 SET_ETHTOOL_OPS(dev, &mlx4_en_ethtool_ops);
1124 /* Set defualt MAC */
1125 dev->addr_len = ETH_ALEN;
1126 for (i = 0; i < ETH_ALEN; i++) {
1127 dev->dev_addr[ETH_ALEN - 1 - i] = (u8) (priv->mac >> (8 * i));
1128 dev->perm_addr[ETH_ALEN - 1 - i] = (u8) (priv->mac >> (8 * i));
1132 * Set driver features
1134 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1135 if (mdev->LSO_support)
1136 dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1138 dev->vlan_features = dev->hw_features;
1140 dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
1141 dev->features = dev->hw_features | NETIF_F_HIGHDMA |
1142 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
1143 NETIF_F_HW_VLAN_FILTER;
1144 dev->hw_features |= NETIF_F_LOOPBACK;
1146 mdev->pndev[port] = dev;
1148 netif_carrier_off(dev);
1149 err = register_netdev(dev);
1150 if (err) {
1151 en_err(priv, "Netdev registration failed for port %d\n", port);
1152 goto out;
1154 priv->registered = 1;
1156 en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
1157 en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
1159 /* Configure port */
1160 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
1161 MLX4_EN_MIN_MTU,
1162 0, 0, 0, 0);
1163 if (err) {
1164 en_err(priv, "Failed setting port general configurations "
1165 "for port %d, with error %d\n", priv->port, err);
1166 goto out;
1169 /* Init port */
1170 en_warn(priv, "Initializing port\n");
1171 err = mlx4_INIT_PORT(mdev->dev, priv->port);
1172 if (err) {
1173 en_err(priv, "Failed Initializing port\n");
1174 goto out;
1176 mlx4_en_set_default_moderation(priv);
1177 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1178 return 0;
1180 out:
1181 mlx4_en_destroy_netdev(dev);
1182 return err;