Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753115AbcCGSC0 (ORCPT ); Mon, 7 Mar 2016 13:02:26 -0500 Received: from mail-bn1bbn0100.outbound.protection.outlook.com ([157.56.111.100]:10594 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752506AbcCGSCQ (ORCPT ); Mon, 7 Mar 2016 13:02:16 -0500 Authentication-Results: caviumnetworks.com; dkim=none (message not signed) header.d=none;caviumnetworks.com; dmarc=none action=none header.from=caviumnetworks.com; Message-ID: <56DDC220.5080003@caviumnetworks.com> Date: Mon, 7 Mar 2016 10:02:08 -0800 From: David Daney User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130625 Thunderbird/17.0.7 MIME-Version: 1.0 To: Yury Norov CC: David Daney , , , Bjorn Helgaas , Rob Herring , Pawel Moll , Mark Rutland , Ian Campbell , Kumar Gala , Will Deacon , , , Dann Frazier , David Daney Subject: Re: [PATCH v7 2/3] pci, pci-thunder-pem: Add PCIe host driver for ThunderX processors. References: <1457130708-3231-1-git-send-email-ddaney.cavm@gmail.com> <1457130708-3231-3-git-send-email-ddaney.cavm@gmail.com> <20160305155409.GA2532@yury-N73SV> In-Reply-To: <20160305155409.GA2532@yury-N73SV> Content-Type: text/plain; charset="ISO-8859-1"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [64.2.3.194] X-ClientProxiedBy: BLUPR07CA0045.namprd07.prod.outlook.com (10.255.223.158) To BN4PR07MB2131.namprd07.prod.outlook.com (25.164.63.13) X-MS-Office365-Filtering-Correlation-Id: ce42c99c-c81b-4687-b2c4-08d346b29ccc X-Microsoft-Exchange-Diagnostics: 1;BN4PR07MB2131;2:84rvfNhLqLuu1v1iZkkOEQ6FWWSqyDlGpnS0taary3RtXoqf2X/G3ggLDdKh4EoGAgS0jSdgClIgXmxQDZpXAPhkSYksthCBQnAZZ9oG1NSCCgYOercbs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eAy5mncrPC+1VuwA7RyDYGTKl9B6R5NxuU9warpTLJYXGIHWUzgLHjeQk5x2qDUIsvQ6+fNscvBteXfBzWCf0juzSYqp8NqQ3kKlBHOg3SKiafG7lb0U5095Iftidgl1mRiCeuh80I6yxVXI1FxsCeU1V3biPcPOJm6pX+H5bZbONex2JKrpM= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046);SRVR:BN4PR07MB2131;BCL:0;PCL:0;RULEID:;SRVR:BN4PR07MB2131; X-Microsoft-Exchange-Diagnostics: 1;BN4PR07MB2131;4:oAPAd3kGf9DOXCF6/JFC9zxvZ3f3Ye/nP71etXTfeVLYM+7hJqNYfBFQK+tHasUnPt0yl+pPTtoZyMIxNsdX/teSV8lBmFlSj3hgud2qOzyuYre2v75nmE9nyAa5c7szKMAp03SnIlJx2PsnWuZw1KodTbi8sytQ1i2xAveQAJ2ZX5rZWzMH9dWfaZqto8a6IecHW7wy/mDHGbc6Ut+ClexS+NTlDnhmviwGDw6rywAVB9pXsQc5WMIuoQ6+cfaReusuzjz0m55/5KWFSJy+Mt3pp0sRt2RPBYQvwW+J4NqXoBwJiX47yIeFDZo6CybF9honnAgdaAMcWjT5fqDOTxP2w5bWe2yNchbHpvLYd3X5P5PDL58/goFd9DUX1AqW X-Forefront-PRVS: 087474FBFA X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(479174004)(377454003)(24454002)(36756003)(230700001)(50986999)(586003)(76176999)(3846002)(1096002)(2906002)(54356999)(87266999)(65816999)(4326007)(42186005)(83506001)(110136002)(81166005)(77096005)(50466002)(4001450100002)(6116002)(5004730100002)(189998001)(87976001)(66066001)(122386002)(33656002)(59896002)(230783001)(47776003)(53416004)(92566002)(40100003)(65956001)(65806001)(2950100001)(4001350100001)(23756003)(80316001)(5008740100001);DIR:OUT;SFP:1101;SCL:1;SRVR:BN4PR07MB2131;H:dl.caveonetworks.com;FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?iso-8859-1?Q?1;BN4PR07MB2131;23:FIWMxqM1/vd0pe07aQJbJiyf0Q8pUwTz44AcTqv?= =?iso-8859-1?Q?PpeVG4mwXTlrZGWKHq0QoFZ4zzdQ709M3M1jxr7XOx9Un/ox0WKH8GoFV9?= =?iso-8859-1?Q?AfXuAgK33R6eH7WsmSqN8d9qwEZuxEpUI7hpoBa1JuLqf9TPysxorAfNkR?= =?iso-8859-1?Q?wZ/GN0RaiD6xKVuUvHRv7Vloutrv6IzdJLYl9/E+sREGueJucx0G55t7Wo?= =?iso-8859-1?Q?1wuSsXqDB9AzzdPbi6XMaoeL+4TPzRto9BEhPyx8S2panMLXAWxGROG8ju?= =?iso-8859-1?Q?IZ9xx1574oVVcO2wPdL2ULbtoIkkBuNLvwQOVVshmZg/MW0sUqJKxceIyo?= =?iso-8859-1?Q?xjFmez6ISyyxqwOpoQ4A7QkulOF2j0VjZRTiBha5YT1AdLg+S5HfvsfSRj?= =?iso-8859-1?Q?FxotuVGhaC2t5M5PFHrO9L6GgwhqZ1kgstJmQYhiyVB8TaCOs7Zqts3BJZ?= =?iso-8859-1?Q?Ak06tojF0A3k+jfCeeS9WlWH+Ryu6zx7ec6BQ4zLAo/9RjT1ULwUSgStz9?= =?iso-8859-1?Q?EsPLm76lCpFjgzqy/lljAdXA6STz4no6/dQ3s3IT3julo9j81CLFgKz3OP?= =?iso-8859-1?Q?5cHYkxB/saoD9oJCErl8o5dkun4KGqi2ov8jVLwvNHSlejmCpfvsajqtbZ?= =?iso-8859-1?Q?9XHTPXYg9DwSVFXRA8zaNLDohn77hi18AE5RoaiKN1tC41fD7/Km89mTtt?= =?iso-8859-1?Q?535cd2Cuy8aJ1ucgLynEuu+noBF75FDmQQWNSN4YJo/sAZm5zX2uSmF3hH?= =?iso-8859-1?Q?yzBEJefEDGg7E2qNCShKUHfi2WsTf59ZvkxM+V7yX/Wm8hXk/DeXKjiUPn?= =?iso-8859-1?Q?cBYP9oHBW0iCyBRbf7LEtqxV1B6PfG5Nt+msNXDA9fb0aihGD05Svj1pe7?= =?iso-8859-1?Q?3k7wSPXTBaL1OdRa87QXko84sa+N4TaQ1y42CvPiPtfx4UyD0TMjD0xXB2?= =?iso-8859-1?Q?rKpZ/7BGXj/pmeO9ucU2llQAtmFGcvNiZ1DxUEvHpFNqeHvcIORB4E4TVd?= =?iso-8859-1?Q?hRAEk3dC/Pqly/DSMYv/BFVUtjBWUje1uV25pWn1Wpb1CeGJqhSVyhK5kl?= =?iso-8859-1?Q?8nJfDNFMYdkICIy6ySh3t/HeZigMlx3dvYIjhTJinmn622qOIqNhTUqKdL?= =?iso-8859-1?Q?Bda/1JoTbTJjKtbUfH//c1vuSxOrFCCYg9VNhbapiAfIxtNOTIHd8kNmBz?= =?iso-8859-1?Q?cniVRQoXNSMGj9VRyq/ot0Ijqu4dM0y7w=3D=3D?= X-Microsoft-Exchange-Diagnostics: 1;BN4PR07MB2131;5:PeomKKLP27zLkFRewYyTqNtg6+WK1cGA7k+587MZDGarGyvTgW2sXlgT2SNvw68LtPhYngjAbng5XLTUL9Q77VNKe9Yq38WwY7ig3JOCON3JGUZ/4Y6HrNEfvNIHVIDcJlJ/pQtt3BMO23fjY1AVUg==;24:SnhU9bYHb+Ev7DULh0PHhXiJsAML+JAj1jZmhPA1ZQjQ1LGw5WKLXAOUtXFMOe2Eb56XhH8977feLm0D4WsiSIv/qCAiBCypMMA3EGLXUoM= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: caviumnetworks.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Mar 2016 18:02:12.4248 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN4PR07MB2131 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 5125 Lines: 166 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; >> +} [...]