2 * Copyright (C) 2015-2017 Netronome Systems, Inc.
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 /* Authors: David Brunecz <david.brunecz@netronome.com>
35 * Jakub Kicinski <jakub.kicinski@netronome.com>
36 * Jason Mcmullan <jason.mcmullan@netronome.com>
39 #include <linux/bitfield.h>
40 #include <linux/ethtool.h>
41 #include <linux/if_ether.h>
42 #include <linux/kernel.h>
43 #include <linux/module.h>
47 #include "nfp6000/nfp6000.h"
49 #define NSP_ETH_NBI_PORT_COUNT 24
50 #define NSP_ETH_MAX_COUNT (2 * NSP_ETH_NBI_PORT_COUNT)
51 #define NSP_ETH_TABLE_SIZE (NSP_ETH_MAX_COUNT * \
52 sizeof(union eth_table_entry))
54 #define NSP_ETH_PORT_LANES GENMASK_ULL(3, 0)
55 #define NSP_ETH_PORT_INDEX GENMASK_ULL(15, 8)
56 #define NSP_ETH_PORT_LABEL GENMASK_ULL(53, 48)
57 #define NSP_ETH_PORT_PHYLABEL GENMASK_ULL(59, 54)
59 #define NSP_ETH_PORT_LANES_MASK cpu_to_le64(NSP_ETH_PORT_LANES)
61 #define NSP_ETH_STATE_CONFIGURED BIT_ULL(0)
62 #define NSP_ETH_STATE_ENABLED BIT_ULL(1)
63 #define NSP_ETH_STATE_TX_ENABLED BIT_ULL(2)
64 #define NSP_ETH_STATE_RX_ENABLED BIT_ULL(3)
65 #define NSP_ETH_STATE_RATE GENMASK_ULL(11, 8)
66 #define NSP_ETH_STATE_INTERFACE GENMASK_ULL(19, 12)
67 #define NSP_ETH_STATE_MEDIA GENMASK_ULL(21, 20)
68 #define NSP_ETH_STATE_OVRD_CHNG BIT_ULL(22)
69 #define NSP_ETH_STATE_ANEG GENMASK_ULL(25, 23)
71 #define NSP_ETH_CTRL_CONFIGURED BIT_ULL(0)
72 #define NSP_ETH_CTRL_ENABLED BIT_ULL(1)
73 #define NSP_ETH_CTRL_TX_ENABLED BIT_ULL(2)
74 #define NSP_ETH_CTRL_RX_ENABLED BIT_ULL(3)
75 #define NSP_ETH_CTRL_SET_RATE BIT_ULL(4)
76 #define NSP_ETH_CTRL_SET_LANES BIT_ULL(5)
77 #define NSP_ETH_CTRL_SET_ANEG BIT_ULL(6)
97 union eth_table_entry {
105 __le64 raw[NSP_ETH_NUM_RAW];
108 static const struct {
109 enum nfp_eth_rate rate;
111 } nsp_eth_rate_tbl[] = {
112 { RATE_INVALID, 0, },
113 { RATE_10M, SPEED_10, },
114 { RATE_100M, SPEED_100, },
115 { RATE_1G, SPEED_1000, },
116 { RATE_10G, SPEED_10000, },
117 { RATE_25G, SPEED_25000, },
120 static unsigned int nfp_eth_rate2speed(enum nfp_eth_rate rate)
124 for (i = 0; i < ARRAY_SIZE(nsp_eth_rate_tbl); i++)
125 if (nsp_eth_rate_tbl[i].rate == rate)
126 return nsp_eth_rate_tbl[i].speed;
131 static unsigned int nfp_eth_speed2rate(unsigned int speed)
135 for (i = 0; i < ARRAY_SIZE(nsp_eth_rate_tbl); i++)
136 if (nsp_eth_rate_tbl[i].speed == speed)
137 return nsp_eth_rate_tbl[i].rate;
142 static void nfp_eth_copy_mac_reverse(u8 *dst, const u8 *src)
146 for (i = 0; i < ETH_ALEN; i++)
147 dst[ETH_ALEN - i - 1] = src[i];
151 nfp_eth_port_translate(struct nfp_nsp *nsp, const union eth_table_entry *src,
152 unsigned int index, struct nfp_eth_table_port *dst)
157 port = le64_to_cpu(src->port);
158 state = le64_to_cpu(src->state);
160 dst->eth_index = FIELD_GET(NSP_ETH_PORT_INDEX, port);
162 dst->nbi = index / NSP_ETH_NBI_PORT_COUNT;
163 dst->base = index % NSP_ETH_NBI_PORT_COUNT;
164 dst->lanes = FIELD_GET(NSP_ETH_PORT_LANES, port);
166 dst->enabled = FIELD_GET(NSP_ETH_STATE_ENABLED, state);
167 dst->tx_enabled = FIELD_GET(NSP_ETH_STATE_TX_ENABLED, state);
168 dst->rx_enabled = FIELD_GET(NSP_ETH_STATE_RX_ENABLED, state);
170 rate = nfp_eth_rate2speed(FIELD_GET(NSP_ETH_STATE_RATE, state));
171 dst->speed = dst->lanes * rate;
173 dst->interface = FIELD_GET(NSP_ETH_STATE_INTERFACE, state);
174 dst->media = FIELD_GET(NSP_ETH_STATE_MEDIA, state);
176 nfp_eth_copy_mac_reverse(dst->mac_addr, src->mac_addr);
178 dst->label_port = FIELD_GET(NSP_ETH_PORT_PHYLABEL, port);
179 dst->label_subport = FIELD_GET(NSP_ETH_PORT_LABEL, port);
181 if (nfp_nsp_get_abi_ver_minor(nsp) < 17)
184 dst->override_changed = FIELD_GET(NSP_ETH_STATE_OVRD_CHNG, state);
185 dst->aneg = FIELD_GET(NSP_ETH_STATE_ANEG, state);
189 nfp_eth_calc_port_geometry(struct nfp_cpp *cpp, struct nfp_eth_table *table)
193 for (i = 0; i < table->count; i++) {
194 table->max_index = max(table->max_index, table->ports[i].index);
196 for (j = 0; j < table->count; j++) {
197 if (table->ports[i].label_port !=
198 table->ports[j].label_port)
200 table->ports[i].port_lanes += table->ports[j].lanes;
204 if (table->ports[i].label_subport ==
205 table->ports[j].label_subport)
207 "Port %d subport %d is a duplicate\n",
208 table->ports[i].label_port,
209 table->ports[i].label_subport);
211 table->ports[i].is_split = true;
217 nfp_eth_calc_port_type(struct nfp_cpp *cpp, struct nfp_eth_table_port *entry)
219 if (entry->interface == NFP_INTERFACE_NONE) {
220 entry->port_type = PORT_NONE;
224 if (entry->media == NFP_MEDIA_FIBRE)
225 entry->port_type = PORT_FIBRE;
227 entry->port_type = PORT_DA;
231 * nfp_eth_read_ports() - retrieve port information
232 * @cpp: NFP CPP handle
234 * Read the port information from the device. Returned structure should
235 * be freed with kfree() once no longer needed.
237 * Return: populated ETH table or NULL on error.
239 struct nfp_eth_table *nfp_eth_read_ports(struct nfp_cpp *cpp)
241 struct nfp_eth_table *ret;
244 nsp = nfp_nsp_open(cpp);
248 ret = __nfp_eth_read_ports(cpp, nsp);
254 struct nfp_eth_table *
255 __nfp_eth_read_ports(struct nfp_cpp *cpp, struct nfp_nsp *nsp)
257 union eth_table_entry *entries;
258 struct nfp_eth_table *table;
259 int i, j, ret, cnt = 0;
261 entries = kzalloc(NSP_ETH_TABLE_SIZE, GFP_KERNEL);
265 ret = nfp_nsp_read_eth_table(nsp, entries, NSP_ETH_TABLE_SIZE);
267 nfp_err(cpp, "reading port table failed %d\n", ret);
271 for (i = 0; i < NSP_ETH_MAX_COUNT; i++)
272 if (entries[i].port & NSP_ETH_PORT_LANES_MASK)
275 /* Some versions of flash will give us 0 instead of port count.
276 * For those that give a port count, verify it against the value
279 if (ret && ret != cnt) {
280 nfp_err(cpp, "table entry count reported (%d) does not match entries present (%d)\n",
285 table = kzalloc(sizeof(*table) +
286 sizeof(struct nfp_eth_table_port) * cnt, GFP_KERNEL);
291 for (i = 0, j = 0; i < NSP_ETH_MAX_COUNT; i++)
292 if (entries[i].port & NSP_ETH_PORT_LANES_MASK)
293 nfp_eth_port_translate(nsp, &entries[i], i,
296 nfp_eth_calc_port_geometry(cpp, table);
297 for (i = 0; i < table->count; i++)
298 nfp_eth_calc_port_type(cpp, &table->ports[i]);
309 struct nfp_nsp *nfp_eth_config_start(struct nfp_cpp *cpp, unsigned int idx)
311 union eth_table_entry *entries;
315 entries = kzalloc(NSP_ETH_TABLE_SIZE, GFP_KERNEL);
317 return ERR_PTR(-ENOMEM);
319 nsp = nfp_nsp_open(cpp);
325 ret = nfp_nsp_read_eth_table(nsp, entries, NSP_ETH_TABLE_SIZE);
327 nfp_err(cpp, "reading port table failed %d\n", ret);
331 if (!(entries[idx].port & NSP_ETH_PORT_LANES_MASK)) {
332 nfp_warn(cpp, "trying to set port state on disabled port %d\n",
337 nfp_nsp_config_set_state(nsp, entries, idx);
343 return ERR_PTR(-EIO);
346 void nfp_eth_config_cleanup_end(struct nfp_nsp *nsp)
348 union eth_table_entry *entries = nfp_nsp_config_entries(nsp);
350 nfp_nsp_config_set_modified(nsp, false);
351 nfp_nsp_config_clear_state(nsp);
357 * nfp_eth_config_commit_end() - perform recorded configuration changes
358 * @nsp: NFP NSP handle returned from nfp_eth_config_start()
360 * Perform the configuration which was requested with __nfp_eth_set_*()
361 * helpers and recorded in @nsp state. If device was already configured
362 * as requested or no __nfp_eth_set_*() operations were made no NSP command
366 * 0 - configuration successful;
367 * 1 - no changes were needed;
368 * -ERRNO - configuration failed.
370 int nfp_eth_config_commit_end(struct nfp_nsp *nsp)
372 union eth_table_entry *entries = nfp_nsp_config_entries(nsp);
375 if (nfp_nsp_config_modified(nsp)) {
376 ret = nfp_nsp_write_eth_table(nsp, entries, NSP_ETH_TABLE_SIZE);
377 ret = ret < 0 ? ret : 0;
380 nfp_eth_config_cleanup_end(nsp);
386 * nfp_eth_set_mod_enable() - set PHY module enable control bit
387 * @cpp: NFP CPP handle
388 * @idx: NFP chip-wide port index
389 * @enable: Desired state
391 * Enable or disable PHY module (this usually means setting the TX lanes
395 * 0 - configuration successful;
396 * 1 - no changes were needed;
397 * -ERRNO - configuration failed.
399 int nfp_eth_set_mod_enable(struct nfp_cpp *cpp, unsigned int idx, bool enable)
401 union eth_table_entry *entries;
405 nsp = nfp_eth_config_start(cpp, idx);
409 entries = nfp_nsp_config_entries(nsp);
411 /* Check if we are already in requested state */
412 reg = le64_to_cpu(entries[idx].state);
413 if (enable != FIELD_GET(NSP_ETH_CTRL_ENABLED, reg)) {
414 reg = le64_to_cpu(entries[idx].control);
415 reg &= ~NSP_ETH_CTRL_ENABLED;
416 reg |= FIELD_PREP(NSP_ETH_CTRL_ENABLED, enable);
417 entries[idx].control = cpu_to_le64(reg);
419 nfp_nsp_config_set_modified(nsp, true);
422 return nfp_eth_config_commit_end(nsp);
426 * nfp_eth_set_configured() - set PHY module configured control bit
427 * @cpp: NFP CPP handle
428 * @idx: NFP chip-wide port index
429 * @configed: Desired state
431 * Set the ifup/ifdown state on the PHY.
434 * 0 - configuration successful;
435 * 1 - no changes were needed;
436 * -ERRNO - configuration failed.
438 int nfp_eth_set_configured(struct nfp_cpp *cpp, unsigned int idx, bool configed)
440 union eth_table_entry *entries;
444 nsp = nfp_eth_config_start(cpp, idx);
448 /* Older ABI versions did support this feature, however this has only
449 * been reliable since ABI 20.
451 if (nfp_nsp_get_abi_ver_minor(nsp) < 20) {
452 nfp_eth_config_cleanup_end(nsp);
456 entries = nfp_nsp_config_entries(nsp);
458 /* Check if we are already in requested state */
459 reg = le64_to_cpu(entries[idx].state);
460 if (configed != FIELD_GET(NSP_ETH_STATE_CONFIGURED, reg)) {
461 reg = le64_to_cpu(entries[idx].control);
462 reg &= ~NSP_ETH_CTRL_CONFIGURED;
463 reg |= FIELD_PREP(NSP_ETH_CTRL_CONFIGURED, configed);
464 entries[idx].control = cpu_to_le64(reg);
466 nfp_nsp_config_set_modified(nsp, true);
469 return nfp_eth_config_commit_end(nsp);
472 /* Force inline, FIELD_* macroes require masks to be compilation-time known */
473 static __always_inline int
474 nfp_eth_set_bit_config(struct nfp_nsp *nsp, unsigned int raw_idx,
475 const u64 mask, unsigned int val, const u64 ctrl_bit)
477 union eth_table_entry *entries = nfp_nsp_config_entries(nsp);
478 unsigned int idx = nfp_nsp_config_idx(nsp);
481 /* Note: set features were added in ABI 0.14 but the error
482 * codes were initially not populated correctly.
484 if (nfp_nsp_get_abi_ver_minor(nsp) < 17) {
485 nfp_err(nfp_nsp_cpp(nsp),
486 "set operations not supported, please update flash\n");
490 /* Check if we are already in requested state */
491 reg = le64_to_cpu(entries[idx].raw[raw_idx]);
492 if (val == FIELD_GET(mask, reg))
496 reg |= FIELD_PREP(mask, val);
497 entries[idx].raw[raw_idx] = cpu_to_le64(reg);
499 entries[idx].control |= cpu_to_le64(ctrl_bit);
501 nfp_nsp_config_set_modified(nsp, true);
507 * __nfp_eth_set_aneg() - set PHY autonegotiation control bit
508 * @nsp: NFP NSP handle returned from nfp_eth_config_start()
509 * @mode: Desired autonegotiation mode
511 * Allow/disallow PHY module to advertise/perform autonegotiation.
512 * Will write to hwinfo overrides in the flash (persistent config).
514 * Return: 0 or -ERRNO.
516 int __nfp_eth_set_aneg(struct nfp_nsp *nsp, enum nfp_eth_aneg mode)
518 return nfp_eth_set_bit_config(nsp, NSP_ETH_RAW_STATE,
519 NSP_ETH_STATE_ANEG, mode,
520 NSP_ETH_CTRL_SET_ANEG);
524 * __nfp_eth_set_speed() - set interface speed/rate
525 * @nsp: NFP NSP handle returned from nfp_eth_config_start()
526 * @speed: Desired speed (per lane)
528 * Set lane speed. Provided @speed value should be subport speed divided
529 * by number of lanes this subport is spanning (i.e. 10000 for 40G, 25000 for
531 * Will write to hwinfo overrides in the flash (persistent config).
533 * Return: 0 or -ERRNO.
535 int __nfp_eth_set_speed(struct nfp_nsp *nsp, unsigned int speed)
537 enum nfp_eth_rate rate;
539 rate = nfp_eth_speed2rate(speed);
540 if (rate == RATE_INVALID) {
541 nfp_warn(nfp_nsp_cpp(nsp),
542 "could not find matching lane rate for speed %u\n",
547 return nfp_eth_set_bit_config(nsp, NSP_ETH_RAW_STATE,
548 NSP_ETH_STATE_RATE, rate,
549 NSP_ETH_CTRL_SET_RATE);
553 * __nfp_eth_set_split() - set interface lane split
554 * @nsp: NFP NSP handle returned from nfp_eth_config_start()
555 * @lanes: Desired lanes per port
557 * Set number of lanes in the port.
558 * Will write to hwinfo overrides in the flash (persistent config).
560 * Return: 0 or -ERRNO.
562 int __nfp_eth_set_split(struct nfp_nsp *nsp, unsigned int lanes)
564 return nfp_eth_set_bit_config(nsp, NSP_ETH_RAW_PORT, NSP_ETH_PORT_LANES,
565 lanes, NSP_ETH_CTRL_SET_LANES);