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/config.h>
24 #include <linux/kernel.h>
25 #include <linux/list.h>
26 #include <linux/string.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/module.h>
31 #include <linux/proc_fs.h>
32 #include <linux/bitops.h>
33 #include <asm/byteorder.h>
34 #include <asm/semaphore.h>
36 #include "ieee1394_types.h"
39 #include "ieee1394_core.h"
40 #include "highlevel.h"
41 #include "ieee1394_transactions.h"
48 * Disable the nodemgr detection and config rom reading functionality.
50 MODULE_PARM(disable_nodemgr, "i");
51 MODULE_PARM_DESC(disable_nodemgr, "Disable nodemgr functionality.");
52 static int disable_nodemgr = 0;
54 /* We are GPL, so treat us special */
55 MODULE_LICENSE("GPL");
57 static kmem_cache_t *hpsb_packet_cache;
59 /* Some globals used */
60 const char *hpsb_speedto_str[] = { "S100", "S200", "S400", "S800", "S1600", "S3200" };
62 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
63 static void dump_packet(const char *text, quadlet_t *data, int size)
68 size = (size > 4 ? 4 : size);
70 printk(KERN_DEBUG "ieee1394: %s", text);
71 for (i = 0; i < size; i++)
72 printk(" %08x", data[i]);
76 #define dump_packet(x,y,z)
79 static void run_packet_complete(struct hpsb_packet *packet)
81 if (packet->complete_routine != NULL) {
82 void (*complete_routine)(void*) = packet->complete_routine;
83 void *complete_data = packet->complete_data;
85 packet->complete_routine = NULL;
86 packet->complete_data = NULL;
87 complete_routine(complete_data);
93 * hpsb_set_packet_complete_task - set the task that runs when a packet
94 * completes. You cannot call this more than once on a single packet
97 * @packet: the packet whose completion we want the task added to
98 * @routine: function to call
99 * @data: data (if any) to pass to the above function
101 void hpsb_set_packet_complete_task(struct hpsb_packet *packet,
102 void (*routine)(void *), void *data)
104 BUG_ON(packet->complete_routine != NULL);
105 packet->complete_routine = routine;
106 packet->complete_data = data;
111 * alloc_hpsb_packet - allocate new packet structure
112 * @data_size: size of the data block to be allocated
114 * This function allocates, initializes and returns a new &struct hpsb_packet.
115 * It can be used in interrupt context. A header block is always included, its
116 * size is big enough to contain all possible 1394 headers. The data block is
117 * only allocated when @data_size is not zero.
119 * For packets for which responses will be received the @data_size has to be big
120 * enough to contain the response's data block since no further allocation
121 * occurs at response matching time.
123 * The packet's generation value will be set to the current generation number
124 * for ease of use. Remember to overwrite it with your own recorded generation
125 * number if you can not be sure that your code will not race with a bus reset.
127 * Return value: A pointer to a &struct hpsb_packet or NULL on allocation
130 struct hpsb_packet *alloc_hpsb_packet(size_t data_size)
132 struct hpsb_packet *packet = NULL;
135 packet = kmem_cache_alloc(hpsb_packet_cache, GFP_ATOMIC);
139 memset(packet, 0, sizeof(struct hpsb_packet));
140 packet->header = packet->embedded_header;
143 data = kmalloc(data_size + 8, GFP_ATOMIC);
145 kmem_cache_free(hpsb_packet_cache, packet);
150 packet->data_size = data_size;
153 INIT_LIST_HEAD(&packet->list);
154 sema_init(&packet->state_change, 0);
155 packet->complete_routine = NULL;
156 packet->complete_data = NULL;
157 packet->state = hpsb_unused;
158 packet->generation = -1;
166 * free_hpsb_packet - free packet and data associated with it
167 * @packet: packet to free (is NULL safe)
169 * This function will free packet->data, packet->header and finally the packet
172 void free_hpsb_packet(struct hpsb_packet *packet)
177 kmem_cache_free(hpsb_packet_cache, packet);
181 int hpsb_reset_bus(struct hpsb_host *host, int type)
183 if (!host->in_bus_reset) {
184 host->driver->devctl(host, RESET_BUS, type);
192 int hpsb_bus_reset(struct hpsb_host *host)
194 if (host->in_bus_reset) {
195 HPSB_NOTICE("%s called while bus reset already in progress",
200 abort_requests(host);
201 host->in_bus_reset = 1;
204 host->busmgr_id = -1;
207 host->node_count = 0;
208 host->selfid_count = 0;
215 * Verify num_of_selfids SelfIDs and return number of nodes. Return zero in
216 * case verification failed.
218 static int check_selfids(struct hpsb_host *host)
221 int rest_of_selfids = host->selfid_count;
222 struct selfid *sid = (struct selfid *)host->topology_map;
223 struct ext_selfid *esid;
226 host->nodes_active = 0;
228 while (rest_of_selfids--) {
229 if (!sid->extended) {
233 if (sid->phy_id != nodeid) {
234 HPSB_INFO("SelfIDs failed monotony check with "
239 if (sid->link_active) {
240 host->nodes_active++;
242 host->irm_id = LOCAL_BUS | sid->phy_id;
245 esid = (struct ext_selfid *)sid;
247 if ((esid->phy_id != nodeid)
248 || (esid->seq_nr != esid_seq)) {
249 HPSB_INFO("SelfIDs failed monotony check with "
250 "%d/%d", esid->phy_id, esid->seq_nr);
258 esid = (struct ext_selfid *)(sid - 1);
259 while (esid->extended) {
260 if ((esid->porta == 0x2) || (esid->portb == 0x2)
261 || (esid->portc == 0x2) || (esid->portd == 0x2)
262 || (esid->porte == 0x2) || (esid->portf == 0x2)
263 || (esid->portg == 0x2) || (esid->porth == 0x2)) {
264 HPSB_INFO("SelfIDs failed root check on "
271 sid = (struct selfid *)esid;
272 if ((sid->port0 == 0x2) || (sid->port1 == 0x2) || (sid->port2 == 0x2)) {
273 HPSB_INFO("SelfIDs failed root check");
277 host->node_count = nodeid + 1;
281 static void build_speed_map(struct hpsb_host *host, int nodecount)
283 u8 speedcap[nodecount];
284 u8 cldcnt[nodecount];
285 u8 *map = host->speed_map;
287 struct ext_selfid *esid;
290 for (i = 0; i < (nodecount * 64); i += 64) {
291 for (j = 0; j < nodecount; j++) {
292 map[i+j] = IEEE1394_SPEED_MAX;
296 for (i = 0; i < nodecount; i++) {
300 /* find direct children count and speed */
301 for (sid = (struct selfid *)&host->topology_map[host->selfid_count-1],
303 (void *)sid >= (void *)host->topology_map; sid--) {
305 esid = (struct ext_selfid *)sid;
307 if (esid->porta == 0x3) cldcnt[n]++;
308 if (esid->portb == 0x3) cldcnt[n]++;
309 if (esid->portc == 0x3) cldcnt[n]++;
310 if (esid->portd == 0x3) cldcnt[n]++;
311 if (esid->porte == 0x3) cldcnt[n]++;
312 if (esid->portf == 0x3) cldcnt[n]++;
313 if (esid->portg == 0x3) cldcnt[n]++;
314 if (esid->porth == 0x3) cldcnt[n]++;
316 if (sid->port0 == 0x3) cldcnt[n]++;
317 if (sid->port1 == 0x3) cldcnt[n]++;
318 if (sid->port2 == 0x3) cldcnt[n]++;
320 speedcap[n] = sid->speed;
325 /* set self mapping */
326 for (i = 0; i < nodecount; i++) {
327 map[64*i + i] = speedcap[i];
330 /* fix up direct children count to total children count;
331 * also fix up speedcaps for sibling and parent communication */
332 for (i = 1; i < nodecount; i++) {
333 for (j = cldcnt[i], n = i - 1; j > 0; j--) {
334 cldcnt[i] += cldcnt[n];
335 speedcap[n] = min(speedcap[n], speedcap[i]);
340 for (n = 0; n < nodecount; n++) {
341 for (i = n - cldcnt[n]; i <= n; i++) {
342 for (j = 0; j < (n - cldcnt[n]); j++) {
343 map[j*64 + i] = map[i*64 + j] =
344 min(map[i*64 + j], speedcap[n]);
346 for (j = n + 1; j < nodecount; j++) {
347 map[j*64 + i] = map[i*64 + j] =
348 min(map[i*64 + j], speedcap[n]);
355 void hpsb_selfid_received(struct hpsb_host *host, quadlet_t sid)
357 if (host->in_bus_reset) {
358 HPSB_VERBOSE("Including SelfID 0x%x", sid);
359 host->topology_map[host->selfid_count++] = sid;
361 HPSB_NOTICE("Spurious SelfID packet (0x%08x) received from bus %d",
362 sid, NODEID_TO_BUS(host->node_id));
366 void hpsb_selfid_complete(struct hpsb_host *host, int phyid, int isroot)
368 if (!host->in_bus_reset)
369 HPSB_NOTICE("SelfID completion called outside of bus reset!");
371 host->node_id = LOCAL_BUS | phyid;
372 host->is_root = isroot;
374 if (!check_selfids(host)) {
375 if (host->reset_retries++ < 20) {
376 /* selfid stage did not complete without error */
377 HPSB_NOTICE("Error in SelfID stage, resetting");
378 host->in_bus_reset = 0;
379 /* this should work from ohci1394 now... */
380 hpsb_reset_bus(host, LONG_RESET);
383 HPSB_NOTICE("Stopping out-of-control reset loop");
384 HPSB_NOTICE("Warning - topology map and speed map will not be valid");
385 host->reset_retries = 0;
388 host->reset_retries = 0;
389 build_speed_map(host, host->node_count);
392 HPSB_VERBOSE("selfid_complete called with successful SelfID stage "
393 "... irm_id: 0x%X node_id: 0x%X",host->irm_id,host->node_id);
395 /* irm_id is kept up to date by check_selfids() */
396 if (host->irm_id == host->node_id) {
404 host->driver->devctl(host, ACT_CYCLE_MASTER, 1);
407 atomic_inc(&host->generation);
408 host->in_bus_reset = 0;
409 highlevel_host_reset(host);
413 void hpsb_packet_sent(struct hpsb_host *host, struct hpsb_packet *packet,
418 packet->ack_code = ackcode;
420 if (packet->no_waiter) {
421 /* must not have a tlabel allocated */
422 free_hpsb_packet(packet);
426 if (ackcode != ACK_PENDING || !packet->expect_response) {
427 packet->state = hpsb_complete;
428 up(&packet->state_change);
429 up(&packet->state_change);
430 run_packet_complete(packet);
434 packet->state = hpsb_pending;
435 packet->sendtime = jiffies;
437 spin_lock_irqsave(&host->pending_pkt_lock, flags);
438 list_add_tail(&packet->list, &host->pending_packets);
439 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
441 up(&packet->state_change);
442 mod_timer(&host->timeout, jiffies + host->timeout_interval);
446 * hpsb_send_phy_config - transmit a PHY configuration packet on the bus
447 * @host: host that PHY config packet gets sent through
448 * @rootid: root whose force_root bit should get set (-1 = don't set force_root)
449 * @gapcnt: gap count value to set (-1 = don't set gap count)
451 * This function sends a PHY config packet on the bus through the specified host.
453 * Return value: 0 for success or error number otherwise.
455 int hpsb_send_phy_config(struct hpsb_host *host, int rootid, int gapcnt)
457 struct hpsb_packet *packet;
460 if (rootid >= ALL_NODES || rootid < -1 || gapcnt > 0x3f || gapcnt < -1 ||
461 (rootid == -1 && gapcnt == -1)) {
462 HPSB_DEBUG("Invalid Parameter: rootid = %d gapcnt = %d",
467 packet = alloc_hpsb_packet(0);
472 packet->header_size = 8;
473 packet->data_size = 0;
474 packet->expect_response = 0;
475 packet->no_waiter = 0;
476 packet->type = hpsb_raw;
477 packet->header[0] = 0;
479 packet->header[0] |= rootid << 24 | 1 << 23;
481 packet->header[0] |= gapcnt << 16 | 1 << 22;
483 packet->header[1] = ~packet->header[0];
485 packet->generation = get_hpsb_generation(host);
487 if (!hpsb_send_packet(packet)) {
492 down(&packet->state_change);
493 down(&packet->state_change);
496 free_hpsb_packet(packet);
502 * hpsb_send_packet - transmit a packet on the bus
503 * @packet: packet to send
505 * The packet is sent through the host specified in the packet->host field.
506 * Before sending, the packet's transmit speed is automatically determined using
507 * the local speed map when it is an async, non-broadcast packet.
509 * Possibilities for failure are that host is either not initialized, in bus
510 * reset, the packet's generation number doesn't match the current generation
511 * number or the host reports a transmit error.
513 * Return value: False (0) on failure, true (1) otherwise.
515 int hpsb_send_packet(struct hpsb_packet *packet)
517 struct hpsb_host *host = packet->host;
519 if (host->is_shutdown || host->in_bus_reset
520 || (packet->generation != get_hpsb_generation(host))) {
524 packet->state = hpsb_queued;
526 if (packet->node_id == host->node_id)
527 { /* it is a local request, so handle it locally */
529 size_t size=packet->data_size+packet->header_size;
531 data = kmalloc(packet->header_size + packet->data_size, GFP_ATOMIC);
533 HPSB_ERR("unable to allocate memory for concatenating header and data");
537 memcpy(data, packet->header, packet->header_size);
539 if (packet->data_size)
541 if (packet->data_be) {
542 memcpy(((u8*)data)+packet->header_size, packet->data, packet->data_size);
545 quadlet_t *my_data=(quadlet_t*) ((u8*) data + packet->data_size);
546 for (i=0; i < packet->data_size/4; i++) {
547 my_data[i] = cpu_to_be32(packet->data[i]);
552 dump_packet("send packet local:", packet->header,
553 packet->header_size);
555 hpsb_packet_sent(host, packet, packet->expect_response?ACK_PENDING:ACK_COMPLETE);
556 hpsb_packet_received(host, data, size, 0);
563 if (packet->type == hpsb_async && packet->node_id != ALL_NODES) {
565 host->speed_map[NODEID_TO_NODE(host->node_id) * 64
566 + NODEID_TO_NODE(packet->node_id)];
569 switch (packet->speed_code) {
571 dump_packet("send packet 400:", packet->header,
572 packet->header_size);
575 dump_packet("send packet 200:", packet->header,
576 packet->header_size);
579 dump_packet("send packet 100:", packet->header,
580 packet->header_size);
583 return host->driver->transmit_packet(host, packet);
586 static void send_packet_nocare(struct hpsb_packet *packet)
588 if (!hpsb_send_packet(packet)) {
589 free_hpsb_packet(packet);
594 void handle_packet_response(struct hpsb_host *host, int tcode, quadlet_t *data,
597 struct hpsb_packet *packet = NULL;
598 struct list_head *lh;
603 tlabel = (data[0] >> 10) & 0x3f;
605 spin_lock_irqsave(&host->pending_pkt_lock, flags);
607 list_for_each(lh, &host->pending_packets) {
608 packet = list_entry(lh, struct hpsb_packet, list);
609 if ((packet->tlabel == tlabel)
610 && (packet->node_id == (data[1] >> 16))){
615 if (lh == &host->pending_packets) {
616 HPSB_DEBUG("unsolicited response packet received - no tlabel match");
617 dump_packet("contents:", data, 16);
618 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
622 switch (packet->tcode) {
625 if (tcode == TCODE_WRITE_RESPONSE) tcode_match = 1;
628 if (tcode == TCODE_READQ_RESPONSE) tcode_match = 1;
631 if (tcode == TCODE_READB_RESPONSE) tcode_match = 1;
633 case TCODE_LOCK_REQUEST:
634 if (tcode == TCODE_LOCK_RESPONSE) tcode_match = 1;
638 if (!tcode_match || (packet->tlabel != tlabel)
639 || (packet->node_id != (data[1] >> 16))) {
640 HPSB_INFO("unsolicited response packet received - tcode mismatch");
641 dump_packet("contents:", data, 16);
643 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
647 list_del(&packet->list);
649 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
651 /* FIXME - update size fields? */
653 case TCODE_WRITE_RESPONSE:
654 memcpy(packet->header, data, 12);
656 case TCODE_READQ_RESPONSE:
657 memcpy(packet->header, data, 16);
659 case TCODE_READB_RESPONSE:
660 memcpy(packet->header, data, 16);
661 memcpy(packet->data, data + 4, size - 16);
663 case TCODE_LOCK_RESPONSE:
664 memcpy(packet->header, data, 16);
665 memcpy(packet->data, data + 4, (size - 16) > 8 ? 8 : size - 16);
669 packet->state = hpsb_complete;
670 up(&packet->state_change);
671 run_packet_complete(packet);
675 static struct hpsb_packet *create_reply_packet(struct hpsb_host *host,
676 quadlet_t *data, size_t dsize)
678 struct hpsb_packet *p;
680 dsize += (dsize % 4 ? 4 - (dsize % 4) : 0);
682 p = alloc_hpsb_packet(dsize);
684 /* FIXME - send data_error response */
688 p->type = hpsb_async;
689 p->state = hpsb_unused;
691 p->node_id = data[1] >> 16;
692 p->tlabel = (data[0] >> 10) & 0x3f;
695 p->generation = get_hpsb_generation(host);
698 p->data[dsize / 4] = 0;
704 #define PREP_ASYNC_HEAD_RCODE(tc) \
705 packet->tcode = tc; \
706 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
707 | (1 << 8) | (tc << 4); \
708 packet->header[1] = (packet->host->node_id << 16) | (rcode << 12); \
709 packet->header[2] = 0
711 static void fill_async_readquad_resp(struct hpsb_packet *packet, int rcode,
714 PREP_ASYNC_HEAD_RCODE(TCODE_READQ_RESPONSE);
715 packet->header[3] = data;
716 packet->header_size = 16;
717 packet->data_size = 0;
720 static void fill_async_readblock_resp(struct hpsb_packet *packet, int rcode,
723 if (rcode != RCODE_COMPLETE)
726 PREP_ASYNC_HEAD_RCODE(TCODE_READB_RESPONSE);
727 packet->header[3] = length << 16;
728 packet->header_size = 16;
729 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
732 static void fill_async_write_resp(struct hpsb_packet *packet, int rcode)
734 PREP_ASYNC_HEAD_RCODE(TCODE_WRITE_RESPONSE);
735 packet->header[2] = 0;
736 packet->header_size = 12;
737 packet->data_size = 0;
740 static void fill_async_lock_resp(struct hpsb_packet *packet, int rcode, int extcode,
743 if (rcode != RCODE_COMPLETE)
746 PREP_ASYNC_HEAD_RCODE(TCODE_LOCK_RESPONSE);
747 packet->header[3] = (length << 16) | extcode;
748 packet->header_size = 16;
749 packet->data_size = length;
752 #define PREP_REPLY_PACKET(length) \
753 packet = create_reply_packet(host, data, length); \
754 if (packet == NULL) break
756 static void handle_incoming_packet(struct hpsb_host *host, int tcode,
757 quadlet_t *data, size_t size, int write_acked)
759 struct hpsb_packet *packet;
760 int length, rcode, extcode;
762 nodeid_t source = data[1] >> 16;
763 nodeid_t dest = data[0] >> 16;
764 u16 flags = (u16) data[0];
767 /* big FIXME - no error checking is done for an out of bounds length */
771 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
772 rcode = highlevel_write(host, source, dest, data+3,
776 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
778 /* not a broadcast write, reply */
779 PREP_REPLY_PACKET(0);
780 fill_async_write_resp(packet, rcode);
781 send_packet_nocare(packet);
786 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
787 rcode = highlevel_write(host, source, dest, data+4,
788 addr, data[3]>>16, flags);
791 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
793 /* not a broadcast write, reply */
794 PREP_REPLY_PACKET(0);
795 fill_async_write_resp(packet, rcode);
796 send_packet_nocare(packet);
801 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
802 rcode = highlevel_read(host, source, &buffer, addr, 4, flags);
805 PREP_REPLY_PACKET(0);
806 fill_async_readquad_resp(packet, rcode, buffer);
807 send_packet_nocare(packet);
812 length = data[3] >> 16;
813 PREP_REPLY_PACKET(length);
815 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
816 rcode = highlevel_read(host, source, packet->data, addr,
820 fill_async_readblock_resp(packet, rcode, length);
821 send_packet_nocare(packet);
823 free_hpsb_packet(packet);
827 case TCODE_LOCK_REQUEST:
828 length = data[3] >> 16;
829 extcode = data[3] & 0xffff;
830 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
832 PREP_REPLY_PACKET(8);
834 if ((extcode == 0) || (extcode >= 7)) {
835 /* let switch default handle error */
841 rcode = highlevel_lock(host, source, packet->data, addr,
842 data[4], 0, extcode,flags);
843 fill_async_lock_resp(packet, rcode, extcode, 4);
846 if ((extcode != EXTCODE_FETCH_ADD)
847 && (extcode != EXTCODE_LITTLE_ADD)) {
848 rcode = highlevel_lock(host, source,
852 fill_async_lock_resp(packet, rcode, extcode, 4);
854 rcode = highlevel_lock64(host, source,
855 (octlet_t *)packet->data, addr,
856 *(octlet_t *)(data + 4), 0ULL,
858 fill_async_lock_resp(packet, rcode, extcode, 8);
862 rcode = highlevel_lock64(host, source,
863 (octlet_t *)packet->data, addr,
864 *(octlet_t *)(data + 6),
865 *(octlet_t *)(data + 4),
867 fill_async_lock_resp(packet, rcode, extcode, 8);
870 rcode = RCODE_TYPE_ERROR;
871 fill_async_lock_resp(packet, rcode,
876 send_packet_nocare(packet);
878 free_hpsb_packet(packet);
884 #undef PREP_REPLY_PACKET
887 void hpsb_packet_received(struct hpsb_host *host, quadlet_t *data, size_t size,
892 if (host->in_bus_reset) {
893 HPSB_INFO("received packet during reset; ignoring");
897 dump_packet("received packet:", data, size);
899 tcode = (data[0] >> 4) & 0xf;
902 case TCODE_WRITE_RESPONSE:
903 case TCODE_READQ_RESPONSE:
904 case TCODE_READB_RESPONSE:
905 case TCODE_LOCK_RESPONSE:
906 handle_packet_response(host, tcode, data, size);
913 case TCODE_LOCK_REQUEST:
914 handle_incoming_packet(host, tcode, data, size, write_acked);
919 highlevel_iso_receive(host, data, size);
922 case TCODE_CYCLE_START:
923 /* simply ignore this packet if it is passed on */
927 HPSB_NOTICE("received packet with bogus transaction code %d",
934 void abort_requests(struct hpsb_host *host)
937 struct hpsb_packet *packet;
938 struct list_head *lh, *tlh;
941 host->driver->devctl(host, CANCEL_REQUESTS, 0);
943 spin_lock_irqsave(&host->pending_pkt_lock, flags);
944 list_splice(&host->pending_packets, &llist);
945 INIT_LIST_HEAD(&host->pending_packets);
946 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
948 list_for_each_safe(lh, tlh, &llist) {
949 packet = list_entry(lh, struct hpsb_packet, list);
950 list_del(&packet->list);
951 packet->state = hpsb_complete;
952 packet->ack_code = ACKX_ABORTED;
953 up(&packet->state_change);
954 run_packet_complete(packet);
958 void abort_timedouts(unsigned long __opaque)
960 struct hpsb_host *host = (struct hpsb_host *)__opaque;
962 struct hpsb_packet *packet;
963 unsigned long expire;
964 struct list_head *lh, *tlh;
965 LIST_HEAD(expiredlist);
967 spin_lock_irqsave(&host->csr.lock, flags);
968 expire = host->csr.expire;
969 spin_unlock_irqrestore(&host->csr.lock, flags);
971 spin_lock_irqsave(&host->pending_pkt_lock, flags);
973 list_for_each_safe(lh, tlh, &host->pending_packets) {
974 packet = list_entry(lh, struct hpsb_packet, list);
975 if (time_before(packet->sendtime + expire, jiffies)) {
976 list_del(&packet->list);
977 list_add(&packet->list, &expiredlist);
981 if (!list_empty(&host->pending_packets))
982 mod_timer(&host->timeout, jiffies + host->timeout_interval);
984 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
986 list_for_each_safe(lh, tlh, &expiredlist) {
987 packet = list_entry(lh, struct hpsb_packet, list);
988 list_del(&packet->list);
989 packet->state = hpsb_complete;
990 packet->ack_code = ACKX_TIMEOUT;
991 up(&packet->state_change);
992 run_packet_complete(packet);
998 * character device dispatching (see ieee1394_core.h)
999 * Dan Maas <dmaas@dcine.com>
1003 struct file_operations *file_ops;
1004 struct module *module;
1005 } ieee1394_chardevs[16];
1007 static rwlock_t ieee1394_chardevs_lock = RW_LOCK_UNLOCKED;
1009 static int ieee1394_dispatch_open(struct inode *inode, struct file *file);
1011 static struct file_operations ieee1394_chardev_ops = {
1012 .owner =THIS_MODULE,
1013 .open = ieee1394_dispatch_open,
1016 devfs_handle_t ieee1394_devfs_handle;
1019 /* claim a block of minor numbers */
1020 int ieee1394_register_chardev(int blocknum,
1021 struct module *module,
1022 struct file_operations *file_ops)
1026 if ( (blocknum < 0) || (blocknum > 15) )
1029 write_lock(&ieee1394_chardevs_lock);
1031 if (ieee1394_chardevs[blocknum].file_ops == NULL) {
1032 /* grab the minor block */
1033 ieee1394_chardevs[blocknum].file_ops = file_ops;
1034 ieee1394_chardevs[blocknum].module = module;
1038 /* block already taken */
1042 write_unlock(&ieee1394_chardevs_lock);
1047 /* release a block of minor numbers */
1048 void ieee1394_unregister_chardev(int blocknum)
1050 if ( (blocknum < 0) || (blocknum > 15) )
1053 write_lock(&ieee1394_chardevs_lock);
1055 if (ieee1394_chardevs[blocknum].file_ops) {
1056 ieee1394_chardevs[blocknum].file_ops = NULL;
1057 ieee1394_chardevs[blocknum].module = NULL;
1060 write_unlock(&ieee1394_chardevs_lock);
1064 ieee1394_get_chardev() - look up and acquire a character device
1065 driver that has previously registered using ieee1394_register_chardev()
1067 On success, returns 1 and sets module and file_ops to the driver.
1068 The module will have an incremented reference count.
1070 On failure, returns 0.
1071 The module will NOT have an incremented reference count.
1074 static int ieee1394_get_chardev(int blocknum,
1075 struct module **module,
1076 struct file_operations **file_ops)
1080 if ((blocknum < 0) || (blocknum > 15))
1083 read_lock(&ieee1394_chardevs_lock);
1085 *module = ieee1394_chardevs[blocknum].module;
1086 *file_ops = ieee1394_chardevs[blocknum].file_ops;
1088 if (*file_ops == NULL)
1091 /* don't need try_inc_mod_count if the driver is non-modular */
1092 if (*module && (try_inc_mod_count(*module) == 0))
1099 read_unlock(&ieee1394_chardevs_lock);
1103 /* the point of entry for open() on any ieee1394 character device */
1104 static int ieee1394_dispatch_open(struct inode *inode, struct file *file)
1106 struct file_operations *file_ops;
1107 struct module *module;
1112 Maintaining correct module reference counts is tricky here!
1114 The key thing to remember is that the VFS increments the
1115 reference count of ieee1394 before it calls
1116 ieee1394_dispatch_open().
1118 If the open() succeeds, then we need to transfer this extra
1119 reference to the task-specific driver module (e.g. raw1394).
1120 The VFS will deref the driver module automatically when the
1121 file is later released.
1123 If the open() fails, then the VFS will drop the
1124 reference count of whatever module file->f_op->owner points
1125 to, immediately after this function returns.
1128 /* shift away lower four bits of the minor
1129 to get the index of the ieee1394_driver
1132 blocknum = (MINOR(inode->i_rdev) >> 4) & 0xF;
1134 /* look up the driver */
1136 if (ieee1394_get_chardev(blocknum, &module, &file_ops) == 0)
1139 /* redirect all subsequent requests to the driver's
1140 own file_operations */
1141 file->f_op = file_ops;
1143 /* at this point BOTH ieee1394 and the task-specific driver have
1144 an extra reference */
1146 /* follow through with the open() */
1147 retval = file_ops->open(inode, file);
1151 /* If the open() succeeded, then ieee1394 will be left
1152 with an extra module reference, so we discard it here.
1154 The task-specific driver still has the extra
1155 reference given to it by ieee1394_get_chardev().
1156 This extra reference prevents the module from
1157 unloading while the file is open, and will be
1158 dropped by the VFS when the file is released.
1162 __MOD_DEC_USE_COUNT((struct module*) THIS_MODULE);
1164 /* note that if ieee1394 is compiled into the kernel,
1165 THIS_MODULE will be (void*) NULL, hence the if and
1166 the cast are necessary */
1170 /* if the open() failed, then we need to drop the
1171 extra reference we gave to the task-specific
1175 __MOD_DEC_USE_COUNT(module);
1177 /* point the file's f_ops back to ieee1394. The VFS will then
1178 decrement ieee1394's reference count immediately after this
1179 function returns. */
1181 file->f_op = &ieee1394_chardev_ops;
1187 struct proc_dir_entry *ieee1394_procfs_entry;
1189 static int __init ieee1394_init(void)
1191 hpsb_packet_cache = kmem_cache_create("hpsb_packet", sizeof(struct hpsb_packet),
1194 ieee1394_devfs_handle = devfs_mk_dir(NULL, "ieee1394", NULL);
1196 if (register_chrdev(IEEE1394_MAJOR, "ieee1394", &ieee1394_chardev_ops)) {
1197 HPSB_ERR("unable to register character device major %d!\n", IEEE1394_MAJOR);
1198 devfs_unregister(ieee1394_devfs_handle);
1202 #ifdef CONFIG_PROC_FS
1203 /* Must be done before we start everything else, since the drivers
1205 ieee1394_procfs_entry = proc_mkdir("ieee1394", proc_bus);
1206 if (ieee1394_procfs_entry == NULL) {
1207 HPSB_ERR("unable to create /proc/bus/ieee1394\n");
1208 unregister_chrdev(IEEE1394_MAJOR, "ieee1394");
1209 devfs_unregister(ieee1394_devfs_handle);
1212 ieee1394_procfs_entry->owner = THIS_MODULE;
1215 init_hpsb_highlevel();
1217 if (!disable_nodemgr)
1218 init_ieee1394_nodemgr();
1220 HPSB_INFO("nodemgr functionality disabled");
1225 static void __exit ieee1394_cleanup(void)
1227 if (!disable_nodemgr)
1228 cleanup_ieee1394_nodemgr();
1231 kmem_cache_destroy(hpsb_packet_cache);
1233 unregister_chrdev(IEEE1394_MAJOR, "ieee1394");
1235 /* it's ok to pass a NULL devfs_handle to devfs_unregister */
1236 devfs_unregister(ieee1394_devfs_handle);
1238 remove_proc_entry("ieee1394", proc_bus);
1241 module_init(ieee1394_init);
1242 module_exit(ieee1394_cleanup);
1244 /* Exported symbols */
1247 EXPORT_SYMBOL(hpsb_alloc_host);
1248 EXPORT_SYMBOL(hpsb_add_host);
1249 EXPORT_SYMBOL(hpsb_remove_host);
1250 EXPORT_SYMBOL(hpsb_ref_host);
1251 EXPORT_SYMBOL(hpsb_unref_host);
1253 /** ieee1394_core.c **/
1254 EXPORT_SYMBOL(hpsb_speedto_str);
1255 EXPORT_SYMBOL(hpsb_set_packet_complete_task);
1256 EXPORT_SYMBOL(alloc_hpsb_packet);
1257 EXPORT_SYMBOL(free_hpsb_packet);
1258 EXPORT_SYMBOL(hpsb_send_phy_config);
1259 EXPORT_SYMBOL(hpsb_send_packet);
1260 EXPORT_SYMBOL(hpsb_reset_bus);
1261 EXPORT_SYMBOL(hpsb_bus_reset);
1262 EXPORT_SYMBOL(hpsb_selfid_received);
1263 EXPORT_SYMBOL(hpsb_selfid_complete);
1264 EXPORT_SYMBOL(hpsb_packet_sent);
1265 EXPORT_SYMBOL(hpsb_packet_received);
1266 EXPORT_SYMBOL(ieee1394_register_chardev);
1267 EXPORT_SYMBOL(ieee1394_unregister_chardev);
1268 EXPORT_SYMBOL(ieee1394_devfs_handle);
1269 EXPORT_SYMBOL(ieee1394_procfs_entry);
1271 /** ieee1394_transactions.c **/
1272 EXPORT_SYMBOL(hpsb_get_tlabel);
1273 EXPORT_SYMBOL(hpsb_free_tlabel);
1274 EXPORT_SYMBOL(hpsb_make_readpacket);
1275 EXPORT_SYMBOL(hpsb_make_writepacket);
1276 EXPORT_SYMBOL(hpsb_make_streampacket);
1277 EXPORT_SYMBOL(hpsb_make_lockpacket);
1278 EXPORT_SYMBOL(hpsb_make_lock64packet);
1279 EXPORT_SYMBOL(hpsb_make_phypacket);
1280 EXPORT_SYMBOL(hpsb_make_isopacket);
1281 EXPORT_SYMBOL(hpsb_read);
1282 EXPORT_SYMBOL(hpsb_write);
1283 EXPORT_SYMBOL(hpsb_lock);
1284 EXPORT_SYMBOL(hpsb_lock64);
1285 EXPORT_SYMBOL(hpsb_send_gasp);
1286 EXPORT_SYMBOL(hpsb_packet_success);
1289 EXPORT_SYMBOL(hpsb_register_highlevel);
1290 EXPORT_SYMBOL(hpsb_unregister_highlevel);
1291 EXPORT_SYMBOL(hpsb_register_addrspace);
1292 EXPORT_SYMBOL(hpsb_unregister_addrspace);
1293 EXPORT_SYMBOL(hpsb_listen_channel);
1294 EXPORT_SYMBOL(hpsb_unlisten_channel);
1295 EXPORT_SYMBOL(hpsb_get_hostinfo);
1296 EXPORT_SYMBOL(hpsb_get_host_bykey);
1297 EXPORT_SYMBOL(hpsb_create_hostinfo);
1298 EXPORT_SYMBOL(hpsb_destroy_hostinfo);
1299 EXPORT_SYMBOL(hpsb_set_hostinfo_key);
1300 EXPORT_SYMBOL(hpsb_get_hostinfo_key);
1301 EXPORT_SYMBOL(hpsb_get_hostinfo_bykey);
1302 EXPORT_SYMBOL(hpsb_set_hostinfo);
1303 EXPORT_SYMBOL(highlevel_read);
1304 EXPORT_SYMBOL(highlevel_write);
1305 EXPORT_SYMBOL(highlevel_lock);
1306 EXPORT_SYMBOL(highlevel_lock64);
1307 EXPORT_SYMBOL(highlevel_add_host);
1308 EXPORT_SYMBOL(highlevel_remove_host);
1309 EXPORT_SYMBOL(highlevel_host_reset);
1312 EXPORT_SYMBOL(hpsb_guid_get_entry);
1313 EXPORT_SYMBOL(hpsb_nodeid_get_entry);
1314 EXPORT_SYMBOL(hpsb_node_fill_packet);
1315 EXPORT_SYMBOL(hpsb_node_read);
1316 EXPORT_SYMBOL(hpsb_node_write);
1317 EXPORT_SYMBOL(hpsb_node_lock);
1318 EXPORT_SYMBOL(hpsb_register_protocol);
1319 EXPORT_SYMBOL(hpsb_unregister_protocol);
1320 EXPORT_SYMBOL(hpsb_release_unit_directory);
1323 EXPORT_SYMBOL(hpsb_update_config_rom);
1324 EXPORT_SYMBOL(hpsb_get_config_rom);
1327 EXPORT_SYMBOL(dma_prog_region_init);
1328 EXPORT_SYMBOL(dma_prog_region_alloc);
1329 EXPORT_SYMBOL(dma_prog_region_free);
1330 EXPORT_SYMBOL(dma_region_init);
1331 EXPORT_SYMBOL(dma_region_alloc);
1332 EXPORT_SYMBOL(dma_region_free);
1333 EXPORT_SYMBOL(dma_region_sync);
1334 EXPORT_SYMBOL(dma_region_mmap);
1335 EXPORT_SYMBOL(dma_region_offset_to_bus);
1338 EXPORT_SYMBOL(hpsb_iso_xmit_init);
1339 EXPORT_SYMBOL(hpsb_iso_recv_init);
1340 EXPORT_SYMBOL(hpsb_iso_xmit_start);
1341 EXPORT_SYMBOL(hpsb_iso_recv_start);
1342 EXPORT_SYMBOL(hpsb_iso_recv_listen_channel);
1343 EXPORT_SYMBOL(hpsb_iso_recv_unlisten_channel);
1344 EXPORT_SYMBOL(hpsb_iso_recv_set_channel_mask);
1345 EXPORT_SYMBOL(hpsb_iso_stop);
1346 EXPORT_SYMBOL(hpsb_iso_shutdown);
1347 EXPORT_SYMBOL(hpsb_iso_xmit_queue_packet);
1348 EXPORT_SYMBOL(hpsb_iso_xmit_sync);
1349 EXPORT_SYMBOL(hpsb_iso_recv_release_packets);
1350 EXPORT_SYMBOL(hpsb_iso_n_ready);
1351 EXPORT_SYMBOL(hpsb_iso_packet_sent);
1352 EXPORT_SYMBOL(hpsb_iso_packet_received);
1353 EXPORT_SYMBOL(hpsb_iso_wake);
1354 EXPORT_SYMBOL(hpsb_iso_recv_flush);