v2.4.8 -> v2.4.8.1
[opensuse:kernel.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 <asm/errno.h>
14 #include <asm/bitops.h>
15
16 #include "ieee1394.h"
17 #include "ieee1394_types.h"
18 #include "hosts.h"
19 #include "ieee1394_core.h"
20 #include "highlevel.h"
21
22
23 #define PREP_ASYNC_HEAD_ADDRESS(tc) \
24         packet->tcode = 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
29
30 #define PREP_ASYNC_HEAD_RCODE(tc) \
31         packet->tcode = 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); \
35         packet->header[2] = 0
36
37
38 void fill_async_readquad(struct hpsb_packet *packet, u64 addr)
39 {
40         PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ);
41         packet->header_size = 12;
42         packet->data_size = 0;
43         packet->expect_response = 1;
44 }
45
46 void fill_async_readquad_resp(struct hpsb_packet *packet, int rcode, 
47                               quadlet_t data)
48 {
49         PREP_ASYNC_HEAD_RCODE(TCODE_READQ_RESPONSE);
50         packet->header[3] = data;
51         packet->header_size = 16;
52         packet->data_size = 0;
53 }
54
55 void fill_async_readblock(struct hpsb_packet *packet, u64 addr, int length)
56 {
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;
62 }
63
64 void fill_async_readblock_resp(struct hpsb_packet *packet, int rcode, 
65                                int length)
66 {
67         if (rcode != RCODE_COMPLETE) {
68                 length = 0;
69         }
70
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);
75 }
76
77 void fill_async_writequad(struct hpsb_packet *packet, u64 addr, quadlet_t data)
78 {
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;
84 }
85
86 void fill_async_writeblock(struct hpsb_packet *packet, u64 addr, int length)
87 {
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);
93 }
94
95 void fill_async_write_resp(struct hpsb_packet *packet, int rcode)
96 {
97         PREP_ASYNC_HEAD_RCODE(TCODE_WRITE_RESPONSE);
98         packet->header[2] = 0;
99         packet->header_size = 12;
100         packet->data_size = 0;
101 }
102
103 void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode, 
104                      int length)
105 {
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;
111 }
112
113 void fill_async_lock_resp(struct hpsb_packet *packet, int rcode, int extcode, 
114                           int length)
115 {
116         if (rcode != RCODE_COMPLETE) {
117                 length = 0;
118         }
119
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;
124 }
125
126 void fill_iso_packet(struct hpsb_packet *packet, int length, int channel,
127                      int tag, int sync)
128 {
129         packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
130                 | (TCODE_ISO_DATA << 4) | sync;
131
132         packet->header_size = 4;
133         packet->data_size = length;
134         packet->type = iso;
135         packet->tcode = TCODE_ISO_DATA;
136 }
137
138 void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data) 
139
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 = raw;             /* No CRC added */
146         packet->speed_code = SPEED_100; /* Force speed to be 100Mbps */
147 }
148
149
150 /**
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
155  *
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.
160  *
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).
164  *
165  * @wait must not be set to true if you are calling from interrupt context.
166  *
167  * Return value: The allocated transaction label or -1 if there was no free
168  * tlabel and @wait is false.
169  */
170 int get_tlabel(struct hpsb_host *host, nodeid_t nodeid, int wait)
171 {
172         int tlabel;
173         unsigned long flags;
174
175         if (wait) {
176                 down(&host->tlabel_count);
177         } else {
178                 if (down_trylock(&host->tlabel_count)) return -1;
179         }
180
181         spin_lock_irqsave(&host->tlabel_lock, flags);
182
183         if (host->tlabel_pool[0] != ~0) {
184                 tlabel = ffz(host->tlabel_pool[0]);
185                 host->tlabel_pool[0] |= 1 << tlabel;
186         } else {
187                 tlabel = ffz(host->tlabel_pool[1]);
188                 host->tlabel_pool[1] |= 1 << tlabel;
189                 tlabel += 32;
190         }
191
192         spin_unlock_irqrestore(&host->tlabel_lock, flags);
193
194         return tlabel;
195 }
196
197 /**
198  * free_tlabel - free an allocated transaction label
199  * @host: host to be used for transmission
200  * @nodeid: the node ID of the transmission target
201  * @tlabel: the transaction label to free
202  *
203  * Frees the transaction label allocated with get_tlabel().  The tlabel has to
204  * be freed after the transaction is complete (i.e. response was received for a
205  * split transaction or packet was sent for a unified transaction).
206  *
207  * A tlabel must not be freed twice.
208  */
209 void free_tlabel(struct hpsb_host *host, nodeid_t nodeid, int tlabel)
210 {
211         unsigned long flags;
212
213         spin_lock_irqsave(&host->tlabel_lock, flags);
214
215         if (tlabel < 32) {
216                 host->tlabel_pool[0] &= ~(1 << tlabel);
217         } else {
218                 host->tlabel_pool[1] &= ~(1 << (tlabel-32));
219         }
220
221         spin_unlock_irqrestore(&host->tlabel_lock, flags);
222
223         up(&host->tlabel_count);
224 }
225
226
227
228 int hpsb_packet_success(struct hpsb_packet *packet)
229 {
230         switch (packet->ack_code) {
231         case ACK_PENDING:
232                 switch ((packet->header[1] >> 12) & 0xf) {
233                 case RCODE_COMPLETE:
234                         return 0;
235                 case RCODE_CONFLICT_ERROR:
236                         return -EAGAIN;
237                 case RCODE_DATA_ERROR:
238                         return -EREMOTEIO;
239                 case RCODE_TYPE_ERROR:
240                         return -EACCES;
241                 case RCODE_ADDRESS_ERROR:
242                         return -EINVAL;
243                 default:
244                         HPSB_ERR("received reserved rcode %d from node %d",
245                                  (packet->header[1] >> 12) & 0xf, 
246                                  packet->node_id);
247                         return -EAGAIN;
248                 }
249                 HPSB_PANIC("reached unreachable code 1 in " __FUNCTION__);
250
251         case ACK_BUSY_X:
252         case ACK_BUSY_A:
253         case ACK_BUSY_B:
254                 return -EBUSY;
255
256         case ACK_TYPE_ERROR:
257                 return -EACCES;
258
259         case ACK_COMPLETE:
260                 if (packet->tcode == TCODE_WRITEQ
261                     || packet->tcode == TCODE_WRITEB) {
262                         return 0;
263                 } else {
264                         HPSB_ERR("impossible ack_complete from node %d "
265                                  "(tcode %d)", packet->node_id, packet->tcode);
266                         return -EAGAIN;
267                 }
268
269
270         case ACK_DATA_ERROR:
271                 if (packet->tcode == TCODE_WRITEB
272                     || packet->tcode == TCODE_LOCK_REQUEST) {
273                         return -EAGAIN;
274                 } else {
275                         HPSB_ERR("impossible ack_data_error from node %d "
276                                  "(tcode %d)", packet->node_id, packet->tcode);
277                         return -EAGAIN;
278                 }
279
280         case ACKX_NONE:
281         case ACKX_SEND_ERROR:
282         case ACKX_ABORTED:
283         case ACKX_TIMEOUT:
284                 /* error while sending */
285                 return -EAGAIN;
286
287         default:
288                 HPSB_ERR("got invalid ack %d from node %d (tcode %d)",
289                          packet->ack_code, packet->node_id, packet->tcode);
290                 return -EAGAIN;
291         }
292
293         HPSB_PANIC("reached unreachable code 2 in " __FUNCTION__);
294 }
295
296
297 int hpsb_read_trylocal(struct hpsb_host *host, nodeid_t node, u64 addr, 
298                        quadlet_t *buffer, size_t length)
299 {
300         if (host->node_id != node) return -1;
301         return highlevel_read(host, node, buffer, addr, length);
302 }
303
304 struct hpsb_packet *hpsb_make_readqpacket(struct hpsb_host *host, nodeid_t node,
305                                           u64 addr)
306 {
307         struct hpsb_packet *p;
308
309         p = alloc_hpsb_packet(0);
310         if (!p) return NULL;
311
312         p->host = host;
313         p->tlabel = get_tlabel(host, node, 1);
314         p->node_id = node;
315         fill_async_readquad(p, addr);
316
317         return p;
318 }
319
320 struct hpsb_packet *hpsb_make_readbpacket(struct hpsb_host *host, nodeid_t node,
321                                           u64 addr, size_t length)
322 {
323         struct hpsb_packet *p;
324
325         p = alloc_hpsb_packet(length + (length % 4 ? 4 - (length % 4) : 0));
326         if (!p) return NULL;
327
328         p->host = host;
329         p->tlabel = get_tlabel(host, node, 1);
330         p->node_id = node;
331         fill_async_readblock(p, addr, length);
332
333         return p;
334 }
335
336 struct hpsb_packet *hpsb_make_writeqpacket(struct hpsb_host *host,
337                                            nodeid_t node, u64 addr,
338                                            quadlet_t data)
339 {
340         struct hpsb_packet *p;
341
342         p = alloc_hpsb_packet(0);
343         if (!p) return NULL;
344
345         p->host = host;
346         p->tlabel = get_tlabel(host, node, 1);
347         p->node_id = node;
348         fill_async_writequad(p, addr, data);
349
350         return p;
351 }
352
353 struct hpsb_packet *hpsb_make_writebpacket(struct hpsb_host *host,
354                                            nodeid_t node, u64 addr,
355                                            size_t length)
356 {
357         struct hpsb_packet *p;
358
359         p = alloc_hpsb_packet(length + (length % 4 ? 4 - (length % 4) : 0));
360         if (!p) return NULL;
361
362         if (length % 4) {
363                 p->data[length / 4] = 0;
364         }
365
366         p->host = host;
367         p->tlabel = get_tlabel(host, node, 1);
368         p->node_id = node;
369         fill_async_writeblock(p, addr, length);
370
371         return p;
372 }
373
374 struct hpsb_packet *hpsb_make_lockpacket(struct hpsb_host *host, nodeid_t node,
375                                          u64 addr, int extcode)
376 {
377         struct hpsb_packet *p;
378
379         p = alloc_hpsb_packet(8);
380         if (!p) return NULL;
381
382         p->host = host;
383         p->tlabel = get_tlabel(host, node, 1);
384         p->node_id = node;
385
386         switch (extcode) {
387         case EXTCODE_FETCH_ADD:
388         case EXTCODE_LITTLE_ADD:
389                 fill_async_lock(p, addr, extcode, 4);
390                 break;
391         default:
392                 fill_async_lock(p, addr, extcode, 8);
393                 break;
394         }
395
396         return p;
397 }
398
399 struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host,
400                                         quadlet_t data) 
401 {
402         struct hpsb_packet *p; 
403
404         p = alloc_hpsb_packet(0); 
405         if (!p) return NULL; 
406
407         p->host = host; 
408         fill_phy_packet(p, data); 
409
410         return p; 
411 }
412
413 /*
414  * FIXME - these functions should probably read from / write to user space to
415  * avoid in kernel buffers for user space callers
416  */
417
418 int hpsb_read(struct hpsb_host *host, nodeid_t node, u64 addr,
419               quadlet_t *buffer, size_t length)
420 {
421         struct hpsb_packet *packet;
422         int retval = 0;
423         
424         if (length == 0) {
425                 return -EINVAL;
426         }
427
428         if (host->node_id == node) {
429                 switch(highlevel_read(host, node, buffer, addr, length)) {
430                 case RCODE_COMPLETE:
431                         return 0;
432                 case RCODE_TYPE_ERROR:
433                         return -EACCES;
434                 case RCODE_ADDRESS_ERROR:
435                 default:
436                         return -EINVAL;
437                 }
438         }
439
440         if (length == 4) {
441                 packet = hpsb_make_readqpacket(host, node, addr);
442         } else {
443                 packet = hpsb_make_readbpacket(host, node, addr, length);
444         }
445
446         if (!packet) {
447                 return -ENOMEM;
448         }
449
450         packet->generation = get_hpsb_generation(host);
451         if (!hpsb_send_packet(packet)) {
452                 retval = -EINVAL;
453                 goto hpsb_read_fail;
454         }
455
456         down(&packet->state_change);
457         down(&packet->state_change);
458         retval = hpsb_packet_success(packet);
459
460         if (retval == 0) {
461                 if (length == 4) {
462                         *buffer = packet->header[3];
463                 } else {
464                         memcpy(buffer, packet->data, length);
465                 }
466         }
467
468 hpsb_read_fail:
469         free_tlabel(host, node, packet->tlabel);
470         free_hpsb_packet(packet);
471
472         return retval;
473 }
474
475 struct hpsb_packet *hpsb_make_packet (struct hpsb_host *host, nodeid_t node,
476                                       u64 addr, quadlet_t *buffer, size_t length)
477 {
478         struct hpsb_packet *packet;
479         
480         if (length == 0)
481                 return NULL;
482
483         if (length == 4)
484                 packet = hpsb_make_writeqpacket(host, node, addr, *buffer);
485         else
486                 packet = hpsb_make_writebpacket(host, node, addr, length);
487
488         if (!packet)
489                 return NULL;
490
491         if (length != 4)
492                 memcpy(packet->data, buffer, length);
493
494         return packet;
495 }
496
497 int hpsb_write(struct hpsb_host *host, nodeid_t node, u64 addr,
498                quadlet_t *buffer, size_t length)
499 {
500         struct hpsb_packet *packet;
501         int retval;
502
503         if (length == 0)
504                 return -EINVAL;
505
506         if (host->node_id == node) {
507                 switch(highlevel_write(host, node, node, buffer, addr, length)) {
508                 case RCODE_COMPLETE:
509                         return 0;
510                 case RCODE_TYPE_ERROR:
511                         return -EACCES;
512                 case RCODE_ADDRESS_ERROR:
513                 default:
514                         return -EINVAL;
515                 }
516         }
517
518         packet = hpsb_make_packet (host, node, addr, buffer, length);
519
520         if (!packet)
521                 return -ENOMEM;
522
523         packet->generation = get_hpsb_generation(host);
524         if (!hpsb_send_packet(packet)) {
525                 retval = -EINVAL;
526                 goto hpsb_write_fail;
527         }
528
529         down(&packet->state_change);
530         down(&packet->state_change);
531         retval = hpsb_packet_success(packet);
532
533 hpsb_write_fail:
534         free_tlabel(host, node, packet->tlabel);
535         free_hpsb_packet(packet);
536
537         return retval;
538 }
539
540
541 /* We need a hpsb_lock64 function for the 64 bit equivalent.  Probably. */
542 int hpsb_lock(struct hpsb_host *host, nodeid_t node, u64 addr, int extcode,
543               quadlet_t *data, quadlet_t arg)
544 {
545         struct hpsb_packet *packet;
546         int retval = 0, length;
547         
548         if (host->node_id == node) {
549                 switch(highlevel_lock(host, node, data, addr, *data, arg,
550                                       extcode)) {
551                 case RCODE_COMPLETE:
552                         return 0;
553                 case RCODE_TYPE_ERROR:
554                         return -EACCES;
555                 case RCODE_ADDRESS_ERROR:
556                 default:
557                         return -EINVAL;
558                 }
559         }
560
561         packet = alloc_hpsb_packet(8);
562         if (!packet) {
563                 return -ENOMEM;
564         }
565
566         packet->host = host;
567         packet->tlabel = get_tlabel(host, node, 1);
568         packet->node_id = node;
569
570         switch (extcode) {
571         case EXTCODE_MASK_SWAP:
572         case EXTCODE_COMPARE_SWAP:
573         case EXTCODE_BOUNDED_ADD:
574         case EXTCODE_WRAP_ADD:
575                 length = 8;
576                 packet->data[0] = arg;
577                 packet->data[1] = *data;
578                 break;
579         case EXTCODE_FETCH_ADD:
580         case EXTCODE_LITTLE_ADD:
581                 length = 4;
582                 packet->data[0] = *data;
583                 break;
584         default:
585                 return -EINVAL;
586         }
587         fill_async_lock(packet, addr, extcode, length);
588
589         packet->generation = get_hpsb_generation(host);
590         if (!hpsb_send_packet(packet)) {
591                 retval = -EINVAL;
592                 goto hpsb_lock_fail;
593         }
594         down(&packet->state_change);
595         down(&packet->state_change);
596         retval = hpsb_packet_success(packet);
597
598         if (retval == 0) {
599                 *data = packet->data[0];
600         }
601
602 hpsb_lock_fail:
603         free_tlabel(host, node, packet->tlabel);
604         free_hpsb_packet(packet);
605
606         return retval;
607 }