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