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_PCIE GENMASK_ULL(39, 38)
72 #define NSP_BUFFER_ADDRESS GENMASK_ULL(37, 0)
74 #define NSP_DFLT_BUFFER 0x18
76 #define NSP_DFLT_BUFFER_CONFIG 0x20
77 #define NSP_DFLT_BUFFER_SIZE_MB GENMASK_ULL(7, 0)
79 #define NSP_MAGIC 0xab10
81 #define NSP_MINOR (__MAX_SPCODE - 1)
83 #define NSP_CODE_MAJOR GENMASK(15, 12)
84 #define NSP_CODE_MINOR GENMASK(11, 0)
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 */
100 static const struct {
104 { 0, "success" } /* placeholder to avoid warnings */
109 struct nfp_resource *res;
115 /* Eth table config state */
121 struct nfp_cpp *nfp_nsp_cpp(struct nfp_nsp *state)
126 bool nfp_nsp_config_modified(struct nfp_nsp *state)
128 return state->modified;
131 void nfp_nsp_config_set_modified(struct nfp_nsp *state, bool modified)
133 state->modified = modified;
136 void *nfp_nsp_config_entries(struct nfp_nsp *state)
138 return state->entries;
141 unsigned int nfp_nsp_config_idx(struct nfp_nsp *state)
147 nfp_nsp_config_set_state(struct nfp_nsp *state, void *entries, unsigned int idx)
149 state->entries = entries;
153 void nfp_nsp_config_clear_state(struct nfp_nsp *state)
155 state->entries = NULL;
159 static void nfp_nsp_print_extended_error(struct nfp_nsp *state, u32 ret_val)
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);
171 static int nfp_nsp_check(struct nfp_nsp *state)
173 struct nfp_cpp *cpp = state->cpp;
178 nsp_cpp = nfp_resource_cpp_id(state->res);
179 nsp_status = nfp_resource_address(state->res) + NSP_STATUS;
181 err = nfp_cpp_readq(cpp, nsp_cpp, nsp_status, ®);
185 if (FIELD_GET(NSP_STATUS_MAGIC, reg) != NSP_MAGIC) {
186 nfp_err(cpp, "Cannot detect NFP Service Processor\n");
190 state->ver.major = FIELD_GET(NSP_STATUS_MAJOR, reg);
191 state->ver.minor = FIELD_GET(NSP_STATUS_MINOR, reg);
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);
199 if (reg & NSP_STATUS_BUSY) {
200 nfp_err(cpp, "Service processor busy!\n");
208 * nfp_nsp_open() - Prepare for communication and lock the NSP resource.
209 * @cpp: NFP CPP Handle
211 struct nfp_nsp *nfp_nsp_open(struct nfp_cpp *cpp)
213 struct nfp_resource *res;
214 struct nfp_nsp *state;
217 res = nfp_resource_acquire(cpp, NFP_RESOURCE_NSP);
221 state = kzalloc(sizeof(*state), GFP_KERNEL);
223 nfp_resource_release(res);
224 return ERR_PTR(-ENOMEM);
229 err = nfp_nsp_check(state);
231 nfp_nsp_close(state);
239 * nfp_nsp_close() - Clean up and unlock the NSP resource.
240 * @state: NFP SP state
242 void nfp_nsp_close(struct nfp_nsp *state)
244 nfp_resource_release(state->res);
248 u16 nfp_nsp_get_abi_ver_major(struct nfp_nsp *state)
250 return state->ver.major;
253 u16 nfp_nsp_get_abi_ver_minor(struct nfp_nsp *state)
255 return state->ver.minor;
259 nfp_nsp_wait_reg(struct nfp_cpp *cpp, u64 *reg,
260 u32 nsp_cpp, u64 addr, u64 mask, u64 val)
262 const unsigned long wait_until = jiffies + 30 * HZ;
266 const unsigned long start_time = jiffies;
268 err = nfp_cpp_readq(cpp, nsp_cpp, addr, reg);
272 if ((*reg & mask) == val)
275 if (msleep_interruptible(25))
278 if (time_after(start_time, wait_until))
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
291 * Return: 0 for success with no result
293 * positive value for NSP completion with a result code
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
301 static int nfp_nsp_command(struct nfp_nsp *state, u16 code, u32 option,
302 u32 buff_cpp, u64 buff_addr)
304 u64 reg, ret_val, nsp_base, nsp_buffer, nsp_status, nsp_command;
305 struct nfp_cpp *cpp = state->cpp;
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;
315 err = nfp_nsp_check(state);
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);
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));
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));
339 /* Wait for NSP_COMMAND_START to go to 0 */
340 err = nfp_nsp_wait_reg(cpp, ®,
341 nsp_cpp, nsp_command, NSP_COMMAND_START, 0);
343 nfp_err(cpp, "Error %d waiting for code 0x%04x to start\n",
348 /* Wait for NSP_STATUS_BUSY to go to 0 */
349 err = nfp_nsp_wait_reg(cpp, ®,
350 nsp_cpp, nsp_status, NSP_STATUS_BUSY, 0);
352 nfp_err(cpp, "Error %d waiting for code 0x%04x to complete\n",
357 err = nfp_cpp_readq(cpp, nsp_cpp, nsp_command, &ret_val);
360 ret_val = FIELD_GET(NSP_COMMAND_OPTION, ret_val);
362 err = FIELD_GET(NSP_STATUS_RESULT, reg);
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);
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)
377 struct nfp_cpp *cpp = nsp->cpp;
378 unsigned int max_size;
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);
389 err = nfp_cpp_readq(cpp, nfp_resource_cpp_id(nsp->res),
390 nfp_resource_address(nsp->res) +
391 NSP_DFLT_BUFFER_CONFIG,
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,
404 err = nfp_cpp_readq(cpp, nfp_resource_cpp_id(nsp->res),
405 nfp_resource_address(nsp->res) +
411 cpp_id = FIELD_GET(NSP_BUFFER_CPP, reg) << 8;
412 cpp_buf = FIELD_GET(NSP_BUFFER_ADDRESS, reg);
414 if (in_buf && in_size) {
415 err = nfp_cpp_write(cpp, cpp_id, cpp_buf, in_buf, in_size);
420 ret = nfp_nsp_command(nsp, code, option, cpp_id, cpp_buf);
424 if (out_buf && out_size) {
425 err = nfp_cpp_read(cpp, cpp_id, cpp_buf, out_buf, out_size);
433 int nfp_nsp_wait(struct nfp_nsp *state)
435 const unsigned long wait_until = jiffies + 30 * HZ;
438 nfp_dbg(state->cpp, "Waiting for NSP to respond (30 sec max).\n");
441 const unsigned long start_time = jiffies;
443 err = nfp_nsp_command(state, SPCODE_NOOP, 0, 0, 0);
447 if (msleep_interruptible(25)) {
452 if (time_after(start_time, wait_until)) {
458 nfp_err(state->cpp, "NSP failed to respond %d\n", err);
463 int nfp_nsp_device_soft_reset(struct nfp_nsp *state)
467 err = nfp_nsp_command(state, SPCODE_SOFT_RESET, 0, 0, 0);
469 nfp_nffw_cache_flush(state->cpp);
474 int nfp_nsp_load_fw(struct nfp_nsp *state, const struct firmware *fw)
476 return nfp_nsp_command_buf(state, SPCODE_FW_LOAD, fw->size, fw->data,
480 int nfp_nsp_read_eth_table(struct nfp_nsp *state, void *buf, unsigned int size)
482 return nfp_nsp_command_buf(state, SPCODE_ETH_RESCAN, size, NULL, 0,
486 int nfp_nsp_write_eth_table(struct nfp_nsp *state,
487 const void *buf, unsigned int size)
489 return nfp_nsp_command_buf(state, SPCODE_ETH_CONTROL, size, buf, size,