From patchwork Fri Sep 14 19:54:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 970070 X-Patchwork-Delegate: lorenzo.pieralisi@arm.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-pci-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=networkplumber.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=networkplumber-org.20150623.gappssmtp.com header.i=@networkplumber-org.20150623.gappssmtp.com header.b="aG+adCH8"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 42BmTB3f3Wz9s4V for ; Sat, 15 Sep 2018 05:55:10 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728034AbeIOBLE (ORCPT ); Fri, 14 Sep 2018 21:11:04 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:42209 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727160AbeIOBLE (ORCPT ); Fri, 14 Sep 2018 21:11:04 -0400 Received: by mail-pf1-f196.google.com with SMTP id l9-v6so4757035pff.9 for ; Fri, 14 Sep 2018 12:55:06 -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=n2b/aaVqdsOssV1NfiL7wO8EwqRy3mAFndQDH/wDvS8=; b=aG+adCH8Jknphiq4tJ++dOyVhVrO/4OcBz5C1XoGcpj8N9HzUT4NhZhz042hPRnhGA odm6PwJpOtbe4Z2iKDcmlySu/b36Pzsn+A3FSfObIkk9GbFpvoCIXD2pmrVcwucTmpbo svqeifSCxYzqM3eAADcg/MD4xgqJ8f29OaF1afD8FKYmidKbgWL6m5KkSgVNaAEyo18n LmbwhkfQPcCg9aFr1sTbnDh2MvyfXsauMPsICKrGzYDMNiE4Pel0NG39Kdhg+TEZZeQf KbHkp0j15MH27i9bwRvRxoLC0Bt67oYyJr5v1D4F7wqg4anycfQ2Z4TNOxNqVz6DeRlI SDzA== 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=n2b/aaVqdsOssV1NfiL7wO8EwqRy3mAFndQDH/wDvS8=; b=ERIWKlPHR5fawe73QuuBTSwwrV6SQRnuxuguN/4I/X9YGGPaTdhPr8sI0eHaaj6RjG mTQZ4r0EZuZlt4/6dRn31ySbfnKc83VPe10WdvfG8A4+HMkjdg1fLkD/ckNzIaHHt7om NqGrXX0NIvsPU5OIEEIrNVkz+tFruwobCQuJYFZ3OZtZvIGi/VUqb4o7wxhnu56o89ZJ EROKgdmZtfcnqf9XuMfK7tEuMHRldXne/vigapD5kvcAt/9vrfSlq16pK8l524ph1thk 27sWEIvRhgoy9ViOT30W0h64FIv/qzOYvqBqJzLs8PMznSdsMTHzFFqomRpwXAEt88Q0 eN8Q== X-Gm-Message-State: APzg51DEV2Bf5A3XrVuxw9nx+I7/dCjr1Dcl92GkP4pVgyXdfHjaFNkK V+V955FMz3HVYm3jYqYCYyBUKQ== X-Google-Smtp-Source: ANB0VdZJYy1Ly2zXegcmw4ZDpEu5yMzxz6Se/4ni47eeuDDr+tuVRWVdS4b9J6f3H8adtknyXFJMmQ== X-Received: by 2002:a63:9b09:: with SMTP id r9-v6mr13248317pgd.324.1536954906359; Fri, 14 Sep 2018 12:55:06 -0700 (PDT) Received: from xeon-e3.lan (204-195-22-127.wavecable.com. [204.195.22.127]) by smtp.gmail.com with ESMTPSA id z8-v6sm9394338pfe.163.2018.09.14.12.55.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 14 Sep 2018 12:55:05 -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 v2 1/2] PCI: hv: support reporting serial number as slot information Date: Fri, 14 Sep 2018 12:54:56 -0700 Message-Id: <20180914195457.20433-2-sthemmin@microsoft.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180914195457.20433-1-sthemmin@microsoft.com> References: <20180914195457.20433-1-sthemmin@microsoft.com> Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org 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 maybe some cases where serial number is not unique such as when using GPU's. But the PCI slot infrastructure will handle that. 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). Signed-off-by: Stephen Hemminger --- drivers/pci/controller/pci-hyperv.c | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c index c00f82cc54aa..ee80e79db21a 100644 --- a/drivers/pci/controller/pci-hyperv.c +++ b/drivers/pci/controller/pci-hyperv.c @@ -89,6 +89,9 @@ static enum pci_protocol_version_t pci_protocol_version; #define STATUS_REVISION_MISMATCH 0xC0000059 +/* space for 32bit serial number as string */ +#define SLOT_NAME_SIZE 11 + /* * Message Types */ @@ -494,6 +497,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 +1461,34 @@ static void prepopulate_bars(struct hv_pcibus_device *hbus) spin_unlock_irqrestore(&hbus->device_list_lock, flags); } +/* + * Assign entries in sysfs pci slot directory. + * + * Note that this function does not need to lock the children list + * because it is called from pci_devices_present_work which + * is serialized with hv_eject_device_work because they are on the + * same ordered workqueue. Therefore hbus->children list will not change + * even when pci_create_slot sleeps. + */ +static void hv_pci_assign_slots(struct hv_pcibus_device *hbus) +{ + struct hv_pci_dev *hpdev; + char name[SLOT_NAME_SIZE]; + int slot_nr; + + 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); + } +} + /** * create_root_hv_pci_bus() - Expose a new root PCI bus * @hbus: Root PCI bus, as understood by this driver @@ -1480,6 +1512,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 +1775,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 +1892,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 Fri Sep 14 19:54:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 970071 X-Patchwork-Delegate: lorenzo.pieralisi@arm.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-pci-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=networkplumber.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=networkplumber-org.20150623.gappssmtp.com header.i=@networkplumber-org.20150623.gappssmtp.com header.b="GMwIFx98"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 42BmTC1VSsz9s3l for ; Sat, 15 Sep 2018 05:55:11 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728089AbeIOBLG (ORCPT ); Fri, 14 Sep 2018 21:11:06 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:39267 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728031AbeIOBLF (ORCPT ); Fri, 14 Sep 2018 21:11:05 -0400 Received: by mail-pf1-f196.google.com with SMTP id j8-v6so4772479pff.6 for ; Fri, 14 Sep 2018 12:55:08 -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=XD4JFU/JN8L52NBKfPrl7RuuqU+ICMRPUxzBZLAGHfg=; b=GMwIFx98zgeahH1YndExLDANxEJkJ4f4bzJgF7gZ/D4fkppH2hv3OflPZWeGBJYRJG fS+zl5VNekFH/Jn7+zxuCAnswfgZb5t8AS2668rkX2/qEnViLIYs40hD48TP39+b0iZB TBiOfPE+G6/T/SNil8rf0tIm5sg/1jkPXmz3TmXbMZc3aaxY5pS4sRk5etV8RoDK5eJ+ c8N+cIT+5lochhEOMg9aLNH2jTnwwFd7KeEDZzP/M8KY2zFqQr/QuM4l1HBE/5GW4/SH H/WYrDuOTuWZh6B/UvMk+mXAtFF0zOJM+SNIeo7XBOHaxfAMiSmWNlROxz6fojB7nheR 3uZQ== 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=XD4JFU/JN8L52NBKfPrl7RuuqU+ICMRPUxzBZLAGHfg=; b=ujvNzxNebrjibEiKmEKPzVUJt1ipfRZRylbx4vWaFSYRBtsmLSSAs9r4pN6UTQReVg o2jU1hQGK2GZDcKv8jVfFrPGJRPWwawuNJeDCReT9jdqzRfAj9xdMme1uxMI4hC/zmMC O/KY117bcbVjJgSqI6TQ4yz2gdba2Cjtc9Krr9ODztWau4lMrwKZzfnvqRce6m33RS9e u7wFXlJEMAZtGeJ//UPNQ89m4k4ffatrn8mXxOvwu650CNxlr3q1ryIgXf8MLbLNuq5F LS37O+Lj/7jhsTU2lKbej2kTOoG5RdTlkjiBcWmIyq6bgbQjrHYHp3cevuYo3gJUi9S6 aGtA== X-Gm-Message-State: APzg51BDoBeABXKUloTc74XIcsjf3VP38wadyNUniZUrS62s2g7ea3Pk EYnwJWnSymRHzrdxQmCTcm4jjg== X-Google-Smtp-Source: ANB0Vdb9l4wlVXbBKZeWSQC9fANVeRdm3ejCmFa4DIjvGWX4GaOMftgRl+m3/R+Gj0JzVFd7Hoodew== X-Received: by 2002:a63:fc55:: with SMTP id r21-v6mr11544816pgk.377.1536954907751; Fri, 14 Sep 2018 12:55:07 -0700 (PDT) Received: from xeon-e3.lan (204-195-22-127.wavecable.com. [204.195.22.127]) by smtp.gmail.com with ESMTPSA id z8-v6sm9394338pfe.163.2018.09.14.12.55.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 14 Sep 2018 12:55:06 -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 v2 2/2] hv_netvsc: pair VF based on serial number Date: Fri, 14 Sep 2018 12:54:57 -0700 Message-Id: <20180914195457.20433-3-sthemmin@microsoft.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180914195457.20433-1-sthemmin@microsoft.com> References: <20180914195457.20433-1-sthemmin@microsoft.com> Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org Matching network device based on MAC address is problematic since a non VF network device can be creted with a duplicate MAC address causing confusion and problems. The VMBus API does provide 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;