PCI_READ_VIA_DWORD_OP: initialize val parameter on fail
PATCH 1/1 PCI_READ_VIA_DWORD_OP: initialize val parameter on fail
Hi,
pci_hose_read_config_{byte,word}_via_dword uses a temporary read buffer `val32', so if read_config_dword returns -1 then val32 also should be initialized.
Without this fix we'll go on scanning bus with vendor or header_ type uninitialized. This brings many unnecessary config trials.
compiled and tested with our MIPS board.
Thanks,
Shinya
drivers/pci.c +6 -3
@@ -75,7 +75,7 @@ PCI_OP(write, word, u16, ) PCI_OP(write, dword, u32, ) #endif /* CONFIG_IXP425 */ -#define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \ +#define PCI_READ_VIA_DWORD_OP(size, type, off_mask, error_code) \ int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\ pci_dev_t dev, \ int offset, type val) \
@@ -83,7 +83,10 @@ int pci_hose_read_config_##size##_via_dwu32 val32; \ \if (pci_hose_read_config_dword (hose, dev, offset &0xfc , &val32) <0 )\ + { \ + error_code; \return -1 ; \ + } \ \ *val = (val32 >> ((offset & (int )off_mask) *8 )); \ \
@@ -111,8 +114,8 @@ int pci_hose_write_config_##size##_via_dreturn 0 ; \ } -PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03) -PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02) +PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03, *val = 0xff) +PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02, *val = 0xffff) PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff)PCI_WRITE_VIA_DWORD_OP (word, u16,0x02 ,0x0000ffff )
PATCH 1/1 PCI_READ_VIA_DWORD_OP: initialize val parameter on fail
Quoted 4 lines · In message <[email protected]> you wrote:
pci_hose_read_config_{byte,word}_via_dword uses a temporary read buffer `val32', so if
read_config_dwordreturns -1 then val32 also should be initialized.
This is actually misleading, since you don't initialize the local variable "val32" (which would not ake sense as it goes out of scope anyway when you return from the function). Instead, you use the variable (more exatly, the pointer), the name of which was passed as a macro argument.
Quoted 2 lines
Without this fix we'll go on scanning bus with vendor or
header_type uninitialized. This brings many unnecessary config trials.
Agreed. The purpose of the patch is OK, but I have to admit that I don't like the implementation.
Quoted 4 lines
compiled and tested with our MIPS board.
Thanks,
Shinya
Your Signed-off-by: line is missing here.
drivers/pci.c int pci_hose_read_config_##size##_via_dw · line 86
Quoted 5 lines
drivers/pci.c +1
-0@@ -83,7 +83,10 @@ int pci_hose_read_config_##size##_via_dwu32 val32; \ \if (pci_hose_read_config_dword (hose, dev, offset &0xfc , &val32) <0 )\ + { \
Bad brace style here.
Quoted 1 line
drivers/pci.c +1
-0+ error_code; \
...
drivers/pci.c int pci_hose_write_config_##size##_via_d · line 118
Quoted 2 lines
drivers/pci.c +2
-0+PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03, *val = 0xff) +PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02, *val = 0xffff)
I know that this is not your invention, and you just copy existing code, but I think injecting C statements like this through macro arguments is bad style and should be avoided.
I have to admit that I don't get wht you need all this "error_code" trickery instead of simpley writing
*val = -1;
Can you please change your patch like that, and while you are at it please fix the other uses of this ugly construct as well?
Thanks.
Best regards,
Wolfgang Denk
Signature · 6 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de So we follow our wandering paths, and the very darkness acts as our guide and our doubts serve to reassure us. - Jean-Pierre de Caussade, eighteenth-century Jesuit priest
PATCH 1/1 PCI_READ_VIA_DWORD_OP: initialize val parameter on fail
Quoted 10 lines · Wolfgang Denk
Quoted 3 lines · In message <[email protected]> you wrote:
pci_hose_read_config_{byte,word}_via_dword uses a temporary read buffer `val32', so if
read_config_dwordreturns -1 then val32 also should be initialized.This is actually misleading, since you don't initialize the local variable "val32" (which would not ake sense as it goes out of scope anyway when you return from the function). Instead, you use the variable (more exatly, the pointer), the name of which was passed as a macro argument.
Ah, my bad. What should be initialized is _val_, not val32.
Quoted 4 lines
I know that this is not your invention, and you just copy existing code, but I think injecting C statements like this through macro arguments is bad style and should be avoided.
Quoted 8 lines
I have to admit that I don't get wht you need all this "
error_code" trickery instead of simpley writing*val = -1;Can you please change your patch like that, and while you are at it please fix the other uses of this ugly construct as well?
Agreed completely. I'll send patches incorporating your comments, and with Signed-off-by.
Thanks for your review.
Shinya
PATCH 1/1 PCI_READ_VIA_DWORD_OP: initialize val parameter on fail
Quoted 11 lines
Quoted 11 lines
I know that this is not your invention, and you just copy existing code, but I think injecting C statements like this through macro arguments is bad style and should be avoided.
I have to admit that I don't get wht you need all this "
error_code" trickery instead of simpley writing*val = -1;Can you please change your patch like that,
Many users of PCI config read routines tend to ignore the function ret value, and are only concerned about the contents of *val. Based on this, pci_hose_read_config_{byte,word}_via_dword should initialize the *val on dword read error.
Without this fix, for example, we'll go on scanning bus with vendor or header_type uninitialized. This brings many unnecessary config trials.
---
drivers/pci.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/pci.c +3 -1
@@ -81,8 +81,10 @@ int pci_hose_read_config_##size##_via_dw { \u32 val32; \ \ -if (pci_hose_read_config_dword (hose, dev, offset &0xfc , &val32) <0 )\ +if (pci_hose_read_config_dword (hose, dev, offset &0xfc , &val32) <0 ) { \ + *val =-1 ; \return -1 ; \ + } \ \ *val = (val32 >> ((offset & (int )off_mask) *8 )); \ \
PATCH 1/1 PCI_READ_VIA_DWORD_OP: initialize val parameter on fail
In reply to Shinya KuribayashiWolfgang,
Quoted 8 lines
I have to admit that I don't get wht you need all this "
error_code" trickery instead of simpley writing*val = -1;Can you please change your patch like that, and while you are at it please fix the other uses of this ugly construct as well?
I simply tried to remove error_code trickery for the remaining part, but failed to compile. *val = -1; is not allowed for config write. If we want to remove error_code completely, pci_##rw##_config_##size() should be revised first.
I have a feeling the current macro may be compact tough, let's see the patch. Of course compiled and tested.
Thanks.
---
This patch contains the following cleanups for PCI_OP macro:
- Split PCI_OP into PCI_READ_OP and PCI_WRITE_OP, respectivly
- Fix bad brace style
- Initialize *val with -1 on error. This obsoletes error_code trick
---
drivers/pci.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-)
drivers/pci.c +22 -12
@@ -53,26 +53,36 @@ PCI_HOSE_OP(write, word, u16) PCI_HOSE_OP(write, dword, u32)#ifndef CONFIG_IXP425 -#define PCI_OP (rw, size, type, error_code) \ -int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value) \ +#define PCI_READ_OP (size, type) \ +int pci_read_config_##size(pci_dev_t dev, int offset, type value) \ { \ struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \ \ - if (!hose) \ - { \ - error_code; \ + if (!hose) { \ + *value = -1; \ return -1; \ } \ \ - return pci_hose_##rw##_config_##size(hose, dev, offset, value); \ + return pci_hose_read_config_##size(hose, dev, offset, value); \ +} + +#define PCI_WRITE_OP (size, type) \ +int pci_write_config_##size(pci_dev_t dev, int offset, type value) \ +{ \ + struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \ + \ + if (!hose) \ + return -1; \ + \ + return pci_hose_write_config_##size(hose, dev, offset, value); \ } -PCI_OP(read, byte, u8 *, *value = 0xff) -PCI_OP(read, word, u16 *, *value = 0xffff) -PCI_OP(read, dword, u32 *, *value = 0xffffffff) -PCI_OP(write, byte, u8, ) -PCI_OP(write, word, u16, ) -PCI_OP(write, dword, u32, ) +PCI_READ_OP(byte, u8 *) +PCI_READ_OP(word, u16 *) +PCI_READ_OP(dword, u32 *) +PCI_WRITE_OP(byte, u8) +PCI_WRITE_OP(word, u16) +PCI_WRITE_OP(dword, u32) #endif /* CONFIG_IXP425 */ #define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \