blob: d29e5fc8258216b9d79604bc99b69b66cce3e443 [file] [log] [blame]
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01001/*
2 * include/net/devlink.h - Network physical device Netlink interface
3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11#ifndef _NET_DEVLINK_H_
12#define _NET_DEVLINK_H_
13
14#include <linux/device.h>
15#include <linux/slab.h>
16#include <linux/gfp.h>
17#include <linux/list.h>
18#include <linux/netdevice.h>
19#include <net/net_namespace.h>
20#include <uapi/linux/devlink.h>
21
22struct devlink_ops;
23
24struct devlink {
25 struct list_head list;
26 struct list_head port_list;
Jiri Pirkobf797472016-04-14 18:19:13 +020027 struct list_head sb_list;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010028 const struct devlink_ops *ops;
29 struct device *dev;
30 possible_net_t _net;
31 char priv[0] __aligned(NETDEV_ALIGN);
32};
33
34struct devlink_port {
35 struct list_head list;
36 struct devlink *devlink;
37 unsigned index;
38 bool registered;
39 enum devlink_port_type type;
40 enum devlink_port_type desired_type;
41 void *type_dev;
42 bool split;
43 u32 split_group;
44};
45
Jiri Pirkobf797472016-04-14 18:19:13 +020046struct devlink_sb_pool_info {
47 enum devlink_sb_pool_type pool_type;
48 u32 size;
49 enum devlink_sb_threshold_type threshold_type;
50};
51
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010052struct devlink_ops {
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010053 int (*port_type_set)(struct devlink_port *devlink_port,
54 enum devlink_port_type port_type);
55 int (*port_split)(struct devlink *devlink, unsigned int port_index,
56 unsigned int count);
57 int (*port_unsplit)(struct devlink *devlink, unsigned int port_index);
Jiri Pirkobf797472016-04-14 18:19:13 +020058 int (*sb_pool_get)(struct devlink *devlink, unsigned int sb_index,
59 u16 pool_index,
60 struct devlink_sb_pool_info *pool_info);
61 int (*sb_pool_set)(struct devlink *devlink, unsigned int sb_index,
62 u16 pool_index, u32 size,
63 enum devlink_sb_threshold_type threshold_type);
64 int (*sb_port_pool_get)(struct devlink_port *devlink_port,
65 unsigned int sb_index, u16 pool_index,
66 u32 *p_threshold);
67 int (*sb_port_pool_set)(struct devlink_port *devlink_port,
68 unsigned int sb_index, u16 pool_index,
69 u32 threshold);
70 int (*sb_tc_pool_bind_get)(struct devlink_port *devlink_port,
71 unsigned int sb_index,
72 u16 tc_index,
73 enum devlink_sb_pool_type pool_type,
74 u16 *p_pool_index, u32 *p_threshold);
75 int (*sb_tc_pool_bind_set)(struct devlink_port *devlink_port,
76 unsigned int sb_index,
77 u16 tc_index,
78 enum devlink_sb_pool_type pool_type,
79 u16 pool_index, u32 threshold);
Jiri Pirkodf38daf2016-04-14 18:19:14 +020080 int (*sb_occ_snapshot)(struct devlink *devlink,
81 unsigned int sb_index);
82 int (*sb_occ_max_clear)(struct devlink *devlink,
83 unsigned int sb_index);
84 int (*sb_occ_port_pool_get)(struct devlink_port *devlink_port,
85 unsigned int sb_index, u16 pool_index,
86 u32 *p_cur, u32 *p_max);
87 int (*sb_occ_tc_port_bind_get)(struct devlink_port *devlink_port,
88 unsigned int sb_index,
89 u16 tc_index,
90 enum devlink_sb_pool_type pool_type,
91 u32 *p_cur, u32 *p_max);
Or Gerlitz08f4b592016-07-01 14:51:01 +030092
93 int (*eswitch_mode_get)(struct devlink *devlink, u16 *p_mode);
94 int (*eswitch_mode_set)(struct devlink *devlink, u16 mode);
Roi Dayan59bfde02016-11-22 23:09:57 +020095 int (*eswitch_inline_mode_get)(struct devlink *devlink, u8 *p_inline_mode);
96 int (*eswitch_inline_mode_set)(struct devlink *devlink, u8 inline_mode);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010097};
98
99static inline void *devlink_priv(struct devlink *devlink)
100{
101 BUG_ON(!devlink);
102 return &devlink->priv;
103}
104
105static inline struct devlink *priv_to_devlink(void *priv)
106{
107 BUG_ON(!priv);
108 return container_of(priv, struct devlink, priv);
109}
110
111struct ib_device;
112
113#if IS_ENABLED(CONFIG_NET_DEVLINK)
114
115struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size);
116int devlink_register(struct devlink *devlink, struct device *dev);
117void devlink_unregister(struct devlink *devlink);
118void devlink_free(struct devlink *devlink);
119int devlink_port_register(struct devlink *devlink,
120 struct devlink_port *devlink_port,
121 unsigned int port_index);
122void devlink_port_unregister(struct devlink_port *devlink_port);
123void devlink_port_type_eth_set(struct devlink_port *devlink_port,
124 struct net_device *netdev);
125void devlink_port_type_ib_set(struct devlink_port *devlink_port,
126 struct ib_device *ibdev);
127void devlink_port_type_clear(struct devlink_port *devlink_port);
128void devlink_port_split_set(struct devlink_port *devlink_port,
129 u32 split_group);
Jiri Pirkobf797472016-04-14 18:19:13 +0200130int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
131 u32 size, u16 ingress_pools_count,
132 u16 egress_pools_count, u16 ingress_tc_count,
133 u16 egress_tc_count);
134void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100135
136#else
137
138static inline struct devlink *devlink_alloc(const struct devlink_ops *ops,
139 size_t priv_size)
140{
141 return kzalloc(sizeof(struct devlink) + priv_size, GFP_KERNEL);
142}
143
144static inline int devlink_register(struct devlink *devlink, struct device *dev)
145{
146 return 0;
147}
148
149static inline void devlink_unregister(struct devlink *devlink)
150{
151}
152
153static inline void devlink_free(struct devlink *devlink)
154{
155 kfree(devlink);
156}
157
158static inline int devlink_port_register(struct devlink *devlink,
159 struct devlink_port *devlink_port,
160 unsigned int port_index)
161{
162 return 0;
163}
164
165static inline void devlink_port_unregister(struct devlink_port *devlink_port)
166{
167}
168
169static inline void devlink_port_type_eth_set(struct devlink_port *devlink_port,
170 struct net_device *netdev)
171{
172}
173
174static inline void devlink_port_type_ib_set(struct devlink_port *devlink_port,
175 struct ib_device *ibdev)
176{
177}
178
179static inline void devlink_port_type_clear(struct devlink_port *devlink_port)
180{
181}
182
183static inline void devlink_port_split_set(struct devlink_port *devlink_port,
184 u32 split_group)
185{
186}
187
Jiri Pirkobf797472016-04-14 18:19:13 +0200188static inline int devlink_sb_register(struct devlink *devlink,
189 unsigned int sb_index, u32 size,
190 u16 ingress_pools_count,
Jiri Pirkode33efd2016-04-15 09:17:08 +0200191 u16 egress_pools_count,
192 u16 ingress_tc_count,
193 u16 egress_tc_count)
Jiri Pirkobf797472016-04-14 18:19:13 +0200194{
195 return 0;
196}
197
198static inline void devlink_sb_unregister(struct devlink *devlink,
199 unsigned int sb_index)
200{
201}
202
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100203#endif
204
205#endif /* _NET_DEVLINK_H_ */