ixgbe: DCB and perfect filters can coexist
[pandora-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         if (state > 0) {
118                 /* Turn on DCB */
119                 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED)
120                         goto out;
121
122                 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED)) {
123                         e_err(drv, "Enable failed, needs MSI-X\n");
124                         err = 1;
125                         goto out;
126                 }
127
128                 adapter->flags |= IXGBE_FLAG_DCB_ENABLED;
129
130                 switch (adapter->hw.mac.type) {
131                 case ixgbe_mac_82598EB:
132                         adapter->last_lfc_mode = adapter->hw.fc.current_mode;
133                         adapter->hw.fc.requested_mode = ixgbe_fc_none;
134                         break;
135                 case ixgbe_mac_82599EB:
136                 case ixgbe_mac_X540:
137                         adapter->flags &= ~IXGBE_FLAG_FDIR_HASH_CAPABLE;
138                         break;
139                 default:
140                         break;
141                 }
142
143                 ixgbe_setup_tc(netdev, MAX_TRAFFIC_CLASS);
144         } else {
145                 /* Turn off DCB */
146                 if (!(adapter->flags & IXGBE_FLAG_DCB_ENABLED))
147                         goto out;
148
149                 adapter->hw.fc.requested_mode = adapter->last_lfc_mode;
150                 adapter->temp_dcb_cfg.pfc_mode_enable = false;
151                 adapter->dcb_cfg.pfc_mode_enable = false;
152                 adapter->flags &= ~IXGBE_FLAG_DCB_ENABLED;
153                 switch (adapter->hw.mac.type) {
154                 case ixgbe_mac_82599EB:
155                 case ixgbe_mac_X540:
156                         adapter->flags |= IXGBE_FLAG_FDIR_HASH_CAPABLE;
157                         break;
158                 default:
159                         break;
160                 }
161                 ixgbe_setup_tc(netdev, 0);
162         }
163
164 out:
165         return err;
166 }
167
168 static void ixgbe_dcbnl_get_perm_hw_addr(struct net_device *netdev,
169                                          u8 *perm_addr)
170 {
171         struct ixgbe_adapter *adapter = netdev_priv(netdev);
172         int i, j;
173
174         memset(perm_addr, 0xff, MAX_ADDR_LEN);
175
176         for (i = 0; i < netdev->addr_len; i++)
177                 perm_addr[i] = adapter->hw.mac.perm_addr[i];
178
179         switch (adapter->hw.mac.type) {
180         case ixgbe_mac_82599EB:
181         case ixgbe_mac_X540:
182                 for (j = 0; j < netdev->addr_len; j++, i++)
183                         perm_addr[i] = adapter->hw.mac.san_addr[j];
184                 break;
185         default:
186                 break;
187         }
188 }
189
190 static void ixgbe_dcbnl_set_pg_tc_cfg_tx(struct net_device *netdev, int tc,
191                                          u8 prio, u8 bwg_id, u8 bw_pct,
192                                          u8 up_map)
193 {
194         struct ixgbe_adapter *adapter = netdev_priv(netdev);
195
196         if (prio != DCB_ATTR_VALUE_UNDEFINED)
197                 adapter->temp_dcb_cfg.tc_config[tc].path[0].prio_type = prio;
198         if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
199                 adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_id = bwg_id;
200         if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
201                 adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_percent =
202                         bw_pct;
203         if (up_map != DCB_ATTR_VALUE_UNDEFINED)
204                 adapter->temp_dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap =
205                         up_map;
206
207         if ((adapter->temp_dcb_cfg.tc_config[tc].path[0].prio_type !=
208              adapter->dcb_cfg.tc_config[tc].path[0].prio_type) ||
209             (adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_id !=
210              adapter->dcb_cfg.tc_config[tc].path[0].bwg_id) ||
211             (adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_percent !=
212              adapter->dcb_cfg.tc_config[tc].path[0].bwg_percent) ||
213             (adapter->temp_dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap !=
214              adapter->dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap))
215                 adapter->dcb_set_bitmap |= BIT_PG_TX;
216 }
217
218 static void ixgbe_dcbnl_set_pg_bwg_cfg_tx(struct net_device *netdev, int bwg_id,
219                                           u8 bw_pct)
220 {
221         struct ixgbe_adapter *adapter = netdev_priv(netdev);
222
223         adapter->temp_dcb_cfg.bw_percentage[0][bwg_id] = bw_pct;
224
225         if (adapter->temp_dcb_cfg.bw_percentage[0][bwg_id] !=
226             adapter->dcb_cfg.bw_percentage[0][bwg_id])
227                 adapter->dcb_set_bitmap |= BIT_PG_TX;
228 }
229
230 static void ixgbe_dcbnl_set_pg_tc_cfg_rx(struct net_device *netdev, int tc,
231                                          u8 prio, u8 bwg_id, u8 bw_pct,
232                                          u8 up_map)
233 {
234         struct ixgbe_adapter *adapter = netdev_priv(netdev);
235
236         if (prio != DCB_ATTR_VALUE_UNDEFINED)
237                 adapter->temp_dcb_cfg.tc_config[tc].path[1].prio_type = prio;
238         if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
239                 adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_id = bwg_id;
240         if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
241                 adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_percent =
242                         bw_pct;
243         if (up_map != DCB_ATTR_VALUE_UNDEFINED)
244                 adapter->temp_dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap =
245                         up_map;
246
247         if ((adapter->temp_dcb_cfg.tc_config[tc].path[1].prio_type !=
248              adapter->dcb_cfg.tc_config[tc].path[1].prio_type) ||
249             (adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_id !=
250              adapter->dcb_cfg.tc_config[tc].path[1].bwg_id) ||
251             (adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_percent !=
252              adapter->dcb_cfg.tc_config[tc].path[1].bwg_percent) ||
253             (adapter->temp_dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap !=
254              adapter->dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap))
255                 adapter->dcb_set_bitmap |= BIT_PG_RX;
256 }
257
258 static void ixgbe_dcbnl_set_pg_bwg_cfg_rx(struct net_device *netdev, int bwg_id,
259                                           u8 bw_pct)
260 {
261         struct ixgbe_adapter *adapter = netdev_priv(netdev);
262
263         adapter->temp_dcb_cfg.bw_percentage[1][bwg_id] = bw_pct;
264
265         if (adapter->temp_dcb_cfg.bw_percentage[1][bwg_id] !=
266             adapter->dcb_cfg.bw_percentage[1][bwg_id])
267                 adapter->dcb_set_bitmap |= BIT_PG_RX;
268 }
269
270 static void ixgbe_dcbnl_get_pg_tc_cfg_tx(struct net_device *netdev, int tc,
271                                          u8 *prio, u8 *bwg_id, u8 *bw_pct,
272                                          u8 *up_map)
273 {
274         struct ixgbe_adapter *adapter = netdev_priv(netdev);
275
276         *prio = adapter->dcb_cfg.tc_config[tc].path[0].prio_type;
277         *bwg_id = adapter->dcb_cfg.tc_config[tc].path[0].bwg_id;
278         *bw_pct = adapter->dcb_cfg.tc_config[tc].path[0].bwg_percent;
279         *up_map = adapter->dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap;
280 }
281
282 static void ixgbe_dcbnl_get_pg_bwg_cfg_tx(struct net_device *netdev, int bwg_id,
283                                           u8 *bw_pct)
284 {
285         struct ixgbe_adapter *adapter = netdev_priv(netdev);
286
287         *bw_pct = adapter->dcb_cfg.bw_percentage[0][bwg_id];
288 }
289
290 static void ixgbe_dcbnl_get_pg_tc_cfg_rx(struct net_device *netdev, int tc,
291                                          u8 *prio, u8 *bwg_id, u8 *bw_pct,
292                                          u8 *up_map)
293 {
294         struct ixgbe_adapter *adapter = netdev_priv(netdev);
295
296         *prio = adapter->dcb_cfg.tc_config[tc].path[1].prio_type;
297         *bwg_id = adapter->dcb_cfg.tc_config[tc].path[1].bwg_id;
298         *bw_pct = adapter->dcb_cfg.tc_config[tc].path[1].bwg_percent;
299         *up_map = adapter->dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap;
300 }
301
302 static void ixgbe_dcbnl_get_pg_bwg_cfg_rx(struct net_device *netdev, int bwg_id,
303                                           u8 *bw_pct)
304 {
305         struct ixgbe_adapter *adapter = netdev_priv(netdev);
306
307         *bw_pct = adapter->dcb_cfg.bw_percentage[1][bwg_id];
308 }
309
310 static void ixgbe_dcbnl_set_pfc_cfg(struct net_device *netdev, int priority,
311                                     u8 setting)
312 {
313         struct ixgbe_adapter *adapter = netdev_priv(netdev);
314
315         adapter->temp_dcb_cfg.tc_config[priority].dcb_pfc = setting;
316         if (adapter->temp_dcb_cfg.tc_config[priority].dcb_pfc !=
317             adapter->dcb_cfg.tc_config[priority].dcb_pfc) {
318                 adapter->dcb_set_bitmap |= BIT_PFC;
319                 adapter->temp_dcb_cfg.pfc_mode_enable = true;
320         }
321 }
322
323 static void ixgbe_dcbnl_get_pfc_cfg(struct net_device *netdev, int priority,
324                                     u8 *setting)
325 {
326         struct ixgbe_adapter *adapter = netdev_priv(netdev);
327
328         *setting = adapter->dcb_cfg.tc_config[priority].dcb_pfc;
329 }
330
331 static u8 ixgbe_dcbnl_set_all(struct net_device *netdev)
332 {
333         struct ixgbe_adapter *adapter = netdev_priv(netdev);
334         struct dcb_app app = {
335                               .selector = DCB_APP_IDTYPE_ETHTYPE,
336                               .protocol = ETH_P_FCOE,
337                              };
338         u8 up = dcb_getapp(netdev, &app);
339         int ret;
340
341         ret = ixgbe_copy_dcb_cfg(&adapter->temp_dcb_cfg, &adapter->dcb_cfg,
342                                  MAX_TRAFFIC_CLASS);
343         if (ret)
344                 return DCB_NO_HW_CHG;
345
346         /* In IEEE mode app data must be parsed into DCBX format for
347          * hardware routines.
348          */
349         if (adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE)
350                 up = (1 << up);
351
352 #ifdef IXGBE_FCOE
353         if (up && (up != (1 << adapter->fcoe.up)))
354                 adapter->dcb_set_bitmap |= BIT_APP_UPCHG;
355
356         /*
357          * Only take down the adapter if an app change occurred. FCoE
358          * may shuffle tx rings in this case and this can not be done
359          * without a reset currently.
360          */
361         if (adapter->dcb_set_bitmap & BIT_APP_UPCHG) {
362                 while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
363                         usleep_range(1000, 2000);
364
365                 ixgbe_fcoe_setapp(adapter, up);
366
367                 if (netif_running(netdev))
368                         netdev->netdev_ops->ndo_stop(netdev);
369                 ixgbe_clear_interrupt_scheme(adapter);
370         }
371 #endif
372
373         if (adapter->dcb_cfg.pfc_mode_enable) {
374                 switch (adapter->hw.mac.type) {
375                 case ixgbe_mac_82599EB:
376                 case ixgbe_mac_X540:
377                         if (adapter->hw.fc.current_mode != ixgbe_fc_pfc)
378                                 adapter->last_lfc_mode =
379                                                   adapter->hw.fc.current_mode;
380                         break;
381                 default:
382                         break;
383                 }
384                 adapter->hw.fc.requested_mode = ixgbe_fc_pfc;
385         } else {
386                 switch (adapter->hw.mac.type) {
387                 case ixgbe_mac_82598EB:
388                         adapter->hw.fc.requested_mode = ixgbe_fc_none;
389                         break;
390                 case ixgbe_mac_82599EB:
391                 case ixgbe_mac_X540:
392                         adapter->hw.fc.requested_mode = adapter->last_lfc_mode;
393                         break;
394                 default:
395                         break;
396                 }
397         }
398
399 #ifdef IXGBE_FCOE
400         if (adapter->dcb_set_bitmap & BIT_APP_UPCHG) {
401                 ixgbe_init_interrupt_scheme(adapter);
402                 if (netif_running(netdev))
403                         netdev->netdev_ops->ndo_open(netdev);
404                 ret = DCB_HW_CHG_RST;
405         }
406 #endif
407
408         if (adapter->dcb_set_bitmap & BIT_PFC) {
409                 u8 pfc_en;
410                 ixgbe_dcb_unpack_pfc(&adapter->dcb_cfg, &pfc_en);
411                 ixgbe_dcb_hw_pfc_config(&adapter->hw, pfc_en);
412                 ret = DCB_HW_CHG;
413         }
414
415         if (adapter->dcb_set_bitmap & (BIT_PG_TX|BIT_PG_RX)) {
416                 u16 refill[MAX_TRAFFIC_CLASS], max[MAX_TRAFFIC_CLASS];
417                 u8 bwg_id[MAX_TRAFFIC_CLASS], prio_type[MAX_TRAFFIC_CLASS];
418                 /* Priority to TC mapping in CEE case default to 1:1 */
419                 u8 prio_tc[MAX_TRAFFIC_CLASS] = {0, 1, 2, 3, 4, 5, 6, 7};
420                 int max_frame = adapter->netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
421
422 #ifdef CONFIG_FCOE
423                 if (adapter->netdev->features & NETIF_F_FCOE_MTU)
424                         max_frame = max(max_frame, IXGBE_FCOE_JUMBO_FRAME_SIZE);
425 #endif
426
427                 ixgbe_dcb_calculate_tc_credits(&adapter->hw, &adapter->dcb_cfg,
428                                                max_frame, DCB_TX_CONFIG);
429                 ixgbe_dcb_calculate_tc_credits(&adapter->hw, &adapter->dcb_cfg,
430                                                max_frame, DCB_RX_CONFIG);
431
432                 ixgbe_dcb_unpack_refill(&adapter->dcb_cfg,
433                                         DCB_TX_CONFIG, refill);
434                 ixgbe_dcb_unpack_max(&adapter->dcb_cfg, max);
435                 ixgbe_dcb_unpack_bwgid(&adapter->dcb_cfg,
436                                        DCB_TX_CONFIG, bwg_id);
437                 ixgbe_dcb_unpack_prio(&adapter->dcb_cfg,
438                                       DCB_TX_CONFIG, prio_type);
439
440                 ixgbe_dcb_hw_ets_config(&adapter->hw, refill, max,
441                                         bwg_id, prio_type, prio_tc);
442         }
443
444         if (adapter->dcb_cfg.pfc_mode_enable)
445                 adapter->hw.fc.current_mode = ixgbe_fc_pfc;
446
447         if (adapter->dcb_set_bitmap & BIT_APP_UPCHG)
448                 clear_bit(__IXGBE_RESETTING, &adapter->state);
449         adapter->dcb_set_bitmap = 0x00;
450         return ret;
451 }
452
453 static u8 ixgbe_dcbnl_getcap(struct net_device *netdev, int capid, u8 *cap)
454 {
455         struct ixgbe_adapter *adapter = netdev_priv(netdev);
456
457         switch (capid) {
458         case DCB_CAP_ATTR_PG:
459                 *cap = true;
460                 break;
461         case DCB_CAP_ATTR_PFC:
462                 *cap = true;
463                 break;
464         case DCB_CAP_ATTR_UP2TC:
465                 *cap = false;
466                 break;
467         case DCB_CAP_ATTR_PG_TCS:
468                 *cap = 0x80;
469                 break;
470         case DCB_CAP_ATTR_PFC_TCS:
471                 *cap = 0x80;
472                 break;
473         case DCB_CAP_ATTR_GSP:
474                 *cap = true;
475                 break;
476         case DCB_CAP_ATTR_BCN:
477                 *cap = false;
478                 break;
479         case DCB_CAP_ATTR_DCBX:
480                 *cap = adapter->dcbx_cap;
481                 break;
482         default:
483                 *cap = false;
484                 break;
485         }
486
487         return 0;
488 }
489
490 static u8 ixgbe_dcbnl_getnumtcs(struct net_device *netdev, int tcid, u8 *num)
491 {
492         struct ixgbe_adapter *adapter = netdev_priv(netdev);
493         u8 rval = 0;
494
495         if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
496                 switch (tcid) {
497                 case DCB_NUMTCS_ATTR_PG:
498                         *num = MAX_TRAFFIC_CLASS;
499                         break;
500                 case DCB_NUMTCS_ATTR_PFC:
501                         *num = MAX_TRAFFIC_CLASS;
502                         break;
503                 default:
504                         rval = -EINVAL;
505                         break;
506                 }
507         } else {
508                 rval = -EINVAL;
509         }
510
511         return rval;
512 }
513
514 static u8 ixgbe_dcbnl_setnumtcs(struct net_device *netdev, int tcid, u8 num)
515 {
516         return -EINVAL;
517 }
518
519 static u8 ixgbe_dcbnl_getpfcstate(struct net_device *netdev)
520 {
521         struct ixgbe_adapter *adapter = netdev_priv(netdev);
522
523         return adapter->dcb_cfg.pfc_mode_enable;
524 }
525
526 static void ixgbe_dcbnl_setpfcstate(struct net_device *netdev, u8 state)
527 {
528         struct ixgbe_adapter *adapter = netdev_priv(netdev);
529
530         adapter->temp_dcb_cfg.pfc_mode_enable = state;
531         if (adapter->temp_dcb_cfg.pfc_mode_enable !=
532                 adapter->dcb_cfg.pfc_mode_enable)
533                 adapter->dcb_set_bitmap |= BIT_PFC;
534 }
535
536 /**
537  * ixgbe_dcbnl_getapp - retrieve the DCBX application user priority
538  * @netdev : the corresponding netdev
539  * @idtype : identifies the id as ether type or TCP/UDP port number
540  * @id: id is either ether type or TCP/UDP port number
541  *
542  * Returns : on success, returns a non-zero 802.1p user priority bitmap
543  * otherwise returns 0 as the invalid user priority bitmap to indicate an
544  * error.
545  */
546 static u8 ixgbe_dcbnl_getapp(struct net_device *netdev, u8 idtype, u16 id)
547 {
548         struct ixgbe_adapter *adapter = netdev_priv(netdev);
549         struct dcb_app app = {
550                                 .selector = idtype,
551                                 .protocol = id,
552                              };
553
554         if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
555                 return 0;
556
557         return dcb_getapp(netdev, &app);
558 }
559
560 static int ixgbe_dcbnl_ieee_getets(struct net_device *dev,
561                                    struct ieee_ets *ets)
562 {
563         struct ixgbe_adapter *adapter = netdev_priv(dev);
564         struct ieee_ets *my_ets = adapter->ixgbe_ieee_ets;
565
566         /* No IEEE PFC settings available */
567         if (!my_ets)
568                 return -EINVAL;
569
570         ets->ets_cap = MAX_TRAFFIC_CLASS;
571         ets->cbs = my_ets->cbs;
572         memcpy(ets->tc_tx_bw, my_ets->tc_tx_bw, sizeof(ets->tc_tx_bw));
573         memcpy(ets->tc_rx_bw, my_ets->tc_rx_bw, sizeof(ets->tc_rx_bw));
574         memcpy(ets->tc_tsa, my_ets->tc_tsa, sizeof(ets->tc_tsa));
575         memcpy(ets->prio_tc, my_ets->prio_tc, sizeof(ets->prio_tc));
576         return 0;
577 }
578
579 static int ixgbe_dcbnl_ieee_setets(struct net_device *dev,
580                                    struct ieee_ets *ets)
581 {
582         struct ixgbe_adapter *adapter = netdev_priv(dev);
583         __u16 refill[IEEE_8021QAZ_MAX_TCS], max[IEEE_8021QAZ_MAX_TCS];
584         __u8 prio_type[IEEE_8021QAZ_MAX_TCS];
585         int max_frame = dev->mtu + ETH_HLEN + ETH_FCS_LEN;
586         int i, err;
587         __u64 *p = (__u64 *) ets->prio_tc;
588         /* naively give each TC a bwg to map onto CEE hardware */
589         __u8 bwg_id[IEEE_8021QAZ_MAX_TCS] = {0, 1, 2, 3, 4, 5, 6, 7};
590
591         if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
592                 return -EINVAL;
593
594         if (!adapter->ixgbe_ieee_ets) {
595                 adapter->ixgbe_ieee_ets = kmalloc(sizeof(struct ieee_ets),
596                                                   GFP_KERNEL);
597                 if (!adapter->ixgbe_ieee_ets)
598                         return -ENOMEM;
599         }
600
601         memcpy(adapter->ixgbe_ieee_ets, ets, sizeof(*adapter->ixgbe_ieee_ets));
602
603         /* Map TSA onto CEE prio type */
604         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
605                 switch (ets->tc_tsa[i]) {
606                 case IEEE_8021QAZ_TSA_STRICT:
607                         prio_type[i] = 2;
608                         break;
609                 case IEEE_8021QAZ_TSA_ETS:
610                         prio_type[i] = 0;
611                         break;
612                 default:
613                         /* Hardware only supports priority strict or
614                          * ETS transmission selection algorithms if
615                          * we receive some other value from dcbnl
616                          * throw an error
617                          */
618                         return -EINVAL;
619                 }
620         }
621
622         if (*p)
623                 ixgbe_dcbnl_set_state(dev, 1);
624         else
625                 ixgbe_dcbnl_set_state(dev, 0);
626
627         ixgbe_ieee_credits(ets->tc_tx_bw, refill, max, max_frame);
628         err = ixgbe_dcb_hw_ets_config(&adapter->hw, refill, max,
629                                       bwg_id, prio_type, ets->prio_tc);
630         return err;
631 }
632
633 static int ixgbe_dcbnl_ieee_getpfc(struct net_device *dev,
634                                    struct ieee_pfc *pfc)
635 {
636         struct ixgbe_adapter *adapter = netdev_priv(dev);
637         struct ieee_pfc *my_pfc = adapter->ixgbe_ieee_pfc;
638         int i;
639
640         /* No IEEE PFC settings available */
641         if (!my_pfc)
642                 return -EINVAL;
643
644         pfc->pfc_cap = MAX_TRAFFIC_CLASS;
645         pfc->pfc_en = my_pfc->pfc_en;
646         pfc->mbc = my_pfc->mbc;
647         pfc->delay = my_pfc->delay;
648
649         for (i = 0; i < MAX_TRAFFIC_CLASS; i++) {
650                 pfc->requests[i] = adapter->stats.pxoffrxc[i];
651                 pfc->indications[i] = adapter->stats.pxofftxc[i];
652         }
653
654         return 0;
655 }
656
657 static int ixgbe_dcbnl_ieee_setpfc(struct net_device *dev,
658                                    struct ieee_pfc *pfc)
659 {
660         struct ixgbe_adapter *adapter = netdev_priv(dev);
661         int err;
662
663         if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
664                 return -EINVAL;
665
666         if (!adapter->ixgbe_ieee_pfc) {
667                 adapter->ixgbe_ieee_pfc = kmalloc(sizeof(struct ieee_pfc),
668                                                   GFP_KERNEL);
669                 if (!adapter->ixgbe_ieee_pfc)
670                         return -ENOMEM;
671         }
672
673         memcpy(adapter->ixgbe_ieee_pfc, pfc, sizeof(*adapter->ixgbe_ieee_pfc));
674         err = ixgbe_dcb_hw_pfc_config(&adapter->hw, pfc->pfc_en);
675         return err;
676 }
677
678 static int ixgbe_dcbnl_ieee_setapp(struct net_device *dev,
679                                    struct dcb_app *app)
680 {
681         struct ixgbe_adapter *adapter = netdev_priv(dev);
682
683         if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
684                 return -EINVAL;
685
686         dcb_setapp(dev, app);
687
688 #ifdef IXGBE_FCOE
689         if (app->selector == 1 && app->protocol == ETH_P_FCOE &&
690             adapter->fcoe.tc == app->priority)
691                 ixgbe_dcbnl_set_all(dev);
692 #endif
693         return 0;
694 }
695
696 static u8 ixgbe_dcbnl_getdcbx(struct net_device *dev)
697 {
698         struct ixgbe_adapter *adapter = netdev_priv(dev);
699         return adapter->dcbx_cap;
700 }
701
702 static u8 ixgbe_dcbnl_setdcbx(struct net_device *dev, u8 mode)
703 {
704         struct ixgbe_adapter *adapter = netdev_priv(dev);
705         struct ieee_ets ets = {0};
706         struct ieee_pfc pfc = {0};
707
708         /* no support for LLD_MANAGED modes or CEE+IEEE */
709         if ((mode & DCB_CAP_DCBX_LLD_MANAGED) ||
710             ((mode & DCB_CAP_DCBX_VER_IEEE) && (mode & DCB_CAP_DCBX_VER_CEE)) ||
711             !(mode & DCB_CAP_DCBX_HOST))
712                 return 1;
713
714         if (mode == adapter->dcbx_cap)
715                 return 0;
716
717         adapter->dcbx_cap = mode;
718
719         /* ETS and PFC defaults */
720         ets.ets_cap = 8;
721         pfc.pfc_cap = 8;
722
723         if (mode & DCB_CAP_DCBX_VER_IEEE) {
724                 ixgbe_dcbnl_ieee_setets(dev, &ets);
725                 ixgbe_dcbnl_ieee_setpfc(dev, &pfc);
726         } else if (mode & DCB_CAP_DCBX_VER_CEE) {
727                 adapter->dcb_set_bitmap |= (BIT_PFC & BIT_PG_TX & BIT_PG_RX);
728                 ixgbe_dcbnl_set_all(dev);
729         } else {
730                 /* Drop into single TC mode strict priority as this
731                  * indicates CEE and IEEE versions are disabled
732                  */
733                 ixgbe_dcbnl_ieee_setets(dev, &ets);
734                 ixgbe_dcbnl_ieee_setpfc(dev, &pfc);
735                 ixgbe_dcbnl_set_state(dev, 0);
736         }
737
738         return 0;
739 }
740
741 const struct dcbnl_rtnl_ops dcbnl_ops = {
742         .ieee_getets    = ixgbe_dcbnl_ieee_getets,
743         .ieee_setets    = ixgbe_dcbnl_ieee_setets,
744         .ieee_getpfc    = ixgbe_dcbnl_ieee_getpfc,
745         .ieee_setpfc    = ixgbe_dcbnl_ieee_setpfc,
746         .ieee_setapp    = ixgbe_dcbnl_ieee_setapp,
747         .getstate       = ixgbe_dcbnl_get_state,
748         .setstate       = ixgbe_dcbnl_set_state,
749         .getpermhwaddr  = ixgbe_dcbnl_get_perm_hw_addr,
750         .setpgtccfgtx   = ixgbe_dcbnl_set_pg_tc_cfg_tx,
751         .setpgbwgcfgtx  = ixgbe_dcbnl_set_pg_bwg_cfg_tx,
752         .setpgtccfgrx   = ixgbe_dcbnl_set_pg_tc_cfg_rx,
753         .setpgbwgcfgrx  = ixgbe_dcbnl_set_pg_bwg_cfg_rx,
754         .getpgtccfgtx   = ixgbe_dcbnl_get_pg_tc_cfg_tx,
755         .getpgbwgcfgtx  = ixgbe_dcbnl_get_pg_bwg_cfg_tx,
756         .getpgtccfgrx   = ixgbe_dcbnl_get_pg_tc_cfg_rx,
757         .getpgbwgcfgrx  = ixgbe_dcbnl_get_pg_bwg_cfg_rx,
758         .setpfccfg      = ixgbe_dcbnl_set_pfc_cfg,
759         .getpfccfg      = ixgbe_dcbnl_get_pfc_cfg,
760         .setall         = ixgbe_dcbnl_set_all,
761         .getcap         = ixgbe_dcbnl_getcap,
762         .getnumtcs      = ixgbe_dcbnl_getnumtcs,
763         .setnumtcs      = ixgbe_dcbnl_setnumtcs,
764         .getpfcstate    = ixgbe_dcbnl_getpfcstate,
765         .setpfcstate    = ixgbe_dcbnl_setpfcstate,
766         .getapp         = ixgbe_dcbnl_getapp,
767         .getdcbx        = ixgbe_dcbnl_getdcbx,
768         .setdcbx        = ixgbe_dcbnl_setdcbx,
769 };