smd_rpcrouter.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565
  1. /* arch/arm/mach-msm/smd_rpcrouter.c
  2. *
  3. * Copyright (C) 2007 Google, Inc.
  4. * Copyright (c) 2007-2013, The Linux Foundation. All rights reserved.
  5. * Author: San Mehat <san@android.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. /* TODO: handle cases where smd_write() will tempfail due to full fifo */
  18. /* TODO: thread priority? schedule a work to bump it? */
  19. /* TODO: maybe make server_list_lock a mutex */
  20. /* TODO: pool fragments to avoid kmalloc/kfree churn */
  21. #include <linux/slab.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/string.h>
  25. #include <linux/errno.h>
  26. #include <linux/cdev.h>
  27. #include <linux/init.h>
  28. #include <linux/device.h>
  29. #include <linux/types.h>
  30. #include <linux/delay.h>
  31. #include <linux/fs.h>
  32. #include <linux/err.h>
  33. #include <linux/sched.h>
  34. #include <linux/poll.h>
  35. #include <linux/wakelock.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/byteorder.h>
  38. #include <linux/platform_device.h>
  39. #include <linux/uaccess.h>
  40. #include <linux/debugfs.h>
  41. #include <linux/reboot.h>
  42. #include <asm/byteorder.h>
  43. #include <mach/msm_smd.h>
  44. #include <mach/smem_log.h>
  45. #include <mach/subsystem_notif.h>
  46. #include "smd_rpcrouter.h"
  47. #include "modem_notifier.h"
  48. #include "smd_rpc_sym.h"
  49. #include "smd_private.h"
  50. enum {
  51. SMEM_LOG = 1U << 0,
  52. RTR_DBG = 1U << 1,
  53. R2R_MSG = 1U << 2,
  54. R2R_RAW = 1U << 3,
  55. RPC_MSG = 1U << 4,
  56. NTFY_MSG = 1U << 5,
  57. RAW_PMR = 1U << 6,
  58. RAW_PMW = 1U << 7,
  59. R2R_RAW_HDR = 1U << 8,
  60. };
  61. static int msm_rpc_connect_timeout_ms;
  62. module_param_named(connect_timeout, msm_rpc_connect_timeout_ms,
  63. int, S_IRUGO | S_IWUSR | S_IWGRP);
  64. static int smd_rpcrouter_debug_mask;
  65. module_param_named(debug_mask, smd_rpcrouter_debug_mask,
  66. int, S_IRUGO | S_IWUSR | S_IWGRP);
  67. #define DIAG(x...) printk(KERN_ERR "[RR] ERROR " x)
  68. #if defined(CONFIG_MSM_ONCRPCROUTER_DEBUG)
  69. #define D(x...) do { \
  70. if (smd_rpcrouter_debug_mask & RTR_DBG) \
  71. printk(KERN_ERR x); \
  72. } while (0)
  73. #define RR(x...) do { \
  74. if (smd_rpcrouter_debug_mask & R2R_MSG) \
  75. printk(KERN_ERR "[RR] "x); \
  76. } while (0)
  77. #define RAW(x...) do { \
  78. if (smd_rpcrouter_debug_mask & R2R_RAW) \
  79. printk(KERN_ERR "[RAW] "x); \
  80. } while (0)
  81. #define RAW_HDR(x...) do { \
  82. if (smd_rpcrouter_debug_mask & R2R_RAW_HDR) \
  83. printk(KERN_ERR "[HDR] "x); \
  84. } while (0)
  85. #define RAW_PMR(x...) do { \
  86. if (smd_rpcrouter_debug_mask & RAW_PMR) \
  87. printk(KERN_ERR "[PMR] "x); \
  88. } while (0)
  89. #define RAW_PMR_NOMASK(x...) do { \
  90. printk(KERN_ERR "[PMR] "x); \
  91. } while (0)
  92. #define RAW_PMW(x...) do { \
  93. if (smd_rpcrouter_debug_mask & RAW_PMW) \
  94. printk(KERN_ERR "[PMW] "x); \
  95. } while (0)
  96. #define RAW_PMW_NOMASK(x...) do { \
  97. printk(KERN_ERR "[PMW] "x); \
  98. } while (0)
  99. #define IO(x...) do { \
  100. if (smd_rpcrouter_debug_mask & RPC_MSG) \
  101. printk(KERN_ERR "[RPC] "x); \
  102. } while (0)
  103. #define NTFY(x...) do { \
  104. if (smd_rpcrouter_debug_mask & NTFY_MSG) \
  105. printk(KERN_ERR "[NOTIFY] "x); \
  106. } while (0)
  107. #else
  108. #define D(x...) do { } while (0)
  109. #define RR(x...) do { } while (0)
  110. #define RAW(x...) do { } while (0)
  111. #define RAW_HDR(x...) do { } while (0)
  112. #define RAW_PMR(x...) do { } while (0)
  113. #define RAW_PMR_NO_MASK(x...) do { } while (0)
  114. #define RAW_PMW(x...) do { } while (0)
  115. #define RAW_PMW_NO_MASK(x...) do { } while (0)
  116. #define IO(x...) do { } while (0)
  117. #define NTFY(x...) do { } while (0)
  118. #endif
  119. static LIST_HEAD(local_endpoints);
  120. static LIST_HEAD(remote_endpoints);
  121. static LIST_HEAD(server_list);
  122. static wait_queue_head_t newserver_wait;
  123. static wait_queue_head_t subsystem_restart_wait;
  124. static DEFINE_SPINLOCK(local_endpoints_lock);
  125. static DEFINE_SPINLOCK(remote_endpoints_lock);
  126. static DEFINE_SPINLOCK(server_list_lock);
  127. static LIST_HEAD(rpc_board_dev_list);
  128. static DEFINE_SPINLOCK(rpc_board_dev_list_lock);
  129. static struct workqueue_struct *rpcrouter_workqueue;
  130. static atomic_t next_xid = ATOMIC_INIT(1);
  131. static atomic_t pm_mid = ATOMIC_INIT(1);
  132. static void do_read_data(struct work_struct *work);
  133. static void do_create_pdevs(struct work_struct *work);
  134. static void do_create_rpcrouter_pdev(struct work_struct *work);
  135. static int msm_rpcrouter_close(void);
  136. static DECLARE_WORK(work_create_pdevs, do_create_pdevs);
  137. static DECLARE_WORK(work_create_rpcrouter_pdev, do_create_rpcrouter_pdev);
  138. #define RR_STATE_IDLE 0
  139. #define RR_STATE_HEADER 1
  140. #define RR_STATE_BODY 2
  141. #define RR_STATE_ERROR 3
  142. /* State for remote ep following restart */
  143. #define RESTART_QUOTA_ABORT 1
  144. struct rr_context {
  145. struct rr_packet *pkt;
  146. uint8_t *ptr;
  147. uint32_t state; /* current assembly state */
  148. uint32_t count; /* bytes needed in this state */
  149. };
  150. struct rr_context the_rr_context;
  151. struct rpc_board_dev_info {
  152. struct list_head list;
  153. struct rpc_board_dev *dev;
  154. };
  155. static struct platform_device rpcrouter_pdev = {
  156. .name = "oncrpc_router",
  157. .id = -1,
  158. };
  159. struct rpcrouter_xprt_info {
  160. struct list_head list;
  161. struct rpcrouter_xprt *xprt;
  162. int remote_pid;
  163. uint32_t initialized;
  164. wait_queue_head_t read_wait;
  165. struct wake_lock wakelock;
  166. spinlock_t lock;
  167. uint32_t need_len;
  168. struct work_struct read_data;
  169. struct workqueue_struct *workqueue;
  170. int abort_data_read;
  171. unsigned char r2r_buf[RPCROUTER_MSGSIZE_MAX];
  172. };
  173. static LIST_HEAD(xprt_info_list);
  174. static DEFINE_MUTEX(xprt_info_list_lock);
  175. DECLARE_COMPLETION(rpc_remote_router_up);
  176. static atomic_t pending_close_count = ATOMIC_INIT(0);
  177. static int msm_rpc_reboot_call(struct notifier_block *this,
  178. unsigned long code, void *_cmd)
  179. {
  180. switch (code) {
  181. case SYS_RESTART:
  182. case SYS_HALT:
  183. case SYS_POWER_OFF:
  184. msm_rpcrouter_close();
  185. break;
  186. }
  187. return NOTIFY_DONE;
  188. }
  189. static struct notifier_block msm_rpc_reboot_notifier = {
  190. .notifier_call = msm_rpc_reboot_call,
  191. .priority = 100
  192. };
  193. /*
  194. * Search for transport (xprt) that matches the provided PID.
  195. *
  196. * Note: The calling function must ensure that the mutex
  197. * xprt_info_list_lock is locked when this function
  198. * is called.
  199. *
  200. * @remote_pid Remote PID for the transport
  201. *
  202. * @returns Pointer to transport or NULL if not found
  203. */
  204. static struct rpcrouter_xprt_info *rpcrouter_get_xprt_info(uint32_t remote_pid)
  205. {
  206. struct rpcrouter_xprt_info *xprt_info;
  207. list_for_each_entry(xprt_info, &xprt_info_list, list) {
  208. if (xprt_info->remote_pid == remote_pid)
  209. return xprt_info;
  210. }
  211. return NULL;
  212. }
  213. static int rpcrouter_send_control_msg(struct rpcrouter_xprt_info *xprt_info,
  214. union rr_control_msg *msg)
  215. {
  216. struct rr_header hdr;
  217. unsigned long flags = 0;
  218. int need;
  219. if (xprt_info->remote_pid == RPCROUTER_PID_LOCAL)
  220. return 0;
  221. if (!(msg->cmd == RPCROUTER_CTRL_CMD_HELLO) &&
  222. !xprt_info->initialized) {
  223. printk(KERN_ERR "rpcrouter_send_control_msg(): Warning, "
  224. "router not initialized\n");
  225. return -EINVAL;
  226. }
  227. hdr.version = RPCROUTER_VERSION;
  228. hdr.type = msg->cmd;
  229. hdr.src_pid = RPCROUTER_PID_LOCAL;
  230. hdr.src_cid = RPCROUTER_ROUTER_ADDRESS;
  231. hdr.confirm_rx = 0;
  232. hdr.size = sizeof(*msg);
  233. hdr.dst_pid = xprt_info->remote_pid;
  234. hdr.dst_cid = RPCROUTER_ROUTER_ADDRESS;
  235. /* TODO: what if channel is full? */
  236. need = sizeof(hdr) + hdr.size;
  237. spin_lock_irqsave(&xprt_info->lock, flags);
  238. while (xprt_info->xprt->write_avail() < need) {
  239. spin_unlock_irqrestore(&xprt_info->lock, flags);
  240. msleep(250);
  241. spin_lock_irqsave(&xprt_info->lock, flags);
  242. }
  243. xprt_info->xprt->write(&hdr, sizeof(hdr), HEADER);
  244. xprt_info->xprt->write(msg, hdr.size, PAYLOAD);
  245. spin_unlock_irqrestore(&xprt_info->lock, flags);
  246. return 0;
  247. }
  248. static void modem_reset_cleanup(struct rpcrouter_xprt_info *xprt_info)
  249. {
  250. struct msm_rpc_endpoint *ept;
  251. struct rr_remote_endpoint *r_ept;
  252. struct rr_packet *pkt, *tmp_pkt;
  253. struct rr_fragment *frag, *next;
  254. struct msm_rpc_reply *reply, *reply_tmp;
  255. unsigned long flags;
  256. if (!xprt_info) {
  257. pr_err("%s: Invalid xprt_info\n", __func__);
  258. return;
  259. }
  260. spin_lock_irqsave(&local_endpoints_lock, flags);
  261. /* remove all partial packets received */
  262. list_for_each_entry(ept, &local_endpoints, list) {
  263. RR("%s EPT DST PID %x, remote_pid:%d\n", __func__,
  264. ept->dst_pid, xprt_info->remote_pid);
  265. if (xprt_info->remote_pid != ept->dst_pid)
  266. continue;
  267. D("calling teardown cb %p\n", ept->cb_restart_teardown);
  268. if (ept->cb_restart_teardown)
  269. ept->cb_restart_teardown(ept->client_data);
  270. ept->do_setup_notif = 1;
  271. /* remove replies */
  272. spin_lock(&ept->reply_q_lock);
  273. list_for_each_entry_safe(reply, reply_tmp,
  274. &ept->reply_pend_q, list) {
  275. list_del(&reply->list);
  276. kfree(reply);
  277. }
  278. list_for_each_entry_safe(reply, reply_tmp,
  279. &ept->reply_avail_q, list) {
  280. list_del(&reply->list);
  281. kfree(reply);
  282. }
  283. ept->reply_cnt = 0;
  284. spin_unlock(&ept->reply_q_lock);
  285. /* Set restart state for local ep */
  286. RR("EPT:0x%p, State %d RESTART_PEND_NTFY_SVR "
  287. "PROG:0x%08x VERS:0x%08x\n",
  288. ept, ept->restart_state,
  289. be32_to_cpu(ept->dst_prog),
  290. be32_to_cpu(ept->dst_vers));
  291. spin_lock(&ept->restart_lock);
  292. ept->restart_state = RESTART_PEND_NTFY_SVR;
  293. /* remove incomplete packets */
  294. spin_lock(&ept->incomplete_lock);
  295. list_for_each_entry_safe(pkt, tmp_pkt,
  296. &ept->incomplete, list) {
  297. list_del(&pkt->list);
  298. frag = pkt->first;
  299. while (frag != NULL) {
  300. next = frag->next;
  301. kfree(frag);
  302. frag = next;
  303. }
  304. kfree(pkt);
  305. }
  306. spin_unlock(&ept->incomplete_lock);
  307. /* remove all completed packets waiting to be read */
  308. spin_lock(&ept->read_q_lock);
  309. list_for_each_entry_safe(pkt, tmp_pkt, &ept->read_q,
  310. list) {
  311. list_del(&pkt->list);
  312. frag = pkt->first;
  313. while (frag != NULL) {
  314. next = frag->next;
  315. kfree(frag);
  316. frag = next;
  317. }
  318. kfree(pkt);
  319. }
  320. spin_unlock(&ept->read_q_lock);
  321. spin_unlock(&ept->restart_lock);
  322. wake_up(&ept->wait_q);
  323. }
  324. spin_unlock_irqrestore(&local_endpoints_lock, flags);
  325. /* Unblock endpoints waiting for quota ack*/
  326. spin_lock_irqsave(&remote_endpoints_lock, flags);
  327. list_for_each_entry(r_ept, &remote_endpoints, list) {
  328. spin_lock(&r_ept->quota_lock);
  329. r_ept->quota_restart_state = RESTART_QUOTA_ABORT;
  330. RR("Set STATE_PENDING PID:0x%08x CID:0x%08x \n", r_ept->pid,
  331. r_ept->cid);
  332. spin_unlock(&r_ept->quota_lock);
  333. wake_up(&r_ept->quota_wait);
  334. }
  335. spin_unlock_irqrestore(&remote_endpoints_lock, flags);
  336. }
  337. static void modem_reset_startup(struct rpcrouter_xprt_info *xprt_info)
  338. {
  339. struct msm_rpc_endpoint *ept;
  340. unsigned long flags;
  341. spin_lock_irqsave(&local_endpoints_lock, flags);
  342. /* notify all endpoints that we are coming back up */
  343. list_for_each_entry(ept, &local_endpoints, list) {
  344. RR("%s EPT DST PID %x, remote_pid:%d\n", __func__,
  345. ept->dst_pid, xprt_info->remote_pid);
  346. if (xprt_info->remote_pid != ept->dst_pid)
  347. continue;
  348. D("calling setup cb %d:%p\n", ept->do_setup_notif,
  349. ept->cb_restart_setup);
  350. if (ept->do_setup_notif && ept->cb_restart_setup)
  351. ept->cb_restart_setup(ept->client_data);
  352. ept->do_setup_notif = 0;
  353. }
  354. spin_unlock_irqrestore(&local_endpoints_lock, flags);
  355. }
  356. /*
  357. * Blocks and waits for endpoint if a reset is in progress.
  358. *
  359. * @returns
  360. * ENETRESET Reset is in progress and a notification needed
  361. * ERESTARTSYS Signal occurred
  362. * 0 Reset is not in progress
  363. */
  364. static int wait_for_restart_and_notify(struct msm_rpc_endpoint *ept)
  365. {
  366. unsigned long flags;
  367. int ret = 0;
  368. DEFINE_WAIT(__wait);
  369. for (;;) {
  370. prepare_to_wait(&ept->restart_wait, &__wait,
  371. TASK_INTERRUPTIBLE);
  372. spin_lock_irqsave(&ept->restart_lock, flags);
  373. if (ept->restart_state == RESTART_NORMAL) {
  374. spin_unlock_irqrestore(&ept->restart_lock, flags);
  375. break;
  376. } else if (ept->restart_state & RESTART_PEND_NTFY) {
  377. ept->restart_state &= ~RESTART_PEND_NTFY;
  378. spin_unlock_irqrestore(&ept->restart_lock, flags);
  379. ret = -ENETRESET;
  380. break;
  381. }
  382. if (signal_pending(current) &&
  383. ((!(ept->flags & MSM_RPC_UNINTERRUPTIBLE)))) {
  384. spin_unlock_irqrestore(&ept->restart_lock, flags);
  385. ret = -ERESTARTSYS;
  386. break;
  387. }
  388. spin_unlock_irqrestore(&ept->restart_lock, flags);
  389. schedule();
  390. }
  391. finish_wait(&ept->restart_wait, &__wait);
  392. return ret;
  393. }
  394. static struct rr_server *rpcrouter_create_server(uint32_t pid,
  395. uint32_t cid,
  396. uint32_t prog,
  397. uint32_t ver)
  398. {
  399. struct rr_server *server;
  400. unsigned long flags;
  401. int rc;
  402. server = kmalloc(sizeof(struct rr_server), GFP_KERNEL);
  403. if (!server)
  404. return ERR_PTR(-ENOMEM);
  405. memset(server, 0, sizeof(struct rr_server));
  406. server->pid = pid;
  407. server->cid = cid;
  408. server->prog = prog;
  409. server->vers = ver;
  410. spin_lock_irqsave(&server_list_lock, flags);
  411. list_add_tail(&server->list, &server_list);
  412. spin_unlock_irqrestore(&server_list_lock, flags);
  413. rc = msm_rpcrouter_create_server_cdev(server);
  414. if (rc < 0)
  415. goto out_fail;
  416. return server;
  417. out_fail:
  418. spin_lock_irqsave(&server_list_lock, flags);
  419. list_del(&server->list);
  420. spin_unlock_irqrestore(&server_list_lock, flags);
  421. kfree(server);
  422. return ERR_PTR(rc);
  423. }
  424. static void rpcrouter_destroy_server(struct rr_server *server)
  425. {
  426. unsigned long flags;
  427. spin_lock_irqsave(&server_list_lock, flags);
  428. list_del(&server->list);
  429. spin_unlock_irqrestore(&server_list_lock, flags);
  430. device_destroy(msm_rpcrouter_class, server->device_number);
  431. kfree(server);
  432. }
  433. int msm_rpc_add_board_dev(struct rpc_board_dev *devices, int num)
  434. {
  435. unsigned long flags;
  436. struct rpc_board_dev_info *board_info;
  437. int i;
  438. for (i = 0; i < num; i++) {
  439. board_info = kzalloc(sizeof(struct rpc_board_dev_info),
  440. GFP_KERNEL);
  441. if (!board_info)
  442. return -ENOMEM;
  443. board_info->dev = &devices[i];
  444. D("%s: adding program %x\n", __func__, board_info->dev->prog);
  445. spin_lock_irqsave(&rpc_board_dev_list_lock, flags);
  446. list_add_tail(&board_info->list, &rpc_board_dev_list);
  447. spin_unlock_irqrestore(&rpc_board_dev_list_lock, flags);
  448. }
  449. return 0;
  450. }
  451. EXPORT_SYMBOL(msm_rpc_add_board_dev);
  452. static void rpcrouter_register_board_dev(struct rr_server *server)
  453. {
  454. struct rpc_board_dev_info *board_info;
  455. unsigned long flags;
  456. int rc;
  457. spin_lock_irqsave(&rpc_board_dev_list_lock, flags);
  458. list_for_each_entry(board_info, &rpc_board_dev_list, list) {
  459. if (server->prog == board_info->dev->prog) {
  460. D("%s: registering device %x\n",
  461. __func__, board_info->dev->prog);
  462. list_del(&board_info->list);
  463. spin_unlock_irqrestore(&rpc_board_dev_list_lock, flags);
  464. rc = platform_device_register(&board_info->dev->pdev);
  465. if (rc)
  466. pr_err("%s: board dev register failed %d\n",
  467. __func__, rc);
  468. kfree(board_info);
  469. return;
  470. }
  471. }
  472. spin_unlock_irqrestore(&rpc_board_dev_list_lock, flags);
  473. }
  474. static struct rr_server *rpcrouter_lookup_server(uint32_t prog, uint32_t ver)
  475. {
  476. struct rr_server *server;
  477. unsigned long flags;
  478. spin_lock_irqsave(&server_list_lock, flags);
  479. list_for_each_entry(server, &server_list, list) {
  480. if (server->prog == prog
  481. && server->vers == ver) {
  482. spin_unlock_irqrestore(&server_list_lock, flags);
  483. return server;
  484. }
  485. }
  486. spin_unlock_irqrestore(&server_list_lock, flags);
  487. return NULL;
  488. }
  489. static struct rr_server *rpcrouter_lookup_server_by_dev(dev_t dev)
  490. {
  491. struct rr_server *server;
  492. unsigned long flags;
  493. spin_lock_irqsave(&server_list_lock, flags);
  494. list_for_each_entry(server, &server_list, list) {
  495. if (server->device_number == dev) {
  496. spin_unlock_irqrestore(&server_list_lock, flags);
  497. return server;
  498. }
  499. }
  500. spin_unlock_irqrestore(&server_list_lock, flags);
  501. return NULL;
  502. }
  503. struct msm_rpc_endpoint *msm_rpcrouter_create_local_endpoint(dev_t dev)
  504. {
  505. struct msm_rpc_endpoint *ept;
  506. unsigned long flags;
  507. ept = kmalloc(sizeof(struct msm_rpc_endpoint), GFP_KERNEL);
  508. if (!ept)
  509. return NULL;
  510. memset(ept, 0, sizeof(struct msm_rpc_endpoint));
  511. ept->cid = (uint32_t) ept;
  512. ept->pid = RPCROUTER_PID_LOCAL;
  513. ept->dev = dev;
  514. if ((dev != msm_rpcrouter_devno) && (dev != MKDEV(0, 0))) {
  515. struct rr_server *srv;
  516. /*
  517. * This is a userspace client which opened
  518. * a program/ver devicenode. Bind the client
  519. * to that destination
  520. */
  521. srv = rpcrouter_lookup_server_by_dev(dev);
  522. /* TODO: bug? really? */
  523. BUG_ON(!srv);
  524. ept->dst_pid = srv->pid;
  525. ept->dst_cid = srv->cid;
  526. ept->dst_prog = cpu_to_be32(srv->prog);
  527. ept->dst_vers = cpu_to_be32(srv->vers);
  528. } else {
  529. /* mark not connected */
  530. ept->dst_pid = 0xffffffff;
  531. }
  532. init_waitqueue_head(&ept->wait_q);
  533. INIT_LIST_HEAD(&ept->read_q);
  534. spin_lock_init(&ept->read_q_lock);
  535. INIT_LIST_HEAD(&ept->reply_avail_q);
  536. INIT_LIST_HEAD(&ept->reply_pend_q);
  537. spin_lock_init(&ept->reply_q_lock);
  538. spin_lock_init(&ept->restart_lock);
  539. init_waitqueue_head(&ept->restart_wait);
  540. ept->restart_state = RESTART_NORMAL;
  541. wake_lock_init(&ept->read_q_wake_lock, WAKE_LOCK_SUSPEND, "rpc_read");
  542. wake_lock_init(&ept->reply_q_wake_lock, WAKE_LOCK_SUSPEND, "rpc_reply");
  543. INIT_LIST_HEAD(&ept->incomplete);
  544. spin_lock_init(&ept->incomplete_lock);
  545. spin_lock_irqsave(&local_endpoints_lock, flags);
  546. list_add_tail(&ept->list, &local_endpoints);
  547. spin_unlock_irqrestore(&local_endpoints_lock, flags);
  548. return ept;
  549. }
  550. int msm_rpcrouter_destroy_local_endpoint(struct msm_rpc_endpoint *ept)
  551. {
  552. int rc;
  553. union rr_control_msg msg = { 0 };
  554. struct msm_rpc_reply *reply, *reply_tmp;
  555. unsigned long flags;
  556. struct rpcrouter_xprt_info *xprt_info;
  557. /* Endpoint with dst_pid = 0xffffffff corresponds to that of
  558. ** router port. So don't send a REMOVE CLIENT message while
  559. ** destroying it.*/
  560. spin_lock_irqsave(&local_endpoints_lock, flags);
  561. list_del(&ept->list);
  562. spin_unlock_irqrestore(&local_endpoints_lock, flags);
  563. if (ept->dst_pid != 0xffffffff) {
  564. msg.cmd = RPCROUTER_CTRL_CMD_REMOVE_CLIENT;
  565. msg.cli.pid = ept->pid;
  566. msg.cli.cid = ept->cid;
  567. RR("x REMOVE_CLIENT id=%d:%08x\n", ept->pid, ept->cid);
  568. mutex_lock(&xprt_info_list_lock);
  569. list_for_each_entry(xprt_info, &xprt_info_list, list) {
  570. rc = rpcrouter_send_control_msg(xprt_info, &msg);
  571. if (rc < 0) {
  572. mutex_unlock(&xprt_info_list_lock);
  573. return rc;
  574. }
  575. }
  576. mutex_unlock(&xprt_info_list_lock);
  577. }
  578. /* Free replies */
  579. spin_lock_irqsave(&ept->reply_q_lock, flags);
  580. list_for_each_entry_safe(reply, reply_tmp, &ept->reply_pend_q, list) {
  581. list_del(&reply->list);
  582. kfree(reply);
  583. }
  584. list_for_each_entry_safe(reply, reply_tmp, &ept->reply_avail_q, list) {
  585. list_del(&reply->list);
  586. kfree(reply);
  587. }
  588. spin_unlock_irqrestore(&ept->reply_q_lock, flags);
  589. wake_lock_destroy(&ept->read_q_wake_lock);
  590. wake_lock_destroy(&ept->reply_q_wake_lock);
  591. kfree(ept);
  592. return 0;
  593. }
  594. static int rpcrouter_create_remote_endpoint(uint32_t pid, uint32_t cid)
  595. {
  596. struct rr_remote_endpoint *new_c;
  597. unsigned long flags;
  598. new_c = kmalloc(sizeof(struct rr_remote_endpoint), GFP_KERNEL);
  599. if (!new_c)
  600. return -ENOMEM;
  601. memset(new_c, 0, sizeof(struct rr_remote_endpoint));
  602. new_c->cid = cid;
  603. new_c->pid = pid;
  604. init_waitqueue_head(&new_c->quota_wait);
  605. spin_lock_init(&new_c->quota_lock);
  606. spin_lock_irqsave(&remote_endpoints_lock, flags);
  607. list_add_tail(&new_c->list, &remote_endpoints);
  608. new_c->quota_restart_state = RESTART_NORMAL;
  609. spin_unlock_irqrestore(&remote_endpoints_lock, flags);
  610. return 0;
  611. }
  612. static struct msm_rpc_endpoint *rpcrouter_lookup_local_endpoint(uint32_t cid)
  613. {
  614. struct msm_rpc_endpoint *ept;
  615. list_for_each_entry(ept, &local_endpoints, list) {
  616. if (ept->cid == cid)
  617. return ept;
  618. }
  619. return NULL;
  620. }
  621. static struct rr_remote_endpoint *rpcrouter_lookup_remote_endpoint(uint32_t pid,
  622. uint32_t cid)
  623. {
  624. struct rr_remote_endpoint *ept;
  625. unsigned long flags;
  626. spin_lock_irqsave(&remote_endpoints_lock, flags);
  627. list_for_each_entry(ept, &remote_endpoints, list) {
  628. if ((ept->pid == pid) && (ept->cid == cid)) {
  629. spin_unlock_irqrestore(&remote_endpoints_lock, flags);
  630. return ept;
  631. }
  632. }
  633. spin_unlock_irqrestore(&remote_endpoints_lock, flags);
  634. return NULL;
  635. }
  636. static void handle_server_restart(struct rr_server *server,
  637. uint32_t pid, uint32_t cid,
  638. uint32_t prog, uint32_t vers)
  639. {
  640. struct rr_remote_endpoint *r_ept;
  641. struct msm_rpc_endpoint *ept;
  642. unsigned long flags;
  643. r_ept = rpcrouter_lookup_remote_endpoint(pid, cid);
  644. if (r_ept && (r_ept->quota_restart_state !=
  645. RESTART_NORMAL)) {
  646. spin_lock_irqsave(&r_ept->quota_lock, flags);
  647. r_ept->tx_quota_cntr = 0;
  648. r_ept->quota_restart_state =
  649. RESTART_NORMAL;
  650. spin_unlock_irqrestore(&r_ept->quota_lock, flags);
  651. D(KERN_INFO "rpcrouter: Remote EPT Reset %0x\n",
  652. (unsigned int)r_ept);
  653. wake_up(&r_ept->quota_wait);
  654. }
  655. spin_lock_irqsave(&local_endpoints_lock, flags);
  656. list_for_each_entry(ept, &local_endpoints, list) {
  657. if ((be32_to_cpu(ept->dst_prog) == prog) &&
  658. (be32_to_cpu(ept->dst_vers) == vers) &&
  659. (ept->restart_state & RESTART_PEND_SVR)) {
  660. spin_lock(&ept->restart_lock);
  661. ept->restart_state &= ~RESTART_PEND_SVR;
  662. spin_unlock(&ept->restart_lock);
  663. D("rpcrouter: Local EPT Reset %08x:%08x \n",
  664. prog, vers);
  665. wake_up(&ept->restart_wait);
  666. wake_up(&ept->wait_q);
  667. }
  668. }
  669. spin_unlock_irqrestore(&local_endpoints_lock, flags);
  670. }
  671. static int process_control_msg(struct rpcrouter_xprt_info *xprt_info,
  672. union rr_control_msg *msg, int len)
  673. {
  674. union rr_control_msg ctl = { 0 };
  675. struct rr_server *server;
  676. struct rr_remote_endpoint *r_ept;
  677. int rc = 0;
  678. unsigned long flags;
  679. static int first = 1;
  680. if (len != sizeof(*msg)) {
  681. RR(KERN_ERR "rpcrouter: r2r msg size %d != %d\n",
  682. len, sizeof(*msg));
  683. return -EINVAL;
  684. }
  685. switch (msg->cmd) {
  686. case RPCROUTER_CTRL_CMD_HELLO:
  687. RR("o HELLO PID %d\n", xprt_info->remote_pid);
  688. memset(&ctl, 0, sizeof(ctl));
  689. ctl.cmd = RPCROUTER_CTRL_CMD_HELLO;
  690. rpcrouter_send_control_msg(xprt_info, &ctl);
  691. xprt_info->initialized = 1;
  692. /* Send list of servers one at a time */
  693. ctl.cmd = RPCROUTER_CTRL_CMD_NEW_SERVER;
  694. /* TODO: long time to hold a spinlock... */
  695. spin_lock_irqsave(&server_list_lock, flags);
  696. list_for_each_entry(server, &server_list, list) {
  697. if (server->pid != RPCROUTER_PID_LOCAL)
  698. continue;
  699. ctl.srv.pid = server->pid;
  700. ctl.srv.cid = server->cid;
  701. ctl.srv.prog = server->prog;
  702. ctl.srv.vers = server->vers;
  703. RR("x NEW_SERVER id=%d:%08x prog=%08x:%08x\n",
  704. server->pid, server->cid,
  705. server->prog, server->vers);
  706. rpcrouter_send_control_msg(xprt_info, &ctl);
  707. }
  708. spin_unlock_irqrestore(&server_list_lock, flags);
  709. if (first) {
  710. first = 0;
  711. queue_work(rpcrouter_workqueue,
  712. &work_create_rpcrouter_pdev);
  713. }
  714. break;
  715. case RPCROUTER_CTRL_CMD_RESUME_TX:
  716. RR("o RESUME_TX id=%d:%08x\n", msg->cli.pid, msg->cli.cid);
  717. r_ept = rpcrouter_lookup_remote_endpoint(msg->cli.pid,
  718. msg->cli.cid);
  719. if (!r_ept) {
  720. printk(KERN_ERR
  721. "rpcrouter: Unable to resume client\n");
  722. break;
  723. }
  724. spin_lock_irqsave(&r_ept->quota_lock, flags);
  725. r_ept->tx_quota_cntr = 0;
  726. spin_unlock_irqrestore(&r_ept->quota_lock, flags);
  727. wake_up(&r_ept->quota_wait);
  728. break;
  729. case RPCROUTER_CTRL_CMD_NEW_SERVER:
  730. if (msg->srv.vers == 0) {
  731. pr_err(
  732. "rpcrouter: Server create rejected, version = 0, "
  733. "program = %08x\n", msg->srv.prog);
  734. break;
  735. }
  736. RR("o NEW_SERVER id=%d:%08x prog=%08x:%08x\n",
  737. msg->srv.pid, msg->srv.cid, msg->srv.prog, msg->srv.vers);
  738. server = rpcrouter_lookup_server(msg->srv.prog, msg->srv.vers);
  739. if (!server) {
  740. server = rpcrouter_create_server(
  741. msg->srv.pid, msg->srv.cid,
  742. msg->srv.prog, msg->srv.vers);
  743. if (!server)
  744. return -ENOMEM;
  745. /*
  746. * XXX: Verify that its okay to add the
  747. * client to our remote client list
  748. * if we get a NEW_SERVER notification
  749. */
  750. if (!rpcrouter_lookup_remote_endpoint(msg->srv.pid,
  751. msg->srv.cid)) {
  752. rc = rpcrouter_create_remote_endpoint(
  753. msg->srv.pid, msg->srv.cid);
  754. if (rc < 0)
  755. printk(KERN_ERR
  756. "rpcrouter:Client create"
  757. "error (%d)\n", rc);
  758. }
  759. rpcrouter_register_board_dev(server);
  760. schedule_work(&work_create_pdevs);
  761. wake_up(&newserver_wait);
  762. } else {
  763. if ((server->pid == msg->srv.pid) &&
  764. (server->cid == msg->srv.cid)) {
  765. handle_server_restart(server,
  766. msg->srv.pid,
  767. msg->srv.cid,
  768. msg->srv.prog,
  769. msg->srv.vers);
  770. } else {
  771. server->pid = msg->srv.pid;
  772. server->cid = msg->srv.cid;
  773. }
  774. }
  775. break;
  776. case RPCROUTER_CTRL_CMD_REMOVE_SERVER:
  777. RR("o REMOVE_SERVER prog=%08x:%d\n",
  778. msg->srv.prog, msg->srv.vers);
  779. server = rpcrouter_lookup_server(msg->srv.prog, msg->srv.vers);
  780. if (server)
  781. rpcrouter_destroy_server(server);
  782. break;
  783. case RPCROUTER_CTRL_CMD_REMOVE_CLIENT:
  784. RR("o REMOVE_CLIENT id=%d:%08x\n", msg->cli.pid, msg->cli.cid);
  785. if (msg->cli.pid == RPCROUTER_PID_LOCAL) {
  786. printk(KERN_ERR
  787. "rpcrouter: Denying remote removal of "
  788. "local client\n");
  789. break;
  790. }
  791. r_ept = rpcrouter_lookup_remote_endpoint(msg->cli.pid,
  792. msg->cli.cid);
  793. if (r_ept) {
  794. spin_lock_irqsave(&remote_endpoints_lock, flags);
  795. list_del(&r_ept->list);
  796. spin_unlock_irqrestore(&remote_endpoints_lock, flags);
  797. kfree(r_ept);
  798. }
  799. /* Notify local clients of this event */
  800. printk(KERN_ERR "rpcrouter: LOCAL NOTIFICATION NOT IMP\n");
  801. rc = -ENOSYS;
  802. break;
  803. case RPCROUTER_CTRL_CMD_PING:
  804. /* No action needed for ping messages received */
  805. RR("o PING\n");
  806. break;
  807. default:
  808. RR("o UNKNOWN(%08x)\n", msg->cmd);
  809. rc = -ENOSYS;
  810. }
  811. return rc;
  812. }
  813. static void do_create_rpcrouter_pdev(struct work_struct *work)
  814. {
  815. D("%s: modem rpc router up\n", __func__);
  816. platform_device_register(&rpcrouter_pdev);
  817. complete_all(&rpc_remote_router_up);
  818. }
  819. static void do_create_pdevs(struct work_struct *work)
  820. {
  821. unsigned long flags;
  822. struct rr_server *server;
  823. /* TODO: race if destroyed while being registered */
  824. spin_lock_irqsave(&server_list_lock, flags);
  825. list_for_each_entry(server, &server_list, list) {
  826. if (server->pid != RPCROUTER_PID_LOCAL) {
  827. if (server->pdev_name[0] == 0) {
  828. sprintf(server->pdev_name, "rs%.8x",
  829. server->prog);
  830. spin_unlock_irqrestore(&server_list_lock,
  831. flags);
  832. msm_rpcrouter_create_server_pdev(server);
  833. schedule_work(&work_create_pdevs);
  834. return;
  835. }
  836. }
  837. }
  838. spin_unlock_irqrestore(&server_list_lock, flags);
  839. }
  840. static void *rr_malloc(unsigned sz)
  841. {
  842. void *ptr = kmalloc(sz, GFP_KERNEL);
  843. if (ptr)
  844. return ptr;
  845. printk(KERN_ERR "rpcrouter: kmalloc of %d failed, retrying...\n", sz);
  846. do {
  847. ptr = kmalloc(sz, GFP_KERNEL);
  848. } while (!ptr);
  849. return ptr;
  850. }
  851. static int rr_read(struct rpcrouter_xprt_info *xprt_info,
  852. void *data, uint32_t len)
  853. {
  854. int rc;
  855. unsigned long flags;
  856. while (!xprt_info->abort_data_read) {
  857. spin_lock_irqsave(&xprt_info->lock, flags);
  858. if (xprt_info->xprt->read_avail() >= len) {
  859. rc = xprt_info->xprt->read(data, len);
  860. spin_unlock_irqrestore(&xprt_info->lock, flags);
  861. if (rc == len && !xprt_info->abort_data_read)
  862. return 0;
  863. else
  864. return -EIO;
  865. }
  866. xprt_info->need_len = len;
  867. wake_unlock(&xprt_info->wakelock);
  868. spin_unlock_irqrestore(&xprt_info->lock, flags);
  869. wait_event(xprt_info->read_wait,
  870. xprt_info->xprt->read_avail() >= len
  871. || xprt_info->abort_data_read);
  872. }
  873. return -EIO;
  874. }
  875. #if defined(CONFIG_MSM_ONCRPCROUTER_DEBUG)
  876. static char *type_to_str(int i)
  877. {
  878. switch (i) {
  879. case RPCROUTER_CTRL_CMD_DATA:
  880. return "data ";
  881. case RPCROUTER_CTRL_CMD_HELLO:
  882. return "hello ";
  883. case RPCROUTER_CTRL_CMD_BYE:
  884. return "bye ";
  885. case RPCROUTER_CTRL_CMD_NEW_SERVER:
  886. return "new_srvr";
  887. case RPCROUTER_CTRL_CMD_REMOVE_SERVER:
  888. return "rmv_srvr";
  889. case RPCROUTER_CTRL_CMD_REMOVE_CLIENT:
  890. return "rmv_clnt";
  891. case RPCROUTER_CTRL_CMD_RESUME_TX:
  892. return "resum_tx";
  893. case RPCROUTER_CTRL_CMD_EXIT:
  894. return "cmd_exit";
  895. default:
  896. return "invalid";
  897. }
  898. }
  899. #endif
  900. static void do_read_data(struct work_struct *work)
  901. {
  902. struct rr_header hdr;
  903. struct rr_packet *pkt;
  904. struct rr_fragment *frag;
  905. struct msm_rpc_endpoint *ept;
  906. #if defined(CONFIG_MSM_ONCRPCROUTER_DEBUG)
  907. struct rpc_request_hdr *rq;
  908. #endif
  909. uint32_t pm, mid;
  910. unsigned long flags;
  911. struct rpcrouter_xprt_info *xprt_info =
  912. container_of(work,
  913. struct rpcrouter_xprt_info,
  914. read_data);
  915. if (rr_read(xprt_info, &hdr, sizeof(hdr)))
  916. goto fail_io;
  917. RR("- ver=%d type=%d src=%d:%08x crx=%d siz=%d dst=%d:%08x\n",
  918. hdr.version, hdr.type, hdr.src_pid, hdr.src_cid,
  919. hdr.confirm_rx, hdr.size, hdr.dst_pid, hdr.dst_cid);
  920. RAW_HDR("[r rr_h] "
  921. "ver=%i,type=%s,src_pid=%08x,src_cid=%08x,"
  922. "confirm_rx=%i,size=%3i,dst_pid=%08x,dst_cid=%08x\n",
  923. hdr.version, type_to_str(hdr.type), hdr.src_pid, hdr.src_cid,
  924. hdr.confirm_rx, hdr.size, hdr.dst_pid, hdr.dst_cid);
  925. if (hdr.version != RPCROUTER_VERSION) {
  926. DIAG("version %d != %d\n", hdr.version, RPCROUTER_VERSION);
  927. goto fail_data;
  928. }
  929. if (hdr.size > RPCROUTER_MSGSIZE_MAX) {
  930. DIAG("msg size %d > max %d\n", hdr.size, RPCROUTER_MSGSIZE_MAX);
  931. goto fail_data;
  932. }
  933. if (hdr.dst_cid == RPCROUTER_ROUTER_ADDRESS) {
  934. if (xprt_info->remote_pid == -1) {
  935. xprt_info->remote_pid = hdr.src_pid;
  936. /* do restart notification */
  937. modem_reset_startup(xprt_info);
  938. }
  939. if (rr_read(xprt_info, xprt_info->r2r_buf, hdr.size))
  940. goto fail_io;
  941. process_control_msg(xprt_info,
  942. (void *) xprt_info->r2r_buf, hdr.size);
  943. goto done;
  944. }
  945. if (hdr.size < sizeof(pm)) {
  946. DIAG("runt packet (no pacmark)\n");
  947. goto fail_data;
  948. }
  949. if (rr_read(xprt_info, &pm, sizeof(pm)))
  950. goto fail_io;
  951. hdr.size -= sizeof(pm);
  952. frag = rr_malloc(sizeof(*frag));
  953. frag->next = NULL;
  954. frag->length = hdr.size;
  955. if (rr_read(xprt_info, frag->data, hdr.size)) {
  956. kfree(frag);
  957. goto fail_io;
  958. }
  959. #if defined(CONFIG_MSM_ONCRPCROUTER_DEBUG)
  960. if ((smd_rpcrouter_debug_mask & RAW_PMR) &&
  961. ((pm >> 30 & 0x1) || (pm >> 31 & 0x1))) {
  962. uint32_t xid = 0;
  963. if (pm >> 30 & 0x1) {
  964. rq = (struct rpc_request_hdr *) frag->data;
  965. xid = ntohl(rq->xid);
  966. }
  967. if ((pm >> 31 & 0x1) || (pm >> 30 & 0x1))
  968. RAW_PMR_NOMASK("xid:0x%03x first=%i,last=%i,mid=%3i,"
  969. "len=%3i,dst_cid=%08x\n",
  970. xid,
  971. pm >> 30 & 0x1,
  972. pm >> 31 & 0x1,
  973. pm >> 16 & 0xFF,
  974. pm & 0xFFFF, hdr.dst_cid);
  975. }
  976. if (smd_rpcrouter_debug_mask & SMEM_LOG) {
  977. rq = (struct rpc_request_hdr *) frag->data;
  978. if (rq->xid == 0)
  979. smem_log_event(SMEM_LOG_PROC_ID_APPS |
  980. RPC_ROUTER_LOG_EVENT_MID_READ,
  981. PACMARK_MID(pm),
  982. hdr.dst_cid,
  983. hdr.src_cid);
  984. else
  985. smem_log_event(SMEM_LOG_PROC_ID_APPS |
  986. RPC_ROUTER_LOG_EVENT_MSG_READ,
  987. ntohl(rq->xid),
  988. hdr.dst_cid,
  989. hdr.src_cid);
  990. }
  991. #endif
  992. spin_lock_irqsave(&local_endpoints_lock, flags);
  993. ept = rpcrouter_lookup_local_endpoint(hdr.dst_cid);
  994. if (!ept) {
  995. spin_unlock_irqrestore(&local_endpoints_lock, flags);
  996. DIAG("no local ept for cid %08x\n", hdr.dst_cid);
  997. kfree(frag);
  998. goto done;
  999. }
  1000. /* See if there is already a partial packet that matches our mid
  1001. * and if so, append this fragment to that packet.
  1002. */
  1003. mid = PACMARK_MID(pm);
  1004. spin_lock(&ept->incomplete_lock);
  1005. list_for_each_entry(pkt, &ept->incomplete, list) {
  1006. if (pkt->mid == mid) {
  1007. pkt->last->next = frag;
  1008. pkt->last = frag;
  1009. pkt->length += frag->length;
  1010. if (PACMARK_LAST(pm)) {
  1011. list_del(&pkt->list);
  1012. spin_unlock(&ept->incomplete_lock);
  1013. goto packet_complete;
  1014. }
  1015. spin_unlock(&ept->incomplete_lock);
  1016. spin_unlock_irqrestore(&local_endpoints_lock, flags);
  1017. goto done;
  1018. }
  1019. }
  1020. spin_unlock(&ept->incomplete_lock);
  1021. spin_unlock_irqrestore(&local_endpoints_lock, flags);
  1022. /* This mid is new -- create a packet for it, and put it on
  1023. * the incomplete list if this fragment is not a last fragment,
  1024. * otherwise put it on the read queue.
  1025. */
  1026. pkt = rr_malloc(sizeof(struct rr_packet));
  1027. pkt->first = frag;
  1028. pkt->last = frag;
  1029. memcpy(&pkt->hdr, &hdr, sizeof(hdr));
  1030. pkt->mid = mid;
  1031. pkt->length = frag->length;
  1032. spin_lock_irqsave(&local_endpoints_lock, flags);
  1033. ept = rpcrouter_lookup_local_endpoint(hdr.dst_cid);
  1034. if (!ept) {
  1035. spin_unlock_irqrestore(&local_endpoints_lock, flags);
  1036. DIAG("no local ept for cid %08x\n", hdr.dst_cid);
  1037. kfree(frag);
  1038. kfree(pkt);
  1039. goto done;
  1040. }
  1041. if (!PACMARK_LAST(pm)) {
  1042. spin_lock(&ept->incomplete_lock);
  1043. list_add_tail(&pkt->list, &ept->incomplete);
  1044. spin_unlock(&ept->incomplete_lock);
  1045. spin_unlock_irqrestore(&local_endpoints_lock, flags);
  1046. goto done;
  1047. }
  1048. packet_complete:
  1049. spin_lock(&ept->read_q_lock);
  1050. D("%s: take read lock on ept %p\n", __func__, ept);
  1051. wake_lock(&ept->read_q_wake_lock);
  1052. list_add_tail(&pkt->list, &ept->read_q);
  1053. wake_up(&ept->wait_q);
  1054. spin_unlock(&ept->read_q_lock);
  1055. spin_unlock_irqrestore(&local_endpoints_lock, flags);
  1056. done:
  1057. if (hdr.confirm_rx) {
  1058. union rr_control_msg msg = { 0 };
  1059. msg.cmd = RPCROUTER_CTRL_CMD_RESUME_TX;
  1060. msg.cli.pid = hdr.dst_pid;
  1061. msg.cli.cid = hdr.dst_cid;
  1062. RR("x RESUME_TX id=%d:%08x\n", msg.cli.pid, msg.cli.cid);
  1063. rpcrouter_send_control_msg(xprt_info, &msg);
  1064. #if defined(CONFIG_MSM_ONCRPCROUTER_DEBUG)
  1065. if (smd_rpcrouter_debug_mask & SMEM_LOG)
  1066. smem_log_event(SMEM_LOG_PROC_ID_APPS |
  1067. RPC_ROUTER_LOG_EVENT_MSG_CFM_SNT,
  1068. RPCROUTER_PID_LOCAL,
  1069. hdr.dst_cid,
  1070. hdr.src_cid);
  1071. #endif
  1072. }
  1073. /* don't requeue if we should be shutting down */
  1074. if (!xprt_info->abort_data_read) {
  1075. queue_work(xprt_info->workqueue, &xprt_info->read_data);
  1076. return;
  1077. }
  1078. D("rpc_router terminating for '%s'\n",
  1079. xprt_info->xprt->name);
  1080. fail_io:
  1081. fail_data:
  1082. D(KERN_ERR "rpc_router has died for '%s'\n",
  1083. xprt_info->xprt->name);
  1084. }
  1085. void msm_rpc_setup_req(struct rpc_request_hdr *hdr, uint32_t prog,
  1086. uint32_t vers, uint32_t proc)
  1087. {
  1088. memset(hdr, 0, sizeof(struct rpc_request_hdr));
  1089. hdr->xid = cpu_to_be32(atomic_add_return(1, &next_xid));
  1090. hdr->rpc_vers = cpu_to_be32(2);
  1091. hdr->prog = cpu_to_be32(prog);
  1092. hdr->vers = cpu_to_be32(vers);
  1093. hdr->procedure = cpu_to_be32(proc);
  1094. }
  1095. EXPORT_SYMBOL(msm_rpc_setup_req);
  1096. struct msm_rpc_endpoint *msm_rpc_open(void)
  1097. {
  1098. struct msm_rpc_endpoint *ept;
  1099. ept = msm_rpcrouter_create_local_endpoint(MKDEV(0, 0));
  1100. if (ept == NULL)
  1101. return ERR_PTR(-ENOMEM);
  1102. return ept;
  1103. }
  1104. void msm_rpc_read_wakeup(struct msm_rpc_endpoint *ept)
  1105. {
  1106. ept->forced_wakeup = 1;
  1107. wake_up(&ept->wait_q);
  1108. }
  1109. int msm_rpc_close(struct msm_rpc_endpoint *ept)
  1110. {
  1111. if (!ept)
  1112. return -EINVAL;
  1113. return msm_rpcrouter_destroy_local_endpoint(ept);
  1114. }
  1115. EXPORT_SYMBOL(msm_rpc_close);
  1116. static int msm_rpc_write_pkt(
  1117. struct rr_header *hdr,
  1118. struct msm_rpc_endpoint *ept,
  1119. struct rr_remote_endpoint *r_ept,
  1120. void *buffer,
  1121. int count,
  1122. int first,
  1123. int last,
  1124. uint32_t mid
  1125. )
  1126. {
  1127. #if defined(CONFIG_MSM_ONCRPCROUTER_DEBUG)
  1128. struct rpc_request_hdr *rq = buffer;
  1129. uint32_t event_id;
  1130. #endif
  1131. uint32_t pacmark;
  1132. unsigned long flags = 0;
  1133. int rc;
  1134. struct rpcrouter_xprt_info *xprt_info;
  1135. int needed;
  1136. DEFINE_WAIT(__wait);
  1137. /* Create routing header */
  1138. hdr->type = RPCROUTER_CTRL_CMD_DATA;
  1139. hdr->version = RPCROUTER_VERSION;
  1140. hdr->src_pid = ept->pid;
  1141. hdr->src_cid = ept->cid;
  1142. hdr->confirm_rx = 0;
  1143. hdr->size = count + sizeof(uint32_t);
  1144. rc = wait_for_restart_and_notify(ept);
  1145. if (rc)
  1146. return rc;
  1147. if (r_ept) {
  1148. for (;;) {
  1149. prepare_to_wait(&r_ept->quota_wait, &__wait,
  1150. TASK_INTERRUPTIBLE);
  1151. spin_lock_irqsave(&r_ept->quota_lock, flags);
  1152. if ((r_ept->tx_quota_cntr <
  1153. RPCROUTER_DEFAULT_RX_QUOTA) ||
  1154. (r_ept->quota_restart_state != RESTART_NORMAL))
  1155. break;
  1156. if (signal_pending(current) &&
  1157. (!(ept->flags & MSM_RPC_UNINTERRUPTIBLE)))
  1158. break;
  1159. spin_unlock_irqrestore(&r_ept->quota_lock, flags);
  1160. schedule();
  1161. }
  1162. finish_wait(&r_ept->quota_wait, &__wait);
  1163. if (r_ept->quota_restart_state != RESTART_NORMAL) {
  1164. spin_lock(&ept->restart_lock);
  1165. ept->restart_state &= ~RESTART_PEND_NTFY;
  1166. spin_unlock(&ept->restart_lock);
  1167. spin_unlock_irqrestore(&r_ept->quota_lock, flags);
  1168. return -ENETRESET;
  1169. }
  1170. if (signal_pending(current) &&
  1171. (!(ept->flags & MSM_RPC_UNINTERRUPTIBLE))) {
  1172. spin_unlock_irqrestore(&r_ept->quota_lock, flags);
  1173. return -ERESTARTSYS;
  1174. }
  1175. r_ept->tx_quota_cntr++;
  1176. if (r_ept->tx_quota_cntr == RPCROUTER_DEFAULT_RX_QUOTA) {
  1177. hdr->confirm_rx = 1;
  1178. #if defined(CONFIG_MSM_ONCRPCROUTER_DEBUG)
  1179. if (smd_rpcrouter_debug_mask & SMEM_LOG) {
  1180. event_id = (rq->xid == 0) ?
  1181. RPC_ROUTER_LOG_EVENT_MID_CFM_REQ :
  1182. RPC_ROUTER_LOG_EVENT_MSG_CFM_REQ;
  1183. smem_log_event(SMEM_LOG_PROC_ID_APPS | event_id,
  1184. hdr->dst_pid,
  1185. hdr->dst_cid,
  1186. hdr->src_cid);
  1187. }
  1188. #endif
  1189. }
  1190. }
  1191. pacmark = PACMARK(count, mid, first, last);
  1192. if (r_ept)
  1193. spin_unlock_irqrestore(&r_ept->quota_lock, flags);
  1194. mutex_lock(&xprt_info_list_lock);
  1195. xprt_info = rpcrouter_get_xprt_info(hdr->dst_pid);
  1196. if (!xprt_info) {
  1197. mutex_unlock(&xprt_info_list_lock);
  1198. return -ENETRESET;
  1199. }
  1200. spin_lock_irqsave(&xprt_info->lock, flags);
  1201. mutex_unlock(&xprt_info_list_lock);
  1202. spin_lock(&ept->restart_lock);
  1203. if (ept->restart_state != RESTART_NORMAL) {
  1204. ept->restart_state &= ~RESTART_PEND_NTFY;
  1205. spin_unlock(&ept->restart_lock);
  1206. spin_unlock_irqrestore(&xprt_info->lock, flags);
  1207. return -ENETRESET;
  1208. }
  1209. needed = sizeof(*hdr) + hdr->size;
  1210. while ((ept->restart_state == RESTART_NORMAL) &&
  1211. (xprt_info->xprt->write_avail() < needed)) {
  1212. spin_unlock(&ept->restart_lock);
  1213. spin_unlock_irqrestore(&xprt_info->lock, flags);
  1214. msleep(250);
  1215. /* refresh xprt pointer to ensure that it hasn't
  1216. * been deleted since our last retrieval */
  1217. mutex_lock(&xprt_info_list_lock);
  1218. xprt_info = rpcrouter_get_xprt_info(hdr->dst_pid);
  1219. if (!xprt_info) {
  1220. mutex_unlock(&xprt_info_list_lock);
  1221. return -ENETRESET;
  1222. }
  1223. spin_lock_irqsave(&xprt_info->lock, flags);
  1224. mutex_unlock(&xprt_info_list_lock);
  1225. spin_lock(&ept->restart_lock);
  1226. }
  1227. if (ept->restart_state != RESTART_NORMAL) {
  1228. ept->restart_state &= ~RESTART_PEND_NTFY;
  1229. spin_unlock(&ept->restart_lock);
  1230. spin_unlock_irqrestore(&xprt_info->lock, flags);
  1231. return -ENETRESET;
  1232. }
  1233. /* TODO: deal with full fifo */
  1234. xprt_info->xprt->write(hdr, sizeof(*hdr), HEADER);
  1235. RAW_HDR("[w rr_h] "
  1236. "ver=%i,type=%s,src_pid=%08x,src_cid=%08x,"
  1237. "confirm_rx=%i,size=%3i,dst_pid=%08x,dst_cid=%08x\n",
  1238. hdr->version, type_to_str(hdr->type),
  1239. hdr->src_pid, hdr->src_cid,
  1240. hdr->confirm_rx, hdr->size, hdr->dst_pid, hdr->dst_cid);
  1241. xprt_info->xprt->write(&pacmark, sizeof(pacmark), PACKMARK);
  1242. #if defined(CONFIG_MSM_ONCRPCROUTER_DEBUG)
  1243. if ((smd_rpcrouter_debug_mask & RAW_PMW) &&
  1244. ((pacmark >> 30 & 0x1) || (pacmark >> 31 & 0x1))) {
  1245. uint32_t xid = 0;
  1246. if (pacmark >> 30 & 0x1)
  1247. xid = ntohl(rq->xid);
  1248. if ((pacmark >> 31 & 0x1) || (pacmark >> 30 & 0x1))
  1249. RAW_PMW_NOMASK("xid:0x%03x first=%i,last=%i,mid=%3i,"
  1250. "len=%3i,src_cid=%x\n",
  1251. xid,
  1252. pacmark >> 30 & 0x1,
  1253. pacmark >> 31 & 0x1,
  1254. pacmark >> 16 & 0xFF,
  1255. pacmark & 0xFFFF, hdr->src_cid);
  1256. }
  1257. #endif
  1258. xprt_info->xprt->write(buffer, count, PAYLOAD);
  1259. spin_unlock(&ept->restart_lock);
  1260. spin_unlock_irqrestore(&xprt_info->lock, flags);
  1261. #if defined(CONFIG_MSM_ONCRPCROUTER_DEBUG)
  1262. if (smd_rpcrouter_debug_mask & SMEM_LOG) {
  1263. if (rq->xid == 0)
  1264. smem_log_event(SMEM_LOG_PROC_ID_APPS |
  1265. RPC_ROUTER_LOG_EVENT_MID_WRITTEN,
  1266. PACMARK_MID(pacmark),
  1267. hdr->dst_cid,
  1268. hdr->src_cid);
  1269. else
  1270. smem_log_event(SMEM_LOG_PROC_ID_APPS |
  1271. RPC_ROUTER_LOG_EVENT_MSG_WRITTEN,
  1272. ntohl(rq->xid),
  1273. hdr->dst_cid,
  1274. hdr->src_cid);
  1275. }
  1276. #endif
  1277. return needed;
  1278. }
  1279. static struct msm_rpc_reply *get_pend_reply(struct msm_rpc_endpoint *ept,
  1280. uint32_t xid)
  1281. {
  1282. unsigned long flags;
  1283. struct msm_rpc_reply *reply;
  1284. spin_lock_irqsave(&ept->reply_q_lock, flags);
  1285. list_for_each_entry(reply, &ept->reply_pend_q, list) {
  1286. if (reply->xid == xid) {
  1287. list_del(&reply->list);
  1288. spin_unlock_irqrestore(&ept->reply_q_lock, flags);
  1289. return reply;
  1290. }
  1291. }
  1292. spin_unlock_irqrestore(&ept->reply_q_lock, flags);
  1293. return NULL;
  1294. }
  1295. void get_requesting_client(struct msm_rpc_endpoint *ept, uint32_t xid,
  1296. struct msm_rpc_client_info *clnt_info)
  1297. {
  1298. unsigned long flags;
  1299. struct msm_rpc_reply *reply;
  1300. if (!clnt_info)
  1301. return;
  1302. spin_lock_irqsave(&ept->reply_q_lock, flags);
  1303. list_for_each_entry(reply, &ept->reply_pend_q, list) {
  1304. if (reply->xid == xid) {
  1305. clnt_info->pid = reply->pid;
  1306. clnt_info->cid = reply->cid;
  1307. clnt_info->prog = reply->prog;
  1308. clnt_info->vers = reply->vers;
  1309. spin_unlock_irqrestore(&ept->reply_q_lock, flags);
  1310. return;
  1311. }
  1312. }
  1313. spin_unlock_irqrestore(&ept->reply_q_lock, flags);
  1314. return;
  1315. }
  1316. static void set_avail_reply(struct msm_rpc_endpoint *ept,
  1317. struct msm_rpc_reply *reply)
  1318. {
  1319. unsigned long flags;
  1320. spin_lock_irqsave(&ept->reply_q_lock, flags);
  1321. list_add_tail(&reply->list, &ept->reply_avail_q);
  1322. spin_unlock_irqrestore(&ept->reply_q_lock, flags);
  1323. }
  1324. static struct msm_rpc_reply *get_avail_reply(struct msm_rpc_endpoint *ept)
  1325. {
  1326. struct msm_rpc_reply *reply;
  1327. unsigned long flags;
  1328. if (list_empty(&ept->reply_avail_q)) {
  1329. if (ept->reply_cnt >= RPCROUTER_PEND_REPLIES_MAX) {
  1330. printk(KERN_ERR
  1331. "exceeding max replies of %d \n",
  1332. RPCROUTER_PEND_REPLIES_MAX);
  1333. return 0;
  1334. }
  1335. reply = kmalloc(sizeof(struct msm_rpc_reply), GFP_KERNEL);
  1336. if (!reply)
  1337. return 0;
  1338. D("Adding reply 0x%08x \n", (unsigned int)reply);
  1339. memset(reply, 0, sizeof(struct msm_rpc_reply));
  1340. spin_lock_irqsave(&ept->reply_q_lock, flags);
  1341. ept->reply_cnt++;
  1342. spin_unlock_irqrestore(&ept->reply_q_lock, flags);
  1343. } else {
  1344. spin_lock_irqsave(&ept->reply_q_lock, flags);
  1345. reply = list_first_entry(&ept->reply_avail_q,
  1346. struct msm_rpc_reply,
  1347. list);
  1348. list_del(&reply->list);
  1349. spin_unlock_irqrestore(&ept->reply_q_lock, flags);
  1350. }
  1351. return reply;
  1352. }
  1353. static void set_pend_reply(struct msm_rpc_endpoint *ept,
  1354. struct msm_rpc_reply *reply)
  1355. {
  1356. unsigned long flags;
  1357. spin_lock_irqsave(&ept->reply_q_lock, flags);
  1358. D("%s: take reply lock on ept %p\n", __func__, ept);
  1359. wake_lock(&ept->reply_q_wake_lock);
  1360. list_add_tail(&reply->list, &ept->reply_pend_q);
  1361. spin_unlock_irqrestore(&ept->reply_q_lock, flags);
  1362. }
  1363. int msm_rpc_write(struct msm_rpc_endpoint *ept, void *buffer, int count)
  1364. {
  1365. struct rr_header hdr;
  1366. struct rpc_request_hdr *rq = buffer;
  1367. struct rr_remote_endpoint *r_ept;
  1368. struct msm_rpc_reply *reply = NULL;
  1369. int max_tx;
  1370. int tx_cnt;
  1371. char *tx_buf;
  1372. int rc;
  1373. int first_pkt = 1;
  1374. uint32_t mid;
  1375. unsigned long flags;
  1376. /* snoop the RPC packet and enforce permissions */
  1377. /* has to have at least the xid and type fields */
  1378. if (count < (sizeof(uint32_t) * 2)) {
  1379. printk(KERN_ERR "rr_write: rejecting runt packet\n");
  1380. return -EINVAL;
  1381. }
  1382. if (rq->type == 0) {
  1383. /* RPC CALL */
  1384. if (count < (sizeof(uint32_t) * 6)) {
  1385. printk(KERN_ERR
  1386. "rr_write: rejecting runt call packet\n");
  1387. return -EINVAL;
  1388. }
  1389. if (ept->dst_pid == 0xffffffff) {
  1390. printk(KERN_ERR "rr_write: not connected\n");
  1391. return -ENOTCONN;
  1392. }
  1393. if ((ept->dst_prog != rq->prog) ||
  1394. ((be32_to_cpu(ept->dst_vers) & 0x0fff0000) !=
  1395. (be32_to_cpu(rq->vers) & 0x0fff0000))) {
  1396. printk(KERN_ERR
  1397. "rr_write: cannot write to %08x:%08x "
  1398. "(bound to %08x:%08x)\n",
  1399. be32_to_cpu(rq->prog), be32_to_cpu(rq->vers),
  1400. be32_to_cpu(ept->dst_prog),
  1401. be32_to_cpu(ept->dst_vers));
  1402. return -EINVAL;
  1403. }
  1404. hdr.dst_pid = ept->dst_pid;
  1405. hdr.dst_cid = ept->dst_cid;
  1406. IO("CALL to %08x:%d @ %d:%08x (%d bytes)\n",
  1407. be32_to_cpu(rq->prog), be32_to_cpu(rq->vers),
  1408. ept->dst_pid, ept->dst_cid, count);
  1409. } else {
  1410. /* RPC REPLY */
  1411. reply = get_pend_reply(ept, rq->xid);
  1412. if (!reply) {
  1413. printk(KERN_ERR
  1414. "rr_write: rejecting, reply not found \n");
  1415. return -EINVAL;
  1416. }
  1417. hdr.dst_pid = reply->pid;
  1418. hdr.dst_cid = reply->cid;
  1419. IO("REPLY to xid=%d @ %d:%08x (%d bytes)\n",
  1420. be32_to_cpu(rq->xid), hdr.dst_pid, hdr.dst_cid, count);
  1421. }
  1422. r_ept = rpcrouter_lookup_remote_endpoint(hdr.dst_pid, hdr.dst_cid);
  1423. if ((!r_ept) && (hdr.dst_pid != RPCROUTER_PID_LOCAL)) {
  1424. printk(KERN_ERR
  1425. "msm_rpc_write(): No route to ept "
  1426. "[PID %x CID %x]\n", hdr.dst_pid, hdr.dst_cid);
  1427. count = -EHOSTUNREACH;
  1428. goto write_release_lock;
  1429. }
  1430. tx_cnt = count;
  1431. tx_buf = buffer;
  1432. mid = atomic_add_return(1, &pm_mid) & 0xFF;
  1433. /* The modem's router can only take 500 bytes of data. The
  1434. first 8 bytes it uses on the modem side for addressing,
  1435. the next 4 bytes are for the pacmark header. */
  1436. max_tx = RPCROUTER_MSGSIZE_MAX - 8 - sizeof(uint32_t);
  1437. IO("Writing %d bytes, max pkt size is %d\n",
  1438. tx_cnt, max_tx);
  1439. while (tx_cnt > 0) {
  1440. if (tx_cnt > max_tx) {
  1441. rc = msm_rpc_write_pkt(&hdr, ept, r_ept,
  1442. tx_buf, max_tx,
  1443. first_pkt, 0, mid);
  1444. if (rc < 0) {
  1445. count = rc;
  1446. goto write_release_lock;
  1447. }
  1448. IO("Wrote %d bytes First %d, Last 0 mid %d\n",
  1449. rc, first_pkt, mid);
  1450. tx_cnt -= max_tx;
  1451. tx_buf += max_tx;
  1452. } else {
  1453. rc = msm_rpc_write_pkt(&hdr, ept, r_ept,
  1454. tx_buf, tx_cnt,
  1455. first_pkt, 1, mid);
  1456. if (rc < 0) {
  1457. count = rc;
  1458. goto write_release_lock;
  1459. }
  1460. IO("Wrote %d bytes First %d Last 1 mid %d\n",
  1461. rc, first_pkt, mid);
  1462. break;
  1463. }
  1464. first_pkt = 0;
  1465. }
  1466. write_release_lock:
  1467. /* if reply, release wakelock after writing to the transport */
  1468. if (rq->type != 0) {
  1469. /* Upon failure, add reply tag to the pending list.
  1470. ** Else add reply tag to the avail/free list. */
  1471. if (count < 0)
  1472. set_pend_reply(ept, reply);
  1473. else
  1474. set_avail_reply(ept, reply);
  1475. spin_lock_irqsave(&ept->reply_q_lock, flags);
  1476. if (list_empty(&ept->reply_pend_q)) {
  1477. D("%s: release reply lock on ept %p\n", __func__, ept);
  1478. wake_unlock(&ept->reply_q_wake_lock);
  1479. }
  1480. spin_unlock_irqrestore(&ept->reply_q_lock, flags);
  1481. }
  1482. return count;
  1483. }
  1484. EXPORT_SYMBOL(msm_rpc_write);
  1485. /*
  1486. * NOTE: It is the responsibility of the caller to kfree buffer
  1487. */
  1488. int msm_rpc_read(struct msm_rpc_endpoint *ept, void **buffer,
  1489. unsigned user_len, long timeout)
  1490. {
  1491. struct rr_fragment *frag, *next;
  1492. char *buf;
  1493. int rc;
  1494. rc = __msm_rpc_read(ept, &frag, user_len, timeout);
  1495. if (rc <= 0)
  1496. return rc;
  1497. /* single-fragment messages conveniently can be
  1498. * returned as-is (the buffer is at the front)
  1499. */
  1500. if (frag->next == 0) {
  1501. *buffer = (void*) frag;
  1502. return rc;
  1503. }
  1504. /* multi-fragment messages, we have to do it the
  1505. * hard way, which is rather disgusting right now
  1506. */
  1507. buf = rr_malloc(rc);
  1508. *buffer = buf;
  1509. while (frag != NULL) {
  1510. memcpy(buf, frag->data, frag->length);
  1511. next = frag->next;
  1512. buf += frag->length;
  1513. kfree(frag);
  1514. frag = next;
  1515. }
  1516. return rc;
  1517. }
  1518. EXPORT_SYMBOL(msm_rpc_read);
  1519. int msm_rpc_call(struct msm_rpc_endpoint *ept, uint32_t proc,
  1520. void *_request, int request_size,
  1521. long timeout)
  1522. {
  1523. return msm_rpc_call_reply(ept, proc,
  1524. _request, request_size,
  1525. NULL, 0, timeout);
  1526. }
  1527. EXPORT_SYMBOL(msm_rpc_call);
  1528. int msm_rpc_call_reply(struct msm_rpc_endpoint *ept, uint32_t proc,
  1529. void *_request, int request_size,
  1530. void *_reply, int reply_size,
  1531. long timeout)
  1532. {
  1533. struct rpc_request_hdr *req = _request;
  1534. struct rpc_reply_hdr *reply;
  1535. int rc;
  1536. if (request_size < sizeof(*req))
  1537. return -ETOOSMALL;
  1538. if (ept->dst_pid == 0xffffffff)
  1539. return -ENOTCONN;
  1540. memset(req, 0, sizeof(*req));
  1541. req->xid = cpu_to_be32(atomic_add_return(1, &next_xid));
  1542. req->rpc_vers = cpu_to_be32(2);
  1543. req->prog = ept->dst_prog;
  1544. req->vers = ept->dst_vers;
  1545. req->procedure = cpu_to_be32(proc);
  1546. rc = msm_rpc_write(ept, req, request_size);
  1547. if (rc < 0)
  1548. return rc;
  1549. for (;;) {
  1550. rc = msm_rpc_read(ept, (void*) &reply, -1, timeout);
  1551. if (rc < 0)
  1552. return rc;
  1553. if (rc < (3 * sizeof(uint32_t))) {
  1554. rc = -EIO;
  1555. break;
  1556. }
  1557. /* we should not get CALL packets -- ignore them */
  1558. if (reply->type == 0) {
  1559. kfree(reply);
  1560. continue;
  1561. }
  1562. /* If an earlier call timed out, we could get the (no
  1563. * longer wanted) reply for it. Ignore replies that
  1564. * we don't expect
  1565. */
  1566. if (reply->xid != req->xid) {
  1567. kfree(reply);
  1568. continue;
  1569. }
  1570. if (reply->reply_stat != 0) {
  1571. rc = -EPERM;
  1572. break;
  1573. }
  1574. if (reply->data.acc_hdr.accept_stat != 0) {
  1575. rc = -EINVAL;
  1576. break;
  1577. }
  1578. if (_reply == NULL) {
  1579. rc = 0;
  1580. break;
  1581. }
  1582. if (rc > reply_size) {
  1583. rc = -ENOMEM;
  1584. } else {
  1585. memcpy(_reply, reply, rc);
  1586. }
  1587. break;
  1588. }
  1589. kfree(reply);
  1590. return rc;
  1591. }
  1592. EXPORT_SYMBOL(msm_rpc_call_reply);
  1593. static inline int ept_packet_available(struct msm_rpc_endpoint *ept)
  1594. {
  1595. unsigned long flags;
  1596. int ret;
  1597. spin_lock_irqsave(&ept->read_q_lock, flags);
  1598. ret = !list_empty(&ept->read_q);
  1599. spin_unlock_irqrestore(&ept->read_q_lock, flags);
  1600. return ret;
  1601. }
  1602. int __msm_rpc_read(struct msm_rpc_endpoint *ept,
  1603. struct rr_fragment **frag_ret,
  1604. unsigned len, long timeout)
  1605. {
  1606. struct rr_packet *pkt;
  1607. struct rpc_request_hdr *rq;
  1608. struct msm_rpc_reply *reply;
  1609. unsigned long flags;
  1610. int rc;
  1611. rc = wait_for_restart_and_notify(ept);
  1612. if (rc)
  1613. return rc;
  1614. IO("READ on ept %p\n", ept);
  1615. if (ept->flags & MSM_RPC_UNINTERRUPTIBLE) {
  1616. if (timeout < 0) {
  1617. wait_event(ept->wait_q, (ept_packet_available(ept) ||
  1618. ept->forced_wakeup ||
  1619. ept->restart_state));
  1620. if (!msm_rpc_clear_netreset(ept))
  1621. return -ENETRESET;
  1622. } else {
  1623. rc = wait_event_timeout(
  1624. ept->wait_q,
  1625. (ept_packet_available(ept) ||
  1626. ept->forced_wakeup ||
  1627. ept->restart_state),
  1628. timeout);
  1629. if (!msm_rpc_clear_netreset(ept))
  1630. return -ENETRESET;
  1631. if (rc == 0)
  1632. return -ETIMEDOUT;
  1633. }
  1634. } else {
  1635. if (timeout < 0) {
  1636. rc = wait_event_interruptible(
  1637. ept->wait_q, (ept_packet_available(ept) ||
  1638. ept->forced_wakeup ||
  1639. ept->restart_state));
  1640. if (!msm_rpc_clear_netreset(ept))
  1641. return -ENETRESET;
  1642. if (rc < 0)
  1643. return rc;
  1644. } else {
  1645. rc = wait_event_interruptible_timeout(
  1646. ept->wait_q,
  1647. (ept_packet_available(ept) ||
  1648. ept->forced_wakeup ||
  1649. ept->restart_state),
  1650. timeout);
  1651. if (!msm_rpc_clear_netreset(ept))
  1652. return -ENETRESET;
  1653. if (rc == 0)
  1654. return -ETIMEDOUT;
  1655. }
  1656. }
  1657. if (ept->forced_wakeup) {
  1658. ept->forced_wakeup = 0;
  1659. return 0;
  1660. }
  1661. spin_lock_irqsave(&ept->read_q_lock, flags);
  1662. if (list_empty(&ept->read_q)) {
  1663. spin_unlock_irqrestore(&ept->read_q_lock, flags);
  1664. return -EAGAIN;
  1665. }
  1666. pkt = list_first_entry(&ept->read_q, struct rr_packet, list);
  1667. if (pkt->length > len) {
  1668. spin_unlock_irqrestore(&ept->read_q_lock, flags);
  1669. return -ETOOSMALL;
  1670. }
  1671. list_del(&pkt->list);
  1672. spin_unlock_irqrestore(&ept->read_q_lock, flags);
  1673. rc = pkt->length;
  1674. *frag_ret = pkt->first;
  1675. rq = (void*) pkt->first->data;
  1676. if ((rc >= (sizeof(uint32_t) * 3)) && (rq->type == 0)) {
  1677. /* RPC CALL */
  1678. reply = get_avail_reply(ept);
  1679. if (!reply) {
  1680. rc = -ENOMEM;
  1681. goto read_release_lock;
  1682. }
  1683. reply->cid = pkt->hdr.src_cid;
  1684. reply->pid = pkt->hdr.src_pid;
  1685. reply->xid = rq->xid;
  1686. reply->prog = rq->prog;
  1687. reply->vers = rq->vers;
  1688. set_pend_reply(ept, reply);
  1689. }
  1690. kfree(pkt);
  1691. IO("READ on ept %p (%d bytes)\n", ept, rc);
  1692. read_release_lock:
  1693. /* release read wakelock after taking reply wakelock */
  1694. spin_lock_irqsave(&ept->read_q_lock, flags);
  1695. if (list_empty(&ept->read_q)) {
  1696. D("%s: release read lock on ept %p\n", __func__, ept);
  1697. wake_unlock(&ept->read_q_wake_lock);
  1698. }
  1699. spin_unlock_irqrestore(&ept->read_q_lock, flags);
  1700. return rc;
  1701. }
  1702. int msm_rpc_is_compatible_version(uint32_t server_version,
  1703. uint32_t client_version)
  1704. {
  1705. if ((server_version & RPC_VERSION_MODE_MASK) !=
  1706. (client_version & RPC_VERSION_MODE_MASK))
  1707. return 0;
  1708. if (server_version & RPC_VERSION_MODE_MASK)
  1709. return server_version == client_version;
  1710. return ((server_version & RPC_VERSION_MAJOR_MASK) ==
  1711. (client_version & RPC_VERSION_MAJOR_MASK)) &&
  1712. ((server_version & RPC_VERSION_MINOR_MASK) >=
  1713. (client_version & RPC_VERSION_MINOR_MASK));
  1714. }
  1715. EXPORT_SYMBOL(msm_rpc_is_compatible_version);
  1716. static struct rr_server *msm_rpc_get_server(uint32_t prog, uint32_t vers,
  1717. uint32_t accept_compatible,
  1718. uint32_t *found_prog)
  1719. {
  1720. struct rr_server *server;
  1721. unsigned long flags;
  1722. if (found_prog == NULL)
  1723. return NULL;
  1724. *found_prog = 0;
  1725. spin_lock_irqsave(&server_list_lock, flags);
  1726. list_for_each_entry(server, &server_list, list) {
  1727. if (server->prog == prog) {
  1728. *found_prog = 1;
  1729. spin_unlock_irqrestore(&server_list_lock, flags);
  1730. if (accept_compatible) {
  1731. if (msm_rpc_is_compatible_version(server->vers,
  1732. vers)) {
  1733. return server;
  1734. } else {
  1735. return NULL;
  1736. }
  1737. } else if (server->vers == vers) {
  1738. return server;
  1739. } else
  1740. return NULL;
  1741. }
  1742. }
  1743. spin_unlock_irqrestore(&server_list_lock, flags);
  1744. return NULL;
  1745. }
  1746. static struct msm_rpc_endpoint *__msm_rpc_connect(uint32_t prog, uint32_t vers,
  1747. uint32_t accept_compatible,
  1748. unsigned flags)
  1749. {
  1750. struct msm_rpc_endpoint *ept;
  1751. struct rr_server *server;
  1752. uint32_t found_prog;
  1753. int rc = 0;
  1754. DEFINE_WAIT(__wait);
  1755. for (;;) {
  1756. prepare_to_wait(&newserver_wait, &__wait,
  1757. TASK_INTERRUPTIBLE);
  1758. server = msm_rpc_get_server(prog, vers, accept_compatible,
  1759. &found_prog);
  1760. if (server)
  1761. break;
  1762. if (found_prog) {
  1763. pr_info("%s: server not found %x:%x\n",
  1764. __func__, prog, vers);
  1765. rc = -EHOSTUNREACH;
  1766. break;
  1767. }
  1768. if (msm_rpc_connect_timeout_ms == 0) {
  1769. rc = -EHOSTUNREACH;
  1770. break;
  1771. }
  1772. if (signal_pending(current)) {
  1773. rc = -ERESTARTSYS;
  1774. break;
  1775. }
  1776. rc = schedule_timeout(
  1777. msecs_to_jiffies(msm_rpc_connect_timeout_ms));
  1778. if (!rc) {
  1779. rc = -ETIMEDOUT;
  1780. break;
  1781. }
  1782. }
  1783. finish_wait(&newserver_wait, &__wait);
  1784. if (!server)
  1785. return ERR_PTR(rc);
  1786. if (accept_compatible && (server->vers != vers)) {
  1787. D("RPC Using new version 0x%08x(0x%08x) prog 0x%08x",
  1788. vers, server->vers, prog);
  1789. D(" ... Continuing\n");
  1790. }
  1791. ept = msm_rpc_open();
  1792. if (IS_ERR(ept))
  1793. return ept;
  1794. ept->flags = flags;
  1795. ept->dst_pid = server->pid;
  1796. ept->dst_cid = server->cid;
  1797. ept->dst_prog = cpu_to_be32(prog);
  1798. ept->dst_vers = cpu_to_be32(server->vers);
  1799. return ept;
  1800. }
  1801. struct msm_rpc_endpoint *msm_rpc_connect_compatible(uint32_t prog,
  1802. uint32_t vers, unsigned flags)
  1803. {
  1804. return __msm_rpc_connect(prog, vers, 1, flags);
  1805. }
  1806. EXPORT_SYMBOL(msm_rpc_connect_compatible);
  1807. struct msm_rpc_endpoint *msm_rpc_connect(uint32_t prog,
  1808. uint32_t vers, unsigned flags)
  1809. {
  1810. return __msm_rpc_connect(prog, vers, 0, flags);
  1811. }
  1812. EXPORT_SYMBOL(msm_rpc_connect);
  1813. /* TODO: permission check? */
  1814. int msm_rpc_register_server(struct msm_rpc_endpoint *ept,
  1815. uint32_t prog, uint32_t vers)
  1816. {
  1817. int rc;
  1818. union rr_control_msg msg = { 0 };
  1819. struct rr_server *server;
  1820. struct rpcrouter_xprt_info *xprt_info;
  1821. server = rpcrouter_create_server(ept->pid, ept->cid,
  1822. prog, vers);
  1823. if (!server)
  1824. return -ENODEV;
  1825. msg.srv.cmd = RPCROUTER_CTRL_CMD_NEW_SERVER;
  1826. msg.srv.pid = ept->pid;
  1827. msg.srv.cid = ept->cid;
  1828. msg.srv.prog = prog;
  1829. msg.srv.vers = vers;
  1830. RR("x NEW_SERVER id=%d:%08x prog=%08x:%08x\n",
  1831. ept->pid, ept->cid, prog, vers);
  1832. mutex_lock(&xprt_info_list_lock);
  1833. list_for_each_entry(xprt_info, &xprt_info_list, list) {
  1834. rc = rpcrouter_send_control_msg(xprt_info, &msg);
  1835. if (rc < 0) {
  1836. mutex_unlock(&xprt_info_list_lock);
  1837. return rc;
  1838. }
  1839. }
  1840. mutex_unlock(&xprt_info_list_lock);
  1841. return 0;
  1842. }
  1843. int msm_rpc_clear_netreset(struct msm_rpc_endpoint *ept)
  1844. {
  1845. unsigned long flags;
  1846. int rc = 1;
  1847. spin_lock_irqsave(&ept->restart_lock, flags);
  1848. if (ept->restart_state != RESTART_NORMAL) {
  1849. ept->restart_state &= ~RESTART_PEND_NTFY;
  1850. rc = 0;
  1851. }
  1852. spin_unlock_irqrestore(&ept->restart_lock, flags);
  1853. return rc;
  1854. }
  1855. /* TODO: permission check -- disallow unreg of somebody else's server */
  1856. int msm_rpc_unregister_server(struct msm_rpc_endpoint *ept,
  1857. uint32_t prog, uint32_t vers)
  1858. {
  1859. struct rr_server *server;
  1860. server = rpcrouter_lookup_server(prog, vers);
  1861. if (!server)
  1862. return -ENOENT;
  1863. rpcrouter_destroy_server(server);
  1864. return 0;
  1865. }
  1866. int msm_rpc_get_curr_pkt_size(struct msm_rpc_endpoint *ept)
  1867. {
  1868. unsigned long flags;
  1869. struct rr_packet *pkt;
  1870. int rc = 0;
  1871. if (!ept)
  1872. return -EINVAL;
  1873. if (!msm_rpc_clear_netreset(ept))
  1874. return -ENETRESET;
  1875. spin_lock_irqsave(&ept->read_q_lock, flags);
  1876. if (!list_empty(&ept->read_q)) {
  1877. pkt = list_first_entry(&ept->read_q, struct rr_packet, list);
  1878. rc = pkt->length;
  1879. }
  1880. spin_unlock_irqrestore(&ept->read_q_lock, flags);
  1881. return rc;
  1882. }
  1883. static int msm_rpcrouter_close(void)
  1884. {
  1885. struct rpcrouter_xprt_info *xprt_info;
  1886. union rr_control_msg ctl = { 0 };
  1887. ctl.cmd = RPCROUTER_CTRL_CMD_BYE;
  1888. mutex_lock(&xprt_info_list_lock);
  1889. while (!list_empty(&xprt_info_list)) {
  1890. xprt_info = list_first_entry(&xprt_info_list,
  1891. struct rpcrouter_xprt_info, list);
  1892. modem_reset_cleanup(xprt_info);
  1893. xprt_info->abort_data_read = 1;
  1894. wake_up(&xprt_info->read_wait);
  1895. rpcrouter_send_control_msg(xprt_info, &ctl);
  1896. xprt_info->xprt->close();
  1897. list_del(&xprt_info->list);
  1898. mutex_unlock(&xprt_info_list_lock);
  1899. flush_workqueue(xprt_info->workqueue);
  1900. destroy_workqueue(xprt_info->workqueue);
  1901. wake_lock_destroy(&xprt_info->wakelock);
  1902. /*free memory*/
  1903. xprt_info->xprt->priv = 0;
  1904. kfree(xprt_info);
  1905. mutex_lock(&xprt_info_list_lock);
  1906. }
  1907. mutex_unlock(&xprt_info_list_lock);
  1908. return 0;
  1909. }
  1910. #if defined(CONFIG_DEBUG_FS)
  1911. static int dump_servers(char *buf, int max)
  1912. {
  1913. int i = 0;
  1914. unsigned long flags;
  1915. struct rr_server *svr;
  1916. const char *sym;
  1917. spin_lock_irqsave(&server_list_lock, flags);
  1918. list_for_each_entry(svr, &server_list, list) {
  1919. i += scnprintf(buf + i, max - i, "pdev_name: %s\n",
  1920. svr->pdev_name);
  1921. i += scnprintf(buf + i, max - i, "pid: 0x%08x\n", svr->pid);
  1922. i += scnprintf(buf + i, max - i, "cid: 0x%08x\n", svr->cid);
  1923. i += scnprintf(buf + i, max - i, "prog: 0x%08x", svr->prog);
  1924. sym = smd_rpc_get_sym(svr->prog);
  1925. if (sym)
  1926. i += scnprintf(buf + i, max - i, " (%s)\n", sym);
  1927. else
  1928. i += scnprintf(buf + i, max - i, "\n");
  1929. i += scnprintf(buf + i, max - i, "vers: 0x%08x\n", svr->vers);
  1930. i += scnprintf(buf + i, max - i, "\n");
  1931. }
  1932. spin_unlock_irqrestore(&server_list_lock, flags);
  1933. return i;
  1934. }
  1935. static int dump_remote_endpoints(char *buf, int max)
  1936. {
  1937. int i = 0;
  1938. unsigned long flags;
  1939. struct rr_remote_endpoint *ept;
  1940. spin_lock_irqsave(&remote_endpoints_lock, flags);
  1941. list_for_each_entry(ept, &remote_endpoints, list) {
  1942. i += scnprintf(buf + i, max - i, "pid: 0x%08x\n", ept->pid);
  1943. i += scnprintf(buf + i, max - i, "cid: 0x%08x\n", ept->cid);
  1944. i += scnprintf(buf + i, max - i, "tx_quota_cntr: %i\n",
  1945. ept->tx_quota_cntr);
  1946. i += scnprintf(buf + i, max - i, "quota_restart_state: %i\n",
  1947. ept->quota_restart_state);
  1948. i += scnprintf(buf + i, max - i, "\n");
  1949. }
  1950. spin_unlock_irqrestore(&remote_endpoints_lock, flags);
  1951. return i;
  1952. }
  1953. static int dump_msm_rpc_endpoint(char *buf, int max)
  1954. {
  1955. int i = 0;
  1956. unsigned long flags;
  1957. struct msm_rpc_reply *reply;
  1958. struct msm_rpc_endpoint *ept;
  1959. struct rr_packet *pkt;
  1960. const char *sym;
  1961. spin_lock_irqsave(&local_endpoints_lock, flags);
  1962. list_for_each_entry(ept, &local_endpoints, list) {
  1963. i += scnprintf(buf + i, max - i, "pid: 0x%08x\n", ept->pid);
  1964. i += scnprintf(buf + i, max - i, "cid: 0x%08x\n", ept->cid);
  1965. i += scnprintf(buf + i, max - i, "dst_pid: 0x%08x\n",
  1966. ept->dst_pid);
  1967. i += scnprintf(buf + i, max - i, "dst_cid: 0x%08x\n",
  1968. ept->dst_cid);
  1969. i += scnprintf(buf + i, max - i, "dst_prog: 0x%08x",
  1970. be32_to_cpu(ept->dst_prog));
  1971. sym = smd_rpc_get_sym(be32_to_cpu(ept->dst_prog));
  1972. if (sym)
  1973. i += scnprintf(buf + i, max - i, " (%s)\n", sym);
  1974. else
  1975. i += scnprintf(buf + i, max - i, "\n");
  1976. i += scnprintf(buf + i, max - i, "dst_vers: 0x%08x\n",
  1977. be32_to_cpu(ept->dst_vers));
  1978. i += scnprintf(buf + i, max - i, "reply_cnt: %i\n",
  1979. ept->reply_cnt);
  1980. i += scnprintf(buf + i, max - i, "restart_state: %i\n",
  1981. ept->restart_state);
  1982. i += scnprintf(buf + i, max - i, "outstanding xids:\n");
  1983. spin_lock(&ept->reply_q_lock);
  1984. list_for_each_entry(reply, &ept->reply_pend_q, list)
  1985. i += scnprintf(buf + i, max - i, " xid = %u\n",
  1986. ntohl(reply->xid));
  1987. spin_unlock(&ept->reply_q_lock);
  1988. i += scnprintf(buf + i, max - i, "complete unread packets:\n");
  1989. spin_lock(&ept->read_q_lock);
  1990. list_for_each_entry(pkt, &ept->read_q, list) {
  1991. i += scnprintf(buf + i, max - i, " mid = %i\n",
  1992. pkt->mid);
  1993. i += scnprintf(buf + i, max - i, " length = %i\n",
  1994. pkt->length);
  1995. }
  1996. spin_unlock(&ept->read_q_lock);
  1997. i += scnprintf(buf + i, max - i, "\n");
  1998. }
  1999. spin_unlock_irqrestore(&local_endpoints_lock, flags);
  2000. return i;
  2001. }
  2002. #define DEBUG_BUFMAX 4096
  2003. static char debug_buffer[DEBUG_BUFMAX];
  2004. static ssize_t debug_read(struct file *file, char __user *buf,
  2005. size_t count, loff_t *ppos)
  2006. {
  2007. int (*fill)(char *buf, int max) = file->private_data;
  2008. int bsize = fill(debug_buffer, DEBUG_BUFMAX);
  2009. return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
  2010. }
  2011. static int debug_open(struct inode *inode, struct file *file)
  2012. {
  2013. file->private_data = inode->i_private;
  2014. return 0;
  2015. }
  2016. static const struct file_operations debug_ops = {
  2017. .read = debug_read,
  2018. .open = debug_open,
  2019. };
  2020. static void debug_create(const char *name, mode_t mode,
  2021. struct dentry *dent,
  2022. int (*fill)(char *buf, int max))
  2023. {
  2024. debugfs_create_file(name, mode, dent, fill, &debug_ops);
  2025. }
  2026. static void debugfs_init(void)
  2027. {
  2028. struct dentry *dent;
  2029. dent = debugfs_create_dir("smd_rpcrouter", 0);
  2030. if (IS_ERR(dent))
  2031. return;
  2032. debug_create("dump_msm_rpc_endpoints", 0444, dent,
  2033. dump_msm_rpc_endpoint);
  2034. debug_create("dump_remote_endpoints", 0444, dent,
  2035. dump_remote_endpoints);
  2036. debug_create("dump_servers", 0444, dent,
  2037. dump_servers);
  2038. }
  2039. #else
  2040. static void debugfs_init(void) {}
  2041. #endif
  2042. static int msm_rpcrouter_add_xprt(struct rpcrouter_xprt *xprt)
  2043. {
  2044. struct rpcrouter_xprt_info *xprt_info;
  2045. D("Registering xprt %s to RPC Router\n", xprt->name);
  2046. xprt_info = kmalloc(sizeof(struct rpcrouter_xprt_info), GFP_KERNEL);
  2047. if (!xprt_info)
  2048. return -ENOMEM;
  2049. xprt_info->xprt = xprt;
  2050. xprt_info->initialized = 0;
  2051. xprt_info->remote_pid = -1;
  2052. init_waitqueue_head(&xprt_info->read_wait);
  2053. spin_lock_init(&xprt_info->lock);
  2054. wake_lock_init(&xprt_info->wakelock,
  2055. WAKE_LOCK_SUSPEND, xprt->name);
  2056. xprt_info->need_len = 0;
  2057. xprt_info->abort_data_read = 0;
  2058. INIT_WORK(&xprt_info->read_data, do_read_data);
  2059. INIT_LIST_HEAD(&xprt_info->list);
  2060. xprt_info->workqueue = create_singlethread_workqueue(xprt->name);
  2061. if (!xprt_info->workqueue) {
  2062. kfree(xprt_info);
  2063. return -ENOMEM;
  2064. }
  2065. if (!strcmp(xprt->name, "rpcrouter_loopback_xprt")) {
  2066. xprt_info->remote_pid = RPCROUTER_PID_LOCAL;
  2067. xprt_info->initialized = 1;
  2068. } else {
  2069. smsm_change_state(SMSM_APPS_STATE, 0, SMSM_RPCINIT);
  2070. }
  2071. mutex_lock(&xprt_info_list_lock);
  2072. list_add_tail(&xprt_info->list, &xprt_info_list);
  2073. mutex_unlock(&xprt_info_list_lock);
  2074. queue_work(xprt_info->workqueue, &xprt_info->read_data);
  2075. xprt->priv = xprt_info;
  2076. return 0;
  2077. }
  2078. static void msm_rpcrouter_remove_xprt(struct rpcrouter_xprt *xprt)
  2079. {
  2080. struct rpcrouter_xprt_info *xprt_info;
  2081. unsigned long flags;
  2082. if (xprt && xprt->priv) {
  2083. xprt_info = xprt->priv;
  2084. /* abort rr_read thread */
  2085. xprt_info->abort_data_read = 1;
  2086. wake_up(&xprt_info->read_wait);
  2087. /* remove xprt from available xprts */
  2088. mutex_lock(&xprt_info_list_lock);
  2089. spin_lock_irqsave(&xprt_info->lock, flags);
  2090. list_del(&xprt_info->list);
  2091. /* unlock the spinlock last to avoid a race
  2092. * condition with rpcrouter_get_xprt_info
  2093. * in msm_rpc_write_pkt in which the
  2094. * xprt is returned from rpcrouter_get_xprt_info
  2095. * and then deleted here. */
  2096. mutex_unlock(&xprt_info_list_lock);
  2097. spin_unlock_irqrestore(&xprt_info->lock, flags);
  2098. /* cleanup workqueues and wakelocks */
  2099. flush_workqueue(xprt_info->workqueue);
  2100. destroy_workqueue(xprt_info->workqueue);
  2101. wake_lock_destroy(&xprt_info->wakelock);
  2102. /* free memory */
  2103. xprt->priv = 0;
  2104. kfree(xprt_info);
  2105. }
  2106. }
  2107. struct rpcrouter_xprt_work {
  2108. struct rpcrouter_xprt *xprt;
  2109. struct work_struct work;
  2110. };
  2111. static void xprt_open_worker(struct work_struct *work)
  2112. {
  2113. struct rpcrouter_xprt_work *xprt_work =
  2114. container_of(work, struct rpcrouter_xprt_work, work);
  2115. msm_rpcrouter_add_xprt(xprt_work->xprt);
  2116. kfree(xprt_work);
  2117. }
  2118. static void xprt_close_worker(struct work_struct *work)
  2119. {
  2120. struct rpcrouter_xprt_work *xprt_work =
  2121. container_of(work, struct rpcrouter_xprt_work, work);
  2122. modem_reset_cleanup(xprt_work->xprt->priv);
  2123. msm_rpcrouter_remove_xprt(xprt_work->xprt);
  2124. if (atomic_dec_return(&pending_close_count) == 0)
  2125. wake_up(&subsystem_restart_wait);
  2126. kfree(xprt_work);
  2127. }
  2128. void msm_rpcrouter_xprt_notify(struct rpcrouter_xprt *xprt, unsigned event)
  2129. {
  2130. struct rpcrouter_xprt_info *xprt_info;
  2131. struct rpcrouter_xprt_work *xprt_work;
  2132. unsigned long flags;
  2133. /* Workqueue is created in init function which works for all existing
  2134. * clients. If this fails in the future, then it will need to be
  2135. * created earlier. */
  2136. BUG_ON(!rpcrouter_workqueue);
  2137. switch (event) {
  2138. case RPCROUTER_XPRT_EVENT_OPEN:
  2139. D("open event for '%s'\n", xprt->name);
  2140. xprt_work = kmalloc(sizeof(struct rpcrouter_xprt_work),
  2141. GFP_ATOMIC);
  2142. xprt_work->xprt = xprt;
  2143. INIT_WORK(&xprt_work->work, xprt_open_worker);
  2144. queue_work(rpcrouter_workqueue, &xprt_work->work);
  2145. break;
  2146. case RPCROUTER_XPRT_EVENT_CLOSE:
  2147. D("close event for '%s'\n", xprt->name);
  2148. atomic_inc(&pending_close_count);
  2149. xprt_work = kmalloc(sizeof(struct rpcrouter_xprt_work),
  2150. GFP_ATOMIC);
  2151. xprt_work->xprt = xprt;
  2152. INIT_WORK(&xprt_work->work, xprt_close_worker);
  2153. queue_work(rpcrouter_workqueue, &xprt_work->work);
  2154. break;
  2155. }
  2156. xprt_info = xprt->priv;
  2157. if (xprt_info) {
  2158. spin_lock_irqsave(&xprt_info->lock, flags);
  2159. /* Check read_avail even for OPEN event to handle missed
  2160. DATA events while processing the OPEN event*/
  2161. if (xprt->read_avail() >= xprt_info->need_len)
  2162. wake_lock(&xprt_info->wakelock);
  2163. wake_up(&xprt_info->read_wait);
  2164. spin_unlock_irqrestore(&xprt_info->lock, flags);
  2165. }
  2166. }
  2167. static int modem_restart_notifier_cb(struct notifier_block *this,
  2168. unsigned long code,
  2169. void *data);
  2170. static struct notifier_block nb = {
  2171. .notifier_call = modem_restart_notifier_cb,
  2172. };
  2173. static int modem_restart_notifier_cb(struct notifier_block *this,
  2174. unsigned long code,
  2175. void *data)
  2176. {
  2177. switch (code) {
  2178. case SUBSYS_BEFORE_SHUTDOWN:
  2179. D("%s: SUBSYS_BEFORE_SHUTDOWN\n", __func__);
  2180. break;
  2181. case SUBSYS_BEFORE_POWERUP:
  2182. D("%s: waiting for RPC restart to complete\n", __func__);
  2183. wait_event(subsystem_restart_wait,
  2184. atomic_read(&pending_close_count) == 0);
  2185. D("%s: finished restart wait\n", __func__);
  2186. break;
  2187. default:
  2188. break;
  2189. }
  2190. return NOTIFY_DONE;
  2191. }
  2192. static void *restart_notifier_handle;
  2193. static __init int modem_restart_late_init(void)
  2194. {
  2195. restart_notifier_handle = subsys_notif_register_notifier("modem", &nb);
  2196. return 0;
  2197. }
  2198. late_initcall(modem_restart_late_init);
  2199. static int __init rpcrouter_init(void)
  2200. {
  2201. int ret;
  2202. msm_rpc_connect_timeout_ms = 0;
  2203. smd_rpcrouter_debug_mask |= SMEM_LOG;
  2204. debugfs_init();
  2205. ret = register_reboot_notifier(&msm_rpc_reboot_notifier);
  2206. if (ret)
  2207. pr_err("%s: Failed to register reboot notifier", __func__);
  2208. /* Initialize what we need to start processing */
  2209. rpcrouter_workqueue =
  2210. create_singlethread_workqueue("rpcrouter");
  2211. if (!rpcrouter_workqueue) {
  2212. msm_rpcrouter_exit_devices();
  2213. return -ENOMEM;
  2214. }
  2215. init_waitqueue_head(&newserver_wait);
  2216. init_waitqueue_head(&subsystem_restart_wait);
  2217. ret = msm_rpcrouter_init_devices();
  2218. if (ret < 0)
  2219. return ret;
  2220. return ret;
  2221. }
  2222. module_init(rpcrouter_init);
  2223. MODULE_DESCRIPTION("MSM RPC Router");
  2224. MODULE_AUTHOR("San Mehat <san@android.com>");
  2225. MODULE_LICENSE("GPL");