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/suspend.h>
34 #include <linux/kthread.h>
35 #include <linux/preempt.h>
36 #include <linux/time.h>
38 #include <asm/system.h>
39 #include <asm/byteorder.h>
41 #include "ieee1394_types.h"
44 #include "ieee1394_core.h"
45 #include "highlevel.h"
46 #include "ieee1394_transactions.h"
51 #include "config_roms.h"
54 * Disable the nodemgr detection and config rom reading functionality.
56 static int disable_nodemgr;
57 module_param(disable_nodemgr, int, 0444);
58 MODULE_PARM_DESC(disable_nodemgr, "Disable nodemgr functionality.");
60 /* Disable Isochronous Resource Manager functionality */
61 int hpsb_disable_irm = 0;
62 module_param_named(disable_irm, hpsb_disable_irm, bool, 0444);
63 MODULE_PARM_DESC(disable_irm,
64 "Disable Isochronous Resource Manager functionality.");
66 /* We are GPL, so treat us special */
67 MODULE_LICENSE("GPL");
69 /* Some globals used */
70 const char *hpsb_speedto_str[] = { "S100", "S200", "S400", "S800", "S1600", "S3200" };
71 struct class *hpsb_protocol_class;
73 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
74 static void dump_packet(const char *text, quadlet_t *data, int size, int speed)
79 size = (size > 4 ? 4 : size);
81 printk(KERN_DEBUG "ieee1394: %s", text);
82 if (speed > -1 && speed < 6)
83 printk(" at %s", hpsb_speedto_str[speed]);
85 for (i = 0; i < size; i++)
86 printk(" %08x", data[i]);
90 #define dump_packet(a,b,c,d) do {} while (0)
93 static void abort_requests(struct hpsb_host *host);
94 static void queue_packet_complete(struct hpsb_packet *packet);
98 * hpsb_set_packet_complete_task - set task that runs when a packet completes
99 * @packet: the packet whose completion we want the task added to
100 * @routine: function to call
101 * @data: data (if any) to pass to the above function
103 * Set the task that runs when a packet completes. You cannot call this more
104 * than once on a single packet before it is sent.
106 * Typically, the complete @routine is responsible to call hpsb_free_packet().
108 void hpsb_set_packet_complete_task(struct hpsb_packet *packet,
109 void (*routine)(void *), void *data)
111 WARN_ON(packet->complete_routine != NULL);
112 packet->complete_routine = routine;
113 packet->complete_data = data;
118 * hpsb_alloc_packet - allocate new packet structure
119 * @data_size: size of the data block to be allocated, in bytes
121 * This function allocates, initializes and returns a new &struct hpsb_packet.
122 * It can be used in interrupt context. A header block is always included and
123 * initialized with zeros. Its size is big enough to contain all possible 1394
124 * headers. The data block is only allocated if @data_size is not zero.
126 * For packets for which responses will be received the @data_size has to be big
127 * enough to contain the response's data block since no further allocation
128 * occurs at response matching time.
130 * The packet's generation value will be set to the current generation number
131 * for ease of use. Remember to overwrite it with your own recorded generation
132 * number if you can not be sure that your code will not race with a bus reset.
134 * Return value: A pointer to a &struct hpsb_packet or NULL on allocation
137 struct hpsb_packet *hpsb_alloc_packet(size_t data_size)
139 struct hpsb_packet *packet;
141 data_size = ((data_size + 3) & ~3);
143 packet = kzalloc(sizeof(*packet) + data_size, GFP_ATOMIC);
147 packet->state = hpsb_unused;
148 packet->generation = -1;
149 INIT_LIST_HEAD(&packet->driver_list);
150 INIT_LIST_HEAD(&packet->queue);
151 atomic_set(&packet->refcnt, 1);
154 packet->data = packet->embedded_data;
155 packet->allocated_data_size = data_size;
161 * hpsb_free_packet - free packet and data associated with it
162 * @packet: packet to free (is NULL safe)
164 * Frees @packet->data only if it was allocated through hpsb_alloc_packet().
166 void hpsb_free_packet(struct hpsb_packet *packet)
168 if (packet && atomic_dec_and_test(&packet->refcnt)) {
169 BUG_ON(!list_empty(&packet->driver_list) ||
170 !list_empty(&packet->queue));
176 * hpsb_reset_bus - initiate bus reset on the given host
177 * @host: host controller whose bus to reset
178 * @type: one of enum reset_types
180 * Returns 1 if bus reset already in progress, 0 otherwise.
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);
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,
212 if (!host || !cycle_timer || !local_time)
216 local_irq_save(flags);
218 ctr = host->driver->devctl(host, GET_CYCLE_COUNTER, 0);
220 do_gettimeofday(&tv);
222 local_irq_restore(flags);
228 *local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
233 * hpsb_bus_reset - notify a bus reset to the core
235 * For host driver module usage. Safe to use in interrupt context, although
236 * quite complex; so you may want to run it in the bottom rather than top half.
238 * Returns 1 if bus reset already in progress, 0 otherwise.
240 int hpsb_bus_reset(struct hpsb_host *host)
242 if (host->in_bus_reset) {
243 HPSB_NOTICE("%s called while bus reset already in progress",
248 abort_requests(host);
249 host->in_bus_reset = 1;
252 host->busmgr_id = -1;
255 host->node_count = 0;
256 host->selfid_count = 0;
263 * Verify num_of_selfids SelfIDs and return number of nodes. Return zero in
264 * case verification failed.
266 static int check_selfids(struct hpsb_host *host)
269 int rest_of_selfids = host->selfid_count;
270 struct selfid *sid = (struct selfid *)host->topology_map;
271 struct ext_selfid *esid;
274 host->nodes_active = 0;
276 while (rest_of_selfids--) {
277 if (!sid->extended) {
281 if (sid->phy_id != nodeid) {
282 HPSB_INFO("SelfIDs failed monotony check with "
287 if (sid->link_active) {
288 host->nodes_active++;
290 host->irm_id = LOCAL_BUS | sid->phy_id;
293 esid = (struct ext_selfid *)sid;
295 if ((esid->phy_id != nodeid)
296 || (esid->seq_nr != esid_seq)) {
297 HPSB_INFO("SelfIDs failed monotony check with "
298 "%d/%d", esid->phy_id, esid->seq_nr);
306 esid = (struct ext_selfid *)(sid - 1);
307 while (esid->extended) {
308 if ((esid->porta == SELFID_PORT_PARENT) ||
309 (esid->portb == SELFID_PORT_PARENT) ||
310 (esid->portc == SELFID_PORT_PARENT) ||
311 (esid->portd == SELFID_PORT_PARENT) ||
312 (esid->porte == SELFID_PORT_PARENT) ||
313 (esid->portf == SELFID_PORT_PARENT) ||
314 (esid->portg == SELFID_PORT_PARENT) ||
315 (esid->porth == SELFID_PORT_PARENT)) {
316 HPSB_INFO("SelfIDs failed root check on "
323 sid = (struct selfid *)esid;
324 if ((sid->port0 == SELFID_PORT_PARENT) ||
325 (sid->port1 == SELFID_PORT_PARENT) ||
326 (sid->port2 == SELFID_PORT_PARENT)) {
327 HPSB_INFO("SelfIDs failed root check");
331 host->node_count = nodeid + 1;
335 static void build_speed_map(struct hpsb_host *host, int nodecount)
337 u8 cldcnt[nodecount];
338 u8 *map = host->speed_map;
339 u8 *speedcap = host->speed;
341 struct ext_selfid *esid;
344 for (i = 0; i < (nodecount * 64); i += 64) {
345 for (j = 0; j < nodecount; j++) {
346 map[i+j] = IEEE1394_SPEED_MAX;
350 for (i = 0; i < nodecount; i++) {
354 /* find direct children count and speed */
355 for (sid = (struct selfid *)&host->topology_map[host->selfid_count-1],
357 (void *)sid >= (void *)host->topology_map; sid--) {
359 esid = (struct ext_selfid *)sid;
361 if (esid->porta == SELFID_PORT_CHILD) cldcnt[n]++;
362 if (esid->portb == SELFID_PORT_CHILD) cldcnt[n]++;
363 if (esid->portc == SELFID_PORT_CHILD) cldcnt[n]++;
364 if (esid->portd == SELFID_PORT_CHILD) cldcnt[n]++;
365 if (esid->porte == SELFID_PORT_CHILD) cldcnt[n]++;
366 if (esid->portf == SELFID_PORT_CHILD) cldcnt[n]++;
367 if (esid->portg == SELFID_PORT_CHILD) cldcnt[n]++;
368 if (esid->porth == SELFID_PORT_CHILD) cldcnt[n]++;
370 if (sid->port0 == SELFID_PORT_CHILD) cldcnt[n]++;
371 if (sid->port1 == SELFID_PORT_CHILD) cldcnt[n]++;
372 if (sid->port2 == SELFID_PORT_CHILD) cldcnt[n]++;
374 speedcap[n] = sid->speed;
379 /* set self mapping */
380 for (i = 0; i < nodecount; i++) {
381 map[64*i + i] = speedcap[i];
384 /* fix up direct children count to total children count;
385 * also fix up speedcaps for sibling and parent communication */
386 for (i = 1; i < nodecount; i++) {
387 for (j = cldcnt[i], n = i - 1; j > 0; j--) {
388 cldcnt[i] += cldcnt[n];
389 speedcap[n] = min(speedcap[n], speedcap[i]);
394 for (n = 0; n < nodecount; n++) {
395 for (i = n - cldcnt[n]; i <= n; i++) {
396 for (j = 0; j < (n - cldcnt[n]); j++) {
397 map[j*64 + i] = map[i*64 + j] =
398 min(map[i*64 + j], speedcap[n]);
400 for (j = n + 1; j < nodecount; j++) {
401 map[j*64 + i] = map[i*64 + j] =
402 min(map[i*64 + j], speedcap[n]);
407 #if SELFID_SPEED_UNKNOWN != IEEE1394_SPEED_MAX
408 /* assume maximum speed for 1394b PHYs, nodemgr will correct it */
409 for (n = 0; n < nodecount; n++)
410 if (speedcap[n] == SELFID_SPEED_UNKNOWN)
411 speedcap[n] = IEEE1394_SPEED_MAX;
417 * hpsb_selfid_received - hand over received selfid packet to the core
419 * For host driver module usage. Safe to use in interrupt context.
421 * The host driver should have done a successful complement check (second
422 * quadlet is complement of first) beforehand.
424 void hpsb_selfid_received(struct hpsb_host *host, quadlet_t sid)
426 if (host->in_bus_reset) {
427 HPSB_VERBOSE("Including SelfID 0x%x", sid);
428 host->topology_map[host->selfid_count++] = sid;
430 HPSB_NOTICE("Spurious SelfID packet (0x%08x) received from bus %d",
431 sid, NODEID_TO_BUS(host->node_id));
436 * hpsb_selfid_complete - notify completion of SelfID stage to the core
438 * For host driver module usage. Safe to use in interrupt context, although
439 * quite complex; so you may want to run it in the bottom rather than top half.
441 * Notify completion of SelfID stage to the core and report new physical ID
442 * and whether host is root now.
444 void hpsb_selfid_complete(struct hpsb_host *host, int phyid, int isroot)
446 if (!host->in_bus_reset)
447 HPSB_NOTICE("SelfID completion called outside of bus reset!");
449 host->node_id = LOCAL_BUS | phyid;
450 host->is_root = isroot;
452 if (!check_selfids(host)) {
453 if (host->reset_retries++ < 20) {
454 /* selfid stage did not complete without error */
455 HPSB_NOTICE("Error in SelfID stage, resetting");
456 host->in_bus_reset = 0;
457 /* this should work from ohci1394 now... */
458 hpsb_reset_bus(host, LONG_RESET);
461 HPSB_NOTICE("Stopping out-of-control reset loop");
462 HPSB_NOTICE("Warning - topology map and speed map will not be valid");
463 host->reset_retries = 0;
466 host->reset_retries = 0;
467 build_speed_map(host, host->node_count);
470 HPSB_VERBOSE("selfid_complete called with successful SelfID stage "
471 "... irm_id: 0x%X node_id: 0x%X",host->irm_id,host->node_id);
473 /* irm_id is kept up to date by check_selfids() */
474 if (host->irm_id == host->node_id) {
482 host->driver->devctl(host, ACT_CYCLE_MASTER, 1);
485 atomic_inc(&host->generation);
486 host->in_bus_reset = 0;
487 highlevel_host_reset(host);
490 static spinlock_t pending_packets_lock = SPIN_LOCK_UNLOCKED;
493 * hpsb_packet_sent - notify core of sending a packet
495 * For host driver module usage. Safe to call from within a transmit packet
498 * Notify core of sending a packet. Ackcode is the ack code returned for async
499 * transmits or ACKX_SEND_ERROR if the transmission failed completely; ACKX_NONE
500 * for other cases (internal errors that don't justify a panic).
502 void hpsb_packet_sent(struct hpsb_host *host, struct hpsb_packet *packet,
507 spin_lock_irqsave(&pending_packets_lock, flags);
509 packet->ack_code = ackcode;
511 if (packet->no_waiter || packet->state == hpsb_complete) {
512 /* if packet->no_waiter, must not have a tlabel allocated */
513 spin_unlock_irqrestore(&pending_packets_lock, flags);
514 hpsb_free_packet(packet);
518 atomic_dec(&packet->refcnt); /* drop HC's reference */
519 /* here the packet must be on the host->pending_packets queue */
521 if (ackcode != ACK_PENDING || !packet->expect_response) {
522 packet->state = hpsb_complete;
523 list_del_init(&packet->queue);
524 spin_unlock_irqrestore(&pending_packets_lock, flags);
525 queue_packet_complete(packet);
529 packet->state = hpsb_pending;
530 packet->sendtime = jiffies;
532 spin_unlock_irqrestore(&pending_packets_lock, flags);
534 mod_timer(&host->timeout, jiffies + host->timeout_interval);
538 * hpsb_send_phy_config - transmit a PHY configuration packet on the bus
539 * @host: host that PHY config packet gets sent through
540 * @rootid: root whose force_root bit should get set (-1 = don't set force_root)
541 * @gapcnt: gap count value to set (-1 = don't set gap count)
543 * This function sends a PHY config packet on the bus through the specified
546 * Return value: 0 for success or negative error number otherwise.
548 int hpsb_send_phy_config(struct hpsb_host *host, int rootid, int gapcnt)
550 struct hpsb_packet *packet;
554 if (rootid >= ALL_NODES || rootid < -1 || gapcnt > 0x3f || gapcnt < -1 ||
555 (rootid == -1 && gapcnt == -1)) {
556 HPSB_DEBUG("Invalid Parameter: rootid = %d gapcnt = %d",
562 d |= PHYPACKET_PHYCONFIG_R | rootid << PHYPACKET_PORT_SHIFT;
564 d |= PHYPACKET_PHYCONFIG_T | gapcnt << PHYPACKET_GAPCOUNT_SHIFT;
566 packet = hpsb_make_phypacket(host, d);
570 packet->generation = get_hpsb_generation(host);
571 retval = hpsb_send_packet_and_wait(packet);
572 hpsb_free_packet(packet);
578 * hpsb_send_packet - transmit a packet on the bus
579 * @packet: packet to send
581 * The packet is sent through the host specified in the packet->host field.
582 * Before sending, the packet's transmit speed is automatically determined
583 * using the local speed map when it is an async, non-broadcast packet.
585 * Possibilities for failure are that host is either not initialized, in bus
586 * reset, the packet's generation number doesn't match the current generation
587 * number or the host reports a transmit error.
589 * Return value: 0 on success, negative errno on failure.
591 int hpsb_send_packet(struct hpsb_packet *packet)
593 struct hpsb_host *host = packet->host;
595 if (host->is_shutdown)
597 if (host->in_bus_reset ||
598 (packet->generation != get_hpsb_generation(host)))
601 packet->state = hpsb_queued;
603 /* This just seems silly to me */
604 WARN_ON(packet->no_waiter && packet->expect_response);
606 if (!packet->no_waiter || packet->expect_response) {
609 atomic_inc(&packet->refcnt);
610 /* Set the initial "sendtime" to 10 seconds from now, to
611 prevent premature expiry. If a packet takes more than
612 10 seconds to hit the wire, we have bigger problems :) */
613 packet->sendtime = jiffies + 10 * HZ;
614 spin_lock_irqsave(&pending_packets_lock, flags);
615 list_add_tail(&packet->queue, &host->pending_packets);
616 spin_unlock_irqrestore(&pending_packets_lock, flags);
619 if (packet->node_id == host->node_id) {
620 /* it is a local request, so handle it locally */
623 size_t size = packet->data_size + packet->header_size;
625 data = kmalloc(size, GFP_ATOMIC);
627 HPSB_ERR("unable to allocate memory for concatenating header and data");
631 memcpy(data, packet->header, packet->header_size);
633 if (packet->data_size)
634 memcpy(((u8*)data) + packet->header_size, packet->data, packet->data_size);
636 dump_packet("send packet local", packet->header, packet->header_size, -1);
638 hpsb_packet_sent(host, packet, packet->expect_response ? ACK_PENDING : ACK_COMPLETE);
639 hpsb_packet_received(host, data, size, 0);
646 if (packet->type == hpsb_async &&
647 NODEID_TO_NODE(packet->node_id) != ALL_NODES)
649 host->speed[NODEID_TO_NODE(packet->node_id)];
651 dump_packet("send packet", packet->header, packet->header_size, packet->speed_code);
653 return host->driver->transmit_packet(host, packet);
656 /* We could just use complete() directly as the packet complete
657 * callback, but this is more typesafe, in the sense that we get a
658 * compiler error if the prototype for complete() changes. */
660 static void complete_packet(void *data)
662 complete((struct completion *) data);
666 * hpsb_send_packet_and_wait - enqueue packet, block until transaction completes
667 * @packet: packet to send
669 * Return value: 0 on success, negative errno on failure.
671 int hpsb_send_packet_and_wait(struct hpsb_packet *packet)
673 struct completion done;
676 init_completion(&done);
677 hpsb_set_packet_complete_task(packet, complete_packet, &done);
678 retval = hpsb_send_packet(packet);
680 wait_for_completion(&done);
685 static void send_packet_nocare(struct hpsb_packet *packet)
687 if (hpsb_send_packet(packet) < 0) {
688 hpsb_free_packet(packet);
692 static size_t packet_size_to_data_size(size_t packet_size, size_t header_size,
693 size_t buffer_size, int tcode)
695 size_t ret = packet_size <= header_size ? 0 : packet_size - header_size;
697 if (unlikely(ret > buffer_size))
700 if (unlikely(ret + header_size != packet_size))
701 HPSB_ERR("unexpected packet size %zd (tcode %d), bug?",
706 static void handle_packet_response(struct hpsb_host *host, int tcode,
707 quadlet_t *data, size_t size)
709 struct hpsb_packet *packet;
710 int tlabel = (data[0] >> 10) & 0x3f;
714 spin_lock_irqsave(&pending_packets_lock, flags);
716 list_for_each_entry(packet, &host->pending_packets, queue)
717 if (packet->tlabel == tlabel &&
718 packet->node_id == (data[1] >> 16))
721 spin_unlock_irqrestore(&pending_packets_lock, flags);
722 HPSB_DEBUG("unsolicited response packet received - %s",
724 dump_packet("contents", data, 16, -1);
728 switch (packet->tcode) {
731 if (unlikely(tcode != TCODE_WRITE_RESPONSE))
738 if (unlikely(tcode != TCODE_READQ_RESPONSE))
745 if (unlikely(tcode != TCODE_READB_RESPONSE))
748 size = packet_size_to_data_size(size, header_size,
749 packet->allocated_data_size,
753 case TCODE_LOCK_REQUEST:
754 if (unlikely(tcode != TCODE_LOCK_RESPONSE))
757 size = packet_size_to_data_size(min(size, (size_t)(16 + 8)),
759 packet->allocated_data_size,
764 spin_unlock_irqrestore(&pending_packets_lock, flags);
765 HPSB_DEBUG("unsolicited response packet received - %s",
767 dump_packet("contents", data, 16, -1);
771 list_del_init(&packet->queue);
772 spin_unlock_irqrestore(&pending_packets_lock, flags);
774 if (packet->state == hpsb_queued) {
775 packet->sendtime = jiffies;
776 packet->ack_code = ACK_PENDING;
778 packet->state = hpsb_complete;
780 memcpy(packet->header, data, header_size);
782 memcpy(packet->data, data + 4, size);
784 queue_packet_complete(packet);
788 static struct hpsb_packet *create_reply_packet(struct hpsb_host *host,
789 quadlet_t *data, size_t dsize)
791 struct hpsb_packet *p;
793 p = hpsb_alloc_packet(dsize);
794 if (unlikely(p == NULL)) {
795 /* FIXME - send data_error response */
796 HPSB_ERR("out of memory, cannot send response packet");
800 p->type = hpsb_async;
801 p->state = hpsb_unused;
803 p->node_id = data[1] >> 16;
804 p->tlabel = (data[0] >> 10) & 0x3f;
807 p->generation = get_hpsb_generation(host);
810 p->data[dsize / 4] = 0;
815 #define PREP_ASYNC_HEAD_RCODE(tc) \
816 packet->tcode = tc; \
817 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
818 | (1 << 8) | (tc << 4); \
819 packet->header[1] = (packet->host->node_id << 16) | (rcode << 12); \
820 packet->header[2] = 0
822 static void fill_async_readquad_resp(struct hpsb_packet *packet, int rcode,
825 PREP_ASYNC_HEAD_RCODE(TCODE_READQ_RESPONSE);
826 packet->header[3] = data;
827 packet->header_size = 16;
828 packet->data_size = 0;
831 static void fill_async_readblock_resp(struct hpsb_packet *packet, int rcode,
834 if (rcode != RCODE_COMPLETE)
837 PREP_ASYNC_HEAD_RCODE(TCODE_READB_RESPONSE);
838 packet->header[3] = length << 16;
839 packet->header_size = 16;
840 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
843 static void fill_async_write_resp(struct hpsb_packet *packet, int rcode)
845 PREP_ASYNC_HEAD_RCODE(TCODE_WRITE_RESPONSE);
846 packet->header_size = 12;
847 packet->data_size = 0;
850 static void fill_async_lock_resp(struct hpsb_packet *packet, int rcode, int extcode,
853 if (rcode != RCODE_COMPLETE)
856 PREP_ASYNC_HEAD_RCODE(TCODE_LOCK_RESPONSE);
857 packet->header[3] = (length << 16) | extcode;
858 packet->header_size = 16;
859 packet->data_size = length;
862 #define PREP_REPLY_PACKET(length) \
863 packet = create_reply_packet(host, data, length); \
864 if (packet == NULL) break
866 static void handle_incoming_packet(struct hpsb_host *host, int tcode,
867 quadlet_t *data, size_t size, int write_acked)
869 struct hpsb_packet *packet;
870 int length, rcode, extcode;
872 nodeid_t source = data[1] >> 16;
873 nodeid_t dest = data[0] >> 16;
874 u16 flags = (u16) data[0];
877 /* big FIXME - no error checking is done for an out of bounds length */
881 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
882 rcode = highlevel_write(host, source, dest, data+3,
886 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
888 /* not a broadcast write, reply */
889 PREP_REPLY_PACKET(0);
890 fill_async_write_resp(packet, rcode);
891 send_packet_nocare(packet);
896 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
897 rcode = highlevel_write(host, source, dest, data+4,
898 addr, data[3]>>16, flags);
901 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
903 /* not a broadcast write, reply */
904 PREP_REPLY_PACKET(0);
905 fill_async_write_resp(packet, rcode);
906 send_packet_nocare(packet);
911 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
912 rcode = highlevel_read(host, source, &buffer, addr, 4, flags);
915 PREP_REPLY_PACKET(0);
916 fill_async_readquad_resp(packet, rcode, buffer);
917 send_packet_nocare(packet);
922 length = data[3] >> 16;
923 PREP_REPLY_PACKET(length);
925 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
926 rcode = highlevel_read(host, source, packet->data, addr,
930 fill_async_readblock_resp(packet, rcode, length);
931 send_packet_nocare(packet);
933 hpsb_free_packet(packet);
937 case TCODE_LOCK_REQUEST:
938 length = data[3] >> 16;
939 extcode = data[3] & 0xffff;
940 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
942 PREP_REPLY_PACKET(8);
944 if ((extcode == 0) || (extcode >= 7)) {
945 /* let switch default handle error */
951 rcode = highlevel_lock(host, source, packet->data, addr,
952 data[4], 0, extcode,flags);
953 fill_async_lock_resp(packet, rcode, extcode, 4);
956 if ((extcode != EXTCODE_FETCH_ADD)
957 && (extcode != EXTCODE_LITTLE_ADD)) {
958 rcode = highlevel_lock(host, source,
962 fill_async_lock_resp(packet, rcode, extcode, 4);
964 rcode = highlevel_lock64(host, source,
965 (octlet_t *)packet->data, addr,
966 *(octlet_t *)(data + 4), 0ULL,
968 fill_async_lock_resp(packet, rcode, extcode, 8);
972 rcode = highlevel_lock64(host, source,
973 (octlet_t *)packet->data, addr,
974 *(octlet_t *)(data + 6),
975 *(octlet_t *)(data + 4),
977 fill_async_lock_resp(packet, rcode, extcode, 8);
980 rcode = RCODE_TYPE_ERROR;
981 fill_async_lock_resp(packet, rcode,
986 send_packet_nocare(packet);
988 hpsb_free_packet(packet);
994 #undef PREP_REPLY_PACKET
997 * hpsb_packet_received - hand over received packet to the core
999 * For host driver module usage.
1001 * The contents of data are expected to be the full packet but with the CRCs
1002 * left out (data block follows header immediately), with the header (i.e. the
1003 * first four quadlets) in machine byte order and the data block in big endian.
1004 * *@data can be safely overwritten after this call.
1006 * If the packet is a write request, @write_acked is to be set to true if it was
1007 * ack_complete'd already, false otherwise. This argument is ignored for any
1008 * other packet type.
1010 void hpsb_packet_received(struct hpsb_host *host, quadlet_t *data, size_t size,
1015 if (unlikely(host->in_bus_reset)) {
1016 HPSB_DEBUG("received packet during reset; ignoring");
1020 dump_packet("received packet", data, size, -1);
1022 tcode = (data[0] >> 4) & 0xf;
1025 case TCODE_WRITE_RESPONSE:
1026 case TCODE_READQ_RESPONSE:
1027 case TCODE_READB_RESPONSE:
1028 case TCODE_LOCK_RESPONSE:
1029 handle_packet_response(host, tcode, data, size);
1036 case TCODE_LOCK_REQUEST:
1037 handle_incoming_packet(host, tcode, data, size, write_acked);
1041 case TCODE_ISO_DATA:
1042 highlevel_iso_receive(host, data, size);
1045 case TCODE_CYCLE_START:
1046 /* simply ignore this packet if it is passed on */
1050 HPSB_DEBUG("received packet with bogus transaction code %d",
1056 static void abort_requests(struct hpsb_host *host)
1058 struct hpsb_packet *packet, *p;
1059 struct list_head tmp;
1060 unsigned long flags;
1062 host->driver->devctl(host, CANCEL_REQUESTS, 0);
1064 INIT_LIST_HEAD(&tmp);
1065 spin_lock_irqsave(&pending_packets_lock, flags);
1066 list_splice_init(&host->pending_packets, &tmp);
1067 spin_unlock_irqrestore(&pending_packets_lock, flags);
1069 list_for_each_entry_safe(packet, p, &tmp, queue) {
1070 list_del_init(&packet->queue);
1071 packet->state = hpsb_complete;
1072 packet->ack_code = ACKX_ABORTED;
1073 queue_packet_complete(packet);
1077 void abort_timedouts(unsigned long __opaque)
1079 struct hpsb_host *host = (struct hpsb_host *)__opaque;
1080 struct hpsb_packet *packet, *p;
1081 struct list_head tmp;
1082 unsigned long flags, expire, j;
1084 spin_lock_irqsave(&host->csr.lock, flags);
1085 expire = host->csr.expire;
1086 spin_unlock_irqrestore(&host->csr.lock, flags);
1089 INIT_LIST_HEAD(&tmp);
1090 spin_lock_irqsave(&pending_packets_lock, flags);
1092 list_for_each_entry_safe(packet, p, &host->pending_packets, queue) {
1093 if (time_before(packet->sendtime + expire, j))
1094 list_move_tail(&packet->queue, &tmp);
1096 /* Since packets are added to the tail, the oldest
1097 * ones are first, always. When we get to one that
1098 * isn't timed out, the rest aren't either. */
1101 if (!list_empty(&host->pending_packets))
1102 mod_timer(&host->timeout, j + host->timeout_interval);
1104 spin_unlock_irqrestore(&pending_packets_lock, flags);
1106 list_for_each_entry_safe(packet, p, &tmp, queue) {
1107 list_del_init(&packet->queue);
1108 packet->state = hpsb_complete;
1109 packet->ack_code = ACKX_TIMEOUT;
1110 queue_packet_complete(packet);
1114 static struct task_struct *khpsbpkt_thread;
1115 static LIST_HEAD(hpsbpkt_queue);
1117 static void queue_packet_complete(struct hpsb_packet *packet)
1119 unsigned long flags;
1121 if (packet->no_waiter) {
1122 hpsb_free_packet(packet);
1125 if (packet->complete_routine != NULL) {
1126 spin_lock_irqsave(&pending_packets_lock, flags);
1127 list_add_tail(&packet->queue, &hpsbpkt_queue);
1128 spin_unlock_irqrestore(&pending_packets_lock, flags);
1129 wake_up_process(khpsbpkt_thread);
1135 * Kernel thread which handles packets that are completed. This way the
1136 * packet's "complete" function is asynchronously run in process context.
1137 * Only packets which have a "complete" function may be sent here.
1139 static int hpsbpkt_thread(void *__hi)
1141 struct hpsb_packet *packet, *p;
1142 struct list_head tmp;
1145 current->flags |= PF_NOFREEZE;
1147 while (!kthread_should_stop()) {
1149 INIT_LIST_HEAD(&tmp);
1150 spin_lock_irq(&pending_packets_lock);
1151 list_splice_init(&hpsbpkt_queue, &tmp);
1152 spin_unlock_irq(&pending_packets_lock);
1154 list_for_each_entry_safe(packet, p, &tmp, queue) {
1155 list_del_init(&packet->queue);
1156 packet->complete_routine(packet->complete_data);
1159 set_current_state(TASK_INTERRUPTIBLE);
1160 spin_lock_irq(&pending_packets_lock);
1161 may_schedule = list_empty(&hpsbpkt_queue);
1162 spin_unlock_irq(&pending_packets_lock);
1165 __set_current_state(TASK_RUNNING);
1170 static int __init ieee1394_init(void)
1174 /* non-fatal error */
1175 if (hpsb_init_config_roms()) {
1176 HPSB_ERR("Failed to initialize some config rom entries.\n");
1177 HPSB_ERR("Some features may not be available\n");
1180 khpsbpkt_thread = kthread_run(hpsbpkt_thread, NULL, "khpsbpkt");
1181 if (IS_ERR(khpsbpkt_thread)) {
1182 HPSB_ERR("Failed to start hpsbpkt thread!\n");
1183 ret = PTR_ERR(khpsbpkt_thread);
1184 goto exit_cleanup_config_roms;
1187 if (register_chrdev_region(IEEE1394_CORE_DEV, 256, "ieee1394")) {
1188 HPSB_ERR("unable to register character device major %d!\n", IEEE1394_MAJOR);
1190 goto exit_release_kernel_thread;
1193 ret = bus_register(&ieee1394_bus_type);
1195 HPSB_INFO("bus register failed");
1196 goto release_chrdev;
1199 for (i = 0; fw_bus_attrs[i]; i++) {
1200 ret = bus_create_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1203 bus_remove_file(&ieee1394_bus_type,
1206 bus_unregister(&ieee1394_bus_type);
1207 goto release_chrdev;
1211 ret = class_register(&hpsb_host_class);
1213 goto release_all_bus;
1215 hpsb_protocol_class = class_create(THIS_MODULE, "ieee1394_protocol");
1216 if (IS_ERR(hpsb_protocol_class)) {
1217 ret = PTR_ERR(hpsb_protocol_class);
1218 goto release_class_host;
1223 HPSB_INFO("init csr failed");
1225 goto release_class_protocol;
1228 if (disable_nodemgr) {
1229 HPSB_INFO("nodemgr and IRM functionality disabled");
1230 /* We shouldn't contend for IRM with nodemgr disabled, since
1231 nodemgr implements functionality required of ieee1394a-2000
1233 hpsb_disable_irm = 1;
1238 if (hpsb_disable_irm) {
1239 HPSB_INFO("IRM functionality disabled");
1242 ret = init_ieee1394_nodemgr();
1244 HPSB_INFO("init nodemgr failed");
1252 release_class_protocol:
1253 class_destroy(hpsb_protocol_class);
1255 class_unregister(&hpsb_host_class);
1257 for (i = 0; fw_bus_attrs[i]; i++)
1258 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1259 bus_unregister(&ieee1394_bus_type);
1261 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
1262 exit_release_kernel_thread:
1263 kthread_stop(khpsbpkt_thread);
1264 exit_cleanup_config_roms:
1265 hpsb_cleanup_config_roms();
1269 static void __exit ieee1394_cleanup(void)
1273 if (!disable_nodemgr)
1274 cleanup_ieee1394_nodemgr();
1278 class_destroy(hpsb_protocol_class);
1279 class_unregister(&hpsb_host_class);
1280 for (i = 0; fw_bus_attrs[i]; i++)
1281 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1282 bus_unregister(&ieee1394_bus_type);
1284 kthread_stop(khpsbpkt_thread);
1286 hpsb_cleanup_config_roms();
1288 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
1291 fs_initcall(ieee1394_init); /* same as ohci1394 */
1292 module_exit(ieee1394_cleanup);
1294 /* Exported symbols */
1297 EXPORT_SYMBOL(hpsb_alloc_host);
1298 EXPORT_SYMBOL(hpsb_add_host);
1299 EXPORT_SYMBOL(hpsb_resume_host);
1300 EXPORT_SYMBOL(hpsb_remove_host);
1301 EXPORT_SYMBOL(hpsb_update_config_rom_image);
1303 /** ieee1394_core.c **/
1304 EXPORT_SYMBOL(hpsb_speedto_str);
1305 EXPORT_SYMBOL(hpsb_protocol_class);
1306 EXPORT_SYMBOL(hpsb_set_packet_complete_task);
1307 EXPORT_SYMBOL(hpsb_alloc_packet);
1308 EXPORT_SYMBOL(hpsb_free_packet);
1309 EXPORT_SYMBOL(hpsb_send_packet);
1310 EXPORT_SYMBOL(hpsb_reset_bus);
1311 EXPORT_SYMBOL(hpsb_read_cycle_timer);
1312 EXPORT_SYMBOL(hpsb_bus_reset);
1313 EXPORT_SYMBOL(hpsb_selfid_received);
1314 EXPORT_SYMBOL(hpsb_selfid_complete);
1315 EXPORT_SYMBOL(hpsb_packet_sent);
1316 EXPORT_SYMBOL(hpsb_packet_received);
1317 EXPORT_SYMBOL_GPL(hpsb_disable_irm);
1319 /** ieee1394_transactions.c **/
1320 EXPORT_SYMBOL(hpsb_get_tlabel);
1321 EXPORT_SYMBOL(hpsb_free_tlabel);
1322 EXPORT_SYMBOL(hpsb_make_readpacket);
1323 EXPORT_SYMBOL(hpsb_make_writepacket);
1324 EXPORT_SYMBOL(hpsb_make_streampacket);
1325 EXPORT_SYMBOL(hpsb_make_lockpacket);
1326 EXPORT_SYMBOL(hpsb_make_lock64packet);
1327 EXPORT_SYMBOL(hpsb_make_phypacket);
1328 EXPORT_SYMBOL(hpsb_make_isopacket);
1329 EXPORT_SYMBOL(hpsb_read);
1330 EXPORT_SYMBOL(hpsb_write);
1331 EXPORT_SYMBOL(hpsb_packet_success);
1334 EXPORT_SYMBOL(hpsb_register_highlevel);
1335 EXPORT_SYMBOL(hpsb_unregister_highlevel);
1336 EXPORT_SYMBOL(hpsb_register_addrspace);
1337 EXPORT_SYMBOL(hpsb_unregister_addrspace);
1338 EXPORT_SYMBOL(hpsb_allocate_and_register_addrspace);
1339 EXPORT_SYMBOL(hpsb_listen_channel);
1340 EXPORT_SYMBOL(hpsb_unlisten_channel);
1341 EXPORT_SYMBOL(hpsb_get_hostinfo);
1342 EXPORT_SYMBOL(hpsb_create_hostinfo);
1343 EXPORT_SYMBOL(hpsb_destroy_hostinfo);
1344 EXPORT_SYMBOL(hpsb_set_hostinfo_key);
1345 EXPORT_SYMBOL(hpsb_get_hostinfo_bykey);
1346 EXPORT_SYMBOL(hpsb_set_hostinfo);
1347 EXPORT_SYMBOL(highlevel_host_reset);
1350 EXPORT_SYMBOL(hpsb_node_fill_packet);
1351 EXPORT_SYMBOL(hpsb_node_write);
1352 EXPORT_SYMBOL(__hpsb_register_protocol);
1353 EXPORT_SYMBOL(hpsb_unregister_protocol);
1356 EXPORT_SYMBOL(hpsb_update_config_rom);
1359 EXPORT_SYMBOL(dma_prog_region_init);
1360 EXPORT_SYMBOL(dma_prog_region_alloc);
1361 EXPORT_SYMBOL(dma_prog_region_free);
1362 EXPORT_SYMBOL(dma_region_init);
1363 EXPORT_SYMBOL(dma_region_alloc);
1364 EXPORT_SYMBOL(dma_region_free);
1365 EXPORT_SYMBOL(dma_region_sync_for_cpu);
1366 EXPORT_SYMBOL(dma_region_sync_for_device);
1367 EXPORT_SYMBOL(dma_region_mmap);
1368 EXPORT_SYMBOL(dma_region_offset_to_bus);
1371 EXPORT_SYMBOL(hpsb_iso_xmit_init);
1372 EXPORT_SYMBOL(hpsb_iso_recv_init);
1373 EXPORT_SYMBOL(hpsb_iso_xmit_start);
1374 EXPORT_SYMBOL(hpsb_iso_recv_start);
1375 EXPORT_SYMBOL(hpsb_iso_recv_listen_channel);
1376 EXPORT_SYMBOL(hpsb_iso_recv_unlisten_channel);
1377 EXPORT_SYMBOL(hpsb_iso_recv_set_channel_mask);
1378 EXPORT_SYMBOL(hpsb_iso_stop);
1379 EXPORT_SYMBOL(hpsb_iso_shutdown);
1380 EXPORT_SYMBOL(hpsb_iso_xmit_queue_packet);
1381 EXPORT_SYMBOL(hpsb_iso_xmit_sync);
1382 EXPORT_SYMBOL(hpsb_iso_recv_release_packets);
1383 EXPORT_SYMBOL(hpsb_iso_n_ready);
1384 EXPORT_SYMBOL(hpsb_iso_packet_sent);
1385 EXPORT_SYMBOL(hpsb_iso_packet_received);
1386 EXPORT_SYMBOL(hpsb_iso_wake);
1387 EXPORT_SYMBOL(hpsb_iso_recv_flush);
1390 EXPORT_SYMBOL(csr1212_attach_keyval_to_directory);
1391 EXPORT_SYMBOL(csr1212_detach_keyval_from_directory);
1392 EXPORT_SYMBOL(csr1212_get_keyval);
1393 EXPORT_SYMBOL(csr1212_new_directory);
1394 EXPORT_SYMBOL(csr1212_parse_keyval);
1395 EXPORT_SYMBOL(csr1212_read);
1396 EXPORT_SYMBOL(csr1212_release_keyval);