Skip to contentSkip to conversation

SPI core refresh

PATCH In review Vitaly Wool 6 participants 25 messages Dec 1, 2005 – Dec 12, 2005

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 SPI core refresh

Greetings,

This is an updated version of SPI framework developed by Dmitry Pervushin and Vitaly Wool.

The main changes are:

- Matching rmk's 2.6.14-git5+ changes for device_driver suspend and resume calls
- Matching rmk's request to get rid of device_driver's calls to suspend/resume/probe/remove
- Matching Greg K-H's requests expressed in his review
- The character device interface was reworked
- No more redundant memcpy's (that should have been in the previous core, I don't know why it's missing there, nm now anyway :))

I still think that we need to continue converging with David Brownell's core, despite some misalignments happening in the email exchange :)
As a part of this convergence process, we took some fifteen minutes to port MTD dataflash driver posted by David to our core and it has really become smaller and more understandlable. This will be the subject of the next message, though.

Again, some advantages of our core compared to David's I'd like to mention

- it can be compiled as a module
- it is priority inversion-free
- it can gain more performance with multiple controllers
- it's more adapted for use in real-time environments
- it's not so lightweight, but it leaves less effort for the bus driver developer.

(As a response to the last bullet David claims that we limit the flexibility. It's not correct.
The core method for message retrieval is just a default one and can easily be overridden by the bus driver. It's a common practice, for instance, see MTD/NAND interface.)

It's also been proven to work on SPI EEPROMs and SPI WiFi module (the latter was a really good survival test! :)).

 Documentation/spi.txt  |  382 ++++++++++++++++++++++++++
 arch/arm/Kconfig       |    2 
 drivers/Kconfig        |    2 
 drivers/Makefile       |    1 
 drivers/spi/Kconfig    |   30 ++
 drivers/spi/Makefile   |   15 +
 drivers/spi/spi-core.c |  700 +++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/spi/spi-dev.c  |  192 +++++++++++++
 include/linux/spi.h    |  422 +++++++++++++++++++++++++++++
 9 files changed, 1746 insertions(+)
arch/arm/Kconfig +2 -0
Index: lsm-2.6/arch/arm/Kconfig
===================================================================
@@ -738,6 +738,8 @@ source "drivers/usb/Kconfig"
 
 source "drivers/mmc/Kconfig"
 
+source "drivers/spi/Kconfig"
+
 endmenu
 
 source "fs/Kconfig"
Documentation/spi.txt new file +382 -0

382 lines not on this page · raw message

drivers/Kconfig +2 -0
Index: lsm-2.6/drivers/Kconfig
===================================================================
@@ -44,6 +44,8 @@ source "drivers/char/Kconfig"
 
 source "drivers/i2c/Kconfig"
 
+source "drivers/spi/Kconfig"
+
 source "drivers/w1/Kconfig"
 
 source "drivers/hwmon/Kconfig"
drivers/spi/Kconfig new file +30 -0
Index: lsm-2.6/drivers/spi/Kconfig
===================================================================
@@ -0,0 +1,30 @@
+#
+# SPI device configuration
+#
+menu "SPI support"
+
+config SPI
+	tristate "SPI (Serial Peripheral Interface) bus support"
+        default false
+	help
+	  Say Y if you need to enable SPI support on your kernel.
+ 	  Say M if you want to create the spi-core loadable module.
+
+config SPI_DEBUG
+	bool "SPI debug output"
+	depends on SPI
+	default false
+	help
+          Say Y there if you'd like to see debug output from SPI drivers
+	  If unsure, say N
+
+config SPI_CHARDEV
+	default Y
+	bool "SPI device interface"
+	depends on SPI
+	help
+	  Say Y here to use /dev/spiNN device files. They make it possible to have user-space
+	  programs use the SPI bus.
+
+endmenu
+
drivers/spi/Makefile new file +15 -0
Index: lsm-2.6/drivers/spi/Makefile
===================================================================
@@ -0,0 +1,15 @@
+#
+# Makefile for the kernel spi bus driver.
+#
+
+spi-y += spi-core.o
+spi-$(CONFIG_SPI_CHARDEV) += spi-dev.o
+# bus drivers
+# ...functional drivers
+# ...and the common spi-dev driver
+obj-$(CONFIG_SPI) += spi.o
+
+ifeq ($(CONFIG_SPI_DEBUG),y)
+EXTRA_CFLAGS += -DDEBUG
+endif
+
drivers/spi/spi-core.c new file +700 -0

700 lines not on this page · raw message

drivers/spi/spi-dev.c new file +192 -0

192 lines not on this page · raw message

include/linux/spi.h new file +422 -0

422 lines not on this page · raw message

drivers/Makefile +1 -0
Index: lsm-2.6/drivers/Makefile
===================================================================
@@ -67,3 +67,4 @@ obj-$(CONFIG_INFINIBAND)	+= infiniband/
 obj-y				+= firmware/
 obj-$(CONFIG_CRYPTO)		+= crypto/
 obj-$(CONFIG_SUPERH)		+= sh/
+obj-$(CONFIG_SPI)		+= spi/
#1 lore raw

PATCH 1/1 MTD/SPI dataflash driver

Greetings,

this is a follow-up message on the one posted a few minutes ago containing the updated version of Pervushin/Wool's SPI core.
It took about fifteen minutes of mindwork to port the MTD dataflash driver posted by David Brownell 11/23 to our SPI core from David's, reducing both the size of the source code and the footprint of the object code. I'd also like to mention that now it looks significatnly easier to understand due to no more use of complicated SPI message structures. The number of variables used was also decreased. No other activity than porting the driver was performed.

 drivers/mtd/devices/Kconfig         |    8 
 drivers/mtd/devices/Makefile        |    1 
 drivers/mtd/devices/mtd_dataflash.c |  568 ++++++++++++++++++++++++++++++++++++
 include/linux/spi/flash.h           |   27 +
 4 files changed, 604 insertions(+)
drivers/mtd/devices/Kconfig +8 -0
Index: lsm-2.6/drivers/mtd/devices/Kconfig
===================================================================
@@ -255,5 +255,13 @@ config MTD_DOCPROBE_55AA
 	  LinuxBIOS or if you need to recover a DiskOnChip Millennium on which
 	  you have managed to wipe the first block.
 
+config MTD_DATAFLASH
+	tristate "Support for AT45xxx DataFlash"
+	depends on MTD && SPI && EXPERIMENTAL
+	help
+	  This enables access to AT45xxx DataFlash chips, using SPI.
+	  Sometimes DataFlash chips are packaged inside MMC-format
+	  cards; at this writing, the MMC stack won't handle those.
+
 endmenu
 
include/linux/spi/flash.h new file +27 -0
Index: lsm-2.6/include/linux/spi/flash.h
===================================================================
@@ -0,0 +1,27 @@
+#ifndef LINUX_SPI_FLASH_H
+#define LINUX_SPI_FLASH_H
+
+struct mtd_partition;
+
+/**
+ * struct flash_platform_data: board-specific flash data
+ * @name: optional flash device name (eg, as used with mtdparts=)
+ * @parts: optional array of mtd_partitions for static partitioning
+ * @nr_parts: number of mtd_partitions for static partitoning
+ *
+ * Board init code (in arch/.../mach-xxx/board-yyy.c files) can
+ * provide information about SPI flash parts (such as DataFlash) to
+ * help set up the device and its appropriate default partitioning.
+ *
+ * Note that for DataFlash, sizes for pages, blocks, and sectors are
+ * rarely powers of two; and partitions should be sector-aligned.
+ */
+struct flash_platform_data {
+	char		*name;
+	struct mtd_partition *parts;
+	unsigned int	nr_parts;
+
+	/* we'll likely add more ... use JEDEC IDs, etc */
+};
+
+#endif
drivers/mtd/devices/Makefile +1 -0
Index: lsm-2.6/drivers/mtd/devices/Makefile
===================================================================
@@ -23,3 +23,4 @@ obj-$(CONFIG_MTD_MTDRAM)	+= mtdram.o
 obj-$(CONFIG_MTD_LART)		+= lart.o
 obj-$(CONFIG_MTD_BLKMTD)	+= blkmtd.o
 obj-$(CONFIG_MTD_BLOCK2MTD)	+= block2mtd.o
+obj-$(CONFIG_MTD_DATAFLASH)	+= mtd_dataflash.o
drivers/mtd/devices/mtd_dataflash.c new file +568 -0

568 lines not on this page · raw message

#2 lore raw

Re: [PATCH 2.6-git] MTD/SPI dataflash driver

Quoted 2 lines · Vitaly Wool

It took about fifteen minutes of mindwork to port the MTD dataflash driver posted by David Brownell 11/23 to our SPI core from David's,

Interesting and informative; +1!

Did you have a chance to test this? There are some post-2.6.15-rc3-mm1 updates to this code, mostly to catch startup fauults better but also to hotplug reasonably. The glitches I saw may as easily be JFFS2 integration issues for the DataFlash code as bitbang adapter problems. (I think you started more or less from what's in rc3-mm1.)

Quoted 5 lines

reducing both
the size of the source code and the footprint of the object code. I'd also like to mention that now it looks significatnly easier to understand due to no more use of complicated SPI message structures. The number of variables used was also decreased

I think that's all the same issue. Other than "spi_driver" replacing "device_driver" (I'd like to see a patch doing that to rc3-mm1), the main changes seem to be:

- Move from original atomic requests like this

	{ write command, read response }

    over to two separate requests

	{ write command, and leave CS active }
	{ read response, and leave CS off }

- Lower the per-request performance ceiling on this driver

* original code could be implemented in a single DMA chain on at least two systems I happen to have handy ... one IRQ.

* this version requires two separate chains, with an intervening task schedule. (More than twice the cost.)

* in your framework I still can't be _sure_ it never does memcpy for those buffers (the last version I looked at did so). the original code just used normal DMA models, so it "obviously" doesn't risk memcpy.

- Add fault handling problems ... probably an oversight, you call routines and don't check for fault reports. Though I'm not clear how the faults between the two requests would be handled (in your framework) with any robustness... doing this right could easily cost all the codespace you've conserved.

- I might agree with this being "easier to understand" code, though it's debatable. (The device sees one transaction, why should the driver writer have to split it up into two?) But that doesn't matter much: filesystems are better with "faster to run" code.

- Chipselect works differently in your code. You're giving one driver control over all the devices sharing a controller, by blocking requests going to other devices until your driver yields the chipselect. This seems like a bad idea even if you ignore how it produces priority inversions in your framework. :)

So the way I see it, this is a good example of why my original I/O model is better. It provides _flexibility_ in the API so that some drivers can be really smart, if they want to. You haven't liked the consequence of that though: controller drivers being able to choose how they represent and manage I/O queues, rather than doing that in your "core".

- Dave

#3 lore raw

Re: [PATCH 2.6-git] MTD/SPI dataflash driver

Quoted 9 lines · David Brownell
Quoted 4 lines · Vitaly Wool

It took about fifteen minutes of mindwork to port the MTD dataflash driver posted by David Brownell 11/23 to our SPI core from David's,

Quoted 10 lines

Interesting and informative; +1!

Did you have a chance to test this? There are some post-2.6.15-rc3-mm1 updates to this code, mostly to catch startup fauults better but also to hotplug reasonably. The glitches I saw may as easily be JFFS2 integration issues for the DataFlash code as bitbang adapter problems. (I think you started more or less from what's in rc3-mm1.)

Very basically. I've make a note that it was a mindwork.I also didn't make the attempt to make it a clean MTD driver. However, it would be nice if someone compared the performance of both variants.

Quoted 22 lines
Quoted 8 lines

reducing both the size of the source code and the footprint of the object code. I'd also like to mention that now it looks significatnly easier to understand due to no more use of complicated SPI message structures. The number of variables used was also decreased

I think that's all the same issue. Other than "spi_driver" replacing "device_driver" (I'd like to see a patch doing that to rc3-mm1), the main changes seem to be:

- Move from original atomic requests like this

	{ write command, read response }
   over to two separate requests

	{ write command, and leave CS active }
	{ read response, and leave CS off }
Quoted 6 lines

- Lower the per-request performance ceiling on this driver

* original code could be implemented in a single DMA chain on at least two systems I happen to have handy ... one IRQ.

Whoops. Lemme express my thoughts here.
First, the DMA controller that can handle chained requests which are working with different devices (i.e. write then read does that - first mem2per, then per2mem) are *very* rare thing.
Then, what _really_ can happen in a single DMA chain? 99,9999% cases of such will be "read x bytes to get aware of the length of the packet, then read the data packet". Here you won't have any noticable performance gain as the first transfer is to small. It's soooo small that the overhead of having a linked list for DMA will probably be bigger than that of having two transfers. A smart controller driver may even want to have a PIO read on first x bytes (as x will be equal to 2 or 4, I guess :)

Quoted 4 lines

* this version requires two separate chains, with an intervening task schedule. (More than twice the cost.)

See above.

Quoted 6 lines

* in your framework I still can't be _sure_ it never does memcpy for those buffers (the last version I looked at did so). the original code just used normal DMA models, so it "obviously" doesn't risk memcpy.

You can be sure if you set SPI_M_DNA flag. I'll update the Doc accordingly.

Quoted 6 lines

- I might agree with this being "easier to understand" code, though it's debatable. (The device sees one transaction, why should the driver writer have to split it up into two?) But that doesn't matter much: filesystems are better with "faster to run" code.

Your clains that the originl code is faster are arguable.

Quoted 6 lines

- Chipselect works differently in your code. You're giving one driver control over all the devices sharing a controller, by blocking requests going to other devices until your driver yields the chipselect.

The controllers we were working with are _not_ able to handle simultaneous requests to different devices on the same bus. Can you please tell me what specific controller you're referring to?

Quoted 8 lines

So the way I see it, this is a good example of why my original I/O model is better. It provides _flexibility_ in the API so that some drivers can be really smart, if they want to. You haven't liked the consequence of that though: controller drivers being able to choose how they represent and manage I/O queues, rather than doing that in your "core".

Not that I agreed to your vision, for the reasons listed above. ;-)

