tools/firewire: nosy-dump: change to kernel coding style
[linux-3.10.git] / tools / firewire / nosy-dump.c
1 /*
2  * nosy-dump - Interface to snoop mode driver for TI PCILynx 1394 controllers
3  * Copyright (C) 2002-2006 Kristian Høgsberg
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <byteswap.h>
21 #include <endian.h>
22 #include <fcntl.h>
23 #include <poll.h>
24 #include <popt.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30 #include <sys/time.h>
31 #include <termios.h>
32 #include <unistd.h>
33
34 #include "list.h"
35 #include "nosy-dump.h"
36 #include "nosy-user.h"
37
38 enum {
39         PACKET_FIELD_DETAIL             = 0x01,
40         PACKET_FIELD_DATA_LENGTH        = 0x02,
41         /* Marks the fields we print in transaction view. */
42         PACKET_FIELD_TRANSACTION        = 0x04,
43 };
44
45 static void print_packet(uint32_t *data, size_t length);
46 static void decode_link_packet(struct link_packet *packet, size_t length,
47                                int include_flags, int exclude_flags);
48 static int run = 1;
49 sig_t sys_sigint_handler;
50
51 static char *option_nosy_device = "/dev/nosy";
52 static char *option_view = "packet";
53 static char *option_output;
54 static char *option_input;
55 static int option_hex;
56 static int option_iso;
57 static int option_cycle_start;
58 static int option_version;
59 static int option_verbose;
60
61 enum {
62         VIEW_TRANSACTION,
63         VIEW_PACKET,
64         VIEW_STATS,
65 };
66
67 static const struct poptOption options[] = {
68         {
69                 .longName       = "device",
70                 .shortName      = 'd',
71                 .argInfo        = POPT_ARG_STRING,
72                 .arg            = &option_nosy_device,
73                 .descrip        = "Path to nosy device.",
74                 .argDescrip     = "DEVICE"
75         },
76         {
77                 .longName       = "view",
78                 .argInfo        = POPT_ARG_STRING,
79                 .arg            = &option_view,
80                 .descrip        = "Specify view of bus traffic: packet, transaction or stats.",
81                 .argDescrip     = "VIEW"
82         },
83         {
84                 .longName       = "hex",
85                 .shortName      = 'x',
86                 .argInfo        = POPT_ARG_NONE,
87                 .arg            = &option_hex,
88                 .descrip        = "Print each packet in hex.",
89         },
90         {
91                 .longName       = "iso",
92                 .argInfo        = POPT_ARG_NONE,
93                 .arg            = &option_iso,
94                 .descrip        = "Print iso packets.",
95         },
96         {
97                 .longName       = "cycle-start",
98                 .argInfo        = POPT_ARG_NONE,
99                 .arg            = &option_cycle_start,
100                 .descrip        = "Print cycle start packets.",
101         },
102         {
103                 .longName       = "verbose",
104                 .shortName      = 'v',
105                 .argInfo        = POPT_ARG_NONE,
106                 .arg            = &option_verbose,
107                 .descrip        = "Verbose packet view.",
108         },
109         {
110                 .longName       = "output",
111                 .shortName      = 'o',
112                 .argInfo        = POPT_ARG_STRING,
113                 .arg            = &option_output,
114                 .descrip        = "Log to output file.",
115                 .argDescrip     = "FILENAME"
116         },
117         {
118                 .longName       = "input",
119                 .shortName      = 'i',
120                 .argInfo        = POPT_ARG_STRING,
121                 .arg            = &option_input,
122                 .descrip        = "Decode log from file.",
123                 .argDescrip     = "FILENAME"
124         },
125         {
126                 .longName       = "version",
127                 .argInfo        = POPT_ARG_NONE,
128                 .arg            = &option_version,
129                 .descrip        = "Specify print version info.",
130         },
131         POPT_AUTOHELP
132         POPT_TABLEEND
133 };
134
135 /* Allow all ^C except the first to interrupt the program in the usual way. */
136 void
137 sigint_handler(int signal_num)
138 {
139         if (run == 1) {
140                 run = 0;
141                 signal(SIGINT, SIG_DFL);
142         }
143 }
144
145 struct subaction *
146 subaction_create(uint32_t *data, size_t length)
147 {
148         struct subaction *sa;
149
150         /* we put the ack in the subaction struct for easy access. */
151         sa = malloc(sizeof *sa - sizeof sa->packet + length);
152         sa->ack = data[length / 4 - 1];
153         sa->length = length;
154         memcpy(&sa->packet, data, length);
155
156         return sa;
157 }
158
159 void
160 subaction_destroy(struct subaction *sa)
161 {
162         free(sa);
163 }
164
165 struct list pending_transaction_list = {
166         &pending_transaction_list, &pending_transaction_list
167 };
168
169 struct link_transaction *
170 link_transaction_lookup(int request_node, int response_node, int tlabel)
171 {
172         struct link_transaction *t;
173
174         list_for_each_entry(t, &pending_transaction_list, link) {
175                 if (t->request_node == request_node &&
176                     t->response_node == response_node &&
177                     t->tlabel == tlabel)
178                         return t;
179         }
180
181         t = malloc(sizeof *t);
182         t->request_node = request_node;
183         t->response_node = response_node;
184         t->tlabel = tlabel;
185         list_init(&t->request_list);
186         list_init(&t->response_list);
187
188         list_append(&pending_transaction_list, &t->link);
189
190         return t;
191 }
192
193 void
194 link_transaction_destroy(struct link_transaction *t)
195 {
196         struct subaction *sa;
197
198         while (!list_empty(&t->request_list)) {
199                 sa = list_head(&t->request_list, struct subaction, link);
200                 list_remove(&sa->link);
201                 subaction_destroy(sa);
202         }
203         while (!list_empty(&t->response_list)) {
204                 sa = list_head(&t->response_list, struct subaction, link);
205                 list_remove(&sa->link);
206                 subaction_destroy(sa);
207         }
208         free(t);
209 }
210
211 struct protocol_decoder {
212         const char *name;
213         int (*decode)(struct link_transaction *t);
214 };
215
216 static struct protocol_decoder protocol_decoders[] = {
217         { "FCP", decode_fcp }
218 };
219
220 void
221 handle_transaction(struct link_transaction *t)
222 {
223         struct subaction *sa;
224         int i;
225
226         if (!t->request) {
227                 printf("BUG in handle_transaction\n");
228                 return;
229         }
230
231         for (i = 0; i < array_length(protocol_decoders); i++)
232                 if (protocol_decoders[i].decode(t))
233                         break;
234
235         /* HACK: decode only fcp right now. */
236         return;
237
238         decode_link_packet(&t->request->packet, t->request->length,
239                            PACKET_FIELD_TRANSACTION, 0);
240         if (t->response)
241                 decode_link_packet(&t->response->packet, t->request->length,
242                                    PACKET_FIELD_TRANSACTION, 0);
243         else
244                 printf("[no response]");
245
246         if (option_verbose) {
247                 list_for_each_entry(sa, &t->request_list, link)
248                         print_packet((uint32_t *) &sa->packet, sa->length);
249                 list_for_each_entry(sa, &t->response_list, link)
250                         print_packet((uint32_t *) &sa->packet, sa->length);
251         }
252         printf("\r\n");
253
254         link_transaction_destroy(t);
255 }
256
257 void
258 clear_pending_transaction_list(void)
259 {
260         struct link_transaction *t;
261
262         while (!list_empty(&pending_transaction_list)) {
263                 t = list_head(&pending_transaction_list,
264                               struct link_transaction, link);
265                 list_remove(&t->link);
266                 link_transaction_destroy(t);
267                 /* print unfinished transactions */
268         }
269 }
270
271 static const char * const tcode_names[] = {
272         [0x0] = "write_quadlet_request",        [0x6] = "read_quadlet_response",
273         [0x1] = "write_block_request",          [0x7] = "read_block_response",
274         [0x2] = "write_response",               [0x8] = "cycle_start",
275         [0x3] = "reserved",                     [0x9] = "lock_request",
276         [0x4] = "read_quadlet_request",         [0xa] = "iso_data",
277         [0x5] = "read_block_request",           [0xb] = "lock_response",
278 };
279
280 static const char * const ack_names[] = {
281         [0x0] = "no ack",                       [0x8] = "reserved (0x08)",
282         [0x1] = "ack_complete",                 [0x9] = "reserved (0x09)",
283         [0x2] = "ack_pending",                  [0xa] = "reserved (0x0a)",
284         [0x3] = "reserved (0x03)",              [0xb] = "reserved (0x0b)",
285         [0x4] = "ack_busy_x",                   [0xc] = "reserved (0x0c)",
286         [0x5] = "ack_busy_a",                   [0xd] = "ack_data_error",
287         [0x6] = "ack_busy_b",                   [0xe] = "ack_type_error",
288         [0x7] = "reserved (0x07)",              [0xf] = "reserved (0x0f)",
289 };
290
291 static const char * const rcode_names[] = {
292         [0x0] = "complete",                     [0x4] = "conflict_error",
293         [0x1] = "reserved (0x01)",              [0x5] = "data_error",
294         [0x2] = "reserved (0x02)",              [0x6] = "type_error",
295         [0x3] = "reserved (0x03)",              [0x7] = "address_error",
296 };
297
298 static const char * const retry_names[] = {
299         [0x0] = "retry_1",
300         [0x1] = "retry_x",
301         [0x2] = "retry_a",
302         [0x3] = "retry_b",
303 };
304
305 enum {
306         PACKET_RESERVED,
307         PACKET_REQUEST,
308         PACKET_RESPONSE,
309         PACKET_OTHER,
310 };
311
312 struct packet_info {
313         const char *name;
314         int type;
315         int response_tcode;
316         struct packet_field *fields;
317         int field_count;
318 };
319
320 struct packet_field {
321         const char *name; /* Short name for field. */
322         int offset;     /* Location of field, specified in bits; */
323                         /* negative means from end of packet.    */
324         int width;      /* Width of field, 0 means use data_length. */
325         int flags;      /* Show options. */
326         const char * const *value_names;
327 };
328
329 #define COMMON_REQUEST_FIELDS                                           \
330         { "dest", 0, 16, PACKET_FIELD_TRANSACTION },                    \
331         { "tl", 16, 6 },                                                \
332         { "rt", 22, 2, PACKET_FIELD_DETAIL, retry_names },              \
333         { "tcode", 24, 4, PACKET_FIELD_TRANSACTION, tcode_names },      \
334         { "pri", 28, 4, PACKET_FIELD_DETAIL },                          \
335         { "src", 32, 16, PACKET_FIELD_TRANSACTION },                    \
336         { "offs", 48, 48, PACKET_FIELD_TRANSACTION }
337
338 #define COMMON_RESPONSE_FIELDS                                          \
339         { "dest", 0, 16 },                                              \
340         { "tl", 16, 6 },                                                \
341         { "rt", 22, 2, PACKET_FIELD_DETAIL, retry_names },              \
342         { "tcode", 24, 4, 0, tcode_names },                             \
343         { "pri", 28, 4, PACKET_FIELD_DETAIL },                          \
344         { "src", 32, 16 },                                              \
345         { "rcode", 48, 4, PACKET_FIELD_TRANSACTION, rcode_names }
346
347 struct packet_field read_quadlet_request_fields[] = {
348         COMMON_REQUEST_FIELDS,
349         { "crc", 96, 32, PACKET_FIELD_DETAIL },
350         { "ack", 156, 4, 0, ack_names },
351 };
352
353 struct packet_field read_quadlet_response_fields[] = {
354         COMMON_RESPONSE_FIELDS,
355         { "data", 96, 32, PACKET_FIELD_TRANSACTION },
356         { "crc", 128, 32, PACKET_FIELD_DETAIL },
357         { "ack", 188, 4, 0, ack_names },
358 };
359
360 struct packet_field read_block_request_fields[] = {
361         COMMON_REQUEST_FIELDS,
362         { "data_length", 96, 16, PACKET_FIELD_TRANSACTION },
363         { "extended_tcode", 112, 16 },
364         { "crc", 128, 32, PACKET_FIELD_DETAIL },
365         { "ack", 188, 4, 0, ack_names },
366 };
367
368 struct packet_field block_response_fields[] = {
369         COMMON_RESPONSE_FIELDS,
370         { "data_length", 96, 16, PACKET_FIELD_DATA_LENGTH },
371         { "extended_tcode", 112, 16 },
372         { "crc", 128, 32, PACKET_FIELD_DETAIL },
373         { "data", 160, 0, PACKET_FIELD_TRANSACTION },
374         { "crc", -64, 32, PACKET_FIELD_DETAIL },
375         { "ack", -4, 4, 0, ack_names },
376 };
377
378 struct packet_field write_quadlet_request_fields[] = {
379         COMMON_REQUEST_FIELDS,
380         { "data", 96, 32, PACKET_FIELD_TRANSACTION },
381         { "ack", -4, 4, 0, ack_names },
382 };
383
384 struct packet_field block_request_fields[] = {
385         COMMON_REQUEST_FIELDS,
386         { "data_length", 96, 16, PACKET_FIELD_DATA_LENGTH | PACKET_FIELD_TRANSACTION },
387         { "extended_tcode", 112, 16, PACKET_FIELD_TRANSACTION },
388         { "crc", 128, 32, PACKET_FIELD_DETAIL },
389         { "data", 160, 0, PACKET_FIELD_TRANSACTION },
390         { "crc", -64, 32, PACKET_FIELD_DETAIL },
391         { "ack", -4, 4, 0, ack_names },
392 };
393
394 struct packet_field write_response_fields[] = {
395         COMMON_RESPONSE_FIELDS,
396         { "reserved", 64, 32, PACKET_FIELD_DETAIL },
397         { "ack", -4, 4, 0, ack_names },
398 };
399
400 struct packet_field iso_data_fields[] = {
401         { "data_length", 0, 16, PACKET_FIELD_DATA_LENGTH },
402         { "tag", 16, 2 },
403         { "channel", 18, 6 },
404         { "tcode", 24, 4, 0, tcode_names },
405         { "sy", 28, 4 },
406         { "crc", 32, 32, PACKET_FIELD_DETAIL },
407         { "data", 64, 0 },
408         { "crc", -64, 32, PACKET_FIELD_DETAIL },
409         { "ack", -4, 4, 0, ack_names },
410 };
411
412 static struct packet_info packet_info[] = {
413         {
414                 .name           = "write_quadlet_request",
415                 .type           = PACKET_REQUEST,
416                 .response_tcode = TCODE_WRITE_RESPONSE,
417                 .fields         = write_quadlet_request_fields,
418                 .field_count    = array_length(write_quadlet_request_fields)
419         },
420         {
421                 .name           = "write_block_request",
422                 .type           = PACKET_REQUEST,
423                 .response_tcode = TCODE_WRITE_RESPONSE,
424                 .fields         = block_request_fields,
425                 .field_count    = array_length(block_request_fields)
426         },
427         {
428                 .name           = "write_response",
429                 .type           = PACKET_RESPONSE,
430                 .fields         = write_response_fields,
431                 .field_count    = array_length(write_response_fields)
432         },
433         {
434                 .name           = "reserved",
435                 .type           = PACKET_RESERVED,
436         },
437         {
438                 .name           = "read_quadlet_request",
439                 .type           = PACKET_REQUEST,
440                 .response_tcode = TCODE_READ_QUADLET_RESPONSE,
441                 .fields         = read_quadlet_request_fields,
442                 .field_count    = array_length(read_quadlet_request_fields)
443         },
444         {
445                 .name           = "read_block_request",
446                 .type           = PACKET_REQUEST,
447                 .response_tcode = TCODE_READ_BLOCK_RESPONSE,
448                 .fields         = read_block_request_fields,
449                 .field_count    = array_length(read_block_request_fields)
450         },
451         {
452                 .name           = "read_quadlet_response",
453                 .type           = PACKET_RESPONSE,
454                 .fields         = read_quadlet_response_fields,
455                 .field_count    = array_length(read_quadlet_response_fields)
456         },
457         {
458                 .name           = "read_block_response",
459                 .type           = PACKET_RESPONSE,
460                 .fields         = block_response_fields,
461                 .field_count    = array_length(block_response_fields)
462         },
463         {
464                 .name           = "cycle_start",
465                 .type           = PACKET_OTHER,
466                 .fields         = write_quadlet_request_fields,
467                 .field_count    = array_length(write_quadlet_request_fields)
468         },
469         {
470                 .name           = "lock_request",
471                 .type           = PACKET_REQUEST,
472                 .fields         = block_request_fields,
473                 .field_count    = array_length(block_request_fields)
474         },
475         {
476                 .name           = "iso_data",
477                 .type           = PACKET_OTHER,
478                 .fields         = iso_data_fields,
479                 .field_count    = array_length(iso_data_fields)
480         },
481         {
482                 .name           = "lock_response",
483                 .type           = PACKET_RESPONSE,
484                 .fields         = block_response_fields,
485                 .field_count    = array_length(block_response_fields)
486         },
487 };
488
489 int
490 handle_packet(uint32_t *data, size_t length)
491 {
492         if (length == 0) {
493                 printf("bus reset\r\n");
494                 clear_pending_transaction_list();
495         } else if (length > sizeof(struct phy_packet)) {
496                 struct link_packet *p = (struct link_packet *) data;
497                 struct subaction *sa, *prev;
498                 struct link_transaction *t;
499
500                 switch (packet_info[p->common.tcode].type) {
501                 case PACKET_REQUEST:
502                         t = link_transaction_lookup(p->common.source, p->common.destination,
503                                         p->common.tlabel);
504                         sa = subaction_create(data, length);
505                         t->request = sa;
506
507                         if (!list_empty(&t->request_list)) {
508                                 prev = list_tail(&t->request_list,
509                                                  struct subaction, link);
510
511                                 if (!ACK_BUSY(prev->ack)) {
512                                         /*
513                                          * error, we should only see ack_busy_* before the
514                                          * ack_pending/ack_complete -- this is an ack_pending
515                                          * instead (ack_complete would have finished the
516                                          * transaction).
517                                          */
518                                 }
519
520                                 if (prev->packet.common.tcode != sa->packet.common.tcode ||
521                                     prev->packet.common.tlabel != sa->packet.common.tlabel) {
522                                         /* memcmp() ? */
523                                         /* error, these should match for retries. */
524                                 }
525                         }
526
527                         list_append(&t->request_list, &sa->link);
528
529                         switch (sa->ack) {
530                         case ACK_COMPLETE:
531                                 if (p->common.tcode != TCODE_WRITE_QUADLET &&
532                                     p->common.tcode != TCODE_WRITE_BLOCK)
533                                         /* error, unified transactions only allowed for write */;
534                                 list_remove(&t->link);
535                                 handle_transaction(t);
536                                 break;
537
538                         case ACK_NO_ACK:
539                         case ACK_DATA_ERROR:
540                         case ACK_TYPE_ERROR:
541                                 list_remove(&t->link);
542                                 handle_transaction(t);
543                                 break;
544
545                         case ACK_PENDING:
546                                 /* request subaction phase over, wait for response. */
547                                 break;
548
549                         case ACK_BUSY_X:
550                         case ACK_BUSY_A:
551                         case ACK_BUSY_B:
552                                 /* ok, wait for retry. */
553                                 /* check that retry protocol is respected. */
554                                 break;
555                         }
556                         break;
557
558                 case PACKET_RESPONSE:
559                         t = link_transaction_lookup(p->common.destination, p->common.source,
560                                         p->common.tlabel);
561                         if (list_empty(&t->request_list)) {
562                                 /* unsolicited response */
563                         }
564
565                         sa = subaction_create(data, length);
566                         t->response = sa;
567
568                         if (!list_empty(&t->response_list)) {
569                                 prev = list_tail(&t->response_list, struct subaction, link);
570
571                                 if (!ACK_BUSY(prev->ack)) {
572                                         /*
573                                          * error, we should only see ack_busy_* before the
574                                          * ack_pending/ack_complete
575                                          */
576                                 }
577
578                                 if (prev->packet.common.tcode != sa->packet.common.tcode ||
579                                     prev->packet.common.tlabel != sa->packet.common.tlabel) {
580                                         /* use memcmp() instead? */
581                                         /* error, these should match for retries. */
582                                 }
583                         } else {
584                                 prev = list_tail(&t->request_list, struct subaction, link);
585                                 if (prev->ack != ACK_PENDING) {
586                                         /*
587                                          * error, should not get response unless last request got
588                                          * ack_pending.
589                                          */
590                                 }
591
592                                 if (packet_info[prev->packet.common.tcode].response_tcode !=
593                                     sa->packet.common.tcode) {
594                                         /* error, tcode mismatch */
595                                 }
596                         }
597
598                         list_append(&t->response_list, &sa->link);
599
600                         switch (sa->ack) {
601                         case ACK_COMPLETE:
602                         case ACK_NO_ACK:
603                         case ACK_DATA_ERROR:
604                         case ACK_TYPE_ERROR:
605                                 list_remove(&t->link);
606                                 handle_transaction(t);
607                                 /* transaction complete, remove t from pending list. */
608                                 break;
609
610                         case ACK_PENDING:
611                                 /* error for responses. */
612                                 break;
613
614                         case ACK_BUSY_X:
615                         case ACK_BUSY_A:
616                         case ACK_BUSY_B:
617                                 /* no problem, wait for next retry */
618                                 break;
619                         }
620
621                         break;
622
623                 case PACKET_OTHER:
624                 case PACKET_RESERVED:
625                         return 0;
626                 }
627         }
628
629         return 1;
630 }
631
632 unsigned int get_bits(struct link_packet *packet, int offset, int width)
633 {
634         uint32_t *data = (uint32_t *) packet;
635         uint32_t index, shift, mask;
636
637         index = offset / 32 + 1;
638         shift = 32 - (offset & 31) - width;
639         mask = width == 32 ? ~0 : (1 << width) - 1;
640
641         return (data[index] >> shift) & mask;
642 }
643
644 #if __BYTE_ORDER == __LITTLE_ENDIAN
645 #define byte_index(i) ((i) ^ 3)
646 #elif __BYTE_ORDER == __BIG_ENDIAN
647 #define byte_index(i) (i)
648 #else
649 #error unsupported byte order.
650 #endif
651
652 void dump_data(unsigned char *data, int length)
653 {
654         int i, print_length;
655
656         if (length > 128)
657                 print_length = 128;
658         else
659                 print_length = length;
660
661         for (i = 0; i < print_length; i++)
662                 printf("%s%02hhx",
663                        (i % 4 == 0 && i != 0) ? " " : "",
664                        data[byte_index(i)]);
665
666         if (print_length < length)
667                 printf(" (%d more bytes)", length - print_length);
668 }
669
670 static void
671 decode_link_packet(struct link_packet *packet, size_t length,
672                    int include_flags, int exclude_flags)
673 {
674         struct packet_info *pi;
675         int data_length = 0;
676         int i;
677
678         pi = &packet_info[packet->common.tcode];
679
680         for (i = 0; i < pi->field_count; i++) {
681                 struct packet_field *f = &pi->fields[i];
682                 int offset;
683
684                 if (f->flags & exclude_flags)
685                         continue;
686                 if (include_flags && !(f->flags & include_flags))
687                         continue;
688
689                 if (f->offset < 0)
690                         offset = length * 8 + f->offset - 32;
691                 else
692                         offset = f->offset;
693
694                 if (f->value_names != NULL) {
695                         uint32_t bits;
696
697                         bits = get_bits(packet, offset, f->width);
698                         printf("%s", f->value_names[bits]);
699                 } else if (f->width == 0) {
700                         printf("%s=[", f->name);
701                         dump_data((unsigned char *) packet + (offset / 8 + 4), data_length);
702                         printf("]");
703                 } else {
704                         unsigned long long bits;
705                         int high_width, low_width;
706
707                         if ((offset & ~31) != ((offset + f->width - 1) & ~31)) {
708                                 /* Bit field spans quadlet boundary. */
709                                 high_width = ((offset + 31) & ~31) - offset;
710                                 low_width = f->width - high_width;
711
712                                 bits = get_bits(packet, offset, high_width);
713                                 bits = (bits << low_width) |
714                                         get_bits(packet, offset + high_width, low_width);
715                         } else {
716                                 bits = get_bits(packet, offset, f->width);
717                         }
718
719                         printf("%s=0x%0*llx", f->name, (f->width + 3) / 4, bits);
720
721                         if (f->flags & PACKET_FIELD_DATA_LENGTH)
722                                 data_length = bits;
723                 }
724
725                 if (i < pi->field_count - 1)
726                         printf(", ");
727         }
728 }
729
730 static void
731 print_packet(uint32_t *data, size_t length)
732 {
733         int i;
734
735         printf("%6u  ", data[0]);
736
737         if (length == 4) {
738                 printf("bus reset");
739         } else if (length < sizeof(struct phy_packet)) {
740                 printf("short packet: ");
741                 for (i = 1; i < length / 4; i++)
742                         printf("%s%08x", i == 0 ? "[" : " ", data[i]);
743                 printf("]");
744
745         } else if (length == sizeof(struct phy_packet) && data[1] == ~data[2]) {
746                 struct phy_packet *pp = (struct phy_packet *) data;
747
748                 /* phy packet are 3 quadlets: the 1 quadlet payload,
749                  * the bitwise inverse of the payload and the snoop
750                  * mode ack */
751
752                 switch (pp->common.identifier) {
753                 case PHY_PACKET_CONFIGURATION:
754                         if (!pp->phy_config.set_root && !pp->phy_config.set_gap_count) {
755                                 printf("ext phy config: phy_id=%02x", pp->phy_config.root_id);
756                         } else {
757                                 printf("phy config:");
758                                 if (pp->phy_config.set_root)
759                                         printf(" set_root_id=%02x", pp->phy_config.root_id);
760                                 if (pp->phy_config.set_gap_count)
761                                         printf(" set_gap_count=%d", pp->phy_config.gap_count);
762                         }
763                         break;
764
765                 case PHY_PACKET_LINK_ON:
766                         printf("link-on packet, phy_id=%02x", pp->link_on.phy_id);
767                         break;
768
769                 case PHY_PACKET_SELF_ID:
770                         if (pp->self_id.extended) {
771                                 printf("extended self id: phy_id=%02x, seq=%d",
772                                        pp->ext_self_id.phy_id, pp->ext_self_id.sequence);
773                         } else {
774                                 static const char * const speed_names[] = {
775                                         "S100", "S200", "S400", "BETA"
776                                 };
777                                 printf("self id: phy_id=%02x, link %s, gap_count=%d, speed=%s%s%s",
778                                        pp->self_id.phy_id,
779                                        (pp->self_id.link_active ? "active" : "not active"),
780                                        pp->self_id.gap_count,
781                                        speed_names[pp->self_id.phy_speed],
782                                        (pp->self_id.contender ? ", irm contender" : ""),
783                                        (pp->self_id.initiated_reset ? ", initiator" : ""));
784                         }
785                         break;
786                 default:
787                         printf("unknown phy packet: ");
788                         for (i = 1; i < length / 4; i++)
789                                 printf("%s%08x", i == 0 ? "[" : " ", data[i]);
790                         printf("]");
791                         break;
792                 }
793         } else {
794                 struct link_packet *packet = (struct link_packet *) data;
795
796                 decode_link_packet(packet, length, 0,
797                                    option_verbose ? 0 : PACKET_FIELD_DETAIL);
798         }
799
800         if (option_hex) {
801                 printf("  [");
802                 dump_data((unsigned char *) data + 4, length - 4);
803                 printf("]");
804         }
805
806         printf("\r\n");
807 }
808
809 #define HIDE_CURSOR     "\033[?25l"
810 #define SHOW_CURSOR     "\033[?25h"
811 #define CLEAR           "\033[H\033[2J"
812
813 static void
814 print_stats(uint32_t *data, size_t length)
815 {
816         static int bus_reset_count, short_packet_count, phy_packet_count;
817         static int tcode_count[16];
818         static struct timeval last_update;
819         struct timeval now;
820         int i;
821
822         if (length == 0)
823                 bus_reset_count++;
824         else if (length < sizeof(struct phy_packet))
825                 short_packet_count++;
826         else if (length == sizeof(struct phy_packet) && data[1] == ~data[2])
827                 phy_packet_count++;
828         else {
829                 struct link_packet *packet = (struct link_packet *) data;
830                 tcode_count[packet->common.tcode]++;
831         }
832
833         gettimeofday(&now, NULL);
834         if (now.tv_sec <= last_update.tv_sec &&
835             now.tv_usec < last_update.tv_usec + 500000)
836                 return;
837
838         last_update = now;
839         printf(CLEAR HIDE_CURSOR
840                "  bus resets              : %8d\n"
841                "  short packets           : %8d\n"
842                "  phy packets             : %8d\n",
843                bus_reset_count, short_packet_count, phy_packet_count);
844
845         for (i = 0; i < array_length(packet_info); i++)
846                 if (packet_info[i].type != PACKET_RESERVED)
847                         printf("  %-24s: %8d\n", packet_info[i].name, tcode_count[i]);
848         printf(SHOW_CURSOR "\n");
849 }
850
851 struct termios saved_attributes;
852
853 void
854 reset_input_mode(void)
855 {
856         tcsetattr(STDIN_FILENO, TCSANOW, &saved_attributes);
857 }
858
859 void
860 set_input_mode(void)
861 {
862         struct termios tattr;
863
864         /* Make sure stdin is a terminal. */
865         if (!isatty(STDIN_FILENO)) {
866                 fprintf(stderr, "Not a terminal.\n");
867                 exit(EXIT_FAILURE);
868         }
869
870         /* Save the terminal attributes so we can restore them later. */
871         tcgetattr(STDIN_FILENO, &saved_attributes);
872         atexit(reset_input_mode);
873
874         /* Set the funny terminal modes. */
875         tcgetattr(STDIN_FILENO, &tattr);
876         tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
877         tattr.c_cc[VMIN] = 1;
878         tattr.c_cc[VTIME] = 0;
879         tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr);
880 }
881
882 int main(int argc, const char *argv[])
883 {
884         int fd = -1;
885         FILE *output = NULL, *input = NULL;
886         poptContext con;
887         int retval;
888         int view;
889         char c;
890         struct pollfd pollfds[2];
891
892         sys_sigint_handler = signal(SIGINT, sigint_handler);
893
894         con = poptGetContext(NULL, argc, argv, options, 0);
895         retval = poptGetNextOpt(con);
896         if (retval < -1) {
897                 poptPrintUsage(con, stdout, 0);
898                 return -1;
899         }
900
901         if (option_version) {
902                 printf("dump tool for nosy sniffer, version %s\n", VERSION);
903                 return 0;
904         }
905
906         if (__BYTE_ORDER != __LITTLE_ENDIAN)
907                 fprintf(stderr, "warning: nosy has only been tested on little "
908                         "endian machines\n");
909
910         if (option_input != NULL) {
911                 input = fopen(option_input, "r");
912                 if (input == NULL) {
913                         fprintf(stderr, "Could not open %s, %m\n", option_input);
914                         return -1;
915                 }
916         } else {
917                 fd = open(option_nosy_device, O_RDWR);
918                 if (fd < 0) {
919                         fprintf(stderr, "Could not open %s, %m\n", option_nosy_device);
920                         return -1;
921                 }
922                 set_input_mode();
923         }
924
925         if (strcmp(option_view, "transaction") == 0)
926                 view = VIEW_TRANSACTION;
927         else if (strcmp(option_view, "stats") == 0)
928                 view = VIEW_STATS;
929         else
930                 view = VIEW_PACKET;
931
932         if (option_output) {
933                 output = fopen(option_output, "w");
934                 if (output == NULL) {
935                         fprintf(stderr, "Could not open %s, %m\n", option_output);
936                         return -1;
937                 }
938         }
939
940         setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
941
942         if (1) {
943                 uint32_t buf[128 * 1024];
944                 uint32_t filter;
945                 int length;
946
947                 filter = ~0;
948                 if (!option_iso)
949                         filter &= ~(1 << TCODE_ISO_DATA);
950                 if (!option_cycle_start)
951                         filter &= ~(1 << TCODE_CYCLE_START);
952                 if (view == VIEW_STATS)
953                         filter = ~(1 << TCODE_CYCLE_START);
954
955                 ioctl(fd, NOSY_IOC_FILTER, filter);
956
957                 ioctl(fd, NOSY_IOC_START);
958
959                 pollfds[0].fd = fd;
960                 pollfds[0].events = POLLIN;
961                 pollfds[1].fd = STDIN_FILENO;
962                 pollfds[1].events = POLLIN;
963
964                 while (run) {
965                         if (input != NULL) {
966                                 if (fread(&length, sizeof length, 1, input) != 1)
967                                         return 0;
968                                 fread(buf, 1, length, input);
969                         } else {
970                                 poll(pollfds, 2, -1);
971                                 if (pollfds[1].revents) {
972                                         read(STDIN_FILENO, &c, sizeof c);
973                                         switch (c) {
974                                         case 'q':
975                                                 if (output != NULL)
976                                                         fclose(output);
977                                                 return 0;
978                                         }
979                                 }
980
981                                 if (pollfds[0].revents)
982                                         length = read(fd, buf, sizeof buf);
983                                 else
984                                         continue;
985                         }
986
987                         if (output != NULL) {
988                                 fwrite(&length, sizeof length, 1, output);
989                                 fwrite(buf, 1, length, output);
990                         }
991
992                         switch (view) {
993                         case VIEW_TRANSACTION:
994                                 handle_packet(buf, length);
995                                 break;
996                         case VIEW_PACKET:
997                                 print_packet(buf, length);
998                                 break;
999                         case VIEW_STATS:
1000                                 print_stats(buf, length);
1001                                 break;
1002                         }
1003                 }
1004         } else {
1005                 poptPrintUsage(con, stdout, 0);
1006         }
1007
1008         if (output != NULL)
1009                 fclose(output);
1010
1011         close(fd);
1012
1013         poptFreeContext(con);
1014
1015         return 0;
1016 }