Every file should #include the headers containing the prototypes for
[safe/jmp/linux-2.6] / drivers / ieee1394 / ieee1394_transactions.c
1 /*
2  * IEEE 1394 for Linux
3  *
4  * Transaction support.
5  *
6  * Copyright (C) 1999 Andreas E. Bombe
7  *
8  * This code is licensed under the GPL.  See the file COPYING in the root
9  * directory of the kernel sources for details.
10  */
11
12 #include <linux/sched.h>
13 #include <linux/bitops.h>
14 #include <linux/smp_lock.h>
15 #include <linux/interrupt.h>
16
17 #include <asm/errno.h>
18
19 #include "ieee1394.h"
20 #include "ieee1394_types.h"
21 #include "hosts.h"
22 #include "ieee1394_core.h"
23 #include "highlevel.h"
24 #include "nodemgr.h"
25 #include "ieee1394_transactions.h"
26
27
28 #define PREP_ASYNC_HEAD_ADDRESS(tc) \
29         packet->tcode = tc; \
30         packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
31                 | (1 << 8) | (tc << 4); \
32         packet->header[1] = (packet->host->node_id << 16) | (addr >> 32); \
33         packet->header[2] = addr & 0xffffffff
34
35
36 static void fill_async_readquad(struct hpsb_packet *packet, u64 addr)
37 {
38         PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ);
39         packet->header_size = 12;
40         packet->data_size = 0;
41         packet->expect_response = 1;
42 }
43
44 static void fill_async_readblock(struct hpsb_packet *packet, u64 addr, int length)
45 {
46         PREP_ASYNC_HEAD_ADDRESS(TCODE_READB);
47         packet->header[3] = length << 16;
48         packet->header_size = 16;
49         packet->data_size = 0;
50         packet->expect_response = 1;
51 }
52
53 static void fill_async_writequad(struct hpsb_packet *packet, u64 addr, quadlet_t data)
54 {
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;
60 }
61
62 static void fill_async_writeblock(struct hpsb_packet *packet, u64 addr, int length)
63 {
64         PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEB);
65         packet->header[3] = length << 16;
66         packet->header_size = 16;
67         packet->expect_response = 1;
68         packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
69 }
70
71 static void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode,
72                      int length)
73 {
74         PREP_ASYNC_HEAD_ADDRESS(TCODE_LOCK_REQUEST);
75         packet->header[3] = (length << 16) | extcode;
76         packet->header_size = 16;
77         packet->data_size = length;
78         packet->expect_response = 1;
79 }
80
81 static void fill_iso_packet(struct hpsb_packet *packet, int length, int channel,
82                      int tag, int sync)
83 {
84         packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
85                 | (TCODE_ISO_DATA << 4) | sync;
86
87         packet->header_size = 4;
88         packet->data_size = length;
89         packet->type = hpsb_iso;
90         packet->tcode = TCODE_ISO_DATA;
91 }
92
93 static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data)
94 {
95         packet->header[0] = data;
96         packet->header[1] = ~data;
97         packet->header_size = 8;
98         packet->data_size = 0;
99         packet->expect_response = 0;
100         packet->type = hpsb_raw;             /* No CRC added */
101         packet->speed_code = IEEE1394_SPEED_100; /* Force speed to be 100Mbps */
102 }
103
104 static void fill_async_stream_packet(struct hpsb_packet *packet, int length,
105                                      int channel, int tag, int sync)
106 {
107         packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
108                           | (TCODE_STREAM_DATA << 4) | sync;
109
110         packet->header_size = 4;
111         packet->data_size = length;
112         packet->type = hpsb_async;
113         packet->tcode = TCODE_ISO_DATA;
114 }
115
116 /**
117  * hpsb_get_tlabel - allocate a transaction label
118  * @packet: the packet who's tlabel/tpool we set
119  *
120  * Every asynchronous transaction on the 1394 bus needs a transaction
121  * label to match the response to the request.  This label has to be
122  * different from any other transaction label in an outstanding request to
123  * the same node to make matching possible without ambiguity.
124  *
125  * There are 64 different tlabels, so an allocated tlabel has to be freed
126  * with hpsb_free_tlabel() after the transaction is complete (unless it's
127  * reused again for the same target node).
128  *
129  * Return value: Zero on success, otherwise non-zero. A non-zero return
130  * generally means there are no available tlabels. If this is called out
131  * of interrupt or atomic context, then it will sleep until can return a
132  * tlabel.
133  */
134 int hpsb_get_tlabel(struct hpsb_packet *packet)
135 {
136         unsigned long flags;
137         struct hpsb_tlabel_pool *tp;
138
139         tp = &packet->host->tpool[packet->node_id & NODE_MASK];
140
141         if (irqs_disabled() || in_atomic()) {
142                 if (down_trylock(&tp->count))
143                         return 1;
144         } else {
145                 down(&tp->count);
146         }
147
148         spin_lock_irqsave(&tp->lock, flags);
149
150         packet->tlabel = find_next_zero_bit(tp->pool, 64, tp->next);
151         if (packet->tlabel > 63)
152                 packet->tlabel = find_first_zero_bit(tp->pool, 64);
153         tp->next = (packet->tlabel + 1) % 64;
154         /* Should _never_ happen */
155         BUG_ON(test_and_set_bit(packet->tlabel, tp->pool));
156         tp->allocations++;
157         spin_unlock_irqrestore(&tp->lock, flags);
158
159         return 0;
160 }
161
162 /**
163  * hpsb_free_tlabel - free an allocated transaction label
164  * @packet: packet whos tlabel/tpool needs to be cleared
165  *
166  * Frees the transaction label allocated with hpsb_get_tlabel().  The
167  * tlabel has to be freed after the transaction is complete (i.e. response
168  * was received for a split transaction or packet was sent for a unified
169  * transaction).
170  *
171  * A tlabel must not be freed twice.
172  */
173 void hpsb_free_tlabel(struct hpsb_packet *packet)
174 {
175         unsigned long flags;
176         struct hpsb_tlabel_pool *tp;
177
178         tp = &packet->host->tpool[packet->node_id & NODE_MASK];
179
180         BUG_ON(packet->tlabel > 63 || packet->tlabel < 0);
181
182         spin_lock_irqsave(&tp->lock, flags);
183         BUG_ON(!test_and_clear_bit(packet->tlabel, tp->pool));
184         spin_unlock_irqrestore(&tp->lock, flags);
185
186         up(&tp->count);
187 }
188
189
190
191 int hpsb_packet_success(struct hpsb_packet *packet)
192 {
193         switch (packet->ack_code) {
194         case ACK_PENDING:
195                 switch ((packet->header[1] >> 12) & 0xf) {
196                 case RCODE_COMPLETE:
197                         return 0;
198                 case RCODE_CONFLICT_ERROR:
199                         return -EAGAIN;
200                 case RCODE_DATA_ERROR:
201                         return -EREMOTEIO;
202                 case RCODE_TYPE_ERROR:
203                         return -EACCES;
204                 case RCODE_ADDRESS_ERROR:
205                         return -EINVAL;
206                 default:
207                         HPSB_ERR("received reserved rcode %d from node %d",
208                                  (packet->header[1] >> 12) & 0xf,
209                                  packet->node_id);
210                         return -EAGAIN;
211                 }
212                 HPSB_PANIC("reached unreachable code 1 in %s", __FUNCTION__);
213
214         case ACK_BUSY_X:
215         case ACK_BUSY_A:
216         case ACK_BUSY_B:
217                 return -EBUSY;
218
219         case ACK_TYPE_ERROR:
220                 return -EACCES;
221
222         case ACK_COMPLETE:
223                 if (packet->tcode == TCODE_WRITEQ
224                     || packet->tcode == TCODE_WRITEB) {
225                         return 0;
226                 } else {
227                         HPSB_ERR("impossible ack_complete from node %d "
228                                  "(tcode %d)", packet->node_id, packet->tcode);
229                         return -EAGAIN;
230                 }
231
232
233         case ACK_DATA_ERROR:
234                 if (packet->tcode == TCODE_WRITEB
235                     || packet->tcode == TCODE_LOCK_REQUEST) {
236                         return -EAGAIN;
237                 } else {
238                         HPSB_ERR("impossible ack_data_error from node %d "
239                                  "(tcode %d)", packet->node_id, packet->tcode);
240                         return -EAGAIN;
241                 }
242
243         case ACK_ADDRESS_ERROR:
244                 return -EINVAL;
245
246         case ACK_TARDY:
247         case ACK_CONFLICT_ERROR:
248         case ACKX_NONE:
249         case ACKX_SEND_ERROR:
250         case ACKX_ABORTED:
251         case ACKX_TIMEOUT:
252                 /* error while sending */
253                 return -EAGAIN;
254
255         default:
256                 HPSB_ERR("got invalid ack %d from node %d (tcode %d)",
257                          packet->ack_code, packet->node_id, packet->tcode);
258                 return -EAGAIN;
259         }
260
261         HPSB_PANIC("reached unreachable code 2 in %s", __FUNCTION__);
262 }
263
264 struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node,
265                                          u64 addr, size_t length)
266 {
267         struct hpsb_packet *packet;
268
269         if (length == 0)
270                 return NULL;
271
272         packet = hpsb_alloc_packet(length);
273         if (!packet)
274                 return NULL;
275
276         packet->host = host;
277         packet->node_id = node;
278
279         if (hpsb_get_tlabel(packet)) {
280                 hpsb_free_packet(packet);
281                 return NULL;
282         }
283
284         if (length == 4)
285                 fill_async_readquad(packet, addr);
286         else
287                 fill_async_readblock(packet, addr, length);
288
289         return packet;
290 }
291
292 struct hpsb_packet *hpsb_make_writepacket (struct hpsb_host *host, nodeid_t node,
293                                            u64 addr, quadlet_t *buffer, size_t length)
294 {
295         struct hpsb_packet *packet;
296
297         if (length == 0)
298                 return NULL;
299
300         packet = hpsb_alloc_packet(length);
301         if (!packet)
302                 return NULL;
303
304         if (length % 4) { /* zero padding bytes */
305                 packet->data[length >> 2] = 0;
306         }
307         packet->host = host;
308         packet->node_id = node;
309
310         if (hpsb_get_tlabel(packet)) {
311                 hpsb_free_packet(packet);
312                 return NULL;
313         }
314
315         if (length == 4) {
316                 fill_async_writequad(packet, addr, buffer ? *buffer : 0);
317         } else {
318                 fill_async_writeblock(packet, addr, length);
319                 if (buffer)
320                         memcpy(packet->data, buffer, length);
321         }
322
323         return packet;
324 }
325
326 struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 *buffer, int length,
327                                            int channel, int tag, int sync)
328 {
329         struct hpsb_packet *packet;
330
331         if (length == 0)
332                 return NULL;
333
334         packet = hpsb_alloc_packet(length);
335         if (!packet)
336                 return NULL;
337
338         if (length % 4) { /* zero padding bytes */
339                 packet->data[length >> 2] = 0;
340         }
341         packet->host = host;
342
343         if (hpsb_get_tlabel(packet)) {
344                 hpsb_free_packet(packet);
345                 return NULL;
346         }
347
348         fill_async_stream_packet(packet, length, channel, tag, sync);
349         if (buffer)
350                 memcpy(packet->data, buffer, length);
351
352         return packet;
353 }
354
355 struct hpsb_packet *hpsb_make_lockpacket(struct hpsb_host *host, nodeid_t node,
356                                          u64 addr, int extcode, quadlet_t *data,
357                                          quadlet_t arg)
358 {
359         struct hpsb_packet *p;
360         u32 length;
361
362         p = hpsb_alloc_packet(8);
363         if (!p) return NULL;
364
365         p->host = host;
366         p->node_id = node;
367         if (hpsb_get_tlabel(p)) {
368                 hpsb_free_packet(p);
369                 return NULL;
370         }
371
372         switch (extcode) {
373         case EXTCODE_FETCH_ADD:
374         case EXTCODE_LITTLE_ADD:
375                 length = 4;
376                 if (data)
377                         p->data[0] = *data;
378                 break;
379         default:
380                 length = 8;
381                 if (data) {
382                         p->data[0] = arg;
383                         p->data[1] = *data;
384                 }
385                 break;
386         }
387         fill_async_lock(p, addr, extcode, length);
388
389         return p;
390 }
391
392 struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host, nodeid_t node,
393                                            u64 addr, int extcode, octlet_t *data,
394                                            octlet_t arg)
395 {
396         struct hpsb_packet *p;
397         u32 length;
398
399         p = hpsb_alloc_packet(16);
400         if (!p) return NULL;
401
402         p->host = host;
403         p->node_id = node;
404         if (hpsb_get_tlabel(p)) {
405                 hpsb_free_packet(p);
406                 return NULL;
407         }
408
409         switch (extcode) {
410         case EXTCODE_FETCH_ADD:
411         case EXTCODE_LITTLE_ADD:
412                 length = 8;
413                 if (data) {
414                         p->data[0] = *data >> 32;
415                         p->data[1] = *data & 0xffffffff;
416                 }
417                 break;
418         default:
419                 length = 16;
420                 if (data) {
421                         p->data[0] = arg >> 32;
422                         p->data[1] = arg & 0xffffffff;
423                         p->data[2] = *data >> 32;
424                         p->data[3] = *data & 0xffffffff;
425                 }
426                 break;
427         }
428         fill_async_lock(p, addr, extcode, length);
429
430         return p;
431 }
432
433 struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host,
434                                         quadlet_t data)
435 {
436         struct hpsb_packet *p;
437
438         p = hpsb_alloc_packet(0);
439         if (!p) return NULL;
440
441         p->host = host;
442         fill_phy_packet(p, data);
443
444         return p;
445 }
446
447 struct hpsb_packet *hpsb_make_isopacket(struct hpsb_host *host,
448                                         int length, int channel,
449                                         int tag, int sync)
450 {
451         struct hpsb_packet *p;
452
453         p = hpsb_alloc_packet(length);
454         if (!p) return NULL;
455
456         p->host = host;
457         fill_iso_packet(p, length, channel, tag, sync);
458
459         p->generation = get_hpsb_generation(host);
460
461         return p;
462 }
463
464 /*
465  * FIXME - these functions should probably read from / write to user space to
466  * avoid in kernel buffers for user space callers
467  */
468
469 int hpsb_read(struct hpsb_host *host, nodeid_t node, unsigned int generation,
470               u64 addr, quadlet_t *buffer, size_t length)
471 {
472         struct hpsb_packet *packet;
473         int retval = 0;
474
475         if (length == 0)
476                 return -EINVAL;
477
478         BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
479
480         packet = hpsb_make_readpacket(host, node, addr, length);
481
482         if (!packet) {
483                 return -ENOMEM;
484         }
485
486         packet->generation = generation;
487         retval = hpsb_send_packet_and_wait(packet);
488         if (retval < 0)
489                 goto hpsb_read_fail;
490
491         retval = hpsb_packet_success(packet);
492
493         if (retval == 0) {
494                 if (length == 4) {
495                         *buffer = packet->header[3];
496                 } else {
497                         memcpy(buffer, packet->data, length);
498                 }
499         }
500
501 hpsb_read_fail:
502         hpsb_free_tlabel(packet);
503         hpsb_free_packet(packet);
504
505         return retval;
506 }
507
508
509 int hpsb_write(struct hpsb_host *host, nodeid_t node, unsigned int generation,
510                u64 addr, quadlet_t *buffer, size_t length)
511 {
512         struct hpsb_packet *packet;
513         int retval;
514
515         if (length == 0)
516                 return -EINVAL;
517
518         BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
519
520         packet = hpsb_make_writepacket (host, node, addr, buffer, length);
521
522         if (!packet)
523                 return -ENOMEM;
524
525         packet->generation = generation;
526         retval = hpsb_send_packet_and_wait(packet);
527         if (retval < 0)
528                 goto hpsb_write_fail;
529
530         retval = hpsb_packet_success(packet);
531
532 hpsb_write_fail:
533         hpsb_free_tlabel(packet);
534         hpsb_free_packet(packet);
535
536         return retval;
537 }
538
539 #if 0
540
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)
543 {
544         struct hpsb_packet *packet;
545         int retval = 0;
546
547         BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
548
549         packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg);
550         if (!packet)
551                 return -ENOMEM;
552
553         packet->generation = generation;
554         retval = hpsb_send_packet_and_wait(packet);
555         if (retval < 0)
556                 goto hpsb_lock_fail;
557
558         retval = hpsb_packet_success(packet);
559
560         if (retval == 0) {
561                 *data = packet->data[0];
562         }
563
564 hpsb_lock_fail:
565         hpsb_free_tlabel(packet);
566         hpsb_free_packet(packet);
567
568         return retval;
569 }
570
571
572 int hpsb_send_gasp(struct hpsb_host *host, int channel, unsigned int generation,
573                    quadlet_t *buffer, size_t length, u32 specifier_id,
574                    unsigned int version)
575 {
576         struct hpsb_packet *packet;
577         int retval = 0;
578         u16 specifier_id_hi = (specifier_id & 0x00ffff00) >> 8;
579         u8 specifier_id_lo = specifier_id & 0xff;
580
581         HPSB_VERBOSE("Send GASP: channel = %d, length = %Zd", channel, length);
582
583         length += 8;
584
585         packet = hpsb_make_streampacket(host, NULL, length, channel, 3, 0);
586         if (!packet)
587                 return -ENOMEM;
588
589         packet->data[0] = cpu_to_be32((host->node_id << 16) | specifier_id_hi);
590         packet->data[1] = cpu_to_be32((specifier_id_lo << 24) | (version & 0x00ffffff));
591
592         memcpy(&(packet->data[2]), buffer, length - 8);
593
594         packet->generation = generation;
595
596         packet->no_waiter = 1;
597
598         retval = hpsb_send_packet(packet);
599         if (retval < 0)
600                 hpsb_free_packet(packet);
601
602         return retval;
603 }
604
605 #endif  /*  0  */