📄 fw-transaction.c
字号:
/* * Core IEEE1394 transaction logic * * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net> * * 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, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#include <linux/kernel.h>#include <linux/module.h>#include <linux/init.h>#include <linux/interrupt.h>#include <linux/pci.h>#include <linux/delay.h>#include <linux/poll.h>#include <linux/list.h>#include <linux/kthread.h>#include <asm/uaccess.h>#include <asm/semaphore.h>#include "fw-transaction.h"#include "fw-topology.h"#include "fw-device.h"#define HEADER_PRI(pri) ((pri) << 0)#define HEADER_TCODE(tcode) ((tcode) << 4)#define HEADER_RETRY(retry) ((retry) << 8)#define HEADER_TLABEL(tlabel) ((tlabel) << 10)#define HEADER_DESTINATION(destination) ((destination) << 16)#define HEADER_SOURCE(source) ((source) << 16)#define HEADER_RCODE(rcode) ((rcode) << 12)#define HEADER_OFFSET_HIGH(offset_high) ((offset_high) << 0)#define HEADER_DATA_LENGTH(length) ((length) << 16)#define HEADER_EXTENDED_TCODE(tcode) ((tcode) << 0)#define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)#define HEADER_GET_TLABEL(q) (((q) >> 10) & 0x3f)#define HEADER_GET_RCODE(q) (((q) >> 12) & 0x0f)#define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)#define HEADER_GET_SOURCE(q) (((q) >> 16) & 0xffff)#define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)#define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)#define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)#define PHY_CONFIG_GAP_COUNT(gap_count) (((gap_count) << 16) | (1 << 22))#define PHY_CONFIG_ROOT_ID(node_id) ((((node_id) & 0x3f) << 24) | (1 << 23))#define PHY_IDENTIFIER(id) ((id) << 30)static intclose_transaction(struct fw_transaction *transaction, struct fw_card *card, int rcode, u32 *payload, size_t length){ struct fw_transaction *t; unsigned long flags; spin_lock_irqsave(&card->lock, flags); list_for_each_entry(t, &card->transaction_list, link) { if (t == transaction) { list_del(&t->link); card->tlabel_mask &= ~(1 << t->tlabel); break; } } spin_unlock_irqrestore(&card->lock, flags); if (&t->link != &card->transaction_list) { t->callback(card, rcode, payload, length, t->callback_data); return 0; } return -ENOENT;}/* * Only valid for transactions that are potentially pending (ie have * been sent). */intfw_cancel_transaction(struct fw_card *card, struct fw_transaction *transaction){ /* * 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. */ return close_transaction(transaction, card, RCODE_CANCELLED, NULL, 0);}EXPORT_SYMBOL(fw_cancel_transaction);static voidtransmit_complete_callback(struct fw_packet *packet, struct fw_card *card, int status){ struct fw_transaction *t = container_of(packet, struct fw_transaction, packet); switch (status) { case ACK_COMPLETE: close_transaction(t, card, RCODE_COMPLETE, NULL, 0); break; case ACK_PENDING: t->timestamp = packet->timestamp; break; case ACK_BUSY_X: case ACK_BUSY_A: case ACK_BUSY_B: close_transaction(t, card, RCODE_BUSY, NULL, 0); break; case ACK_DATA_ERROR: close_transaction(t, card, RCODE_DATA_ERROR, NULL, 0); break; case ACK_TYPE_ERROR: close_transaction(t, card, RCODE_TYPE_ERROR, NULL, 0); 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, NULL, 0); break; }}static voidfw_fill_request(struct fw_packet *packet, int tcode, int tlabel, int node_id, int source_id, int generation, int speed, unsigned long long offset, void *payload, size_t length){ int ext_tcode; if (tcode > 0x10) { ext_tcode = tcode - 0x10; tcode = TCODE_LOCK_REQUEST; } else ext_tcode = 0; packet->header[0] = HEADER_RETRY(RETRY_X) | HEADER_TLABEL(tlabel) | HEADER_TCODE(tcode) | HEADER_DESTINATION(node_id); packet->header[1] = HEADER_OFFSET_HIGH(offset >> 32) | HEADER_SOURCE(source_id); packet->header[2] = offset; switch (tcode) { case TCODE_WRITE_QUADLET_REQUEST: packet->header[3] = *(u32 *)payload; packet->header_length = 16; packet->payload_length = 0; break; case TCODE_LOCK_REQUEST: case TCODE_WRITE_BLOCK_REQUEST: packet->header[3] = HEADER_DATA_LENGTH(length) | HEADER_EXTENDED_TCODE(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: packet->header[3] = HEADER_DATA_LENGTH(length) | HEADER_EXTENDED_TCODE(ext_tcode); packet->header_length = 16; packet->payload_length = 0; break; } packet->speed = speed; packet->generation = generation; packet->ack = 0;}/** * This function provides low-level access to the IEEE1394 transaction * logic. Most C programs would use either fw_read(), fw_write() or * fw_lock() instead - those function are convenience wrappers for * this function. The fw_send_request() function is primarily * provided as a flexible, one-stop entry point for languages bindings * and protocol bindings. * * FIXME: Document this function further, in particular the possible * values for rcode in the callback. In short, we map ACK_COMPLETE to * RCODE_COMPLETE, internal errors set errno and set rcode to * RCODE_SEND_ERROR (which is out of range for standard ieee1394 * rcodes). All other rcodes are forwarded unchanged. For all * errors, payload is NULL, length is 0. * * Can not expect the callback to be called before the function * returns, though this does happen in some cases (ACK_COMPLETE and * errors). * * The payload is only used for write requests and must not be freed * until the callback has been called. * * @param card the card from which to send the request * @param tcode the tcode for this transaction. Do not use * TCODE_LOCK_REQUEST directly, instead use TCODE_LOCK_MASK_SWAP * etc. to specify tcode and ext_tcode. * @param node_id the destination node ID (bus ID and PHY ID concatenated) * @param generation the generation for which node_id is valid * @param speed the speed to use for sending the request * @param offset the 48 bit offset on the destination node * @param payload the data payload for the request subaction * @param length the length in bytes of the data to read * @param callback function to be called when the transaction is completed * @param callback_data pointer to arbitrary data, which will be * passed to the callback */voidfw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode, int node_id, int generation, int speed, unsigned long long offset, void *payload, size_t length, fw_transaction_callback_t callback, void *callback_data){ unsigned long flags; int tlabel, source; /* * Bump the flush timer up 100ms first of all so we * don't race with a flush timer callback. */ mod_timer(&card->flush_timer, jiffies + DIV_ROUND_UP(HZ, 10)); /* * Allocate tlabel from the bitmap and put the transaction on * the list while holding the card spinlock. */ spin_lock_irqsave(&card->lock, flags); source = card->node_id; tlabel = card->current_tlabel; if (card->tlabel_mask & (1 << tlabel)) { spin_unlock_irqrestore(&card->lock, flags); callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data); return; } card->current_tlabel = (card->current_tlabel + 1) & 0x1f; card->tlabel_mask |= (1 << tlabel); list_add_tail(&t->link, &card->transaction_list); spin_unlock_irqrestore(&card->lock, flags); /* Initialize rest of transaction, fill out packet and send it. */ t->node_id = node_id; t->tlabel = tlabel; t->callback = callback; t->callback_data = callback_data; fw_fill_request(&t->packet, tcode, t->tlabel, node_id, source, generation, speed, offset, payload, length); t->packet.callback = transmit_complete_callback; card->driver->send_request(card, &t->packet);}EXPORT_SYMBOL(fw_send_request);static voidtransmit_phy_packet_callback(struct fw_packet *packet, struct fw_card *card, int status){ kfree(packet);}static void send_phy_packet(struct fw_card *card, u32 data, int generation){ struct fw_packet *packet; packet = kzalloc(sizeof(*packet), GFP_ATOMIC); if (packet == NULL) return; packet->header[0] = data; packet->header[1] = ~data; packet->header_length = 8; packet->payload_length = 0; packet->speed = SCODE_100; packet->generation = generation; packet->callback = transmit_phy_packet_callback; card->driver->send_request(card, packet);}void fw_send_phy_config(struct fw_card *card, int node_id, int generation, int gap_count){ u32 q; q = PHY_IDENTIFIER(PHY_PACKET_CONFIG) | PHY_CONFIG_ROOT_ID(node_id) | PHY_CONFIG_GAP_COUNT(gap_count); send_phy_packet(card, q, generation);}void fw_flush_transactions(struct fw_card *card){ struct fw_transaction *t, *next; struct list_head list; unsigned long flags; INIT_LIST_HEAD(&list); spin_lock_irqsave(&card->lock, flags); list_splice_init(&card->transaction_list, &list); card->tlabel_mask = 0; spin_unlock_irqrestore(&card->lock, flags); list_for_each_entry_safe(t, next, &list, link) { card->driver->cancel_packet(card, &t->packet); /* * At this point cancel_packet will never call the * transaction callback, since we just took all the * transactions out of the list. So do it here. */ t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data); }}static struct fw_address_handler *lookup_overlapping_address_handler(struct list_head *list, unsigned long long offset, size_t length){ struct fw_address_handler *handler; list_for_each_entry(handler, list, link) { if (handler->offset < offset + length && offset < handler->offset + handler->length) return handler; } return NULL;}static struct fw_address_handler *lookup_enclosing_address_handler(struct list_head *list, unsigned long long offset, size_t length){ struct fw_address_handler *handler; list_for_each_entry(handler, list, link) { if (handler->offset <= offset && offset + length <= handler->offset + handler->length) return handler; } return NULL;}static DEFINE_SPINLOCK(address_handler_lock);static LIST_HEAD(address_handler_list);const struct fw_address_region fw_low_memory_region = { .start = 0x000000000000ULL, .end = 0x000100000000ULL, };const struct fw_address_region fw_high_memory_region = { .start = 0x000100000000ULL, .end = 0xffffe0000000ULL, };const struct fw_address_region fw_private_region = { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, };const struct fw_address_region fw_csr_region = { .start = 0xfffff0000000ULL, .end = 0xfffff0000800ULL, };const struct fw_address_region fw_unit_space_region = { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };EXPORT_SYMBOL(fw_low_memory_region);EXPORT_SYMBOL(fw_high_memory_region);EXPORT_SYMBOL(fw_private_region);EXPORT_SYMBOL(fw_csr_region);EXPORT_SYMBOL(fw_unit_space_region);/** * Allocate a range of addresses in the node space of the OHCI * controller. When a request is received that falls within the * specified address range, the specified callback is invoked. The * parameters passed to the callback give the details of the * particular request. * * Return value: 0 on success, non-zero otherwise. * The start offset of the handler's address region is determined by * fw_core_add_address_handler() and is returned in handler->offset. * The offset is quadlet-aligned. */intfw_core_add_address_handler(struct fw_address_handler *handler, const struct fw_address_region *region){ struct fw_address_handler *other; unsigned long flags; int ret = -EBUSY; spin_lock_irqsave(&address_handler_lock, flags); handler->offset = roundup(region->start, 4); while (handler->offset + handler->length <= region->end) { other = lookup_overlapping_address_handler(&address_handler_list, handler->offset, handler->length); if (other != NULL) { handler->offset = roundup(other->offset + other->length, 4); } else { list_add_tail(&handler->link, &address_handler_list); ret = 0; break; } } spin_unlock_irqrestore(&address_handler_lock, flags); return ret;}EXPORT_SYMBOL(fw_core_add_address_handler);/** * Deallocate a range of addresses allocated with fw_allocate. This * will call the associated callback one last time with a the special * tcode TCODE_DEALLOCATE, to let the client destroy the registered * callback data. For convenience, the callback parameters offset and * length are set to the start and the length respectively for the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -