1508 lines · 44.6 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* * Core IEEE1394 transaction logic * * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net> */ #include <linux/bug.h>#include <linux/completion.h>#include <linux/device.h>#include <linux/errno.h>#include <linux/firewire.h>#include <linux/firewire-constants.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/jiffies.h>#include <linux/kernel.h>#include <linux/list.h>#include <linux/module.h>#include <linux/rculist.h>#include <linux/slab.h>#include <linux/spinlock.h>#include <linux/string.h>#include <linux/timer.h>#include <linux/types.h>#include <linux/workqueue.h> #include <asm/byteorder.h> #include "core.h"#include "packet-header-definitions.h"#include "phy-packet-definitions.h"#include <trace/events/firewire.h> #define HEADER_DESTINATION_IS_BROADCAST(header) \	((async_header_get_destination(header) & 0x3f) == 0x3f) /* returns 0 if the split timeout handler is already running */static int try_cancel_split_timeout(struct fw_transaction *t){	if (t->is_split_transaction)		return timer_delete(&t->split_timeout_timer);	else		return 1;} // card->transactions.lock must be acquired in advance.static void remove_transaction_entry(struct fw_card *card, struct fw_transaction *entry){	list_del_init(&entry->link);	card->transactions.tlabel_mask &= ~(1ULL << entry->tlabel);} // Must be called without holding card->transactions.lock.void fw_cancel_pending_transactions(struct fw_card *card){	struct fw_transaction *t, *tmp;	LIST_HEAD(pending_list); 	// 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->transactions.lock) {		list_for_each_entry_safe(t, tmp, &card->transactions.list, link) {			if (try_cancel_split_timeout(t))				list_move(&t->link, &pending_list);		}	} 	list_for_each_entry_safe(t, tmp, &pending_list, link) {		list_del(&t->link); 		if (!t->with_tstamp) {			t->callback.without_tstamp(card, RCODE_CANCELLED, NULL, 0,						   t->callback_data);		} else {			t->callback.with_tstamp(card, RCODE_CANCELLED, t->packet.timestamp, 0,						NULL, 0, t->callback_data);		}	}} // card->transactions.lock must be acquired in advance.#define find_and_pop_transaction_entry(card, condition)			\({									\	struct fw_transaction *iter, *t = NULL;				\	list_for_each_entry(iter, &card->transactions.list, link) {	\		if (condition) {					\			t = iter;					\			break;						\		}							\	}								\	if (t && try_cancel_split_timeout(t))				\		remove_transaction_entry(card, t);			\	t;								\}) static int close_transaction(struct fw_transaction *transaction, struct fw_card *card, int rcode,			     u32 response_tstamp){	struct fw_transaction *t; 	// 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->transactions.lock) {		t = find_and_pop_transaction_entry(card, iter == transaction);		if (!t)			return -ENOENT;	} 	if (!t->with_tstamp) {		t->callback.without_tstamp(card, rcode, NULL, 0, t->callback_data);	} else {		t->callback.with_tstamp(card, rcode, t->packet.timestamp, response_tstamp, NULL, 0,					t->callback_data);	} 	return 0;} /* * Only valid for transactions that are potentially pending (ie have * been sent). */int fw_cancel_transaction(struct fw_card *card,			  struct fw_transaction *transaction){	u32 tstamp; 	/*	 * Cancel the packet transmission if it's still queued.  That	 * will call the packet transmission callback which cancels	 * the transaction.	 */ 	if (card->driver->cancel_packet(card, &transaction->packet) == 0)		return 0; 	/*	 * If the request packet has already been sent, we need to see	 * if the transaction is still pending and remove it in that case.	 */ 	if (transaction->packet.ack == 0) {		// The timestamp is reused since it was just read now.		tstamp = transaction->packet.timestamp;	} else {		u32 curr_cycle_time = 0; 		(void)fw_card_read_cycle_time(card, &curr_cycle_time);		tstamp = cycle_time_to_ohci_tstamp(curr_cycle_time);	} 	return close_transaction(transaction, card, RCODE_CANCELLED, tstamp);}EXPORT_SYMBOL(fw_cancel_transaction); static void split_transaction_timeout_callback(struct timer_list *timer){	struct fw_transaction *t = timer_container_of(t, timer, split_timeout_timer);	struct fw_card *card = t->card; 	scoped_guard(spinlock_irqsave, &card->transactions.lock) {		if (list_empty(&t->link))			return;		remove_transaction_entry(card, t);	} 	if (!t->with_tstamp) {		t->callback.without_tstamp(card, RCODE_CANCELLED, NULL, 0, t->callback_data);	} else {		t->callback.with_tstamp(card, RCODE_CANCELLED, t->packet.timestamp,					t->split_timeout_cycle, NULL, 0, t->callback_data);	}} // card->transactions.lock should be acquired in advance for the linked list.static void start_split_transaction_timeout(struct fw_transaction *t, unsigned int delta){	if (list_empty(&t->link) || WARN_ON(t->is_split_transaction))		return; 	t->is_split_transaction = true; 	mod_timer(&t->split_timeout_timer, jiffies + delta);} static u32 compute_split_timeout_timestamp(struct fw_card *card, u32 request_timestamp); static void transmit_complete_callback(struct fw_packet *packet,				       struct fw_card *card, int status){	struct fw_transaction *t =	    container_of(packet, struct fw_transaction, packet); 	trace_async_request_outbound_complete((uintptr_t)t, card->index, packet->generation,					      packet->speed, status, packet->timestamp); 	switch (status) {	case ACK_COMPLETE:		close_transaction(t, card, RCODE_COMPLETE, packet->timestamp);		break;	case ACK_PENDING:	{		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) {			t->split_timeout_cycle =				compute_split_timeout_timestamp(card, packet->timestamp) & 0xffff;			delta = card->split_timeout.jiffies;		} 		// 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->transactions.lock)			start_split_transaction_timeout(t, delta);		break;	}	case ACK_BUSY_X:	case ACK_BUSY_A:	case ACK_BUSY_B:		close_transaction(t, card, RCODE_BUSY, packet->timestamp);		break;	case ACK_DATA_ERROR:		close_transaction(t, card, RCODE_DATA_ERROR, packet->timestamp);		break;	case ACK_TYPE_ERROR:		close_transaction(t, card, RCODE_TYPE_ERROR, packet->timestamp);		break;	default:		/*		 * In this case the ack is really a juju specific		 * rcode, so just forward that to the callback.		 */		close_transaction(t, card, status, packet->timestamp);		break;	}} static void fw_fill_request(struct fw_packet *packet, int tcode, int tlabel,		int destination_id, int source_id, int generation, int speed,		unsigned long long offset, void *payload, size_t length){	int ext_tcode; 	if (tcode == TCODE_STREAM_DATA) {		// The value of destination_id argument should include tag, channel, and sy fields		// as isochronous packet header has.		packet->header[0] = destination_id;		isoc_header_set_data_length(packet->header, length);		isoc_header_set_tcode(packet->header, TCODE_STREAM_DATA);		packet->header_length = 4;		packet->payload = payload;		packet->payload_length = length; 		goto common;	} 	if (tcode > 0x10) {		ext_tcode = tcode & ~0x10;		tcode = TCODE_LOCK_REQUEST;	} else		ext_tcode = 0; 	async_header_set_retry(packet->header, RETRY_X);	async_header_set_tlabel(packet->header, tlabel);	async_header_set_tcode(packet->header, tcode);	async_header_set_destination(packet->header, destination_id);	async_header_set_source(packet->header, source_id);	async_header_set_offset(packet->header, offset); 	switch (tcode) {	case TCODE_WRITE_QUADLET_REQUEST:		async_header_set_quadlet_data(packet->header, *(u32 *)payload);		packet->header_length = 16;		packet->payload_length = 0;		break; 	case TCODE_LOCK_REQUEST:	case TCODE_WRITE_BLOCK_REQUEST:		async_header_set_data_length(packet->header, length);		async_header_set_extended_tcode(packet->header, ext_tcode);		packet->header_length = 16;		packet->payload = payload;		packet->payload_length = length;		break; 	case TCODE_READ_QUADLET_REQUEST:		packet->header_length = 12;		packet->payload_length = 0;		break; 	case TCODE_READ_BLOCK_REQUEST:		async_header_set_data_length(packet->header, length);		async_header_set_extended_tcode(packet->header, ext_tcode);		packet->header_length = 16;		packet->payload_length = 0;		break; 	default:		WARN(1, "wrong tcode %d\n", tcode);	} common:	packet->speed = speed;	packet->generation = generation;	packet->ack = 0;	packet->payload_mapped = false;} static int allocate_tlabel(struct fw_card *card)__must_hold(&card->transactions.lock){	int tlabel; 	lockdep_assert_held(&card->transactions.lock); 	tlabel = card->transactions.current_tlabel;	while (card->transactions.tlabel_mask & (1ULL << tlabel)) {		tlabel = (tlabel + 1) & 0x3f;		if (tlabel == card->transactions.current_tlabel)			return -EBUSY;	} 	card->transactions.current_tlabel = (tlabel + 1) & 0x3f;	card->transactions.tlabel_mask |= 1ULL << tlabel; 	return tlabel;} /** * __fw_send_request() - submit a request packet for transmission to generate callback for response *			 subaction with or without time stamp. * @card:		interface to send the request at * @t:			transaction instance to which the request belongs * @tcode:		transaction code * @destination_id:	destination node ID, consisting of bus_ID and phy_ID * @generation:		bus generation in which request and response are valid * @speed:		transmission speed * @offset:		48bit wide offset into destination's address space * @payload:		data payload for the request subaction * @length:		length of the payload, in bytes * @callback:		union of two functions whether to receive time stamp or not for response *			subaction. * @with_tstamp:	Whether to receive time stamp or not for response subaction. * @callback_data:	data to be passed to the transaction completion callback * * Submit a request packet into the asynchronous request transmission queue. * Can be called from atomic context.  If you prefer a blocking API, use * fw_run_transaction() in a context that can sleep. * * In case of lock requests, specify one of the firewire-core specific %TCODE_ * constants instead of %TCODE_LOCK_REQUEST in @tcode. * * Make sure that the value in @destination_id is not older than the one in * @generation.  Otherwise the request is in danger to be sent to a wrong node. * * In case of asynchronous stream packets i.e. %TCODE_STREAM_DATA, the caller * needs to synthesize @destination_id with fw_stream_packet_destination_id(). * It will contain tag, channel, and sy data instead of a node ID then. * * The payload buffer at @data is going to be DMA-mapped except in case of * @length <= 8 or of local (loopback) requests.  Hence make sure that the * buffer complies with the restrictions of the streaming DMA mapping API. * @payload must not be freed before the @callback is called. * * In case of request types without payload, @data is NULL and @length is 0. * * After the transaction is completed successfully or unsuccessfully, the * @callback will be called.  Among its parameters is the response code which * is either one of the rcodes per IEEE 1394 or, in case of internal errors, * the firewire-core specific %RCODE_SEND_ERROR.  The other firewire-core * specific rcodes (%RCODE_CANCELLED, %RCODE_BUSY, %RCODE_GENERATION, * %RCODE_NO_ACK) denote transaction timeout, busy responder, stale request * generation, or missing ACK respectively. * * Note some timing corner cases:  fw_send_request() may complete much earlier * than when the request packet actually hits the wire.  On the other hand, * transaction completion and hence execution of @callback may happen even * before fw_send_request() returns. */void __fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode,		int destination_id, int generation, int speed, unsigned long long offset,		void *payload, size_t length, union fw_transaction_callback callback,		bool with_tstamp, void *callback_data){	int tlabel; 	/*	 * Allocate tlabel from the bitmap and put the transaction on	 * the list while holding the card spinlock.	 */ 	// 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->transactions.lock)		tlabel = allocate_tlabel(card);	if (tlabel < 0) {		if (!with_tstamp) {			callback.without_tstamp(card, RCODE_SEND_ERROR, NULL, 0, callback_data);		} else {			// Timestamping on behalf of hardware.			u32 curr_cycle_time = 0;			u32 tstamp; 			(void)fw_card_read_cycle_time(card, &curr_cycle_time);			tstamp = cycle_time_to_ohci_tstamp(curr_cycle_time); 			callback.with_tstamp(card, RCODE_SEND_ERROR, tstamp, tstamp, NULL, 0,					     callback_data);		}		return;	} 	t->node_id = destination_id;	t->tlabel = tlabel;	t->card = card;	t->is_split_transaction = false;	timer_setup(&t->split_timeout_timer, split_transaction_timeout_callback, 0);	t->callback = callback;	t->with_tstamp = with_tstamp;	t->callback_data = callback_data;	t->packet.callback = transmit_complete_callback; 	// 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->lock) {		// The node_id field of fw_card can be updated when handling SelfIDComplete.		fw_fill_request(&t->packet, tcode, t->tlabel, destination_id, card->node_id,				generation, speed, offset, payload, length);	} 	// 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->transactions.lock)		list_add_tail(&t->link, &card->transactions.list); 	// Safe with no lock, since the index field of fw_card is immutable once assigned.	trace_async_request_outbound_initiate((uintptr_t)t, card->index, generation, speed,					      t->packet.header, payload,					      tcode_is_read_request(tcode) ? 0 : length / 4); 	card->driver->send_request(card, &t->packet);}EXPORT_SYMBOL_GPL(__fw_send_request); struct transaction_callback_data {	struct completion done;	void *payload;	int rcode;}; static void transaction_callback(struct fw_card *card, int rcode,				 void *payload, size_t length, void *data){	struct transaction_callback_data *d = data; 	if (rcode == RCODE_COMPLETE)		memcpy(d->payload, payload, length);	d->rcode = rcode;	complete(&d->done);} /** * fw_run_transaction() - send request and sleep until transaction is completed * @card:		card interface for this request * @tcode:		transaction code * @destination_id:	destination node ID, consisting of bus_ID and phy_ID * @generation:		bus generation in which request and response are valid * @speed:		transmission speed * @offset:		48bit wide offset into destination's address space * @payload:		data payload for the request subaction * @length:		length of the payload, in bytes * * Returns the RCODE.  See fw_send_request() for parameter documentation. * Unlike fw_send_request(), @data points to the payload of the request or/and * to the payload of the response.  DMA mapping restrictions apply to outbound * request payloads of >= 8 bytes but not to inbound response payloads. */int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,		       int generation, int speed, unsigned long long offset,		       void *payload, size_t length){	struct transaction_callback_data d;	struct fw_transaction t; 	timer_setup_on_stack(&t.split_timeout_timer, NULL, 0);	init_completion(&d.done);	d.payload = payload;	fw_send_request(card, &t, tcode, destination_id, generation, speed,			offset, payload, length, transaction_callback, &d);	wait_for_completion(&d.done);	timer_destroy_on_stack(&t.split_timeout_timer); 	return d.rcode;}EXPORT_SYMBOL(fw_run_transaction); static DEFINE_MUTEX(phy_config_mutex);static DECLARE_COMPLETION(phy_config_done);

Lines 1–500 of 1508.