Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[firefly-linux-kernel-4.4.55.git] / drivers / firmware / dmi_scan.c
index f7318b3b51f2a0860bf2f952896d60f746bd98a8..653265a40b7f9721eed8fcf1c08d6f75cdfdba3d 100644 (file)
@@ -8,9 +8,11 @@
 #include <linux/slab.h>
 #include <asm/dmi.h>
 
-static char * __init dmi_string(struct dmi_header *dm, u8 s)
+static char dmi_empty_string[] = "        ";
+
+static char * __init dmi_string(const struct dmi_header *dm, u8 s)
 {
-       u8 *bp = ((u8 *) dm) + dm->length;
+       const u8 *bp = ((u8 *) dm) + dm->length;
        char *str = "";
 
        if (s) {
@@ -21,11 +23,16 @@ static char * __init dmi_string(struct dmi_header *dm, u8 s)
                }
 
                if (*bp != 0) {
-                       str = dmi_alloc(strlen(bp) + 1);
+                       size_t len = strlen(bp)+1;
+                       size_t cmp_len = len > 8 ? 8 : len;
+
+                       if (!memcmp(bp, dmi_empty_string, cmp_len))
+                               return dmi_empty_string;
+                       str = dmi_alloc(len);
                        if (str != NULL)
                                strcpy(str, bp);
                        else
-                               printk(KERN_ERR "dmi_string: out of memory.\n");
+                               printk(KERN_ERR "dmi_string: cannot allocate %Zu bytes.\n", len);
                }
        }
 
@@ -36,24 +43,19 @@ static char * __init dmi_string(struct dmi_header *dm, u8 s)
  *     We have to be cautious here. We have seen BIOSes with DMI pointers
  *     pointing to completely the wrong place for example
  */
