Skip to contentSkip to conversation

ssb: Save sprom image for dump of device at alternate location

PATCH New Larry Finger 2 participants 9 messages Dec 12, 2010 Sonics silicon backplane driver (ssb)

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 ssb: Save sprom image for dump of device at alternate location

Some recent Broadcom devices in netbooks have an SPROM that is located at 0x0800, not the normal location of 0x1000. Initial reading of the SPROM has been solved in a previous commit; however, dumping to a console no longer works. This difficulty is fixed by saving the SPROM image from the initial read, and only freeing that memory at module unload.

Uploading a new SPROM image is not supported for these devices.

---

John,

This is 2.6.38 material.

Larry
---

drivers/ssb/pci.c +4 -3
Index: wireless-testing/drivers/ssb/pci.c
===================================================================
@@ -709,7 +709,7 @@ static int ssb_pci_sprom_get(struct ssb_
 			if (fallback) {
 				memcpy(sprom, fallback, sizeof(*sprom));
 				err = 0;
-				goto out_free;
+				goto out;
 			}
 			ssb_printk(KERN_WARNING PFX "WARNING: Invalid"
 				   " SPROM CRC (corrupt SPROM)\n");
@@ -763,8 +763,8 @@ static int ssb_pci_sprom_get(struct ssb_
 	}
 	err = sprom_extract(bus, sprom, buf, bus->sprom_size);
 
-out_free:
-	kfree(buf);
+out:
+	bus->sprom_data = buf;
 	return err;
 }
 
@@ -1013,6 +1013,7 @@ void ssb_pci_exit(struct ssb_bus *bus)
 	if (bus->bustype != SSB_BUSTYPE_PCI)
 		return;
 
+	kfree(bus->sprom_data);
 	pdev = bus->host_pci;
 	device_remove_file(&pdev->dev, &dev_attr_ssb_sprom);
 }
drivers/ssb/sprom.c +21 -14
Index: wireless-testing/drivers/ssb/sprom.c
===================================================================
@@ -72,24 +72,29 @@ ssize_t ssb_attr_sprom_show(struct ssb_b
 	ssize_t count = 0;
 	size_t sprom_size_words = bus->sprom_size;
 
-	sprom = kcalloc(sprom_size_words, sizeof(u16), GFP_KERNEL);
-	if (!sprom)
-		goto out;
-
-	/* Use interruptible locking, as the SPROM write might
-	 * be holding the lock for several seconds. So allow userspace
-	 * to cancel operation. */
-	err = -ERESTARTSYS;
-	if (mutex_lock_interruptible(&bus->sprom_mutex))
-		goto out_kfree;
-	err = sprom_read(bus, sprom);
-	mutex_unlock(&bus->sprom_mutex);
-
+	if (bus->sprom_data) {
+		sprom = bus->sprom_data;
+		err = 0;
+	} else {
+		sprom = kcalloc(sprom_size_words, sizeof(u16), GFP_KERNEL);
+		if (!sprom)
+			goto out;
+
+		/* Use interruptible locking, as the SPROM write might
+		 * be holding the lock for several seconds. So allow userspace
+		 * to cancel operation. */
+		err = -ERESTARTSYS;
+		if (mutex_lock_interruptible(&bus->sprom_mutex))
+			goto out_kfree;
+		err = sprom_read(bus, sprom);
+		mutex_unlock(&bus->sprom_mutex);
+	}
 	if (!err)
 		count = sprom2hex(sprom, buf, PAGE_SIZE, sprom_size_words);
 
 out_kfree:
-	kfree(sprom);
+	if (!bus->sprom_data)
+		kfree(sprom);
 out:
 	return err ? err : count;
 }
