Skip to contentSkip to conversation

firewire: ohci: invoke all AT packet callbacks from a workqueue

PATCH Applied Takashi Sakamoto 1 participant 7 messages Sep 19 – Sep 20 Firewire

lore
Share & tools
lore

Takashi Sakamoto

author maintainer
#0 lore raw

cover firewire: ohci: invoke all AT packet callbacks from a workqueue

Hi,

For local-to-local asynchronous transactions, a shortcut is implemented to handle the relevant packets in the request initiator's context instead of queueing them to the hardware. This may seem beneficial since the transaction completes immediately within the software stack.

However, this has the drawback that the context in which the callback handler is invoked is unspecified, as it runs in the initiator's context. This makes it difficult to implement the callback handlers, especially address handlers.

This patchset addresses this issue by using a workqueue. All packet callbacks are now invoked in process context.

Takashi Sakamoto (5):
firewire: ohci: use workqueue to handle error case of AT
request/response packet queueing
firewire: ohci: use workqueue to handle local AT request/response
packets
firewire: ohci: refactor branches in at_context_transmit()
firewire: core: use spinlock without irqsave for card split_timeout
member
firewire: core: use spinlock without irqsave for card topology_map
member

 drivers/firewire/core-card.c        |   7 +-
 drivers/firewire/core-transaction.c |  22 ++---
 drivers/firewire/ohci.c             | 119 ++++++++++++++++++++++------
 include/linux/firewire.h            |   5 +-
 4 files changed, 107 insertions(+), 46 deletions(-)

base-commit: a20545fbf9226591017fc371bdfb1bfd490b0e0b

Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/5 firewire: ohci: use workqueue to handle error case of AT request/response packet queueing

When a packet cannot be queued to an AT context, the packet callback is invoked to report the error to the caller. Since the callback runs in the caller's context, its execution context depends on where the packet was submitted. This makes the callback context inconsistent between successful and failed subactions.

Use a workqueue to handle the error cases.

---
 drivers/firewire/core-card.c |  7 +--
 drivers/firewire/ohci.c      | 90 +++++++++++++++++++++++++++++++++---
 include/linux/firewire.h     |  3 ++
 3 files changed, 91 insertions(+), 9 deletions(-)
drivers/firewire/core-card.c +4 -3
index 23749434d900..be6f18d67ece 100644
@@ -643,11 +643,12 @@ int fw_card_add(struct fw_card *card, u32 max_receive, u32 link_speed, u64 guid,
 	//  * == WQ_FREEZABLE		The target device would not be available when being freezed.
 	//  * == WQ_HIGHPRI		High priority to process semi-realtime timestamped data.
 	//  * == WQ_SYSFS		Parameters are available via sysfs.
-	//  * max_active == 4		A hardIRQ could notify events for a pair of requests and
-	//				response AR/AT contexts.
+	//  * max_active == 4 + 2	A hardIRQ could notify events for a pair of requests and
+	//				response AR/AT contexts. Additional 2 capacity are for the
+	//				internal handling of local AT request and response packets.
 	async_wq = alloc_workqueue("firewire-async-card%u",
 				   WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_HIGHPRI | WQ_SYSFS,
-				   4, card->index);
+				   6, card->index);
 	if (!async_wq)
 		return -ENOMEM;
 
drivers/firewire/ohci.c +84 -6

138 lines not on this page · raw message

