Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-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
32 /* Callbacks for DCB netlink in the kernel */
33 #define BIT_DCB_MODE    0x01
34 #define BIT_PFC         0x02
35 #define BIT_PG_RX       0x04
36 #define BIT_PG_TX       0x08
37 #define BIT_BCN         0x10
38 #define BIT_LINKSPEED   0x80
39
40 int ixgbe_copy_dcb_cfg(struct ixgbe_dcb_config *src_dcb_cfg,
41                        struct ixgbe_dcb_config *dst_dcb_cfg, int tc_max)
42 {
43         struct tc_configuration *src_tc_cfg = NULL;
44         struct tc_configuration *dst_tc_cfg = NULL;
45         int i;
46
47         if (!src_dcb_cfg || !dst_dcb_cfg)
48                 return -EINVAL;
49
50         for (i = DCB_PG_ATTR_TC_0; i < tc_max + DCB_PG_ATTR_TC_0; i++) {
51                 src_tc_cfg = &src_dcb_cfg->tc_config[i - DCB_PG_ATTR_TC_0];
52                 dst_tc_cfg = &dst_dcb_cfg->tc_config[i - DCB_PG_ATTR_TC_0];
53
54                 dst_tc_cfg->path[DCB_TX_CONFIG].prio_type =
55                                 src_tc_cfg->path[DCB_TX_CONFIG].prio_type;
56
57                 dst_tc_cfg->path[DCB_TX_CONFIG].bwg_id =
58                                 src_tc_cfg->path[DCB_TX_CONFIG].bwg_id;
59
60                 dst_tc_cfg->path[DCB_TX_CONFIG].bwg_percent =
61                                 src_tc_cfg->path[DCB_TX_CONFIG].bwg_percent;
62
63                 dst_tc_cfg->path[DCB_TX_CONFIG].up_to_tc_bitmap =
64                                 src_tc_cfg->path[DCB_TX_CONFIG].up_to_tc_bitmap;
65
66                 dst_tc_cfg->path[DCB_RX_CONFIG].prio_type =
67                                 src_tc_cfg->path[DCB_RX_CONFIG].prio_type;
68
69                 dst_tc_cfg->path[DCB_RX_CONFIG].bwg_id =
70                                 src_tc_cfg->path[DCB_RX_CONFIG].bwg_id;
71
72                 dst_tc_cfg->path[DCB_RX_CONFIG].bwg_percent =
73                                 src_tc_cfg->path[DCB_RX_CONFIG].bwg_percent;
74
75                 dst_tc_cfg->path[DCB_RX_CONFIG].up_to_tc_bitmap =
76                                 src_tc_cfg->path[DCB_RX_CONFIG].up_to_tc_bitmap;
77         }
78
79         for (i = DCB_PG_ATTR_BW_ID_0; i < DCB_PG_ATTR_BW_ID_MAX; i++) {
80                 dst_dcb_cfg->bw_percentage[DCB_TX_CONFIG]
81                         [i-DCB_PG_ATTR_BW_ID_0] = src_dcb_cfg->bw_percentage
82                                 [DCB_TX_CONFIG][i-DCB_PG_ATTR_BW_ID_0];
83                 dst_dcb_cfg->bw_percentage[DCB_RX_CONFIG]
84                         [i-DCB_PG_ATTR_BW_ID_0] = src_dcb_cfg->bw_percentage
85                                 [DCB_RX_CONFIG][i-DCB_PG_ATTR_BW_ID_0];
86         }
87
88         for (i = DCB_PFC_UP_ATTR_0; i < DCB_PFC_UP_ATTR_MAX; i++) {
89                 dst_dcb_cfg->tc_config[i - DCB_PFC_UP_ATTR_0].dcb_pfc =
90                         src_dcb_cfg->tc_config[i - DCB_PFC_UP_ATTR_0].dcb_pfc;
91         }
92
93         return 0;
94 }
95
96 static u8 ixgbe_dcbnl_get_state(struct net_device *netdev)
97 {
98         struct ixgbe_adapter *adapter = netdev_priv(netdev);
99
100         DPRINTK(DRV, INFO, "Get DCB Admin Mode.\n");
101
102         return !!(adapter->flags & IXGBE_FLAG_DCB_ENABLED);
103 }
104
105 static u8 ixgbe_dcbnl_set_state(struct net_device *netdev, u8 state)
106 {
107         u8 err = 0;
108         struct ixgbe_adapter *adapter = netdev_priv(netdev);
109
110         DPRINTK(DRV, INFO, "Set DCB Admin Mode.\n");
111
112         if (state > 0) {
113                 /* Turn on DCB */
114                 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED)
115                         goto out;
116
117                 if (!(adapter->flags & IXGBE_FLAG_MSIX_ENABLED)) {
118                         DPRINTK(DRV, ERR, "Enable failed, needs MSI-X\n");
119                         err = 1;
120                         goto out;
121                 }
122
123                 if (netif_running(netdev))
124                         netdev->netdev_ops->ndo_stop(netdev);
125                 ixgbe_reset_interrupt_capability(adapter);
126                 ixgbe_napi_del_all(adapter);
127                 INIT_LIST_HEAD(&netdev->napi_list);
128                 kfree(adapter->tx_ring);
129                 kfree(adapter->rx_ring);
130                 adapter->tx_ring = NULL;
131                 adapter->rx_ring = NULL;
132
133                 adapter->hw.fc.requested_mode = ixgbe_fc_pfc;
134                 adapter->flags &= ~IXGBE_FLAG_RSS_ENABLED;
135                 adapter->flags |= IXGBE_FLAG_DCB_ENABLED;
136                 ixgbe_init_interrupt_scheme(adapter);
137                 if (netif_running(netdev))
138                         netdev->netdev_ops->ndo_open(netdev);
139         } else {
140                 /* Turn off DCB */
141                 if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
142                         adapter->hw.fc.requested_mode = ixgbe_fc_default;
143                         if (netif_running(netdev))
144                                 netdev->netdev_ops->ndo_stop(netdev);
145                         ixgbe_reset_interrupt_capability(adapter);
146                         ixgbe_napi_del_all(adapter);
147                         INIT_LIST_HEAD(&netdev->napi_list);
148                         kfree(adapter->tx_ring);
149                         kfree(adapter->rx_ring);
150                         adapter->tx_ring = NULL;
151                         adapter->rx_ring = NULL;
152
153                         adapter->flags &= ~IXGBE_FLAG_DCB_ENABLED;
154                         adapter->flags |= IXGBE_FLAG_RSS_ENABLED;
155                         ixgbe_init_interrupt_scheme(adapter);
156                         if (netif_running(netdev))
157                                 netdev->netdev_ops->ndo_open(netdev);
158                 }
159         }
160 out:
161         return err;
162 }
163
164 static void ixgbe_dcbnl_get_perm_hw_addr(struct net_device *netdev,
165                                          u8 *perm_addr)
166 {
167         struct ixgbe_adapter *adapter = netdev_priv(netdev);
168         int i;
169
170         for (i = 0; i < netdev->addr_len; i++)
171                 perm_addr[i] = adapter->hw.mac.perm_addr[i];
172 }
173
174 static void ixgbe_dcbnl_set_pg_tc_cfg_tx(struct net_device *netdev, int tc,
175                                          u8 prio, u8 bwg_id, u8 bw_pct,
176                                          u8 up_map)
177 {
178         struct ixgbe_adapter *adapter = netdev_priv(netdev);
179
180         if (prio != DCB_ATTR_VALUE_UNDEFINED)
181                 adapter->temp_dcb_cfg.tc_config[tc].path[0].prio_type = prio;
182         if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
183                 adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_id = bwg_id;
184         if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
185                 adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_percent =
186                         bw_pct;
187         if (up_map != DCB_ATTR_VALUE_UNDEFINED)
188                 adapter->temp_dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap =
189                         up_map;
190
191         if ((adapter->temp_dcb_cfg.tc_config[tc].path[0].prio_type !=
192              adapter->dcb_cfg.tc_config[tc].path[0].prio_type) ||
193             (adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_id !=
194              adapter->dcb_cfg.tc_config[tc].path[0].bwg_id) ||
195             (adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_percent !=
196              adapter->dcb_cfg.tc_config[tc].path[0].bwg_percent) ||
197             (adapter->temp_dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap !=
198              adapter->dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap))
199                 adapter->dcb_set_bitmap |= BIT_PG_TX;
200 }
201
202 static void ixgbe_dcbnl_set_pg_bwg_cfg_tx(struct net_device *netdev, int bwg_id,
203                                           u8 bw_pct)
204 {
205         struct ixgbe_adapter *adapter = netdev_priv(netdev);
206
207         adapter->temp_dcb_cfg.bw_percentage[0][bwg_id] = bw_pct;
208
209         if (adapter->temp_dcb_cfg.bw_percentage[0][bwg_id] !=
210             adapter->dcb_cfg.bw_percentage[0][bwg_id])
211                 adapter->dcb_set_bitmap |= BIT_PG_RX;
212 }
213
214 static void ixgbe_dcbnl_set_pg_tc_cfg_rx(struct net_device *netdev, int tc,
215                                          u8 prio, u8 bwg_id, u8 bw_pct,
216                                          u8 up_map)
217 {
218         struct ixgbe_adapter *adapter = netdev_priv(netdev);
219
220         if (prio != DCB_ATTR_VALUE_UNDEFINED)
221                 adapter->temp_dcb_cfg.tc_config[tc].path[1].prio_type = prio;
222         if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
223                 adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_id = bwg_id;
224         if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
225                 adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_percent =
226                         bw_pct;
227         if (up_map != DCB_ATTR_VALUE_UNDEFINED)
228                 adapter->temp_dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap =
229                         up_map;
230
231         if ((adapter->temp_dcb_cfg.tc_config[tc].path[1].prio_type !=
232              adapter->dcb_cfg.tc_config[tc].path[1].prio_type) ||
233             (adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_id !=
234              adapter->dcb_cfg.tc_config[tc].path[1].bwg_id) ||
235             (adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_percent !=
236              adapter->dcb_cfg.tc_config[tc].path[1].bwg_percent) ||
237             (adapter->temp_dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap !=
238              adapter->dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap))
239                 adapter->dcb_set_bitmap |= BIT_PG_RX;
240 }
241
242 static void ixgbe_dcbnl_set_pg_bwg_cfg_rx(struct net_device *netdev, int bwg_id,
243                                           u8 bw_pct)
244 {
245         struct ixgbe_adapter *adapter = netdev_priv(netdev);
246
247         adapter->temp_dcb_cfg.bw_percentage[1][bwg_id] = bw_pct;
248
249         if (adapter->temp_dcb_cfg.bw_percentage[1][bwg_id] !=
250             adapter->dcb_cfg.bw_percentage[1][bwg_id])
251                 adapter->dcb_set_bitmap |= BIT_PG_RX;
252 }
253
254 static void ixgbe_dcbnl_get_pg_tc_cfg_tx(struct net_device *netdev, int tc,
255                                          u8 *prio, u8 *bwg_id, u8 *bw_pct,
256                                          u8 *up_map)
257 {
258         struct ixgbe_adapter *adapter = netdev_priv(netdev);
259
260         *prio = adapter->dcb_cfg.tc_config[tc].path[0].prio_type;
261         *bwg_id = adapter->dcb_cfg.tc_config[tc].path[0].bwg_id;
262         *bw_pct = adapter->dcb_cfg.tc_config[tc].path[0].bwg_percent;
263         *up_map = adapter->dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap;
264 }
265
266 static void ixgbe_dcbnl_get_pg_bwg_cfg_tx(struct net_device *netdev, int bwg_id,
267                                           u8 *bw_pct)
268 {
269         struct ixgbe_adapter *adapter = netdev_priv(netdev);
270
271         *bw_pct = adapter->dcb_cfg.bw_percentage[0][bwg_id];
272 }
273
274 static void ixgbe_dcbnl_get_pg_tc_cfg_rx(struct net_device *netdev, int tc,
275                                          u8 *prio, u8 *bwg_id, u8 *bw_pct,
276                                          u8 *up_map)
277 {
278         struct ixgbe_adapter *adapter = netdev_priv(netdev);
279
280         *prio = adapter->dcb_cfg.tc_config[tc].path[1].prio_type;
281         *bwg_id = adapter->dcb_cfg.tc_config[tc].path[1].bwg_id;
282         *bw_pct = adapter->dcb_cfg.tc_config[tc].path[1].bwg_percent;
283         *up_map = adapter->dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap;
284 }
285
286 static void ixgbe_dcbnl_get_pg_bwg_cfg_rx(struct net_device *netdev, int bwg_id,
287                                           u8 *bw_pct)
288 {
289         struct ixgbe_adapter *adapter = netdev_priv(netdev);
290
291         *bw_pct = adapter->dcb_cfg.bw_percentage[1][bwg_id];
292 }
293
294 static void ixgbe_dcbnl_set_pfc_cfg(struct net_device *netdev, int priority,
295                                     u8 setting)
296 {
297         struct ixgbe_adapter *adapter = netdev_priv(netdev);
298
299         adapter->temp_dcb_cfg.tc_config[priority].dcb_pfc = setting;
300         if (adapter->temp_dcb_cfg.tc_config[priority].dcb_pfc !=
301             adapter->dcb_cfg.tc_config[priority].dcb_pfc)
302                 adapter->dcb_set_bitmap |= BIT_PFC;
303 }
304
305 static void ixgbe_dcbnl_get_pfc_cfg(struct net_device *netdev, int priority,
306                                     u8 *setting)
307 {
308         struct ixgbe_adapter *adapter = netdev_priv(netdev);
309
310         *setting = adapter->dcb_cfg.tc_config[priority].dcb_pfc;
311 }
312
313 static u8 ixgbe_dcbnl_set_all(struct net_device *netdev)
314 {
315         struct ixgbe_adapter *adapter = netdev_priv(netdev);
316         int ret;
317
318         adapter->dcb_set_bitmap &= ~BIT_BCN;    /* no set for BCN */
319         if (!adapter->dcb_set_bitmap)
320                 return 1;
321
322         while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
323                 msleep(1);
324
325         if (netif_running(netdev))
326                 ixgbe_down(adapter);
327
328         ret = ixgbe_copy_dcb_cfg(&adapter->temp_dcb_cfg, &adapter->dcb_cfg,
329                                  adapter->ring_feature[RING_F_DCB].indices);
330         if (ret) {
331                 clear_bit(__IXGBE_RESETTING, &adapter->state);
332                 return ret;
333         }
334
335         if (netif_running(netdev))
336                 ixgbe_up(adapter);
337
338         adapter->dcb_set_bitmap = 0x00;
339         clear_bit(__IXGBE_RESETTING, &adapter->state);
340         return ret;
341 }
342
343 static u8 ixgbe_dcbnl_getcap(struct net_device *netdev, int capid, u8 *cap)
344 {
345         struct ixgbe_adapter *adapter = netdev_priv(netdev);
346         u8 rval = 0;
347
348         if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
349                 switch (capid) {
350                 case DCB_CAP_ATTR_PG:
351                         *cap = true;
352                         break;
353                 case DCB_CAP_ATTR_PFC:
354                         *cap = true;
355                         break;
356                 case DCB_CAP_ATTR_UP2TC:
357                         *cap = false;
358                         break;
359                 case DCB_CAP_ATTR_PG_TCS:
360                         *cap = 0x80;
361                         break;
362                 case DCB_CAP_ATTR_PFC_TCS:
363                         *cap = 0x80;
364                         break;
365                 case DCB_CAP_ATTR_GSP:
366                         *cap = true;
367                         break;
368                 case DCB_CAP_ATTR_BCN:
369                         *cap = false;
370                         break;
371                 default:
372                         rval = -EINVAL;
373                         break;
374                 }
375         } else {
376                 rval = -EINVAL;
377         }
378
379         return rval;
380 }
381
382 static u8 ixgbe_dcbnl_getnumtcs(struct net_device *netdev, int tcid, u8 *num)
383 {
384         struct ixgbe_adapter *adapter = netdev_priv(netdev);
385         u8 rval = 0;
386
387         if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
388                 switch (tcid) {
389                 case DCB_NUMTCS_ATTR_PG:
390                         *num = MAX_TRAFFIC_CLASS;
391                         break;
392                 case DCB_NUMTCS_ATTR_PFC:
393                         *num = MAX_TRAFFIC_CLASS;
394                         break;
395                 default:
396                         rval = -EINVAL;
397                         break;
398                 }
399         } else {
400                 rval = -EINVAL;
401         }
402
403         return rval;
404 }
405
406 static u8 ixgbe_dcbnl_setnumtcs(struct net_device *netdev, int tcid, u8 num)
407 {
408         return -EINVAL;
409 }
410
411 static u8 ixgbe_dcbnl_getpfcstate(struct net_device *netdev)
412 {
413         struct ixgbe_adapter *adapter = netdev_priv(netdev);
414
415         return !!(adapter->flags & IXGBE_FLAG_DCB_ENABLED);
416 }
417
418 static void ixgbe_dcbnl_setpfcstate(struct net_device *netdev, u8 state)
419 {
420         return;
421 }
422
423 struct dcbnl_rtnl_ops dcbnl_ops = {
424         .getstate       = ixgbe_dcbnl_get_state,
425         .setstate       = ixgbe_dcbnl_set_state,
426         .getpermhwaddr  = ixgbe_dcbnl_get_perm_hw_addr,
427         .setpgtccfgtx   = ixgbe_dcbnl_set_pg_tc_cfg_tx,
428         .setpgbwgcfgtx  = ixgbe_dcbnl_set_pg_bwg_cfg_tx,
429         .setpgtccfgrx   = ixgbe_dcbnl_set_pg_tc_cfg_rx,
430         .setpgbwgcfgrx  = ixgbe_dcbnl_set_pg_bwg_cfg_rx,
431         .getpgtccfgtx   = ixgbe_dcbnl_get_pg_tc_cfg_tx,
432         .getpgbwgcfgtx  = ixgbe_dcbnl_get_pg_bwg_cfg_tx,
433         .getpgtccfgrx   = ixgbe_dcbnl_get_pg_tc_cfg_rx,
434         .getpgbwgcfgrx  = ixgbe_dcbnl_get_pg_bwg_cfg_rx,
435         .setpfccfg      = ixgbe_dcbnl_set_pfc_cfg,
436         .getpfccfg      = ixgbe_dcbnl_get_pfc_cfg,
437         .setall         = ixgbe_dcbnl_set_all,
438         .getcap         = ixgbe_dcbnl_getcap,
439         .getnumtcs      = ixgbe_dcbnl_getnumtcs,
440         .setnumtcs      = ixgbe_dcbnl_setnumtcs,
441         .getpfcstate    = ixgbe_dcbnl_getpfcstate,
442         .setpfcstate    = ixgbe_dcbnl_setpfcstate,
443 };
444