ptp: ocp: A fix and refactoring
cover ptp: ocp: A fix and refactoring
Here is the fix for incorrect use of %ptT with the associated refactoring and additional cleanups.
Note, %ptS, which is introduced in another series, doesn't fit here, that's why this fix is separated from that series.
Andy Shevchenko (7):
ptp: ocp: Refactor signal_show() and fix %ptT misuse
ptp: ocp: Make ptp_ocp_unregister_ext() NULL-aware
ptp: ocp: Refactor ptp_ocp_i2c_notifier_call()
ptp: ocp: Apply standard pattern for cleaning up loop
ptp: ocp: Reuse META's PCI vendor ID
ptp: ocp: Sort headers alphabetically
ptp: ocp: don't use "proxy" headers
drivers/ptp/ptp_ocp.c | 128 +++++++++++++++++++++++------------------- 1 file changed, 70 insertions(+), 58 deletions(-)
Signature · 1 line
2.50.1
PATCH 4/7 ptp: ocp: Apply standard pattern for cleaning up loop
The while (i--) is a standard pattern for the cleaning up loops. Apply this pattern where it makes sense in the driver.
--- drivers/ptp/ptp_ocp.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/ptp/ptp_ocp.c +1 -2
@@ -4821,8 +4821,7 @@ ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)return 0 ; out_dpll: -while (i) { - --i; +while (i--) {dpll_pin_unregister (bp->dpll, bp->sma[i].dpll_pin, &dpll_pins_ops, &bp->sma[i]);dpll_pin_put (bp->sma[i].dpll_pin); }
Signature · 1 line
2.50.1
PATCH 1/7 ptp: ocp: Refactor signal_show() and fix %ptT misuse
In reply to Andy ShevchenkoRefactor signal_show() to avoid sequential calls to sysfs_emit*() and use the same pattern to get the index of a signal as it's done in signal_store().
While at it, fix wrong use of %ptT against struct timespec64. It's kinda lucky that it worked just because the first member there 64-bit and it's of time64_t type. Now with %ptS it may be used correctly.
--- drivers/ptp/ptp_ocp.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/ptp/ptp_ocp.c +5 -9
@@ -3250,20 +3250,16 @@ signal_show(struct device *dev, struct device_attribute *attr, char *buf)struct dev_ext_attribute *ea =to_ext_attr (attr);struct ptp_ocp *bp =dev_get_drvdata (dev);struct ptp_ocp_signal *signal; +int gen = (uintptr_t )ea->var;struct timespec64 ts; -ssize_t count; -int i; - i = (uintptr_t )ea->var; - signal = &bp->signal[i]; - - count =sysfs_emit (buf,"%llu %d %llu %d" , signal->period, - signal->duty, signal->phase, signal->polarity); + signal = &bp->signal[gen]; ts =ktime_to_timespec64 (signal->start); - count +=sysfs_emit_at (buf, count," %ptT TAI\n" , &ts); -return count; +return sysfs_emit (buf,"%llu %d %llu %d %ptT TAI\n" , + signal->period, signal->duty, signal->phase, signal->polarity, + &ts.tv_sec); } static EXT_ATTR_RW(signal, signal, 0); static EXT_ATTR_RW(signal, signal, 1);
Signature · 1 line
2.50.1
PATCH 3/7 ptp: ocp: Refactor ptp_ocp_i2c_notifier_call()
In reply to Andy ShevchenkoRefactor ptp_ocp_i2c_notifier_call() to avoid unneeded local variable.
--- drivers/ptp/ptp_ocp.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-)
drivers/ptp/ptp_ocp.c +9 -12
@@ -4872,16 +4872,6 @@ ptp_ocp_i2c_notifier_call(struct notifier_block *nb, {struct device *dev, *child = data;struct ptp_ocp *bp; -bool add; - -switch (action) { -case BUS_NOTIFY_ADD_DEVICE : -case BUS_NOTIFY_DEL_DEVICE : - add = action ==BUS_NOTIFY_ADD_DEVICE ; -break ; -default : -return 0 ; - }if (!i2c_verify_adapter (child))return 0 ;
@@ -4894,10 +4884,17 @@ ptp_ocp_i2c_notifier_call(struct notifier_block *nb, found: bp =dev_get_drvdata (dev); -if (add) + +switch (action) { +case BUS_NOTIFY_ADD_DEVICE :ptp_ocp_symlink (bp, child,"i2c" ); -else +break ; +case BUS_NOTIFY_DEL_DEVICE :sysfs_remove_link (&bp->dev.kobj,"i2c" ); +break ; +default : +return 0 ; + }return 0 ; }
Signature · 1 line
2.50.1
PATCH 5/7 ptp: ocp: Reuse META's PCI vendor ID
In reply to Andy ShevchenkoThe META's PCI vendor ID is listed already in the pci_ids.h. Reuse it here.
--- drivers/ptp/ptp_ocp.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/ptp/ptp_ocp.c +2 -3
@@ -25,8 +25,7 @@#include <linux/crc16.h> #include <linux/dpll.h> -#define PCI_VENDOR_ID_FACEBOOK 0x1d9b -#define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400 +#define PCI_DEVICE_ID_META_TIMECARD 0x0400#define PCI_VENDOR_ID_CELESTICA 0x18d4#define PCI_DEVICE_ID_CELESTICA_TIMECARD 0x1008
@@ -1030,7 +1029,7 @@ static struct ocp_resource ocp_adva_resource[] = { };static const struct pci_device_id ptp_ocp_pcidev_id[] = { - {PCI_DEVICE_DATA (TIMECARD , &ocp_fb_resource) }, + {PCI_DEVICE_DATA (META ,TIMECARD , &ocp_fb_resource) }, {PCI_DEVICE_DATA (CELESTICA ,TIMECARD , &ocp_fb_resource) }, {PCI_DEVICE_DATA (OROLIA ,ARTCARD , &ocp_art_resource) }, {PCI_DEVICE_DATA (ADVA ,TIMECARD , &ocp_adva_resource) },
Signature · 1 line
2.50.1
PATCH 6/7 ptp: ocp: Sort headers alphabetically
In reply to Andy ShevchenkoFor better maintenance sort headers alphabetically.
--- drivers/ptp/ptp_ocp.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
drivers/ptp/ptp_ocp.c +17 -17
@@ -2,28 +2,28 @@/* Copyright (c) 2020 Facebook */ #include <linux/bits.h> +#include <linux/clk-provider.h> +#include <linux/clkdev.h> +#include <linux/crc16.h> +#include <linux/debugfs.h> +#include <linux/dpll.h> #include <linux/err.h> +#include <linux/i2c.h> +#include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h> -#include <linux/debugfs.h> -#include <linux/init.h> -#include <linux/pci.h> -#include <linux/serial_8250.h> -#include <linux/clkdev.h> -#include <linux/clk-provider.h> -#include <linux/platform_device.h> -#include <linux/platform_data/i2c-xiic.h> -#include <linux/platform_data/i2c-ocores.h> -#include <linux/ptp_clock_kernel.h> -#include <linux/spi/spi.h> -#include <linux/spi/xilinx_spi.h> -#include <linux/spi/altera.h> -#include <net/devlink.h> -#include <linux/i2c.h> #include <linux/mtd/mtd.h> #include <linux/nvmem-consumer.h> -#include <linux/crc16.h> -#include <linux/dpll.h> +#include <linux/pci.h> +#include <linux/platform_data/i2c-ocores.h> +#include <linux/platform_data/i2c-xiic.h> +#include <linux/platform_device.h> +#include <linux/ptp_clock_kernel.h> +#include <linux/serial_8250.h> +#include <linux/spi/altera.h> +#include <linux/spi/spi.h> +#include <linux/spi/xilinx_spi.h> +#include <net/devlink.h> #define PCI_DEVICE_ID_META_TIMECARD 0x0400
Signature · 1 line
2.50.1
PATCH 2/7 ptp: ocp: Make ptp_ocp_unregister_ext() NULL-aware
In reply to Andy ShevchenkoIt's a common practice to make resource release functions be NULL-aware. Make ptp_ocp_unregister_ext() NULL-aware.
--- drivers/ptp/ptp_ocp.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-)
drivers/ptp/ptp_ocp.c +10 -14
@@ -2225,6 +2225,9 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)static void ptp_ocp_unregister_ext (struct ptp_ocp_ext_src *ext) { +if (!ext) +return ; + ext->info->enable (ext, ~0 , false);pci_free_irq (ext->bp->pdev, ext->irq_vec, ext);kfree (ext);
@@ -4558,21 +4561,14 @@ ptp_ocp_detach(struct ptp_ocp *bp)ptp_ocp_detach_sysfs (bp);ptp_ocp_attr_group_del (bp);timer_delete_sync (&bp->watchdog); -if (bp->ts0) -ptp_ocp_unregister_ext (bp->ts0); -if (bp->ts1) -ptp_ocp_unregister_ext (bp->ts1); -if (bp->ts2) -ptp_ocp_unregister_ext (bp->ts2); -if (bp->ts3) -ptp_ocp_unregister_ext (bp->ts3); -if (bp->ts4) -ptp_ocp_unregister_ext (bp->ts4); -if (bp->pps) -ptp_ocp_unregister_ext (bp->pps); +ptp_ocp_unregister_ext (bp->ts0); +ptp_ocp_unregister_ext (bp->ts1); +ptp_ocp_unregister_ext (bp->ts2); +ptp_ocp_unregister_ext (bp->ts3); +ptp_ocp_unregister_ext (bp->ts4); +ptp_ocp_unregister_ext (bp->pps);for (i =0 ; i <4 ; i++) -if (bp->signal_out[i]) -ptp_ocp_unregister_ext (bp->signal_out[i]); +ptp_ocp_unregister_ext (bp->signal_out[i]);for (i =0 ; i < __PORT_COUNT; i++)if (bp->port[i].line !=-1 )serial8250_unregister_port (bp->port[i].line);
Signature · 1 line
2.50.1
PATCH 7/7 ptp: ocp: don't use "proxy" headers
In reply to Andy ShevchenkoUpdate header inclusions to follow IWYU (Include What You Use) principle.
--- drivers/ptp/ptp_ocp.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-)
drivers/ptp/ptp_ocp.c +28 -3
@@ -1,28 +1,53 @@// SPDX-License-Identifier: GPL-2.0-only /* Copyright (c) 2020 Facebook */ -#include <linux/bits.h> +#include <linux/array_size.h> +#include <linux/bitops.h> #include <linux/clk-provider.h> #include <linux/clkdev.h> +#include <linux/container_of.h> #include <linux/crc16.h> #include <linux/debugfs.h> +#include <linux/device.h> #include <linux/dpll.h> #include <linux/err.h> #include <linux/i2c.h> -#include <linux/init.h> -#include <linux/kernel.h> +#include <linux/idr.h> +#include <linux/interrupt.h> +#include <linux/io.h> +#include <linux/ioport.h> +#include <linux/kstrtox.h> +#include <linux/limits.h> +#include <linux/math64.h> +#include <linux/minmax.h> #include <linux/module.h> #include <linux/mtd/mtd.h> +#include <linux/mutex.h> #include <linux/nvmem-consumer.h> #include <linux/pci.h> #include <linux/platform_data/i2c-ocores.h> #include <linux/platform_data/i2c-xiic.h> #include <linux/platform_device.h> #include <linux/ptp_clock_kernel.h> +#include <linux/seq_file.h> #include <linux/serial_8250.h> +#include <linux/slab.h> #include <linux/spi/altera.h> #include <linux/spi/spi.h> #include <linux/spi/xilinx_spi.h> +#include <linux/spinlock.h> +#include <linux/sprintf.h> +#include <linux/stddef.h> +#include <linux/string.h> +#include <linux/sysfs.h> +#include <linux/time.h> +#include <linux/timekeeping.h> +#include <linux/timer.h> +#include <linux/types.h> +#include <linux/workqueue.h> + +#include <asm/byteorder.h> +#include <net/devlink.h> #define PCI_DEVICE_ID_META_TIMECARD 0x0400
Signature · 1 line
2.50.1
Re: [PATCH net-next v1 1/7] ptp: ocp: Refactor signal_show() and fix %ptT misuse
In reply to Andy ShevchenkoQuoted 34 lines · Andy Shevchenko
Refactor
signal_show()to avoid sequential calls to sysfs_emit*() and use the same pattern to get the index of a signal as it's done insignal_store().While at it, fix wrong use of %ptT against struct timespec64. It's kinda lucky that it worked just because the first member there 64-bit and it's of
time64_ttype. Now with %ptS it may be used correctly.Signed-off-by Andy Shevchenko <[email protected]>--- drivers/ptp/ptp_ocp.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-)drivers/ptp/ptp_ocp.c +2
-7@@ -3250,20 +3250,16 @@ signal_show(struct device *dev, struct device_attribute *attr, char *buf)struct dev_ext_attribute *ea =to_ext_attr (attr);struct ptp_ocp *bp =dev_get_drvdata (dev);struct ptp_ocp_signal *signal; +int gen = (uintptr_t )ea->var;struct timespec64 ts; -ssize_t count; -int i; - i = (uintptr_t )ea->var; - signal = &bp->signal[i]; - - count =sysfs_emit (buf,"%llu %d %llu %d" , signal->period, - signal->duty, signal->phase, signal->polarity); + signal = &bp->signal[gen];
drivers/ptp/ptp_ocp.c signal_show(struct device *dev, struct device_attribute *attr, char *buf) · line 3265
Quoted 10 lines
drivers/ptp/ptp_ocp.c +3
-2ts =ktime_to_timespec64 (signal->start); - count +=sysfs_emit_at (buf, count," %ptT TAI\n" , &ts); -return count; +return sysfs_emit (buf,"%llu %d %llu %d %ptT TAI\n" , + signal->period, signal->duty, signal->phase, signal->polarity, + &ts.tv_sec); } static EXT_ATTR_RW(signal, signal, 0); static EXT_ATTR_RW(signal, signal, 1);
Re: [PATCH net-next v1 4/7] ptp: ocp: Apply standard pattern for cleaning up loop
In reply to Andy ShevchenkoQuoted 13 lines · Andy Shevchenko
The while (i--) is a standard pattern for the cleaning up loops. Apply this pattern where it makes sense in the driver.
Signed-off-by Andy Shevchenko <[email protected]>--- drivers/ptp/ptp_ocp.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)drivers/ptp/ptp_ocp.c +0
-0
drivers/ptp/ptp_ocp.c ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 4827
Quoted 9 lines
drivers/ptp/ptp_ocp.c +1
-2@@ -4821,8 +4821,7 @@ ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)return 0 ; out_dpll: -while (i) { - --i; +while (i--) {dpll_pin_unregister (bp->dpll, bp->sma[i].dpll_pin, &dpll_pins_ops, &bp->sma[i]);dpll_pin_put (bp->sma[i].dpll_pin); }
Re: [PATCH net-next v1 5/7] ptp: ocp: Reuse META's PCI vendor ID
In reply to Andy ShevchenkoQuoted 23 lines · Andy Shevchenko
The META's PCI vendor ID is listed already in the
pci_ids.h. Reuse it here.Signed-off-by Andy Shevchenko <[email protected]>--- drivers/ptp/ptp_ocp.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/ptp/ptp_ocp.c +1
-2@@ -25,8 +25,7 @@#include <linux/crc16.h> #include <linux/dpll.h> -#define PCI_VENDOR_ID_FACEBOOK 0x1d9b -#define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400 +#define PCI_DEVICE_ID_META_TIMECARD 0x0400#define PCI_VENDOR_ID_CELESTICA 0x18d4#define PCI_DEVICE_ID_CELESTICA_TIMECARD 0x1008
drivers/ptp/ptp_ocp.c static struct ocp_resource ocp_adva_resource[] = { · line 1035
Quoted 8 lines
drivers/ptp/ptp_ocp.c +1
-1@@ -1030,7 +1029,7 @@ static struct ocp_resource ocp_adva_resource[] = { };static const struct pci_device_id ptp_ocp_pcidev_id[] = { - {PCI_DEVICE_DATA (TIMECARD , &ocp_fb_resource) }, + {PCI_DEVICE_DATA (META ,TIMECARD , &ocp_fb_resource) }, {PCI_DEVICE_DATA (CELESTICA ,TIMECARD , &ocp_fb_resource) }, {PCI_DEVICE_DATA (OROLIA ,ARTCARD , &ocp_art_resource) }, {PCI_DEVICE_DATA (ADVA ,TIMECARD , &ocp_adva_resource) },
Re: [PATCH net-next v1 2/7] ptp: ocp: Make ptp_ocp_unregister_ext() NULL-aware
In reply to Andy ShevchenkoQuoted 41 lines · Andy Shevchenko
It's a common practice to make resource release functions be NULL-aware. Make
ptp_ocp_unregister_ext()NULL-aware.Signed-off-by Andy Shevchenko <[email protected]>--- drivers/ptp/ptp_ocp.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-)drivers/ptp/ptp_ocp.c +6
-12@@ -2225,6 +2225,9 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)static void ptp_ocp_unregister_ext (struct ptp_ocp_ext_src *ext) { +if (!ext) +return ; + ext->info->enable (ext, ~0 , false);pci_free_irq (ext->bp->pdev, ext->irq_vec, ext);kfree (ext);@@ -4558,21 +4561,14 @@ ptp_ocp_detach(struct ptp_ocp *bp)ptp_ocp_detach_sysfs (bp);ptp_ocp_attr_group_del (bp);timer_delete_sync (&bp->watchdog); -if (bp->ts0) -ptp_ocp_unregister_ext (bp->ts0); -if (bp->ts1) -ptp_ocp_unregister_ext (bp->ts1); -if (bp->ts2) -ptp_ocp_unregister_ext (bp->ts2); -if (bp->ts3) -ptp_ocp_unregister_ext (bp->ts3); -if (bp->ts4) -ptp_ocp_unregister_ext (bp->ts4); -if (bp->pps) -ptp_ocp_unregister_ext (bp->pps); +ptp_ocp_unregister_ext (bp->ts0); +ptp_ocp_unregister_ext (bp->ts1); +ptp_ocp_unregister_ext (bp->ts2);
drivers/ptp/ptp_ocp.c ptp_ocp_detach(struct ptp_ocp *bp) · line 4574
Quoted 10 lines
drivers/ptp/ptp_ocp.c +4
-2+ptp_ocp_unregister_ext (bp->ts3); +ptp_ocp_unregister_ext (bp->ts4); +ptp_ocp_unregister_ext (bp->pps);for (i =0 ; i <4 ; i++) -if (bp->signal_out[i]) -ptp_ocp_unregister_ext (bp->signal_out[i]); +ptp_ocp_unregister_ext (bp->signal_out[i]);for (i =0 ; i < __PORT_COUNT; i++)if (bp->port[i].line !=-1 )serial8250_unregister_port (bp->port[i].line);
Vadim Fedorenko
maintainerRe: [PATCH net-next v1 3/7] ptp: ocp: Refactor ptp_ocp_i2c_notifier_call()
In reply to Andy ShevchenkoQuoted 15 lines · Andy Shevchenko
Refactor
ptp_ocp_i2c_notifier_call()to avoid unneeded local variable.Signed-off-by Andy Shevchenko <[email protected]>--- drivers/ptp/ptp_ocp.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-)drivers/ptp/ptp_ocp.c +0
-0@@ -4872,16 +4872,6 @@ ptp_ocp_i2c_notifier_call(struct notifier_block *nb, {struct device *dev, *child = data;struct ptp_ocp *bp;
drivers/ptp/ptp_ocp.c ptp_ocp_i2c_notifier_call(struct notifier_block *nb, · line 4884
Quoted 10 lines
drivers/ptp/ptp_ocp.c +0
-10-bool add; - -switch (action) { -case BUS_NOTIFY_ADD_DEVICE : -case BUS_NOTIFY_DEL_DEVICE : - add = action ==BUS_NOTIFY_ADD_DEVICE ; -break ; -default : -return 0 ; - }
the reason we've done it is to avoid iterating over devices and do string comparisons for actions we don't care about. We can still avoid local variable by changing if condition later in the code, but I'm not sure this refactor gives any benefits.
Quoted 23 lines
drivers/ptp/ptp_ocp.c +9
-2if (!i2c_verify_adapter (child))return 0 ;@@ -4894,10 +4884,17 @@ ptp_ocp_i2c_notifier_call(struct notifier_block *nb, found: bp =dev_get_drvdata (dev); -if (add) + +switch (action) { +case BUS_NOTIFY_ADD_DEVICE :ptp_ocp_symlink (bp, child,"i2c" ); -else +break ; +case BUS_NOTIFY_DEL_DEVICE :sysfs_remove_link (&bp->dev.kobj,"i2c" ); +break ; +default : +return 0 ; + }return 0 ; }
Vadim Fedorenko
maintainerQuoted 2 lines · Andy Shevchenko
ptp: ocp: Sort headers alphabetically
ptp: ocp: don't use "proxy" headers
I don't see benefits of these 2 patches, what's the reason?
Re: [PATCH net-next v1 3/7] ptp: ocp: Refactor ptp_ocp_i2c_notifier_call()
In reply to Vadim FedorenkoQuoted 2 lines · Vadim Fedorenko
Quoted 1 line · Andy Shevchenko
Refactor
ptp_ocp_i2c_notifier_call()to avoid unneeded local variable.
...
Quoted 4 lines
the reason we've done it is to avoid iterating over devices and do string comparisons for actions we don't care about. We can still avoid local variable by changing if condition later in the code, but I'm not sure this refactor gives any benefits.
This is definitely a slow path and having less LoCs and straightforward code flow is important for readability and maintenance. I find my proposed change useful.
Signature · 2 lines
With Best Regards, Andy Shevchenko