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/hardirq.h>
14 #include <linux/spinlock.h>
15 #include <linux/sched.h> /* because linux/wait.h is broken if CONFIG_SMP=n */
16 #include <linux/wait.h>
19 #include <asm/errno.h>
22 #include "ieee1394_types.h"
24 #include "ieee1394_core.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 #ifndef HPSB_DEBUG_TLABELS
37 spinlock_t hpsb_tlabel_lock = SPIN_LOCK_UNLOCKED;
39 static DECLARE_WAIT_QUEUE_HEAD(tlabel_wq);
41 static void fill_async_readquad(struct hpsb_packet *packet, u64 addr)
43 PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ);
44 packet->header_size = 12;
45 packet->data_size = 0;
46 packet->expect_response = 1;
49 static void fill_async_readblock(struct hpsb_packet *packet, u64 addr,
52 PREP_ASYNC_HEAD_ADDRESS(TCODE_READB);
53 packet->header[3] = length << 16;
54 packet->header_size = 16;
55 packet->data_size = 0;
56 packet->expect_response = 1;
59 static void fill_async_writequad(struct hpsb_packet *packet, u64 addr,
62 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEQ);
63 packet->header[3] = data;
64 packet->header_size = 16;
65 packet->data_size = 0;
66 packet->expect_response = 1;
69 static void fill_async_writeblock(struct hpsb_packet *packet, u64 addr,
72 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEB);
73 packet->header[3] = length << 16;
74 packet->header_size = 16;
75 packet->expect_response = 1;
76 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
79 static void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode,
82 PREP_ASYNC_HEAD_ADDRESS(TCODE_LOCK_REQUEST);
83 packet->header[3] = (length << 16) | extcode;
84 packet->header_size = 16;
85 packet->data_size = length;
86 packet->expect_response = 1;
89 static void fill_iso_packet(struct hpsb_packet *packet, int length, int channel,
92 packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
93 | (TCODE_ISO_DATA << 4) | sync;
95 packet->header_size = 4;
96 packet->data_size = length;
97 packet->type = hpsb_iso;
98 packet->tcode = TCODE_ISO_DATA;
101 static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data)
103 packet->header[0] = data;
104 packet->header[1] = ~data;
105 packet->header_size = 8;
106 packet->data_size = 0;
107 packet->expect_response = 0;
108 packet->type = hpsb_raw; /* No CRC added */
109 packet->speed_code = IEEE1394_SPEED_100; /* Force speed to be 100Mbps */
112 static void fill_async_stream_packet(struct hpsb_packet *packet, int length,
113 int channel, int tag, int sync)
115 packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
116 | (TCODE_STREAM_DATA << 4) | sync;
118 packet->header_size = 4;
119 packet->data_size = length;
120 packet->type = hpsb_async;
121 packet->tcode = TCODE_ISO_DATA;
124 /* same as hpsb_get_tlabel, except that it returns immediately */
125 static int hpsb_get_tlabel_atomic(struct hpsb_packet *packet)
127 unsigned long flags, *tp;
129 int tlabel, n = NODEID_TO_NODE(packet->node_id);
131 /* Broadcast transactions are complete once the request has been sent.
132 * Use the same transaction label for all broadcast transactions. */
133 if (unlikely(n == ALL_NODES)) {
137 tp = packet->host->tl_pool[n].map;
138 next = &packet->host->next_tl[n];
140 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
141 tlabel = find_next_zero_bit(tp, 64, *next);
143 tlabel = find_first_zero_bit(tp, 64);
145 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
148 __set_bit(tlabel, tp);
149 *next = (tlabel + 1) & 63;
150 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
152 packet->tlabel = tlabel;
157 * hpsb_get_tlabel - allocate a transaction label
158 * @packet: the packet whose tlabel and tl_pool we set
160 * Every asynchronous transaction on the 1394 bus needs a transaction
161 * label to match the response to the request. This label has to be
162 * different from any other transaction label in an outstanding request to
163 * the same node to make matching possible without ambiguity.
165 * There are 64 different tlabels, so an allocated tlabel has to be freed
166 * with hpsb_free_tlabel() after the transaction is complete (unless it's
167 * reused again for the same target node).
169 * Return value: Zero on success, otherwise non-zero. A non-zero return
170 * generally means there are no available tlabels. If this is called out
171 * of interrupt or atomic context, then it will sleep until can return a
172 * tlabel or a signal is received.
174 int hpsb_get_tlabel(struct hpsb_packet *packet)
176 if (irqs_disabled() || in_atomic())
177 return hpsb_get_tlabel_atomic(packet);
179 /* NB: The macro wait_event_interruptible() is called with a condition
180 * argument with side effect. This is only possible because the side
181 * effect does not occur until the condition became true, and
182 * wait_event_interruptible() won't evaluate the condition again after
184 return wait_event_interruptible(tlabel_wq,
185 !hpsb_get_tlabel_atomic(packet));
189 * hpsb_free_tlabel - free an allocated transaction label
190 * @packet: packet whose tlabel and tl_pool needs to be cleared
192 * Frees the transaction label allocated with hpsb_get_tlabel(). The
193 * tlabel has to be freed after the transaction is complete (i.e. response
194 * was received for a split transaction or packet was sent for a unified
197 * A tlabel must not be freed twice.
199 void hpsb_free_tlabel(struct hpsb_packet *packet)
201 unsigned long flags, *tp;
202 int tlabel, n = NODEID_TO_NODE(packet->node_id);
204 if (unlikely(n == ALL_NODES))
206 tp = packet->host->tl_pool[n].map;
207 tlabel = packet->tlabel;
208 BUG_ON(tlabel > 63 || tlabel < 0);
210 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
211 BUG_ON(!__test_and_clear_bit(tlabel, tp));
212 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
214 wake_up_interruptible(&tlabel_wq);
218 * hpsb_packet_success - Make sense of the ack and reply codes
220 * Make sense of the ack and reply codes and return more convenient error codes:
221 * 0 = success. -%EBUSY = node is busy, try again. -%EAGAIN = error which can
222 * probably resolved by retry. -%EREMOTEIO = node suffers from an internal
223 * error. -%EACCES = this transaction is not allowed on requested address.
224 * -%EINVAL = invalid address at node.
226 int hpsb_packet_success(struct hpsb_packet *packet)
228 switch (packet->ack_code) {
230 switch ((packet->header[1] >> 12) & 0xf) {
233 case RCODE_CONFLICT_ERROR:
235 case RCODE_DATA_ERROR:
237 case RCODE_TYPE_ERROR:
239 case RCODE_ADDRESS_ERROR:
242 HPSB_ERR("received reserved rcode %d from node %d",
243 (packet->header[1] >> 12) & 0xf,
258 if (packet->tcode == TCODE_WRITEQ
259 || packet->tcode == TCODE_WRITEB) {
262 HPSB_ERR("impossible ack_complete from node %d "
263 "(tcode %d)", packet->node_id, packet->tcode);
268 if (packet->tcode == TCODE_WRITEB
269 || packet->tcode == TCODE_LOCK_REQUEST) {
272 HPSB_ERR("impossible ack_data_error from node %d "
273 "(tcode %d)", packet->node_id, packet->tcode);
277 case ACK_ADDRESS_ERROR:
281 case ACK_CONFLICT_ERROR:
283 case ACKX_SEND_ERROR:
286 /* error while sending */
290 HPSB_ERR("got invalid ack %d from node %d (tcode %d)",
291 packet->ack_code, packet->node_id, packet->tcode);
297 struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node,
298 u64 addr, size_t length)
300 struct hpsb_packet *packet;
305 packet = hpsb_alloc_packet(length);
310 packet->node_id = node;
312 if (hpsb_get_tlabel(packet)) {
313 hpsb_free_packet(packet);
318 fill_async_readquad(packet, addr);
320 fill_async_readblock(packet, addr, length);
325 struct hpsb_packet *hpsb_make_writepacket(struct hpsb_host *host, nodeid_t node,
326 u64 addr, quadlet_t * buffer,
329 struct hpsb_packet *packet;
334 packet = hpsb_alloc_packet(length);
338 if (length % 4) { /* zero padding bytes */
339 packet->data[length >> 2] = 0;
342 packet->node_id = node;
344 if (hpsb_get_tlabel(packet)) {
345 hpsb_free_packet(packet);
350 fill_async_writequad(packet, addr, buffer ? *buffer : 0);
352 fill_async_writeblock(packet, addr, length);
354 memcpy(packet->data, buffer, length);
360 struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 * buffer,
361 int length, int channel, int tag,
364 struct hpsb_packet *packet;
369 packet = hpsb_alloc_packet(length);
373 if (length % 4) { /* zero padding bytes */
374 packet->data[length >> 2] = 0;
378 if (hpsb_get_tlabel(packet)) {
379 hpsb_free_packet(packet);
383 fill_async_stream_packet(packet, length, channel, tag, sync);
385 memcpy(packet->data, buffer, length);
390 struct hpsb_packet *hpsb_make_lockpacket(struct hpsb_host *host, nodeid_t node,
391 u64 addr, int extcode,
392 quadlet_t * data, quadlet_t arg)
394 struct hpsb_packet *p;
397 p = hpsb_alloc_packet(8);
403 if (hpsb_get_tlabel(p)) {
409 case EXTCODE_FETCH_ADD:
410 case EXTCODE_LITTLE_ADD:
423 fill_async_lock(p, addr, extcode, length);
428 struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host,
429 nodeid_t node, u64 addr, int extcode,
430 octlet_t * data, octlet_t arg)
432 struct hpsb_packet *p;
435 p = hpsb_alloc_packet(16);
441 if (hpsb_get_tlabel(p)) {
447 case EXTCODE_FETCH_ADD:
448 case EXTCODE_LITTLE_ADD:
451 p->data[0] = *data >> 32;
452 p->data[1] = *data & 0xffffffff;
458 p->data[0] = arg >> 32;
459 p->data[1] = arg & 0xffffffff;
460 p->data[2] = *data >> 32;
461 p->data[3] = *data & 0xffffffff;
465 fill_async_lock(p, addr, extcode, length);
470 struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host, quadlet_t data)
472 struct hpsb_packet *p;
474 p = hpsb_alloc_packet(0);
479 fill_phy_packet(p, data);
484 struct hpsb_packet *hpsb_make_isopacket(struct hpsb_host *host,
485 int length, int channel,
488 struct hpsb_packet *p;
490 p = hpsb_alloc_packet(length);
495 fill_iso_packet(p, length, channel, tag, sync);
497 p->generation = get_hpsb_generation(host);
503 * FIXME - these functions should probably read from / write to user space to
504 * avoid in kernel buffers for user space callers
508 * hpsb_read - generic read function
510 * Recognizes the local node ID and act accordingly. Automatically uses a
511 * quadlet read request if @length == 4 and and a block read request otherwise.
512 * It does not yet support lengths that are not a multiple of 4.
514 * You must explicitly specifiy the @generation for which the node ID is valid,
515 * to avoid sending packets to the wrong nodes when we race with a bus reset.
517 int hpsb_read(struct hpsb_host *host, nodeid_t node, unsigned int generation,
518 u64 addr, quadlet_t * buffer, size_t length)
520 struct hpsb_packet *packet;
526 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
528 packet = hpsb_make_readpacket(host, node, addr, length);
534 packet->generation = generation;
535 retval = hpsb_send_packet_and_wait(packet);
539 retval = hpsb_packet_success(packet);
543 *buffer = packet->header[3];
545 memcpy(buffer, packet->data, length);
550 hpsb_free_tlabel(packet);
551 hpsb_free_packet(packet);
557 * hpsb_write - generic write function
559 * Recognizes the local node ID and act accordingly. Automatically uses a
560 * quadlet write request if @length == 4 and and a block write request
561 * otherwise. It does not yet support lengths that are not a multiple of 4.
563 * You must explicitly specifiy the @generation for which the node ID is valid,
564 * to avoid sending packets to the wrong nodes when we race with a bus reset.
566 int hpsb_write(struct hpsb_host *host, nodeid_t node, unsigned int generation,
567 u64 addr, quadlet_t * buffer, size_t length)
569 struct hpsb_packet *packet;
575 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
577 packet = hpsb_make_writepacket(host, node, addr, buffer, length);
582 packet->generation = generation;
583 retval = hpsb_send_packet_and_wait(packet);
585 goto hpsb_write_fail;
587 retval = hpsb_packet_success(packet);
590 hpsb_free_tlabel(packet);
591 hpsb_free_packet(packet);
598 int hpsb_lock(struct hpsb_host *host, nodeid_t node, unsigned int generation,
599 u64 addr, int extcode, quadlet_t * data, quadlet_t arg)
601 struct hpsb_packet *packet;
604 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
606 packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg);
610 packet->generation = generation;
611 retval = hpsb_send_packet_and_wait(packet);
615 retval = hpsb_packet_success(packet);
618 *data = packet->data[0];
622 hpsb_free_tlabel(packet);
623 hpsb_free_packet(packet);
628 int hpsb_send_gasp(struct hpsb_host *host, int channel, unsigned int generation,
629 quadlet_t * buffer, size_t length, u32 specifier_id,
630 unsigned int version)
632 struct hpsb_packet *packet;
634 u16 specifier_id_hi = (specifier_id & 0x00ffff00) >> 8;
635 u8 specifier_id_lo = specifier_id & 0xff;
637 HPSB_VERBOSE("Send GASP: channel = %d, length = %Zd", channel, length);
641 packet = hpsb_make_streampacket(host, NULL, length, channel, 3, 0);
645 packet->data[0] = cpu_to_be32((host->node_id << 16) | specifier_id_hi);
647 cpu_to_be32((specifier_id_lo << 24) | (version & 0x00ffffff));
649 memcpy(&(packet->data[2]), buffer, length - 8);
651 packet->generation = generation;
653 packet->no_waiter = 1;
655 retval = hpsb_send_packet(packet);
657 hpsb_free_packet(packet);