nosy-dump.c 25 KB

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