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/bitops.h>
13 #include <linux/compiler.h>
14 #include <linux/hardirq.h>
15 #include <linux/spinlock.h>
16 #include <linux/string.h>
17 #include <linux/sched.h> /* because linux/wait.h is broken if CONFIG_SMP=n */
18 #include <linux/wait.h>
21 #include <asm/errno.h>
22 #include <asm/system.h>
25 #include "ieee1394_types.h"
27 #include "ieee1394_core.h"
28 #include "ieee1394_transactions.h"
30 #define PREP_ASYNC_HEAD_ADDRESS(tc) \
32 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
33 | (1 << 8) | (tc << 4); \
34 packet->header[1] = (packet->host->node_id << 16) | (addr >> 32); \
35 packet->header[2] = addr & 0xffffffff
37 #ifndef HPSB_DEBUG_TLABELS
40 DEFINE_SPINLOCK(hpsb_tlabel_lock);
42 static DECLARE_WAIT_QUEUE_HEAD(tlabel_wq);
44 static void fill_async_readquad(struct hpsb_packet *packet, u64 addr)
46 PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ);
47 packet->header_size = 12;
48 packet->data_size = 0;
49 packet->expect_response = 1;
52 static void fill_async_readblock(struct hpsb_packet *packet, u64 addr,
55 PREP_ASYNC_HEAD_ADDRESS(TCODE_READB);
56 packet->header[3] = length << 16;
57 packet->header_size = 16;
58 packet->data_size = 0;
59 packet->expect_response = 1;
62 static void fill_async_writequad(struct hpsb_packet *packet, u64 addr,
65 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEQ);
66 packet->header[3] = data;
67 packet->header_size = 16;
68 packet->data_size = 0;
69 packet->expect_response = 1;
72 static void fill_async_writeblock(struct hpsb_packet *packet, u64 addr,
75 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEB);
76 packet->header[3] = length << 16;
77 packet->header_size = 16;
78 packet->expect_response = 1;
79 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
82 static void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode,
85 PREP_ASYNC_HEAD_ADDRESS(TCODE_LOCK_REQUEST);
86 packet->header[3] = (length << 16) | extcode;
87 packet->header_size = 16;
88 packet->data_size = length;
89 packet->expect_response = 1;
92 static void fill_iso_packet(struct hpsb_packet *packet, int length, int channel,
95 packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
96 | (TCODE_ISO_DATA << 4) | sync;
98 packet->header_size = 4;
99 packet->data_size = length;
100 packet->type = hpsb_iso;
101 packet->tcode = TCODE_ISO_DATA;
104 static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data)
106 packet->header[0] = data;
107 packet->header[1] = ~data;
108 packet->header_size = 8;
109 packet->data_size = 0;
110 packet->expect_response = 0;
111 packet->type = hpsb_raw; /* No CRC added */
112 packet->speed_code = IEEE1394_SPEED_100; /* Force speed to be 100Mbps */
115 static void fill_async_stream_packet(struct hpsb_packet *packet, int length,
116 int channel, int tag, int sync)
118 packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
119 | (TCODE_STREAM_DATA << 4) | sync;
121 packet->header_size = 4;
122 packet->data_size = length;
123 packet->type = hpsb_async;
124 packet->tcode = TCODE_ISO_DATA;
127 /* same as hpsb_get_tlabel, except that it returns immediately */
128 static int hpsb_get_tlabel_atomic(struct hpsb_packet *packet)
130 unsigned long flags, *tp;
132 int tlabel, n = NODEID_TO_NODE(packet->node_id);
134 /* Broadcast transactions are complete once the request has been sent.
135 * Use the same transaction label for all broadcast transactions. */
136 if (unlikely(n == ALL_NODES)) {
140 tp = packet->host->tl_pool[n].map;
141 next = &packet->host->next_tl[n];
143 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
144 tlabel = find_next_zero_bit(tp, 64, *next);
146 tlabel = find_first_zero_bit(tp, 64);
148 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
151 __set_bit(tlabel, tp);
152 *next = (tlabel + 1) & 63;
153 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
155 packet->tlabel = tlabel;
160 * hpsb_get_tlabel - allocate a transaction label
161 * @packet: the packet whose tlabel and tl_pool we set
163 * Every asynchronous transaction on the 1394 bus needs a transaction
164 * label to match the response to the request. This label has to be
165 * different from any other transaction label in an outstanding request to
166 * the same node to make matching possible without ambiguity.
168 * There are 64 different tlabels, so an allocated tlabel has to be freed
169 * with hpsb_free_tlabel() after the transaction is complete (unless it's
170 * reused again for the same target node).
172 * Return value: Zero on success, otherwise non-zero. A non-zero return
173 * generally means there are no available tlabels. If this is called out
174 * of interrupt or atomic context, then it will sleep until can return a
175 * tlabel or a signal is received.
177 int hpsb_get_tlabel(struct hpsb_packet *packet)
179 if (irqs_disabled() || in_atomic())
180 return hpsb_get_tlabel_atomic(packet);
182 /* NB: The macro wait_event_interruptible() is called with a condition
183 * argument with side effect. This is only possible because the side
184 * effect does not occur until the condition became true, and
185 * wait_event_interruptible() won't evaluate the condition again after
187 return wait_event_interruptible(tlabel_wq,
188 !hpsb_get_tlabel_atomic(packet));
192 * hpsb_free_tlabel - free an allocated transaction label
193 * @packet: packet whose tlabel and tl_pool needs to be cleared
195 * Frees the transaction label allocated with hpsb_get_tlabel(). The
196 * tlabel has to be freed after the transaction is complete (i.e. response
197 * was received for a split transaction or packet was sent for a unified
200 * A tlabel must not be freed twice.
202 void hpsb_free_tlabel(struct hpsb_packet *packet)
204 unsigned long flags, *tp;
205 int tlabel, n = NODEID_TO_NODE(packet->node_id);
207 if (unlikely(n == ALL_NODES))
209 tp = packet->host->tl_pool[n].map;
210 tlabel = packet->tlabel;
211 BUG_ON(tlabel > 63 || tlabel < 0);
213 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
214 BUG_ON(!__test_and_clear_bit(tlabel, tp));
215 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
217 wake_up_interruptible(&tlabel_wq);
221 * hpsb_packet_success - Make sense of the ack and reply codes
223 * Make sense of the ack and reply codes and return more convenient error codes:
224 * 0 = success. -%EBUSY = node is busy, try again. -%EAGAIN = error which can
225 * probably resolved by retry. -%EREMOTEIO = node suffers from an internal
226 * error. -%EACCES = this transaction is not allowed on requested address.
227 * -%EINVAL = invalid address at node.
229 int hpsb_packet_success(struct hpsb_packet *packet)
231 switch (packet->ack_code) {
233 switch ((packet->header[1] >> 12) & 0xf) {
236 case RCODE_CONFLICT_ERROR:
238 case RCODE_DATA_ERROR:
240 case RCODE_TYPE_ERROR:
242 case RCODE_ADDRESS_ERROR:
245 HPSB_ERR("received reserved rcode %d from node %d",
246 (packet->header[1] >> 12) & 0xf,
261 if (packet->tcode == TCODE_WRITEQ
262 || packet->tcode == TCODE_WRITEB) {
265 HPSB_ERR("impossible ack_complete from node %d "
266 "(tcode %d)", packet->node_id, packet->tcode);
271 if (packet->tcode == TCODE_WRITEB
272 || packet->tcode == TCODE_LOCK_REQUEST) {
275 HPSB_ERR("impossible ack_data_error from node %d "
276 "(tcode %d)", packet->node_id, packet->tcode);
280 case ACK_ADDRESS_ERROR:
284 case ACK_CONFLICT_ERROR:
286 case ACKX_SEND_ERROR:
289 /* error while sending */
293 HPSB_ERR("got invalid ack %d from node %d (tcode %d)",
294 packet->ack_code, packet->node_id, packet->tcode);
300 struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node,
301 u64 addr, size_t length)
303 struct hpsb_packet *packet;
308 packet = hpsb_alloc_packet(length);
313 packet->node_id = node;
315 if (hpsb_get_tlabel(packet)) {
316 hpsb_free_packet(packet);
321 fill_async_readquad(packet, addr);
323 fill_async_readblock(packet, addr, length);
328 struct hpsb_packet *hpsb_make_writepacket(struct hpsb_host *host, nodeid_t node,
329 u64 addr, quadlet_t * buffer,
332 struct hpsb_packet *packet;
337 packet = hpsb_alloc_packet(length);
341 if (length % 4) { /* zero padding bytes */
342 packet->data[length >> 2] = 0;
345 packet->node_id = node;
347 if (hpsb_get_tlabel(packet)) {
348 hpsb_free_packet(packet);
353 fill_async_writequad(packet, addr, buffer ? *buffer : 0);
355 fill_async_writeblock(packet, addr, length);
357 memcpy(packet->data, buffer, length);
363 struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 * buffer,
364 int length, int channel, int tag,
367 struct hpsb_packet *packet;
372 packet = hpsb_alloc_packet(length);
376 if (length % 4) { /* zero padding bytes */
377 packet->data[length >> 2] = 0;
381 if (hpsb_get_tlabel(packet)) {
382 hpsb_free_packet(packet);
386 fill_async_stream_packet(packet, length, channel, tag, sync);
388 memcpy(packet->data, buffer, length);
393 struct hpsb_packet *hpsb_make_lockpacket(struct hpsb_host *host, nodeid_t node,
394 u64 addr, int extcode,
395 quadlet_t * data, quadlet_t arg)
397 struct hpsb_packet *p;
400 p = hpsb_alloc_packet(8);
406 if (hpsb_get_tlabel(p)) {
412 case EXTCODE_FETCH_ADD:
413 case EXTCODE_LITTLE_ADD:
426 fill_async_lock(p, addr, extcode, length);
431 struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host,
432 nodeid_t node, u64 addr, int extcode,
433 octlet_t * data, octlet_t arg)
435 struct hpsb_packet *p;
438 p = hpsb_alloc_packet(16);
444 if (hpsb_get_tlabel(p)) {
450 case EXTCODE_FETCH_ADD:
451 case EXTCODE_LITTLE_ADD:
454 p->data[0] = *data >> 32;
455 p->data[1] = *data & 0xffffffff;
461 p->data[0] = arg >> 32;
462 p->data[1] = arg & 0xffffffff;
463 p->data[2] = *data >> 32;
464 p->data[3] = *data & 0xffffffff;
468 fill_async_lock(p, addr, extcode, length);
473 struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host, quadlet_t data)
475 struct hpsb_packet *p;
477 p = hpsb_alloc_packet(0);
482 fill_phy_packet(p, data);
487 struct hpsb_packet *hpsb_make_isopacket(struct hpsb_host *host,
488 int length, int channel,
491 struct hpsb_packet *p;
493 p = hpsb_alloc_packet(length);
498 fill_iso_packet(p, length, channel, tag, sync);
500 p->generation = get_hpsb_generation(host);
506 * FIXME - these functions should probably read from / write to user space to
507 * avoid in kernel buffers for user space callers
511 * hpsb_read - generic read function
513 * Recognizes the local node ID and act accordingly. Automatically uses a
514 * quadlet read request if @length == 4 and and a block read request otherwise.
515 * It does not yet support lengths that are not a multiple of 4.
517 * You must explicitly specifiy the @generation for which the node ID is valid,
518 * to avoid sending packets to the wrong nodes when we race with a bus reset.
520 int hpsb_read(struct hpsb_host *host, nodeid_t node, unsigned int generation,
521 u64 addr, quadlet_t * buffer, size_t length)
523 struct hpsb_packet *packet;
529 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
531 packet = hpsb_make_readpacket(host, node, addr, length);
537 packet->generation = generation;
538 retval = hpsb_send_packet_and_wait(packet);
542 retval = hpsb_packet_success(packet);
546 *buffer = packet->header[3];
548 memcpy(buffer, packet->data, length);
553 hpsb_free_tlabel(packet);
554 hpsb_free_packet(packet);
560 * hpsb_write - generic write function
562 * Recognizes the local node ID and act accordingly. Automatically uses a
563 * quadlet write request if @length == 4 and and a block write request
564 * otherwise. It does not yet support lengths that are not a multiple of 4.
566 * You must explicitly specifiy the @generation for which the node ID is valid,
567 * to avoid sending packets to the wrong nodes when we race with a bus reset.
569 int hpsb_write(struct hpsb_host *host, nodeid_t node, unsigned int generation,
570 u64 addr, quadlet_t * buffer, size_t length)
572 struct hpsb_packet *packet;
578 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
580 packet = hpsb_make_writepacket(host, node, addr, buffer, length);
585 packet->generation = generation;
586 retval = hpsb_send_packet_and_wait(packet);
588 goto hpsb_write_fail;
590 retval = hpsb_packet_success(packet);
593 hpsb_free_tlabel(packet);
594 hpsb_free_packet(packet);
601 int hpsb_lock(struct hpsb_host *host, nodeid_t node, unsigned int generation,
602 u64 addr, int extcode, quadlet_t * data, quadlet_t arg)
604 struct hpsb_packet *packet;
607 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
609 packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg);
613 packet->generation = generation;
614 retval = hpsb_send_packet_and_wait(packet);
618 retval = hpsb_packet_success(packet);
621 *data = packet->data[0];
625 hpsb_free_tlabel(packet);
626 hpsb_free_packet(packet);
631 int hpsb_send_gasp(struct hpsb_host *host, int channel, unsigned int generation,
632 quadlet_t * buffer, size_t length, u32 specifier_id,
633 unsigned int version)
635 struct hpsb_packet *packet;
637 u16 specifier_id_hi = (specifier_id & 0x00ffff00) >> 8;
638 u8 specifier_id_lo = specifier_id & 0xff;
640 HPSB_VERBOSE("Send GASP: channel = %d, length = %Zd", channel, length);
644 packet = hpsb_make_streampacket(host, NULL, length, channel, 3, 0);
648 packet->data[0] = cpu_to_be32((host->node_id << 16) | specifier_id_hi);
650 cpu_to_be32((specifier_id_lo << 24) | (version & 0x00ffffff));
652 memcpy(&(packet->data[2]), buffer, length - 8);
654 packet->generation = generation;
656 packet->no_waiter = 1;
658 retval = hpsb_send_packet(packet);
660 hpsb_free_packet(packet);