Vitaly

#4 lore raw

Re: [PATCH 2.6-git] MTD/SPI dataflash driver

Quoted 9 lines

However, it would be nice if someone compared the performance of both variants.

Quoted 5 lines

    * original code could be implemented in a single DMA chain on at least two systems I happen to have handy ... one IRQ.

    * this version requires two separate chains, with an intervening task schedule.  (More than twice the cost.)

They differ by at least the cost of an extra IRQ and task schedule, so the performance delta would be load-dependant. In a real-time setup where that task was given time slices at fixed intervals, that might mean the two-request version takes twice as long. ;)

Quoted 4 lines

Whoops. Lemme express my thoughts here.
First, the DMA controller that can handle chained requests which are working with different devices (i.e. write then read does that - first mem2per, then per2mem) are *very* rare thing.

The way they usually work is allocate two DMA channels to the device, one for read and one for write. The controllers just makes sure there's one byte read for every one written, and the DMA channels just stream along with them. (Possibly one channel talks to a bitbucket.)

AT91, OMAP, and PXA hardware all claim hardware support sufficient to do that. (Errata aside.) That's not even rare hardware, much less "very rare"; they're stocked at DigiKey, ready to throw into boards.

Quoted 8 lines
Quoted 6 lines

- Chipselect works differently in your code. You're giving one driver control over all the devices sharing a controller, by blocking requests going to other devices until your driver yields the chipselect.

The controllers we were working with are _not_ able to handle simultaneous requests to different devices on the same bus.

Of course not; SPI doesn't work that way. But my point was that your changes show a controller driver that's clearly required to "lock" the bus, through that chipselect, between requests ... which is not only antithetical to the "requests are asynchronous" model that had earlier been agreed on, but also creates problems of its own:

Quoted 2 lines
Quoted 2 lines

  This seems like a bad idea even if you ignore how it produces priority inversions in your framework.  :)

So consider two different tasks accessing devices on the same SPI bus.

One is lower priority, currently waiting for an SPI request to complete. A request that has your magic "leave me owning chipselect/bus after this request flag" ... because the first thing it's going to do when it returns is start another transfer. (And in the case of the MTD driver, that transfer could already have been queued, removing the issue as well as the need for that flag.)

Now the high priority task issues a request to the other device on that same SPI bus. This means that *two* other tasks ought to be temporarily operating with that higher priority: whatever task you've allocated to manage the I/O queue on that bus (plus maybe an IRQ task with RT_PREEMPT); and the task that's waiting for that transfer to complete. Inversions ...

- Dave

#5 lore raw
In reply to Vitaly Wool
Quoted 6 lines · Vitaly Wool

The main changes are:

- Matching rmk's 2.6.14-git5+ changes for device_driver suspend and resume calls
- Matching rmk's request to get rid of device_driver's calls to suspend/resume/probe/remove

Thanks. Please see comments below though.

Documentation/spi.txt line 224

Quoted 11 lines
Documentation/spi.txt new file +11 -0
+iii. struct spi_driver
+~~~~~~~~~~~~~~~~~~~~~~
+
+struct spi_driver {
+    	void* (*alloc)( size_t, int );
+    	void  (*free)( const void* );
+    	unsigned char* (*get_buffer)( struct spi_device*, void* );
+    	void (*release_buffer)( struct spi_device*, unsigned char*);
+    	void (*control)( struct spi_device*, int mode, u32 ctl );
+        struct device_driver driver;
+};

