From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM01-BN3-obe.outbound.protection.outlook.com (mail-bn3nam01on0052.outbound.protection.outlook.com [104.47.33.52]) by dpdk.org (Postfix) with ESMTP id 126DFBB40 for ; Fri, 28 Oct 2016 14:31:06 +0200 (CEST) Received: from BN3PR0301CA0056.namprd03.prod.outlook.com (10.160.152.152) by SN2PR03MB2382.namprd03.prod.outlook.com (10.166.210.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.679.12; Fri, 28 Oct 2016 12:31:05 +0000 Received: from BN1BFFO11FD043.protection.gbl (2a01:111:f400:7c10::1:188) by BN3PR0301CA0056.outlook.office365.com (2a01:111:e400:401e::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.693.12 via Frontend Transport; Fri, 28 Oct 2016 12:31:05 +0000 Authentication-Results: spf=fail (sender IP is 192.88.168.50) smtp.mailfrom=nxp.com; nxp.com; dkim=none (message not signed) header.d=none;nxp.com; dmarc=fail action=none header.from=nxp.com;nxp.com; dkim=none (message not signed) header.d=none; Received-SPF: Fail (protection.outlook.com: domain of nxp.com does not designate 192.88.168.50 as permitted sender) receiver=protection.outlook.com; client-ip=192.88.168.50; helo=tx30smr01.am.freescale.net; Received: from tx30smr01.am.freescale.net (192.88.168.50) by BN1BFFO11FD043.mail.protection.outlook.com (10.58.144.106) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.1.679.5 via Frontend Transport; Fri, 28 Oct 2016 12:31:04 +0000 Received: from Tophie.ap.freescale.net ([10.232.14.87]) by tx30smr01.am.freescale.net (8.14.3/8.14.0) with ESMTP id u9SCPEMR019399; Fri, 28 Oct 2016 05:31:02 -0700 From: Shreyansh Jain To: Date: Fri, 28 Oct 2016 17:56:32 +0530 Message-ID: <1477657598-826-16-git-send-email-shreyansh.jain@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1477657598-826-1-git-send-email-shreyansh.jain@nxp.com> References: <1477581467-12588-1-git-send-email-shreyansh.jain@nxp.com> <1477657598-826-1-git-send-email-shreyansh.jain@nxp.com> X-EOPAttributedMessage: 0 X-Matching-Connectors: 131221314650241740; (91ab9b29-cfa4-454e-5278-08d120cd25b8); () X-Forefront-Antispam-Report: CIP:192.88.168.50; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(7916002)(2980300002)(1110001)(1109001)(339900001)(189002)(199003)(68736007)(33646002)(86362001)(77096005)(229853001)(2906002)(2351001)(92566002)(305945005)(97736004)(85426001)(47776003)(36756003)(11100500001)(6666003)(104016004)(5003940100001)(586003)(2950100002)(6916009)(19580405001)(19580395003)(110136003)(189998001)(5660300001)(50226002)(87936001)(105606002)(76176999)(50986999)(626004)(106466001)(8666005)(4326007)(7846002)(356003)(81166006)(50466002)(8936002)(81156014)(8676002)(48376002)(7059030)(403724002); DIR:OUT; SFP:1101; SCL:1; SRVR:SN2PR03MB2382; H:tx30smr01.am.freescale.net; FPR:; SPF:Fail; PTR:InfoDomainNonexistent; MX:1; A:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BN1BFFO11FD043; 1:b/HW/oPPR0Xqwusd2VhpAKd8hsfRLygMJSPWKQs1TgjUgCLGXt31r8/ZeD8kWHkqwAqxUWSrsEqxo8f1c9WNgcuYhLhAryB9wD2TzTae0jSv3RPW8Dv0+WjnXsuQh6t6b5c0gLvKE9rvMJU9GUA3Lkg32NGRNqNRj1fxWPvLwcKq8I+zt1OzeeX32vo0dI/kpQqFrcSHWNb/kLXruAOHZSbx7AVEyL7RrVqBtLgz8pvwo3iSIcT2M2Af6/7v5VFQovFji5ImZlWWKZUKFe7ldbLU429I9MTZh6eEvrz+ReG7gdtwcVMTAglFitkl/pO8cXT7QMMEEqLm8evImYFLSTiDyqMtO9frieIURtgY+N0oKoiRZWaO6CooIZNGAxO6zsBFXSMO+TeJcDQzZFSPHj80JnoAC6ceNj3PnbepbbOjOfN8Te1JHGOZj+XErFkn0CcpqD3+P9/HtXW+KxFLkGhxx5Zhb7P/lKxb54bMCRcqR8W8tJ/0qP4Imlyqz0fWHueRpC6gb/Z8xpF3GHaLg74dwnv+fatxtDaXvCaA1C5ofVgIw42KuiHkwNleZWDv92yG19auIQG+sOhKMolNmE7f7RLJ3rvVLbl3QyyJ0bgDeCDt7ngo0K6X15GItqLr8T/q3G34F59jIbe4jxWAhMT/V9FdCV273tOPX3zTSP5kVIZTM/R42vmVVajbPa7nFXu69B89dbmO8lhDDDPP0vc11okLQeuxRP+KJ/i6WtA= MIME-Version: 1.0 Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: cefc7924-7f8c-48c4-facc-08d3ff2e4926 X-Microsoft-Exchange-Diagnostics: 1; SN2PR03MB2382; 2:rgC+F1Dl0DH1iRMuugTiQStFX2JGu2YcLKa3SzAAOv1iCj6Bse+dTzbYAJteURIjU38fzZ3GBUDJ5JjpW7GW/CiaVyZ2AZVHv3Mx2LDNLweQpqzcook7GLfhTeiC5fqJzjLxHZGm8xXq3UulN8MpSnqFspzluWr7oJ/K/HA1Xof8lcVsyKWrtwUa0A0jJixOUSUYpWpLaNklm5rBFAH0Xg==; 3:wQzZQ1su1vHNzkPf9jgTYYn12GqJOWtVmKscet8my+Jc60NfsjeOuxT/22pqrPMxqhj1jz5FFdIAacAnq4WIWTYnkZskQCxmkgEhHUqxJxX9QK2dhpAc9NKAu6deOOgCMI/wXarIEmdrsWfXsNWuUZeoI5TX0f7xx/rLeBokARK2+Ow0rMAZAFXxa+3iqwA6K593zhSnDfQeGyhJgOC3tnKZmtOERN56qTGftJhmIZ9f51LajljrIuc6Iv/T2sjL; 25:9ewSzcKAB84SRPOgCPgF6cDnjIAqrFkb6kF6QNl8xGuIWFZ47z8Eaoj8Pt9tWTPJusvbKh/APmY8DxXElD4ZsFzde578p8Tvbj1LXK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hKBvUo0N1cEyKccIkYJAOyuTt9NwS0PpPrRT+p4iZXz6VVK6E47VO7cDulsUPQp8Lwtb6yWLGvwlFu/Dyh+sW4H1ktob+GB5MkSCmtjhjyLzGyQgMVqOu+oFzbV144h+9PEEdnNrKbNjuaCcP7RJfiL7MjZy/rv6rtc/ZiZBOMA71AtpcxDcAFLiIjcA/I9fFuT6c+oihh4BI2j+tet79c9llAb+HorUJhrANpJ+Mfp/scwbKZv4UGye4/6xGZXy4pau6kL+AlaRYIOjNF X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(185117386973197); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040176)(601004)(2401047)(13017025)(13015025)(13018025)(13024025)(5005006)(8121501046)(13023025)(3002001)(10201501046)(6055026); SRVR:SN2PR03MB2382; BCL:0; PCL:0; RULEID:(400006); SRVR:SN2PR03MB2382; X-Forefront-PRVS: 0109D382B0 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; SN2PR03MB2382; 23:NP2u37kKu3npwTe5UeR0i4n96XM6O3WxTobI/tN7J?= =?us-ascii?Q?qQpCBTaReD/Qbsux8Qr8p1NBB4i2A6/ZGXDSupvIQYoUXbf0kubcQ0gF90nd?= =?us-ascii?Q?/9S4KgHHS4oSXFVKSlKV05/Djj18SpyIpSthBMY/lEt8jPHwl+cT/XOTUXcK?= =?us-ascii?Q?EXuuI+wdekqJ3zeHDOTIugzzyWMGh5HgYV7d8R7mUzDq4R3aSfE1Ae1/W9na?= =?us-ascii?Q?mDMBUdTQTkEYlXjrQUE+Zmd9fL7eMU6y25Zngb9wi4bzUNBOGqHKNP4uSIj3?= =?us-ascii?Q?GIviEUGFCUrDtgpwyGzpEmawMD23N/s9cHSilGM2UJftgONm0swmeK4PFCrJ?= =?us-ascii?Q?ZRBqGb2mKY6SYrdad7q+ecQrFZMCAGu/CpGEBIyl2J/5UzC2v0TbAVJVWIPz?= =?us-ascii?Q?J+ZgKmFc1L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ange-CrossTenant-OriginalArrivalTime: 28 Oct 2016 12:31:04.8369 (UTC) X-MS-Exchange-CrossTenant-Id: 5afe0b00-7697-4969-b663-5eab37d5f47e X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=5afe0b00-7697-4969-b663-5eab37d5f47e; Ip=[192.88.168.50]; Helo=[tx30smr01.am.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN2PR03MB2382 Cc: Shreyansh Jain , thomas.monjalon@6wind.com, viktorin@rehivetech.com Subject: [dpdk-dev] [PATCH v7 15/21] eal/soc: add default scan for Soc devices X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 Oct 2016 12:31:06 -0000 From: Jan Viktorin Default implementation which scans the sysfs platform devices hierarchy. For each device, extract the ueven and convert into rte_soc_device. The information populated can then be used in probe to match against the drivers registered. Signed-off-by: Jan Viktorin [Shreyansh: restructure commit to be an optional implementation] Signed-off-by: Shreyansh Jain -- v5: - Update rte_eal_soc_scan to rte_eal_soc_scan_platform_bus - Fix comments over scan and match functions --- lib/librte_eal/common/include/rte_soc.h | 16 +- lib/librte_eal/linuxapp/eal/eal_soc.c | 315 ++++++++++++++++++++++++++++++++ 2 files changed, 329 insertions(+), 2 deletions(-) diff --git a/lib/librte_eal/common/include/rte_soc.h b/lib/librte_eal/common/include/rte_soc.h index 38f897d..8be3db7 100644 --- a/lib/librte_eal/common/include/rte_soc.h +++ b/lib/librte_eal/common/include/rte_soc.h @@ -64,7 +64,10 @@ TAILQ_HEAD(soc_driver_list, rte_soc_driver); /**< SoC drivers in D-linked Q. */ TAILQ_HEAD(soc_device_list, rte_soc_device); /**< SoC devices in D-linked Q. */ struct rte_soc_id { - const char *compatible; /**< OF compatible specification */ + union { + const char *compatible; /**< OF compatible specification */ + char *_compatible; + }; uint64_t priv_data; /**< SoC Driver specific data */ }; @@ -200,7 +203,16 @@ rte_eal_parse_soc_spec(const char *spec, struct rte_soc_addr *addr) } /** - * Default function for matching the Soc driver with device. Each driver can + * Helper function for scanning for new SoC devices on platform bus. + * + * @return + * 0 on success + * !0 on failure to scan + */ +int rte_eal_soc_scan_platform_bus(void); + +/** + * Helper function for matching the Soc driver with device. Each driver can * either use this function or define their own soc matching function. * This function relies on the compatible string extracted from sysfs. But, * a SoC might have different way of identifying its devices. Such SoC can diff --git a/lib/librte_eal/linuxapp/eal/eal_soc.c b/lib/librte_eal/linuxapp/eal/eal_soc.c index 3929a76..d8dfe97 100644 --- a/lib/librte_eal/linuxapp/eal/eal_soc.c +++ b/lib/librte_eal/linuxapp/eal/eal_soc.c @@ -48,6 +48,321 @@ #include #include +/** Pathname of SoC devices directory. */ +#define SYSFS_SOC_DEVICES "/sys/bus/platform/devices" + +static const char * +soc_get_sysfs_path(void) +{ + const char *path = NULL; + + path = getenv("SYSFS_SOC_DEVICES"); + if (path == NULL) + return SYSFS_SOC_DEVICES; + + return path; +} + +static char * +dev_read_uevent(const char *dirname) +{ + char filename[PATH_MAX]; + struct stat st; + char *buf; + ssize_t total = 0; + int fd; + + snprintf(filename, sizeof(filename), "%s/uevent", dirname); + fd = open(filename, O_RDONLY); + if (fd < 0) { + RTE_LOG(WARNING, EAL, "Failed to open file %s\n", filename); + return strdup(""); + } + + if (fstat(fd, &st) < 0) { + RTE_LOG(ERR, EAL, "Failed to stat file %s\n", filename); + close(fd); + return NULL; + } + + if (st.st_size == 0) { + close(fd); + return strdup(""); + } + + buf = malloc(st.st_size + 1); + if (buf == NULL) { + RTE_LOG(ERR, EAL, "Failed to alloc memory to read %s\n", + filename); + close(fd); + return NULL; + } + + while (total < st.st_size) { + ssize_t rlen = read(fd, buf + total, st.st_size - total); + if (rlen < 0) { + if (errno == EINTR) + continue; + + RTE_LOG(ERR, EAL, "Failed to read file %s\n", filename); + + free(buf); + close(fd); + return NULL; + } + if (rlen == 0) /* EOF */ + break; + + total += rlen; + } + + buf[total] = '\0'; + close(fd); + + return buf; +} + +static const char * +dev_uevent_find(const char *uevent, const char *key) +{ + const size_t keylen = strlen(key); + const size_t total = strlen(uevent); + const char *p = uevent; + + /* check whether it is the first key */ + if (!strncmp(uevent, key, keylen)) + return uevent + keylen; + + /* check 2nd key or further... */ + do { + p = strstr(p, key); + if (p == NULL) + break; + + if (p[-1] == '\n') /* check we are at a new line */ + return p + keylen; + + p += keylen; /* skip this one */ + } while (p - uevent < (ptrdiff_t) total); + + return NULL; +} + +static char * +strdup_until_nl(const char *p) +{ + const char *nl = strchr(p, '\n'); + if (nl == NULL) + return strdup(p); /* no newline, copy until '\0' */ + + return strndup(p, nl - p); +} + +static int +dev_parse_uevent(struct rte_soc_device *dev, const char *uevent) +{ + const char *of; + const char *compat_n; + char *err; + long n; + char compat[strlen("OF_COMPATIBLE_NNNN=")]; + long i; + + of = dev_uevent_find(uevent, "OF_FULLNAME="); + if (of == NULL) + return 1; /* don't care about this device */ + + dev->addr.fdt_path = strdup_until_nl(of); + if (dev->addr.fdt_path == NULL) { + RTE_LOG(ERR, PMD, + "Failed to alloc memory for fdt_path\n"); + return -1; + } + + RTE_LOG(DEBUG, EAL, "Detected device %s (%s)\n", + dev->addr.name, dev->addr.fdt_path); + + compat_n = dev_uevent_find(uevent, "OF_COMPATIBLE_N="); + if (compat_n == NULL) { + RTE_LOG(ERR, EAL, "No OF_COMPATIBLE_N found\n"); + return -1; + } + + n = strtoul(compat_n, &err, 0); + if (*err != '\n' && err != NULL) { + RTE_LOG(ERR, EAL, "Failed to parse OF_COMPATIBLE_N: %.10s\n", + err); + goto fail_fdt_path; + } + + if (n == 0) + return 1; /* cannot match anything */ + if (n > 9999) { /* match NNNN */ + RTE_LOG(ERR, EAL, "OF_COMPATIBLE_N is invalid: %ld\n", n); + goto fail_fdt_path; + } + + dev->id = calloc(n + 1, sizeof(*dev->id)); + if (dev->id == NULL) { + RTE_LOG(ERR, PMD, "Failed to alloc memory for ID\n"); + free(dev->addr.fdt_path); + return -1; + } + + for (i = 0; i < n; ++i) { + snprintf(compat, sizeof(compat), "OF_COMPATIBLE_%lu=", i); + const char *val; + + val = dev_uevent_find(uevent, compat); + if (val == NULL) { + RTE_LOG(ERR, EAL, "%s was not found\n", compat); + goto fail_id; + } + + dev->id[i]._compatible = strdup_until_nl(val); + if (dev->id[i]._compatible == NULL) { + RTE_LOG(ERR, PMD, + "Failed to alloc memory for compatible\n"); + goto fail_id; + } + + RTE_LOG(DEBUG, EAL, " compatible: %s\n", + dev->id[i].compatible); + } + + dev->id[n]._compatible = NULL; /* mark last one */ + + return 0; + +fail_id: + while (i-- >= 0) + free(dev->id[i]._compatible); + free(dev->id); +fail_fdt_path: + free(dev->addr.fdt_path); + return -1; +} + +static void +dev_content_free(struct rte_soc_device *dev) +{ + int i; + + if (dev->addr.fdt_path) + free(dev->addr.fdt_path); + + if (dev->id != NULL) { + for (i = 0; dev->id[i]._compatible; ++i) + free(dev->id[i]._compatible); + + free(dev->id); + dev->id = NULL; + } +} + +/** + * Scan one SoC sysfs entry, and fill the devices list from it. + * We require to have the uevent file with records: OF_FULLNAME and + * OF_COMPATIBLE array (with at least one entry). Otherwise, such device + * is skipped. + */ +static int +soc_scan_one(const char *dirname, const char *name) +{ + struct rte_soc_device *dev; + char *uevent; + int ret; + + uevent = dev_read_uevent(dirname); + if (uevent == NULL) + return -1; + + if (uevent[0] == '\0') { + /* ignore directory without uevent file */ + free(uevent); + return 1; + } + + dev = malloc(sizeof(*dev) + strlen(name) + 1); + if (dev == NULL) { + RTE_LOG(ERR, PMD, "Failed to alloc memory for %s\n", name); + free(uevent); + return -1; + } + + memset(dev, 0, sizeof(*dev)); + dev->addr.name = (char *) (dev + 1); + strcpy(dev->addr.name, name); + + ret = dev_parse_uevent(dev, uevent); + if (ret) + goto fail; + free(uevent); /* not needed anymore */ + + /* device is valid, add in list (sorted) */ + if (TAILQ_EMPTY(&soc_device_list)) { + TAILQ_INSERT_TAIL(&soc_device_list, dev, next); + } else { + struct rte_soc_device *dev2; + + TAILQ_FOREACH(dev2, &soc_device_list, next) { + ret = rte_eal_compare_soc_addr(&dev->addr, &dev2->addr); + if (ret > 0) + continue; + + if (ret < 0) { + TAILQ_INSERT_BEFORE(dev2, dev, next); + } else { /* already registered */ + dev_content_free(dev2); + dev2->addr.fdt_path = dev->addr.fdt_path; + dev2->id = dev->id; + free(dev); + } + return 0; + } + TAILQ_INSERT_TAIL(&soc_device_list, dev, next); + } + + return 0; + +fail: + free(uevent); + dev_content_free(dev); + free(dev); + return ret; +} + +int +rte_eal_soc_scan_platform_bus(void) +{ + struct dirent *e; + DIR *dir; + char dirname[PATH_MAX]; + + dir = opendir(soc_get_sysfs_path()); + if (dir == NULL) { + RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n", + __func__, strerror(errno)); + return -1; + } + + while ((e = readdir(dir)) != NULL) { + if (e->d_name[0] == '.') + continue; + + snprintf(dirname, sizeof(dirname), "%s/%s", + soc_get_sysfs_path(), e->d_name); + if (soc_scan_one(dirname, e->d_name) < 0) + goto error; + } + closedir(dir); + return 0; + +error: + closedir(dir); + return -1; +} + /* Init the SoC EAL subsystem */ int rte_eal_soc_init(void) -- 2.7.4