[POWERPC] Add missing newline in xmon help output
[linux-2.6/kvm.git] / drivers / ieee1394 / ieee1394_core.c
blobd791d08c743ce22d100ad34e440f3cdb932ca30f
1 /*
2 * IEEE 1394 for Linux
4 * Core support: hpsb_packet management, packet handling and forwarding to
5 * highlevel or lowlevel code
7 * Copyright (C) 1999, 2000 Andreas E. Bombe
8 * 2002 Manfred Weihs <weihs@ict.tuwien.ac.at>
10 * This code is licensed under the GPL. See the file COPYING in the root
11 * directory of the kernel sources for details.
14 * Contributions:
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)
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 #include <linux/preempt.h>
37 #include <linux/time.h>
39 #include <asm/system.h>
40 #include <asm/byteorder.h>
42 #include "ieee1394_types.h"
43 #include "ieee1394.h"
44 #include "hosts.h"
45 #include "ieee1394_core.h"
46 #include "highlevel.h"
47 #include "ieee1394_transactions.h"
48 #include "csr.h"
49 #include "nodemgr.h"
50 #include "dma.h"
51 #include "iso.h"
52 #include "config_roms.h"
55 * Disable the nodemgr detection and config rom reading functionality.
57 static int disable_nodemgr;
58 module_param(disable_nodemgr, int, 0444);
59 MODULE_PARM_DESC(disable_nodemgr, "Disable nodemgr functionality.");
61 /* Disable Isochronous Resource Manager functionality */
62 int hpsb_disable_irm = 0;
63 module_param_named(disable_irm, hpsb_disable_irm, bool, 0444);
64 MODULE_PARM_DESC(disable_irm,
65 "Disable Isochronous Resource Manager functionality.");
67 /* We are GPL, so treat us special */
68 MODULE_LICENSE("GPL");
70 /* Some globals used */
71 const char *hpsb_speedto_str[] = { "S100", "S200", "S400", "S800", "S1600", "S3200" };
72 struct class *hpsb_protocol_class;
74 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
75 static void dump_packet(const char *text, quadlet_t *data, int size, int speed)
77 int i;
79 size /= 4;
80 size = (size > 4 ? 4 : size);
82 printk(KERN_DEBUG "ieee1394: %s", text);
83 if (speed > -1 && speed < 6)
84 printk(" at %s", hpsb_speedto_str[speed]);
85 printk(":");
86 for (i = 0; i < size; i++)
87 printk(" %08x", data[i]);
88 printk("\n");
90 #else
91 #define dump_packet(a,b,c,d) do {} while (0)
92 #endif
94 static void abort_requests(struct hpsb_host *host);
95 static void queue_packet_complete(struct hpsb_packet *packet);
98 /**
99 * hpsb_set_packet_complete_task - set the task that runs when a packet
100 * completes. You cannot call this more than once on a single packet
101 * before it is sent.
103 * @packet: the packet whose completion we want the task added to
104 * @routine: function to call
105 * @data: data (if any) to pass to the above function
107 void hpsb_set_packet_complete_task(struct hpsb_packet *packet,
108 void (*routine)(void *), void *data)
110 WARN_ON(packet->complete_routine != NULL);
111 packet->complete_routine = routine;
112 packet->complete_data = data;
113 return;
117 * hpsb_alloc_packet - allocate new packet structure
118 * @data_size: size of the data block to be allocated
120 * This function allocates, initializes and returns a new &struct hpsb_packet.
121 * It can be used in interrupt context. A header block is always included, its
122 * size is big enough to contain all possible 1394 headers. The data block is
123 * only allocated when @data_size is not zero.
125 * For packets for which responses will be received the @data_size has to be big
126 * enough to contain the response's data block since no further allocation
127 * occurs at response matching time.
129 * The packet's generation value will be set to the current generation number
130 * for ease of use. Remember to overwrite it with your own recorded generation
131 * number if you can not be sure that your code will not race with a bus reset.
133 * Return value: A pointer to a &struct hpsb_packet or NULL on allocation
134 * failure.
136 struct hpsb_packet *hpsb_alloc_packet(size_t data_size)
138 struct hpsb_packet *packet = NULL;
139 struct sk_buff *skb;
141 data_size = ((data_size + 3) & ~3);
143 skb = alloc_skb(data_size + sizeof(*packet), GFP_ATOMIC);
144 if (skb == NULL)
145 return NULL;
147 memset(skb->data, 0, data_size + sizeof(*packet));
149 packet = (struct hpsb_packet *)skb->data;
150 packet->skb = skb;
152 packet->header = packet->embedded_header;
153 packet->state = hpsb_unused;
154 packet->generation = -1;
155 INIT_LIST_HEAD(&packet->driver_list);
156 atomic_set(&packet->refcnt, 1);
158 if (data_size) {
159 packet->data = (quadlet_t *)(skb->data + sizeof(*packet));
160 packet->data_size = data_size;
163 return packet;
168 * hpsb_free_packet - free packet and data associated with it
169 * @packet: packet to free (is NULL safe)
171 * This function will free packet->data and finally the packet itself.
173 void hpsb_free_packet(struct hpsb_packet *packet)
175 if (packet && atomic_dec_and_test(&packet->refcnt)) {
176 BUG_ON(!list_empty(&packet->driver_list));
177 kfree_skb(packet->skb);
182 int hpsb_reset_bus(struct hpsb_host *host, int type)
184 if (!host->in_bus_reset) {
185 host->driver->devctl(host, RESET_BUS, type);
186 return 0;
187 } else {
188 return 1;
193 * hpsb_read_cycle_timer - read cycle timer register and system time
194 * @host: host whose isochronous cycle timer register is read
195 * @cycle_timer: address of bitfield to return the register contents
196 * @local_time: address to return the system time
198 * The format of * @cycle_timer, is described in OHCI 1.1 clause 5.13. This
199 * format is also read from non-OHCI controllers. * @local_time contains the
200 * system time in microseconds since the Epoch, read at the moment when the
201 * cycle timer was read.
203 * Return value: 0 for success or error number otherwise.
205 int hpsb_read_cycle_timer(struct hpsb_host *host, u32 *cycle_timer,
206 u64 *local_time)
208 int ctr;
209 struct timeval tv;
210 unsigned long flags;
212 if (!host || !cycle_timer || !local_time)
213 return -EINVAL;
215 preempt_disable();
216 local_irq_save(flags);
218 ctr = host->driver->devctl(host, GET_CYCLE_COUNTER, 0);
219 if (ctr)
220 do_gettimeofday(&tv);
222 local_irq_restore(flags);
223 preempt_enable();
225 if (!ctr)
226 return -EIO;
227 *cycle_timer = ctr;
228 *local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
229 return 0;
232 int hpsb_bus_reset(struct hpsb_host *host)
234 if (host->in_bus_reset) {
235 HPSB_NOTICE("%s called while bus reset already in progress",
236 __FUNCTION__);
237 return 1;
240 abort_requests(host);
241 host->in_bus_reset = 1;
242 host->irm_id = -1;
243 host->is_irm = 0;
244 host->busmgr_id = -1;
245 host->is_busmgr = 0;
246 host->is_cycmst = 0;
247 host->node_count = 0;
248 host->selfid_count = 0;
250 return 0;
255 * Verify num_of_selfids SelfIDs and return number of nodes. Return zero in
256 * case verification failed.
258 static int check_selfids(struct hpsb_host *host)
260 int nodeid = -1;
261 int rest_of_selfids = host->selfid_count;
262 struct selfid *sid = (struct selfid *)host->topology_map;
263 struct ext_selfid *esid;
264 int esid_seq = 23;
266 host->nodes_active = 0;
268 while (rest_of_selfids--) {
269 if (!sid->extended) {
270 nodeid++;
271 esid_seq = 0;
273 if (sid->phy_id != nodeid) {
274 HPSB_INFO("SelfIDs failed monotony check with "
275 "%d", sid->phy_id);
276 return 0;
279 if (sid->link_active) {
280 host->nodes_active++;
281 if (sid->contender)
282 host->irm_id = LOCAL_BUS | sid->phy_id;
284 } else {
285 esid = (struct ext_selfid *)sid;
287 if ((esid->phy_id != nodeid)
288 || (esid->seq_nr != esid_seq)) {
289 HPSB_INFO("SelfIDs failed monotony check with "
290 "%d/%d", esid->phy_id, esid->seq_nr);
291 return 0;
293 esid_seq++;
295 sid++;
298 esid = (struct ext_selfid *)(sid - 1);
299 while (esid->extended) {
300 if ((esid->porta == SELFID_PORT_PARENT) ||
301 (esid->portb == SELFID_PORT_PARENT) ||
302 (esid->portc == SELFID_PORT_PARENT) ||
303 (esid->portd == SELFID_PORT_PARENT) ||
304 (esid->porte == SELFID_PORT_PARENT) ||
305 (esid->portf == SELFID_PORT_PARENT) ||
306 (esid->portg == SELFID_PORT_PARENT) ||
307 (esid->porth == SELFID_PORT_PARENT)) {
308 HPSB_INFO("SelfIDs failed root check on "
309 "extended SelfID");
310 return 0;
312 esid--;
315 sid = (struct selfid *)esid;
316 if ((sid->port0 == SELFID_PORT_PARENT) ||
317 (sid->port1 == SELFID_PORT_PARENT) ||
318 (sid->port2 == SELFID_PORT_PARENT)) {
319 HPSB_INFO("SelfIDs failed root check");
320 return 0;
323 host->node_count = nodeid + 1;
324 return 1;
327 static void build_speed_map(struct hpsb_host *host, int nodecount)
329 u8 cldcnt[nodecount];
330 u8 *map = host->speed_map;
331 u8 *speedcap = host->speed;
332 struct selfid *sid;
333 struct ext_selfid *esid;
334 int i, j, n;
336 for (i = 0; i < (nodecount * 64); i += 64) {
337 for (j = 0; j < nodecount; j++) {
338 map[i+j] = IEEE1394_SPEED_MAX;
342 for (i = 0; i < nodecount; i++) {
343 cldcnt[i] = 0;
346 /* find direct children count and speed */
347 for (sid = (struct selfid *)&host->topology_map[host->selfid_count-1],
348 n = nodecount - 1;
349 (void *)sid >= (void *)host->topology_map; sid--) {
350 if (sid->extended) {
351 esid = (struct ext_selfid *)sid;
353 if (esid->porta == SELFID_PORT_CHILD) cldcnt[n]++;
354 if (esid->portb == SELFID_PORT_CHILD) cldcnt[n]++;
355 if (esid->portc == SELFID_PORT_CHILD) cldcnt[n]++;
356 if (esid->portd == SELFID_PORT_CHILD) cldcnt[n]++;
357 if (esid->porte == SELFID_PORT_CHILD) cldcnt[n]++;
358 if (esid->portf == SELFID_PORT_CHILD) cldcnt[n]++;
359 if (esid->portg == SELFID_PORT_CHILD) cldcnt[n]++;
360 if (esid->porth == SELFID_PORT_CHILD) cldcnt[n]++;
361 } else {
362 if (sid->port0 == SELFID_PORT_CHILD) cldcnt[n]++;
363 if (sid->port1 == SELFID_PORT_CHILD) cldcnt[n]++;
364 if (sid->port2 == SELFID_PORT_CHILD) cldcnt[n]++;
366 speedcap[n] = sid->speed;
367 n--;
371 /* set self mapping */
372 for (i = 0; i < nodecount; i++) {
373 map[64*i + i] = speedcap[i];
376 /* fix up direct children count to total children count;
377 * also fix up speedcaps for sibling and parent communication */
378 for (i = 1; i < nodecount; i++) {
379 for (j = cldcnt[i], n = i - 1; j > 0; j--) {
380 cldcnt[i] += cldcnt[n];
381 speedcap[n] = min(speedcap[n], speedcap[i]);
382 n -= cldcnt[n] + 1;
386 for (n = 0; n < nodecount; n++) {
387 for (i = n - cldcnt[n]; i <= n; i++) {
388 for (j = 0; j < (n - cldcnt[n]); j++) {
389 map[j*64 + i] = map[i*64 + j] =
390 min(map[i*64 + j], speedcap[n]);
392 for (j = n + 1; j < nodecount; j++) {
393 map[j*64 + i] = map[i*64 + j] =
394 min(map[i*64 + j], speedcap[n]);
399 #if SELFID_SPEED_UNKNOWN != IEEE1394_SPEED_MAX
400 /* assume maximum speed for 1394b PHYs, nodemgr will correct it */
401 for (n = 0; n < nodecount; n++)
402 if (speedcap[n] == SELFID_SPEED_UNKNOWN)
403 speedcap[n] = IEEE1394_SPEED_MAX;
404 #endif
408 void hpsb_selfid_received(struct hpsb_host *host, quadlet_t sid)
410 if (host->in_bus_reset) {
411 HPSB_VERBOSE("Including SelfID 0x%x", sid);
412 host->topology_map[host->selfid_count++] = sid;
413 } else {
414 HPSB_NOTICE("Spurious SelfID packet (0x%08x) received from bus %d",
415 sid, NODEID_TO_BUS(host->node_id));
419 void hpsb_selfid_complete(struct hpsb_host *host, int phyid, int isroot)
421 if (!host->in_bus_reset)
422 HPSB_NOTICE("SelfID completion called outside of bus reset!");
424 host->node_id = LOCAL_BUS | phyid;
425 host->is_root = isroot;
427 if (!check_selfids(host)) {
428 if (host->reset_retries++ < 20) {
429 /* selfid stage did not complete without error */
430 HPSB_NOTICE("Error in SelfID stage, resetting");
431 host->in_bus_reset = 0;
432 /* this should work from ohci1394 now... */
433 hpsb_reset_bus(host, LONG_RESET);
434 return;
435 } else {
436 HPSB_NOTICE("Stopping out-of-control reset loop");
437 HPSB_NOTICE("Warning - topology map and speed map will not be valid");
438 host->reset_retries = 0;
440 } else {
441 host->reset_retries = 0;
442 build_speed_map(host, host->node_count);
445 HPSB_VERBOSE("selfid_complete called with successful SelfID stage "
446 "... irm_id: 0x%X node_id: 0x%X",host->irm_id,host->node_id);
448 /* irm_id is kept up to date by check_selfids() */
449 if (host->irm_id == host->node_id) {
450 host->is_irm = 1;
451 } else {
452 host->is_busmgr = 0;
453 host->is_irm = 0;
456 if (isroot) {
457 host->driver->devctl(host, ACT_CYCLE_MASTER, 1);
458 host->is_cycmst = 1;
460 atomic_inc(&host->generation);
461 host->in_bus_reset = 0;
462 highlevel_host_reset(host);
466 void hpsb_packet_sent(struct hpsb_host *host, struct hpsb_packet *packet,
467 int ackcode)
469 unsigned long flags;
471 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
473 packet->ack_code = ackcode;
475 if (packet->no_waiter || packet->state == hpsb_complete) {
476 /* if packet->no_waiter, must not have a tlabel allocated */
477 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
478 hpsb_free_packet(packet);
479 return;
482 atomic_dec(&packet->refcnt); /* drop HC's reference */
483 /* here the packet must be on the host->pending_packet_queue */
485 if (ackcode != ACK_PENDING || !packet->expect_response) {
486 packet->state = hpsb_complete;
487 __skb_unlink(packet->skb, &host->pending_packet_queue);
488 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
489 queue_packet_complete(packet);
490 return;
493 packet->state = hpsb_pending;
494 packet->sendtime = jiffies;
496 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
498 mod_timer(&host->timeout, jiffies + host->timeout_interval);
502 * hpsb_send_phy_config - transmit a PHY configuration packet on the bus
503 * @host: host that PHY config packet gets sent through
504 * @rootid: root whose force_root bit should get set (-1 = don't set force_root)
505 * @gapcnt: gap count value to set (-1 = don't set gap count)
507 * This function sends a PHY config packet on the bus through the specified host.
509 * Return value: 0 for success or error number otherwise.
511 int hpsb_send_phy_config(struct hpsb_host *host, int rootid, int gapcnt)
513 struct hpsb_packet *packet;
514 quadlet_t d = 0;
515 int retval = 0;
517 if (rootid >= ALL_NODES || rootid < -1 || gapcnt > 0x3f || gapcnt < -1 ||
518 (rootid == -1 && gapcnt == -1)) {
519 HPSB_DEBUG("Invalid Parameter: rootid = %d gapcnt = %d",
520 rootid, gapcnt);
521 return -EINVAL;
524 if (rootid != -1)
525 d |= PHYPACKET_PHYCONFIG_R | rootid << PHYPACKET_PORT_SHIFT;
526 if (gapcnt != -1)
527 d |= PHYPACKET_PHYCONFIG_T | gapcnt << PHYPACKET_GAPCOUNT_SHIFT;
529 packet = hpsb_make_phypacket(host, d);
530 if (!packet)
531 return -ENOMEM;
533 packet->generation = get_hpsb_generation(host);
534 retval = hpsb_send_packet_and_wait(packet);
535 hpsb_free_packet(packet);
537 return retval;
541 * hpsb_send_packet - transmit a packet on the bus
542 * @packet: packet to send
544 * The packet is sent through the host specified in the packet->host field.
545 * Before sending, the packet's transmit speed is automatically determined
546 * using the local speed map when it is an async, non-broadcast packet.
548 * Possibilities for failure are that host is either not initialized, in bus
549 * reset, the packet's generation number doesn't match the current generation
550 * number or the host reports a transmit error.
552 * Return value: 0 on success, negative errno on failure.
554 int hpsb_send_packet(struct hpsb_packet *packet)
556 struct hpsb_host *host = packet->host;
558 if (host->is_shutdown)
559 return -EINVAL;
560 if (host->in_bus_reset ||
561 (packet->generation != get_hpsb_generation(host)))
562 return -EAGAIN;
564 packet->state = hpsb_queued;
566 /* This just seems silly to me */
567 WARN_ON(packet->no_waiter && packet->expect_response);
569 if (!packet->no_waiter || packet->expect_response) {
570 atomic_inc(&packet->refcnt);
571 /* Set the initial "sendtime" to 10 seconds from now, to
572 prevent premature expiry. If a packet takes more than
573 10 seconds to hit the wire, we have bigger problems :) */
574 packet->sendtime = jiffies + 10 * HZ;
575 skb_queue_tail(&host->pending_packet_queue, packet->skb);
578 if (packet->node_id == host->node_id) {
579 /* it is a local request, so handle it locally */
581 quadlet_t *data;
582 size_t size = packet->data_size + packet->header_size;
584 data = kmalloc(size, GFP_ATOMIC);
585 if (!data) {
586 HPSB_ERR("unable to allocate memory for concatenating header and data");
587 return -ENOMEM;
590 memcpy(data, packet->header, packet->header_size);
592 if (packet->data_size)
593 memcpy(((u8*)data) + packet->header_size, packet->data, packet->data_size);
595 dump_packet("send packet local", packet->header, packet->header_size, -1);
597 hpsb_packet_sent(host, packet, packet->expect_response ? ACK_PENDING : ACK_COMPLETE);
598 hpsb_packet_received(host, data, size, 0);
600 kfree(data);
602 return 0;
605 if (packet->type == hpsb_async &&
606 NODEID_TO_NODE(packet->node_id) != ALL_NODES)
607 packet->speed_code =
608 host->speed[NODEID_TO_NODE(packet->node_id)];
610 dump_packet("send packet", packet->header, packet->header_size, packet->speed_code);
612 return host->driver->transmit_packet(host, packet);
615 /* We could just use complete() directly as the packet complete
616 * callback, but this is more typesafe, in the sense that we get a
617 * compiler error if the prototype for complete() changes. */
619 static void complete_packet(void *data)
621 complete((struct completion *) data);
624 int hpsb_send_packet_and_wait(struct hpsb_packet *packet)
626 struct completion done;
627 int retval;
629 init_completion(&done);
630 hpsb_set_packet_complete_task(packet, complete_packet, &done);
631 retval = hpsb_send_packet(packet);
632 if (retval == 0)
633 wait_for_completion(&done);
635 return retval;
638 static void send_packet_nocare(struct hpsb_packet *packet)
640 if (hpsb_send_packet(packet) < 0) {
641 hpsb_free_packet(packet);
646 static void handle_packet_response(struct hpsb_host *host, int tcode,
647 quadlet_t *data, size_t size)
649 struct hpsb_packet *packet = NULL;
650 struct sk_buff *skb;
651 int tcode_match = 0;
652 int tlabel;
653 unsigned long flags;
655 tlabel = (data[0] >> 10) & 0x3f;
657 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
659 skb_queue_walk(&host->pending_packet_queue, skb) {
660 packet = (struct hpsb_packet *)skb->data;
661 if ((packet->tlabel == tlabel)
662 && (packet->node_id == (data[1] >> 16))){
663 break;
666 packet = NULL;
669 if (packet == NULL) {
670 HPSB_DEBUG("unsolicited response packet received - no tlabel match");
671 dump_packet("contents", data, 16, -1);
672 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
673 return;
676 switch (packet->tcode) {
677 case TCODE_WRITEQ:
678 case TCODE_WRITEB:
679 if (tcode != TCODE_WRITE_RESPONSE)
680 break;
681 tcode_match = 1;
682 memcpy(packet->header, data, 12);
683 break;
684 case TCODE_READQ:
685 if (tcode != TCODE_READQ_RESPONSE)
686 break;
687 tcode_match = 1;
688 memcpy(packet->header, data, 16);
689 break;
690 case TCODE_READB:
691 if (tcode != TCODE_READB_RESPONSE)
692 break;
693 tcode_match = 1;
694 BUG_ON(packet->skb->len - sizeof(*packet) < size - 16);
695 memcpy(packet->header, data, 16);
696 memcpy(packet->data, data + 4, size - 16);
697 break;
698 case TCODE_LOCK_REQUEST:
699 if (tcode != TCODE_LOCK_RESPONSE)
700 break;
701 tcode_match = 1;
702 size = min((size - 16), (size_t)8);
703 BUG_ON(packet->skb->len - sizeof(*packet) < size);
704 memcpy(packet->header, data, 16);
705 memcpy(packet->data, data + 4, size);
706 break;
709 if (!tcode_match) {
710 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
711 HPSB_INFO("unsolicited response packet received - tcode mismatch");
712 dump_packet("contents", data, 16, -1);
713 return;
716 __skb_unlink(skb, &host->pending_packet_queue);
718 if (packet->state == hpsb_queued) {
719 packet->sendtime = jiffies;
720 packet->ack_code = ACK_PENDING;
723 packet->state = hpsb_complete;
724 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
726 queue_packet_complete(packet);
730 static struct hpsb_packet *create_reply_packet(struct hpsb_host *host,
731 quadlet_t *data, size_t dsize)
733 struct hpsb_packet *p;
735 p = hpsb_alloc_packet(dsize);
736 if (unlikely(p == NULL)) {
737 /* FIXME - send data_error response */
738 return NULL;
741 p->type = hpsb_async;
742 p->state = hpsb_unused;
743 p->host = host;
744 p->node_id = data[1] >> 16;
745 p->tlabel = (data[0] >> 10) & 0x3f;
746 p->no_waiter = 1;
748 p->generation = get_hpsb_generation(host);
750 if (dsize % 4)
751 p->data[dsize / 4] = 0;
753 return p;
756 #define PREP_ASYNC_HEAD_RCODE(tc) \
757 packet->tcode = tc; \
758 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
759 | (1 << 8) | (tc << 4); \
760 packet->header[1] = (packet->host->node_id << 16) | (rcode << 12); \
761 packet->header[2] = 0
763 static void fill_async_readquad_resp(struct hpsb_packet *packet, int rcode,
764 quadlet_t data)
766 PREP_ASYNC_HEAD_RCODE(TCODE_READQ_RESPONSE);
767 packet->header[3] = data;
768 packet->header_size = 16;
769 packet->data_size = 0;
772 static void fill_async_readblock_resp(struct hpsb_packet *packet, int rcode,
773 int length)
775 if (rcode != RCODE_COMPLETE)
776 length = 0;
778 PREP_ASYNC_HEAD_RCODE(TCODE_READB_RESPONSE);
779 packet->header[3] = length << 16;
780 packet->header_size = 16;
781 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
784 static void fill_async_write_resp(struct hpsb_packet *packet, int rcode)
786 PREP_ASYNC_HEAD_RCODE(TCODE_WRITE_RESPONSE);
787 packet->header[2] = 0;
788 packet->header_size = 12;
789 packet->data_size = 0;
792 static void fill_async_lock_resp(struct hpsb_packet *packet, int rcode, int extcode,
793 int length)
795 if (rcode != RCODE_COMPLETE)
796 length = 0;
798 PREP_ASYNC_HEAD_RCODE(TCODE_LOCK_RESPONSE);
799 packet->header[3] = (length << 16) | extcode;
800 packet->header_size = 16;
801 packet->data_size = length;
804 #define PREP_REPLY_PACKET(length) \
805 packet = create_reply_packet(host, data, length); \
806 if (packet == NULL) break
808 static void handle_incoming_packet(struct hpsb_host *host, int tcode,
809 quadlet_t *data, size_t size, int write_acked)
811 struct hpsb_packet *packet;
812 int length, rcode, extcode;
813 quadlet_t buffer;
814 nodeid_t source = data[1] >> 16;
815 nodeid_t dest = data[0] >> 16;
816 u16 flags = (u16) data[0];
817 u64 addr;
819 /* big FIXME - no error checking is done for an out of bounds length */
821 switch (tcode) {
822 case TCODE_WRITEQ:
823 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
824 rcode = highlevel_write(host, source, dest, data+3,
825 addr, 4, flags);
827 if (!write_acked
828 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
829 && (rcode >= 0)) {
830 /* not a broadcast write, reply */
831 PREP_REPLY_PACKET(0);
832 fill_async_write_resp(packet, rcode);
833 send_packet_nocare(packet);
835 break;
837 case TCODE_WRITEB:
838 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
839 rcode = highlevel_write(host, source, dest, data+4,
840 addr, data[3]>>16, flags);
842 if (!write_acked
843 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
844 && (rcode >= 0)) {
845 /* not a broadcast write, reply */
846 PREP_REPLY_PACKET(0);
847 fill_async_write_resp(packet, rcode);
848 send_packet_nocare(packet);
850 break;
852 case TCODE_READQ:
853 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
854 rcode = highlevel_read(host, source, &buffer, addr, 4, flags);
856 if (rcode >= 0) {
857 PREP_REPLY_PACKET(0);
858 fill_async_readquad_resp(packet, rcode, buffer);
859 send_packet_nocare(packet);
861 break;
863 case TCODE_READB:
864 length = data[3] >> 16;
865 PREP_REPLY_PACKET(length);
867 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
868 rcode = highlevel_read(host, source, packet->data, addr,
869 length, flags);
871 if (rcode >= 0) {
872 fill_async_readblock_resp(packet, rcode, length);
873 send_packet_nocare(packet);
874 } else {
875 hpsb_free_packet(packet);
877 break;
879 case TCODE_LOCK_REQUEST:
880 length = data[3] >> 16;
881 extcode = data[3] & 0xffff;
882 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
884 PREP_REPLY_PACKET(8);
886 if ((extcode == 0) || (extcode >= 7)) {
887 /* let switch default handle error */
888 length = 0;
891 switch (length) {
892 case 4:
893 rcode = highlevel_lock(host, source, packet->data, addr,
894 data[4], 0, extcode,flags);
895 fill_async_lock_resp(packet, rcode, extcode, 4);
896 break;
897 case 8:
898 if ((extcode != EXTCODE_FETCH_ADD)
899 && (extcode != EXTCODE_LITTLE_ADD)) {
900 rcode = highlevel_lock(host, source,
901 packet->data, addr,
902 data[5], data[4],
903 extcode, flags);
904 fill_async_lock_resp(packet, rcode, extcode, 4);
905 } else {
906 rcode = highlevel_lock64(host, source,
907 (octlet_t *)packet->data, addr,
908 *(octlet_t *)(data + 4), 0ULL,
909 extcode, flags);
910 fill_async_lock_resp(packet, rcode, extcode, 8);
912 break;
913 case 16:
914 rcode = highlevel_lock64(host, source,
915 (octlet_t *)packet->data, addr,
916 *(octlet_t *)(data + 6),
917 *(octlet_t *)(data + 4),
918 extcode, flags);
919 fill_async_lock_resp(packet, rcode, extcode, 8);
920 break;
921 default:
922 rcode = RCODE_TYPE_ERROR;
923 fill_async_lock_resp(packet, rcode,
924 extcode, 0);
927 if (rcode >= 0) {
928 send_packet_nocare(packet);
929 } else {
930 hpsb_free_packet(packet);
932 break;
936 #undef PREP_REPLY_PACKET
939 void hpsb_packet_received(struct hpsb_host *host, quadlet_t *data, size_t size,
940 int write_acked)
942 int tcode;
944 if (host->in_bus_reset) {
945 HPSB_INFO("received packet during reset; ignoring");
946 return;
949 dump_packet("received packet", data, size, -1);
951 tcode = (data[0] >> 4) & 0xf;
953 switch (tcode) {
954 case TCODE_WRITE_RESPONSE:
955 case TCODE_READQ_RESPONSE:
956 case TCODE_READB_RESPONSE:
957 case TCODE_LOCK_RESPONSE:
958 handle_packet_response(host, tcode, data, size);
959 break;
961 case TCODE_WRITEQ:
962 case TCODE_WRITEB:
963 case TCODE_READQ:
964 case TCODE_READB:
965 case TCODE_LOCK_REQUEST:
966 handle_incoming_packet(host, tcode, data, size, write_acked);
967 break;
970 case TCODE_ISO_DATA:
971 highlevel_iso_receive(host, data, size);
972 break;
974 case TCODE_CYCLE_START:
975 /* simply ignore this packet if it is passed on */
976 break;
978 default:
979 HPSB_NOTICE("received packet with bogus transaction code %d",
980 tcode);
981 break;
986 static void abort_requests(struct hpsb_host *host)
988 struct hpsb_packet *packet;
989 struct sk_buff *skb;
991 host->driver->devctl(host, CANCEL_REQUESTS, 0);
993 while ((skb = skb_dequeue(&host->pending_packet_queue)) != NULL) {
994 packet = (struct hpsb_packet *)skb->data;
996 packet->state = hpsb_complete;
997 packet->ack_code = ACKX_ABORTED;
998 queue_packet_complete(packet);
1002 void abort_timedouts(unsigned long __opaque)
1004 struct hpsb_host *host = (struct hpsb_host *)__opaque;
1005 unsigned long flags;
1006 struct hpsb_packet *packet;
1007 struct sk_buff *skb;
1008 unsigned long expire;
1010 spin_lock_irqsave(&host->csr.lock, flags);
1011 expire = host->csr.expire;
1012 spin_unlock_irqrestore(&host->csr.lock, flags);
1014 /* Hold the lock around this, since we aren't dequeuing all
1015 * packets, just ones we need. */
1016 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
1018 while (!skb_queue_empty(&host->pending_packet_queue)) {
1019 skb = skb_peek(&host->pending_packet_queue);
1021 packet = (struct hpsb_packet *)skb->data;
1023 if (time_before(packet->sendtime + expire, jiffies)) {
1024 __skb_unlink(skb, &host->pending_packet_queue);
1025 packet->state = hpsb_complete;
1026 packet->ack_code = ACKX_TIMEOUT;
1027 queue_packet_complete(packet);
1028 } else {
1029 /* Since packets are added to the tail, the oldest
1030 * ones are first, always. When we get to one that
1031 * isn't timed out, the rest aren't either. */
1032 break;
1036 if (!skb_queue_empty(&host->pending_packet_queue))
1037 mod_timer(&host->timeout, jiffies + host->timeout_interval);
1039 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
1043 /* Kernel thread and vars, which handles packets that are completed. Only
1044 * packets that have a "complete" function are sent here. This way, the
1045 * completion is run out of kernel context, and doesn't block the rest of
1046 * the stack. */
1047 static struct task_struct *khpsbpkt_thread;
1048 static struct sk_buff_head hpsbpkt_queue;
1050 static void queue_packet_complete(struct hpsb_packet *packet)
1052 if (packet->no_waiter) {
1053 hpsb_free_packet(packet);
1054 return;
1056 if (packet->complete_routine != NULL) {
1057 skb_queue_tail(&hpsbpkt_queue, packet->skb);
1058 wake_up_process(khpsbpkt_thread);
1060 return;
1063 static int hpsbpkt_thread(void *__hi)
1065 struct sk_buff *skb;
1066 struct hpsb_packet *packet;
1067 void (*complete_routine)(void*);
1068 void *complete_data;
1070 current->flags |= PF_NOFREEZE;
1072 while (!kthread_should_stop()) {
1073 while ((skb = skb_dequeue(&hpsbpkt_queue)) != NULL) {
1074 packet = (struct hpsb_packet *)skb->data;
1076 complete_routine = packet->complete_routine;
1077 complete_data = packet->complete_data;
1079 packet->complete_routine = packet->complete_data = NULL;
1081 complete_routine(complete_data);
1084 set_current_state(TASK_INTERRUPTIBLE);
1085 if (!skb_peek(&hpsbpkt_queue))
1086 schedule();
1087 __set_current_state(TASK_RUNNING);
1089 return 0;
1092 static int __init ieee1394_init(void)
1094 int i, ret;
1096 skb_queue_head_init(&hpsbpkt_queue);
1098 /* non-fatal error */
1099 if (hpsb_init_config_roms()) {
1100 HPSB_ERR("Failed to initialize some config rom entries.\n");
1101 HPSB_ERR("Some features may not be available\n");
1104 khpsbpkt_thread = kthread_run(hpsbpkt_thread, NULL, "khpsbpkt");
1105 if (IS_ERR(khpsbpkt_thread)) {
1106 HPSB_ERR("Failed to start hpsbpkt thread!\n");
1107 ret = PTR_ERR(khpsbpkt_thread);
1108 goto exit_cleanup_config_roms;
1111 if (register_chrdev_region(IEEE1394_CORE_DEV, 256, "ieee1394")) {
1112 HPSB_ERR("unable to register character device major %d!\n", IEEE1394_MAJOR);
1113 ret = -ENODEV;
1114 goto exit_release_kernel_thread;
1117 ret = bus_register(&ieee1394_bus_type);
1118 if (ret < 0) {
1119 HPSB_INFO("bus register failed");
1120 goto release_chrdev;
1123 for (i = 0; fw_bus_attrs[i]; i++) {
1124 ret = bus_create_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1125 if (ret < 0) {
1126 while (i >= 0) {
1127 bus_remove_file(&ieee1394_bus_type,
1128 fw_bus_attrs[i--]);
1130 bus_unregister(&ieee1394_bus_type);
1131 goto release_chrdev;
1135 ret = class_register(&hpsb_host_class);
1136 if (ret < 0)
1137 goto release_all_bus;
1139 hpsb_protocol_class = class_create(THIS_MODULE, "ieee1394_protocol");
1140 if (IS_ERR(hpsb_protocol_class)) {
1141 ret = PTR_ERR(hpsb_protocol_class);
1142 goto release_class_host;
1145 ret = init_csr();
1146 if (ret) {
1147 HPSB_INFO("init csr failed");
1148 ret = -ENOMEM;
1149 goto release_class_protocol;
1152 if (disable_nodemgr) {
1153 HPSB_INFO("nodemgr and IRM functionality disabled");
1154 /* We shouldn't contend for IRM with nodemgr disabled, since
1155 nodemgr implements functionality required of ieee1394a-2000
1156 IRMs */
1157 hpsb_disable_irm = 1;
1159 return 0;
1162 if (hpsb_disable_irm) {
1163 HPSB_INFO("IRM functionality disabled");
1166 ret = init_ieee1394_nodemgr();
1167 if (ret < 0) {
1168 HPSB_INFO("init nodemgr failed");
1169 goto cleanup_csr;
1172 return 0;
1174 cleanup_csr:
1175 cleanup_csr();
1176 release_class_protocol:
1177 class_destroy(hpsb_protocol_class);
1178 release_class_host:
1179 class_unregister(&hpsb_host_class);
1180 release_all_bus:
1181 for (i = 0; fw_bus_attrs[i]; i++)
1182 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1183 bus_unregister(&ieee1394_bus_type);
1184 release_chrdev:
1185 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
1186 exit_release_kernel_thread:
1187 kthread_stop(khpsbpkt_thread);
1188 exit_cleanup_config_roms:
1189 hpsb_cleanup_config_roms();
1190 return ret;
1193 static void __exit ieee1394_cleanup(void)
1195 int i;
1197 if (!disable_nodemgr)
1198 cleanup_ieee1394_nodemgr();
1200 cleanup_csr();
1202 class_destroy(hpsb_protocol_class);
1203 class_unregister(&hpsb_host_class);
1204 for (i = 0; fw_bus_attrs[i]; i++)
1205 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1206 bus_unregister(&ieee1394_bus_type);
1208 kthread_stop(khpsbpkt_thread);
1210 hpsb_cleanup_config_roms();
1212 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
1215 fs_initcall(ieee1394_init); /* same as ohci1394 */
1216 module_exit(ieee1394_cleanup);
1218 /* Exported symbols */
1220 /** hosts.c **/
1221 EXPORT_SYMBOL(hpsb_alloc_host);
1222 EXPORT_SYMBOL(hpsb_add_host);
1223 EXPORT_SYMBOL(hpsb_resume_host);
1224 EXPORT_SYMBOL(hpsb_remove_host);
1225 EXPORT_SYMBOL(hpsb_update_config_rom_image);
1227 /** ieee1394_core.c **/
1228 EXPORT_SYMBOL(hpsb_speedto_str);
1229 EXPORT_SYMBOL(hpsb_protocol_class);
1230 EXPORT_SYMBOL(hpsb_set_packet_complete_task);
1231 EXPORT_SYMBOL(hpsb_alloc_packet);
1232 EXPORT_SYMBOL(hpsb_free_packet);
1233 EXPORT_SYMBOL(hpsb_send_packet);
1234 EXPORT_SYMBOL(hpsb_reset_bus);
1235 EXPORT_SYMBOL(hpsb_read_cycle_timer);
1236 EXPORT_SYMBOL(hpsb_bus_reset);
1237 EXPORT_SYMBOL(hpsb_selfid_received);
1238 EXPORT_SYMBOL(hpsb_selfid_complete);
1239 EXPORT_SYMBOL(hpsb_packet_sent);
1240 EXPORT_SYMBOL(hpsb_packet_received);
1241 EXPORT_SYMBOL_GPL(hpsb_disable_irm);
1243 /** ieee1394_transactions.c **/
1244 EXPORT_SYMBOL(hpsb_get_tlabel);
1245 EXPORT_SYMBOL(hpsb_free_tlabel);
1246 EXPORT_SYMBOL(hpsb_make_readpacket);
1247 EXPORT_SYMBOL(hpsb_make_writepacket);
1248 EXPORT_SYMBOL(hpsb_make_streampacket);
1249 EXPORT_SYMBOL(hpsb_make_lockpacket);
1250 EXPORT_SYMBOL(hpsb_make_lock64packet);
1251 EXPORT_SYMBOL(hpsb_make_phypacket);
1252 EXPORT_SYMBOL(hpsb_make_isopacket);
1253 EXPORT_SYMBOL(hpsb_read);
1254 EXPORT_SYMBOL(hpsb_write);
1255 EXPORT_SYMBOL(hpsb_packet_success);
1257 /** highlevel.c **/
1258 EXPORT_SYMBOL(hpsb_register_highlevel);
1259 EXPORT_SYMBOL(hpsb_unregister_highlevel);
1260 EXPORT_SYMBOL(hpsb_register_addrspace);
1261 EXPORT_SYMBOL(hpsb_unregister_addrspace);
1262 EXPORT_SYMBOL(hpsb_allocate_and_register_addrspace);
1263 EXPORT_SYMBOL(hpsb_listen_channel);
1264 EXPORT_SYMBOL(hpsb_unlisten_channel);
1265 EXPORT_SYMBOL(hpsb_get_hostinfo);
1266 EXPORT_SYMBOL(hpsb_create_hostinfo);
1267 EXPORT_SYMBOL(hpsb_destroy_hostinfo);
1268 EXPORT_SYMBOL(hpsb_set_hostinfo_key);
1269 EXPORT_SYMBOL(hpsb_get_hostinfo_bykey);
1270 EXPORT_SYMBOL(hpsb_set_hostinfo);
1271 EXPORT_SYMBOL(highlevel_host_reset);
1273 /** nodemgr.c **/
1274 EXPORT_SYMBOL(hpsb_node_fill_packet);
1275 EXPORT_SYMBOL(hpsb_node_write);
1276 EXPORT_SYMBOL(__hpsb_register_protocol);
1277 EXPORT_SYMBOL(hpsb_unregister_protocol);
1279 /** csr.c **/
1280 EXPORT_SYMBOL(hpsb_update_config_rom);
1282 /** dma.c **/
1283 EXPORT_SYMBOL(dma_prog_region_init);
1284 EXPORT_SYMBOL(dma_prog_region_alloc);
1285 EXPORT_SYMBOL(dma_prog_region_free);
1286 EXPORT_SYMBOL(dma_region_init);
1287 EXPORT_SYMBOL(dma_region_alloc);
1288 EXPORT_SYMBOL(dma_region_free);
1289 EXPORT_SYMBOL(dma_region_sync_for_cpu);
1290 EXPORT_SYMBOL(dma_region_sync_for_device);
1291 EXPORT_SYMBOL(dma_region_mmap);
1292 EXPORT_SYMBOL(dma_region_offset_to_bus);
1294 /** iso.c **/
1295 EXPORT_SYMBOL(hpsb_iso_xmit_init);
1296 EXPORT_SYMBOL(hpsb_iso_recv_init);
1297 EXPORT_SYMBOL(hpsb_iso_xmit_start);
1298 EXPORT_SYMBOL(hpsb_iso_recv_start);
1299 EXPORT_SYMBOL(hpsb_iso_recv_listen_channel);
1300 EXPORT_SYMBOL(hpsb_iso_recv_unlisten_channel);
1301 EXPORT_SYMBOL(hpsb_iso_recv_set_channel_mask);
1302 EXPORT_SYMBOL(hpsb_iso_stop);
1303 EXPORT_SYMBOL(hpsb_iso_shutdown);
1304 EXPORT_SYMBOL(hpsb_iso_xmit_queue_packet);
1305 EXPORT_SYMBOL(hpsb_iso_xmit_sync);
1306 EXPORT_SYMBOL(hpsb_iso_recv_release_packets);
1307 EXPORT_SYMBOL(hpsb_iso_n_ready);
1308 EXPORT_SYMBOL(hpsb_iso_packet_sent);
1309 EXPORT_SYMBOL(hpsb_iso_packet_received);
1310 EXPORT_SYMBOL(hpsb_iso_wake);
1311 EXPORT_SYMBOL(hpsb_iso_recv_flush);
1313 /** csr1212.c **/
1314 EXPORT_SYMBOL(csr1212_new_directory);
1315 EXPORT_SYMBOL(csr1212_attach_keyval_to_directory);
1316 EXPORT_SYMBOL(csr1212_detach_keyval_from_directory);
1317 EXPORT_SYMBOL(csr1212_release_keyval);
1318 EXPORT_SYMBOL(csr1212_read);
1319 EXPORT_SYMBOL(csr1212_parse_keyval);
1320 EXPORT_SYMBOL(_csr1212_read_keyval);
1321 EXPORT_SYMBOL(_csr1212_destroy_keyval);