public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot-Users] [PATCH] PCI_READ_VIA_DWORD_OP: initialize val parameter on fail
@ 2007-06-05  7:35 Shinya Kuribayashi
  2007-06-05 16:04 ` Wolfgang Denk
  0 siblings, 1 reply; 5+ messages in thread
From: Shinya Kuribayashi @ 2007-06-05  7:35 UTC (permalink / raw)
  To: u-boot

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

Index: b/drivers/pci.c
===================================================================
--- a/drivers/pci.c
+++ b/drivers/pci.c
@@ -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_dw
 	u32 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_d
 	return 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)
 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [U-Boot-Users] [PATCH] PCI_READ_VIA_DWORD_OP: initialize val parameter on fail
  2007-06-05  7:35 [U-Boot-Users] [PATCH] PCI_READ_VIA_DWORD_OP: initialize val parameter on fail Shinya Kuribayashi
@ 2007-06-05 16:04 ` Wolfgang Denk
  2007-06-06  0:51   ` Shinya Kuribayashi
  0 siblings, 1 reply; 5+ messages in thread
From: Wolfgang Denk @ 2007-06-05 16:04 UTC (permalink / raw)
  To: u-boot

In message <4665122B.6010707@necel.com> you wrote:
> 
> 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.

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.

> 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.

> compiled and tested with our MIPS board.
> 
> Thanks,
> Shinya

Your Signed-off-by: line is missing here.

> @@ -83,7 +83,10 @@ int pci_hose_read_config_##size##_via_dw
>  	u32 val32;							\
>  									\
>  	if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\
> +	{								\

Bad brace style here.

> +		error_code;						\
...
> +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

-- 
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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [U-Boot-Users] [PATCH] PCI_READ_VIA_DWORD_OP: initialize val parameter on fail
  2007-06-05 16:04 ` Wolfgang Denk
@ 2007-06-06  0:51   ` Shinya Kuribayashi
  2007-06-06  2:18     ` Shinya Kuribayashi
  2007-06-06  3:33     ` Shinya Kuribayashi
  0 siblings, 2 replies; 5+ messages in thread
From: Shinya Kuribayashi @ 2007-06-06  0:51 UTC (permalink / raw)
  To: u-boot

Wolfgang Denk wrote:
> In message <4665122B.6010707@necel.com> you wrote:
>> 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.
> 
> 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.

> 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?

Agreed completely. I'll send patches incorporating your comments, and
with Signed-off-by.

Thanks for your review.
Shinya

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [U-Boot-Users] [PATCH] PCI_READ_VIA_DWORD_OP: initialize val parameter on fail
  2007-06-06  0:51   ` Shinya Kuribayashi
@ 2007-06-06  2:18     ` Shinya Kuribayashi
  2007-06-06  3:33     ` Shinya Kuribayashi
  1 sibling, 0 replies; 5+ messages in thread
From: Shinya Kuribayashi @ 2007-06-06  2:18 UTC (permalink / raw)
  To: u-boot

>> 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.

Signed-off-by: Shinya Kuribayashi <shinya.kuribayashi@necel.com>

---

 drivers/pci.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Index: b/drivers/pci.c
===================================================================
--- a/drivers/pci.c
+++ b/drivers/pci.c
@@ -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));		\
 									\

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [U-Boot-Users] [PATCH] PCI_READ_VIA_DWORD_OP: initialize val parameter on fail
  2007-06-06  0:51   ` Shinya Kuribayashi
  2007-06-06  2:18     ` Shinya Kuribayashi
@ 2007-06-06  3:33     ` Shinya Kuribayashi
  1 sibling, 0 replies; 5+ messages in thread
From: Shinya Kuribayashi @ 2007-06-06  3:33 UTC (permalink / raw)
  To: u-boot

Wolfgang,

> 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

Signed-off-by: Shinya Kuribayashi <shinya.kuribayashi@necel.com>

---

 drivers/pci.c |   34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)

Index: b/drivers/pci.c
===================================================================
--- a/drivers/pci.c
+++ b/drivers/pci.c
@@ -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)			\

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-06-06  3:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-06-05  7:35 [U-Boot-Users] [PATCH] PCI_READ_VIA_DWORD_OP: initialize val parameter on fail Shinya Kuribayashi
2007-06-05 16:04 ` Wolfgang Denk
2007-06-06  0:51   ` Shinya Kuribayashi
2007-06-06  2:18     ` Shinya Kuribayashi
2007-06-06  3:33     ` Shinya Kuribayashi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox