ecm_ipa.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/debugfs.h>
  13. #include <linux/errno.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/fs.h>
  16. #include <linux/module.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/sched.h>
  20. #include <linux/atomic.h>
  21. #include <mach/ecm_ipa.h>
  22. #define DRIVER_NAME "ecm_ipa"
  23. #define ECM_IPA_IPV4_HDR_NAME "ecm_eth_ipv4"
  24. #define ECM_IPA_IPV6_HDR_NAME "ecm_eth_ipv6"
  25. #define IPA_TO_USB_CLIENT IPA_CLIENT_USB_CONS
  26. #define INACTIVITY_MSEC_DELAY 100
  27. #define DEFAULT_OUTSTANDING_HIGH 64
  28. #define DEFAULT_OUTSTANDING_LOW 32
  29. #define DEBUGFS_TEMP_BUF_SIZE 4
  30. #define ECM_IPA_ERROR(fmt, args...) \
  31. pr_err(DRIVER_NAME "@%s@%d@ctx:%s: "\
  32. fmt, __func__, __LINE__, current->comm, ## args)
  33. #define NULL_CHECK(ptr) \
  34. do { \
  35. if (!(ptr)) { \
  36. ECM_IPA_ERROR("null pointer #ptr\n"); \
  37. return -EINVAL; \
  38. } \
  39. } \
  40. while (0)
  41. #define ECM_IPA_LOG_ENTRY() pr_debug("begin\n")
  42. #define ECM_IPA_LOG_EXIT() pr_debug("end\n")
  43. /**
  44. * enum ecm_ipa_state - specify the current driver internal state
  45. * which is guarded by a state machine.
  46. *
  47. * The driver internal state changes due to its external API usage.
  48. * The driver saves its internal state to guard from caller illegal
  49. * call sequence.
  50. * states:
  51. * UNLOADED is the first state which is the default one and is also the state
  52. * after the driver gets unloaded(cleanup).
  53. * INITIALIZED is the driver state once it finished registering
  54. * the network device and all internal data struct were initialized
  55. * CONNECTED is the driver state once the USB pipes were connected to IPA
  56. * UP is the driver state after the interface mode was set to UP but the
  57. * pipes are not connected yet - this state is meta-stable state.
  58. * CONNECTED_AND_UP is the driver state when the pipe were connected and
  59. * the interface got UP request from the network stack. this is the driver
  60. * idle operation state which allows it to transmit/receive data.
  61. * INVALID is a state which is not allowed.
  62. */
  63. enum ecm_ipa_state {
  64. ECM_IPA_UNLOADED = 0,
  65. ECM_IPA_INITIALIZED,
  66. ECM_IPA_CONNECTED,
  67. ECM_IPA_UP,
  68. ECM_IPA_CONNECTED_AND_UP,
  69. ECM_IPA_INVALID,
  70. };
  71. /**
  72. * enum ecm_ipa_operation - enumerations used to descibe the API operation
  73. *
  74. * Those enums are used as input for the driver state machine.
  75. */
  76. enum ecm_ipa_operation {
  77. ECM_IPA_INITIALIZE,
  78. ECM_IPA_CONNECT,
  79. ECM_IPA_OPEN,
  80. ECM_IPA_STOP,
  81. ECM_IPA_DISCONNECT,
  82. ECM_IPA_CLEANUP,
  83. };
  84. #define ECM_IPA_STATE_DEBUG(ecm_ipa_ctx) \
  85. pr_debug("Driver state - %s", ecm_ipa_state_string(ecm_ipa_ctx->state));
  86. /**
  87. * struct ecm_ipa_dev - main driver context parameters
  88. * @net: network interface struct implemented by this driver
  89. * @directory: debugfs directory for various debuging switches
  90. * @tx_enable: flag that enable/disable Tx path to continue to IPA
  91. * @rx_enable: flag that enable/disable Rx path to continue to IPA
  92. * @rm_enable: flag that enable/disable Resource manager request prior to Tx
  93. * @dma_enable: flag that allow on-the-fly DMA mode for IPA
  94. * @eth_ipv4_hdr_hdl: saved handle for ipv4 header-insertion table
  95. * @eth_ipv6_hdr_hdl: saved handle for ipv6 header-insertion table
  96. * @usb_to_ipa_hdl: save handle for IPA pipe operations
  97. * @ipa_to_usb_hdl: save handle for IPA pipe operations
  98. * @outstanding_pkts: number of packets sent to IPA without TX complete ACKed
  99. * @outstanding_high: number of outstanding packets allowed
  100. * @outstanding_low: number of outstanding packets which shall cause
  101. * to netdev queue start (after stopped due to outstanding_high reached)
  102. * @state: current state of ecm_ipa driver
  103. */
  104. struct ecm_ipa_dev {
  105. struct net_device *net;
  106. u32 tx_enable;
  107. u32 rx_enable;
  108. u32 rm_enable;
  109. bool dma_enable;
  110. struct dentry *directory;
  111. uint32_t eth_ipv4_hdr_hdl;
  112. uint32_t eth_ipv6_hdr_hdl;
  113. u32 usb_to_ipa_hdl;
  114. u32 ipa_to_usb_hdl;
  115. atomic_t outstanding_pkts;
  116. u8 outstanding_high;
  117. u8 outstanding_low;
  118. enum ecm_ipa_state state;
  119. };
  120. static int ecm_ipa_open(struct net_device *net);
  121. static void ecm_ipa_packet_receive_notify(void *priv,
  122. enum ipa_dp_evt_type evt, unsigned long data);
  123. static void ecm_ipa_tx_complete_notify(void *priv,
  124. enum ipa_dp_evt_type evt, unsigned long data);
  125. static int ecm_ipa_stop(struct net_device *net);
  126. static int ecm_ipa_rules_cfg(struct ecm_ipa_dev *ecm_ipa_ctx,
  127. const void *dst_mac, const void *src_mac);
  128. static void ecm_ipa_rules_destroy(struct ecm_ipa_dev *ecm_ipa_ctx);
  129. static int ecm_ipa_register_properties(void);
  130. static void ecm_ipa_deregister_properties(void);
  131. static void ecm_ipa_rm_notify(void *user_data, enum ipa_rm_event event,
  132. unsigned long data);
  133. static int ecm_ipa_create_rm_resource(struct ecm_ipa_dev *ecm_ipa_ctx);
  134. static void ecm_ipa_destory_rm_resource(struct ecm_ipa_dev *ecm_ipa_ctx);
  135. static bool rx_filter(struct sk_buff *skb);
  136. static bool tx_filter(struct sk_buff *skb);
  137. static bool rm_enabled(struct ecm_ipa_dev *ecm_ipa_ctx);
  138. static int resource_request(struct ecm_ipa_dev *ecm_ipa_ctx);
  139. static void resource_release(struct ecm_ipa_dev *ecm_ipa_ctx);
  140. static netdev_tx_t ecm_ipa_start_xmit(struct sk_buff *skb,
  141. struct net_device *net);
  142. static int ecm_ipa_debugfs_atomic_open(struct inode *inode, struct file *file);
  143. static ssize_t ecm_ipa_debugfs_enable_write_dma(struct file *file,
  144. const char __user *buf, size_t count, loff_t *ppos);
  145. static int ecm_ipa_debugfs_dma_open(struct inode *inode, struct file *file);
  146. static ssize_t ecm_ipa_debugfs_enable_write(struct file *file,
  147. const char __user *buf, size_t count, loff_t *ppos);
  148. static ssize_t ecm_ipa_debugfs_enable_read(struct file *file,
  149. char __user *ubuf, size_t count, loff_t *ppos);
  150. static ssize_t ecm_ipa_debugfs_atomic_read(struct file *file,
  151. char __user *ubuf, size_t count, loff_t *ppos);
  152. static int ecm_ipa_debugfs_init(struct ecm_ipa_dev *ecm_ipa_ctx);
  153. static void ecm_ipa_debugfs_destroy(struct ecm_ipa_dev *ecm_ipa_ctx);
  154. static int ecm_ipa_ep_registers_cfg(u32 usb_to_ipa_hdl, u32 ipa_to_usb_hdl);
  155. static int ecm_ipa_ep_registers_dma_cfg(u32 usb_to_ipa_hdl);
  156. static int ecm_ipa_set_device_ethernet_addr(u8 *dev_ethaddr,
  157. u8 device_ethaddr[]);
  158. static enum ecm_ipa_state ecm_ipa_next_state(enum ecm_ipa_state current_state,
  159. enum ecm_ipa_operation operation);
  160. static const char *ecm_ipa_state_string(enum ecm_ipa_state state);
  161. static int ecm_ipa_init_module(void);
  162. static void ecm_ipa_cleanup_module(void);
  163. static const struct net_device_ops ecm_ipa_netdev_ops = {
  164. .ndo_open = ecm_ipa_open,
  165. .ndo_stop = ecm_ipa_stop,
  166. .ndo_start_xmit = ecm_ipa_start_xmit,
  167. .ndo_set_mac_address = eth_mac_addr,
  168. };
  169. const struct file_operations ecm_ipa_debugfs_dma_ops = {
  170. .open = ecm_ipa_debugfs_dma_open,
  171. .read = ecm_ipa_debugfs_enable_read,
  172. .write = ecm_ipa_debugfs_enable_write_dma,
  173. };
  174. const struct file_operations ecm_ipa_debugfs_atomic_ops = {
  175. .open = ecm_ipa_debugfs_atomic_open,
  176. .read = ecm_ipa_debugfs_atomic_read,
  177. };
  178. /**
  179. * ecm_ipa_init() - create network device and initializes internal
  180. * data structures
  181. * @params: in/out parameters required for ecm_ipa initialization
  182. *
  183. * Shall be called prior to pipe connection.
  184. * The out parameters (the callbacks) shall be supplied to ipa_connect.
  185. * Detailed description:
  186. * - allocate the network device
  187. * - set default values for driver internals
  188. * - create debugfs folder and files
  189. * - create IPA resource manager client
  190. * - add header insertion rules for IPA driver (based on host/device
  191. * Ethernet addresses given in input params)
  192. * - register tx/rx properties to IPA driver (will be later used
  193. * by IPA configuration manager to configure reset of the IPA rules)
  194. * - set the carrier state to "off" (until ecm_ipa_connect is called)
  195. * - register the network device
  196. * - set the out parameters
  197. *
  198. * Returns negative errno, or zero on success
  199. */
  200. int ecm_ipa_init(struct ecm_ipa_params *params)
  201. {
  202. int result = 0;
  203. struct net_device *net;
  204. struct ecm_ipa_dev *ecm_ipa_ctx;
  205. ECM_IPA_LOG_ENTRY();
  206. pr_debug("%s initializing\n", DRIVER_NAME);
  207. NULL_CHECK(params);
  208. pr_debug("host_ethaddr=%pM, device_ethaddr=%pM\n",
  209. params->host_ethaddr,
  210. params->device_ethaddr);
  211. net = alloc_etherdev(sizeof(struct ecm_ipa_dev));
  212. if (!net) {
  213. result = -ENOMEM;
  214. ECM_IPA_ERROR("fail to allocate etherdev\n");
  215. goto fail_alloc_etherdev;
  216. }
  217. pr_debug("network device was successfully allocated\n");
  218. ecm_ipa_ctx = netdev_priv(net);
  219. memset(ecm_ipa_ctx, 0, sizeof(*ecm_ipa_ctx));
  220. ecm_ipa_ctx->net = net;
  221. ecm_ipa_ctx->tx_enable = true;
  222. ecm_ipa_ctx->rx_enable = true;
  223. ecm_ipa_ctx->rm_enable = true;
  224. ecm_ipa_ctx->outstanding_high = DEFAULT_OUTSTANDING_HIGH;
  225. ecm_ipa_ctx->outstanding_low = DEFAULT_OUTSTANDING_LOW;
  226. atomic_set(&ecm_ipa_ctx->outstanding_pkts, 0);
  227. snprintf(net->name, sizeof(net->name), "%s%%d", "ecm");
  228. net->netdev_ops = &ecm_ipa_netdev_ops;
  229. pr_debug("internal data structures were intialized and defaults set\n");
  230. result = ecm_ipa_debugfs_init(ecm_ipa_ctx);
  231. if (result)
  232. goto fail_debugfs;
  233. pr_debug("debugfs entries were created\n");
  234. result = ecm_ipa_create_rm_resource(ecm_ipa_ctx);
  235. if (result) {
  236. ECM_IPA_ERROR("fail on RM create\n");
  237. goto fail_create_rm;
  238. }
  239. pr_debug("RM resource was created\n");
  240. result = ecm_ipa_set_device_ethernet_addr(net->dev_addr,
  241. params->device_ethaddr);
  242. if (result) {
  243. ECM_IPA_ERROR("set device MAC failed\n");
  244. goto fail_set_device_ethernet;
  245. }
  246. pr_debug("Device Ethernet address set %pM\n", net->dev_addr);
  247. result = ecm_ipa_rules_cfg(ecm_ipa_ctx, params->host_ethaddr,
  248. params->device_ethaddr);
  249. if (result) {
  250. ECM_IPA_ERROR("fail on ipa rules set\n");
  251. goto fail_rules_cfg;
  252. }
  253. pr_debug("Ethernet header insertion set\n");
  254. result = ecm_ipa_register_properties();
  255. if (result) {
  256. ECM_IPA_ERROR("fail on properties set\n");
  257. goto fail_register_tx;
  258. }
  259. pr_debug("ecm_ipa 2 Tx and 2 Rx properties were registered\n");
  260. netif_carrier_off(net);
  261. pr_debug("set carrier off\n");
  262. result = register_netdev(net);
  263. if (result) {
  264. ECM_IPA_ERROR("register_netdev failed: %d\n", result);
  265. goto fail_register_netdev;
  266. }
  267. pr_debug("register_netdev succeeded\n");
  268. params->ecm_ipa_rx_dp_notify = ecm_ipa_packet_receive_notify;
  269. params->ecm_ipa_tx_dp_notify = ecm_ipa_tx_complete_notify;
  270. params->private = (void *)ecm_ipa_ctx;
  271. params->skip_ep_cfg = false;
  272. ecm_ipa_ctx->state = ECM_IPA_INITIALIZED;
  273. ECM_IPA_STATE_DEBUG(ecm_ipa_ctx);
  274. ECM_IPA_LOG_EXIT();
  275. return 0;
  276. fail_register_netdev:
  277. ecm_ipa_deregister_properties();
  278. fail_register_tx:
  279. ecm_ipa_rules_destroy(ecm_ipa_ctx);
  280. fail_set_device_ethernet:
  281. fail_rules_cfg:
  282. ecm_ipa_destory_rm_resource(ecm_ipa_ctx);
  283. fail_create_rm:
  284. ecm_ipa_debugfs_destroy(ecm_ipa_ctx);
  285. fail_debugfs:
  286. free_netdev(net);
  287. fail_alloc_etherdev:
  288. return result;
  289. }
  290. EXPORT_SYMBOL(ecm_ipa_init);
  291. /**
  292. * ecm_ipa_connect() - notify ecm_ipa for IPA<->USB pipes connection
  293. * @usb_to_ipa_hdl: handle of IPA driver client for USB->IPA
  294. * @ipa_to_usb_hdl: handle of IPA driver client for IPA->USB
  295. * @priv: same value that was set by ecm_ipa_init(), this
  296. * parameter holds the network device pointer.
  297. *
  298. * Once USB driver finishes the pipe connection between IPA core
  299. * and USB core this method shall be called in order to
  300. * allow ecm_ipa complete the data path configurations.
  301. * Detailed description:
  302. * - configure the IPA end-points register
  303. * - notify the Linux kernel for "carrier_on"
  304. * After this function is done the driver state changes to "Connected".
  305. * This API is expected to be called after ecm_ipa_init() or
  306. * after a call to ecm_ipa_disconnect.
  307. */
  308. int ecm_ipa_connect(u32 usb_to_ipa_hdl, u32 ipa_to_usb_hdl,
  309. void *priv)
  310. {
  311. struct ecm_ipa_dev *ecm_ipa_ctx = priv;
  312. int next_state;
  313. ECM_IPA_LOG_ENTRY();
  314. NULL_CHECK(priv);
  315. pr_debug("usb_to_ipa_hdl = %d, ipa_to_usb_hdl = %d, priv=0x%p\n",
  316. usb_to_ipa_hdl, ipa_to_usb_hdl, priv);
  317. next_state = ecm_ipa_next_state(ecm_ipa_ctx->state, ECM_IPA_CONNECT);
  318. if (next_state == ECM_IPA_INVALID) {
  319. ECM_IPA_ERROR("can't call connect before calling initialize\n");
  320. return -EPERM;
  321. }
  322. ecm_ipa_ctx->state = next_state;
  323. ECM_IPA_STATE_DEBUG(ecm_ipa_ctx);
  324. if (!usb_to_ipa_hdl || usb_to_ipa_hdl >= IPA_CLIENT_MAX) {
  325. ECM_IPA_ERROR("usb_to_ipa_hdl(%d) is not a valid ipa handle\n",
  326. usb_to_ipa_hdl);
  327. return -EINVAL;
  328. }
  329. if (!ipa_to_usb_hdl || ipa_to_usb_hdl >= IPA_CLIENT_MAX) {
  330. ECM_IPA_ERROR("ipa_to_usb_hdl(%d) is not a valid ipa handle\n",
  331. ipa_to_usb_hdl);
  332. return -EINVAL;
  333. }
  334. ecm_ipa_ctx->ipa_to_usb_hdl = ipa_to_usb_hdl;
  335. ecm_ipa_ctx->usb_to_ipa_hdl = usb_to_ipa_hdl;
  336. ecm_ipa_ep_registers_cfg(usb_to_ipa_hdl, ipa_to_usb_hdl);
  337. pr_debug("end-point configured\n");
  338. netif_carrier_on(ecm_ipa_ctx->net);
  339. if (!netif_carrier_ok(ecm_ipa_ctx->net)) {
  340. ECM_IPA_ERROR("netif_carrier_ok error\n");
  341. return -EBUSY;
  342. }
  343. pr_debug("carrier_on notified, ecm_ipa is operational\n");
  344. if (ecm_ipa_ctx->state == ECM_IPA_CONNECTED_AND_UP) {
  345. netif_start_queue(ecm_ipa_ctx->net);
  346. pr_debug("queue started\n");
  347. }
  348. ECM_IPA_LOG_EXIT();
  349. return 0;
  350. }
  351. EXPORT_SYMBOL(ecm_ipa_connect);
  352. /**
  353. * ecm_ipa_open() - notify Linux network stack to start sending packets
  354. * @net: the network interface supplied by the network stack
  355. *
  356. * Linux uses this API to notify the driver that the network interface
  357. * transitions to the up state.
  358. * The driver will instruct the Linux network stack to start
  359. * delivering data packets.
  360. */
  361. static int ecm_ipa_open(struct net_device *net)
  362. {
  363. struct ecm_ipa_dev *ecm_ipa_ctx;
  364. int next_state;
  365. ECM_IPA_LOG_ENTRY();
  366. ecm_ipa_ctx = netdev_priv(net);
  367. next_state = ecm_ipa_next_state(ecm_ipa_ctx->state, ECM_IPA_OPEN);
  368. if (next_state == ECM_IPA_INVALID) {
  369. ECM_IPA_ERROR("can't bring driver up before initialize\n");
  370. return -EPERM;
  371. }
  372. ecm_ipa_ctx->state = next_state;
  373. ECM_IPA_STATE_DEBUG(ecm_ipa_ctx);
  374. if (ecm_ipa_ctx->state == ECM_IPA_CONNECTED_AND_UP) {
  375. netif_start_queue(net);
  376. pr_debug("queue started\n");
  377. } else {
  378. pr_debug("queue was not started due to meta-stabilie state\n");
  379. }
  380. ECM_IPA_LOG_EXIT();
  381. return 0;
  382. }
  383. /**
  384. * ecm_ipa_start_xmit() - send data from APPs to USB core via IPA core
  385. * @skb: packet received from Linux network stack
  386. * @net: the network device being used to send this packet
  387. *
  388. * Several conditions needed in order to send the packet to IPA:
  389. * - Transmit queue for the network driver is currently
  390. * in "send" state
  391. * - The driver internal state is in "UP" state.
  392. * - Filter Tx switch is turned off
  393. * - The IPA resource manager state for the driver producer client
  394. * is "Granted" which implies that all the resources in the dependency
  395. * graph are valid for data flow.
  396. * - outstanding high boundary did not reach.
  397. *
  398. * In case all of the above conditions are met, the network driver will
  399. * send the packet by using the IPA API for Tx.
  400. * In case the outstanding packet high boundary is reached, the driver will
  401. * stop the send queue until enough packet were proceeded by the IPA core.
  402. */
  403. static netdev_tx_t ecm_ipa_start_xmit(struct sk_buff *skb,
  404. struct net_device *net)
  405. {
  406. int ret;
  407. netdev_tx_t status = NETDEV_TX_BUSY;
  408. struct ecm_ipa_dev *ecm_ipa_ctx = netdev_priv(net);
  409. if (unlikely(netif_queue_stopped(net))) {
  410. ECM_IPA_ERROR("interface queue is stopped\n");
  411. goto out;
  412. }
  413. if (unlikely(ecm_ipa_ctx->state != ECM_IPA_CONNECTED_AND_UP)) {
  414. ECM_IPA_ERROR("Missing pipe connected and/or iface up\n");
  415. return -NETDEV_TX_BUSY;
  416. }
  417. if (unlikely(tx_filter(skb))) {
  418. dev_kfree_skb_any(skb);
  419. pr_debug("packet got filtered out on Tx path\n");
  420. status = NETDEV_TX_OK;
  421. goto out;
  422. }
  423. ret = resource_request(ecm_ipa_ctx);
  424. if (ret) {
  425. pr_debug("Waiting to resource\n");
  426. netif_stop_queue(net);
  427. goto resource_busy;
  428. }
  429. if (atomic_read(&ecm_ipa_ctx->outstanding_pkts) >=
  430. ecm_ipa_ctx->outstanding_high) {
  431. pr_debug("Outstanding high boundary reached (%d)- stopping queue\n",
  432. ecm_ipa_ctx->outstanding_high);
  433. netif_stop_queue(net);
  434. status = -NETDEV_TX_BUSY;
  435. goto out;
  436. }
  437. ret = ipa_tx_dp(IPA_TO_USB_CLIENT, skb, NULL);
  438. if (ret) {
  439. ECM_IPA_ERROR("ipa transmit failed (%d)\n", ret);
  440. goto fail_tx_packet;
  441. }
  442. atomic_inc(&ecm_ipa_ctx->outstanding_pkts);
  443. net->stats.tx_packets++;
  444. net->stats.tx_bytes += skb->len;
  445. status = NETDEV_TX_OK;
  446. goto out;
  447. fail_tx_packet:
  448. out:
  449. resource_release(ecm_ipa_ctx);
  450. resource_busy:
  451. return status;
  452. }
  453. /**
  454. * ecm_ipa_packet_receive_notify() - Rx notify
  455. *
  456. * @priv: ecm driver context
  457. * @evt: event type
  458. * @data: data provided with event
  459. *
  460. * IPA will pass a packet to the Linux network stack with skb->data pointing
  461. * to Ethernet packet frame.
  462. */
  463. static void ecm_ipa_packet_receive_notify(void *priv,
  464. enum ipa_dp_evt_type evt,
  465. unsigned long data)
  466. {
  467. struct sk_buff *skb = (struct sk_buff *)data;
  468. struct ecm_ipa_dev *ecm_ipa_ctx = priv;
  469. int result;
  470. if (unlikely(ecm_ipa_ctx->state != ECM_IPA_CONNECTED_AND_UP)) {
  471. ECM_IPA_ERROR("Missing pipe connected and/or iface up\n");
  472. return;
  473. }
  474. if (evt != IPA_RECEIVE) {
  475. ECM_IPA_ERROR("A none IPA_RECEIVE event in ecm_ipa_receive\n");
  476. return;
  477. }
  478. skb->dev = ecm_ipa_ctx->net;
  479. skb->protocol = eth_type_trans(skb, ecm_ipa_ctx->net);
  480. if (rx_filter(skb)) {
  481. pr_debug("packet got filtered out on Rx path\n");
  482. dev_kfree_skb_any(skb);
  483. return;
  484. }
  485. result = netif_rx(skb);
  486. if (result)
  487. ECM_IPA_ERROR("fail on netif_rx\n");
  488. ecm_ipa_ctx->net->stats.rx_packets++;
  489. ecm_ipa_ctx->net->stats.rx_bytes += skb->len;
  490. return;
  491. }
  492. /** ecm_ipa_stop() - called when network device transitions to the down
  493. * state.
  494. * @net: the network device being stopped.
  495. *
  496. * This API is used by Linux network stack to notify the network driver that
  497. * its state was changed to "down"
  498. * The driver will stop the "send" queue and change its internal
  499. * state to "Connected".
  500. */
  501. static int ecm_ipa_stop(struct net_device *net)
  502. {
  503. struct ecm_ipa_dev *ecm_ipa_ctx = netdev_priv(net);
  504. int next_state;
  505. ECM_IPA_LOG_ENTRY();
  506. next_state = ecm_ipa_next_state(ecm_ipa_ctx->state, ECM_IPA_STOP);
  507. if (next_state == ECM_IPA_INVALID) {
  508. ECM_IPA_ERROR("can't do network interface down without up\n");
  509. return -EPERM;
  510. }
  511. ecm_ipa_ctx->state = next_state;
  512. ECM_IPA_STATE_DEBUG(ecm_ipa_ctx);
  513. netif_stop_queue(net);
  514. pr_debug("network device stopped\n");
  515. ECM_IPA_LOG_EXIT();
  516. return 0;
  517. }
  518. /** ecm_ipa_disconnect() - called when the USB cable is unplugged.
  519. * @priv: same value that was set by ecm_ipa_init(), this
  520. * parameter holds the network device pointer.
  521. *
  522. * Once the USB cable is unplugged the USB driver will notify the network
  523. * interface driver.
  524. * The internal driver state will returned to its initialized state and
  525. * Linux network stack will be informed for carrier off and the send queue
  526. * will be stopped.
  527. */
  528. int ecm_ipa_disconnect(void *priv)
  529. {
  530. struct ecm_ipa_dev *ecm_ipa_ctx = priv;
  531. int next_state;
  532. ECM_IPA_LOG_ENTRY();
  533. NULL_CHECK(ecm_ipa_ctx);
  534. pr_debug("priv=0x%p\n", priv);
  535. next_state = ecm_ipa_next_state(ecm_ipa_ctx->state, ECM_IPA_DISCONNECT);
  536. if (next_state == ECM_IPA_INVALID) {
  537. ECM_IPA_ERROR("can't disconnect before connect\n");
  538. return -EPERM;
  539. }
  540. ecm_ipa_ctx->state = next_state;
  541. ECM_IPA_STATE_DEBUG(ecm_ipa_ctx);
  542. netif_carrier_off(ecm_ipa_ctx->net);
  543. pr_debug("carrier_off notifcation was sent\n");
  544. netif_stop_queue(ecm_ipa_ctx->net);
  545. pr_debug("queue stopped\n");
  546. ECM_IPA_LOG_EXIT();
  547. return 0;
  548. }
  549. EXPORT_SYMBOL(ecm_ipa_disconnect);
  550. /**
  551. * ecm_ipa_cleanup() - unregister the network interface driver and free
  552. * internal data structs.
  553. * @priv: same value that was set by ecm_ipa_init(), this
  554. * parameter holds the network device pointer.
  555. *
  556. * This function shall be called once the network interface is not
  557. * needed anymore, e.g: when the USB composition does not support ECM.
  558. * This function shall be called after the pipes were disconnected.
  559. * Detailed description:
  560. * - delete the driver dependency defined for IPA resource manager and
  561. * destroy the producer resource.
  562. * - remove the debugfs entries
  563. * - deregister the network interface from Linux network stack
  564. * - free all internal data structs
  565. */
  566. void ecm_ipa_cleanup(void *priv)
  567. {
  568. struct ecm_ipa_dev *ecm_ipa_ctx = priv;
  569. int next_state;
  570. ECM_IPA_LOG_ENTRY();
  571. pr_debug("priv=0x%p\n", priv);
  572. if (!ecm_ipa_ctx) {
  573. ECM_IPA_ERROR("ecm_ipa_ctx NULL pointer\n");
  574. return;
  575. }
  576. next_state = ecm_ipa_next_state(ecm_ipa_ctx->state, ECM_IPA_CLEANUP);
  577. if (next_state == ECM_IPA_INVALID) {
  578. ECM_IPA_ERROR("can't clean driver without cable disconnect\n");
  579. return;
  580. }
  581. ecm_ipa_ctx->state = next_state;
  582. ECM_IPA_STATE_DEBUG(ecm_ipa_ctx);
  583. ecm_ipa_destory_rm_resource(ecm_ipa_ctx);
  584. ecm_ipa_debugfs_destroy(ecm_ipa_ctx);
  585. unregister_netdev(ecm_ipa_ctx->net);
  586. free_netdev(ecm_ipa_ctx->net);
  587. pr_debug("cleanup done\n");
  588. ECM_IPA_LOG_EXIT();
  589. return ;
  590. }
  591. EXPORT_SYMBOL(ecm_ipa_cleanup);
  592. /**
  593. * ecm_ipa_rules_cfg() - set header insertion and register Tx/Rx properties
  594. * Headers will be commited to HW
  595. * @ecm_ipa_ctx: main driver context parameters
  596. * @dst_mac: destination MAC address
  597. * @src_mac: source MAC address
  598. *
  599. * Returns negative errno, or zero on success
  600. */
  601. static int ecm_ipa_rules_cfg(struct ecm_ipa_dev *ecm_ipa_ctx,
  602. const void *dst_mac, const void *src_mac)
  603. {
  604. struct ipa_ioc_add_hdr *hdrs;
  605. struct ipa_hdr_add *ipv4_hdr;
  606. struct ipa_hdr_add *ipv6_hdr;
  607. struct ethhdr *eth_ipv4;
  608. struct ethhdr *eth_ipv6;
  609. int result = 0;
  610. ECM_IPA_LOG_ENTRY();
  611. hdrs = kzalloc(sizeof(*hdrs) + sizeof(*ipv4_hdr) + sizeof(*ipv6_hdr),
  612. GFP_KERNEL);
  613. if (!hdrs) {
  614. result = -ENOMEM;
  615. goto out;
  616. }
  617. ipv4_hdr = &hdrs->hdr[0];
  618. eth_ipv4 = (struct ethhdr *)ipv4_hdr->hdr;
  619. ipv6_hdr = &hdrs->hdr[1];
  620. eth_ipv6 = (struct ethhdr *)ipv6_hdr->hdr;
  621. strlcpy(ipv4_hdr->name, ECM_IPA_IPV4_HDR_NAME, IPA_RESOURCE_NAME_MAX);
  622. memcpy(eth_ipv4->h_dest, dst_mac, ETH_ALEN);
  623. memcpy(eth_ipv4->h_source, src_mac, ETH_ALEN);
  624. eth_ipv4->h_proto = htons(ETH_P_IP);
  625. ipv4_hdr->hdr_len = ETH_HLEN;
  626. ipv4_hdr->is_partial = 0;
  627. strlcpy(ipv6_hdr->name, ECM_IPA_IPV6_HDR_NAME, IPA_RESOURCE_NAME_MAX);
  628. memcpy(eth_ipv6->h_dest, dst_mac, ETH_ALEN);
  629. memcpy(eth_ipv6->h_source, src_mac, ETH_ALEN);
  630. eth_ipv6->h_proto = htons(ETH_P_IPV6);
  631. ipv6_hdr->hdr_len = ETH_HLEN;
  632. ipv6_hdr->is_partial = 0;
  633. hdrs->commit = 1;
  634. hdrs->num_hdrs = 2;
  635. result = ipa_add_hdr(hdrs);
  636. if (result) {
  637. ECM_IPA_ERROR("Fail on Header-Insertion(%d)\n", result);
  638. goto out_free_mem;
  639. }
  640. if (ipv4_hdr->status) {
  641. ECM_IPA_ERROR("Fail on Header-Insertion ipv4(%d)\n",
  642. ipv4_hdr->status);
  643. result = ipv4_hdr->status;
  644. goto out_free_mem;
  645. }
  646. if (ipv6_hdr->status) {
  647. ECM_IPA_ERROR("Fail on Header-Insertion ipv6(%d)\n",
  648. ipv6_hdr->status);
  649. result = ipv6_hdr->status;
  650. goto out_free_mem;
  651. }
  652. ecm_ipa_ctx->eth_ipv4_hdr_hdl = ipv4_hdr->hdr_hdl;
  653. ecm_ipa_ctx->eth_ipv6_hdr_hdl = ipv6_hdr->hdr_hdl;
  654. ECM_IPA_LOG_EXIT();
  655. out_free_mem:
  656. kfree(hdrs);
  657. out:
  658. return result;
  659. }
  660. /**
  661. * ecm_ipa_rules_destroy() - remove the IPA core configuration done for
  662. * the driver data path.
  663. * @ecm_ipa_ctx: the driver context
  664. *
  665. * Revert the work done on ecm_ipa_rules_cfg.
  666. */
  667. static void ecm_ipa_rules_destroy(struct ecm_ipa_dev *ecm_ipa_ctx)
  668. {
  669. struct ipa_ioc_del_hdr *del_hdr;
  670. struct ipa_hdr_del *ipv4;
  671. struct ipa_hdr_del *ipv6;
  672. int result;
  673. del_hdr = kzalloc(sizeof(*del_hdr) + sizeof(*ipv4) +
  674. sizeof(*ipv6), GFP_KERNEL);
  675. if (!del_hdr)
  676. return;
  677. del_hdr->commit = 1;
  678. del_hdr->num_hdls = 2;
  679. ipv4 = &del_hdr->hdl[0];
  680. ipv4->hdl = ecm_ipa_ctx->eth_ipv4_hdr_hdl;
  681. ipv6 = &del_hdr->hdl[1];
  682. ipv6->hdl = ecm_ipa_ctx->eth_ipv6_hdr_hdl;
  683. result = ipa_del_hdr(del_hdr);
  684. if (result || ipv4->status || ipv6->status)
  685. ECM_IPA_ERROR("ipa_del_hdr failed");
  686. }
  687. /* ecm_ipa_register_properties() - set Tx/Rx properties for ipacm
  688. *
  689. * Register ecm0 interface with 2 Tx properties and 2 Rx properties:
  690. * The 2 Tx properties are for data flowing from IPA to USB, they
  691. * have Header-Insertion properties both for Ipv4 and Ipv6 Ethernet framing.
  692. * The 2 Rx properties are for data flowing from USB to IPA, they have
  693. * simple rule which always "hit".
  694. *
  695. */
  696. static int ecm_ipa_register_properties(void)
  697. {
  698. struct ipa_tx_intf tx_properties = {0};
  699. struct ipa_ioc_tx_intf_prop properties[2] = { {0}, {0} };
  700. struct ipa_ioc_tx_intf_prop *ipv4_property;
  701. struct ipa_ioc_tx_intf_prop *ipv6_property;
  702. struct ipa_ioc_rx_intf_prop rx_ioc_properties[2] = { {0}, {0} };
  703. struct ipa_rx_intf rx_properties = {0};
  704. struct ipa_ioc_rx_intf_prop *rx_ipv4_property;
  705. struct ipa_ioc_rx_intf_prop *rx_ipv6_property;
  706. int result = 0;
  707. ECM_IPA_LOG_ENTRY();
  708. tx_properties.prop = properties;
  709. ipv4_property = &tx_properties.prop[0];
  710. ipv4_property->ip = IPA_IP_v4;
  711. ipv4_property->dst_pipe = IPA_TO_USB_CLIENT;
  712. strlcpy(ipv4_property->hdr_name, ECM_IPA_IPV4_HDR_NAME,
  713. IPA_RESOURCE_NAME_MAX);
  714. ipv6_property = &tx_properties.prop[1];
  715. ipv6_property->ip = IPA_IP_v6;
  716. ipv6_property->dst_pipe = IPA_TO_USB_CLIENT;
  717. strlcpy(ipv6_property->hdr_name, ECM_IPA_IPV6_HDR_NAME,
  718. IPA_RESOURCE_NAME_MAX);
  719. tx_properties.num_props = 2;
  720. rx_properties.prop = rx_ioc_properties;
  721. rx_ipv4_property = &rx_properties.prop[0];
  722. rx_ipv4_property->ip = IPA_IP_v4;
  723. rx_ipv4_property->attrib.attrib_mask = 0;
  724. rx_ipv4_property->src_pipe = IPA_CLIENT_USB_PROD;
  725. rx_ipv6_property = &rx_properties.prop[1];
  726. rx_ipv6_property->ip = IPA_IP_v6;
  727. rx_ipv6_property->attrib.attrib_mask = 0;
  728. rx_ipv6_property->src_pipe = IPA_CLIENT_USB_PROD;
  729. rx_properties.num_props = 2;
  730. result = ipa_register_intf("ecm0", &tx_properties, &rx_properties);
  731. if (result)
  732. ECM_IPA_ERROR("fail on Tx/Rx properties registration\n");
  733. ECM_IPA_LOG_EXIT();
  734. return result;
  735. }
  736. static void ecm_ipa_deregister_properties(void)
  737. {
  738. int result;
  739. ECM_IPA_LOG_ENTRY();
  740. result = ipa_deregister_intf("ecm0");
  741. if (result)
  742. pr_debug("Fail on Tx prop deregister\n");
  743. ECM_IPA_LOG_EXIT();
  744. return;
  745. }
  746. /**
  747. * ecm_ipa_configure() - make IPA core end-point specific configuration
  748. * @usb_to_ipa_hdl: handle of usb_to_ipa end-point for IPA driver
  749. * @ipa_to_usb_hdl: handle of ipa_to_usb end-point for IPA driver
  750. * @host_ethaddr: host Ethernet address in network order
  751. * @device_ethaddr: device Ethernet address in network order
  752. *
  753. * Configure the usb_to_ipa and ipa_to_usb end-point registers
  754. * - USB->IPA end-point: disable de-aggregation, enable link layer
  755. * header removal (Ethernet removal), source NATing and default routing.
  756. * - IPA->USB end-point: disable aggregation, add link layer header (Ethernet)
  757. * - allocate Ethernet device
  758. * - register to Linux network stack
  759. *
  760. * Returns negative errno, or zero on success
  761. */
  762. static void ecm_ipa_rm_notify(void *user_data, enum ipa_rm_event event,
  763. unsigned long data)
  764. {
  765. struct ecm_ipa_dev *ecm_ipa_ctx = user_data;
  766. ECM_IPA_LOG_ENTRY();
  767. if (event == IPA_RM_RESOURCE_GRANTED &&
  768. netif_queue_stopped(ecm_ipa_ctx->net)) {
  769. pr_debug("Resource Granted - waking queue\n");
  770. netif_wake_queue(ecm_ipa_ctx->net);
  771. } else {
  772. pr_debug("Resource released\n");
  773. }
  774. ECM_IPA_LOG_EXIT();
  775. }
  776. static int ecm_ipa_create_rm_resource(struct ecm_ipa_dev *ecm_ipa_ctx)
  777. {
  778. struct ipa_rm_create_params create_params = {0};
  779. int result;
  780. ECM_IPA_LOG_ENTRY();
  781. create_params.name = IPA_RM_RESOURCE_STD_ECM_PROD;
  782. create_params.reg_params.user_data = ecm_ipa_ctx;
  783. create_params.reg_params.notify_cb = ecm_ipa_rm_notify;
  784. result = ipa_rm_create_resource(&create_params);
  785. if (result) {
  786. ECM_IPA_ERROR("Fail on ipa_rm_create_resource\n");
  787. goto fail_rm_create;
  788. }
  789. pr_debug("rm client was created");
  790. result = ipa_rm_inactivity_timer_init(IPA_RM_RESOURCE_STD_ECM_PROD,
  791. INACTIVITY_MSEC_DELAY);
  792. if (result) {
  793. ECM_IPA_ERROR("Fail on ipa_rm_inactivity_timer_init\n");
  794. goto fail_it;
  795. }
  796. pr_debug("rm_it client was created");
  797. result = ipa_rm_add_dependency(IPA_RM_RESOURCE_STD_ECM_PROD,
  798. IPA_RM_RESOURCE_USB_CONS);
  799. if (result)
  800. ECM_IPA_ERROR("unable to add dependency (%d)\n", result);
  801. pr_debug("rm dependency was set\n");
  802. ECM_IPA_LOG_EXIT();
  803. return 0;
  804. fail_it:
  805. fail_rm_create:
  806. return result;
  807. }
  808. static void ecm_ipa_destory_rm_resource(struct ecm_ipa_dev *ecm_ipa_ctx)
  809. {
  810. int result;
  811. ECM_IPA_LOG_ENTRY();
  812. ipa_rm_delete_dependency(IPA_RM_RESOURCE_STD_ECM_PROD,
  813. IPA_RM_RESOURCE_USB_CONS);
  814. ipa_rm_inactivity_timer_destroy(IPA_RM_RESOURCE_STD_ECM_PROD);
  815. result = ipa_rm_delete_resource(IPA_RM_RESOURCE_STD_ECM_PROD);
  816. if (result)
  817. ECM_IPA_ERROR("resource deletion failed\n");
  818. ECM_IPA_LOG_EXIT();
  819. }
  820. static bool rx_filter(struct sk_buff *skb)
  821. {
  822. struct ecm_ipa_dev *ecm_ipa_ctx = netdev_priv(skb->dev);
  823. return !ecm_ipa_ctx->rx_enable;
  824. }
  825. static bool tx_filter(struct sk_buff *skb)
  826. {
  827. struct ecm_ipa_dev *ecm_ipa_ctx = netdev_priv(skb->dev);
  828. return !ecm_ipa_ctx->tx_enable;
  829. }
  830. static bool rm_enabled(struct ecm_ipa_dev *ecm_ipa_ctx)
  831. {
  832. return ecm_ipa_ctx->rm_enable;
  833. }
  834. static int resource_request(struct ecm_ipa_dev *ecm_ipa_ctx)
  835. {
  836. int result = 0;
  837. if (!rm_enabled(ecm_ipa_ctx))
  838. goto out;
  839. result = ipa_rm_inactivity_timer_request_resource(
  840. IPA_RM_RESOURCE_STD_ECM_PROD);
  841. out:
  842. return result;
  843. }
  844. static void resource_release(struct ecm_ipa_dev *ecm_ipa_ctx)
  845. {
  846. if (!rm_enabled(ecm_ipa_ctx))
  847. goto out;
  848. ipa_rm_inactivity_timer_release_resource(IPA_RM_RESOURCE_STD_ECM_PROD);
  849. out:
  850. return;
  851. }
  852. /**
  853. * ecm_ipa_tx_complete_notify() - Rx notify
  854. *
  855. * @priv: ecm driver context
  856. * @evt: event type
  857. * @data: data provided with event
  858. *
  859. * Check that the packet is the one we sent and release it
  860. * This function will be called in defered context in IPA wq.
  861. */
  862. static void ecm_ipa_tx_complete_notify(void *priv,
  863. enum ipa_dp_evt_type evt,
  864. unsigned long data)
  865. {
  866. struct sk_buff *skb = (struct sk_buff *)data;
  867. struct ecm_ipa_dev *ecm_ipa_ctx = priv;
  868. if (!ecm_ipa_ctx) {
  869. ECM_IPA_ERROR("ecm_ipa_ctx is NULL pointer\n");
  870. return;
  871. }
  872. if (evt != IPA_WRITE_DONE) {
  873. ECM_IPA_ERROR("unsupported event on Tx callback\n");
  874. return;
  875. }
  876. atomic_dec(&ecm_ipa_ctx->outstanding_pkts);
  877. if (netif_queue_stopped(ecm_ipa_ctx->net) &&
  878. atomic_read(&ecm_ipa_ctx->outstanding_pkts) <
  879. (ecm_ipa_ctx->outstanding_low)) {
  880. pr_debug("Outstanding low boundary reached (%d) - waking up queue\n",
  881. ecm_ipa_ctx->outstanding_low);
  882. netif_wake_queue(ecm_ipa_ctx->net);
  883. }
  884. dev_kfree_skb_any(skb);
  885. return;
  886. }
  887. static int ecm_ipa_debugfs_atomic_open(struct inode *inode, struct file *file)
  888. {
  889. struct ecm_ipa_dev *ecm_ipa_ctx = inode->i_private;
  890. ECM_IPA_LOG_ENTRY();
  891. file->private_data = &(ecm_ipa_ctx->outstanding_pkts);
  892. ECM_IPA_LOG_EXIT();
  893. return 0;
  894. }
  895. static ssize_t ecm_ipa_debugfs_enable_write_dma(struct file *file,
  896. const char __user *buf, size_t count, loff_t *ppos)
  897. {
  898. struct ecm_ipa_dev *ecm_ipa_ctx = file->private_data;
  899. int result;
  900. ECM_IPA_LOG_ENTRY();
  901. file->private_data = &ecm_ipa_ctx->dma_enable;
  902. result = ecm_ipa_debugfs_enable_write(file, buf, count, ppos);
  903. if (ecm_ipa_ctx->dma_enable)
  904. ecm_ipa_ep_registers_dma_cfg(ecm_ipa_ctx->usb_to_ipa_hdl);
  905. else
  906. ecm_ipa_ep_registers_cfg(ecm_ipa_ctx->usb_to_ipa_hdl,
  907. ecm_ipa_ctx->usb_to_ipa_hdl);
  908. ECM_IPA_LOG_EXIT();
  909. return result;
  910. }
  911. static int ecm_ipa_debugfs_dma_open(struct inode *inode, struct file *file)
  912. {
  913. struct ecm_ipa_dev *ecm_ipa_ctx = inode->i_private;
  914. ECM_IPA_LOG_ENTRY();
  915. file->private_data = ecm_ipa_ctx;
  916. ECM_IPA_LOG_EXIT();
  917. return 0;
  918. }
  919. static ssize_t ecm_ipa_debugfs_enable_write(struct file *file,
  920. const char __user *buf, size_t count, loff_t *ppos)
  921. {
  922. unsigned long missing;
  923. char input;
  924. bool *enable = file->private_data;
  925. if (count != sizeof(input) + 1) {
  926. ECM_IPA_ERROR("wrong input length(%zd)\n", count);
  927. return -EINVAL;
  928. }
  929. if (!buf) {
  930. ECM_IPA_ERROR("Bad argument\n");
  931. return -EINVAL;
  932. }
  933. missing = copy_from_user(&input, buf, 1);
  934. if (missing)
  935. return -EFAULT;
  936. pr_debug("input received %c\n", input);
  937. *enable = input - '0';
  938. pr_debug("value was set to %d\n", *enable);
  939. return count;
  940. }
  941. static ssize_t ecm_ipa_debugfs_enable_read(struct file *file,
  942. char __user *ubuf, size_t count, loff_t *ppos)
  943. {
  944. int nbytes;
  945. int size = 0;
  946. int ret;
  947. loff_t pos;
  948. u8 enable_str[sizeof(char)*3] = {0};
  949. bool *enable = file->private_data;
  950. pos = *ppos;
  951. nbytes = scnprintf(enable_str, sizeof(enable_str), "%d\n", *enable);
  952. ret = simple_read_from_buffer(ubuf, count, ppos, enable_str, nbytes);
  953. if (ret < 0) {
  954. ECM_IPA_ERROR("simple_read_from_buffer problem");
  955. return ret;
  956. }
  957. size += ret;
  958. count -= nbytes;
  959. *ppos = pos + size;
  960. return size;
  961. }
  962. static ssize_t ecm_ipa_debugfs_atomic_read(struct file *file,
  963. char __user *ubuf, size_t count, loff_t *ppos)
  964. {
  965. int nbytes;
  966. u8 atomic_str[DEBUGFS_TEMP_BUF_SIZE] = {0};
  967. atomic_t *atomic_var = file->private_data;
  968. nbytes = scnprintf(atomic_str, sizeof(atomic_str), "%d\n",
  969. atomic_read(atomic_var));
  970. return simple_read_from_buffer(ubuf, count, ppos, atomic_str, nbytes);
  971. }
  972. static int ecm_ipa_debugfs_init(struct ecm_ipa_dev *ecm_ipa_ctx)
  973. {
  974. const mode_t flags_read_write = S_IRUGO | S_IWUGO;
  975. const mode_t flags_read_only = S_IRUGO;
  976. struct dentry *file;
  977. ECM_IPA_LOG_ENTRY();
  978. if (!ecm_ipa_ctx)
  979. return -EINVAL;
  980. ecm_ipa_ctx->directory = debugfs_create_dir("ecm_ipa", NULL);
  981. if (!ecm_ipa_ctx->directory) {
  982. ECM_IPA_ERROR("could not create debugfs directory entry\n");
  983. goto fail_directory;
  984. }
  985. file = debugfs_create_bool("tx_enable", flags_read_write,
  986. ecm_ipa_ctx->directory, &ecm_ipa_ctx->tx_enable);
  987. if (!file) {
  988. ECM_IPA_ERROR("could not create debugfs tx file\n");
  989. goto fail_file;
  990. }
  991. file = debugfs_create_bool("rx_enable", flags_read_write,
  992. ecm_ipa_ctx->directory, &ecm_ipa_ctx->rx_enable);
  993. if (!file) {
  994. ECM_IPA_ERROR("could not create debugfs rx file\n");
  995. goto fail_file;
  996. }
  997. file = debugfs_create_bool("rm_enable", flags_read_write,
  998. ecm_ipa_ctx->directory, &ecm_ipa_ctx->rm_enable);
  999. if (!file) {
  1000. ECM_IPA_ERROR("could not create debugfs rm file\n");
  1001. goto fail_file;
  1002. }
  1003. file = debugfs_create_u8("outstanding_high", flags_read_write,
  1004. ecm_ipa_ctx->directory, &ecm_ipa_ctx->outstanding_high);
  1005. if (!file) {
  1006. ECM_IPA_ERROR("could not create outstanding_high file\n");
  1007. goto fail_file;
  1008. }
  1009. file = debugfs_create_u8("outstanding_low", flags_read_write,
  1010. ecm_ipa_ctx->directory, &ecm_ipa_ctx->outstanding_low);
  1011. if (!file) {
  1012. ECM_IPA_ERROR("could not create outstanding_low file\n");
  1013. goto fail_file;
  1014. }
  1015. file = debugfs_create_file("dma_enable", flags_read_write,
  1016. ecm_ipa_ctx->directory,
  1017. ecm_ipa_ctx, &ecm_ipa_debugfs_dma_ops);
  1018. if (!file) {
  1019. ECM_IPA_ERROR("could not create debugfs dma file\n");
  1020. goto fail_file;
  1021. }
  1022. file = debugfs_create_file("outstanding", flags_read_only,
  1023. ecm_ipa_ctx->directory,
  1024. ecm_ipa_ctx, &ecm_ipa_debugfs_atomic_ops);
  1025. if (!file) {
  1026. ECM_IPA_ERROR("could not create outstanding file\n");
  1027. goto fail_file;
  1028. }
  1029. ECM_IPA_LOG_EXIT();
  1030. return 0;
  1031. fail_file:
  1032. debugfs_remove_recursive(ecm_ipa_ctx->directory);
  1033. fail_directory:
  1034. return -EFAULT;
  1035. }
  1036. static void ecm_ipa_debugfs_destroy(struct ecm_ipa_dev *ecm_ipa_ctx)
  1037. {
  1038. debugfs_remove_recursive(ecm_ipa_ctx->directory);
  1039. }
  1040. /**
  1041. * ecm_ipa_ep_cfg() - configure the USB endpoints for ECM
  1042. *
  1043. *usb_to_ipa_hdl: handle received from ipa_connect
  1044. *ipa_to_usb_hdl: handle received from ipa_connect
  1045. *
  1046. * USB to IPA pipe:
  1047. * - No de-aggregation
  1048. * - Remove Ethernet header
  1049. * - SRC NAT
  1050. * - Default routing(0)
  1051. * IPA to USB Pipe:
  1052. * - No aggregation
  1053. * - Add Ethernet header
  1054. */
  1055. static int ecm_ipa_ep_registers_cfg(u32 usb_to_ipa_hdl, u32 ipa_to_usb_hdl)
  1056. {
  1057. int result = 0;
  1058. struct ipa_ep_cfg usb_to_ipa_ep_cfg;
  1059. struct ipa_ep_cfg ipa_to_usb_ep_cfg;
  1060. ECM_IPA_LOG_ENTRY();
  1061. memset(&usb_to_ipa_ep_cfg, 0 , sizeof(struct ipa_ep_cfg));
  1062. usb_to_ipa_ep_cfg.aggr.aggr_en = IPA_BYPASS_AGGR;
  1063. usb_to_ipa_ep_cfg.hdr.hdr_len = ETH_HLEN;
  1064. usb_to_ipa_ep_cfg.nat.nat_en = IPA_SRC_NAT;
  1065. usb_to_ipa_ep_cfg.route.rt_tbl_hdl = 0;
  1066. usb_to_ipa_ep_cfg.mode.dst = IPA_CLIENT_A5_LAN_WAN_CONS;
  1067. usb_to_ipa_ep_cfg.mode.mode = IPA_BASIC;
  1068. result = ipa_cfg_ep(usb_to_ipa_hdl, &usb_to_ipa_ep_cfg);
  1069. if (result) {
  1070. ECM_IPA_ERROR("failed to configure USB to IPA point\n");
  1071. goto out;
  1072. }
  1073. memset(&ipa_to_usb_ep_cfg, 0 , sizeof(struct ipa_ep_cfg));
  1074. ipa_to_usb_ep_cfg.aggr.aggr_en = IPA_BYPASS_AGGR;
  1075. ipa_to_usb_ep_cfg.hdr.hdr_len = ETH_HLEN;
  1076. ipa_to_usb_ep_cfg.nat.nat_en = IPA_BYPASS_NAT;
  1077. result = ipa_cfg_ep(ipa_to_usb_hdl, &ipa_to_usb_ep_cfg);
  1078. if (result) {
  1079. ECM_IPA_ERROR("failed to configure IPA to USB end-point\n");
  1080. goto out;
  1081. }
  1082. pr_debug("end-point registers successfully configured\n");
  1083. out:
  1084. ECM_IPA_LOG_EXIT();
  1085. return result;
  1086. }
  1087. /**
  1088. * ecm_ipa_ep_registers_dma_cfg() - configure the USB endpoints for ECM
  1089. * DMA
  1090. * @usb_to_ipa_hdl: handle received from ipa_connect
  1091. *
  1092. * This function will override the previous configuration
  1093. * which is needed for cores that does not support blocks logic
  1094. * Note that client handles are the actual pipe index
  1095. */
  1096. static int ecm_ipa_ep_registers_dma_cfg(u32 usb_to_ipa_hdl)
  1097. {
  1098. int result = 0;
  1099. struct ipa_ep_cfg_mode cfg_mode;
  1100. u32 apps_to_ipa_hdl = 2;
  1101. ECM_IPA_LOG_ENTRY();
  1102. /* Apps to IPA - override the configuration made by IPA driver
  1103. * in order to allow data path on older platforms*/
  1104. memset(&cfg_mode, 0 , sizeof(cfg_mode));
  1105. cfg_mode.mode = IPA_DMA;
  1106. cfg_mode.dst = IPA_CLIENT_USB_CONS;
  1107. result = ipa_cfg_ep_mode(apps_to_ipa_hdl, &cfg_mode);
  1108. if (result) {
  1109. ECM_IPA_ERROR("failed to configure Apps to IPA\n");
  1110. goto out;
  1111. }
  1112. memset(&cfg_mode, 0 , sizeof(cfg_mode));
  1113. cfg_mode.mode = IPA_DMA;
  1114. cfg_mode.dst = IPA_CLIENT_A5_LAN_WAN_CONS;
  1115. result = ipa_cfg_ep_mode(usb_to_ipa_hdl, &cfg_mode);
  1116. if (result) {
  1117. ECM_IPA_ERROR("failed to configure USB to IPA\n");
  1118. goto out;
  1119. }
  1120. pr_debug("end-point registers successfully configured\n");
  1121. out:
  1122. ECM_IPA_LOG_EXIT();
  1123. return result;
  1124. }
  1125. /**
  1126. * ecm_ipa_set_device_ethernet_addr() - set device etherenet address
  1127. * @dev_ethaddr: device etherenet address
  1128. *
  1129. * Returns 0 for success, negative otherwise
  1130. */
  1131. static int ecm_ipa_set_device_ethernet_addr(u8 *dev_ethaddr,
  1132. u8 device_ethaddr[])
  1133. {
  1134. if (!is_valid_ether_addr(device_ethaddr))
  1135. return -EINVAL;
  1136. memcpy(dev_ethaddr, device_ethaddr, ETH_ALEN);
  1137. pr_debug("device ethernet address: %pM\n", dev_ethaddr);
  1138. return 0;
  1139. }
  1140. /** ecm_ipa_next_state - return the next state of the driver
  1141. * @current_state: the current state of the driver
  1142. * @operation: an enum which represent the operation being made on the driver
  1143. * by its API.
  1144. *
  1145. * This function implements the driver internal state machine.
  1146. * Its decisions are based on the driver current state and the operation
  1147. * being made.
  1148. * In case the operation is invalid this state machine will return
  1149. * the value ECM_IPA_INVALID to inform the caller for a forbidden sequence.
  1150. */
  1151. static enum ecm_ipa_state ecm_ipa_next_state(enum ecm_ipa_state current_state,
  1152. enum ecm_ipa_operation operation)
  1153. {
  1154. int next_state = ECM_IPA_INVALID;
  1155. switch (current_state) {
  1156. case ECM_IPA_UNLOADED:
  1157. if (operation == ECM_IPA_INITIALIZE)
  1158. next_state = ECM_IPA_INITIALIZED;
  1159. break;
  1160. case ECM_IPA_INITIALIZED:
  1161. if (operation == ECM_IPA_CONNECT)
  1162. next_state = ECM_IPA_CONNECTED;
  1163. else if (operation == ECM_IPA_OPEN)
  1164. next_state = ECM_IPA_UP;
  1165. else if (operation == ECM_IPA_CLEANUP)
  1166. next_state = ECM_IPA_UNLOADED;
  1167. break;
  1168. case ECM_IPA_CONNECTED:
  1169. if (operation == ECM_IPA_DISCONNECT)
  1170. next_state = ECM_IPA_INITIALIZED;
  1171. else if (operation == ECM_IPA_OPEN)
  1172. next_state = ECM_IPA_CONNECTED_AND_UP;
  1173. break;
  1174. case ECM_IPA_UP:
  1175. if (operation == ECM_IPA_STOP)
  1176. next_state = ECM_IPA_INITIALIZED;
  1177. else if (operation == ECM_IPA_CONNECT)
  1178. next_state = ECM_IPA_CONNECTED_AND_UP;
  1179. else if (operation == ECM_IPA_CLEANUP)
  1180. next_state = ECM_IPA_UNLOADED;
  1181. break;
  1182. case ECM_IPA_CONNECTED_AND_UP:
  1183. if (operation == ECM_IPA_STOP)
  1184. next_state = ECM_IPA_CONNECTED;
  1185. else if (operation == ECM_IPA_DISCONNECT)
  1186. next_state = ECM_IPA_UP;
  1187. break;
  1188. default:
  1189. ECM_IPA_ERROR("State is not supported\n");
  1190. break;
  1191. }
  1192. pr_debug("state transition ( %s -> %s )- %s\n",
  1193. ecm_ipa_state_string(current_state),
  1194. ecm_ipa_state_string(next_state) ,
  1195. next_state == ECM_IPA_INVALID ?
  1196. "Forbidden" : "Allowed");
  1197. return next_state;
  1198. }
  1199. /**
  1200. * ecm_ipa_state_string - return the state string representation
  1201. * @state: enum which describe the state
  1202. */
  1203. static const char *ecm_ipa_state_string(enum ecm_ipa_state state)
  1204. {
  1205. switch (state) {
  1206. case ECM_IPA_UNLOADED:
  1207. return "ECM_IPA_UNLOADED";
  1208. case ECM_IPA_INITIALIZED:
  1209. return "ECM_IPA_INITIALIZED";
  1210. case ECM_IPA_CONNECTED:
  1211. return "ECM_IPA_CONNECTED";
  1212. case ECM_IPA_UP:
  1213. return "ECM_IPA_UP";
  1214. case ECM_IPA_CONNECTED_AND_UP:
  1215. return "ECM_IPA_CONNECTED_AND_UP";
  1216. default:
  1217. return "Not supported";
  1218. }
  1219. }
  1220. /**
  1221. * ecm_ipa_init_module() - module initialization
  1222. *
  1223. */
  1224. static int ecm_ipa_init_module(void)
  1225. {
  1226. ECM_IPA_LOG_ENTRY();
  1227. ECM_IPA_LOG_EXIT();
  1228. return 0;
  1229. }
  1230. /**
  1231. * ecm_ipa_cleanup_module() - module cleanup
  1232. *
  1233. */
  1234. static void ecm_ipa_cleanup_module(void)
  1235. {
  1236. ECM_IPA_LOG_ENTRY();
  1237. ECM_IPA_LOG_EXIT();
  1238. return;
  1239. }
  1240. MODULE_LICENSE("GPL v2");
  1241. MODULE_DESCRIPTION("ECM IPA network interface");
  1242. late_initcall(ecm_ipa_init_module);
  1243. module_exit(ecm_ipa_cleanup_module);