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 <asm/errno.h>
14 #include <asm/bitops.h>
17 #include "ieee1394_types.h"
19 #include "ieee1394_core.h"
20 #include "highlevel.h"
23 #define PREP_ASYNC_HEAD_ADDRESS(tc) \
25 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
26 | (1 << 8) | (tc << 4); \
27 packet->header[1] = (packet->host->node_id << 16) | (addr >> 32); \
28 packet->header[2] = addr & 0xffffffff
30 #define PREP_ASYNC_HEAD_RCODE(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) | (rcode << 12); \
38 void fill_async_readquad(struct hpsb_packet *packet, u64 addr)
40 PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ);
41 packet->header_size = 12;
42 packet->data_size = 0;
43 packet->expect_response = 1;
46 void fill_async_readquad_resp(struct hpsb_packet *packet, int rcode,
49 PREP_ASYNC_HEAD_RCODE(TCODE_READQ_RESPONSE);
50 packet->header[3] = data;
51 packet->header_size = 16;
52 packet->data_size = 0;
55 void fill_async_readblock(struct hpsb_packet *packet, u64 addr, int length)
57 PREP_ASYNC_HEAD_ADDRESS(TCODE_READB);
58 packet->header[3] = length << 16;
59 packet->header_size = 16;
60 packet->data_size = 0;
61 packet->expect_response = 1;
64 void fill_async_readblock_resp(struct hpsb_packet *packet, int rcode,
67 if (rcode != RCODE_COMPLETE) {
71 PREP_ASYNC_HEAD_RCODE(TCODE_READB_RESPONSE);
72 packet->header[3] = length << 16;
73 packet->header_size = 16;
74 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
77 void fill_async_writequad(struct hpsb_packet *packet, u64 addr, quadlet_t data)
79 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEQ);
80 packet->header[3] = data;
81 packet->header_size = 16;
82 packet->data_size = 0;
83 packet->expect_response = 1;
86 void fill_async_writeblock(struct hpsb_packet *packet, u64 addr, int length)
88 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEB);
89 packet->header[3] = length << 16;
90 packet->header_size = 16;
91 packet->expect_response = 1;
92 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
95 void fill_async_write_resp(struct hpsb_packet *packet, int rcode)
97 PREP_ASYNC_HEAD_RCODE(TCODE_WRITE_RESPONSE);
98 packet->header[2] = 0;
99 packet->header_size = 12;
100 packet->data_size = 0;
103 void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode,
106 PREP_ASYNC_HEAD_ADDRESS(TCODE_LOCK_REQUEST);
107 packet->header[3] = (length << 16) | extcode;
108 packet->header_size = 16;
109 packet->data_size = length;
110 packet->expect_response = 1;
113 void fill_async_lock_resp(struct hpsb_packet *packet, int rcode, int extcode,
116 if (rcode != RCODE_COMPLETE) {
120 PREP_ASYNC_HEAD_RCODE(TCODE_LOCK_RESPONSE);
121 packet->header[3] = (length << 16) | extcode;
122 packet->header_size = 16;
123 packet->data_size = length;
126 void fill_iso_packet(struct hpsb_packet *packet, int length, int channel,
129 packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
130 | (TCODE_ISO_DATA << 4) | sync;
132 packet->header_size = 4;
133 packet->data_size = length;
134 packet->type = hpsb_iso;
135 packet->tcode = TCODE_ISO_DATA;
138 void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data)
140 packet->header[0] = data;
141 packet->header[1] = ~data;
142 packet->header_size = 8;
143 packet->data_size = 0;
144 packet->expect_response = 0;
145 packet->type = hpsb_raw; /* No CRC added */
146 packet->speed_code = SPEED_100; /* Force speed to be 100Mbps */
151 * get_tlabel - allocate a transaction label
152 * @host: host to be used for transmission
153 * @nodeid: the node ID of the transmission target
154 * @wait: whether to sleep if no tlabel is available
156 * Every asynchronous transaction on the 1394 bus needs a transaction label to
157 * match the response to the request. This label has to be different from any
158 * other transaction label in an outstanding request to the same node to make
159 * matching possible without ambiguity.
161 * There are 64 different tlabels, so an allocated tlabel has to be freed with
162 * free_tlabel() after the transaction is complete (unless it's reused again for
163 * the same target node).
165 * @wait must not be set to true if you are calling from interrupt context.
167 * Return value: The allocated transaction label or -1 if there was no free
168 * tlabel and @wait is false.
170 int get_tlabel(struct hpsb_host *host, nodeid_t nodeid, int wait)
174 int found_tlabel = 0;
177 down(&host->tlabel_count);
179 if (down_trylock(&host->tlabel_count)) return -1;
182 spin_lock_irqsave(&host->tlabel_lock, flags);
184 while (!found_tlabel) {
185 tlabel = host->tlabel_current;
186 if (tlabel < 32 && !(host->tlabel_pool[0] & 1 << tlabel)) {
187 host->tlabel_pool[0] |= 1 << tlabel;
189 } else if (!(host->tlabel_pool[1] & 1 << (tlabel - 32))) {
190 host->tlabel_pool[1] |= 1 << (tlabel - 32);
193 host->tlabel_current = (host->tlabel_current + 1) % 64;
196 spin_unlock_irqrestore(&host->tlabel_lock, flags);
202 * free_tlabel - free an allocated transaction label
203 * @host: host to be used for transmission
204 * @nodeid: the node ID of the transmission target
205 * @tlabel: the transaction label to free
207 * Frees the transaction label allocated with get_tlabel(). The tlabel has to
208 * be freed after the transaction is complete (i.e. response was received for a
209 * split transaction or packet was sent for a unified transaction).
211 * A tlabel must not be freed twice.
213 void free_tlabel(struct hpsb_host *host, nodeid_t nodeid, int tlabel)
217 spin_lock_irqsave(&host->tlabel_lock, flags);
220 host->tlabel_pool[0] &= ~(1 << tlabel);
222 host->tlabel_pool[1] &= ~(1 << (tlabel-32));
225 spin_unlock_irqrestore(&host->tlabel_lock, flags);
227 up(&host->tlabel_count);
232 int hpsb_packet_success(struct hpsb_packet *packet)
234 switch (packet->ack_code) {
236 switch ((packet->header[1] >> 12) & 0xf) {
239 case RCODE_CONFLICT_ERROR:
241 case RCODE_DATA_ERROR:
243 case RCODE_TYPE_ERROR:
245 case RCODE_ADDRESS_ERROR:
248 HPSB_ERR("received reserved rcode %d from node %d",
249 (packet->header[1] >> 12) & 0xf,
253 HPSB_PANIC("reached unreachable code 1 in %s", __FUNCTION__);
264 if (packet->tcode == TCODE_WRITEQ
265 || packet->tcode == TCODE_WRITEB) {
268 HPSB_ERR("impossible ack_complete from node %d "
269 "(tcode %d)", packet->node_id, packet->tcode);
275 if (packet->tcode == TCODE_WRITEB
276 || packet->tcode == TCODE_LOCK_REQUEST) {
279 HPSB_ERR("impossible ack_data_error from node %d "
280 "(tcode %d)", packet->node_id, packet->tcode);
285 case ACKX_SEND_ERROR:
288 /* error while sending */
292 HPSB_ERR("got invalid ack %d from node %d (tcode %d)",
293 packet->ack_code, packet->node_id, packet->tcode);
297 HPSB_PANIC("reached unreachable code 2 in %s", __FUNCTION__);
300 struct hpsb_packet *hpsb_make_readqpacket(struct hpsb_host *host, nodeid_t node,
303 struct hpsb_packet *p;
305 p = alloc_hpsb_packet(0);
309 p->tlabel = get_tlabel(host, node, 1);
311 fill_async_readquad(p, addr);
316 struct hpsb_packet *hpsb_make_readbpacket(struct hpsb_host *host, nodeid_t node,
317 u64 addr, size_t length)
319 struct hpsb_packet *p;
321 p = alloc_hpsb_packet(length + (length % 4 ? 4 - (length % 4) : 0));
325 p->tlabel = get_tlabel(host, node, 1);
327 fill_async_readblock(p, addr, length);
332 struct hpsb_packet *hpsb_make_writeqpacket(struct hpsb_host *host,
333 nodeid_t node, u64 addr,
336 struct hpsb_packet *p;
338 p = alloc_hpsb_packet(0);
342 p->tlabel = get_tlabel(host, node, 1);
344 fill_async_writequad(p, addr, data);
349 struct hpsb_packet *hpsb_make_writebpacket(struct hpsb_host *host,
350 nodeid_t node, u64 addr,
353 struct hpsb_packet *p;
355 p = alloc_hpsb_packet(length + (length % 4 ? 4 - (length % 4) : 0));
359 p->data[length / 4] = 0;
363 p->tlabel = get_tlabel(host, node, 1);
365 fill_async_writeblock(p, addr, length);
370 struct hpsb_packet *hpsb_make_lockpacket(struct hpsb_host *host, nodeid_t node,
371 u64 addr, int extcode)
373 struct hpsb_packet *p;
375 p = alloc_hpsb_packet(8);
379 p->tlabel = get_tlabel(host, node, 1);
383 case EXTCODE_FETCH_ADD:
384 case EXTCODE_LITTLE_ADD:
385 fill_async_lock(p, addr, extcode, 4);
388 fill_async_lock(p, addr, extcode, 8);
395 struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host, nodeid_t node,
396 u64 addr, int extcode)
398 struct hpsb_packet *p;
400 p = alloc_hpsb_packet(16);
404 p->tlabel = get_tlabel(host, node, 1);
408 case EXTCODE_FETCH_ADD:
409 case EXTCODE_LITTLE_ADD:
410 fill_async_lock(p, addr, extcode, 8);
413 fill_async_lock(p, addr, extcode, 16);
420 struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host,
423 struct hpsb_packet *p;
425 p = alloc_hpsb_packet(0);
429 fill_phy_packet(p, data);
435 * FIXME - these functions should probably read from / write to user space to
436 * avoid in kernel buffers for user space callers
439 int hpsb_read(struct hpsb_host *host, nodeid_t node, unsigned int generation,
440 u64 addr, quadlet_t *buffer, size_t length)
442 struct hpsb_packet *packet;
450 packet = hpsb_make_readqpacket(host, node, addr);
452 packet = hpsb_make_readbpacket(host, node, addr, length);
459 packet->generation = generation;
460 if (!hpsb_send_packet(packet)) {
465 down(&packet->state_change);
466 down(&packet->state_change);
467 retval = hpsb_packet_success(packet);
471 *buffer = packet->header[3];
473 memcpy(buffer, packet->data, length);
478 free_tlabel(host, node, packet->tlabel);
479 free_hpsb_packet(packet);
484 struct hpsb_packet *hpsb_make_packet (struct hpsb_host *host, nodeid_t node,
485 u64 addr, quadlet_t *buffer, size_t length)
487 struct hpsb_packet *packet;
493 packet = hpsb_make_writeqpacket(host, node, addr, *buffer);
495 packet = hpsb_make_writebpacket(host, node, addr, length);
500 /* Sometimes this may be called without data, just to allocate the
502 if (length != 4 && buffer)
503 memcpy(packet->data, buffer, length);
508 int hpsb_write(struct hpsb_host *host, nodeid_t node, unsigned int generation,
509 u64 addr, quadlet_t *buffer, size_t length)
511 struct hpsb_packet *packet;
517 packet = hpsb_make_packet (host, node, addr, buffer, length);
522 packet->generation = generation;
523 if (!hpsb_send_packet(packet)) {
525 goto hpsb_write_fail;
528 down(&packet->state_change);
529 down(&packet->state_change);
530 retval = hpsb_packet_success(packet);
533 free_tlabel(host, node, packet->tlabel);
534 free_hpsb_packet(packet);
540 /* We need a hpsb_lock64 function for the 64 bit equivalent. Probably. */
541 int hpsb_lock(struct hpsb_host *host, nodeid_t node, unsigned int generation,
542 u64 addr, int extcode, quadlet_t *data, quadlet_t arg)
544 struct hpsb_packet *packet;
545 int retval = 0, length;
547 packet = alloc_hpsb_packet(8);
553 packet->tlabel = get_tlabel(host, node, 1);
554 packet->node_id = node;
557 case EXTCODE_MASK_SWAP:
558 case EXTCODE_COMPARE_SWAP:
559 case EXTCODE_BOUNDED_ADD:
560 case EXTCODE_WRAP_ADD:
562 packet->data[0] = arg;
563 packet->data[1] = *data;
565 case EXTCODE_FETCH_ADD:
566 case EXTCODE_LITTLE_ADD:
568 packet->data[0] = *data;
573 fill_async_lock(packet, addr, extcode, length);
575 packet->generation = generation;
576 if (!hpsb_send_packet(packet)) {
580 down(&packet->state_change);
581 down(&packet->state_change);
582 retval = hpsb_packet_success(packet);
585 *data = packet->data[0];
589 free_tlabel(host, node, packet->tlabel);
590 free_hpsb_packet(packet);