rndis_ipa.c 70 KB

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