Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / drivers / net / ixgbe / ixgbe_dcb_nl.c
1 /*******************************************************************************
2
3   Intel 10 Gigabit PCI Express Linux driver
4   Copyright(c) 1999 - 2009 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_RESETLINK   0x40
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         DPRINTK(DRV, INFO, "Get DCB Admin Mode.\n");
110
111         return !!(adapter->flags & IXGBE_FLAG_DCB_ENABLED);
112 }
113
114 static u8 ixgbe_dcbnl_set_state(struct net_device *netdev, u8 state)
115 {
116         u8 err = 0;
117         struct ixgbe_adapter *adapter = netdev_priv(netdev);
118
119         DPRINTK(DRV, INFO, "Set DCB Admin Mode.\n");
120
121         if (state > 0) {
122                 /* Turn on DCB */
123                 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED)
124                         goto out;
125
126                 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED)) {
127                         DPRINTK(DRV, ERR, "Enable failed, needs MSI-X\n");
128                         err = 1;
129                         goto out;
130                 }
131
132                 if (netif_running(netdev))
133                         netdev->netdev_ops->ndo_stop(netdev);
134                 ixgbe_clear_interrupt_scheme(adapter);
135
136                 if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
137                         adapter->last_lfc_mode = adapter->hw.fc.current_mode;
138                         adapter->hw.fc.requested_mode = ixgbe_fc_none;
139                 }
140                 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
141                 adapter->flags |= IXGBE_FLAG_DCB_ENABLED;
142                 ixgbe_init_interrupt_scheme(adapter);
143                 if (netif_running(netdev))
144                         netdev->netdev_ops->ndo_open(netdev);
145         } else {
146                 /* Turn off DCB */
147                 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
148                         if (netif_running(netdev))
149                                 netdev->netdev_ops->ndo_stop(netdev);
150                         ixgbe_clear_interrupt_scheme(adapter);
151
152                         adapter->hw.fc.requested_mode = adapter->last_lfc_mode;
153                         adapter->temp_dcb_cfg.pfc_mode_enable = false;
154                         adapter->dcb_cfg.pfc_mode_enable = false;
155                         adapter->flags &= ~IXGBE_FLAG_DCB_ENABLED;
156                         adapter->flags |= IXGBE_FLAG_RSS_ENABLED;
157                         ixgbe_init_interrupt_scheme(adapter);
158                         if (netif_running(netdev))
159                                 netdev->netdev_ops->ndo_open(netdev);
160                 }
161         }
162 out:
163         return err;
164 }
165
166 static void ixgbe_dcbnl_get_perm_hw_addr(struct net_device *netdev,
167                                          u8 *perm_addr)
168 {
169         struct ixgbe_adapter *adapter = netdev_priv(netdev);
170         int i, j;
171
172         for (i = 0; i < netdev->addr_len; i++)
173                 perm_addr[i] = adapter->hw.mac.perm_addr[i];
174
175         if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
176                 for (j = 0; j < netdev->addr_len; j++, i++)
177                         perm_addr[i] = adapter->hw.mac.san_addr[j];
178         }
179 }
180
181 static void ixgbe_dcbnl_set_pg_tc_cfg_tx(struct net_device *netdev, int tc,
182                                          u8 prio, u8 bwg_id, u8 bw_pct,
183                                          u8 up_map)
184 {
185         struct ixgbe_adapter *adapter = netdev_priv(netdev);
186
187         if (prio != DCB_ATTR_VALUE_UNDEFINED)
188                 adapter->temp_dcb_cfg.tc_config[tc].path[0].prio_type = prio;
189         if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
190                 adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_id = bwg_id;
191         if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
192                 adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_percent =
193                         bw_pct;
194         if (up_map != DCB_ATTR_VALUE_UNDEFINED)
195                 adapter->temp_dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap =
196                         up_map;
197
198         if ((adapter->temp_dcb_cfg.tc_config[tc].path[0].prio_type !=
199              adapter->dcb_cfg.tc_config[tc].path[0].prio_type) ||
200             (adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_id !=
201              adapter->dcb_cfg.tc_config[tc].path[0].bwg_id) ||
202             (adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_percent !=
203              adapter->dcb_cfg.tc_config[tc].path[0].bwg_percent) ||
204             (adapter->temp_dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap !=
205              adapter->dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap)) {
206                 adapter->dcb_set_bitmap |= BIT_PG_TX;
207                 adapter->dcb_set_bitmap |= BIT_RESETLINK;
208         }
209 }
210
211 static void ixgbe_dcbnl_set_pg_bwg_cfg_tx(struct net_device *netdev, int bwg_id,
212                                           u8 bw_pct)
213 {
214         struct ixgbe_adapter *adapter = netdev_priv(netdev);
215
216         adapter->temp_dcb_cfg.bw_percentage[0][bwg_id] = bw_pct;
217
218         if (adapter->temp_dcb_cfg.bw_percentage[0][bwg_id] !=
219             adapter->dcb_cfg.bw_percentage[0][bwg_id]) {
220                 adapter->dcb_set_bitmap |= BIT_PG_RX;
221                 adapter->dcb_set_bitmap |= BIT_RESETLINK;
222         }
223 }
224
225 static void ixgbe_dcbnl_set_pg_tc_cfg_rx(struct net_device *netdev, int tc,
226                                          u8 prio, u8 bwg_id, u8 bw_pct,
227                                          u8 up_map)
228 {
229         struct ixgbe_adapter *adapter = netdev_priv(netdev);
230
231         if (prio != DCB_ATTR_VALUE_UNDEFINED)
232                 adapter->temp_dcb_cfg.tc_config[tc].path[1].prio_type = prio;
233         if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
234                 adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_id = bwg_id;
235         if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
236                 adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_percent =
237                         bw_pct;
238         if (up_map != DCB_ATTR_VALUE_UNDEFINED)
239                 adapter->temp_dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap =
240                         up_map;
241
242         if ((adapter->temp_dcb_cfg.tc_config[tc].path[1].prio_type !=
243              adapter->dcb_cfg.tc_config[tc].path[1].prio_type) ||
244             (adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_id !=
245              adapter->dcb_cfg.tc_config[tc].path[1].bwg_id) ||
246             (adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_percent !=
247              adapter->dcb_cfg.tc_config[tc].path[1].bwg_percent) ||
248             (adapter->temp_dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap !=
249              adapter->dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap)) {
250                 adapter->dcb_set_bitmap |= BIT_PG_RX;
251                 adapter->dcb_set_bitmap |= BIT_RESETLINK;
252         }
253 }
254
255 static void ixgbe_dcbnl_set_pg_bwg_cfg_rx(struct net_device *netdev, int bwg_id,
256                                           u8 bw_pct)
257 {
258         struct ixgbe_adapter *adapter = netdev_priv(netdev);
259
260         adapter->temp_dcb_cfg.bw_percentage[1][bwg_id] = bw_pct;
261
262         if (adapter->temp_dcb_cfg.bw_percentage[1][bwg_id] !=
263             adapter->dcb_cfg.bw_percentage[1][bwg_id]) {
264                 adapter->dcb_set_bitmap |= BIT_PG_RX;
265                 adapter->dcb_set_bitmap |= BIT_RESETLINK;
266         }
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         int ret;
334
335         if (!adapter->dcb_set_bitmap)
336                 return DCB_NO_HW_CHG;
337
338         /*
339          * Only take down the adapter if the configuration change
340          * requires a reset.
341          */
342         if (adapter->dcb_set_bitmap & BIT_RESETLINK) {
343                 while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
344                         msleep(1);
345
346                 if (netif_running(netdev))
347                         ixgbe_down(adapter);
348         }
349
350         ret = ixgbe_copy_dcb_cfg(&adapter->temp_dcb_cfg, &adapter->dcb_cfg,
351                                  adapter->ring_feature[RING_F_DCB].indices);
352         if (ret) {
353                 if (adapter->dcb_set_bitmap & BIT_RESETLINK)
354                         clear_bit(__IXGBE_RESETTING, &adapter->state);
355                 return DCB_NO_HW_CHG;
356         }
357
358         if (adapter->dcb_cfg.pfc_mode_enable) {
359                 if ((adapter->hw.mac.type != ixgbe_mac_82598EB) &&
360                         (adapter->hw.fc.current_mode != ixgbe_fc_pfc))
361                         adapter->last_lfc_mode = adapter->hw.fc.current_mode;
362                 adapter->hw.fc.requested_mode = ixgbe_fc_pfc;
363         } else {
364                 if (adapter->hw.mac.type != ixgbe_mac_82598EB)
365                         adapter->hw.fc.requested_mode = adapter->last_lfc_mode;
366                 else
367                         adapter->hw.fc.requested_mode = ixgbe_fc_none;
368         }
369
370         if (adapter->dcb_set_bitmap & BIT_RESETLINK) {
371                 if (netif_running(netdev))
372                         ixgbe_up(adapter);
373                 ret = DCB_HW_CHG_RST;
374         } else if (adapter->dcb_set_bitmap & BIT_PFC) {
375                 if (adapter->hw.mac.type == ixgbe_mac_82598EB)
376                         ixgbe_dcb_config_pfc_82598(&adapter->hw,
377                                                    &adapter->dcb_cfg);
378                 else if (adapter->hw.mac.type == ixgbe_mac_82599EB)
379                         ixgbe_dcb_config_pfc_82599(&adapter->hw,
380                                                    &adapter->dcb_cfg);
381                 ret = DCB_HW_CHG;
382         }
383         if (adapter->dcb_cfg.pfc_mode_enable)
384                 adapter->hw.fc.current_mode = ixgbe_fc_pfc;
385
386         if (adapter->dcb_set_bitmap & BIT_RESETLINK)
387                 clear_bit(__IXGBE_RESETTING, &adapter->state);
388         adapter->dcb_set_bitmap = 0x00;
389         return ret;
390 }
391
392 static u8 ixgbe_dcbnl_getcap(struct net_device *netdev, int capid, u8 *cap)
393 {
394         struct ixgbe_adapter *adapter = netdev_priv(netdev);
395         u8 rval = 0;
396
397         if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
398                 switch (capid) {
399                 case DCB_CAP_ATTR_PG:
400                         *cap = true;
401                         break;
402                 case DCB_CAP_ATTR_PFC:
403                         *cap = true;
404                         break;
405                 case DCB_CAP_ATTR_UP2TC:
406                         *cap = false;
407                         break;
408                 case DCB_CAP_ATTR_PG_TCS:
409                         *cap = 0x80;
410                         break;
411                 case DCB_CAP_ATTR_PFC_TCS:
412                         *cap = 0x80;
413                         break;
414                 case DCB_CAP_ATTR_GSP:
415                         *cap = true;
416                         break;
417                 case DCB_CAP_ATTR_BCN:
418                         *cap = false;
419                         break;
420                 default:
421                         rval = -EINVAL;
422                         break;
423                 }
424         } else {
425                 rval = -EINVAL;
426         }
427
428         return rval;
429 }
430
431 static u8 ixgbe_dcbnl_getnumtcs(struct net_device *netdev, int tcid, u8 *num)
432 {
433         struct ixgbe_adapter *adapter = netdev_priv(netdev);
434         u8 rval = 0;
435
436         if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
437                 switch (tcid) {
438                 case DCB_NUMTCS_ATTR_PG:
439                         *num = MAX_TRAFFIC_CLASS;
440                         break;
441                 case DCB_NUMTCS_ATTR_PFC:
442                         *num = MAX_TRAFFIC_CLASS;
443                         break;
444                 default:
445                         rval = -EINVAL;
446                         break;
447                 }
448         } else {
449                 rval = -EINVAL;
450         }
451
452         return rval;
453 }
454
455 static u8 ixgbe_dcbnl_setnumtcs(struct net_device *netdev, int tcid, u8 num)
456 {
457         return -EINVAL;
458 }
459
460 static u8 ixgbe_dcbnl_getpfcstate(struct net_device *netdev)
461 {
462         struct ixgbe_adapter *adapter = netdev_priv(netdev);
463
464         return adapter->dcb_cfg.pfc_mode_enable;
465 }
466
467 static void ixgbe_dcbnl_setpfcstate(struct net_device *netdev, u8 state)
468 {
469         struct ixgbe_adapter *adapter = netdev_priv(netdev);
470
471         adapter->temp_dcb_cfg.pfc_mode_enable = state;
472         if (adapter->temp_dcb_cfg.pfc_mode_enable !=
473                 adapter->dcb_cfg.pfc_mode_enable)
474                 adapter->dcb_set_bitmap |= BIT_PFC;
475         return;
476 }
477
478 struct dcbnl_rtnl_ops dcbnl_ops = {
479         .getstate       = ixgbe_dcbnl_get_state,
480         .setstate       = ixgbe_dcbnl_set_state,
481         .getpermhwaddr  = ixgbe_dcbnl_get_perm_hw_addr,
482         .setpgtccfgtx   = ixgbe_dcbnl_set_pg_tc_cfg_tx,
483         .setpgbwgcfgtx  = ixgbe_dcbnl_set_pg_bwg_cfg_tx,
484         .setpgtccfgrx   = ixgbe_dcbnl_set_pg_tc_cfg_rx,
485         .setpgbwgcfgrx  = ixgbe_dcbnl_set_pg_bwg_cfg_rx,
486         .getpgtccfgtx   = ixgbe_dcbnl_get_pg_tc_cfg_tx,
487         .getpgbwgcfgtx  = ixgbe_dcbnl_get_pg_bwg_cfg_tx,
488         .getpgtccfgrx   = ixgbe_dcbnl_get_pg_tc_cfg_rx,
489         .getpgbwgcfgrx  = ixgbe_dcbnl_get_pg_bwg_cfg_rx,
490         .setpfccfg      = ixgbe_dcbnl_set_pfc_cfg,
491         .getpfccfg      = ixgbe_dcbnl_get_pfc_cfg,
492         .setall         = ixgbe_dcbnl_set_all,
493         .getcap         = ixgbe_dcbnl_getcap,
494         .getnumtcs      = ixgbe_dcbnl_getnumtcs,
495         .setnumtcs      = ixgbe_dcbnl_setnumtcs,
496         .getpfcstate    = ixgbe_dcbnl_getpfcstate,
497         .setpfcstate    = ixgbe_dcbnl_setpfcstate,
498 };
499