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