Skip to contentSkip to conversation

Add support for Sony SxS cards

PATCH Superseded Kieran Kunhya 3 participants 7 messages Jul 7, 2014 – Sep 28, 2014 1 message not in the archive Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add support for Sony SxS cards

This patch adds a reverse-engineered read-only driver for Sony SxS cards. SxS cards are an Expresscard based memory card used in professional cameras.

I've not managed to get block device DMA working for some reason and currently this driver uses a memcpy. I may well be using the block device DMA API incorrectly.

The block device DMA code can be found here:
https://github.com/kierank/sxs-linux/blob/dma/sxs.c

Any comments on the DMA code would also be appreciated.

Kieran Kunhya (1):
block: Add support for Sony SxS cards Signed-off-by: Kieran
Kunhya <[email protected]>

 drivers/block/Kconfig  |    9 +
 drivers/block/Makefile |    1 +
 drivers/block/sxs.c    |  491 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 501 insertions(+)
 create mode 100644 drivers/block/sxs.c
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/1 block: Add support for Sony SxS cards Signed-off-by: Kieran Kunhya <[email protected]>

---
 drivers/block/Kconfig  |    9 +
 drivers/block/Makefile |    1 +
 drivers/block/sxs.c    |  491 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 501 insertions(+)
 create mode 100644 drivers/block/sxs.c
drivers/block/Kconfig +9 -0
index 014a1cf..50ed13e 100644
@@ -356,6 +356,15 @@ config BLK_DEV_SX8
 
 	  Use devices /dev/sx8/$N and /dev/sx8/$Np$M.
 
+config BLK_DEV_SXS
+        tristate "Sony SxS card support"
+        depends on PCI
+        ---help---
+          Saying Y or M here will enable support for reading
+          from Sony SxS cards.
+
+          It creates a device called /dev/sxs
+
 config BLK_DEV_RAM
 	tristate "RAM block device support"
 	---help---
drivers/block/Makefile +1 -0
index 02b688d..1e10fb6 100644
@@ -33,6 +33,7 @@ obj-$(CONFIG_VIRTIO_BLK)	+= virtio_blk.o
 
 obj-$(CONFIG_BLK_DEV_SX8)	+= sx8.o
 obj-$(CONFIG_BLK_DEV_HD)	+= hd.o
+obj-$(CONFIG_BLK_DEV_SXS)       += sxs.o
 
 obj-$(CONFIG_XEN_BLKDEV_FRONTEND)	+= xen-blkfront.o
 obj-$(CONFIG_XEN_BLKDEV_BACKEND)	+= xen-blkback/
drivers/block/sxs.c new file +491 -0

491 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#2 lore raw

Re: [PATCH] block: Add support for Sony SxS cards Signed-off-by: Kieran Kunhya <[email protected]>

On 07/07/2014 02:53 AM, Kieran Kunhya wrote:

Signed-off-by: should be here,
not in the Subject line.

Quoted 14 lines
---
 drivers/block/Kconfig  |    9 +
 drivers/block/Makefile |    1 +
 drivers/block/sxs.c    |  491 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 501 insertions(+)
 create mode 100644 drivers/block/sxs.c
drivers/block/Kconfig +0 -0
index 014a1cf..50ed13e 100644
@@ -356,6 +356,15 @@ config BLK_DEV_SX8
 
 	  Use devices /dev/sx8/$N and /dev/sx8/$Np$M.

drivers/block/Kconfig config BLK_DEV_SX8 · line 367

Quoted 10 lines
drivers/block/Kconfig +9 -0
 
+config BLK_DEV_SXS
+        tristate "Sony SxS card support"
+        depends on PCI
+        ---help---
+          Saying Y or M here will enable support for reading
+          from Sony SxS cards.
+
+          It creates a device called /dev/sxs
+

Use tab to indent tristate, depends, and help.
Use tab + 2 spaces to indent the help text.

drivers/block/Makefile obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o · line 36

Quoted 12 lines
drivers/block/Kconfig +0 -0
 config BLK_DEV_RAM
 	tristate "RAM block device support"
 	---help---
drivers/block/Makefile +1 -0
index 02b688d..1e10fb6 100644
@@ -33,6 +33,7 @@ obj-$(CONFIG_VIRTIO_BLK)	+= virtio_blk.o
 
 obj-$(CONFIG_BLK_DEV_SX8)	+= sx8.o
 obj-$(CONFIG_BLK_DEV_HD)	+= hd.o
+obj-$(CONFIG_BLK_DEV_SXS)       += sxs.o

Use tab(s) here ^^^^^^
not spaces.

Quoted 3 lines
drivers/block/Makefile +0 -0
 obj-$(CONFIG_XEN_BLKDEV_FRONTEND)	+= xen-blkfront.o
 obj-$(CONFIG_XEN_BLKDEV_BACKEND)	+= xen-blkback/
Signature · 1 line
~Randy
#3 lore raw

PATCH v2 1/1 block: Add support for Sony SxS cards

