commit e326c6065d8daa51189abc84a968e0da7b0c2394
Author: Jason Gunthorpe <jgg@mellanox.com>
Date:   Thu Apr 5 11:04:14 2018 -0600

    umad: Do not check for umad sysfs files in umad_init
    
    Now that we don't load the umad module if the HW doesn't use it (eg
    for roce only hardware) umad_init is failing to read the ABI version
    from the kernel.
    
    Applications still want to use some libibumad services that are not
    related to the char device, so move the version check to umad_open_port
    instead.
    
    Backport from:
     * abf72057c277 (umad: Do not check for umad sysfs files in umad_init)
     * c4b883dd49ae (libibumad/man/umad_init.3.md: Trivial punctuation changes)
     * b19fb6d4194e (libibumad/man/umad_open_port.3: Update return values)
    
    Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
    Signed-off-by: Nicolas Morey-Chaisemartin <nmoreychaisemartin@suse.com>

diff --git libibumad/man/umad_init.3 libibumad/man/umad_init.3
index 64366b9de7e4..32c41a9e9878 100644
--- libibumad/man/umad_init.3
+++ libibumad/man/umad_init.3
@@ -13,26 +13,23 @@ umad_init, umad_done \- perform library initialization and finalization
 .BI "int umad_done(void);
 .fi
 .SH "DESCRIPTION"
-.B umad_init()
-initializes the umad library for use. Must be called before any
-other call to this library.
-.PP
-.B umad_done()
-finalizes the use of the umad library.
+.B umad_init(), umad_done()
+do nothing.
 .SH "RETURN VALUE"
+Always 0.
+.SH "COMPATIBILITY"
+For compatibility applications should continue to call
 .B umad_init()
-and
-.B umad_done()
-return 0 on success, and \-1 on error.
-Error is returned from
+, and check the return code, prior to calling other
+.B umad_
+functions.
+.PP
+If
 .B umad_init()
-if infiniband umad
-can\'t be opened, or the abi version doesn\'t match.
-There are no errors currently returned by
-.B umad_done().
-.SH "NOTES"
-If an error occurs during the library initialization, no further use of the
-umad library should be attempted.
+returns an error, then no further use of the umad library
+should be attempted.
+If an error occurs during the library initialization,
+no further use of the umad library should be attempted.
 .SH "AUTHORS"
 .TP
 Hal Rosenstock <halr@voltaire.com>
diff --git libibumad/man/umad_open_port.3 libibumad/man/umad_open_port.3
index cac01a71026b..bd7026be39c0 100644
--- libibumad/man/umad_open_port.3
+++ libibumad/man/umad_open_port.3
@@ -24,11 +24,12 @@ for details).
 .SH "RETURN VALUE"
 .B umad_open_port()
 returns 0 or an unique positive value of umad device descriptor on success, and a negative value on error as follows:
- -ENODEV IB device can\'t be resolved
- -EINVAL port is not valid (bad
+ -EOPNOTSUPP ABI version doesn\'t match
+ -ENODEV     IB device can\'t be resolved
+ -EINVAL     port is not valid (bad
 .I portnum\fR
 or no umad device)
- -EIO    umad device for this port can\'t be opened
+ -EIO        umad device for this port can\'t be opened
 .SH "SEE ALSO"
 .BR umad_close_port (3),
 .BR umad_get_cas_names (3),
diff --git libibumad/umad.c libibumad/umad.c
index dcb2c6809eb1..08efb089d990 100644
--- libibumad/umad.c
+++ libibumad/umad.c
@@ -90,9 +90,31 @@ static int umaddebug = 0;
 static const char *def_ca_name = "mthca0";
 static int def_ca_port = 1;
 
-static unsigned abi_version;
 static unsigned new_user_mad_api;
 
+static unsigned int get_abi_version(void)
+{
+	static unsigned int abi_version;
+
+	if (abi_version != 0)
+		return abi_version & 0x7FFFFFFF;
+
+	if (sys_read_uint(IB_UMAD_ABI_DIR, IB_UMAD_ABI_FILE, &abi_version) <
+	    0) {
+		IBWARN("can't read ABI version from %s/%s (%m): is ib_umad module loaded?",
+		       IB_UMAD_ABI_DIR, IB_UMAD_ABI_FILE);
+		abi_version = 1 << 31;
+		return 0;
+	}
+
+	if (abi_version < IB_UMAD_ABI_VERSION) {
+		abi_version = 1 << 31;
+		return 0;
+	}
+
+	return abi_version;
+}
+
 /*************************************
  * Port
  */
@@ -502,19 +524,6 @@ static int dev_to_umad_id(const char *dev, unsigned port)
 int umad_init(void)
 {
 	TRACE("umad_init");
-	if (sys_read_uint(IB_UMAD_ABI_DIR, IB_UMAD_ABI_FILE, &abi_version) < 0) {
-		IBWARN
-		    ("can't read ABI version from %s/%s (%m): is ib_umad module loaded?",
-		     IB_UMAD_ABI_DIR, IB_UMAD_ABI_FILE);
-		return -1;
-	}
-	if (abi_version < IB_UMAD_ABI_VERSION) {
-		IBWARN
-		    ("wrong ABI version: %s/%s is %d but library minimal ABI is %d",
-		     IB_UMAD_ABI_DIR, IB_UMAD_ABI_FILE, abi_version,
-		     IB_UMAD_ABI_VERSION);
-		return -1;
-	}
 	return 0;
 }
 
@@ -618,9 +627,13 @@ int umad_open_port(const char *ca_name, int portnum)
 {
 	char dev_file[UMAD_DEV_FILE_SZ];
 	int umad_id, fd;
+	unsigned int abi_version = get_abi_version();
 
 	TRACE("ca %s port %d", ca_name, portnum);
 
+	if (!abi_version)
+		return -EOPNOTSUPP;
+
 	if (!(ca_name = resolve_ca_name(ca_name, &portnum)))
 		return -ENODEV;
 
