- Kai Germaschewski: ymfpci cleanups and resource leak fixes
[davej-history.git] / drivers / ieee1394 / ieee1394_core.c
blobd186fc7559d392611a83d815734ecde45b6f7ccd
1 /*
2 * IEEE 1394 for Linux
4 * Core support: hpsb_packet management, packet handling and forwarding to
5 * highlevel or lowlevel code
7 * Copyright (C) 1999, 2000 Andreas E. Bombe
9 * This code is licensed under the GPL. See the file COPYING in the root
10 * directory of the kernel sources for details.
13 #include <linux/config.h>
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/string.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/interrupt.h>
20 #include <asm/bitops.h>
21 #include <asm/byteorder.h>
22 #include <asm/semaphore.h>
24 #include "ieee1394_types.h"
25 #include "ieee1394.h"
26 #include "hosts.h"
27 #include "ieee1394_core.h"
28 #include "highlevel.h"
29 #include "ieee1394_transactions.h"
30 #include "csr.h"
31 #include "guid.h"
34 atomic_t hpsb_generation = ATOMIC_INIT(0);
37 static void dump_packet(const char *text, quadlet_t *data, int size)
39 int i;
41 size /= 4;
42 size = (size > 4 ? 4 : size);
44 printk(KERN_DEBUG "ieee1394: %s", text);
45 for (i = 0; i < size; i++) {
46 printk(" %8.8x", data[i]);
48 printk("\n");
52 /**
53 * alloc_hpsb_packet - allocate new packet structure
54 * @data_size: size of the data block to be allocated
56 * This function allocates, initializes and returns a new &struct hpsb_packet.
57 * It can be used in interrupt context. A header block is always included, its
58 * size is big enough to contain all possible 1394 headers. The data block is
59 * only allocated when @data_size is not zero.
61 * For packets for which responses will be received the @data_size has to be big
62 * enough to contain the response's data block since no further allocation
63 * occurs at response matching time.
65 * The packet's generation value will be set to the current generation number
66 * for ease of use. Remember to overwrite it with your own recorded generation
67 * number if you can not be sure that your code will not race with a bus reset.
69 * Return value: A pointer to a &struct hpsb_packet or NULL on allocation
70 * failure.
72 struct hpsb_packet *alloc_hpsb_packet(size_t data_size)
74 struct hpsb_packet *packet = NULL;
75 void *header = NULL, *data = NULL;
76 int kmflags = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
78 packet = kmalloc(sizeof(struct hpsb_packet), kmflags);
79 header = kmalloc(5 * 4, kmflags);
80 if (header == NULL || packet == NULL) {
81 kfree(header);
82 kfree(packet);
83 return NULL;
86 memset(packet, 0, sizeof(struct hpsb_packet));
87 packet->header = header;
89 if (data_size) {
90 data = kmalloc(data_size + 8, kmflags);
91 if (data == NULL) {
92 kfree(header);
93 kfree(packet);
94 return NULL;
97 packet->data = data;
98 packet->data_size = data_size;
101 INIT_LIST_HEAD(&packet->list);
102 sema_init(&packet->state_change, 0);
103 packet->state = unused;
104 packet->generation = get_hpsb_generation();
105 packet->data_be = 1;
107 return packet;
112 * free_hpsb_packet - free packet and data associated with it
113 * @packet: packet to free (is NULL safe)
115 * This function will free packet->data, packet->header and finally the packet
116 * itself.
118 void free_hpsb_packet(struct hpsb_packet *packet)
120 if (packet == NULL) {
121 return;
124 kfree(packet->data);
125 kfree(packet->header);
126 kfree(packet);
130 int hpsb_reset_bus(struct hpsb_host *host)
132 if (!host->initialized) {
133 return 1;
136 if (!hpsb_bus_reset(host)) {
137 host->template->devctl(host, RESET_BUS, 0);
138 return 0;
139 } else {
140 return 1;
145 int hpsb_bus_reset(struct hpsb_host *host)
147 if (host->in_bus_reset) {
148 HPSB_NOTICE(__FUNCTION__
149 " called while bus reset already in progress");
150 return 1;
153 abort_requests(host);
154 host->in_bus_reset = 1;
155 host->irm_id = -1;
156 host->busmgr_id = -1;
157 host->node_count = 0;
158 host->selfid_count = 0;
160 return 0;
165 * Verify num_of_selfids SelfIDs and return number of nodes. Return zero in
166 * case verification failed.
168 static int check_selfids(struct hpsb_host *host, unsigned int num_of_selfids)
170 int nodeid = -1;
171 int rest_of_selfids = num_of_selfids;
172 struct selfid *sid = (struct selfid *)host->topology_map;
173 struct ext_selfid *esid;
174 int esid_seq = 23;
176 while (rest_of_selfids--) {
177 if (!sid->extended) {
178 nodeid++;
179 esid_seq = 0;
181 if (sid->phy_id != nodeid) {
182 HPSB_INFO("SelfIDs failed monotony check with "
183 "%d", sid->phy_id);
184 return 0;
187 if (sid->contender && sid->link_active) {
188 host->irm_id = LOCAL_BUS | sid->phy_id;
190 } else {
191 esid = (struct ext_selfid *)sid;
193 if ((esid->phy_id != nodeid)
194 || (esid->seq_nr != esid_seq)) {
195 HPSB_INFO("SelfIDs failed monotony check with "
196 "%d/%d", esid->phy_id, esid->seq_nr);
197 return 0;
199 esid_seq++;
201 sid++;
204 esid = (struct ext_selfid *)(sid - 1);
205 while (esid->extended) {
206 if ((esid->porta == 0x2) || (esid->portb == 0x2)
207 || (esid->portc == 0x2) || (esid->portd == 0x2)
208 || (esid->porte == 0x2) || (esid->portf == 0x2)
209 || (esid->portg == 0x2) || (esid->porth == 0x2)) {
210 HPSB_INFO("SelfIDs failed root check on "
211 "extended SelfID");
212 return 0;
214 esid--;
217 sid = (struct selfid *)esid;
218 if ((sid->port0 == 0x2) || (sid->port1 == 0x2) || (sid->port2 == 0x2)) {
219 HPSB_INFO("SelfIDs failed root check");
220 return 0;
223 return nodeid + 1;
226 static void build_speed_map(struct hpsb_host *host, int nodecount)
228 char speedcap[nodecount];
229 char cldcnt[nodecount];
230 u8 *map = host->speed_map;
231 struct selfid *sid;
232 struct ext_selfid *esid;
233 int i, j, n;
235 for (i = 0; i < (nodecount * 64); i += 64) {
236 for (j = 0; j < nodecount; j++) {
237 map[i+j] = SPEED_400;
241 for (i = 0; i < nodecount; i++) {
242 cldcnt[i] = 0;
245 /* find direct children count and speed */
246 for (sid = (struct selfid *)&host->topology_map[host->selfid_count-1],
247 n = nodecount - 1;
248 (void *)sid >= (void *)host->topology_map; sid--) {
249 if (sid->extended) {
250 esid = (struct ext_selfid *)sid;
252 if (esid->porta == 0x3) cldcnt[n]++;
253 if (esid->portb == 0x3) cldcnt[n]++;
254 if (esid->portc == 0x3) cldcnt[n]++;
255 if (esid->portd == 0x3) cldcnt[n]++;
256 if (esid->porte == 0x3) cldcnt[n]++;
257 if (esid->portf == 0x3) cldcnt[n]++;
258 if (esid->portg == 0x3) cldcnt[n]++;
259 if (esid->porth == 0x3) cldcnt[n]++;
260 } else {
261 if (sid->port0 == 0x3) cldcnt[n]++;
262 if (sid->port1 == 0x3) cldcnt[n]++;
263 if (sid->port2 == 0x3) cldcnt[n]++;
265 speedcap[n] = sid->speed;
266 n--;
270 /* set self mapping */
271 for (i = nodecount - 1; i; i--) {
272 map[64*i + i] = speedcap[i];
275 /* fix up direct children count to total children count;
276 * also fix up speedcaps for sibling and parent communication */
277 for (i = 1; i < nodecount; i++) {
278 for (j = cldcnt[i], n = i - 1; j > 0; j--) {
279 cldcnt[i] += cldcnt[n];
280 speedcap[n] = MIN(speedcap[n], speedcap[i]);
281 n -= cldcnt[n] + 1;
285 for (n = 0; n < nodecount; n++) {
286 for (i = n - cldcnt[n]; i <= n; i++) {
287 for (j = 0; j < (n - cldcnt[n]); j++) {
288 map[j*64 + i] = map[i*64 + j] =
289 MIN(map[i*64 + j], speedcap[n]);
291 for (j = n + 1; j < nodecount; j++) {
292 map[j*64 + i] = map[i*64 + j] =
293 MIN(map[i*64 + j], speedcap[n]);
299 void hpsb_selfid_received(struct hpsb_host *host, quadlet_t sid)
301 if (host->in_bus_reset) {
302 HPSB_DEBUG("including selfid 0x%x", sid);
303 host->topology_map[host->selfid_count++] = sid;
304 } else {
305 /* FIXME - info on which host */
306 HPSB_NOTICE("spurious selfid packet (0x%8.8x) received", sid);
310 void hpsb_selfid_complete(struct hpsb_host *host, int phyid, int isroot)
312 host->node_id = 0xffc0 | phyid;
313 host->in_bus_reset = 0;
314 host->is_root = isroot;
316 host->node_count = check_selfids(host, host->selfid_count);
317 if (!host->node_count) {
318 if (host->reset_retries++ < 20) {
319 /* selfid stage did not complete without error */
320 HPSB_NOTICE("error in SelfID stage - resetting");
321 hpsb_reset_bus(host);
322 return;
323 } else {
324 HPSB_NOTICE("stopping out-of-control reset loop");
325 HPSB_NOTICE("warning - topology map and speed map will "
326 "therefore not be valid");
328 } else {
329 build_speed_map(host, host->node_count);
332 /* irm_id is kept up to date by check_selfids() */
333 if (host->irm_id == host->node_id) {
334 host->is_irm = 1;
335 host->is_busmgr = 1;
336 host->busmgr_id = host->node_id;
337 host->csr.bus_manager_id = host->node_id;
340 host->reset_retries = 0;
341 inc_hpsb_generation();
342 if (isroot) host->template->devctl(host, ACT_CYCLE_MASTER, 1);
343 highlevel_host_reset(host);
347 void hpsb_packet_sent(struct hpsb_host *host, struct hpsb_packet *packet,
348 int ackcode)
350 unsigned long flags;
352 packet->ack_code = ackcode;
354 if (packet->no_waiter) {
355 /* must not have a tlabel allocated */
356 free_hpsb_packet(packet);
357 return;
360 if (ackcode != ACK_PENDING || !packet->expect_response) {
361 packet->state = complete;
362 up(&packet->state_change);
363 up(&packet->state_change);
364 run_task_queue(&packet->complete_tq);
365 return;
368 packet->state = pending;
369 packet->sendtime = jiffies;
371 spin_lock_irqsave(&host->pending_pkt_lock, flags);
372 list_add_tail(&packet->list, &host->pending_packets);
373 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
375 up(&packet->state_change);
376 queue_task(&host->timeout_tq, &tq_timer);
380 * hpsb_send_packet - transmit a packet on the bus
381 * @packet: packet to send
383 * The packet is sent through the host specified in the packet->host field.
384 * Before sending, the packet's transmit speed is automatically determined using
385 * the local speed map when it is an async, non-broadcast packet.
387 * Possibilities for failure are that host is either not initialized, in bus
388 * reset, the packet's generation number doesn't match the current generation
389 * number or the host reports a transmit error.
391 * Return value: False (0) on failure, true (1) otherwise.
393 int hpsb_send_packet(struct hpsb_packet *packet)
395 struct hpsb_host *host = packet->host;
397 if (!host->initialized || host->in_bus_reset
398 || (packet->generation != get_hpsb_generation())) {
399 return 0;
402 packet->state = queued;
404 if (packet->type == async && packet->node_id != ALL_NODES) {
405 packet->speed_code =
406 host->speed_map[(host->node_id & NODE_MASK) * 64
407 + (packet->node_id & NODE_MASK)];
410 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
411 switch (packet->speed_code) {
412 case 2:
413 dump_packet("send packet 400:", packet->header,
414 packet->header_size);
415 break;
416 case 1:
417 dump_packet("send packet 200:", packet->header,
418 packet->header_size);
419 break;
420 default:
421 dump_packet("send packet 100:", packet->header,
422 packet->header_size);
424 #endif
426 return host->template->transmit_packet(host, packet);
429 static void send_packet_nocare(struct hpsb_packet *packet)
431 if (!hpsb_send_packet(packet)) {
432 free_hpsb_packet(packet);
437 void handle_packet_response(struct hpsb_host *host, int tcode, quadlet_t *data,
438 size_t size)
440 struct hpsb_packet *packet = NULL;
441 struct list_head *lh;
442 int tcode_match = 0;
443 int tlabel;
444 unsigned long flags;
446 tlabel = (data[0] >> 10) & 0x3f;
448 spin_lock_irqsave(&host->pending_pkt_lock, flags);
450 lh = host->pending_packets.next;
451 while (lh != &host->pending_packets) {
452 packet = list_entry(lh, struct hpsb_packet, list);
453 if ((packet->tlabel == tlabel)
454 && (packet->node_id == (data[1] >> 16))){
455 break;
457 lh = lh->next;
460 if (lh == &host->pending_packets) {
461 HPSB_INFO("unsolicited response packet received - np");
462 dump_packet("contents:", data, 16);
463 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
464 return;
467 switch (packet->tcode) {
468 case TCODE_WRITEQ:
469 case TCODE_WRITEB:
470 if (tcode == TCODE_WRITE_RESPONSE) tcode_match = 1;
471 break;
472 case TCODE_READQ:
473 if (tcode == TCODE_READQ_RESPONSE) tcode_match = 1;
474 break;
475 case TCODE_READB:
476 if (tcode == TCODE_READB_RESPONSE) tcode_match = 1;
477 break;
478 case TCODE_LOCK_REQUEST:
479 if (tcode == TCODE_LOCK_RESPONSE) tcode_match = 1;
480 break;
483 if (!tcode_match || (packet->tlabel != tlabel)
484 || (packet->node_id != (data[1] >> 16))) {
485 HPSB_INFO("unsolicited response packet received");
486 dump_packet("contents:", data, 16);
488 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
489 return;
492 list_del(&packet->list);
494 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
496 /* FIXME - update size fields? */
497 switch (tcode) {
498 case TCODE_WRITE_RESPONSE:
499 memcpy(packet->header, data, 12);
500 break;
501 case TCODE_READQ_RESPONSE:
502 memcpy(packet->header, data, 16);
503 break;
504 case TCODE_READB_RESPONSE:
505 memcpy(packet->header, data, 16);
506 memcpy(packet->data, data + 4, size - 16);
507 break;
508 case TCODE_LOCK_RESPONSE:
509 memcpy(packet->header, data, 16);
510 memcpy(packet->data, data + 4, (size - 16) > 8 ? 8 : size - 16);
511 break;
514 packet->state = complete;
515 up(&packet->state_change);
516 run_task_queue(&packet->complete_tq);
520 struct hpsb_packet *create_reply_packet(struct hpsb_host *host, quadlet_t *data,
521 size_t dsize)
523 struct hpsb_packet *p;
525 dsize += (dsize % 4 ? 4 - (dsize % 4) : 0);
527 p = alloc_hpsb_packet(dsize);
528 if (p == NULL) {
529 /* FIXME - send data_error response */
530 return NULL;
533 p->type = async;
534 p->state = unused;
535 p->host = host;
536 p->node_id = data[1] >> 16;
537 p->tlabel = (data[0] >> 10) & 0x3f;
538 p->no_waiter = 1;
540 if (dsize % 4) {
541 p->data[dsize / 4] = 0;
544 return p;
547 #define PREP_REPLY_PACKET(length) \
548 packet = create_reply_packet(host, data, length); \
549 if (packet == NULL) break
551 void handle_incoming_packet(struct hpsb_host *host, int tcode, quadlet_t *data,
552 size_t size, int write_acked)
554 struct hpsb_packet *packet;
555 int length, rcode, extcode;
556 int source = data[1] >> 16;
557 u64 addr;
559 /* big FIXME - no error checking is done for an out of bounds length */
561 switch (tcode) {
562 case TCODE_WRITEQ:
563 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
564 rcode = highlevel_write(host, source, data+3, addr, 4);
566 if (!write_acked
567 && ((data[0] >> 16) & NODE_MASK) != NODE_MASK) {
568 /* not a broadcast write, reply */
569 PREP_REPLY_PACKET(0);
570 fill_async_write_resp(packet, rcode);
571 send_packet_nocare(packet);
573 break;
575 case TCODE_WRITEB:
576 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
577 rcode = highlevel_write(host, source, data+4, addr,
578 data[3]>>16);
580 if (!write_acked
581 && ((data[0] >> 16) & NODE_MASK) != NODE_MASK) {
582 /* not a broadcast write, reply */
583 PREP_REPLY_PACKET(0);
584 fill_async_write_resp(packet, rcode);
585 send_packet_nocare(packet);
587 break;
589 case TCODE_READQ:
590 PREP_REPLY_PACKET(0);
592 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
593 rcode = highlevel_read(host, source, data, addr, 4);
594 fill_async_readquad_resp(packet, rcode, *data);
595 send_packet_nocare(packet);
596 break;
598 case TCODE_READB:
599 length = data[3] >> 16;
600 PREP_REPLY_PACKET(length);
602 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
603 rcode = highlevel_read(host, source, packet->data, addr,
604 length);
605 fill_async_readblock_resp(packet, rcode, length);
606 send_packet_nocare(packet);
607 break;
609 case TCODE_LOCK_REQUEST:
610 length = data[3] >> 16;
611 extcode = data[3] & 0xffff;
612 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
614 PREP_REPLY_PACKET(8);
616 if ((extcode == 0) || (extcode >= 7)) {
617 /* let switch default handle error */
618 length = 0;
621 switch (length) {
622 case 4:
623 rcode = highlevel_lock(host, source, packet->data, addr,
624 data[4], 0, extcode);
625 fill_async_lock_resp(packet, rcode, extcode, 4);
626 break;
627 case 8:
628 if ((extcode != EXTCODE_FETCH_ADD)
629 && (extcode != EXTCODE_LITTLE_ADD)) {
630 rcode = highlevel_lock(host, source,
631 packet->data, addr,
632 data[5], data[4],
633 extcode);
634 fill_async_lock_resp(packet, rcode, extcode, 4);
635 } else {
636 rcode = highlevel_lock64(host, source,
637 (octlet_t *)packet->data, addr,
638 *(octlet_t *)(data + 4), 0ULL,
639 extcode);
640 fill_async_lock_resp(packet, rcode, extcode, 8);
642 break;
643 case 16:
644 rcode = highlevel_lock64(host, source,
645 (octlet_t *)packet->data, addr,
646 *(octlet_t *)(data + 6),
647 *(octlet_t *)(data + 4),
648 extcode);
649 fill_async_lock_resp(packet, rcode, extcode, 8);
650 break;
651 default:
652 fill_async_lock_resp(packet, RCODE_TYPE_ERROR,
653 extcode, 0);
656 send_packet_nocare(packet);
657 break;
661 #undef PREP_REPLY_PACKET
664 void hpsb_packet_received(struct hpsb_host *host, quadlet_t *data, size_t size,
665 int write_acked)
667 int tcode;
669 if (host->in_bus_reset) {
670 HPSB_INFO("received packet during reset; ignoring");
671 return;
674 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
675 dump_packet("received packet:", data, size);
676 #endif
678 tcode = (data[0] >> 4) & 0xf;
680 switch (tcode) {
681 case TCODE_WRITE_RESPONSE:
682 case TCODE_READQ_RESPONSE:
683 case TCODE_READB_RESPONSE:
684 case TCODE_LOCK_RESPONSE:
685 handle_packet_response(host, tcode, data, size);
686 break;
688 case TCODE_WRITEQ:
689 case TCODE_WRITEB:
690 case TCODE_READQ:
691 case TCODE_READB:
692 case TCODE_LOCK_REQUEST:
693 handle_incoming_packet(host, tcode, data, size, write_acked);
694 break;
697 case TCODE_ISO_DATA:
698 highlevel_iso_receive(host, data, size);
699 break;
701 case TCODE_CYCLE_START:
702 /* simply ignore this packet if it is passed on */
703 break;
705 default:
706 HPSB_NOTICE("received packet with bogus transaction code %d",
707 tcode);
708 break;
713 void abort_requests(struct hpsb_host *host)
715 unsigned long flags;
716 struct hpsb_packet *packet;
717 struct list_head *lh;
718 LIST_HEAD(llist);
720 host->template->devctl(host, CANCEL_REQUESTS, 0);
722 spin_lock_irqsave(&host->pending_pkt_lock, flags);
723 list_splice(&host->pending_packets, &llist);
724 INIT_LIST_HEAD(&host->pending_packets);
725 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
727 lh = llist.next;
729 while (lh != &llist) {
730 packet = list_entry(lh, struct hpsb_packet, list);
731 lh = lh->next;
732 packet->state = complete;
733 packet->ack_code = ACKX_ABORTED;
734 up(&packet->state_change);
735 run_task_queue(&packet->complete_tq);
739 void abort_timedouts(struct hpsb_host *host)
741 unsigned long flags;
742 struct hpsb_packet *packet;
743 unsigned long expire;
744 struct list_head *lh;
745 LIST_HEAD(expiredlist);
747 spin_lock_irqsave(&host->csr.lock, flags);
748 expire = (host->csr.split_timeout_hi * 8000
749 + (host->csr.split_timeout_lo >> 19))
750 * HZ / 8000;
751 /* Avoid shortening of timeout due to rounding errors: */
752 expire++;
753 spin_unlock_irqrestore(&host->csr.lock, flags);
756 spin_lock_irqsave(&host->pending_pkt_lock, flags);
757 lh = host->pending_packets.next;
759 while (lh != &host->pending_packets) {
760 packet = list_entry(lh, struct hpsb_packet, list);
761 lh = lh->next;
762 if (time_before(packet->sendtime + expire, jiffies)) {
763 list_del(&packet->list);
764 list_add(&packet->list, &expiredlist);
768 if (!list_empty(&host->pending_packets)) {
769 queue_task(&host->timeout_tq, &tq_timer);
771 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
773 lh = expiredlist.next;
774 while (lh != &expiredlist) {
775 packet = list_entry(lh, struct hpsb_packet, list);
776 lh = lh->next;
777 packet->state = complete;
778 packet->ack_code = ACKX_TIMEOUT;
779 up(&packet->state_change);
780 run_task_queue(&packet->complete_tq);
785 #ifndef MODULE
787 void __init ieee1394_init(void)
789 register_builtin_lowlevels();
790 init_hpsb_highlevel();
791 init_csr();
792 init_ieee1394_guid();
795 #else
797 int init_module(void)
799 init_hpsb_highlevel();
800 init_csr();
801 init_ieee1394_guid();
803 return 0;
806 #endif