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