23 #include <linux/device.h>
24 #include <linux/errno.h>
31 #include <linux/kernel.h>
32 #include <linux/list.h>
33 #include <linux/module.h>
35 #include <linux/slab.h>
37 #include <linux/string.h>
39 #include <linux/types.h>
42 #include <asm/byteorder.h>
46 #define HEADER_PRI(pri) ((pri) << 0)
47 #define HEADER_TCODE(tcode) ((tcode) << 4)
48 #define HEADER_RETRY(retry) ((retry) << 8)
49 #define HEADER_TLABEL(tlabel) ((tlabel) << 10)
50 #define HEADER_DESTINATION(destination) ((destination) << 16)
51 #define HEADER_SOURCE(source) ((source) << 16)
52 #define HEADER_RCODE(rcode) ((rcode) << 12)
53 #define HEADER_OFFSET_HIGH(offset_high) ((offset_high) << 0)
54 #define HEADER_DATA_LENGTH(length) ((length) << 16)
55 #define HEADER_EXTENDED_TCODE(tcode) ((tcode) << 0)
57 #define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
58 #define HEADER_GET_TLABEL(q) (((q) >> 10) & 0x3f)
59 #define HEADER_GET_RCODE(q) (((q) >> 12) & 0x0f)
60 #define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
61 #define HEADER_GET_SOURCE(q) (((q) >> 16) & 0xffff)
62 #define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
63 #define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
64 #define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
66 #define HEADER_DESTINATION_IS_BROADCAST(q) \
67 (((q) & HEADER_DESTINATION(0x3f)) == HEADER_DESTINATION(0x3f))
69 #define PHY_PACKET_CONFIG 0x0
70 #define PHY_PACKET_LINK_ON 0x1
71 #define PHY_PACKET_SELF_ID 0x2
73 #define PHY_CONFIG_GAP_COUNT(gap_count) (((gap_count) << 16) | (1 << 22))
74 #define PHY_CONFIG_ROOT_ID(node_id) ((((node_id) & 0x3f) << 24) | (1 << 23))
75 #define PHY_IDENTIFIER(id) ((id) << 30)
94 if (t == transaction) {
95 if (!try_cancel_split_timeout(t)) {
96 spin_unlock_irqrestore(&card->
lock, flags);
99 list_del_init(&t->
link);
104 spin_unlock_irqrestore(&card->
lock, flags);
128 if (card->
driver->cancel_packet(card, &transaction->
packet) == 0)
140 static void split_transaction_timeout_callback(
unsigned long data)
147 if (list_empty(&t->
link)) {
148 spin_unlock_irqrestore(&card->
lock, flags);
153 spin_unlock_irqrestore(&card->
lock, flags);
158 static void start_split_transaction_timeout(
struct fw_transaction *t,
166 spin_unlock_irqrestore(&card->
lock, flags);
174 spin_unlock_irqrestore(&card->
lock, flags);
188 start_split_transaction_timeout(t, card);
206 close_transaction(t, card, status);
211 static void fw_fill_request(
struct fw_packet *packet,
int tcode,
int tlabel,
212 int destination_id,
int source_id,
int generation,
int speed,
230 ext_tcode = tcode & ~0x10;
276 WARN(1,
"wrong tcode %d\n", tcode);
279 packet->
speed = speed;
285 static int allocate_tlabel(
struct fw_card *card)
291 tlabel = (tlabel + 1) & 0x3f;
351 int destination_id,
int generation,
int speed,
352 unsigned long long offset,
void *payload,
size_t length,
365 tlabel = allocate_tlabel(card);
367 spin_unlock_irqrestore(&card->
lock, flags);
377 split_transaction_timeout_callback, (
unsigned long)t);
381 fw_fill_request(&t->packet, tcode, t->tlabel,
382 destination_id, card->
node_id, generation,
383 speed, offset, payload, length);
384 t->packet.callback = transmit_complete_callback;
388 spin_unlock_irqrestore(&card->
lock, flags);
390 card->
driver->send_request(card, &t->packet);
400 static void transaction_callback(
struct fw_card *card,
int rcode,
401 void *payload,
size_t length,
void *data)
420 int generation,
int speed,
unsigned long long offset,
421 void *payload,
size_t length)
427 init_completion(&d.
done);
430 offset, payload, length, transaction_callback, &d);
441 static void transmit_phy_packet_callback(
struct fw_packet *packet,
442 struct fw_card *card,
int status)
447 static struct fw_packet phy_config_packet = {
452 .callback = transmit_phy_packet_callback,
456 int node_id,
int generation,
int gap_count)
465 gap_count = card->
driver->read_phy_reg(card, 1);
478 phy_config_packet.
header[2] = ~data;
482 card->
driver->send_request(card, &phy_config_packet);
489 struct list_head *
list,
unsigned long long offset,
size_t length)
493 list_for_each_entry_rcu(handler, list,
link) {
494 if (handler->
offset < offset + length &&
503 unsigned long long offset,
size_t length)
505 return handler->
offset <= offset &&
510 struct list_head *list,
unsigned long long offset,
size_t length)
514 list_for_each_entry_rcu(handler, list,
link) {
515 if (is_enclosing_handler(handler, offset, length))
526 { .start = 0x000100000000ULL, .end = 0xffffe0000000ULL, };
530 { .start = 0x000000000000ULL, .end = 0x000100000000ULL, };
534 { .
start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL, };
539 { .
start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };
542 static bool is_in_fcp_region(
u64 offset,
size_t length)
573 if (region->
start & 0xffff000000000003ULL ||
575 region->
end > 0x0001000000000000ULL ||
580 spin_lock(&address_handler_list_lock);
587 other = lookup_overlapping_address_handler
588 (&address_handler_list,
593 list_add_tail_rcu(&handler->
link, &address_handler_list);
599 spin_unlock(&address_handler_list_lock);
615 spin_lock(&address_handler_list_lock);
616 list_del_rcu(&handler->
link);
617 spin_unlock(&address_handler_list_lock);
630 static void free_response_callback(
struct fw_packet *packet,
631 struct fw_card *card,
int status)
665 return data_length / 2;
669 WARN(1,
"wrong tcode %d\n", tcode);
675 int rcode,
void *payload,
size_t length)
677 int tcode, tlabel, extended_tcode,
source, destination;
725 WARN(1,
"wrong tcode %d\n", tcode);
732 static u32 compute_split_timeout_timestamp(
struct fw_card *card,
733 u32 request_timestamp)
739 cycles += request_timestamp & 0x1fff;
741 timestamp = request_timestamp & ~0x1fff;
742 timestamp += (cycles / 8000) << 13;
743 timestamp |= cycles % 8000;
756 switch (request_tcode) {
779 fw_notice(card,
"ERROR - corrupt request received - %08x %08x %08x\n",
790 compute_split_timeout_timestamp(card, p->
timestamp);
793 request->
response.callback = free_response_callback;
807 if (
WARN_ONCE(!request,
"invalid for FCP address handlers"))
819 rcode, request->
data,
838 static void handle_exclusive_region_request(
struct fw_card *card,
841 unsigned long long offset)
844 int tcode, destination,
source;
853 handler = lookup_enclosing_address_handler(&address_handler_list,
857 tcode, destination, source,
867 static void handle_fcp_region_request(
struct fw_card *card,
870 unsigned long long offset)
873 int tcode, destination,
source;
877 request->
length > 0x200) {
895 list_for_each_entry_rcu(handler, &address_handler_list,
link) {
896 if (is_enclosing_handler(handler, offset, request->
length))
912 unsigned long long offset;
922 request = allocate_request(card, p);
923 if (request ==
NULL) {
931 if (!is_in_fcp_region(offset, request->
length))
932 handle_exclusive_region_request(card, p, request, offset);
934 handle_fcp_region_request(card, p, request, offset);
955 if (!try_cancel_split_timeout(t)) {
956 spin_unlock_irqrestore(&card->
lock, flags);
959 list_del_init(&t->
link);
964 spin_unlock_irqrestore(&card->
lock, flags);
968 fw_notice(card,
"unsolicited response (source %x, tlabel %x)\n",
1018 static const char *
const names[] = {
1031 if ((
unsigned int)rcode <
ARRAY_SIZE(names) && names[rcode])
1032 return names[rcode];
1042 static void handle_topology_map(
struct fw_card *card,
struct fw_request *request,
1043 int tcode,
int destination,
int source,
int generation,
1044 unsigned long long offset,
void *payload,
size_t length,
1045 void *callback_data)
1054 if ((offset & 3) > 0 || (length & 3) > 0) {
1059 start = (offset - topology_map_region.
start) / 4;
1067 .address_callback = handle_topology_map,
1074 static void update_split_timeout(
struct fw_card *card)
1076 unsigned int cycles;
1081 cycles =
clamp(cycles, 800
u, 3
u * 8000
u);
1088 int tcode,
int destination,
int source,
int generation,
1089 unsigned long long offset,
void *payload,
size_t length,
1090 void *callback_data)
1095 unsigned long flags;
1139 update_split_timeout(card);
1140 spin_unlock_irqrestore(&card->
lock, flags);
1153 update_split_timeout(card);
1154 spin_unlock_irqrestore(&card->
lock, flags);
1204 .address_callback = handle_registers,
1208 int tcode,
int destination,
int source,
int generation,
1209 unsigned long long offset,
void *payload,
size_t length,
1210 void *callback_data)
1220 .length = 0x000100000000ULL,
1221 .address_callback = handle_low_memory,
1228 static const u32 vendor_textual_descriptor[] = {
1239 static const u32 model_textual_descriptor[] = {
1248 .length =
ARRAY_SIZE(vendor_textual_descriptor),
1249 .immediate = 0x03d00d1e,
1251 .data = vendor_textual_descriptor,
1255 .length =
ARRAY_SIZE(model_textual_descriptor),
1256 .immediate = 0x17000001,
1258 .data = model_textual_descriptor,
1261 static int __init fw_core_init(
void)
1292 static void __exit fw_core_cleanup(
void)