import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / ieee1394 / ieee1394_core.c
1 /*
2  * IEEE 1394 for Linux
3  *
4  * Core support: hpsb_packet management, packet handling and forwarding to
5  *               highlevel or lowlevel code
6  *
7  * Copyright (C) 1999, 2000 Andreas E. Bombe
8  *                     2002 Manfred Weihs <weihs@ict.tuwien.ac.at>
9  *
10  * This code is licensed under the GPL.  See the file COPYING in the root
11  * directory of the kernel sources for details.
12  *
13  *
14  * Contributions:
15  *
16  * Manfred Weihs <weihs@ict.tuwien.ac.at>
17  *        loopback functionality in hpsb_send_packet
18  *        allow highlevel drivers to disable automatic response generation
19  *              and to generate responses themselves (deferred)
20  *
21  */
22
23 #include <linux/config.h>
24 #include <linux/kernel.h>
25 #include <linux/list.h>
26 #include <linux/string.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/module.h>
31 #include <linux/proc_fs.h>
32 #include <asm/bitops.h>
33 #include <asm/byteorder.h>
34 #include <asm/semaphore.h>
35
36 #include "ieee1394_types.h"
37 #include "ieee1394.h"
38 #include "hosts.h"
39 #include "ieee1394_core.h"
40 #include "highlevel.h"
41 #include "ieee1394_transactions.h"
42 #include "csr.h"
43 #include "nodemgr.h"
44 #include "ieee1394_hotplug.h"
45
46 /*
47  * Disable the nodemgr detection and config rom reading functionality.
48  */
49 MODULE_PARM(disable_nodemgr, "i");
50 MODULE_PARM_DESC(disable_nodemgr, "Disable nodemgr functionality.");
51 static int disable_nodemgr = 0;
52
53 MODULE_PARM(disable_hotplug, "i");
54 MODULE_PARM_DESC(disable_hotplug, "Disable hotplug for detected nodes.");
55 static int disable_hotplug = 0;
56
57 /* We are GPL, so treat us special */
58 MODULE_LICENSE("GPL");
59
60 static kmem_cache_t *hpsb_packet_cache;
61
62 /* Some globals used */
63 const char *hpsb_speedto_str[] = { "S100", "S200", "S400" };
64
65 static void dump_packet(const char *text, quadlet_t *data, int size)
66 {
67         int i;
68
69         size /= 4;
70         size = (size > 4 ? 4 : size);
71
72         printk(KERN_DEBUG "ieee1394: %s", text);
73         for (i = 0; i < size; i++) {
74                 printk(" %8.8x", data[i]);
75         }
76         printk("\n");
77 }
78
79 static void process_complete_tasks(struct hpsb_packet *packet)
80 {
81         struct list_head *lh, *next;
82
83         list_for_each_safe(lh, next, &packet->complete_tq) {
84                 struct hpsb_queue_struct *tq =
85                         list_entry(lh, struct hpsb_queue_struct, hpsb_queue_list);
86                 list_del(&tq->hpsb_queue_list);
87                 hpsb_schedule_work(tq);
88         }
89
90         return;
91 }
92
93 /**
94  * hpsb_add_packet_complete_task - add a new task for when a packet completes
95  * @packet: the packet whose completion we want the task added to
96  * @tq: the hpsb_queue_struct describing the task to add
97  */
98 void hpsb_add_packet_complete_task(struct hpsb_packet *packet, struct hpsb_queue_struct *tq)
99 {
100         list_add_tail(&tq->hpsb_queue_list, &packet->complete_tq);
101         return;
102 }
103
104 /**
105  * alloc_hpsb_packet - allocate new packet structure
106  * @data_size: size of the data block to be allocated
107  *
108  * This function allocates, initializes and returns a new &struct hpsb_packet.
109  * It can be used in interrupt context.  A header block is always included, its
110  * size is big enough to contain all possible 1394 headers.  The data block is
111  * only allocated when @data_size is not zero.
112  *
113  * For packets for which responses will be received the @data_size has to be big
114  * enough to contain the response's data block since no further allocation
115  * occurs at response matching time.
116  *
117  * The packet's generation value will be set to the current generation number
118  * for ease of use.  Remember to overwrite it with your own recorded generation
119  * number if you can not be sure that your code will not race with a bus reset.
120  *
121  * Return value: A pointer to a &struct hpsb_packet or NULL on allocation
122  * failure.
123  */
124 struct hpsb_packet *alloc_hpsb_packet(size_t data_size)
125 {
126         struct hpsb_packet *packet = NULL;
127         void *data = NULL;
128         int kmflags = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
129
130         packet = kmem_cache_alloc(hpsb_packet_cache, kmflags);
131         if (packet == NULL)
132                 return NULL;
133
134         memset(packet, 0, sizeof(struct hpsb_packet));
135         packet->header = packet->embedded_header;
136
137         if (data_size) {
138                 data = kmalloc(data_size + 8, kmflags);
139                 if (data == NULL) {
140                         kmem_cache_free(hpsb_packet_cache, packet);
141                         return NULL;
142                 }
143
144                 packet->data = data;
145                 packet->data_size = data_size;
146         }
147
148         INIT_LIST_HEAD(&packet->complete_tq);
149         INIT_LIST_HEAD(&packet->list);
150         sema_init(&packet->state_change, 0);
151         packet->state = hpsb_unused;
152         packet->generation = -1;
153         packet->data_be = 1;
154
155         return packet;
156 }
157
158
159 /**
160  * free_hpsb_packet - free packet and data associated with it
161  * @packet: packet to free (is NULL safe)
162  *
163  * This function will free packet->data, packet->header and finally the packet
164  * itself.
165  */
166 void free_hpsb_packet(struct hpsb_packet *packet)
167 {
168         if (!packet) return;
169
170         kfree(packet->data);
171         kmem_cache_free(hpsb_packet_cache, packet);
172 }
173
174
175 int hpsb_reset_bus(struct hpsb_host *host, int type)
176 {
177         if (!host->in_bus_reset) {
178                 host->driver->devctl(host, RESET_BUS, type);
179                 return 0;
180         } else {
181                 return 1;
182         }
183 }
184
185
186 int hpsb_bus_reset(struct hpsb_host *host)
187 {
188         if (host->in_bus_reset) {
189                 HPSB_NOTICE("%s called while bus reset already in progress",
190                             __FUNCTION__);
191                 return 1;
192         }
193
194         abort_requests(host);
195         host->in_bus_reset = 1;
196         host->irm_id = -1;
197         host->is_irm = 0;
198         host->busmgr_id = -1;
199         host->is_busmgr = 0;
200         host->is_cycmst = 0;
201         host->node_count = 0;
202         host->selfid_count = 0;
203
204         return 0;
205 }
206
207
208 /*
209  * Verify num_of_selfids SelfIDs and return number of nodes.  Return zero in
210  * case verification failed.
211  */
212 static int check_selfids(struct hpsb_host *host)
213 {
214         int nodeid = -1;
215         int rest_of_selfids = host->selfid_count;
216         struct selfid *sid = (struct selfid *)host->topology_map;
217         struct ext_selfid *esid;
218         int esid_seq = 23;
219
220         host->nodes_active = 0;
221
222         while (rest_of_selfids--) {
223                 if (!sid->extended) {
224                         nodeid++;
225                         esid_seq = 0;
226                         
227                         if (sid->phy_id != nodeid) {
228                                 HPSB_INFO("SelfIDs failed monotony check with "
229                                           "%d", sid->phy_id);
230                                 return 0;
231                         }
232                         
233                         if (sid->link_active) {
234                                 host->nodes_active++;
235                                 if (sid->contender)
236                                         host->irm_id = LOCAL_BUS | sid->phy_id;
237                         }
238                 } else {
239                         esid = (struct ext_selfid *)sid;
240
241                         if ((esid->phy_id != nodeid) 
242                             || (esid->seq_nr != esid_seq)) {
243                                 HPSB_INFO("SelfIDs failed monotony check with "
244                                           "%d/%d", esid->phy_id, esid->seq_nr);
245                                 return 0;
246                         }
247                         esid_seq++;
248                 }
249                 sid++;
250         }
251         
252         esid = (struct ext_selfid *)(sid - 1);
253         while (esid->extended) {
254                 if ((esid->porta == 0x2) || (esid->portb == 0x2)
255                     || (esid->portc == 0x2) || (esid->portd == 0x2)
256                     || (esid->porte == 0x2) || (esid->portf == 0x2)
257                     || (esid->portg == 0x2) || (esid->porth == 0x2)) {
258                                 HPSB_INFO("SelfIDs failed root check on "
259                                           "extended SelfID");
260                                 return 0;
261                 }
262                 esid--;
263         }
264
265         sid = (struct selfid *)esid;
266         if ((sid->port0 == 0x2) || (sid->port1 == 0x2) || (sid->port2 == 0x2)) {
267                         HPSB_INFO("SelfIDs failed root check");
268                         return 0;
269         }
270
271         host->node_count = nodeid + 1;
272         return 1;
273 }
274
275 static void build_speed_map(struct hpsb_host *host, int nodecount)
276 {
277         char speedcap[nodecount];
278         char cldcnt[nodecount];
279         u8 *map = host->speed_map;
280         struct selfid *sid;
281         struct ext_selfid *esid;
282         int i, j, n;
283
284         for (i = 0; i < (nodecount * 64); i += 64) {
285                 for (j = 0; j < nodecount; j++) {
286                         map[i+j] = SPEED_400;
287                 }
288         }
289
290         for (i = 0; i < nodecount; i++) {
291                 cldcnt[i] = 0;
292         }
293
294         /* find direct children count and speed */
295         for (sid = (struct selfid *)&host->topology_map[host->selfid_count-1],
296                      n = nodecount - 1;
297              (void *)sid >= (void *)host->topology_map; sid--) {
298                 if (sid->extended) {
299                         esid = (struct ext_selfid *)sid;
300
301                         if (esid->porta == 0x3) cldcnt[n]++;
302                         if (esid->portb == 0x3) cldcnt[n]++;
303                         if (esid->portc == 0x3) cldcnt[n]++;
304                         if (esid->portd == 0x3) cldcnt[n]++;
305                         if (esid->porte == 0x3) cldcnt[n]++;
306                         if (esid->portf == 0x3) cldcnt[n]++;
307                         if (esid->portg == 0x3) cldcnt[n]++;
308                         if (esid->porth == 0x3) cldcnt[n]++;
309                 } else {
310                         if (sid->port0 == 0x3) cldcnt[n]++;
311                         if (sid->port1 == 0x3) cldcnt[n]++;
312                         if (sid->port2 == 0x3) cldcnt[n]++;
313
314                         speedcap[n] = sid->speed;
315                         n--;
316                 }
317         }
318
319         /* set self mapping */
320         for (i = 0; i < nodecount; i++) {
321                 map[64*i + i] = speedcap[i];
322         }
323
324         /* fix up direct children count to total children count;
325          * also fix up speedcaps for sibling and parent communication */
326         for (i = 1; i < nodecount; i++) {
327                 for (j = cldcnt[i], n = i - 1; j > 0; j--) {
328                         cldcnt[i] += cldcnt[n];
329                         speedcap[n] = MIN(speedcap[n], speedcap[i]);
330                         n -= cldcnt[n] + 1;
331                 }
332         }
333
334         for (n = 0; n < nodecount; n++) {
335                 for (i = n - cldcnt[n]; i <= n; i++) {
336                         for (j = 0; j < (n - cldcnt[n]); j++) {
337                                 map[j*64 + i] = map[i*64 + j] =
338                                         MIN(map[i*64 + j], speedcap[n]);
339                         }
340                         for (j = n + 1; j < nodecount; j++) {
341                                 map[j*64 + i] = map[i*64 + j] =
342                                         MIN(map[i*64 + j], speedcap[n]);
343                         }
344                 }
345         }
346 }
347
348
349 void hpsb_selfid_received(struct hpsb_host *host, quadlet_t sid)
350 {
351         if (host->in_bus_reset) {
352 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
353                 HPSB_INFO("Including SelfID 0x%x", sid);
354 #endif
355                 host->topology_map[host->selfid_count++] = sid;
356         } else {
357                 HPSB_NOTICE("Spurious SelfID packet (0x%08x) received from bus %d",
358                             sid, (host->node_id & BUS_MASK) >> 6);
359         }
360 }
361
362 void hpsb_selfid_complete(struct hpsb_host *host, int phyid, int isroot)
363 {
364         if (!host->in_bus_reset)
365                 HPSB_NOTICE("SelfID completion called outside of bus reset!");
366
367         host->node_id = LOCAL_BUS | phyid;
368         host->is_root = isroot;
369
370         if (!check_selfids(host)) {
371                 if (host->reset_retries++ < 20) {
372                         /* selfid stage did not complete without error */
373                         HPSB_NOTICE("Error in SelfID stage, resetting");
374                         host->in_bus_reset = 0;
375                         hpsb_reset_bus(host, LONG_RESET);
376                         return;
377                 } else {
378                         HPSB_NOTICE("Stopping out-of-control reset loop");
379                         HPSB_NOTICE("Warning - topology map and speed map will not be valid");
380                 }
381         } else {
382                 build_speed_map(host, host->node_count);
383         }
384
385 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
386         HPSB_INFO("selfid_complete called with successful SelfID stage "
387                 "... irm_id: 0x%X node_id: 0x%X",host->irm_id,host->node_id);
388 #endif
389         /* irm_id is kept up to date by check_selfids() */
390         if (host->irm_id == host->node_id) {
391                 host->is_irm = 1;
392                 host->is_busmgr = 1;
393                 host->busmgr_id = host->node_id;
394                 host->csr.bus_manager_id = host->node_id;
395         } else {
396                 host->is_busmgr = 0;
397                 host->is_irm = 0;
398         }
399
400         host->reset_retries = 0;
401         if (isroot) {
402                 host->driver->devctl(host, ACT_CYCLE_MASTER, 1);
403                 host->is_cycmst = 1;
404         }
405         atomic_inc(&host->generation);
406         host->in_bus_reset = 0;
407         highlevel_host_reset(host);
408 }
409
410
411 void hpsb_packet_sent(struct hpsb_host *host, struct hpsb_packet *packet, 
412                       int ackcode)
413 {
414         unsigned long flags;
415
416         packet->ack_code = ackcode;
417
418         if (packet->no_waiter) {
419                 /* must not have a tlabel allocated */
420                 free_hpsb_packet(packet);
421                 return;
422         }
423
424         if (ackcode != ACK_PENDING || !packet->expect_response) {
425                 packet->state = hpsb_complete;
426                 up(&packet->state_change);
427                 up(&packet->state_change);
428                 process_complete_tasks(packet);
429                 return;
430         }
431
432         packet->state = hpsb_pending;
433         packet->sendtime = jiffies;
434
435         spin_lock_irqsave(&host->pending_pkt_lock, flags);
436         list_add_tail(&packet->list, &host->pending_packets);
437         spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
438
439         up(&packet->state_change);
440         hpsb_schedule_work(&host->timeout_tq);
441 }
442
443 /**
444  * hpsb_send_packet - transmit a packet on the bus
445  * @packet: packet to send
446  *
447  * The packet is sent through the host specified in the packet->host field.
448  * Before sending, the packet's transmit speed is automatically determined using
449  * the local speed map when it is an async, non-broadcast packet.
450  *
451  * Possibilities for failure are that host is either not initialized, in bus
452  * reset, the packet's generation number doesn't match the current generation
453  * number or the host reports a transmit error.
454  *
455  * Return value: False (0) on failure, true (1) otherwise.
456  */
457 int hpsb_send_packet(struct hpsb_packet *packet)
458 {
459         struct hpsb_host *host = packet->host;
460
461         if (host->is_shutdown || host->in_bus_reset
462             || (packet->generation != get_hpsb_generation(host))) {
463                 return 0;
464         }
465
466         packet->state = hpsb_queued;
467
468         if (packet->node_id == host->node_id)
469         { /* it is a local request, so handle it locally */
470                 quadlet_t *data;
471                 size_t size=packet->data_size+packet->header_size;
472
473                 int kmflags = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
474                 data = kmalloc(packet->header_size + packet->data_size, kmflags);
475                 if (!data) {
476                         HPSB_ERR("unable to allocate memory for concatenating header and data");
477                         return 0;
478                 }
479
480                 memcpy(data, packet->header, packet->header_size);
481
482                 if (packet->data_size)
483                 {
484                         if (packet->data_be) {
485                                 memcpy(((u8*)data)+packet->header_size, packet->data, packet->data_size);
486                         } else {
487                                 int i;
488                                 quadlet_t *my_data=(quadlet_t*) ((u8*) data + packet->data_size);
489                                 for (i=0; i < packet->data_size/4; i++) {
490                                         my_data[i] = cpu_to_be32(packet->data[i]);
491                                 }
492                         }
493                 }
494
495 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
496                 dump_packet("send packet local:", packet->header,
497                             packet->header_size);
498 #endif
499                 hpsb_packet_sent(host, packet,  packet->expect_response?ACK_PENDING:ACK_COMPLETE);
500                 hpsb_packet_received(host, data, size, 0);
501
502                 kfree(data);
503
504                 return 1;
505         }
506
507         if (packet->type == hpsb_async && packet->node_id != ALL_NODES) {
508                 packet->speed_code =
509                         host->speed_map[(host->node_id & NODE_MASK) * 64
510                                        + (packet->node_id & NODE_MASK)];
511         }
512
513 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
514         switch (packet->speed_code) {
515         case 2:
516                 dump_packet("send packet 400:", packet->header,
517                             packet->header_size);
518                 break;
519         case 1:
520                 dump_packet("send packet 200:", packet->header,
521                             packet->header_size);
522                 break;
523         default:
524                 dump_packet("send packet 100:", packet->header,
525                             packet->header_size);
526         }
527 #endif
528
529         return host->driver->transmit_packet(host, packet);
530 }
531
532 static void send_packet_nocare(struct hpsb_packet *packet)
533 {
534         if (!hpsb_send_packet(packet)) {
535                 free_hpsb_packet(packet);
536         }
537 }
538
539
540 void handle_packet_response(struct hpsb_host *host, int tcode, quadlet_t *data,
541                             size_t size)
542 {
543         struct hpsb_packet *packet = NULL;
544         struct list_head *lh;
545         int tcode_match = 0;
546         int tlabel;
547         unsigned long flags;
548
549         tlabel = (data[0] >> 10) & 0x3f;
550
551         spin_lock_irqsave(&host->pending_pkt_lock, flags);
552
553         list_for_each(lh, &host->pending_packets) {
554                 packet = list_entry(lh, struct hpsb_packet, list);
555                 if ((packet->tlabel == tlabel)
556                     && (packet->node_id == (data[1] >> 16))){
557                         break;
558                 }
559         }
560
561         if (lh == &host->pending_packets) {
562                 HPSB_DEBUG("unsolicited response packet received - np");
563                 dump_packet("contents:", data, 16);
564                 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
565                 return;
566         }
567
568         switch (packet->tcode) {
569         case TCODE_WRITEQ:
570         case TCODE_WRITEB:
571                 if (tcode == TCODE_WRITE_RESPONSE) tcode_match = 1;
572                 break;
573         case TCODE_READQ:
574                 if (tcode == TCODE_READQ_RESPONSE) tcode_match = 1;
575                 break;
576         case TCODE_READB:
577                 if (tcode == TCODE_READB_RESPONSE) tcode_match = 1;
578                 break;
579         case TCODE_LOCK_REQUEST:
580                 if (tcode == TCODE_LOCK_RESPONSE) tcode_match = 1;
581                 break;
582         }
583
584         if (!tcode_match || (packet->tlabel != tlabel)
585             || (packet->node_id != (data[1] >> 16))) {
586                 HPSB_INFO("unsolicited response packet received");
587                 dump_packet("contents:", data, 16);
588
589                 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
590                 return;
591         }
592
593         list_del(&packet->list);
594
595         spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
596
597         /* FIXME - update size fields? */
598         switch (tcode) {
599         case TCODE_WRITE_RESPONSE:
600                 memcpy(packet->header, data, 12);
601                 break;
602         case TCODE_READQ_RESPONSE:
603                 memcpy(packet->header, data, 16);
604                 break;
605         case TCODE_READB_RESPONSE:
606                 memcpy(packet->header, data, 16);
607                 memcpy(packet->data, data + 4, size - 16);
608                 break;
609         case TCODE_LOCK_RESPONSE:
610                 memcpy(packet->header, data, 16);
611                 memcpy(packet->data, data + 4, (size - 16) > 8 ? 8 : size - 16);
612                 break;
613         }
614
615         packet->state = hpsb_complete;
616         up(&packet->state_change);
617         process_complete_tasks(packet);
618 }
619
620
621 static struct hpsb_packet *create_reply_packet(struct hpsb_host *host,
622                                                quadlet_t *data, size_t dsize)
623 {
624         struct hpsb_packet *p;
625
626         dsize += (dsize % 4 ? 4 - (dsize % 4) : 0);
627
628         p = alloc_hpsb_packet(dsize);
629         if (p == NULL) {
630                 /* FIXME - send data_error response */
631                 return NULL;
632         }
633
634         p->type = hpsb_async;
635         p->state = hpsb_unused;
636         p->host = host;
637         p->node_id = data[1] >> 16;
638         p->tlabel = (data[0] >> 10) & 0x3f;
639         p->no_waiter = 1;
640
641         p->generation = get_hpsb_generation(host);
642
643         if (dsize % 4) {
644                 p->data[dsize / 4] = 0;
645         }
646
647         return p;
648 }
649
650 #define PREP_REPLY_PACKET(length) \
651                 packet = create_reply_packet(host, data, length); \
652                 if (packet == NULL) break
653
654 static void handle_incoming_packet(struct hpsb_host *host, int tcode,
655                                    quadlet_t *data, size_t size, int write_acked)
656 {
657         struct hpsb_packet *packet;
658         int length, rcode, extcode;
659         quadlet_t buffer;
660         nodeid_t source = data[1] >> 16;
661         nodeid_t dest = data[0] >> 16;
662         u16 flags = (u16) data[0];
663         u64 addr;
664
665         /* big FIXME - no error checking is done for an out of bounds length */
666
667         switch (tcode) {
668         case TCODE_WRITEQ:
669                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
670                 rcode = highlevel_write(host, source, dest, data+3,
671                                         addr, 4, flags);
672
673                 if (!write_acked
674                     && (((data[0] >> 16) & NODE_MASK) != NODE_MASK)
675                     && (rcode >= 0)) {
676                         /* not a broadcast write, reply */
677                         PREP_REPLY_PACKET(0);
678                         fill_async_write_resp(packet, rcode);
679                         send_packet_nocare(packet);
680                 }
681                 break;
682
683         case TCODE_WRITEB:
684                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
685                 rcode = highlevel_write(host, source, dest, data+4,
686                                         addr, data[3]>>16, flags);
687
688                 if (!write_acked
689                     && (((data[0] >> 16) & NODE_MASK) != NODE_MASK)
690                     && (rcode >= 0)) {
691                         /* not a broadcast write, reply */
692                         PREP_REPLY_PACKET(0);
693                         fill_async_write_resp(packet, rcode);
694                         send_packet_nocare(packet);
695                 }
696                 break;
697
698         case TCODE_READQ:
699                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
700                 rcode = highlevel_read(host, source, &buffer, addr, 4, flags);
701
702                 if (rcode >= 0) {
703                         PREP_REPLY_PACKET(0);
704                         fill_async_readquad_resp(packet, rcode, buffer);
705                         send_packet_nocare(packet);
706                 }
707                 break;
708
709         case TCODE_READB:
710                 length = data[3] >> 16;
711                 PREP_REPLY_PACKET(length);
712
713                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
714                 rcode = highlevel_read(host, source, packet->data, addr,
715                                        length, flags);
716
717                 if (rcode >= 0) {
718                         fill_async_readblock_resp(packet, rcode, length);
719                         send_packet_nocare(packet);
720                 }
721                 break;
722
723         case TCODE_LOCK_REQUEST:
724                 length = data[3] >> 16;
725                 extcode = data[3] & 0xffff;
726                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
727
728                 PREP_REPLY_PACKET(8);
729
730                 if ((extcode == 0) || (extcode >= 7)) {
731                         /* let switch default handle error */
732                         length = 0;
733                 }
734
735                 switch (length) {
736                 case 4:
737                         rcode = highlevel_lock(host, source, packet->data, addr,
738                                                data[4], 0, extcode,flags);
739                         fill_async_lock_resp(packet, rcode, extcode, 4);
740                         break;
741                 case 8:
742                         if ((extcode != EXTCODE_FETCH_ADD) 
743                             && (extcode != EXTCODE_LITTLE_ADD)) {
744                                 rcode = highlevel_lock(host, source,
745                                                        packet->data, addr,
746                                                        data[5], data[4], 
747                                                        extcode, flags);
748                                 fill_async_lock_resp(packet, rcode, extcode, 4);
749                         } else {
750                                 rcode = highlevel_lock64(host, source,
751                                              (octlet_t *)packet->data, addr,
752                                              *(octlet_t *)(data + 4), 0ULL,
753                                              extcode, flags);
754                                 fill_async_lock_resp(packet, rcode, extcode, 8);
755                         }
756                         break;
757                 case 16:
758                         rcode = highlevel_lock64(host, source,
759                                                  (octlet_t *)packet->data, addr,
760                                                  *(octlet_t *)(data + 6),
761                                                  *(octlet_t *)(data + 4), 
762                                                  extcode, flags);
763                         fill_async_lock_resp(packet, rcode, extcode, 8);
764                         break;
765                 default:
766                         rcode = RCODE_TYPE_ERROR;
767                         fill_async_lock_resp(packet, rcode,
768                                              extcode, 0);
769                 }
770
771                 if (rcode >= 0) {
772                         send_packet_nocare(packet);
773                 } else {
774                         free_hpsb_packet(packet);
775                 }
776                 break;
777         }
778
779 }
780 #undef PREP_REPLY_PACKET
781
782
783 void hpsb_packet_received(struct hpsb_host *host, quadlet_t *data, size_t size,
784                           int write_acked)
785 {
786         int tcode;
787
788         if (host->in_bus_reset) {
789                 HPSB_INFO("received packet during reset; ignoring");
790                 return;
791         }
792
793 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
794         dump_packet("received packet:", data, size);
795 #endif
796
797         tcode = (data[0] >> 4) & 0xf;
798
799         switch (tcode) {
800         case TCODE_WRITE_RESPONSE:
801         case TCODE_READQ_RESPONSE:
802         case TCODE_READB_RESPONSE:
803         case TCODE_LOCK_RESPONSE:
804                 handle_packet_response(host, tcode, data, size);
805                 break;
806
807         case TCODE_WRITEQ:
808         case TCODE_WRITEB:
809         case TCODE_READQ:
810         case TCODE_READB:
811         case TCODE_LOCK_REQUEST:
812                 handle_incoming_packet(host, tcode, data, size, write_acked);
813                 break;
814
815
816         case TCODE_ISO_DATA:
817                 highlevel_iso_receive(host, data, size);
818                 break;
819
820         case TCODE_CYCLE_START:
821                 /* simply ignore this packet if it is passed on */
822                 break;
823
824         default:
825                 HPSB_NOTICE("received packet with bogus transaction code %d", 
826                             tcode);
827                 break;
828         }
829 }
830
831
832 void abort_requests(struct hpsb_host *host)
833 {
834         unsigned long flags;
835         struct hpsb_packet *packet;
836         struct list_head *lh;
837         LIST_HEAD(llist);
838
839         host->driver->devctl(host, CANCEL_REQUESTS, 0);
840
841         spin_lock_irqsave(&host->pending_pkt_lock, flags);
842         list_splice(&host->pending_packets, &llist);
843         INIT_LIST_HEAD(&host->pending_packets);
844         spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
845
846         list_for_each(lh, &llist) {
847                 packet = list_entry(lh, struct hpsb_packet, list);
848                 packet->state = hpsb_complete;
849                 packet->ack_code = ACKX_ABORTED;
850                 up(&packet->state_change);
851                 process_complete_tasks(packet);
852         }
853 }
854
855 void abort_timedouts(struct hpsb_host *host)
856 {
857         unsigned long flags;
858         struct hpsb_packet *packet;
859         unsigned long expire;
860         struct list_head *lh, *next;
861         LIST_HEAD(expiredlist);
862
863         spin_lock_irqsave(&host->csr.lock, flags);
864         expire = (host->csr.split_timeout_hi * 8000 
865                   + (host->csr.split_timeout_lo >> 19))
866                 * HZ / 8000;
867         /* Avoid shortening of timeout due to rounding errors: */
868         expire++;
869         spin_unlock_irqrestore(&host->csr.lock, flags);
870
871
872         spin_lock_irqsave(&host->pending_pkt_lock, flags);
873
874         for (lh = host->pending_packets.next; lh != &host->pending_packets; lh = next) {
875                 packet = list_entry(lh, struct hpsb_packet, list);
876                 next = lh->next;
877                 if (time_before(packet->sendtime + expire, jiffies)) {
878                         list_del(&packet->list);
879                         list_add(&packet->list, &expiredlist);
880                 }
881         }
882
883         if (!list_empty(&host->pending_packets))
884                 hpsb_schedule_work(&host->timeout_tq);
885
886         spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
887
888         list_for_each(lh, &expiredlist) {
889                 packet = list_entry(lh, struct hpsb_packet, list);
890                 packet->state = hpsb_complete;
891                 packet->ack_code = ACKX_TIMEOUT;
892                 up(&packet->state_change);
893                 process_complete_tasks(packet);
894         }
895 }
896
897
898 /*
899  * character device dispatching (see ieee1394_core.h)
900  * Dan Maas <dmaas@dcine.com>
901  */
902
903 static struct {
904         struct file_operations *file_ops;
905         struct module *module;
906 } ieee1394_chardevs[16];
907
908 static rwlock_t ieee1394_chardevs_lock = RW_LOCK_UNLOCKED;
909
910 static int ieee1394_dispatch_open(struct inode *inode, struct file *file);
911
912 static struct file_operations ieee1394_chardev_ops = {
913         .owner =THIS_MODULE,
914         .open = ieee1394_dispatch_open,
915 };
916
917 devfs_handle_t ieee1394_devfs_handle;
918
919
920 /* claim a block of minor numbers */
921 int ieee1394_register_chardev(int blocknum,
922                               struct module *module,
923                               struct file_operations *file_ops)
924 {
925         int retval;
926         
927         if( (blocknum < 0) || (blocknum > 15) )
928                 return -EINVAL;
929
930         write_lock(&ieee1394_chardevs_lock);
931
932         if(ieee1394_chardevs[blocknum].file_ops == NULL) {
933                 /* grab the minor block */
934                 ieee1394_chardevs[blocknum].file_ops = file_ops;
935                 ieee1394_chardevs[blocknum].module = module;
936                 
937                 retval = 0;
938         } else {
939                 /* block already taken */
940                 retval = -EBUSY;
941         }
942         
943         write_unlock(&ieee1394_chardevs_lock);
944
945         return retval;
946 }
947
948 /* release a block of minor numbers */
949 void ieee1394_unregister_chardev(int blocknum)
950 {
951         if( (blocknum < 0) || (blocknum > 15) )
952                 return;
953         
954         write_lock(&ieee1394_chardevs_lock);
955         
956         if(ieee1394_chardevs[blocknum].file_ops) {
957                 ieee1394_chardevs[blocknum].file_ops = NULL;
958                 ieee1394_chardevs[blocknum].module = NULL;
959         }
960         
961         write_unlock(&ieee1394_chardevs_lock);
962 }
963
964 /*
965   ieee1394_get_chardev() - look up and acquire a character device
966   driver that has previously registered using ieee1394_register_chardev()
967   
968   On success, returns 1 and sets module and file_ops to the driver.
969   The module will have an incremented reference count.
970    
971   On failure, returns 0.
972   The module will NOT have an incremented reference count.
973 */
974
975 static int ieee1394_get_chardev(int blocknum,
976                                 struct module **module,
977                                 struct file_operations **file_ops)
978 {
979         int ret = 0;
980        
981         if( (blocknum < 0) || (blocknum > 15) )
982                 return ret;
983
984         read_lock(&ieee1394_chardevs_lock);
985
986         *module = ieee1394_chardevs[blocknum].module;
987         *file_ops = ieee1394_chardevs[blocknum].file_ops;
988
989         if(*file_ops == NULL)
990                 goto out;
991
992         /* don't need try_inc_mod_count if the driver is non-modular */
993         if(*module && (try_inc_mod_count(*module) == 0))
994                 goto out;
995
996         /* success! */
997         ret = 1;
998         
999 out:
1000         read_unlock(&ieee1394_chardevs_lock);
1001         return ret;
1002 }
1003
1004 /* the point of entry for open() on any ieee1394 character device */
1005 static int ieee1394_dispatch_open(struct inode *inode, struct file *file)
1006 {
1007         struct file_operations *file_ops;
1008         struct module *module;
1009         int blocknum;
1010         int retval;
1011
1012         /*
1013           Maintaining correct module reference counts is tricky here!
1014
1015           The key thing to remember is that the VFS increments the
1016           reference count of ieee1394 before it calls
1017           ieee1394_dispatch_open().
1018
1019           If the open() succeeds, then we need to transfer this extra
1020           reference to the task-specific driver module (e.g. raw1394).
1021           The VFS will deref the driver module automatically when the
1022           file is later released.
1023
1024           If the open() fails, then the VFS will drop the
1025           reference count of whatever module file->f_op->owner points
1026           to, immediately after this function returns.
1027         */
1028         
1029         /* shift away lower four bits of the minor
1030            to get the index of the ieee1394_driver
1031            we want */
1032         
1033         blocknum = (minor(inode->i_rdev) >> 4) & 0xF;
1034
1035         /* look up the driver */
1036
1037         if(ieee1394_get_chardev(blocknum, &module, &file_ops) == 0)
1038                 return -ENODEV;
1039
1040         /* redirect all subsequent requests to the driver's
1041            own file_operations */
1042         file->f_op = file_ops;
1043
1044         /* at this point BOTH ieee1394 and the task-specific driver have
1045            an extra reference */
1046
1047         /* follow through with the open() */
1048         retval = file_ops->open(inode, file);
1049
1050         if(retval == 0) {
1051                 
1052                 /* If the open() succeeded, then ieee1394 will be left
1053                    with an extra module reference, so we discard it here.
1054
1055                    The task-specific driver still has the extra
1056                    reference given to it by ieee1394_get_chardev().
1057                    This extra reference prevents the module from
1058                    unloading while the file is open, and will be
1059                    dropped by the VFS when the file is released.
1060                 */
1061                 
1062                 if(THIS_MODULE)
1063                         __MOD_DEC_USE_COUNT((struct module*) THIS_MODULE);
1064                 
1065                 /* note that if ieee1394 is compiled into the kernel,
1066                    THIS_MODULE will be (void*) NULL, hence the if and
1067                    the cast are necessary */
1068
1069         } else {
1070
1071                 /* if the open() failed, then we need to drop the
1072                    extra reference we gave to the task-specific
1073                    driver */
1074                 
1075                 if(module)
1076                         __MOD_DEC_USE_COUNT(module);
1077         
1078                 /* point the file's f_ops back to ieee1394. The VFS will then
1079                    decrement ieee1394's reference count immediately after this
1080                    function returns. */
1081                 
1082                 file->f_op = &ieee1394_chardev_ops;
1083         }
1084
1085         return retval;
1086 }
1087
1088 struct proc_dir_entry *ieee1394_procfs_entry;
1089
1090 static int __init ieee1394_init(void)
1091 {
1092         hpsb_packet_cache = kmem_cache_create("hpsb_packet", sizeof(struct hpsb_packet),
1093                                               0, 0, NULL, NULL);
1094
1095         ieee1394_devfs_handle = devfs_mk_dir(NULL, "ieee1394", NULL);
1096
1097         if (register_chrdev(IEEE1394_MAJOR, "ieee1394", &ieee1394_chardev_ops)) {
1098                 HPSB_ERR("unable to register character device major %d!\n", IEEE1394_MAJOR);
1099                 devfs_unregister(ieee1394_devfs_handle);
1100                 return -ENODEV;
1101         }
1102
1103 #ifdef CONFIG_PROC_FS
1104         /* Must be done before we start everything else, since the drivers
1105          * may use it.  */
1106         ieee1394_procfs_entry = proc_mkdir( "ieee1394", proc_bus);
1107         if (ieee1394_procfs_entry == NULL) {
1108                 HPSB_ERR("unable to create /proc/bus/ieee1394\n");
1109                 unregister_chrdev(IEEE1394_MAJOR, "ieee1394");
1110                 devfs_unregister(ieee1394_devfs_handle);
1111                 return -ENOMEM;
1112         }
1113         ieee1394_procfs_entry->owner = THIS_MODULE;
1114 #endif
1115
1116         init_hpsb_highlevel();
1117         init_csr();
1118         if (!disable_nodemgr)
1119                 init_ieee1394_nodemgr(disable_hotplug);
1120         else
1121                 HPSB_INFO("nodemgr functionality disabled");
1122
1123         return 0;
1124 }
1125
1126 static void __exit ieee1394_cleanup(void)
1127 {
1128         if (!disable_nodemgr)
1129                 cleanup_ieee1394_nodemgr();
1130
1131         cleanup_csr();
1132         kmem_cache_destroy(hpsb_packet_cache);
1133
1134         unregister_chrdev(IEEE1394_MAJOR, "ieee1394");
1135         
1136         /* it's ok to pass a NULL devfs_handle to devfs_unregister */
1137         devfs_unregister(ieee1394_devfs_handle);
1138         
1139         remove_proc_entry("ieee1394", proc_bus);
1140 }
1141
1142 module_init(ieee1394_init);
1143 module_exit(ieee1394_cleanup);
1144
1145 /* Exported symbols */
1146 EXPORT_SYMBOL(hpsb_alloc_host);
1147 EXPORT_SYMBOL(hpsb_add_host);
1148 EXPORT_SYMBOL(hpsb_remove_host);
1149 EXPORT_SYMBOL(hpsb_ref_host);
1150 EXPORT_SYMBOL(hpsb_unref_host);
1151 EXPORT_SYMBOL(hpsb_speedto_str);
1152 EXPORT_SYMBOL(hpsb_add_packet_complete_task);
1153
1154 EXPORT_SYMBOL(alloc_hpsb_packet);
1155 EXPORT_SYMBOL(free_hpsb_packet);
1156 EXPORT_SYMBOL(hpsb_send_packet);
1157 EXPORT_SYMBOL(hpsb_reset_bus);
1158 EXPORT_SYMBOL(hpsb_bus_reset);
1159 EXPORT_SYMBOL(hpsb_selfid_received);
1160 EXPORT_SYMBOL(hpsb_selfid_complete);
1161 EXPORT_SYMBOL(hpsb_packet_sent);
1162 EXPORT_SYMBOL(hpsb_packet_received);
1163
1164 EXPORT_SYMBOL(get_tlabel);
1165 EXPORT_SYMBOL(free_tlabel);
1166 EXPORT_SYMBOL(fill_async_readquad);
1167 EXPORT_SYMBOL(fill_async_readquad_resp);
1168 EXPORT_SYMBOL(fill_async_readblock);
1169 EXPORT_SYMBOL(fill_async_readblock_resp);
1170 EXPORT_SYMBOL(fill_async_writequad);
1171 EXPORT_SYMBOL(fill_async_writeblock);
1172 EXPORT_SYMBOL(fill_async_write_resp);
1173 EXPORT_SYMBOL(fill_async_lock);
1174 EXPORT_SYMBOL(fill_async_lock_resp);
1175 EXPORT_SYMBOL(fill_iso_packet);
1176 EXPORT_SYMBOL(fill_phy_packet);
1177 EXPORT_SYMBOL(hpsb_make_readqpacket);
1178 EXPORT_SYMBOL(hpsb_make_readbpacket);
1179 EXPORT_SYMBOL(hpsb_make_writeqpacket);
1180 EXPORT_SYMBOL(hpsb_make_writebpacket);
1181 EXPORT_SYMBOL(hpsb_make_lockpacket);
1182 EXPORT_SYMBOL(hpsb_make_lock64packet);
1183 EXPORT_SYMBOL(hpsb_make_phypacket);
1184 EXPORT_SYMBOL(hpsb_packet_success);
1185 EXPORT_SYMBOL(hpsb_make_packet);
1186 EXPORT_SYMBOL(hpsb_read);
1187 EXPORT_SYMBOL(hpsb_write);
1188 EXPORT_SYMBOL(hpsb_lock);
1189
1190 EXPORT_SYMBOL(hpsb_register_highlevel);
1191 EXPORT_SYMBOL(hpsb_unregister_highlevel);
1192 EXPORT_SYMBOL(hpsb_register_addrspace);
1193 EXPORT_SYMBOL(hpsb_unregister_addrspace);
1194 EXPORT_SYMBOL(hpsb_listen_channel);
1195 EXPORT_SYMBOL(hpsb_unlisten_channel);
1196 EXPORT_SYMBOL(highlevel_read);
1197 EXPORT_SYMBOL(highlevel_write);
1198 EXPORT_SYMBOL(highlevel_lock);
1199 EXPORT_SYMBOL(highlevel_lock64);
1200 EXPORT_SYMBOL(highlevel_add_host);
1201 EXPORT_SYMBOL(highlevel_remove_host);
1202 EXPORT_SYMBOL(highlevel_host_reset);
1203
1204 EXPORT_SYMBOL(hpsb_guid_get_entry);
1205 EXPORT_SYMBOL(hpsb_nodeid_get_entry);
1206 EXPORT_SYMBOL(hpsb_node_fill_packet);
1207 EXPORT_SYMBOL(hpsb_node_read);
1208 EXPORT_SYMBOL(hpsb_node_write);
1209 EXPORT_SYMBOL(hpsb_node_lock);
1210 EXPORT_SYMBOL(hpsb_update_config_rom);
1211 EXPORT_SYMBOL(hpsb_get_config_rom);
1212 EXPORT_SYMBOL(hpsb_register_protocol);
1213 EXPORT_SYMBOL(hpsb_unregister_protocol);
1214 EXPORT_SYMBOL(hpsb_release_unit_directory);
1215
1216 EXPORT_SYMBOL(ieee1394_register_chardev);
1217 EXPORT_SYMBOL(ieee1394_unregister_chardev);
1218 EXPORT_SYMBOL(ieee1394_devfs_handle);
1219
1220 EXPORT_SYMBOL(ieee1394_procfs_entry);