include/linux/firewire.h +3 -0
index cbe59a18162e..1c71ff69c42f 100644
@@ -329,6 +329,9 @@ struct fw_packet {
 	bool payload_mapped;
 	u32 timestamp;
 
+	// Used to handle the local-to-local packets in the AT request/response contexts.
+	struct list_head link_for_local;
+
 	/*
 	 * This callback is called when the packet transmission has completed.
 	 * For successful transmission, the status code is the ack received
Signature · 1 line
2.53.0
author maintainer
#2 lore raw

PATCH 2/5 firewire: ohci: use workqueue to handle local AT request/response packets

In reply to Takashi Sakamoto

Local-to-local asynchronous transactions are currently handled in the initiator's context. This requires the request handlers to support any context in which the initiator may run.

Use workqueue to handle the AT request and response packets targeting local address offsets, so that the request handler always runs in process context.

---
 drivers/firewire/ohci.c  | 21 +++++++++------------
 include/linux/firewire.h |  2 +-
 2 files changed, 10 insertions(+), 13 deletions(-)
drivers/firewire/ohci.c +9 -12
index 45f03095a196..dddb08dbb45f 100644
@@ -1546,7 +1546,7 @@ static bool in_bus_management_csr_registers(u64 offset)
 	return in_range(offset, CSR_BUS_MANAGER_ID, 0x22c - CSR_BUS_MANAGER_ID);
 }
 
-static void handle_local_at_request_packet(struct fw_ohci *ohci, struct fw_packet *packet)
+static void handle_at_request_local_packet(struct fw_ohci *ohci, struct fw_packet *packet)
 {
 	// Emulate split transaction.
 	packet->ack = ACK_PENDING;
@@ -1574,7 +1574,7 @@ static void handle_local_at_request_packet(struct fw_ohci *ohci, struct fw_packe
 	}
 }
 
-static void handle_local_at_response_packet(struct fw_ohci *ohci, struct fw_packet *packet)
+static void handle_at_response_local_packet(struct fw_ohci *ohci, struct fw_packet *packet)
 {
 	u64 csr_offset = async_header_get_offset(packet->header) - CSR_REGISTER_BASE;
 
@@ -1589,7 +1589,8 @@ static void handle_local_at_response_packet(struct fw_ohci *ohci, struct fw_pack
 	packet->callback(packet, &ohci->card, packet->ack);
 }
 
-static void handle_at_local_packets(struct at_local *local, struct fw_ohci *ohci)
+static void handle_at_local_packets(struct at_local *local, struct fw_ohci *ohci,
+			void (*handle_at_local_packet)(struct fw_ohci *, struct fw_packet *))
 {
 	struct fw_packet *packet;
 
@@ -1603,6 +1604,8 @@ static void handle_at_local_packets(struct at_local *local, struct fw_ohci *ohci
 			// This case is active when the call of at_context_queue_packet() returns
 			// error in at_context_transmit().
 			packet->callback(packet, &ohci->card, packet->ack);
+		} else {
+			handle_at_local_packet(ohci, packet);
 		}
 
 		spin_lock(&local->lock);
@@ -1616,7 +1619,7 @@ static void at_request_local_work(struct work_struct *work)
 	struct at_local *local = from_work(local, work, work);
 	struct fw_ohci *ohci = container_of(local, struct fw_ohci, at_request_local);
 
-	handle_at_local_packets(local, ohci);
+	handle_at_local_packets(local, ohci, handle_at_request_local_packet);
 }
 
 static void at_response_local_work(struct work_struct *work)
@@ -1624,7 +1627,7 @@ static void at_response_local_work(struct work_struct *work)
 	struct at_local *local = from_work(local, work, work);
 	struct fw_ohci *ohci = container_of(local, struct fw_ohci, at_response_local);
 
-	handle_at_local_packets(local, ohci);
+	handle_at_local_packets(local, ohci, handle_at_response_local_packet);
 }
 
 static void at_local_init(struct at_local *local, work_func_t func)
@@ -1672,13 +1675,7 @@ static void at_context_transmit(struct at_context *ctx, struct fw_packet *packet
 	if (destination_is_local(packet, ohci)) {
 		spin_unlock_irqrestore(&ohci->lock, flags);
 
-		// Timestamping on behalf of the hardware.
-		packet->timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ohci));
-
-		if (ctx == &ohci->at_request_ctx)
-			handle_local_at_request_packet(ohci, packet);
-		else
-			handle_local_at_response_packet(ohci, packet);
+		queue_work_for_at_local_packet(ctx, packet, ohci);
 		return;
 	}
 
include/linux/firewire.h +1 -1
index 1c71ff69c42f..2b065f03565d 100644
@@ -337,7 +337,7 @@ struct fw_packet {
 	 * For successful transmission, the status code is the ack received
 	 * from the destination.  Otherwise it is one of the juju-specific
 	 * rcodes:  RCODE_SEND_ERROR, _CANCELLED, _BUSY, _GENERATION, _NO_ACK.
-	 * The callback can be called from workqueue and thus must never block.
+	 * The callback is called from a workqueue. It is not preferable to block it so long.
 	 */
 	fw_packet_callback_t callback;
 	int ack;
Signature · 1 line
2.53.0
author maintainer
#3 lore raw

PATCH 3/5 firewire: ohci: refactor branches in at_context_transmit()

In reply to Takashi Sakamoto

Refactor the branches to consolidate the handling of local requests and responses, as well as hardware queueing errors.

---
 drivers/firewire/ohci.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)
