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 if (fstat(fw_f, &file_stat) < 0) {
345 RTE_LOG(INFO, PMD, "Firmware file %s/%s size is unknown",
346 DEFAULT_FW_PATH, DEFAULT_FW_FILENAME);
351 fsize = file_stat.st_size;
352 RTE_LOG(DEBUG, PMD, "Firmware file with size: %" PRIu64 "\n",
355 if (fsize > (off_t)size) {
356 RTE_LOG(INFO, PMD, "fw file too big: %" PRIu64
357 " bytes (%" PRIu64 " max)",
358 (uint64_t)fsize, (uint64_t)size);
363 fw_buf = malloc((size_t)size);
365 RTE_LOG(INFO, PMD, "malloc failed for fw buffer");
369 memset(fw_buf, 0, size);
371 bytes = read(fw_f, fw_buf, fsize);
372 if (bytes != fsize) {
373 RTE_LOG(INFO, PMD, "Reading fw to buffer failed.\n"
374 "Just %" PRIu64 " of %" PRIu64 " bytes read.",
375 (uint64_t)bytes, (uint64_t)fsize);
381 ret = nspu_command(nspu_desc, NSP_CMD_FW_LOAD, 0, 1, fw_buf, 0, bytes);
389 /* Firmware symbol descriptor size */
390 #define NFP_SYM_DESC_LEN 40
392 #define SYMBOL_DATA(b, off) (*(int64_t *)((b) + (off)))
393 #define SYMBOL_UDATA(b, off) (*(uint64_t *)((b) + (off)))
395 /* Firmware symbols contain information about how to access what they
396 * represent. It can be as simple as an numeric variable declared at a
397 * specific NFP memory, but it can also be more complex structures and
398 * related to specific hardware functionalities or components. Target,
399 * domain and address allow to create the BAR window for accessing such
400 * hw object and size defines the length to map.
402 * A vNIC is a network interface implemented inside the NFP and using a
403 * subset of device PCI BARs. Specific firmware symbols allow to map those
404 * vNIC bars by host drivers like the NFP PMD.
406 * Accessing what the symbol represents implies to map the access through
407 * a PCI BAR window. NFP expansion BARs are used in this regard through
408 * the NSPU interface.
411 nfp_nspu_set_bar_from_symbl(nspu_desc_t *desc, const char *symbl,
412 uint32_t expbar, uint64_t *pcie_offset,
422 sym_buf = malloc(desc->buf_size);
426 strncpy(sym_buf, symbl, strlen(symbl));
427 ret = nspu_command(desc, NSP_CMD_GET_SYMBOL, 1, 1, sym_buf,
428 NFP_SYM_DESC_LEN, strlen(symbl));
430 RTE_LOG(DEBUG, PMD, "symbol resolution (%s) failed\n", symbl);
434 /* Reading symbol information */
435 type = SYMBOL_DATA(sym_buf, 0);
436 target = SYMBOL_DATA(sym_buf, 8);
437 domain = SYMBOL_DATA(sym_buf, 16);
438 addr = SYMBOL_UDATA(sym_buf, 24);
439 *size = (ssize_t)SYMBOL_UDATA(sym_buf, 32);
442 RTE_LOG(INFO, PMD, "wrong symbol type\n");
446 if (!(target == 7 || target == -7)) {
447 RTE_LOG(INFO, PMD, "wrong symbol target\n");
451 if (domain == 8 || domain == 9) {
452 RTE_LOG(INFO, PMD, "wrong symbol domain\n");
457 /* Adjusting address based on symbol location */
458 if ((domain >= 24) && (domain < 28) && (target == 7)) {
459 addr = 1ULL << 37 | addr | ((uint64_t)domain & 0x3) << 35;
461 addr = 1ULL << 39 | addr | ((uint64_t)domain & 0x3f) << 32;
466 /* Configuring NFP expansion bar for mapping specific PCI BAR window */
467 nfp_nspu_mem_bar_cfg(desc, expbar, target, addr, pcie_offset);
469 /* This is the PCI BAR offset to use by the host */
470 *pcie_offset |= ((expbar & 0x7) << (desc->barsz - 3));
478 nfp_nsp_fw_setup(nspu_desc_t *desc, const char *sym, uint64_t *pcie_offset)
480 ssize_t bar0_sym_size;
482 /* If the symbol resolution works, it implies a firmware app
485 if (!nfp_nspu_set_bar_from_symbl(desc, sym, NFP_NET_PF_CFG_EXP_BAR,
486 pcie_offset, &bar0_sym_size))
489 /* No firmware app detected or not the right one */
490 RTE_LOG(INFO, PMD, "No firmware detected. Resetting NFP...\n");
491 if (nfp_fw_reset(desc) < 0) {
492 RTE_LOG(ERR, PMD, "nfp fw reset failed\n");
496 RTE_LOG(INFO, PMD, "Reset done.\n");
497 RTE_LOG(INFO, PMD, "Uploading firmware...\n");
499 if (nfp_fw_upload(desc) < 0) {
500 RTE_LOG(ERR, PMD, "nfp fw upload failed\n");
504 RTE_LOG(INFO, PMD, "Done.\n");
506 /* Now the symbol should be there */
507 if (nfp_nspu_set_bar_from_symbl(desc, sym, NFP_NET_PF_CFG_EXP_BAR,
508 pcie_offset, &bar0_sym_size)) {
509 RTE_LOG(ERR, PMD, "nfp PF BAR symbol resolution failed\n");
517 nfp_nsp_map_ctrl_bar(nspu_desc_t *desc, uint64_t *pcie_offset)
519 ssize_t bar0_sym_size;
521 if (nfp_nspu_set_bar_from_symbl(desc, "_pf0_net_bar0",
522 NFP_NET_PF_CFG_EXP_BAR,
523 pcie_offset, &bar0_sym_size))
530 * This is a hardcoded fixed NFP internal CPP bus address for the hw queues unit
531 * inside the PCIE island.
533 #define NFP_CPP_PCIE_QUEUES ((uint64_t)(1ULL << 39) | 0x80000 | \
534 ((uint64_t)0x4 & 0x3f) << 32)
536 /* Configure a specific NFP expansion bar for accessing the vNIC rx/tx BARs */
538 nfp_nsp_map_queues_bar(nspu_desc_t *desc, uint64_t *pcie_offset)
540 nfp_nspu_mem_bar_cfg(desc, NFP_NET_PF_HW_QUEUES_EXP_BAR, 0,
541 NFP_CPP_PCIE_QUEUES, pcie_offset);
543 /* This is the pcie offset to use by the host */
544 *pcie_offset |= ((NFP_NET_PF_HW_QUEUES_EXP_BAR & 0x7) << (27 - 3));
548 nfp_nsp_eth_config(nspu_desc_t *desc, int port, int up)
550 union eth_table_entry *entries, *entry;
557 RTE_LOG(INFO, PMD, "Hw ethernet port %d configure...\n", port);
558 rte_spinlock_lock(&desc->nsp_lock);
559 entries = malloc(NSP_ETH_TABLE_SIZE);
561 rte_spinlock_unlock(&desc->nsp_lock);
565 ret = nspu_command(desc, NSP_CMD_READ_ETH_TABLE, 1, 0, entries,
566 NSP_ETH_TABLE_SIZE, 0);
568 rte_spinlock_unlock(&desc->nsp_lock);
575 for (i = 0; i < NSP_ETH_MAX_COUNT; i++) {
576 /* ports in use do not appear sequentially in the table */
577 if (!(entry->port & NSP_ETH_PORT_LANES_MASK)) {
578 /* entry not in use */
588 if (i == NSP_ETH_MAX_COUNT) {
589 rte_spinlock_unlock(&desc->nsp_lock);
594 if (up && !(entry->state & NSP_ETH_STATE_CONFIGURED)) {
595 entry->control |= NSP_ETH_STATE_CONFIGURED;
599 if (!up && (entry->state & NSP_ETH_STATE_CONFIGURED)) {
600 entry->control &= ~NSP_ETH_STATE_CONFIGURED;
605 ret = nspu_command(desc, NSP_CMD_WRITE_ETH_TABLE, 0, 1, entries,
606 0, NSP_ETH_TABLE_SIZE);
609 "Hw ethernet port %d configure done\n", port);
612 "Hw ethernet port %d configure failed\n", port);
614 rte_spinlock_unlock(&desc->nsp_lock);
620 nfp_nsp_eth_read_table(nspu_desc_t *desc, union eth_table_entry **table)
627 RTE_LOG(INFO, PMD, "Reading hw ethernet table...\n");
629 /* port 0 allocates the eth table and read it using NSPU */
630 *table = malloc(NSP_ETH_TABLE_SIZE);
634 ret = nspu_command(desc, NSP_CMD_READ_ETH_TABLE, 1, 0, *table,
635 NSP_ETH_TABLE_SIZE, 0);
639 RTE_LOG(INFO, PMD, "Done\n");