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 <linux/smp_lock.h>
15 #include <linux/interrupt.h>
17 #include <asm/errno.h>
20 #include "ieee1394_types.h"
22 #include "ieee1394_core.h"
23 #include "highlevel.h"
25 #include "ieee1394_transactions.h"
27 #define PREP_ASYNC_HEAD_ADDRESS(tc) \
29 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
30 | (1 << 8) | (tc << 4); \
31 packet->header[1] = (packet->host->node_id << 16) | (addr >> 32); \
32 packet->header[2] = addr & 0xffffffff
34 static void fill_async_readquad(struct hpsb_packet *packet, u64 addr)
36 PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ);
37 packet->header_size = 12;
38 packet->data_size = 0;
39 packet->expect_response = 1;
42 static void fill_async_readblock(struct hpsb_packet *packet, u64 addr,
45 PREP_ASYNC_HEAD_ADDRESS(TCODE_READB);
46 packet->header[3] = length << 16;
47 packet->header_size = 16;
48 packet->data_size = 0;
49 packet->expect_response = 1;
52 static void fill_async_writequad(struct hpsb_packet *packet, u64 addr,
55 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEQ);
56 packet->header[3] = data;
57 packet->header_size = 16;
58 packet->data_size = 0;
59 packet->expect_response = 1;
62 static void fill_async_writeblock(struct hpsb_packet *packet, u64 addr,
65 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEB);
66 packet->header[3] = length << 16;
67 packet->header_size = 16;
68 packet->expect_response = 1;
69 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
72 static void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode,
75 PREP_ASYNC_HEAD_ADDRESS(TCODE_LOCK_REQUEST);
76 packet->header[3] = (length << 16) | extcode;
77 packet->header_size = 16;
78 packet->data_size = length;
79 packet->expect_response = 1;
82 static void fill_iso_packet(struct hpsb_packet *packet, int length, int channel,
85 packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
86 | (TCODE_ISO_DATA << 4) | sync;
88 packet->header_size = 4;
89 packet->data_size = length;
90 packet->type = hpsb_iso;
91 packet->tcode = TCODE_ISO_DATA;
94 static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data)
96 packet->header[0] = data;
97 packet->header[1] = ~data;
98 packet->header_size = 8;
99 packet->data_size = 0;
100 packet->expect_response = 0;
101 packet->type = hpsb_raw; /* No CRC added */
102 packet->speed_code = IEEE1394_SPEED_100; /* Force speed to be 100Mbps */
105 static void fill_async_stream_packet(struct hpsb_packet *packet, int length,
106 int channel, int tag, int sync)
108 packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
109 | (TCODE_STREAM_DATA << 4) | sync;
111 packet->header_size = 4;
112 packet->data_size = length;
113 packet->type = hpsb_async;
114 packet->tcode = TCODE_ISO_DATA;
118 * hpsb_get_tlabel - allocate a transaction label
119 * @packet: the packet who's tlabel/tpool we set
121 * Every asynchronous transaction on the 1394 bus needs a transaction
122 * label to match the response to the request. This label has to be
123 * different from any other transaction label in an outstanding request to
124 * the same node to make matching possible without ambiguity.
126 * There are 64 different tlabels, so an allocated tlabel has to be freed
127 * with hpsb_free_tlabel() after the transaction is complete (unless it's
128 * reused again for the same target node).
130 * Return value: Zero on success, otherwise non-zero. A non-zero return
131 * generally means there are no available tlabels. If this is called out
132 * of interrupt or atomic context, then it will sleep until can return a
135 int hpsb_get_tlabel(struct hpsb_packet *packet)
138 struct hpsb_tlabel_pool *tp;
140 tp = &packet->host->tpool[packet->node_id & NODE_MASK];
142 if (irqs_disabled() || in_atomic()) {
143 if (down_trylock(&tp->count))
149 spin_lock_irqsave(&tp->lock, flags);
151 packet->tlabel = find_next_zero_bit(tp->pool, 64, tp->next);
152 if (packet->tlabel > 63)
153 packet->tlabel = find_first_zero_bit(tp->pool, 64);
154 tp->next = (packet->tlabel + 1) % 64;
155 /* Should _never_ happen */
156 BUG_ON(test_and_set_bit(packet->tlabel, tp->pool));
158 spin_unlock_irqrestore(&tp->lock, flags);
164 * hpsb_free_tlabel - free an allocated transaction label
165 * @packet: packet whos tlabel/tpool needs to be cleared
167 * Frees the transaction label allocated with hpsb_get_tlabel(). The
168 * tlabel has to be freed after the transaction is complete (i.e. response
169 * was received for a split transaction or packet was sent for a unified
172 * A tlabel must not be freed twice.
174 void hpsb_free_tlabel(struct hpsb_packet *packet)
177 struct hpsb_tlabel_pool *tp;
179 tp = &packet->host->tpool[packet->node_id & NODE_MASK];
181 BUG_ON(packet->tlabel > 63 || packet->tlabel < 0);
183 spin_lock_irqsave(&tp->lock, flags);
184 BUG_ON(!test_and_clear_bit(packet->tlabel, tp->pool));
185 spin_unlock_irqrestore(&tp->lock, flags);
190 int hpsb_packet_success(struct hpsb_packet *packet)
192 switch (packet->ack_code) {
194 switch ((packet->header[1] >> 12) & 0xf) {
197 case RCODE_CONFLICT_ERROR:
199 case RCODE_DATA_ERROR:
201 case RCODE_TYPE_ERROR:
203 case RCODE_ADDRESS_ERROR:
206 HPSB_ERR("received reserved rcode %d from node %d",
207 (packet->header[1] >> 12) & 0xf,
211 HPSB_PANIC("reached unreachable code 1 in %s", __FUNCTION__);
222 if (packet->tcode == TCODE_WRITEQ
223 || packet->tcode == TCODE_WRITEB) {
226 HPSB_ERR("impossible ack_complete from node %d "
227 "(tcode %d)", packet->node_id, packet->tcode);
232 if (packet->tcode == TCODE_WRITEB
233 || packet->tcode == TCODE_LOCK_REQUEST) {
236 HPSB_ERR("impossible ack_data_error from node %d "
237 "(tcode %d)", packet->node_id, packet->tcode);
241 case ACK_ADDRESS_ERROR:
245 case ACK_CONFLICT_ERROR:
247 case ACKX_SEND_ERROR:
250 /* error while sending */
254 HPSB_ERR("got invalid ack %d from node %d (tcode %d)",
255 packet->ack_code, packet->node_id, packet->tcode);
259 HPSB_PANIC("reached unreachable code 2 in %s", __FUNCTION__);
262 struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node,
263 u64 addr, size_t length)
265 struct hpsb_packet *packet;
270 packet = hpsb_alloc_packet(length);
275 packet->node_id = node;
277 if (hpsb_get_tlabel(packet)) {
278 hpsb_free_packet(packet);
283 fill_async_readquad(packet, addr);
285 fill_async_readblock(packet, addr, length);
290 struct hpsb_packet *hpsb_make_writepacket(struct hpsb_host *host, nodeid_t node,
291 u64 addr, quadlet_t * buffer,
294 struct hpsb_packet *packet;
299 packet = hpsb_alloc_packet(length);
303 if (length % 4) { /* zero padding bytes */
304 packet->data[length >> 2] = 0;
307 packet->node_id = node;
309 if (hpsb_get_tlabel(packet)) {
310 hpsb_free_packet(packet);
315 fill_async_writequad(packet, addr, buffer ? *buffer : 0);
317 fill_async_writeblock(packet, addr, length);
319 memcpy(packet->data, buffer, length);
325 struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 * buffer,
326 int length, int channel, int tag,
329 struct hpsb_packet *packet;
334 packet = hpsb_alloc_packet(length);
338 if (length % 4) { /* zero padding bytes */
339 packet->data[length >> 2] = 0;
343 if (hpsb_get_tlabel(packet)) {
344 hpsb_free_packet(packet);
348 fill_async_stream_packet(packet, length, channel, tag, sync);
350 memcpy(packet->data, buffer, length);
355 struct hpsb_packet *hpsb_make_lockpacket(struct hpsb_host *host, nodeid_t node,
356 u64 addr, int extcode,
357 quadlet_t * data, quadlet_t arg)
359 struct hpsb_packet *p;
362 p = hpsb_alloc_packet(8);
368 if (hpsb_get_tlabel(p)) {
374 case EXTCODE_FETCH_ADD:
375 case EXTCODE_LITTLE_ADD:
388 fill_async_lock(p, addr, extcode, length);
393 struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host,
394 nodeid_t node, u64 addr, int extcode,
395 octlet_t * data, octlet_t arg)
397 struct hpsb_packet *p;
400 p = hpsb_alloc_packet(16);
406 if (hpsb_get_tlabel(p)) {
412 case EXTCODE_FETCH_ADD:
413 case EXTCODE_LITTLE_ADD:
416 p->data[0] = *data >> 32;
417 p->data[1] = *data & 0xffffffff;
423 p->data[0] = arg >> 32;
424 p->data[1] = arg & 0xffffffff;
425 p->data[2] = *data >> 32;
426 p->data[3] = *data & 0xffffffff;
430 fill_async_lock(p, addr, extcode, length);
435 struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host, quadlet_t data)
437 struct hpsb_packet *p;
439 p = hpsb_alloc_packet(0);
444 fill_phy_packet(p, data);
449 struct hpsb_packet *hpsb_make_isopacket(struct hpsb_host *host,
450 int length, int channel,
453 struct hpsb_packet *p;
455 p = hpsb_alloc_packet(length);
460 fill_iso_packet(p, length, channel, tag, sync);
462 p->generation = get_hpsb_generation(host);
468 * FIXME - these functions should probably read from / write to user space to
469 * avoid in kernel buffers for user space callers
472 int hpsb_read(struct hpsb_host *host, nodeid_t node, unsigned int generation,
473 u64 addr, quadlet_t * buffer, size_t length)
475 struct hpsb_packet *packet;
481 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
483 packet = hpsb_make_readpacket(host, node, addr, length);
489 packet->generation = generation;
490 retval = hpsb_send_packet_and_wait(packet);
494 retval = hpsb_packet_success(packet);
498 *buffer = packet->header[3];
500 memcpy(buffer, packet->data, length);
505 hpsb_free_tlabel(packet);
506 hpsb_free_packet(packet);
511 int hpsb_write(struct hpsb_host *host, nodeid_t node, unsigned int generation,
512 u64 addr, quadlet_t * buffer, size_t length)
514 struct hpsb_packet *packet;
520 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
522 packet = hpsb_make_writepacket(host, node, addr, buffer, length);
527 packet->generation = generation;
528 retval = hpsb_send_packet_and_wait(packet);
530 goto hpsb_write_fail;
532 retval = hpsb_packet_success(packet);
535 hpsb_free_tlabel(packet);
536 hpsb_free_packet(packet);
543 int hpsb_lock(struct hpsb_host *host, nodeid_t node, unsigned int generation,
544 u64 addr, int extcode, quadlet_t * data, quadlet_t arg)
546 struct hpsb_packet *packet;
549 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
551 packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg);
555 packet->generation = generation;
556 retval = hpsb_send_packet_and_wait(packet);
560 retval = hpsb_packet_success(packet);
563 *data = packet->data[0];
567 hpsb_free_tlabel(packet);
568 hpsb_free_packet(packet);
573 int hpsb_send_gasp(struct hpsb_host *host, int channel, unsigned int generation,
574 quadlet_t * buffer, size_t length, u32 specifier_id,
575 unsigned int version)
577 struct hpsb_packet *packet;
579 u16 specifier_id_hi = (specifier_id & 0x00ffff00) >> 8;
580 u8 specifier_id_lo = specifier_id & 0xff;
582 HPSB_VERBOSE("Send GASP: channel = %d, length = %Zd", channel, length);
586 packet = hpsb_make_streampacket(host, NULL, length, channel, 3, 0);
590 packet->data[0] = cpu_to_be32((host->node_id << 16) | specifier_id_hi);
592 cpu_to_be32((specifier_id_lo << 24) | (version & 0x00ffffff));
594 memcpy(&(packet->data[2]), buffer, length - 8);
596 packet->generation = generation;
598 packet->no_waiter = 1;
600 retval = hpsb_send_packet(packet);
602 hpsb_free_packet(packet);