This doesn't appear to have been updated.

Documentation/spi.txt line 240

Quoted 12 lines
Documentation/spi.txt new file +12 -0
+formed spi_driver structure:
+	extern struct bus_type spi_bus;
+	static struct spi_driver pnx4008_eeprom_driver = {
+        	.driver = {
+                   	.bus = &spi_bus,
+                   	.name = "pnx4008-eeprom",
+                   	.probe = pnx4008_spiee_probe,
+                   	.remove = pnx4008_spiee_remove,
+                   	.suspend = pnx4008_spiee_suspend,
+                   	.resume = pnx4008_spiee_resume,
+               	},
+};

Ditto.

Quoted 7 lines
Documentation/spi.txt new file +7 -0
+iv. struct spi_bus_driver
+~~~~~~~~~~~~~~~~~~~~~~~~~
+To handle transactions over the SPI bus, the spi_bus_driver structure must
+be prepared and registered with core. Any transactions, either synchronous
+or asynchronous, go through spi_bus_driver->xfer function.
+
+struct spi_bus_driver

Documentation/spi.txt line 269

Quoted 10 lines
Documentation/spi.txt new file +10 -0
+{
+        int (*xfer)( struct spi_msg* msgs );
+        void (*select) ( struct spi_device* arg );
+        void (*deselect)( struct spi_device* arg );
+
+	struct spi_msg *(*retrieve)( struct spi_bus_driver *this, struct spi_bus_data *bd);
+	void (*reset)( struct spi_bus_driver *this, u32 context);
+
+        struct device_driver driver;
+};

Does this need updating as well?

Quoted 43 lines
Documentation/spi.txt new file +12 -0
+spi_bus_driver structure:
+	static struct spi_bus_driver spipnx_driver = {
+        .driver = {
+                   .bus = &platform_bus_type,
+                   .name = "spipnx",
+                   .probe = spipnx_probe,
+                   .remove = spipnx_remove,
+                   .suspend = spipnx_suspend,
+                   .resume = spipnx_resume,
+                   },
+        .xfer = spipnx_xfer,
+};

From this it looks like it does.

drivers/spi/spi-core.c new file +28 -0
+/**
+ * spi_bus_suspend - suspend all devices on the spi bus
+ *
+ * @dev: spi device to be suspended
+ * @message: PM message
+ *
+ * This function set device on SPI bus to suspended state, just like platform_bus does
+**/
+static int spi_bus_suspend(struct device * dev, pm_message_t message)
+{
+	int ret = 0;
+
+	if (dev && dev->driver && dev->driver->suspend ) {
+		ret = TO_SPI_DRIVER(dev->driver)->suspend( TO_SPI_DEV(dev), message);
+		if (ret == 0 )
+			dev->power.power_state = message;
+	}
+	return ret;
+}
+
+/**
+ * spi_bus_resume - resume functioning of all devices on spi bus
+ *
+ * @dev: device to resume
+ *
+ * This function resumes device on SPI bus, just like platform_bus does
+**/
+static int spi_bus_resume(struct device * dev)

drivers/spi/spi-core.c line 81

Quoted 10 lines
drivers/spi/spi-core.c new file +10 -0
+{
+	int ret = 0;
+
+	if (dev && dev->driver && dev->driver->suspend ) {
+		ret = TO_SPI_DRIVER(dev->driver)->resume(TO_SPI_DEV(dev));
+		if (ret == 0)
+			dev->power.power_state = PMSG_ON;
+	}
+	return ret;
+}

Ok, your bus_type suspend/resume methods call your spi_driver's suspend and resume methods - good.

Quoted 8 lines
drivers/spi/spi-core.c new file +8 -0
+/*
+ * the functions below are wrappers for corresponding device_driver's methods
+ */
+int spi_driver_probe (struct device *dev)
+{
+	struct spi_driver *sdrv = TO_SPI_DRIVER(dev->driver);
+	struct spi_device *sdev = TO_SPI_DEV(dev);
+

drivers/spi/spi-core.c line 638

Quoted 10 lines
drivers/spi/spi-core.c new file +10 -0
+	return sdrv && sdrv->probe ? sdrv->probe(sdev) : -EFAULT;
+}
+
+int spi_driver_remove (struct device *dev)
+{
+	struct spi_driver *sdrv = TO_SPI_DRIVER(dev->driver);
+	struct spi_device *sdev = TO_SPI_DEV(dev);
+
+	return  (sdrv && sdrv->remove)  ? sdrv->remove(sdev) : -EFAULT;
+}

These are fine, although sdrv will always be non-NULL here.

drivers/spi/spi-core.c line 647

Quoted 9 lines
drivers/spi/spi-core.c new file +9 -0
+
+void spi_driver_shutdown (struct device *dev)
+{
+	struct spi_driver *sdrv = TO_SPI_DRIVER(dev->driver);
+	struct spi_device *sdev = TO_SPI_DEV(dev);
+
+	if (sdrv && sdrv->shutdown)
+		sdrv->shutdown(sdev);
+}

dev->driver may be NULL here. If it is NULL, sdrv will not be. Hence you want to do:

	if (dev->driver && sdrv->shutdown)

instead.

Quoted 6 lines
drivers/spi/spi-core.c new file +6 -0
+
+int spi_driver_suspend (struct device *dev, pm_message_t pm)
+{
+	struct spi_driver *sdrv = TO_SPI_DRIVER(dev->driver);
+	struct spi_device *sdev = TO_SPI_DEV(dev);
+

drivers/spi/spi-core.c line 663

Quoted 10 lines
drivers/spi/spi-core.c new file +10 -0
+	return (sdrv && sdrv->suspend) ?  sdrv->suspend(sdev, pm) : -EFAULT;
+}
+
+int spi_driver_resume (struct device *dev)
+{
+	struct spi_driver *sdrv = TO_SPI_DRIVER(dev->driver);
+	struct spi_device *sdev = TO_SPI_DEV(dev);
+
+	return (sdrv && sdrv->resume) ? sdrv->resume(sdev) : -EFAULT;
+}

If the bus_type does not call the device_driver suspend/resume methods, these are not necessary.

Another oddity I notice is that if there isn't a driver or method, you're returning -EFAULT - seems odd since if there isn't a driver do you really want to prevent suspend/resume?

include/linux/spi.h line 380

