From patchwork Wed Aug 29 16:24:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 10580633 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0509317DE for ; Wed, 29 Aug 2018 16:25:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EA45B2B73D for ; Wed, 29 Aug 2018 16:25:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DE2F42B737; Wed, 29 Aug 2018 16:25:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6FEED2B6BB for ; Wed, 29 Aug 2018 16:25:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729247AbeH2UWo (ORCPT ); Wed, 29 Aug 2018 16:22:44 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:40526 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729189AbeH2UWn (ORCPT ); Wed, 29 Aug 2018 16:22:43 -0400 Received: by mail-pl1-f193.google.com with SMTP id s17-v6so2512780plp.7 for ; Wed, 29 Aug 2018 09:25:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Gj1Nwu3PYvQsiMIFhh9XUT/R4gQi4SYlSLS/MiUu0/M=; b=MUq2vd0s7KsxgwOlNNkUqM1vFNW3/Zhc2RC2/hDDEfxeR5FlhQT6kW4uEVHBRy3w0d OGHmG/pCuZkD6sB06iDoIqswvTbNe62L7hioSjZ57OcHwQJwqATdyhM9GEurQpGZpZPN QCPpZB76DJORNxNo6QPdUm/Xn8dejqZ639F6Dg+egW503sOwLVZOVz1fbfIf3f1gEsMi FDB5t/hdhW4qm7TY7oHbvgIU94NQTundFIkNfNimanCcS3HVZUYCaH6iq9Ew6wGuSAOl x+W4aq10K77w9mt/VfQhERsqXs5XE5F00AnPAOpGVsAfVl9p+JDECH4Pl156tmA1uj8i /sTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Gj1Nwu3PYvQsiMIFhh9XUT/R4gQi4SYlSLS/MiUu0/M=; b=Mm/HL5QLX5e93OjkTXtQIdZW85vEY0fgzA+yDmdD4KLXSwlDkRpiNiHRTgfslhm/Vv ALMi6U9QIdPl5sXw+Tg1lNakcoM3yqqqDSbCw5xyrKKYJVkG6qTKeXUar9fd6t2WMsTb ZPSx80EN35TMW3HqpPVLmcaxa46bT57j6uCsccTR1m8fVG+n1lDRSTzkTdzx+s0HFli6 XU5OUiqbsdB9U7A5LeAz+qLYzXvxcmda1jvzAFVKOQXG/UFEMVoS4mv0uxbVwQIZgpgv 4y8McmuPusOG/jYpzvWawS67crXwe9E6xRy66k8FdFGo47W+KOWM65r1aMMZ4Q/aKIAY dXIw== X-Gm-Message-State: APzg51DKlkAITvLQMUS9gOGqAwpRmdYOIQBTNvGS3RCyCDVOcZ8W5FPH RIUfVe8pM7RlLOuLezcwkcL5mw== X-Google-Smtp-Source: ANB0VdZcdu1VBoI0EfRH+FgZrwUvjVldphkJOiEmZZv2fWi6oQFhPE3x+wEcKa+JwGqDLN0cnwOOFQ== X-Received: by 2002:a17:902:8215:: with SMTP id x21-v6mr6514489pln.175.1535559902040; Wed, 29 Aug 2018 09:25:02 -0700 (PDT) Received: from xeon-e3.lan (204-195-22-127.wavecable.com. [204.195.22.127]) by smtp.gmail.com with ESMTPSA id y18-v6sm5742943pfl.90.2018.08.29.09.25.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Aug 2018 09:25:00 -0700 (PDT) From: Stephen Hemminger X-Google-Original-From: Stephen Hemminger To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com Cc: devel@linuxdriverproject.org, netdev@vger.kernel.org, linux-pci@vger.kernel.org Subject: [PATCH net-next 1/2] PCI: hv: support reporting serial number as slot information Date: Wed, 29 Aug 2018 09:24:51 -0700 Message-Id: <20180829162452.25805-2-sthemmin@microsoft.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180829162452.25805-1-sthemmin@microsoft.com> References: <20180829162452.25805-1-sthemmin@microsoft.com> Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The Hyper-V host API for PCI provides a unique "serial number" which can be used as basis for sysfs PCI slot table. This can be useful for cases where userspace wants to find the PCI device based on serial number. When an SR-IOV NIC is added, the host sends an attach message with serial number. The kernel doesn't use the serial number, but it is useful when doing the same thing in a userspace driver such as the DPDK. By having /sys/bus/pci/slots/N it provides a direct way to find the matching PCI device. There may be some cases where serial number is not unique such as when using GPU's. But the PCI slot infrastructure will handle that by adding suffix "2-1" etc. This has a side effect which may also be useful. The common udev network device naming policy uses the slot information (rather than PCI address). This causes udev to give shorter network device names for VF devices. It does not break applications or startup because the VF device must never be configured directly. Signed-off-by: Stephen Hemminger --- drivers/pci/controller/pci-hyperv.c | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c index c00f82cc54aa..e6a6c1146a41 100644 --- a/drivers/pci/controller/pci-hyperv.c +++ b/drivers/pci/controller/pci-hyperv.c @@ -89,6 +89,8 @@ static enum pci_protocol_version_t pci_protocol_version; #define STATUS_REVISION_MISMATCH 0xC0000059 +#define SLOT_NAME_SIZE 21 + /* * Message Types */ @@ -494,6 +496,7 @@ struct hv_pci_dev { struct list_head list_entry; refcount_t refs; enum hv_pcichild_state state; + struct pci_slot *pci_slot; struct pci_function_description desc; bool reported_missing; struct hv_pcibus_device *hbus; @@ -1457,6 +1460,28 @@ static void prepopulate_bars(struct hv_pcibus_device *hbus) spin_unlock_irqrestore(&hbus->device_list_lock, flags); } +static void hv_pci_assign_slots(struct hv_pcibus_device *hbus) +{ + struct hv_pci_dev *hpdev; + char name[SLOT_NAME_SIZE]; + unsigned long flags; + int slot_nr; + + spin_lock_irqsave(&hbus->device_list_lock, flags); + list_for_each_entry(hpdev, &hbus->children, list_entry) { + if (hpdev->pci_slot) + continue; + + slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot)); + snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser); + hpdev->pci_slot = pci_create_slot(hbus->pci_bus, slot_nr, + name, NULL); + if (!hpdev->pci_slot) + pr_warn("pci_create slot %s failed\n", name); + } + spin_unlock_irqrestore(&hbus->device_list_lock, flags); +} + /** * create_root_hv_pci_bus() - Expose a new root PCI bus * @hbus: Root PCI bus, as understood by this driver @@ -1480,6 +1505,7 @@ static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus) pci_lock_rescan_remove(); pci_scan_child_bus(hbus->pci_bus); pci_bus_assign_resources(hbus->pci_bus); + hv_pci_assign_slots(hbus); pci_bus_add_devices(hbus->pci_bus); pci_unlock_rescan_remove(); hbus->state = hv_pcibus_installed; @@ -1742,6 +1768,7 @@ static void pci_devices_present_work(struct work_struct *work) */ pci_lock_rescan_remove(); pci_scan_child_bus(hbus->pci_bus); + hv_pci_assign_slots(hbus); pci_unlock_rescan_remove(); break; @@ -1858,6 +1885,9 @@ static void hv_eject_device_work(struct work_struct *work) list_del(&hpdev->list_entry); spin_unlock_irqrestore(&hpdev->hbus->device_list_lock, flags); + if (hpdev->pci_slot) + pci_destroy_slot(hpdev->pci_slot); + memset(&ctxt, 0, sizeof(ctxt)); ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message; ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE; From patchwork Wed Aug 29 16:24:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 10580635 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CA9C014E1 for ; Wed, 29 Aug 2018 16:25:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BA0F02B6BC for ; Wed, 29 Aug 2018 16:25:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ADD542B72E; Wed, 29 Aug 2018 16:25:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4A6172B73D for ; Wed, 29 Aug 2018 16:25:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729263AbeH2UWo (ORCPT ); Wed, 29 Aug 2018 16:22:44 -0400 Received: from mail-pl1-f195.google.com ([209.85.214.195]:34540 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729262AbeH2UWo (ORCPT ); Wed, 29 Aug 2018 16:22:44 -0400 Received: by mail-pl1-f195.google.com with SMTP id f6-v6so2532025plo.1 for ; Wed, 29 Aug 2018 09:25:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=INcqbEG9mxxjTSWMc59XzO8oSUVX2NeKV4ogYcYOXBU=; b=OMNK42t5EhJz0zAUEZDmBO5uhXyCXlzHhogeHewZPVKSh50uQtBYSRE1Ne8cu4N9JG 85dR/USn5Y//gJbUEy/MR/rp0FJR5wGgVceiRSZLR0LH8wWQhOzm05ckgFIojWJ3y9Rc GjNVPllzwl8boTIuEcPX8f++i51JSRrNlJTBTgyy7LvxZonHjxYSkoFOQl3iUg1hoNvG LMr15ZFZzO2me3oN82GYOXMk4Xo3QjBmQNAIf9pRuOjrd6CuRKkEIewgIT7WbXz0m3IA UBlvCcJWLvigfK6lMbRzE80TGsYbUioc3l2dUwCx04w9EjCJf/Sny5WhUHNrMeH/paVp CUcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=INcqbEG9mxxjTSWMc59XzO8oSUVX2NeKV4ogYcYOXBU=; b=uX7Q3k6kwhtDdhsLEj3Pj8lfJLkGSvaG4XElyh/WWEB962nl5aT3oPIN7J4kNM0BcQ HISpNeSUcuS2ocscWkp22xAMzr9tatUCUBPoyFolUGp60+rBZgRnW2Oz9Ov/2z18ivh+ fty0zCVa4iFrgllQdm0x3ym8S7gYGc5aWBEvOb5+XkXp6vG2HajrKXWpJh+CYceELuKO vouhb4LkEkSFZRd4fWoXiEqCK/VllDVoLuGjbdOgTz5QPZArSNXDrIhrKdBdG8a6C3og luuxSHu+YMHweoUNKb74/K1sjZ806/QsQERIFb70yk4sDdl6TFgXhe89/sWPRi5Gp9Gg Ok2A== X-Gm-Message-State: APzg51D5gPDOeOrLiG6bljj3mNwkcyrqK1/6JOpp1fg2LSYKMOgMAjGB qJAtDymWi2E8CE6ZIS1JraNnaA== X-Google-Smtp-Source: ANB0Vdb3WM6gGxXzItpcCyJJF4DKBVgIdzhn2bqDkjtabf/m0UDLe5EyU43CSICZ0QHfPq//oyxpzw== X-Received: by 2002:a17:902:c6:: with SMTP id a64-v6mr6710562pla.180.1535559903399; Wed, 29 Aug 2018 09:25:03 -0700 (PDT) Received: from xeon-e3.lan (204-195-22-127.wavecable.com. [204.195.22.127]) by smtp.gmail.com with ESMTPSA id y18-v6sm5742943pfl.90.2018.08.29.09.25.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Aug 2018 09:25:02 -0700 (PDT) From: Stephen Hemminger X-Google-Original-From: Stephen Hemminger To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com Cc: devel@linuxdriverproject.org, netdev@vger.kernel.org, linux-pci@vger.kernel.org Subject: [PATCH net-next 2/2] hv_netvsc: pair VF based on serial number Date: Wed, 29 Aug 2018 09:24:52 -0700 Message-Id: <20180829162452.25805-3-sthemmin@microsoft.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180829162452.25805-1-sthemmin@microsoft.com> References: <20180829162452.25805-1-sthemmin@microsoft.com> Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Matching network device based on MAC address is problematic since a non-VF network device can be created with a duplicate MAC address causing confusion and problems. The VMBus API provides a serial number that is a better matching method. Signed-off-by: Stephen Hemminger --- drivers/net/hyperv/netvsc.c | 3 ++ drivers/net/hyperv/netvsc_drv.c | 58 +++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/drivers/net/hyperv/netvsc.c b/drivers/net/hyperv/netvsc.c index 31c3d77b4733..fe01e141c8f8 100644 --- a/drivers/net/hyperv/netvsc.c +++ b/drivers/net/hyperv/netvsc.c @@ -1203,6 +1203,9 @@ static void netvsc_send_vf(struct net_device *ndev, net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated; net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial; + netdev_info(ndev, "VF slot %u %s\n", + net_device_ctx->vf_serial, + net_device_ctx->vf_alloc ? "added" : "removed"); } static void netvsc_receive_inband(struct net_device *ndev, diff --git a/drivers/net/hyperv/netvsc_drv.c b/drivers/net/hyperv/netvsc_drv.c index 1121a1ec407c..9dedc1463e88 100644 --- a/drivers/net/hyperv/netvsc_drv.c +++ b/drivers/net/hyperv/netvsc_drv.c @@ -1894,20 +1894,6 @@ static void netvsc_link_change(struct work_struct *w) rtnl_unlock(); } -static struct net_device *get_netvsc_bymac(const u8 *mac) -{ - struct net_device_context *ndev_ctx; - - list_for_each_entry(ndev_ctx, &netvsc_dev_list, list) { - struct net_device *dev = hv_get_drvdata(ndev_ctx->device_ctx); - - if (ether_addr_equal(mac, dev->perm_addr)) - return dev; - } - - return NULL; -} - static struct net_device *get_netvsc_byref(struct net_device *vf_netdev) { struct net_device_context *net_device_ctx; @@ -2036,26 +2022,48 @@ static void netvsc_vf_setup(struct work_struct *w) rtnl_unlock(); } +/* Find netvsc by VMBus serial number. + * The PCI hyperv controller records the serial number as the slot. + */ +static struct net_device *get_netvsc_byslot(const struct net_device *vf_netdev) +{ + struct device *parent = vf_netdev->dev.parent; + struct net_device_context *ndev_ctx; + struct pci_dev *pdev; + + if (!parent || !dev_is_pci(parent)) + return NULL; /* not a PCI device */ + + pdev = to_pci_dev(parent); + if (!pdev->slot) { + netdev_notice(vf_netdev, "no PCI slot information\n"); + return NULL; + } + + list_for_each_entry(ndev_ctx, &netvsc_dev_list, list) { + if (!ndev_ctx->vf_alloc) + continue; + + if (ndev_ctx->vf_serial == pdev->slot->number) + return hv_get_drvdata(ndev_ctx->device_ctx); + } + + netdev_notice(vf_netdev, + "no netdev found for slot %u\n", pdev->slot->number); + return NULL; +} + static int netvsc_register_vf(struct net_device *vf_netdev) { - struct net_device *ndev; struct net_device_context *net_device_ctx; - struct device *pdev = vf_netdev->dev.parent; struct netvsc_device *netvsc_dev; + struct net_device *ndev; int ret; if (vf_netdev->addr_len != ETH_ALEN) return NOTIFY_DONE; - if (!pdev || !dev_is_pci(pdev) || dev_is_pf(pdev)) - return NOTIFY_DONE; - - /* - * We will use the MAC address to locate the synthetic interface to - * associate with the VF interface. If we don't find a matching - * synthetic interface, move on. - */ - ndev = get_netvsc_bymac(vf_netdev->perm_addr); + ndev = get_netvsc_byslot(vf_netdev); if (!ndev) return NOTIFY_DONE;