drivers/firewire/ohci.c +7 -13
index dddb08dbb45f..710640b759e4 100644
@@ -1667,22 +1667,16 @@ static void queue_work_for_at_local_packet(struct at_context *ctx, struct fw_pac
 static void at_context_transmit(struct at_context *ctx, struct fw_packet *packet)
 {
 	struct fw_ohci *ohci = ctx->context.ohci;
-	unsigned long flags;
-	int ret;
-
-	spin_lock_irqsave(&ohci->lock, flags);
+	bool use_work = true;
 
-	if (destination_is_local(packet, ohci)) {
-		spin_unlock_irqrestore(&ohci->lock, flags);
-
-		queue_work_for_at_local_packet(ctx, packet, ohci);
-		return;
+	scoped_guard(spinlock_irqsave, &ohci->lock) {
+		if (!destination_is_local(packet, ohci)) {
+			if (!at_context_queue_packet(ctx, packet))
+				use_work = false;
+		}
 	}
 
-	ret = at_context_queue_packet(ctx, packet);
-	spin_unlock_irqrestore(&ohci->lock, flags);
-
-	if (ret < 0)
+	if (use_work)
 		queue_work_for_at_local_packet(ctx, packet, ohci);
 }
 
Signature · 1 line
2.53.0
author maintainer
#4 lore raw

PATCH 4/5 firewire: core: use spinlock without irqsave for card split_timeout member

In reply to Takashi Sakamoto

Asynchronous packet callbacks are now always invoked in process context. Therefore, the split_timeout member is not accessed from IRQ context.

Use spin_lock() for the split_timeout member since disabling local IRQs is unnecessary.

---
 drivers/firewire/core-transaction.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
drivers/firewire/core-transaction.c +4 -14
index 995c2001bee0..91ed85b7aa22 100644
@@ -204,9 +204,7 @@ static void transmit_complete_callback(struct fw_packet *packet,
 	{
 		unsigned int delta;
 
-		// NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for
-		// local destination never runs in any type of IRQ context.
-		scoped_guard(spinlock_irqsave, &card->split_timeout.lock) {
+		scoped_guard(spinlock, &card->split_timeout.lock) {
 			t->split_timeout_cycle =
 				compute_split_timeout_timestamp(card, packet->timestamp) & 0xffff;
 			delta = card->split_timeout.jiffies;
@@ -900,9 +898,7 @@ static struct fw_request *allocate_request(struct fw_card *card,
 		return NULL;
 	kref_init(&request->kref);
 
-	// NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for
-	// local destination never runs in any type of IRQ context.
-	scoped_guard(spinlock_irqsave, &card->split_timeout.lock)
+	scoped_guard(spinlock, &card->split_timeout.lock)
 		request->response.timestamp = compute_split_timeout_timestamp(card, p->timestamp);
 
 	request->response.speed = p->speed;
@@ -1340,10 +1336,7 @@ static void handle_registers(struct fw_card *card, struct fw_request *request,
 		if (tcode == TCODE_READ_QUADLET_REQUEST) {
 			*data = cpu_to_be32(card->split_timeout.hi);
 		} else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
-			// NOTE: This can be without irqsave when we can guarantee that
-			// __fw_send_request() for local destination never runs in any type of IRQ
-			// context.
-			scoped_guard(spinlock_irqsave, &card->split_timeout.lock) {
+			scoped_guard(spinlock, &card->split_timeout.lock) {
 				card->split_timeout.hi = be32_to_cpu(*data) & 7;
 				update_split_timeout(card);
 			}
@@ -1356,10 +1349,7 @@ static void handle_registers(struct fw_card *card, struct fw_request *request,
 		if (tcode == TCODE_READ_QUADLET_REQUEST) {
 			*data = cpu_to_be32(card->split_timeout.lo);
 		} else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
-			// NOTE: This can be without irqsave when we can guarantee that
-			// __fw_send_request() for local destination never runs in any type of IRQ
-			// context.
-			scoped_guard(spinlock_irqsave, &card->split_timeout.lock) {
+			scoped_guard(spinlock, &card->split_timeout.lock) {
 				card->split_timeout.lo = be32_to_cpu(*data) & 0xfff80000;
 				update_split_timeout(card);
 			}
Signature · 1 line
2.53.0
author maintainer
#5 lore raw

PATCH 5/5 firewire: core: use spinlock without irqsave for card topology_map member

In reply to Takashi Sakamoto

Asynchronous packet callbacks are now always invoked in process context. Therefore, the topology_map member is not accessed from IRQ context.

Use spin_lock() for the topology_map member since disabling local IRQs is unnecessary.

---
 drivers/firewire/core-transaction.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/firewire/core-transaction.c +1 -3
index 91ed85b7aa22..a2a8d755ad0a 100644
@@ -1256,9 +1256,7 @@ static void handle_topology_map(struct fw_card *card, struct fw_request *request
 
 	start = (offset - topology_map_region.start) / 4;
 
-	// NOTE: This can be without irqsave when we can guarantee that fw_send_request() for local
-	// destination never runs in any type of IRQ context.
-	scoped_guard(spinlock_irqsave, &card->topology_map.lock)
+	scoped_guard(spinlock, &card->topology_map.lock)
 		memcpy(payload, &card->topology_map.buffer[start], length);
 
 	fw_send_response(card, request, RCODE_COMPLETE);
Signature · 1 line
2.53.0
author maintainer Applied
#6 lore raw
In reply to Takashi Sakamoto
Quoted 26 lines · Takashi Sakamoto

Hi,

For local-to-local asynchronous transactions, a shortcut is implemented to handle the relevant packets in the request initiator's context instead of queueing them to the hardware. This may seem beneficial since the transaction completes immediately within the software stack.

However, this has the drawback that the context in which the callback handler is invoked is unspecified, as it runs in the initiator's context. This makes it difficult to implement the callback handlers, especially address handlers.

This patchset addresses this issue by using a workqueue. All packet callbacks are now invoked in process context.

Takashi Sakamoto (5):
firewire: ohci: use workqueue to handle error case of AT
request/response packet queueing
firewire: ohci: use workqueue to handle local AT request/response
packets
firewire: ohci: refactor branches in at_context_transmit()
firewire: core: use spinlock without irqsave for card split_timeout
member
firewire: core: use spinlock without irqsave for card topology_map
member

Quoted 5 lines
 drivers/firewire/core-card.c        |   7 +-
 drivers/firewire/core-transaction.c |  22 ++---
 drivers/firewire/ohci.c             | 119 ++++++++++++++++++++++------
 include/linux/firewire.h            |   5 +-
 4 files changed, 107 insertions(+), 46 deletions(-)

Applied to for-next branch.

Regards

Takashi Sakamoto