Quoted 9 lines
include/linux/spi.h new file +8 -0
+static inline int spi_driver_add(struct spi_driver *drv)
+{
+	drv->driver.bus = &spi_bus;
+	drv->driver.probe = spi_driver_probe;
+	drv->driver.remove = spi_driver_remove;
+	drv->driver.shutdown = spi_driver_shutdown;
 
+	drv->driver.suspend = spi_driver_suspend;
+	drv->driver.resume = spi_driver_resume;

As a result of the comment above, you don't need these two initialisers.

Signature · 3 lines
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core
#6 lore raw
Quoted 61 lines · Russell King

Documentation/spi.txt line 224

Quoted 13 lines
Documentation/spi.txt new file +11 -0
+iii. struct spi_driver
+~~~~~~~~~~~~~~~~~~~~~~
+
+struct spi_driver {
+    	void* (*alloc)( size_t, int );
+    	void  (*free)( const void* );
+    	unsigned char* (*get_buffer)( struct spi_device*, void* );
+    	void (*release_buffer)( struct spi_device*, unsigned char*);
+    	void (*control)( struct spi_device*, int mode, u32 ctl );
+        struct device_driver driver;
+};

This doesn't appear to have been updated.

Quoted 4 lines
Documentation/spi.txt new file +2 -0
+formed spi_driver structure:
+	extern struct bus_type spi_bus;

Documentation/spi.txt line 240

Quoted 10 lines
Documentation/spi.txt new file +10 -0
+	static struct spi_driver pnx4008_eeprom_driver = {
+        	.driver = {
+                   	.bus = &spi_bus,
+                   	.name = "pnx4008-eeprom",
+                   	.probe = pnx4008_spiee_probe,
+                   	.remove = pnx4008_spiee_remove,
+                   	.suspend = pnx4008_spiee_suspend,
+                   	.resume = pnx4008_spiee_resume,
+               	},
+};

Ditto.

Quoted 9 lines
Documentation/spi.txt new file +7 -0
+iv. struct spi_bus_driver
+~~~~~~~~~~~~~~~~~~~~~~~~~
+To handle transactions over the SPI bus, the spi_bus_driver structure must
+be prepared and registered with core. Any transactions, either synchronous
+or asynchronous, go through spi_bus_driver->xfer function.
+
+struct spi_bus_driver

Documentation/spi.txt line 269

Quoted 10 lines
Documentation/spi.txt new file +10 -0
+{
+        int (*xfer)( struct spi_msg* msgs );
+        void (*select) ( struct spi_device* arg );
+        void (*deselect)( struct spi_device* arg );
+
+	struct spi_msg *(*retrieve)( struct spi_bus_driver *this, struct spi_bus_data *bd);
+	void (*reset)( struct spi_bus_driver *this, u32 context);
+
+        struct device_driver driver;
+};

Does this need updating as well?

Quoted 19 lines
Quoted 4 lines
Documentation/spi.txt new file +2 -0
+spi_bus_driver structure:
+	static struct spi_bus_driver spipnx_driver = {

Documentation/spi.txt line 302

Quoted 10 lines
Documentation/spi.txt new file +10 -0
+        .driver = {
+                   .bus = &platform_bus_type,
+                   .name = "spipnx",
+                   .probe = spipnx_probe,
+                   .remove = spipnx_remove,
+                   .suspend = spipnx_suspend,
+                   .resume = spipnx_resume,
+                   },
+        .xfer = spipnx_xfer,
+};

From this it looks like it does.

Yep, thanks for pointing that out. The Documentation part needs to be updated. Will do soon.

Quoted 24 lines
Quoted 10 lines
drivers/spi/spi-core.c new file +6 -0
+
+int spi_driver_suspend (struct device *dev, pm_message_t pm)
+{
+	struct spi_driver *sdrv = TO_SPI_DRIVER(dev->driver);
+	struct spi_device *sdev = TO_SPI_DEV(dev);
+

drivers/spi/spi-core.c line 663

Quoted 10 lines
drivers/spi/spi-core.c new file +10 -0
+	return (sdrv && sdrv->suspend) ?  sdrv->suspend(sdev, pm) : -EFAULT;
+}
+
+int spi_driver_resume (struct device *dev)
+{
+	struct spi_driver *sdrv = TO_SPI_DRIVER(dev->driver);
+	struct spi_device *sdev = TO_SPI_DEV(dev);
+
+	return (sdrv && sdrv->resume) ? sdrv->resume(sdev) : -EFAULT;
+}

If the bus_type does not call the device_driver suspend/resume methods, these are not necessary.

Quoted 5 lines

Another oddity I notice is that if there isn't a driver or method, you're returning -EFAULT - seems odd since if there isn't a driver do you really want to prevent suspend/resume?

Yep, I must admit here that it's really better to do something like if (!dev->driver)
return 0;
else if (sdrv->suspend)
return sdrv->suspend(...)

Does that sound better to you?

Quoted 13 lines
Quoted 10 lines
include/linux/spi.h new file +6 -0
+static inline int spi_driver_add(struct spi_driver *drv)
+{
+	drv->driver.bus = &spi_bus;
+	drv->driver.probe = spi_driver_probe;
+	drv->driver.remove = spi_driver_remove;
+	drv->driver.shutdown = spi_driver_shutdown;
Quoted 9 lines

include/linux/spi.h line 380

Quoted 4 lines
include/linux/spi.h new file +2 -0
+	drv->driver.suspend = spi_driver_suspend;
+	drv->driver.resume = spi_driver_resume;

As a result of the comment above, you don't need these two initialisers.

Yup. Thanks.

Vitaly

#7 lore raw
In reply to Vitaly Wool
Quoted 8 lines · Vitaly Wool

Again, some advantages of our core compared to David's I'd like to mention

- it can be compiled as a module
- it is priority inversion-free
- it can gain more performance with multiple controllers
- it's more adapted for use in real-time environments
- it's not so lightweight, but it leaves less effort for the bus driver developer.

Quoted 4 lines

(As a response to the last bullet David claims that we limit the flexibility. It's not correct.
The core method for message retrieval is just a default one and can easily be overridden by the bus driver. It's a common practice, for instance, see MTD/NAND interface.)

It's also been proven to work on SPI EEPROMs and SPI WiFi module (the latter was a really good survival test! :)).

I have a question about your proposed core. But first a little background:

My board has a 3 Cirrus Logic SPI devices (CS8415A, CS8405A and a CS4341) connected to a PXA255 NSSP port. I have implemented the PXA2xx NSSP SPI driver with DMA support using Davids framework and implemented an working CS8415A driver.

Page 18 of the CS8415A data sheet discusses the SPI IO operation. Three paragraphs and 1 timing diagram.

http://www.cirrus.com/en/pubs/proDatasheet/CS8415A_F4.pdf

The critical things to get from the datasheet are:

1) The chip has an internal register file pointer MAP which must be positioned before write and read register operations.

2) The MAP has a auto-increment feature.

3) Register writes can be performed in one chip select cycles while register reads MAY require a MAP write cycle first and thus require two chip select cycles.

Now assume the CS8415A register operations will be generated from two different sources: "process context" and "interrupt context". This assumption forces a "guaranteed message order" requirement onto the IO Model because of the possibility that an "interrupt context" will move the MAP in between an "process context" write MAP message and read register message. If this is not clear, let me know because it is important.

I'm using David's SPI IO model to enforce "guaranteed message order" by building multiple write/read transfers in a single SPI message which is guaranteed execute in the correct order.

How do I accomplish the same with your core?

-Stephen

#8 lore raw
In reply to Vitaly Wool
Quoted 1 line · Vitaly Wool

- Matching Greg K-H's requests expressed in his review

No, you got the kernel doc wrong, and you didn't move your inline functions.

Quoted 1 line

- The character device interface was reworked

reworked how?

Quoted 1 line

- it's more adapted for use in real-time environments

I still do not see why you are stating this. Why do you say this?

I think you should work with David more...

thanks,

greg k-h

#9 lore raw
Quoted 7 lines · Greg KH
Quoted 3 lines

- The character device interface was reworked

reworked how?

It was originally designed for 2.6.10 and now it's 2.6-git-synchronized.

Quoted 9 lines
Quoted 5 lines

- it's more adapted for use in real-time environments

I still do not see why you are stating this. Why do you say this?

Due to possible priority inversion problems in David's core.

Quoted 3 lines

I think you should work with David more...