-static int __init dmi_table(u32 base, int len, int num,
-                           void (*decode)(struct dmi_header *))
+static void dmi_table(u8 *buf, int len, int num,
+                     void (*decode)(const struct dmi_header *))
 {
-       u8 *buf, *data;
+       u8 *data = buf;
        int i = 0;
 
-       buf = dmi_ioremap(base, len);
-       if (buf == NULL)
-               return -1;
-
-       data = buf;
-
        /*
         *      Stop when we see all the items the table claimed to have
         *      OR we run off the end of the table (also happens)
         */
        while ((i < num) && (data - buf + sizeof(struct dmi_header)) <= len) {
-               struct dmi_header *dm = (struct dmi_header *)data;
+               const struct dmi_header *dm = (const struct dmi_header *)data;
+
                /*
                 *  We want to know the total length (formated area and strings)
                 *  before decoding to make sure we won't run off the table in
@@ -67,11 +69,27 @@ static int __init dmi_table(u32 base, int len, int num,
                data += 2;
                i++;
        }
-       dmi_iounmap(buf, len);
+}
+
+static u32 dmi_base;
+static u16 dmi_len;
+static u16 dmi_num;
+
+static int __init dmi_walk_early(void (*decode)(const struct dmi_header *))
+{
+       u8 *buf;
+
+       buf = dmi_ioremap(dmi_base, dmi_len);
+       if (buf == NULL)
+               return -1;
+
+       dmi_table(buf, dmi_len, dmi_num, decode);
+
+       dmi_iounmap(buf, dmi_len);
        return 0;
 }
 
-static int __init dmi_checksum(u8 *buf)
+static int __init dmi_checksum(const u8 *buf)
 {
        u8 sum = 0;
        int a;
@@ -89,9 +107,10 @@ int dmi_available;
 /*
  *     Save a DMI string
  */
-static void __init dmi_save_ident(struct dmi_header *dm, int slot, int string)
+static void __init dmi_save_ident(const struct dmi_header *dm, int slot, int string)
 {
-       char *p, *d = (char*) dm;
+       const char *d = (const char*) dm;
+       char *p;
 
        if (dmi_ident[slot])
                return;
@@ -103,9 +122,9 @@ static void __init dmi_save_ident(struct dmi_header *dm, int slot, int string)
        dmi_ident[slot] = p;
 }
 
-static void __init dmi_save_uuid(struct dmi_header *dm, int slot, int index)
+static void __init dmi_save_uuid(const struct dmi_header *dm, int slot, int index)
 {
-       u8 *d = (u8*) dm + index;
+       const u8 *d = (u8*) dm + index;
        char *s;
        int is_ff = 1, is_00 = 1, i;
 
@@ -132,9 +151,9 @@ static void __init dmi_save_uuid(struct dmi_header *dm, int slot, int index)
         dmi_ident[slot] = s;
 }
 
-static void __init dmi_save_type(struct dmi_header *dm, int slot, int index)
+static void __init dmi_save_type(const struct dmi_header *dm, int slot, int index)
 {
-       u8 *d = (u8*) dm + index;
+       const u8 *d = (u8*) dm + index;
        char *s;
 
        if (dmi_ident[slot])
@@ -148,13 +167,13 @@ static void __init dmi_save_type(struct dmi_header *dm, int slot, int index)
        dmi_ident[slot] = s;
 }
 
-static void __init dmi_save_devices(struct dmi_header *dm)
+static void __init dmi_save_devices(const struct dmi_header *dm)
 {
        int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
        struct dmi_device *dev;
 
        for (i = 0; i < count; i++) {
-               char *d = (char *)(dm + 1) + (i * 2);
+               const char *d = (char *)(dm + 1) + (i * 2);
 
                /* Skip disabled device */
                if ((*d & 0x80) == 0)
@@ -173,12 +192,23 @@ static void __init dmi_save_devices(struct dmi_header *dm)
        }
 }
 
-static void __init dmi_save_oem_strings_devices(struct dmi_header *dm)
+static struct dmi_device empty_oem_string_dev = {
+       .name = dmi_empty_string,
+};
+
+static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
 {
        int i, count = *(u8 *)(dm + 1);
        struct dmi_device *dev;
 
        for (i = 1; i <= count; i++) {
+               char *devname = dmi_string(dm, i);
+
+               if (!strcmp(devname, dmi_empty_string)) {
+                       list_add(&empty_oem_string_dev.list, &dmi_devices);
+                       continue;
+               }
+
                dev = dmi_alloc(sizeof(*dev));
                if (!dev) {
                        printk(KERN_ERR
@@ -187,14 +217,14 @@ static void __init dmi_save_oem_strings_devices(struct dmi_header *dm)
                }
 
                dev->type = DMI_DEV_TYPE_OEM_STRING;
-               dev->name = dmi_string(dm, i);
+               dev->name = devname;
                dev->device_data = NULL;
 
                list_add(&dev->list, &dmi_devices);
        }
 }
 
-static void __init dmi_save_ipmi_device(struct dmi_header *dm)
+static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
 {
        struct dmi_device *dev;
        void * data;
@@ -220,12 +250,34 @@ static void __init dmi_save_ipmi_device(struct dmi_header *dm)
        list_add(&dev->list, &dmi_devices);
 }
 
+static void __init dmi_save_extended_devices(const struct dmi_header *dm)
+{
+       const u8 *d = (u8*) dm + 5;
+       struct dmi_device *dev;
+
+       /* Skip disabled device */
+       if ((*d & 0x80) == 0)
+               return;
+
+       dev = dmi_alloc(sizeof(*dev));
+       if (!dev) {
+               printk(KERN_ERR "dmi_save_extended_devices: out of memory.\n");
+               return;
+       }
+
+       dev->type = *d-- & 0x7f;
+       dev->name = dmi_string(dm, *d);
+       dev->device_data = NULL;
+
+       list_add(&dev->list, &dmi_devices);
+}
+
 /*
  *     Process a DMI table entry. Right now all we care about are the BIOS
  *     and machine entries. For 2.5 we should pull the smbus controller info
  *     out of here.
  */
-static void __init dmi_decode(struct dmi_header *dm)
+static void __init dmi_decode(const struct dmi_header *dm)
 {
        switch(dm->type) {
        case 0:         /* BIOS Information */
@@ -262,17 +314,21 @@ static void __init dmi_decode(struct dmi_header *dm)
                break;
        case 38:        /* IPMI Device Information */
                dmi_save_ipmi_device(dm);
+               break;
+       case 41:        /* Onboard Devices Extended Information */
+               dmi_save_extended_devices(dm);
        }
 }
 
-static int __init dmi_present(char __iomem *p)
+static int __init dmi_present(const char __iomem *p)
 {
        u8 buf[15];
+
        memcpy_fromio(buf, p, 15);
        if ((memcmp(buf, "_DMI_", 5) == 0) && dmi_checksum(buf)) {
-               u16 num = (buf[13] << 8) | buf[12];
-               u16 len = (buf[7] << 8) | buf[6];
-               u32 base = (buf[11] << 24) | (buf[10] << 16) |
+               dmi_num = (buf[13] << 8) | buf[12];
+               dmi_len = (buf[7] << 8) | buf[6];
+               dmi_base = (buf[11] << 24) | (buf[10] << 16) |
                        (buf[9] << 8) | buf[8];
 
                /*
@@ -284,7 +340,7 @@ static int __init dmi_present(char __iomem *p)
                               buf[14] >> 4, buf[14] & 0xF);
                else
                        printk(KERN_INFO "DMI present.\n");
-               if (dmi_table(base,len, num, dmi_decode) == 0)
+               if (dmi_walk_early(dmi_decode) == 0)
                        return 0;
        }
        return 1;
@@ -328,9 +384,11 @@ void __init dmi_scan_machine(void)
                        rc = dmi_present(q);
                        if (!rc) {
                                dmi_available = 1;
+                               dmi_iounmap(p, 0x10000);
                                return;
                        }
                }
+               dmi_iounmap(p, 0x10000);
        }
  out:  printk(KERN_INFO "DMI not present or invalid.\n");
 }
@@ -348,10 +406,10 @@ void __init dmi_scan_machine(void)
  *     returns non zero or we hit the end. Callback function is called for
  *     each successful match. Returns the number of matches.
  */
-int dmi_check_system(struct dmi_system_id *list)
+int dmi_check_system(const struct dmi_system_id *list)
 {
        int i, count = 0;
-       struct dmi_system_id *d = list;
+       const struct dmi_system_id *d = list;
 
        while (d->ident) {
                for (i = 0; i < ARRAY_SIZE(d->matches); i++) {
@@ -380,7 +438,7 @@ EXPORT_SYMBOL(dmi_check_system);
  *     Returns one DMI data value, can be used to perform
  *     complex DMI data checks.
  */
-char *dmi_get_system_info(int field)
+const char *dmi_get_system_info(int field)
 {
        return dmi_ident[field];
 }
@@ -391,7 +449,7 @@ EXPORT_SYMBOL(dmi_get_system_info);
  *     dmi_name_in_vendors - Check if string is anywhere in the DMI vendor information.
  *     @str:   Case sensitive Name
  */
-int dmi_name_in_vendors(char *str)
+int dmi_name_in_vendors(const char *str)
 {
        static int fields[] = { DMI_BIOS_VENDOR, DMI_BIOS_VERSION, DMI_SYS_VENDOR,
                                DMI_PRODUCT_NAME, DMI_PRODUCT_VERSION, DMI_BOARD_VENDOR,
@@ -418,13 +476,15 @@ EXPORT_SYMBOL(dmi_name_in_vendors);
  *     A new search is initiated by passing %NULL as the @from argument.
  *     If @from is not %NULL, searches continue from next device.
  */
-struct dmi_device * dmi_find_device(int type, const char *name,
-                                   struct dmi_device *from)
+const struct dmi_device * dmi_find_device(int type, const char *name,
+                                   const struct dmi_device *from)
 {
-       struct list_head *d, *head = from ? &from->list : &dmi_devices;
+       const struct list_head *head = from ? &from->list : &dmi_devices;
+       struct list_head *d;
 
        for(d = head->next; d != &dmi_devices; d = d->next) {
-               struct dmi_device *dev = list_entry(d, struct dmi_device, list);
+               const struct dmi_device *dev =
+                       list_entry(d, struct dmi_device, list);
 
                if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
                    ((name == NULL) || (strcmp(dev->name, name) == 0)))
@@ -444,7 +504,7 @@ EXPORT_SYMBOL(dmi_find_device);
 int dmi_get_year(int field)
 {
        int year;
-       char *s = dmi_get_system_info(field);
+       const char *s = dmi_get_system_info(field);
 
        if (!s)
                return -1;
@@ -465,3 +525,26 @@ int dmi_get_year(int field)
        return year;
 }
 
+/**
+ *     dmi_walk - Walk the DMI table and get called back for every record
+ *     @decode: Callback function
+ *
+ *     Returns -1 when the DMI table can't be reached, 0 on success.
+ */
+int dmi_walk(void (*decode)(const struct dmi_header *))
+{
+       u8 *buf;
+
+       if (!dmi_available)
+               return -1;
+
+       buf = ioremap(dmi_base, dmi_len);
+       if (buf == NULL)
+               return -1;
+
+       dmi_table(buf, dmi_len, dmi_num, decode);
+
+       iounmap(buf);
+       return 0;
+}
+EXPORT_SYMBOL_GPL(dmi_walk);