rndis_ipa.c 78 KB

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