nosy-dump.c 24 KB

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