Skip to content

Commit 44c494c

Browse files
yangbolu1991davem330
authored andcommitted
ptp: track available ptp vclocks information
Track available ptp vclocks information. Record index values of available ptp vclocks during registering and unregistering. This is preparation for supporting ptp vclocks info query through ethtool. Signed-off-by: Yangbo Lu <yangbo.lu@nxp.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 73f3706 commit 44c494c

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

drivers/ptp/ptp_clock.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
196196
{
197197
struct ptp_clock *ptp;
198198
int err = 0, index, major = MAJOR(ptp_devt);
199+
size_t size;
199200

200201
if (info->n_alarm > PTP_MAX_ALARMS)
201202
return ERR_PTR(-EINVAL);
@@ -238,9 +239,17 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
238239
strcmp(parent->class->name, "ptp") == 0)
239240
ptp->is_virtual_clock = true;
240241

241-
if (!ptp->is_virtual_clock)
242+
if (!ptp->is_virtual_clock) {
242243
ptp->max_vclocks = PTP_DEFAULT_MAX_VCLOCKS;
243244

245+
size = sizeof(int) * ptp->max_vclocks;
246+
ptp->vclock_index = kzalloc(size, GFP_KERNEL);
247+
if (!ptp->vclock_index) {
248+
err = -ENOMEM;
249+
goto no_mem_for_vclocks;
250+
}
251+
}
252+
244253
err = ptp_populate_pin_groups(ptp);
245254
if (err)
246255
goto no_pin_groups;
@@ -285,6 +294,8 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
285294
no_pps:
286295
ptp_cleanup_pin_groups(ptp);
287296
no_pin_groups:
297+
kfree(ptp->vclock_index);
298+
no_mem_for_vclocks:
288299
if (ptp->kworker)
289300
kthread_destroy_worker(ptp->kworker);
290301
kworker_err:
@@ -309,6 +320,8 @@ int ptp_clock_unregister(struct ptp_clock *ptp)
309320
ptp->defunct = 1;
310321
wake_up_interruptible(&ptp->tsev_wq);
311322

323+
kfree(ptp->vclock_index);
324+
312325
if (ptp->kworker) {
313326
kthread_cancel_delayed_work_sync(&ptp->aux_work);
314327
kthread_destroy_worker(ptp->kworker);

drivers/ptp/ptp_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct ptp_clock {
4949
struct kthread_delayed_work aux_work;
5050
unsigned int max_vclocks;
5151
unsigned int n_vclocks;
52+
int *vclock_index;
5253
struct mutex n_vclocks_mux; /* protect concurrent n_vclocks access */
5354
bool is_virtual_clock;
5455
};

drivers/ptp/ptp_sysfs.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ static ssize_t n_vclocks_store(struct device *dev,
213213
if (!vclock)
214214
goto out;
215215

216+
*(ptp->vclock_index + ptp->n_vclocks + i) =
217+
vclock->clock->index;
218+
216219
dev_info(dev, "new virtual clock ptp%d\n",
217220
vclock->clock->index);
218221
}
@@ -223,6 +226,9 @@ static ssize_t n_vclocks_store(struct device *dev,
223226
i = ptp->n_vclocks - num;
224227
device_for_each_child_reverse(dev, &i,
225228
unregister_vclock);
229+
230+
for (i = 1; i <= ptp->n_vclocks - num; i++)
231+
*(ptp->vclock_index + ptp->n_vclocks - i) = -1;
226232
}
227233

228234
if (num == 0)
@@ -256,6 +262,9 @@ static ssize_t max_vclocks_store(struct device *dev,
256262
const char *buf, size_t count)
257263
{
258264
struct ptp_clock *ptp = dev_get_drvdata(dev);
265+
unsigned int *vclock_index;
266+
int err = -EINVAL;
267+
size_t size;
259268
u32 max;
260269

261270
if (kstrtou32(buf, 0, &max) || max == 0)
@@ -267,16 +276,29 @@ static ssize_t max_vclocks_store(struct device *dev,
267276
if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
268277
return -ERESTARTSYS;
269278

270-
if (max < ptp->n_vclocks) {
271-
mutex_unlock(&ptp->n_vclocks_mux);
272-
return -EINVAL;
279+
if (max < ptp->n_vclocks)
280+
goto out;
281+
282+
size = sizeof(int) * max;
283+
vclock_index = kzalloc(size, GFP_KERNEL);
284+
if (!vclock_index) {
285+
err = -ENOMEM;
286+
goto out;
273287
}
274288

289+
size = sizeof(int) * ptp->n_vclocks;
290+
memcpy(vclock_index, ptp->vclock_index, size);
291+
292+
kfree(ptp->vclock_index);
293+
ptp->vclock_index = vclock_index;
275294
ptp->max_vclocks = max;
276295

277296
mutex_unlock(&ptp->n_vclocks_mux);
278297

279298
return count;
299+
out:
300+
mutex_unlock(&ptp->n_vclocks_mux);
301+
return err;
280302
}
281303
static DEVICE_ATTR_RW(max_vclocks);
282304

0 commit comments

Comments
 (0)