ieee1394: ieee1394_transactions needs sched.h
[powerpc.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/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>
17
18 #include <asm/bug.h>
19 #include <asm/errno.h>
20
21 #include "ieee1394.h"
22 #include "ieee1394_types.h"
23 #include "hosts.h"
24 #include "ieee1394_core.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 #ifndef HPSB_DEBUG_TLABELS
35 static
36 #endif
37 spinlock_t hpsb_tlabel_lock = SPIN_LOCK_UNLOCKED;
38
39 static DECLARE_WAIT_QUEUE_HEAD(tlabel_wq);
40
41 static void fill_async_readquad(struct hpsb_packet *packet, u64 addr)
42 {
43         PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ);
44         packet->header_size = 12;
45         packet->data_size = 0;
46         packet->expect_response = 1;
47 }
48
49 static void fill_async_readblock(struct hpsb_packet *packet, u64 addr,
50                                  int length)
51 {
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;
57 }
58
59 static void fill_async_writequad(struct hpsb_packet *packet, u64 addr,
60                                  quadlet_t data)
61 {
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;
67 }
68
69 static void fill_async_writeblock(struct hpsb_packet *packet, u64 addr,
70                                   int length)
71 {
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);
77 }
78
79 static void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode,
80                             int length)
81 {
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;
87 }
88
89 static void fill_iso_packet(struct hpsb_packet *packet, int length, int channel,
90                             int tag, int sync)
91 {
92         packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
93             | (TCODE_ISO_DATA << 4) | sync;
94
95         packet->header_size = 4;
96         packet->data_size = length;
97         packet->type = hpsb_iso;
98         packet->tcode = TCODE_ISO_DATA;
99 }
100
101 static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data)
102 {
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 */
110 }
111
112 static void fill_async_stream_packet(struct hpsb_packet *packet, int length,
113                                      int channel, int tag, int sync)
114 {
115         packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
116             | (TCODE_STREAM_DATA << 4) | sync;
117
118         packet->header_size = 4;
119         packet->data_size = length;
120         packet->type = hpsb_async;
121         packet->tcode = TCODE_ISO_DATA;
122 }
123
124 /* same as hpsb_get_tlabel, except that it returns immediately */
125 static int hpsb_get_tlabel_atomic(struct hpsb_packet *packet)
126 {
127         unsigned long flags, *tp;
128         u8 *next;
129         int tlabel, n = NODEID_TO_NODE(packet->node_id);
130
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)) {
134                 packet->tlabel = 0;
135                 return 0;
136         }
137         tp = packet->host->tl_pool[n].map;
138         next = &packet->host->next_tl[n];
139
140         spin_lock_irqsave(&hpsb_tlabel_lock, flags);
141         tlabel = find_next_zero_bit(tp, 64, *next);
142         if (tlabel > 63)
143                 tlabel = find_first_zero_bit(tp, 64);
144         if (tlabel > 63) {
145                 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
146                 return -EAGAIN;
147         }
148         __set_bit(tlabel, tp);
149         *next = (tlabel + 1) & 63;
150         spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
151
152         packet->tlabel = tlabel;
153         return 0;
154 }
155
156 /**
157  * hpsb_get_tlabel - allocate a transaction label
158  * @packet: the packet whose tlabel and tl_pool we set
159  *
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.
164  *
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).
168  *
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.
173  */
174 int hpsb_get_tlabel(struct hpsb_packet *packet)
175 {
176         if (irqs_disabled() || in_atomic())
177                 return hpsb_get_tlabel_atomic(packet);
178
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
183          * that. */
184         return wait_event_interruptible(tlabel_wq,
185                                         !hpsb_get_tlabel_atomic(packet));
186 }
187
188 /**
189  * hpsb_free_tlabel - free an allocated transaction label
190  * @packet: packet whose tlabel and tl_pool needs to be cleared
191  *
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
195  * transaction).
196  *
197  * A tlabel must not be freed twice.
198  */
199 void hpsb_free_tlabel(struct hpsb_packet *packet)
200 {
201         unsigned long flags, *tp;
202         int tlabel, n = NODEID_TO_NODE(packet->node_id);
203
204         if (unlikely(n == ALL_NODES))
205                 return;
206         tp = packet->host->tl_pool[n].map;
207         tlabel = packet->tlabel;
208         BUG_ON(tlabel > 63 || tlabel < 0);
209
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);
213
214         wake_up_interruptible(&tlabel_wq);
215 }
216
217 /**
218  * hpsb_packet_success - Make sense of the ack and reply codes
219  *
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.
225  */
226 int hpsb_packet_success(struct hpsb_packet *packet)
227 {
228         switch (packet->ack_code) {
229         case ACK_PENDING:
230                 switch ((packet->header[1] >> 12) & 0xf) {
231                 case RCODE_COMPLETE:
232                         return 0;
233                 case RCODE_CONFLICT_ERROR:
234                         return -EAGAIN;
235                 case RCODE_DATA_ERROR:
236                         return -EREMOTEIO;
237                 case RCODE_TYPE_ERROR:
238                         return -EACCES;
239                 case RCODE_ADDRESS_ERROR:
240                         return -EINVAL;
241                 default:
242                         HPSB_ERR("received reserved rcode %d from node %d",
243                                  (packet->header[1] >> 12) & 0xf,
244                                  packet->node_id);
245                         return -EAGAIN;
246                 }
247                 BUG();
248
249         case ACK_BUSY_X:
250         case ACK_BUSY_A:
251         case ACK_BUSY_B:
252                 return -EBUSY;
253
254         case ACK_TYPE_ERROR:
255                 return -EACCES;
256
257         case ACK_COMPLETE:
258                 if (packet->tcode == TCODE_WRITEQ
259                     || packet->tcode == TCODE_WRITEB) {
260                         return 0;
261                 } else {
262                         HPSB_ERR("impossible ack_complete from node %d "
263                                  "(tcode %d)", packet->node_id, packet->tcode);
264                         return -EAGAIN;
265                 }
266
267         case ACK_DATA_ERROR:
268                 if (packet->tcode == TCODE_WRITEB
269                     || packet->tcode == TCODE_LOCK_REQUEST) {
270                         return -EAGAIN;
271                 } else {
272                         HPSB_ERR("impossible ack_data_error from node %d "
273                                  "(tcode %d)", packet->node_id, packet->tcode);
274                         return -EAGAIN;
275                 }
276
277         case ACK_ADDRESS_ERROR:
278                 return -EINVAL;
279
280         case ACK_TARDY:
281         case ACK_CONFLICT_ERROR:
282         case ACKX_NONE:
283         case ACKX_SEND_ERROR:
284         case ACKX_ABORTED:
285         case ACKX_TIMEOUT:
286                 /* error while sending */
287                 return -EAGAIN;
288
289         default:
290                 HPSB_ERR("got invalid ack %d from node %d (tcode %d)",
291                          packet->ack_code, packet->node_id, packet->tcode);
292                 return -EAGAIN;
293         }
294         BUG();
295 }
296
297 struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node,
298                                          u64 addr, 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         packet->host = host;
310         packet->node_id = node;
311
312         if (hpsb_get_tlabel(packet)) {
313                 hpsb_free_packet(packet);
314                 return NULL;
315         }
316
317         if (length == 4)
318                 fill_async_readquad(packet, addr);
319         else
320                 fill_async_readblock(packet, addr, length);
321
322         return packet;
323 }
324
325 struct hpsb_packet *hpsb_make_writepacket(struct hpsb_host *host, nodeid_t node,
326                                           u64 addr, quadlet_t * buffer,
327                                           size_t length)
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         packet->node_id = node;
343
344         if (hpsb_get_tlabel(packet)) {
345                 hpsb_free_packet(packet);
346                 return NULL;
347         }
348
349         if (length == 4) {
350                 fill_async_writequad(packet, addr, buffer ? *buffer : 0);
351         } else {
352                 fill_async_writeblock(packet, addr, length);
353                 if (buffer)
354                         memcpy(packet->data, buffer, length);
355         }
356
357         return packet;
358 }
359
360 struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 * buffer,
361                                            int length, int channel, int tag,
362                                            int sync)
363 {
364         struct hpsb_packet *packet;
365
366         if (length == 0)
367                 return NULL;
368
369         packet = hpsb_alloc_packet(length);
370         if (!packet)
371                 return NULL;
372
373         if (length % 4) {       /* zero padding bytes */
374                 packet->data[length >> 2] = 0;
375         }
376         packet->host = host;
377
378         if (hpsb_get_tlabel(packet)) {
379                 hpsb_free_packet(packet);
380                 return NULL;
381         }
382
383         fill_async_stream_packet(packet, length, channel, tag, sync);
384         if (buffer)
385                 memcpy(packet->data, buffer, length);
386
387         return packet;
388 }
389
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)
393 {
394         struct hpsb_packet *p;
395         u32 length;
396
397         p = hpsb_alloc_packet(8);
398         if (!p)
399                 return NULL;
400
401         p->host = host;
402         p->node_id = node;
403         if (hpsb_get_tlabel(p)) {
404                 hpsb_free_packet(p);
405                 return NULL;
406         }
407
408         switch (extcode) {
409         case EXTCODE_FETCH_ADD:
410         case EXTCODE_LITTLE_ADD:
411                 length = 4;
412                 if (data)
413                         p->data[0] = *data;
414                 break;
415         default:
416                 length = 8;
417                 if (data) {
418                         p->data[0] = arg;
419                         p->data[1] = *data;
420                 }
421                 break;
422         }
423         fill_async_lock(p, addr, extcode, length);
424
425         return p;
426 }
427
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)
431 {
432         struct hpsb_packet *p;
433         u32 length;
434
435         p = hpsb_alloc_packet(16);
436         if (!p)
437                 return NULL;
438
439         p->host = host;
440         p->node_id = node;
441         if (hpsb_get_tlabel(p)) {
442                 hpsb_free_packet(p);
443                 return NULL;
444         }
445
446         switch (extcode) {
447         case EXTCODE_FETCH_ADD:
448         case EXTCODE_LITTLE_ADD:
449                 length = 8;
450                 if (data) {
451                         p->data[0] = *data >> 32;
452                         p->data[1] = *data & 0xffffffff;
453                 }
454                 break;
455         default:
456                 length = 16;
457                 if (data) {
458                         p->data[0] = arg >> 32;
459                         p->data[1] = arg & 0xffffffff;
460                         p->data[2] = *data >> 32;
461                         p->data[3] = *data & 0xffffffff;
462                 }
463                 break;
464         }
465         fill_async_lock(p, addr, extcode, length);
466
467         return p;
468 }
469
470 struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host, quadlet_t data)
471 {
472         struct hpsb_packet *p;
473
474         p = hpsb_alloc_packet(0);
475         if (!p)
476                 return NULL;
477
478         p->host = host;
479         fill_phy_packet(p, data);
480
481         return p;
482 }
483
484 struct hpsb_packet *hpsb_make_isopacket(struct hpsb_host *host,
485                                         int length, int channel,
486                                         int tag, int sync)
487 {
488         struct hpsb_packet *p;
489
490         p = hpsb_alloc_packet(length);
491         if (!p)
492                 return NULL;
493
494         p->host = host;
495         fill_iso_packet(p, length, channel, tag, sync);
496
497         p->generation = get_hpsb_generation(host);
498
499         return p;
500 }
501
502 /*
503  * FIXME - these functions should probably read from / write to user space to
504  * avoid in kernel buffers for user space callers
505  */
506
507 /**
508  * hpsb_read - generic read function
509  *
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.
513  *
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.
516  */
517 int hpsb_read(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 = 0;
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_readpacket(host, node, addr, length);
529
530         if (!packet) {
531                 return -ENOMEM;
532         }
533
534         packet->generation = generation;
535         retval = hpsb_send_packet_and_wait(packet);
536         if (retval < 0)
537                 goto hpsb_read_fail;
538
539         retval = hpsb_packet_success(packet);
540
541         if (retval == 0) {
542                 if (length == 4) {
543                         *buffer = packet->header[3];
544                 } else {
545                         memcpy(buffer, packet->data, length);
546                 }
547         }
548
549       hpsb_read_fail:
550         hpsb_free_tlabel(packet);
551         hpsb_free_packet(packet);
552
553         return retval;
554 }
555
556 /**
557  * hpsb_write - generic write function
558  *
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.
562  *
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.
565  */
566 int hpsb_write(struct hpsb_host *host, nodeid_t node, unsigned int generation,
567                u64 addr, quadlet_t * buffer, size_t length)
568 {
569         struct hpsb_packet *packet;
570         int retval;
571
572         if (length == 0)
573                 return -EINVAL;
574
575         BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
576
577         packet = hpsb_make_writepacket(host, node, addr, buffer, length);
578
579         if (!packet)
580                 return -ENOMEM;
581
582         packet->generation = generation;
583         retval = hpsb_send_packet_and_wait(packet);
584         if (retval < 0)
585                 goto hpsb_write_fail;
586
587         retval = hpsb_packet_success(packet);
588
589       hpsb_write_fail:
590         hpsb_free_tlabel(packet);
591         hpsb_free_packet(packet);
592
593         return retval;
594 }
595
596 #if 0
597
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)
600 {
601         struct hpsb_packet *packet;
602         int retval = 0;
603
604         BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
605
606         packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg);
607         if (!packet)
608                 return -ENOMEM;
609
610         packet->generation = generation;
611         retval = hpsb_send_packet_and_wait(packet);
612         if (retval < 0)
613                 goto hpsb_lock_fail;
614
615         retval = hpsb_packet_success(packet);
616
617         if (retval == 0) {
618                 *data = packet->data[0];
619         }
620
621       hpsb_lock_fail:
622         hpsb_free_tlabel(packet);
623         hpsb_free_packet(packet);
624
625         return retval;
626 }
627
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)
631 {
632         struct hpsb_packet *packet;
633         int retval = 0;
634         u16 specifier_id_hi = (specifier_id & 0x00ffff00) >> 8;
635         u8 specifier_id_lo = specifier_id & 0xff;
636
637         HPSB_VERBOSE("Send GASP: channel = %d, length = %Zd", channel, length);
638
639         length += 8;
640
641         packet = hpsb_make_streampacket(host, NULL, length, channel, 3, 0);
642         if (!packet)
643                 return -ENOMEM;
644
645         packet->data[0] = cpu_to_be32((host->node_id << 16) | specifier_id_hi);
646         packet->data[1] =
647             cpu_to_be32((specifier_id_lo << 24) | (version & 0x00ffffff));
648
649         memcpy(&(packet->data[2]), buffer, length - 8);
650
651         packet->generation = generation;
652
653         packet->no_waiter = 1;
654
655         retval = hpsb_send_packet(packet);
656         if (retval < 0)
657                 hpsb_free_packet(packet);
658
659         return retval;
660 }
661
662 #endif                          /*  0  */