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.
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"
45 #include "ieee1394_core.h"
46 #include "highlevel.h"
47 #include "ieee1394_transactions.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)
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]);
86 for (i = 0; i < size; i++)
87 printk(" %08x", data[i]);
91 #define dump_packet(a,b,c,d) do {} while (0)
94 static void abort_requests(struct hpsb_host *host);
95 static void queue_packet_complete(struct hpsb_packet *packet);
99 * hpsb_set_packet_complete_task - set task that runs when a packet completes
100 * @packet: the packet whose completion we want the task added to
101 * @routine: function to call
102 * @data: data (if any) to pass to the above function
104 * Set the task that runs when a packet completes. You cannot call this more
105 * than once on a single packet before it is sent.
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;
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
136 struct hpsb_packet *hpsb_alloc_packet(size_t data_size)
138 struct hpsb_packet *packet = NULL;
141 data_size = ((data_size + 3) & ~3);
143 skb = alloc_skb(data_size + sizeof(*packet), GFP_ATOMIC);
147 memset(skb->data, 0, data_size + sizeof(*packet));
149 packet = (struct hpsb_packet *)skb->data;
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);
159 packet->data = (quadlet_t *)(skb->data + sizeof(*packet));
160 packet->data_size = data_size;
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);
183 * hpsb_reset_bus - initiate bus reset on the given host
184 * @host: host controller whose bus to reset
185 * @type: one of enum reset_types
187 * Returns 1 if bus reset already in progress, 0 otherwise.
189 int hpsb_reset_bus(struct hpsb_host *host, int type)
191 if (!host->in_bus_reset) {
192 host->driver->devctl(host, RESET_BUS, type);
200 * hpsb_read_cycle_timer - read cycle timer register and system time
201 * @host: host whose isochronous cycle timer register is read
202 * @cycle_timer: address of bitfield to return the register contents
203 * @local_time: address to return the system time
205 * The format of * @cycle_timer, is described in OHCI 1.1 clause 5.13. This
206 * format is also read from non-OHCI controllers. * @local_time contains the
207 * system time in microseconds since the Epoch, read at the moment when the
208 * cycle timer was read.
210 * Return value: 0 for success or error number otherwise.
212 int hpsb_read_cycle_timer(struct hpsb_host *host, u32 *cycle_timer,
219 if (!host || !cycle_timer || !local_time)
223 local_irq_save(flags);
225 ctr = host->driver->devctl(host, GET_CYCLE_COUNTER, 0);
227 do_gettimeofday(&tv);
229 local_irq_restore(flags);
235 *local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
240 * hpsb_bus_reset - notify a bus reset to the core
242 * For host driver module usage. Safe to use in interrupt context, although
243 * quite complex; so you may want to run it in the bottom rather than top half.
245 * Returns 1 if bus reset already in progress, 0 otherwise.
247 int hpsb_bus_reset(struct hpsb_host *host)
249 if (host->in_bus_reset) {
250 HPSB_NOTICE("%s called while bus reset already in progress",
255 abort_requests(host);
256 host->in_bus_reset = 1;
259 host->busmgr_id = -1;
262 host->node_count = 0;
263 host->selfid_count = 0;
270 * Verify num_of_selfids SelfIDs and return number of nodes. Return zero in
271 * case verification failed.
273 static int check_selfids(struct hpsb_host *host)
276 int rest_of_selfids = host->selfid_count;
277 struct selfid *sid = (struct selfid *)host->topology_map;
278 struct ext_selfid *esid;
281 host->nodes_active = 0;
283 while (rest_of_selfids--) {
284 if (!sid->extended) {
288 if (sid->phy_id != nodeid) {
289 HPSB_INFO("SelfIDs failed monotony check with "
294 if (sid->link_active) {
295 host->nodes_active++;
297 host->irm_id = LOCAL_BUS | sid->phy_id;
300 esid = (struct ext_selfid *)sid;
302 if ((esid->phy_id != nodeid)
303 || (esid->seq_nr != esid_seq)) {
304 HPSB_INFO("SelfIDs failed monotony check with "
305 "%d/%d", esid->phy_id, esid->seq_nr);
313 esid = (struct ext_selfid *)(sid - 1);
314 while (esid->extended) {
315 if ((esid->porta == SELFID_PORT_PARENT) ||
316 (esid->portb == SELFID_PORT_PARENT) ||
317 (esid->portc == SELFID_PORT_PARENT) ||
318 (esid->portd == SELFID_PORT_PARENT) ||
319 (esid->porte == SELFID_PORT_PARENT) ||
320 (esid->portf == SELFID_PORT_PARENT) ||
321 (esid->portg == SELFID_PORT_PARENT) ||
322 (esid->porth == SELFID_PORT_PARENT)) {
323 HPSB_INFO("SelfIDs failed root check on "
330 sid = (struct selfid *)esid;
331 if ((sid->port0 == SELFID_PORT_PARENT) ||
332 (sid->port1 == SELFID_PORT_PARENT) ||
333 (sid->port2 == SELFID_PORT_PARENT)) {
334 HPSB_INFO("SelfIDs failed root check");
338 host->node_count = nodeid + 1;
342 static void build_speed_map(struct hpsb_host *host, int nodecount)
344 u8 cldcnt[nodecount];
345 u8 *map = host->speed_map;
346 u8 *speedcap = host->speed;
348 struct ext_selfid *esid;
351 for (i = 0; i < (nodecount * 64); i += 64) {
352 for (j = 0; j < nodecount; j++) {
353 map[i+j] = IEEE1394_SPEED_MAX;
357 for (i = 0; i < nodecount; i++) {
361 /* find direct children count and speed */
362 for (sid = (struct selfid *)&host->topology_map[host->selfid_count-1],
364 (void *)sid >= (void *)host->topology_map; sid--) {
366 esid = (struct ext_selfid *)sid;
368 if (esid->porta == SELFID_PORT_CHILD) cldcnt[n]++;
369 if (esid->portb == SELFID_PORT_CHILD) cldcnt[n]++;
370 if (esid->portc == SELFID_PORT_CHILD) cldcnt[n]++;
371 if (esid->portd == SELFID_PORT_CHILD) cldcnt[n]++;
372 if (esid->porte == SELFID_PORT_CHILD) cldcnt[n]++;
373 if (esid->portf == SELFID_PORT_CHILD) cldcnt[n]++;
374 if (esid->portg == SELFID_PORT_CHILD) cldcnt[n]++;
375 if (esid->porth == SELFID_PORT_CHILD) cldcnt[n]++;
377 if (sid->port0 == SELFID_PORT_CHILD) cldcnt[n]++;
378 if (sid->port1 == SELFID_PORT_CHILD) cldcnt[n]++;
379 if (sid->port2 == SELFID_PORT_CHILD) cldcnt[n]++;
381 speedcap[n] = sid->speed;
386 /* set self mapping */
387 for (i = 0; i < nodecount; i++) {
388 map[64*i + i] = speedcap[i];
391 /* fix up direct children count to total children count;
392 * also fix up speedcaps for sibling and parent communication */
393 for (i = 1; i < nodecount; i++) {
394 for (j = cldcnt[i], n = i - 1; j > 0; j--) {
395 cldcnt[i] += cldcnt[n];
396 speedcap[n] = min(speedcap[n], speedcap[i]);
401 for (n = 0; n < nodecount; n++) {
402 for (i = n - cldcnt[n]; i <= n; i++) {
403 for (j = 0; j < (n - cldcnt[n]); j++) {
404 map[j*64 + i] = map[i*64 + j] =
405 min(map[i*64 + j], speedcap[n]);
407 for (j = n + 1; j < nodecount; j++) {
408 map[j*64 + i] = map[i*64 + j] =
409 min(map[i*64 + j], speedcap[n]);
414 #if SELFID_SPEED_UNKNOWN != IEEE1394_SPEED_MAX
415 /* assume maximum speed for 1394b PHYs, nodemgr will correct it */
416 for (n = 0; n < nodecount; n++)
417 if (speedcap[n] == SELFID_SPEED_UNKNOWN)
418 speedcap[n] = IEEE1394_SPEED_MAX;
424 * hpsb_selfid_received - hand over received selfid packet to the core
426 * For host driver module usage. Safe to use in interrupt context.
428 * The host driver should have done a successful complement check (second
429 * quadlet is complement of first) beforehand.
431 void hpsb_selfid_received(struct hpsb_host *host, quadlet_t sid)
433 if (host->in_bus_reset) {
434 HPSB_VERBOSE("Including SelfID 0x%x", sid);
435 host->topology_map[host->selfid_count++] = sid;
437 HPSB_NOTICE("Spurious SelfID packet (0x%08x) received from bus %d",
438 sid, NODEID_TO_BUS(host->node_id));
443 * hpsb_selfid_complete - notify completion of SelfID stage to the core
445 * For host driver module usage. Safe to use in interrupt context, although
446 * quite complex; so you may want to run it in the bottom rather than top half.
448 * Notify completion of SelfID stage to the core and report new physical ID
449 * and whether host is root now.
451 void hpsb_selfid_complete(struct hpsb_host *host, int phyid, int isroot)
453 if (!host->in_bus_reset)
454 HPSB_NOTICE("SelfID completion called outside of bus reset!");
456 host->node_id = LOCAL_BUS | phyid;
457 host->is_root = isroot;
459 if (!check_selfids(host)) {
460 if (host->reset_retries++ < 20) {
461 /* selfid stage did not complete without error */
462 HPSB_NOTICE("Error in SelfID stage, resetting");
463 host->in_bus_reset = 0;
464 /* this should work from ohci1394 now... */
465 hpsb_reset_bus(host, LONG_RESET);
468 HPSB_NOTICE("Stopping out-of-control reset loop");
469 HPSB_NOTICE("Warning - topology map and speed map will not be valid");
470 host->reset_retries = 0;
473 host->reset_retries = 0;
474 build_speed_map(host, host->node_count);
477 HPSB_VERBOSE("selfid_complete called with successful SelfID stage "
478 "... irm_id: 0x%X node_id: 0x%X",host->irm_id,host->node_id);
480 /* irm_id is kept up to date by check_selfids() */
481 if (host->irm_id == host->node_id) {
489 host->driver->devctl(host, ACT_CYCLE_MASTER, 1);
492 atomic_inc(&host->generation);
493 host->in_bus_reset = 0;
494 highlevel_host_reset(host);
498 * hpsb_packet_sent - notify core of sending a packet
500 * For host driver module usage. Safe to call from within a transmit packet
503 * Notify core of sending a packet. Ackcode is the ack code returned for async
504 * transmits or ACKX_SEND_ERROR if the transmission failed completely; ACKX_NONE
505 * for other cases (internal errors that don't justify a panic).
507 void hpsb_packet_sent(struct hpsb_host *host, struct hpsb_packet *packet,
512 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
514 packet->ack_code = ackcode;
516 if (packet->no_waiter || packet->state == hpsb_complete) {
517 /* if packet->no_waiter, must not have a tlabel allocated */
518 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
519 hpsb_free_packet(packet);
523 atomic_dec(&packet->refcnt); /* drop HC's reference */
524 /* here the packet must be on the host->pending_packet_queue */
526 if (ackcode != ACK_PENDING || !packet->expect_response) {
527 packet->state = hpsb_complete;
528 __skb_unlink(packet->skb, &host->pending_packet_queue);
529 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
530 queue_packet_complete(packet);
534 packet->state = hpsb_pending;
535 packet->sendtime = jiffies;
537 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
539 mod_timer(&host->timeout, jiffies + host->timeout_interval);
543 * hpsb_send_phy_config - transmit a PHY configuration packet on the bus
544 * @host: host that PHY config packet gets sent through
545 * @rootid: root whose force_root bit should get set (-1 = don't set force_root)
546 * @gapcnt: gap count value to set (-1 = don't set gap count)
548 * This function sends a PHY config packet on the bus through the specified
551 * Return value: 0 for success or negative error number otherwise.
553 int hpsb_send_phy_config(struct hpsb_host *host, int rootid, int gapcnt)
555 struct hpsb_packet *packet;
559 if (rootid >= ALL_NODES || rootid < -1 || gapcnt > 0x3f || gapcnt < -1 ||
560 (rootid == -1 && gapcnt == -1)) {
561 HPSB_DEBUG("Invalid Parameter: rootid = %d gapcnt = %d",
567 d |= PHYPACKET_PHYCONFIG_R | rootid << PHYPACKET_PORT_SHIFT;
569 d |= PHYPACKET_PHYCONFIG_T | gapcnt << PHYPACKET_GAPCOUNT_SHIFT;
571 packet = hpsb_make_phypacket(host, d);
575 packet->generation = get_hpsb_generation(host);
576 retval = hpsb_send_packet_and_wait(packet);
577 hpsb_free_packet(packet);
583 * hpsb_send_packet - transmit a packet on the bus
584 * @packet: packet to send
586 * The packet is sent through the host specified in the packet->host field.
587 * Before sending, the packet's transmit speed is automatically determined
588 * using the local speed map when it is an async, non-broadcast packet.
590 * Possibilities for failure are that host is either not initialized, in bus
591 * reset, the packet's generation number doesn't match the current generation
592 * number or the host reports a transmit error.
594 * Return value: 0 on success, negative errno on failure.
596 int hpsb_send_packet(struct hpsb_packet *packet)
598 struct hpsb_host *host = packet->host;
600 if (host->is_shutdown)
602 if (host->in_bus_reset ||
603 (packet->generation != get_hpsb_generation(host)))
606 packet->state = hpsb_queued;
608 /* This just seems silly to me */
609 WARN_ON(packet->no_waiter && packet->expect_response);
611 if (!packet->no_waiter || packet->expect_response) {
612 atomic_inc(&packet->refcnt);
613 /* Set the initial "sendtime" to 10 seconds from now, to
614 prevent premature expiry. If a packet takes more than
615 10 seconds to hit the wire, we have bigger problems :) */
616 packet->sendtime = jiffies + 10 * HZ;
617 skb_queue_tail(&host->pending_packet_queue, packet->skb);
620 if (packet->node_id == host->node_id) {
621 /* it is a local request, so handle it locally */
624 size_t size = packet->data_size + packet->header_size;
626 data = kmalloc(size, GFP_ATOMIC);
628 HPSB_ERR("unable to allocate memory for concatenating header and data");
632 memcpy(data, packet->header, packet->header_size);
634 if (packet->data_size)
635 memcpy(((u8*)data) + packet->header_size, packet->data, packet->data_size);
637 dump_packet("send packet local", packet->header, packet->header_size, -1);
639 hpsb_packet_sent(host, packet, packet->expect_response ? ACK_PENDING : ACK_COMPLETE);
640 hpsb_packet_received(host, data, size, 0);
647 if (packet->type == hpsb_async &&
648 NODEID_TO_NODE(packet->node_id) != ALL_NODES)
650 host->speed[NODEID_TO_NODE(packet->node_id)];
652 dump_packet("send packet", packet->header, packet->header_size, packet->speed_code);
654 return host->driver->transmit_packet(host, packet);
657 /* We could just use complete() directly as the packet complete
658 * callback, but this is more typesafe, in the sense that we get a
659 * compiler error if the prototype for complete() changes. */
661 static void complete_packet(void *data)
663 complete((struct completion *) data);
667 * hpsb_send_packet_and_wait - enqueue packet, block until transaction completes
668 * @packet: packet to send
670 * Return value: 0 on success, negative errno on failure.
672 int hpsb_send_packet_and_wait(struct hpsb_packet *packet)
674 struct completion done;
677 init_completion(&done);
678 hpsb_set_packet_complete_task(packet, complete_packet, &done);
679 retval = hpsb_send_packet(packet);
681 wait_for_completion(&done);
686 static void send_packet_nocare(struct hpsb_packet *packet)
688 if (hpsb_send_packet(packet) < 0) {
689 hpsb_free_packet(packet);
694 static void handle_packet_response(struct hpsb_host *host, int tcode,
695 quadlet_t *data, size_t size)
697 struct hpsb_packet *packet = NULL;
703 tlabel = (data[0] >> 10) & 0x3f;
705 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
707 skb_queue_walk(&host->pending_packet_queue, skb) {
708 packet = (struct hpsb_packet *)skb->data;
709 if ((packet->tlabel == tlabel)
710 && (packet->node_id == (data[1] >> 16))){
717 if (packet == NULL) {
718 HPSB_DEBUG("unsolicited response packet received - no tlabel match");
719 dump_packet("contents", data, 16, -1);
720 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
724 switch (packet->tcode) {
727 if (tcode != TCODE_WRITE_RESPONSE)
730 memcpy(packet->header, data, 12);
733 if (tcode != TCODE_READQ_RESPONSE)
736 memcpy(packet->header, data, 16);
739 if (tcode != TCODE_READB_RESPONSE)
742 BUG_ON(packet->skb->len - sizeof(*packet) < size - 16);
743 memcpy(packet->header, data, 16);
744 memcpy(packet->data, data + 4, size - 16);
746 case TCODE_LOCK_REQUEST:
747 if (tcode != TCODE_LOCK_RESPONSE)
750 size = min((size - 16), (size_t)8);
751 BUG_ON(packet->skb->len - sizeof(*packet) < size);
752 memcpy(packet->header, data, 16);
753 memcpy(packet->data, data + 4, size);
758 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
759 HPSB_INFO("unsolicited response packet received - tcode mismatch");
760 dump_packet("contents", data, 16, -1);
764 __skb_unlink(skb, &host->pending_packet_queue);
766 if (packet->state == hpsb_queued) {
767 packet->sendtime = jiffies;
768 packet->ack_code = ACK_PENDING;
771 packet->state = hpsb_complete;
772 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
774 queue_packet_complete(packet);
778 static struct hpsb_packet *create_reply_packet(struct hpsb_host *host,
779 quadlet_t *data, size_t dsize)
781 struct hpsb_packet *p;
783 p = hpsb_alloc_packet(dsize);
784 if (unlikely(p == NULL)) {
785 /* FIXME - send data_error response */
789 p->type = hpsb_async;
790 p->state = hpsb_unused;
792 p->node_id = data[1] >> 16;
793 p->tlabel = (data[0] >> 10) & 0x3f;
796 p->generation = get_hpsb_generation(host);
799 p->data[dsize / 4] = 0;
804 #define PREP_ASYNC_HEAD_RCODE(tc) \
805 packet->tcode = tc; \
806 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
807 | (1 << 8) | (tc << 4); \
808 packet->header[1] = (packet->host->node_id << 16) | (rcode << 12); \
809 packet->header[2] = 0
811 static void fill_async_readquad_resp(struct hpsb_packet *packet, int rcode,
814 PREP_ASYNC_HEAD_RCODE(TCODE_READQ_RESPONSE);
815 packet->header[3] = data;
816 packet->header_size = 16;
817 packet->data_size = 0;
820 static void fill_async_readblock_resp(struct hpsb_packet *packet, int rcode,
823 if (rcode != RCODE_COMPLETE)
826 PREP_ASYNC_HEAD_RCODE(TCODE_READB_RESPONSE);
827 packet->header[3] = length << 16;
828 packet->header_size = 16;
829 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
832 static void fill_async_write_resp(struct hpsb_packet *packet, int rcode)
834 PREP_ASYNC_HEAD_RCODE(TCODE_WRITE_RESPONSE);
835 packet->header[2] = 0;
836 packet->header_size = 12;
837 packet->data_size = 0;
840 static void fill_async_lock_resp(struct hpsb_packet *packet, int rcode, int extcode,
843 if (rcode != RCODE_COMPLETE)
846 PREP_ASYNC_HEAD_RCODE(TCODE_LOCK_RESPONSE);
847 packet->header[3] = (length << 16) | extcode;
848 packet->header_size = 16;
849 packet->data_size = length;
852 #define PREP_REPLY_PACKET(length) \
853 packet = create_reply_packet(host, data, length); \
854 if (packet == NULL) break
856 static void handle_incoming_packet(struct hpsb_host *host, int tcode,
857 quadlet_t *data, size_t size, int write_acked)
859 struct hpsb_packet *packet;
860 int length, rcode, extcode;
862 nodeid_t source = data[1] >> 16;
863 nodeid_t dest = data[0] >> 16;
864 u16 flags = (u16) data[0];
867 /* big FIXME - no error checking is done for an out of bounds length */
871 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
872 rcode = highlevel_write(host, source, dest, data+3,
876 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
878 /* not a broadcast write, reply */
879 PREP_REPLY_PACKET(0);
880 fill_async_write_resp(packet, rcode);
881 send_packet_nocare(packet);
886 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
887 rcode = highlevel_write(host, source, dest, data+4,
888 addr, data[3]>>16, flags);
891 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
893 /* not a broadcast write, reply */
894 PREP_REPLY_PACKET(0);
895 fill_async_write_resp(packet, rcode);
896 send_packet_nocare(packet);
901 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
902 rcode = highlevel_read(host, source, &buffer, addr, 4, flags);
905 PREP_REPLY_PACKET(0);
906 fill_async_readquad_resp(packet, rcode, buffer);
907 send_packet_nocare(packet);
912 length = data[3] >> 16;
913 PREP_REPLY_PACKET(length);
915 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
916 rcode = highlevel_read(host, source, packet->data, addr,
920 fill_async_readblock_resp(packet, rcode, length);
921 send_packet_nocare(packet);
923 hpsb_free_packet(packet);
927 case TCODE_LOCK_REQUEST:
928 length = data[3] >> 16;
929 extcode = data[3] & 0xffff;
930 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
932 PREP_REPLY_PACKET(8);
934 if ((extcode == 0) || (extcode >= 7)) {
935 /* let switch default handle error */
941 rcode = highlevel_lock(host, source, packet->data, addr,
942 data[4], 0, extcode,flags);
943 fill_async_lock_resp(packet, rcode, extcode, 4);
946 if ((extcode != EXTCODE_FETCH_ADD)
947 && (extcode != EXTCODE_LITTLE_ADD)) {
948 rcode = highlevel_lock(host, source,
952 fill_async_lock_resp(packet, rcode, extcode, 4);
954 rcode = highlevel_lock64(host, source,
955 (octlet_t *)packet->data, addr,
956 *(octlet_t *)(data + 4), 0ULL,
958 fill_async_lock_resp(packet, rcode, extcode, 8);
962 rcode = highlevel_lock64(host, source,
963 (octlet_t *)packet->data, addr,
964 *(octlet_t *)(data + 6),
965 *(octlet_t *)(data + 4),
967 fill_async_lock_resp(packet, rcode, extcode, 8);
970 rcode = RCODE_TYPE_ERROR;
971 fill_async_lock_resp(packet, rcode,
976 send_packet_nocare(packet);
978 hpsb_free_packet(packet);
984 #undef PREP_REPLY_PACKET
987 * hpsb_packet_received - hand over received packet to the core
989 * For host driver module usage.
991 * The contents of data are expected to be the full packet but with the CRCs
992 * left out (data block follows header immediately), with the header (i.e. the
993 * first four quadlets) in machine byte order and the data block in big endian.
994 * *@data can be safely overwritten after this call.
996 * If the packet is a write request, @write_acked is to be set to true if it was
997 * ack_complete'd already, false otherwise. This argument is ignored for any
1000 void hpsb_packet_received(struct hpsb_host *host, quadlet_t *data, size_t size,
1005 if (host->in_bus_reset) {
1006 HPSB_INFO("received packet during reset; ignoring");
1010 dump_packet("received packet", data, size, -1);
1012 tcode = (data[0] >> 4) & 0xf;
1015 case TCODE_WRITE_RESPONSE:
1016 case TCODE_READQ_RESPONSE:
1017 case TCODE_READB_RESPONSE:
1018 case TCODE_LOCK_RESPONSE:
1019 handle_packet_response(host, tcode, data, size);
1026 case TCODE_LOCK_REQUEST:
1027 handle_incoming_packet(host, tcode, data, size, write_acked);
1031 case TCODE_ISO_DATA:
1032 highlevel_iso_receive(host, data, size);
1035 case TCODE_CYCLE_START:
1036 /* simply ignore this packet if it is passed on */
1040 HPSB_NOTICE("received packet with bogus transaction code %d",
1047 static void abort_requests(struct hpsb_host *host)
1049 struct hpsb_packet *packet;
1050 struct sk_buff *skb;
1052 host->driver->devctl(host, CANCEL_REQUESTS, 0);
1054 while ((skb = skb_dequeue(&host->pending_packet_queue)) != NULL) {
1055 packet = (struct hpsb_packet *)skb->data;
1057 packet->state = hpsb_complete;
1058 packet->ack_code = ACKX_ABORTED;
1059 queue_packet_complete(packet);
1063 void abort_timedouts(unsigned long __opaque)
1065 struct hpsb_host *host = (struct hpsb_host *)__opaque;
1066 unsigned long flags;
1067 struct hpsb_packet *packet;
1068 struct sk_buff *skb;
1069 unsigned long expire;
1071 spin_lock_irqsave(&host->csr.lock, flags);
1072 expire = host->csr.expire;
1073 spin_unlock_irqrestore(&host->csr.lock, flags);
1075 /* Hold the lock around this, since we aren't dequeuing all
1076 * packets, just ones we need. */
1077 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
1079 while (!skb_queue_empty(&host->pending_packet_queue)) {
1080 skb = skb_peek(&host->pending_packet_queue);
1082 packet = (struct hpsb_packet *)skb->data;
1084 if (time_before(packet->sendtime + expire, jiffies)) {
1085 __skb_unlink(skb, &host->pending_packet_queue);
1086 packet->state = hpsb_complete;
1087 packet->ack_code = ACKX_TIMEOUT;
1088 queue_packet_complete(packet);
1090 /* Since packets are added to the tail, the oldest
1091 * ones are first, always. When we get to one that
1092 * isn't timed out, the rest aren't either. */
1097 if (!skb_queue_empty(&host->pending_packet_queue))
1098 mod_timer(&host->timeout, jiffies + host->timeout_interval);
1100 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
1104 /* Kernel thread and vars, which handles packets that are completed. Only
1105 * packets that have a "complete" function are sent here. This way, the
1106 * completion is run out of kernel context, and doesn't block the rest of
1108 static struct task_struct *khpsbpkt_thread;
1109 static struct sk_buff_head hpsbpkt_queue;
1111 static void queue_packet_complete(struct hpsb_packet *packet)
1113 if (packet->no_waiter) {
1114 hpsb_free_packet(packet);
1117 if (packet->complete_routine != NULL) {
1118 skb_queue_tail(&hpsbpkt_queue, packet->skb);
1119 wake_up_process(khpsbpkt_thread);
1124 static int hpsbpkt_thread(void *__hi)
1126 struct sk_buff *skb;
1127 struct hpsb_packet *packet;
1128 void (*complete_routine)(void*);
1129 void *complete_data;
1131 current->flags |= PF_NOFREEZE;
1133 while (!kthread_should_stop()) {
1134 while ((skb = skb_dequeue(&hpsbpkt_queue)) != NULL) {
1135 packet = (struct hpsb_packet *)skb->data;
1137 complete_routine = packet->complete_routine;
1138 complete_data = packet->complete_data;
1140 packet->complete_routine = packet->complete_data = NULL;
1142 complete_routine(complete_data);
1145 set_current_state(TASK_INTERRUPTIBLE);
1146 if (!skb_peek(&hpsbpkt_queue))
1148 __set_current_state(TASK_RUNNING);
1153 static int __init ieee1394_init(void)
1157 skb_queue_head_init(&hpsbpkt_queue);
1159 /* non-fatal error */
1160 if (hpsb_init_config_roms()) {
1161 HPSB_ERR("Failed to initialize some config rom entries.\n");
1162 HPSB_ERR("Some features may not be available\n");
1165 khpsbpkt_thread = kthread_run(hpsbpkt_thread, NULL, "khpsbpkt");
1166 if (IS_ERR(khpsbpkt_thread)) {
1167 HPSB_ERR("Failed to start hpsbpkt thread!\n");
1168 ret = PTR_ERR(khpsbpkt_thread);
1169 goto exit_cleanup_config_roms;
1172 if (register_chrdev_region(IEEE1394_CORE_DEV, 256, "ieee1394")) {
1173 HPSB_ERR("unable to register character device major %d!\n", IEEE1394_MAJOR);
1175 goto exit_release_kernel_thread;
1178 ret = bus_register(&ieee1394_bus_type);
1180 HPSB_INFO("bus register failed");
1181 goto release_chrdev;
1184 for (i = 0; fw_bus_attrs[i]; i++) {
1185 ret = bus_create_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1188 bus_remove_file(&ieee1394_bus_type,
1191 bus_unregister(&ieee1394_bus_type);
1192 goto release_chrdev;
1196 ret = class_register(&hpsb_host_class);
1198 goto release_all_bus;
1200 hpsb_protocol_class = class_create(THIS_MODULE, "ieee1394_protocol");
1201 if (IS_ERR(hpsb_protocol_class)) {
1202 ret = PTR_ERR(hpsb_protocol_class);
1203 goto release_class_host;
1208 HPSB_INFO("init csr failed");
1210 goto release_class_protocol;
1213 if (disable_nodemgr) {
1214 HPSB_INFO("nodemgr and IRM functionality disabled");
1215 /* We shouldn't contend for IRM with nodemgr disabled, since
1216 nodemgr implements functionality required of ieee1394a-2000
1218 hpsb_disable_irm = 1;
1223 if (hpsb_disable_irm) {
1224 HPSB_INFO("IRM functionality disabled");
1227 ret = init_ieee1394_nodemgr();
1229 HPSB_INFO("init nodemgr failed");
1237 release_class_protocol:
1238 class_destroy(hpsb_protocol_class);
1240 class_unregister(&hpsb_host_class);
1242 for (i = 0; fw_bus_attrs[i]; i++)
1243 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1244 bus_unregister(&ieee1394_bus_type);
1246 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
1247 exit_release_kernel_thread:
1248 kthread_stop(khpsbpkt_thread);
1249 exit_cleanup_config_roms:
1250 hpsb_cleanup_config_roms();
1254 static void __exit ieee1394_cleanup(void)
1258 if (!disable_nodemgr)
1259 cleanup_ieee1394_nodemgr();
1263 class_destroy(hpsb_protocol_class);
1264 class_unregister(&hpsb_host_class);
1265 for (i = 0; fw_bus_attrs[i]; i++)
1266 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1267 bus_unregister(&ieee1394_bus_type);
1269 kthread_stop(khpsbpkt_thread);
1271 hpsb_cleanup_config_roms();
1273 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
1276 fs_initcall(ieee1394_init); /* same as ohci1394 */
1277 module_exit(ieee1394_cleanup);
1279 /* Exported symbols */
1282 EXPORT_SYMBOL(hpsb_alloc_host);
1283 EXPORT_SYMBOL(hpsb_add_host);
1284 EXPORT_SYMBOL(hpsb_resume_host);
1285 EXPORT_SYMBOL(hpsb_remove_host);
1286 EXPORT_SYMBOL(hpsb_update_config_rom_image);
1288 /** ieee1394_core.c **/
1289 EXPORT_SYMBOL(hpsb_speedto_str);
1290 EXPORT_SYMBOL(hpsb_protocol_class);
1291 EXPORT_SYMBOL(hpsb_set_packet_complete_task);
1292 EXPORT_SYMBOL(hpsb_alloc_packet);
1293 EXPORT_SYMBOL(hpsb_free_packet);
1294 EXPORT_SYMBOL(hpsb_send_packet);
1295 EXPORT_SYMBOL(hpsb_reset_bus);
1296 EXPORT_SYMBOL(hpsb_read_cycle_timer);
1297 EXPORT_SYMBOL(hpsb_bus_reset);
1298 EXPORT_SYMBOL(hpsb_selfid_received);
1299 EXPORT_SYMBOL(hpsb_selfid_complete);
1300 EXPORT_SYMBOL(hpsb_packet_sent);
1301 EXPORT_SYMBOL(hpsb_packet_received);
1302 EXPORT_SYMBOL_GPL(hpsb_disable_irm);
1304 /** ieee1394_transactions.c **/
1305 EXPORT_SYMBOL(hpsb_get_tlabel);
1306 EXPORT_SYMBOL(hpsb_free_tlabel);
1307 EXPORT_SYMBOL(hpsb_make_readpacket);
1308 EXPORT_SYMBOL(hpsb_make_writepacket);
1309 EXPORT_SYMBOL(hpsb_make_streampacket);
1310 EXPORT_SYMBOL(hpsb_make_lockpacket);
1311 EXPORT_SYMBOL(hpsb_make_lock64packet);
1312 EXPORT_SYMBOL(hpsb_make_phypacket);
1313 EXPORT_SYMBOL(hpsb_make_isopacket);
1314 EXPORT_SYMBOL(hpsb_read);
1315 EXPORT_SYMBOL(hpsb_write);
1316 EXPORT_SYMBOL(hpsb_packet_success);
1319 EXPORT_SYMBOL(hpsb_register_highlevel);
1320 EXPORT_SYMBOL(hpsb_unregister_highlevel);
1321 EXPORT_SYMBOL(hpsb_register_addrspace);
1322 EXPORT_SYMBOL(hpsb_unregister_addrspace);
1323 EXPORT_SYMBOL(hpsb_allocate_and_register_addrspace);
1324 EXPORT_SYMBOL(hpsb_listen_channel);
1325 EXPORT_SYMBOL(hpsb_unlisten_channel);
1326 EXPORT_SYMBOL(hpsb_get_hostinfo);
1327 EXPORT_SYMBOL(hpsb_create_hostinfo);
1328 EXPORT_SYMBOL(hpsb_destroy_hostinfo);
1329 EXPORT_SYMBOL(hpsb_set_hostinfo_key);
1330 EXPORT_SYMBOL(hpsb_get_hostinfo_bykey);
1331 EXPORT_SYMBOL(hpsb_set_hostinfo);
1332 EXPORT_SYMBOL(highlevel_host_reset);
1335 EXPORT_SYMBOL(hpsb_node_fill_packet);
1336 EXPORT_SYMBOL(hpsb_node_write);
1337 EXPORT_SYMBOL(__hpsb_register_protocol);
1338 EXPORT_SYMBOL(hpsb_unregister_protocol);
1341 EXPORT_SYMBOL(hpsb_update_config_rom);
1344 EXPORT_SYMBOL(dma_prog_region_init);
1345 EXPORT_SYMBOL(dma_prog_region_alloc);
1346 EXPORT_SYMBOL(dma_prog_region_free);
1347 EXPORT_SYMBOL(dma_region_init);
1348 EXPORT_SYMBOL(dma_region_alloc);
1349 EXPORT_SYMBOL(dma_region_free);
1350 EXPORT_SYMBOL(dma_region_sync_for_cpu);
1351 EXPORT_SYMBOL(dma_region_sync_for_device);
1352 EXPORT_SYMBOL(dma_region_mmap);
1353 EXPORT_SYMBOL(dma_region_offset_to_bus);
1356 EXPORT_SYMBOL(hpsb_iso_xmit_init);
1357 EXPORT_SYMBOL(hpsb_iso_recv_init);
1358 EXPORT_SYMBOL(hpsb_iso_xmit_start);
1359 EXPORT_SYMBOL(hpsb_iso_recv_start);
1360 EXPORT_SYMBOL(hpsb_iso_recv_listen_channel);
1361 EXPORT_SYMBOL(hpsb_iso_recv_unlisten_channel);
1362 EXPORT_SYMBOL(hpsb_iso_recv_set_channel_mask);
1363 EXPORT_SYMBOL(hpsb_iso_stop);
1364 EXPORT_SYMBOL(hpsb_iso_shutdown);
1365 EXPORT_SYMBOL(hpsb_iso_xmit_queue_packet);
1366 EXPORT_SYMBOL(hpsb_iso_xmit_sync);
1367 EXPORT_SYMBOL(hpsb_iso_recv_release_packets);
1368 EXPORT_SYMBOL(hpsb_iso_n_ready);
1369 EXPORT_SYMBOL(hpsb_iso_packet_sent);
1370 EXPORT_SYMBOL(hpsb_iso_packet_received);
1371 EXPORT_SYMBOL(hpsb_iso_wake);
1372 EXPORT_SYMBOL(hpsb_iso_recv_flush);
1375 EXPORT_SYMBOL(csr1212_attach_keyval_to_directory);
1376 EXPORT_SYMBOL(csr1212_detach_keyval_from_directory);
1377 EXPORT_SYMBOL(csr1212_get_keyval);
1378 EXPORT_SYMBOL(csr1212_new_directory);
1379 EXPORT_SYMBOL(csr1212_parse_keyval);
1380 EXPORT_SYMBOL(csr1212_read);
1381 EXPORT_SYMBOL(csr1212_release_keyval);