P'haps you're right. I suggest re-enumerating all the differences between the cores and working them out.
However, if David's not going to accept any facts or speculations that contradict his being sure his core is the best a man could ever do, we're screwed. :(

Vitaly

#10 lore raw
Quoted 13 lines · --- Vitaly Wool
Quoted 7 lines · Greg KH
Quoted 3 lines

- The character device interface was reworked

reworked how?

It was originally designed for 2.6.10 and now it's 2.6-git-synchronized.

Quoted 2 lines
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

- it's more adapted for use in real-time environments

I still do not see why you are stating this. Why do you say this?

Due to possible priority inversion problems in David's core.

Which you still haven't proven, in fact you now seem to be changing your mind and saying that there might be a problem if an adapter driver was implemented badly although I still don't see how this could happen (the priority inversion I mean not the badly implemented driver ;).

Quoted 9 lines
Quoted 4 lines

I think you should work with David more...

P'haps you're right. I suggest re-enumerating all the differences between the cores and working them out.
However, if David's not going to accept any facts or speculations that contradict his being sure his core is the best a man could ever do, we're screwed. :(

When I worked with David he was very helpful. He pointed out my misconceptions and accepted my comments that is core was missing functionality which he then added.

In fact at one time there were 3 SPI subsystems being prosposed, David's, Vitaly's and mine. David and I work together to take the best from both which is now the solution he is proposing.

Mark

Quoted 8 lines

Vitaly
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

___________________________________________________________
Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail http://uk.messenger.yahoo.com

#11 lore raw
In reply to Vitaly Wool
Quoted 6 lines · Vitaly Wool
Quoted 4 lines · Greg KH
Quoted 2 lines

- it's more adapted for use in real-time environments

I still do not see why you are stating this. Why do you say this?

Due to possible priority inversion problems in David's core.

I am still not convinced of your statements here, sorry. Specifics please.

thanks,

greg k-h

#12 lore raw

PATCH 1/1 SPI core refresh

In reply to Vitaly Wool

Greetings,

This is an updated version of SPI framework developed by Dmitry Pervushin and Vitaly Wool.

The main changes are:

- simplified memory allocation framework in case of non-DMAable buffers
- Greg's review matched
- thread-based handling of the async messages is now an option
- spi_msg is now an abstract structure for SPI core users
- spi_msg can now be chained
- documentation updated

Again, some advantages of our core compared to David's I'd like to mention

- it can be compiled as a module (as opposed to basic David's core w/o Mark Underwood's patch)
- it is less priority inversion-exposed
- it can gain more performance with multiple controllers
- it's more adapted for use in real-time environments

Well, what else?
1. Now thw footprint of the pure (i. e. w/o device interface and thread-based handling) .text section is _less_ than 2k for ARM.
2. We still think that thread-based async messages processing will be the most commonly used option so we didn't remove it from core but made it a compication option whether to include it or not. In any case it can be overridden by a specific bus driver, so even when compiled in, thread-based handling won't necessarily _start_ the threads, so the overhead is minimal even in that case.
3. We still don't feel comportable with complicated structure of SPI message in David's core being exposed to all over the world. On the other hand, chaining SPI messages can really be helpful, so we added this option to our core (yeeeep, convergence is gong on :)) but
4. We made struct spi_msg not being exposed to anywhere except the core itself. Thus we're free to change its implementation in future if necessary.
5. Some changes weren't tested thoroughly yet, so you may encounter problems. Feel free to provide feedback and patches :)

 Documentation/spi.txt    |  115 +++++++++
 arch/arm/Kconfig         |    2 
 drivers/Kconfig          |    2 
 drivers/Makefile         |    1 
 drivers/spi/Kconfig      |   39 +++
 drivers/spi/Makefile     |   16 +
 drivers/spi/spi-core.c   |  551 +++++++++++++++++++++++++++++++++++++++++++++++
 drivers/spi/spi-core.h   |   41 +++
 drivers/spi/spi-dev.c    |  191 ++++++++++++++++
 drivers/spi/spi-thread.c |  192 ++++++++++++++++
 drivers/spi/spi-thread.h |   33 ++
 include/linux/spi.h      |  169 ++++++++++++++
 12 files changed, 1352 insertions(+)
arch/arm/Kconfig +2 -0
diff -uNr linux-2.6.orig/arch/arm/Kconfig linux-2.6/arch/arm/Kconfig
@@ -748,6 +748,8 @@
 
 source "drivers/mmc/Kconfig"
 
+source "drivers/spi/Kconfig"
+
 endmenu
 
 source "fs/Kconfig"
Documentation/spi.txt +115 -0

115 lines not on this page · raw message

drivers/Kconfig +2 -0
diff -uNr linux-2.6.orig/drivers/Kconfig linux-2.6/drivers/Kconfig
@@ -44,6 +44,8 @@
 
 source "drivers/i2c/Kconfig"
 
+source "drivers/spi/Kconfig"
+
 source "drivers/w1/Kconfig"
 
 source "drivers/hwmon/Kconfig"
drivers/Makefile +1 -0
diff -uNr linux-2.6.orig/drivers/Makefile linux-2.6/drivers/Makefile
@@ -69,3 +69,4 @@
 obj-y				+= firmware/
 obj-$(CONFIG_CRYPTO)		+= crypto/
 obj-$(CONFIG_SUPERH)		+= sh/
+obj-$(CONFIG_SPI)		+= spi/
drivers/spi/Kconfig +39 -0
diff -uNr linux-2.6.orig/drivers/spi/Kconfig linux-2.6/drivers/spi/Kconfig
@@ -0,0 +1,39 @@
+#
+# SPI device configuration
+#
+menu "SPI support"
+
+config SPI
+	tristate "SPI (Serial Peripheral Interface) bus support"
+        default false
+	help
+	  Say Y if you need to enable SPI support on your kernel.
+ 	  Say M if you want to create the spi-core loadable module.
+
+config SPI_THREAD
+	bool "Threaded handling of SPI asynchronous messages"
+	default true
+	help
+	  Say Y here to compile thread-based asynchronous message
+	  handling for SPI. This will be a default SPI async message
+	  handling method, which can be overridden by bus driver.
+	  If unsure, say Y.
+
+config SPI_DEBUG
+	bool "SPI debug output"
+	depends on SPI
+	default false
+	help
+          Say Y there if you'd like to see debug output from SPI drivers
+	  If unsure, say N
+
+config SPI_CHARDEV
+	default Y
+	bool "SPI device interface"
+	depends on SPI
+	help
+	  Say Y here to use /dev/spiNN device files. They make it possible to have user-space
+	  programs use the SPI bus.
+
+endmenu
+
drivers/spi/Makefile +16 -0
diff -uNr linux-2.6.orig/drivers/spi/Makefile linux-2.6/drivers/spi/Makefile
@@ -0,0 +1,16 @@
+#
+# Makefile for the kernel spi bus driver.
+#
+
+spi-y += spi-core.o
+spi-$(CONFIG_SPI_CHARDEV) += spi-dev.o
+spi-$(CONFIG_SPI_THREAD) += spi-thread.o
+# bus drivers
+# ...functional drivers
+# ...and the common spi-dev driver
+obj-$(CONFIG_SPI) += spi.o
+
+ifeq ($(CONFIG_SPI_DEBUG),y)
+EXTRA_CFLAGS += -DDEBUG
+endif
+
drivers/spi/spi-core.c +551 -0

551 lines not on this page · raw message

drivers/spi/spi-core.h +41 -0

41 lines not on this page · raw message

drivers/spi/spi-dev.c +191 -0

191 lines not on this page · raw message

drivers/spi/spi-thread.c +192 -0

192 lines not on this page · raw message

drivers/spi/spi-thread.h +33 -0
diff -uNr linux-2.6.orig/drivers/spi/spi-thread.h linux-2.6/drivers/spi/spi-thread.h
@@ -0,0 +1,33 @@
+/*
+ *  linux/drivers/spi/spi-thread.h
+ *
+ *  Copyright (C) 2005 MontaVista Software, Inc <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License.
+ *
+ */
+#ifndef __SPI_THREAD_H
+#define __SPI_THREAD_H
+
+
+static inline void *spi_start_async (struct device *dev) 
+{
+#ifdef CONFIG_SPI_THREAD
+extern void *__spi_start_async (struct device *dev);
+	return  __spi_start_async(dev);
+#else 
+	return 0;
+#endif
+}
+
+static inline void spi_stop_async (struct device *dev, void *t)
+{
+#ifdef CONFIG_SPI_THREAD
+extern void __spi_stop_async (struct device *dev, void *t);
+	__spi_stop_async (dev, t);
+#endif
+}
+
+#endif /* __SPI_THREAD_H */
include/linux/spi.h +169 -0

169 lines not on this page · raw message

#13 lore raw
Quoted 5 lines · Vitaly Wool

Again, some advantages of our core compared to David's I'd like to mention

- it can be compiled as a module (as opposed to basic David's core w/o Mark Underwood's patch)
- it is less priority inversion-exposed

These are actually minor issues, with almost trivial fixes. (Pending.)

And I'd argue about the priority inversion thing ... the inversions in your stuff come from the API, not the implementation. Which makes the problems inherent, rather than fixable: "more" exposed, not "less".

Quoted 1 line

- it can gain more performance with multiple controllers

If this isn't a repeat of that priority-inversion case (fix available), then I don't see what you're implying. I have a hard time seeing any potential for an issue there, since the I/O request path just shortcuts to the controller driver. It can't exactly get in the way!

Quoted 1 line

- it's more adapted for use in real-time environments

I think you still haven't explained what you mean by this, other than maybe reminding that PREEMPT_RT interacts with some implementations.

Quoted 3 lines

Well, what else?
1. Now thw footprint of the pure (i. e. w/o device interface and thread-based handling) .text section is _less_ than 2k for ARM.

Hey, I started from that size *including* device interface etc. If it's exceeded now, it's because of the extra overhead from wrapping device_driver with some spi_driver code. ;)

Quoted 5 lines

2. We still think that thread-based async messages processing will be the most commonly used option so we didn't remove it from core but made it a compication option whether to include it or not. In any case it can be overridden by a specific bus driver, so even when compiled in, thread-based handling won't necessarily _start_ the threads, so the overhead is minimal even in that case.

Whereas I've just said such threading policies don't belong in a "core" at all. You may have noticed the bitbanging adapter I posted ... oddly, that implementation allocates a thread. Hmm ...

That is: you're not talking about capabilities that aren't already in the SPI patches already circulating in 2.6.15-rc5-mm1 (from Sunday). They're just layered differently ... the core is _minimal_ and those implementation policies can be chosen without adding to the core. (Or impacting drivers that want different implementation policies.)

Quoted 3 lines

3. We still don't feel comportable with complicated structure of SPI message in David's core being exposed to all over the world. On the other hand, chaining SPI messages can really be helpful,

The point has been made that such chaining is actually "essential", since device interaction protocols have constraints like "chipselect must be asserted during all these transfers" or contrariwise "between these transfers, chipselect must be dropped for N microseconds". If the async messages didn't cover such linked message, then two activities could interfere with each other quite badly ... they'd break hardware protocol requirements.

Quoted 2 lines

so we added this option to our core (yeeeep,
convergence is gong on :)) but

My preferred level of convergence would be changes to make your code look more like mine, especially where you're providing a mechanism that's been in mine all along ... hmm, like spi_driver is the same now. I made one such change; maybe it's your turn now. ;)

- Dave

#14 lore raw
Quoted 10 lines · David Brownell
Quoted 6 lines · Vitaly Wool

