1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
3 
4 #ifndef NSP_NSP_H
5 #define NSP_NSP_H 1
6 
7 #include <linux/types.h>
8 #include <linux/if_ether.h>
9 
10 struct firmware;
11 struct nfp_cpp;
12 struct nfp_nsp;
13 
14 struct nfp_nsp *nfp_nsp_open(struct nfp_cpp *cpp);
15 void nfp_nsp_close(struct nfp_nsp *state);
16 u16 nfp_nsp_get_abi_ver_major(struct nfp_nsp *state);
17 u16 nfp_nsp_get_abi_ver_minor(struct nfp_nsp *state);
18 int nfp_nsp_wait(struct nfp_nsp *state);
19 int nfp_nsp_device_soft_reset(struct nfp_nsp *state);
20 int nfp_nsp_load_fw(struct nfp_nsp *state, const struct firmware *fw);
21 int nfp_nsp_write_flash(struct nfp_nsp *state, const struct firmware *fw);
22 int nfp_nsp_mac_reinit(struct nfp_nsp *state);
23 int nfp_nsp_load_stored_fw(struct nfp_nsp *state);
24 int nfp_nsp_hwinfo_lookup(struct nfp_nsp *state, void *buf, unsigned int size);
25 int nfp_nsp_hwinfo_lookup_optional(struct nfp_nsp *state, void *buf,
26 				   unsigned int size, const char *default_val);
27 int nfp_nsp_hwinfo_set(struct nfp_nsp *state, void *buf, unsigned int size);
28 int nfp_nsp_fw_loaded(struct nfp_nsp *state);
29 int nfp_nsp_read_module_eeprom(struct nfp_nsp *state, int eth_index,
30 			       unsigned int offset, void *data,
31 			       unsigned int len, unsigned int *read_len);
32 
33 static inline bool nfp_nsp_has_mac_reinit(struct nfp_nsp *state)
34 {
35 	return nfp_nsp_get_abi_ver_minor(state) > 20;
36 }
37 
38 static inline bool nfp_nsp_has_stored_fw_load(struct nfp_nsp *state)
39 {
40 	return nfp_nsp_get_abi_ver_minor(state) > 23;
41 }
42 
43 static inline bool nfp_nsp_has_hwinfo_lookup(struct nfp_nsp *state)
44 {
45 	return nfp_nsp_get_abi_ver_minor(state) > 24;
46 }
47 
48 static inline bool nfp_nsp_has_hwinfo_set(struct nfp_nsp *state)
49 {
50 	return nfp_nsp_get_abi_ver_minor(state) > 25;
51 }
52 
53 static inline bool nfp_nsp_has_fw_loaded(struct nfp_nsp *state)
54 {
55 	return nfp_nsp_get_abi_ver_minor(state) > 25;
56 }
57 
58 static inline bool nfp_nsp_has_versions(struct nfp_nsp *state)
59 {
60 	return nfp_nsp_get_abi_ver_minor(state) > 27;
61 }
62 
63 static inline bool nfp_nsp_has_read_module_eeprom(struct nfp_nsp *state)
64 {
65 	return nfp_nsp_get_abi_ver_minor(state) > 28;
66 }
67 
68 static inline bool nfp_nsp_has_read_media(struct nfp_nsp *state)
69 {
70 	return nfp_nsp_get_abi_ver_minor(state) > 33;
71 }
72 
73 enum nfp_eth_interface {
74 	NFP_INTERFACE_NONE	= 0,
75 	NFP_INTERFACE_SFP	= 1,
76 	NFP_INTERFACE_SFPP	= 10,
77 	NFP_INTERFACE_SFP28	= 28,
78 	NFP_INTERFACE_QSFP	= 40,
79 	NFP_INTERFACE_RJ45	= 45,
80 	NFP_INTERFACE_CXP	= 100,
81 	NFP_INTERFACE_QSFP28	= 112,
82 };
83 
84 enum nfp_eth_media {
85 	NFP_MEDIA_DAC_PASSIVE = 0,
86 	NFP_MEDIA_DAC_ACTIVE,
87 	NFP_MEDIA_FIBRE,
88 };
89 
90 enum nfp_eth_aneg {
91 	NFP_ANEG_AUTO = 0,
92 	NFP_ANEG_SEARCH,
93 	NFP_ANEG_25G_CONSORTIUM,
94 	NFP_ANEG_25G_IEEE,
95 	NFP_ANEG_DISABLED,
96 };
97 
98 enum nfp_eth_fec {
99 	NFP_FEC_AUTO_BIT = 0,
100 	NFP_FEC_BASER_BIT,
101 	NFP_FEC_REED_SOLOMON_BIT,
102 	NFP_FEC_DISABLED_BIT,
103 };
104 
105 /* link modes about RJ45 haven't been used, so there's no mapping to them */
106 enum nfp_ethtool_link_mode_list {
107 	NFP_MEDIA_W0_RJ45_10M,
108 	NFP_MEDIA_W0_RJ45_10M_HD,
109 	NFP_MEDIA_W0_RJ45_100M,
110 	NFP_MEDIA_W0_RJ45_100M_HD,
111 	NFP_MEDIA_W0_RJ45_1G,
112 	NFP_MEDIA_W0_RJ45_2P5G,
113 	NFP_MEDIA_W0_RJ45_5G,
114 	NFP_MEDIA_W0_RJ45_10G,
115 	NFP_MEDIA_1000BASE_CX,
116 	NFP_MEDIA_1000BASE_KX,
117 	NFP_MEDIA_10GBASE_KX4,
118 	NFP_MEDIA_10GBASE_KR,
119 	NFP_MEDIA_10GBASE_CX4,
120 	NFP_MEDIA_10GBASE_CR,
121 	NFP_MEDIA_10GBASE_SR,
122 	NFP_MEDIA_10GBASE_ER,
123 	NFP_MEDIA_25GBASE_KR,
124 	NFP_MEDIA_25GBASE_KR_S,
125 	NFP_MEDIA_25GBASE_CR,
126 	NFP_MEDIA_25GBASE_CR_S,
127 	NFP_MEDIA_25GBASE_SR,
128 	NFP_MEDIA_40GBASE_CR4,
129 	NFP_MEDIA_40GBASE_KR4,
130 	NFP_MEDIA_40GBASE_SR4,
131 	NFP_MEDIA_40GBASE_LR4,
132 	NFP_MEDIA_50GBASE_KR,
133 	NFP_MEDIA_50GBASE_SR,
134 	NFP_MEDIA_50GBASE_CR,
135 	NFP_MEDIA_50GBASE_LR,
136 	NFP_MEDIA_50GBASE_ER,
137 	NFP_MEDIA_50GBASE_FR,
138 	NFP_MEDIA_100GBASE_KR4,
139 	NFP_MEDIA_100GBASE_SR4,
140 	NFP_MEDIA_100GBASE_CR4,
141 	NFP_MEDIA_100GBASE_KP4,
142 	NFP_MEDIA_100GBASE_CR10,
143 	NFP_MEDIA_10GBASE_LR,
144 	NFP_MEDIA_25GBASE_LR,
145 	NFP_MEDIA_25GBASE_ER,
146 	NFP_MEDIA_LINK_MODES_NUMBER
147 };
148 
149 #define NFP_FEC_AUTO		BIT(NFP_FEC_AUTO_BIT)
150 #define NFP_FEC_BASER		BIT(NFP_FEC_BASER_BIT)
151 #define NFP_FEC_REED_SOLOMON	BIT(NFP_FEC_REED_SOLOMON_BIT)
152 #define NFP_FEC_DISABLED	BIT(NFP_FEC_DISABLED_BIT)
153 
154 /* Defines the valid values of the 'abi_drv_reset' hwinfo key */
155 #define NFP_NSP_DRV_RESET_DISK			0
156 #define NFP_NSP_DRV_RESET_ALWAYS		1
157 #define NFP_NSP_DRV_RESET_NEVER			2
158 #define NFP_NSP_DRV_RESET_DEFAULT		"0"
159 
160 /* Defines the valid values of the 'app_fw_from_flash' hwinfo key */
161 #define NFP_NSP_APP_FW_LOAD_DISK		0
162 #define NFP_NSP_APP_FW_LOAD_FLASH		1
163 #define NFP_NSP_APP_FW_LOAD_PREF		2
164 #define NFP_NSP_APP_FW_LOAD_DEFAULT		"2"
165 
166 /* Define the default value for the 'abi_drv_load_ifc' key */
167 #define NFP_NSP_DRV_LOAD_IFC_DEFAULT		"0x10ff"
168 
169 /**
170  * struct nfp_eth_table - ETH table information
171  * @count:	number of table entries
172  * @max_index:	max of @index fields of all @ports
173  * @ports:	table of ports
174  *
175  * @ports.eth_index:	port index according to legacy ethX numbering
176  * @ports.index:	chip-wide first channel index
177  * @ports.nbi:		NBI index
178  * @ports.base:		first channel index (within NBI)
179  * @ports.lanes:	number of channels
180  * @ports.speed:	interface speed (in Mbps)
181  * @ports.interface:	interface (module) plugged in
182  * @ports.media:	media type of the @interface
183  * @ports.fec:		forward error correction mode
184  * @ports.act_fec:	active forward error correction mode
185  * @ports.aneg:		auto negotiation mode
186  * @ports.mac_addr:	interface MAC address
187  * @ports.label_port:	port id
188  * @ports.label_subport:  id of interface within port (for split ports)
189  * @ports.enabled:	is enabled?
190  * @ports.tx_enabled:	is TX enabled?
191  * @ports.rx_enabled:	is RX enabled?
192  * @ports.override_changed: is media reconfig pending?
193  *
194  * @ports.port_type:	one of %PORT_* defines for ethtool
195  * @ports.port_lanes:	total number of lanes on the port (sum of lanes of all
196  *			subports)
197  * @ports.is_split:	is interface part of a split port
198  * @ports.fec_modes_supported:	bitmap of FEC modes supported
199  */
200 struct nfp_eth_table {
201 	unsigned int count;
202 	unsigned int max_index;
203 	struct nfp_eth_table_port {
204 		unsigned int eth_index;
205 		unsigned int index;
206 		unsigned int nbi;
207 		unsigned int base;
208 		unsigned int lanes;
209 		unsigned int speed;
210 
211 		unsigned int interface;
212 		enum nfp_eth_media media;
213 
214 		enum nfp_eth_fec fec;
215 		enum nfp_eth_fec act_fec;
216 		enum nfp_eth_aneg aneg;
217 
218 		u8 mac_addr[ETH_ALEN];
219 
220 		u8 label_port;
221 		u8 label_subport;
222 
223 		bool enabled;
224 		bool tx_enabled;
225 		bool rx_enabled;
226 		bool supp_aneg;
227 
228 		bool override_changed;
229 
230 		/* Computed fields */
231 		u8 port_type;
232 
233 		unsigned int port_lanes;
234 
235 		bool is_split;
236 
237 		unsigned int fec_modes_supported;
238 	} ports[];
239 };
240 
241 struct nfp_eth_table *nfp_eth_read_ports(struct nfp_cpp *cpp);
242 struct nfp_eth_table *
243 __nfp_eth_read_ports(struct nfp_cpp *cpp, struct nfp_nsp *nsp);
244 
245 int nfp_eth_set_mod_enable(struct nfp_cpp *cpp, unsigned int idx, bool enable);
246 int nfp_eth_set_configured(struct nfp_cpp *cpp, unsigned int idx,
247 			   bool configed);
248 int
249 nfp_eth_set_fec(struct nfp_cpp *cpp, unsigned int idx, enum nfp_eth_fec mode);
250 
251 int nfp_eth_set_idmode(struct nfp_cpp *cpp, unsigned int idx, bool state);
252 
253 static inline bool nfp_eth_can_support_fec(struct nfp_eth_table_port *eth_port)
254 {
255 	return !!eth_port->fec_modes_supported;
256 }
257 
258 static inline unsigned int
259 nfp_eth_supported_fec_modes(struct nfp_eth_table_port *eth_port)
260 {
261 	return eth_port->fec_modes_supported;
262 }
263 
264 struct nfp_nsp *nfp_eth_config_start(struct nfp_cpp *cpp, unsigned int idx);
265 int nfp_eth_config_commit_end(struct nfp_nsp *nsp);
266 void nfp_eth_config_cleanup_end(struct nfp_nsp *nsp);
267 
268 int __nfp_eth_set_aneg(struct nfp_nsp *nsp, enum nfp_eth_aneg mode);
269 int __nfp_eth_set_speed(struct nfp_nsp *nsp, unsigned int speed);
270 int __nfp_eth_set_split(struct nfp_nsp *nsp, unsigned int lanes);
271 
272 /**
273  * struct nfp_nsp_identify - NSP static information
274  * @version:      opaque version string
275  * @flags:        version flags
276  * @br_primary:   branch id of primary bootloader
277  * @br_secondary: branch id of secondary bootloader
278  * @br_nsp:       branch id of NSP
279  * @primary:      version of primarary bootloader
280  * @secondary:    version id of secondary bootloader
281  * @nsp:          version id of NSP
282  * @sensor_mask:  mask of present sensors available on NIC
283  */
284 struct nfp_nsp_identify {
285 	char version[40];
286 	u8 flags;
287 	u8 br_primary;
288 	u8 br_secondary;
289 	u8 br_nsp;
290 	u16 primary;
291 	u16 secondary;
292 	u16 nsp;
293 	u64 sensor_mask;
294 };
295 
296 struct nfp_nsp_identify *__nfp_nsp_identify(struct nfp_nsp *nsp);
297 
298 enum nfp_nsp_sensor_id {
299 	NFP_SENSOR_CHIP_TEMPERATURE,
300 	NFP_SENSOR_ASSEMBLY_POWER,
301 	NFP_SENSOR_ASSEMBLY_12V_POWER,
302 	NFP_SENSOR_ASSEMBLY_3V3_POWER,
303 };
304 
305 int nfp_hwmon_read_sensor(struct nfp_cpp *cpp, enum nfp_nsp_sensor_id id,
306 			  long *val);
307 
308 struct nfp_eth_media_buf {
309 	u8 eth_index;
310 	u8 reserved[7];
311 	__le64 supported_modes[2];
312 	__le64 advertised_modes[2];
313 };
314 
315 int nfp_nsp_read_media(struct nfp_nsp *state, void *buf, unsigned int size);
316 int nfp_eth_read_media(struct nfp_cpp *cpp, struct nfp_eth_media_buf *ethm);
317 
318 #define NFP_NSP_VERSION_BUFSZ	1024 /* reasonable size, not in the ABI */
319 
320 enum nfp_nsp_versions {
321 	NFP_VERSIONS_BSP,
322 	NFP_VERSIONS_CPLD,
323 	NFP_VERSIONS_APP,
324 	NFP_VERSIONS_BUNDLE,
325 	NFP_VERSIONS_UNDI,
326 	NFP_VERSIONS_NCSI,
327 	NFP_VERSIONS_CFGR,
328 };
329 
330 int nfp_nsp_versions(struct nfp_nsp *state, void *buf, unsigned int size);
331 const char *nfp_nsp_versions_get(enum nfp_nsp_versions id, bool flash,
332 				 const u8 *buf, unsigned int size);
333 #endif
334