Skip to contentSkip to conversation

firewire: fw-ohci: fix DMA mapping for ar context

PATCH In review Stefan Richter 1 participant 6 messages Oct 16, 2007 Firewire

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 firewire: fw-ohci: fix DMA mapping for ar context

The CPU must not touch the buffer after it was DMA-mapped.

---
 drivers/firewire/fw-ohci.c |   12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/firewire/fw-ohci.c +5 -7
Index: linux/drivers/firewire/fw-ohci.c
===================================================================
@@ -253,12 +253,6 @@ static int ar_context_add_page(struct ar
 	if (ab == NULL)
 		return -ENOMEM;
 
-	ab_bus = dma_map_single(dev, ab, PAGE_SIZE, DMA_BIDIRECTIONAL);
-	if (dma_mapping_error(ab_bus)) {
-		free_page((unsigned long) ab);
-		return -ENOMEM;
-	}
-
 	memset(&ab->descriptor, 0, sizeof(ab->descriptor));
 	ab->descriptor.control        = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
 						    DESCRIPTOR_STATUS |
@@ -269,7 +263,11 @@ static int ar_context_add_page(struct ar
 	ab->descriptor.res_count      = cpu_to_le16(PAGE_SIZE - offset);
 	ab->descriptor.branch_address = 0;
 
-	dma_sync_single_for_device(dev, ab_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
+	ab_bus = dma_map_single(dev, ab, PAGE_SIZE, DMA_BIDIRECTIONAL);
+	if (dma_mapping_error(ab_bus)) {
+		free_page((unsigned long) ab);
+		return -ENOMEM;
+	}
 
 	ctx->last_buffer->descriptor.branch_address = cpu_to_le32(ab_bus | 1);
 	ctx->last_buffer->next = ab;
Signature · 3 lines
Stefan Richter
-=====-=-=== =-=- =----
http://arcgraph.de/sr/
#1 lore raw

drivers/firewire/fw-ohci.c static int ar_context_add_page(struct ar · line 256

Quoted 5 lines · I wrote:
drivers/firewire/fw-ohci.c +0 -1
@@ -253,12 +253,6 @@ static int ar_context_add_page(struct ar
 	if (ab == NULL)
 		return -ENOMEM;
 
-	ab_bus = dma_map_single(dev, ab, PAGE_SIZE, DMA_BIDIRECTIONAL);

[...]

Argh, it's bogus. ab_bus is used immediately after that. Correction follows in a minute.

Signature · 3 lines
Stefan Richter
-=====-=-=== =-=- =----
http://arcgraph.de/sr/
#2 lore raw

PATCH 1/1 firewire: fw-ohci: fix DMA mapping for ar context

The CPU must not touch the buffer after it was DMA-mapped.

---
 drivers/firewire/fw-ohci.c |    1 +
 1 file changed, 1 insertion(+)
drivers/firewire/fw-ohci.c +1 -0
Index: linux/drivers/firewire/fw-ohci.c
===================================================================
@@ -258,6 +258,7 @@ static int ar_context_add_page(struct ar
 		free_page((unsigned long) ab);
 		return -ENOMEM;
 	}
+	dma_sync_single_for_cpu(dev, ab_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
 
 	memset(&ab->descriptor, 0, sizeof(ab->descriptor));
 	ab->descriptor.control        = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
Signature · 3 lines
Stefan Richter
-=====-=-=== =-=- =----
http://arcgraph.de/sr/
#3 lore raw

PATCH 1/1 firewire: fw-ohci: optimize initialization of ar context

Sync only the parts for CPU which are actually accessed by the CPU. Replace a memset by direct assignment.

---
 drivers/firewire/fw-ohci.c |   22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
drivers/firewire/fw-ohci.c +12 -10
Index: linux/drivers/firewire/fw-ohci.c
===================================================================
@@ -258,19 +258,21 @@ static int ar_context_add_page(struct ar
 		free_page((unsigned long) ab);
 		return -ENOMEM;
 	}
-	dma_sync_single_for_cpu(dev, ab_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
+	dma_sync_single_for_cpu(dev, ab_bus, sizeof(ab->descriptor),
+				DMA_BIDIRECTIONAL);
 
-	memset(&ab->descriptor, 0, sizeof(ab->descriptor));
-	ab->descriptor.control        = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
-						    DESCRIPTOR_STATUS |
-						    DESCRIPTOR_BRANCH_ALWAYS);
 	offset = offsetof(struct ar_buffer, data);
-	ab->descriptor.req_count      = cpu_to_le16(PAGE_SIZE - offset);
-	ab->descriptor.data_address   = cpu_to_le32(ab_bus + offset);
-	ab->descriptor.res_count      = cpu_to_le16(PAGE_SIZE - offset);
-	ab->descriptor.branch_address = 0;
+	ab->descriptor.req_count       = cpu_to_le16(PAGE_SIZE - offset);
+	ab->descriptor.control         = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
+						     DESCRIPTOR_STATUS |
+						     DESCRIPTOR_BRANCH_ALWAYS);
+	ab->descriptor.data_address    = cpu_to_le32(ab_bus + offset);
+	ab->descriptor.branch_address  = 0;
+	ab->descriptor.res_count       = cpu_to_le16(PAGE_SIZE - offset);
+	ab->descriptor.transfer_status = 0;
 
-	dma_sync_single_for_device(dev, ab_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
+	dma_sync_single_for_device(dev, ab_bus, sizeof(ab->descriptor),
+				   DMA_BIDIRECTIONAL);
 
 	ctx->last_buffer->descriptor.branch_address = cpu_to_le32(ab_bus | 1);
 	ctx->last_buffer->next = ab;
Signature · 3 lines
Stefan Richter
-=====-=-=== =-=- =----
http://arcgraph.de/sr/
#4 lore raw

PATCH 1/1 firewire: fw-ohci: avoid an atomic allocation

All of the buffers which are allocated during fw-ohci's pci_probe can be allocated with GFP_KERNEL.

---
 drivers/firewire/fw-ohci.c |   10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/firewire/fw-ohci.c +5 -5
Index: linux-2.6.23-rc6/drivers/firewire/fw-ohci.c
===================================================================
@@ -242,14 +242,14 @@ ohci_update_phy_reg(struct fw_card *card
 	return 0;
 }
 
-static int ar_context_add_page(struct ar_context *ctx)
+static int ar_context_add_page(struct ar_context *ctx, gfp_t gfp_mask)
 {
 	struct device *dev = ctx->ohci->card.device;
 	struct ar_buffer *ab;
 	dma_addr_t ab_bus;
 	size_t offset;
 
-	ab = (struct ar_buffer *) __get_free_page(GFP_ATOMIC);
+	ab = (struct ar_buffer *) __get_free_page(gfp_mask);
 	if (ab == NULL)
 		return -ENOMEM;
 
@@ -397,7 +397,7 @@ static void ar_context_tasklet(unsigned 
 			buffer = handle_ar_packet(ctx, buffer);
 
 		free_page((unsigned long)buffer);
-		ar_context_add_page(ctx);
+		ar_context_add_page(ctx, GFP_ATOMIC);
 	} else {
 		buffer = ctx->pointer;
 		ctx->pointer = end =
@@ -418,8 +418,8 @@ ar_context_init(struct ar_context *ctx, 
 	ctx->last_buffer = &ab;
 	tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
 
-	ar_context_add_page(ctx);
-	ar_context_add_page(ctx);
+	ar_context_add_page(ctx, GFP_KERNEL);
+	ar_context_add_page(ctx, GFP_KERNEL);
 	ctx->current_buffer = ab.next;
 	ctx->pointer = ctx->current_buffer->data;
 
Signature · 3 lines
Stefan Richter
-=====-=-=== =-=- =----
http://arcgraph.de/sr/
#5 lore raw
In reply to Stefan Richter
Quoted 13 lines · I wrote:

The CPU must not touch the buffer after it was DMA-mapped.

---
 drivers/firewire/fw-ohci.c |    1 +
 1 file changed, 1 insertion(+)

Index: linux/drivers/firewire/fw-ohci.c
===================================================================

drivers/firewire/fw-ohci.c static int ar_context_add_page(struct ar · line 264

Quoted 7 lines
drivers/firewire/fw-ohci.c +1 -0
@@ -258,6 +258,7 @@ static int ar_context_add_page(struct ar
 		free_page((unsigned long) ab);
 		return -ENOMEM;
 	}
+	dma_sync_single_for_cpu(dev, ab_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
 
 	memset(&ab->descriptor, 0, sizeof(ab->descriptor));
 	ab->descriptor.control        = cpu_to_le16(DESCRIPTOR_INPUT_MORE |

This is still not complete:

in ar_context_add_page():

	ctx->last_buffer->descriptor.branch_address = ...;

in ar_context_tasklet():

	dma_unmap_single(ohci->card.device,
		le32_to_cpu(ab->descriptor.data_address) - offset,
		PAGE_SIZE, DMA_BIDIRECTIONAL);

They both access DMA-mapped areas.

Signature · 3 lines
Stefan Richter
-=====-=-=== =-=- =----
http://arcgraph.de/sr/