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