Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-2.6.git] / drivers / net / ixgbe / ixgbe_dcb_nl.c
1 /*******************************************************************************
2
3   Intel 10 Gigabit PCI Express Linux driver
4   Copyright(c) 1999 - 2010 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                 if (netif_running(netdev))
129                         netdev->netdev_ops->ndo_stop(netdev);
130                 ixgbe_clear_interrupt_scheme(adapter);
131
132                 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
133                 switch (adapter->hw.mac.type) {
134                 case ixgbe_mac_82598EB:
135                         adapter->last_lfc_mode = adapter->hw.fc.current_mode;
136                         adapter->hw.fc.requested_mode = ixgbe_fc_none;
137                         break;
138                 case ixgbe_mac_82599EB:
139                 case ixgbe_mac_X540:
140                         adapter->flags &= ~IXGBE_FLAG_FDIR_HASH_CAPABLE;
141                         adapter->flags &= ~IXGBE_FLAG_FDIR_PERFECT_CAPABLE;
142                         break;
143                 default:
144                         break;
145                 }
146
147                 adapter->flags |= IXGBE_FLAG_DCB_ENABLED;
148                 ixgbe_init_interrupt_scheme(adapter);
149                 if (netif_running(netdev))
150                         netdev->netdev_ops->ndo_open(netdev);
151         } else {
152                 /* Turn off DCB */
153                 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
154                         if (netif_running(netdev))
155                                 netdev->netdev_ops->ndo_stop(netdev);
156                         ixgbe_clear_interrupt_scheme(adapter);
157
158                         adapter->hw.fc.requested_mode = adapter->last_lfc_mode;
159                         adapter->temp_dcb_cfg.pfc_mode_enable = false;
160                         adapter->dcb_cfg.pfc_mode_enable = false;
161                         adapter->flags &= ~IXGBE_FLAG_DCB_ENABLED;
162                         adapter->flags |= IXGBE_FLAG_RSS_ENABLED;
163                         switch (adapter->hw.mac.type) {
164                         case ixgbe_mac_82599EB:
165                         case ixgbe_mac_X540:
166                                 adapter->flags |= IXGBE_FLAG_FDIR_HASH_CAPABLE;
167                                 break;
168                         default:
169                                 break;
170                         }
171
172                         ixgbe_init_interrupt_scheme(adapter);
173                         if (netif_running(netdev))
174                                 netdev->netdev_ops->ndo_open(netdev);
175                 }
176         }
177 out:
178         return err;
179 }
180
181 static void ixgbe_dcbnl_get_perm_hw_addr(struct net_device *netdev,
182                                          u8 *perm_addr)
183 {
184         struct ixgbe_adapter *adapter = netdev_priv(netdev);
185         int i, j;
186
187         memset(perm_addr, 0xff, MAX_ADDR_LEN);
188
189         for (i = 0; i < netdev->addr_len; i++)
190                 perm_addr[i] = adapter->hw.mac.perm_addr[i];
191
192         switch (adapter->hw.mac.type) {
193         case ixgbe_mac_82599EB:
194         case ixgbe_mac_X540:
195                 for (j = 0; j < netdev->addr_len; j++, i++)
196                         perm_addr[i] = adapter->hw.mac.san_addr[j];
197                 break;
198         default:
199                 break;
200         }
201 }
202
203 static void ixgbe_dcbnl_set_pg_tc_cfg_tx(struct net_device *netdev, int tc,
204                                          u8 prio, u8 bwg_id, u8 bw_pct,
205                                          u8 up_map)
206 {
207         struct ixgbe_adapter *adapter = netdev_priv(netdev);
208
209         if (prio != DCB_ATTR_VALUE_UNDEFINED)
210                 adapter->temp_dcb_cfg.tc_config[tc].path[0].prio_type = prio;
211         if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
212                 adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_id = bwg_id;
213         if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
214                 adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_percent =
215                         bw_pct;
216         if (up_map != DCB_ATTR_VALUE_UNDEFINED)
217                 adapter->temp_dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap =
218                         up_map;
219
220         if ((adapter->temp_dcb_cfg.tc_config[tc].path[0].prio_type !=
221              adapter->dcb_cfg.tc_config[tc].path[0].prio_type) ||
222             (adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_id !=
223              adapter->dcb_cfg.tc_config[tc].path[0].bwg_id) ||
224             (adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_percent !=
225              adapter->dcb_cfg.tc_config[tc].path[0].bwg_percent) ||
226             (adapter->temp_dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap !=
227              adapter->dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap))
228                 adapter->dcb_set_bitmap |= BIT_PG_TX;
229 }
230
231 static void ixgbe_dcbnl_set_pg_bwg_cfg_tx(struct net_device *netdev, int bwg_id,
232                                           u8 bw_pct)
233 {
234         struct ixgbe_adapter *adapter = netdev_priv(netdev);
235
236         adapter->temp_dcb_cfg.bw_percentage[0][bwg_id] = bw_pct;
237
238         if (adapter->temp_dcb_cfg.bw_percentage[0][bwg_id] !=
239             adapter->dcb_cfg.bw_percentage[0][bwg_id])
240                 adapter->dcb_set_bitmap |= BIT_PG_TX;
241 }
242
243 static void ixgbe_dcbnl_set_pg_tc_cfg_rx(struct net_device *netdev, int tc,
244                                          u8 prio, u8 bwg_id, u8 bw_pct,
245                                          u8 up_map)
246 {
247         struct ixgbe_adapter *adapter = netdev_priv(netdev);
248
249         if (prio != DCB_ATTR_VALUE_UNDEFINED)
250                 adapter->temp_dcb_cfg.tc_config[tc].path[1].prio_type = prio;
251         if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
252                 adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_id = bwg_id;
253         if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
254                 adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_percent =
255                         bw_pct;
256         if (up_map != DCB_ATTR_VALUE_UNDEFINED)
257                 adapter->temp_dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap =
258                         up_map;
259
260         if ((adapter->temp_dcb_cfg.tc_config[tc].path[1].prio_type !=
261              adapter->dcb_cfg.tc_config[tc].path[1].prio_type) ||
262             (adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_id !=
263              adapter->dcb_cfg.tc_config[tc].path[1].bwg_id) ||
264             (adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_percent !=
265              adapter->dcb_cfg.tc_config[tc].path[1].bwg_percent) ||
266             (adapter->temp_dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap !=
267              adapter->dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap))
268                 adapter->dcb_set_bitmap |= BIT_PG_RX;
269 }
270
271 static void ixgbe_dcbnl_set_pg_bwg_cfg_rx(struct net_device *netdev, int bwg_id,
272                                           u8 bw_pct)
273 {
274         struct ixgbe_adapter *adapter = netdev_priv(netdev);
275
276         adapter->temp_dcb_cfg.bw_percentage[1][bwg_id] = bw_pct;
277
278         if (adapter->temp_dcb_cfg.bw_percentage[1][bwg_id] !=
279             adapter->dcb_cfg.bw_percentage[1][bwg_id])
280                 adapter->dcb_set_bitmap |= BIT_PG_RX;
281 }
282
283 static void ixgbe_dcbnl_get_pg_tc_cfg_tx(struct net_device *netdev, int tc,
284                                          u8 *prio, u8 *bwg_id, u8 *bw_pct,
285                                          u8 *up_map)
286 {
287         struct ixgbe_adapter *adapter = netdev_priv(netdev);
288
289         *prio = adapter->dcb_cfg.tc_config[tc].path[0].prio_type;
290         *bwg_id = adapter->dcb_cfg.tc_config[tc].path[0].bwg_id;
291         *bw_pct = adapter->dcb_cfg.tc_config[tc].path[0].bwg_percent;
292         *up_map = adapter->dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap;
293 }
294
295 static void ixgbe_dcbnl_get_pg_bwg_cfg_tx(struct net_device *netdev, int bwg_id,
296                                           u8 *bw_pct)
297 {
298         struct ixgbe_adapter *adapter = netdev_priv(netdev);
299
300         *bw_pct = adapter->dcb_cfg.bw_percentage[0][bwg_id];
301 }
302
303 static void ixgbe_dcbnl_get_pg_tc_cfg_rx(struct net_device *netdev, int tc,
304                                          u8 *prio, u8 *bwg_id, u8 *bw_pct,
305                                          u8 *up_map)
306 {
307         struct ixgbe_adapter *adapter = netdev_priv(netdev);
308
309         *prio = adapter->dcb_cfg.tc_config[tc].path[1].prio_type;
310         *bwg_id = adapter->dcb_cfg.tc_config[tc].path[1].bwg_id;
311         *bw_pct = adapter->dcb_cfg.tc_config[tc].path[1].bwg_percent;
312         *up_map = adapter->dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap;
313 }
314
315 static void ixgbe_dcbnl_get_pg_bwg_cfg_rx(struct net_device *netdev, int bwg_id,
316                                           u8 *bw_pct)
317 {
318         struct ixgbe_adapter *adapter = netdev_priv(netdev);
319
320         *bw_pct = adapter->dcb_cfg.bw_percentage[1][bwg_id];
321 }
322
323 static void ixgbe_dcbnl_set_pfc_cfg(struct net_device *netdev, int priority,
324                                     u8 setting)
325 {
326         struct ixgbe_adapter *adapter = netdev_priv(netdev);
327
328         adapter->temp_dcb_cfg.tc_config[priority].dcb_pfc = setting;
329         if (adapter->temp_dcb_cfg.tc_config[priority].dcb_pfc !=
330             adapter->dcb_cfg.tc_config[priority].dcb_pfc) {
331                 adapter->dcb_set_bitmap |= BIT_PFC;
332                 adapter->temp_dcb_cfg.pfc_mode_enable = true;
333         }
334 }
335
336 static void ixgbe_dcbnl_get_pfc_cfg(struct net_device *netdev, int priority,
337                                     u8 *setting)
338 {
339         struct ixgbe_adapter *adapter = netdev_priv(netdev);
340
341         *setting = adapter->dcb_cfg.tc_config[priority].dcb_pfc;
342 }
343
344 static u8 ixgbe_dcbnl_set_all(struct net_device *netdev)
345 {
346         struct ixgbe_adapter *adapter = netdev_priv(netdev);
347         int ret;
348
349         if (!adapter->dcb_set_bitmap)
350                 return DCB_NO_HW_CHG;
351
352         ret = ixgbe_copy_dcb_cfg(&adapter->temp_dcb_cfg, &adapter->dcb_cfg,
353                                  adapter->ring_feature[RING_F_DCB].indices);
354
355         if (ret)
356                 return DCB_NO_HW_CHG;
357
358         /*
359          * Only take down the adapter if an app change occured. FCoE
360          * may shuffle tx rings in this case and this can not be done
361          * without a reset currently.
362          */
363         if (adapter->dcb_set_bitmap & BIT_APP_UPCHG) {
364                 while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
365                         msleep(1);
366
367                 if (netif_running(netdev))
368                         netdev->netdev_ops->ndo_stop(netdev);
369                 ixgbe_clear_interrupt_scheme(adapter);
370         }
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         if (adapter->dcb_set_bitmap & BIT_APP_UPCHG) {
399                 ixgbe_init_interrupt_scheme(adapter);
400                 if (netif_running(netdev))
401                         netdev->netdev_ops->ndo_open(netdev);
402                 ret = DCB_HW_CHG_RST;
403         }
404
405         if (adapter->dcb_set_bitmap & BIT_PFC) {
406                 u8 pfc_en;
407                 ixgbe_dcb_unpack_pfc(&adapter->dcb_cfg, &pfc_en);
408                 ixgbe_dcb_hw_pfc_config(&adapter->hw, pfc_en);
409                 ret = DCB_HW_CHG;
410         }
411
412         if (adapter->dcb_set_bitmap & (BIT_PG_TX|BIT_PG_RX)) {
413                 u16 refill[MAX_TRAFFIC_CLASS], max[MAX_TRAFFIC_CLASS];
414                 u8 bwg_id[MAX_TRAFFIC_CLASS], prio_type[MAX_TRAFFIC_CLASS];
415                 int max_frame = adapter->netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
416
417 #ifdef CONFIG_FCOE
418                 if (adapter->netdev->features & NETIF_F_FCOE_MTU)
419                         max_frame = max(max_frame, IXGBE_FCOE_JUMBO_FRAME_SIZE);
420 #endif
421
422                 ixgbe_dcb_calculate_tc_credits(&adapter->hw, &adapter->dcb_cfg,
423                                                max_frame, DCB_TX_CONFIG);
424                 ixgbe_dcb_calculate_tc_credits(&adapter->hw, &adapter->dcb_cfg,
425                                                max_frame, DCB_RX_CONFIG);
426
427                 ixgbe_dcb_unpack_refill(&adapter->dcb_cfg,
428                                         DCB_TX_CONFIG, refill);
429                 ixgbe_dcb_unpack_max(&adapter->dcb_cfg, max);
430                 ixgbe_dcb_unpack_bwgid(&adapter->dcb_cfg,
431                                        DCB_TX_CONFIG, bwg_id);
432                 ixgbe_dcb_unpack_prio(&adapter->dcb_cfg,
433                                       DCB_TX_CONFIG, prio_type);
434
435                 ixgbe_dcb_hw_ets_config(&adapter->hw, refill, max,
436                                         bwg_id, prio_type);
437         }
438
439         if (adapter->dcb_cfg.pfc_mode_enable)
440                 adapter->hw.fc.current_mode = ixgbe_fc_pfc;
441
442         if (adapter->dcb_set_bitmap & BIT_APP_UPCHG)
443                 clear_bit(__IXGBE_RESETTING, &adapter->state);
444         adapter->dcb_set_bitmap = 0x00;
445         return ret;
446 }
447
448 static u8 ixgbe_dcbnl_getcap(struct net_device *netdev, int capid, u8 *cap)
449 {
450         struct ixgbe_adapter *adapter = netdev_priv(netdev);
451         u8 rval = 0;
452
453         if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
454                 switch (capid) {
455                 case DCB_CAP_ATTR_PG:
456                         *cap = true;
457                         break;
458                 case DCB_CAP_ATTR_PFC:
459                         *cap = true;
460                         break;
461                 case DCB_CAP_ATTR_UP2TC:
462                         *cap = false;
463                         break;
464                 case DCB_CAP_ATTR_PG_TCS:
465                         *cap = 0x80;
466                         break;
467                 case DCB_CAP_ATTR_PFC_TCS:
468                         *cap = 0x80;
469                         break;
470                 case DCB_CAP_ATTR_GSP:
471                         *cap = true;
472                         break;
473                 case DCB_CAP_ATTR_BCN:
474                         *cap = false;
475                         break;
476                 default:
477                         rval = -EINVAL;
478                         break;
479                 }
480         } else {
481                 rval = -EINVAL;
482         }
483
484         return rval;
485 }
486
487 static u8 ixgbe_dcbnl_getnumtcs(struct net_device *netdev, int tcid, u8 *num)
488 {
489         struct ixgbe_adapter *adapter = netdev_priv(netdev);
490         u8 rval = 0;
491
492         if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
493                 switch (tcid) {
494                 case DCB_NUMTCS_ATTR_PG:
495                         *num = MAX_TRAFFIC_CLASS;
496                         break;
497                 case DCB_NUMTCS_ATTR_PFC:
498                         *num = MAX_TRAFFIC_CLASS;
499                         break;
500                 default:
501                         rval = -EINVAL;
502                         break;
503                 }
504         } else {
505                 rval = -EINVAL;
506         }
507
508         return rval;
509 }
510
511 static u8 ixgbe_dcbnl_setnumtcs(struct net_device *netdev, int tcid, u8 num)
512 {
513         return -EINVAL;
514 }
515
516 static u8 ixgbe_dcbnl_getpfcstate(struct net_device *netdev)
517 {
518         struct ixgbe_adapter *adapter = netdev_priv(netdev);
519
520         return adapter->dcb_cfg.pfc_mode_enable;
521 }
522
523 static void ixgbe_dcbnl_setpfcstate(struct net_device *netdev, u8 state)
524 {
525         struct ixgbe_adapter *adapter = netdev_priv(netdev);
526
527         adapter->temp_dcb_cfg.pfc_mode_enable = state;
528         if (adapter->temp_dcb_cfg.pfc_mode_enable !=
529                 adapter->dcb_cfg.pfc_mode_enable)
530                 adapter->dcb_set_bitmap |= BIT_PFC;
531 }
532
533 /**
534  * ixgbe_dcbnl_getapp - retrieve the DCBX application user priority
535  * @netdev : the corresponding netdev
536  * @idtype : identifies the id as ether type or TCP/UDP port number
537  * @id: id is either ether type or TCP/UDP port number
538  *
539  * Returns : on success, returns a non-zero 802.1p user priority bitmap
540  * otherwise returns 0 as the invalid user priority bitmap to indicate an
541  * error.
542  */
543 static u8 ixgbe_dcbnl_getapp(struct net_device *netdev, u8 idtype, u16 id)
544 {
545         u8 rval = 0;
546
547         switch (idtype) {
548         case DCB_APP_IDTYPE_ETHTYPE:
549 #ifdef IXGBE_FCOE
550                 if (id == ETH_P_FCOE)
551                         rval = ixgbe_fcoe_getapp(netdev_priv(netdev));
552 #endif
553                 break;
554         case DCB_APP_IDTYPE_PORTNUM:
555                 break;
556         default:
557                 break;
558         }
559         return rval;
560 }
561
562 /**
563  * ixgbe_dcbnl_setapp - set the DCBX application user priority
564  * @netdev : the corresponding netdev
565  * @idtype : identifies the id as ether type or TCP/UDP port number
566  * @id: id is either ether type or TCP/UDP port number
567  * @up: the 802.1p user priority bitmap
568  *
569  * Returns : 0 on success or 1 on error
570  */
571 static u8 ixgbe_dcbnl_setapp(struct net_device *netdev,
572                              u8 idtype, u16 id, u8 up)
573 {
574         u8 rval = 1;
575
576         switch (idtype) {
577         case DCB_APP_IDTYPE_ETHTYPE:
578 #ifdef IXGBE_FCOE
579                 if (id == ETH_P_FCOE) {
580                         u8 old_tc;
581                         struct ixgbe_adapter *adapter = netdev_priv(netdev);
582
583                         /* Get current programmed tc */
584                         old_tc = adapter->fcoe.tc;
585                         rval = ixgbe_fcoe_setapp(adapter, up);
586
587                         if (rval ||
588                            !(adapter->flags & IXGBE_FLAG_DCB_ENABLED) ||
589                            !(adapter->flags & IXGBE_FLAG_FCOE_ENABLED))
590                                 break;
591
592                         /* The FCoE application priority may be changed multiple
593                          * times in quick sucession with switches that build up
594                          * TLVs. To avoid creating uneeded device resets this
595                          * checks the actual HW configuration and clears
596                          * BIT_APP_UPCHG if a HW configuration change is not
597                          * need
598                          */
599                         if (old_tc == adapter->fcoe.tc)
600                                 adapter->dcb_set_bitmap &= ~BIT_APP_UPCHG;
601                         else
602                                 adapter->dcb_set_bitmap |= BIT_APP_UPCHG;
603                 }
604 #endif
605                 break;
606         case DCB_APP_IDTYPE_PORTNUM:
607                 break;
608         default:
609                 break;
610         }
611         return rval;
612 }
613
614 static int ixgbe_dcbnl_ieee_getets(struct net_device *dev,
615                                    struct ieee_ets *ets)
616 {
617         struct ixgbe_adapter *adapter = netdev_priv(dev);
618         struct ieee_ets *my_ets = adapter->ixgbe_ieee_ets;
619
620         /* No IEEE PFC settings available */
621         if (!my_ets)
622                 return -EINVAL;
623
624         ets->ets_cap = MAX_TRAFFIC_CLASS;
625         ets->cbs = my_ets->cbs;
626         memcpy(ets->tc_tx_bw, my_ets->tc_tx_bw, sizeof(ets->tc_tx_bw));
627         memcpy(ets->tc_rx_bw, my_ets->tc_rx_bw, sizeof(ets->tc_rx_bw));
628         memcpy(ets->tc_tsa, my_ets->tc_tsa, sizeof(ets->tc_tsa));
629         memcpy(ets->prio_tc, my_ets->prio_tc, sizeof(ets->prio_tc));
630         return 0;
631 }
632
633 static int ixgbe_dcbnl_ieee_setets(struct net_device *dev,
634                                    struct ieee_ets *ets)
635 {
636         struct ixgbe_adapter *adapter = netdev_priv(dev);
637         __u16 refill[IEEE_8021QAZ_MAX_TCS], max[IEEE_8021QAZ_MAX_TCS];
638         int max_frame = dev->mtu + ETH_HLEN + ETH_FCS_LEN;
639         int err;
640         /* naively give each TC a bwg to map onto CEE hardware */
641         __u8 bwg_id[IEEE_8021QAZ_MAX_TCS] = {0, 1, 2, 3, 4, 5, 6, 7};
642
643         if (!adapter->ixgbe_ieee_ets) {
644                 adapter->ixgbe_ieee_ets = kmalloc(sizeof(struct ieee_ets),
645                                                   GFP_KERNEL);
646                 if (!adapter->ixgbe_ieee_ets)
647                         return -ENOMEM;
648         }
649
650
651         memcpy(adapter->ixgbe_ieee_ets, ets, sizeof(*adapter->ixgbe_ieee_ets));
652
653         ixgbe_ieee_credits(ets->tc_tx_bw, refill, max, max_frame);
654         err = ixgbe_dcb_hw_ets_config(&adapter->hw, refill, max,
655                                       bwg_id, ets->tc_tsa);
656         return err;
657 }
658
659 static int ixgbe_dcbnl_ieee_getpfc(struct net_device *dev,
660                                    struct ieee_pfc *pfc)
661 {
662         struct ixgbe_adapter *adapter = netdev_priv(dev);
663         struct ieee_pfc *my_pfc = adapter->ixgbe_ieee_pfc;
664         int i;
665
666         /* No IEEE PFC settings available */
667         if (!my_pfc)
668                 return -EINVAL;
669
670         pfc->pfc_cap = MAX_TRAFFIC_CLASS;
671         pfc->pfc_en = my_pfc->pfc_en;
672         pfc->mbc = my_pfc->mbc;
673         pfc->delay = my_pfc->delay;
674
675         for (i = 0; i < MAX_TRAFFIC_CLASS; i++) {
676                 pfc->requests[i] = adapter->stats.pxoffrxc[i];
677                 pfc->indications[i] = adapter->stats.pxofftxc[i];
678         }
679
680         return 0;
681 }
682
683 static int ixgbe_dcbnl_ieee_setpfc(struct net_device *dev,
684                                    struct ieee_pfc *pfc)
685 {
686         struct ixgbe_adapter *adapter = netdev_priv(dev);
687         int err;
688
689         if (!adapter->ixgbe_ieee_pfc) {
690                 adapter->ixgbe_ieee_pfc = kmalloc(sizeof(struct ieee_pfc),
691                                                   GFP_KERNEL);
692                 if (!adapter->ixgbe_ieee_pfc)
693                         return -ENOMEM;
694         }
695
696         memcpy(adapter->ixgbe_ieee_pfc, pfc, sizeof(*adapter->ixgbe_ieee_pfc));
697         err = ixgbe_dcb_hw_pfc_config(&adapter->hw, pfc->pfc_en);
698         return err;
699 }
700
701 const struct dcbnl_rtnl_ops dcbnl_ops = {
702         .ieee_getets    = ixgbe_dcbnl_ieee_getets,
703         .ieee_setets    = ixgbe_dcbnl_ieee_setets,
704         .ieee_getpfc    = ixgbe_dcbnl_ieee_getpfc,
705         .ieee_setpfc    = ixgbe_dcbnl_ieee_setpfc,
706         .getstate       = ixgbe_dcbnl_get_state,
707         .setstate       = ixgbe_dcbnl_set_state,
708         .getpermhwaddr  = ixgbe_dcbnl_get_perm_hw_addr,
709         .setpgtccfgtx   = ixgbe_dcbnl_set_pg_tc_cfg_tx,
710         .setpgbwgcfgtx  = ixgbe_dcbnl_set_pg_bwg_cfg_tx,
711         .setpgtccfgrx   = ixgbe_dcbnl_set_pg_tc_cfg_rx,
712         .setpgbwgcfgrx  = ixgbe_dcbnl_set_pg_bwg_cfg_rx,
713         .getpgtccfgtx   = ixgbe_dcbnl_get_pg_tc_cfg_tx,
714         .getpgbwgcfgtx  = ixgbe_dcbnl_get_pg_bwg_cfg_tx,
715         .getpgtccfgrx   = ixgbe_dcbnl_get_pg_tc_cfg_rx,
716         .getpgbwgcfgrx  = ixgbe_dcbnl_get_pg_bwg_cfg_rx,
717         .setpfccfg      = ixgbe_dcbnl_set_pfc_cfg,
718         .getpfccfg      = ixgbe_dcbnl_get_pfc_cfg,
719         .setall         = ixgbe_dcbnl_set_all,
720         .getcap         = ixgbe_dcbnl_getcap,
721         .getnumtcs      = ixgbe_dcbnl_getnumtcs,
722         .setnumtcs      = ixgbe_dcbnl_setnumtcs,
723         .getpfcstate    = ixgbe_dcbnl_getpfcstate,
724         .setpfcstate    = ixgbe_dcbnl_setpfcstate,
725         .getapp         = ixgbe_dcbnl_getapp,
726         .setapp         = ixgbe_dcbnl_setapp,
727 };
728