Again, some advantages of our core compared to David's I'd like to mention

- it can be compiled as a module (as opposed to basic David's core w/o Mark Underwood's patch)
- it is less priority inversion-exposed

Quoted 7 lines

These are actually minor issues, with almost trivial fixes. (Pending.)

And I'd argue about the priority inversion thing ... the inversions in your stuff come from the API, not the implementation. Which makes the problems inherent, rather than fixable: "more" exposed, not "less".

I don't think it's true any more (with this new patch).

Quoted 8 lines
Quoted 7 lines

2. We still think that thread-based async messages processing will be the most commonly used option so we didn't remove it from core but made it a compication option whether to include it or not. In any case it can be overridden by a specific bus driver, so even when compiled in, thread-based handling won't necessarily _start_ the threads, so the overhead is minimal even in that case.

Quoted 5 lines

Whereas I've just said such threading policies don't belong in a "core" at all. You may have noticed the bitbanging adapter I posted ... oddly, that implementation allocates a thread. Hmm ...

Please remember that using threads is just a default option which may be even turned off at the kernel configuration stage.
Using threads for async transfers is reasonable _default_ assumption. It can save time for controller driver dev'ers as well as reduce overall kernel footprint (as opposed to different controller drivers each implementing thread-based async messages handling).

Quoted 7 lines

That is: you're not talking about capabilities that aren't already in the SPI patches already circulating in 2.6.15-rc5-mm1 (from Sunday). They're just layered differently ... the core is _minimal_ and those implementation policies can be chosen without adding to the core. (Or impacting drivers that want different implementation policies.)

Hmm, how are we impacting drivers that want different implementation policies? Please look at the latest core :)

Quoted 9 lines
Quoted 8 lines

3. We still don't feel comportable with complicated structure of SPI message in David's core being exposed to all over the world. On the other hand, chaining SPI messages can really be helpful,

Quoted 9 lines

The point has been made that such chaining is actually "essential", since device interaction protocols have constraints like "chipselect must be asserted during all these transfers" or contrariwise "between these transfers, chipselect must be dropped for N microseconds". If the async messages didn't cover such linked message, then two activities could interfere with each other quite badly ... they'd break hardware protocol requirements.

I don't argue about the need of chaining.
But don't you want to agree that things like this

+ struct spi_transfer x[1] = { { .tx_dma = 0, }, };
...more initialization follows, spread around the code...

are not well-readable and may shadow what's going on and even lead to errors during support/extension of the functionality?
Exposing SPI message structure doesn't look good to me also because you're making it a part of the interface (and thus unlikely to change).
We're hiding spi_msg internals what allows us to be more flexible in implementation (for instance, implement our own allocation technique for spi_msg (more lightweight as the size is always the same).

Yeah thus we don't have an ability to allocate SPI messages on stack as you do, that's what votes for your approach. Yours is thus a bit faster, though I suspect that this method is a possible *danger* for really high-speed devices with data bursts on the SPI bus like WiFi adapters: stack's gonna suffer from large amounts of data allocated.

Quoted 10 lines
Quoted 3 lines

so we added this option to our core (yeeeep, convergence is gong on :)) but

My preferred level of convergence would be changes to make your code look more like mine, especially where you're providing a mechanism that's been in mine all along ... hmm, like spi_driver is the same now. I made one such change; maybe it's your turn now. ;)

Okay :)

Vitaly

#15 lore raw
Quoted 8 lines
Quoted 6 lines

Whereas I've just said such threading policies don't belong in a "core" at all. You may have noticed the bitbanging adapter I posted ... oddly, that implementation allocates a thread. Hmm ...

Please remember that using threads is just a default option which may be even turned off at the kernel configuration stage.

People keep saying "make it a library" in such cases, and that's the kind of layering I've come to think is more appropriate. So that bitbang code needs some reshaping. As a library, not driver or core.

Quoted 5 lines

I don't argue about the need of chaining.
But don't you want to agree that things like this

(quoted diff) +1 -0
+	struct spi_transfer	x[1] = { { .tx_dma = 0, }, };

...more initialization follows, spread around the code...

Not quite accurate. That initializes everything to zero, much like memcpy. What happens later is just kicking in the relevant options.

Quoted 2 lines

are not well-readable and may shadow what's going on and even lead to errors during support/extension of the functionality?

I've had my concerns, but that "zero init is safe" rule makes it easy to add things in backwards-compatible ways. (I'll document it.) So that code is equivalent to GCC calling

static inline void
spi_transfer_init(struct spi_transfer *t, unsigned count)
{ memset(t, 0, count * sizeof t); }

It might be useful having and using some SPI_TRANSFER_INITIALIZER for cases like that one.

Quoted 2 lines

Exposing SPI message structure doesn't look good to me also because you're making it a part of the interface (and thus unlikely to change).

The interface will be the interface, sure. Whatever it is. And will accordingly be unlikely to change. We know that from every other API in Linux and elsewhere; nothing new.

Was there some specific issue you forgot to raise here?

This one includes a chained/atomic message, which we agreed are important. It's also simple to set up, IMO another Good Thing. Dropping transfer sequencing, or making things harder to set up, doesn't sound so good ...

Quoted 3 lines

We're hiding spi_msg internals what allows us to be more flexible in implementation (for instance, implement our own allocation technique for spi_msg (more lightweight as the size is always the same).

What you're doing is requiring a standard dynamic allocation model for your "spi_msg" objects ... one per transfer even, much heavier weight than the "one per transfer group" model of current "spi_message". (And much heavier than stack based allocation too.)

At which point krefcounting should surely kick in, and all kinds of stuff. I'd rather not require such things, but there's no reason such a model couldn't be a layer on top of what I've shown. Either a thin one (just add krefs) or a fat one (which one expects would add real value).

Quoted 5 lines

Yeah thus we don't have an ability to allocate SPI messages on stack as you do, that's what votes for your approach. Yours is thus a bit faster, though I suspect that this method is a possible *danger* for really high-speed devices with data bursts on the SPI bus like WiFi adapters:
stack's gonna suffer from large amounts of data allocated.

No, you're still thinking about a purely synchronous programming model; which we had agreed ages ago was not required.

Have a look at that ADS 7846 driver. It always submits batched requests, which happen to be heap allocated (not stack allocated) since some of them must be issued from hardware IRQ context. The technique generalizes easily. And yes, kzalloc() is your friend.

- Dave

#16 lore raw
Quoted 9 lines · David Brownell
Quoted 3 lines

Please remember that using threads is just a default option which may be even turned off at the kernel configuration stage.

People keep saying "make it a library" in such cases, and that's the kind of layering I've come to think is more appropriate. So that bitbang code needs some reshaping. As a library, not driver or core.

Well, effectively this is the way we do it now... Of course one can establish drivers/spi/lib subdir... :)

Quoted 11 lines
Quoted 3 lines

Exposing SPI message structure doesn't look good to me also because you're making it a part of the interface (and thus unlikely to change).

The interface will be the interface, sure. Whatever it is. And will accordingly be unlikely to change. We know that from every other API in Linux and elsewhere; nothing new.

Was there some specific issue you forgot to raise here?

Well, I've had an experience working with full duplex SPI controller. I'm afraid that if you're gonna support it some time in the future, you'll have to rework the message interface, won't you?

Quoted 10 lines
Quoted 3 lines

We're hiding spi_msg internals what allows us to be more flexible in implementation (for instance, implement our own allocation technique for spi_msg (more lightweight as the size is always the same).

What you're doing is requiring a standard dynamic allocation model for your "spi_msg" objects ... one per transfer even, much heavier weight than the "one per transfer group" model of current "spi_message". (And much heavier than stack based allocation too.)

We can allocate a pool of messages at the very init stage and use our own technique to grab the next one from the pool in spimsg_alloc. So we're not requiring _standard_ dynamic allocation model.

Vitaly

#17 lore raw
In reply to David Brownell
Quoted 8 lines · David Brownell
Quoted 3 lines

Yeah thus we don't have an ability to allocate SPI messages on stack as you do, that's what votes for your approach. Yours is thus a bit faster, though I suspect that this method is a possible *danger* for really high-speed devices with data bursts on the SPI bus like WiFi adapters: stack's gonna suffer from large amounts of data allocated.

No, you're still thinking about a purely synchronous programming model; which we had agreed ages ago was not required.

