1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
6 * Author: Jakub Kicinski <jakub.kicinski@netronome.com>
7 * Jason McMullan <jason.mcmullan@netronome.com>
10 #include <asm/unaligned.h>
11 #include <linux/bitfield.h>
12 #include <linux/delay.h>
13 #include <linux/firmware.h>
14 #include <linux/kernel.h>
15 #include <linux/kthread.h>
16 #include <linux/overflow.h>
17 #include <linux/sizes.h>
18 #include <linux/slab.h>
20 #define NFP_SUBSYS "nfp_nsp"
26 #define NFP_NSP_TIMEOUT_DEFAULT 30
27 #define NFP_NSP_TIMEOUT_BOOT 30
29 /* Offsets relative to the CSR base */
30 #define NSP_STATUS 0x00
31 #define NSP_STATUS_MAGIC GENMASK_ULL(63, 48)
32 #define NSP_STATUS_MAJOR GENMASK_ULL(47, 44)
33 #define NSP_STATUS_MINOR GENMASK_ULL(43, 32)
34 #define NSP_STATUS_CODE GENMASK_ULL(31, 16)
35 #define NSP_STATUS_RESULT GENMASK_ULL(15, 8)
36 #define NSP_STATUS_BUSY BIT_ULL(0)
38 #define NSP_COMMAND 0x08
39 #define NSP_COMMAND_OPTION GENMASK_ULL(63, 32)
40 #define NSP_COMMAND_CODE GENMASK_ULL(31, 16)
41 #define NSP_COMMAND_DMA_BUF BIT_ULL(1)
42 #define NSP_COMMAND_START BIT_ULL(0)
44 /* CPP address to retrieve the data from */
45 #define NSP_BUFFER 0x10
46 #define NSP_BUFFER_CPP GENMASK_ULL(63, 40)
47 #define NSP_BUFFER_ADDRESS GENMASK_ULL(39, 0)
49 #define NSP_DFLT_BUFFER 0x18
50 #define NSP_DFLT_BUFFER_CPP GENMASK_ULL(63, 40)
51 #define NSP_DFLT_BUFFER_ADDRESS GENMASK_ULL(39, 0)
53 #define NSP_DFLT_BUFFER_CONFIG 0x20
54 #define NSP_DFLT_BUFFER_DMA_CHUNK_ORDER GENMASK_ULL(63, 58)
55 #define NSP_DFLT_BUFFER_SIZE_4KB GENMASK_ULL(15, 8)
56 #define NSP_DFLT_BUFFER_SIZE_MB GENMASK_ULL(7, 0)
58 #define NFP_CAP_CMD_DMA_SG 0x28
60 #define NSP_MAGIC 0xab10
64 #define NSP_CODE_MAJOR GENMASK(15, 12)
65 #define NSP_CODE_MINOR GENMASK(11, 0)
67 #define NFP_FW_LOAD_RET_MAJOR GENMASK(15, 8)
68 #define NFP_FW_LOAD_RET_MINOR GENMASK(23, 16)
70 #define NFP_HWINFO_LOOKUP_SIZE GENMASK(11, 0)
72 #define NFP_VERSIONS_SIZE GENMASK(11, 0)
73 #define NFP_VERSIONS_CNT_OFF 0
74 #define NFP_VERSIONS_BSP_OFF 2
75 #define NFP_VERSIONS_CPLD_OFF 6
76 #define NFP_VERSIONS_APP_OFF 10
77 #define NFP_VERSIONS_BUNDLE_OFF 14
78 #define NFP_VERSIONS_UNDI_OFF 18
79 #define NFP_VERSIONS_NCSI_OFF 22
80 #define NFP_VERSIONS_CFGR_OFF 26
82 #define NSP_SFF_EEPROM_BLOCK_LEN 8
85 SPCODE_NOOP = 0, /* No operation */
86 SPCODE_SOFT_RESET = 1, /* Soft reset the NFP */
87 SPCODE_FW_DEFAULT = 2, /* Load default (UNDI) FW */
88 SPCODE_PHY_INIT = 3, /* Initialize the PHY */
89 SPCODE_MAC_INIT = 4, /* Initialize the MAC */
90 SPCODE_PHY_RXADAPT = 5, /* Re-run PHY RX Adaptation */
91 SPCODE_FW_LOAD = 6, /* Load fw from buffer, len in option */
92 SPCODE_ETH_RESCAN = 7, /* Rescan ETHs, write ETH_TABLE to buf */
93 SPCODE_ETH_CONTROL = 8, /* Update media config from buffer */
94 SPCODE_NSP_WRITE_FLASH = 11, /* Load and flash image from buffer */
95 SPCODE_NSP_SENSORS = 12, /* Read NSP sensor(s) */
96 SPCODE_NSP_IDENTIFY = 13, /* Read NSP version */
97 SPCODE_FW_STORED = 16, /* If no FW loaded, load flash app FW */
98 SPCODE_HWINFO_LOOKUP = 17, /* Lookup HWinfo with overwrites etc. */
99 SPCODE_HWINFO_SET = 18, /* Set HWinfo entry */
100 SPCODE_FW_LOADED = 19, /* Is application firmware loaded */
101 SPCODE_VERSIONS = 21, /* Report FW versions */
102 SPCODE_READ_SFF_EEPROM = 22, /* Read module EEPROM */
105 struct nfp_nsp_dma_buf {
115 static const struct {
119 { 6010, "could not map to phy for port" },
120 { 6011, "not an allowed rate/lanes for port" },
121 { 6012, "not an allowed rate/lanes for port" },
122 { 6013, "high/low error, change other port first" },
123 { 6014, "config not found in flash" },
128 struct nfp_resource *res;
134 /* Eth table config state */
141 * struct nfp_nsp_command_arg - NFP command argument structure
142 * @code: NFP SP Command Code
143 * @dma: @buf points to a host buffer, not NSP buffer
144 * @timeout_sec:Timeout value to wait for completion in seconds
145 * @option: NFP SP Command Argument
146 * @buf: NFP SP Buffer Address
147 * @error_cb: Callback for interpreting option if error occurred
148 * @error_quiet:Don't print command error/warning. Protocol errors are still
151 struct nfp_nsp_command_arg {
154 unsigned int timeout_sec;
157 void (*error_cb)(struct nfp_nsp *state, u32 ret_val);
162 * struct nfp_nsp_command_buf_arg - NFP command with buffer argument structure
163 * @arg: NFP command argument structure
164 * @in_buf: Buffer with data for input
165 * @in_size: Size of @in_buf
166 * @out_buf: Buffer for output data
167 * @out_size: Size of @out_buf
169 struct nfp_nsp_command_buf_arg {
170 struct nfp_nsp_command_arg arg;
172 unsigned int in_size;
174 unsigned int out_size;
177 struct nfp_cpp *nfp_nsp_cpp(struct nfp_nsp *state)
182 bool nfp_nsp_config_modified(struct nfp_nsp *state)
184 return state->modified;
187 void nfp_nsp_config_set_modified(struct nfp_nsp *state, bool modified)
189 state->modified = modified;
192 void *nfp_nsp_config_entries(struct nfp_nsp *state)
194 return state->entries;
197 unsigned int nfp_nsp_config_idx(struct nfp_nsp *state)
203 nfp_nsp_config_set_state(struct nfp_nsp *state, void *entries, unsigned int idx)
205 state->entries = entries;
209 void nfp_nsp_config_clear_state(struct nfp_nsp *state)
211 state->entries = NULL;
215 static void nfp_nsp_print_extended_error(struct nfp_nsp *state, u32 ret_val)
222 for (i = 0; i < ARRAY_SIZE(nsp_errors); i++)
223 if (ret_val == nsp_errors[i].code)
224 nfp_err(state->cpp, "err msg: %s\n", nsp_errors[i].msg);
227 static int nfp_nsp_check(struct nfp_nsp *state)
229 struct nfp_cpp *cpp = state->cpp;
234 nsp_cpp = nfp_resource_cpp_id(state->res);
235 nsp_status = nfp_resource_address(state->res) + NSP_STATUS;
237 err = nfp_cpp_readq(cpp, nsp_cpp, nsp_status, ®);
241 if (FIELD_GET(NSP_STATUS_MAGIC, reg) != NSP_MAGIC) {
242 nfp_err(cpp, "Cannot detect NFP Service Processor\n");
246 state->ver.major = FIELD_GET(NSP_STATUS_MAJOR, reg);
247 state->ver.minor = FIELD_GET(NSP_STATUS_MINOR, reg);
249 if (state->ver.major != NSP_MAJOR) {
250 nfp_err(cpp, "Unsupported ABI %hu.%hu\n",
251 state->ver.major, state->ver.minor);
254 if (state->ver.minor < NSP_MINOR) {
255 nfp_err(cpp, "ABI too old to support NIC operation (%u.%hu < %u.%u), please update the management FW on the flash\n",
256 NSP_MAJOR, state->ver.minor, NSP_MAJOR, NSP_MINOR);
260 if (reg & NSP_STATUS_BUSY) {
261 nfp_err(cpp, "Service processor busy!\n");
269 * nfp_nsp_open() - Prepare for communication and lock the NSP resource.
270 * @cpp: NFP CPP Handle
272 struct nfp_nsp *nfp_nsp_open(struct nfp_cpp *cpp)
274 struct nfp_resource *res;
275 struct nfp_nsp *state;
278 res = nfp_resource_acquire(cpp, NFP_RESOURCE_NSP);
282 state = kzalloc(sizeof(*state), GFP_KERNEL);
284 nfp_resource_release(res);
285 return ERR_PTR(-ENOMEM);
290 err = nfp_nsp_check(state);
292 nfp_nsp_close(state);
300 * nfp_nsp_close() - Clean up and unlock the NSP resource.
301 * @state: NFP SP state
303 void nfp_nsp_close(struct nfp_nsp *state)
305 nfp_resource_release(state->res);
309 u16 nfp_nsp_get_abi_ver_major(struct nfp_nsp *state)
311 return state->ver.major;
314 u16 nfp_nsp_get_abi_ver_minor(struct nfp_nsp *state)
316 return state->ver.minor;
320 nfp_nsp_wait_reg(struct nfp_cpp *cpp, u64 *reg, u32 nsp_cpp, u64 addr,
321 u64 mask, u64 val, u32 timeout_sec)
323 const unsigned long wait_until = jiffies + timeout_sec * HZ;
327 const unsigned long start_time = jiffies;
329 err = nfp_cpp_readq(cpp, nsp_cpp, addr, reg);
333 if ((*reg & mask) == val)
338 if (time_after(start_time, wait_until))
344 * __nfp_nsp_command() - Execute a command on the NFP Service Processor
345 * @state: NFP SP state
346 * @arg: NFP command argument structure
348 * Return: 0 for success with no result
350 * positive value for NSP completion with a result code
352 * -EAGAIN if the NSP is not yet present
353 * -ENODEV if the NSP is not a supported model
354 * -EBUSY if the NSP is stuck
355 * -EINTR if interrupted while waiting for completion
356 * -ETIMEDOUT if the NSP took longer than @timeout_sec seconds to complete
359 __nfp_nsp_command(struct nfp_nsp *state, const struct nfp_nsp_command_arg *arg)
361 u64 reg, ret_val, nsp_base, nsp_buffer, nsp_status, nsp_command;
362 struct nfp_cpp *cpp = state->cpp;
366 nsp_cpp = nfp_resource_cpp_id(state->res);
367 nsp_base = nfp_resource_address(state->res);
368 nsp_status = nsp_base + NSP_STATUS;
369 nsp_command = nsp_base + NSP_COMMAND;
370 nsp_buffer = nsp_base + NSP_BUFFER;
372 err = nfp_nsp_check(state);
376 err = nfp_cpp_writeq(cpp, nsp_cpp, nsp_buffer, arg->buf);
380 err = nfp_cpp_writeq(cpp, nsp_cpp, nsp_command,
381 FIELD_PREP(NSP_COMMAND_OPTION, arg->option) |
382 FIELD_PREP(NSP_COMMAND_CODE, arg->code) |
383 FIELD_PREP(NSP_COMMAND_DMA_BUF, arg->dma) |
384 FIELD_PREP(NSP_COMMAND_START, 1));
388 /* Wait for NSP_COMMAND_START to go to 0 */
389 err = nfp_nsp_wait_reg(cpp, ®, nsp_cpp, nsp_command,
390 NSP_COMMAND_START, 0, NFP_NSP_TIMEOUT_DEFAULT);
392 nfp_err(cpp, "Error %d waiting for code 0x%04x to start\n",
397 /* Wait for NSP_STATUS_BUSY to go to 0 */
398 err = nfp_nsp_wait_reg(cpp, ®, nsp_cpp, nsp_status, NSP_STATUS_BUSY,
399 0, arg->timeout_sec ?: NFP_NSP_TIMEOUT_DEFAULT);
401 nfp_err(cpp, "Error %d waiting for code 0x%04x to complete\n",
406 err = nfp_cpp_readq(cpp, nsp_cpp, nsp_command, &ret_val);
409 ret_val = FIELD_GET(NSP_COMMAND_OPTION, ret_val);
411 err = FIELD_GET(NSP_STATUS_RESULT, reg);
413 if (!arg->error_quiet)
414 nfp_warn(cpp, "Result (error) code set: %d (%d) command: %d\n",
415 -err, (int)ret_val, arg->code);
418 arg->error_cb(state, ret_val);
420 nfp_nsp_print_extended_error(state, ret_val);
427 static int nfp_nsp_command(struct nfp_nsp *state, u16 code)
429 const struct nfp_nsp_command_arg arg = {
433 return __nfp_nsp_command(state, &arg);
437 nfp_nsp_command_buf_def(struct nfp_nsp *nsp,
438 struct nfp_nsp_command_buf_arg *arg)
440 struct nfp_cpp *cpp = nsp->cpp;
445 err = nfp_cpp_readq(cpp, nfp_resource_cpp_id(nsp->res),
446 nfp_resource_address(nsp->res) +
452 cpp_id = FIELD_GET(NSP_DFLT_BUFFER_CPP, reg) << 8;
453 cpp_buf = FIELD_GET(NSP_DFLT_BUFFER_ADDRESS, reg);
455 if (arg->in_buf && arg->in_size) {
456 err = nfp_cpp_write(cpp, cpp_id, cpp_buf,
457 arg->in_buf, arg->in_size);
461 /* Zero out remaining part of the buffer */
462 if (arg->out_buf && arg->out_size && arg->out_size > arg->in_size) {
463 err = nfp_cpp_write(cpp, cpp_id, cpp_buf + arg->in_size,
464 arg->out_buf, arg->out_size - arg->in_size);
469 if (!FIELD_FIT(NSP_BUFFER_CPP, cpp_id >> 8) ||
470 !FIELD_FIT(NSP_BUFFER_ADDRESS, cpp_buf)) {
471 nfp_err(cpp, "Buffer out of reach %08x %016llx\n",
476 arg->arg.buf = FIELD_PREP(NSP_BUFFER_CPP, cpp_id >> 8) |
477 FIELD_PREP(NSP_BUFFER_ADDRESS, cpp_buf);
478 ret = __nfp_nsp_command(nsp, &arg->arg);
482 if (arg->out_buf && arg->out_size) {
483 err = nfp_cpp_read(cpp, cpp_id, cpp_buf,
484 arg->out_buf, arg->out_size);
493 nfp_nsp_command_buf_dma_sg(struct nfp_nsp *nsp,
494 struct nfp_nsp_command_buf_arg *arg,
495 unsigned int max_size, unsigned int chunk_order,
496 unsigned int dma_order)
498 struct nfp_cpp *cpp = nsp->cpp;
499 struct nfp_nsp_dma_buf *desc;
505 size_t chunk_size, dma_size;
512 chunk_size = BIT_ULL(chunk_order);
513 dma_size = BIT_ULL(dma_order);
514 nseg = DIV_ROUND_UP(max_size, chunk_size);
516 chunks = kzalloc(array_size(sizeof(*chunks), nseg), GFP_KERNEL);
522 for (i = 0; i < nseg; i++) {
525 chunks[i].chunk = kmalloc(chunk_size,
526 GFP_KERNEL | __GFP_NOWARN);
527 if (!chunks[i].chunk)
530 chunks[i].len = min_t(u64, chunk_size, max_size - off);
533 if (arg->in_size > off) {
534 coff = min_t(u64, arg->in_size - off, chunk_size);
535 memcpy(chunks[i].chunk, arg->in_buf + off, coff);
537 memset(chunks[i].chunk + coff, 0, chunk_size - coff);
539 off += chunks[i].len;
542 dev = nfp_cpp_device(cpp)->parent;
544 for (i = 0; i < nseg; i++) {
547 addr = dma_map_single(dev, chunks[i].chunk, chunks[i].len,
549 chunks[i].dma_addr = addr;
551 ret = dma_mapping_error(dev, addr);
553 goto exit_unmap_prev;
555 if (WARN_ONCE(round_down(addr, dma_size) !=
556 round_down(addr + chunks[i].len - 1, dma_size),
557 "unaligned DMA address: %pad %lu %zd\n",
558 &addr, chunks[i].len, dma_size)) {
561 goto exit_unmap_prev;
565 desc_sz = struct_size(desc, descs, nseg);
566 desc = kmalloc(desc_sz, GFP_KERNEL);
572 desc->chunk_cnt = cpu_to_le32(nseg);
573 for (i = 0; i < nseg; i++) {
574 desc->descs[i].size = cpu_to_le32(chunks[i].len);
575 desc->descs[i].addr = cpu_to_le64(chunks[i].dma_addr);
578 dma_desc = dma_map_single(dev, desc, desc_sz, DMA_TO_DEVICE);
579 ret = dma_mapping_error(dev, dma_desc);
584 arg->arg.buf = dma_desc;
585 ret = __nfp_nsp_command(nsp, &arg->arg);
587 goto exit_unmap_desc;
591 while (off < arg->out_size) {
594 len = min_t(u64, chunks[i].len, arg->out_size - off);
595 memcpy(arg->out_buf + off, chunks[i].chunk, len);
601 dma_unmap_single(dev, dma_desc, desc_sz, DMA_TO_DEVICE);
608 dma_unmap_single(dev, chunks[i].dma_addr, chunks[i].len,
613 kfree(chunks[i].chunk);
616 nfp_err(cpp, "NSP: SG DMA failed for command 0x%04x: %d (sz:%d cord:%d)\n",
617 arg->arg.code, ret, max_size, chunk_order);
622 nfp_nsp_command_buf_dma(struct nfp_nsp *nsp,
623 struct nfp_nsp_command_buf_arg *arg,
624 unsigned int max_size, unsigned int dma_order)
626 unsigned int chunk_order, buf_order;
627 struct nfp_cpp *cpp = nsp->cpp;
632 buf_order = order_base_2(roundup_pow_of_two(max_size));
634 err = nfp_cpp_readq(cpp, nfp_resource_cpp_id(nsp->res),
635 nfp_resource_address(nsp->res) + NFP_CAP_CMD_DMA_SG,
639 sg_ok = reg & BIT_ULL(arg->arg.code - 1);
642 if (buf_order > dma_order) {
643 nfp_err(cpp, "NSP: can't service non-SG DMA for command 0x%04x\n",
647 chunk_order = buf_order;
649 chunk_order = min_t(unsigned int, dma_order, PAGE_SHIFT);
652 return nfp_nsp_command_buf_dma_sg(nsp, arg, max_size, chunk_order,
657 nfp_nsp_command_buf(struct nfp_nsp *nsp, struct nfp_nsp_command_buf_arg *arg)
659 unsigned int dma_order, def_size, max_size;
660 struct nfp_cpp *cpp = nsp->cpp;
664 if (nsp->ver.minor < 13) {
665 nfp_err(cpp, "NSP: Code 0x%04x with buffer not supported (ABI %hu.%hu)\n",
666 arg->arg.code, nsp->ver.major, nsp->ver.minor);
670 err = nfp_cpp_readq(cpp, nfp_resource_cpp_id(nsp->res),
671 nfp_resource_address(nsp->res) +
672 NSP_DFLT_BUFFER_CONFIG,
677 /* Zero out undefined part of the out buffer */
678 if (arg->out_buf && arg->out_size && arg->out_size > arg->in_size)
679 memset(arg->out_buf, 0, arg->out_size - arg->in_size);
681 max_size = max(arg->in_size, arg->out_size);
682 def_size = FIELD_GET(NSP_DFLT_BUFFER_SIZE_MB, reg) * SZ_1M +
683 FIELD_GET(NSP_DFLT_BUFFER_SIZE_4KB, reg) * SZ_4K;
684 dma_order = FIELD_GET(NSP_DFLT_BUFFER_DMA_CHUNK_ORDER, reg);
685 if (def_size >= max_size) {
686 return nfp_nsp_command_buf_def(nsp, arg);
687 } else if (!dma_order) {
688 nfp_err(cpp, "NSP: default buffer too small for command 0x%04x (%u < %u)\n",
689 arg->arg.code, def_size, max_size);
693 return nfp_nsp_command_buf_dma(nsp, arg, max_size, dma_order);
696 int nfp_nsp_wait(struct nfp_nsp *state)
698 const unsigned long wait_until = jiffies + NFP_NSP_TIMEOUT_BOOT * HZ;
701 nfp_dbg(state->cpp, "Waiting for NSP to respond (%u sec max).\n",
702 NFP_NSP_TIMEOUT_BOOT);
705 const unsigned long start_time = jiffies;
707 err = nfp_nsp_command(state, SPCODE_NOOP);
711 if (msleep_interruptible(25)) {
716 if (time_after(start_time, wait_until)) {
722 nfp_err(state->cpp, "NSP failed to respond %d\n", err);
727 int nfp_nsp_device_soft_reset(struct nfp_nsp *state)
729 return nfp_nsp_command(state, SPCODE_SOFT_RESET);
732 int nfp_nsp_mac_reinit(struct nfp_nsp *state)
734 return nfp_nsp_command(state, SPCODE_MAC_INIT);
737 static void nfp_nsp_load_fw_extended_msg(struct nfp_nsp *state, u32 ret_val)
739 static const char * const major_msg[] = {
740 /* 0 */ "Firmware from driver loaded",
741 /* 1 */ "Firmware from flash loaded",
742 /* 2 */ "Firmware loading failure",
744 static const char * const minor_msg[] = {
746 /* 1 */ "no named partition on flash",
747 /* 2 */ "error reading from flash",
748 /* 3 */ "can not deflate",
749 /* 4 */ "not a trusted file",
750 /* 5 */ "can not parse FW file",
751 /* 6 */ "MIP not found in FW file",
752 /* 7 */ "null firmware name in MIP",
753 /* 8 */ "FW version none",
754 /* 9 */ "FW build number none",
755 /* 10 */ "no FW selection policy HWInfo key found",
756 /* 11 */ "static FW selection policy",
757 /* 12 */ "FW version has precedence",
758 /* 13 */ "different FW application load requested",
759 /* 14 */ "development build",
761 unsigned int major, minor;
764 major = FIELD_GET(NFP_FW_LOAD_RET_MAJOR, ret_val);
765 minor = FIELD_GET(NFP_FW_LOAD_RET_MINOR, ret_val);
767 if (!nfp_nsp_has_stored_fw_load(state))
770 /* Lower the message level in legacy case */
771 if (major == 0 && (minor == 0 || minor == 10))
778 if (major >= ARRAY_SIZE(major_msg))
779 nfp_printk(level, state->cpp, "FW loading status: %x\n",
781 else if (minor >= ARRAY_SIZE(minor_msg))
782 nfp_printk(level, state->cpp, "%s, reason code: %d\n",
783 major_msg[major], minor);
785 nfp_printk(level, state->cpp, "%s%c %s\n",
786 major_msg[major], minor ? ',' : '.',
790 int nfp_nsp_load_fw(struct nfp_nsp *state, const struct firmware *fw)
792 struct nfp_nsp_command_buf_arg load_fw = {
794 .code = SPCODE_FW_LOAD,
796 .error_cb = nfp_nsp_load_fw_extended_msg,
803 ret = nfp_nsp_command_buf(state, &load_fw);
807 nfp_nsp_load_fw_extended_msg(state, ret);
811 int nfp_nsp_write_flash(struct nfp_nsp *state, const struct firmware *fw)
813 struct nfp_nsp_command_buf_arg write_flash = {
815 .code = SPCODE_NSP_WRITE_FLASH,
823 return nfp_nsp_command_buf(state, &write_flash);
826 int nfp_nsp_read_eth_table(struct nfp_nsp *state, void *buf, unsigned int size)
828 struct nfp_nsp_command_buf_arg eth_rescan = {
830 .code = SPCODE_ETH_RESCAN,
837 return nfp_nsp_command_buf(state, ð_rescan);
840 int nfp_nsp_write_eth_table(struct nfp_nsp *state,
841 const void *buf, unsigned int size)
843 struct nfp_nsp_command_buf_arg eth_ctrl = {
845 .code = SPCODE_ETH_CONTROL,
852 return nfp_nsp_command_buf(state, ð_ctrl);
855 int nfp_nsp_read_identify(struct nfp_nsp *state, void *buf, unsigned int size)
857 struct nfp_nsp_command_buf_arg identify = {
859 .code = SPCODE_NSP_IDENTIFY,
866 return nfp_nsp_command_buf(state, &identify);
869 int nfp_nsp_read_sensors(struct nfp_nsp *state, unsigned int sensor_mask,
870 void *buf, unsigned int size)
872 struct nfp_nsp_command_buf_arg sensors = {
874 .code = SPCODE_NSP_SENSORS,
875 .option = sensor_mask,
881 return nfp_nsp_command_buf(state, &sensors);
884 int nfp_nsp_load_stored_fw(struct nfp_nsp *state)
886 const struct nfp_nsp_command_arg arg = {
887 .code = SPCODE_FW_STORED,
888 .error_cb = nfp_nsp_load_fw_extended_msg,
892 ret = __nfp_nsp_command(state, &arg);
896 nfp_nsp_load_fw_extended_msg(state, ret);
901 __nfp_nsp_hwinfo_lookup(struct nfp_nsp *state, void *buf, unsigned int size,
904 struct nfp_nsp_command_buf_arg hwinfo_lookup = {
906 .code = SPCODE_HWINFO_LOOKUP,
908 .error_quiet = optional,
916 return nfp_nsp_command_buf(state, &hwinfo_lookup);
919 int nfp_nsp_hwinfo_lookup(struct nfp_nsp *state, void *buf, unsigned int size)
923 size = min_t(u32, size, NFP_HWINFO_LOOKUP_SIZE);
925 err = __nfp_nsp_hwinfo_lookup(state, buf, size, false);
929 if (strnlen(buf, size) == size) {
930 nfp_err(state->cpp, "NSP HWinfo value not NULL-terminated\n");
937 int nfp_nsp_hwinfo_lookup_optional(struct nfp_nsp *state, void *buf,
938 unsigned int size, const char *default_val)
942 /* Ensure that the default value is usable irrespective of whether
943 * it is actually going to be used.
945 if (strnlen(default_val, size) == size)
948 if (!nfp_nsp_has_hwinfo_lookup(state)) {
949 strcpy(buf, default_val);
953 size = min_t(u32, size, NFP_HWINFO_LOOKUP_SIZE);
955 err = __nfp_nsp_hwinfo_lookup(state, buf, size, true);
957 if (err == -ENOENT) {
958 strcpy(buf, default_val);
962 nfp_err(state->cpp, "NSP HWinfo lookup failed: %d\n", err);
966 if (strnlen(buf, size) == size) {
967 nfp_err(state->cpp, "NSP HWinfo value not NULL-terminated\n");
974 int nfp_nsp_hwinfo_set(struct nfp_nsp *state, void *buf, unsigned int size)
976 struct nfp_nsp_command_buf_arg hwinfo_set = {
978 .code = SPCODE_HWINFO_SET,
985 return nfp_nsp_command_buf(state, &hwinfo_set);
988 int nfp_nsp_fw_loaded(struct nfp_nsp *state)
990 const struct nfp_nsp_command_arg arg = {
991 .code = SPCODE_FW_LOADED,
994 return __nfp_nsp_command(state, &arg);
997 int nfp_nsp_versions(struct nfp_nsp *state, void *buf, unsigned int size)
999 struct nfp_nsp_command_buf_arg versions = {
1001 .code = SPCODE_VERSIONS,
1002 .option = min_t(u32, size, NFP_VERSIONS_SIZE),
1005 .out_size = min_t(u32, size, NFP_VERSIONS_SIZE),
1008 return nfp_nsp_command_buf(state, &versions);
1011 const char *nfp_nsp_versions_get(enum nfp_nsp_versions id, bool flash,
1012 const u8 *buf, unsigned int size)
1014 static const u32 id2off[] = {
1015 [NFP_VERSIONS_BSP] = NFP_VERSIONS_BSP_OFF,
1016 [NFP_VERSIONS_CPLD] = NFP_VERSIONS_CPLD_OFF,
1017 [NFP_VERSIONS_APP] = NFP_VERSIONS_APP_OFF,
1018 [NFP_VERSIONS_BUNDLE] = NFP_VERSIONS_BUNDLE_OFF,
1019 [NFP_VERSIONS_UNDI] = NFP_VERSIONS_UNDI_OFF,
1020 [NFP_VERSIONS_NCSI] = NFP_VERSIONS_NCSI_OFF,
1021 [NFP_VERSIONS_CFGR] = NFP_VERSIONS_CFGR_OFF,
1023 unsigned int field, buf_field_cnt, buf_off;
1025 if (id >= ARRAY_SIZE(id2off) || !id2off[id])
1026 return ERR_PTR(-EINVAL);
1028 field = id * 2 + flash;
1030 buf_field_cnt = get_unaligned_le16(buf);
1031 if (buf_field_cnt <= field)
1032 return ERR_PTR(-ENOENT);
1034 buf_off = get_unaligned_le16(buf + id2off[id] + flash * 2);
1036 return ERR_PTR(-ENOENT);
1038 if (buf_off >= size)
1039 return ERR_PTR(-EINVAL);
1040 if (strnlen(&buf[buf_off], size - buf_off) == size - buf_off)
1041 return ERR_PTR(-EINVAL);
1043 return (const char *)&buf[buf_off];
1047 __nfp_nsp_module_eeprom(struct nfp_nsp *state, void *buf, unsigned int size)
1049 struct nfp_nsp_command_buf_arg module_eeprom = {
1051 .code = SPCODE_READ_SFF_EEPROM,
1060 return nfp_nsp_command_buf(state, &module_eeprom);
1063 int nfp_nsp_read_module_eeprom(struct nfp_nsp *state, int eth_index,
1064 unsigned int offset, void *data,
1065 unsigned int len, unsigned int *read_len)
1077 BUILD_BUG_ON(offsetof(struct eeprom_buf, data) % 8);
1079 /* Buffer must be large enough and rounded to the next block size. */
1080 bufsz = struct_size(buf, data, round_up(len, NSP_SFF_EEPROM_BLOCK_LEN));
1081 buf = kzalloc(bufsz, GFP_KERNEL);
1086 offsetof(struct eeprom_buf, data) / NSP_SFF_EEPROM_BLOCK_LEN;
1087 buf->length = cpu_to_le16(len);
1088 buf->offset = cpu_to_le16(offset);
1089 buf->eth_index = eth_index;
1091 ret = __nfp_nsp_module_eeprom(state, buf, bufsz);
1093 *read_len = min_t(unsigned int, len, le16_to_cpu(buf->readlen));
1095 memcpy(data, buf->data, *read_len);
1097 if (!ret && *read_len < len)