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