---
 drivers/block/Kconfig  |    9 +
 drivers/block/Makefile |    1 +
 drivers/block/sxs.c    |  491 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 501 insertions(+)
 create mode 100644 drivers/block/sxs.c
drivers/block/Kconfig +9 -0
index 014a1cf..8ebb655 100644
@@ -356,6 +356,15 @@ config BLK_DEV_SX8
 
 	  Use devices /dev/sx8/$N and /dev/sx8/$Np$M.
 
+config BLK_DEV_SXS
+	tristate "Sony SxS card support"
+	depends on PCI
+	---help---
+	  Saying Y or M here will enable support for reading
+	  from Sony SxS cards.
+
+	  It creates a device called /dev/sxs
+
 config BLK_DEV_RAM
 	tristate "RAM block device support"
 	---help---
drivers/block/Makefile +1 -0
index 02b688d..59b9c79 100644
@@ -33,6 +33,7 @@ obj-$(CONFIG_VIRTIO_BLK)	+= virtio_blk.o
 
 obj-$(CONFIG_BLK_DEV_SX8)	+= sx8.o
 obj-$(CONFIG_BLK_DEV_HD)	+= hd.o
+obj-$(CONFIG_BLK_DEV_SXS)	+= sxs.o
 
 obj-$(CONFIG_XEN_BLKDEV_FRONTEND)	+= xen-blkfront.o
 obj-$(CONFIG_XEN_BLKDEV_BACKEND)	+= xen-blkback/
drivers/block/sxs.c new file +491 -0

491 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#4 lore raw

PATCH v3 1/1 block: Add support for Sony SxS cards

---
 MAINTAINERS            |    5 +
 drivers/block/Kconfig  |    9 +
 drivers/block/Makefile |    1 +
 drivers/block/sxs.c    |  494 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 509 insertions(+)
 create mode 100644 drivers/block/sxs.c
MAINTAINERS +5 -0
index 3705430..f3a5231 100644
@@ -8539,6 +8539,11 @@ M:	Maxim Levitsky <[email protected]>
 S:	Maintained
 F:	drivers/memstick/core/ms_block.*
 
+SONY SXS CARD SUPPORT
+M:      Kieran Kunhya <[email protected]>
+S:      Maintained
+F:      drivers/block/sxs.c
+
 SOUND
 M:	Jaroslav Kysela <[email protected]>
 M:	Takashi Iwai <[email protected]>
drivers/block/Kconfig +9 -0
index 014a1cf..0b41ee0 100644
@@ -356,6 +356,15 @@ config BLK_DEV_SX8
 
 	  Use devices /dev/sx8/$N and /dev/sx8/$Np$M.
 
+config BLK_DEV_SXS
+       tristate "Sony SxS card support"
+       depends on PCI
+       ---help---
+         Saying Y or M here will enable support for reading
+         from Sony SxS cards.
+
+         It creates a device called /dev/sxs
+
 config BLK_DEV_RAM
 	tristate "RAM block device support"
 	---help---
drivers/block/Makefile +1 -0
index 02b688d..59b9c79 100644
@@ -33,6 +33,7 @@ obj-$(CONFIG_VIRTIO_BLK)	+= virtio_blk.o
 
 obj-$(CONFIG_BLK_DEV_SX8)	+= sx8.o
 obj-$(CONFIG_BLK_DEV_HD)	+= hd.o
+obj-$(CONFIG_BLK_DEV_SXS)	+= sxs.o
 
 obj-$(CONFIG_XEN_BLKDEV_FRONTEND)	+= xen-blkfront.o
 obj-$(CONFIG_XEN_BLKDEV_BACKEND)	+= xen-blkback/
drivers/block/sxs.c new file +494 -0

494 lines not on this page · raw message

Signature · 1 line
1.7.9.5
maintainer
#5 lore raw

Re: [PATCHv3] block: Add support for Sony SxS cards