Ah yes. But wait... I've got an important question here.
For instance, let's take your MTD driver. You're allocating a message structure on stack and passing it then down to spi->master->transfer function.
The benefit you're talking about is that you don't have to use heavyweight memory allocation. But... the transfer is basically async so spi->master->transfer will need to copy your message structure to its own-allocated structure so some memory copying will occur as this might be an async transfer (and therefore the stack-allocated message may be freed at some point when it's yet used!) So your model implies concealed double message allocation/copying, doesn't it?
And if I'm wrong, can you please explain me this?

Vitaly

#18 lore raw
Quoted 10 lines · Vitaly Wool
Quoted 8 lines · David Brownell
Quoted 2 lines

Yeah thus we don't have an ability to allocate SPI messages on stack as you do, that's what votes for your approach. Yours is thus a bit faster, though I suspect that this method is a possible *danger* for really high-speed devices with data bursts on the SPI bus like WiFi adapters: stack's gonna suffer from large amounts of data allocated.

No, you're still thinking about a purely synchronous programming model; which we had agreed ages ago was not required.

Quoted 5 lines

Ah yes. But wait... I've got an important question here.
For instance, let's take your MTD driver. You're allocating a message structure on stack and passing it then down to spi->master->transfer function.
The benefit you're talking about is that you don't have to use heavyweight memory allocation. But... the transfer is basically async so spi->master->transfer will need to copy your message structure to its own-allocated structure so some memory copying will occur as this might be an async transfer (and therefore the stack-allocated message may be freed at some point when it's yet used!) So your model implies concealed double message allocation/copying, doesn't it?
And if I'm wrong, can you please explain me this?

Oh, now looks like I understood what is meant. If a function uses stack-allocated messages, it should ensure that it will not exit until the message is processed (shouldn't it be documented somewhere?). But this solves the problem only partially since this technique fits only the synchronous transfers. Functions targeting async transfers will anyway have to kmalloc the memory for message structure which makes your approach not really more lightweight then ours. It's mainly async transfers that need high throughput; and the drivers based on your core will have to kmalloc memory for messages in that case.

Vitaly

#19 lore raw
Quoted 5 lines
Quoted 5 lines

The benefit you're talking about is that you don't have to use heavyweight memory allocation. But... the transfer is basically async so spi->master->transfer will need to copy your message structure to its own-allocated structure so some memory copying will occur as this

Incorrect, as you note below.

Quoted 9 lines
Quoted 5 lines

might be an async transfer (and therefore the stack-allocated message may be freed at some point when it's yet used!)
So your model implies concealed double message allocation/copying, doesn't it?
And if I'm wrong, can you please explain me this?

Oh, now looks like I understood what is meant. If a function uses stack-allocated messages, it should ensure that it will not exit until the message is processed (shouldn't it be documented somewhere?).

It is documented, but I'll make sure it comes up in a few more of the places this confusion might arise. It's a fairly basic rule for C programming: don't use pointers after they become invalid by means of freeing back to the heap, or invalidating a stack frame.

Quoted 3 lines

But
this solves the problem only partially since this technique fits only the synchronous transfers.

Synchronous transfers can easily use stack allocation for the descriptions, yes.

Not that they need to ... the ads7846 driver allocates its spi_message and spi_transfer objects on the heap both for synchronous operations (temperature and voltage sensing) and for asynch ones (touchscreen tracking from timer and irq).

Quoted 3 lines

Functions targeting async transfers will anyway have to kmalloc the memory for message structure which makes your approach not really more lightweight then ours.

If you measure the number of error/fault cases when you ask how lightweight an API is, it's clearly lighter weight to allow for example one kzalloc -(with spi_message and its N spi_transfer descriptors, plus possibly other driver state) rather than to require many of them. Just one fault path to write -- and debug.

My usual rule of thumb is that 1/3 of code (by lines) must handle
fault cases. So APIs requiring more fault handling require more
driver code ... not lightweight! That was a help, when fitting into a tight size budget. (As appropriate to what's more or less a shift register API, needing to run quickly in uCLinux and such.)

Plus, letting the driver do the kzalloc means there's no new API. No-new-API is another way to promote lighter weight systems. ;)

That said ... I know some people _do_ like krefcounted APIs that do that kind of stuff. Strongly. Greg's been silent here other than pointing out that your request alloc was too fat to inline. Mine is trivially inlined, but not refcounted. Likely there's a happy middle ground, maybe

  mesg = spi_message_alloc(struct spi_device *, unsigned ntrans);
  mesg = spi_message_get(mesg);
  spi_message_put();

Or whatever. Just add a kref to spi_message, and patch against the current mm set (to the core and both drivers, but not necessarily the spi_bitbang stuff).

- Dave

#20 lore raw
Quoted 4 lines · David Brownell
Quoted 3 lines

But this solves the problem only partially since this technique fits only the synchronous transfers.

Quoted 9 lines

Synchronous transfers can easily use stack allocation for the descriptions, yes.

Not that they need to ... the ads7846 driver allocates its spi_message and spi_transfer objects on the heap both for synchronous operations (temperature and voltage sensing) and for asynch ones (touchscreen tracking from timer and irq).

So are you talking about only kzalloc vs kmalloc?
I was trying to compare the approaches in a somehow deeper way.
That said, I meant that not exposing any structures you don't have to expose was usually a right way to do things.
We don't expose SPI message and you do.
The only advantage of such exposure I could think of was possibilily to allocate messages on stack but this approach has some limitations we've just agrred upon.
On the other hand, our approach is flexible in means of message allocation. I. e. a small memory allocation library can be implemented transparently to device drivers that handles message allocation. It's very easy (and lightweight :)) since the message structure is always of a same length... Agree?

Quoted 11 lines

That said ... I know some people _do_ like krefcounted APIs that do that kind of stuff. Strongly. Greg's been silent here other than pointing out that your request alloc was too fat to inline. Mine is trivially inlined, but not refcounted. Likely there's a happy middle ground, maybe

mesg = spi_message_alloc(struct spi_device *, unsigned ntrans); mesg = spi_message_get(mesg);
spi_message_put();

Well, it's pretty much what we do in the latest one...
Though we use one-by-one chaining and not specifying the number of messages in chain.
I'm not sure which approach is better, really. Maybe an API like
struct spi_mjsg *spi_message_alloc(struct spi_device *, unsigned ntrans);
spi_add_transfer(struct spi_msg *, ...);
...
is better than what we've got now (see patch sent 12/05; I plan to post the update tomorrow).
I would just like to say that defining such an API looks better thing to me than dealing with SPI message structure explicitly.

Vitaly

#21 lore raw
Quoted 10 lines
Quoted 8 lines

Synchronous transfers can easily use stack allocation for the descriptions, yes.

Not that they need to ... the ads7846 driver allocates its spi_message and spi_transfer objects on the heap both for synchronous operations (temperature and voltage sensing) and for asynch ones (touchscreen tracking from timer and irq).

So are you talking about only kzalloc vs kmalloc?

Certainly not. But I was re-emphasizing that zero-init rule, in a different part of my reply.

Quoted 4 lines

I was trying to compare the approaches in a somehow deeper way. That said, I meant that not exposing any structures you don't have to expose was usually a right way to do things.
We don't expose SPI message and you do.

What information there _isn't_ in the "have to expose" category?

Yours certainly has some ... clocks as per-message not per-device, and your message utilities still doing kmalloc() and memcpy() in places where it's not clearly necessary. Plus the error-prone notion that completion callbacks could be optional, and a few other things I've pointed out previously.

The choice of an array of spi_transfer rather than a custom singly linked list (not even list_head)? I just picked the one with the smallest mandatory overhead (including adding the least number of fault cases to test and recover from). Lots of APIs made the same choice; it works just fine here. Heck, it's one of the few things here that resembles the I2C API!

Quoted 5 lines

On the other hand, our approach is flexible in means of message allocation. I. e. a small memory allocation library can be implemented transparently to device drivers that handles message allocation. It's very easy (and lightweight :)) since the message structure is always of a same length... Agree?

No, as I explained before. But I'd not mind having optional layers like that, so long as they were in fact optional. Yours is not; plus it's heavy-weight (embedding mallocation of dma bounce buffers and copying into/out of them, even when it's not necessary) and it's not refcounted.

Quoted 6 lines
Quoted 6 lines

That said ... I know some people _do_ like krefcounted APIs that do that kind of stuff. Strongly. Greg's been silent here other than pointing out that your request alloc was too fat to inline. Mine is trivially inlined, but not refcounted. Likely there's a happy middle ground, maybe

Quoted 6 lines
Quoted 5 lines

mesg = spi_message_alloc(struct spi_device *, unsigned ntrans); mesg = spi_message_get(mesg);
spi_message_put();

Well, it's pretty much what we do in the latest one...

No, you have no get/put krefcounting, and (to repeat an earlier comment) you also require "ntrans" separate allocations. So "what 'we' do in the latest one" is substantially different.

Quoted 3 lines

Though we use one-by-one chaining and not specifying the number of messages in chain.
I'm not sure which approach is better, really.

In terms of potential faults that requires (debugging) driver code to recover from, having a single allocation is a clear win. Have you ever noticed now many patches go into Linux to handle cases where driver fault cleanup got confused, and oopsed in one of the less common (but still observed in the Real World) scenarios?

Quoted 4 lines

Maybe an API like
struct spi_mjsg *spi_message_alloc(struct spi_device *, unsigned ntrans); spi_add_transfer(struct spi_msg *, ...);
...

I thought about such an "add" call, but there'd need to be three of them (to add a TX buffer, an RX buffer, or both TX and RX buffers) and it really doesn't seem even conceptually hard to expect folk to write

	msg->transfer[0].tx_buf = ...;
	msg->transfer[0].length = ...;

	msg->transfer[1].rx_buf = ...;
	msg->transfer[1].length = ...;

	msg->transfer[2].tx_buf = ...;
	msg->transfer[2].rx_buf = ...;
	msg->transfer[2].length = ...;

And in fact, having that stuff explicit seems preferable to me; no point in "convenience" wrappers for something already that simple.

The only potentially useful thing about an spi_transfer_add_{rx,tx,txrx}() set of inlines would be that it might make the protocol tweaking options stand out more. Say, like the "drop chipselect for 20 usec after this and before the next one", or other customization. Me, I'd rather highlight such options with intelligent use of whitespace and comments.

Quoted 4 lines

is better than what we've got now (see patch sent 12/05; I plan to post the update tomorrow).
I would just like to say that defining such an API looks better thing to me than dealing with SPI message structure explicitly.

You've heard where and why I disagree. But I'd probably not turn down a patch that adds optional krefcounting support for spi_message, returning a message with the transfer[] array ready to be filled out.

- Dave

#22 lore raw
Quoted 9 lines · David Brownell
Quoted 5 lines

I was trying to compare the approaches in a somehow deeper way.
That said, I meant that not exposing any structures you don't have to expose was usually a right way to do things. We don't expose SPI message and you do.

What information there _isn't_ in the "have to expose" category?

I'm sure that any message internals aren't; it leaves freedom to implement messages allocation in the way we like.

Quoted 7 lines

Yours certainly has some ... clocks as per-message not per-device, and your message utilities still doing kmalloc() and memcpy() in places where it's not clearly necessary. Plus the error-prone notion that completion callbacks could be optional, and a few other things I've pointed out previously.

What's wrong with a) clocks per device b) no completion callback?
b) just makes it a sync call, it's a convenient thing for a developer.
As for places with kmalloc's and memcpy's, can you just point out one? I'm afraid it's all bare words here.
On the _contrary_, the approach we use allows to use lightweight non-standard allocation methods for messages which you just can't do.

Quoted 8 lines

The choice of an array of spi_transfer rather than a custom singly linked list (not even list_head)? I just picked the one with the smallest mandatory overhead (including adding the least number of fault cases to test and recover from). Lots of APIs made the same choice; it works just fine here. Heck, it's one of the few things here that resembles the I2C API!

Yes, but the transfer array is not of a fixed size, therefore it's not that easy to use custom allocation techniques. I'm afraid though that you don't quite understand what I'm meaning here, but you'll see it in the core I'll post today.

Quoted 7 lines
Quoted 6 lines

On the other hand, our approach is flexible in means of message allocation. I. e. a small memory allocation library can be implemented transparently to device drivers that handles message allocation. It's very easy (and lightweight :)) since the message structure is always of a same length... Agree?

Quoted 7 lines

No, as I explained before. But I'd not mind having optional layers like that, so long as they were in fact optional. Yours is not; plus it's heavy-weight (embedding mallocation of dma bounce buffers and copying into/out of them, even when it's not necessary) and it's not refcounted.

What? What optional layers are you talking about???
And please look *attentively* into the code, memory allocation/copying happens only if a specific flag is set so it's no way it can be unnecessary.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

That said ... I know some people _do_ like krefcounted APIs that do that kind of stuff. Strongly. Greg's been silent here other than pointing out that your request alloc was too fat to inline. Mine is trivially inlined, but not refcounted. Likely there's a happy middle ground, maybe

mesg = spi_message_alloc(struct spi_device *, unsigned ntrans); mesg = spi_message_get(mesg);
spi_message_put();

Quoted 9 lines
Quoted 3 lines

Well, it's pretty much what we do in the latest one...

No, you have no get/put krefcounting, and (to repeat an earlier comment) you also require "ntrans" separate allocations. So "what 'we' do in the latest one" is substantially different.

I was speaking about the API. Sorry for being not that clear here,

Quoted 8 lines
Quoted 7 lines

Though we use one-by-one chaining and not specifying the number of messages in chain. I'm not sure which approach is better, really.

Quoted 7 lines

In terms of potential faults that requires (debugging) driver code to recover from, having a single allocation is a clear win. Have you ever noticed now many patches go into Linux to handle cases where driver fault cleanup got confused, and oopsed in one of the less common (but still observed in the Real World) scenarios?

Not necessarily; if we've got a preallocated memory pool, than it's not that important. On the contrary, having messages of a same size makes it easier for write a custom malloc routine.

Vitaly

#23 lore raw
In reply to David Brownell
Quoted 4 lines · David Brownell
Quoted 3 lines

But this solves the problem only partially since this technique fits only the synchronous transfers.

Quoted 9 lines

Synchronous transfers can easily use stack allocation for the descriptions, yes.

Not that they need to ... the ads7846 driver allocates its spi_message and spi_transfer objects on the heap both for synchronous operations (temperature and voltage sensing) and for asynch ones (touchscreen tracking from timer and irq).

So are you talking about only kzalloc vs kmalloc?
I was trying to compare the approaches in a somehow deeper way.
That said, I meant that not exposing any structures you don't have to expose was usually a right way to do things.
We don't expose SPI message and you do.
The only advantage of such exposure I could think of was possibilily to allocate messages on stack but this approach has some limitations we've just agrred upon.
On the other hand, our approach is flexible in means of message allocation. I. e. a small memory allocation library can be implemented transparently to device drivers that handles message allocation. It's very easy (and lightweight :)) since the message structure is always of a same length... Agree?

Quoted 11 lines

That said ... I know some people _do_ like krefcounted APIs that do that kind of stuff. Strongly. Greg's been silent here other than pointing out that your request alloc was too fat to inline. Mine is trivially inlined, but not refcounted. Likely there's a happy middle ground, maybe

mesg = spi_message_alloc(struct spi_device *, unsigned ntrans); mesg = spi_message_get(mesg);
spi_message_put();

Well, it's pretty much what we do in the latest one...
Though we use one-by-one chaining and not specifying the number of messages in chain.
I'm not sure which approach is better, really. Maybe an API like
struct spi_msg *spi_message_alloc(struct spi_device *, unsigned ntrans);
spi_add_transfer(struct spi_msg *, ...);
...
is better than what we've got now (see patch sent 12/05; I plan to post the update tomorrow).
I would just like to say that defining such an API looks better thing to me than dealing with SPI message structure explicitly.

Vitaly

#24 lore raw
In reply to David Brownell
Quoted 4 lines · David Brownell
Quoted 3 lines

But this solves the problem only partially since this technique fits only the synchronous transfers.

Quoted 9 lines

Synchronous transfers can easily use stack allocation for the descriptions, yes.

Not that they need to ... the ads7846 driver allocates its spi_message and spi_transfer objects on the heap both for synchronous operations (temperature and voltage sensing) and for asynch ones (touchscreen tracking from timer and irq).

So are you talking about only kzalloc vs kmalloc?
I was trying to compare the approaches in a somehow deeper way.
That said, I meant that not exposing any structures you don't have to expose was usually a right way to do things.
We don't expose SPI message and you do.
The only advantage of such exposure I could think of was possibilily to allocate messages on stack but this approach has some limitations we've just agrred upon.
On the other hand, our approach is flexible in means of message allocation. I. e. a small memory allocation library can be implemented transparently to device drivers that handles message allocation. It's very easy (and lightweight :)) since the message structure is always of a same length... Agree?

Quoted 11 lines

That said ... I know some people _do_ like krefcounted APIs that do that kind of stuff. Strongly. Greg's been silent here other than pointing out that your request alloc was too fat to inline. Mine is trivially inlined, but not refcounted. Likely there's a happy middle ground, maybe

mesg = spi_message_alloc(struct spi_device *, unsigned ntrans); mesg = spi_message_get(mesg);
spi_message_put();

Well, it's pretty much what we do in the latest one...
Though we use one-by-one chaining and not specifying the number of messages in chain.
I'm not sure which approach is better, really. Maybe an API like
struct spi_msg *spi_message_alloc(struct spi_device *, unsigned ntrans);
spi_add_transfer(struct spi_msg *, ...);
...
is better than what we've got now (see patch sent 12/05; I plan to post the update tomorrow).
I would just like to say that defining such an API looks better thing to me than dealing with SPI message structure explicitly.

Vitaly