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