Quoted 78 lines · Kieran Kunhya
drivers/block/sxs.c new file +78 -0
+static void sxs_memcpy_read(struct sxs_device *dev, unsigned long sector,
+			    unsigned long nsect, char *buffer)
+{
+	struct pci_dev *pdev = dev->pci_dev;
+	u32 status;
+	u32 data[4];
+	u16 *tmp;
+	u32 *tmp2;
+
+	void *dma2;
+	dma_addr_t dma2_handle;
+	void *dma3;
+	dma_addr_t dma3_handle;
+
+	sector >>= dev->sector_shift;
+	nsect >>= dev->sector_shift;
+
+	/* Read */
+	dma2 = pci_alloc_consistent(pdev, 8192, &dma2_handle);
+	dma3 = pci_alloc_consistent(pdev, 8192, &dma3_handle);
+
+	tmp = dma2;
+	tmp2 = dma3;
+	tmp2[0] = dma2_handle;
+	tmp2[2] = dma3_handle;
+
+	dev_dbg_ratelimited(&pdev->dev, "CALL %lu %lu\n",
+			    sector & 0xffffffff, nsect & 0xffffffff);
+
+	reinit_completion(&dev->irq_response);
+	status = readl(dev->mmio+SXS_STATUS_REG);
+	data[0] = cpu_to_le32(0x00010028);
+	data[1] = cpu_to_le32(sector & 0xffffffff);
+	data[2] = 0x0;
+	data[3] = cpu_to_le32(nsect & 0xffffffff);
+	memcpy_toio(dev->mmio, data, sizeof(data));
+	writel(0xa0, dev->mmio+SXS_ENABLE_REG);
+	writel(0x80, dev->mmio+SXS_CONTROL_REG);
+
+	if (!wait_for_completion_timeout(&dev->irq_response,
+					 msecs_to_jiffies(5000))) {
+		dev_dbg(&pdev->dev, "No IRQ\n");
+	}
+
+	reinit_completion(&dev->irq_response);
+	writel(dma3_handle, dev->mmio+SXS_MASTER_LINK_REG_L);
+	writel(0x0, dev->mmio+SXS_MASTER_LINK_REG_H);
+	writel(0x20, dev->mmio+SXS_CONTROL_REG);
+
+	if (!wait_for_completion_timeout(&dev->irq_response,
+					 msecs_to_jiffies(5000))) {
+		dev_dbg(&pdev->dev, "No IRQ\n");
+	}
+
+	/* FIXME: Use DMA properly */
+	memcpy(buffer, dma2, dev->sector_size * nsect);
+
+	dev_dbg_ratelimited(&pdev->dev, "boot-signature %x\n",
+			    tmp[255]);
+
+	writel(0, dev->mmio+SXS_ENABLE_REG);
+
+	pci_free_consistent(pdev, 8192, dma3, dma3_handle);
+	pci_free_consistent(pdev, 8192, dma2, dma2_handle);
+}
+
+static void sxs_request(struct request_queue *q, struct bio *bio)
+{
+	struct bvec_iter iter;
+	struct bio_vec bvec;
+	char *buffer;
+	unsigned long flags;
+	struct sxs_device *dev = q->queuedata;
+	struct pci_dev *pdev = dev->pci_dev;
+	sector_t sector = bio->bi_iter.bi_sector;
+
+	bio_for_each_segment(bvec, bio, iter) {
+		dev_dbg_ratelimited(&pdev->dev, "REQUEST %i %i %i\n",

drivers/block/sxs.c line 176

Quoted 10 lines
drivers/block/sxs.c new file +10 -0
+				    bio_cur_bytes(bio), bio->bi_vcnt,
+				    bvec.bv_len);
+		buffer = bvec_kmap_irq(&bvec, &flags);
+		sxs_memcpy_read(dev, sector, bio_cur_bytes(bio) >> 9,
+				buffer);
+		sector += bio_cur_bytes(bio) >> 9;
+		bvec_kunmap_irq(buffer, &flags);
+	}
+
+	bio_endio(bio, 0);

So a few questions here... This device only does reads? And it seems to be assuming that only reads end up in the request handler? How so?

Second question. IO never fails? There's no status checking and IO is always ended successfully. This too seems odd.

Third, this wong work as-is, at least not of HIGHMEM is used. After the bvec_kmap_irq(), irqs will be disabled. But your sxs_memcpy_read() invokes schedule through the completion waits, that will instantly go bad.

Signature · 1 line
Jens Axboe

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#7 lore raw

Re: [PATCHv3] block: Add support for Sony SxS cards

Quoted 5 lines · Kieran Kunhya
Quoted 2 lines

So a few questions here... This device only does reads? And it seems to be assuming that only reads end up in the request handler? How so?

I have only reverse engineered the read-part of the device. Yes, the driver should check whether the request is a read.

And you should also deny any writeable opens, in that case. Add an open method and check for FMODE_WRITE. Then add a check ala:

	if (bio_data_dir(bio)) {
		bio_endio(bio, -EROFS);
		return;
	}

and the top of your sxs_request().

Quoted 5 lines
Quoted 2 lines

Second question. IO never fails? There's no status checking and IO is always ended successfully. This too seems odd.

I will fix this.

Quoted 7 lines
Quoted 3 lines

Third, this wong work as-is, at least not of HIGHMEM is used. After the bvec_kmap_irq(), irqs will be disabled. But your sxs_memcpy_read() invokes schedule through the completion waits, that will instantly go bad.

Is there a better way of doing this? I spent a long time trying to get DMA to work but couldn't so had to resort to this ugly hack. I assume the memcpy also needs to have some locking somewhere?

It really is pretty horrible and slow, but if you can't get DMA working, then so be it. And yes, you should add a mutex that is held for the duration of the memcpy_read(), otherwise things will go bad very fast if two or more processes attempt to read from it at the same time. A quick work-around for the irq issue would be to have the block layer bounce the highmem pages for you. That's actually the default behavior, but I would make it explicit with a call to:

	blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);

after the blk_queue_make_request() call. If you do that, then get rid of the bvec_kmap_irq(), and just do:

	buffer = page_address(bvec.bv_page) + bvec.bv_offset;

to get the destination for your read.

Signature · 1 line
Jens Axboe