GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / net / ethernet / netronome / nfp / nfpcore / nfp_nsp_eth.c
1 /*
2  * Copyright (C) 2015-2017 Netronome Systems, Inc.
3  *
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.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
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.
23  *
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
31  * SOFTWARE.
32  */
33
34 /* Authors: David Brunecz <david.brunecz@netronome.com>
35  *          Jakub Kicinski <jakub.kicinski@netronome.com>
36  *          Jason Mcmullan <jason.mcmullan@netronome.com>
37  */
38
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>
44
45 #include "nfp.h"
46 #include "nfp_nsp.h"
47 #include "nfp6000/nfp6000.h"
48
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))
53
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)
58
59 #define NSP_ETH_PORT_LANES_MASK         cpu_to_le64(NSP_ETH_PORT_LANES)
60
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)
70
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)
78
79 enum nfp_eth_raw {
80         NSP_ETH_RAW_PORT = 0,
81         NSP_ETH_RAW_STATE,
82         NSP_ETH_RAW_MAC,
83         NSP_ETH_RAW_CONTROL,
84
85         NSP_ETH_NUM_RAW
86 };
87
88 enum nfp_eth_rate {
89         RATE_INVALID = 0,
90         RATE_10M,
91         RATE_100M,
92         RATE_1G,
93         RATE_10G,
94         RATE_25G,
95 };
96
97 union eth_table_entry {
98         struct {
99                 __le64 port;
100                 __le64 state;
101                 u8 mac_addr[6];
102                 u8 resv[2];
103                 __le64 control;
104         };
105         __le64 raw[NSP_ETH_NUM_RAW];
106 };
107
108 static const struct {
109         enum nfp_eth_rate rate;
110         unsigned int speed;
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, },
118 };
119
120 static unsigned int nfp_eth_rate2speed(enum nfp_eth_rate rate)
121 {
122         int i;
123
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;
127
128         return 0;
129 }
130
131 static unsigned int nfp_eth_speed2rate(unsigned int speed)
132 {
133         int i;
134
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;
138
139         return RATE_INVALID;
140 }
141
142 static void nfp_eth_copy_mac_reverse(u8 *dst, const u8 *src)
143 {
144         int i;
145
146         for (i = 0; i < ETH_ALEN; i++)
147                 dst[ETH_ALEN - i - 1] = src[i];
148 }
149
150 static void
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)
153 {
154         unsigned int rate;
155         u64 port, state;
156
157         port = le64_to_cpu(src->port);
158         state = le64_to_cpu(src->state);
159
160         dst->eth_index = FIELD_GET(NSP_ETH_PORT_INDEX, port);
161         dst->index = index;
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);
165
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);
169
170         rate = nfp_eth_rate2speed(FIELD_GET(NSP_ETH_STATE_RATE, state));
171         dst->speed = dst->lanes * rate;
172
173         dst->interface = FIELD_GET(NSP_ETH_STATE_INTERFACE, state);
174         dst->media = FIELD_GET(NSP_ETH_STATE_MEDIA, state);
175
176         nfp_eth_copy_mac_reverse(dst->mac_addr, src->mac_addr);
177
178         dst->label_port = FIELD_GET(NSP_ETH_PORT_PHYLABEL, port);
179         dst->label_subport = FIELD_GET(NSP_ETH_PORT_LABEL, port);
180
181         if (nfp_nsp_get_abi_ver_minor(nsp) < 17)
182                 return;
183
184         dst->override_changed = FIELD_GET(NSP_ETH_STATE_OVRD_CHNG, state);
185         dst->aneg = FIELD_GET(NSP_ETH_STATE_ANEG, state);
186 }
187
188 static void
189 nfp_eth_calc_port_geometry(struct nfp_cpp *cpp, struct nfp_eth_table *table)
190 {
191         unsigned int i, j;
192
193         for (i = 0; i < table->count; i++) {
194                 table->max_index = max(table->max_index, table->ports[i].index);
195
196                 for (j = 0; j < table->count; j++) {
197                         if (table->ports[i].label_port !=
198                             table->ports[j].label_port)
199                                 continue;
200                         table->ports[i].port_lanes += table->ports[j].lanes;
201
202                         if (i == j)
203                                 continue;
204                         if (table->ports[i].label_subport ==
205                             table->ports[j].label_subport)
206                                 nfp_warn(cpp,
207                                          "Port %d subport %d is a duplicate\n",
208                                          table->ports[i].label_port,
209                                          table->ports[i].label_subport);
210
211                         table->ports[i].is_split = true;
212                 }
213         }
214 }
215
216 static void
217 nfp_eth_calc_port_type(struct nfp_cpp *cpp, struct nfp_eth_table_port *entry)
218 {
219         if (entry->interface == NFP_INTERFACE_NONE) {
220                 entry->port_type = PORT_NONE;
221                 return;
222         }
223
224         if (entry->media == NFP_MEDIA_FIBRE)
225                 entry->port_type = PORT_FIBRE;
226         else
227                 entry->port_type = PORT_DA;
228 }
229
230 /**
231  * nfp_eth_read_ports() - retrieve port information
232  * @cpp:        NFP CPP handle
233  *
234  * Read the port information from the device.  Returned structure should
235  * be freed with kfree() once no longer needed.
236  *
237  * Return: populated ETH table or NULL on error.
238  */
239 struct nfp_eth_table *nfp_eth_read_ports(struct nfp_cpp *cpp)
240 {
241         struct nfp_eth_table *ret;
242         struct nfp_nsp *nsp;
243
244         nsp = nfp_nsp_open(cpp);
245         if (IS_ERR(nsp))
246                 return NULL;
247
248         ret = __nfp_eth_read_ports(cpp, nsp);
249         nfp_nsp_close(nsp);
250
251         return ret;
252 }
253
254 struct nfp_eth_table *
255 __nfp_eth_read_ports(struct nfp_cpp *cpp, struct nfp_nsp *nsp)
256 {
257         union eth_table_entry *entries;
258         struct nfp_eth_table *table;
259         int i, j, ret, cnt = 0;
260
261         entries = kzalloc(NSP_ETH_TABLE_SIZE, GFP_KERNEL);
262         if (!entries)
263                 return NULL;
264
265         ret = nfp_nsp_read_eth_table(nsp, entries, NSP_ETH_TABLE_SIZE);
266         if (ret < 0) {
267                 nfp_err(cpp, "reading port table failed %d\n", ret);
268                 goto err;
269         }
270
271         for (i = 0; i < NSP_ETH_MAX_COUNT; i++)
272                 if (entries[i].port & NSP_ETH_PORT_LANES_MASK)
273                         cnt++;
274
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
277          * calculated above.
278          */
279         if (ret && ret != cnt) {
280                 nfp_err(cpp, "table entry count reported (%d) does not match entries present (%d)\n",
281                         ret, cnt);
282                 goto err;
283         }
284
285         table = kzalloc(sizeof(*table) +
286                         sizeof(struct nfp_eth_table_port) * cnt, GFP_KERNEL);
287         if (!table)
288                 goto err;
289
290         table->count = cnt;
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,
294                                                &table->ports[j++]);
295
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]);
299
300         kfree(entries);
301
302         return table;
303
304 err:
305         kfree(entries);
306         return NULL;
307 }
308
309 struct nfp_nsp *nfp_eth_config_start(struct nfp_cpp *cpp, unsigned int idx)
310 {
311         union eth_table_entry *entries;
312         struct nfp_nsp *nsp;
313         int ret;
314
315         entries = kzalloc(NSP_ETH_TABLE_SIZE, GFP_KERNEL);
316         if (!entries)
317                 return ERR_PTR(-ENOMEM);
318
319         nsp = nfp_nsp_open(cpp);
320         if (IS_ERR(nsp)) {
321                 kfree(entries);
322                 return nsp;
323         }
324
325         ret = nfp_nsp_read_eth_table(nsp, entries, NSP_ETH_TABLE_SIZE);
326         if (ret < 0) {
327                 nfp_err(cpp, "reading port table failed %d\n", ret);
328                 goto err;
329         }
330
331         if (!(entries[idx].port & NSP_ETH_PORT_LANES_MASK)) {
332                 nfp_warn(cpp, "trying to set port state on disabled port %d\n",
333                          idx);
334                 goto err;
335         }
336
337         nfp_nsp_config_set_state(nsp, entries, idx);
338         return nsp;
339
340 err:
341         nfp_nsp_close(nsp);
342         kfree(entries);
343         return ERR_PTR(-EIO);
344 }
345
346 void nfp_eth_config_cleanup_end(struct nfp_nsp *nsp)
347 {
348         union eth_table_entry *entries = nfp_nsp_config_entries(nsp);
349
350         nfp_nsp_config_set_modified(nsp, false);
351         nfp_nsp_config_clear_state(nsp);
352         nfp_nsp_close(nsp);
353         kfree(entries);
354 }
355
356 /**
357  * nfp_eth_config_commit_end() - perform recorded configuration changes
358  * @nsp:        NFP NSP handle returned from nfp_eth_config_start()
359  *
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
363  * will be performed.
364  *
365  * Return:
366  * 0 - configuration successful;
367  * 1 - no changes were needed;
368  * -ERRNO - configuration failed.
369  */
370 int nfp_eth_config_commit_end(struct nfp_nsp *nsp)
371 {
372         union eth_table_entry *entries = nfp_nsp_config_entries(nsp);
373         int ret = 1;
374
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;
378         }
379
380         nfp_eth_config_cleanup_end(nsp);
381
382         return ret;
383 }
384
385 /**
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
390  *
391  * Enable or disable PHY module (this usually means setting the TX lanes
392  * disable bits).
393  *
394  * Return:
395  * 0 - configuration successful;
396  * 1 - no changes were needed;
397  * -ERRNO - configuration failed.
398  */
399 int nfp_eth_set_mod_enable(struct nfp_cpp *cpp, unsigned int idx, bool enable)
400 {
401         union eth_table_entry *entries;
402         struct nfp_nsp *nsp;
403         u64 reg;
404
405         nsp = nfp_eth_config_start(cpp, idx);
406         if (IS_ERR(nsp))
407                 return PTR_ERR(nsp);
408
409         entries = nfp_nsp_config_entries(nsp);
410
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);
418
419                 nfp_nsp_config_set_modified(nsp, true);
420         }
421
422         return nfp_eth_config_commit_end(nsp);
423 }
424
425 /**
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
430  *
431  * Set the ifup/ifdown state on the PHY.
432  *
433  * Return:
434  * 0 - configuration successful;
435  * 1 - no changes were needed;
436  * -ERRNO - configuration failed.
437  */
438 int nfp_eth_set_configured(struct nfp_cpp *cpp, unsigned int idx, bool configed)
439 {
440         union eth_table_entry *entries;
441         struct nfp_nsp *nsp;
442         u64 reg;
443
444         nsp = nfp_eth_config_start(cpp, idx);
445         if (IS_ERR(nsp))
446                 return PTR_ERR(nsp);
447
448         /* Older ABI versions did support this feature, however this has only
449          * been reliable since ABI 20.
450          */
451         if (nfp_nsp_get_abi_ver_minor(nsp) < 20) {
452                 nfp_eth_config_cleanup_end(nsp);
453                 return -EOPNOTSUPP;
454         }
455
456         entries = nfp_nsp_config_entries(nsp);
457
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);
465
466                 nfp_nsp_config_set_modified(nsp, true);
467         }
468
469         return nfp_eth_config_commit_end(nsp);
470 }
471
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)
476 {
477         union eth_table_entry *entries = nfp_nsp_config_entries(nsp);
478         unsigned int idx = nfp_nsp_config_idx(nsp);
479         u64 reg;
480
481         /* Note: set features were added in ABI 0.14 but the error
482          *       codes were initially not populated correctly.
483          */
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");
487                 return -EOPNOTSUPP;
488         }
489
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))
493                 return 0;
494
495         reg &= ~mask;
496         reg |= FIELD_PREP(mask, val);
497         entries[idx].raw[raw_idx] = cpu_to_le64(reg);
498
499         entries[idx].control |= cpu_to_le64(ctrl_bit);
500
501         nfp_nsp_config_set_modified(nsp, true);
502
503         return 0;
504 }
505
506 /**
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
510  *
511  * Allow/disallow PHY module to advertise/perform autonegotiation.
512  * Will write to hwinfo overrides in the flash (persistent config).
513  *
514  * Return: 0 or -ERRNO.
515  */
516 int __nfp_eth_set_aneg(struct nfp_nsp *nsp, enum nfp_eth_aneg mode)
517 {
518         return nfp_eth_set_bit_config(nsp, NSP_ETH_RAW_STATE,
519                                       NSP_ETH_STATE_ANEG, mode,
520                                       NSP_ETH_CTRL_SET_ANEG);
521 }
522
523 /**
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)
527  *
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
530  * 50G, etc.)
531  * Will write to hwinfo overrides in the flash (persistent config).
532  *
533  * Return: 0 or -ERRNO.
534  */
535 int __nfp_eth_set_speed(struct nfp_nsp *nsp, unsigned int speed)
536 {
537         enum nfp_eth_rate rate;
538
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",
543                          speed);
544                 return -EINVAL;
545         }
546
547         return nfp_eth_set_bit_config(nsp, NSP_ETH_RAW_STATE,
548                                       NSP_ETH_STATE_RATE, rate,
549                                       NSP_ETH_CTRL_SET_RATE);
550 }
551
552 /**
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
556  *
557  * Set number of lanes in the port.
558  * Will write to hwinfo overrides in the flash (persistent config).
559  *
560  * Return: 0 or -ERRNO.
561  */
562 int __nfp_eth_set_split(struct nfp_nsp *nsp, unsigned int lanes)
563 {
564         return nfp_eth_set_bit_config(nsp, NSP_ETH_RAW_PORT, NSP_ETH_PORT_LANES,
565                                       lanes, NSP_ETH_CTRL_SET_LANES);
566 }