public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Daney <ddaney@caviumnetworks.com>
To: Yury Norov <ynorov@caviumnetworks.com>
Cc: David Daney <ddaney.cavm@gmail.com>,
	<linux-kernel@vger.kernel.org>, <linux-pci@vger.kernel.org>,
	Bjorn Helgaas <bhelgaas@google.com>,
	Rob Herring <robh+dt@kernel.org>, Pawel Moll <pawel.moll@arm.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Ian Campbell <ijc+devicetree@hellion.org.uk>,
	Kumar Gala <galak@codeaurora.org>,
	Will Deacon <will.deacon@arm.com>,
	<linux-arm-kernel@lists.infradead.org>,
	<devicetree@vger.kernel.org>,
	Dann Frazier <dann.frazier@canonical.com>,
	David Daney <david.daney@cavium.com>
Subject: Re: [PATCH v7 2/3] pci, pci-thunder-pem: Add PCIe host driver for ThunderX processors.
Date: Mon, 7 Mar 2016 10:02:08 -0800	[thread overview]
Message-ID: <56DDC220.5080003@caviumnetworks.com> (raw)
In-Reply-To: <20160305155409.GA2532@yury-N73SV>

On 03/05/2016 07:54 AM, Yury Norov wrote:
[...]
>> +static u32 thunder_pem_bridge_w1c_bits(int where)
>> +{
>> +	u32 w1c_bits = 0;
>> +
>> +	switch (where & ~3) {
>> +	case 0x04: /* Command/Status */
>> +	case 0x1c: /* Base and I/O Limit/Secondary Status */
>> +		w1c_bits = 0xff000000;
>> +		break;
>> +	case 0x44: /* Power Management Control and Status */
>> +		w1c_bits = 0xfffffe00;
>> +		break;
>> +	case 0x78: /* Device Control/Device Status */
>> +	case 0x80: /* Link Control/Link Status */
>> +	case 0x88: /* Slot Control/Slot Status */
>> +	case 0x90: /* Root Status */
>> +	case 0xa0: /* Link Control 2 Registers/Link Status 2 */
>> +		w1c_bits = 0xffff0000;
>> +		break;
>> +	case 0x104: /* Uncorrectable Error Status */
>> +	case 0x110: /* Correctable Error Status */
>> +	case 0x130: /* Error Status */
>> +	case 0x160: /* Link Control 4 */
>
> This patchset is full of magic numbers.

Yes.  Did you notice that there is a comment with each one describing 
what it is, or what it is doing?


> For better readability

I disagree with that.

Doing a:

  #define CN8890_PASS1_LINK_CONTROL_4_CONFIG_SPACE_OFFSET 0x160

and then later using the symbol adds clutter.  The current code is 
compact, and *more* readable than scattering the information across 
multiple sites in the file.

> and portability,

The whole point of this file is that we are fixing up accesses for a 
very small and tightly constrained set of systems.  It is not a general 
purpose PCI root complex with standard bridges that will be used by 
multiple vendors and architectures.  Portability is not a big concern.

> it's better to declare all that as macro:
> #define LINK_CONTROL_4 0x160
>
> If there's some specific reason to use numbers, I think, it should be
> explained.
>

I had hoped that the changlog for the commit combined with the comments 
in the file would be sufficient.

I try to explain in this e-mail my thoughts on some of the stylistic 
choices I made while writing the code, but I don't plan on including 
them into the patch itself.

>> +		w1c_bits = 0xffffffff;
>> +		break;
>> +	default:
>> +		break;
>> +	}
>> +	return w1c_bits;
>> +}
>> +
>> +static int thunder_pem_bridge_write(struct pci_bus *bus, unsigned int devfn,
>> +				    int where, int size, u32 val)
>> +{
>> +	struct gen_pci *pci = bus->sysdata;
>> +	struct thunder_pem_pci *pem_pci;
>> +	u64 write_val, read_val;
>> +	u32 mask = 0;
>> +
>> +	pem_pci = container_of(pci, struct thunder_pem_pci, gen_pci);
>> +
>> +	if (devfn != 0 || where >= 2048)
>> +		return PCIBIOS_DEVICE_NOT_FOUND;
>> +
>> +	/*
>> +	 * 32-bit accesses only.  If the write is for a size smaller
>> +	 * than 32-bits, we must first read the 32-bit value and merge
>> +	 * in the desired bits and then write the whole 32-bits back
>> +	 * out.
>> +	 */
>> +	switch (size) {
>> +	case 1:
>> +		read_val = where & ~3ull;
>> +		writeq(read_val, pem_pci->pem_reg_base + PEM_CFG_RD);
>> +		read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
>> +		read_val >>= 32;
>> +		mask = ~(0xff << (8 * (where & 3)));
>
> I'm pretty sure, there's no any rocket science, but it's hard to
> understand what happens here. What is 8? Bits in byte? Bytes in word?
> Anything PCI-specific?  Moreover, you repeat this line several times
> here (though little modified). Maybe it deserves to be wrapped by some
> explaining macro?

I tried to explain this in the comment above the switch statement.

I doubt breaking the masking operations out into out-of-line functions 
would add to the readability.

>
>> +		read_val &= mask;
>> +		val = (val & 0xff) << (8 * (where & 3));
>> +		val |= (u32)read_val;
>> +		break;
>> +	case 2:
>
> Case 1 and 2 are looking very similar. Is it possible to wrap them?
> For now it looks like code duplication.

They are indeed similar, differing only in mask width.

If Bjorn insists, we could probably factor some of this code out into a 
separate function.  Personally, I don't think it is worthwhile, as doing 
so would probably increase the number of lines in the file.

>
>> +		read_val = where & ~3ull;
>> +		writeq(read_val, pem_pci->pem_reg_base + PEM_CFG_RD);
>> +		read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
>> +		read_val >>= 32;
>> +		mask = ~(0xffff << (8 * (where & 3)));
>> +		read_val &= mask;
>> +		val = (val & 0xffff) << (8 * (where & 3));
>> +		val |= (u32)read_val;
>> +		break;
>> +	default:
>> +		break;
>> +	}
>> +
>> +	/*
>> +	 * By expanding the write width to 32 bits, we may
>> +	 * inadvertently hit some W1C bits that were not intended to
>> +	 * be written.  Calculate the mask that must be applied to the
>> +	 * data to be written to avoid these cases.
>> +	 */
>> +	if (mask) {
>> +		u32 w1c_bits = thunder_pem_bridge_w1c_bits(where);
>> +
>> +		if (w1c_bits) {
>> +			mask &= w1c_bits;
>> +			val &= ~mask;
>> +		}
>> +	}
>> +
>> +	/*
>> +	 * Low order bits are the config address, the high order 32
>> +	 * bits are the data to be written.
>> +	 */
>> +	write_val = where & ~3ull;
>> +	write_val |= (((u64)val) << 32);
>> +	writeq(write_val, pem_pci->pem_reg_base + PEM_CFG_WR);
>> +	return PCIBIOS_SUCCESSFUL;
>> +}
[...]

  reply	other threads:[~2016-03-07 18:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-04 22:31 [PATCH v7 0/3] Add host controller drivers for Cavium ThunderX PCI David Daney
2016-03-04 22:31 ` [PATCH v7 1/3] PCI: generic: Refactor code to enable reuse by other drivers David Daney
2016-03-04 22:31 ` [PATCH v7 2/3] pci, pci-thunder-pem: Add PCIe host driver for ThunderX processors David Daney
2016-03-05 15:54   ` Yury Norov
2016-03-07 18:02     ` David Daney [this message]
2016-03-04 22:31 ` [PATCH v7 3/3] pci, pci-thunder-ecam: Add driver for ThunderX-pass{1,2} on-chip devices David Daney
2016-03-11 22:18 ` [PATCH v7 0/3] Add host controller drivers for Cavium ThunderX PCI Bjorn Helgaas
2016-03-11 22:22   ` David Daney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=56DDC220.5080003@caviumnetworks.com \
    --to=ddaney@caviumnetworks.com \
    --cc=bhelgaas@google.com \
    --cc=dann.frazier@canonical.com \
    --cc=david.daney@cavium.com \
    --cc=ddaney.cavm@gmail.com \
    --cc=devicetree@vger.kernel.org \
    --cc=galak@codeaurora.org \
    --cc=ijc+devicetree@hellion.org.uk \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=pawel.moll@arm.com \
    --cc=robh+dt@kernel.org \
    --cc=will.deacon@arm.com \
    --cc=ynorov@caviumnetworks.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox