1 /*
2 * IEEE 1394 for Linux
3 *
4 * Core support: hpsb_packet management, packet handling and forwarding to
5 * highlevel or lowlevel code
6 *
7 * Copyright (C) 1999, 2000 Andreas E. Bombe
8 * 2002 Manfred Weihs <weihs@ict.tuwien.ac.at>
9 *
10 * This code is licensed under the GPL. See the file COPYING in the root
11 * directory of the kernel sources for details.
12 *
13 *
14 * Contributions:
15 *
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)
20 *
21 */
22
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/moduleparam.h>
32 #include <linux/bitops.h>
33 #include <linux/kdev_t.h>
34 #include <linux/skbuff.h>
35 #include <linux/suspend.h>
36
37 #include <asm/byteorder.h>
38 #include <asm/semaphore.h>
39
40 #include "ieee1394_types.h"
41 #include "ieee1394.h"
42 #include "hosts.h"
43 #include "ieee1394_core.h"
44 #include "highlevel.h"
45 #include "ieee1394_transactions.h"
46 #include "csr.h"
47 #include "nodemgr.h"
48 #include "dma.h"
49 #include "iso.h"
50 #include "config_roms.h"
51
52 /*
53 * Disable the nodemgr detection and config rom reading functionality.
54 */
55 static int disable_nodemgr = 0;
56 module_param(disable_nodemgr, int, 0444);
57 MODULE_PARM_DESC(disable_nodemgr, "Disable nodemgr functionality.");
58
59 /* We are GPL, so treat us special */
60 MODULE_LICENSE("GPL");
61
62 /* Some globals used */
63 const char *hpsb_speedto_str[] = { "S100", "S200", "S400", "S800", "S1600", "S3200" };
64
65 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
66 static void dump_packet(const char *text, quadlet_t *data, int size)
67 {
68 int i;
69
70 size /= 4;
71 size = (size > 4 ? 4 : size);
72
73 printk(KERN_DEBUG "ieee1394: %s", text);
74 for (i = 0; i < size; i++)
75 printk(" %08x", data[i]);
76 printk("\n");
77 }
78 #else
79 #define dump_packet(x,y,z)
80 #endif
81
82 static void queue_packet_complete(struct hpsb_packet *packet);
83
84
85 /**
86 * hpsb_set_packet_complete_task - set the task that runs when a packet
87 * completes. You cannot call this more than once on a single packet
88 * before it is sent.
89 *
90 * @packet: the packet whose completion we want the task added to
91 * @routine: function to call
92 * @data: data (if any) to pass to the above function
93 */
94 void hpsb_set_packet_complete_task(struct hpsb_packet *packet,
95 void (*routine)(void *), void *data)
96 {
97 WARN_ON(packet->complete_routine != NULL);
98 packet->complete_routine = routine;
99 packet->complete_data = data;
100 return;
101 }
102
103 /**
104 * hpsb_alloc_packet - allocate new packet structure
105 * @data_size: size of the data block to be allocated
106 *
107 * This function allocates, initializes and returns a new &struct hpsb_packet.
108 * It can be used in interrupt context. A header block is always included, its
109 * size is big enough to contain all possible 1394 headers. The data block is
110 * only allocated when @data_size is not zero.
111 *
112 * For packets for which responses will be received the @data_size has to be big
113 * enough to contain the response's data block since no further allocation
114 * occurs at response matching time.
115 *
116 * The packet's generation value will be set to the current generation number
117 * for ease of use. Remember to overwrite it with your own recorded generation
118 * number if you can not be sure that your code will not race with a bus reset.
119 *
120 * Return value: A pointer to a &struct hpsb_packet or NULL on allocation
121 * failure.
122 */
123 struct hpsb_packet *hpsb_alloc_packet(size_t data_size)
124 {
125 struct hpsb_packet *packet = NULL;
126 struct sk_buff *skb;
127
128 data_size = ((data_size + 3) & ~3);
129
130 skb = alloc_skb(data_size + sizeof(*packet), GFP_ATOMIC);
131 if (skb == NULL)
132 return NULL;
133
134 memset(skb->data, 0, data_size + sizeof(*packet));
135
136 packet = (struct hpsb_packet *)skb->data;
137 packet->skb = skb;
138
139 packet->header = packet->embedded_header;
140 packet->state = hpsb_unused;
141 packet->generation = -1;
142 INIT_LIST_HEAD(&packet->driver_list);
143 atomic_set(&packet->refcnt, 1);
144
145 if (data_size) {
146 packet->data = (quadlet_t *)(skb->data + sizeof(*packet));
147 packet->data_size = data_size;
148 }
149
150 return packet;
151 }
152
153
154 /**
155 * hpsb_free_packet - free packet and data associated with it
156 * @packet: packet to free (is NULL safe)
157 *
158 * This function will free packet->data and finally the packet itself.
159 */
160 void hpsb_free_packet(struct hpsb_packet *packet)
161 {
162 if (packet && atomic_dec_and_test(&packet->refcnt)) {
163 BUG_ON(!list_empty(&packet->driver_list));
164 kfree_skb(packet->skb);
165 }
166 }
167
168
169 int hpsb_reset_bus(struct hpsb_host *host, int type)
170 {
171 if (!host->in_bus_reset) {
172 host->driver->devctl(host, RESET_BUS, type);
173 return 0;
174 } else {
175 return 1;
176 }
177 }
178
179
180 int hpsb_bus_reset(struct hpsb_host *host)
181 {
182 if (host->in_bus_reset) {
183 HPSB_NOTICE("%s called while bus reset already in progress",
184 __FUNCTION__);
185 return 1;
186 }
187
188 abort_requests(host);
189 host->in_bus_reset = 1;
190 host->irm_id = -1;
191 host->is_irm = 0;
192 host->busmgr_id = -1;
193 host->is_busmgr = 0;
194 host->is_cycmst = 0;
195 host->node_count = 0;
196 host->selfid_count = 0;
197
198 return 0;
199 }
200
201
202 /*
203 * Verify num_of_selfids SelfIDs and return number of nodes. Return zero in
204 * case verification failed.
205 */
206 static int check_selfids(struct hpsb_host *host)
207 {
208 int nodeid = -1;
209 int rest_of_selfids = host->selfid_count;
210 struct selfid *sid = (struct selfid *)host->topology_map;
211 struct ext_selfid *esid;
212 int esid_seq = 23;
213
214 host->nodes_active = 0;
215
216 while (rest_of_selfids--) {
217 if (!sid->extended) {
218 nodeid++;
219 esid_seq = 0;
220
221 if (sid->phy_id != nodeid) {
222 HPSB_INFO("SelfIDs failed monotony check with "
223 "%d", sid->phy_id);
224 return 0;
225 }
226
227 if (sid->link_active) {
228 host->nodes_active++;
229 if (sid->contender)
230 host->irm_id = LOCAL_BUS | sid->phy_id;
231 }
232 } else {
233 esid = (struct ext_selfid *)sid;
234
235 if ((esid->phy_id != nodeid)
236 || (esid->seq_nr != esid_seq)) {
237 HPSB_INFO("SelfIDs failed monotony check with "
238 "%d/%d", esid->phy_id, esid->seq_nr);
239 return 0;
240 }
241 esid_seq++;
242 }
243 sid++;
244 }
245
246 esid = (struct ext_selfid *)(sid - 1);
247 while (esid->extended) {
248 if ((esid->porta == 0x2) || (esid->portb == 0x2)
249 || (esid->portc == 0x2) || (esid->portd == 0x2)
250 || (esid->porte == 0x2) || (esid->portf == 0x2)
251 || (esid->portg == 0x2) || (esid->porth == 0x2)) {
252 HPSB_INFO("SelfIDs failed root check on "
253 "extended SelfID");
254 return 0;
255 }
256 esid--;
257 }
258
259 sid = (struct selfid *)esid;
260 if ((sid->port0 == 0x2) || (sid->port1 == 0x2) || (sid->port2 == 0x2)) {
261 HPSB_INFO("SelfIDs failed root check");
262 return 0;
263 }
264
265 host->node_count = nodeid + 1;
266 return 1;
267 }
268
269 static void build_speed_map(struct hpsb_host *host, int nodecount)
270 {
271 u8 speedcap[nodecount];
272 u8 cldcnt[nodecount];
273 u8 *map = host->speed_map;
274 struct selfid *sid;
275 struct ext_selfid *esid;
276 int i, j, n;
277
278 for (i = 0; i < (nodecount * 64); i += 64) {
279 for (j = 0; j < nodecount; j++) {
280 map[i+j] = IEEE1394_SPEED_MAX;
281 }
282 }
283
284 for (i = 0; i < nodecount; i++) {
285 cldcnt[i] = 0;
286 }
287
288 /* find direct children count and speed */
289 for (sid = (struct selfid *)&host->topology_map[host->selfid_count-1],
290 n = nodecount - 1;
291 (void *)sid >= (void *)host->topology_map; sid--) {
292 if (sid->extended) {
293 esid = (struct ext_selfid *)sid;
294
295 if (esid->porta == 0x3) cldcnt[n]++;
296 if (esid->portb == 0x3) cldcnt[n]++;
297 if (esid->portc == 0x3) cldcnt[n]++;
298 if (esid->portd == 0x3) cldcnt[n]++;
299 if (esid->porte == 0x3) cldcnt[n]++;
300 if (esid->portf == 0x3) cldcnt[n]++;
301 if (esid->portg == 0x3) cldcnt[n]++;
302 if (esid->porth == 0x3) cldcnt[n]++;
303 } else {
304 if (sid->port0 == 0x3) cldcnt[n]++;
305 if (sid->port1 == 0x3) cldcnt[n]++;
306 if (sid->port2 == 0x3) cldcnt[n]++;
307
308 speedcap[n] = sid->speed;
309 n--;
310 }
311 }
312
313 /* set self mapping */
314 for (i = 0; i < nodecount; i++) {
315 map[64*i + i] = speedcap[i];
316 }
317
318 /* fix up direct children count to total children count;
319 * also fix up speedcaps for sibling and parent communication */
320 for (i = 1; i < nodecount; i++) {
321 for (j = cldcnt[i], n = i - 1; j > 0; j--) {
322 cldcnt[i] += cldcnt[n];
323 speedcap[n] = min(speedcap[n], speedcap[i]);
324 n -= cldcnt[n] + 1;
325 }
326 }
327
328 for (n = 0; n < nodecount; n++) {
329 for (i = n - cldcnt[n]; i <= n; i++) {
330 for (j = 0; j < (n - cldcnt[n]); j++) {
331 map[j*64 + i] = map[i*64 + j] =
332 min(map[i*64 + j], speedcap[n]);
333 }
334 for (j = n + 1; j < nodecount; j++) {
335 map[j*64 + i] = map[i*64 + j] =
336 min(map[i*64 + j], speedcap[n]);
337 }
338 }
339 }
340 }
341
342
343 void hpsb_selfid_received(struct hpsb_host *host, quadlet_t sid)
344 {
345 if (host->in_bus_reset) {
346 HPSB_VERBOSE("Including SelfID 0x%x", sid);
347 host->topology_map[host->selfid_count++] = sid;
348 } else {
349 HPSB_NOTICE("Spurious SelfID packet (0x%08x) received from bus %d",
350 sid, NODEID_TO_BUS(host->node_id));
351 }
352 }
353
354 void hpsb_selfid_complete(struct hpsb_host *host, int phyid, int isroot)
355 {
356 if (!host->in_bus_reset)
357 HPSB_NOTICE("SelfID completion called outside of bus reset!");
358
359 host->node_id = LOCAL_BUS | phyid;
360 host->is_root = isroot;
361
362 if (!check_selfids(host)) {
363 if (host->reset_retries++ < 20) {
364 /* selfid stage did not complete without error */
365 HPSB_NOTICE("Error in SelfID stage, resetting");
366 host->in_bus_reset = 0;
367 /* this should work from ohci1394 now... */
368 hpsb_reset_bus(host, LONG_RESET);
369 return;
370 } else {
371 HPSB_NOTICE("Stopping out-of-control reset loop");
372 HPSB_NOTICE("Warning - topology map and speed map will not be valid");
373 host->reset_retries = 0;
374 }
375 } else {
376 host->reset_retries = 0;
377 build_speed_map(host, host->node_count);
378 }
379
380 HPSB_VERBOSE("selfid_complete called with successful SelfID stage "
381 "... irm_id: 0x%X node_id: 0x%X",host->irm_id,host->node_id);
382
383 /* irm_id is kept up to date by check_selfids() */
384 if (host->irm_id == host->node_id) {
385 host->is_irm = 1;
386 } else {
387 host->is_busmgr = 0;
388 host->is_irm = 0;
389 }
390
391 if (isroot) {
392 host->driver->devctl(host, ACT_CYCLE_MASTER, 1);
393 host->is_cycmst = 1;
394 }
395 atomic_inc(&host->generation);
396 host->in_bus_reset = 0;
397 highlevel_host_reset(host);
398 }
399
400
401 void hpsb_packet_sent(struct hpsb_host *host, struct hpsb_packet *packet,
402 int ackcode)
403 {
404 unsigned long flags;
405
406 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
407
408 packet->ack_code = ackcode;
409
410 if (packet->no_waiter || packet->state == hpsb_complete) {
411 /* if packet->no_waiter, must not have a tlabel allocated */
412 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
413 hpsb_free_packet(packet);
414 return;
415 }
416
417 atomic_dec(&packet->refcnt); /* drop HC's reference */
418 /* here the packet must be on the host->pending_packet_queue */
419
420 if (ackcode != ACK_PENDING || !packet->expect_response) {
421 packet->state = hpsb_complete;
422 __skb_unlink(packet->skb, &host->pending_packet_queue);
423 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
424 queue_packet_complete(packet);
425 return;
426 }
427
428 packet->state = hpsb_pending;
429 packet->sendtime = jiffies;
430
431 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
432
433 mod_timer(&host->timeout, jiffies + host->timeout_interval);
434 }
435
436 /**
437 * hpsb_send_phy_config - transmit a PHY configuration packet on the bus
438 * @host: host that PHY config packet gets sent through
439 * @rootid: root whose force_root bit should get set (-1 = don't set force_root)
440 * @gapcnt: gap count value to set (-1 = don't set gap count)
441 *
442 * This function sends a PHY config packet on the bus through the specified host.
443 *
444 * Return value: 0 for success or error number otherwise.
445 */
446 int hpsb_send_phy_config(struct hpsb_host *host, int rootid, int gapcnt)
447 {
448 struct hpsb_packet *packet;
449 int retval = 0;
450
451 if (rootid >= ALL_NODES || rootid < -1 || gapcnt > 0x3f || gapcnt < -1 ||
452 (rootid == -1 && gapcnt == -1)) {
453 HPSB_DEBUG("Invalid Parameter: rootid = %d gapcnt = %d",
454 rootid, gapcnt);
455 return -EINVAL;
456 }
457
458 packet = hpsb_alloc_packet(0);
459 if (!packet)
460 return -ENOMEM;
461
462 packet->host = host;
463 packet->header_size = 8;
464 packet->data_size = 0;
465 packet->expect_response = 0;
466 packet->no_waiter = 0;
467 packet->type = hpsb_raw;
468 packet->header[0] = 0;
469 if (rootid != -1)
470 packet->header[0] |= rootid << 24 | 1 << 23;
471 if (gapcnt != -1)
472 packet->header[0] |= gapcnt << 16 | 1 << 22;
473
474 packet->header[1] = ~packet->header[0];
475
476 packet->generation = get_hpsb_generation(host);
477
478 retval = hpsb_send_packet_and_wait(packet);
479 hpsb_free_packet(packet);
480
481 return retval;
482 }
483
484 /**
485 * hpsb_send_packet - transmit a packet on the bus
486 * @packet: packet to send
487 *
488 * The packet is sent through the host specified in the packet->host field.
489 * Before sending, the packet's transmit speed is automatically determined
490 * using the local speed map when it is an async, non-broadcast packet.
491 *
492 * Possibilities for failure are that host is either not initialized, in bus
493 * reset, the packet's generation number doesn't match the current generation
494 * number or the host reports a transmit error.
495 *
496 * Return value: 0 on success, negative errno on failure.
497 */
498 int hpsb_send_packet(struct hpsb_packet *packet)
499 {
500 struct hpsb_host *host = packet->host;
501
502 if (host->is_shutdown)
503 return -EINVAL;
504 if (host->in_bus_reset ||
505 (packet->generation != get_hpsb_generation(host)))
506 return -EAGAIN;
507
508 packet->state = hpsb_queued;
509
510 /* This just seems silly to me */
511 WARN_ON(packet->no_waiter && packet->expect_response);
512
513 if (!packet->no_waiter || packet->expect_response) {
514 atomic_inc(&packet->refcnt);
515 skb_queue_tail(&host->pending_packet_queue, packet->skb);
516 }
517
518 if (packet->node_id == host->node_id) {
519 /* it is a local request, so handle it locally */
520
521 quadlet_t *data;
522 size_t size = packet->data_size + packet->header_size;
523
524 data = kmalloc(size, GFP_ATOMIC);
525 if (!data) {
526 HPSB_ERR("unable to allocate memory for concatenating header and data");
527 return -ENOMEM;
528 }
529
530 memcpy(data, packet->header, packet->header_size);
531
532 if (packet->data_size)
533 memcpy(((u8*)data) + packet->header_size, packet->data, packet->data_size);
534
535 dump_packet("send packet local:", packet->header,
536 packet->header_size);
537
538 hpsb_packet_sent(host, packet, packet->expect_response ? ACK_PENDING : ACK_COMPLETE);
539 hpsb_packet_received(host, data, size, 0);
540
541 kfree(data);
542
543 return 0;
544 }
545
546 if (packet->type == hpsb_async && packet->node_id != ALL_NODES) {
547 packet->speed_code =
548 host->speed_map[NODEID_TO_NODE(host->node_id) * 64
549 + NODEID_TO_NODE(packet->node_id)];
550 }
551
552 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
553 switch (packet->speed_code) {
554 case 2:
555 dump_packet("send packet 400:", packet->header,
556 packet->header_size);
557 break;
558 case 1:
559 dump_packet("send packet 200:", packet->header,
560 packet->header_size);
561 break;
562 default:
563 dump_packet("send packet 100:", packet->header,
564 packet->header_size);
565 }
566 #endif
567
568 return host->driver->transmit_packet(host, packet);
569 }
570
571 /* We could just use complete() directly as the packet complete
572 * callback, but this is more typesafe, in the sense that we get a
573 * compiler error if the prototype for complete() changes. */
574
575 static void complete_packet(void *data)
576 {
577 complete((struct completion *) data);
578 }
579
580 int hpsb_send_packet_and_wait(struct hpsb_packet *packet)
581 {
582 struct completion done;
583 int retval;
584
585 init_completion(&done);
586 hpsb_set_packet_complete_task(packet, complete_packet, &done);
587 retval = hpsb_send_packet(packet);
588 if (retval == 0)
589 wait_for_completion(&done);
590
591 return retval;
592 }
593
594 static void send_packet_nocare(struct hpsb_packet *packet)
595 {
596 if (hpsb_send_packet(packet) < 0) {
597 hpsb_free_packet(packet);
598 }
599 }
600
601
602 static void handle_packet_response(struct hpsb_host *host, int tcode,
603 quadlet_t *data, size_t size)
604 {
605 struct hpsb_packet *packet = NULL;
606 struct sk_buff *skb;
607 int tcode_match = 0;
608 int tlabel;
609 unsigned long flags;
610
611 tlabel = (data[0] >> 10) & 0x3f;
612
613 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
614
615 skb_queue_walk(&host->pending_packet_queue, skb) {
616 packet = (struct hpsb_packet *)skb->data;
617 if ((packet->tlabel == tlabel)
618 && (packet->node_id == (data[1] >> 16))){
619 break;
620 }
621
622 packet = NULL;
623 }
624
625 if (packet == NULL) {
626 HPSB_DEBUG("unsolicited response packet received - no tlabel match");
627 dump_packet("contents:", data, 16);
628 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
629 return;
630 }
631
632 switch (packet->tcode) {
633 case TCODE_WRITEQ:
634 case TCODE_WRITEB:
635 if (tcode != TCODE_WRITE_RESPONSE)
636 break;
637 tcode_match = 1;
638 memcpy(packet->header, data, 12);
639 break;
640 case TCODE_READQ:
641 if (tcode != TCODE_READQ_RESPONSE)
642 break;
643 tcode_match = 1;
644 memcpy(packet->header, data, 16);
645 break;
646 case TCODE_READB:
647 if (tcode != TCODE_READB_RESPONSE)
648 break;
649 tcode_match = 1;
650 BUG_ON(packet->skb->len - sizeof(*packet) < size - 16);
651 memcpy(packet->header, data, 16);
652 memcpy(packet->data, data + 4, size - 16);
653 break;
654 case TCODE_LOCK_REQUEST:
655 if (tcode != TCODE_LOCK_RESPONSE)
656 break;
657 tcode_match = 1;
658 size = min((size - 16), (size_t)8);
659 BUG_ON(packet->skb->len - sizeof(*packet) < size);
660 memcpy(packet->header, data, 16);
661 memcpy(packet->data, data + 4, size);
662 break;
663 }
664
665 if (!tcode_match) {
666 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
667 HPSB_INFO("unsolicited response packet received - tcode mismatch");
668 dump_packet("contents:", data, 16);
669 return;
670 }
671
672 __skb_unlink(skb, skb->list);
673
674 if (packet->state == hpsb_queued) {
675 packet->sendtime = jiffies;
676 packet->ack_code = ACK_PENDING;
677 }
678
679 packet->state = hpsb_complete;
680 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
681
682 queue_packet_complete(packet);
683 }
684
685
686 static struct hpsb_packet *create_reply_packet(struct hpsb_host *host,
687 quadlet_t *data, size_t dsize)
688 {
689 struct hpsb_packet *p;
690
691 p = hpsb_alloc_packet(dsize);
692 if (unlikely(p == NULL)) {
693 /* FIXME - send data_error response */
694 return NULL;
695 }
696
697 p->type = hpsb_async;
698 p->state = hpsb_unused;
699 p->host = host;
700 p->node_id = data[1] >> 16;
701 p->tlabel = (data[0] >> 10) & 0x3f;
702 p->no_waiter = 1;
703
704 p->generation = get_hpsb_generation(host);
705
706 if (dsize % 4)
707 p->data[dsize / 4] = 0;
708
709 return p;
710 }
711
712 #define PREP_ASYNC_HEAD_RCODE(tc) \
713 packet->tcode = tc; \
714 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
715 | (1 << 8) | (tc << 4); \
716 packet->header[1] = (packet->host->node_id << 16) | (rcode << 12); \
717 packet->header[2] = 0
718
719 static void fill_async_readquad_resp(struct hpsb_packet *packet, int rcode,
720 quadlet_t data)
721 {
722 PREP_ASYNC_HEAD_RCODE(TCODE_READQ_RESPONSE);
723 packet->header[3] = data;
724 packet->header_size = 16;
725 packet->data_size = 0;
726 }
727
728 static void fill_async_readblock_resp(struct hpsb_packet *packet, int rcode,
729 int length)
730 {
731 if (rcode != RCODE_COMPLETE)
732 length = 0;
733
734 PREP_ASYNC_HEAD_RCODE(TCODE_READB_RESPONSE);
735 packet->header[3] = length << 16;
736 packet->header_size = 16;
737 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
738 }
739
740 static void fill_async_write_resp(struct hpsb_packet *packet, int rcode)
741 {
742 PREP_ASYNC_HEAD_RCODE(TCODE_WRITE_RESPONSE);
743 packet->header[2] = 0;
744 packet->header_size = 12;
745 packet->data_size = 0;
746 }
747
748 static void fill_async_lock_resp(struct hpsb_packet *packet, int rcode, int extcode,
749 int length)
750 {
751 if (rcode != RCODE_COMPLETE)
752 length = 0;
753
754 PREP_ASYNC_HEAD_RCODE(TCODE_LOCK_RESPONSE);
755 packet->header[3] = (length << 16) | extcode;
756 packet->header_size = 16;
757 packet->data_size = length;
758 }
759
760 #define PREP_REPLY_PACKET(length) \
761 packet = create_reply_packet(host, data, length); \
762 if (packet == NULL) break
763
764 static void handle_incoming_packet(struct hpsb_host *host, int tcode,
765 quadlet_t *data, size_t size, int write_acked)
766 {
767 struct hpsb_packet *packet;
768 int length, rcode, extcode;
769 quadlet_t buffer;
770 nodeid_t source = data[1] >> 16;
771 nodeid_t dest = data[0] >> 16;
772 u16 flags = (u16) data[0];
773 u64 addr;
774
775 /* big FIXME - no error checking is done for an out of bounds length */
776
777 switch (tcode) {
778 case TCODE_WRITEQ:
779 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
780 rcode = highlevel_write(host, source, dest, data+3,
781 addr, 4, flags);
782
783 if (!write_acked
784 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
785 && (rcode >= 0)) {
786 /* not a broadcast write, reply */
787 PREP_REPLY_PACKET(0);
788 fill_async_write_resp(packet, rcode);
789 send_packet_nocare(packet);
790 }
791 break;
792
793 case TCODE_WRITEB:
794 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
795 rcode = highlevel_write(host, source, dest, data+4,
796 addr, data[3]>>16, flags);
797
798 if (!write_acked
799 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
800 && (rcode >= 0)) {
801 /* not a broadcast write, reply */
802 PREP_REPLY_PACKET(0);
803 fill_async_write_resp(packet, rcode);
804 send_packet_nocare(packet);
805 }
806 break;
807
808 case TCODE_READQ:
809 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
810 rcode = highlevel_read(host, source, &buffer, addr, 4, flags);
811
812 if (rcode >= 0) {
813 PREP_REPLY_PACKET(0);
814 fill_async_readquad_resp(packet, rcode, buffer);
815 send_packet_nocare(packet);
816 }
817 break;
818
819 case TCODE_READB:
820 length = data[3] >> 16;
821 PREP_REPLY_PACKET(length);
822
823 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
824 rcode = highlevel_read(host, source, packet->data, addr,
825 length, flags);
826
827 if (rcode >= 0) {
828 fill_async_readblock_resp(packet, rcode, length);
829 send_packet_nocare(packet);
830 } else {
831 hpsb_free_packet(packet);
832 }
833 break;
834
835 case TCODE_LOCK_REQUEST:
836 length = data[3] >> 16;
837 extcode = data[3] & 0xffff;
838 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
839
840 PREP_REPLY_PACKET(8);
841
842 if ((extcode == 0) || (extcode >= 7)) {
843 /* let switch default handle error */
844 length = 0;
845 }
846
847 switch (length) {
848 case 4:
849 rcode = highlevel_lock(host, source, packet->data, addr,
850 data[4], 0, extcode,flags);
851 fill_async_lock_resp(packet, rcode, extcode, 4);
852 break;
853 case 8:
854 if ((extcode != EXTCODE_FETCH_ADD)
855 && (extcode != EXTCODE_LITTLE_ADD)) {
856 rcode = highlevel_lock(host, source,
857 packet->data, addr,
858 data[5], data[4],
859 extcode, flags);
860 fill_async_lock_resp(packet, rcode, extcode, 4);
861 } else {
862 rcode = highlevel_lock64(host, source,
863 (octlet_t *)packet->data, addr,
864 *(octlet_t *)(data + 4), 0ULL,
865 extcode, flags);
866 fill_async_lock_resp(packet, rcode, extcode, 8);
867 }
868 break;
869 case 16:
870 rcode = highlevel_lock64(host, source,
871 (octlet_t *)packet->data, addr,
872 *(octlet_t *)(data + 6),
873 *(octlet_t *)(data + 4),
874 extcode, flags);
875 fill_async_lock_resp(packet, rcode, extcode, 8);
876 break;
877 default:
878 rcode = RCODE_TYPE_ERROR;
879 fill_async_lock_resp(packet, rcode,
880 extcode, 0);
881 }
882
883 if (rcode >= 0) {
884 send_packet_nocare(packet);
885 } else {
886 hpsb_free_packet(packet);
887 }
888 break;
889 }
890
891 }
892 #undef PREP_REPLY_PACKET
893
894
895 void hpsb_packet_received(struct hpsb_host *host, quadlet_t *data, size_t size,
896 int write_acked)
897 {
898 int tcode;
899
900 if (host->in_bus_reset) {
901 HPSB_INFO("received packet during reset; ignoring");
902 return;
903 }
904
905 dump_packet("received packet:", data, size);
906
907 tcode = (data[0] >> 4) & 0xf;
908
909 switch (tcode) {
910 case TCODE_WRITE_RESPONSE:
911 case TCODE_READQ_RESPONSE:
912 case TCODE_READB_RESPONSE:
913 case TCODE_LOCK_RESPONSE:
914 handle_packet_response(host, tcode, data, size);
915 break;
916
917 case TCODE_WRITEQ:
918 case TCODE_WRITEB:
919 case TCODE_READQ:
920 case TCODE_READB:
921 case TCODE_LOCK_REQUEST:
922 handle_incoming_packet(host, tcode, data, size, write_acked);
923 break;
924
925
926 case TCODE_ISO_DATA:
927 highlevel_iso_receive(host, data, size);
928 break;
929
930 case TCODE_CYCLE_START:
931 /* simply ignore this packet if it is passed on */
932 break;
933
934 default:
935 HPSB_NOTICE("received packet with bogus transaction code %d",
936 tcode);
937 break;
938 }
939 }
940
941
942 void abort_requests(struct hpsb_host *host)
943 {
944 struct hpsb_packet *packet;
945 struct sk_buff *skb;
946
947 host->driver->devctl(host, CANCEL_REQUESTS, 0);
948
949 while ((skb = skb_dequeue(&host->pending_packet_queue)) != NULL) {
950 packet = (struct hpsb_packet *)skb->data;
951
952 packet->state = hpsb_complete;
953 packet->ack_code = ACKX_ABORTED;
954 queue_packet_complete(packet);
955 }
956 }
957
958 void abort_timedouts(unsigned long __opaque)
959 {
960 struct hpsb_host *host = (struct hpsb_host *)__opaque;
961 unsigned long flags;
962 struct hpsb_packet *packet;
963 struct sk_buff *skb;
964 unsigned long expire;
965
966 spin_lock_irqsave(&host->csr.lock, flags);
967 expire = host->csr.expire;
968 spin_unlock_irqrestore(&host->csr.lock, flags);
969
970 /* Hold the lock around this, since we aren't dequeuing all
971 * packets, just ones we need. */
972 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
973
974 while (!skb_queue_empty(&host->pending_packet_queue)) {
975 skb = skb_peek(&host->pending_packet_queue);
976
977 packet = (struct hpsb_packet *)skb->data;
978
979 if (time_before(packet->sendtime + expire, jiffies)) {
980 __skb_unlink(skb, skb->list);
981 packet->state = hpsb_complete;
982 packet->ack_code = ACKX_TIMEOUT;
983 queue_packet_complete(packet);
984 } else {
985 /* Since packets are added to the tail, the oldest
986 * ones are first, always. When we get to one that
987 * isn't timed out, the rest aren't either. */
988 break;
989 }
990 }
991
992 if (!skb_queue_empty(&host->pending_packet_queue))
993 mod_timer(&host->timeout, jiffies + host->timeout_interval);
994
995 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
996 }
997
998
999 /* Kernel thread and vars, which handles packets that are completed. Only
1000 * packets that have a "complete" function are sent here. This way, the
1001 * completion is run out of kernel context, and doesn't block the rest of
1002 * the stack. */
1003 static int khpsbpkt_pid = -1, khpsbpkt_kill;
1004 static DECLARE_COMPLETION(khpsbpkt_complete);
1005 struct sk_buff_head hpsbpkt_queue;
1006 static DECLARE_MUTEX_LOCKED(khpsbpkt_sig);
1007
1008
1009 static void queue_packet_complete(struct hpsb_packet *packet)
1010 {
1011 if (packet->no_waiter) {
1012 hpsb_free_packet(packet);
1013 return;
1014 }
1015 if (packet->complete_routine != NULL) {
1016 skb_queue_tail(&hpsbpkt_queue, packet->skb);
1017
1018 /* Signal the kernel thread to handle this */
1019 up(&khpsbpkt_sig);
1020 }
1021 return;
1022 }
1023
1024 static int hpsbpkt_thread(void *__hi)
1025 {
1026 struct sk_buff *skb;
1027 struct hpsb_packet *packet;
1028 void (*complete_routine)(void*);
1029 void *complete_data;
1030
1031 daemonize("khpsbpkt");
1032
1033 while (!down_interruptible(&khpsbpkt_sig)) {
1034 if (khpsbpkt_kill)
1035 break;
1036
1037 if (current->flags & PF_FREEZE) {
1038 refrigerator(0);
1039 continue;
1040 }
1041
1042 while ((skb = skb_dequeue(&hpsbpkt_queue)) != NULL) {
1043 packet = (struct hpsb_packet *)skb->data;
1044
1045 complete_routine = packet->complete_routine;
1046 complete_data = packet->complete_data;
1047
1048 packet->complete_routine = packet->complete_data = NULL;
1049
1050 complete_routine(complete_data);
1051 }
1052 }
1053
1054 complete_and_exit(&khpsbpkt_complete, 0);
1055 }
1056
1057
1058 static int __init ieee1394_init(void)
1059 {
1060 int i, ret;
1061
1062 skb_queue_head_init(&hpsbpkt_queue);
1063
1064 /* non-fatal error */
1065 if (hpsb_init_config_roms()) {
1066 HPSB_ERR("Failed to initialize some config rom entries.\n");
1067 HPSB_ERR("Some features may not be available\n");
1068 }
1069
1070 khpsbpkt_pid = kernel_thread(hpsbpkt_thread, NULL, CLONE_KERNEL);
1071 if (khpsbpkt_pid < 0) {
1072 HPSB_ERR("Failed to start hpsbpkt thread!\n");
1073 ret = -ENOMEM;
1074 goto exit_cleanup_config_roms;
1075 }
1076
1077 if (register_chrdev_region(IEEE1394_CORE_DEV, 256, "ieee1394")) {
1078 HPSB_ERR("unable to register character device major %d!\n", IEEE1394_MAJOR);
1079 ret = -ENODEV;
1080 goto exit_release_kernel_thread;
1081 }
1082
1083 /* actually this is a non-fatal error */
1084 ret = devfs_mk_dir("ieee1394");
1085 if (ret < 0) {
1086 HPSB_ERR("unable to make devfs dir for device major %d!\n", IEEE1394_MAJOR);
1087 goto release_chrdev;
1088 }
1089
1090 ret = bus_register(&ieee1394_bus_type);
1091 if (ret < 0) {
1092 HPSB_INFO("bus register failed");
1093 goto release_devfs;
1094 }
1095
1096 for (i = 0; fw_bus_attrs[i]; i++) {
1097 ret = bus_create_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1098 if (ret < 0) {
1099 while (i >= 0) {
1100 bus_remove_file(&ieee1394_bus_type,
1101 fw_bus_attrs[i--]);
1102 }
1103 bus_unregister(&ieee1394_bus_type);
1104 goto release_devfs;
1105 }
1106 }
1107
1108 ret = class_register(&hpsb_host_class);
1109 if (ret < 0)
1110 goto release_all_bus;
1111
1112 ret = init_csr();
1113 if (ret) {
1114 HPSB_INFO("init csr failed");
1115 ret = -ENOMEM;
1116 goto release_class;
1117 }
1118
1119 if (disable_nodemgr) {
1120 HPSB_INFO("nodemgr functionality disabled");
1121 return 0;
1122 }
1123
1124 ret = init_ieee1394_nodemgr();
1125 if (ret < 0) {
1126 HPSB_INFO("init nodemgr failed");
1127 goto cleanup_csr;
1128 }
1129
1130 return 0;
1131
1132 cleanup_csr:
1133 cleanup_csr();
1134 release_class:
1135 class_unregister(&hpsb_host_class);
1136 release_all_bus:
1137 for (i = 0; fw_bus_attrs[i]; i++)
1138 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1139 bus_unregister(&ieee1394_bus_type);
1140 release_devfs:
1141 devfs_remove("ieee1394");
1142 release_chrdev:
1143 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
1144 exit_release_kernel_thread:
1145 if (khpsbpkt_pid >= 0) {
1146 kill_proc(khpsbpkt_pid, SIGTERM, 1);
1147 wait_for_completion(&khpsbpkt_complete);
1148 }
1149 exit_cleanup_config_roms:
1150 hpsb_cleanup_config_roms();
1151 return ret;
1152 }
1153
1154 static void __exit ieee1394_cleanup(void)
1155 {
1156 int i;
1157
1158 if (!disable_nodemgr)
1159 cleanup_ieee1394_nodemgr();
1160
1161 cleanup_csr();
1162
1163 class_unregister(&hpsb_host_class);
1164 for (i = 0; fw_bus_attrs[i]; i++)
1165 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1166 bus_unregister(&ieee1394_bus_type);
1167
1168 if (khpsbpkt_pid >= 0) {
1169 khpsbpkt_kill = 1;
1170 mb();
1171 up(&khpsbpkt_sig);
1172 wait_for_completion(&khpsbpkt_complete);
1173 }
1174
1175 hpsb_cleanup_config_roms();
1176
1177 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
1178 devfs_remove("ieee1394");
1179 }
1180
1181 module_init(ieee1394_init);
1182 module_exit(ieee1394_cleanup);
1183
1184 /* Exported symbols */
1185
1186 /** hosts.c **/
1187 EXPORT_SYMBOL(hpsb_alloc_host);
1188 EXPORT_SYMBOL(hpsb_add_host);
1189 EXPORT_SYMBOL(hpsb_remove_host);
1190 EXPORT_SYMBOL(hpsb_update_config_rom_image);
1191
1192 /** ieee1394_core.c **/
1193 EXPORT_SYMBOL(hpsb_speedto_str);
1194 EXPORT_SYMBOL(hpsb_set_packet_complete_task);
1195 EXPORT_SYMBOL(hpsb_alloc_packet);
1196 EXPORT_SYMBOL(hpsb_free_packet);
1197 EXPORT_SYMBOL(hpsb_send_phy_config);
1198 EXPORT_SYMBOL(hpsb_send_packet);
1199 EXPORT_SYMBOL(hpsb_send_packet_and_wait);
1200 EXPORT_SYMBOL(hpsb_reset_bus);
1201 EXPORT_SYMBOL(hpsb_bus_reset);
1202 EXPORT_SYMBOL(hpsb_selfid_received);
1203 EXPORT_SYMBOL(hpsb_selfid_complete);
1204 EXPORT_SYMBOL(hpsb_packet_sent);
1205 EXPORT_SYMBOL(hpsb_packet_received);
1206
1207 /** ieee1394_transactions.c **/
1208 EXPORT_SYMBOL(hpsb_get_tlabel);
1209 EXPORT_SYMBOL(hpsb_free_tlabel);
1210 EXPORT_SYMBOL(hpsb_make_readpacket);
1211 EXPORT_SYMBOL(hpsb_make_writepacket);
1212 EXPORT_SYMBOL(hpsb_make_streampacket);
1213 EXPORT_SYMBOL(hpsb_make_lockpacket);
1214 EXPORT_SYMBOL(hpsb_make_lock64packet);
1215 EXPORT_SYMBOL(hpsb_make_phypacket);
1216 EXPORT_SYMBOL(hpsb_make_isopacket);
1217 EXPORT_SYMBOL(hpsb_read);
1218 EXPORT_SYMBOL(hpsb_write);
1219 EXPORT_SYMBOL(hpsb_lock);
1220 EXPORT_SYMBOL(hpsb_lock64);
1221 EXPORT_SYMBOL(hpsb_send_gasp);
1222 EXPORT_SYMBOL(hpsb_packet_success);
1223
1224 /** highlevel.c **/
1225 EXPORT_SYMBOL(hpsb_register_highlevel);
1226 EXPORT_SYMBOL(hpsb_unregister_highlevel);
1227 EXPORT_SYMBOL(hpsb_register_addrspace);
1228 EXPORT_SYMBOL(hpsb_unregister_addrspace);
1229 EXPORT_SYMBOL(hpsb_allocate_and_register_addrspace);
1230 EXPORT_SYMBOL(hpsb_listen_channel);
1231 EXPORT_SYMBOL(hpsb_unlisten_channel);
1232 EXPORT_SYMBOL(hpsb_get_hostinfo);
1233 EXPORT_SYMBOL(hpsb_get_host_bykey);
1234 EXPORT_SYMBOL(hpsb_create_hostinfo);
1235 EXPORT_SYMBOL(hpsb_destroy_hostinfo);
1236 EXPORT_SYMBOL(hpsb_set_hostinfo_key);
1237 EXPORT_SYMBOL(hpsb_get_hostinfo_key);
1238 EXPORT_SYMBOL(hpsb_get_hostinfo_bykey);
1239 EXPORT_SYMBOL(hpsb_set_hostinfo);
1240 EXPORT_SYMBOL(highlevel_read);
1241 EXPORT_SYMBOL(highlevel_write);
1242 EXPORT_SYMBOL(highlevel_lock);
1243 EXPORT_SYMBOL(highlevel_lock64);
1244 EXPORT_SYMBOL(highlevel_add_host);
1245 EXPORT_SYMBOL(highlevel_remove_host);
1246 EXPORT_SYMBOL(highlevel_host_reset);
1247
1248 /** nodemgr.c **/
1249 EXPORT_SYMBOL(hpsb_guid_get_entry);
1250 EXPORT_SYMBOL(hpsb_nodeid_get_entry);
1251 EXPORT_SYMBOL(hpsb_node_fill_packet);
1252 EXPORT_SYMBOL(hpsb_node_read);
1253 EXPORT_SYMBOL(hpsb_node_write);
1254 EXPORT_SYMBOL(hpsb_node_lock);
1255 EXPORT_SYMBOL(hpsb_register_protocol);
1256 EXPORT_SYMBOL(hpsb_unregister_protocol);
1257 EXPORT_SYMBOL(ieee1394_bus_type);
1258 EXPORT_SYMBOL(nodemgr_for_each_host);
1259
1260 /** csr.c **/
1261 EXPORT_SYMBOL(hpsb_update_config_rom);
1262
1263 /** dma.c **/
1264 EXPORT_SYMBOL(dma_prog_region_init);
1265 EXPORT_SYMBOL(dma_prog_region_alloc);
1266 EXPORT_SYMBOL(dma_prog_region_free);
1267 EXPORT_SYMBOL(dma_region_init);
1268 EXPORT_SYMBOL(dma_region_alloc);
1269 EXPORT_SYMBOL(dma_region_free);
1270 EXPORT_SYMBOL(dma_region_sync_for_cpu);
1271 EXPORT_SYMBOL(dma_region_sync_for_device);
1272 EXPORT_SYMBOL(dma_region_mmap);
1273 EXPORT_SYMBOL(dma_region_offset_to_bus);
1274
1275 /** iso.c **/
1276 EXPORT_SYMBOL(hpsb_iso_xmit_init);
1277 EXPORT_SYMBOL(hpsb_iso_recv_init);
1278 EXPORT_SYMBOL(hpsb_iso_xmit_start);
1279 EXPORT_SYMBOL(hpsb_iso_recv_start);
1280 EXPORT_SYMBOL(hpsb_iso_recv_listen_channel);
1281 EXPORT_SYMBOL(hpsb_iso_recv_unlisten_channel);
1282 EXPORT_SYMBOL(hpsb_iso_recv_set_channel_mask);
1283 EXPORT_SYMBOL(hpsb_iso_stop);
1284 EXPORT_SYMBOL(hpsb_iso_shutdown);
1285 EXPORT_SYMBOL(hpsb_iso_xmit_queue_packet);
1286 EXPORT_SYMBOL(hpsb_iso_xmit_sync);
1287 EXPORT_SYMBOL(hpsb_iso_recv_release_packets);
1288 EXPORT_SYMBOL(hpsb_iso_n_ready);
1289 EXPORT_SYMBOL(hpsb_iso_packet_sent);
1290 EXPORT_SYMBOL(hpsb_iso_packet_received);
1291 EXPORT_SYMBOL(hpsb_iso_wake);
1292 EXPORT_SYMBOL(hpsb_iso_recv_flush);
1293
1294 /** csr1212.c **/
1295 EXPORT_SYMBOL(csr1212_create_csr);
1296 EXPORT_SYMBOL(csr1212_init_local_csr);
1297 EXPORT_SYMBOL(csr1212_new_immediate);
1298 EXPORT_SYMBOL(csr1212_new_leaf);
1299 EXPORT_SYMBOL(csr1212_new_csr_offset);
1300 EXPORT_SYMBOL(csr1212_new_directory);
1301 EXPORT_SYMBOL(csr1212_associate_keyval);
1302 EXPORT_SYMBOL(csr1212_attach_keyval_to_directory);
1303 EXPORT_SYMBOL(csr1212_new_extended_immediate);
1304 EXPORT_SYMBOL(csr1212_new_extended_leaf);
1305 EXPORT_SYMBOL(csr1212_new_descriptor_leaf);
1306 EXPORT_SYMBOL(csr1212_new_textual_descriptor_leaf);
1307 EXPORT_SYMBOL(csr1212_new_string_descriptor_leaf);
1308 EXPORT_SYMBOL(csr1212_new_icon_descriptor_leaf);
1309 EXPORT_SYMBOL(csr1212_new_modifiable_descriptor_leaf);
1310 EXPORT_SYMBOL(csr1212_new_keyword_leaf);
1311 EXPORT_SYMBOL(csr1212_detach_keyval_from_directory);
1312 EXPORT_SYMBOL(csr1212_disassociate_keyval);
1313 EXPORT_SYMBOL(csr1212_release_keyval);
1314 EXPORT_SYMBOL(csr1212_destroy_csr);
1315 EXPORT_SYMBOL(csr1212_read);
1316 EXPORT_SYMBOL(csr1212_generate_positions);
1317 EXPORT_SYMBOL(csr1212_generate_layout_order);
1318 EXPORT_SYMBOL(csr1212_fill_cache);
1319 EXPORT_SYMBOL(csr1212_generate_csr_image);
1320 EXPORT_SYMBOL(csr1212_parse_keyval);
1321 EXPORT_SYMBOL(csr1212_parse_csr);
1322 EXPORT_SYMBOL(_csr1212_read_keyval);
1323 EXPORT_SYMBOL(_csr1212_destroy_keyval);
1324
|
This page was automatically generated by the
LXR engine.
|