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 "efx_common.h"
15 #include "efx_channels.h"
17 #include "ef100_nic.h"
18 #include "ef100_netdev.h"
19 #include "ef100_sriov.h"
20 #include "ef100_regs.h"
23 #define EFX_EF100_PCI_DEFAULT_BAR 2
25 /* Number of bytes at start of vendor specified extended capability that indicate
26 * that the capability is vendor specified. i.e. offset from value returned by
27 * pci_find_next_ext_capability() to beginning of vendor specified capability
30 #define PCI_EXT_CAP_HDR_LENGTH 4
32 /* Expected size of a Xilinx continuation address table entry. */
33 #define ESE_GZ_CFGBAR_CONT_CAP_MIN_LENGTH 16
35 struct ef100_func_ctl_window
{
41 static int ef100_pci_walk_xilinx_table(struct efx_nic
*efx
, u64 offset
,
42 struct ef100_func_ctl_window
*result
);
44 /* Number of bytes to offset when reading bit position x with dword accessors. */
45 #define ROUND_DOWN_TO_DWORD(x) (((x) & (~31)) >> 3)
47 #define EXTRACT_BITS(x, lbn, width) \
48 (((x) >> ((lbn) & 31)) & ((1ull << (width)) - 1))
50 static u32
_ef100_pci_get_bar_bits_with_width(struct efx_nic
*efx
,
56 efx_readd(efx
, &dword
, structure_start
+ ROUND_DOWN_TO_DWORD(lbn
));
58 return EXTRACT_BITS(le32_to_cpu(dword
.u32
[0]), lbn
, width
);
61 #define ef100_pci_get_bar_bits(efx, entry_location, bitdef) \
62 _ef100_pci_get_bar_bits_with_width(efx, entry_location, \
63 ESF_GZ_CFGBAR_ ## bitdef ## _LBN, \
64 ESF_GZ_CFGBAR_ ## bitdef ## _WIDTH)
66 static int ef100_pci_parse_ef100_entry(struct efx_nic
*efx
, int entry_location
,
67 struct ef100_func_ctl_window
*result
)
69 u64 offset
= ef100_pci_get_bar_bits(efx
, entry_location
, EF100_FUNC_CTL_WIN_OFF
) <<
70 ESE_GZ_EF100_FUNC_CTL_WIN_OFF_SHIFT
;
71 u32 bar
= ef100_pci_get_bar_bits(efx
, entry_location
, EF100_BAR
);
73 netif_dbg(efx
, probe
, efx
->net_dev
,
74 "Found EF100 function control window bar=%d offset=0x%llx\n",
78 netif_err(efx
, probe
, efx
->net_dev
,
79 "Duplicated EF100 table entry.\n");
83 if (bar
== ESE_GZ_CFGBAR_EF100_BAR_NUM_EXPANSION_ROM
||
84 bar
== ESE_GZ_CFGBAR_EF100_BAR_NUM_INVALID
) {
85 netif_err(efx
, probe
, efx
->net_dev
,
86 "Bad BAR value of %d in Xilinx capabilities EF100 entry.\n",
92 result
->offset
= offset
;
97 static bool ef100_pci_does_bar_overflow(struct efx_nic
*efx
, int bar
,
100 return next_entry
+ ESE_GZ_CFGBAR_ENTRY_HEADER_SIZE
>
101 pci_resource_len(efx
->pci_dev
, bar
);
104 /* Parse a Xilinx capabilities table entry describing a continuation to a new
107 static int ef100_pci_parse_continue_entry(struct efx_nic
*efx
, int entry_location
,
108 struct ef100_func_ctl_window
*result
)
110 unsigned int previous_bar
;
116 efx_reado(efx
, &entry
, entry_location
);
118 bar
= EFX_OWORD_FIELD32(entry
, ESF_GZ_CFGBAR_CONT_CAP_BAR
);
120 offset
= EFX_OWORD_FIELD64(entry
, ESF_GZ_CFGBAR_CONT_CAP_OFFSET
) <<
121 ESE_GZ_CONT_CAP_OFFSET_BYTES_SHIFT
;
123 previous_bar
= efx
->mem_bar
;
125 if (bar
== ESE_GZ_VSEC_BAR_NUM_EXPANSION_ROM
||
126 bar
== ESE_GZ_VSEC_BAR_NUM_INVALID
) {
127 netif_err(efx
, probe
, efx
->net_dev
,
128 "Bad BAR value of %d in Xilinx capabilities sub-table.\n",
133 if (bar
!= previous_bar
) {
136 if (ef100_pci_does_bar_overflow(efx
, bar
, offset
)) {
137 netif_err(efx
, probe
, efx
->net_dev
,
138 "Xilinx table will overrun BAR[%d] offset=0x%llx\n",
143 /* Temporarily map new BAR. */
144 rc
= efx_init_io(efx
, bar
,
145 (dma_addr_t
)DMA_BIT_MASK(ESF_GZ_TX_SEND_ADDR_WIDTH
),
146 pci_resource_len(efx
->pci_dev
, bar
));
148 netif_err(efx
, probe
, efx
->net_dev
,
149 "Mapping new BAR for Xilinx table failed, rc=%d\n", rc
);
154 rc
= ef100_pci_walk_xilinx_table(efx
, offset
, result
);
158 if (bar
!= previous_bar
) {
161 /* Put old BAR back. */
162 rc
= efx_init_io(efx
, previous_bar
,
163 (dma_addr_t
)DMA_BIT_MASK(ESF_GZ_TX_SEND_ADDR_WIDTH
),
164 pci_resource_len(efx
->pci_dev
, previous_bar
));
166 netif_err(efx
, probe
, efx
->net_dev
,
167 "Putting old BAR back failed, rc=%d\n", rc
);
175 /* Iterate over the Xilinx capabilities table in the currently mapped BAR and
176 * call ef100_pci_parse_ef100_entry() on any EF100 entries and
177 * ef100_pci_parse_continue_entry() on any table continuations.
179 static int ef100_pci_walk_xilinx_table(struct efx_nic
*efx
, u64 offset
,
180 struct ef100_func_ctl_window
*result
)
182 u64 current_entry
= offset
;
186 u32 id
= ef100_pci_get_bar_bits(efx
, current_entry
, ENTRY_FORMAT
);
187 u32 last
= ef100_pci_get_bar_bits(efx
, current_entry
, ENTRY_LAST
);
188 u32 rev
= ef100_pci_get_bar_bits(efx
, current_entry
, ENTRY_REV
);
191 if (id
== ESE_GZ_CFGBAR_ENTRY_LAST
)
194 entry_size
= ef100_pci_get_bar_bits(efx
, current_entry
, ENTRY_SIZE
);
196 netif_dbg(efx
, probe
, efx
->net_dev
,
197 "Seen Xilinx table entry 0x%x size 0x%x at 0x%llx in BAR[%d]\n",
198 id
, entry_size
, current_entry
, efx
->mem_bar
);
200 if (entry_size
< sizeof(u32
) * 2) {
201 netif_err(efx
, probe
, efx
->net_dev
,
202 "Xilinx table entry too short len=0x%x\n", entry_size
);
207 case ESE_GZ_CFGBAR_ENTRY_EF100
:
208 if (rev
!= ESE_GZ_CFGBAR_ENTRY_REV_EF100
||
209 entry_size
< ESE_GZ_CFGBAR_ENTRY_SIZE_EF100
) {
210 netif_err(efx
, probe
, efx
->net_dev
,
211 "Bad length or rev for EF100 entry in Xilinx capabilities table. entry_size=%d rev=%d.\n",
216 rc
= ef100_pci_parse_ef100_entry(efx
, current_entry
,
221 case ESE_GZ_CFGBAR_ENTRY_CONT_CAP_ADDR
:
222 if (rev
!= 0 || entry_size
< ESE_GZ_CFGBAR_CONT_CAP_MIN_LENGTH
) {
223 netif_err(efx
, probe
, efx
->net_dev
,
224 "Bad length or rev for continue entry in Xilinx capabilities table. entry_size=%d rev=%d.\n",
229 rc
= ef100_pci_parse_continue_entry(efx
, current_entry
, result
);
234 /* Ignore unknown table entries. */
241 current_entry
+= entry_size
;
243 if (ef100_pci_does_bar_overflow(efx
, efx
->mem_bar
, current_entry
)) {
244 netif_err(efx
, probe
, efx
->net_dev
,
245 "Xilinx table overrun at position=0x%llx.\n",
252 static int _ef100_pci_get_config_bits_with_width(struct efx_nic
*efx
,
253 int structure_start
, int lbn
,
254 int width
, u32
*result
)
256 int rc
, pos
= structure_start
+ ROUND_DOWN_TO_DWORD(lbn
);
259 rc
= pci_read_config_dword(efx
->pci_dev
, pos
, &temp
);
261 netif_err(efx
, probe
, efx
->net_dev
,
262 "Failed to read PCI config dword at %d\n",
267 *result
= EXTRACT_BITS(temp
, lbn
, width
);
272 #define ef100_pci_get_config_bits(efx, entry_location, bitdef, result) \
273 _ef100_pci_get_config_bits_with_width(efx, entry_location, \
274 ESF_GZ_VSEC_ ## bitdef ## _LBN, \
275 ESF_GZ_VSEC_ ## bitdef ## _WIDTH, result)
277 /* Call ef100_pci_walk_xilinx_table() for the Xilinx capabilities table pointed
278 * to by this PCI_EXT_CAP_ID_VNDR.
280 static int ef100_pci_parse_xilinx_cap(struct efx_nic
*efx
, int vndr_cap
,
282 struct ef100_func_ctl_window
*result
)
290 rc
= ef100_pci_get_config_bits(efx
, vndr_cap
, TBL_BAR
, &bar
);
292 netif_err(efx
, probe
, efx
->net_dev
,
293 "Failed to read ESF_GZ_VSEC_TBL_BAR, rc=%d\n",
298 if (bar
== ESE_GZ_CFGBAR_CONT_CAP_BAR_NUM_EXPANSION_ROM
||
299 bar
== ESE_GZ_CFGBAR_CONT_CAP_BAR_NUM_INVALID
) {
300 netif_err(efx
, probe
, efx
->net_dev
,
301 "Bad BAR value of %d in Xilinx capabilities sub-table.\n",
306 rc
= ef100_pci_get_config_bits(efx
, vndr_cap
, TBL_OFF_LO
, &offset_lo
);
308 netif_err(efx
, probe
, efx
->net_dev
,
309 "Failed to read ESF_GZ_VSEC_TBL_OFF_LO, rc=%d\n",
314 /* Get optional extension to 64bit offset. */
316 rc
= ef100_pci_get_config_bits(efx
, vndr_cap
, TBL_OFF_HI
, &offset_high
);
318 netif_err(efx
, probe
, efx
->net_dev
,
319 "Failed to read ESF_GZ_VSEC_TBL_OFF_HI, rc=%d\n",
325 offset
= (((u64
)offset_lo
) << ESE_GZ_VSEC_TBL_OFF_LO_BYTES_SHIFT
) |
326 (((u64
)offset_high
) << ESE_GZ_VSEC_TBL_OFF_HI_BYTES_SHIFT
);
328 if (offset
> pci_resource_len(efx
->pci_dev
, bar
) - sizeof(u32
) * 2) {
329 netif_err(efx
, probe
, efx
->net_dev
,
330 "Xilinx table will overrun BAR[%d] offset=0x%llx\n",
335 /* Temporarily map BAR. */
336 rc
= efx_init_io(efx
, bar
,
337 (dma_addr_t
)DMA_BIT_MASK(ESF_GZ_TX_SEND_ADDR_WIDTH
),
338 pci_resource_len(efx
->pci_dev
, bar
));
340 netif_err(efx
, probe
, efx
->net_dev
,
341 "efx_init_io failed, rc=%d\n", rc
);
345 rc
= ef100_pci_walk_xilinx_table(efx
, offset
, result
);
347 /* Unmap temporarily mapped BAR. */
352 /* Call ef100_pci_parse_ef100_entry() for each Xilinx PCI_EXT_CAP_ID_VNDR
355 static int ef100_pci_find_func_ctrl_window(struct efx_nic
*efx
,
356 struct ef100_func_ctl_window
*result
)
358 int num_xilinx_caps
= 0;
361 result
->valid
= false;
363 while ((cap
= pci_find_next_ext_capability(efx
->pci_dev
, cap
, PCI_EXT_CAP_ID_VNDR
)) != 0) {
364 int vndr_cap
= cap
+ PCI_EXT_CAP_HDR_LENGTH
;
372 rc
= ef100_pci_get_config_bits(efx
, vndr_cap
, ID
, &vsec_id
);
374 netif_err(efx
, probe
, efx
->net_dev
,
375 "Failed to read ESF_GZ_VSEC_ID, rc=%d\n",
380 rc
= ef100_pci_get_config_bits(efx
, vndr_cap
, VER
, &vsec_ver
);
382 netif_err(efx
, probe
, efx
->net_dev
,
383 "Failed to read ESF_GZ_VSEC_VER, rc=%d\n",
388 /* Get length of whole capability - i.e. starting at cap */
389 rc
= ef100_pci_get_config_bits(efx
, vndr_cap
, LEN
, &vsec_len
);
391 netif_err(efx
, probe
, efx
->net_dev
,
392 "Failed to read ESF_GZ_VSEC_LEN, rc=%d\n",
397 if (vsec_id
== ESE_GZ_XILINX_VSEC_ID
&&
398 vsec_ver
== ESE_GZ_VSEC_VER_XIL_CFGBAR
&&
399 vsec_len
>= ESE_GZ_VSEC_LEN_MIN
) {
400 bool has_offset_hi
= (vsec_len
>= ESE_GZ_VSEC_LEN_HIGH_OFFT
);
402 rc
= ef100_pci_parse_xilinx_cap(efx
, vndr_cap
,
403 has_offset_hi
, result
);
409 if (num_xilinx_caps
&& !result
->valid
) {
410 netif_err(efx
, probe
, efx
->net_dev
,
411 "Seen %d Xilinx tables, but no EF100 entry.\n",
419 /* Final NIC shutdown
420 * This is called only at module unload (or hotplug removal). A PF can call
421 * this on its VFs to ensure they are unbound first.
423 static void ef100_pci_remove(struct pci_dev
*pci_dev
)
425 struct efx_nic
*efx
= pci_get_drvdata(pci_dev
);
426 struct efx_probe_data
*probe_data
;
431 probe_data
= container_of(efx
, struct efx_probe_data
, efx
);
432 ef100_remove_netdev(probe_data
);
433 #ifdef CONFIG_SFC_SRIOV
434 efx_fini_struct_tc(efx
);
440 pci_dbg(pci_dev
, "shutdown successful\n");
442 pci_set_drvdata(pci_dev
, NULL
);
443 efx_fini_struct(efx
);
447 static int ef100_pci_probe(struct pci_dev
*pci_dev
,
448 const struct pci_device_id
*entry
)
450 struct ef100_func_ctl_window fcw
= { 0 };
451 struct efx_probe_data
*probe_data
;
455 /* Allocate probe data and struct efx_nic */
456 probe_data
= kzalloc(sizeof(*probe_data
), GFP_KERNEL
);
459 probe_data
->pci_dev
= pci_dev
;
460 efx
= &probe_data
->efx
;
462 efx
->type
= (const struct efx_nic_type
*)entry
->driver_data
;
464 efx
->pci_dev
= pci_dev
;
465 pci_set_drvdata(pci_dev
, efx
);
466 rc
= efx_init_struct(efx
, pci_dev
);
470 efx
->vi_stride
= EF100_DEFAULT_VI_STRIDE
;
471 pci_info(pci_dev
, "Solarflare EF100 NIC detected\n");
473 rc
= ef100_pci_find_func_ctrl_window(efx
, &fcw
);
476 "Error looking for ef100 function control window, rc=%d\n",
482 /* Extended capability not found - use defaults. */
483 fcw
.bar
= EFX_EF100_PCI_DEFAULT_BAR
;
488 if (fcw
.offset
> pci_resource_len(efx
->pci_dev
, fcw
.bar
) - ESE_GZ_FCW_LEN
) {
489 pci_err(pci_dev
, "Func control window overruns BAR\n");
494 /* Set up basic I/O (BAR mappings etc) */
495 rc
= efx_init_io(efx
, fcw
.bar
,
496 (dma_addr_t
)DMA_BIT_MASK(ESF_GZ_TX_SEND_ADDR_WIDTH
),
497 pci_resource_len(efx
->pci_dev
, fcw
.bar
));
501 efx
->reg_base
= fcw
.offset
;
503 rc
= efx
->type
->probe(efx
);
507 efx
->state
= STATE_PROBED
;
508 rc
= ef100_probe_netdev(probe_data
);
512 pci_dbg(pci_dev
, "initialisation successful\n");
517 ef100_pci_remove(pci_dev
);
521 #ifdef CONFIG_SFC_SRIOV
522 static int ef100_pci_sriov_configure(struct pci_dev
*dev
, int num_vfs
)
524 struct efx_nic
*efx
= pci_get_drvdata(dev
);
527 if (efx
->type
->sriov_configure
) {
528 rc
= efx
->type
->sriov_configure(efx
, num_vfs
);
538 /* PCI device ID table */
539 static const struct pci_device_id ef100_pci_table
[] = {
540 {PCI_DEVICE(PCI_VENDOR_ID_XILINX
, 0x0100), /* Riverhead PF */
541 .driver_data
= (unsigned long) &ef100_pf_nic_type
},
542 {PCI_DEVICE(PCI_VENDOR_ID_XILINX
, 0x1100), /* Riverhead VF */
543 .driver_data
= (unsigned long) &ef100_vf_nic_type
},
544 {0} /* end of list */
547 struct pci_driver ef100_pci_driver
= {
549 .id_table
= ef100_pci_table
,
550 .probe
= ef100_pci_probe
,
551 .remove
= ef100_pci_remove
,
552 #ifdef CONFIG_SFC_SRIOV
553 .sriov_configure
= ef100_pci_sriov_configure
,
555 .err_handler
= &efx_err_handlers
,
558 MODULE_DEVICE_TABLE(pci
, ef100_pci_table
);