bd2d75265389720dae49b49e8024380bce29f424
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / net / ixgbe / ixgbe_dcb_nl.c
1 /*******************************************************************************
2
3   Intel 10 Gigabit PCI Express Linux driver
4   Copyright(c) 1999 - 2011 Intel Corporation.
5
6   This program is free software; you can redistribute it and/or modify it
7   under the terms and conditions of the GNU General Public License,
8   version 2, as published by the Free Software Foundation.
9
10   This program is distributed in the hope it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13   more details.
14
15   You should have received a copy of the GNU General Public License along with
16   this program; if not, write to the Free Software Foundation, Inc.,
17   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21
22   Contact Information:
23   Linux NICS <linux.nics@intel.com>
24   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27 *******************************************************************************/
28
29 #include "ixgbe.h"
30 #include <linux/dcbnl.h>
31 #include "ixgbe_dcb_82598.h"
32 #include "ixgbe_dcb_82599.h"
33
34 /* Callbacks for DCB netlink in the kernel */
35 #define BIT_DCB_MODE    0x01
36 #define BIT_PFC         0x02
37 #define BIT_PG_RX       0x04
38 #define BIT_PG_TX       0x08
39 #define BIT_APP_UPCHG   0x10
40 #define BIT_LINKSPEED   0x80
41
42 /* Responses for the DCB_C_SET_ALL command */
43 #define DCB_HW_CHG_RST  0  /* DCB configuration changed with reset */
44 #define DCB_NO_HW_CHG   1  /* DCB configuration did not change */
45 #define DCB_HW_CHG      2  /* DCB configuration changed, no reset */
46
47 int ixgbe_copy_dcb_cfg(struct ixgbe_dcb_config *src_dcb_cfg,
48                        struct ixgbe_dcb_config *dst_dcb_cfg, int tc_max)
49 {
50         struct tc_configuration *src_tc_cfg = NULL;
51         struct tc_configuration *dst_tc_cfg = NULL;
52         int i;
53
54         if (!src_dcb_cfg || !dst_dcb_cfg)
55                 return -EINVAL;
56
57         for (i = DCB_PG_ATTR_TC_0; i < tc_max + DCB_PG_ATTR_TC_0; i++) {
58                 src_tc_cfg = &src_dcb_cfg->tc_config[i - DCB_PG_ATTR_TC_0];
59                 dst_tc_cfg = &dst_dcb_cfg->tc_config[i - DCB_PG_ATTR_TC_0];
60
61                 dst_tc_cfg->path[DCB_TX_CONFIG].prio_type =
62                                 src_tc_cfg->path[DCB_TX_CONFIG].prio_type;
63
64                 dst_tc_cfg->path[DCB_TX_CONFIG].bwg_id =
65                                 src_tc_cfg->path[DCB_TX_CONFIG].bwg_id;
66
67                 dst_tc_cfg->path[DCB_TX_CONFIG].bwg_percent =
68                                 src_tc_cfg->path[DCB_TX_CONFIG].bwg_percent;
69
70                 dst_tc_cfg->path[DCB_TX_CONFIG].up_to_tc_bitmap =
71                                 src_tc_cfg->path[DCB_TX_CONFIG].up_to_tc_bitmap;
72
73                 dst_tc_cfg->path[DCB_RX_CONFIG].prio_type =
74                                 src_tc_cfg->path[DCB_RX_CONFIG].prio_type;
75
76                 dst_tc_cfg->path[DCB_RX_CONFIG].bwg_id =
77                                 src_tc_cfg->path[DCB_RX_CONFIG].bwg_id;
78
79                 dst_tc_cfg->path[DCB_RX_CONFIG].bwg_percent =
80                                 src_tc_cfg->path[DCB_RX_CONFIG].bwg_percent;
81
82                 dst_tc_cfg->path[DCB_RX_CONFIG].up_to_tc_bitmap =
83                                 src_tc_cfg->path[DCB_RX_CONFIG].up_to_tc_bitmap;
84         }
85
86         for (i = DCB_PG_ATTR_BW_ID_0; i < DCB_PG_ATTR_BW_ID_MAX; i++) {
87                 dst_dcb_cfg->bw_percentage[DCB_TX_CONFIG]
88                         [i-DCB_PG_ATTR_BW_ID_0] = src_dcb_cfg->bw_percentage
89                                 [DCB_TX_CONFIG][i-DCB_PG_ATTR_BW_ID_0];
90                 dst_dcb_cfg->bw_percentage[DCB_RX_CONFIG]
91                         [i-DCB_PG_ATTR_BW_ID_0] = src_dcb_cfg->bw_percentage
92                                 [DCB_RX_CONFIG][i-DCB_PG_ATTR_BW_ID_0];
93         }
94
95         for (i = DCB_PFC_UP_ATTR_0; i < DCB_PFC_UP_ATTR_MAX; i++) {
96                 dst_dcb_cfg->tc_config[i - DCB_PFC_UP_ATTR_0].dcb_pfc =
97                         src_dcb_cfg->tc_config[i - DCB_PFC_UP_ATTR_0].dcb_pfc;
98         }
99
100         dst_dcb_cfg->pfc_mode_enable = src_dcb_cfg->pfc_mode_enable;
101
102         return 0;
103 }
104
105 static u8 ixgbe_dcbnl_get_state(struct net_device *netdev)
106 {
107         struct ixgbe_adapter *adapter = netdev_priv(netdev);
108
109         return !!(adapter->flags & IXGBE_FLAG_DCB_ENABLED);
110 }
111
112 static u8 ixgbe_dcbnl_set_state(struct net_device *netdev, u8 state)
113 {
114         u8 err = 0;
115         struct ixgbe_adapter *adapter = netdev_priv(netdev);
116
117         /* verify there is something to do, if not then exit */
118         if (!!state != !(adapter->flags & IXGBE_FLAG_DCB_ENABLED))
119                 return err;
120
121         if (state > 0) {
122                 /* Turn on DCB */
123                 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED)) {
124                         e_err(drv, "Enable failed, needs MSI-X\n");
125                         err = 1;
126                         goto out;
127                 }
128
129                 adapter->flags |= IXGBE_FLAG_DCB_ENABLED;
130
131                 switch (adapter->hw.mac.type) {
132                 case ixgbe_mac_82598EB:
133                         adapter->last_lfc_mode = adapter->hw.fc.current_mode;
134                         adapter->hw.fc.requested_mode = ixgbe_fc_none;
135                         break;
136                 case ixgbe_mac_82599EB:
137                 case ixgbe_mac_X540:
138                         adapter->flags &= ~IXGBE_FLAG_FDIR_HASH_CAPABLE;
139                         break;
140                 default:
141                         break;
142                 }
143
144                 ixgbe_setup_tc(netdev, MAX_TRAFFIC_CLASS);
145         } else {
146                 /* Turn off DCB */
147                 adapter->hw.fc.requested_mode = adapter->last_lfc_mode;
148                 adapter->temp_dcb_cfg.pfc_mode_enable = false;
149                 adapter->dcb_cfg.pfc_mode_enable = false;
150                 adapter->flags &= ~IXGBE_FLAG_DCB_ENABLED;
151                 switch (adapter->hw.mac.type) {
152                 case ixgbe_mac_82599EB:
153                 case ixgbe_mac_X540:
154                         if (!(adapter->flags & IXGBE_FLAG_FDIR_PERFECT_CAPABLE))
155                                 adapter->flags |= IXGBE_FLAG_FDIR_HASH_CAPABLE;
156                         break;
157                 default:
158                         break;
159                 }
160                 ixgbe_setup_tc(netdev, 0);
161         }
162
163 out:
164         return err;
165 }
166
167 static void ixgbe_dcbnl_get_perm_hw_addr(struct net_device *netdev,
168                                          u8 *perm_addr)
169 {
170         struct ixgbe_adapter *adapter = netdev_priv(netdev);
171         int i, j;
172
173         memset(perm_addr, 0xff, MAX_ADDR_LEN);
174
175         for (i = 0; i < netdev->addr_len; i++)
176                 perm_addr[i] = adapter->hw.mac.perm_addr[i];
177
178         switch (adapter->hw.mac.type) {
179         case ixgbe_mac_82599EB:
180         case ixgbe_mac_X540:
181                 for (j = 0; j < netdev->addr_len; j++, i++)
182                         perm_addr[i] = adapter->hw.mac.san_addr[j];
183                 break;
184         default:
185                 break;
186         }
187 }
188
189 static void ixgbe_dcbnl_set_pg_tc_cfg_tx(struct net_device *netdev, int tc,
190                                          u8 prio, u8 bwg_id, u8 bw_pct,
191                                          u8 up_map)
192 {
193         struct ixgbe_adapter *adapter = netdev_priv(netdev);
194
195         if (prio != DCB_ATTR_VALUE_UNDEFINED)
196                 adapter->temp_dcb_cfg.tc_config[tc].path[0].prio_type = prio;
197         if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
198                 adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_id = bwg_id;
199         if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
200                 adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_percent =
201                         bw_pct;
202         if (up_map != DCB_ATTR_VALUE_UNDEFINED)
203                 adapter->temp_dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap =
204                         up_map;
205
206         if ((adapter->temp_dcb_cfg.tc_config[tc].path[0].prio_type !=
207              adapter->dcb_cfg.tc_config[tc].path[0].prio_type) ||
208             (adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_id !=
209              adapter->dcb_cfg.tc_config[tc].path[0].bwg_id) ||
210             (adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_percent !=
211              adapter->dcb_cfg.tc_config[tc].path[0].bwg_percent) ||
212             (adapter->temp_dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap !=
213              adapter->dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap))
214                 adapter->dcb_set_bitmap |= BIT_PG_TX;
215 }
216
217 static void ixgbe_dcbnl_set_pg_bwg_cfg_tx(struct net_device *netdev, int bwg_id,
218                                           u8 bw_pct)
219 {
220         struct ixgbe_adapter *adapter = netdev_priv(netdev);
221
222         adapter->temp_dcb_cfg.bw_percentage[0][bwg_id] = bw_pct;
223
224         if (adapter->temp_dcb_cfg.bw_percentage[0][bwg_id] !=
225             adapter->dcb_cfg.bw_percentage[0][bwg_id])
226                 adapter->dcb_set_bitmap |= BIT_PG_TX;
227 }
228
229 static void ixgbe_dcbnl_set_pg_tc_cfg_rx(struct net_device *netdev, int tc,
230                                          u8 prio, u8 bwg_id, u8 bw_pct,
231                                          u8 up_map)
232 {
233         struct ixgbe_adapter *adapter = netdev_priv(netdev);
234
235         if (prio != DCB_ATTR_VALUE_UNDEFINED)
236                 adapter->temp_dcb_cfg.tc_config[tc].path[1].prio_type = prio;
237         if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
238                 adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_id = bwg_id;
239         if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
240                 adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_percent =
241                         bw_pct;
242         if (up_map != DCB_ATTR_VALUE_UNDEFINED)
243                 adapter->temp_dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap =
244                         up_map;
245
246         if ((adapter->temp_dcb_cfg.tc_config[tc].path[1].prio_type !=
247              adapter->dcb_cfg.tc_config[tc].path[1].prio_type) ||
248             (adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_id !=
249              adapter->dcb_cfg.tc_config[tc].path[1].bwg_id) ||
250             (adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_percent !=
251              adapter->dcb_cfg.tc_config[tc].path[1].bwg_percent) ||
252             (adapter->temp_dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap !=
253              adapter->dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap))
254                 adapter->dcb_set_bitmap |= BIT_PG_RX;
255 }
256
257 static void ixgbe_dcbnl_set_pg_bwg_cfg_rx(struct net_device *netdev, int bwg_id,
258                                           u8 bw_pct)
259 {
260         struct ixgbe_adapter *adapter = netdev_priv(netdev);
261
262         adapter->temp_dcb_cfg.bw_percentage[1][bwg_id] = bw_pct;
263
264         if (adapter->temp_dcb_cfg.bw_percentage[1][bwg_id] !=
265             adapter->dcb_cfg.bw_percentage[1][bwg_id])
266                 adapter->dcb_set_bitmap |= BIT_PG_RX;
267 }
268
269 static void ixgbe_dcbnl_get_pg_tc_cfg_tx(struct net_device *netdev, int tc,
270                                          u8 *prio, u8 *bwg_id, u8 *bw_pct,
271                                          u8 *up_map)
272 {
273         struct ixgbe_adapter *adapter = netdev_priv(netdev);
274
275         *prio = adapter->dcb_cfg.tc_config[tc].path[0].prio_type;
276         *bwg_id = adapter->dcb_cfg.tc_config[tc].path[0].bwg_id;
277         *bw_pct = adapter->dcb_cfg.tc_config[tc].path[0].bwg_percent;
278         *up_map = adapter->dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap;
279 }
280
281 static void ixgbe_dcbnl_get_pg_bwg_cfg_tx(struct net_device *netdev, int bwg_id,
282                                           u8 *bw_pct)
283 {
284         struct ixgbe_adapter *adapter = netdev_priv(netdev);
285
286         *bw_pct = adapter->dcb_cfg.bw_percentage[0][bwg_id];
287 }
288
289 static void ixgbe_dcbnl_get_pg_tc_cfg_rx(struct net_device *netdev, int tc,
290                                          u8 *prio, u8 *bwg_id, u8 *bw_pct,
291                                          u8 *up_map)
292 {
293         struct ixgbe_adapter *adapter = netdev_priv(netdev);
294
295         *prio = adapter->dcb_cfg.tc_config[tc].path[1].prio_type;
296         *bwg_id = adapter->dcb_cfg.tc_config[tc].path[1].bwg_id;
297         *bw_pct = adapter->dcb_cfg.tc_config[tc].path[1].bwg_percent;
298         *up_map = adapter->dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap;
299 }
300
301 static void ixgbe_dcbnl_get_pg_bwg_cfg_rx(struct net_device *netdev, int bwg_id,
302                                           u8 *bw_pct)
303 {
304         struct ixgbe_adapter *adapter = netdev_priv(netdev);
305
306         *bw_pct = adapter->dcb_cfg.bw_percentage[1][bwg_id];
307 }
308
309 static void ixgbe_dcbnl_set_pfc_cfg(struct net_device *netdev, int priority,
310                                     u8 setting)
311 {
312         struct ixgbe_adapter *adapter = netdev_priv(netdev);
313
314         adapter->temp_dcb_cfg.tc_config[priority].dcb_pfc = setting;
315         if (adapter->temp_dcb_cfg.tc_config[priority].dcb_pfc !=
316             adapter->dcb_cfg.tc_config[priority].dcb_pfc) {
317                 adapter->dcb_set_bitmap |= BIT_PFC;
318                 adapter->temp_dcb_cfg.pfc_mode_enable = true;
319         }
320 }
321
322 static void ixgbe_dcbnl_get_pfc_cfg(struct net_device *netdev, int priority,
323                                     u8 *setting)
324 {
325         struct ixgbe_adapter *adapter = netdev_priv(netdev);
326
327         *setting = adapter->dcb_cfg.tc_config[priority].dcb_pfc;
328 }
329
330 static u8 ixgbe_dcbnl_set_all(struct net_device *netdev)
331 {
332         struct ixgbe_adapter *adapter = netdev_priv(netdev);
333         struct dcb_app app = {
334                               .selector = DCB_APP_IDTYPE_ETHTYPE,
335                               .protocol = ETH_P_FCOE,
336                              };
337         u8 up = dcb_getapp(netdev, &app);
338         int ret;
339
340         ret = ixgbe_copy_dcb_cfg(&adapter->temp_dcb_cfg, &adapter->dcb_cfg,
341                                  MAX_TRAFFIC_CLASS);
342         if (ret)
343                 return DCB_NO_HW_CHG;
344
345         /* In IEEE mode app data must be parsed into DCBX format for
346          * hardware routines.
347          */
348         if (adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE)
349                 up = (1 << up);
350
351 #ifdef IXGBE_FCOE
352         if (up && (up != (1 << adapter->fcoe.up)))
353                 adapter->dcb_set_bitmap |= BIT_APP_UPCHG;
354
355         /*
356          * Only take down the adapter if an app change occurred. FCoE
357          * may shuffle tx rings in this case and this can not be done
358          * without a reset currently.
359          */
360         if (adapter->dcb_set_bitmap & BIT_APP_UPCHG) {
361                 while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
362                         usleep_range(1000, 2000);
363
364                 ixgbe_fcoe_setapp(adapter, up);
365
366                 if (netif_running(netdev))
367                         netdev->netdev_ops->ndo_stop(netdev);
368                 ixgbe_clear_interrupt_scheme(adapter);
369         }
370 #endif
371
372         if (adapter->dcb_cfg.pfc_mode_enable) {
373                 switch (adapter->hw.mac.type) {
374                 case ixgbe_mac_82599EB:
375                 case ixgbe_mac_X540:
376                         if (adapter->hw.fc.current_mode != ixgbe_fc_pfc)
377                                 adapter->last_lfc_mode =
378                                                   adapter->hw.fc.current_mode;
379                         break;
380                 default:
381                         break;
382                 }
383                 adapter->hw.fc.requested_mode = ixgbe_fc_pfc;
384         } else {
385                 switch (adapter->hw.mac.type) {
386                 case ixgbe_mac_82598EB:
387                         adapter->hw.fc.requested_mode = ixgbe_fc_none;
388                         break;
389                 case ixgbe_mac_82599EB:
390                 case ixgbe_mac_X540:
391                         adapter->hw.fc.requested_mode = adapter->last_lfc_mode;
392                         break;
393                 default:
394                         break;
395                 }
396         }
397
398 #ifdef IXGBE_FCOE
399         if (adapter->dcb_set_bitmap & BIT_APP_UPCHG) {
400                 ixgbe_init_interrupt_scheme(adapter);
401                 if (netif_running(netdev))
402                         netdev->netdev_ops->ndo_open(netdev);
403                 ret = DCB_HW_CHG_RST;
404         }
405 #endif
406
407         if (adapter->dcb_set_bitmap & BIT_PFC) {
408                 u8 pfc_en;
409                 ixgbe_dcb_unpack_pfc(&adapter->dcb_cfg, &pfc_en);
410                 ixgbe_dcb_hw_pfc_config(&adapter->hw, pfc_en);
411                 ret = DCB_HW_CHG;
412         }
413
414         if (adapter->dcb_set_bitmap & (BIT_PG_TX|BIT_PG_RX)) {
415                 u16 refill[MAX_TRAFFIC_CLASS], max[MAX_TRAFFIC_CLASS];
416                 u8 bwg_id[MAX_TRAFFIC_CLASS], prio_type[MAX_TRAFFIC_CLASS];
417                 /* Priority to TC mapping in CEE case default to 1:1 */
418                 u8 prio_tc[MAX_TRAFFIC_CLASS] = {0, 1, 2, 3, 4, 5, 6, 7};
419                 int max_frame = adapter->netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
420
421 #ifdef CONFIG_FCOE
422                 if (adapter->netdev->features & NETIF_F_FCOE_MTU)
423                         max_frame = max(max_frame, IXGBE_FCOE_JUMBO_FRAME_SIZE);
424 #endif
425
426                 ixgbe_dcb_calculate_tc_credits(&adapter->hw, &adapter->dcb_cfg,
427                                                max_frame, DCB_TX_CONFIG);
428                 ixgbe_dcb_calculate_tc_credits(&adapter->hw, &adapter->dcb_cfg,
429                                                max_frame, DCB_RX_CONFIG);
430
431                 ixgbe_dcb_unpack_refill(&adapter->dcb_cfg,
432                                         DCB_TX_CONFIG, refill);
433                 ixgbe_dcb_unpack_max(&adapter->dcb_cfg, max);
434                 ixgbe_dcb_unpack_bwgid(&adapter->dcb_cfg,
435                                        DCB_TX_CONFIG, bwg_id);
436                 ixgbe_dcb_unpack_prio(&adapter->dcb_cfg,
437                                       DCB_TX_CONFIG, prio_type);
438
439                 ixgbe_dcb_hw_ets_config(&adapter->hw, refill, max,
440                                         bwg_id, prio_type, prio_tc);
441         }
442
443         if (adapter->dcb_cfg.pfc_mode_enable)
444                 adapter->hw.fc.current_mode = ixgbe_fc_pfc;
445
446         if (adapter->dcb_set_bitmap & BIT_APP_UPCHG)
447                 clear_bit(__IXGBE_RESETTING, &adapter->state);
448         adapter->dcb_set_bitmap = 0x00;
449         return ret;
450 }
451
452 static u8 ixgbe_dcbnl_getcap(struct net_device *netdev, int capid, u8 *cap)
453 {
454         struct ixgbe_adapter *adapter = netdev_priv(netdev);
455
456         switch (capid) {
457         case DCB_CAP_ATTR_PG:
458                 *cap = true;
459                 break;
460         case DCB_CAP_ATTR_PFC:
461                 *cap = true;
462                 break;
463         case DCB_CAP_ATTR_UP2TC:
464                 *cap = false;
465                 break;
466         case DCB_CAP_ATTR_PG_TCS:
467                 *cap = 0x80;
468                 break;
469         case DCB_CAP_ATTR_PFC_TCS:
470                 *cap = 0x80;
471                 break;
472         case DCB_CAP_ATTR_GSP:
473                 *cap = true;
474                 break;
475         case DCB_CAP_ATTR_BCN:
476                 *cap = false;
477                 break;
478         case DCB_CAP_ATTR_DCBX:
479                 *cap = adapter->dcbx_cap;
480                 break;
481         default:
482                 *cap = false;
483                 break;
484         }
485
486         return 0;
487 }
488
489 static u8 ixgbe_dcbnl_getnumtcs(struct net_device *netdev, int tcid, u8 *num)
490 {
491         struct ixgbe_adapter *adapter = netdev_priv(netdev);
492         u8 rval = 0;
493
494         if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
495                 switch (tcid) {
496                 case DCB_NUMTCS_ATTR_PG:
497                         *num = MAX_TRAFFIC_CLASS;
498                         break;
499                 case DCB_NUMTCS_ATTR_PFC:
500                         *num = MAX_TRAFFIC_CLASS;
501                         break;
502                 default:
503                         rval = -EINVAL;
504                         break;
505                 }
506         } else {
507                 rval = -EINVAL;
508         }
509
510         return rval;
511 }
512
513 static u8 ixgbe_dcbnl_setnumtcs(struct net_device *netdev, int tcid, u8 num)
514 {
515         return -EINVAL;
516 }
517
518 static u8 ixgbe_dcbnl_getpfcstate(struct net_device *netdev)
519 {
520         struct ixgbe_adapter *adapter = netdev_priv(netdev);
521
522         return adapter->dcb_cfg.pfc_mode_enable;
523 }
524
525 static void ixgbe_dcbnl_setpfcstate(struct net_device *netdev, u8 state)
526 {
527         struct ixgbe_adapter *adapter = netdev_priv(netdev);
528
529         adapter->temp_dcb_cfg.pfc_mode_enable = state;
530         if (adapter->temp_dcb_cfg.pfc_mode_enable !=
531                 adapter->dcb_cfg.pfc_mode_enable)
532                 adapter->dcb_set_bitmap |= BIT_PFC;
533 }
534
535 /**
536  * ixgbe_dcbnl_getapp - retrieve the DCBX application user priority
537  * @netdev : the corresponding netdev
538  * @idtype : identifies the id as ether type or TCP/UDP port number
539  * @id: id is either ether type or TCP/UDP port number
540  *
541  * Returns : on success, returns a non-zero 802.1p user priority bitmap
542  * otherwise returns 0 as the invalid user priority bitmap to indicate an
543  * error.
544  */
545 static u8 ixgbe_dcbnl_getapp(struct net_device *netdev, u8 idtype, u16 id)
546 {
547         struct ixgbe_adapter *adapter = netdev_priv(netdev);
548         struct dcb_app app = {
549                                 .selector = idtype,
550                                 .protocol = id,
551                              };
552
553         if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
554                 return 0;
555
556         return dcb_getapp(netdev, &app);
557 }
558
559 static int ixgbe_dcbnl_ieee_getets(struct net_device *dev,
560                                    struct ieee_ets *ets)
561 {
562         struct ixgbe_adapter *adapter = netdev_priv(dev);
563         struct ieee_ets *my_ets = adapter->ixgbe_ieee_ets;
564
565         /* No IEEE PFC settings available */
566         if (!my_ets)
567                 return -EINVAL;
568
569         ets->ets_cap = MAX_TRAFFIC_CLASS;
570         ets->cbs = my_ets->cbs;
571         memcpy(ets->tc_tx_bw, my_ets->tc_tx_bw, sizeof(ets->tc_tx_bw));
572         memcpy(ets->tc_rx_bw, my_ets->tc_rx_bw, sizeof(ets->tc_rx_bw));
573         memcpy(ets->tc_tsa, my_ets->tc_tsa, sizeof(ets->tc_tsa));
574         memcpy(ets->prio_tc, my_ets->prio_tc, sizeof(ets->prio_tc));
575         return 0;
576 }
577
578 static int ixgbe_dcbnl_ieee_setets(struct net_device *dev,
579                                    struct ieee_ets *ets)
580 {
581         struct ixgbe_adapter *adapter = netdev_priv(dev);
582         __u16 refill[IEEE_8021QAZ_MAX_TCS], max[IEEE_8021QAZ_MAX_TCS];
583         __u8 prio_type[IEEE_8021QAZ_MAX_TCS];
584         int max_frame = dev->mtu + ETH_HLEN + ETH_FCS_LEN;
585         int i, err;
586         __u64 *p = (__u64 *) ets->prio_tc;
587         /* naively give each TC a bwg to map onto CEE hardware */
588         __u8 bwg_id[IEEE_8021QAZ_MAX_TCS] = {0, 1, 2, 3, 4, 5, 6, 7};
589
590         if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
591                 return -EINVAL;
592
593         if (!adapter->ixgbe_ieee_ets) {
594                 adapter->ixgbe_ieee_ets = kmalloc(sizeof(struct ieee_ets),
595                                                   GFP_KERNEL);
596                 if (!adapter->ixgbe_ieee_ets)
597                         return -ENOMEM;
598         }
599
600         memcpy(adapter->ixgbe_ieee_ets, ets, sizeof(*adapter->ixgbe_ieee_ets));
601
602         /* Map TSA onto CEE prio type */
603         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
604                 switch (ets->tc_tsa[i]) {
605                 case IEEE_8021QAZ_TSA_STRICT:
606                         prio_type[i] = 2;
607                         break;
608                 case IEEE_8021QAZ_TSA_ETS:
609                         prio_type[i] = 0;
610                         break;
611                 default:
612                         /* Hardware only supports priority strict or
613                          * ETS transmission selection algorithms if
614                          * we receive some other value from dcbnl
615                          * throw an error
616                          */
617                         return -EINVAL;
618                 }
619         }
620
621         if (*p)
622                 ixgbe_dcbnl_set_state(dev, 1);
623         else
624                 ixgbe_dcbnl_set_state(dev, 0);
625
626         ixgbe_ieee_credits(ets->tc_tx_bw, refill, max, max_frame);
627         err = ixgbe_dcb_hw_ets_config(&adapter->hw, refill, max,
628                                       bwg_id, prio_type, ets->prio_tc);
629         return err;
630 }
631
632 static int ixgbe_dcbnl_ieee_getpfc(struct net_device *dev,
633                                    struct ieee_pfc *pfc)
634 {
635         struct ixgbe_adapter *adapter = netdev_priv(dev);
636         struct ieee_pfc *my_pfc = adapter->ixgbe_ieee_pfc;
637         int i;
638
639         /* No IEEE PFC settings available */
640         if (!my_pfc)
641                 return -EINVAL;
642
643         pfc->pfc_cap = MAX_TRAFFIC_CLASS;
644         pfc->pfc_en = my_pfc->pfc_en;
645         pfc->mbc = my_pfc->mbc;
646         pfc->delay = my_pfc->delay;
647
648         for (i = 0; i < MAX_TRAFFIC_CLASS; i++) {
649                 pfc->requests[i] = adapter->stats.pxoffrxc[i];
650                 pfc->indications[i] = adapter->stats.pxofftxc[i];
651         }
652
653         return 0;
654 }
655
656 static int ixgbe_dcbnl_ieee_setpfc(struct net_device *dev,
657                                    struct ieee_pfc *pfc)
658 {
659         struct ixgbe_adapter *adapter = netdev_priv(dev);
660         int err;
661
662         if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
663                 return -EINVAL;
664
665         if (!adapter->ixgbe_ieee_pfc) {
666                 adapter->ixgbe_ieee_pfc = kmalloc(sizeof(struct ieee_pfc),
667                                                   GFP_KERNEL);
668                 if (!adapter->ixgbe_ieee_pfc)
669                         return -ENOMEM;
670         }
671
672         memcpy(adapter->ixgbe_ieee_pfc, pfc, sizeof(*adapter->ixgbe_ieee_pfc));
673         err = ixgbe_dcb_hw_pfc_config(&adapter->hw, pfc->pfc_en);
674         return err;
675 }
676
677 static int ixgbe_dcbnl_ieee_setapp(struct net_device *dev,
678                                    struct dcb_app *app)
679 {
680         struct ixgbe_adapter *adapter = netdev_priv(dev);
681
682         if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
683                 return -EINVAL;
684
685         dcb_setapp(dev, app);
686
687 #ifdef IXGBE_FCOE
688         if (app->selector == 1 && app->protocol == ETH_P_FCOE &&
689             adapter->fcoe.tc == app->priority)
690                 ixgbe_dcbnl_set_all(dev);
691 #endif
692         return 0;
693 }
694
695 static u8 ixgbe_dcbnl_getdcbx(struct net_device *dev)
696 {
697         struct ixgbe_adapter *adapter = netdev_priv(dev);
698         return adapter->dcbx_cap;
699 }
700
701 static u8 ixgbe_dcbnl_setdcbx(struct net_device *dev, u8 mode)
702 {
703         struct ixgbe_adapter *adapter = netdev_priv(dev);
704         struct ieee_ets ets = {0};
705         struct ieee_pfc pfc = {0};
706
707         /* no support for LLD_MANAGED modes or CEE+IEEE */
708         if ((mode & DCB_CAP_DCBX_LLD_MANAGED) ||
709             ((mode & DCB_CAP_DCBX_VER_IEEE) && (mode & DCB_CAP_DCBX_VER_CEE)) ||
710             !(mode & DCB_CAP_DCBX_HOST))
711                 return 1;
712
713         if (mode == adapter->dcbx_cap)
714                 return 0;
715
716         adapter->dcbx_cap = mode;
717
718         /* ETS and PFC defaults */
719         ets.ets_cap = 8;
720         pfc.pfc_cap = 8;
721
722         if (mode & DCB_CAP_DCBX_VER_IEEE) {
723                 ixgbe_dcbnl_ieee_setets(dev, &ets);
724                 ixgbe_dcbnl_ieee_setpfc(dev, &pfc);
725         } else if (mode & DCB_CAP_DCBX_VER_CEE) {
726                 adapter->dcb_set_bitmap |= (BIT_PFC & BIT_PG_TX & BIT_PG_RX);
727                 ixgbe_dcbnl_set_all(dev);
728         } else {
729                 /* Drop into single TC mode strict priority as this
730                  * indicates CEE and IEEE versions are disabled
731                  */
732                 ixgbe_dcbnl_ieee_setets(dev, &ets);
733                 ixgbe_dcbnl_ieee_setpfc(dev, &pfc);
734                 ixgbe_dcbnl_set_state(dev, 0);
735         }
736
737         return 0;
738 }
739
740 const struct dcbnl_rtnl_ops dcbnl_ops = {
741         .ieee_getets    = ixgbe_dcbnl_ieee_getets,
742         .ieee_setets    = ixgbe_dcbnl_ieee_setets,
743         .ieee_getpfc    = ixgbe_dcbnl_ieee_getpfc,
744         .ieee_setpfc    = ixgbe_dcbnl_ieee_setpfc,
745         .ieee_setapp    = ixgbe_dcbnl_ieee_setapp,
746         .getstate       = ixgbe_dcbnl_get_state,
747         .setstate       = ixgbe_dcbnl_set_state,
748         .getpermhwaddr  = ixgbe_dcbnl_get_perm_hw_addr,
749         .setpgtccfgtx   = ixgbe_dcbnl_set_pg_tc_cfg_tx,
750         .setpgbwgcfgtx  = ixgbe_dcbnl_set_pg_bwg_cfg_tx,
751         .setpgtccfgrx   = ixgbe_dcbnl_set_pg_tc_cfg_rx,
752         .setpgbwgcfgrx  = ixgbe_dcbnl_set_pg_bwg_cfg_rx,
753         .getpgtccfgtx   = ixgbe_dcbnl_get_pg_tc_cfg_tx,
754         .getpgbwgcfgtx  = ixgbe_dcbnl_get_pg_bwg_cfg_tx,
755         .getpgtccfgrx   = ixgbe_dcbnl_get_pg_tc_cfg_rx,
756         .getpgbwgcfgrx  = ixgbe_dcbnl_get_pg_bwg_cfg_rx,
757         .setpfccfg      = ixgbe_dcbnl_set_pfc_cfg,
758         .getpfccfg      = ixgbe_dcbnl_get_pfc_cfg,
759         .setall         = ixgbe_dcbnl_set_all,
760         .getcap         = ixgbe_dcbnl_getcap,
761         .getnumtcs      = ixgbe_dcbnl_getnumtcs,
762         .setnumtcs      = ixgbe_dcbnl_setnumtcs,
763         .getpfcstate    = ixgbe_dcbnl_getpfcstate,
764         .setpfcstate    = ixgbe_dcbnl_setpfcstate,
765         .getapp         = ixgbe_dcbnl_getapp,
766         .getdcbx        = ixgbe_dcbnl_getdcbx,
767         .setdcbx        = ixgbe_dcbnl_setdcbx,
768 };