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
36 * Author: Jakub Kicinski <jakub.kicinski@netronome.com>
37 * Jason McMullan <jason.mcmullan@netronome.com>
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>
48 #define NFP_SUBSYS "nfp_nsp"
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)
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)
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_ADDRESS GENMASK_ULL(39, 0)
73 #define NSP_DFLT_BUFFER 0x18
74 #define NSP_DFLT_BUFFER_CPP GENMASK_ULL(63, 40)
75 #define NSP_DFLT_BUFFER_ADDRESS GENMASK_ULL(39, 0)
77 #define NSP_DFLT_BUFFER_CONFIG 0x20
78 #define NSP_DFLT_BUFFER_SIZE_MB GENMASK_ULL(7, 0)
80 #define NSP_MAGIC 0xab10
84 #define NSP_CODE_MAJOR GENMASK(15, 12)
85 #define NSP_CODE_MINOR GENMASK(11, 0)
88 SPCODE_NOOP = 0, /* No operation */
89 SPCODE_SOFT_RESET = 1, /* Soft reset the NFP */
90 SPCODE_FW_DEFAULT = 2, /* Load default (UNDI) FW */
91 SPCODE_PHY_INIT = 3, /* Initialize the PHY */
92 SPCODE_MAC_INIT = 4, /* Initialize the MAC */
93 SPCODE_PHY_RXADAPT = 5, /* Re-run PHY RX Adaptation */
94 SPCODE_FW_LOAD = 6, /* Load fw from buffer, len in option */
95 SPCODE_ETH_RESCAN = 7, /* Rescan ETHs, write ETH_TABLE to buf */
96 SPCODE_ETH_CONTROL = 8, /* Update media config from buffer */
97 SPCODE_NSP_SENSORS = 12, /* Read NSP sensor(s) */
98 SPCODE_NSP_IDENTIFY = 13, /* Read NSP version */
101 static const struct {
105 { 6010, "could not map to phy for port" },
106 { 6011, "not an allowed rate/lanes for port" },
107 { 6012, "not an allowed rate/lanes for port" },
108 { 6013, "high/low error, change other port first" },
109 { 6014, "config not found in flash" },
114 struct nfp_resource *res;
120 /* Eth table config state */
126 struct nfp_cpp *nfp_nsp_cpp(struct nfp_nsp *state)
131 bool nfp_nsp_config_modified(struct nfp_nsp *state)
133 return state->modified;
136 void nfp_nsp_config_set_modified(struct nfp_nsp *state, bool modified)
138 state->modified = modified;
141 void *nfp_nsp_config_entries(struct nfp_nsp *state)
143 return state->entries;
146 unsigned int nfp_nsp_config_idx(struct nfp_nsp *state)
152 nfp_nsp_config_set_state(struct nfp_nsp *state, void *entries, unsigned int idx)
154 state->entries = entries;
158 void nfp_nsp_config_clear_state(struct nfp_nsp *state)
160 state->entries = NULL;
164 static void nfp_nsp_print_extended_error(struct nfp_nsp *state, u32 ret_val)
171 for (i = 0; i < ARRAY_SIZE(nsp_errors); i++)
172 if (ret_val == nsp_errors[i].code)
173 nfp_err(state->cpp, "err msg: %s\n", nsp_errors[i].msg);
176 static int nfp_nsp_check(struct nfp_nsp *state)
178 struct nfp_cpp *cpp = state->cpp;
183 nsp_cpp = nfp_resource_cpp_id(state->res);
184 nsp_status = nfp_resource_address(state->res) + NSP_STATUS;
186 err = nfp_cpp_readq(cpp, nsp_cpp, nsp_status, ®);
190 if (FIELD_GET(NSP_STATUS_MAGIC, reg) != NSP_MAGIC) {
191 nfp_err(cpp, "Cannot detect NFP Service Processor\n");
195 state->ver.major = FIELD_GET(NSP_STATUS_MAJOR, reg);
196 state->ver.minor = FIELD_GET(NSP_STATUS_MINOR, reg);
198 if (state->ver.major != NSP_MAJOR || state->ver.minor < NSP_MINOR) {
199 nfp_err(cpp, "Unsupported ABI %hu.%hu\n",
200 state->ver.major, state->ver.minor);
204 if (reg & NSP_STATUS_BUSY) {
205 nfp_err(cpp, "Service processor busy!\n");
213 * nfp_nsp_open() - Prepare for communication and lock the NSP resource.
214 * @cpp: NFP CPP Handle
216 struct nfp_nsp *nfp_nsp_open(struct nfp_cpp *cpp)
218 struct nfp_resource *res;
219 struct nfp_nsp *state;
222 res = nfp_resource_acquire(cpp, NFP_RESOURCE_NSP);
226 state = kzalloc(sizeof(*state), GFP_KERNEL);
228 nfp_resource_release(res);
229 return ERR_PTR(-ENOMEM);
234 err = nfp_nsp_check(state);
236 nfp_nsp_close(state);
244 * nfp_nsp_close() - Clean up and unlock the NSP resource.
245 * @state: NFP SP state
247 void nfp_nsp_close(struct nfp_nsp *state)
249 nfp_resource_release(state->res);
253 u16 nfp_nsp_get_abi_ver_major(struct nfp_nsp *state)
255 return state->ver.major;
258 u16 nfp_nsp_get_abi_ver_minor(struct nfp_nsp *state)
260 return state->ver.minor;
264 nfp_nsp_wait_reg(struct nfp_cpp *cpp, u64 *reg,
265 u32 nsp_cpp, u64 addr, u64 mask, u64 val)
267 const unsigned long wait_until = jiffies + 30 * HZ;
271 const unsigned long start_time = jiffies;
273 err = nfp_cpp_readq(cpp, nsp_cpp, addr, reg);
277 if ((*reg & mask) == val)
282 if (time_after(start_time, wait_until))
288 * nfp_nsp_command() - Execute a command on the NFP Service Processor
289 * @state: NFP SP state
290 * @code: NFP SP Command Code
291 * @option: NFP SP Command Argument
292 * @buff_cpp: NFP SP Buffer CPP Address info
293 * @buff_addr: NFP SP Buffer Host address
295 * Return: 0 for success with no result
297 * positive value for NSP completion with a result code
299 * -EAGAIN if the NSP is not yet present
300 * -ENODEV if the NSP is not a supported model
301 * -EBUSY if the NSP is stuck
302 * -EINTR if interrupted while waiting for completion
303 * -ETIMEDOUT if the NSP took longer than 30 seconds to complete
305 static int nfp_nsp_command(struct nfp_nsp *state, u16 code, u32 option,
306 u32 buff_cpp, u64 buff_addr)
308 u64 reg, ret_val, nsp_base, nsp_buffer, nsp_status, nsp_command;
309 struct nfp_cpp *cpp = state->cpp;
313 nsp_cpp = nfp_resource_cpp_id(state->res);
314 nsp_base = nfp_resource_address(state->res);
315 nsp_status = nsp_base + NSP_STATUS;
316 nsp_command = nsp_base + NSP_COMMAND;
317 nsp_buffer = nsp_base + NSP_BUFFER;
319 err = nfp_nsp_check(state);
323 if (!FIELD_FIT(NSP_BUFFER_CPP, buff_cpp >> 8) ||
324 !FIELD_FIT(NSP_BUFFER_ADDRESS, buff_addr)) {
325 nfp_err(cpp, "Host buffer out of reach %08x %016llx\n",
326 buff_cpp, buff_addr);
330 err = nfp_cpp_writeq(cpp, nsp_cpp, nsp_buffer,
331 FIELD_PREP(NSP_BUFFER_CPP, buff_cpp >> 8) |
332 FIELD_PREP(NSP_BUFFER_ADDRESS, buff_addr));
336 err = nfp_cpp_writeq(cpp, nsp_cpp, nsp_command,
337 FIELD_PREP(NSP_COMMAND_OPTION, option) |
338 FIELD_PREP(NSP_COMMAND_CODE, code) |
339 FIELD_PREP(NSP_COMMAND_START, 1));
343 /* Wait for NSP_COMMAND_START to go to 0 */
344 err = nfp_nsp_wait_reg(cpp, ®,
345 nsp_cpp, nsp_command, NSP_COMMAND_START, 0);
347 nfp_err(cpp, "Error %d waiting for code 0x%04x to start\n",
352 /* Wait for NSP_STATUS_BUSY to go to 0 */
353 err = nfp_nsp_wait_reg(cpp, ®,
354 nsp_cpp, nsp_status, NSP_STATUS_BUSY, 0);
356 nfp_err(cpp, "Error %d waiting for code 0x%04x to complete\n",
361 err = nfp_cpp_readq(cpp, nsp_cpp, nsp_command, &ret_val);
364 ret_val = FIELD_GET(NSP_COMMAND_OPTION, ret_val);
366 err = FIELD_GET(NSP_STATUS_RESULT, reg);
368 nfp_warn(cpp, "Result (error) code set: %d (%d) command: %d\n",
369 -err, (int)ret_val, code);
370 nfp_nsp_print_extended_error(state, ret_val);
377 static int nfp_nsp_command_buf(struct nfp_nsp *nsp, u16 code, u32 option,
378 const void *in_buf, unsigned int in_size,
379 void *out_buf, unsigned int out_size)
381 struct nfp_cpp *cpp = nsp->cpp;
382 unsigned int max_size;
387 if (nsp->ver.minor < 13) {
388 nfp_err(cpp, "NSP: Code 0x%04x with buffer not supported (ABI %hu.%hu)\n",
389 code, nsp->ver.major, nsp->ver.minor);
393 err = nfp_cpp_readq(cpp, nfp_resource_cpp_id(nsp->res),
394 nfp_resource_address(nsp->res) +
395 NSP_DFLT_BUFFER_CONFIG,
400 max_size = max(in_size, out_size);
401 if (FIELD_GET(NSP_DFLT_BUFFER_SIZE_MB, reg) * SZ_1M < max_size) {
402 nfp_err(cpp, "NSP: default buffer too small for command 0x%04x (%llu < %u)\n",
403 code, FIELD_GET(NSP_DFLT_BUFFER_SIZE_MB, reg) * SZ_1M,
408 err = nfp_cpp_readq(cpp, nfp_resource_cpp_id(nsp->res),
409 nfp_resource_address(nsp->res) +
415 cpp_id = FIELD_GET(NSP_DFLT_BUFFER_CPP, reg) << 8;
416 cpp_buf = FIELD_GET(NSP_DFLT_BUFFER_ADDRESS, reg);
418 if (in_buf && in_size) {
419 err = nfp_cpp_write(cpp, cpp_id, cpp_buf, in_buf, in_size);
423 /* Zero out remaining part of the buffer */
424 if (out_buf && out_size && out_size > in_size) {
425 memset(out_buf, 0, out_size - in_size);
426 err = nfp_cpp_write(cpp, cpp_id, cpp_buf + in_size,
427 out_buf, out_size - in_size);
432 ret = nfp_nsp_command(nsp, code, option, cpp_id, cpp_buf);
436 if (out_buf && out_size) {
437 err = nfp_cpp_read(cpp, cpp_id, cpp_buf, out_buf, out_size);
445 int nfp_nsp_wait(struct nfp_nsp *state)
447 const unsigned long wait_until = jiffies + 30 * HZ;
450 nfp_dbg(state->cpp, "Waiting for NSP to respond (30 sec max).\n");
453 const unsigned long start_time = jiffies;
455 err = nfp_nsp_command(state, SPCODE_NOOP, 0, 0, 0);
459 if (msleep_interruptible(25)) {
464 if (time_after(start_time, wait_until)) {
470 nfp_err(state->cpp, "NSP failed to respond %d\n", err);
475 int nfp_nsp_device_soft_reset(struct nfp_nsp *state)
477 return nfp_nsp_command(state, SPCODE_SOFT_RESET, 0, 0, 0);
480 int nfp_nsp_load_fw(struct nfp_nsp *state, const struct firmware *fw)
482 return nfp_nsp_command_buf(state, SPCODE_FW_LOAD, fw->size, fw->data,
486 int nfp_nsp_read_eth_table(struct nfp_nsp *state, void *buf, unsigned int size)
488 return nfp_nsp_command_buf(state, SPCODE_ETH_RESCAN, size, NULL, 0,
492 int nfp_nsp_write_eth_table(struct nfp_nsp *state,
493 const void *buf, unsigned int size)
495 return nfp_nsp_command_buf(state, SPCODE_ETH_CONTROL, size, buf, size,
499 int nfp_nsp_read_identify(struct nfp_nsp *state, void *buf, unsigned int size)
501 return nfp_nsp_command_buf(state, SPCODE_NSP_IDENTIFY, size, NULL, 0,
505 int nfp_nsp_read_sensors(struct nfp_nsp *state, unsigned int sensor_mask,
506 void *buf, unsigned int size)
508 return nfp_nsp_command_buf(state, SPCODE_NSP_SENSORS, sensor_mask,