// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/* Copyright (C) 2015-2018 Netronome Systems, Inc. */
/*
* nfp_nsp.c
* Author: Jakub Kicinski <jakub.kicinski@netronome.com>
* Jason McMullan <jason.mcmullan@netronome.com>
*/
#include <asm/unaligned.h>
#include <linux/bitfield.h>
#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/overflow.h>
#include <linux/sizes.h>
#include <linux/slab.h>
#define NFP_SUBSYS "nfp_nsp"
#include "nfp.h"
#include "nfp_cpp.h"
#include "nfp_nsp.h"
#define NFP_NSP_TIMEOUT_DEFAULT 30
#define NFP_NSP_TIMEOUT_BOOT 30
/* Offsets relative to the CSR base */
#define NSP_STATUS 0x00
#define NSP_STATUS_MAGIC GENMASK_ULL(63, 48)
#define NSP_STATUS_MAJOR GENMASK_ULL(47, 44)
#define NSP_STATUS_MINOR GENMASK_ULL(43, 32)
#define NSP_STATUS_CODE GENMASK_ULL(31, 16)
#define NSP_STATUS_RESULT GENMASK_ULL(15, 8)
#define NSP_STATUS_BUSY BIT_ULL(0)
#define NSP_COMMAND 0x08
#define NSP_COMMAND_OPTION GENMASK_ULL(63, 32)
#define NSP_COMMAND_CODE GENMASK_ULL(31, 16)
#define NSP_COMMAND_DMA_BUF BIT_ULL(1)
#define NSP_COMMAND_START BIT_ULL(0)
/* CPP address to retrieve the data from */
#define NSP_BUFFER 0x10
#define NSP_BUFFER_CPP GENMASK_ULL(63, 40)
#define NSP_BUFFER_ADDRESS GENMASK_ULL(39, 0)
#define NSP_DFLT_BUFFER 0x18
#define NSP_DFLT_BUFFER_CPP GENMASK_ULL(63, 40)
#define NSP_DFLT_BUFFER_ADDRESS GENMASK_ULL(39, 0)
#define NSP_DFLT_BUFFER_CONFIG 0x20
#define NSP_DFLT_BUFFER_DMA_CHUNK_ORDER GENMASK_ULL(63, 58)
#define NSP_DFLT_BUFFER_SIZE_4KB GENMASK_ULL(15, 8)
#define NSP_DFLT_BUFFER_SIZE_MB GENMASK_ULL(7, 0)
#define NFP_CAP_CMD_DMA_SG 0x28
#define NSP_MAGIC 0xab10
#define NSP_MAJOR 0
#define NSP_MINOR 8
#define NSP_CODE_MAJOR GENMASK(15, 12)
#define NSP_CODE_MINOR GENMASK(11, 0)
#define NFP_FW_LOAD_RET_MAJOR GENMASK(15, 8)
#define NFP_FW_LOAD_RET_MINOR GENMASK(23, 16)
#define NFP_HWINFO_LOOKUP_SIZE GENMASK(11, 0)
#define NFP_VERSIONS_SIZE GENMASK(11, 0)
#define NFP_VERSIONS_CNT_OFF 0
#define NFP_VERSIONS_BSP_OFF 2
#define NFP_VERSIONS_CPLD_OFF 6
#define NFP_VERSIONS_APP_OFF 10
#define NFP_VERSIONS_BUNDLE_OFF 14
#define NFP_VERSIONS_UNDI_OFF 18
#define NFP_VERSIONS_NCSI_OFF 22
#define NFP_VERSIONS_CFGR_OFF 26
#define NSP_SFF_EEPROM_BLOCK_LEN 8
enum nfp_nsp_cmd {
SPCODE_NOOP = 0, /* No operation */
SPCODE_SOFT_RESET = 1, /* Soft reset the NFP */
SPCODE_FW_DEFAULT = 2, /* Load default (UNDI) FW */
SPCODE_PHY_INIT = 3, /* Initialize the PHY */
SPCODE_MAC_INIT = 4, /* Initialize the MAC */
SPCODE_PHY_RXADAPT = 5, /* Re-run PHY RX Adaptation */
SPCODE_FW_LOAD = 6, /* Load fw from buffer, len in option */
SPCODE_ETH_RESCAN = 7, /* Rescan ETHs, write ETH_TABLE to buf */
SPCODE_ETH_CONTROL = 8, /* Update media config from buffer */
SPCODE_NSP_WRITE_FLASH = 11, /* Load and flash image from buffer */
SPCODE_NSP_SENSORS = 12, /* Read NSP sensor(s) */
SPCODE_NSP_IDENTIFY = 13, /* Read NSP version */
SPCODE_FW_STORED = 16, /* If no FW loaded, load flash app FW */
SPCODE_HWINFO_LOOKUP = 17, /* Lookup HWinfo with overwrites etc. */
SPCODE_HWINFO_SET = 18, /* Set HWinfo entry */
SPCODE_FW_LOADED = 19, /* Is application firmware loaded */
SPCODE_VERSIONS = 21, /* Report FW versions */
SPCODE_READ_SFF_EEPROM = 22, /* Read module EEPROM */
};
struct nfp_nsp_dma_buf {
__le32 chunk_cnt;
__le32 reserved[3];
struct {
__le32 size;
__le32 reserved;
__le64 addr;
} descs[];
};
static const struct {
int code;
const char *msg;
} nsp_errors[] = {
{ 6010, "could not map to phy for port" },
{ 6011, "not an allowed rate/lanes for port" },
{ 6012, "not an allowed rate/lanes for port" },
{ 6013, "high/low error, change other port first" },
{ 6014, "config not found in flash" },
};
struct nfp_nsp {
struct nfp_cpp *cpp;
struct nfp_resource *res;
struct {
u16 major;
u16 minor;
} ver;
/* Eth table config state */
bool modified;
unsigned int idx;
void *entries;
};
/**
* struct nfp_nsp_command_arg - NFP command argument structure
* @code: NFP SP Command Code
* @dma: @buf points to a host buffer, not NSP buffer
* @timeout_sec:Timeout value to wait for completion in seconds
* @option: NFP SP Command Argument
* @buf: NFP SP Buffer Address
* @error_cb: Callback for interpreting option if error occurred
* @error_quiet:Don't print command error/warning. Protocol errors are still
* logged.
*/
struct nfp_nsp_command_arg {
u16 code;
bool dma;
unsigned int timeout_sec;
u32 option;
u64 buf;
void (*error_cb)(struct nfp_nsp *state, u32 ret_val);
bool error_quiet;
};
/**
* struct nfp_nsp_command_buf_arg - NFP command with buffer argument structure
* @arg: NFP command argument structure
* @in_buf: Buffer with data for input
* @in_size: Size of @in_buf
* @out_buf: Buffer for output data
* @out_size: Size of @out_buf
*/
struct nfp_nsp_command_buf_arg {
struct nfp_nsp_command_arg arg;
const void *in_buf;
unsigned int in_size;
void *out_buf;
unsigned int out_size;
};
struct nfp_cpp *nfp_nsp_cpp(struct nfp_nsp *state)
{