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