rndis_ipa.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/atomic.h>
  6. #include <linux/errno.h>
  7. #include <linux/etherdevice.h>
  8. #include <linux/if_vlan.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/in.h>
  11. #include <linux/stddef.h>
  12. #include <linux/ip.h>
  13. #include <linux/fs.h>
  14. #include <linux/module.h>
  15. #include <linux/msm_ipa.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/sched.h>
  19. #include <linux/ipa.h>
  20. #include <linux/random.h>
  21. #include <linux/rndis_ipa.h>
  22. #include <linux/workqueue.h>
  23. #include "../ipa_common_i.h"
  24. #include "../ipa_v3/ipa_pm.h"
  25. #define CREATE_TRACE_POINTS
  26. #include "rndis_ipa_trace.h"
  27. #define DRV_NAME "RNDIS_IPA"
  28. #define DEBUGFS_DIR_NAME "rndis_ipa"
  29. #define DEBUGFS_AGGR_DIR_NAME "rndis_ipa_aggregation"
  30. #define NETDEV_NAME "rndis"
  31. #define IPV4_HDR_NAME "rndis_eth_ipv4"
  32. #define IPV6_HDR_NAME "rndis_eth_ipv6"
  33. #define IPA_TO_USB_CLIENT IPA_CLIENT_USB_CONS
  34. #define INACTIVITY_MSEC_DELAY 100
  35. #define DEFAULT_OUTSTANDING_HIGH 64
  36. #define DEFAULT_OUTSTANDING_LOW 32
  37. #define DEBUGFS_TEMP_BUF_SIZE 4
  38. #define RNDIS_IPA_PKT_TYPE 0x00000001
  39. #define RNDIS_IPA_DFLT_RT_HDL 0
  40. #define FROM_IPA_TO_USB_BAMDMA 4
  41. #define FROM_USB_TO_IPA_BAMDMA 5
  42. #define BAM_DMA_MAX_PKT_NUMBER 10
  43. #define BAM_DMA_DATA_FIFO_SIZE \
  44. (BAM_DMA_MAX_PKT_NUMBER * \
  45. (ETH_FRAME_LEN + sizeof(struct rndis_pkt_hdr)))
  46. #define BAM_DMA_DESC_FIFO_SIZE \
  47. (BAM_DMA_MAX_PKT_NUMBER * (sizeof(struct sps_iovec)))
  48. #define TX_TIMEOUT (5 * HZ)
  49. #define MIN_TX_ERROR_SLEEP_PERIOD 500
  50. #define DEFAULT_AGGR_TIME_LIMIT 1000 /* 1ms */
  51. #define DEFAULT_AGGR_PKT_LIMIT 0
  52. #define IPA_RNDIS_IPC_LOG_PAGES 50
  53. #define IPA_RNDIS_IPC_LOGGING(buf, fmt, args...) \
  54. do { \
  55. if (buf) \
  56. ipc_log_string((buf), fmt, __func__, __LINE__, \
  57. ## args); \
  58. } while (0)
  59. static void *ipa_rndis_logbuf;
  60. #define RNDIS_IPA_DEBUG(fmt, args...) \
  61. do { \
  62. pr_debug(DRV_NAME " %s:%d " fmt, __func__, __LINE__, ## args);\
  63. if (ipa_rndis_logbuf) { \
  64. IPA_RNDIS_IPC_LOGGING(ipa_rndis_logbuf, \
  65. DRV_NAME " %s:%d " fmt, ## args); \
  66. } \
  67. } while (0)
  68. #define RNDIS_IPA_DEBUG_XMIT(fmt, args...) \
  69. pr_debug(DRV_NAME " %s:%d " fmt, __func__, __LINE__, ## args)
  70. #define RNDIS_IPA_ERROR(fmt, args...) \
  71. do { \
  72. pr_err(DRV_NAME "@%s@%d@ctx:%s: "\
  73. fmt, __func__, __LINE__, current->comm, ## args);\
  74. if (ipa_rndis_logbuf) { \
  75. IPA_RNDIS_IPC_LOGGING(ipa_rndis_logbuf, \
  76. DRV_NAME " %s:%d " fmt, ## args); \
  77. } \
  78. } while (0)
  79. #define NULL_CHECK_RETVAL(ptr) \
  80. do { \
  81. if (!(ptr)) { \
  82. RNDIS_IPA_ERROR("null pointer #ptr\n"); \
  83. ret = -EINVAL; \
  84. } \
  85. } \
  86. while (0)
  87. #define RNDIS_HDR_OFST(field) offsetof(struct rndis_pkt_hdr, field)
  88. #define RNDIS_IPA_LOG_ENTRY() RNDIS_IPA_DEBUG("begin\n")
  89. #define RNDIS_IPA_LOG_EXIT() RNDIS_IPA_DEBUG("end\n")
  90. /**
  91. * enum rndis_ipa_state - specify the current driver internal state
  92. * which is guarded by a state machine.
  93. *
  94. * The driver internal state changes due to its external API usage.
  95. * The driver saves its internal state to guard from caller illegal
  96. * call sequence.
  97. * states:
  98. * UNLOADED is the first state which is the default one and is also the state
  99. * after the driver gets unloaded(cleanup).
  100. * INITIALIZED is the driver state once it finished registering
  101. * the network device and all internal data struct were initialized
  102. * CONNECTED is the driver state once the USB pipes were connected to IPA
  103. * UP is the driver state after the interface mode was set to UP but the
  104. * pipes are not connected yet - this state is meta-stable state.
  105. * CONNECTED_AND_UP is the driver state when the pipe were connected and
  106. * the interface got UP request from the network stack. this is the driver
  107. * idle operation state which allows it to transmit/receive data.
  108. * INVALID is a state which is not allowed.
  109. */
  110. enum rndis_ipa_state {
  111. RNDIS_IPA_UNLOADED = 0,
  112. RNDIS_IPA_INITIALIZED = 1,
  113. RNDIS_IPA_CONNECTED = 2,
  114. RNDIS_IPA_UP = 3,
  115. RNDIS_IPA_CONNECTED_AND_UP = 4,
  116. RNDIS_IPA_INVALID = 5,
  117. };
  118. /**
  119. * enum rndis_ipa_operation - enumerations used to describe the API operation
  120. *
  121. * Those enums are used as input for the driver state machine.
  122. */
  123. enum rndis_ipa_operation {
  124. RNDIS_IPA_INITIALIZE,
  125. RNDIS_IPA_CONNECT,
  126. RNDIS_IPA_OPEN,
  127. RNDIS_IPA_STOP,
  128. RNDIS_IPA_DISCONNECT,
  129. RNDIS_IPA_CLEANUP,
  130. };
  131. #define RNDIS_IPA_STATE_DEBUG(ctx) \
  132. RNDIS_IPA_DEBUG("Driver state: %s\n",\
  133. rndis_ipa_state_string((ctx)->state))
  134. /**
  135. * struct rndis_ipa_dev - main driver context parameters
  136. *
  137. * @net: network interface struct implemented by this driver
  138. * @directory: debugfs directory for various debugging switches
  139. * @tx_filter: flag that enable/disable Tx path to continue to IPA
  140. * @tx_dropped: number of filtered out Tx packets
  141. * @tx_dump_enable: dump all Tx packets
  142. * @rx_filter: flag that enable/disable Rx path to continue to IPA
  143. * @rx_dropped: number of filtered out Rx packets
  144. * @rx_dump_enable: dump all Rx packets
  145. * @icmp_filter: allow all ICMP packet to pass through the filters
  146. * @deaggregation_enable: enable/disable IPA HW deaggregation logic
  147. * @during_xmit_error: flags that indicate that the driver is in a middle
  148. * of error handling in Tx path
  149. * @directory: holds all debug flags used by the driver to allow cleanup
  150. * for driver unload
  151. * @eth_ipv4_hdr_hdl: saved handle for ipv4 header-insertion table
  152. * @eth_ipv6_hdr_hdl: saved handle for ipv6 header-insertion table
  153. * @usb_to_ipa_hdl: save handle for IPA pipe operations
  154. * @ipa_to_usb_hdl: save handle for IPA pipe operations
  155. * @outstanding_pkts: number of packets sent to IPA without TX complete ACKed
  156. * @outstanding_high: number of outstanding packets allowed
  157. * @outstanding_low: number of outstanding packets which shall cause
  158. * to netdev queue start (after stopped due to outstanding_high reached)
  159. * @error_msec_sleep_time: number of msec for sleeping in case of Tx error
  160. * @state: current state of the driver
  161. * @host_ethaddr: holds the tethered PC ethernet address
  162. * @device_ethaddr: holds the device ethernet address
  163. * @device_ready_notify: callback supplied by USB core driver
  164. * This callback shall be called by the Netdev once the Netdev internal
  165. * state is changed to RNDIS_IPA_CONNECTED_AND_UP
  166. * @xmit_error_delayed_work: work item for cases where IPA driver Tx fails
  167. * @state_lock: used to protect the state variable.
  168. * @pm_hdl: handle for IPA PM framework
  169. * @is_vlan_mode: should driver work in vlan mode?
  170. * @netif_rx_function: holds the correct network stack API, needed for NAPI
  171. */
  172. struct rndis_ipa_dev {
  173. struct net_device *net;
  174. bool tx_filter;
  175. u32 tx_dropped;
  176. bool tx_dump_enable;
  177. bool rx_filter;
  178. u32 rx_dropped;
  179. bool rx_dump_enable;
  180. bool icmp_filter;
  181. bool deaggregation_enable;
  182. bool during_xmit_error;
  183. struct dentry *directory;
  184. u32 eth_ipv4_hdr_hdl;
  185. u32 eth_ipv6_hdr_hdl;
  186. u32 usb_to_ipa_hdl;
  187. u32 ipa_to_usb_hdl;
  188. atomic_t outstanding_pkts;
  189. u32 outstanding_high;
  190. u32 outstanding_low;
  191. u32 error_msec_sleep_time;
  192. enum rndis_ipa_state state;
  193. u8 host_ethaddr[ETH_ALEN];
  194. u8 device_ethaddr[ETH_ALEN];
  195. void (*device_ready_notify)(void);
  196. struct delayed_work xmit_error_delayed_work;
  197. spinlock_t state_lock; /* Spinlock for the state variable.*/
  198. u32 pm_hdl;
  199. bool is_vlan_mode;
  200. int (*netif_rx_function)(struct sk_buff *skb);
  201. };
  202. /**
  203. * rndis_pkt_hdr - RNDIS_IPA representation of REMOTE_NDIS_PACKET_MSG
  204. * @msg_type: for REMOTE_NDIS_PACKET_MSG this value should be 1
  205. * @msg_len: total message length in bytes, including RNDIS header an payload
  206. * @data_ofst: offset in bytes from start of the data_ofst to payload
  207. * @data_len: payload size in bytes
  208. * @zeroes: OOB place holder - not used for RNDIS_IPA.
  209. */
  210. struct rndis_pkt_hdr {
  211. __le32 msg_type;
  212. __le32 msg_len;
  213. __le32 data_ofst;
  214. __le32 data_len;
  215. __le32 zeroes[7];
  216. } __packed__;
  217. static int rndis_ipa_open(struct net_device *net);
  218. static void rndis_ipa_packet_receive_notify
  219. (void *private, enum ipa_dp_evt_type evt, unsigned long data);
  220. static void rndis_ipa_tx_complete_notify
  221. (void *private, enum ipa_dp_evt_type evt, unsigned long data);
  222. static void rndis_ipa_tx_timeout(struct net_device *net);
  223. static int rndis_ipa_stop(struct net_device *net);
  224. static void rndis_ipa_enable_data_path(struct rndis_ipa_dev *rndis_ipa_ctx);
  225. static struct sk_buff *rndis_encapsulate_skb(struct sk_buff *skb,
  226. struct rndis_ipa_dev *rndis_ipa_ctx);
  227. static void rndis_ipa_xmit_error(struct sk_buff *skb);
  228. static void rndis_ipa_xmit_error_aftercare_wq(struct work_struct *work);
  229. static void rndis_ipa_prepare_header_insertion
  230. (int eth_type,
  231. const char *hdr_name, struct ipa_hdr_add *add_hdr,
  232. const void *dst_mac, const void *src_mac, bool is_vlan_mode);
  233. static int rndis_ipa_hdrs_cfg
  234. (struct rndis_ipa_dev *rndis_ipa_ctx,
  235. const void *dst_mac, const void *src_mac);
  236. static int rndis_ipa_hdrs_destroy(struct rndis_ipa_dev *rndis_ipa_ctx);
  237. static struct net_device_stats *rndis_ipa_get_stats(struct net_device *net);
  238. static int rndis_ipa_register_properties(char *netdev_name, bool is_vlan_mode);
  239. static int rndis_ipa_deregister_properties(char *netdev_name);
  240. static int rndis_ipa_register_pm_client(struct rndis_ipa_dev *rndis_ipa_ctx);
  241. static int rndis_ipa_deregister_pm_client(struct rndis_ipa_dev *rndis_ipa_ctx);
  242. static bool rx_filter(struct sk_buff *skb);
  243. static bool tx_filter(struct sk_buff *skb);
  244. static netdev_tx_t rndis_ipa_start_xmit
  245. (struct sk_buff *skb, struct net_device *net);
  246. static int rndis_ipa_debugfs_atomic_open
  247. (struct inode *inode, struct file *file);
  248. static int rndis_ipa_debugfs_aggr_open
  249. (struct inode *inode, struct file *file);
  250. static ssize_t rndis_ipa_debugfs_aggr_write
  251. (struct file *file,
  252. const char __user *buf, size_t count, loff_t *ppos);
  253. static ssize_t rndis_ipa_debugfs_atomic_read
  254. (struct file *file,
  255. char __user *ubuf, size_t count, loff_t *ppos);
  256. static void rndis_ipa_dump_skb(struct sk_buff *skb);
  257. static void rndis_ipa_debugfs_init(struct rndis_ipa_dev *rndis_ipa_ctx);
  258. static void rndis_ipa_debugfs_destroy(struct rndis_ipa_dev *rndis_ipa_ctx);
  259. static int rndis_ipa_ep_registers_cfg
  260. (u32 usb_to_ipa_hdl,
  261. u32 ipa_to_usb_hdl, u32 max_xfer_size_bytes_to_dev,
  262. u32 max_xfer_size_bytes_to_host, u32 mtu,
  263. bool deaggr_enable,
  264. bool is_vlan_mode);
  265. static int rndis_ipa_set_device_ethernet_addr
  266. (u8 *dev_ethaddr,
  267. u8 device_ethaddr[]);
  268. static enum rndis_ipa_state rndis_ipa_next_state
  269. (enum rndis_ipa_state current_state,
  270. enum rndis_ipa_operation operation);
  271. static const char *rndis_ipa_state_string(enum rndis_ipa_state state);
  272. static struct rndis_ipa_dev *rndis_ipa;
  273. static const struct net_device_ops rndis_ipa_netdev_ops = {
  274. .ndo_open = rndis_ipa_open,
  275. .ndo_stop = rndis_ipa_stop,
  276. .ndo_start_xmit = rndis_ipa_start_xmit,
  277. .ndo_tx_timeout = rndis_ipa_tx_timeout,
  278. .ndo_get_stats = rndis_ipa_get_stats,
  279. .ndo_set_mac_address = eth_mac_addr,
  280. };
  281. static const struct file_operations rndis_ipa_debugfs_atomic_ops = {
  282. .open = rndis_ipa_debugfs_atomic_open,
  283. .read = rndis_ipa_debugfs_atomic_read,
  284. };
  285. static const struct file_operations rndis_ipa_aggr_ops = {
  286. .open = rndis_ipa_debugfs_aggr_open,
  287. .write = rndis_ipa_debugfs_aggr_write,
  288. };
  289. static struct ipa_ep_cfg ipa_to_usb_ep_cfg = {
  290. .mode = {
  291. .mode = IPA_BASIC,
  292. .dst = IPA_CLIENT_APPS_LAN_CONS,
  293. },
  294. .hdr = {
  295. .hdr_len = ETH_HLEN + sizeof(struct rndis_pkt_hdr),
  296. .hdr_ofst_metadata_valid = false,
  297. .hdr_ofst_metadata = 0,
  298. .hdr_additional_const_len = ETH_HLEN,
  299. .hdr_ofst_pkt_size_valid = true,
  300. .hdr_ofst_pkt_size = 3 * sizeof(u32),
  301. .hdr_a5_mux = false,
  302. .hdr_remove_additional = false,
  303. .hdr_metadata_reg_valid = false,
  304. },
  305. .hdr_ext = {
  306. .hdr_pad_to_alignment = 0,
  307. .hdr_total_len_or_pad_offset = 1 * sizeof(u32),
  308. .hdr_payload_len_inc_padding = false,
  309. .hdr_total_len_or_pad = IPA_HDR_TOTAL_LEN,
  310. .hdr_total_len_or_pad_valid = true,
  311. .hdr_little_endian = true,
  312. },
  313. .aggr = {
  314. .aggr_en = IPA_ENABLE_AGGR,
  315. .aggr = IPA_GENERIC,
  316. .aggr_byte_limit = 4,
  317. .aggr_time_limit = DEFAULT_AGGR_TIME_LIMIT,
  318. .aggr_pkt_limit = DEFAULT_AGGR_PKT_LIMIT,
  319. },
  320. .deaggr = {
  321. .deaggr_hdr_len = 0,
  322. .packet_offset_valid = 0,
  323. .packet_offset_location = 0,
  324. .max_packet_len = 0,
  325. },
  326. .route = {
  327. .rt_tbl_hdl = RNDIS_IPA_DFLT_RT_HDL,
  328. },
  329. .nat = {
  330. .nat_en = IPA_SRC_NAT,
  331. },
  332. };
  333. static struct ipa_ep_cfg usb_to_ipa_ep_cfg_deaggr_dis = {
  334. .mode = {
  335. .mode = IPA_BASIC,
  336. .dst = IPA_CLIENT_APPS_LAN_CONS,
  337. },
  338. .hdr = {
  339. .hdr_len = ETH_HLEN + sizeof(struct rndis_pkt_hdr),
  340. .hdr_ofst_metadata_valid = false,
  341. .hdr_ofst_metadata = 0,
  342. .hdr_additional_const_len = 0,
  343. .hdr_ofst_pkt_size_valid = true,
  344. .hdr_ofst_pkt_size = 3 * sizeof(u32) +
  345. sizeof(struct rndis_pkt_hdr),
  346. .hdr_a5_mux = false,
  347. .hdr_remove_additional = false,
  348. .hdr_metadata_reg_valid = true,
  349. },
  350. .hdr_ext = {
  351. .hdr_pad_to_alignment = 0,
  352. .hdr_total_len_or_pad_offset = 1 * sizeof(u32),
  353. .hdr_payload_len_inc_padding = false,
  354. .hdr_total_len_or_pad = IPA_HDR_TOTAL_LEN,
  355. .hdr_total_len_or_pad_valid = true,
  356. .hdr_little_endian = true,
  357. },
  358. .aggr = {
  359. .aggr_en = IPA_BYPASS_AGGR,
  360. .aggr = 0,
  361. .aggr_byte_limit = 0,
  362. .aggr_time_limit = 0,
  363. .aggr_pkt_limit = 0,
  364. },
  365. .deaggr = {
  366. .deaggr_hdr_len = 0,
  367. .packet_offset_valid = false,
  368. .packet_offset_location = 0,
  369. .max_packet_len = 0,
  370. },
  371. .route = {
  372. .rt_tbl_hdl = RNDIS_IPA_DFLT_RT_HDL,
  373. },
  374. .nat = {
  375. .nat_en = IPA_BYPASS_NAT,
  376. },
  377. };
  378. static struct ipa_ep_cfg usb_to_ipa_ep_cfg_deaggr_en = {
  379. .mode = {
  380. .mode = IPA_BASIC,
  381. .dst = IPA_CLIENT_APPS_LAN_CONS,
  382. },
  383. .hdr = {
  384. .hdr_len = ETH_HLEN,
  385. .hdr_ofst_metadata_valid = false,
  386. .hdr_ofst_metadata = 0,
  387. .hdr_additional_const_len = 0,
  388. .hdr_ofst_pkt_size_valid = true,
  389. .hdr_ofst_pkt_size = 3 * sizeof(u32),
  390. .hdr_a5_mux = false,
  391. .hdr_remove_additional = false,
  392. .hdr_metadata_reg_valid = true,
  393. },
  394. .hdr_ext = {
  395. .hdr_pad_to_alignment = 0,
  396. .hdr_total_len_or_pad_offset = 1 * sizeof(u32),
  397. .hdr_payload_len_inc_padding = false,
  398. .hdr_total_len_or_pad = IPA_HDR_TOTAL_LEN,
  399. .hdr_total_len_or_pad_valid = true,
  400. .hdr_little_endian = true,
  401. },
  402. .aggr = {
  403. .aggr_en = IPA_ENABLE_DEAGGR,
  404. .aggr = IPA_GENERIC,
  405. .aggr_byte_limit = 0,
  406. .aggr_time_limit = 0,
  407. .aggr_pkt_limit = 0,
  408. },
  409. .deaggr = {
  410. .deaggr_hdr_len = sizeof(struct rndis_pkt_hdr),
  411. .packet_offset_valid = true,
  412. .packet_offset_location = 8,
  413. .max_packet_len = 8192, /* Will be overridden*/
  414. },
  415. .route = {
  416. .rt_tbl_hdl = RNDIS_IPA_DFLT_RT_HDL,
  417. },
  418. .nat = {
  419. .nat_en = IPA_BYPASS_NAT,
  420. },
  421. };
  422. /**
  423. * rndis_template_hdr - RNDIS template structure for RNDIS_IPA SW insertion
  424. * @msg_type: set for REMOTE_NDIS_PACKET_MSG (0x00000001)
  425. * this value will be used for all data packets
  426. * @msg_len: will add the skb length to get final size
  427. * @data_ofst: this field value will not be changed
  428. * @data_len: set as skb length to get final size
  429. * @zeroes: make sure all OOB data is not used
  430. */
  431. static struct rndis_pkt_hdr rndis_template_hdr = {
  432. .msg_type = RNDIS_IPA_PKT_TYPE,
  433. .msg_len = sizeof(struct rndis_pkt_hdr),
  434. .data_ofst = sizeof(struct rndis_pkt_hdr) - RNDIS_HDR_OFST(data_ofst),
  435. .data_len = 0,
  436. .zeroes = {0},
  437. };
  438. static void rndis_ipa_msg_free_cb(void *buff, u32 len, u32 type)
  439. {
  440. kfree(buff);
  441. }
  442. /**
  443. * rndis_ipa_init() - create network device and initialize internal
  444. * data structures
  445. * @params: in/out parameters required for initialization,
  446. * see "struct ipa_usb_init_params" for more details
  447. *
  448. * Shall be called prior to pipe connection.
  449. * Detailed description:
  450. * - allocate the network device
  451. * - set default values for driver internal switches and stash them inside
  452. * the netdev private field
  453. * - set needed headroom for RNDIS header
  454. * - create debugfs folder and files
  455. * - create IPA resource manager client
  456. * - set the ethernet address for the netdev to be added on SW Tx path
  457. * - add header insertion rules for IPA driver (based on host/device Ethernet
  458. * addresses given in input params and on RNDIS data template struct)
  459. * - register tx/rx properties to IPA driver (will be later used
  460. * by IPA configuration manager to configure rest of the IPA rules)
  461. * - set the carrier state to "off" (until connect is called)
  462. * - register the network device
  463. * - set the out parameters
  464. * - change driver internal state to INITIALIZED
  465. *
  466. * Returns negative errno, or zero on success
  467. */
  468. int rndis_ipa_init(struct ipa_usb_init_params *params)
  469. {
  470. int result = 0;
  471. struct net_device *net;
  472. struct rndis_ipa_dev *rndis_ipa_ctx;
  473. int ret;
  474. RNDIS_IPA_LOG_ENTRY();
  475. RNDIS_IPA_DEBUG("%s initializing\n", DRV_NAME);
  476. ret = 0;
  477. NULL_CHECK_RETVAL(params);
  478. if (ret)
  479. return ret;
  480. RNDIS_IPA_DEBUG
  481. ("host_ethaddr=%pM, device_ethaddr=%pM\n",
  482. params->host_ethaddr,
  483. params->device_ethaddr);
  484. net = alloc_etherdev(sizeof(struct rndis_ipa_dev));
  485. if (!net) {
  486. result = -ENOMEM;
  487. RNDIS_IPA_ERROR("fail to allocate Ethernet device\n");
  488. goto fail_alloc_etherdev;
  489. }
  490. RNDIS_IPA_DEBUG("network device was successfully allocated\n");
  491. rndis_ipa_ctx = netdev_priv(net);
  492. if (!rndis_ipa_ctx) {
  493. result = -ENOMEM;
  494. RNDIS_IPA_ERROR("fail to extract netdev priv\n");
  495. goto fail_netdev_priv;
  496. }
  497. memset(rndis_ipa_ctx, 0, sizeof(*rndis_ipa_ctx));
  498. RNDIS_IPA_DEBUG("rndis_ipa_ctx (private)=%pK\n", rndis_ipa_ctx);
  499. spin_lock_init(&rndis_ipa_ctx->state_lock);
  500. rndis_ipa_ctx->net = net;
  501. rndis_ipa_ctx->tx_filter = false;
  502. rndis_ipa_ctx->rx_filter = false;
  503. rndis_ipa_ctx->icmp_filter = true;
  504. rndis_ipa_ctx->tx_dropped = 0;
  505. rndis_ipa_ctx->rx_dropped = 0;
  506. rndis_ipa_ctx->tx_dump_enable = false;
  507. rndis_ipa_ctx->rx_dump_enable = false;
  508. rndis_ipa_ctx->deaggregation_enable = false;
  509. rndis_ipa_ctx->outstanding_high = DEFAULT_OUTSTANDING_HIGH;
  510. rndis_ipa_ctx->outstanding_low = DEFAULT_OUTSTANDING_LOW;
  511. atomic_set(&rndis_ipa_ctx->outstanding_pkts, 0);
  512. memcpy
  513. (rndis_ipa_ctx->device_ethaddr, params->device_ethaddr,
  514. sizeof(rndis_ipa_ctx->device_ethaddr));
  515. memcpy
  516. (rndis_ipa_ctx->host_ethaddr, params->host_ethaddr,
  517. sizeof(rndis_ipa_ctx->host_ethaddr));
  518. INIT_DELAYED_WORK
  519. (&rndis_ipa_ctx->xmit_error_delayed_work,
  520. rndis_ipa_xmit_error_aftercare_wq);
  521. rndis_ipa_ctx->error_msec_sleep_time =
  522. MIN_TX_ERROR_SLEEP_PERIOD;
  523. RNDIS_IPA_DEBUG("internal data structures were set\n");
  524. if (!params->device_ready_notify)
  525. RNDIS_IPA_DEBUG("device_ready_notify() was not supplied\n");
  526. rndis_ipa_ctx->device_ready_notify = params->device_ready_notify;
  527. snprintf(net->name, sizeof(net->name), "%s%%d", NETDEV_NAME);
  528. RNDIS_IPA_DEBUG
  529. ("Setting network interface driver name to: %s\n",
  530. net->name);
  531. net->netdev_ops = &rndis_ipa_netdev_ops;
  532. net->watchdog_timeo = TX_TIMEOUT;
  533. net->needed_headroom = sizeof(rndis_template_hdr);
  534. RNDIS_IPA_DEBUG
  535. ("Needed headroom for RNDIS header set to %d\n",
  536. net->needed_headroom);
  537. rndis_ipa_debugfs_init(rndis_ipa_ctx);
  538. result = rndis_ipa_set_device_ethernet_addr
  539. (net->dev_addr, rndis_ipa_ctx->device_ethaddr);
  540. if (result) {
  541. RNDIS_IPA_ERROR("set device MAC failed\n");
  542. goto fail_set_device_ethernet;
  543. }
  544. RNDIS_IPA_DEBUG("Device Ethernet address set %pM\n", net->dev_addr);
  545. if (ipa_is_vlan_mode(IPA_VLAN_IF_RNDIS,
  546. &rndis_ipa_ctx->is_vlan_mode)) {
  547. RNDIS_IPA_ERROR("couldn't acquire vlan mode, is ipa ready?\n");
  548. goto fail_get_vlan_mode;
  549. }
  550. RNDIS_IPA_DEBUG("is_vlan_mode %d\n", rndis_ipa_ctx->is_vlan_mode);
  551. result = rndis_ipa_hdrs_cfg
  552. (rndis_ipa_ctx,
  553. params->host_ethaddr,
  554. params->device_ethaddr);
  555. if (result) {
  556. RNDIS_IPA_ERROR("fail on ipa hdrs set\n");
  557. goto fail_hdrs_cfg;
  558. }
  559. RNDIS_IPA_DEBUG("IPA header-insertion configed for Ethernet+RNDIS\n");
  560. result = rndis_ipa_register_properties(net->name,
  561. rndis_ipa_ctx->is_vlan_mode);
  562. if (result) {
  563. RNDIS_IPA_ERROR("fail on properties set\n");
  564. goto fail_register_tx;
  565. }
  566. RNDIS_IPA_DEBUG("2 TX and 2 RX properties were registered\n");
  567. netif_carrier_off(net);
  568. RNDIS_IPA_DEBUG("set carrier off until pipes are connected\n");
  569. result = register_netdev(net);
  570. if (result) {
  571. RNDIS_IPA_ERROR("register_netdev failed: %d\n", result);
  572. goto fail_register_netdev;
  573. }
  574. RNDIS_IPA_DEBUG
  575. ("netdev:%s registration succeeded, index=%d\n",
  576. net->name, net->ifindex);
  577. if (ipa_get_lan_rx_napi()) {
  578. rndis_ipa_ctx->netif_rx_function = netif_receive_skb;
  579. RNDIS_IPA_DEBUG("LAN RX NAPI enabled = True");
  580. } else {
  581. rndis_ipa_ctx->netif_rx_function = netif_rx_ni;
  582. RNDIS_IPA_DEBUG("LAN RX NAPI enabled = False");
  583. }
  584. rndis_ipa = rndis_ipa_ctx;
  585. params->ipa_rx_notify = rndis_ipa_packet_receive_notify;
  586. params->ipa_tx_notify = rndis_ipa_tx_complete_notify;
  587. params->private = rndis_ipa_ctx;
  588. params->skip_ep_cfg = false;
  589. rndis_ipa_ctx->state = RNDIS_IPA_INITIALIZED;
  590. RNDIS_IPA_STATE_DEBUG(rndis_ipa_ctx);
  591. pr_info("RNDIS_IPA NetDev was initialized\n");
  592. RNDIS_IPA_LOG_EXIT();
  593. return 0;
  594. fail_register_netdev:
  595. rndis_ipa_deregister_properties(net->name);
  596. fail_register_tx:
  597. rndis_ipa_hdrs_destroy(rndis_ipa_ctx);
  598. fail_hdrs_cfg:
  599. fail_get_vlan_mode:
  600. fail_set_device_ethernet:
  601. rndis_ipa_debugfs_destroy(rndis_ipa_ctx);
  602. fail_netdev_priv:
  603. free_netdev(net);
  604. fail_alloc_etherdev:
  605. return result;
  606. }
  607. EXPORT_SYMBOL(rndis_ipa_init);
  608. /**
  609. * rndis_ipa_pipe_connect_notify() - notify rndis_ipa Netdev that the USB pipes
  610. * were connected
  611. * @usb_to_ipa_hdl: handle from IPA driver client for USB->IPA
  612. * @ipa_to_usb_hdl: handle from IPA driver client for IPA->USB
  613. * @private: same value that was set by init(), this parameter holds the
  614. * network device pointer.
  615. * @max_transfer_byte_size: RNDIS protocol specific, the maximum size that
  616. * the host expect
  617. * @max_packet_number: RNDIS protocol specific, the maximum packet number
  618. * that the host expects
  619. *
  620. * Once USB driver finishes the pipe connection between IPA core
  621. * and USB core this method shall be called in order to
  622. * allow the driver to complete the data path configurations.
  623. * Detailed description:
  624. * - configure the IPA end-points register
  625. * - notify the Linux kernel for "carrier_on"
  626. * - change the driver internal state
  627. *
  628. * After this function is done the driver state changes to "Connected" or
  629. * Connected and Up.
  630. * This API is expected to be called after initialization() or
  631. * after a call to disconnect().
  632. *
  633. * Returns negative errno, or zero on success
  634. */
  635. int rndis_ipa_pipe_connect_notify(
  636. u32 usb_to_ipa_hdl,
  637. u32 ipa_to_usb_hdl,
  638. u32 max_xfer_size_bytes_to_dev,
  639. u32 max_packet_number_to_dev,
  640. u32 max_xfer_size_bytes_to_host,
  641. void *private)
  642. {
  643. struct rndis_ipa_dev *rndis_ipa_ctx = private;
  644. int next_state;
  645. int result;
  646. int ret;
  647. unsigned long flags;
  648. struct ipa_ecm_msg *rndis_msg;
  649. struct ipa_msg_meta msg_meta;
  650. RNDIS_IPA_LOG_ENTRY();
  651. ret = 0;
  652. NULL_CHECK_RETVAL(private);
  653. if (ret)
  654. return ret;
  655. RNDIS_IPA_DEBUG
  656. ("usb_to_ipa_hdl=%d, ipa_to_usb_hdl=%d, private=0x%pK\n",
  657. usb_to_ipa_hdl, ipa_to_usb_hdl, private);
  658. RNDIS_IPA_DEBUG
  659. ("max_xfer_sz_to_dev=%d, max_pkt_num_to_dev=%d\n",
  660. max_xfer_size_bytes_to_dev,
  661. max_packet_number_to_dev);
  662. RNDIS_IPA_DEBUG
  663. ("max_xfer_sz_to_host=%d\n",
  664. max_xfer_size_bytes_to_host);
  665. spin_lock_irqsave(&rndis_ipa_ctx->state_lock, flags);
  666. next_state = rndis_ipa_next_state
  667. (rndis_ipa_ctx->state,
  668. RNDIS_IPA_CONNECT);
  669. if (next_state == RNDIS_IPA_INVALID) {
  670. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  671. RNDIS_IPA_ERROR("use init()/disconnect() before connect()\n");
  672. return -EPERM;
  673. }
  674. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  675. if (usb_to_ipa_hdl >= IPA_CLIENT_MAX) {
  676. RNDIS_IPA_ERROR
  677. ("usb_to_ipa_hdl(%d) - not valid ipa handle\n",
  678. usb_to_ipa_hdl);
  679. return -EINVAL;
  680. }
  681. if (ipa_to_usb_hdl >= IPA_CLIENT_MAX) {
  682. RNDIS_IPA_ERROR
  683. ("ipa_to_usb_hdl(%d) - not valid ipa handle\n",
  684. ipa_to_usb_hdl);
  685. return -EINVAL;
  686. }
  687. result = rndis_ipa_register_pm_client(rndis_ipa_ctx);
  688. if (result) {
  689. RNDIS_IPA_ERROR("fail on PM register\n");
  690. goto fail_register_pm;
  691. }
  692. RNDIS_IPA_DEBUG("PM client was registered\n");
  693. rndis_ipa_ctx->ipa_to_usb_hdl = ipa_to_usb_hdl;
  694. rndis_ipa_ctx->usb_to_ipa_hdl = usb_to_ipa_hdl;
  695. if (max_packet_number_to_dev > 1)
  696. rndis_ipa_ctx->deaggregation_enable = true;
  697. else
  698. rndis_ipa_ctx->deaggregation_enable = false;
  699. result = rndis_ipa_ep_registers_cfg
  700. (usb_to_ipa_hdl,
  701. ipa_to_usb_hdl,
  702. max_xfer_size_bytes_to_dev,
  703. max_xfer_size_bytes_to_host,
  704. rndis_ipa_ctx->net->mtu,
  705. rndis_ipa_ctx->deaggregation_enable,
  706. rndis_ipa_ctx->is_vlan_mode);
  707. if (result) {
  708. RNDIS_IPA_ERROR("fail on ep cfg\n");
  709. goto fail;
  710. }
  711. RNDIS_IPA_DEBUG("end-points configured\n");
  712. netif_stop_queue(rndis_ipa_ctx->net);
  713. RNDIS_IPA_DEBUG("netif_stop_queue() was called\n");
  714. netif_carrier_on(rndis_ipa_ctx->net);
  715. if (!netif_carrier_ok(rndis_ipa_ctx->net)) {
  716. RNDIS_IPA_ERROR("netif_carrier_ok error\n");
  717. result = -EBUSY;
  718. goto fail;
  719. }
  720. RNDIS_IPA_DEBUG("netif_carrier_on() was called\n");
  721. rndis_msg = kzalloc(sizeof(*rndis_msg), GFP_KERNEL);
  722. if (!rndis_msg) {
  723. result = -ENOMEM;
  724. goto fail;
  725. }
  726. memset(&msg_meta, 0, sizeof(struct ipa_msg_meta));
  727. msg_meta.msg_type = ECM_CONNECT;
  728. msg_meta.msg_len = sizeof(struct ipa_ecm_msg);
  729. strlcpy(rndis_msg->name, rndis_ipa_ctx->net->name,
  730. IPA_RESOURCE_NAME_MAX);
  731. rndis_msg->ifindex = rndis_ipa_ctx->net->ifindex;
  732. result = ipa_send_msg(&msg_meta, rndis_msg, rndis_ipa_msg_free_cb);
  733. if (result) {
  734. RNDIS_IPA_ERROR("fail to send ECM_CONNECT for rndis\n");
  735. kfree(rndis_msg);
  736. goto fail;
  737. }
  738. spin_lock_irqsave(&rndis_ipa_ctx->state_lock, flags);
  739. next_state = rndis_ipa_next_state(rndis_ipa_ctx->state,
  740. RNDIS_IPA_CONNECT);
  741. if (next_state == RNDIS_IPA_INVALID) {
  742. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  743. RNDIS_IPA_ERROR("use init()/disconnect() before connect()\n");
  744. return -EPERM;
  745. }
  746. rndis_ipa_ctx->state = next_state;
  747. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  748. RNDIS_IPA_STATE_DEBUG(rndis_ipa_ctx);
  749. if (next_state == RNDIS_IPA_CONNECTED_AND_UP)
  750. rndis_ipa_enable_data_path(rndis_ipa_ctx);
  751. else
  752. RNDIS_IPA_DEBUG("queue shall be started after open()\n");
  753. pr_info("RNDIS_IPA NetDev pipes were connected\n");
  754. RNDIS_IPA_LOG_EXIT();
  755. return 0;
  756. fail:
  757. rndis_ipa_deregister_pm_client(rndis_ipa_ctx);
  758. fail_register_pm:
  759. return result;
  760. }
  761. EXPORT_SYMBOL(rndis_ipa_pipe_connect_notify);
  762. /**
  763. * rndis_ipa_open() - notify Linux network stack to start sending packets
  764. * @net: the network interface supplied by the network stack
  765. *
  766. * Linux uses this API to notify the driver that the network interface
  767. * transitions to the up state.
  768. * The driver will instruct the Linux network stack to start
  769. * delivering data packets.
  770. * The driver internal state shall be changed to Up or Connected and Up
  771. *
  772. * Returns negative errno, or zero on success
  773. */
  774. static int rndis_ipa_open(struct net_device *net)
  775. {
  776. struct rndis_ipa_dev *rndis_ipa_ctx;
  777. int next_state;
  778. unsigned long flags;
  779. RNDIS_IPA_LOG_ENTRY();
  780. rndis_ipa_ctx = netdev_priv(net);
  781. spin_lock_irqsave(&rndis_ipa_ctx->state_lock, flags);
  782. next_state = rndis_ipa_next_state(rndis_ipa_ctx->state, RNDIS_IPA_OPEN);
  783. if (next_state == RNDIS_IPA_INVALID) {
  784. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  785. RNDIS_IPA_ERROR("can't bring driver up before initialize\n");
  786. return -EPERM;
  787. }
  788. rndis_ipa_ctx->state = next_state;
  789. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  790. RNDIS_IPA_STATE_DEBUG(rndis_ipa_ctx);
  791. if (next_state == RNDIS_IPA_CONNECTED_AND_UP)
  792. rndis_ipa_enable_data_path(rndis_ipa_ctx);
  793. else
  794. RNDIS_IPA_DEBUG("queue shall be started after connect()\n");
  795. pr_info("RNDIS_IPA NetDev was opened\n");
  796. RNDIS_IPA_LOG_EXIT();
  797. return 0;
  798. }
  799. /**
  800. * rndis_ipa_start_xmit() - send data from APPs to USB core via IPA core
  801. * using SW path (Tx data path)
  802. * Tx path for this Netdev is Apps-processor->IPA->USB
  803. * @skb: packet received from Linux network stack destined for tethered PC
  804. * @net: the network device being used to send this packet (rndis0)
  805. *
  806. * Several conditions needed in order to send the packet to IPA:
  807. * - Transmit queue for the network driver is currently
  808. * in "started" state
  809. * - The driver internal state is in Connected and Up state.
  810. * - Filters Tx switch are turned off
  811. * - The IPA resource manager state for the driver producer client
  812. * is "Granted" which implies that all the resources in the dependency
  813. * graph are valid for data flow.
  814. * - outstanding high boundary was not reached.
  815. *
  816. * In case the outstanding packets high boundary is reached, the driver will
  817. * stop the send queue until enough packets are processed by
  818. * the IPA core (based on calls to rndis_ipa_tx_complete_notify).
  819. *
  820. * In case all of the conditions are met, the network driver shall:
  821. * - encapsulate the Ethernet packet with RNDIS header (REMOTE_NDIS_PACKET_MSG)
  822. * - send the packet by using IPA Driver SW path (IP_PACKET_INIT)
  823. * - Netdev status fields shall be updated based on the current Tx packet
  824. *
  825. * Returns NETDEV_TX_BUSY if retry should be made later,
  826. * or NETDEV_TX_OK on success.
  827. */
  828. static netdev_tx_t rndis_ipa_start_xmit(struct sk_buff *skb,
  829. struct net_device *net)
  830. {
  831. int ret;
  832. netdev_tx_t status = NETDEV_TX_BUSY;
  833. struct rndis_ipa_dev *rndis_ipa_ctx = netdev_priv(net);
  834. netif_trans_update(net);
  835. RNDIS_IPA_DEBUG_XMIT
  836. ("Tx, len=%d, skb->protocol=%d, outstanding=%d\n",
  837. skb->len, skb->protocol,
  838. atomic_read(&rndis_ipa_ctx->outstanding_pkts));
  839. if (unlikely(netif_queue_stopped(net))) {
  840. RNDIS_IPA_ERROR("interface queue is stopped\n");
  841. goto out;
  842. }
  843. if (unlikely(rndis_ipa_ctx->tx_dump_enable))
  844. rndis_ipa_dump_skb(skb);
  845. if (unlikely(rndis_ipa_ctx->state != RNDIS_IPA_CONNECTED_AND_UP)) {
  846. RNDIS_IPA_ERROR("Missing pipe connected and/or iface up\n");
  847. return NETDEV_TX_BUSY;
  848. }
  849. if (unlikely(tx_filter(skb))) {
  850. dev_kfree_skb_any(skb);
  851. RNDIS_IPA_DEBUG("packet got filtered out on Tx path\n");
  852. rndis_ipa_ctx->tx_dropped++;
  853. status = NETDEV_TX_OK;
  854. goto out;
  855. }
  856. ret = ipa_pm_activate(rndis_ipa_ctx->pm_hdl);
  857. if (ret) {
  858. RNDIS_IPA_DEBUG("Failed activate PM client\n");
  859. netif_stop_queue(net);
  860. goto fail_pm_activate;
  861. }
  862. if (atomic_read(&rndis_ipa_ctx->outstanding_pkts) >=
  863. rndis_ipa_ctx->outstanding_high) {
  864. RNDIS_IPA_DEBUG("Outstanding high boundary reached (%d)\n",
  865. rndis_ipa_ctx->outstanding_high);
  866. netif_stop_queue(net);
  867. RNDIS_IPA_DEBUG("send queue was stopped\n");
  868. status = NETDEV_TX_BUSY;
  869. goto out;
  870. }
  871. skb = rndis_encapsulate_skb(skb, rndis_ipa_ctx);
  872. trace_rndis_tx_dp(skb->protocol);
  873. ret = ipa_tx_dp(IPA_TO_USB_CLIENT, skb, NULL);
  874. if (ret) {
  875. RNDIS_IPA_ERROR("ipa transmit failed (%d)\n", ret);
  876. goto fail_tx_packet;
  877. }
  878. atomic_inc(&rndis_ipa_ctx->outstanding_pkts);
  879. status = NETDEV_TX_OK;
  880. goto out;
  881. fail_tx_packet:
  882. rndis_ipa_xmit_error(skb);
  883. out:
  884. ipa_pm_deferred_deactivate(rndis_ipa_ctx->pm_hdl);
  885. fail_pm_activate:
  886. RNDIS_IPA_DEBUG
  887. ("packet Tx done - %s\n",
  888. (status == NETDEV_TX_OK) ? "OK" : "FAIL");
  889. return status;
  890. }
  891. /**
  892. * rndis_ipa_tx_complete_notify() - notification for Netdev that the
  893. * last packet was successfully sent
  894. * @private: driver context stashed by IPA driver upon pipe connect
  895. * @evt: event type (expected to be write-done event)
  896. * @data: data provided with event (this is actually the skb that
  897. * holds the sent packet)
  898. *
  899. * This function will be called on interrupt bottom halve deferred context.
  900. * outstanding packets counter shall be decremented.
  901. * Network stack send queue will be re-started in case low outstanding
  902. * boundary is reached and queue was stopped before.
  903. * At the end the skb shall be freed.
  904. */
  905. static void rndis_ipa_tx_complete_notify(
  906. void *private,
  907. enum ipa_dp_evt_type evt,
  908. unsigned long data)
  909. {
  910. struct sk_buff *skb = (struct sk_buff *)data;
  911. struct rndis_ipa_dev *rndis_ipa_ctx = private;
  912. int ret;
  913. ret = 0;
  914. NULL_CHECK_RETVAL(private);
  915. if (ret)
  916. return;
  917. trace_rndis_status_rcvd(skb->protocol);
  918. RNDIS_IPA_DEBUG
  919. ("Tx-complete, len=%d, skb->prot=%d, outstanding=%d\n",
  920. skb->len, skb->protocol,
  921. atomic_read(&rndis_ipa_ctx->outstanding_pkts));
  922. if (unlikely((evt != IPA_WRITE_DONE))) {
  923. RNDIS_IPA_ERROR("unsupported event on TX call-back\n");
  924. return;
  925. }
  926. if (unlikely(rndis_ipa_ctx->state != RNDIS_IPA_CONNECTED_AND_UP)) {
  927. RNDIS_IPA_DEBUG
  928. ("dropping Tx-complete pkt, state=%s\n",
  929. rndis_ipa_state_string(rndis_ipa_ctx->state));
  930. goto out;
  931. }
  932. rndis_ipa_ctx->net->stats.tx_packets++;
  933. rndis_ipa_ctx->net->stats.tx_bytes += skb->len;
  934. if (atomic_read(&rndis_ipa_ctx->outstanding_pkts) > 0)
  935. atomic_dec(&rndis_ipa_ctx->outstanding_pkts);
  936. if
  937. (netif_queue_stopped(rndis_ipa_ctx->net) &&
  938. netif_carrier_ok(rndis_ipa_ctx->net) &&
  939. atomic_read(&rndis_ipa_ctx->outstanding_pkts) <
  940. (rndis_ipa_ctx->outstanding_low)) {
  941. RNDIS_IPA_DEBUG("outstanding low boundary reached (%d)n",
  942. rndis_ipa_ctx->outstanding_low);
  943. netif_wake_queue(rndis_ipa_ctx->net);
  944. RNDIS_IPA_DEBUG("send queue was awaken\n");
  945. }
  946. out:
  947. dev_kfree_skb_any(skb);
  948. }
  949. static void rndis_ipa_tx_timeout(struct net_device *net)
  950. {
  951. struct rndis_ipa_dev *rndis_ipa_ctx = netdev_priv(net);
  952. int outstanding = atomic_read(&rndis_ipa_ctx->outstanding_pkts);
  953. RNDIS_IPA_ERROR
  954. ("possible IPA stall was detected, %d outstanding\n",
  955. outstanding);
  956. net->stats.tx_errors++;
  957. }
  958. /**
  959. * rndis_ipa_packet_receive_notify() - Rx notify for packet sent from
  960. * tethered PC (USB->IPA).
  961. * is USB->IPA->Apps-processor
  962. * @private: driver context
  963. * @evt: event type
  964. * @data: data provided with event
  965. *
  966. * Once IPA driver receives a packet from USB client this callback will be
  967. * called from bottom-half interrupt handling context (ipa Rx workqueue).
  968. *
  969. * Packets that shall be sent to Apps processor may be of two types:
  970. * 1) Packets that are destined for Apps (e.g: WEBSERVER running on Apps)
  971. * 2) Exception packets that need special handling (based on IPA core
  972. * configuration, e.g: new TCP session or any other packets that IPA core
  973. * can't handle)
  974. * If the next conditions are met, the packet shall be sent up to the
  975. * Linux network stack:
  976. * - Driver internal state is Connected and Up
  977. * - Notification received from IPA driver meets the expected type
  978. * for Rx packet
  979. * -Filters Rx switch are turned off
  980. *
  981. * Prior to the sending to the network stack:
  982. * - Netdev struct shall be stashed to the skb as required by the network stack
  983. * - Ethernet header shall be removed (skb->data shall point to the Ethernet
  984. * payload, Ethernet still stashed under MAC header).
  985. * - The skb->pkt_protocol shall be set based on the ethernet destination
  986. * address, Can be Broadcast, Multicast or Other-Host, The later
  987. * pkt-types packets shall be dropped in case the Netdev is not
  988. * in promisc mode.
  989. * - Set the skb protocol field based on the EtherType field
  990. *
  991. * Netdev status fields shall be updated based on the current Rx packet
  992. */
  993. static void rndis_ipa_packet_receive_notify(
  994. void *private,
  995. enum ipa_dp_evt_type evt,
  996. unsigned long data)
  997. {
  998. struct sk_buff *skb = (struct sk_buff *)data;
  999. struct rndis_ipa_dev *rndis_ipa_ctx = private;
  1000. int result;
  1001. unsigned int packet_len = skb->len;
  1002. RNDIS_IPA_DEBUG
  1003. ("packet Rx, len=%d\n",
  1004. skb->len);
  1005. if (unlikely(rndis_ipa_ctx->rx_dump_enable))
  1006. rndis_ipa_dump_skb(skb);
  1007. if (unlikely(rndis_ipa_ctx->state != RNDIS_IPA_CONNECTED_AND_UP)) {
  1008. RNDIS_IPA_DEBUG("use connect()/up() before receive()\n");
  1009. RNDIS_IPA_DEBUG("packet dropped (length=%d)\n",
  1010. skb->len);
  1011. return;
  1012. }
  1013. if (evt != IPA_RECEIVE) {
  1014. RNDIS_IPA_ERROR("a none IPA_RECEIVE event in driver RX\n");
  1015. return;
  1016. }
  1017. if (!rndis_ipa_ctx->deaggregation_enable)
  1018. skb_pull(skb, sizeof(struct rndis_pkt_hdr));
  1019. skb->dev = rndis_ipa_ctx->net;
  1020. skb->protocol = eth_type_trans(skb, rndis_ipa_ctx->net);
  1021. if (rx_filter(skb)) {
  1022. RNDIS_IPA_DEBUG("packet got filtered out on RX path\n");
  1023. rndis_ipa_ctx->rx_dropped++;
  1024. dev_kfree_skb_any(skb);
  1025. return;
  1026. }
  1027. trace_rndis_netif_ni(skb->protocol);
  1028. result = rndis_ipa_ctx->netif_rx_function(skb);
  1029. if (unlikely(result))
  1030. RNDIS_IPA_ERROR("fail on netif_rx_function\n");
  1031. rndis_ipa_ctx->net->stats.rx_packets++;
  1032. rndis_ipa_ctx->net->stats.rx_bytes += packet_len;
  1033. }
  1034. /** rndis_ipa_stop() - notify the network interface to stop
  1035. * sending/receiving data
  1036. * @net: the network device being stopped.
  1037. *
  1038. * This API is used by Linux network stack to notify the network driver that
  1039. * its state was changed to "down"
  1040. * The driver will stop the "send" queue and change its internal
  1041. * state to "Connected".
  1042. * The Netdev shall be returned to be "Up" after rndis_ipa_open().
  1043. */
  1044. static int rndis_ipa_stop(struct net_device *net)
  1045. {
  1046. struct rndis_ipa_dev *rndis_ipa_ctx = netdev_priv(net);
  1047. int next_state;
  1048. unsigned long flags;
  1049. RNDIS_IPA_LOG_ENTRY();
  1050. spin_lock_irqsave(&rndis_ipa_ctx->state_lock, flags);
  1051. next_state = rndis_ipa_next_state(rndis_ipa_ctx->state, RNDIS_IPA_STOP);
  1052. if (next_state == RNDIS_IPA_INVALID) {
  1053. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  1054. RNDIS_IPA_DEBUG("can't do network interface down without up\n");
  1055. return -EPERM;
  1056. }
  1057. rndis_ipa_ctx->state = next_state;
  1058. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  1059. netif_stop_queue(net);
  1060. pr_info("RNDIS_IPA NetDev queue is stopped\n");
  1061. RNDIS_IPA_STATE_DEBUG(rndis_ipa_ctx);
  1062. RNDIS_IPA_LOG_EXIT();
  1063. return 0;
  1064. }
  1065. /** rndis_ipa_disconnect() - notify rndis_ipa Netdev that the USB pipes
  1066. * were disconnected
  1067. * @private: same value that was set by init(), this parameter holds the
  1068. * network device pointer.
  1069. *
  1070. * USB shall notify the Netdev after disconnecting the pipe.
  1071. * - The internal driver state shall returned to its previous
  1072. * state (Up or Initialized).
  1073. * - Linux network stack shall be informed for carrier off to notify
  1074. * user space for pipe disconnect
  1075. * - send queue shall be stopped
  1076. * During the transition between the pipe disconnection to
  1077. * the Netdev notification packets
  1078. * are expected to be dropped by IPA driver or IPA core.
  1079. */
  1080. int rndis_ipa_pipe_disconnect_notify(void *private)
  1081. {
  1082. struct rndis_ipa_dev *rndis_ipa_ctx = private;
  1083. int next_state;
  1084. int outstanding_dropped_pkts;
  1085. int retval;
  1086. int ret;
  1087. unsigned long flags;
  1088. struct ipa_ecm_msg *rndis_msg;
  1089. struct ipa_msg_meta msg_meta;
  1090. RNDIS_IPA_LOG_ENTRY();
  1091. ret = 0;
  1092. NULL_CHECK_RETVAL(rndis_ipa_ctx);
  1093. if (ret)
  1094. return ret;
  1095. RNDIS_IPA_DEBUG("private=0x%pK\n", private);
  1096. spin_lock_irqsave(&rndis_ipa_ctx->state_lock, flags);
  1097. next_state = rndis_ipa_next_state
  1098. (rndis_ipa_ctx->state,
  1099. RNDIS_IPA_DISCONNECT);
  1100. if (next_state == RNDIS_IPA_INVALID) {
  1101. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  1102. RNDIS_IPA_ERROR("can't disconnect before connect\n");
  1103. return -EPERM;
  1104. }
  1105. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  1106. if (rndis_ipa_ctx->during_xmit_error) {
  1107. RNDIS_IPA_DEBUG("canceling xmit-error delayed work\n");
  1108. cancel_delayed_work_sync(
  1109. &rndis_ipa_ctx->xmit_error_delayed_work);
  1110. rndis_ipa_ctx->during_xmit_error = false;
  1111. }
  1112. netif_carrier_off(rndis_ipa_ctx->net);
  1113. RNDIS_IPA_DEBUG("carrier_off notification was sent\n");
  1114. rndis_msg = kzalloc(sizeof(*rndis_msg), GFP_KERNEL);
  1115. if (!rndis_msg)
  1116. return -ENOMEM;
  1117. memset(&msg_meta, 0, sizeof(struct ipa_msg_meta));
  1118. msg_meta.msg_type = ECM_DISCONNECT;
  1119. msg_meta.msg_len = sizeof(struct ipa_ecm_msg);
  1120. strlcpy(rndis_msg->name, rndis_ipa_ctx->net->name,
  1121. IPA_RESOURCE_NAME_MAX);
  1122. rndis_msg->ifindex = rndis_ipa_ctx->net->ifindex;
  1123. retval = ipa_send_msg(&msg_meta, rndis_msg, rndis_ipa_msg_free_cb);
  1124. if (retval) {
  1125. RNDIS_IPA_ERROR("fail to send ECM_DISCONNECT for rndis\n");
  1126. kfree(rndis_msg);
  1127. return -EPERM;
  1128. }
  1129. netif_stop_queue(rndis_ipa_ctx->net);
  1130. RNDIS_IPA_DEBUG("queue stopped\n");
  1131. outstanding_dropped_pkts =
  1132. atomic_read(&rndis_ipa_ctx->outstanding_pkts);
  1133. rndis_ipa_ctx->net->stats.tx_dropped += outstanding_dropped_pkts;
  1134. atomic_set(&rndis_ipa_ctx->outstanding_pkts, 0);
  1135. retval = rndis_ipa_deregister_pm_client(rndis_ipa_ctx);
  1136. if (retval) {
  1137. RNDIS_IPA_ERROR("Fail to deregister PM\n");
  1138. return retval;
  1139. }
  1140. RNDIS_IPA_DEBUG("PM was successfully deregistered\n");
  1141. spin_lock_irqsave(&rndis_ipa_ctx->state_lock, flags);
  1142. next_state = rndis_ipa_next_state(rndis_ipa_ctx->state,
  1143. RNDIS_IPA_DISCONNECT);
  1144. if (next_state == RNDIS_IPA_INVALID) {
  1145. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  1146. RNDIS_IPA_ERROR("can't disconnect before connect\n");
  1147. return -EPERM;
  1148. }
  1149. rndis_ipa_ctx->state = next_state;
  1150. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  1151. RNDIS_IPA_STATE_DEBUG(rndis_ipa_ctx);
  1152. pr_info("RNDIS_IPA NetDev pipes disconnected (%d outstanding clr)\n",
  1153. outstanding_dropped_pkts);
  1154. RNDIS_IPA_LOG_EXIT();
  1155. return 0;
  1156. }
  1157. EXPORT_SYMBOL(rndis_ipa_pipe_disconnect_notify);
  1158. /**
  1159. * rndis_ipa_cleanup() - unregister the network interface driver and free
  1160. * internal data structs.
  1161. * @private: same value that was set by init(), this
  1162. * parameter holds the network device pointer.
  1163. *
  1164. * This function shall be called once the network interface is not
  1165. * needed anymore, e.g: when the USB composition does not support it.
  1166. * This function shall be called after the pipes were disconnected.
  1167. * Detailed description:
  1168. * - remove header-insertion headers from IPA core
  1169. * - delete the driver dependency defined for IPA resource manager and
  1170. * destroy the producer resource.
  1171. * - remove the debugfs entries
  1172. * - deregister the network interface from Linux network stack
  1173. * - free all internal data structs
  1174. *
  1175. * It is assumed that no packets shall be sent through HW bridging
  1176. * during cleanup to avoid packets trying to add an header that is
  1177. * removed during cleanup (IPA configuration manager should have
  1178. * removed them at this point)
  1179. */
  1180. void rndis_ipa_cleanup(void *private)
  1181. {
  1182. struct rndis_ipa_dev *rndis_ipa_ctx = private;
  1183. int next_state;
  1184. int ret;
  1185. unsigned long flags;
  1186. RNDIS_IPA_LOG_ENTRY();
  1187. RNDIS_IPA_DEBUG("private=0x%pK\n", private);
  1188. ret = 0;
  1189. NULL_CHECK_RETVAL(rndis_ipa_ctx);
  1190. if (ret)
  1191. return;
  1192. spin_lock_irqsave(&rndis_ipa_ctx->state_lock, flags);
  1193. next_state = rndis_ipa_next_state
  1194. (rndis_ipa_ctx->state,
  1195. RNDIS_IPA_CLEANUP);
  1196. if (next_state == RNDIS_IPA_INVALID) {
  1197. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  1198. RNDIS_IPA_ERROR("use disconnect()before clean()\n");
  1199. return;
  1200. }
  1201. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  1202. RNDIS_IPA_STATE_DEBUG(rndis_ipa_ctx);
  1203. ret = rndis_ipa_deregister_properties(rndis_ipa_ctx->net->name);
  1204. if (ret) {
  1205. RNDIS_IPA_ERROR("Fail to deregister Tx/Rx properties\n");
  1206. return;
  1207. }
  1208. RNDIS_IPA_DEBUG("deregister Tx/Rx properties was successful\n");
  1209. ret = rndis_ipa_hdrs_destroy(rndis_ipa_ctx);
  1210. if (ret)
  1211. RNDIS_IPA_ERROR(
  1212. "Failed removing RNDIS headers from IPA core. Continue anyway\n");
  1213. else
  1214. RNDIS_IPA_DEBUG("RNDIS headers were removed from IPA core\n");
  1215. rndis_ipa_debugfs_destroy(rndis_ipa_ctx);
  1216. RNDIS_IPA_DEBUG("debugfs remove was done\n");
  1217. unregister_netdev(rndis_ipa_ctx->net);
  1218. RNDIS_IPA_DEBUG("netdev unregistered\n");
  1219. spin_lock_irqsave(&rndis_ipa_ctx->state_lock, flags);
  1220. next_state = rndis_ipa_next_state(rndis_ipa_ctx->state,
  1221. RNDIS_IPA_CLEANUP);
  1222. if (next_state == RNDIS_IPA_INVALID) {
  1223. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  1224. RNDIS_IPA_ERROR("use disconnect()before clean()\n");
  1225. return;
  1226. }
  1227. rndis_ipa_ctx->state = next_state;
  1228. spin_unlock_irqrestore(&rndis_ipa_ctx->state_lock, flags);
  1229. free_netdev(rndis_ipa_ctx->net);
  1230. pr_info("RNDIS_IPA NetDev was cleaned\n");
  1231. RNDIS_IPA_LOG_EXIT();
  1232. }
  1233. EXPORT_SYMBOL(rndis_ipa_cleanup);
  1234. static void rndis_ipa_enable_data_path(struct rndis_ipa_dev *rndis_ipa_ctx)
  1235. {
  1236. if (rndis_ipa_ctx->device_ready_notify) {
  1237. rndis_ipa_ctx->device_ready_notify();
  1238. RNDIS_IPA_DEBUG("USB device_ready_notify() was called\n");
  1239. } else {
  1240. RNDIS_IPA_DEBUG("device_ready_notify() not supplied\n");
  1241. }
  1242. netif_start_queue(rndis_ipa_ctx->net);
  1243. RNDIS_IPA_DEBUG("netif_start_queue() was called\n");
  1244. }
  1245. static void rndis_ipa_xmit_error(struct sk_buff *skb)
  1246. {
  1247. bool retval;
  1248. struct rndis_ipa_dev *rndis_ipa_ctx = netdev_priv(skb->dev);
  1249. unsigned long delay_jiffies;
  1250. u8 rand_dealy_msec;
  1251. RNDIS_IPA_LOG_ENTRY();
  1252. RNDIS_IPA_DEBUG("starting Tx-queue backoff\n");
  1253. netif_stop_queue(rndis_ipa_ctx->net);
  1254. RNDIS_IPA_DEBUG("netif_stop_queue was called\n");
  1255. skb_pull(skb, sizeof(rndis_template_hdr));
  1256. rndis_ipa_ctx->net->stats.tx_errors++;
  1257. get_random_bytes(&rand_dealy_msec, sizeof(rand_dealy_msec));
  1258. delay_jiffies = msecs_to_jiffies(
  1259. rndis_ipa_ctx->error_msec_sleep_time + rand_dealy_msec);
  1260. retval = schedule_delayed_work(
  1261. &rndis_ipa_ctx->xmit_error_delayed_work, delay_jiffies);
  1262. if (!retval) {
  1263. RNDIS_IPA_ERROR("fail to schedule delayed work\n");
  1264. netif_start_queue(rndis_ipa_ctx->net);
  1265. } else {
  1266. RNDIS_IPA_DEBUG
  1267. ("work scheduled to start Tx-queue in %d msec\n",
  1268. rndis_ipa_ctx->error_msec_sleep_time +
  1269. rand_dealy_msec);
  1270. rndis_ipa_ctx->during_xmit_error = true;
  1271. }
  1272. RNDIS_IPA_LOG_EXIT();
  1273. }
  1274. static void rndis_ipa_xmit_error_aftercare_wq(struct work_struct *work)
  1275. {
  1276. struct rndis_ipa_dev *rndis_ipa_ctx;
  1277. struct delayed_work *delayed_work;
  1278. RNDIS_IPA_LOG_ENTRY();
  1279. RNDIS_IPA_DEBUG("Starting queue after xmit error\n");
  1280. delayed_work = to_delayed_work(work);
  1281. rndis_ipa_ctx = container_of
  1282. (delayed_work, struct rndis_ipa_dev,
  1283. xmit_error_delayed_work);
  1284. if (unlikely(rndis_ipa_ctx->state != RNDIS_IPA_CONNECTED_AND_UP)) {
  1285. RNDIS_IPA_ERROR
  1286. ("error aftercare handling in bad state (%d)",
  1287. rndis_ipa_ctx->state);
  1288. return;
  1289. }
  1290. rndis_ipa_ctx->during_xmit_error = false;
  1291. netif_start_queue(rndis_ipa_ctx->net);
  1292. RNDIS_IPA_DEBUG("netif_start_queue() was called\n");
  1293. RNDIS_IPA_LOG_EXIT();
  1294. }
  1295. /**
  1296. * rndis_ipa_prepare_header_insertion() - prepare the header insertion request
  1297. * for IPA driver
  1298. * eth_type: the Ethernet type for this header-insertion header
  1299. * hdr_name: string that shall represent this header in IPA data base
  1300. * add_hdr: output for caller to be used with ipa_add_hdr() to configure
  1301. * the IPA core
  1302. * dst_mac: tethered PC MAC (Ethernet) address to be added to packets
  1303. * for IPA->USB pipe
  1304. * src_mac: device MAC (Ethernet) address to be added to packets
  1305. * for IPA->USB pipe
  1306. * is_vlan_mode: should driver work in vlan mode?
  1307. *
  1308. * This function shall build the header-insertion block request for a
  1309. * single Ethernet+RNDIS header)
  1310. * this header shall be inserted for packets processed by IPA
  1311. * and destined for USB client.
  1312. * This header shall be used for HW bridging for packets destined for
  1313. * tethered PC.
  1314. * For SW data-path, this header won't be used.
  1315. */
  1316. static void rndis_ipa_prepare_header_insertion(
  1317. int eth_type,
  1318. const char *hdr_name, struct ipa_hdr_add *add_hdr,
  1319. const void *dst_mac, const void *src_mac, bool is_vlan_mode)
  1320. {
  1321. struct ethhdr *eth_hdr;
  1322. struct vlan_ethhdr *eth_vlan_hdr;
  1323. add_hdr->hdr_len = sizeof(rndis_template_hdr);
  1324. add_hdr->is_partial = false;
  1325. strlcpy(add_hdr->name, hdr_name, IPA_RESOURCE_NAME_MAX);
  1326. memcpy(add_hdr->hdr, &rndis_template_hdr, sizeof(rndis_template_hdr));
  1327. add_hdr->is_eth2_ofst_valid = true;
  1328. add_hdr->eth2_ofst = sizeof(rndis_template_hdr);
  1329. if (is_vlan_mode) {
  1330. eth_vlan_hdr = (struct vlan_ethhdr *)(add_hdr->hdr +
  1331. sizeof(rndis_template_hdr));
  1332. memcpy(eth_vlan_hdr->h_dest, dst_mac, ETH_ALEN);
  1333. memcpy(eth_vlan_hdr->h_source, src_mac, ETH_ALEN);
  1334. eth_vlan_hdr->h_vlan_encapsulated_proto = htons(eth_type);
  1335. eth_vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
  1336. add_hdr->hdr_len += VLAN_ETH_HLEN;
  1337. add_hdr->type = IPA_HDR_L2_802_1Q;
  1338. } else {
  1339. eth_hdr = (struct ethhdr *)(add_hdr->hdr +
  1340. sizeof(rndis_template_hdr));
  1341. memcpy(eth_hdr->h_dest, dst_mac, ETH_ALEN);
  1342. memcpy(eth_hdr->h_source, src_mac, ETH_ALEN);
  1343. eth_hdr->h_proto = htons(eth_type);
  1344. add_hdr->hdr_len += ETH_HLEN;
  1345. add_hdr->type = IPA_HDR_L2_ETHERNET_II;
  1346. }
  1347. }
  1348. /**
  1349. * rndis_ipa_hdrs_cfg() - configure header insertion block in IPA core
  1350. * to allow HW bridging
  1351. * @rndis_ipa_ctx: main driver context
  1352. * @dst_mac: destination MAC address (tethered PC)
  1353. * @src_mac: source MAC address (MDM device)
  1354. *
  1355. * This function shall add 2 headers.
  1356. * One header for Ipv4 and one header for Ipv6.
  1357. * Both headers shall contain Ethernet header and RNDIS header, the only
  1358. * difference shall be in the EtherTye field.
  1359. * Headers will be committed to HW
  1360. *
  1361. * Returns negative errno, or zero on success
  1362. */
  1363. static int rndis_ipa_hdrs_cfg(
  1364. struct rndis_ipa_dev *rndis_ipa_ctx,
  1365. const void *dst_mac, const void *src_mac)
  1366. {
  1367. struct ipa_ioc_add_hdr *hdrs;
  1368. struct ipa_hdr_add *ipv4_hdr;
  1369. struct ipa_hdr_add *ipv6_hdr;
  1370. int result = 0;
  1371. RNDIS_IPA_LOG_ENTRY();
  1372. hdrs = kzalloc
  1373. (sizeof(*hdrs) + sizeof(*ipv4_hdr) + sizeof(*ipv6_hdr),
  1374. GFP_KERNEL);
  1375. if (!hdrs) {
  1376. result = -ENOMEM;
  1377. goto fail_mem;
  1378. }
  1379. ipv4_hdr = &hdrs->hdr[0];
  1380. ipv6_hdr = &hdrs->hdr[1];
  1381. rndis_ipa_prepare_header_insertion
  1382. (ETH_P_IP, IPV4_HDR_NAME,
  1383. ipv4_hdr, dst_mac, src_mac, rndis_ipa_ctx->is_vlan_mode);
  1384. rndis_ipa_prepare_header_insertion
  1385. (ETH_P_IPV6, IPV6_HDR_NAME,
  1386. ipv6_hdr, dst_mac, src_mac, rndis_ipa_ctx->is_vlan_mode);
  1387. hdrs->commit = 1;
  1388. hdrs->num_hdrs = 2;
  1389. result = ipa_add_hdr(hdrs);
  1390. if (result) {
  1391. RNDIS_IPA_ERROR("Fail on Header-Insertion(%d)\n", result);
  1392. goto fail_add_hdr;
  1393. }
  1394. if (ipv4_hdr->status) {
  1395. RNDIS_IPA_ERROR("Fail on Header-Insertion ipv4(%d)\n",
  1396. ipv4_hdr->status);
  1397. result = ipv4_hdr->status;
  1398. goto fail_add_hdr;
  1399. }
  1400. if (ipv6_hdr->status) {
  1401. RNDIS_IPA_ERROR("Fail on Header-Insertion ipv6(%d)\n",
  1402. ipv6_hdr->status);
  1403. result = ipv6_hdr->status;
  1404. goto fail_add_hdr;
  1405. }
  1406. rndis_ipa_ctx->eth_ipv4_hdr_hdl = ipv4_hdr->hdr_hdl;
  1407. rndis_ipa_ctx->eth_ipv6_hdr_hdl = ipv6_hdr->hdr_hdl;
  1408. RNDIS_IPA_LOG_EXIT();
  1409. fail_add_hdr:
  1410. kfree(hdrs);
  1411. fail_mem:
  1412. return result;
  1413. }
  1414. /**
  1415. * rndis_ipa_hdrs_destroy() - remove the IPA core configuration done for
  1416. * the driver data path bridging.
  1417. * @rndis_ipa_ctx: the driver context
  1418. *
  1419. * Revert the work done on rndis_ipa_hdrs_cfg(), which is,
  1420. * remove 2 headers for Ethernet+RNDIS.
  1421. */
  1422. static int rndis_ipa_hdrs_destroy(struct rndis_ipa_dev *rndis_ipa_ctx)
  1423. {
  1424. struct ipa_ioc_del_hdr *del_hdr;
  1425. struct ipa_hdr_del *ipv4;
  1426. struct ipa_hdr_del *ipv6;
  1427. int result;
  1428. del_hdr = kzalloc(sizeof(*del_hdr) + sizeof(*ipv4) +
  1429. sizeof(*ipv6), GFP_KERNEL);
  1430. if (!del_hdr)
  1431. return -ENOMEM;
  1432. del_hdr->commit = 1;
  1433. del_hdr->num_hdls = 2;
  1434. ipv4 = &del_hdr->hdl[0];
  1435. ipv4->hdl = rndis_ipa_ctx->eth_ipv4_hdr_hdl;
  1436. ipv6 = &del_hdr->hdl[1];
  1437. ipv6->hdl = rndis_ipa_ctx->eth_ipv6_hdr_hdl;
  1438. result = ipa_del_hdr(del_hdr);
  1439. if (result || ipv4->status || ipv6->status)
  1440. RNDIS_IPA_ERROR("ipa_del_hdr failed\n");
  1441. else
  1442. RNDIS_IPA_DEBUG("hdrs deletion done\n");
  1443. kfree(del_hdr);
  1444. return result;
  1445. }
  1446. static struct net_device_stats *rndis_ipa_get_stats(struct net_device *net)
  1447. {
  1448. return &net->stats;
  1449. }
  1450. /**
  1451. * rndis_ipa_register_properties() - set Tx/Rx properties needed
  1452. * by IPA configuration manager
  1453. * @netdev_name: a string with the name of the network interface device
  1454. * @is_vlan_mode: should driver work in vlan mode?
  1455. *
  1456. * Register Tx/Rx properties to allow user space configuration (IPA
  1457. * Configuration Manager):
  1458. *
  1459. * - Two Tx properties (IPA->USB): specify the header names and pipe number
  1460. * that shall be used by user space for header-addition configuration
  1461. * for ipv4/ipv6 packets flowing from IPA to USB for HW bridging data.
  1462. * That header-addition header is added by the Netdev and used by user
  1463. * space to close the the HW bridge by adding filtering and routing rules
  1464. * that point to this header.
  1465. *
  1466. * - Two Rx properties (USB->IPA): these properties shall be used by user space
  1467. * to configure the IPA core to identify the packets destined
  1468. * for Apps-processor by configuring the unicast rules destined for
  1469. * the Netdev IP address.
  1470. * This rules shall be added based on the attribute mask supplied at
  1471. * this function, that is, always hit rule.
  1472. */
  1473. static int rndis_ipa_register_properties(char *netdev_name, bool is_vlan_mode)
  1474. {
  1475. struct ipa_tx_intf tx_properties = {0};
  1476. struct ipa_ioc_tx_intf_prop properties[2] = { {0}, {0} };
  1477. struct ipa_ioc_tx_intf_prop *ipv4_property;
  1478. struct ipa_ioc_tx_intf_prop *ipv6_property;
  1479. struct ipa_ioc_rx_intf_prop rx_ioc_properties[2] = { {0}, {0} };
  1480. struct ipa_rx_intf rx_properties = {0};
  1481. struct ipa_ioc_rx_intf_prop *rx_ipv4_property;
  1482. struct ipa_ioc_rx_intf_prop *rx_ipv6_property;
  1483. enum ipa_hdr_l2_type hdr_l2_type = IPA_HDR_L2_ETHERNET_II;
  1484. int result = 0;
  1485. RNDIS_IPA_LOG_ENTRY();
  1486. if (is_vlan_mode)
  1487. hdr_l2_type = IPA_HDR_L2_802_1Q;
  1488. tx_properties.prop = properties;
  1489. ipv4_property = &tx_properties.prop[0];
  1490. ipv4_property->ip = IPA_IP_v4;
  1491. ipv4_property->dst_pipe = IPA_TO_USB_CLIENT;
  1492. strlcpy
  1493. (ipv4_property->hdr_name, IPV4_HDR_NAME,
  1494. IPA_RESOURCE_NAME_MAX);
  1495. ipv4_property->hdr_l2_type = hdr_l2_type;
  1496. ipv6_property = &tx_properties.prop[1];
  1497. ipv6_property->ip = IPA_IP_v6;
  1498. ipv6_property->dst_pipe = IPA_TO_USB_CLIENT;
  1499. strlcpy
  1500. (ipv6_property->hdr_name, IPV6_HDR_NAME,
  1501. IPA_RESOURCE_NAME_MAX);
  1502. ipv6_property->hdr_l2_type = hdr_l2_type;
  1503. tx_properties.num_props = 2;
  1504. rx_properties.prop = rx_ioc_properties;
  1505. rx_ipv4_property = &rx_properties.prop[0];
  1506. rx_ipv4_property->ip = IPA_IP_v4;
  1507. rx_ipv4_property->attrib.attrib_mask = 0;
  1508. rx_ipv4_property->src_pipe = IPA_CLIENT_USB_PROD;
  1509. rx_ipv4_property->hdr_l2_type = hdr_l2_type;
  1510. rx_ipv6_property = &rx_properties.prop[1];
  1511. rx_ipv6_property->ip = IPA_IP_v6;
  1512. rx_ipv6_property->attrib.attrib_mask = 0;
  1513. rx_ipv6_property->src_pipe = IPA_CLIENT_USB_PROD;
  1514. rx_ipv6_property->hdr_l2_type = hdr_l2_type;
  1515. rx_properties.num_props = 2;
  1516. result = ipa_register_intf("rndis0", &tx_properties, &rx_properties);
  1517. if (result)
  1518. RNDIS_IPA_ERROR("fail on Tx/Rx properties registration\n");
  1519. else
  1520. RNDIS_IPA_DEBUG("Tx/Rx properties registration done\n");
  1521. RNDIS_IPA_LOG_EXIT();
  1522. return result;
  1523. }
  1524. /**
  1525. * rndis_ipa_deregister_properties() - remove the 2 Tx and 2 Rx properties
  1526. * @netdev_name: a string with the name of the network interface device
  1527. *
  1528. * This function revert the work done on rndis_ipa_register_properties().
  1529. */
  1530. static int rndis_ipa_deregister_properties(char *netdev_name)
  1531. {
  1532. int result;
  1533. RNDIS_IPA_LOG_ENTRY();
  1534. result = ipa_deregister_intf(netdev_name);
  1535. if (result) {
  1536. RNDIS_IPA_DEBUG("Fail on Tx prop deregister\n");
  1537. return result;
  1538. }
  1539. RNDIS_IPA_LOG_EXIT();
  1540. return 0;
  1541. }
  1542. static void rndis_ipa_pm_cb(void *p, enum ipa_pm_cb_event event)
  1543. {
  1544. struct rndis_ipa_dev *rndis_ipa_ctx = p;
  1545. RNDIS_IPA_LOG_ENTRY();
  1546. if (event != IPA_PM_CLIENT_ACTIVATED) {
  1547. RNDIS_IPA_ERROR("unexpected event %d\n", event);
  1548. WARN_ON(1);
  1549. return;
  1550. }
  1551. RNDIS_IPA_DEBUG("Resource Granted\n");
  1552. if (netif_queue_stopped(rndis_ipa_ctx->net)) {
  1553. RNDIS_IPA_DEBUG("starting queue\n");
  1554. netif_start_queue(rndis_ipa_ctx->net);
  1555. } else {
  1556. RNDIS_IPA_DEBUG("queue already awake\n");
  1557. }
  1558. RNDIS_IPA_LOG_EXIT();
  1559. }
  1560. static int rndis_ipa_register_pm_client(struct rndis_ipa_dev *rndis_ipa_ctx)
  1561. {
  1562. int result;
  1563. struct ipa_pm_register_params pm_reg;
  1564. memset(&pm_reg, 0, sizeof(pm_reg));
  1565. pm_reg.name = rndis_ipa_ctx->net->name;
  1566. pm_reg.user_data = rndis_ipa_ctx;
  1567. pm_reg.callback = rndis_ipa_pm_cb;
  1568. pm_reg.group = IPA_PM_GROUP_APPS;
  1569. result = ipa_pm_register(&pm_reg, &rndis_ipa_ctx->pm_hdl);
  1570. if (result) {
  1571. RNDIS_IPA_ERROR("failed to create IPA PM client %d\n", result);
  1572. return result;
  1573. }
  1574. return 0;
  1575. }
  1576. static int rndis_ipa_deregister_pm_client(struct rndis_ipa_dev *rndis_ipa_ctx)
  1577. {
  1578. ipa_pm_deactivate_sync(rndis_ipa_ctx->pm_hdl);
  1579. ipa_pm_deregister(rndis_ipa_ctx->pm_hdl);
  1580. rndis_ipa_ctx->pm_hdl = ~0;
  1581. return 0;
  1582. }
  1583. /**
  1584. * rndis_encapsulate_skb() - encapsulate the given Ethernet skb with
  1585. * an RNDIS header
  1586. * @skb: packet to be encapsulated with the RNDIS header
  1587. * @rndis_ipa_ctx: main driver context
  1588. *
  1589. * Shall use a template header for RNDIS and update it with the given
  1590. * skb values.
  1591. * Ethernet is expected to be already encapsulate the packet.
  1592. */
  1593. static struct sk_buff *rndis_encapsulate_skb(struct sk_buff *skb,
  1594. struct rndis_ipa_dev *rndis_ipa_ctx)
  1595. {
  1596. struct rndis_pkt_hdr *rndis_hdr;
  1597. int payload_byte_len = skb->len;
  1598. /* if there is no room in this skb, allocate a new one */
  1599. if (unlikely(skb_headroom(skb) < sizeof(rndis_template_hdr))) {
  1600. struct sk_buff *new_skb = skb_copy_expand(skb,
  1601. sizeof(rndis_template_hdr), 0, GFP_ATOMIC);
  1602. if (!new_skb) {
  1603. RNDIS_IPA_ERROR("no memory for skb expand\n");
  1604. return skb;
  1605. }
  1606. RNDIS_IPA_DEBUG("skb expanded. old %pK new %pK\n",
  1607. skb, new_skb);
  1608. dev_kfree_skb_any(skb);
  1609. skb = new_skb;
  1610. }
  1611. if (rndis_ipa_ctx->is_vlan_mode)
  1612. if (unlikely(skb->protocol != htons(ETH_P_8021Q)))
  1613. RNDIS_IPA_DEBUG(
  1614. "ether_type != ETH_P_8021Q && vlan, prot = 0x%X\n"
  1615. , skb->protocol);
  1616. /* make room at the head of the SKB to put the RNDIS header */
  1617. rndis_hdr = (struct rndis_pkt_hdr *)skb_push(skb,
  1618. sizeof(rndis_template_hdr));
  1619. memcpy(rndis_hdr, &rndis_template_hdr, sizeof(*rndis_hdr));
  1620. rndis_hdr->msg_len += payload_byte_len;
  1621. rndis_hdr->data_len += payload_byte_len;
  1622. return skb;
  1623. }
  1624. /**
  1625. * rx_filter() - logic that decide if the current skb is to be filtered out
  1626. * @skb: skb that may be sent up to the network stack
  1627. *
  1628. * This function shall do Rx packet filtering on the Netdev level.
  1629. */
  1630. static bool rx_filter(struct sk_buff *skb)
  1631. {
  1632. struct rndis_ipa_dev *rndis_ipa_ctx = netdev_priv(skb->dev);
  1633. return rndis_ipa_ctx->rx_filter;
  1634. }
  1635. /**
  1636. * tx_filter() - logic that decide if the current skb is to be filtered out
  1637. * @skb: skb that may be sent to the USB core
  1638. *
  1639. * This function shall do Tx packet filtering on the Netdev level.
  1640. * ICMP filter bypass is possible to allow only ICMP packet to be
  1641. * sent (pings and etc)
  1642. */
  1643. static bool tx_filter(struct sk_buff *skb)
  1644. {
  1645. struct rndis_ipa_dev *rndis_ipa_ctx = netdev_priv(skb->dev);
  1646. bool is_icmp;
  1647. if (likely(!rndis_ipa_ctx->tx_filter))
  1648. return false;
  1649. is_icmp = (skb->protocol == htons(ETH_P_IP) &&
  1650. ip_hdr(skb)->protocol == IPPROTO_ICMP);
  1651. if ((!rndis_ipa_ctx->icmp_filter) && is_icmp)
  1652. return false;
  1653. return true;
  1654. }
  1655. /**
  1656. * rndis_ipa_ep_registers_cfg() - configure the USB endpoints
  1657. * @usb_to_ipa_hdl: handle received from ipa_connect which represents
  1658. * the USB to IPA end-point
  1659. * @ipa_to_usb_hdl: handle received from ipa_connect which represents
  1660. * the IPA to USB end-point
  1661. * @max_xfer_size_bytes_to_dev: the maximum size, in bytes, that the device
  1662. * expects to receive from the host. supplied on REMOTE_NDIS_INITIALIZE_CMPLT.
  1663. * @max_xfer_size_bytes_to_host: the maximum size, in bytes, that the host
  1664. * expects to receive from the device. supplied on REMOTE_NDIS_INITIALIZE_MSG.
  1665. * @mtu: the netdev MTU size, in bytes
  1666. * @deaggr_enable: should deaggregation be enabled?
  1667. * @is_vlan_mode: should driver work in vlan mode?
  1668. *
  1669. * USB to IPA pipe:
  1670. * - de-aggregation
  1671. * - Remove Ethernet header
  1672. * - Remove RNDIS header
  1673. * - SRC NAT
  1674. * - Default routing(0)
  1675. * IPA to USB Pipe:
  1676. * - aggregation
  1677. * - Add Ethernet header
  1678. * - Add RNDIS header
  1679. */
  1680. static int rndis_ipa_ep_registers_cfg(
  1681. u32 usb_to_ipa_hdl,
  1682. u32 ipa_to_usb_hdl,
  1683. u32 max_xfer_size_bytes_to_dev,
  1684. u32 max_xfer_size_bytes_to_host,
  1685. u32 mtu,
  1686. bool deaggr_enable,
  1687. bool is_vlan_mode)
  1688. {
  1689. int result;
  1690. struct ipa_ep_cfg *usb_to_ipa_ep_cfg;
  1691. int add = 0;
  1692. if (deaggr_enable) {
  1693. usb_to_ipa_ep_cfg = &usb_to_ipa_ep_cfg_deaggr_en;
  1694. RNDIS_IPA_DEBUG("deaggregation enabled\n");
  1695. } else {
  1696. usb_to_ipa_ep_cfg = &usb_to_ipa_ep_cfg_deaggr_dis;
  1697. RNDIS_IPA_DEBUG("deaggregation disabled\n");
  1698. add = sizeof(struct rndis_pkt_hdr);
  1699. }
  1700. if (is_vlan_mode) {
  1701. usb_to_ipa_ep_cfg->hdr.hdr_len =
  1702. VLAN_ETH_HLEN + add;
  1703. ipa_to_usb_ep_cfg.hdr.hdr_len =
  1704. VLAN_ETH_HLEN + sizeof(struct rndis_pkt_hdr);
  1705. ipa_to_usb_ep_cfg.hdr.hdr_additional_const_len = VLAN_ETH_HLEN;
  1706. } else {
  1707. usb_to_ipa_ep_cfg->hdr.hdr_len =
  1708. ETH_HLEN + add;
  1709. ipa_to_usb_ep_cfg.hdr.hdr_len =
  1710. ETH_HLEN + sizeof(struct rndis_pkt_hdr);
  1711. ipa_to_usb_ep_cfg.hdr.hdr_additional_const_len = ETH_HLEN;
  1712. }
  1713. usb_to_ipa_ep_cfg->deaggr.max_packet_len = max_xfer_size_bytes_to_dev;
  1714. result = ipa_cfg_ep(usb_to_ipa_hdl, usb_to_ipa_ep_cfg);
  1715. if (result) {
  1716. pr_err("failed to configure USB to IPA point\n");
  1717. return result;
  1718. }
  1719. RNDIS_IPA_DEBUG("IPA<-USB end-point configured\n");
  1720. ipa_to_usb_ep_cfg.aggr.aggr_byte_limit =
  1721. (max_xfer_size_bytes_to_host - mtu) / 1024;
  1722. if (ipa_to_usb_ep_cfg.aggr.aggr_byte_limit == 0) {
  1723. ipa_to_usb_ep_cfg.aggr.aggr_time_limit = 0;
  1724. ipa_to_usb_ep_cfg.aggr.aggr_pkt_limit = 1;
  1725. } else {
  1726. ipa_to_usb_ep_cfg.aggr.aggr_time_limit =
  1727. DEFAULT_AGGR_TIME_LIMIT;
  1728. ipa_to_usb_ep_cfg.aggr.aggr_pkt_limit =
  1729. DEFAULT_AGGR_PKT_LIMIT;
  1730. }
  1731. RNDIS_IPA_DEBUG(
  1732. "RNDIS aggregation param: en=%d byte_limit=%d time_limit=%d pkt_limit=%d\n"
  1733. , ipa_to_usb_ep_cfg.aggr.aggr_en,
  1734. ipa_to_usb_ep_cfg.aggr.aggr_byte_limit,
  1735. ipa_to_usb_ep_cfg.aggr.aggr_time_limit,
  1736. ipa_to_usb_ep_cfg.aggr.aggr_pkt_limit);
  1737. /* enable hdr_metadata_reg_valid */
  1738. usb_to_ipa_ep_cfg->hdr.hdr_metadata_reg_valid = true;
  1739. result = ipa_cfg_ep(ipa_to_usb_hdl, &ipa_to_usb_ep_cfg);
  1740. if (result) {
  1741. pr_err("failed to configure IPA to USB end-point\n");
  1742. return result;
  1743. }
  1744. RNDIS_IPA_DEBUG("IPA->USB end-point configured\n");
  1745. return 0;
  1746. }
  1747. /**
  1748. * rndis_ipa_set_device_ethernet_addr() - set device Ethernet address
  1749. * @dev_ethaddr: device Ethernet address
  1750. *
  1751. * Returns 0 for success, negative otherwise
  1752. */
  1753. static int rndis_ipa_set_device_ethernet_addr(
  1754. u8 *dev_ethaddr,
  1755. u8 device_ethaddr[])
  1756. {
  1757. if (!is_valid_ether_addr(device_ethaddr))
  1758. return -EINVAL;
  1759. memcpy(dev_ethaddr, device_ethaddr, ETH_ALEN);
  1760. return 0;
  1761. }
  1762. /** rndis_ipa_next_state - return the next state of the driver
  1763. * @current_state: the current state of the driver
  1764. * @operation: an enum which represent the operation being made on the driver
  1765. * by its API.
  1766. *
  1767. * This function implements the driver internal state machine.
  1768. * Its decisions are based on the driver current state and the operation
  1769. * being made.
  1770. * In case the operation is invalid this state machine will return
  1771. * the value RNDIS_IPA_INVALID to inform the caller for a forbidden sequence.
  1772. */
  1773. static enum rndis_ipa_state rndis_ipa_next_state(
  1774. enum rndis_ipa_state current_state,
  1775. enum rndis_ipa_operation operation)
  1776. {
  1777. int next_state = RNDIS_IPA_INVALID;
  1778. switch (current_state) {
  1779. case RNDIS_IPA_UNLOADED:
  1780. if (operation == RNDIS_IPA_INITIALIZE)
  1781. next_state = RNDIS_IPA_INITIALIZED;
  1782. break;
  1783. case RNDIS_IPA_INITIALIZED:
  1784. if (operation == RNDIS_IPA_CONNECT)
  1785. next_state = RNDIS_IPA_CONNECTED;
  1786. else if (operation == RNDIS_IPA_OPEN)
  1787. next_state = RNDIS_IPA_UP;
  1788. else if (operation == RNDIS_IPA_CLEANUP)
  1789. next_state = RNDIS_IPA_UNLOADED;
  1790. break;
  1791. case RNDIS_IPA_CONNECTED:
  1792. if (operation == RNDIS_IPA_DISCONNECT)
  1793. next_state = RNDIS_IPA_INITIALIZED;
  1794. else if (operation == RNDIS_IPA_OPEN)
  1795. next_state = RNDIS_IPA_CONNECTED_AND_UP;
  1796. break;
  1797. case RNDIS_IPA_UP:
  1798. if (operation == RNDIS_IPA_STOP)
  1799. next_state = RNDIS_IPA_INITIALIZED;
  1800. else if (operation == RNDIS_IPA_CONNECT)
  1801. next_state = RNDIS_IPA_CONNECTED_AND_UP;
  1802. else if (operation == RNDIS_IPA_CLEANUP)
  1803. next_state = RNDIS_IPA_UNLOADED;
  1804. break;
  1805. case RNDIS_IPA_CONNECTED_AND_UP:
  1806. if (operation == RNDIS_IPA_STOP)
  1807. next_state = RNDIS_IPA_CONNECTED;
  1808. else if (operation == RNDIS_IPA_DISCONNECT)
  1809. next_state = RNDIS_IPA_UP;
  1810. break;
  1811. default:
  1812. RNDIS_IPA_ERROR("State is not supported\n");
  1813. break;
  1814. }
  1815. RNDIS_IPA_DEBUG
  1816. ("state transition ( %s -> %s )- %s\n",
  1817. rndis_ipa_state_string(current_state),
  1818. rndis_ipa_state_string(next_state),
  1819. next_state == RNDIS_IPA_INVALID ?
  1820. "Forbidden" : "Allowed");
  1821. return next_state;
  1822. }
  1823. /**
  1824. * rndis_ipa_state_string - return the state string representation
  1825. * @state: enum which describe the state
  1826. */
  1827. static const char *rndis_ipa_state_string(enum rndis_ipa_state state)
  1828. {
  1829. switch (state) {
  1830. case RNDIS_IPA_UNLOADED:
  1831. return "RNDIS_IPA_UNLOADED";
  1832. case RNDIS_IPA_INITIALIZED:
  1833. return "RNDIS_IPA_INITIALIZED";
  1834. case RNDIS_IPA_CONNECTED:
  1835. return "RNDIS_IPA_CONNECTED";
  1836. case RNDIS_IPA_UP:
  1837. return "RNDIS_IPA_UP";
  1838. case RNDIS_IPA_CONNECTED_AND_UP:
  1839. return "RNDIS_IPA_CONNECTED_AND_UP";
  1840. default:
  1841. return "Not supported";
  1842. }
  1843. }
  1844. static void rndis_ipa_dump_skb(struct sk_buff *skb)
  1845. {
  1846. int i;
  1847. u32 *cur = (u32 *)skb->data;
  1848. u8 *byte;
  1849. RNDIS_IPA_DEBUG
  1850. ("packet dump start for skb->len=%d\n",
  1851. skb->len);
  1852. for (i = 0; i < (skb->len / 4); i++) {
  1853. byte = (u8 *)(cur + i);
  1854. pr_info
  1855. ("%2d %08x %02x %02x %02x %02x\n",
  1856. i, *(cur + i),
  1857. byte[0], byte[1], byte[2], byte[3]);
  1858. }
  1859. RNDIS_IPA_DEBUG
  1860. ("packet dump ended for skb->len=%d\n", skb->len);
  1861. }
  1862. #ifdef CONFIG_DEBUG_FS
  1863. /**
  1864. * Creates the root folder for the driver
  1865. */
  1866. static void rndis_ipa_debugfs_init(struct rndis_ipa_dev *rndis_ipa_ctx)
  1867. {
  1868. const mode_t flags_read_write = 0666;
  1869. const mode_t flags_read_only = 0444;
  1870. const mode_t flags_write_only = 0222;
  1871. struct dentry *file;
  1872. struct dentry *aggr_directory;
  1873. RNDIS_IPA_LOG_ENTRY();
  1874. if (!rndis_ipa_ctx)
  1875. return;
  1876. rndis_ipa_ctx->directory = debugfs_create_dir(DEBUGFS_DIR_NAME, NULL);
  1877. if (!rndis_ipa_ctx->directory) {
  1878. RNDIS_IPA_ERROR("could not create debugfs directory entry\n");
  1879. goto fail_directory;
  1880. }
  1881. file = debugfs_create_bool
  1882. ("tx_filter", flags_read_write,
  1883. rndis_ipa_ctx->directory, &rndis_ipa_ctx->tx_filter);
  1884. if (!file) {
  1885. RNDIS_IPA_ERROR("could not create debugfs tx_filter file\n");
  1886. goto fail_file;
  1887. }
  1888. file = debugfs_create_bool
  1889. ("rx_filter", flags_read_write,
  1890. rndis_ipa_ctx->directory, &rndis_ipa_ctx->rx_filter);
  1891. if (!file) {
  1892. RNDIS_IPA_ERROR("could not create debugfs rx_filter file\n");
  1893. goto fail_file;
  1894. }
  1895. file = debugfs_create_bool
  1896. ("icmp_filter", flags_read_write,
  1897. rndis_ipa_ctx->directory, &rndis_ipa_ctx->icmp_filter);
  1898. if (!file) {
  1899. RNDIS_IPA_ERROR("could not create debugfs icmp_filter file\n");
  1900. goto fail_file;
  1901. }
  1902. file = debugfs_create_u32
  1903. ("outstanding_high", flags_read_write,
  1904. rndis_ipa_ctx->directory,
  1905. &rndis_ipa_ctx->outstanding_high);
  1906. if (!file) {
  1907. RNDIS_IPA_ERROR("could not create outstanding_high file\n");
  1908. goto fail_file;
  1909. }
  1910. file = debugfs_create_u32
  1911. ("outstanding_low", flags_read_write,
  1912. rndis_ipa_ctx->directory,
  1913. &rndis_ipa_ctx->outstanding_low);
  1914. if (!file) {
  1915. RNDIS_IPA_ERROR("could not create outstanding_low file\n");
  1916. goto fail_file;
  1917. }
  1918. file = debugfs_create_file
  1919. ("outstanding", flags_read_only,
  1920. rndis_ipa_ctx->directory,
  1921. rndis_ipa_ctx, &rndis_ipa_debugfs_atomic_ops);
  1922. if (!file) {
  1923. RNDIS_IPA_ERROR("could not create outstanding file\n");
  1924. goto fail_file;
  1925. }
  1926. file = debugfs_create_u8
  1927. ("state", flags_read_only,
  1928. rndis_ipa_ctx->directory, (u8 *)&rndis_ipa_ctx->state);
  1929. if (!file) {
  1930. RNDIS_IPA_ERROR("could not create state file\n");
  1931. goto fail_file;
  1932. }
  1933. file = debugfs_create_u32
  1934. ("tx_dropped", flags_read_only,
  1935. rndis_ipa_ctx->directory, &rndis_ipa_ctx->tx_dropped);
  1936. if (!file) {
  1937. RNDIS_IPA_ERROR("could not create tx_dropped file\n");
  1938. goto fail_file;
  1939. }
  1940. file = debugfs_create_u32
  1941. ("rx_dropped", flags_read_only,
  1942. rndis_ipa_ctx->directory, &rndis_ipa_ctx->rx_dropped);
  1943. if (!file) {
  1944. RNDIS_IPA_ERROR("could not create rx_dropped file\n");
  1945. goto fail_file;
  1946. }
  1947. aggr_directory = debugfs_create_dir
  1948. (DEBUGFS_AGGR_DIR_NAME,
  1949. rndis_ipa_ctx->directory);
  1950. if (!aggr_directory) {
  1951. RNDIS_IPA_ERROR("could not create debugfs aggr entry\n");
  1952. goto fail_directory;
  1953. }
  1954. file = debugfs_create_file
  1955. ("aggr_value_set", flags_write_only,
  1956. aggr_directory,
  1957. rndis_ipa_ctx, &rndis_ipa_aggr_ops);
  1958. if (!file) {
  1959. RNDIS_IPA_ERROR("could not create aggr_value_set file\n");
  1960. goto fail_file;
  1961. }
  1962. file = debugfs_create_u8
  1963. ("aggr_enable", flags_read_write,
  1964. aggr_directory, (u8 *)&ipa_to_usb_ep_cfg.aggr.aggr_en);
  1965. if (!file) {
  1966. RNDIS_IPA_ERROR("could not create aggr_enable file\n");
  1967. goto fail_file;
  1968. }
  1969. file = debugfs_create_u8
  1970. ("aggr_type", flags_read_write,
  1971. aggr_directory, (u8 *)&ipa_to_usb_ep_cfg.aggr.aggr);
  1972. if (!file) {
  1973. RNDIS_IPA_ERROR("could not create aggr_type file\n");
  1974. goto fail_file;
  1975. }
  1976. file = debugfs_create_u32
  1977. ("aggr_byte_limit", flags_read_write,
  1978. aggr_directory,
  1979. &ipa_to_usb_ep_cfg.aggr.aggr_byte_limit);
  1980. if (!file) {
  1981. RNDIS_IPA_ERROR("could not create aggr_byte_limit file\n");
  1982. goto fail_file;
  1983. }
  1984. file = debugfs_create_u32
  1985. ("aggr_time_limit", flags_read_write,
  1986. aggr_directory,
  1987. &ipa_to_usb_ep_cfg.aggr.aggr_time_limit);
  1988. if (!file) {
  1989. RNDIS_IPA_ERROR("could not create aggr_time_limit file\n");
  1990. goto fail_file;
  1991. }
  1992. file = debugfs_create_u32
  1993. ("aggr_pkt_limit", flags_read_write,
  1994. aggr_directory,
  1995. &ipa_to_usb_ep_cfg.aggr.aggr_pkt_limit);
  1996. if (!file) {
  1997. RNDIS_IPA_ERROR("could not create aggr_pkt_limit file\n");
  1998. goto fail_file;
  1999. }
  2000. file = debugfs_create_bool
  2001. ("tx_dump_enable", flags_read_write,
  2002. rndis_ipa_ctx->directory,
  2003. &rndis_ipa_ctx->tx_dump_enable);
  2004. if (!file) {
  2005. RNDIS_IPA_ERROR("fail to create tx_dump_enable file\n");
  2006. goto fail_file;
  2007. }
  2008. file = debugfs_create_bool
  2009. ("rx_dump_enable", flags_read_write,
  2010. rndis_ipa_ctx->directory,
  2011. &rndis_ipa_ctx->rx_dump_enable);
  2012. if (!file) {
  2013. RNDIS_IPA_ERROR("fail to create rx_dump_enable file\n");
  2014. goto fail_file;
  2015. }
  2016. file = debugfs_create_bool
  2017. ("deaggregation_enable", flags_read_write,
  2018. rndis_ipa_ctx->directory,
  2019. &rndis_ipa_ctx->deaggregation_enable);
  2020. if (!file) {
  2021. RNDIS_IPA_ERROR("fail to create deaggregation_enable file\n");
  2022. goto fail_file;
  2023. }
  2024. file = debugfs_create_u32
  2025. ("error_msec_sleep_time", flags_read_write,
  2026. rndis_ipa_ctx->directory,
  2027. &rndis_ipa_ctx->error_msec_sleep_time);
  2028. if (!file) {
  2029. RNDIS_IPA_ERROR("fail to create error_msec_sleep_time file\n");
  2030. goto fail_file;
  2031. }
  2032. file = debugfs_create_bool
  2033. ("during_xmit_error", flags_read_only,
  2034. rndis_ipa_ctx->directory,
  2035. &rndis_ipa_ctx->during_xmit_error);
  2036. if (!file) {
  2037. RNDIS_IPA_ERROR("fail to create during_xmit_error file\n");
  2038. goto fail_file;
  2039. }
  2040. file = debugfs_create_bool("is_vlan_mode", flags_read_only,
  2041. rndis_ipa_ctx->directory,
  2042. &rndis_ipa_ctx->is_vlan_mode);
  2043. if (!file) {
  2044. RNDIS_IPA_ERROR("fail to create is_vlan_mode file\n");
  2045. goto fail_file;
  2046. }
  2047. RNDIS_IPA_DEBUG("debugfs entries were created\n");
  2048. RNDIS_IPA_LOG_EXIT();
  2049. return;
  2050. fail_file:
  2051. debugfs_remove_recursive(rndis_ipa_ctx->directory);
  2052. fail_directory:
  2053. return;
  2054. }
  2055. static void rndis_ipa_debugfs_destroy(struct rndis_ipa_dev *rndis_ipa_ctx)
  2056. {
  2057. debugfs_remove_recursive(rndis_ipa_ctx->directory);
  2058. }
  2059. #else /* !CONFIG_DEBUG_FS */
  2060. static void rndis_ipa_debugfs_init(struct rndis_ipa_dev *rndis_ipa_ctx) {}
  2061. static void rndis_ipa_debugfs_destroy(struct rndis_ipa_dev *rndis_ipa_ctx) {}
  2062. #endif /* CONFIG_DEBUG_FS*/
  2063. static int rndis_ipa_debugfs_aggr_open
  2064. (struct inode *inode,
  2065. struct file *file)
  2066. {
  2067. struct rndis_ipa_dev *rndis_ipa_ctx = inode->i_private;
  2068. file->private_data = rndis_ipa_ctx;
  2069. return 0;
  2070. }
  2071. static ssize_t rndis_ipa_debugfs_aggr_write
  2072. (struct file *file,
  2073. const char __user *buf, size_t count, loff_t *ppos)
  2074. {
  2075. struct rndis_ipa_dev *rndis_ipa_ctx = NULL;
  2076. int result;
  2077. if (file == NULL)
  2078. return -EFAULT;
  2079. rndis_ipa_ctx = file->private_data;
  2080. result = ipa_cfg_ep(rndis_ipa_ctx->usb_to_ipa_hdl, &ipa_to_usb_ep_cfg);
  2081. if (result) {
  2082. pr_err("failed to re-configure USB to IPA point\n");
  2083. return result;
  2084. }
  2085. pr_info("IPA<-USB end-point re-configured\n");
  2086. return count;
  2087. }
  2088. static int rndis_ipa_debugfs_atomic_open(struct inode *inode, struct file *file)
  2089. {
  2090. struct rndis_ipa_dev *rndis_ipa_ctx = inode->i_private;
  2091. RNDIS_IPA_LOG_ENTRY();
  2092. file->private_data = &rndis_ipa_ctx->outstanding_pkts;
  2093. RNDIS_IPA_LOG_EXIT();
  2094. return 0;
  2095. }
  2096. static ssize_t rndis_ipa_debugfs_atomic_read
  2097. (struct file *file, char __user *ubuf, size_t count, loff_t *ppos)
  2098. {
  2099. int nbytes;
  2100. u8 atomic_str[DEBUGFS_TEMP_BUF_SIZE] = {0};
  2101. atomic_t *atomic_var = file->private_data;
  2102. RNDIS_IPA_LOG_ENTRY();
  2103. nbytes = scnprintf
  2104. (atomic_str, sizeof(atomic_str), "%d\n",
  2105. atomic_read(atomic_var));
  2106. RNDIS_IPA_LOG_EXIT();
  2107. return simple_read_from_buffer(ubuf, count, ppos, atomic_str, nbytes);
  2108. }
  2109. static int __init rndis_ipa_init_module(void)
  2110. {
  2111. ipa_rndis_logbuf = ipc_log_context_create(IPA_RNDIS_IPC_LOG_PAGES,
  2112. "ipa_rndis", 0);
  2113. if (ipa_rndis_logbuf == NULL)
  2114. RNDIS_IPA_ERROR("failed to create IPC log, continue...\n");
  2115. pr_info("RNDIS_IPA module is loaded.\n");
  2116. return 0;
  2117. }
  2118. static void __exit rndis_ipa_cleanup_module(void)
  2119. {
  2120. if (ipa_rndis_logbuf)
  2121. ipc_log_context_destroy(ipa_rndis_logbuf);
  2122. ipa_rndis_logbuf = NULL;
  2123. pr_info("RNDIS_IPA module is unloaded.\n");
  2124. }
  2125. MODULE_LICENSE("GPL v2");
  2126. MODULE_DESCRIPTION("RNDIS_IPA network interface");
  2127. late_initcall(rndis_ipa_init_module);
  2128. module_exit(rndis_ipa_cleanup_module);