From patchwork Tue Oct 7 14:48:32 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcel Apfelbaum X-Patchwork-Id: 5046541 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 0DF2AC11AB for ; Tue, 7 Oct 2014 14:49:40 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 178B5201F2 for ; Tue, 7 Oct 2014 14:49:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id F209920148 for ; Tue, 7 Oct 2014 14:49:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753539AbaJGOtg (ORCPT ); Tue, 7 Oct 2014 10:49:36 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50529 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752998AbaJGOtg (ORCPT ); Tue, 7 Oct 2014 10:49:36 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s97EnXDN022662 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 7 Oct 2014 10:49:34 -0400 Received: from localhost.localdomain.com (vpn1-6-70.ams2.redhat.com [10.36.6.70]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s97EnNEK009886; Tue, 7 Oct 2014 10:49:26 -0400 From: Marcel Apfelbaum To: linux-pci@vger.kernel.org, bhelgaas@google.com Cc: linux-kernel@vger.kernel.org, marcel@redhat.com, mst@redhat.com, alex.williamson@redhat.com Subject: [PATCH v3] PCI: add kernel parameter to override devid<->driver mapping. Date: Tue, 7 Oct 2014 17:48:32 +0300 Message-Id: <1412693312-2449-1-git-send-email-marcel.a@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Scanning a lot of devices during boot requires a lot of time. On other scenarios there is a need to bind a driver to a specific slot. Binding devices to pci-stub driver does not work, as it will not differentiate between devices of the same type. Using some start scripts is error prone. The solution leverages driver_override functionality introduced by commit: 782a985d7af26db39e86070d28f987cad21313c0 Author: Alex Williamson Date: Tue May 20 08:53:21 2014 -0600 PCI: Introduce new device binding path using pci_dev.driver_override In order to bind PCI slots to specific drivers use: pci=driver[xxxx:xx:xx.x]=foo,driver[xxxx:xx:xx.x]=bar,... Signed-off-by: Marcel Apfelbaum --- v2 -> v3: - Corrected subject line v1 -> v2: - Addressed Michael S. Tsirkin comments - Removed 32 slots limitation - Better handling of memory allocation failures (preferred BUG_ON over error messages) - Addressed Alex Williamson's comments: - Modified commit message to show parameter usage more clear. - I preferred to re-use parse_args instead of manually using strstr in order to better comply with command line parsing rules. - I didn't use any locking when parsing the command line args (see parse_done usage) assuming that first call will be early in system boot and no race can occur. Please correct me if I am wrong. Notes: - I have further ideas on top of this patch based on your reviews. I thought of: - Use wildcards to specify entire buses/devices, something like: driver[0001:02:*.*]=pci-stub - Use comma to separate several devices: driver[0001:02:03.4,0001:02:04.0,...]=pci-stub - Make domain optional: driver[00:03.0]=pci-stub Comments will be appreciated, Thanks, Marcel Documentation/kernel-parameters.txt | 4 ++ drivers/pci/bus.c | 86 +++++++++++++++++++++++++++++++++++++ drivers/pci/pci.c | 2 + 3 files changed, 92 insertions(+) diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt index 5ae8608..c1cbb4c 100644 --- a/Documentation/kernel-parameters.txt +++ b/Documentation/kernel-parameters.txt @@ -2631,6 +2631,10 @@ bytes respectively. Such letter suffixes can also be entirely omitted. pcie_scan_all Scan all possible PCIe devices. Otherwise we only look for one device below a PCIe downstream port. + driver Provide an override to the devid<->driver mapping + for a specific slot. + Bind PCI slot 0001:02:03.4 to pci-stub by: + driver[0001:02:03.4]=pci-stub pcie_aspm= [PCIE] Forcibly enable or disable PCIe Active State Power Management. diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c index 73aef51..d2c0721 100644 --- a/drivers/pci/bus.c +++ b/drivers/pci/bus.c @@ -230,6 +230,91 @@ EXPORT_SYMBOL(pci_bus_alloc_resource); void __weak pcibios_resource_survey_bus(struct pci_bus *bus) { } +#define DRIVER_OVERRIDE_NAME_LENGTH 64 + +struct driver_override_entry { + unsigned int domain; + unsigned char bus; + unsigned int devfn; + char driver_name[DRIVER_OVERRIDE_NAME_LENGTH]; + struct list_head list; +}; + +static LIST_HEAD(driver_override_entries); + +static int pci_device_parse_driver_override(char *param, char *val, + const char *unused) +{ + unsigned int domain, bus, dev, fn; + char driver_name[DRIVER_OVERRIDE_NAME_LENGTH]; + char format[] = "[%4x:%2x:%2x.%2x]=%%%ds"; + struct driver_override_entry *entry; + int ret; + + while (val) { + char *k = strchr(val, ','); + + if (k) + *k++ = 0; + + if (strncmp(val, "driver", 6)) { + val = k; + continue; + } + + sprintf(format + 18, "%%%ds", DRIVER_OVERRIDE_NAME_LENGTH); + ret = sscanf(val + 6, format, &domain, &bus, &dev, &fn, + driver_name); + if (ret != 5) { + pr_err("PCI: Invalid command line: %s\n", val); + val = k; + continue; + } + + entry = kzalloc(sizeof(*entry), GFP_KERNEL); + BUG_ON(!entry); + + INIT_LIST_HEAD(&entry->list); + entry->domain = domain; + entry->bus = bus && 0xff; + entry->devfn = ((dev & 0x1f) << 3) | (fn & 0x7); + memcpy(entry->driver_name, driver_name, sizeof(driver_name)); + + list_add_tail(&entry->list, &driver_override_entries); + val = k; + } + + return 0; +} + +static void pci_device_setup_driver_override(struct pci_dev *dev) +{ + static int parse_done; + struct driver_override_entry *entry; + + if (!parse_done) { + char *cmdline = kstrdup(saved_command_line, GFP_KERNEL); + + BUG_ON(!cmdline); + + parse_args("pci", cmdline, NULL, + 0, 0, 0, &pci_device_parse_driver_override); + kfree(cmdline); + parse_done = 1; + } + + list_for_each_entry(entry, &driver_override_entries, list) { + if (pci_domain_nr(dev->bus) != entry->domain || + dev->bus->number != entry->bus || + dev->devfn != entry->devfn) + continue; + + dev->driver_override = kstrdup(entry->driver_name, GFP_KERNEL); + BUG_ON(!dev->driver_override); + break; + } +} + /** * pci_bus_add_device - start driver for a single device * @dev: device to add @@ -245,6 +330,7 @@ void pci_bus_add_device(struct pci_dev *dev) * are not assigned yet for some devices. */ pci_fixup_device(pci_fixup_final, dev); + pci_device_setup_driver_override(dev); pci_create_sysfs_dev_files(dev); pci_proc_attach_device(dev); diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index b7678be..6bd7844 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -4468,6 +4468,8 @@ static int __init pci_setup(char *str) pcie_bus_config = PCIE_BUS_PEER2PEER; } else if (!strncmp(str, "pcie_scan_all", 13)) { pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS); + } else if (!strncmp(str, "driver", 6)) { + /* lazy evaluation by the pci subsystem */ } else { printk(KERN_ERR "PCI: Unknown option `%s'\n", str);