To iterate through all possible BARs, loop conditions refactored to the
*number* of BARs "i < PCI_STD_NUM_BARS", instead of the index of the last
valid BAR "i <= BAR_5". This is more idiomatic C style and allows to avoid
the fencepost error. Array definitions changed to PCI_STD_NUM_BARS where
appropriate.
Cc: Kishon Vijay Abraham I <[email protected]>
Cc: Lorenzo Pieralisi <[email protected]>
Signed-off-by: Denis Efremov <[email protected]>
---
drivers/pci/endpoint/functions/pci-epf-test.c | 10 +++++-----
include/linux/pci-epc.h | 2 +-
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/drivers/pci/endpoint/functions/pci-epf-test.c b/drivers/pci/endpoint/functions/pci-epf-test.c
index 1cfe3687a211..5d74f81ddfe4 100644
--- a/drivers/pci/endpoint/functions/pci-epf-test.c
+++ b/drivers/pci/endpoint/functions/pci-epf-test.c
@@ -44,7 +44,7 @@
static struct workqueue_struct *kpcitest_workqueue;
struct pci_epf_test {
- void *reg[6];
+ void *reg[PCI_STD_NUM_BARS];
struct pci_epf *epf;
enum pci_barno test_reg_bar;
struct delayed_work cmd_handler;
@@ -377,7 +377,7 @@ static void pci_epf_test_unbind(struct pci_epf *epf)
cancel_delayed_work(&epf_test->cmd_handler);
pci_epc_stop(epc);
- for (bar = BAR_0; bar <= BAR_5; bar++) {
+ for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
epf_bar = &epf->bar[bar];
if (epf_test->reg[bar]) {
@@ -400,7 +400,7 @@ static int pci_epf_test_set_bar(struct pci_epf *epf)
epc_features = epf_test->epc_features;
- for (bar = BAR_0; bar <= BAR_5; bar += add) {
+ for (bar = 0; bar < PCI_STD_NUM_BARS; bar += add) {
epf_bar = &epf->bar[bar];
/*
* pci_epc_set_bar() sets PCI_BASE_ADDRESS_MEM_TYPE_64
@@ -450,7 +450,7 @@ static int pci_epf_test_alloc_space(struct pci_epf *epf)
}
epf_test->reg[test_reg_bar] = base;
- for (bar = BAR_0; bar <= BAR_5; bar += add) {
+ for (bar = 0; bar < PCI_STD_NUM_BARS; bar += add) {
epf_bar = &epf->bar[bar];
add = (epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ? 2 : 1;
@@ -478,7 +478,7 @@ static void pci_epf_configure_bar(struct pci_epf *epf,
bool bar_fixed_64bit;
int i;
- for (i = BAR_0; i <= BAR_5; i++) {
+ for (i = 0; i < PCI_STD_NUM_BARS; i++) {
epf_bar = &epf->bar[i];
bar_fixed_64bit = !!(epc_features->bar_fixed_64bit & (1 << i));
if (bar_fixed_64bit)
diff --git a/include/linux/pci-epc.h b/include/linux/pci-epc.h
index f641badc2c61..56f1846b9d39 100644
--- a/include/linux/pci-epc.h
+++ b/include/linux/pci-epc.h
@@ -117,7 +117,7 @@ struct pci_epc_features {
unsigned int msix_capable : 1;
u8 reserved_bar;
u8 bar_fixed_64bit;
- u64 bar_fixed_size[BAR_5 + 1];
+ u64 bar_fixed_size[PCI_STD_NUM_BARS];
size_t align;
};
--
2.21.0
On Mon, Sep 16, 2019 at 11:41:36PM +0300, Denis Efremov wrote:
> To iterate through all possible BARs, loop conditions refactored to the
> *number* of BARs "i < PCI_STD_NUM_BARS", instead of the index of the last
> valid BAR "i <= BAR_5". This is more idiomatic C style and allows to avoid
> the fencepost error. Array definitions changed to PCI_STD_NUM_BARS where
> appropriate.
>
> Cc: Kishon Vijay Abraham I <[email protected]>
> Cc: Lorenzo Pieralisi <[email protected]>
> Signed-off-by: Denis Efremov <[email protected]>
> ---
> drivers/pci/endpoint/functions/pci-epf-test.c | 10 +++++-----
> include/linux/pci-epc.h | 2 +-
> 2 files changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/pci/endpoint/functions/pci-epf-test.c b/drivers/pci/endpoint/functions/pci-epf-test.c
> index 1cfe3687a211..5d74f81ddfe4 100644
> --- a/drivers/pci/endpoint/functions/pci-epf-test.c
> +++ b/drivers/pci/endpoint/functions/pci-epf-test.c
> @@ -44,7 +44,7 @@
> static struct workqueue_struct *kpcitest_workqueue;
>
> struct pci_epf_test {
> - void *reg[6];
> + void *reg[PCI_STD_NUM_BARS];
> struct pci_epf *epf;
> enum pci_barno test_reg_bar;
> struct delayed_work cmd_handler;
> @@ -377,7 +377,7 @@ static void pci_epf_test_unbind(struct pci_epf *epf)
>
> cancel_delayed_work(&epf_test->cmd_handler);
> pci_epc_stop(epc);
> - for (bar = BAR_0; bar <= BAR_5; bar++) {
> + for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
> epf_bar = &epf->bar[bar];
>
> if (epf_test->reg[bar]) {
> @@ -400,7 +400,7 @@ static int pci_epf_test_set_bar(struct pci_epf *epf)
>
> epc_features = epf_test->epc_features;
>
> - for (bar = BAR_0; bar <= BAR_5; bar += add) {
> + for (bar = 0; bar < PCI_STD_NUM_BARS; bar += add) {
Is it possible to completely remove the BAR_x macros, or are there exsiting
users after this patchset?
As your patchset replaces BAR_0 with 0 and BAR_1 with 1, does this suggest
that other users of BAR_x should be removed and also replaced with a number?
Apologies if you this doesn't fall in the remit of this patchset.
Thanks,
Andrew Murray
> epf_bar = &epf->bar[bar];
> /*
> * pci_epc_set_bar() sets PCI_BASE_ADDRESS_MEM_TYPE_64
> @@ -450,7 +450,7 @@ static int pci_epf_test_alloc_space(struct pci_epf *epf)
> }
> epf_test->reg[test_reg_bar] = base;
>
> - for (bar = BAR_0; bar <= BAR_5; bar += add) {
> + for (bar = 0; bar < PCI_STD_NUM_BARS; bar += add) {
> epf_bar = &epf->bar[bar];
> add = (epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ? 2 : 1;
>
> @@ -478,7 +478,7 @@ static void pci_epf_configure_bar(struct pci_epf *epf,
> bool bar_fixed_64bit;
> int i;
>
> - for (i = BAR_0; i <= BAR_5; i++) {
> + for (i = 0; i < PCI_STD_NUM_BARS; i++) {
> epf_bar = &epf->bar[i];
> bar_fixed_64bit = !!(epc_features->bar_fixed_64bit & (1 << i));
> if (bar_fixed_64bit)
> diff --git a/include/linux/pci-epc.h b/include/linux/pci-epc.h
> index f641badc2c61..56f1846b9d39 100644
> --- a/include/linux/pci-epc.h
> +++ b/include/linux/pci-epc.h
> @@ -117,7 +117,7 @@ struct pci_epc_features {
> unsigned int msix_capable : 1;
> u8 reserved_bar;
> u8 bar_fixed_64bit;
> - u64 bar_fixed_size[BAR_5 + 1];
> + u64 bar_fixed_size[PCI_STD_NUM_BARS];
> size_t align;
> };
>
> --
> 2.21.0
>
On 9/18/19 12:19 PM, Andrew Murray wrote:
> On Mon, Sep 16, 2019 at 11:41:36PM +0300, Denis Efremov wrote:
>> To iterate through all possible BARs, loop conditions refactored to the
>> *number* of BARs "i < PCI_STD_NUM_BARS", instead of the index of the last
>> valid BAR "i <= BAR_5". This is more idiomatic C style and allows to avoid
>> the fencepost error. Array definitions changed to PCI_STD_NUM_BARS where
>> appropriate.
>>
>> Cc: Kishon Vijay Abraham I <[email protected]>
>> Cc: Lorenzo Pieralisi <[email protected]>
>> Signed-off-by: Denis Efremov <[email protected]>
>> ---
>> drivers/pci/endpoint/functions/pci-epf-test.c | 10 +++++-----
>> include/linux/pci-epc.h | 2 +-
>> 2 files changed, 6 insertions(+), 6 deletions(-)
>>
>> diff --git a/drivers/pci/endpoint/functions/pci-epf-test.c b/drivers/pci/endpoint/functions/pci-epf-test.c
>> index 1cfe3687a211..5d74f81ddfe4 100644
>> --- a/drivers/pci/endpoint/functions/pci-epf-test.c
>> +++ b/drivers/pci/endpoint/functions/pci-epf-test.c
>> @@ -44,7 +44,7 @@
>> static struct workqueue_struct *kpcitest_workqueue;
>>
>> struct pci_epf_test {
>> - void *reg[6];
>> + void *reg[PCI_STD_NUM_BARS];
>> struct pci_epf *epf;
>> enum pci_barno test_reg_bar;
>> struct delayed_work cmd_handler;
>> @@ -377,7 +377,7 @@ static void pci_epf_test_unbind(struct pci_epf *epf)
>>
>> cancel_delayed_work(&epf_test->cmd_handler);
>> pci_epc_stop(epc);
>> - for (bar = BAR_0; bar <= BAR_5; bar++) {
>> + for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
>> epf_bar = &epf->bar[bar];
>>
>> if (epf_test->reg[bar]) {
>> @@ -400,7 +400,7 @@ static int pci_epf_test_set_bar(struct pci_epf *epf)
>>
>> epc_features = epf_test->epc_features;
>>
>> - for (bar = BAR_0; bar <= BAR_5; bar += add) {
>> + for (bar = 0; bar < PCI_STD_NUM_BARS; bar += add) {
>
> Is it possible to completely remove the BAR_x macros, or are there exsiting
> users after this patchset?
They are still used in other parts of the code. So, I've decided to preserve
the defines in this case.
pci-epc-core.c
400: (epf_bar->barno == BAR_5 &&
429: (epf_bar->barno == BAR_5 &&
functions/pci-epf-test.c
497: enum pci_barno test_reg_bar = BAR_0;
>
> As your patchset replaces BAR_0 with 0 and BAR_1 with 1, does this suggest
> that other users of BAR_x should be removed and also replaced with a number?
I changed BAR_0 to 0 in order to not mix different notions, i.e. the number
of bars and the concrete bar.
>
> Apologies if you this doesn't fall in the remit of this patchset.
I don't know what is better here. It's simple enough to remove these defines.
However, I would prefer to wait for the endpoint developers opinion.
Thanks for the review!
Denis
>
> Thanks,
>
> Andrew Murray
>
>> epf_bar = &epf->bar[bar];
>> /*
>> * pci_epc_set_bar() sets PCI_BASE_ADDRESS_MEM_TYPE_64
>> @@ -450,7 +450,7 @@ static int pci_epf_test_alloc_space(struct pci_epf *epf)
>> }
>> epf_test->reg[test_reg_bar] = base;
>>
>> - for (bar = BAR_0; bar <= BAR_5; bar += add) {
>> + for (bar = 0; bar < PCI_STD_NUM_BARS; bar += add) {
>> epf_bar = &epf->bar[bar];
>> add = (epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ? 2 : 1;
>>
>> @@ -478,7 +478,7 @@ static void pci_epf_configure_bar(struct pci_epf *epf,
>> bool bar_fixed_64bit;
>> int i;
>>
>> - for (i = BAR_0; i <= BAR_5; i++) {
>> + for (i = 0; i < PCI_STD_NUM_BARS; i++) {
>> epf_bar = &epf->bar[i];
>> bar_fixed_64bit = !!(epc_features->bar_fixed_64bit & (1 << i));
>> if (bar_fixed_64bit)
>> diff --git a/include/linux/pci-epc.h b/include/linux/pci-epc.h
>> index f641badc2c61..56f1846b9d39 100644
>> --- a/include/linux/pci-epc.h
>> +++ b/include/linux/pci-epc.h
>> @@ -117,7 +117,7 @@ struct pci_epc_features {
>> unsigned int msix_capable : 1;
>> u8 reserved_bar;
>> u8 bar_fixed_64bit;
>> - u64 bar_fixed_size[BAR_5 + 1];
>> + u64 bar_fixed_size[PCI_STD_NUM_BARS];
>> size_t align;
>> };
>>
>> --
>> 2.21.0
>>