@@ -105,6 +110,8 @@ ssize_t ssb_attr_sprom_store(struct ssb_
 	size_t sprom_size_words = bus->sprom_size;
 	struct ssb_freeze_context freeze;
 
+	if (bus->sprom_offset < SSB_SPROM_BASE1)
+		return -EINVAL;
 	sprom = kcalloc(bus->sprom_size, sizeof(u16), GFP_KERNEL);
 	if (!sprom)
 		goto out;
include/linux/ssb/ssb.h +1 -0
Index: wireless-testing/include/linux/ssb/ssb.h
===================================================================
@@ -311,6 +311,7 @@ struct ssb_bus {
 	u16 chip_rev;
 	u16 sprom_offset;
 	u16 sprom_size;		/* number of words in sprom */
+	u16 *sprom_data;	/* saved sprom raw data */
 	u8 chip_package;
 
 	/* List of devices (cores) on the backplane. */
#1 lore raw

PATCH 2/2 ssb: Save sprom image for dump of device at alternate location

Quoted 5 lines · Larry Finger

Some recent Broadcom devices in netbooks have an SPROM that is located at 0x0800, not the normal location of 0x1000. Initial reading of the SPROM has been solved in a previous commit; however, dumping to a console no longer works. This difficulty is fixed by saving the SPROM image from the initial read, and only freeing that memory at module unload.

Ah wait. Now I get what you were talking about.
Yes, those devices map the SPROM into the wireless core window. So, yes, the wireless core has to be mapped for the readout to work, of course. I don't know what other prerequisites have to be met to read the data. Maybe IHR region has to be disabled.
I don't know how writing works on these devices.

Signature · 1 line
Greetings Michael.
#2 lore raw

PATCH 2/2 ssb: Save sprom image for dump of device at alternate location

Quoted 7 lines · Michael Büsch
Quoted 5 lines · Larry Finger

Some recent Broadcom devices in netbooks have an SPROM that is located at 0x0800, not the normal location of 0x1000. Initial reading of the SPROM has been solved in a previous commit; however, dumping to a console no longer works. This difficulty is fixed by saving the SPROM image from the initial read, and only freeing that memory at module unload.

Quoted 6 lines

Ah wait. Now I get what you were talking about.
Yes, those devices map the SPROM into the wireless core window. So, yes, the wireless core has to be mapped for the readout to work, of course. I don't know what other prerequisites have to be met to read the data. Maybe IHR region has to be disabled.
I don't know how writing works on these devices.

I do not think it is worthwhile to bother to find out how to rewrite the SPROM. As long as we can read it in case of questions regarding content, that should be enough. In V2, I will return -ENOTSUP rather than -EINVAL as that is more appropriate.

Larry

#3 lore raw

PATCH 2/2 ssb: Save sprom image for dump of device at alternate location

Quoted 15 lines · Larry Finger
Quoted 7 lines · Michael B?sch
Quoted 5 lines · Larry Finger

Some recent Broadcom devices in netbooks have an SPROM that is located at 0x0800, not the normal location of 0x1000. Initial reading of the SPROM has been solved in a previous commit; however, dumping to a console no longer works. This difficulty is fixed by saving the SPROM image from the initial read, and only freeing that memory at module unload.

Quoted 6 lines

Ah wait. Now I get what you were talking about.
Yes, those devices map the SPROM into the wireless core window. So, yes, the wireless core has to be mapped for the readout to work, of course. I don't know what other prerequisites have to be met to read the data. Maybe IHR region has to be disabled.
I don't know how writing works on these devices.

Quoted 4 lines

I do not think it is worthwhile to bother to find out how to rewrite the SPROM. As long as we can read it in case of questions regarding content, that should be enough. In V2, I will return -ENOTSUP rather than -EINVAL as that is more appropriate.

So, do we actually make sure the wireless core is mapped while reading SPROM from offset 0x800? I guess not and it just works by accident, because the core is still mapped from a previous operation.

Signature · 1 line
Greetings Michael.
#4 lore raw

PATCH 2/2 ssb: Save sprom image for dump of device at alternate location

Quoted 4 lines · Michael Büsch

So, do we actually make sure the wireless core is mapped while reading SPROM from offset 0x800? I guess not and it just works by accident, because the core is still mapped from a previous operation.

I have core switching debug enabled on that box. When the SPROM is read, the ChipCommon core is mapped, and no other core has yet been selected.

Larry

#5 lore raw

PATCH 2/2 ssb: Save sprom image for dump of device at alternate location

Quoted 8 lines · Larry Finger
Quoted 4 lines · Michael B?sch

So, do we actually make sure the wireless core is mapped while reading SPROM from offset 0x800? I guess not and it just works by accident, because the core is still mapped from a previous operation.

I have core switching debug enabled on that box. When the SPROM is read, the ChipCommon core is mapped, and no other core has yet been selected.

Well, ok. Whatever core it is, it's coincidence that it is mapped. I'd rather prefer an explicit mapping of the chipcommon before the area is accessed. The chipcommon MMIO functions could probably be used. That would be easiest. (16bit function does not exist, yet, but is trivial to add).

Signature · 1 line
Greetings Michael.
#6 lore raw

PATCH 2/2 ssb: Save sprom image for dump of device at alternate location

In reply to Larry Finger

drivers/ssb/pci.c void ssb_pci_exit(struct ssb_bus *bus) · line 1019

Quoted 8 lines · Larry Finger
drivers/ssb/pci.c +1 -0
@@ -1013,6 +1013,7 @@ void ssb_pci_exit(struct ssb_bus *bus)
 	if (bus->bustype != SSB_BUSTYPE_PCI)
 		return;
 
+	kfree(bus->sprom_data);
 	pdev = bus->host_pci;
 	device_remove_file(&pdev->dev, &dev_attr_ssb_sprom);
 }

Need to put kfree after removing the sprom file. Otherwise there's a race condition with userspace.

Quoted 15 lines

Index: wireless-testing/drivers/ssb/sprom.c
===================================================================

drivers/ssb/sprom.c +0 -7
@@ -72,24 +72,29 @@ ssize_t ssb_attr_sprom_show(struct ssb_b
 	ssize_t count = 0;
 	size_t sprom_size_words = bus->sprom_size;
 
-	sprom = kcalloc(sprom_size_words, sizeof(u16), GFP_KERNEL);
-	if (!sprom)
-		goto out;
-
-	/* Use interruptible locking, as the SPROM write might
-	 * be holding the lock for several seconds. So allow userspace
-	 * to cancel operation. */

drivers/ssb/sprom.c ssize_t ssb_attr_sprom_show(struct ssb_b · line 78

Quoted 10 lines
drivers/ssb/sprom.c +4 -6
-	err = -ERESTARTSYS;
-	if (mutex_lock_interruptible(&bus->sprom_mutex))
-		goto out_kfree;
-	err = sprom_read(bus, sprom);
-	mutex_unlock(&bus->sprom_mutex);
-
+	if (bus->sprom_data) {
+		sprom = bus->sprom_data;
+		err = 0;
+	} else {

This branch is dead now, or do I miss something?

Quoted 13 lines
drivers/ssb/sprom.c +13 -0
+		sprom = kcalloc(sprom_size_words, sizeof(u16), GFP_KERNEL);
+		if (!sprom)
+			goto out;
+
+		/* Use interruptible locking, as the SPROM write might
+		 * be holding the lock for several seconds. So allow userspace
+		 * to cancel operation. */
+		err = -ERESTARTSYS;
+		if (mutex_lock_interruptible(&bus->sprom_mutex))
+			goto out_kfree;
+		err = sprom_read(bus, sprom);
+		mutex_unlock(&bus->sprom_mutex);
+	}

drivers/ssb/sprom.c ssize_t ssb_attr_sprom_show(struct ssb_b · line 100

Quoted 10 lines
drivers/ssb/sprom.c +2 -1
 	if (!err)
 		count = sprom2hex(sprom, buf, PAGE_SIZE, sprom_size_words);
 
 out_kfree:
-	kfree(sprom);
+	if (!bus->sprom_data)
+		kfree(sprom);
 out:
 	return err ? err : count;
 }

I agree that caching the SPROM probably is easier than reading it from the wireless core at sysfs read time. There are a few concurrency issues that are hard to solve with the current ssb-pci MMIO access code. Most notably is that the SPROM read would race with wireless interrupts.

Signature · 1 line
Greetings Michael.
#7 lore raw

PATCH 2/2 ssb: Save sprom image for dump of device at alternate location

Quoted 5 lines · Michael Büsch
Quoted 4 lines · Larry Finger
drivers/ssb/pci.c +0 -0
@@ -1013,6 +1013,7 @@ void ssb_pci_exit(struct ssb_bus *bus)
 	if (bus->bustype != SSB_BUSTYPE_PCI)
 		return;
 
Quoted 7 lines

drivers/ssb/pci.c void ssb_pci_exit(struct ssb_bus *bus) · line 1019

Quoted 4 lines
drivers/ssb/pci.c +1 -0
+	kfree(bus->sprom_data);
 	pdev = bus->host_pci;
 	device_remove_file(&pdev->dev, &dev_attr_ssb_sprom);
 }

Need to put kfree after removing the sprom file. Otherwise there's a race condition with userspace.

Will do. Thanks.

Quoted 19 lines
Quoted 19 lines

Index: wireless-testing/drivers/ssb/sprom.c
===================================================================

drivers/ssb/sprom.c +0 -11
@@ -72,24 +72,29 @@ ssize_t ssb_attr_sprom_show(struct ssb_b
 	ssize_t count = 0;
 	size_t sprom_size_words = bus->sprom_size;
 
-	sprom = kcalloc(sprom_size_words, sizeof(u16), GFP_KERNEL);
-	if (!sprom)
-		goto out;
-
-	/* Use interruptible locking, as the SPROM write might
-	 * be holding the lock for several seconds. So allow userspace
-	 * to cancel operation. */
-	err = -ERESTARTSYS;
-	if (mutex_lock_interruptible(&bus->sprom_mutex))
-		goto out_kfree;
-	err = sprom_read(bus, sprom);
Quoted 8 lines

drivers/ssb/sprom.c ssize_t ssb_attr_sprom_show(struct ssb_b · line 78

Quoted 6 lines
drivers/ssb/sprom.c +4 -2
-	mutex_unlock(&bus->sprom_mutex);
-
+	if (bus->sprom_data) {
+		sprom = bus->sprom_data;
+		err = 0;
+	} else {

This branch is dead now, or do I miss something?

I kept it for those devices with SPROMs that are not PCI. If there are none, then it can de removed.

Quoted 16 lines
Quoted 16 lines
drivers/ssb/sprom.c +13 -0
+		sprom = kcalloc(sprom_size_words, sizeof(u16), GFP_KERNEL);
+		if (!sprom)
+			goto out;
+
+		/* Use interruptible locking, as the SPROM write might
+		 * be holding the lock for several seconds. So allow userspace
+		 * to cancel operation. */
+		err = -ERESTARTSYS;
+		if (mutex_lock_interruptible(&bus->sprom_mutex))
+			goto out_kfree;
+		err = sprom_read(bus, sprom);
+		mutex_unlock(&bus->sprom_mutex);
+	}
 	if (!err)
 		count = sprom2hex(sprom, buf, PAGE_SIZE, sprom_size_words);
 
Quoted 12 lines

drivers/ssb/sprom.c ssize_t ssb_attr_sprom_show(struct ssb_b · line 100

Quoted 7 lines
drivers/ssb/sprom.c +2 -1
 out_kfree:
-	kfree(sprom);
+	if (!bus->sprom_data)
+		kfree(sprom);
 out:
 	return err ? err : count;
 }

I agree that caching the SPROM probably is easier than reading it from the wireless core at sysfs read time. There are a few concurrency issues that are hard to solve with the current ssb-pci MMIO access code. Most notably is that the SPROM read would race with wireless interrupts.

As it was easy and adds very little to the memory footprint, it seems the way to go.

Larry

#8 lore raw

PATCH 2/2 ssb: Save sprom image for dump of device at alternate location

Quoted 9 lines · Larry Finger
Quoted 6 lines

drivers/ssb/sprom.c ssize_t ssb_attr_sprom_show(struct ssb_b · line 78

Quoted 4 lines
drivers/ssb/sprom.c +4 -0
+	if (bus->sprom_data) {
+		sprom = bus->sprom_data;
+		err = 0;
+	} else {

This branch is dead now, or do I miss something?

I kept it for those devices with SPROMs that are not PCI. If there are none, then it can de removed.

Ah ok. Seems it would still be needed for pcmcia. At least with the current implementation.

Signature · 1 line
Greetings Michael.