1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2005-2018 Solarflare Communications Inc.
5 * Copyright 2019-2022 Xilinx Inc.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published
9 * by the Free Software Foundation, incorporated herein by reference.
12 #include "net_driver.h"
13 #include <linux/module.h>
14 #include <linux/aer.h>
15 #include "efx_common.h"
16 #include "efx_channels.h"
18 #include "ef100_nic.h"
19 #include "ef100_netdev.h"
20 #include "ef100_sriov.h"
21 #include "ef100_regs.h"
24 #define EFX_EF100_PCI_DEFAULT_BAR 2
26 /* Number of bytes at start of vendor specified extended capability that indicate
27 * that the capability is vendor specified. i.e. offset from value returned by
28 * pci_find_next_ext_capability() to beginning of vendor specified capability
31 #define PCI_EXT_CAP_HDR_LENGTH 4
33 /* Expected size of a Xilinx continuation address table entry. */
34 #define ESE_GZ_CFGBAR_CONT_CAP_MIN_LENGTH 16
36 struct ef100_func_ctl_window {
42 static int ef100_pci_walk_xilinx_table(struct efx_nic *efx, u64 offset,
43 struct ef100_func_ctl_window *result);
45 /* Number of bytes to offset when reading bit position x with dword accessors. */
46 #define ROUND_DOWN_TO_DWORD(x) (((x) & (~31)) >> 3)
48 #define EXTRACT_BITS(x, lbn, width) \
49 (((x) >> ((lbn) & 31)) & ((1ull << (width)) - 1))
51 static u32 _ef100_pci_get_bar_bits_with_width(struct efx_nic *efx,
57 efx_readd(efx, &dword, structure_start + ROUND_DOWN_TO_DWORD(lbn));
59 return EXTRACT_BITS(le32_to_cpu(dword.u32[0]), lbn, width);
62 #define ef100_pci_get_bar_bits(efx, entry_location, bitdef) \
63 _ef100_pci_get_bar_bits_with_width(efx, entry_location, \
64 ESF_GZ_CFGBAR_ ## bitdef ## _LBN, \
65 ESF_GZ_CFGBAR_ ## bitdef ## _WIDTH)
67 static int ef100_pci_parse_ef100_entry(struct efx_nic *efx, int entry_location,
68 struct ef100_func_ctl_window *result)
70 u64 offset = ef100_pci_get_bar_bits(efx, entry_location, EF100_FUNC_CTL_WIN_OFF) <<
71 ESE_GZ_EF100_FUNC_CTL_WIN_OFF_SHIFT;
72 u32 bar = ef100_pci_get_bar_bits(efx, entry_location, EF100_BAR);
74 netif_dbg(efx, probe, efx->net_dev,
75 "Found EF100 function control window bar=%d offset=0x%llx\n",
79 netif_err(efx, probe, efx->net_dev,
80 "Duplicated EF100 table entry.\n");
84 if (bar == ESE_GZ_CFGBAR_EF100_BAR_NUM_EXPANSION_ROM ||
85 bar == ESE_GZ_CFGBAR_EF100_BAR_NUM_INVALID) {
86 netif_err(efx, probe, efx->net_dev,
87 "Bad BAR value of %d in Xilinx capabilities EF100 entry.\n",
93 result->offset = offset;
98 static bool ef100_pci_does_bar_overflow(struct efx_nic *efx, int bar,
101 return next_entry + ESE_GZ_CFGBAR_ENTRY_HEADER_SIZE >
102 pci_resource_len(efx->pci_dev, bar);
105 /* Parse a Xilinx capabilities table entry describing a continuation to a new
108 static int ef100_pci_parse_continue_entry(struct efx_nic *efx, int entry_location,
109 struct ef100_func_ctl_window *result)
111 unsigned int previous_bar;
117 efx_reado(efx, &entry, entry_location);
119 bar = EFX_OWORD_FIELD32(entry, ESF_GZ_CFGBAR_CONT_CAP_BAR);
121 offset = EFX_OWORD_FIELD64(entry, ESF_GZ_CFGBAR_CONT_CAP_OFFSET) <<
122 ESE_GZ_CONT_CAP_OFFSET_BYTES_SHIFT;
124 previous_bar = efx->mem_bar;
126 if (bar == ESE_GZ_VSEC_BAR_NUM_EXPANSION_ROM ||
127 bar == ESE_GZ_VSEC_BAR_NUM_INVALID) {
128 netif_err(efx, probe, efx->net_dev,
129 "Bad BAR value of %d in Xilinx capabilities sub-table.\n",
134 if (bar != previous_bar) {
137 if (ef100_pci_does_bar_overflow(efx, bar, offset)) {
138 netif_err(efx, probe, efx->net_dev,
139 "Xilinx table will overrun BAR[%d] offset=0x%llx\n",
144 /* Temporarily map new BAR. */
145 rc = efx_init_io(efx, bar,
146 (dma_addr_t)DMA_BIT_MASK(ESF_GZ_TX_SEND_ADDR_WIDTH),
147 pci_resource_len(efx->pci_dev, bar));
149 netif_err(efx, probe, efx->net_dev,
150 "Mapping new BAR for Xilinx table failed, rc=%d\n", rc);
155 rc = ef100_pci_walk_xilinx_table(efx, offset, result);
159 if (bar != previous_bar) {
162 /* Put old BAR back. */
163 rc = efx_init_io(efx, previous_bar,
164 (dma_addr_t)DMA_BIT_MASK(ESF_GZ_TX_SEND_ADDR_WIDTH),
165 pci_resource_len(efx->pci_dev, previous_bar));
167 netif_err(efx, probe, efx->net_dev,
168 "Putting old BAR back failed, rc=%d\n", rc);
176 /* Iterate over the Xilinx capabilities table in the currently mapped BAR and
177 * call ef100_pci_parse_ef100_entry() on any EF100 entries and
178 * ef100_pci_parse_continue_entry() on any table continuations.
180 static int ef100_pci_walk_xilinx_table(struct efx_nic *efx, u64 offset,
181 struct ef100_func_ctl_window *result)
183 u64 current_entry = offset;
187 u32 id = ef100_pci_get_bar_bits(efx, current_entry, ENTRY_FORMAT);
188 u32 last = ef100_pci_get_bar_bits(efx, current_entry, ENTRY_LAST);
189 u32 rev = ef100_pci_get_bar_bits(efx, current_entry, ENTRY_REV);
192 if (id == ESE_GZ_CFGBAR_ENTRY_LAST)
195 entry_size = ef100_pci_get_bar_bits(efx, current_entry, ENTRY_SIZE);
197 netif_dbg(efx, probe, efx->net_dev,
198 "Seen Xilinx table entry 0x%x size 0x%x at 0x%llx in BAR[%d]\n",
199 id, entry_size, current_entry, efx->mem_bar);
201 if (entry_size < sizeof(u32) * 2) {
202 netif_err(efx, probe, efx->net_dev,
203 "Xilinx table entry too short len=0x%x\n", entry_size);
208 case ESE_GZ_CFGBAR_ENTRY_EF100:
209 if (rev != ESE_GZ_CFGBAR_ENTRY_REV_EF100 ||
210 entry_size < ESE_GZ_CFGBAR_ENTRY_SIZE_EF100) {
211 netif_err(efx, probe, efx->net_dev,
212 "Bad length or rev for EF100 entry in Xilinx capabilities table. entry_size=%d rev=%d.\n",
217 rc = ef100_pci_parse_ef100_entry(efx, current_entry,
222 case ESE_GZ_CFGBAR_ENTRY_CONT_CAP_ADDR:
223 if (rev != 0 || entry_size < ESE_GZ_CFGBAR_CONT_CAP_MIN_LENGTH) {
224 netif_err(efx, probe, efx->net_dev,
225 "Bad length or rev for continue entry in Xilinx capabilities table. entry_size=%d rev=%d.\n",
230 rc = ef100_pci_parse_continue_entry(efx, current_entry, result);
235 /* Ignore unknown table entries. */
242 current_entry += entry_size;
244 if (ef100_pci_does_bar_overflow(efx, efx->mem_bar, current_entry)) {
245 netif_err(efx, probe, efx->net_dev,
246 "Xilinx table overrun at position=0x%llx.\n",
253 static int _ef100_pci_get_config_bits_with_width(struct efx_nic *efx,
254 int structure_start, int lbn,
255 int width, u32 *result)
257 int rc, pos = structure_start + ROUND_DOWN_TO_DWORD(lbn);
260 rc = pci_read_config_dword(efx->pci_dev, pos, &temp);
262 netif_err(efx, probe, efx->net_dev,
263 "Failed to read PCI config dword at %d\n",
268 *result = EXTRACT_BITS(temp, lbn, width);
273 #define ef100_pci_get_config_bits(efx, entry_location, bitdef, result) \
274 _ef100_pci_get_config_bits_with_width(efx, entry_location, \
275 ESF_GZ_VSEC_ ## bitdef ## _LBN, \
276 ESF_GZ_VSEC_ ## bitdef ## _WIDTH, result)
278 /* Call ef100_pci_walk_xilinx_table() for the Xilinx capabilities table pointed
279 * to by this PCI_EXT_CAP_ID_VNDR.
281 static int ef100_pci_parse_xilinx_cap(struct efx_nic *efx, int vndr_cap,
283 struct ef100_func_ctl_window *result)
291 rc = ef100_pci_get_config_bits(efx, vndr_cap, TBL_BAR, &bar);
293 netif_err(efx, probe, efx->net_dev,
294 "Failed to read ESF_GZ_VSEC_TBL_BAR, rc=%d\n",
299 if (bar == ESE_GZ_CFGBAR_CONT_CAP_BAR_NUM_EXPANSION_ROM ||
300 bar == ESE_GZ_CFGBAR_CONT_CAP_BAR_NUM_INVALID) {
301 netif_err(efx, probe, efx->net_dev,
302 "Bad BAR value of %d in Xilinx capabilities sub-table.\n",
307 rc = ef100_pci_get_config_bits(efx, vndr_cap, TBL_OFF_LO, &offset_lo);
309 netif_err(efx, probe, efx->net_dev,
310 "Failed to read ESF_GZ_VSEC_TBL_OFF_LO, rc=%d\n",
315 /* Get optional extension to 64bit offset. */
317 rc = ef100_pci_get_config_bits(efx, vndr_cap, TBL_OFF_HI, &offset_high);
319 netif_err(efx, probe, efx->net_dev,
320 "Failed to read ESF_GZ_VSEC_TBL_OFF_HI, rc=%d\n",
326 offset = (((u64)offset_lo) << ESE_GZ_VSEC_TBL_OFF_LO_BYTES_SHIFT) |
327 (((u64)offset_high) << ESE_GZ_VSEC_TBL_OFF_HI_BYTES_SHIFT);
329 if (offset > pci_resource_len(efx->pci_dev, bar) - sizeof(u32) * 2) {
330 netif_err(efx, probe, efx->net_dev,
331 "Xilinx table will overrun BAR[%d] offset=0x%llx\n",
336 /* Temporarily map BAR. */
337 rc = efx_init_io(efx, bar,
338 (dma_addr_t)DMA_BIT_MASK(ESF_GZ_TX_SEND_ADDR_WIDTH),
339 pci_resource_len(efx->pci_dev, bar));
341 netif_err(efx, probe, efx->net_dev,
342 "efx_init_io failed, rc=%d\n", rc);
346 rc = ef100_pci_walk_xilinx_table(efx, offset, result);
348 /* Unmap temporarily mapped BAR. */
353 /* Call ef100_pci_parse_ef100_entry() for each Xilinx PCI_EXT_CAP_ID_VNDR
356 static int ef100_pci_find_func_ctrl_window(struct efx_nic *efx,
357 struct ef100_func_ctl_window *result)
359 int num_xilinx_caps = 0;
362 result->valid = false;
364 while ((cap = pci_find_next_ext_capability(efx->pci_dev, cap, PCI_EXT_CAP_ID_VNDR)) != 0) {
365 int vndr_cap = cap + PCI_EXT_CAP_HDR_LENGTH;
373 rc = ef100_pci_get_config_bits(efx, vndr_cap, ID, &vsec_id);
375 netif_err(efx, probe, efx->net_dev,
376 "Failed to read ESF_GZ_VSEC_ID, rc=%d\n",
381 rc = ef100_pci_get_config_bits(efx, vndr_cap, VER, &vsec_ver);
383 netif_err(efx, probe, efx->net_dev,
384 "Failed to read ESF_GZ_VSEC_VER, rc=%d\n",
389 /* Get length of whole capability - i.e. starting at cap */
390 rc = ef100_pci_get_config_bits(efx, vndr_cap, LEN, &vsec_len);
392 netif_err(efx, probe, efx->net_dev,
393 "Failed to read ESF_GZ_VSEC_LEN, rc=%d\n",
398 if (vsec_id == ESE_GZ_XILINX_VSEC_ID &&
399 vsec_ver == ESE_GZ_VSEC_VER_XIL_CFGBAR &&
400 vsec_len >= ESE_GZ_VSEC_LEN_MIN) {
401 bool has_offset_hi = (vsec_len >= ESE_GZ_VSEC_LEN_HIGH_OFFT);
403 rc = ef100_pci_parse_xilinx_cap(efx, vndr_cap,
404 has_offset_hi, result);
410 if (num_xilinx_caps && !result->valid) {
411 netif_err(efx, probe, efx->net_dev,
412 "Seen %d Xilinx tables, but no EF100 entry.\n",
420 /* Final NIC shutdown
421 * This is called only at module unload (or hotplug removal). A PF can call
422 * this on its VFs to ensure they are unbound first.
424 static void ef100_pci_remove(struct pci_dev *pci_dev)
426 struct efx_nic *efx = pci_get_drvdata(pci_dev);
427 struct efx_probe_data *probe_data;
432 probe_data = container_of(efx, struct efx_probe_data, efx);
433 ef100_remove_netdev(probe_data);
434 #ifdef CONFIG_SFC_SRIOV
435 efx_fini_struct_tc(efx);
441 pci_dbg(pci_dev, "shutdown successful\n");
443 pci_disable_pcie_error_reporting(pci_dev);
445 pci_set_drvdata(pci_dev, NULL);
446 efx_fini_struct(efx);
450 static int ef100_pci_probe(struct pci_dev *pci_dev,
451 const struct pci_device_id *entry)
453 struct ef100_func_ctl_window fcw = { 0 };
454 struct efx_probe_data *probe_data;
458 /* Allocate probe data and struct efx_nic */
459 probe_data = kzalloc(sizeof(*probe_data), GFP_KERNEL);
462 probe_data->pci_dev = pci_dev;
463 efx = &probe_data->efx;
465 efx->type = (const struct efx_nic_type *)entry->driver_data;
467 efx->pci_dev = pci_dev;
468 pci_set_drvdata(pci_dev, efx);
469 rc = efx_init_struct(efx, pci_dev);
473 efx->vi_stride = EF100_DEFAULT_VI_STRIDE;
474 pci_info(pci_dev, "Solarflare EF100 NIC detected\n");
476 rc = ef100_pci_find_func_ctrl_window(efx, &fcw);
479 "Error looking for ef100 function control window, rc=%d\n",
485 /* Extended capability not found - use defaults. */
486 fcw.bar = EFX_EF100_PCI_DEFAULT_BAR;
491 if (fcw.offset > pci_resource_len(efx->pci_dev, fcw.bar) - ESE_GZ_FCW_LEN) {
492 pci_err(pci_dev, "Func control window overruns BAR\n");
497 /* Set up basic I/O (BAR mappings etc) */
498 rc = efx_init_io(efx, fcw.bar,
499 (dma_addr_t)DMA_BIT_MASK(ESF_GZ_TX_SEND_ADDR_WIDTH),
500 pci_resource_len(efx->pci_dev, fcw.bar));
504 efx->reg_base = fcw.offset;
506 rc = efx->type->probe(efx);
510 efx->state = STATE_PROBED;
511 rc = ef100_probe_netdev(probe_data);
515 pci_dbg(pci_dev, "initialisation successful\n");
520 ef100_pci_remove(pci_dev);
524 #ifdef CONFIG_SFC_SRIOV
525 static int ef100_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
527 struct efx_nic *efx = pci_get_drvdata(dev);
530 if (efx->type->sriov_configure) {
531 rc = efx->type->sriov_configure(efx, num_vfs);
541 /* PCI device ID table */
542 static const struct pci_device_id ef100_pci_table[] = {
543 {PCI_DEVICE(PCI_VENDOR_ID_XILINX, 0x0100), /* Riverhead PF */
544 .driver_data = (unsigned long) &ef100_pf_nic_type },
545 {PCI_DEVICE(PCI_VENDOR_ID_XILINX, 0x1100), /* Riverhead VF */
546 .driver_data = (unsigned long) &ef100_vf_nic_type },
547 {0} /* end of list */
550 struct pci_driver ef100_pci_driver = {
552 .id_table = ef100_pci_table,
553 .probe = ef100_pci_probe,
554 .remove = ef100_pci_remove,
555 #ifdef CONFIG_SFC_SRIOV
556 .sriov_configure = ef100_pci_sriov_configure,
558 .err_handler = &efx_err_handlers,
561 MODULE_DEVICE_TABLE(pci, ef100_pci_table);