nosy-dump.c 25 KB

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