net/mlx4_en: Fix mixed PFC and Global pause user control requests
[pandora-kernel.git] / drivers / net / ethernet / mellanox / mlx4 / en_netdev.c
1 /*
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 #include <linux/slab.h>
39
40 #include <linux/mlx4/driver.h>
41 #include <linux/mlx4/device.h>
42 #include <linux/mlx4/cmd.h>
43 #include <linux/mlx4/cq.h>
44
45 #include "mlx4_en.h"
46 #include "en_port.h"
47
48 static void mlx4_en_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
49 {
50         struct mlx4_en_priv *priv = netdev_priv(dev);
51         struct mlx4_en_dev *mdev = priv->mdev;
52         int err;
53         int idx;
54
55         en_dbg(HW, priv, "adding VLAN:%d\n", vid);
56
57         set_bit(vid, priv->active_vlans);
58
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");
65         }
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);
69
70 }
71
72 static void mlx4_en_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
73 {
74         struct mlx4_en_priv *priv = netdev_priv(dev);
75         struct mlx4_en_dev *mdev = priv->mdev;
76         int err;
77         int idx;
78
79         en_dbg(HW, priv, "Killing VID:%d\n", vid);
80
81         clear_bit(vid, priv->active_vlans);
82
83         /* Remove VID from port VLAN filter */
84         mutex_lock(&mdev->state_lock);
85         if (!mlx4_find_cached_vlan(mdev->dev, priv->port, vid, &idx))
86                 mlx4_unregister_vlan(mdev->dev, priv->port, idx);
87         else
88                 en_err(priv, "could not find vid %d in cache\n", vid);
89
90         if (mdev->device_up && priv->port_up) {
91                 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
92                 if (err)
93                         en_err(priv, "Failed configuring VLAN filter\n");
94         }
95         mutex_unlock(&mdev->state_lock);
96 }
97
98 u64 mlx4_en_mac_to_u64(u8 *addr)
99 {
100         u64 mac = 0;
101         int i;
102
103         for (i = 0; i < ETH_ALEN; i++) {
104                 mac <<= 8;
105                 mac |= addr[i];
106         }
107         return mac;
108 }
109
110 static int mlx4_en_set_mac(struct net_device *dev, void *addr)
111 {
112         struct mlx4_en_priv *priv = netdev_priv(dev);
113         struct mlx4_en_dev *mdev = priv->mdev;
114         struct sockaddr *saddr = addr;
115
116         if (!is_valid_ether_addr(saddr->sa_data))
117                 return -EADDRNOTAVAIL;
118
119         memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
120         priv->mac = mlx4_en_mac_to_u64(dev->dev_addr);
121         queue_work(mdev->workqueue, &priv->mac_task);
122         return 0;
123 }
124
125 static void mlx4_en_do_set_mac(struct work_struct *work)
126 {
127         struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
128                                                  mac_task);
129         struct mlx4_en_dev *mdev = priv->mdev;
130         int err = 0;
131
132         mutex_lock(&mdev->state_lock);
133         if (priv->port_up) {
134                 /* Remove old MAC and insert the new one */
135                 err = mlx4_replace_mac(mdev->dev, priv->port,
136                                        priv->base_qpn, priv->mac, 0);
137                 if (err)
138                         en_err(priv, "Failed changing HW MAC address\n");
139         } else
140                 en_dbg(HW, priv, "Port is down while "
141                                  "registering mac, exiting...\n");
142
143         mutex_unlock(&mdev->state_lock);
144 }
145
146 static void mlx4_en_clear_list(struct net_device *dev)
147 {
148         struct mlx4_en_priv *priv = netdev_priv(dev);
149
150         kfree(priv->mc_addrs);
151         priv->mc_addrs_cnt = 0;
152 }
153
154 static void mlx4_en_cache_mclist(struct net_device *dev)
155 {
156         struct mlx4_en_priv *priv = netdev_priv(dev);
157         struct netdev_hw_addr *ha;
158         char *mc_addrs;
159         int mc_addrs_cnt = netdev_mc_count(dev);
160         int i;
161
162         mc_addrs = kmalloc(mc_addrs_cnt * ETH_ALEN, GFP_ATOMIC);
163         if (!mc_addrs) {
164                 en_err(priv, "failed to allocate multicast list\n");
165                 return;
166         }
167         i = 0;
168         netdev_for_each_mc_addr(ha, dev)
169                 memcpy(mc_addrs + i++ * ETH_ALEN, ha->addr, ETH_ALEN);
170         priv->mc_addrs = mc_addrs;
171         priv->mc_addrs_cnt = mc_addrs_cnt;
172 }
173
174
175 static void mlx4_en_set_multicast(struct net_device *dev)
176 {
177         struct mlx4_en_priv *priv = netdev_priv(dev);
178
179         if (!priv->port_up)
180                 return;
181
182         queue_work(priv->mdev->workqueue, &priv->mcast_task);
183 }
184
185 static void mlx4_en_do_set_multicast(struct work_struct *work)
186 {
187         struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
188                                                  mcast_task);
189         struct mlx4_en_dev *mdev = priv->mdev;
190         struct net_device *dev = priv->dev;
191         u64 mcast_addr = 0;
192         u8 mc_list[16] = {0};
193         int err;
194
195         mutex_lock(&mdev->state_lock);
196         if (!mdev->device_up) {
197                 en_dbg(HW, priv, "Card is not up, "
198                                  "ignoring multicast change.\n");
199                 goto out;
200         }
201         if (!priv->port_up) {
202                 en_dbg(HW, priv, "Port is down, "
203                                  "ignoring  multicast change.\n");
204                 goto out;
205         }
206
207         /*
208          * Promsicuous mode: disable all filters
209          */
210
211         if (dev->flags & IFF_PROMISC) {
212                 if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
213                         if (netif_msg_rx_status(priv))
214                                 en_warn(priv, "Entering promiscuous mode\n");
215                         priv->flags |= MLX4_EN_FLAG_PROMISC;
216
217                         /* Enable promiscouos mode */
218                         if (!(mdev->dev->caps.flags &
219                                                 MLX4_DEV_CAP_FLAG_VEP_UC_STEER))
220                                 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port,
221                                                              priv->base_qpn, 1);
222                         else
223                                 err = mlx4_unicast_promisc_add(mdev->dev, priv->base_qpn,
224                                                                priv->port);
225                         if (err)
226                                 en_err(priv, "Failed enabling "
227                                              "promiscuous mode\n");
228
229                         /* Disable port multicast filter (unconditionally) */
230                         err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
231                                                   0, MLX4_MCAST_DISABLE);
232                         if (err)
233                                 en_err(priv, "Failed disabling "
234                                              "multicast filter\n");
235
236                         /* Add the default qp number as multicast promisc */
237                         if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
238                                 err = mlx4_multicast_promisc_add(mdev->dev, priv->base_qpn,
239                                                                  priv->port);
240                                 if (err)
241                                         en_err(priv, "Failed entering multicast promisc mode\n");
242                                 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
243                         }
244
245                         /* Disable port VLAN filter */
246                         err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
247                         if (err)
248                                 en_err(priv, "Failed disabling VLAN filter\n");
249                 }
250                 goto out;
251         }
252
253         /*
254          * Not in promiscuous mode
255          */
256
257         if (priv->flags & MLX4_EN_FLAG_PROMISC) {
258                 if (netif_msg_rx_status(priv))
259                         en_warn(priv, "Leaving promiscuous mode\n");
260                 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
261
262                 /* Disable promiscouos mode */
263                 if (!(mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER))
264                         err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port,
265                                                      priv->base_qpn, 0);
266                 else
267                         err = mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
268                                                           priv->port);
269                 if (err)
270                         en_err(priv, "Failed disabling promiscuous mode\n");
271
272                 /* Disable Multicast promisc */
273                 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
274                         err = mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
275                                                             priv->port);
276                         if (err)
277                                 en_err(priv, "Failed disabling multicast promiscuous mode\n");
278                         priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
279                 }
280
281                 /* Enable port VLAN filter */
282                 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
283                 if (err)
284                         en_err(priv, "Failed enabling VLAN filter\n");
285         }
286
287         /* Enable/disable the multicast filter according to IFF_ALLMULTI */
288         if (dev->flags & IFF_ALLMULTI) {
289                 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
290                                           0, MLX4_MCAST_DISABLE);
291                 if (err)
292                         en_err(priv, "Failed disabling multicast filter\n");
293
294                 /* Add the default qp number as multicast promisc */
295                 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
296                         err = mlx4_multicast_promisc_add(mdev->dev, priv->base_qpn,
297                                                          priv->port);
298                         if (err)
299                                 en_err(priv, "Failed entering multicast promisc mode\n");
300                         priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
301                 }
302         } else {
303                 int i;
304                 /* Disable Multicast promisc */
305                 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
306                         err = mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
307                                                             priv->port);
308                         if (err)
309                                 en_err(priv, "Failed disabling multicast promiscuous mode\n");
310                         priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
311                 }
312
313                 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
314                                           0, MLX4_MCAST_DISABLE);
315                 if (err)
316                         en_err(priv, "Failed disabling multicast filter\n");
317
318                 /* Detach our qp from all the multicast addresses */
319                 for (i = 0; i < priv->mc_addrs_cnt; i++) {
320                         memcpy(&mc_list[10], priv->mc_addrs + i * ETH_ALEN, ETH_ALEN);
321                         mc_list[5] = priv->port;
322                         mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
323                                               mc_list, MLX4_PROT_ETH);
324                 }
325                 /* Flush mcast filter and init it with broadcast address */
326                 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
327                                     1, MLX4_MCAST_CONFIG);
328
329                 /* Update multicast list - we cache all addresses so they won't
330                  * change while HW is updated holding the command semaphor */
331                 netif_tx_lock_bh(dev);
332                 mlx4_en_cache_mclist(dev);
333                 netif_tx_unlock_bh(dev);
334                 for (i = 0; i < priv->mc_addrs_cnt; i++) {
335                         mcast_addr =
336                               mlx4_en_mac_to_u64(priv->mc_addrs + i * ETH_ALEN);
337                         memcpy(&mc_list[10], priv->mc_addrs + i * ETH_ALEN, ETH_ALEN);
338                         mc_list[5] = priv->port;
339                         mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp,
340                                               mc_list, 0, MLX4_PROT_ETH);
341                         mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
342                                             mcast_addr, 0, MLX4_MCAST_CONFIG);
343                 }
344                 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
345                                           0, MLX4_MCAST_ENABLE);
346                 if (err)
347                         en_err(priv, "Failed enabling multicast filter\n");
348         }
349 out:
350         mutex_unlock(&mdev->state_lock);
351 }
352
353 #ifdef CONFIG_NET_POLL_CONTROLLER
354 static void mlx4_en_netpoll(struct net_device *dev)
355 {
356         struct mlx4_en_priv *priv = netdev_priv(dev);
357         struct mlx4_en_cq *cq;
358         int i;
359
360         for (i = 0; i < priv->rx_ring_num; i++) {
361                 cq = &priv->rx_cq[i];
362                 napi_schedule(&cq->napi);
363         }
364 }
365 #endif
366
367 static void mlx4_en_tx_timeout(struct net_device *dev)
368 {
369         struct mlx4_en_priv *priv = netdev_priv(dev);
370         struct mlx4_en_dev *mdev = priv->mdev;
371
372         if (netif_msg_timer(priv))
373                 en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
374
375         priv->port_stats.tx_timeout++;
376         en_dbg(DRV, priv, "Scheduling watchdog\n");
377         queue_work(mdev->workqueue, &priv->watchdog_task);
378 }
379
380
381 static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
382 {
383         struct mlx4_en_priv *priv = netdev_priv(dev);
384
385         spin_lock_bh(&priv->stats_lock);
386         memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
387         spin_unlock_bh(&priv->stats_lock);
388
389         return &priv->ret_stats;
390 }
391
392 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
393 {
394         struct mlx4_en_cq *cq;
395         int i;
396
397         /* If we haven't received a specific coalescing setting
398          * (module param), we set the moderation parameters as follows:
399          * - moder_cnt is set to the number of mtu sized packets to
400          *   satisfy our coelsing target.
401          * - moder_time is set to a fixed value.
402          */
403         priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
404         priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
405         en_dbg(INTR, priv, "Default coalesing params for mtu:%d - "
406                            "rx_frames:%d rx_usecs:%d\n",
407                  priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
408
409         /* Setup cq moderation params */
410         for (i = 0; i < priv->rx_ring_num; i++) {
411                 cq = &priv->rx_cq[i];
412                 cq->moder_cnt = priv->rx_frames;
413                 cq->moder_time = priv->rx_usecs;
414                 priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
415                 priv->last_moder_packets[i] = 0;
416                 priv->last_moder_bytes[i] = 0;
417         }
418
419         for (i = 0; i < priv->tx_ring_num; i++) {
420                 cq = &priv->tx_cq[i];
421                 cq->moder_cnt = MLX4_EN_TX_COAL_PKTS;
422                 cq->moder_time = MLX4_EN_TX_COAL_TIME;
423         }
424
425         /* Reset auto-moderation params */
426         priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
427         priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
428         priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
429         priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
430         priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
431         priv->adaptive_rx_coal = 1;
432         priv->last_moder_jiffies = 0;
433         priv->last_moder_tx_packets = 0;
434 }
435
436 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
437 {
438         unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
439         struct mlx4_en_cq *cq;
440         unsigned long packets;
441         unsigned long rate;
442         unsigned long avg_pkt_size;
443         unsigned long rx_packets;
444         unsigned long rx_bytes;
445         unsigned long rx_pkt_diff;
446         int moder_time;
447         int ring, err;
448
449         if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
450                 return;
451
452         for (ring = 0; ring < priv->rx_ring_num; ring++) {
453                 spin_lock_bh(&priv->stats_lock);
454                 rx_packets = priv->rx_ring[ring].packets;
455                 rx_bytes = priv->rx_ring[ring].bytes;
456                 spin_unlock_bh(&priv->stats_lock);
457
458                 rx_pkt_diff = ((unsigned long) (rx_packets -
459                                 priv->last_moder_packets[ring]));
460                 packets = rx_pkt_diff;
461                 rate = packets * HZ / period;
462                 avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
463                                 priv->last_moder_bytes[ring])) / packets : 0;
464
465                 /* Apply auto-moderation only when packet rate
466                  * exceeds a rate that it matters */
467                 if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
468                     avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
469                         if (rate < priv->pkt_rate_low)
470                                 moder_time = priv->rx_usecs_low;
471                         else if (rate > priv->pkt_rate_high)
472                                 moder_time = priv->rx_usecs_high;
473                         else
474                                 moder_time = (rate - priv->pkt_rate_low) *
475                                         (priv->rx_usecs_high - priv->rx_usecs_low) /
476                                         (priv->pkt_rate_high - priv->pkt_rate_low) +
477                                         priv->rx_usecs_low;
478                 } else {
479                         moder_time = priv->rx_usecs_low;
480                 }
481
482                 if (moder_time != priv->last_moder_time[ring]) {
483                         priv->last_moder_time[ring] = moder_time;
484                         cq = &priv->rx_cq[ring];
485                         cq->moder_time = moder_time;
486                         err = mlx4_en_set_cq_moder(priv, cq);
487                         if (err)
488                                 en_err(priv, "Failed modifying moderation "
489                                              "for cq:%d\n", ring);
490                 }
491                 priv->last_moder_packets[ring] = rx_packets;
492                 priv->last_moder_bytes[ring] = rx_bytes;
493         }
494
495         priv->last_moder_jiffies = jiffies;
496 }
497
498 static void mlx4_en_do_get_stats(struct work_struct *work)
499 {
500         struct delayed_work *delay = to_delayed_work(work);
501         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)
508                 en_dbg(HW, priv, "Could not update stats\n");
509
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         if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
518                 queue_work(mdev->workqueue, &priv->mac_task);
519                 mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
520         }
521         mutex_unlock(&mdev->state_lock);
522 }
523
524 static void mlx4_en_linkstate(struct work_struct *work)
525 {
526         struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
527                                                  linkstate_task);
528         struct mlx4_en_dev *mdev = priv->mdev;
529         int linkstate = priv->link_state;
530
531         mutex_lock(&mdev->state_lock);
532         /* If observable port state changed set carrier state and
533          * report to system log */
534         if (priv->last_link_state != linkstate) {
535                 if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
536                         en_info(priv, "Link Down\n");
537                         netif_carrier_off(priv->dev);
538                 } else {
539                         en_info(priv, "Link Up\n");
540                         netif_carrier_on(priv->dev);
541                 }
542         }
543         priv->last_link_state = linkstate;
544         mutex_unlock(&mdev->state_lock);
545 }
546
547
548 int mlx4_en_start_port(struct net_device *dev)
549 {
550         struct mlx4_en_priv *priv = netdev_priv(dev);
551         struct mlx4_en_dev *mdev = priv->mdev;
552         struct mlx4_en_cq *cq;
553         struct mlx4_en_tx_ring *tx_ring;
554         int rx_index = 0;
555         int tx_index = 0;
556         int err = 0;
557         int i;
558         int j;
559         u8 mc_list[16] = {0};
560
561         if (priv->port_up) {
562                 en_dbg(DRV, priv, "start port called while port already up\n");
563                 return 0;
564         }
565
566         /* Calculate Rx buf size */
567         dev->mtu = min(dev->mtu, priv->max_mtu);
568         mlx4_en_calc_rx_buf(dev);
569         en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
570
571         /* Configure rx cq's and rings */
572         err = mlx4_en_activate_rx_rings(priv);
573         if (err) {
574                 en_err(priv, "Failed to activate RX rings\n");
575                 return err;
576         }
577         for (i = 0; i < priv->rx_ring_num; i++) {
578                 cq = &priv->rx_cq[i];
579
580                 err = mlx4_en_activate_cq(priv, cq, i);
581                 if (err) {
582                         en_err(priv, "Failed activating Rx CQ\n");
583                         goto cq_err;
584                 }
585                 for (j = 0; j < cq->size; j++)
586                         cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
587                 err = mlx4_en_set_cq_moder(priv, cq);
588                 if (err) {
589                         en_err(priv, "Failed setting cq moderation parameters");
590                         mlx4_en_deactivate_cq(priv, cq);
591                         goto cq_err;
592                 }
593                 mlx4_en_arm_cq(priv, cq);
594                 priv->rx_ring[i].cqn = cq->mcq.cqn;
595                 ++rx_index;
596         }
597
598         /* Set port mac number */
599         en_dbg(DRV, priv, "Setting mac for port %d\n", priv->port);
600         err = mlx4_register_mac(mdev->dev, priv->port,
601                                 priv->mac, &priv->base_qpn, 0);
602         if (err) {
603                 en_err(priv, "Failed setting port mac\n");
604                 goto cq_err;
605         }
606         mdev->mac_removed[priv->port] = 0;
607
608         err = mlx4_en_config_rss_steer(priv);
609         if (err) {
610                 en_err(priv, "Failed configuring rss steering\n");
611                 goto mac_err;
612         }
613
614         /* Configure tx cq's and rings */
615         for (i = 0; i < priv->tx_ring_num; i++) {
616                 /* Configure cq */
617                 cq = &priv->tx_cq[i];
618                 err = mlx4_en_activate_cq(priv, cq, i);
619                 if (err) {
620                         en_err(priv, "Failed allocating Tx CQ\n");
621                         goto tx_err;
622                 }
623                 err = mlx4_en_set_cq_moder(priv, cq);
624                 if (err) {
625                         en_err(priv, "Failed setting cq moderation parameters");
626                         mlx4_en_deactivate_cq(priv, cq);
627                         goto tx_err;
628                 }
629                 en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
630                 cq->buf->wqe_index = cpu_to_be16(0xffff);
631
632                 /* Configure ring */
633                 tx_ring = &priv->tx_ring[i];
634                 err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn);
635                 if (err) {
636                         en_err(priv, "Failed allocating Tx ring\n");
637                         mlx4_en_deactivate_cq(priv, cq);
638                         goto tx_err;
639                 }
640                 /* Set initial ownership of all Tx TXBBs to SW (1) */
641                 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
642                         *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
643                 ++tx_index;
644         }
645
646         /* Configure port */
647         err = mlx4_SET_PORT_general(mdev->dev, priv->port,
648                                     priv->rx_skb_size + ETH_FCS_LEN,
649                                     priv->prof->tx_pause,
650                                     priv->prof->tx_ppp,
651                                     priv->prof->rx_pause,
652                                     priv->prof->rx_ppp);
653         if (err) {
654                 en_err(priv, "Failed setting port general configurations "
655                              "for port %d, with error %d\n", priv->port, err);
656                 goto tx_err;
657         }
658         /* Set default qp number */
659         err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
660         if (err) {
661                 en_err(priv, "Failed setting default qp numbers\n");
662                 goto tx_err;
663         }
664
665         /* Init port */
666         en_dbg(HW, priv, "Initializing port\n");
667         err = mlx4_INIT_PORT(mdev->dev, priv->port);
668         if (err) {
669                 en_err(priv, "Failed Initializing port\n");
670                 goto tx_err;
671         }
672
673         /* Attach rx QP to bradcast address */
674         memset(&mc_list[10], 0xff, ETH_ALEN);
675         mc_list[5] = priv->port;
676         if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
677                                   0, MLX4_PROT_ETH))
678                 mlx4_warn(mdev, "Failed Attaching Broadcast\n");
679
680         /* Must redo promiscuous mode setup. */
681         priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
682
683         /* Schedule multicast task to populate multicast list */
684         queue_work(mdev->workqueue, &priv->mcast_task);
685
686         priv->port_up = true;
687
688         /* Process all completions if exist to prevent
689          * the queues freezing if they are full
690          */
691         for (i = 0; i < priv->rx_ring_num; i++)
692                 napi_schedule(&priv->rx_cq[i].napi);
693
694         netif_tx_start_all_queues(dev);
695         return 0;
696
697 tx_err:
698         while (tx_index--) {
699                 mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[tx_index]);
700                 mlx4_en_deactivate_cq(priv, &priv->tx_cq[tx_index]);
701         }
702
703         mlx4_en_release_rss_steer(priv);
704 mac_err:
705         mlx4_unregister_mac(mdev->dev, priv->port, priv->base_qpn);
706 cq_err:
707         while (rx_index--)
708                 mlx4_en_deactivate_cq(priv, &priv->rx_cq[rx_index]);
709         for (i = 0; i < priv->rx_ring_num; i++)
710                 mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
711
712         return err; /* need to close devices */
713 }
714
715
716 void mlx4_en_stop_port(struct net_device *dev)
717 {
718         struct mlx4_en_priv *priv = netdev_priv(dev);
719         struct mlx4_en_dev *mdev = priv->mdev;
720         int i;
721         u8 mc_list[16] = {0};
722
723         if (!priv->port_up) {
724                 en_dbg(DRV, priv, "stop port called while port already down\n");
725                 return;
726         }
727
728         /* Synchronize with tx routine */
729         netif_tx_lock_bh(dev);
730         netif_tx_stop_all_queues(dev);
731         netif_tx_unlock_bh(dev);
732
733         /* Set port as not active */
734         priv->port_up = false;
735
736         /* Detach All multicasts */
737         memset(&mc_list[10], 0xff, ETH_ALEN);
738         mc_list[5] = priv->port;
739         mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
740                               MLX4_PROT_ETH);
741         for (i = 0; i < priv->mc_addrs_cnt; i++) {
742                 memcpy(&mc_list[10], priv->mc_addrs + i * ETH_ALEN, ETH_ALEN);
743                 mc_list[5] = priv->port;
744                 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
745                                       mc_list, MLX4_PROT_ETH);
746         }
747         mlx4_en_clear_list(dev);
748         /* Flush multicast filter */
749         mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
750
751         /* Unregister Mac address for the port */
752         mlx4_unregister_mac(mdev->dev, priv->port, priv->base_qpn);
753         mdev->mac_removed[priv->port] = 1;
754
755         /* Free TX Rings */
756         for (i = 0; i < priv->tx_ring_num; i++) {
757                 mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[i]);
758                 mlx4_en_deactivate_cq(priv, &priv->tx_cq[i]);
759         }
760         msleep(10);
761
762         for (i = 0; i < priv->tx_ring_num; i++)
763                 mlx4_en_free_tx_buf(dev, &priv->tx_ring[i]);
764
765         /* Free RSS qps */
766         mlx4_en_release_rss_steer(priv);
767
768         /* Free RX Rings */
769         for (i = 0; i < priv->rx_ring_num; i++) {
770                 mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
771                 while (test_bit(NAPI_STATE_SCHED, &priv->rx_cq[i].napi.state))
772                         msleep(1);
773                 mlx4_en_deactivate_cq(priv, &priv->rx_cq[i]);
774         }
775
776         /* close port*/
777         mlx4_CLOSE_PORT(mdev->dev, priv->port);
778 }
779
780 static void mlx4_en_restart(struct work_struct *work)
781 {
782         struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
783                                                  watchdog_task);
784         struct mlx4_en_dev *mdev = priv->mdev;
785         struct net_device *dev = priv->dev;
786
787         en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
788
789         mutex_lock(&mdev->state_lock);
790         if (priv->port_up) {
791                 mlx4_en_stop_port(dev);
792                 if (mlx4_en_start_port(dev))
793                         en_err(priv, "Failed restarting port %d\n", priv->port);
794         }
795         mutex_unlock(&mdev->state_lock);
796 }
797
798
799 static int mlx4_en_open(struct net_device *dev)
800 {
801         struct mlx4_en_priv *priv = netdev_priv(dev);
802         struct mlx4_en_dev *mdev = priv->mdev;
803         int i;
804         int err = 0;
805
806         mutex_lock(&mdev->state_lock);
807
808         if (!mdev->device_up) {
809                 en_err(priv, "Cannot open - device down/disabled\n");
810                 err = -EBUSY;
811                 goto out;
812         }
813
814         /* Reset HW statistics and performance counters */
815         if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
816                 en_dbg(HW, priv, "Failed dumping statistics\n");
817
818         memset(&priv->stats, 0, sizeof(priv->stats));
819         memset(&priv->pstats, 0, sizeof(priv->pstats));
820
821         for (i = 0; i < priv->tx_ring_num; i++) {
822                 priv->tx_ring[i].bytes = 0;
823                 priv->tx_ring[i].packets = 0;
824         }
825         for (i = 0; i < priv->rx_ring_num; i++) {
826                 priv->rx_ring[i].bytes = 0;
827                 priv->rx_ring[i].packets = 0;
828         }
829
830         err = mlx4_en_start_port(dev);
831         if (err)
832                 en_err(priv, "Failed starting port:%d\n", priv->port);
833
834 out:
835         mutex_unlock(&mdev->state_lock);
836         return err;
837 }
838
839
840 static int mlx4_en_close(struct net_device *dev)
841 {
842         struct mlx4_en_priv *priv = netdev_priv(dev);
843         struct mlx4_en_dev *mdev = priv->mdev;
844
845         en_dbg(IFDOWN, priv, "Close port called\n");
846
847         mutex_lock(&mdev->state_lock);
848
849         mlx4_en_stop_port(dev);
850         netif_carrier_off(dev);
851
852         mutex_unlock(&mdev->state_lock);
853         return 0;
854 }
855
856 void mlx4_en_free_resources(struct mlx4_en_priv *priv)
857 {
858         int i;
859
860         for (i = 0; i < priv->tx_ring_num; i++) {
861                 if (priv->tx_ring[i].tx_info)
862                         mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
863                 if (priv->tx_cq[i].buf)
864                         mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
865         }
866
867         for (i = 0; i < priv->rx_ring_num; i++) {
868                 if (priv->rx_ring[i].rx_info)
869                         mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i]);
870                 if (priv->rx_cq[i].buf)
871                         mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
872         }
873 }
874
875 int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
876 {
877         struct mlx4_en_port_profile *prof = priv->prof;
878         int i;
879         int base_tx_qpn, err;
880
881         err = mlx4_qp_reserve_range(priv->mdev->dev, priv->tx_ring_num, 256, &base_tx_qpn);
882         if (err) {
883                 en_err(priv, "failed reserving range for TX rings\n");
884                 return err;
885         }
886
887         /* Create tx Rings */
888         for (i = 0; i < priv->tx_ring_num; i++) {
889                 if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
890                                       prof->tx_ring_size, i, TX))
891                         goto err;
892
893                 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i], base_tx_qpn + i,
894                                            prof->tx_ring_size, TXBB_SIZE))
895                         goto err;
896         }
897
898         /* Create rx Rings */
899         for (i = 0; i < priv->rx_ring_num; i++) {
900                 if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
901                                       prof->rx_ring_size, i, RX))
902                         goto err;
903
904                 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
905                                            prof->rx_ring_size, priv->stride))
906                         goto err;
907         }
908
909         return 0;
910
911 err:
912         en_err(priv, "Failed to allocate NIC resources\n");
913         mlx4_qp_release_range(priv->mdev->dev, base_tx_qpn, priv->tx_ring_num);
914         return -ENOMEM;
915 }
916
917
918 void mlx4_en_destroy_netdev(struct net_device *dev)
919 {
920         struct mlx4_en_priv *priv = netdev_priv(dev);
921         struct mlx4_en_dev *mdev = priv->mdev;
922
923         en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
924
925         /* Unregister device - this will close the port if it was up */
926         if (priv->registered)
927                 unregister_netdev(dev);
928
929         if (priv->allocated)
930                 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
931
932         cancel_delayed_work(&priv->stats_task);
933         /* flush any pending task for this netdev */
934         flush_workqueue(mdev->workqueue);
935
936         /* Detach the netdev so tasks would not attempt to access it */
937         mutex_lock(&mdev->state_lock);
938         mdev->pndev[priv->port] = NULL;
939         mutex_unlock(&mdev->state_lock);
940
941         mlx4_en_free_resources(priv);
942         free_netdev(dev);
943 }
944
945 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
946 {
947         struct mlx4_en_priv *priv = netdev_priv(dev);
948         struct mlx4_en_dev *mdev = priv->mdev;
949         int err = 0;
950
951         en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
952                  dev->mtu, new_mtu);
953
954         if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
955                 en_err(priv, "Bad MTU size:%d.\n", new_mtu);
956                 return -EPERM;
957         }
958         dev->mtu = new_mtu;
959
960         if (netif_running(dev)) {
961                 mutex_lock(&mdev->state_lock);
962                 if (!mdev->device_up) {
963                         /* NIC is probably restarting - let watchdog task reset
964                          * the port */
965                         en_dbg(DRV, priv, "Change MTU called with card down!?\n");
966                 } else {
967                         mlx4_en_stop_port(dev);
968                         err = mlx4_en_start_port(dev);
969                         if (err) {
970                                 en_err(priv, "Failed restarting port:%d\n",
971                                          priv->port);
972                                 queue_work(mdev->workqueue, &priv->watchdog_task);
973                         }
974                 }
975                 mutex_unlock(&mdev->state_lock);
976         }
977         return 0;
978 }
979
980 static const struct net_device_ops mlx4_netdev_ops = {
981         .ndo_open               = mlx4_en_open,
982         .ndo_stop               = mlx4_en_close,
983         .ndo_start_xmit         = mlx4_en_xmit,
984         .ndo_select_queue       = mlx4_en_select_queue,
985         .ndo_get_stats          = mlx4_en_get_stats,
986         .ndo_set_rx_mode        = mlx4_en_set_multicast,
987         .ndo_set_mac_address    = mlx4_en_set_mac,
988         .ndo_validate_addr      = eth_validate_addr,
989         .ndo_change_mtu         = mlx4_en_change_mtu,
990         .ndo_tx_timeout         = mlx4_en_tx_timeout,
991         .ndo_vlan_rx_add_vid    = mlx4_en_vlan_rx_add_vid,
992         .ndo_vlan_rx_kill_vid   = mlx4_en_vlan_rx_kill_vid,
993 #ifdef CONFIG_NET_POLL_CONTROLLER
994         .ndo_poll_controller    = mlx4_en_netpoll,
995 #endif
996 };
997
998 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
999                         struct mlx4_en_port_profile *prof)
1000 {
1001         struct net_device *dev;
1002         struct mlx4_en_priv *priv;
1003         int i;
1004         int err;
1005
1006         dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
1007             prof->tx_ring_num, prof->rx_ring_num);
1008         if (dev == NULL) {
1009                 mlx4_err(mdev, "Net device allocation failed\n");
1010                 return -ENOMEM;
1011         }
1012
1013         SET_NETDEV_DEV(dev, &mdev->dev->pdev->dev);
1014         dev->dev_id =  port - 1;
1015
1016         /*
1017          * Initialize driver private data
1018          */
1019
1020         priv = netdev_priv(dev);
1021         memset(priv, 0, sizeof(struct mlx4_en_priv));
1022         priv->dev = dev;
1023         priv->mdev = mdev;
1024         priv->prof = prof;
1025         priv->port = port;
1026         priv->port_up = false;
1027         priv->flags = prof->flags;
1028         priv->tx_ring_num = prof->tx_ring_num;
1029         priv->rx_ring_num = prof->rx_ring_num;
1030         priv->mac_index = -1;
1031         priv->msg_enable = MLX4_EN_MSG_LEVEL;
1032         spin_lock_init(&priv->stats_lock);
1033         INIT_WORK(&priv->mcast_task, mlx4_en_do_set_multicast);
1034         INIT_WORK(&priv->mac_task, mlx4_en_do_set_mac);
1035         INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
1036         INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
1037         INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
1038
1039         /* Query for default mac and max mtu */
1040         priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
1041         priv->mac = mdev->dev->caps.def_mac[priv->port];
1042         if (ILLEGAL_MAC(priv->mac)) {
1043                 en_err(priv, "Port: %d, invalid mac burned: 0x%llx, quiting\n",
1044                          priv->port, priv->mac);
1045                 err = -EINVAL;
1046                 goto out;
1047         }
1048
1049         priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
1050                                           DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
1051         err = mlx4_en_alloc_resources(priv);
1052         if (err)
1053                 goto out;
1054
1055         /* Allocate page for receive rings */
1056         err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
1057                                 MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
1058         if (err) {
1059                 en_err(priv, "Failed to allocate page for rx qps\n");
1060                 goto out;
1061         }
1062         priv->allocated = 1;
1063
1064         /*
1065          * Initialize netdev entry points
1066          */
1067         dev->netdev_ops = &mlx4_netdev_ops;
1068         dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
1069         netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
1070         netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
1071
1072         SET_ETHTOOL_OPS(dev, &mlx4_en_ethtool_ops);
1073
1074         /* Set defualt MAC */
1075         dev->addr_len = ETH_ALEN;
1076         for (i = 0; i < ETH_ALEN; i++) {
1077                 dev->dev_addr[ETH_ALEN - 1 - i] = (u8) (priv->mac >> (8 * i));
1078                 dev->perm_addr[ETH_ALEN - 1 - i] = (u8) (priv->mac >> (8 * i));
1079         }
1080
1081         /*
1082          * Set driver features
1083          */
1084         dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1085         if (mdev->LSO_support)
1086                 dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1087
1088         dev->vlan_features = dev->hw_features;
1089
1090         dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
1091         dev->features = dev->hw_features | NETIF_F_HIGHDMA |
1092                         NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
1093                         NETIF_F_HW_VLAN_FILTER;
1094
1095         mdev->pndev[port] = dev;
1096
1097         netif_carrier_off(dev);
1098         err = register_netdev(dev);
1099         if (err) {
1100                 en_err(priv, "Netdev registration failed for port %d\n", port);
1101                 goto out;
1102         }
1103         priv->registered = 1;
1104
1105         en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
1106         en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
1107
1108         /* Configure port */
1109         err = mlx4_SET_PORT_general(mdev->dev, priv->port,
1110                                     MLX4_EN_MIN_MTU,
1111                                     0, 0, 0, 0);
1112         if (err) {
1113                 en_err(priv, "Failed setting port general configurations "
1114                        "for port %d, with error %d\n", priv->port, err);
1115                 goto out;
1116         }
1117
1118         /* Init port */
1119         en_warn(priv, "Initializing port\n");
1120         err = mlx4_INIT_PORT(mdev->dev, priv->port);
1121         if (err) {
1122                 en_err(priv, "Failed Initializing port\n");
1123                 goto out;
1124         }
1125         mlx4_en_set_default_moderation(priv);
1126         queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1127         return 0;
1128
1129 out:
1130         mlx4_en_destroy_netdev(dev);
1131         return err;
1132 }
1133