Subject: Re: [PATCH v2 3/6] powerpc/pci: use pci_is_pcie() to simplify code

From: Gavin Shan <[email-masked]>
Date: Fri, 11 Oct 2013 14:53:29 +0800

On Fri, Oct 11, 2013 at 02:33:58PM +0800, Yijing Wang wrote:
>On 2013/10/11 14:16, Gavin Shan wrote:
>> On Fri, Oct 11, 2013 at 04:49:56PM +1100, Benjamin Herrenschmidt wrote:
>>> On Fri, 2013-09-06 at 14:30 -0600, Bjorn Helgaas wrote:
>>>> On Thu, Sep 05, 2013 at 03:55:27PM +0800, Yijing Wang wrote:

.../...

>>>>> Use pci_is_pcie() to simplify code.
>>>>> diff --git a/arch/powerpc/kernel/eeh.c b/arch/powerpc/kernel/eeh.c
>>>>> index 55593ee..6ebbe54 100644
>>>>> --- a/arch/powerpc/kernel/eeh.c
>>>>> +++ b/arch/powerpc/kernel/eeh.c
>>>>> @@ -189,8 +189,7 @@ static size_t eeh_gather_pci_data(struct eeh_dev *edev, char * buf, size_t len)
>>>>>  	}
>>>>>  
>>>>>  	/* If PCI-E capable, dump PCI-E cap 10, and the AER */
>>>>> -	cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
>>>>> -	if (cap) {
>>>>> +	if (pci_is_pcie(dev)) {
>>>>>  		n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
>>>>>  		printk(KERN_WARNING
>>>>>  		       "EEH: PCI-E capabilities and status follow:\n");
>>>
>>> So we remove reading of "cap", but slightly further down the code does:
>>>
>>> 		for (i=0; i<=8; i++) {
>>> 			eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
>>> 			n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
>>> 			printk(KERN_WARNING "EEH: PCI-E %02x: %08x\n", i, cfg);
>>> 		}
>>>
>>> Which actually *uses* the value of "cap" ... oops :-)
>>>
>> 
>> It's my fault and I should have looked into the changes more closely.
>> How about changing it like this:
>> 
>> 	cap = pci_is_pcie(dev) ? pci_pcie_cap(dev) :
>> 	      pci_find_capability(dev, PCI_CAP_ID_EXP);
>> 	if (cap) {
>> 		...
>> 	}
>> 
>> It would save some PCI-CFG access cycles for most cases :-)
>
>Hi Gavin,  it's not your fault, it's my fault. :)
>
>Because pci_pcie_cap(dev) == dev->pcie_cap == pci_find_capability(dev, PCI_CAP_ID_EXP);
>
>so I think it's ok to use dev->pcie_cap instead of stale "cap".
>

Yijing, There has one exception: dev->pcie_cap isn't updated yet.
This function has possibility to be invoked before that. However,
we don't have the binding (eeh device <-> PCI device) for the case.
So the piece of code shouldn't be running

However, it's a bit safer to have pci_find_capability(dev, PCI_CAP_ID_EXP)
as well even though we needn't it for 99.9% cases if you agree :-)

Thanks,
Gavin