nosy-dump.c 25 KB

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