6 * Copyright (C) 1999 Andreas E. Bombe
8 * This code is licensed under the GPL. See the file COPYING in the root
9 * directory of the kernel sources for details.
12 #include <linux/sched.h>
13 #include <linux/bitops.h>
14 #include <asm/errno.h>
15 #include <linux/interrupt.h>
18 #include "ieee1394_types.h"
20 #include "ieee1394_core.h"
21 #include "highlevel.h"
25 #define PREP_ASYNC_HEAD_ADDRESS(tc) \
27 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
28 | (1 << 8) | (tc << 4); \
29 packet->header[1] = (packet->host->node_id << 16) | (addr >> 32); \
30 packet->header[2] = addr & 0xffffffff
33 static void fill_async_readquad(struct hpsb_packet *packet, u64 addr)
35 PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ);
36 packet->header_size = 12;
37 packet->data_size = 0;
38 packet->expect_response = 1;
41 static void fill_async_readblock(struct hpsb_packet *packet, u64 addr, int length)
43 PREP_ASYNC_HEAD_ADDRESS(TCODE_READB);
44 packet->header[3] = length << 16;
45 packet->header_size = 16;
46 packet->data_size = 0;
47 packet->expect_response = 1;
50 static void fill_async_writequad(struct hpsb_packet *packet, u64 addr, quadlet_t data)
52 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEQ);
53 packet->header[3] = data;
54 packet->header_size = 16;
55 packet->data_size = 0;
56 packet->expect_response = 1;
59 static void fill_async_writeblock(struct hpsb_packet *packet, u64 addr, int length)
61 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEB);
62 packet->header[3] = length << 16;
63 packet->header_size = 16;
64 packet->expect_response = 1;
65 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
68 static void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode,
71 PREP_ASYNC_HEAD_ADDRESS(TCODE_LOCK_REQUEST);
72 packet->header[3] = (length << 16) | extcode;
73 packet->header_size = 16;
74 packet->data_size = length;
75 packet->expect_response = 1;
78 static void fill_iso_packet(struct hpsb_packet *packet, int length, int channel,
81 packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
82 | (TCODE_ISO_DATA << 4) | sync;
84 packet->header_size = 4;
85 packet->data_size = length;
86 packet->type = hpsb_iso;
87 packet->tcode = TCODE_ISO_DATA;
90 static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data)
92 packet->header[0] = data;
93 packet->header[1] = ~data;
94 packet->header_size = 8;
95 packet->data_size = 0;
96 packet->expect_response = 0;
97 packet->type = hpsb_raw; /* No CRC added */
98 packet->speed_code = IEEE1394_SPEED_100; /* Force speed to be 100Mbps */
101 static void fill_async_stream_packet(struct hpsb_packet *packet, int length,
102 int channel, int tag, int sync)
104 packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
105 | (TCODE_STREAM_DATA << 4) | sync;
107 packet->header_size = 4;
108 packet->data_size = length;
109 packet->type = hpsb_async;
110 packet->tcode = TCODE_ISO_DATA;
114 * hpsb_get_tlabel - allocate a transaction label
115 * @packet: the packet who's tlabel/tpool we set
117 * Every asynchronous transaction on the 1394 bus needs a transaction
118 * label to match the response to the request. This label has to be
119 * different from any other transaction label in an outstanding request to
120 * the same node to make matching possible without ambiguity.
122 * There are 64 different tlabels, so an allocated tlabel has to be freed
123 * with hpsb_free_tlabel() after the transaction is complete (unless it's
124 * reused again for the same target node).
126 * Return value: Zero on success, otherwise non-zero. A non-zero return
127 * generally means there are no available tlabels. If this is called out
128 * of interrupt or atomic context, then it will sleep until can return a
131 int hpsb_get_tlabel(struct hpsb_packet *packet)
134 struct hpsb_tlabel_pool *tp;
136 tp = &packet->host->tpool[packet->node_id & NODE_MASK];
138 if (in_interrupt()) {
139 if (down_trylock(&tp->count))
145 spin_lock_irqsave(&tp->lock, flags);
147 packet->tlabel = find_next_zero_bit(tp->pool, 64, tp->next);
148 if (packet->tlabel > 63)
149 packet->tlabel = find_first_zero_bit(tp->pool, 64);
150 tp->next = (packet->tlabel + 1) % 64;
151 /* Should _never_ happen */
152 BUG_ON(test_and_set_bit(packet->tlabel, tp->pool));
154 spin_unlock_irqrestore(&tp->lock, flags);
160 * hpsb_free_tlabel - free an allocated transaction label
161 * @packet: packet whos tlabel/tpool needs to be cleared
163 * Frees the transaction label allocated with hpsb_get_tlabel(). The
164 * tlabel has to be freed after the transaction is complete (i.e. response
165 * was received for a split transaction or packet was sent for a unified
168 * A tlabel must not be freed twice.
170 void hpsb_free_tlabel(struct hpsb_packet *packet)
173 struct hpsb_tlabel_pool *tp;
175 tp = &packet->host->tpool[packet->node_id & NODE_MASK];
177 BUG_ON(packet->tlabel > 63 || packet->tlabel < 0);
179 spin_lock_irqsave(&tp->lock, flags);
180 BUG_ON(!test_and_clear_bit(packet->tlabel, tp->pool));
181 spin_unlock_irqrestore(&tp->lock, flags);
188 int hpsb_packet_success(struct hpsb_packet *packet)
190 switch (packet->ack_code) {
192 switch ((packet->header[1] >> 12) & 0xf) {
195 case RCODE_CONFLICT_ERROR:
197 case RCODE_DATA_ERROR:
199 case RCODE_TYPE_ERROR:
201 case RCODE_ADDRESS_ERROR:
204 HPSB_ERR("received reserved rcode %d from node %d",
205 (packet->header[1] >> 12) & 0xf,
209 HPSB_PANIC("reached unreachable code 1 in %s", __FUNCTION__);
220 if (packet->tcode == TCODE_WRITEQ
221 || packet->tcode == TCODE_WRITEB) {
224 HPSB_ERR("impossible ack_complete from node %d "
225 "(tcode %d)", packet->node_id, packet->tcode);
231 if (packet->tcode == TCODE_WRITEB
232 || packet->tcode == TCODE_LOCK_REQUEST) {
235 HPSB_ERR("impossible ack_data_error from node %d "
236 "(tcode %d)", packet->node_id, packet->tcode);
241 case ACKX_SEND_ERROR:
244 /* error while sending */
248 HPSB_ERR("got invalid ack %d from node %d (tcode %d)",
249 packet->ack_code, packet->node_id, packet->tcode);
253 HPSB_PANIC("reached unreachable code 2 in %s", __FUNCTION__);
256 struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node,
257 u64 addr, size_t length)
259 struct hpsb_packet *packet;
264 packet = alloc_hpsb_packet(length + (length % 4 ? 4 - (length % 4) : 0));
269 packet->node_id = node;
271 if (hpsb_get_tlabel(packet)) {
272 free_hpsb_packet(packet);
277 fill_async_readquad(packet, addr);
279 fill_async_readblock(packet, addr, length);
284 struct hpsb_packet *hpsb_make_writepacket (struct hpsb_host *host, nodeid_t node,
285 u64 addr, quadlet_t *buffer, size_t length)
287 struct hpsb_packet *packet;
292 packet = alloc_hpsb_packet(length + (length % 4 ? 4 - (length % 4) : 0));
296 if (length % 4) { /* zero padding bytes */
297 packet->data[length >> 2] = 0;
300 packet->node_id = node;
302 if (hpsb_get_tlabel(packet)) {
303 free_hpsb_packet(packet);
308 fill_async_writequad(packet, addr, buffer ? *buffer : 0);
310 fill_async_writeblock(packet, addr, length);
312 memcpy(packet->data, buffer, length);
318 struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 *buffer, int length,
319 int channel, int tag, int sync)
321 struct hpsb_packet *packet;
326 packet = alloc_hpsb_packet(length + (length % 4 ? 4 - (length % 4) : 0));
330 if (length % 4) { /* zero padding bytes */
331 packet->data[length >> 2] = 0;
335 if (hpsb_get_tlabel(packet)) {
336 free_hpsb_packet(packet);
340 fill_async_stream_packet(packet, length, channel, tag, sync);
342 memcpy(packet->data, buffer, length);
347 struct hpsb_packet *hpsb_make_lockpacket(struct hpsb_host *host, nodeid_t node,
348 u64 addr, int extcode, quadlet_t *data,
351 struct hpsb_packet *p;
354 p = alloc_hpsb_packet(8);
359 if (hpsb_get_tlabel(p)) {
365 case EXTCODE_FETCH_ADD:
366 case EXTCODE_LITTLE_ADD:
379 fill_async_lock(p, addr, extcode, length);
384 struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host, nodeid_t node,
385 u64 addr, int extcode, octlet_t *data,
388 struct hpsb_packet *p;
391 p = alloc_hpsb_packet(16);
396 if (hpsb_get_tlabel(p)) {
402 case EXTCODE_FETCH_ADD:
403 case EXTCODE_LITTLE_ADD:
406 p->data[0] = *data >> 32;
407 p->data[1] = *data & 0xffffffff;
413 p->data[0] = arg >> 32;
414 p->data[1] = arg & 0xffffffff;
415 p->data[2] = *data >> 32;
416 p->data[3] = *data & 0xffffffff;
420 fill_async_lock(p, addr, extcode, length);
425 struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host,
428 struct hpsb_packet *p;
430 p = alloc_hpsb_packet(0);
434 fill_phy_packet(p, data);
439 struct hpsb_packet *hpsb_make_isopacket(struct hpsb_host *host,
440 int length, int channel,
443 struct hpsb_packet *p;
445 p = alloc_hpsb_packet(length);
449 fill_iso_packet(p, length, channel, tag, sync);
451 p->generation = get_hpsb_generation(host);
457 * FIXME - these functions should probably read from / write to user space to
458 * avoid in kernel buffers for user space callers
461 int hpsb_read(struct hpsb_host *host, nodeid_t node, unsigned int generation,
462 u64 addr, quadlet_t *buffer, size_t length)
464 struct hpsb_packet *packet;
470 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
472 packet = hpsb_make_readpacket(host, node, addr, length);
478 packet->generation = generation;
479 if (!hpsb_send_packet(packet)) {
484 down(&packet->state_change);
485 down(&packet->state_change);
486 retval = hpsb_packet_success(packet);
490 *buffer = packet->header[3];
492 memcpy(buffer, packet->data, length);
497 hpsb_free_tlabel(packet);
498 free_hpsb_packet(packet);
504 int hpsb_write(struct hpsb_host *host, nodeid_t node, unsigned int generation,
505 u64 addr, quadlet_t *buffer, size_t length)
507 struct hpsb_packet *packet;
513 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
515 packet = hpsb_make_writepacket (host, node, addr, buffer, length);
520 packet->generation = generation;
521 if (!hpsb_send_packet(packet)) {
523 goto hpsb_write_fail;
526 down(&packet->state_change);
527 down(&packet->state_change);
528 retval = hpsb_packet_success(packet);
531 hpsb_free_tlabel(packet);
532 free_hpsb_packet(packet);
538 int hpsb_lock(struct hpsb_host *host, nodeid_t node, unsigned int generation,
539 u64 addr, int extcode, quadlet_t *data, quadlet_t arg)
541 struct hpsb_packet *packet;
544 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
546 packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg);
550 packet->generation = generation;
551 if (!hpsb_send_packet(packet)) {
555 down(&packet->state_change);
556 down(&packet->state_change);
557 retval = hpsb_packet_success(packet);
560 *data = packet->data[0];
564 hpsb_free_tlabel(packet);
565 free_hpsb_packet(packet);
570 int hpsb_lock64(struct hpsb_host *host, nodeid_t node, unsigned int generation,
571 u64 addr, int extcode, octlet_t *data, octlet_t arg)
573 struct hpsb_packet *packet;
576 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
578 packet = hpsb_make_lock64packet(host, node, addr, extcode, data, arg);
582 packet->generation = generation;
583 if (!hpsb_send_packet(packet)) {
585 goto hpsb_lock64_fail;
587 down(&packet->state_change);
588 down(&packet->state_change);
589 retval = hpsb_packet_success(packet);
592 *data = (u64)packet->data[1] << 32 | packet->data[0];
595 hpsb_free_tlabel(packet);
596 free_hpsb_packet(packet);
601 int hpsb_send_gasp(struct hpsb_host *host, int channel, unsigned int generation,
602 quadlet_t *buffer, size_t length, u32 specifier_id,
603 unsigned int version)
605 struct hpsb_packet *packet;
607 u16 specifier_id_hi = (specifier_id & 0x00ffff00) >> 8;
608 u8 specifier_id_lo = specifier_id & 0xff;
610 HPSB_VERBOSE("Send GASP: channel = %d, length = %Zd", channel, length);
614 packet = hpsb_make_streampacket(host, NULL, length, channel, 3, 0);
618 packet->data[0] = cpu_to_be32((host->node_id << 16) | specifier_id_hi);
619 packet->data[1] = cpu_to_be32((specifier_id_lo << 24) | (version & 0x00ffffff));
621 memcpy(&(packet->data[2]), buffer, length - 8);
623 packet->generation = generation;
625 packet->no_waiter = 1;
627 if (!hpsb_send_packet(packet)) {
628 free_hpsb_packet(packet);