ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / ieee1394 / ieee1394_core.c
1 /*
2  * IEEE 1394 for Linux
3  *
4  * Core support: hpsb_packet management, packet handling and forwarding to
5  *               highlevel or lowlevel code
6  *
7  * Copyright (C) 1999, 2000 Andreas E. Bombe
8  *                     2002 Manfred Weihs <weihs@ict.tuwien.ac.at>
9  *
10  * This code is licensed under the GPL.  See the file COPYING in the root
11  * directory of the kernel sources for details.
12  *
13  *
14  * Contributions:
15  *
16  * Manfred Weihs <weihs@ict.tuwien.ac.at>
17  *        loopback functionality in hpsb_send_packet
18  *        allow highlevel drivers to disable automatic response generation
19  *              and to generate responses themselves (deferred)
20  *
21  */
22
23 #include <linux/config.h>
24 #include <linux/kernel.h>
25 #include <linux/list.h>
26 #include <linux/string.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/bitops.h>
33 #include <linux/kdev_t.h>
34 #include <linux/skbuff.h>
35
36 #include <asm/byteorder.h>
37 #include <asm/semaphore.h>
38
39 #include "ieee1394_types.h"
40 #include "ieee1394.h"
41 #include "hosts.h"
42 #include "ieee1394_core.h"
43 #include "highlevel.h"
44 #include "ieee1394_transactions.h"
45 #include "csr.h"
46 #include "nodemgr.h"
47 #include "dma.h"
48 #include "iso.h"
49 #include "config_roms.h"
50
51 /*
52  * Disable the nodemgr detection and config rom reading functionality.
53  */
54 static int disable_nodemgr = 0;
55 module_param(disable_nodemgr, int, 0444);
56 MODULE_PARM_DESC(disable_nodemgr, "Disable nodemgr functionality.");
57
58 /* We are GPL, so treat us special */
59 MODULE_LICENSE("GPL");
60
61 /* Some globals used */
62 const char *hpsb_speedto_str[] = { "S100", "S200", "S400", "S800", "S1600", "S3200" };
63
64 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
65 static void dump_packet(const char *text, quadlet_t *data, int size)
66 {
67         int i;
68
69         size /= 4;
70         size = (size > 4 ? 4 : size);
71
72         printk(KERN_DEBUG "ieee1394: %s", text);
73         for (i = 0; i < size; i++)
74                 printk(" %08x", data[i]);
75         printk("\n");
76 }
77 #else
78 #define dump_packet(x,y,z)
79 #endif
80
81 static void queue_packet_complete(struct hpsb_packet *packet);
82
83
84 /**
85  * hpsb_set_packet_complete_task - set the task that runs when a packet
86  * completes. You cannot call this more than once on a single packet
87  * before it is sent.
88  *
89  * @packet: the packet whose completion we want the task added to
90  * @routine: function to call
91  * @data: data (if any) to pass to the above function
92  */
93 void hpsb_set_packet_complete_task(struct hpsb_packet *packet,
94                                    void (*routine)(void *), void *data)
95 {
96         WARN_ON(packet->complete_routine != NULL);
97         packet->complete_routine = routine;
98         packet->complete_data = data;
99         return;
100 }
101
102 /**
103  * hpsb_alloc_packet - allocate new packet structure
104  * @data_size: size of the data block to be allocated
105  *
106  * This function allocates, initializes and returns a new &struct hpsb_packet.
107  * It can be used in interrupt context.  A header block is always included, its
108  * size is big enough to contain all possible 1394 headers.  The data block is
109  * only allocated when @data_size is not zero.
110  *
111  * For packets for which responses will be received the @data_size has to be big
112  * enough to contain the response's data block since no further allocation
113  * occurs at response matching time.
114  *
115  * The packet's generation value will be set to the current generation number
116  * for ease of use.  Remember to overwrite it with your own recorded generation
117  * number if you can not be sure that your code will not race with a bus reset.
118  *
119  * Return value: A pointer to a &struct hpsb_packet or NULL on allocation
120  * failure.
121  */
122 struct hpsb_packet *hpsb_alloc_packet(size_t data_size)
123 {
124         struct hpsb_packet *packet = NULL;
125         struct sk_buff *skb;
126
127         data_size = ((data_size + 3) & ~3);
128
129         skb = alloc_skb(data_size + sizeof(*packet), GFP_ATOMIC);
130         if (skb == NULL)
131                 return NULL;
132
133         memset(skb->data, 0, data_size + sizeof(*packet));
134
135         packet = (struct hpsb_packet *)skb->data;
136         packet->skb = skb;
137
138         packet->header = packet->embedded_header;
139         packet->state = hpsb_unused;
140         packet->generation = -1;
141         INIT_LIST_HEAD(&packet->driver_list);
142         atomic_set(&packet->refcnt, 1);
143
144         if (data_size) {
145                 packet->data = (quadlet_t *)(skb->data + sizeof(*packet));
146                 packet->data_size = data_size;
147         }
148
149         return packet;
150 }
151
152
153 /**
154  * hpsb_free_packet - free packet and data associated with it
155  * @packet: packet to free (is NULL safe)
156  *
157  * This function will free packet->data and finally the packet itself.
158  */
159 void hpsb_free_packet(struct hpsb_packet *packet)
160 {
161         if (packet && atomic_dec_and_test(&packet->refcnt)) {
162                 BUG_ON(!list_empty(&packet->driver_list));
163                 kfree_skb(packet->skb);
164         }
165 }
166
167
168 int hpsb_reset_bus(struct hpsb_host *host, int type)
169 {
170         if (!host->in_bus_reset) {
171                 host->driver->devctl(host, RESET_BUS, type);
172                 return 0;
173         } else {
174                 return 1;
175         }
176 }
177
178
179 int hpsb_bus_reset(struct hpsb_host *host)
180 {
181         if (host->in_bus_reset) {
182                 HPSB_NOTICE("%s called while bus reset already in progress",
183                             __FUNCTION__);
184                 return 1;
185         }
186
187         abort_requests(host);
188         host->in_bus_reset = 1;
189         host->irm_id = -1;
190         host->is_irm = 0;
191         host->busmgr_id = -1;
192         host->is_busmgr = 0;
193         host->is_cycmst = 0;
194         host->node_count = 0;
195         host->selfid_count = 0;
196
197         return 0;
198 }
199
200
201 /*
202  * Verify num_of_selfids SelfIDs and return number of nodes.  Return zero in
203  * case verification failed.
204  */
205 static int check_selfids(struct hpsb_host *host)
206 {
207         int nodeid = -1;
208         int rest_of_selfids = host->selfid_count;
209         struct selfid *sid = (struct selfid *)host->topology_map;
210         struct ext_selfid *esid;
211         int esid_seq = 23;
212
213         host->nodes_active = 0;
214
215         while (rest_of_selfids--) {
216                 if (!sid->extended) {
217                         nodeid++;
218                         esid_seq = 0;
219
220                         if (sid->phy_id != nodeid) {
221                                 HPSB_INFO("SelfIDs failed monotony check with "
222                                           "%d", sid->phy_id);
223                                 return 0;
224                         }
225
226                         if (sid->link_active) {
227                                 host->nodes_active++;
228                                 if (sid->contender)
229                                         host->irm_id = LOCAL_BUS | sid->phy_id;
230                         }
231                 } else {
232                         esid = (struct ext_selfid *)sid;
233
234                         if ((esid->phy_id != nodeid)
235                             || (esid->seq_nr != esid_seq)) {
236                                 HPSB_INFO("SelfIDs failed monotony check with "
237                                           "%d/%d", esid->phy_id, esid->seq_nr);
238                                 return 0;
239                         }
240                         esid_seq++;
241                 }
242                 sid++;
243         }
244
245         esid = (struct ext_selfid *)(sid - 1);
246         while (esid->extended) {
247                 if ((esid->porta == 0x2) || (esid->portb == 0x2)
248                     || (esid->portc == 0x2) || (esid->portd == 0x2)
249                     || (esid->porte == 0x2) || (esid->portf == 0x2)
250                     || (esid->portg == 0x2) || (esid->porth == 0x2)) {
251                         HPSB_INFO("SelfIDs failed root check on "
252                                   "extended SelfID");
253                         return 0;
254                 }
255                 esid--;
256         }
257
258         sid = (struct selfid *)esid;
259         if ((sid->port0 == 0x2) || (sid->port1 == 0x2) || (sid->port2 == 0x2)) {
260                 HPSB_INFO("SelfIDs failed root check");
261                 return 0;
262         }
263
264         host->node_count = nodeid + 1;
265         return 1;
266 }
267
268 static void build_speed_map(struct hpsb_host *host, int nodecount)
269 {
270         u8 speedcap[nodecount];
271         u8 cldcnt[nodecount];
272         u8 *map = host->speed_map;
273         struct selfid *sid;
274         struct ext_selfid *esid;
275         int i, j, n;
276
277         for (i = 0; i < (nodecount * 64); i += 64) {
278                 for (j = 0; j < nodecount; j++) {
279                         map[i+j] = IEEE1394_SPEED_MAX;
280                 }
281         }
282
283         for (i = 0; i < nodecount; i++) {
284                 cldcnt[i] = 0;
285         }
286
287         /* find direct children count and speed */
288         for (sid = (struct selfid *)&host->topology_map[host->selfid_count-1],
289                      n = nodecount - 1;
290              (void *)sid >= (void *)host->topology_map; sid--) {
291                 if (sid->extended) {
292                         esid = (struct ext_selfid *)sid;
293
294                         if (esid->porta == 0x3) cldcnt[n]++;
295                         if (esid->portb == 0x3) cldcnt[n]++;
296                         if (esid->portc == 0x3) cldcnt[n]++;
297                         if (esid->portd == 0x3) cldcnt[n]++;
298                         if (esid->porte == 0x3) cldcnt[n]++;
299                         if (esid->portf == 0x3) cldcnt[n]++;
300                         if (esid->portg == 0x3) cldcnt[n]++;
301                         if (esid->porth == 0x3) cldcnt[n]++;
302                 } else {
303                         if (sid->port0 == 0x3) cldcnt[n]++;
304                         if (sid->port1 == 0x3) cldcnt[n]++;
305                         if (sid->port2 == 0x3) cldcnt[n]++;
306
307                         speedcap[n] = sid->speed;
308                         n--;
309                 }
310         }
311
312         /* set self mapping */
313         for (i = 0; i < nodecount; i++) {
314                 map[64*i + i] = speedcap[i];
315         }
316
317         /* fix up direct children count to total children count;
318          * also fix up speedcaps for sibling and parent communication */
319         for (i = 1; i < nodecount; i++) {
320                 for (j = cldcnt[i], n = i - 1; j > 0; j--) {
321                         cldcnt[i] += cldcnt[n];
322                         speedcap[n] = min(speedcap[n], speedcap[i]);
323                         n -= cldcnt[n] + 1;
324                 }
325         }
326
327         for (n = 0; n < nodecount; n++) {
328                 for (i = n - cldcnt[n]; i <= n; i++) {
329                         for (j = 0; j < (n - cldcnt[n]); j++) {
330                                 map[j*64 + i] = map[i*64 + j] =
331                                         min(map[i*64 + j], speedcap[n]);
332                         }
333                         for (j = n + 1; j < nodecount; j++) {
334                                 map[j*64 + i] = map[i*64 + j] =
335                                         min(map[i*64 + j], speedcap[n]);
336                         }
337                 }
338         }
339 }
340
341
342 void hpsb_selfid_received(struct hpsb_host *host, quadlet_t sid)
343 {
344         if (host->in_bus_reset) {
345                 HPSB_VERBOSE("Including SelfID 0x%x", sid);
346                 host->topology_map[host->selfid_count++] = sid;
347         } else {
348                 HPSB_NOTICE("Spurious SelfID packet (0x%08x) received from bus %d",
349                             sid, NODEID_TO_BUS(host->node_id));
350         }
351 }
352
353 void hpsb_selfid_complete(struct hpsb_host *host, int phyid, int isroot)
354 {
355         if (!host->in_bus_reset)
356                 HPSB_NOTICE("SelfID completion called outside of bus reset!");
357
358         host->node_id = LOCAL_BUS | phyid;
359         host->is_root = isroot;
360
361         if (!check_selfids(host)) {
362                 if (host->reset_retries++ < 20) {
363                         /* selfid stage did not complete without error */
364                         HPSB_NOTICE("Error in SelfID stage, resetting");
365                         host->in_bus_reset = 0;
366                         /* this should work from ohci1394 now... */
367                         hpsb_reset_bus(host, LONG_RESET);
368                         return;
369                 } else {
370                         HPSB_NOTICE("Stopping out-of-control reset loop");
371                         HPSB_NOTICE("Warning - topology map and speed map will not be valid");
372                         host->reset_retries = 0;
373                 }
374         } else {
375                 host->reset_retries = 0;
376                 build_speed_map(host, host->node_count);
377         }
378
379         HPSB_VERBOSE("selfid_complete called with successful SelfID stage "
380                      "... irm_id: 0x%X node_id: 0x%X",host->irm_id,host->node_id);
381
382         /* irm_id is kept up to date by check_selfids() */
383         if (host->irm_id == host->node_id) {
384                 host->is_irm = 1;
385         } else {
386                 host->is_busmgr = 0;
387                 host->is_irm = 0;
388         }
389
390         if (isroot) {
391                 host->driver->devctl(host, ACT_CYCLE_MASTER, 1);
392                 host->is_cycmst = 1;
393         }
394         atomic_inc(&host->generation);
395         host->in_bus_reset = 0;
396         highlevel_host_reset(host);
397 }
398
399
400 void hpsb_packet_sent(struct hpsb_host *host, struct hpsb_packet *packet,
401                       int ackcode)
402 {
403         packet->ack_code = ackcode;
404
405         if (packet->no_waiter) {
406                 /* must not have a tlabel allocated */
407                 hpsb_free_packet(packet);
408                 return;
409         }
410
411         if (ackcode != ACK_PENDING || !packet->expect_response) {
412                 atomic_dec(&packet->refcnt);
413                 skb_unlink(packet->skb);
414                 packet->state = hpsb_complete;
415                 queue_packet_complete(packet);
416                 return;
417         }
418
419         if (packet->state == hpsb_complete) {
420                 hpsb_free_packet(packet);
421                 return;
422         }
423
424         atomic_dec(&packet->refcnt);
425         packet->state = hpsb_pending;
426         packet->sendtime = jiffies;
427
428         mod_timer(&host->timeout, jiffies + host->timeout_interval);
429 }
430
431 /**
432  * hpsb_send_phy_config - transmit a PHY configuration packet on the bus
433  * @host: host that PHY config packet gets sent through
434  * @rootid: root whose force_root bit should get set (-1 = don't set force_root)
435  * @gapcnt: gap count value to set (-1 = don't set gap count)
436  *
437  * This function sends a PHY config packet on the bus through the specified host.
438  *
439  * Return value: 0 for success or error number otherwise.
440  */
441 int hpsb_send_phy_config(struct hpsb_host *host, int rootid, int gapcnt)
442 {
443         struct hpsb_packet *packet;
444         int retval = 0;
445
446         if (rootid >= ALL_NODES || rootid < -1 || gapcnt > 0x3f || gapcnt < -1 ||
447            (rootid == -1 && gapcnt == -1)) {
448                 HPSB_DEBUG("Invalid Parameter: rootid = %d   gapcnt = %d",
449                            rootid, gapcnt);
450                 return -EINVAL;
451         }
452
453         packet = hpsb_alloc_packet(0);
454         if (!packet)
455                 return -ENOMEM;
456
457         packet->host = host;
458         packet->header_size = 8;
459         packet->data_size = 0;
460         packet->expect_response = 0;
461         packet->no_waiter = 0;
462         packet->type = hpsb_raw;
463         packet->header[0] = 0;
464         if (rootid != -1)
465                 packet->header[0] |= rootid << 24 | 1 << 23;
466         if (gapcnt != -1)
467                 packet->header[0] |= gapcnt << 16 | 1 << 22;
468
469         packet->header[1] = ~packet->header[0];
470
471         packet->generation = get_hpsb_generation(host);
472
473         retval = hpsb_send_packet_and_wait(packet);
474         hpsb_free_packet(packet);
475
476         return retval;
477 }
478
479 /**
480  * hpsb_send_packet - transmit a packet on the bus
481  * @packet: packet to send
482  *
483  * The packet is sent through the host specified in the packet->host field.
484  * Before sending, the packet's transmit speed is automatically determined
485  * using the local speed map when it is an async, non-broadcast packet.
486  *
487  * Possibilities for failure are that host is either not initialized, in bus
488  * reset, the packet's generation number doesn't match the current generation
489  * number or the host reports a transmit error.
490  *
491  * Return value: 0 on success, negative errno on failure.
492  */
493 int hpsb_send_packet(struct hpsb_packet *packet)
494 {
495         struct hpsb_host *host = packet->host;
496
497         if (host->is_shutdown)
498                 return -EINVAL;
499         if (host->in_bus_reset ||
500             (packet->generation != get_hpsb_generation(host)))
501                 return -EAGAIN;
502
503         packet->state = hpsb_queued;
504
505         /* This just seems silly to me */
506         WARN_ON(packet->no_waiter && packet->expect_response);
507
508         if (!packet->no_waiter || packet->expect_response) {
509                 atomic_inc(&packet->refcnt);
510                 skb_queue_tail(&host->pending_packet_queue, packet->skb);
511         }
512
513         if (packet->node_id == host->node_id) {
514                 /* it is a local request, so handle it locally */
515
516                 quadlet_t *data;
517                 size_t size = packet->data_size + packet->header_size;
518
519                 data = kmalloc(size, GFP_ATOMIC);
520                 if (!data) {
521                         HPSB_ERR("unable to allocate memory for concatenating header and data");
522                         return -ENOMEM;
523                 }
524
525                 memcpy(data, packet->header, packet->header_size);
526
527                 if (packet->data_size)
528                         memcpy(((u8*)data) + packet->header_size, packet->data, packet->data_size);
529
530                 dump_packet("send packet local:", packet->header,
531                             packet->header_size);
532
533                 hpsb_packet_sent(host, packet, packet->expect_response ? ACK_PENDING : ACK_COMPLETE);
534                 hpsb_packet_received(host, data, size, 0);
535
536                 kfree(data);
537
538                 return 0;
539         }
540
541         if (packet->type == hpsb_async && packet->node_id != ALL_NODES) {
542                 packet->speed_code =
543                         host->speed_map[NODEID_TO_NODE(host->node_id) * 64
544                                        + NODEID_TO_NODE(packet->node_id)];
545         }
546
547 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
548         switch (packet->speed_code) {
549         case 2:
550                 dump_packet("send packet 400:", packet->header,
551                             packet->header_size);
552                 break;
553         case 1:
554                 dump_packet("send packet 200:", packet->header,
555                             packet->header_size);
556                 break;
557         default:
558                 dump_packet("send packet 100:", packet->header,
559                             packet->header_size);
560         }
561 #endif
562
563         return host->driver->transmit_packet(host, packet);
564 }
565
566 /* We could just use complete() directly as the packet complete
567  * callback, but this is more typesafe, in the sense that we get a
568  * compiler error if the prototype for complete() changes. */
569
570 static void complete_packet(void *data)
571 {
572         complete((struct completion *) data);
573 }
574
575 int hpsb_send_packet_and_wait(struct hpsb_packet *packet)
576 {
577         struct completion done;
578         int retval;
579
580         init_completion(&done);
581         hpsb_set_packet_complete_task(packet, complete_packet, &done);
582         retval = hpsb_send_packet(packet);
583         if (retval == 0)
584                 wait_for_completion(&done);
585
586         return retval;
587 }
588
589 static void send_packet_nocare(struct hpsb_packet *packet)
590 {
591         if (hpsb_send_packet(packet) < 0) {
592                 hpsb_free_packet(packet);
593         }
594 }
595
596
597 static void handle_packet_response(struct hpsb_host *host, int tcode,
598                                    quadlet_t *data, size_t size)
599 {
600         struct hpsb_packet *packet = NULL;
601         struct sk_buff *skb;
602         int tcode_match = 0;
603         int tlabel;
604         unsigned long flags;
605
606         tlabel = (data[0] >> 10) & 0x3f;
607
608         spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
609
610         skb_queue_walk(&host->pending_packet_queue, skb) {
611                 packet = (struct hpsb_packet *)skb->data;
612                 if ((packet->tlabel == tlabel)
613                     && (packet->node_id == (data[1] >> 16))){
614                         break;
615                 }
616
617                 packet = NULL;
618         }
619
620         if (packet == NULL) {
621                 HPSB_DEBUG("unsolicited response packet received - no tlabel match");
622                 dump_packet("contents:", data, 16);
623                 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
624                 return;
625         }
626
627         switch (packet->tcode) {
628         case TCODE_WRITEQ:
629         case TCODE_WRITEB:
630                 if (tcode != TCODE_WRITE_RESPONSE)
631                         break;
632                 tcode_match = 1;
633                 memcpy(packet->header, data, 12);
634                 break;
635         case TCODE_READQ:
636                 if (tcode != TCODE_READQ_RESPONSE)
637                         break;
638                 tcode_match = 1;
639                 memcpy(packet->header, data, 16);
640                 break;
641         case TCODE_READB:
642                 if (tcode != TCODE_READB_RESPONSE)
643                         break;
644                 tcode_match = 1;
645                 BUG_ON(packet->skb->len - sizeof(*packet) < size - 16);
646                 memcpy(packet->header, data, 16);
647                 memcpy(packet->data, data + 4, size - 16);
648                 break;
649         case TCODE_LOCK_REQUEST:
650                 if (tcode != TCODE_LOCK_RESPONSE)
651                         break;
652                 tcode_match = 1;
653                 size = min((size - 16), (size_t)8);
654                 BUG_ON(packet->skb->len - sizeof(*packet) < size);
655                 memcpy(packet->header, data, 16);
656                 memcpy(packet->data, data + 4, size);
657                 break;
658         }
659
660         if (!tcode_match) {
661                 HPSB_INFO("unsolicited response packet received - tcode mismatch");
662                 dump_packet("contents:", data, 16);
663                 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
664                 return;
665         }
666
667         __skb_unlink(skb, skb->list);
668         spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
669
670         if (packet->state == hpsb_queued) {
671                 packet->sendtime = jiffies;
672                 packet->ack_code = ACK_PENDING;
673         }
674
675         packet->state = hpsb_complete;
676         queue_packet_complete(packet);
677 }
678
679
680 static struct hpsb_packet *create_reply_packet(struct hpsb_host *host,
681                                                quadlet_t *data, size_t dsize)
682 {
683         struct hpsb_packet *p;
684
685         p = hpsb_alloc_packet(dsize);
686         if (unlikely(p == NULL)) {
687                 /* FIXME - send data_error response */
688                 return NULL;
689         }
690
691         p->type = hpsb_async;
692         p->state = hpsb_unused;
693         p->host = host;
694         p->node_id = data[1] >> 16;
695         p->tlabel = (data[0] >> 10) & 0x3f;
696         p->no_waiter = 1;
697
698         p->generation = get_hpsb_generation(host);
699
700         if (dsize % 4)
701                 p->data[dsize / 4] = 0;
702
703         return p;
704 }
705
706 #define PREP_ASYNC_HEAD_RCODE(tc) \
707         packet->tcode = tc; \
708         packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
709                 | (1 << 8) | (tc << 4); \
710         packet->header[1] = (packet->host->node_id << 16) | (rcode << 12); \
711         packet->header[2] = 0
712
713 static void fill_async_readquad_resp(struct hpsb_packet *packet, int rcode,
714                               quadlet_t data)
715 {
716         PREP_ASYNC_HEAD_RCODE(TCODE_READQ_RESPONSE);
717         packet->header[3] = data;
718         packet->header_size = 16;
719         packet->data_size = 0;
720 }
721
722 static void fill_async_readblock_resp(struct hpsb_packet *packet, int rcode,
723                                int length)
724 {
725         if (rcode != RCODE_COMPLETE)
726                 length = 0;
727
728         PREP_ASYNC_HEAD_RCODE(TCODE_READB_RESPONSE);
729         packet->header[3] = length << 16;
730         packet->header_size = 16;
731         packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
732 }
733
734 static void fill_async_write_resp(struct hpsb_packet *packet, int rcode)
735 {
736         PREP_ASYNC_HEAD_RCODE(TCODE_WRITE_RESPONSE);
737         packet->header[2] = 0;
738         packet->header_size = 12;
739         packet->data_size = 0;
740 }
741
742 static void fill_async_lock_resp(struct hpsb_packet *packet, int rcode, int extcode,
743                           int length)
744 {
745         if (rcode != RCODE_COMPLETE)
746                 length = 0;
747
748         PREP_ASYNC_HEAD_RCODE(TCODE_LOCK_RESPONSE);
749         packet->header[3] = (length << 16) | extcode;
750         packet->header_size = 16;
751         packet->data_size = length;
752 }
753
754 #define PREP_REPLY_PACKET(length) \
755                 packet = create_reply_packet(host, data, length); \
756                 if (packet == NULL) break
757
758 static void handle_incoming_packet(struct hpsb_host *host, int tcode,
759                                    quadlet_t *data, size_t size, int write_acked)
760 {
761         struct hpsb_packet *packet;
762         int length, rcode, extcode;
763         quadlet_t buffer;
764         nodeid_t source = data[1] >> 16;
765         nodeid_t dest = data[0] >> 16;
766         u16 flags = (u16) data[0];
767         u64 addr;
768
769         /* big FIXME - no error checking is done for an out of bounds length */
770
771         switch (tcode) {
772         case TCODE_WRITEQ:
773                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
774                 rcode = highlevel_write(host, source, dest, data+3,
775                                         addr, 4, flags);
776
777                 if (!write_acked
778                     && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
779                     && (rcode >= 0)) {
780                         /* not a broadcast write, reply */
781                         PREP_REPLY_PACKET(0);
782                         fill_async_write_resp(packet, rcode);
783                         send_packet_nocare(packet);
784                 }
785                 break;
786
787         case TCODE_WRITEB:
788                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
789                 rcode = highlevel_write(host, source, dest, data+4,
790                                         addr, data[3]>>16, flags);
791
792                 if (!write_acked
793                     && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
794                     && (rcode >= 0)) {
795                         /* not a broadcast write, reply */
796                         PREP_REPLY_PACKET(0);
797                         fill_async_write_resp(packet, rcode);
798                         send_packet_nocare(packet);
799                 }
800                 break;
801
802         case TCODE_READQ:
803                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
804                 rcode = highlevel_read(host, source, &buffer, addr, 4, flags);
805
806                 if (rcode >= 0) {
807                         PREP_REPLY_PACKET(0);
808                         fill_async_readquad_resp(packet, rcode, buffer);
809                         send_packet_nocare(packet);
810                 }
811                 break;
812
813         case TCODE_READB:
814                 length = data[3] >> 16;
815                 PREP_REPLY_PACKET(length);
816
817                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
818                 rcode = highlevel_read(host, source, packet->data, addr,
819                                        length, flags);
820
821                 if (rcode >= 0) {
822                         fill_async_readblock_resp(packet, rcode, length);
823                         send_packet_nocare(packet);
824                 } else {
825                         hpsb_free_packet(packet);
826                 }
827                 break;
828
829         case TCODE_LOCK_REQUEST:
830                 length = data[3] >> 16;
831                 extcode = data[3] & 0xffff;
832                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
833
834                 PREP_REPLY_PACKET(8);
835
836                 if ((extcode == 0) || (extcode >= 7)) {
837                         /* let switch default handle error */
838                         length = 0;
839                 }
840
841                 switch (length) {
842                 case 4:
843                         rcode = highlevel_lock(host, source, packet->data, addr,
844                                                data[4], 0, extcode,flags);
845                         fill_async_lock_resp(packet, rcode, extcode, 4);
846                         break;
847                 case 8:
848                         if ((extcode != EXTCODE_FETCH_ADD)
849                             && (extcode != EXTCODE_LITTLE_ADD)) {
850                                 rcode = highlevel_lock(host, source,
851                                                        packet->data, addr,
852                                                        data[5], data[4],
853                                                        extcode, flags);
854                                 fill_async_lock_resp(packet, rcode, extcode, 4);
855                         } else {
856                                 rcode = highlevel_lock64(host, source,
857                                              (octlet_t *)packet->data, addr,
858                                              *(octlet_t *)(data + 4), 0ULL,
859                                              extcode, flags);
860                                 fill_async_lock_resp(packet, rcode, extcode, 8);
861                         }
862                         break;
863                 case 16:
864                         rcode = highlevel_lock64(host, source,
865                                                  (octlet_t *)packet->data, addr,
866                                                  *(octlet_t *)(data + 6),
867                                                  *(octlet_t *)(data + 4),
868                                                  extcode, flags);
869                         fill_async_lock_resp(packet, rcode, extcode, 8);
870                         break;
871                 default:
872                         rcode = RCODE_TYPE_ERROR;
873                         fill_async_lock_resp(packet, rcode,
874                                              extcode, 0);
875                 }
876
877                 if (rcode >= 0) {
878                         send_packet_nocare(packet);
879                 } else {
880                         hpsb_free_packet(packet);
881                 }
882                 break;
883         }
884
885 }
886 #undef PREP_REPLY_PACKET
887
888
889 void hpsb_packet_received(struct hpsb_host *host, quadlet_t *data, size_t size,
890                           int write_acked)
891 {
892         int tcode;
893
894         if (host->in_bus_reset) {
895                 HPSB_INFO("received packet during reset; ignoring");
896                 return;
897         }
898
899         dump_packet("received packet:", data, size);
900
901         tcode = (data[0] >> 4) & 0xf;
902
903         switch (tcode) {
904         case TCODE_WRITE_RESPONSE:
905         case TCODE_READQ_RESPONSE:
906         case TCODE_READB_RESPONSE:
907         case TCODE_LOCK_RESPONSE:
908                 handle_packet_response(host, tcode, data, size);
909                 break;
910
911         case TCODE_WRITEQ:
912         case TCODE_WRITEB:
913         case TCODE_READQ:
914         case TCODE_READB:
915         case TCODE_LOCK_REQUEST:
916                 handle_incoming_packet(host, tcode, data, size, write_acked);
917                 break;
918
919
920         case TCODE_ISO_DATA:
921                 highlevel_iso_receive(host, data, size);
922                 break;
923
924         case TCODE_CYCLE_START:
925                 /* simply ignore this packet if it is passed on */
926                 break;
927
928         default:
929                 HPSB_NOTICE("received packet with bogus transaction code %d",
930                             tcode);
931                 break;
932         }
933 }
934
935
936 void abort_requests(struct hpsb_host *host)
937 {
938         struct hpsb_packet *packet;
939         struct sk_buff *skb;
940
941         host->driver->devctl(host, CANCEL_REQUESTS, 0);
942
943         while ((skb = skb_dequeue(&host->pending_packet_queue)) != NULL) {
944                 packet = (struct hpsb_packet *)skb->data;
945
946                 packet->state = hpsb_complete;
947                 packet->ack_code = ACKX_ABORTED;
948                 queue_packet_complete(packet);
949         }
950 }
951
952 void abort_timedouts(unsigned long __opaque)
953 {
954         struct hpsb_host *host = (struct hpsb_host *)__opaque;
955         unsigned long flags;
956         struct hpsb_packet *packet;
957         struct sk_buff *skb;
958         unsigned long expire;
959
960         spin_lock_irqsave(&host->csr.lock, flags);
961         expire = host->csr.expire;
962         spin_unlock_irqrestore(&host->csr.lock, flags);
963
964         /* Hold the lock around this, since we aren't dequeuing all
965          * packets, just ones we need. */
966         spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
967
968         while (!skb_queue_empty(&host->pending_packet_queue)) {
969                 skb = skb_peek(&host->pending_packet_queue);
970
971                 packet = (struct hpsb_packet *)skb->data;
972
973                 if (time_before(packet->sendtime + expire, jiffies)) {
974                         __skb_unlink(skb, skb->list);
975                         packet->state = hpsb_complete;
976                         packet->ack_code = ACKX_TIMEOUT;
977                         queue_packet_complete(packet);
978                 } else {
979                         /* Since packets are added to the tail, the oldest
980                          * ones are first, always. When we get to one that
981                          * isn't timed out, the rest aren't either. */
982                         break;
983                 }
984         }
985
986         if (!skb_queue_empty(&host->pending_packet_queue))
987                 mod_timer(&host->timeout, jiffies + host->timeout_interval);
988
989         spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
990 }
991
992
993 /* Kernel thread and vars, which handles packets that are completed. Only
994  * packets that have a "complete" function are sent here. This way, the
995  * completion is run out of kernel context, and doesn't block the rest of
996  * the stack. */
997 static int khpsbpkt_pid = -1, khpsbpkt_kill;
998 static DECLARE_COMPLETION(khpsbpkt_complete);
999 struct sk_buff_head hpsbpkt_queue;
1000 static DECLARE_MUTEX_LOCKED(khpsbpkt_sig);
1001
1002
1003 static void queue_packet_complete(struct hpsb_packet *packet)
1004 {
1005         if (packet->complete_routine != NULL) {
1006                 skb_queue_tail(&hpsbpkt_queue, packet->skb);
1007
1008                 /* Signal the kernel thread to handle this */
1009                 up(&khpsbpkt_sig);
1010         }
1011         return;
1012 }
1013
1014 static int hpsbpkt_thread(void *__hi)
1015 {
1016         struct sk_buff *skb;
1017         struct hpsb_packet *packet;
1018         void (*complete_routine)(void*);
1019         void *complete_data;
1020
1021         daemonize("khpsbpkt");
1022
1023         while (!down_interruptible(&khpsbpkt_sig)) {
1024                 if (khpsbpkt_kill)
1025                         break;
1026
1027                 while ((skb = skb_dequeue(&hpsbpkt_queue)) != NULL) {
1028                         packet = (struct hpsb_packet *)skb->data;
1029
1030                         complete_routine = packet->complete_routine;
1031                         complete_data = packet->complete_data;
1032
1033                         packet->complete_routine = packet->complete_data = NULL;
1034
1035                         complete_routine(complete_data);
1036                 }
1037         }
1038
1039         complete_and_exit(&khpsbpkt_complete, 0);
1040 }
1041
1042
1043 static int __init ieee1394_init(void)
1044 {
1045         int i;
1046
1047         skb_queue_head_init(&hpsbpkt_queue);
1048
1049         if (hpsb_init_config_roms()) {
1050                 HPSB_ERR("Failed to initialize some config rom entries.\n");
1051                 HPSB_ERR("Some features may not be available\n");
1052         }
1053
1054         khpsbpkt_pid = kernel_thread(hpsbpkt_thread, NULL, CLONE_KERNEL);
1055         if (khpsbpkt_pid < 0) {
1056                 HPSB_ERR("Failed to start hpsbpkt thread!\n");
1057                 return -ENOMEM;
1058         }
1059
1060         devfs_mk_dir("ieee1394");
1061
1062         if (register_chrdev_region(IEEE1394_CORE_DEV, 256, "ieee1394")) {
1063                 HPSB_ERR("unable to register character device major %d!\n", IEEE1394_MAJOR);
1064                 return -ENODEV;
1065         }
1066
1067         devfs_mk_dir("ieee1394");
1068
1069         bus_register(&ieee1394_bus_type);
1070         for (i = 0; fw_bus_attrs[i]; i++)
1071                 bus_create_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1072         class_register(&hpsb_host_class);
1073
1074         if (init_csr())
1075                 return -ENOMEM;
1076
1077         if (!disable_nodemgr)
1078                 init_ieee1394_nodemgr();
1079         else
1080                 HPSB_INFO("nodemgr functionality disabled");
1081
1082         return 0;
1083 }
1084
1085 static void __exit ieee1394_cleanup(void)
1086 {
1087         int i;
1088
1089         if (!disable_nodemgr)
1090                 cleanup_ieee1394_nodemgr();
1091
1092         cleanup_csr();
1093
1094         class_unregister(&hpsb_host_class);
1095         for (i = 0; fw_bus_attrs[i]; i++)
1096                 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1097         bus_unregister(&ieee1394_bus_type);
1098
1099         if (khpsbpkt_pid >= 0) {
1100                 khpsbpkt_kill = 1;
1101                 mb();
1102                 up(&khpsbpkt_sig);
1103                 wait_for_completion(&khpsbpkt_complete);
1104         }
1105
1106         hpsb_cleanup_config_roms();
1107
1108         unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
1109         devfs_remove("ieee1394");
1110 }
1111
1112 module_init(ieee1394_init);
1113 module_exit(ieee1394_cleanup);
1114
1115 /* Exported symbols */
1116
1117 /** hosts.c **/
1118 EXPORT_SYMBOL(hpsb_alloc_host);
1119 EXPORT_SYMBOL(hpsb_add_host);
1120 EXPORT_SYMBOL(hpsb_remove_host);
1121 EXPORT_SYMBOL(hpsb_update_config_rom_image);
1122
1123 /** ieee1394_core.c **/
1124 EXPORT_SYMBOL(hpsb_speedto_str);
1125 EXPORT_SYMBOL(hpsb_set_packet_complete_task);
1126 EXPORT_SYMBOL(hpsb_alloc_packet);
1127 EXPORT_SYMBOL(hpsb_free_packet);
1128 EXPORT_SYMBOL(hpsb_send_phy_config);
1129 EXPORT_SYMBOL(hpsb_send_packet);
1130 EXPORT_SYMBOL(hpsb_send_packet_and_wait);
1131 EXPORT_SYMBOL(hpsb_reset_bus);
1132 EXPORT_SYMBOL(hpsb_bus_reset);
1133 EXPORT_SYMBOL(hpsb_selfid_received);
1134 EXPORT_SYMBOL(hpsb_selfid_complete);
1135 EXPORT_SYMBOL(hpsb_packet_sent);
1136 EXPORT_SYMBOL(hpsb_packet_received);
1137
1138 /** ieee1394_transactions.c **/
1139 EXPORT_SYMBOL(hpsb_get_tlabel);
1140 EXPORT_SYMBOL(hpsb_free_tlabel);
1141 EXPORT_SYMBOL(hpsb_make_readpacket);
1142 EXPORT_SYMBOL(hpsb_make_writepacket);
1143 EXPORT_SYMBOL(hpsb_make_streampacket);
1144 EXPORT_SYMBOL(hpsb_make_lockpacket);
1145 EXPORT_SYMBOL(hpsb_make_lock64packet);
1146 EXPORT_SYMBOL(hpsb_make_phypacket);
1147 EXPORT_SYMBOL(hpsb_make_isopacket);
1148 EXPORT_SYMBOL(hpsb_read);
1149 EXPORT_SYMBOL(hpsb_write);
1150 EXPORT_SYMBOL(hpsb_lock);
1151 EXPORT_SYMBOL(hpsb_lock64);
1152 EXPORT_SYMBOL(hpsb_send_gasp);
1153 EXPORT_SYMBOL(hpsb_packet_success);
1154
1155 /** highlevel.c **/
1156 EXPORT_SYMBOL(hpsb_register_highlevel);
1157 EXPORT_SYMBOL(hpsb_unregister_highlevel);
1158 EXPORT_SYMBOL(hpsb_register_addrspace);
1159 EXPORT_SYMBOL(hpsb_unregister_addrspace);
1160 EXPORT_SYMBOL(hpsb_allocate_and_register_addrspace);
1161 EXPORT_SYMBOL(hpsb_listen_channel);
1162 EXPORT_SYMBOL(hpsb_unlisten_channel);
1163 EXPORT_SYMBOL(hpsb_get_hostinfo);
1164 EXPORT_SYMBOL(hpsb_get_host_bykey);
1165 EXPORT_SYMBOL(hpsb_create_hostinfo);
1166 EXPORT_SYMBOL(hpsb_destroy_hostinfo);
1167 EXPORT_SYMBOL(hpsb_set_hostinfo_key);
1168 EXPORT_SYMBOL(hpsb_get_hostinfo_key);
1169 EXPORT_SYMBOL(hpsb_get_hostinfo_bykey);
1170 EXPORT_SYMBOL(hpsb_set_hostinfo);
1171 EXPORT_SYMBOL(highlevel_read);
1172 EXPORT_SYMBOL(highlevel_write);
1173 EXPORT_SYMBOL(highlevel_lock);
1174 EXPORT_SYMBOL(highlevel_lock64);
1175 EXPORT_SYMBOL(highlevel_add_host);
1176 EXPORT_SYMBOL(highlevel_remove_host);
1177 EXPORT_SYMBOL(highlevel_host_reset);
1178
1179 /** nodemgr.c **/
1180 EXPORT_SYMBOL(hpsb_guid_get_entry);
1181 EXPORT_SYMBOL(hpsb_nodeid_get_entry);
1182 EXPORT_SYMBOL(hpsb_node_fill_packet);
1183 EXPORT_SYMBOL(hpsb_node_read);
1184 EXPORT_SYMBOL(hpsb_node_write);
1185 EXPORT_SYMBOL(hpsb_node_lock);
1186 EXPORT_SYMBOL(hpsb_register_protocol);
1187 EXPORT_SYMBOL(hpsb_unregister_protocol);
1188 EXPORT_SYMBOL(ieee1394_bus_type);
1189 EXPORT_SYMBOL(nodemgr_for_each_host);
1190
1191 /** csr.c **/
1192 EXPORT_SYMBOL(hpsb_update_config_rom);
1193
1194 /** dma.c **/
1195 EXPORT_SYMBOL(dma_prog_region_init);
1196 EXPORT_SYMBOL(dma_prog_region_alloc);
1197 EXPORT_SYMBOL(dma_prog_region_free);
1198 EXPORT_SYMBOL(dma_region_init);
1199 EXPORT_SYMBOL(dma_region_alloc);
1200 EXPORT_SYMBOL(dma_region_free);
1201 EXPORT_SYMBOL(dma_region_sync_for_cpu);
1202 EXPORT_SYMBOL(dma_region_sync_for_device);
1203 EXPORT_SYMBOL(dma_region_mmap);
1204 EXPORT_SYMBOL(dma_region_offset_to_bus);
1205
1206 /** iso.c **/
1207 EXPORT_SYMBOL(hpsb_iso_xmit_init);
1208 EXPORT_SYMBOL(hpsb_iso_recv_init);
1209 EXPORT_SYMBOL(hpsb_iso_xmit_start);
1210 EXPORT_SYMBOL(hpsb_iso_recv_start);
1211 EXPORT_SYMBOL(hpsb_iso_recv_listen_channel);
1212 EXPORT_SYMBOL(hpsb_iso_recv_unlisten_channel);
1213 EXPORT_SYMBOL(hpsb_iso_recv_set_channel_mask);
1214 EXPORT_SYMBOL(hpsb_iso_stop);
1215 EXPORT_SYMBOL(hpsb_iso_shutdown);
1216 EXPORT_SYMBOL(hpsb_iso_xmit_queue_packet);
1217 EXPORT_SYMBOL(hpsb_iso_xmit_sync);
1218 EXPORT_SYMBOL(hpsb_iso_recv_release_packets);
1219 EXPORT_SYMBOL(hpsb_iso_n_ready);
1220 EXPORT_SYMBOL(hpsb_iso_packet_sent);
1221 EXPORT_SYMBOL(hpsb_iso_packet_received);
1222 EXPORT_SYMBOL(hpsb_iso_wake);
1223 EXPORT_SYMBOL(hpsb_iso_recv_flush);
1224
1225 /** csr1212.c **/
1226 EXPORT_SYMBOL(csr1212_create_csr);
1227 EXPORT_SYMBOL(csr1212_init_local_csr);
1228 EXPORT_SYMBOL(csr1212_new_immediate);
1229 EXPORT_SYMBOL(csr1212_new_leaf);
1230 EXPORT_SYMBOL(csr1212_new_csr_offset);
1231 EXPORT_SYMBOL(csr1212_new_directory);
1232 EXPORT_SYMBOL(csr1212_associate_keyval);
1233 EXPORT_SYMBOL(csr1212_attach_keyval_to_directory);
1234 EXPORT_SYMBOL(csr1212_new_extended_immediate);
1235 EXPORT_SYMBOL(csr1212_new_extended_leaf);
1236 EXPORT_SYMBOL(csr1212_new_descriptor_leaf);
1237 EXPORT_SYMBOL(csr1212_new_textual_descriptor_leaf);
1238 EXPORT_SYMBOL(csr1212_new_string_descriptor_leaf);
1239 EXPORT_SYMBOL(csr1212_new_icon_descriptor_leaf);
1240 EXPORT_SYMBOL(csr1212_new_modifiable_descriptor_leaf);
1241 EXPORT_SYMBOL(csr1212_new_keyword_leaf);
1242 EXPORT_SYMBOL(csr1212_detach_keyval_from_directory);
1243 EXPORT_SYMBOL(csr1212_disassociate_keyval);
1244 EXPORT_SYMBOL(csr1212_release_keyval);
1245 EXPORT_SYMBOL(csr1212_destroy_csr);
1246 EXPORT_SYMBOL(csr1212_read);
1247 EXPORT_SYMBOL(csr1212_generate_positions);
1248 EXPORT_SYMBOL(csr1212_generate_layout_order);
1249 EXPORT_SYMBOL(csr1212_fill_cache);
1250 EXPORT_SYMBOL(csr1212_generate_csr_image);
1251 EXPORT_SYMBOL(csr1212_parse_keyval);
1252 EXPORT_SYMBOL(csr1212_parse_csr);
1253 EXPORT_SYMBOL(_csr1212_read_keyval);
1254 EXPORT_SYMBOL(_csr1212_destroy_keyval);