11 #include <rte_byteorder.h>
15 #define CFG_EXP_BAR_ADDR_SZ 1
16 #define CFG_EXP_BAR_MAP_TYPE 1
18 #define EXP_BAR_TARGET_SHIFT 23
19 #define EXP_BAR_LENGTH_SHIFT 27 /* 0=32, 1=64 bit increment */
20 #define EXP_BAR_MAP_TYPE_SHIFT 29 /* Bulk BAR map */
22 /* NFP target for NSP access */
23 #define NFP_NSP_TARGET 7
25 /* Expansion BARs for mapping PF vnic BARs */
26 #define NFP_NET_PF_CFG_EXP_BAR 6
27 #define NFP_NET_PF_HW_QUEUES_EXP_BAR 5
30 * This is an NFP internal address used for configuring properly an NFP
33 #define MEM_CMD_BASE_ADDR 0x8100000000
35 /* NSP interface registers */
36 #define NSP_BASE (MEM_CMD_BASE_ADDR + 0x22100)
37 #define NSP_STATUS 0x00
38 #define NSP_COMMAND 0x08
39 #define NSP_BUFFER 0x10
40 #define NSP_DEFAULT_BUF 0x18
41 #define NSP_DEFAULT_BUF_CFG 0x20
43 #define NSP_MAGIC 0xab10
44 #define NSP_STATUS_MAGIC(x) (((x) >> 48) & 0xffff)
45 #define NSP_STATUS_MAJOR(x) (int)(((x) >> 44) & 0xf)
46 #define NSP_STATUS_MINOR(x) (int)(((x) >> 32) & 0xfff)
49 #define NSP_CMD_RESET 1
50 #define NSP_CMD_FW_LOAD 6
51 #define NSP_CMD_READ_ETH_TABLE 7
52 #define NSP_CMD_WRITE_ETH_TABLE 8
53 #define NSP_CMD_GET_SYMBOL 14
55 #define NSP_BUFFER_CFG_SIZE_MASK (0xff)
57 #define NSP_REG_ADDR(d, off, reg) ((uint8_t *)(d)->mem_base + (off) + (reg))
58 #define NSP_REG_VAL(p) (*(uint64_t *)(p))
61 * An NFP expansion BAR is configured for allowing access to a specific NFP
65 * desc: struct with basic NSP addresses to work with
66 * expbar: NFP PF expansion BAR index to configure
67 * tgt: NFP target to configure access
68 * addr: NFP target address
71 * pcie_offset: NFP PCI BAR offset to work with
74 nfp_nspu_mem_bar_cfg(nspu_desc_t *desc, int expbar, int tgt,
75 uint64_t addr, uint64_t *pcie_offset)
83 * NFP CPP address to configure. This comes from NFP 6000
84 * datasheet document based on Bulk mapping.
86 x = (addr >> (barsz - 3)) << (21 - (40 - (barsz - 3)));
87 x |= CFG_EXP_BAR_MAP_TYPE << EXP_BAR_MAP_TYPE_SHIFT;
88 x |= CFG_EXP_BAR_ADDR_SZ << EXP_BAR_LENGTH_SHIFT;
89 x |= tgt << EXP_BAR_TARGET_SHIFT;
91 /* Getting expansion bar configuration register address */
92 expbar_ptr = (uint32_t *)desc->cfg_base;
93 /* Each physical PCI BAR has 8 NFP expansion BARs */
94 expbar_ptr += (desc->pcie_bar * 8) + expbar;
96 /* Writing to the expansion BAR register */
97 *expbar_ptr = (uint32_t)x;
99 /* Getting the pcie offset to work with from userspace */
100 y = addr & ((uint64_t)(1 << (barsz - 3)) - 1);
105 * Configuring an expansion bar for accessing NSP userspace interface. This
106 * function configures always the same expansion bar, which implies access to
107 * previously configured NFP target is lost.
110 nspu_xlate(nspu_desc_t *desc, uint64_t addr, uint64_t *pcie_offset)
112 nfp_nspu_mem_bar_cfg(desc, desc->exp_bar, NFP_NSP_TARGET, addr,
117 nfp_nsp_get_abi_version(nspu_desc_t *desc, int *major, int *minor)
119 uint64_t pcie_offset;
122 nspu_xlate(desc, NSP_BASE, &pcie_offset);
123 nsp_reg = NSP_REG_VAL(NSP_REG_ADDR(desc, pcie_offset, NSP_STATUS));
125 if (NSP_STATUS_MAGIC(nsp_reg) != NSP_MAGIC)
128 *major = NSP_STATUS_MAJOR(nsp_reg);
129 *minor = NSP_STATUS_MINOR(nsp_reg);
135 nfp_nspu_init(nspu_desc_t *desc, int nfp, int pcie_bar, size_t pcie_barsz,
136 int exp_bar, void *exp_bar_cfg_base, void *exp_bar_mmap)
138 uint64_t offset, buffaddr;
142 desc->pcie_bar = pcie_bar;
143 desc->exp_bar = exp_bar;
144 desc->barsz = pcie_barsz;
145 desc->windowsz = 1 << (desc->barsz - 3);
146 desc->cfg_base = exp_bar_cfg_base;
147 desc->mem_base = exp_bar_mmap;
149 nspu_xlate(desc, NSP_BASE, &offset);
152 * Other NSPU clients can use other buffers. Let's tell NSPU we use the
155 buffaddr = NSP_REG_VAL(NSP_REG_ADDR(desc, offset, NSP_DEFAULT_BUF));
156 NSP_REG_VAL(NSP_REG_ADDR(desc, offset, NSP_BUFFER)) = buffaddr;
158 /* NFP internal addresses are 40 bits. Clean all other bits here */
159 buffaddr = buffaddr & (((uint64_t)1 << 40) - 1);
160 desc->bufaddr = buffaddr;
162 /* Lets get information about the buffer */
163 nsp_reg = NSP_REG_VAL(NSP_REG_ADDR(desc, offset, NSP_DEFAULT_BUF_CFG));
165 /* Buffer size comes in MBs. Coversion to bytes */
166 desc->buf_size = ((size_t)nsp_reg & NSP_BUFFER_CFG_SIZE_MASK) << 20;
171 #define NSPU_NFP_BUF(addr, base, off) \
172 (*(uint64_t *)((uint8_t *)(addr)->mem_base + ((base) | (off))))
174 #define NSPU_HOST_BUF(base, off) (*(uint64_t *)((uint8_t *)(base) + (off)))
177 nspu_buff_write(nspu_desc_t *desc, void *buffer, size_t size)
179 uint64_t pcie_offset, pcie_window_base, pcie_window_offset;
180 uint64_t windowsz = desc->windowsz;
181 uint64_t buffaddr, j, i = 0;
184 if (size > desc->buf_size)
187 buffaddr = desc->bufaddr;
188 windowsz = desc->windowsz;
191 /* Expansion bar reconfiguration per window size */
192 nspu_xlate(desc, buffaddr + i, &pcie_offset);
193 pcie_window_base = pcie_offset & (~(windowsz - 1));
194 pcie_window_offset = pcie_offset & (windowsz - 1);
195 for (j = pcie_window_offset; ((j < windowsz) && (i < size));
197 NSPU_NFP_BUF(desc, pcie_window_base, j) =
198 NSPU_HOST_BUF(buffer, i);
207 nspu_buff_read(nspu_desc_t *desc, void *buffer, size_t size)
209 uint64_t pcie_offset, pcie_window_base, pcie_window_offset;
210 uint64_t windowsz, i = 0, j;
214 if (size > desc->buf_size)
217 buffaddr = desc->bufaddr;
218 windowsz = desc->windowsz;
221 /* Expansion bar reconfiguration per window size */
222 nspu_xlate(desc, buffaddr + i, &pcie_offset);
223 pcie_window_base = pcie_offset & (~(windowsz - 1));
224 pcie_window_offset = pcie_offset & (windowsz - 1);
225 for (j = pcie_window_offset; ((j < windowsz) && (i < size));
227 NSPU_HOST_BUF(buffer, i) =
228 NSPU_NFP_BUF(desc, pcie_window_base, j);
237 nspu_command(nspu_desc_t *desc, uint16_t cmd, int read, int write,
238 void *buffer, size_t rsize, size_t wsize)
240 uint64_t status, cmd_reg;
246 /* Same expansion BAR is used for different things */
247 nspu_xlate(desc, NSP_BASE, &offset);
249 status = NSP_REG_VAL(NSP_REG_ADDR(desc, offset, NSP_STATUS));
251 while ((status & 0x1) && (retry < retries)) {
252 status = NSP_REG_VAL(NSP_REG_ADDR(desc, offset, NSP_STATUS));
257 if (retry == retries)
261 ret = nspu_buff_write(desc, buffer, wsize);
265 /* Expansion BAR changes when writing the buffer */
266 nspu_xlate(desc, NSP_BASE, &offset);
269 NSP_REG_VAL(NSP_REG_ADDR(desc, offset, NSP_COMMAND)) =
270 (uint64_t)wsize << 32 | (uint64_t)cmd << 16 | 1;
274 cmd_reg = NSP_REG_VAL(NSP_REG_ADDR(desc, offset, NSP_COMMAND));
275 while ((cmd_reg & 0x1) && (retry < retries)) {
276 cmd_reg = NSP_REG_VAL(NSP_REG_ADDR(desc, offset, NSP_COMMAND));
280 if (retry == retries)
284 status = NSP_REG_VAL(NSP_REG_ADDR(desc, offset, NSP_STATUS));
285 while ((status & 0x1) && (retry < retries)) {
286 status = NSP_REG_VAL(NSP_REG_ADDR(desc, offset, NSP_STATUS));
291 if (retry == retries)
294 ret = status & (0xff << 8);
299 ret = nspu_buff_read(desc, buffer, rsize);
308 nfp_fw_reset(nspu_desc_t *nspu_desc)
312 res = nspu_command(nspu_desc, NSP_CMD_RESET, 0, 0, 0, 0, 0);
315 RTE_LOG(INFO, PMD, "fw reset failed: error %d", res);
320 #define DEFAULT_FW_PATH "/lib/firmware/netronome"
321 #define DEFAULT_FW_FILENAME "nic_dpdk_default.nffw"
324 nfp_fw_upload(nspu_desc_t *nspu_desc)
329 struct stat file_stat;
334 size = nspu_desc->buf_size;
336 sprintf(filename, "%s/%s", DEFAULT_FW_PATH, DEFAULT_FW_FILENAME);
337 fw_f = open(filename, O_RDONLY);
339 RTE_LOG(INFO, PMD, "Firmware file %s/%s not found.",
340 DEFAULT_FW_PATH, DEFAULT_FW_FILENAME);
344 fstat(fw_f, &file_stat);
346 fsize = file_stat.st_size;
347 RTE_LOG(DEBUG, PMD, "Firmware file with size: %" PRIu64 "\n",
350 if (fsize > (off_t)size) {
351 RTE_LOG(INFO, PMD, "fw file too big: %" PRIu64
352 " bytes (%" PRIu64 " max)",
353 (uint64_t)fsize, (uint64_t)size);
357 fw_buf = malloc((size_t)size);
359 RTE_LOG(INFO, PMD, "malloc failed for fw buffer");
362 memset(fw_buf, 0, size);
364 bytes = read(fw_f, fw_buf, fsize);
365 if (bytes != fsize) {
366 RTE_LOG(INFO, PMD, "Reading fw to buffer failed.\n"
367 "Just %" PRIu64 " of %" PRIu64 " bytes read.",
368 (uint64_t)bytes, (uint64_t)fsize);
373 ret = nspu_command(nspu_desc, NSP_CMD_FW_LOAD, 0, 1, fw_buf, 0, bytes);
380 /* Firmware symbol descriptor size */
381 #define NFP_SYM_DESC_LEN 40
383 #define SYMBOL_DATA(b, off) (*(int64_t *)((b) + (off)))
384 #define SYMBOL_UDATA(b, off) (*(uint64_t *)((b) + (off)))
386 /* Firmware symbols contain information about how to access what they
387 * represent. It can be as simple as an numeric variable declared at a
388 * specific NFP memory, but it can also be more complex structures and
389 * related to specific hardware functionalities or components. Target,
390 * domain and address allow to create the BAR window for accessing such
391 * hw object and size defines the length to map.
393 * A vNIC is a network interface implemented inside the NFP and using a
394 * subset of device PCI BARs. Specific firmware symbols allow to map those
395 * vNIC bars by host drivers like the NFP PMD.
397 * Accessing what the symbol represents implies to map the access through
398 * a PCI BAR window. NFP expansion BARs are used in this regard through
399 * the NSPU interface.
402 nfp_nspu_set_bar_from_symbl(nspu_desc_t *desc, const char *symbl,
403 uint32_t expbar, uint64_t *pcie_offset,
413 sym_buf = malloc(desc->buf_size);
414 strncpy(sym_buf, symbl, strlen(symbl));
415 ret = nspu_command(desc, NSP_CMD_GET_SYMBOL, 1, 1, sym_buf,
416 NFP_SYM_DESC_LEN, strlen(symbl));
418 RTE_LOG(DEBUG, PMD, "symbol resolution (%s) failed\n", symbl);
422 /* Reading symbol information */
423 type = SYMBOL_DATA(sym_buf, 0);
424 target = SYMBOL_DATA(sym_buf, 8);
425 domain = SYMBOL_DATA(sym_buf, 16);
426 addr = SYMBOL_UDATA(sym_buf, 24);
427 *size = (ssize_t)SYMBOL_UDATA(sym_buf, 32);
430 RTE_LOG(INFO, PMD, "wrong symbol type\n");
434 if (!(target == 7 || target == -7)) {
435 RTE_LOG(INFO, PMD, "wrong symbol target\n");
439 if (domain == 8 || domain == 9) {
440 RTE_LOG(INFO, PMD, "wrong symbol domain\n");
445 /* Adjusting address based on symbol location */
446 if ((domain >= 24) && (domain < 28) && (target == 7)) {
447 addr = 1ULL << 37 | addr | ((uint64_t)domain & 0x3) << 35;
449 addr = 1ULL << 39 | addr | ((uint64_t)domain & 0x3f) << 32;
454 /* Configuring NFP expansion bar for mapping specific PCI BAR window */
455 nfp_nspu_mem_bar_cfg(desc, expbar, target, addr, pcie_offset);
457 /* This is the PCI BAR offset to use by the host */
458 *pcie_offset |= ((expbar & 0x7) << (desc->barsz - 3));
466 nfp_nsp_fw_setup(nspu_desc_t *desc, const char *sym, uint64_t *pcie_offset)
468 ssize_t bar0_sym_size;
470 /* If the symbol resolution works, it implies a firmware app
473 if (!nfp_nspu_set_bar_from_symbl(desc, sym, NFP_NET_PF_CFG_EXP_BAR,
474 pcie_offset, &bar0_sym_size))
477 /* No firmware app detected or not the right one */
478 RTE_LOG(INFO, PMD, "No firmware detected. Resetting NFP...\n");
479 if (nfp_fw_reset(desc) < 0) {
480 RTE_LOG(ERR, PMD, "nfp fw reset failed\n");
484 RTE_LOG(INFO, PMD, "Reset done.\n");
485 RTE_LOG(INFO, PMD, "Uploading firmware...\n");
487 if (nfp_fw_upload(desc) < 0) {
488 RTE_LOG(ERR, PMD, "nfp fw upload failed\n");
492 RTE_LOG(INFO, PMD, "Done.\n");
494 /* Now the symbol should be there */
495 if (nfp_nspu_set_bar_from_symbl(desc, sym, NFP_NET_PF_CFG_EXP_BAR,
496 pcie_offset, &bar0_sym_size)) {
497 RTE_LOG(ERR, PMD, "nfp PF BAR symbol resolution failed\n");
505 nfp_nsp_map_ctrl_bar(nspu_desc_t *desc, uint64_t *pcie_offset)
507 ssize_t bar0_sym_size;
509 if (nfp_nspu_set_bar_from_symbl(desc, "_pf0_net_bar0",
510 NFP_NET_PF_CFG_EXP_BAR,
511 pcie_offset, &bar0_sym_size))
518 * This is a hardcoded fixed NFP internal CPP bus address for the hw queues unit
519 * inside the PCIE island.
521 #define NFP_CPP_PCIE_QUEUES ((uint64_t)(1ULL << 39) | 0x80000 | \
522 ((uint64_t)0x4 & 0x3f) << 32)
524 /* Configure a specific NFP expansion bar for accessing the vNIC rx/tx BARs */
526 nfp_nsp_map_queues_bar(nspu_desc_t *desc, uint64_t *pcie_offset)
528 nfp_nspu_mem_bar_cfg(desc, NFP_NET_PF_HW_QUEUES_EXP_BAR, 0,
529 NFP_CPP_PCIE_QUEUES, pcie_offset);
531 /* This is the pcie offset to use by the host */
532 *pcie_offset |= ((NFP_NET_PF_HW_QUEUES_EXP_BAR & 0x7) << (27 - 3));
536 nfp_nsp_eth_config(nspu_desc_t *desc, int port, int up)
538 union eth_table_entry *entries, *entry;
545 RTE_LOG(INFO, PMD, "Hw ethernet port %d configure...\n", port);
546 rte_spinlock_lock(&desc->nsp_lock);
547 entries = malloc(NSP_ETH_TABLE_SIZE);
549 rte_spinlock_unlock(&desc->nsp_lock);
553 ret = nspu_command(desc, NSP_CMD_READ_ETH_TABLE, 1, 0, entries,
554 NSP_ETH_TABLE_SIZE, 0);
556 rte_spinlock_unlock(&desc->nsp_lock);
562 for (i = 0; i < NSP_ETH_MAX_COUNT; i++) {
563 /* ports in use do not appear sequentially in the table */
564 if (!(entry->port & NSP_ETH_PORT_LANES_MASK)) {
565 /* entry not in use */
575 if (i == NSP_ETH_MAX_COUNT) {
576 rte_spinlock_unlock(&desc->nsp_lock);
580 if (up && !(entry->state & NSP_ETH_STATE_CONFIGURED)) {
581 entry->control |= NSP_ETH_STATE_CONFIGURED;
585 if (!up && (entry->state & NSP_ETH_STATE_CONFIGURED)) {
586 entry->control &= ~NSP_ETH_STATE_CONFIGURED;
591 ret = nspu_command(desc, NSP_CMD_WRITE_ETH_TABLE, 0, 1, entries,
592 0, NSP_ETH_TABLE_SIZE);
595 "Hw ethernet port %d configure done\n", port);
598 "Hw ethernet port %d configure failed\n", port);
600 rte_spinlock_unlock(&desc->nsp_lock);
605 nfp_nsp_eth_read_table(nspu_desc_t *desc, union eth_table_entry **table)
609 RTE_LOG(INFO, PMD, "Reading hw ethernet table...\n");
610 /* port 0 allocates the eth table and read it using NSPU */
611 *table = malloc(NSP_ETH_TABLE_SIZE);
615 ret = nspu_command(desc, NSP_CMD_READ_ETH_TABLE, 1, 0, *table,
616 NSP_ETH_TABLE_SIZE, 0);
620 RTE_LOG(INFO, PMD, "Done\n");