Skip to content

Commit 3e3e9fd

Browse files
Jakub Kicinskidavem330
authored andcommitted
nfp: reset local locks on init
NFP locks record the owner when held, for PCIe devices the owner ID will be the PCIe link number. When driver loads it should scan known locks and if they indicate that they are held by local endpoint but the driver doesn't hold them - release them. Locks can be left taken for instance when kernel gets kexec-ed or after a crash. Management FW tries to clean up stale locks too, but it currently depends on PCIe link going down which doesn't always happen. Signed-off-by: Jakub Kicinski <[email protected]> Reviewed-by: Dirk van der Merwe <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent c77bbc6 commit 3e3e9fd

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

drivers/net/ethernet/netronome/nfp/nfp_main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,10 @@ static int nfp_pci_probe(struct pci_dev *pdev,
486486
goto err_disable_msix;
487487
}
488488

489+
err = nfp_resource_table_init(pf->cpp);
490+
if (err)
491+
goto err_cpp_free;
492+
489493
pf->hwinfo = nfp_hwinfo_read(pf->cpp);
490494

491495
dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
@@ -548,6 +552,7 @@ static int nfp_pci_probe(struct pci_dev *pdev,
548552
vfree(pf->dumpspec);
549553
err_hwinfo_free:
550554
kfree(pf->hwinfo);
555+
err_cpp_free:
551556
nfp_cpp_free(pf->cpp);
552557
err_disable_msix:
553558
destroy_workqueue(pf->wq);

drivers/net/ethernet/netronome/nfp/nfpcore/nfp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ int nfp_nsp_read_sensors(struct nfp_nsp *state, unsigned int sensor_mask,
9494
/* MAC Statistics Accumulator */
9595
#define NFP_RESOURCE_MAC_STATISTICS "mac.stat"
9696

97+
int nfp_resource_table_init(struct nfp_cpp *cpp);
98+
9799
struct nfp_resource *
98100
nfp_resource_acquire(struct nfp_cpp *cpp, const char *name);
99101

drivers/net/ethernet/netronome/nfp/nfpcore/nfp_cpp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ void nfp_cpp_mutex_free(struct nfp_cpp_mutex *mutex);
295295
int nfp_cpp_mutex_lock(struct nfp_cpp_mutex *mutex);
296296
int nfp_cpp_mutex_unlock(struct nfp_cpp_mutex *mutex);
297297
int nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex);
298+
int nfp_cpp_mutex_reclaim(struct nfp_cpp *cpp, int target,
299+
unsigned long long address);
298300

299301
/**
300302
* nfp_cppcore_pcie_unit() - Get PCI Unit of a CPP handle

drivers/net/ethernet/netronome/nfp/nfpcore/nfp_mutex.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ static u32 nfp_mutex_unlocked(u16 interface)
5959
return (u32)interface << 16 | 0x0000;
6060
}
6161

62+
static u32 nfp_mutex_owner(u32 val)
63+
{
64+
return val >> 16;
65+
}
66+
6267
static bool nfp_mutex_is_locked(u32 val)
6368
{
6469
return (val & 0xffff) == 0x000f;
@@ -351,3 +356,43 @@ int nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex)
351356

352357
return nfp_mutex_is_locked(tmp) ? -EBUSY : -EINVAL;
353358
}
359+
360+
/**
361+
* nfp_cpp_mutex_reclaim() - Unlock mutex if held by local endpoint
362+
* @cpp: NFP CPP handle
363+
* @target: NFP CPP target ID (ie NFP_CPP_TARGET_CLS or NFP_CPP_TARGET_MU)
364+
* @address: Offset into the address space of the NFP CPP target ID
365+
*
366+
* Release lock if held by local system. Extreme care is advised, call only
367+
* when no local lock users can exist.
368+
*
369+
* Return: 0 if the lock was OK, 1 if locked by us, -errno on invalid mutex
370+
*/
371+
int nfp_cpp_mutex_reclaim(struct nfp_cpp *cpp, int target,
372+
unsigned long long address)
373+
{
374+
const u32 mur = NFP_CPP_ID(target, 3, 0); /* atomic_read */
375+
const u32 muw = NFP_CPP_ID(target, 4, 0); /* atomic_write */
376+
u16 interface = nfp_cpp_interface(cpp);
377+
int err;
378+
u32 tmp;
379+
380+
err = nfp_cpp_mutex_validate(interface, &target, address);
381+
if (err)
382+
return err;
383+
384+
/* Check lock */
385+
err = nfp_cpp_readl(cpp, mur, address, &tmp);
386+
if (err < 0)
387+
return err;
388+
389+
if (nfp_mutex_is_unlocked(tmp) || nfp_mutex_owner(tmp) != interface)
390+
return 0;
391+
392+
/* Bust the lock */
393+
err = nfp_cpp_writel(cpp, muw, address, nfp_mutex_unlocked(interface));
394+
if (err < 0)
395+
return err;
396+
397+
return 1;
398+
}

drivers/net/ethernet/netronome/nfp/nfpcore/nfp_resource.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,62 @@ u64 nfp_resource_size(struct nfp_resource *res)
338338
{
339339
return res->size;
340340
}
341+
342+
/**
343+
* nfp_resource_table_init() - Run initial checks on the resource table
344+
* @cpp: NFP CPP handle
345+
*
346+
* Start-of-day init procedure for resource table. Must be called before
347+
* any local resource table users may exist.
348+
*
349+
* Return: 0 on success, -errno on failure
350+
*/
351+
int nfp_resource_table_init(struct nfp_cpp *cpp)
352+
{
353+
struct nfp_cpp_mutex *dev_mutex;
354+
int i, err;
355+
356+
err = nfp_cpp_mutex_reclaim(cpp, NFP_RESOURCE_TBL_TARGET,
357+
NFP_RESOURCE_TBL_BASE);
358+
if (err < 0) {
359+
nfp_err(cpp, "Error: failed to reclaim resource table mutex\n");
360+
return err;
361+
}
362+
if (err)
363+
nfp_warn(cpp, "Warning: busted main resource table mutex\n");
364+
365+
dev_mutex = nfp_cpp_mutex_alloc(cpp, NFP_RESOURCE_TBL_TARGET,
366+
NFP_RESOURCE_TBL_BASE,
367+
NFP_RESOURCE_TBL_KEY);
368+
if (!dev_mutex)
369+
return -ENOMEM;
370+
371+
if (nfp_cpp_mutex_lock(dev_mutex)) {
372+
nfp_err(cpp, "Error: failed to claim resource table mutex\n");
373+
nfp_cpp_mutex_free(dev_mutex);
374+
return -EINVAL;
375+
}
376+
377+
/* Resource 0 is the dev_mutex, start from 1 */
378+
for (i = 1; i < NFP_RESOURCE_TBL_ENTRIES; i++) {
379+
u64 addr = NFP_RESOURCE_TBL_BASE +
380+
sizeof(struct nfp_resource_entry) * i;
381+
382+
err = nfp_cpp_mutex_reclaim(cpp, NFP_RESOURCE_TBL_TARGET, addr);
383+
if (err < 0) {
384+
nfp_err(cpp,
385+
"Error: failed to reclaim resource %d mutex\n",
386+
i);
387+
goto err_unlock;
388+
}
389+
if (err)
390+
nfp_warn(cpp, "Warning: busted resource %d mutex\n", i);
391+
}
392+
393+
err = 0;
394+
err_unlock:
395+
nfp_cpp_mutex_unlock(dev_mutex);
396+
nfp_cpp_mutex_free(dev_mutex);
397+
398+
return err;
399+
}

0 commit comments

Comments
 (0)