modem_utils.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (C) 2012 Samsung Electronics.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #ifndef __MODEM_UTILS_H__
  15. #define __MODEM_UTILS_H__
  16. #include <linux/rbtree.h>
  17. #define IS_CONNECTED(iod, ld) ((iod)->link_types & LINKTYPE((ld)->link_type))
  18. #define MAX_MIF_BUFF_SIZE 0x80000 /* 512kb */
  19. #define MAX_IPC_SKB_SIZE 4096
  20. #define MAX_LOG_SIZE 64
  21. #define MAX_LOG_CNT (MAX_MIF_BUFF_SIZE / MAX_LOG_SIZE)
  22. #define MIF_ID_SIZE sizeof(enum mif_log_id)
  23. #define MAX_IPC_LOG_SIZE \
  24. (MAX_LOG_SIZE - sizeof(enum mif_log_id) \
  25. - sizeof(unsigned long long) - sizeof(size_t))
  26. #define MAX_IRQ_LOG_SIZE \
  27. (MAX_LOG_SIZE - sizeof(enum mif_log_id) \
  28. - sizeof(unsigned long long) - sizeof(struct mif_irq_map))
  29. #define MAX_COM_LOG_SIZE \
  30. (MAX_LOG_SIZE - sizeof(enum mif_log_id) \
  31. - sizeof(unsigned long long))
  32. #define MAX_TIM_LOG_SIZE \
  33. (MAX_LOG_SIZE - sizeof(enum mif_log_id) \
  34. - sizeof(unsigned long long) - sizeof(struct timespec))
  35. enum mif_log_id {
  36. MIF_IPC_RL2AP = 1,
  37. MIF_IPC_AP2CP,
  38. MIF_IPC_CP2AP,
  39. MIF_IPC_AP2RL,
  40. MIF_IRQ,
  41. MIF_COM,
  42. MIF_TIME
  43. };
  44. struct mif_irq_map {
  45. u16 magic;
  46. u16 access;
  47. u16 fmt_tx_in;
  48. u16 fmt_tx_out;
  49. u16 fmt_rx_in;
  50. u16 fmt_rx_out;
  51. u16 raw_tx_in;
  52. u16 raw_tx_out;
  53. u16 raw_rx_in;
  54. u16 raw_rx_out;
  55. u16 cp2ap;
  56. };
  57. struct mif_ipc_block {
  58. enum mif_log_id id;
  59. unsigned long long time;
  60. size_t len;
  61. char buff[MAX_IPC_LOG_SIZE];
  62. };
  63. struct mif_irq_block {
  64. enum mif_log_id id;
  65. unsigned long long time;
  66. struct mif_irq_map map;
  67. char buff[MAX_IRQ_LOG_SIZE];
  68. };
  69. struct mif_common_block {
  70. enum mif_log_id id;
  71. unsigned long long time;
  72. char buff[MAX_COM_LOG_SIZE];
  73. };
  74. struct mif_time_block {
  75. enum mif_log_id id;
  76. unsigned long long time;
  77. struct timespec epoch;
  78. char buff[MAX_TIM_LOG_SIZE];
  79. };
  80. int mif_dump_dpram(struct io_device *);
  81. int mif_dump_log(struct modem_shared *, struct io_device *);
  82. #define mif_irq_log(msd, map, data, len) \
  83. _mif_irq_log(MIF_IRQ, msd, map, data, len)
  84. #define mif_com_log(msd, format, ...) \
  85. _mif_com_log(MIF_COM, msd, pr_fmt(format), ##__VA_ARGS__)
  86. #define mif_time_log(msd, epoch, data, len) \
  87. _mif_time_log(MIF_TIME, msd, epoch, data, len)
  88. void mif_ipc_log(enum mif_log_id,
  89. struct modem_shared *, const char *, size_t);
  90. void _mif_irq_log(enum mif_log_id,
  91. struct modem_shared *, struct mif_irq_map, const char *, size_t);
  92. void _mif_com_log(enum mif_log_id,
  93. struct modem_shared *, const char *, ...);
  94. void _mif_time_log(enum mif_log_id,
  95. struct modem_shared *, struct timespec, const char *, size_t);
  96. /** find_linkdev - find a link device
  97. * @msd: struct modem_shared *
  98. */
  99. static inline struct link_device *find_linkdev(struct modem_shared *msd,
  100. enum modem_link link_type)
  101. {
  102. struct link_device *ld;
  103. list_for_each_entry(ld, &msd->link_dev_list, list) {
  104. if (ld->link_type == link_type)
  105. return ld;
  106. }
  107. return NULL;
  108. }
  109. /** countbits - count number of 1 bits as fastest way
  110. * @n: number
  111. */
  112. static inline unsigned int countbits(unsigned int n)
  113. {
  114. unsigned int i;
  115. for (i = 0; n != 0; i++)
  116. n &= (n - 1);
  117. return i;
  118. }
  119. /* print buffer as hex string */
  120. int pr_buffer(const char *tag, const char *data, size_t data_len,
  121. size_t max_len);
  122. /* print a sk_buff as hex string */
  123. #define pr_skb(tag, skb) \
  124. pr_buffer(tag, (char *)((skb)->data), (size_t)((skb)->len), (size_t)16)
  125. /* print a urb as hex string */
  126. #define pr_urb(tag, urb) \
  127. pr_buffer(tag, (char *)((urb)->transfer_buffer), \
  128. (size_t)((urb)->actual_length), (size_t)16)
  129. /* flow control CMD from CP, it use in serial devices */
  130. int link_rx_flowctl_cmd(struct link_device *ld, const char *data, size_t len);
  131. /* get iod from tree functions */
  132. struct io_device *get_iod_with_format(struct modem_shared *msd,
  133. enum dev_format format);
  134. struct io_device *get_iod_with_channel(struct modem_shared *msd,
  135. unsigned channel);
  136. static inline struct io_device *link_get_iod_with_format(
  137. struct link_device *ld, enum dev_format format)
  138. {
  139. struct io_device *iod = get_iod_with_format(ld->msd, format);
  140. return (iod && IS_CONNECTED(iod, ld)) ? iod : NULL;
  141. }
  142. static inline struct io_device *link_get_iod_with_channel(
  143. struct link_device *ld, unsigned channel)
  144. {
  145. struct io_device *iod = get_iod_with_channel(ld->msd, channel);
  146. return (iod && IS_CONNECTED(iod, ld)) ? iod : NULL;
  147. }
  148. /* insert iod to tree functions */
  149. struct io_device *insert_iod_with_format(struct modem_shared *msd,
  150. enum dev_format format, struct io_device *iod);
  151. struct io_device *insert_iod_with_channel(struct modem_shared *msd,
  152. unsigned channel, struct io_device *iod);
  153. /* iodev for each */
  154. typedef void (*action_fn)(struct io_device *iod, void *args);
  155. void iodevs_for_each(struct modem_shared *msd, action_fn action, void *args);
  156. /* netif wake/stop queue of iod */
  157. void iodev_netif_wake(struct io_device *iod, void *args);
  158. void iodev_netif_stop(struct io_device *iod, void *args);
  159. /* change tx_link of raw devices */
  160. void rawdevs_set_tx_link(struct modem_shared *msd, enum modem_link link_type);
  161. void mif_add_timer(struct timer_list *timer, unsigned long expire,
  162. void (*function)(unsigned long), unsigned long data);
  163. /* debug helper functions for sipc4, sipc5 */
  164. void mif_print_data(char *buf, int len);
  165. void print_sipc4_hdlc_fmt_frame(const u8 *psrc);
  166. void print_sipc4_fmt_frame(const u8 *psrc);
  167. /*---------------------------------------------------------------------------
  168. IPv4 Header Format
  169. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  170. |Version| IHL |Type of Service| Total Length |
  171. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  172. | Identification |C|D|M| Fragment Offset |
  173. | |E|F|F| |
  174. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  175. | Time to Live | Protocol | Header Checksum |
  176. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  177. | Source Address |
  178. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  179. | Destination Address |
  180. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  181. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  182. | Options | Padding |
  183. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  184. IHL - Header Length
  185. Flags - Consist of 3 bits
  186. The 1st bit is "Congestion" bit.
  187. The 2nd bit is "Dont Fragment" bit.
  188. The 3rd bit is "More Fragments" bit.
  189. ---------------------------------------------------------------------------*/
  190. #define IPV4_HDR_SIZE 20
  191. /*-------------------------------------------------------------------------
  192. TCP Header Format
  193. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  194. | Source Port | Destination Port |
  195. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  196. | Sequence Number |
  197. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  198. | Acknowledgment Number |
  199. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  200. | Data | |C|E|U|A|P|R|S|F| |
  201. | Offset| Rsvd |W|C|R|C|S|S|Y|I| Window |
  202. | | |R|E|G|K|H|T|N|N| |
  203. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  204. | Checksum | Urgent Pointer |
  205. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  206. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  207. | Options | Padding |
  208. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  209. | data |
  210. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  211. -------------------------------------------------------------------------*/
  212. #define TCP_HDR_SIZE 20
  213. /*-------------------------------------------------------------------------
  214. UDP Header Format
  215. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  216. | Source Port | Destination Port |
  217. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  218. | Length | Checksum |
  219. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  220. | data |
  221. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  222. -------------------------------------------------------------------------*/
  223. #define UDP_HDR_SIZE 8
  224. void print_ip4_packet(u8 *ip_pkt);
  225. bool is_dns_packet(u8 *ip_pkt);
  226. bool is_syn_packet(u8 *ip_pkt);
  227. #endif/*__MODEM_UTILS_H__*/