]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp.c
nfp: add extended error messages
[karo-tx-linux.git] / drivers / net / ethernet / netronome / nfp / nfpcore / nfp_nsp.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 /*
35  * nfp_nsp.c
36  * Author: Jakub Kicinski <jakub.kicinski@netronome.com>
37  *         Jason McMullan <jason.mcmullan@netronome.com>
38  */
39
40 #include <linux/bitfield.h>
41 #include <linux/delay.h>
42 #include <linux/firmware.h>
43 #include <linux/kernel.h>
44 #include <linux/kthread.h>
45 #include <linux/sizes.h>
46 #include <linux/slab.h>
47
48 #define NFP_SUBSYS "nfp_nsp"
49
50 #include "nfp.h"
51 #include "nfp_cpp.h"
52 #include "nfp_nsp.h"
53
54 /* Offsets relative to the CSR base */
55 #define NSP_STATUS              0x00
56 #define   NSP_STATUS_MAGIC      GENMASK_ULL(63, 48)
57 #define   NSP_STATUS_MAJOR      GENMASK_ULL(47, 44)
58 #define   NSP_STATUS_MINOR      GENMASK_ULL(43, 32)
59 #define   NSP_STATUS_CODE       GENMASK_ULL(31, 16)
60 #define   NSP_STATUS_RESULT     GENMASK_ULL(15, 8)
61 #define   NSP_STATUS_BUSY       BIT_ULL(0)
62
63 #define NSP_COMMAND             0x08
64 #define   NSP_COMMAND_OPTION    GENMASK_ULL(63, 32)
65 #define   NSP_COMMAND_CODE      GENMASK_ULL(31, 16)
66 #define   NSP_COMMAND_START     BIT_ULL(0)
67
68 /* CPP address to retrieve the data from */
69 #define NSP_BUFFER              0x10
70 #define   NSP_BUFFER_CPP        GENMASK_ULL(63, 40)
71 #define   NSP_BUFFER_PCIE       GENMASK_ULL(39, 38)
72 #define   NSP_BUFFER_ADDRESS    GENMASK_ULL(37, 0)
73
74 #define NSP_DFLT_BUFFER         0x18
75
76 #define NSP_DFLT_BUFFER_CONFIG  0x20
77 #define   NSP_DFLT_BUFFER_SIZE_MB       GENMASK_ULL(7, 0)
78
79 #define NSP_MAGIC               0xab10
80 #define NSP_MAJOR               0
81 #define NSP_MINOR               (__MAX_SPCODE - 1)
82
83 #define NSP_CODE_MAJOR          GENMASK(15, 12)
84 #define NSP_CODE_MINOR          GENMASK(11, 0)
85
86 enum nfp_nsp_cmd {
87         SPCODE_NOOP             = 0, /* No operation */
88         SPCODE_SOFT_RESET       = 1, /* Soft reset the NFP */
89         SPCODE_FW_DEFAULT       = 2, /* Load default (UNDI) FW */
90         SPCODE_PHY_INIT         = 3, /* Initialize the PHY */
91         SPCODE_MAC_INIT         = 4, /* Initialize the MAC */
92         SPCODE_PHY_RXADAPT      = 5, /* Re-run PHY RX Adaptation */
93         SPCODE_FW_LOAD          = 6, /* Load fw from buffer, len in option */
94         SPCODE_ETH_RESCAN       = 7, /* Rescan ETHs, write ETH_TABLE to buf */
95         SPCODE_ETH_CONTROL      = 8, /* Update media config from buffer */
96
97         __MAX_SPCODE,
98 };
99
100 static const struct {
101         int code;
102         const char *msg;
103 } nsp_errors[] = {
104         { 0, "success" } /* placeholder to avoid warnings */
105 };
106
107 struct nfp_nsp {
108         struct nfp_cpp *cpp;
109         struct nfp_resource *res;
110         struct {
111                 u16 major;
112                 u16 minor;
113         } ver;
114
115         /* Eth table config state */
116         bool modified;
117         unsigned int idx;
118         void *entries;
119 };
120
121 struct nfp_cpp *nfp_nsp_cpp(struct nfp_nsp *state)
122 {
123         return state->cpp;
124 }
125
126 bool nfp_nsp_config_modified(struct nfp_nsp *state)
127 {
128         return state->modified;
129 }
130
131 void nfp_nsp_config_set_modified(struct nfp_nsp *state, bool modified)
132 {
133         state->modified = modified;
134 }
135
136 void *nfp_nsp_config_entries(struct nfp_nsp *state)
137 {
138         return state->entries;
139 }
140
141 unsigned int nfp_nsp_config_idx(struct nfp_nsp *state)
142 {
143         return state->idx;
144 }
145
146 void
147 nfp_nsp_config_set_state(struct nfp_nsp *state, void *entries, unsigned int idx)
148 {
149         state->entries = entries;
150         state->idx = idx;
151 }
152
153 void nfp_nsp_config_clear_state(struct nfp_nsp *state)
154 {
155         state->entries = NULL;
156         state->idx = 0;
157 }
158
159 static void nfp_nsp_print_extended_error(struct nfp_nsp *state, u32 ret_val)
160 {
161         int i;
162
163         if (!ret_val)
164                 return;
165
166         for (i = 0; i < ARRAY_SIZE(nsp_errors); i++)
167                 if (ret_val == nsp_errors[i].code)
168                         nfp_err(state->cpp, "err msg: %s\n", nsp_errors[i].msg);
169 }
170
171 static int nfp_nsp_check(struct nfp_nsp *state)
172 {
173         struct nfp_cpp *cpp = state->cpp;
174         u64 nsp_status, reg;
175         u32 nsp_cpp;
176         int err;
177
178         nsp_cpp = nfp_resource_cpp_id(state->res);
179         nsp_status = nfp_resource_address(state->res) + NSP_STATUS;
180
181         err = nfp_cpp_readq(cpp, nsp_cpp, nsp_status, &reg);
182         if (err < 0)
183                 return err;
184
185         if (FIELD_GET(NSP_STATUS_MAGIC, reg) != NSP_MAGIC) {
186                 nfp_err(cpp, "Cannot detect NFP Service Processor\n");
187                 return -ENODEV;
188         }
189
190         state->ver.major = FIELD_GET(NSP_STATUS_MAJOR, reg);
191         state->ver.minor = FIELD_GET(NSP_STATUS_MINOR, reg);
192
193         if (state->ver.major != NSP_MAJOR || state->ver.minor < NSP_MINOR) {
194                 nfp_err(cpp, "Unsupported ABI %hu.%hu\n",
195                         state->ver.major, state->ver.minor);
196                 return -EINVAL;
197         }
198
199         if (reg & NSP_STATUS_BUSY) {
200                 nfp_err(cpp, "Service processor busy!\n");
201                 return -EBUSY;
202         }
203
204         return 0;
205 }
206
207 /**
208  * nfp_nsp_open() - Prepare for communication and lock the NSP resource.
209  * @cpp:        NFP CPP Handle
210  */
211 struct nfp_nsp *nfp_nsp_open(struct nfp_cpp *cpp)
212 {
213         struct nfp_resource *res;
214         struct nfp_nsp *state;
215         int err;
216
217         res = nfp_resource_acquire(cpp, NFP_RESOURCE_NSP);
218         if (IS_ERR(res))
219                 return (void *)res;
220
221         state = kzalloc(sizeof(*state), GFP_KERNEL);
222         if (!state) {
223                 nfp_resource_release(res);
224                 return ERR_PTR(-ENOMEM);
225         }
226         state->cpp = cpp;
227         state->res = res;
228
229         err = nfp_nsp_check(state);
230         if (err) {
231                 nfp_nsp_close(state);
232                 return ERR_PTR(err);
233         }
234
235         return state;
236 }
237
238 /**
239  * nfp_nsp_close() - Clean up and unlock the NSP resource.
240  * @state:      NFP SP state
241  */
242 void nfp_nsp_close(struct nfp_nsp *state)
243 {
244         nfp_resource_release(state->res);
245         kfree(state);
246 }
247
248 u16 nfp_nsp_get_abi_ver_major(struct nfp_nsp *state)
249 {
250         return state->ver.major;
251 }
252
253 u16 nfp_nsp_get_abi_ver_minor(struct nfp_nsp *state)
254 {
255         return state->ver.minor;
256 }
257
258 static int
259 nfp_nsp_wait_reg(struct nfp_cpp *cpp, u64 *reg,
260                  u32 nsp_cpp, u64 addr, u64 mask, u64 val)
261 {
262         const unsigned long wait_until = jiffies + 30 * HZ;
263         int err;
264
265         for (;;) {
266                 const unsigned long start_time = jiffies;
267
268                 err = nfp_cpp_readq(cpp, nsp_cpp, addr, reg);
269                 if (err < 0)
270                         return err;
271
272                 if ((*reg & mask) == val)
273                         return 0;
274
275                 if (msleep_interruptible(25))
276                         return -ERESTARTSYS;
277
278                 if (time_after(start_time, wait_until))
279                         return -ETIMEDOUT;
280         }
281 }
282
283 /**
284  * nfp_nsp_command() - Execute a command on the NFP Service Processor
285  * @state:      NFP SP state
286  * @code:       NFP SP Command Code
287  * @option:     NFP SP Command Argument
288  * @buff_cpp:   NFP SP Buffer CPP Address info
289  * @buff_addr:  NFP SP Buffer Host address
290  *
291  * Return: 0 for success with no result
292  *
293  *       positive value for NSP completion with a result code
294  *
295  *      -EAGAIN if the NSP is not yet present
296  *      -ENODEV if the NSP is not a supported model
297  *      -EBUSY if the NSP is stuck
298  *      -EINTR if interrupted while waiting for completion
299  *      -ETIMEDOUT if the NSP took longer than 30 seconds to complete
300  */
301 static int nfp_nsp_command(struct nfp_nsp *state, u16 code, u32 option,
302                            u32 buff_cpp, u64 buff_addr)
303 {
304         u64 reg, ret_val, nsp_base, nsp_buffer, nsp_status, nsp_command;
305         struct nfp_cpp *cpp = state->cpp;
306         u32 nsp_cpp;
307         int err;
308
309         nsp_cpp = nfp_resource_cpp_id(state->res);
310         nsp_base = nfp_resource_address(state->res);
311         nsp_status = nsp_base + NSP_STATUS;
312         nsp_command = nsp_base + NSP_COMMAND;
313         nsp_buffer = nsp_base + NSP_BUFFER;
314
315         err = nfp_nsp_check(state);
316         if (err)
317                 return err;
318
319         if (!FIELD_FIT(NSP_BUFFER_CPP, buff_cpp >> 8) ||
320             !FIELD_FIT(NSP_BUFFER_ADDRESS, buff_addr)) {
321                 nfp_err(cpp, "Host buffer out of reach %08x %016llx\n",
322                         buff_cpp, buff_addr);
323                 return -EINVAL;
324         }
325
326         err = nfp_cpp_writeq(cpp, nsp_cpp, nsp_buffer,
327                              FIELD_PREP(NSP_BUFFER_CPP, buff_cpp >> 8) |
328                              FIELD_PREP(NSP_BUFFER_ADDRESS, buff_addr));
329         if (err < 0)
330                 return err;
331
332         err = nfp_cpp_writeq(cpp, nsp_cpp, nsp_command,
333                              FIELD_PREP(NSP_COMMAND_OPTION, option) |
334                              FIELD_PREP(NSP_COMMAND_CODE, code) |
335                              FIELD_PREP(NSP_COMMAND_START, 1));
336         if (err < 0)
337                 return err;
338
339         /* Wait for NSP_COMMAND_START to go to 0 */
340         err = nfp_nsp_wait_reg(cpp, &reg,
341                                nsp_cpp, nsp_command, NSP_COMMAND_START, 0);
342         if (err) {
343                 nfp_err(cpp, "Error %d waiting for code 0x%04x to start\n",
344                         err, code);
345                 return err;
346         }
347
348         /* Wait for NSP_STATUS_BUSY to go to 0 */
349         err = nfp_nsp_wait_reg(cpp, &reg,
350                                nsp_cpp, nsp_status, NSP_STATUS_BUSY, 0);
351         if (err) {
352                 nfp_err(cpp, "Error %d waiting for code 0x%04x to complete\n",
353                         err, code);
354                 return err;
355         }
356
357         err = nfp_cpp_readq(cpp, nsp_cpp, nsp_command, &ret_val);
358         if (err < 0)
359                 return err;
360         ret_val = FIELD_GET(NSP_COMMAND_OPTION, ret_val);
361
362         err = FIELD_GET(NSP_STATUS_RESULT, reg);
363         if (err) {
364                 nfp_warn(cpp, "Result (error) code set: %d (%d) command: %d\n",
365                          -err, (int)ret_val, code);
366                 nfp_nsp_print_extended_error(state, ret_val);
367                 return -err;
368         }
369
370         return ret_val;
371 }
372
373 static int nfp_nsp_command_buf(struct nfp_nsp *nsp, u16 code, u32 option,
374                                const void *in_buf, unsigned int in_size,
375                                void *out_buf, unsigned int out_size)
376 {
377         struct nfp_cpp *cpp = nsp->cpp;
378         unsigned int max_size;
379         u64 reg, cpp_buf;
380         int ret, err;
381         u32 cpp_id;
382
383         if (nsp->ver.minor < 13) {
384                 nfp_err(cpp, "NSP: Code 0x%04x with buffer not supported (ABI %hu.%hu)\n",
385                         code, nsp->ver.major, nsp->ver.minor);
386                 return -EOPNOTSUPP;
387         }
388
389         err = nfp_cpp_readq(cpp, nfp_resource_cpp_id(nsp->res),
390                             nfp_resource_address(nsp->res) +
391                             NSP_DFLT_BUFFER_CONFIG,
392                             &reg);
393         if (err < 0)
394                 return err;
395
396         max_size = max(in_size, out_size);
397         if (FIELD_GET(NSP_DFLT_BUFFER_SIZE_MB, reg) * SZ_1M < max_size) {
398                 nfp_err(cpp, "NSP: default buffer too small for command 0x%04x (%llu < %u)\n",
399                         code, FIELD_GET(NSP_DFLT_BUFFER_SIZE_MB, reg) * SZ_1M,
400                         max_size);
401                 return -EINVAL;
402         }
403
404         err = nfp_cpp_readq(cpp, nfp_resource_cpp_id(nsp->res),
405                             nfp_resource_address(nsp->res) +
406                             NSP_DFLT_BUFFER,
407                             &reg);
408         if (err < 0)
409                 return err;
410
411         cpp_id = FIELD_GET(NSP_BUFFER_CPP, reg) << 8;
412         cpp_buf = FIELD_GET(NSP_BUFFER_ADDRESS, reg);
413
414         if (in_buf && in_size) {
415                 err = nfp_cpp_write(cpp, cpp_id, cpp_buf, in_buf, in_size);
416                 if (err < 0)
417                         return err;
418         }
419
420         ret = nfp_nsp_command(nsp, code, option, cpp_id, cpp_buf);
421         if (ret < 0)
422                 return ret;
423
424         if (out_buf && out_size) {
425                 err = nfp_cpp_read(cpp, cpp_id, cpp_buf, out_buf, out_size);
426                 if (err < 0)
427                         return err;
428         }
429
430         return ret;
431 }
432
433 int nfp_nsp_wait(struct nfp_nsp *state)
434 {
435         const unsigned long wait_until = jiffies + 30 * HZ;
436         int err;
437
438         nfp_dbg(state->cpp, "Waiting for NSP to respond (30 sec max).\n");
439
440         for (;;) {
441                 const unsigned long start_time = jiffies;
442
443                 err = nfp_nsp_command(state, SPCODE_NOOP, 0, 0, 0);
444                 if (err != -EAGAIN)
445                         break;
446
447                 if (msleep_interruptible(25)) {
448                         err = -ERESTARTSYS;
449                         break;
450                 }
451
452                 if (time_after(start_time, wait_until)) {
453                         err = -ETIMEDOUT;
454                         break;
455                 }
456         }
457         if (err)
458                 nfp_err(state->cpp, "NSP failed to respond %d\n", err);
459
460         return err;
461 }
462
463 int nfp_nsp_device_soft_reset(struct nfp_nsp *state)
464 {
465         int err;
466
467         err = nfp_nsp_command(state, SPCODE_SOFT_RESET, 0, 0, 0);
468
469         nfp_nffw_cache_flush(state->cpp);
470
471         return err;
472 }
473
474 int nfp_nsp_load_fw(struct nfp_nsp *state, const struct firmware *fw)
475 {
476         return nfp_nsp_command_buf(state, SPCODE_FW_LOAD, fw->size, fw->data,
477                                    fw->size, NULL, 0);
478 }
479
480 int nfp_nsp_read_eth_table(struct nfp_nsp *state, void *buf, unsigned int size)
481 {
482         return nfp_nsp_command_buf(state, SPCODE_ETH_RESCAN, size, NULL, 0,
483                                    buf, size);
484 }
485
486 int nfp_nsp_write_eth_table(struct nfp_nsp *state,
487                             const void *buf, unsigned int size)
488 {
489         return nfp_nsp_command_buf(state, SPCODE_ETH_CONTROL, size, buf, size,
490                                    NULL, 0);
491 }