Commit f095fefa authored by Kory Maincent's avatar Kory Maincent Committed by Jakub Kicinski

ptp: Move from simple ida to xarray

Move from simple ida to xarray for storing and loading the ptp_clock
pointer. This prepares support for future hardware timestamp selection by
being able to link the ptp clock index to its pointer.
Signed-off-by: default avatarKory Maincent <kory.maincent@bootlin.com>
Reviewed-by: default avatarPrzemek Kitszel <przemyslaw.kitszel@intel.com>
Link: https://lore.kernel.org/r/20240311144730.1239594-1-kory.maincent@bootlin.comSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 195f88c5
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
* *
* Copyright (C) 2010 OMICRON electronics GmbH * Copyright (C) 2010 OMICRON electronics GmbH
*/ */
#include <linux/idr.h>
#include <linux/device.h> #include <linux/device.h>
#include <linux/err.h> #include <linux/err.h>
#include <linux/init.h> #include <linux/init.h>
...@@ -16,6 +15,7 @@ ...@@ -16,6 +15,7 @@
#include <linux/syscalls.h> #include <linux/syscalls.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/debugfs.h> #include <linux/debugfs.h>
#include <linux/xarray.h>
#include <uapi/linux/sched/types.h> #include <uapi/linux/sched/types.h>
#include "ptp_private.h" #include "ptp_private.h"
...@@ -34,7 +34,7 @@ const struct class ptp_class = { ...@@ -34,7 +34,7 @@ const struct class ptp_class = {
static dev_t ptp_devt; static dev_t ptp_devt;
static DEFINE_IDA(ptp_clocks_map); static DEFINE_XARRAY_ALLOC(ptp_clocks_map);
/* time stamp event queue operations */ /* time stamp event queue operations */
...@@ -204,7 +204,7 @@ static void ptp_clock_release(struct device *dev) ...@@ -204,7 +204,7 @@ static void ptp_clock_release(struct device *dev)
bitmap_free(tsevq->mask); bitmap_free(tsevq->mask);
kfree(tsevq); kfree(tsevq);
debugfs_remove(ptp->debugfs_root); debugfs_remove(ptp->debugfs_root);
ida_free(&ptp_clocks_map, ptp->index); xa_erase(&ptp_clocks_map, ptp->index);
kfree(ptp); kfree(ptp);
} }
...@@ -236,7 +236,7 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, ...@@ -236,7 +236,7 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
{ {
struct ptp_clock *ptp; struct ptp_clock *ptp;
struct timestamp_event_queue *queue = NULL; struct timestamp_event_queue *queue = NULL;
int err = 0, index, major = MAJOR(ptp_devt); int err, index, major = MAJOR(ptp_devt);
char debugfsname[16]; char debugfsname[16];
size_t size; size_t size;
...@@ -244,16 +244,16 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, ...@@ -244,16 +244,16 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
/* Initialize a clock structure. */ /* Initialize a clock structure. */
err = -ENOMEM;
ptp = kzalloc(sizeof(struct ptp_clock), GFP_KERNEL); ptp = kzalloc(sizeof(struct ptp_clock), GFP_KERNEL);
if (ptp == NULL) if (!ptp) {
err = -ENOMEM;
goto no_memory; goto no_memory;
}
index = ida_alloc_max(&ptp_clocks_map, MINORMASK, GFP_KERNEL); err = xa_alloc(&ptp_clocks_map, &index, ptp, xa_limit_31b,
if (index < 0) { GFP_KERNEL);
err = index; if (err)
goto no_slot; goto no_slot;
}
ptp->clock.ops = ptp_clock_ops; ptp->clock.ops = ptp_clock_ops;
ptp->info = info; ptp->info = info;
...@@ -261,13 +261,17 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, ...@@ -261,13 +261,17 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
ptp->index = index; ptp->index = index;
INIT_LIST_HEAD(&ptp->tsevqs); INIT_LIST_HEAD(&ptp->tsevqs);
queue = kzalloc(sizeof(*queue), GFP_KERNEL); queue = kzalloc(sizeof(*queue), GFP_KERNEL);
if (!queue) if (!queue) {
err = -ENOMEM;
goto no_memory_queue; goto no_memory_queue;
}
list_add_tail(&queue->qlist, &ptp->tsevqs); list_add_tail(&queue->qlist, &ptp->tsevqs);
spin_lock_init(&ptp->tsevqs_lock); spin_lock_init(&ptp->tsevqs_lock);
queue->mask = bitmap_alloc(PTP_MAX_CHANNELS, GFP_KERNEL); queue->mask = bitmap_alloc(PTP_MAX_CHANNELS, GFP_KERNEL);
if (!queue->mask) if (!queue->mask) {
err = -ENOMEM;
goto no_memory_bitmap; goto no_memory_bitmap;
}
bitmap_set(queue->mask, 0, PTP_MAX_CHANNELS); bitmap_set(queue->mask, 0, PTP_MAX_CHANNELS);
spin_lock_init(&queue->lock); spin_lock_init(&queue->lock);
mutex_init(&ptp->pincfg_mux); mutex_init(&ptp->pincfg_mux);
...@@ -381,7 +385,7 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, ...@@ -381,7 +385,7 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
list_del(&queue->qlist); list_del(&queue->qlist);
kfree(queue); kfree(queue);
no_memory_queue: no_memory_queue:
ida_free(&ptp_clocks_map, index); xa_erase(&ptp_clocks_map, index);
no_slot: no_slot:
kfree(ptp); kfree(ptp);
no_memory: no_memory:
...@@ -514,7 +518,7 @@ static void __exit ptp_exit(void) ...@@ -514,7 +518,7 @@ static void __exit ptp_exit(void)
{ {
class_unregister(&ptp_class); class_unregister(&ptp_class);
unregister_chrdev_region(ptp_devt, MINORMASK + 1); unregister_chrdev_region(ptp_devt, MINORMASK + 1);
ida_destroy(&ptp_clocks_map); xa_destroy(&ptp_clocks_map);
} }
static int __init ptp_init(void) static int __init ptp_init(void)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment