spcom.c 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2019, 2021 The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. /*
  7. * Secure-Processor-Communication (SPCOM).
  8. *
  9. * This driver provides communication to Secure Processor (SP)
  10. * over RPMSG framework.
  11. *
  12. * It provides interface to userspace spcomlib.
  13. *
  14. * Userspace application shall use spcomlib for communication with SP.
  15. * Userspace application can be either client or server. spcomlib shall
  16. * use write() file operation to send data, and read() file operation
  17. * to read data.
  18. *
  19. * This driver uses RPMSG with glink-spss as a transport layer.
  20. * This driver exposes "/dev/<sp-channel-name>" file node for each rpmsg
  21. * logical channel.
  22. * This driver exposes "/dev/spcom" file node for some debug/control command.
  23. * The predefined channel "/dev/sp_kernel" is used for loading SP application
  24. * from HLOS.
  25. * This driver exposes "/dev/sp_ssr" file node to allow user space poll for SSR.
  26. * After the remote SP App is loaded, this driver exposes a new file node
  27. * "/dev/<ch-name>" for the matching HLOS App to use.
  28. * The access to predefined file nodes and dynamically allocated file nodes is
  29. * restricted by using unix group and SELinux.
  30. *
  31. * No message routing is used, but using the rpmsg/G-Link "multiplexing" feature
  32. * to use a dedicated logical channel for HLOS and SP Application-Pair.
  33. *
  34. * Each HLOS/SP Application can be either Client or Server or both,
  35. * Messaging is allways point-to-point between 2 HLOS<=>SP applications.
  36. * Each channel exclusevly used by single Client or Server.
  37. *
  38. * User Space Request & Response are synchronous.
  39. * read() & write() operations are blocking until completed or terminated.
  40. */
  41. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  42. #include <linux/kernel.h> /* min() */
  43. #include <linux/module.h> /* MODULE_LICENSE */
  44. #include <linux/device.h> /* class_create() */
  45. #include <linux/slab.h> /* kzalloc() */
  46. #include <linux/fs.h> /* file_operations */
  47. #include <linux/cdev.h> /* cdev_add() */
  48. #include <linux/errno.h> /* EINVAL, ETIMEDOUT */
  49. #include <linux/printk.h> /* pr_err() */
  50. #include <linux/bitops.h> /* BIT(x) */
  51. #include <linux/completion.h> /* wait_for_completion_timeout() */
  52. #include <linux/poll.h> /* POLLOUT */
  53. #include <linux/platform_device.h>
  54. #include <linux/of.h> /* of_property_count_strings() */
  55. #include <linux/workqueue.h>
  56. #include <linux/delay.h> /* msleep() */
  57. #include <linux/dma-buf.h>
  58. #include <linux/limits.h>
  59. #include <linux/rpmsg.h>
  60. #include <linux/atomic.h>
  61. #include <linux/list.h>
  62. #include <linux/spcom.h>
  63. #include <linux/remoteproc.h>
  64. #include <linux/remoteproc.h>
  65. #include <linux/ioctl.h>
  66. #include <linux/ipc_logging.h>
  67. #include <linux/pm.h>
  68. #include <linux/string.h>
  69. #define SPCOM_LOG_PAGE_CNT 10
  70. #define spcom_ipc_log_string(_x...) \
  71. ipc_log_string(spcom_ipc_log_context, _x)
  72. #define spcom_pr_err(_fmt, ...) do { \
  73. pr_err(_fmt, ##__VA_ARGS__); \
  74. spcom_ipc_log_string("%s" pr_fmt(_fmt), "", ##__VA_ARGS__); \
  75. } while (0)
  76. #define spcom_pr_warn(_fmt, ...) do { \
  77. pr_warn(_fmt, ##__VA_ARGS__); \
  78. spcom_ipc_log_string("%s" pr_fmt(_fmt), "", ##__VA_ARGS__); \
  79. } while (0)
  80. #define spcom_pr_info(_fmt, ...) do { \
  81. pr_info(_fmt, ##__VA_ARGS__); \
  82. spcom_ipc_log_string("%s" pr_fmt(_fmt), "", ##__VA_ARGS__); \
  83. } while (0)
  84. #if defined(DEBUG)
  85. #define spcom_pr_dbg(_fmt, ...) do { \
  86. pr_debug(_fmt, ##__VA_ARGS__); \
  87. spcom_ipc_log_string("%s" pr_fmt(_fmt), "", ##__VA_ARGS__); \
  88. } while (0)
  89. #else
  90. #define spcom_pr_dbg(_fmt, ...) do { \
  91. no_printk("%s" pr_fmt(_fmt), KERN_DEBUG, ##__VA_ARGS__); \
  92. spcom_ipc_log_string("%s" pr_fmt(_fmt), "", ##__VA_ARGS__); \
  93. } while (0)
  94. #endif
  95. /**
  96. * Request buffer size.
  97. * Any large data (multiply of 4KB) is provided by temp buffer in DDR.
  98. * Request shall provide the temp buffer physical address (align to 4KB).
  99. * Maximum request/response size of 268 is used to accommodate APDU size.
  100. * From kernel spcom driver perspective a PAGE_SIZE of 4K
  101. * is the actual maximum size for a single read/write file operation.
  102. */
  103. #define SPCOM_MAX_RESPONSE_SIZE 268
  104. /* SPCOM driver name */
  105. #define DEVICE_NAME "spcom"
  106. /* maximum clients that can register over a single channel */
  107. #define SPCOM_MAX_CHANNEL_CLIENTS 5
  108. /* maximum shared DMA_buf buffers should be >= SPCOM_MAX_CHANNELS */
  109. #define SPCOM_MAX_DMA_BUF_PER_CH (SPCOM_MAX_CHANNELS + 4)
  110. /* maximum ION buffer per send request/response command */
  111. #define SPCOM_MAX_ION_BUF_PER_CMD SPCOM_MAX_ION_BUF
  112. /* Maximum command size */
  113. #define SPCOM_MAX_COMMAND_SIZE (PAGE_SIZE)
  114. /* Maximum input size */
  115. #define SPCOM_MAX_READ_SIZE (PAGE_SIZE)
  116. /* Current Process ID */
  117. #define current_pid() ((u32)(current->tgid))
  118. /*
  119. * After both sides get CONNECTED,
  120. * there is a race between one side queueing rx buffer and the other side
  121. * trying to call glink_tx() , this race is only on the 1st tx.
  122. * Do tx retry with some delay to allow the other side to queue rx buffer.
  123. */
  124. #define TX_RETRY_DELAY_MSEC 100
  125. /* SPCOM_MAX_REQUEST_SIZE-or-SPCOM_MAX_RESPONSE_SIZE + header */
  126. #define SPCOM_RX_BUF_SIZE 300
  127. /*
  128. * Initial transaction id, use non-zero nonce for debug.
  129. * Incremented by client on request, and copied back by server on response.
  130. */
  131. #define INITIAL_TXN_ID 0x12345678
  132. /*
  133. * Maximum number of control channels between spcom driver and
  134. * user-mode processes
  135. */
  136. #define SPCOM_MAX_CONTROL_CHANNELS SPCOM_MAX_CHANNELS
  137. /*
  138. * To be used for ioctl copy arg from user if the IOCTL direction is _IOC_WRITE
  139. * Update the union when new ioctl struct is added
  140. */
  141. union spcom_ioctl_arg {
  142. struct spcom_poll_param poll;
  143. struct spcom_ioctl_poll_event poll_event;
  144. struct spcom_ioctl_ch channel;
  145. struct spcom_ioctl_message message;
  146. struct spcom_ioctl_modified_message modified_message;
  147. struct spcom_ioctl_next_request_size next_req_size;
  148. struct spcom_ioctl_dmabuf_lock dmabuf_lock;
  149. } __packed;
  150. /*
  151. * Max time to keep PM from suspend.
  152. * From receive RPMSG packet till wakeup source will be deactivated.
  153. */
  154. #define SPCOM_PM_PACKET_HANDLE_TIMEOUT (2 * MSEC_PER_SEC)
  155. /**
  156. * struct spcom_msg_hdr - Request/Response message header between HLOS and SP.
  157. *
  158. * This header is proceeding any request specific parameters.
  159. * The transaction id is used to match request with response.
  160. * Note: rpmsg API provides the rx/tx data size, so user payload size is
  161. * calculated by reducing the header size.
  162. */
  163. struct spcom_msg_hdr {
  164. uint32_t reserved; /* for future use */
  165. uint32_t txn_id; /* transaction id */
  166. char buf[0]; /* Variable buffer size, must be last field */
  167. } __packed;
  168. /**
  169. * struct spcom_client - Client handle
  170. */
  171. struct spcom_client {
  172. struct spcom_channel *ch;
  173. };
  174. /**
  175. * struct spcom_server - Server handle
  176. */
  177. struct spcom_server {
  178. struct spcom_channel *ch;
  179. };
  180. /**
  181. * struct dma_buf_info - DMA BUF support information
  182. */
  183. struct dma_buf_info {
  184. int fd;
  185. struct dma_buf *handle;
  186. struct dma_buf_attachment *attach;
  187. struct sg_table *sg;
  188. u32 owner_pid;
  189. };
  190. /**
  191. * struct spcom_channel - channel context
  192. */
  193. struct spcom_channel {
  194. char name[SPCOM_CHANNEL_NAME_SIZE];
  195. struct mutex lock;
  196. uint32_t txn_id; /* incrementing nonce per client request */
  197. bool is_server; /* for txn_id and response_timeout_msec */
  198. bool comm_role_undefined; /* is true on channel creation before first tx/rx on channel */
  199. uint32_t response_timeout_msec; /* for client only */
  200. /* char dev */
  201. struct cdev *cdev;
  202. struct device *dev;
  203. struct device_attribute attr;
  204. dev_t devt;
  205. /* rpmsg */
  206. struct rpmsg_driver *rpdrv;
  207. struct rpmsg_device *rpdev;
  208. /* Events notification */
  209. struct completion rx_done;
  210. struct completion connect;
  211. /**
  212. * Only one client or server per non-sharable channel
  213. * SPCOM_MAX_CHANNEL_CLIENTS clients for sharable channel
  214. * Only one tx-rx transaction at a time (request + response)
  215. */
  216. bool is_busy;
  217. bool is_sharable; /* channel's sharable property */
  218. u32 max_clients; /* number of max clients */
  219. u32 active_pid; /* current tx-rx transaction pid */
  220. uint8_t num_clients; /* current number of clients */
  221. struct mutex shared_sync_lock;
  222. u32 pid[SPCOM_MAX_CHANNEL_CLIENTS];
  223. /* abort flags */
  224. bool rpmsg_abort;
  225. /* rx data info */
  226. size_t actual_rx_size; /* actual data size received */
  227. void *rpmsg_rx_buf;
  228. /**
  229. * to track if rx_buf is read in the same session
  230. * in which it is updated
  231. */
  232. uint32_t rx_buf_txn_id;
  233. /* shared buffer lock/unlock support */
  234. struct dma_buf_info dmabuf_array[SPCOM_MAX_DMA_BUF_PER_CH];
  235. };
  236. /**
  237. * struct rx_buff_list - holds rx rpmsg data, before it will be consumed
  238. * by spcom_signal_rx_done worker, item per rx packet
  239. */
  240. struct rx_buff_list {
  241. struct list_head list;
  242. void *rpmsg_rx_buf;
  243. int rx_buf_size;
  244. struct spcom_channel *ch;
  245. };
  246. /**
  247. * struct spcom_channel - control channel information
  248. */
  249. struct spcom_control_channel_info {
  250. u32 pid;
  251. u32 ref_cnt;
  252. } spcom_control_channel_info;
  253. /**
  254. * struct spcom_device - device state structure.
  255. */
  256. struct spcom_device {
  257. char predefined_ch_name[SPCOM_MAX_CHANNELS][SPCOM_CHANNEL_NAME_SIZE];
  258. /* char device info */
  259. struct cdev cdev;
  260. dev_t device_no;
  261. struct class *driver_class;
  262. struct device *class_dev;
  263. struct platform_device *pdev;
  264. struct wakeup_source *ws;
  265. /* rpmsg channels */
  266. struct spcom_channel channels[SPCOM_MAX_CHANNELS];
  267. unsigned int chdev_count;
  268. struct mutex chdev_count_lock;
  269. struct mutex ch_list_lock;
  270. struct completion rpmsg_state_change;
  271. atomic_t rpmsg_dev_count;
  272. atomic_t remove_in_progress;
  273. /* rx data path */
  274. struct list_head rx_list_head;
  275. spinlock_t rx_lock;
  276. int32_t nvm_ion_fd;
  277. uint32_t rmb_error; /* PBL error value storet here */
  278. atomic_t subsys_req;
  279. struct rproc *spss_rproc;
  280. struct property *rproc_prop;
  281. /* Control channels */
  282. struct spcom_control_channel_info control_channels[SPCOM_MAX_CONTROL_CHANNELS];
  283. };
  284. /* Device Driver State */
  285. static struct spcom_device *spcom_dev;
  286. static void *spcom_ipc_log_context;
  287. /* static functions declaration */
  288. static int spcom_create_channel_chardev(const char *name, bool is_sharable);
  289. static int spcom_destroy_channel_chardev(const char *name);
  290. static struct spcom_channel *spcom_find_channel_by_name(const char *name);
  291. static int spcom_register_rpmsg_drv(struct spcom_channel *ch);
  292. static int spcom_unregister_rpmsg_drv(struct spcom_channel *ch);
  293. static void spcom_release_all_channels_of_process(u32 pid);
  294. static int spom_control_channel_add_client(u32 pid);
  295. static int spom_control_channel_remove_client(u32 pid);
  296. /**
  297. * spcom_is_channel_open() - channel is open on this side.
  298. *
  299. * Channel is fully connected, when rpmsg driver is registered and
  300. * rpmsg device probed
  301. */
  302. static inline bool spcom_is_channel_open(struct spcom_channel *ch)
  303. {
  304. return ch->rpdrv != NULL;
  305. }
  306. /**
  307. * spcom_is_channel_connected() - channel is fully connected by both sides.
  308. */
  309. static inline bool spcom_is_channel_connected(struct spcom_channel *ch)
  310. {
  311. /* Channel must be open before it gets connected */
  312. if (!spcom_is_channel_open(ch))
  313. return false;
  314. return ch->rpdev != NULL;
  315. }
  316. /**
  317. * spcom_create_predefined_channels_chardev() - expose predefined channels to
  318. * user space.
  319. *
  320. * Predefined channels list is provided by device tree. Typically, it is for
  321. * known servers on remote side that are not loaded by the HLOS
  322. */
  323. static int spcom_create_predefined_channels_chardev(void)
  324. {
  325. int i, j;
  326. int ret, rc;
  327. static bool is_predefined_created;
  328. const char *name;
  329. if (is_predefined_created)
  330. return 0;
  331. for (i = 0; i < SPCOM_MAX_CHANNELS; i++) {
  332. name = spcom_dev->predefined_ch_name[i];
  333. if (name[0] == 0)
  334. break;
  335. mutex_lock(&spcom_dev->chdev_count_lock);
  336. ret = spcom_create_channel_chardev(name, false);
  337. mutex_unlock(&spcom_dev->chdev_count_lock);
  338. if (ret) {
  339. spcom_pr_err("fail to create chardev [%s], ret [%d]\n",
  340. name, ret);
  341. goto destroy_channels;
  342. }
  343. }
  344. is_predefined_created = true;
  345. return 0;
  346. destroy_channels:
  347. /* destroy previously created channels */
  348. for (j = 0; j < i; j++) {
  349. name = spcom_dev->predefined_ch_name[j];
  350. if (name[0] == 0)
  351. break;
  352. rc = spcom_destroy_channel_chardev(name);
  353. if (rc) {
  354. spcom_pr_err("fail to destroy chardev [%s], ret [%d]\n",
  355. name, rc);
  356. }
  357. }
  358. return ret;
  359. }
  360. /*======================================================================*/
  361. /* UTILITIES */
  362. /*======================================================================*/
  363. /**
  364. * spcom_init_channel() - initialize channel state.
  365. *
  366. * @ch: channel state struct pointer
  367. * @is_sharable: whether channel is sharable
  368. * @name: channel name
  369. */
  370. static int spcom_init_channel(struct spcom_channel *ch,
  371. bool is_sharable,
  372. const char *name)
  373. {
  374. if (!ch || !name || !name[0]) {
  375. spcom_pr_err("invalid parameters\n");
  376. return -EINVAL;
  377. }
  378. strscpy(ch->name, name, SPCOM_CHANNEL_NAME_SIZE);
  379. init_completion(&ch->rx_done);
  380. init_completion(&ch->connect);
  381. mutex_init(&ch->lock);
  382. ch->rpdrv = NULL;
  383. ch->rpdev = NULL;
  384. ch->actual_rx_size = 0;
  385. ch->is_busy = false;
  386. ch->txn_id = INITIAL_TXN_ID; /* use non-zero nonce for debug */
  387. ch->rx_buf_txn_id = ch->txn_id;
  388. memset(ch->pid, 0, sizeof(ch->pid));
  389. ch->rpmsg_abort = false;
  390. ch->rpmsg_rx_buf = NULL;
  391. ch->comm_role_undefined = true;
  392. ch->is_sharable = is_sharable;
  393. ch->max_clients = is_sharable ? SPCOM_MAX_CHANNEL_CLIENTS : 1;
  394. ch->active_pid = 0;
  395. ch->num_clients = 0;
  396. mutex_init(&ch->shared_sync_lock);
  397. return 0;
  398. }
  399. /**
  400. * spcom_find_channel_by_name() - find a channel by name.
  401. *
  402. * @name: channel name
  403. *
  404. * Return: a channel state struct.
  405. */
  406. static struct spcom_channel *spcom_find_channel_by_name(const char *name)
  407. {
  408. int i;
  409. for (i = 0 ; i < ARRAY_SIZE(spcom_dev->channels); i++) {
  410. struct spcom_channel *ch = &spcom_dev->channels[i];
  411. if (strcmp(ch->name, name) == 0)
  412. return ch;
  413. }
  414. return NULL;
  415. }
  416. /**
  417. * spcom_rx() - wait for received data until timeout, unless pending rx data is
  418. * already ready
  419. *
  420. * @ch: channel state struct pointer
  421. * @buf: buffer pointer
  422. * @size: buffer size
  423. *
  424. * Return: size in bytes on success, negative value on failure.
  425. */
  426. static int spcom_rx(struct spcom_channel *ch,
  427. void *buf,
  428. uint32_t size,
  429. uint32_t timeout_msec)
  430. {
  431. unsigned long jiffies = msecs_to_jiffies(timeout_msec);
  432. long timeleft = 1;
  433. int ret = 0;
  434. mutex_lock(&ch->lock);
  435. if (ch->rx_buf_txn_id != ch->txn_id) {
  436. spcom_pr_dbg("ch[%s]:ch->rx_buf_txn_id=%d is updated in a different session\n",
  437. ch->name, ch->rx_buf_txn_id);
  438. if (ch->rpmsg_rx_buf) {
  439. memset(ch->rpmsg_rx_buf, 0, ch->actual_rx_size);
  440. kfree((void *)ch->rpmsg_rx_buf);
  441. ch->rpmsg_rx_buf = NULL;
  442. ch->actual_rx_size = 0;
  443. }
  444. }
  445. /* check for already pending data */
  446. if (!ch->actual_rx_size) {
  447. reinit_completion(&ch->rx_done);
  448. mutex_unlock(&ch->lock); /* unlock while waiting */
  449. /* wait for rx response */
  450. if (timeout_msec)
  451. timeleft = wait_for_completion_interruptible_timeout(
  452. &ch->rx_done, jiffies);
  453. else
  454. ret = wait_for_completion_interruptible(&ch->rx_done);
  455. mutex_lock(&ch->lock);
  456. if (timeout_msec && timeleft == 0) {
  457. spcom_pr_err("ch[%s]: timeout expired %d ms, set txn_id=%d\n",
  458. ch->name, timeout_msec, ch->txn_id);
  459. ch->txn_id++; /* to drop expired rx packet later */
  460. ret = -ETIMEDOUT;
  461. goto exit_err;
  462. } else if (ch->rpmsg_abort) {
  463. spcom_pr_warn("rpmsg channel is closing\n");
  464. ret = -ERESTART;
  465. goto exit_err;
  466. } else if (ret < 0 || timeleft < 0) {
  467. spcom_pr_err("rx wait was interrupted!");
  468. ret = -EINTR; /* abort, not restartable */
  469. goto exit_err;
  470. } else if (ch->actual_rx_size) {
  471. spcom_pr_dbg("ch[%s]:actual_rx_size is [%zu], txn_id %d\n",
  472. ch->name, ch->actual_rx_size, ch->txn_id);
  473. } else {
  474. spcom_pr_err("ch[%s]:actual_rx_size==0\n", ch->name);
  475. ret = -EFAULT;
  476. goto exit_err;
  477. }
  478. } else {
  479. spcom_pr_dbg("ch[%s]:rx data size [%zu], txn_id:%d\n",
  480. ch->name, ch->actual_rx_size, ch->txn_id);
  481. }
  482. if (!ch->rpmsg_rx_buf) {
  483. spcom_pr_err("ch[%s]:invalid rpmsg_rx_buf\n", ch->name);
  484. ret = -ENOMEM;
  485. goto exit_err;
  486. }
  487. size = min_t(size_t, ch->actual_rx_size, size);
  488. memcpy(buf, ch->rpmsg_rx_buf, size);
  489. memset(ch->rpmsg_rx_buf, 0, ch->actual_rx_size);
  490. kfree((void *)ch->rpmsg_rx_buf);
  491. ch->rpmsg_rx_buf = NULL;
  492. ch->actual_rx_size = 0;
  493. mutex_unlock(&ch->lock);
  494. return size;
  495. exit_err:
  496. mutex_unlock(&ch->lock);
  497. return ret;
  498. }
  499. /**
  500. * spcom_get_next_request_size() - get request size.
  501. * already ready
  502. *
  503. * @ch: channel state struct pointer
  504. *
  505. * Server needs the size of the next request to allocate a request buffer.
  506. * Initially used intent-request, however this complicated the remote side,
  507. * so both sides are not using glink_tx() with INTENT_REQ anymore.
  508. *
  509. * Return: size in bytes on success, negative value on failure.
  510. */
  511. static int spcom_get_next_request_size(struct spcom_channel *ch)
  512. {
  513. int size = -1;
  514. int ret = 0;
  515. /* NOTE: Remote clients might not be connected yet.*/
  516. mutex_lock(&ch->lock);
  517. /* Update communication role of channel if not set yet */
  518. if (ch->comm_role_undefined) {
  519. spcom_pr_dbg("server [%s] reading it's first request\n", ch->name);
  520. ch->comm_role_undefined = false;
  521. ch->is_server = true;
  522. }
  523. reinit_completion(&ch->rx_done);
  524. /* check if already got it via callback */
  525. if (ch->actual_rx_size) {
  526. spcom_pr_dbg("next-req-size already ready ch [%s] size [%zu]\n",
  527. ch->name, ch->actual_rx_size);
  528. goto exit_ready;
  529. }
  530. mutex_unlock(&ch->lock); /* unlock while waiting */
  531. ret = wait_for_completion_interruptible(&ch->rx_done);
  532. if (ret < 0) {
  533. spcom_pr_dbg("ch [%s]:interrupted wait ret=%d\n",
  534. ch->name, ret);
  535. goto exit_error;
  536. }
  537. mutex_lock(&ch->lock); /* re-lock after waiting */
  538. if (ch->actual_rx_size == 0) {
  539. spcom_pr_err("invalid rx size [%zu] ch [%s]\n",
  540. ch->actual_rx_size, ch->name);
  541. mutex_unlock(&ch->lock);
  542. ret = -EFAULT;
  543. goto exit_error;
  544. }
  545. exit_ready:
  546. /* actual_rx_size not ecxeeds SPCOM_RX_BUF_SIZE*/
  547. size = (int)ch->actual_rx_size;
  548. if (size > sizeof(struct spcom_msg_hdr)) {
  549. size -= sizeof(struct spcom_msg_hdr);
  550. } else {
  551. spcom_pr_err("rx size [%d] too small\n", size);
  552. ret = -EFAULT;
  553. mutex_unlock(&ch->lock);
  554. goto exit_error;
  555. }
  556. mutex_unlock(&ch->lock);
  557. return size;
  558. exit_error:
  559. return ret;
  560. }
  561. /*======================================================================*/
  562. /* USER SPACE commands handling */
  563. /*======================================================================*/
  564. /**
  565. * spcom_handle_create_channel_command() - Handle Create Channel command from
  566. * user space.
  567. *
  568. * @cmd_buf: command buffer.
  569. * @cmd_size: command buffer size.
  570. *
  571. * Return: 0 on successful operation, negative value otherwise.
  572. */
  573. static int spcom_handle_create_channel_command(void *cmd_buf, int cmd_size)
  574. {
  575. int ret = 0;
  576. struct spcom_user_create_channel_command *cmd = cmd_buf;
  577. if (cmd_size != sizeof(*cmd)) {
  578. spcom_pr_err("cmd_size [%d] , expected [%d]\n",
  579. (int) cmd_size, (int) sizeof(*cmd));
  580. return -EINVAL;
  581. }
  582. mutex_lock(&spcom_dev->chdev_count_lock);
  583. ret = spcom_create_channel_chardev(cmd->ch_name, cmd->is_sharable);
  584. mutex_unlock(&spcom_dev->chdev_count_lock);
  585. if (ret) {
  586. if (-EINVAL == ret)
  587. spcom_pr_err("failed to create channel, ret [%d]\n", ret);
  588. else
  589. spcom_pr_err("failed to create ch[%s], ret [%d]\n", cmd->ch_name, ret);
  590. }
  591. return ret;
  592. }
  593. /**
  594. * spcom_handle_restart_sp_command() - Handle Restart SP command from
  595. * user space.
  596. *
  597. * @cmd_buf: command buffer.
  598. * @cmd_size: command buffer size.
  599. *
  600. * Return: 0 on successful operation, negative value otherwise.
  601. */
  602. static int spcom_handle_restart_sp_command(void *cmd_buf, int cmd_size)
  603. {
  604. struct spcom_user_restart_sp_command *cmd = cmd_buf;
  605. int ret;
  606. if (!cmd) {
  607. spcom_pr_err("NULL cmd_buf\n");
  608. return -EINVAL;
  609. }
  610. if (cmd_size != sizeof(*cmd)) {
  611. spcom_pr_err("cmd_size [%d] , expected [%d]\n",
  612. (int) cmd_size, (int) sizeof(*cmd));
  613. return -EINVAL;
  614. }
  615. spcom_dev->spss_rproc = rproc_get_by_phandle(be32_to_cpup(spcom_dev->rproc_prop->value));
  616. if (!spcom_dev->spss_rproc) {
  617. pr_err("rproc device not found\n");
  618. return -ENODEV; /* no spss peripheral exist */
  619. }
  620. ret = rproc_boot(spcom_dev->spss_rproc);
  621. if (ret == -ETIMEDOUT) {
  622. /* userspace handles retry if needed */
  623. spcom_pr_err("FW loading process timeout\n");
  624. } else if (ret) {
  625. /*
  626. * SPU shutdown. Return value comes from SPU PBL message.
  627. * The error is not recoverable and userspace handles it
  628. * by request and analyse rmb_error value
  629. */
  630. spcom_dev->rmb_error = (uint32_t)ret;
  631. spcom_pr_err("spss crashed during device bootup rmb_error[0x%x]\n",
  632. spcom_dev->rmb_error);
  633. ret = -ENODEV;
  634. } else {
  635. spcom_pr_info("FW loading process is complete\n");
  636. }
  637. return ret;
  638. }
  639. /**
  640. * spcom_handle_send_command() - Handle send request/response from user space.
  641. *
  642. * @buf: command buffer.
  643. * @buf_size: command buffer size.
  644. *
  645. * Return: 0 on successful operation, negative value otherwise.
  646. */
  647. static int spcom_handle_send_command(struct spcom_channel *ch,
  648. void *cmd_buf, int size)
  649. {
  650. int ret = 0;
  651. struct spcom_send_command *cmd = cmd_buf;
  652. uint32_t buf_size;
  653. void *buf;
  654. struct spcom_msg_hdr *hdr;
  655. void *tx_buf;
  656. int tx_buf_size;
  657. uint32_t timeout_msec;
  658. int time_msec = 0;
  659. spcom_pr_dbg("send req/resp ch [%s] size [%d]\n", ch->name, size);
  660. /*
  661. * check that cmd buf size is at least struct size,
  662. * to allow access to struct fields.
  663. */
  664. if (size < sizeof(*cmd)) {
  665. spcom_pr_err("ch [%s] invalid cmd buf\n",
  666. ch->name);
  667. return -EINVAL;
  668. }
  669. /* Check if remote side connect */
  670. if (!spcom_is_channel_connected(ch)) {
  671. spcom_pr_err("ch [%s] remote side not connect\n", ch->name);
  672. return -ENOTCONN;
  673. }
  674. /* parse command buffer */
  675. buf = &cmd->buf;
  676. buf_size = cmd->buf_size;
  677. timeout_msec = cmd->timeout_msec;
  678. /* Check param validity */
  679. if (buf_size > SPCOM_MAX_RESPONSE_SIZE) {
  680. spcom_pr_err("ch [%s] invalid buf size [%d]\n",
  681. ch->name, buf_size);
  682. return -EINVAL;
  683. }
  684. if (size != sizeof(*cmd) + buf_size) {
  685. spcom_pr_err("ch [%s] invalid cmd size [%d]\n",
  686. ch->name, size);
  687. return -EINVAL;
  688. }
  689. /* Allocate Buffers*/
  690. tx_buf_size = sizeof(*hdr) + buf_size;
  691. tx_buf = kzalloc(tx_buf_size, GFP_KERNEL);
  692. if (!tx_buf)
  693. return -ENOMEM;
  694. /* Prepare Tx Buf */
  695. hdr = tx_buf;
  696. mutex_lock(&ch->lock);
  697. if (ch->comm_role_undefined) {
  698. spcom_pr_dbg("ch [%s] send first -> it is client\n", ch->name);
  699. ch->comm_role_undefined = false;
  700. ch->is_server = false;
  701. }
  702. if (!ch->is_server) {
  703. ch->txn_id++; /* client sets the request txn_id */
  704. ch->response_timeout_msec = timeout_msec;
  705. }
  706. hdr->txn_id = ch->txn_id;
  707. /* user buf */
  708. memcpy(hdr->buf, buf, buf_size);
  709. time_msec = 0;
  710. do {
  711. if (ch->rpmsg_abort) {
  712. spcom_pr_err("ch [%s] aborted\n", ch->name);
  713. ret = -ECANCELED;
  714. break;
  715. }
  716. /* may fail when RX intent not queued by SP */
  717. ret = rpmsg_trysend(ch->rpdev->ept, tx_buf, tx_buf_size);
  718. if (ret == 0) {
  719. spcom_pr_dbg("ch[%s]: successfully sent txn_id=%d\n",
  720. ch->name, ch->txn_id);
  721. break;
  722. }
  723. time_msec += TX_RETRY_DELAY_MSEC;
  724. mutex_unlock(&ch->lock);
  725. msleep(TX_RETRY_DELAY_MSEC);
  726. mutex_lock(&ch->lock);
  727. } while ((ret == -EBUSY || ret == -EAGAIN) && time_msec < timeout_msec);
  728. if (ret)
  729. spcom_pr_err("ch [%s] rpmsg_trysend() error (%d), timeout_msec=%d\n",
  730. ch->name, ret, timeout_msec);
  731. if (ch->is_server) {
  732. __pm_relax(spcom_dev->ws);
  733. spcom_pr_dbg("ch[%s]:pm_relax() called for server, after tx\n",
  734. ch->name);
  735. }
  736. mutex_unlock(&ch->lock);
  737. kfree(tx_buf);
  738. return ret;
  739. }
  740. /**
  741. * modify_dma_buf_addr() - replace the ION buffer virtual address with physical
  742. * address in a request or response buffer.
  743. *
  744. * @buf: buffer to modify
  745. * @buf_size: buffer size
  746. * @info: DMA buffer info such as FD and offset in buffer.
  747. *
  748. * Return: 0 on successful operation, negative value otherwise.
  749. */
  750. static int modify_dma_buf_addr(struct spcom_channel *ch, void *buf,
  751. uint32_t buf_size,
  752. struct spcom_dma_buf_info *info)
  753. {
  754. struct dma_buf *dma_buf = NULL;
  755. struct dma_buf_attachment *attach = NULL;
  756. struct sg_table *sg = NULL;
  757. char *ptr = (char *)buf;
  758. dma_addr_t phy_addr = 0;
  759. uint32_t buf_offset = 0;
  760. int fd, ret = 0;
  761. int i = 0;
  762. bool found_handle = false;
  763. fd = info->fd;
  764. buf_offset = info->offset;
  765. ptr += buf_offset;
  766. if (fd < 0) {
  767. spcom_pr_err("invalid fd [%d]\n", fd);
  768. return -ENODEV;
  769. }
  770. if (buf_size < sizeof(uint64_t)) {
  771. spcom_pr_err("buf size too small [%d]\n", buf_size);
  772. return -ENODEV;
  773. }
  774. if (buf_offset % sizeof(uint64_t))
  775. spcom_pr_dbg("offset [%d] is NOT 64-bit aligned\n", buf_offset);
  776. else
  777. spcom_pr_dbg("offset [%d] is 64-bit aligned\n", buf_offset);
  778. if (buf_offset > buf_size - sizeof(uint64_t)) {
  779. spcom_pr_err("invalid buf_offset [%d]\n", buf_offset);
  780. return -ENODEV;
  781. }
  782. dma_buf = dma_buf_get(fd);
  783. if (IS_ERR_OR_NULL(dma_buf)) {
  784. spcom_pr_err("fail to get dma buf handle\n");
  785. return -EINVAL;
  786. }
  787. attach = dma_buf_attach(dma_buf, &spcom_dev->pdev->dev);
  788. if (IS_ERR_OR_NULL(attach)) {
  789. ret = PTR_ERR(attach);
  790. spcom_pr_err("fail to attach dma buf %d\n", ret);
  791. dma_buf_put(dma_buf);
  792. goto mem_map_table_failed;
  793. }
  794. sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  795. if (IS_ERR_OR_NULL(sg)) {
  796. ret = PTR_ERR(sg);
  797. spcom_pr_err("fail to get sg table of dma buf %d\n", ret);
  798. goto mem_map_table_failed;
  799. }
  800. if (sg->sgl) {
  801. phy_addr = sg->sgl->dma_address;
  802. } else {
  803. spcom_pr_err("sgl is NULL\n");
  804. ret = -ENOMEM;
  805. goto mem_map_sg_failed;
  806. }
  807. for (i = 0 ; i < ARRAY_SIZE(ch->dmabuf_array) ; i++) {
  808. if (ch->dmabuf_array[i].handle == dma_buf) {
  809. ch->dmabuf_array[i].attach = attach;
  810. ch->dmabuf_array[i].sg = sg;
  811. found_handle = true;
  812. break;
  813. }
  814. }
  815. if (!found_handle) {
  816. spcom_pr_err("ch [%s]: trying to send modified command on unlocked buffer\n",
  817. ch->name);
  818. ret = -EPERM;
  819. goto mem_map_sg_failed;
  820. }
  821. /* Set the physical address at the buffer offset */
  822. spcom_pr_dbg("dma phys addr = [0x%lx]\n", (long) phy_addr);
  823. memcpy(ptr, &phy_addr, sizeof(phy_addr));
  824. /* Don't unmap the buffer to allow dmabuf sync start/end. */
  825. dma_buf_put(dma_buf);
  826. return 0;
  827. mem_map_sg_failed:
  828. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  829. mem_map_table_failed:
  830. dma_buf_detach(dma_buf, attach);
  831. dma_buf_put(dma_buf);
  832. return ret;
  833. }
  834. /**
  835. * spcom_handle_send_modified_command() - send a request/response with ION
  836. * buffer address. Modify the request/response by replacing the ION buffer
  837. * virtual address with the physical address.
  838. *
  839. * @ch: channel pointer
  840. * @cmd_buf: User space command buffer
  841. * @size: size of user command buffer
  842. *
  843. * Return: 0 on successful operation, negative value otherwise.
  844. */
  845. static int spcom_handle_send_modified_command(struct spcom_channel *ch,
  846. void *cmd_buf, int size)
  847. {
  848. int ret = 0;
  849. struct spcom_user_send_modified_command *cmd = cmd_buf;
  850. uint32_t buf_size;
  851. void *buf;
  852. struct spcom_msg_hdr *hdr;
  853. void *tx_buf;
  854. int tx_buf_size;
  855. struct spcom_ion_info ion_info[SPCOM_MAX_ION_BUF_PER_CMD];
  856. int i;
  857. uint32_t timeout_msec;
  858. int time_msec = 0;
  859. struct spcom_dma_buf_info curr_info = {0};
  860. spcom_pr_dbg("send req/resp ch [%s] size [%d]\n", ch->name, size);
  861. /*
  862. * check that cmd buf size is at least struct size,
  863. * to allow access to struct fields.
  864. */
  865. if (size < sizeof(*cmd)) {
  866. spcom_pr_err("ch [%s] invalid cmd buf\n",
  867. ch->name);
  868. return -EINVAL;
  869. }
  870. /* Check if remote side connect */
  871. if (!spcom_is_channel_connected(ch)) {
  872. spcom_pr_err("ch [%s] remote side not connect\n", ch->name);
  873. return -ENOTCONN;
  874. }
  875. /* parse command buffer */
  876. buf = &cmd->buf;
  877. buf_size = cmd->buf_size;
  878. timeout_msec = cmd->timeout_msec;
  879. memcpy(ion_info, cmd->ion_info, sizeof(ion_info));
  880. /* Check param validity */
  881. if (buf_size > SPCOM_MAX_RESPONSE_SIZE) {
  882. spcom_pr_err("ch [%s] invalid buf size [%d]\n",
  883. ch->name, buf_size);
  884. return -EINVAL;
  885. }
  886. if (size != sizeof(*cmd) + buf_size) {
  887. spcom_pr_err("ch [%s] invalid cmd size [%d]\n",
  888. ch->name, size);
  889. return -EINVAL;
  890. }
  891. /* Allocate Buffers*/
  892. tx_buf_size = sizeof(*hdr) + buf_size;
  893. tx_buf = kzalloc(tx_buf_size, GFP_KERNEL);
  894. if (!tx_buf)
  895. return -ENOMEM;
  896. /* Prepare Tx Buf */
  897. hdr = tx_buf;
  898. mutex_lock(&ch->lock);
  899. if (ch->comm_role_undefined) {
  900. spcom_pr_dbg("ch [%s] send first -> it is client\n", ch->name);
  901. ch->comm_role_undefined = false;
  902. ch->is_server = false;
  903. }
  904. if (!ch->is_server) {
  905. ch->txn_id++; /* client sets the request txn_id */
  906. ch->response_timeout_msec = timeout_msec;
  907. }
  908. hdr->txn_id = ch->txn_id;
  909. /* user buf */
  910. memcpy(hdr->buf, buf, buf_size);
  911. for (i = 0 ; i < ARRAY_SIZE(ion_info) ; i++) {
  912. if (ion_info[i].fd >= 0) {
  913. curr_info.fd = ion_info[i].fd;
  914. curr_info.offset = ion_info[i].buf_offset;
  915. ret = modify_dma_buf_addr(ch, hdr->buf, buf_size, &curr_info);
  916. if (ret < 0) {
  917. mutex_unlock(&ch->lock);
  918. memset(tx_buf, 0, tx_buf_size);
  919. kfree(tx_buf);
  920. return -EFAULT;
  921. }
  922. }
  923. }
  924. time_msec = 0;
  925. do {
  926. if (ch->rpmsg_abort) {
  927. spcom_pr_err("ch[%s]: aborted, txn_id=%d\n",
  928. ch->name, ch->txn_id);
  929. ret = -ECANCELED;
  930. break;
  931. }
  932. /* may fail when RX intent not queued by SP */
  933. ret = rpmsg_trysend(ch->rpdev->ept, tx_buf, tx_buf_size);
  934. if (ret == 0)
  935. break;
  936. time_msec += TX_RETRY_DELAY_MSEC;
  937. mutex_unlock(&ch->lock);
  938. msleep(TX_RETRY_DELAY_MSEC);
  939. mutex_lock(&ch->lock);
  940. } while ((ret == -EBUSY || ret == -EAGAIN) && time_msec < timeout_msec);
  941. if (ret)
  942. spcom_pr_err("ch [%s] rpmsg_trysend() error (%d), timeout_msec=%d\n",
  943. ch->name, ret, timeout_msec);
  944. if (ch->is_server) {
  945. __pm_relax(spcom_dev->ws);
  946. spcom_pr_dbg("ch[%s]:pm_relax() called for server, after tx\n",
  947. ch->name);
  948. }
  949. mutex_unlock(&ch->lock);
  950. memset(tx_buf, 0, tx_buf_size);
  951. kfree(tx_buf);
  952. return ret;
  953. }
  954. /**
  955. * spcom_handle_lock_ion_buf_command() - Lock an shared buffer.
  956. *
  957. * Lock an shared buffer, prevent it from being free if the userspace App crash,
  958. * while it is used by the remote subsystem.
  959. */
  960. static int spcom_handle_lock_ion_buf_command(struct spcom_channel *ch,
  961. void *cmd_buf, int size)
  962. {
  963. struct spcom_user_command *cmd = cmd_buf;
  964. int fd;
  965. int i;
  966. struct dma_buf *dma_buf;
  967. if (size != sizeof(*cmd)) {
  968. spcom_pr_err("cmd size [%d] , expected [%d]\n",
  969. (int) size, (int) sizeof(*cmd));
  970. return -EINVAL;
  971. }
  972. if (cmd->arg > (unsigned int)INT_MAX) {
  973. spcom_pr_err("int overflow [%u]\n", cmd->arg);
  974. return -EINVAL;
  975. }
  976. fd = cmd->arg;
  977. dma_buf = dma_buf_get(fd);
  978. if (IS_ERR_OR_NULL(dma_buf)) {
  979. spcom_pr_err("fail to get dma buf handle\n");
  980. return -EINVAL;
  981. }
  982. /* shared buf lock doesn't involve any rx/tx data to SP. */
  983. mutex_lock(&ch->lock);
  984. /* Check if this shared buffer is already locked */
  985. for (i = 0 ; i < ARRAY_SIZE(ch->dmabuf_array) ; i++) {
  986. if (ch->dmabuf_array[i].handle == dma_buf) {
  987. spcom_pr_dbg("fd [%d] shared buf is already locked\n",
  988. fd);
  989. /* decrement back the ref count */
  990. mutex_unlock(&ch->lock);
  991. dma_buf_put(dma_buf);
  992. return -EINVAL;
  993. }
  994. }
  995. /* Store the dma_buf handle */
  996. for (i = 0 ; i < ARRAY_SIZE(ch->dmabuf_array) ; i++) {
  997. if (ch->dmabuf_array[i].handle == NULL) {
  998. ch->dmabuf_array[i].handle = dma_buf;
  999. ch->dmabuf_array[i].fd = fd;
  1000. spcom_pr_dbg("ch [%s] locked ion buf #%d fd [%d] dma_buf=0x%pK\n",
  1001. ch->name, i,
  1002. ch->dmabuf_array[i].fd,
  1003. ch->dmabuf_array[i].handle);
  1004. mutex_unlock(&ch->lock);
  1005. return 0;
  1006. }
  1007. }
  1008. mutex_unlock(&ch->lock);
  1009. /* decrement back the ref count */
  1010. dma_buf_put(dma_buf);
  1011. spcom_pr_err("no free entry to store ion handle of fd [%d]\n", fd);
  1012. return -EFAULT;
  1013. }
  1014. /**
  1015. * spcom_dmabuf_unlock() - unattach and free dmabuf
  1016. *
  1017. * unattach the dmabuf from spcom driver.
  1018. * decrememt dmabuf ref count.
  1019. */
  1020. static int spcom_dmabuf_unlock(struct dma_buf_info *info, bool verify_buf_owner)
  1021. {
  1022. u32 pid = current_pid();
  1023. if (info == NULL) {
  1024. spcom_pr_err("Invalid dmabuf info pointer\n");
  1025. return -EINVAL;
  1026. }
  1027. if (info->handle == NULL) {
  1028. spcom_pr_err("DMA buffer handle is NULL\n");
  1029. return -EINVAL;
  1030. }
  1031. if (verify_buf_owner) {
  1032. if (pid == 0) {
  1033. spcom_pr_err("Unknown PID\n");
  1034. return -EINVAL;
  1035. }
  1036. if (info->owner_pid != pid) {
  1037. spcom_pr_err("PID [%u] is not the owner of this DMA buffer\n", pid);
  1038. return -EPERM;
  1039. }
  1040. }
  1041. spcom_pr_dbg("unlock dmbuf fd [%d], PID [%u]\n", info->fd, pid);
  1042. if (info->attach) {
  1043. dma_buf_unmap_attachment(info->attach, info->sg, DMA_BIDIRECTIONAL);
  1044. dma_buf_detach(info->handle, info->attach);
  1045. info->attach = NULL;
  1046. info->sg = NULL;
  1047. }
  1048. dma_buf_put(info->handle);
  1049. info->handle = NULL;
  1050. info->fd = -1;
  1051. info->owner_pid = 0;
  1052. return 0;
  1053. }
  1054. /**
  1055. * spcom_handle_unlock_ion_buf_command() - Unlock an ION buffer.
  1056. *
  1057. * Unlock an ION buffer, let it be free, when it is no longer being used by
  1058. * the remote subsystem.
  1059. */
  1060. static int spcom_handle_unlock_ion_buf_command(struct spcom_channel *ch,
  1061. void *cmd_buf, int size)
  1062. {
  1063. int i;
  1064. struct spcom_user_command *cmd = cmd_buf;
  1065. int fd;
  1066. bool found = false;
  1067. struct dma_buf *dma_buf;
  1068. if (size != sizeof(*cmd)) {
  1069. spcom_pr_err("cmd size [%d], expected [%d]\n",
  1070. (int)size, (int)sizeof(*cmd));
  1071. return -EINVAL;
  1072. }
  1073. if (cmd->arg > (unsigned int)INT_MAX) {
  1074. spcom_pr_err("int overflow [%u]\n", cmd->arg);
  1075. return -EINVAL;
  1076. }
  1077. fd = cmd->arg;
  1078. spcom_pr_dbg("Unlock ion buf ch [%s] fd [%d]\n", ch->name, fd);
  1079. dma_buf = dma_buf_get(fd);
  1080. if (IS_ERR_OR_NULL(dma_buf)) {
  1081. spcom_pr_err("fail to get dma buf handle\n");
  1082. return -EINVAL;
  1083. }
  1084. dma_buf_put(dma_buf);
  1085. /* shared buf unlock doesn't involve any rx/tx data to SP. */
  1086. mutex_lock(&ch->lock);
  1087. if (fd == (int) SPCOM_ION_FD_UNLOCK_ALL) {
  1088. spcom_pr_dbg("unlocked ALL ion buf ch [%s]\n", ch->name);
  1089. found = true;
  1090. /* unlock all buf */
  1091. for (i = 0; i < ARRAY_SIZE(ch->dmabuf_array); i++)
  1092. spcom_dmabuf_unlock(&ch->dmabuf_array[i], true);
  1093. } else {
  1094. /* unlock specific buf */
  1095. for (i = 0 ; i < ARRAY_SIZE(ch->dmabuf_array) ; i++) {
  1096. if (!ch->dmabuf_array[i].handle)
  1097. continue;
  1098. if (ch->dmabuf_array[i].handle == dma_buf) {
  1099. spcom_dmabuf_unlock(&ch->dmabuf_array[i], true);
  1100. found = true;
  1101. break;
  1102. }
  1103. }
  1104. }
  1105. mutex_unlock(&ch->lock);
  1106. if (!found) {
  1107. spcom_pr_err("ch [%s] fd [%d] was not found\n", ch->name, fd);
  1108. return -ENODEV;
  1109. }
  1110. return 0;
  1111. }
  1112. /**
  1113. * spcom_handle_enable_ssr_command() - Handle user space request to enable ssr
  1114. *
  1115. * After FOTA SSR is disabled until IAR update occurs.
  1116. * Then - enable SSR again
  1117. *
  1118. * Return: size in bytes on success, negative value on failure.
  1119. */
  1120. static int spcom_handle_enable_ssr_command(void)
  1121. {
  1122. spcom_pr_info("TBD: SSR is enabled after FOTA\n");
  1123. return 0;
  1124. }
  1125. /**
  1126. * spcom_handle_write() - Handle user space write commands.
  1127. *
  1128. * @buf: command buffer.
  1129. * @buf_size: command buffer size.
  1130. *
  1131. * Return: 0 on successful operation, negative value otherwise.
  1132. */
  1133. static int spcom_handle_write(struct spcom_channel *ch,
  1134. void *buf,
  1135. int buf_size)
  1136. {
  1137. int ret = 0;
  1138. struct spcom_user_command *cmd = NULL;
  1139. int cmd_id = 0;
  1140. /* Minimal command should have command-id and argument */
  1141. if (buf_size < sizeof(struct spcom_user_command)) {
  1142. spcom_pr_err("Command buffer size [%d] too small\n", buf_size);
  1143. return -EINVAL;
  1144. }
  1145. cmd = (struct spcom_user_command *)buf;
  1146. cmd_id = (int) cmd->cmd_id;
  1147. spcom_pr_dbg("cmd_id [0x%x]\n", cmd_id);
  1148. if (!ch && cmd_id != SPCOM_CMD_CREATE_CHANNEL
  1149. && cmd_id != SPCOM_CMD_RESTART_SP
  1150. && cmd_id != SPCOM_CMD_ENABLE_SSR) {
  1151. spcom_pr_err("channel context is null\n");
  1152. return -EINVAL;
  1153. }
  1154. if (cmd_id == SPCOM_CMD_SEND || cmd_id == SPCOM_CMD_SEND_MODIFIED) {
  1155. if (!spcom_is_channel_connected(ch)) {
  1156. pr_err("ch [%s] remote side not connected\n", ch->name);
  1157. return -ENOTCONN;
  1158. }
  1159. }
  1160. switch (cmd_id) {
  1161. case SPCOM_CMD_SEND:
  1162. if (ch->is_sharable) {
  1163. /* Channel shared, mutex protect TxRx */
  1164. mutex_lock(&ch->shared_sync_lock);
  1165. /* pid indicates the current active ch */
  1166. ch->active_pid = current_pid();
  1167. }
  1168. ret = spcom_handle_send_command(ch, buf, buf_size);
  1169. break;
  1170. case SPCOM_CMD_SEND_MODIFIED:
  1171. if (ch->is_sharable) {
  1172. /* Channel shared, mutex protect TxRx */
  1173. mutex_lock(&ch->shared_sync_lock);
  1174. /* pid indicates the current active ch */
  1175. ch->active_pid = current_pid();
  1176. }
  1177. ret = spcom_handle_send_modified_command(ch, buf, buf_size);
  1178. break;
  1179. case SPCOM_CMD_LOCK_ION_BUF:
  1180. ret = spcom_handle_lock_ion_buf_command(ch, buf, buf_size);
  1181. break;
  1182. case SPCOM_CMD_UNLOCK_ION_BUF:
  1183. ret = spcom_handle_unlock_ion_buf_command(ch, buf, buf_size);
  1184. break;
  1185. case SPCOM_CMD_CREATE_CHANNEL:
  1186. ret = spcom_handle_create_channel_command(buf, buf_size);
  1187. break;
  1188. case SPCOM_CMD_RESTART_SP:
  1189. ret = spcom_handle_restart_sp_command(buf, buf_size);
  1190. break;
  1191. case SPCOM_CMD_ENABLE_SSR:
  1192. ret = spcom_handle_enable_ssr_command();
  1193. break;
  1194. default:
  1195. spcom_pr_err("Invalid Command Id [0x%x]\n", (int) cmd->cmd_id);
  1196. ret = -EINVAL;
  1197. }
  1198. return ret;
  1199. }
  1200. /**
  1201. * spcom_handle_get_req_size() - Handle user space get request size command
  1202. *
  1203. * @ch: channel handle
  1204. * @buf: command buffer.
  1205. * @size: command buffer size.
  1206. *
  1207. * Return: size in bytes on success, negative value on failure.
  1208. */
  1209. static int spcom_handle_get_req_size(struct spcom_channel *ch,
  1210. void *buf,
  1211. uint32_t size)
  1212. {
  1213. int ret = -1;
  1214. uint32_t next_req_size = 0;
  1215. if (size < sizeof(next_req_size)) {
  1216. spcom_pr_err("buf size [%d] too small\n", (int) size);
  1217. return -EINVAL;
  1218. }
  1219. ret = spcom_get_next_request_size(ch);
  1220. if (ret < 0)
  1221. return ret;
  1222. next_req_size = (uint32_t) ret;
  1223. memcpy(buf, &next_req_size, sizeof(next_req_size));
  1224. spcom_pr_dbg("next_req_size [%d]\n", next_req_size);
  1225. return sizeof(next_req_size); /* can't exceed user buffer size */
  1226. }
  1227. /**
  1228. * spcom_handle_read_req_resp() - Handle user space get request/response command
  1229. *
  1230. * @ch: channel handle
  1231. * @buf: command buffer.
  1232. * @size: command buffer size.
  1233. *
  1234. * Return: size in bytes on success, negative value on failure.
  1235. */
  1236. static int spcom_handle_read_req_resp(struct spcom_channel *ch,
  1237. void *buf,
  1238. uint32_t size)
  1239. {
  1240. int ret;
  1241. struct spcom_msg_hdr *hdr;
  1242. void *rx_buf;
  1243. int rx_buf_size;
  1244. uint32_t timeout_msec = 0; /* client only */
  1245. /* Check if remote side connect */
  1246. if (!spcom_is_channel_connected(ch)) {
  1247. spcom_pr_err("ch [%s] remote side not connect\n", ch->name);
  1248. return -ENOTCONN;
  1249. }
  1250. /* Check param validity */
  1251. if (size > SPCOM_MAX_RESPONSE_SIZE) {
  1252. spcom_pr_err("ch [%s] invalid size [%d]\n",
  1253. ch->name, size);
  1254. return -EINVAL;
  1255. }
  1256. /* Allocate Buffers*/
  1257. rx_buf_size = sizeof(*hdr) + size;
  1258. rx_buf = kzalloc(rx_buf_size, GFP_KERNEL);
  1259. if (!rx_buf)
  1260. return -ENOMEM;
  1261. /*
  1262. * client response timeout depends on the request
  1263. * handling time on the remote side .
  1264. */
  1265. if (!ch->is_server) {
  1266. timeout_msec = ch->response_timeout_msec;
  1267. spcom_pr_dbg("response_timeout_msec:%d\n", (int) timeout_msec);
  1268. }
  1269. ret = spcom_rx(ch, rx_buf, rx_buf_size, timeout_msec);
  1270. if (ret < 0) {
  1271. spcom_pr_err("rx error %d\n", ret);
  1272. goto exit_err;
  1273. } else {
  1274. size = ret; /* actual_rx_size */
  1275. }
  1276. hdr = rx_buf;
  1277. if (ch->is_server) {
  1278. ch->txn_id = hdr->txn_id;
  1279. spcom_pr_dbg("ch[%s]:request txn_id [0x%x]\n",
  1280. ch->name, ch->txn_id);
  1281. }
  1282. /* copy data to user without the header */
  1283. if (size > sizeof(*hdr)) {
  1284. size -= sizeof(*hdr);
  1285. memcpy(buf, hdr->buf, size);
  1286. } else {
  1287. spcom_pr_err("rx size [%d] too small\n", size);
  1288. ret = -EFAULT;
  1289. goto exit_err;
  1290. }
  1291. kfree(rx_buf);
  1292. return size;
  1293. exit_err:
  1294. kfree(rx_buf);
  1295. return ret;
  1296. }
  1297. /**
  1298. * spcom_handle_read() - Handle user space read request/response or
  1299. * request-size command
  1300. *
  1301. * @ch: channel handle
  1302. * @buf: command buffer.
  1303. * @size: command buffer size.
  1304. *
  1305. * A special size SPCOM_GET_NEXT_REQUEST_SIZE, which is bigger than the max
  1306. * response/request tells the kernel that user space only need the size.
  1307. *
  1308. * Return: size in bytes on success, negative value on failure.
  1309. */
  1310. static int spcom_handle_read(struct spcom_channel *ch,
  1311. void *buf,
  1312. uint32_t size)
  1313. {
  1314. int ret = -1;
  1315. if (size == SPCOM_GET_NEXT_REQUEST_SIZE) {
  1316. ch->is_server = true;
  1317. ret = spcom_handle_get_req_size(ch, buf, size);
  1318. } else {
  1319. ret = spcom_handle_read_req_resp(ch, buf, size);
  1320. }
  1321. mutex_lock(&ch->lock);
  1322. if (!ch->is_server) {
  1323. __pm_relax(spcom_dev->ws);
  1324. spcom_pr_dbg("ch[%s]:pm_relax() called for client\n",
  1325. ch->name);
  1326. }
  1327. mutex_unlock(&ch->lock);
  1328. return ret;
  1329. }
  1330. /*======================================================================*/
  1331. /* CHAR DEVICE USER SPACE INTERFACE */
  1332. /*======================================================================*/
  1333. /**
  1334. * file_to_filename() - get the filename from file pointer.
  1335. *
  1336. * @filp: file pointer
  1337. *
  1338. * it is used for debug prints.
  1339. *
  1340. * Return: filename string or "unknown".
  1341. */
  1342. static char *file_to_filename(struct file *filp)
  1343. {
  1344. struct dentry *dentry = NULL;
  1345. char *filename = NULL;
  1346. if (!filp || !filp->f_path.dentry)
  1347. return "unknown";
  1348. dentry = filp->f_path.dentry;
  1349. filename = dentry->d_iname;
  1350. return filename;
  1351. }
  1352. bool is_proc_channel_owner(struct spcom_channel *ch, u32 pid)
  1353. {
  1354. int i = 0;
  1355. for (i = 0; i < ch->max_clients; ++i) {
  1356. if (ch->pid[i] == pid)
  1357. return true;
  1358. }
  1359. return false;
  1360. }
  1361. /**
  1362. * spcom_device_open() - handle channel file open() from user space.
  1363. *
  1364. * @filp: file pointer
  1365. *
  1366. * The file name (without path) is the channel name.
  1367. * Register rpmsg driver matching with channel name.
  1368. * Store the channel context in the file private date pointer for future
  1369. * read/write/close operations.
  1370. */
  1371. static int spcom_device_open(struct inode *inode, struct file *filp)
  1372. {
  1373. struct spcom_channel *ch;
  1374. int ret;
  1375. const char *name = file_to_filename(filp);
  1376. u32 pid = current_pid();
  1377. int i = 0;
  1378. if (atomic_read(&spcom_dev->remove_in_progress)) {
  1379. spcom_pr_err("module remove in progress\n");
  1380. return -ENODEV;
  1381. }
  1382. if (strcmp(name, "unknown") == 0) {
  1383. spcom_pr_err("name is unknown\n");
  1384. return -EINVAL;
  1385. }
  1386. if (strcmp(name, "sp_ssr") == 0) {
  1387. spcom_pr_dbg("sp_ssr dev node skipped\n");
  1388. return 0;
  1389. }
  1390. if (pid == 0) {
  1391. spcom_pr_err("unknown PID\n");
  1392. return -EINVAL;
  1393. }
  1394. if (strcmp(name, DEVICE_NAME) == 0) {
  1395. spcom_pr_dbg("control channel is opened by pid %u\n", pid);
  1396. return spom_control_channel_add_client(pid);
  1397. }
  1398. ch = spcom_find_channel_by_name(name);
  1399. if (!ch) {
  1400. spcom_pr_err("ch[%s] doesn't exist, load app first\n", name);
  1401. return -ENODEV;
  1402. }
  1403. mutex_lock(&ch->lock);
  1404. if (!spcom_is_channel_open(ch)) {
  1405. reinit_completion(&ch->connect);
  1406. /* channel was closed need to register drv again */
  1407. ret = spcom_register_rpmsg_drv(ch);
  1408. if (ret < 0) {
  1409. spcom_pr_err("register rpmsg driver failed %d\n", ret);
  1410. mutex_unlock(&ch->lock);
  1411. return ret;
  1412. }
  1413. }
  1414. /* max number of channel clients reached */
  1415. if (ch->is_busy) {
  1416. spcom_pr_err("channel [%s] is BUSY and has %d of clients, already in use\n",
  1417. name, ch->num_clients);
  1418. mutex_unlock(&ch->lock);
  1419. return -EBUSY;
  1420. }
  1421. /*
  1422. * if same client trying to register again, this will fail
  1423. */
  1424. for (i = 0; i < SPCOM_MAX_CHANNEL_CLIENTS; i++) {
  1425. if (ch->pid[i] == pid) {
  1426. spcom_pr_err("client with pid [%d] is already registered with channel[%s]\n",
  1427. pid, name);
  1428. mutex_unlock(&ch->lock);
  1429. return -EINVAL;
  1430. }
  1431. }
  1432. if (ch->is_sharable) {
  1433. ch->num_clients++;
  1434. if (ch->num_clients >= SPCOM_MAX_CHANNEL_CLIENTS)
  1435. ch->is_busy = true;
  1436. else
  1437. ch->is_busy = false;
  1438. /* pid array has pid of all the registered client.
  1439. * If we reach here, the is_busy flag check above guarantees
  1440. * that we have at least one non-zero pid index
  1441. */
  1442. for (i = 0; i < SPCOM_MAX_CHANNEL_CLIENTS; i++) {
  1443. if (ch->pid[i] == 0) {
  1444. ch->pid[i] = pid;
  1445. break;
  1446. }
  1447. }
  1448. } else {
  1449. ch->num_clients = 1;
  1450. ch->is_busy = true;
  1451. /* Only first index of pid is relevant in case of
  1452. * non-shareable
  1453. */
  1454. ch->pid[0] = pid;
  1455. }
  1456. mutex_unlock(&ch->lock);
  1457. filp->private_data = ch;
  1458. return 0;
  1459. }
  1460. /**
  1461. * spcom_device_release() - handle channel file close() from user space.
  1462. *
  1463. * @filp: file pointer
  1464. *
  1465. * The file name (without path) is the channel name.
  1466. * Open the relevant glink channel.
  1467. * Store the channel context in the file private
  1468. * date pointer for future read/write/close
  1469. * operations.
  1470. */
  1471. static int spcom_device_release(struct inode *inode, struct file *filp)
  1472. {
  1473. struct spcom_channel *ch;
  1474. const char *name = file_to_filename(filp);
  1475. int ret = 0;
  1476. int i = 0;
  1477. u32 pid = current_pid();
  1478. if (strcmp(name, "unknown") == 0) {
  1479. spcom_pr_err("name is unknown\n");
  1480. return -EINVAL;
  1481. }
  1482. if (strcmp(name, "sp_ssr") == 0) {
  1483. spcom_pr_dbg("sp_ssr dev node skipped\n");
  1484. return 0;
  1485. }
  1486. if (pid == 0) {
  1487. spcom_pr_err("unknown PID\n");
  1488. return -EINVAL;
  1489. }
  1490. if (strcmp(name, DEVICE_NAME) == 0) {
  1491. spcom_pr_dbg("PID [%d] release control channel\n", pid);
  1492. return spom_control_channel_remove_client(pid);
  1493. }
  1494. ch = filp->private_data;
  1495. if (!ch) {
  1496. spcom_pr_dbg("ch is NULL, file name %s\n",
  1497. file_to_filename(filp));
  1498. return -ENODEV;
  1499. }
  1500. mutex_lock(&ch->lock);
  1501. /* channel might be already closed or disconnected */
  1502. if (!spcom_is_channel_open(ch)) {
  1503. spcom_pr_dbg("ch [%s] already closed\n", name);
  1504. mutex_unlock(&ch->lock);
  1505. return 0;
  1506. }
  1507. for (i = 0; i < SPCOM_MAX_CHANNEL_CLIENTS; i++) {
  1508. if (ch->pid[i] == pid) {
  1509. spcom_pr_dbg("PID [%x] is releasing ch [%s]\n", pid, name);
  1510. ch->pid[i] = 0;
  1511. break;
  1512. }
  1513. }
  1514. if (ch->num_clients > 1) {
  1515. /*
  1516. * Shared client is trying to close channel,
  1517. * release the sync_lock if applicable
  1518. */
  1519. if (ch->active_pid == pid) {
  1520. spcom_pr_dbg("active_pid [%x] is releasing ch [%s] sync lock\n",
  1521. ch->active_pid, name);
  1522. /* No longer the current active user of the channel */
  1523. ch->active_pid = 0;
  1524. mutex_unlock(&ch->shared_sync_lock);
  1525. }
  1526. ch->num_clients--;
  1527. ch->is_busy = false;
  1528. mutex_unlock(&ch->lock);
  1529. return 0;
  1530. }
  1531. ch->is_busy = false;
  1532. ch->num_clients = 0;
  1533. ch->active_pid = 0;
  1534. if (ch->rpmsg_rx_buf) {
  1535. spcom_pr_dbg("ch [%s] discarding unconsumed rx packet actual_rx_size=%zd\n",
  1536. name, ch->actual_rx_size);
  1537. kfree(ch->rpmsg_rx_buf);
  1538. ch->rpmsg_rx_buf = NULL;
  1539. }
  1540. ch->actual_rx_size = 0;
  1541. mutex_unlock(&ch->lock);
  1542. filp->private_data = NULL;
  1543. return ret;
  1544. }
  1545. /**
  1546. * spcom_device_write() - handle channel file write() from user space.
  1547. *
  1548. * @filp: file pointer
  1549. *
  1550. * Return: On Success - same size as number of bytes to write.
  1551. * On Failure - negative value.
  1552. */
  1553. static ssize_t spcom_device_write(struct file *filp,
  1554. const char __user *user_buff,
  1555. size_t size, loff_t *f_pos)
  1556. {
  1557. int ret;
  1558. char *buf;
  1559. struct spcom_channel *ch;
  1560. const char *name = file_to_filename(filp);
  1561. int buf_size = 0;
  1562. if (!user_buff || !f_pos || !filp) {
  1563. spcom_pr_err("invalid null parameters\n");
  1564. return -EINVAL;
  1565. }
  1566. if (atomic_read(&spcom_dev->remove_in_progress)) {
  1567. spcom_pr_err("module remove in progress\n");
  1568. return -ENODEV;
  1569. }
  1570. if (*f_pos != 0) {
  1571. spcom_pr_err("offset should be zero, no sparse buffer\n");
  1572. return -EINVAL;
  1573. }
  1574. if (!name) {
  1575. spcom_pr_err("name is NULL\n");
  1576. return -EINVAL;
  1577. }
  1578. if (strcmp(name, "unknown") == 0) {
  1579. spcom_pr_err("name is unknown\n");
  1580. return -EINVAL;
  1581. }
  1582. if (size > SPCOM_MAX_COMMAND_SIZE) {
  1583. spcom_pr_err("size [%d] > max size [%d]\n",
  1584. (int) size, (int) SPCOM_MAX_COMMAND_SIZE);
  1585. return -EINVAL;
  1586. }
  1587. ch = filp->private_data;
  1588. if (!ch) {
  1589. if (strcmp(name, DEVICE_NAME) != 0) {
  1590. spcom_pr_err("NULL ch, command not allowed\n");
  1591. return -EINVAL;
  1592. }
  1593. }
  1594. buf_size = size; /* explicit casting size_t to int */
  1595. buf = kzalloc(size, GFP_KERNEL);
  1596. if (buf == NULL)
  1597. return -ENOMEM;
  1598. ret = copy_from_user(buf, user_buff, size);
  1599. if (ret) {
  1600. spcom_pr_err("Unable to copy from user (err %d)\n", ret);
  1601. kfree(buf);
  1602. return -EFAULT;
  1603. }
  1604. ret = spcom_handle_write(ch, buf, buf_size);
  1605. if (ret) {
  1606. spcom_pr_err("handle command error [%d]\n", ret);
  1607. kfree(buf);
  1608. if (ch && ch->active_pid == current_pid()) {
  1609. ch->active_pid = 0;
  1610. mutex_unlock(&ch->shared_sync_lock);
  1611. }
  1612. return ret;
  1613. }
  1614. kfree(buf);
  1615. return size;
  1616. }
  1617. /**
  1618. * spcom_device_read() - handle channel file read() from user space.
  1619. *
  1620. * @filp: file pointer
  1621. *
  1622. * Return: number of bytes to read on success, negative value on
  1623. * failure.
  1624. */
  1625. static ssize_t spcom_device_read(struct file *filp, char __user *user_buff,
  1626. size_t size, loff_t *f_pos)
  1627. {
  1628. int ret = 0;
  1629. int actual_size = 0;
  1630. char *buf;
  1631. struct spcom_channel *ch;
  1632. const char *name = file_to_filename(filp);
  1633. uint32_t buf_size = 0;
  1634. u32 cur_pid = current_pid();
  1635. spcom_pr_dbg("read file [%s], size = %d bytes\n", name, (int) size);
  1636. if (atomic_read(&spcom_dev->remove_in_progress)) {
  1637. spcom_pr_err("module remove in progress\n");
  1638. return -ENODEV;
  1639. }
  1640. if (strcmp(name, "unknown") == 0) {
  1641. spcom_pr_err("name is unknown\n");
  1642. return -EINVAL;
  1643. }
  1644. if (!user_buff || !f_pos ||
  1645. (size == 0) || (size > SPCOM_MAX_READ_SIZE)) {
  1646. spcom_pr_err("invalid parameters\n");
  1647. return -EINVAL;
  1648. }
  1649. buf_size = size; /* explicit casting size_t to uint32_t */
  1650. ch = filp->private_data;
  1651. if (ch == NULL) {
  1652. spcom_pr_err("invalid ch pointer, file [%s]\n", name);
  1653. return -EINVAL;
  1654. }
  1655. if (!spcom_is_channel_open(ch)) {
  1656. spcom_pr_err("ch is not open, file [%s]\n", name);
  1657. return -EINVAL;
  1658. }
  1659. buf = kzalloc(size, GFP_KERNEL);
  1660. if (buf == NULL) {
  1661. ret = -ENOMEM;
  1662. goto exit_err;
  1663. }
  1664. ret = spcom_handle_read(ch, buf, buf_size);
  1665. if (ret < 0) {
  1666. if (ret != -ERESTARTSYS)
  1667. spcom_pr_err("read error [%d]\n", ret);
  1668. goto exit_err;
  1669. }
  1670. actual_size = ret;
  1671. if ((actual_size == 0) || (actual_size > size)) {
  1672. spcom_pr_err("invalid actual_size [%d]\n", actual_size);
  1673. ret = -EFAULT;
  1674. goto exit_err;
  1675. }
  1676. ret = copy_to_user(user_buff, buf, actual_size);
  1677. if (ret) {
  1678. spcom_pr_err("Unable to copy to user, err = %d\n", ret);
  1679. ret = -EFAULT;
  1680. goto exit_err;
  1681. }
  1682. kfree(buf);
  1683. if (ch->active_pid == cur_pid) {
  1684. ch->active_pid = 0;
  1685. mutex_unlock(&ch->shared_sync_lock);
  1686. }
  1687. return actual_size;
  1688. exit_err:
  1689. kfree(buf);
  1690. if (ch->active_pid == cur_pid) {
  1691. ch->active_pid = 0;
  1692. mutex_unlock(&ch->shared_sync_lock);
  1693. }
  1694. return ret;
  1695. }
  1696. static inline int handle_poll(struct file *file,
  1697. struct spcom_poll_param *op, int *user_retval)
  1698. {
  1699. struct spcom_channel *ch = NULL;
  1700. const char *name = file_to_filename(file);
  1701. int ready = 0;
  1702. int ret = 0;
  1703. switch (op->cmd_id) {
  1704. case SPCOM_LINK_STATE_REQ:
  1705. if (op->wait) {
  1706. reinit_completion(&spcom_dev->rpmsg_state_change);
  1707. ready = wait_for_completion_interruptible(
  1708. &spcom_dev->rpmsg_state_change);
  1709. spcom_pr_dbg("ch [%s] link state change signaled\n",
  1710. name);
  1711. }
  1712. op->retval = atomic_read(&spcom_dev->rpmsg_dev_count) > 0;
  1713. break;
  1714. case SPCOM_CH_CONN_STATE_REQ:
  1715. if (strcmp(name, DEVICE_NAME) == 0) {
  1716. spcom_pr_err("invalid control device: %s\n", name);
  1717. return -EINVAL;
  1718. }
  1719. /*
  1720. * ch is not expected to be NULL since user must call open()
  1721. * to get FD before it can call poll().
  1722. * open() will fail if no ch related to the char-device.
  1723. */
  1724. ch = file->private_data;
  1725. if (!ch) {
  1726. spcom_pr_err("invalid ch pointer, file [%s]\n", name);
  1727. ret = -EINVAL;
  1728. break;
  1729. }
  1730. if (op->wait) {
  1731. reinit_completion(&ch->connect);
  1732. ready = wait_for_completion_interruptible(&ch->connect);
  1733. spcom_pr_dbg("ch [%s] connect signaled\n", name);
  1734. }
  1735. mutex_lock(&ch->lock);
  1736. op->retval = (ch->rpdev != NULL);
  1737. mutex_unlock(&ch->lock);
  1738. break;
  1739. default:
  1740. spcom_pr_err("ch [%s] unsupported ioctl:%u\n",
  1741. name, op->cmd_id);
  1742. ret = -EINVAL;
  1743. }
  1744. if (ready < 0) { /* wait was interrupted */
  1745. spcom_pr_info("interrupted wait retval=%d\n", op->retval);
  1746. ret = -EINTR;
  1747. }
  1748. if (!ret) {
  1749. ret = put_user(op->retval, user_retval);
  1750. if (ret) {
  1751. spcom_pr_err("Unable to copy link state to user [%d]\n", ret);
  1752. ret = -EFAULT;
  1753. }
  1754. }
  1755. return ret;
  1756. }
  1757. /*======================================================================*/
  1758. /* IOCTL USER SPACE COMMANDS HANDLING */
  1759. /*======================================================================*/
  1760. /**
  1761. * spcom_register_channel
  1762. *
  1763. * @brief Helper function to register SPCOM channel
  1764. *
  1765. * @param[in] ch SPCOM channel
  1766. *
  1767. * @return zero on success, negative value otherwise.
  1768. */
  1769. static int spcom_register_channel(struct spcom_channel *ch)
  1770. {
  1771. const char *ch_name = NULL;
  1772. u32 pid = current_pid();
  1773. u32 i = 0;
  1774. ch_name = ch->name;
  1775. mutex_lock(&ch->lock);
  1776. spcom_pr_dbg("the pid name [%s] of pid [%d] try to open [%s] channel\n",
  1777. current->comm, pid, ch_name);
  1778. if (!spcom_is_channel_open(ch))
  1779. spcom_pr_err("channel [%s] is not open\n", ch_name);
  1780. /* max number of channel clients reached */
  1781. if (ch->is_busy) {
  1782. spcom_pr_err("channel [%s] is occupied by max num of clients [%d]\n",
  1783. ch_name, ch->num_clients);
  1784. mutex_unlock(&ch->lock);
  1785. return -EBUSY;
  1786. }
  1787. /* check if same client trying to register again */
  1788. for (i = 0; i < ch->max_clients; ++i) {
  1789. if (ch->pid[i] == pid) {
  1790. spcom_pr_err("client with pid[%d] is already registered with channel[%s]\n",
  1791. pid, ch_name);
  1792. mutex_unlock(&ch->lock);
  1793. return -EINVAL;
  1794. }
  1795. }
  1796. if (ch->is_sharable) {
  1797. /* set or add channel owner PID */
  1798. for (i = 0; i < ch->max_clients; ++i) {
  1799. if (ch->pid[i] == 0)
  1800. break;
  1801. }
  1802. } else {
  1803. i = 0;
  1804. }
  1805. /* update channel client, whether the channel is shared or not */
  1806. ch->pid[i] = pid;
  1807. ch->num_clients++;
  1808. ch->is_busy = (ch->num_clients == ch->max_clients) ? true : false;
  1809. mutex_unlock(&ch->lock);
  1810. return 0;
  1811. }
  1812. /**
  1813. * is_valid_ch_name
  1814. *
  1815. * @brief Helper function to verify channel name pointer
  1816. *
  1817. * @param[in] ch_name channel name
  1818. *
  1819. * @return true if valid channel name pointer, false otherwise.
  1820. */
  1821. static inline bool is_valid_ch_name(const char *ch_name)
  1822. {
  1823. static const uint32_t maxlen = SPCOM_CHANNEL_NAME_SIZE;
  1824. return (ch_name && ch_name[0] && (strnlen(ch_name, maxlen) < maxlen));
  1825. }
  1826. /**
  1827. * is_control_channel_name
  1828. *
  1829. * @brief Helper function to check if channel name is the control channel name
  1830. *
  1831. * @param[in] ch_name channel name
  1832. *
  1833. * @return true if control channel name, false otherwise.
  1834. */
  1835. static inline bool is_control_channel_name(const char *ch_name)
  1836. {
  1837. return (is_valid_ch_name(ch_name) && (!strcmp(ch_name, DEVICE_NAME)));
  1838. }
  1839. /**
  1840. * spcom_channel_deinit_locked
  1841. *
  1842. * @brief Helper function to handle deinit of SPCOM channel while holding the channel's lock
  1843. *
  1844. * @param[in] ch SPCOM channel
  1845. *
  1846. * @return zero on successful operation, negative value otherwise.
  1847. */
  1848. static int spcom_channel_deinit_locked(struct spcom_channel *ch, u32 pid)
  1849. {
  1850. const char *ch_name = ch->name;
  1851. bool found = false;
  1852. u32 i = 0;
  1853. /* channel might be already closed or disconnected */
  1854. if (!spcom_is_channel_open(ch)) {
  1855. spcom_pr_dbg("ch [%s] already closed\n", ch_name);
  1856. return 0;
  1857. }
  1858. /* check that current process is a client of this channel */
  1859. for (i = 0; i < ch->max_clients; ++i) {
  1860. if (ch->pid[i] == pid) {
  1861. found = true;
  1862. spcom_pr_dbg("pid [%x] is releasing ch [%s]\n", pid, ch_name);
  1863. ch->pid[i] = 0;
  1864. break;
  1865. }
  1866. }
  1867. /* if the current process is not a valid client of this channel, return an error */
  1868. if (!found) {
  1869. spcom_pr_dbg("pid [%d] is not a client of ch [%s]\n", pid, ch_name);
  1870. return -EFAULT;
  1871. }
  1872. /* If shared client owner is trying to close channel, release the sync_lock if
  1873. * applicable
  1874. */
  1875. if (ch->active_pid == pid) {
  1876. spcom_pr_dbg("active_pid [%d] is releasing ch [%s] sync lock\n",
  1877. ch->active_pid, ch_name);
  1878. ch->active_pid = 0;
  1879. mutex_unlock(&ch->shared_sync_lock);
  1880. }
  1881. ch->num_clients--;
  1882. ch->is_busy = false;
  1883. if (ch->rpmsg_rx_buf) {
  1884. spcom_pr_dbg("ch [%s] discarding unconsumed rx packet actual_rx_size=%zd\n",
  1885. ch_name, ch->actual_rx_size);
  1886. kfree(ch->rpmsg_rx_buf);
  1887. ch->rpmsg_rx_buf = NULL;
  1888. }
  1889. ch->actual_rx_size = 0;
  1890. return 0;
  1891. }
  1892. /**
  1893. * spcom_channel_deinit
  1894. *
  1895. * @brief Helper function to handle deinit of SPCOM channel
  1896. *
  1897. * @param[in] ch SPCOM channel
  1898. *
  1899. * @return zero on successful operation, negative value otherwise.
  1900. */
  1901. static int spcom_channel_deinit(struct spcom_channel *ch)
  1902. {
  1903. uint32_t pid = current_pid();
  1904. int ret;
  1905. if (!pid) {
  1906. spcom_pr_err("unknown PID\n");
  1907. return -EINVAL;
  1908. }
  1909. mutex_lock(&ch->lock);
  1910. ret = spcom_channel_deinit_locked(ch, pid);
  1911. mutex_unlock(&ch->lock);
  1912. return ret;
  1913. }
  1914. /**
  1915. * spcom_send_message
  1916. *
  1917. * @brief Helper function to send request/response IOCTL command from user space
  1918. *
  1919. * @param[in] arg IOCTL command arguments
  1920. * @param[in] buffer user message buffer
  1921. * @param[in] is_modified flag to indicate if this is a modified message or regular message
  1922. *
  1923. * Return: 0 on successful operation, negative value otherwise.
  1924. */
  1925. static int spcom_send_message(void *arg, void *buffer, bool is_modified)
  1926. {
  1927. struct spcom_channel *ch = NULL;
  1928. struct spcom_msg_hdr *hdr = NULL;
  1929. struct spcom_ioctl_message *usr_msg = NULL;
  1930. struct spcom_ioctl_modified_message *usr_mod_msg = NULL;
  1931. const char *ch_name = NULL;
  1932. void *msg_buf = NULL;
  1933. void *tx_buf = NULL;
  1934. int tx_buf_size = 0;
  1935. uint32_t msg_buf_sz = 0;
  1936. uint32_t dma_info_array_sz = 0;
  1937. int ret = 0;
  1938. int time_msec = 0;
  1939. int timeout_msec = 0;
  1940. int i = 0;
  1941. /* Parse message command arguments */
  1942. if (is_modified) {
  1943. /* Send regular message */
  1944. usr_mod_msg = arg;
  1945. msg_buf = buffer;
  1946. msg_buf_sz = usr_mod_msg->buffer_size;
  1947. timeout_msec = usr_mod_msg->timeout_msec;
  1948. ch_name = usr_mod_msg->ch_name;
  1949. } else {
  1950. /* Send modified message */
  1951. usr_msg = arg;
  1952. msg_buf = buffer;
  1953. msg_buf_sz = usr_msg->buffer_size;
  1954. timeout_msec = usr_msg->timeout_msec;
  1955. ch_name = usr_msg->ch_name;
  1956. }
  1957. /* Verify channel name */
  1958. if (!is_valid_ch_name(ch_name)) {
  1959. spcom_pr_err("invalid channel name\n");
  1960. return -EINVAL;
  1961. }
  1962. /* Verify message buffer size */
  1963. if (msg_buf_sz > SPCOM_MAX_RESPONSE_SIZE) {
  1964. spcom_pr_err("ch [%s] message size is too big [%d]\n", ch_name, msg_buf_sz);
  1965. return -EINVAL;
  1966. }
  1967. /* DEVICE_NAME is reserved for control channel */
  1968. if (is_control_channel_name(ch_name)) {
  1969. spcom_pr_err("cannot send message on control channel\n");
  1970. return -EFAULT;
  1971. }
  1972. /* Find spcom channel in spcom channel list by name */
  1973. ch = spcom_find_channel_by_name(ch_name);
  1974. if (!ch)
  1975. return -ENODEV;
  1976. /* Check if remote side connect */
  1977. if (!spcom_is_channel_connected(ch)) {
  1978. spcom_pr_err("ch [%s] remote side not connected\n", ch_name);
  1979. return -ENOTCONN;
  1980. }
  1981. spcom_pr_dbg("sending message with size [%d], ch [%s]\n", msg_buf_sz, ch_name);
  1982. /* Allocate and prepare Tx buffer */
  1983. tx_buf_size = sizeof(*hdr) + msg_buf_sz;
  1984. tx_buf = kzalloc(tx_buf_size, GFP_KERNEL);
  1985. if (!tx_buf)
  1986. return -ENOMEM;
  1987. hdr = tx_buf;
  1988. if (ch->is_sharable)
  1989. mutex_lock(&ch->shared_sync_lock);
  1990. mutex_lock(&ch->lock);
  1991. /* For SPCOM server, get next request size must be called before sending a response
  1992. * if we got here and the role is not set it means the channel is SPCOM client
  1993. */
  1994. if (ch->comm_role_undefined) {
  1995. spcom_pr_dbg("client ch [%s] sending it's first message\n", ch_name);
  1996. ch->comm_role_undefined = false;
  1997. ch->is_server = false;
  1998. }
  1999. /* Protect shared channel Tx by lock and set the current process as the owner */
  2000. if (ch->is_sharable) {
  2001. if (ch->is_server) {
  2002. mutex_unlock(&ch->shared_sync_lock);
  2003. spcom_pr_err("server spcom channel cannot be shared\n");
  2004. goto send_message_err;
  2005. }
  2006. ch->active_pid = current_pid();
  2007. }
  2008. /* SPCom client sets the request txn_id */
  2009. if (!ch->is_server) {
  2010. ch->txn_id++;
  2011. ch->response_timeout_msec = timeout_msec;
  2012. }
  2013. hdr->txn_id = ch->txn_id;
  2014. /* Copy user buffer to tx */
  2015. memcpy(hdr->buf, msg_buf, msg_buf_sz);
  2016. /* For modified message write the DMA buffer addresses to the user defined offset in the
  2017. * message buffer
  2018. */
  2019. if (is_modified) {
  2020. dma_info_array_sz = ARRAY_SIZE(usr_mod_msg->info);
  2021. if (dma_info_array_sz != SPCOM_MAX_DMA_BUF) {
  2022. spcom_pr_err("invalid info array size [%d], ch[%s]\n", dma_info_array_sz,
  2023. ch_name);
  2024. ret = -EINVAL;
  2025. goto send_message_err;
  2026. }
  2027. for (i = 0; i < dma_info_array_sz; ++i) {
  2028. if (usr_mod_msg->info[i].fd >= 0) {
  2029. ret = modify_dma_buf_addr(ch, hdr->buf, msg_buf_sz,
  2030. &usr_mod_msg->info[i]);
  2031. if (ret) {
  2032. ret = -EFAULT;
  2033. goto send_message_err;
  2034. }
  2035. }
  2036. }
  2037. }
  2038. /* Send Tx to remote edge */
  2039. do {
  2040. if (ch->rpmsg_abort) {
  2041. spcom_pr_err("ch [%s] aborted\n", ch_name);
  2042. ret = -ECANCELED;
  2043. break;
  2044. }
  2045. /* may fail when Rx intent not queued by remote edge */
  2046. ret = rpmsg_trysend(ch->rpdev->ept, tx_buf, tx_buf_size);
  2047. if (ret == 0) {
  2048. spcom_pr_dbg("ch[%s]: successfully sent txn_id=%d\n", ch_name, ch->txn_id);
  2049. break;
  2050. }
  2051. time_msec += TX_RETRY_DELAY_MSEC;
  2052. /* release channel lock before sleep */
  2053. mutex_unlock(&ch->lock);
  2054. msleep(TX_RETRY_DELAY_MSEC);
  2055. mutex_lock(&ch->lock);
  2056. } while ((ret == -EBUSY || ret == -EAGAIN) && time_msec < timeout_msec);
  2057. if (ret)
  2058. spcom_pr_err("Tx failed: ch [%s], err [%d], timeout [%d]ms\n",
  2059. ch_name, ret, timeout_msec);
  2060. ret = msg_buf_sz;
  2061. send_message_err:
  2062. if (ret < 0 && ch->is_sharable && ch->active_pid == current_pid()) {
  2063. ch->active_pid = 0;
  2064. mutex_unlock(&ch->shared_sync_lock);
  2065. }
  2066. /* close pm awake window after spcom server response */
  2067. if (ch->is_server) {
  2068. __pm_relax(spcom_dev->ws);
  2069. spcom_pr_dbg("ch[%s]:pm_relax() called for server, after tx\n",
  2070. ch->name);
  2071. }
  2072. mutex_unlock(&ch->lock);
  2073. memset(tx_buf, 0, tx_buf_size);
  2074. kfree(tx_buf);
  2075. return ret;
  2076. }
  2077. /**
  2078. * is_control_channel
  2079. *
  2080. * @brief Helper function to check if device file if of a control channel
  2081. *
  2082. * @param[in] file device file
  2083. *
  2084. * @return true if file is control device file, false otherwise.
  2085. */
  2086. static inline bool is_control_channel(struct file *file)
  2087. {
  2088. return (!strcmp(file_to_filename(file), DEVICE_NAME)) ? true : false;
  2089. }
  2090. /**
  2091. * spcom_ioctl_handle_restart_spu_command
  2092. *
  2093. * @brief Handle SPU restart IOCTL command from user space
  2094. *
  2095. * @return zero on success, negative value otherwise.
  2096. */
  2097. static int spcom_ioctl_handle_restart_spu_command(void)
  2098. {
  2099. int ret = 0;
  2100. spcom_pr_dbg("SPSS restart command\n");
  2101. spcom_dev->spss_rproc = rproc_get_by_phandle(be32_to_cpup(spcom_dev->rproc_prop->value));
  2102. if (!spcom_dev->spss_rproc) {
  2103. pr_err("rproc device not found\n");
  2104. return -ENODEV; /* no spss peripheral exist */
  2105. }
  2106. ret = rproc_boot(spcom_dev->spss_rproc);
  2107. if (ret == -ETIMEDOUT) {
  2108. /* userspace should handle retry if needed */
  2109. spcom_pr_err("FW loading process timeout\n");
  2110. } else if (ret) {
  2111. /*
  2112. * SPU shutdown. Return value comes from SPU PBL message.
  2113. * The error is not recoverable and userspace handles it
  2114. * by request and analyse rmb_error value
  2115. */
  2116. spcom_dev->rmb_error = (uint32_t)ret;
  2117. spcom_pr_err("spss crashed during device bootup rmb_error[0x%x]\n",
  2118. spcom_dev->rmb_error);
  2119. ret = -ENODEV;
  2120. } else {
  2121. spcom_pr_info("FW loading process is complete\n");
  2122. }
  2123. return ret;
  2124. }
  2125. /**
  2126. * spcom_create_channel
  2127. *
  2128. * @brief Helper function to create spcom channel
  2129. *
  2130. * @param[in] ch_name spcom channel name
  2131. * @param[in] is_sharable true if sharable channel, false otherwise
  2132. *
  2133. * @return zero on success, negative value otherwise.
  2134. */
  2135. static int spcom_create_channel(const char *ch_name, bool is_sharable)
  2136. {
  2137. struct spcom_channel *ch = NULL;
  2138. struct spcom_channel *free_ch = NULL;
  2139. int ret = 0;
  2140. int i = 0;
  2141. /* check if spcom remove was called */
  2142. if (atomic_read(&spcom_dev->remove_in_progress)) {
  2143. spcom_pr_err("module remove in progress\n");
  2144. ret = -ENODEV;
  2145. }
  2146. if (!is_valid_ch_name(ch_name)) {
  2147. spcom_pr_err("invalid channel name\n");
  2148. return -EINVAL;
  2149. }
  2150. if (is_control_channel_name(ch_name)) {
  2151. spcom_pr_err("cannot create control channel\n");
  2152. return -EINVAL;
  2153. }
  2154. spcom_pr_dbg("create spcom channel, name[%s], is sharable[%d]\n", ch_name, is_sharable);
  2155. for (i = 0; i < SPCOM_MAX_CHANNELS; ++i) {
  2156. /* Check if channel already exist */
  2157. ch = &spcom_dev->channels[i];
  2158. if (!strcmp(ch->name, ch_name))
  2159. break;
  2160. /* Keep address of first free channel */
  2161. if (!free_ch && ch->name[0] == 0)
  2162. free_ch = ch;
  2163. }
  2164. /* Channel doesn't exist */
  2165. if (i == SPCOM_MAX_CHANNELS) {
  2166. /* No free slot to create a new channel */
  2167. if (!free_ch) {
  2168. spcom_pr_err("no free channel\n");
  2169. return -ENODEV;
  2170. }
  2171. /* Create a new channel */
  2172. ret = spcom_init_channel(free_ch, is_sharable, ch_name);
  2173. if (ret)
  2174. ret = -ENODEV;
  2175. } else if (is_sharable) {
  2176. /* Channel is already created as sharable */
  2177. if (spcom_dev->channels[i].is_sharable) {
  2178. spcom_pr_err("already created channel as sharable\n");
  2179. return 0;
  2180. }
  2181. /* Cannot create sharable channel if channel already created */
  2182. spcom_pr_err("channel already exist, cannot create sharable channel\n");
  2183. ret = -EINVAL;
  2184. }
  2185. if (ret)
  2186. spcom_pr_err("create channel [%s] failed, ret[%d]\n", ch_name, ret);
  2187. else
  2188. spcom_pr_dbg("create channel [%s] is done\n", ch_name);
  2189. return ret;
  2190. }
  2191. /**
  2192. * spcom_ioctl_handle_create_shared_ch_command
  2193. *
  2194. * @brief Handle SPCOM create shared channel IOCTL command from user space
  2195. *
  2196. * @param[in] arg SPCOM create shared channel IOCTL command arguments
  2197. *
  2198. * @return zero on success, negative value otherwise.
  2199. */
  2200. static inline int spcom_ioctl_handle_create_shared_ch_command(struct spcom_ioctl_ch *arg)
  2201. {
  2202. int ret = 0;
  2203. /* Lock before modifying spcom device global channel list */
  2204. mutex_lock(&spcom_dev->ch_list_lock);
  2205. ret = spcom_create_channel(arg->ch_name, true /*sharable*/);
  2206. /* Unlock spcom device global channel list */
  2207. mutex_unlock(&spcom_dev->ch_list_lock);
  2208. return ret;
  2209. }
  2210. /**
  2211. * spcom_handle_channel_register_command
  2212. *
  2213. * @brief Handle register to SPCOM channel IOCTL command from user space
  2214. *
  2215. * Handle both create SPCOM channel (if needed) and register SPCOM channel to avoid
  2216. * race condition between two processes trying to register to the same channel. The
  2217. * channel list is protected by a single lock during the create and register flow.
  2218. *
  2219. * @param[in] arg IOCTL command arguments
  2220. *
  2221. * @return zero on success, negative value otherwise.
  2222. */
  2223. static int spcom_ioctl_handle_channel_register_command(struct spcom_ioctl_ch *arg)
  2224. {
  2225. struct spcom_channel *ch = NULL;
  2226. const char *ch_name = arg->ch_name;
  2227. int ret = 0;
  2228. if (!current_pid()) {
  2229. spcom_pr_err("unknown PID\n");
  2230. return -EINVAL;
  2231. }
  2232. /* Lock before modifying spcom device global channel list */
  2233. mutex_lock(&spcom_dev->ch_list_lock);
  2234. ret = spcom_create_channel(ch_name, false /*non-sharable*/);
  2235. if (ret) {
  2236. mutex_unlock(&spcom_dev->ch_list_lock);
  2237. return ret;
  2238. }
  2239. ch = spcom_find_channel_by_name(ch_name);
  2240. if (!ch) {
  2241. mutex_unlock(&spcom_dev->ch_list_lock);
  2242. return -ENODEV;
  2243. }
  2244. /* If channel open is called for the first time need to register rpmsg_drv
  2245. * Please note: spcom_register_rpmsg_drv acquire the channel lock
  2246. */
  2247. if (!spcom_is_channel_open(ch)) {
  2248. reinit_completion(&ch->connect);
  2249. ret = spcom_register_rpmsg_drv(ch);
  2250. if (ret < 0) {
  2251. mutex_unlock(&spcom_dev->ch_list_lock);
  2252. spcom_pr_err("register rpmsg driver failed %d\n", ret);
  2253. return ret;
  2254. }
  2255. }
  2256. ret = spcom_register_channel(ch);
  2257. /* Unlock spcom device global channel list */
  2258. mutex_unlock(&spcom_dev->ch_list_lock);
  2259. return ret;
  2260. }
  2261. /**
  2262. * spcom_ioctl_handle_channel_unregister_commnad
  2263. *
  2264. * @brief Handle SPCOM channel unregister IOCTL command from user space
  2265. *
  2266. * @param[in] arg IOCTL command arguments
  2267. *
  2268. * @return zero on successful operation, negative value otherwise.
  2269. */
  2270. static int spcom_ioctl_handle_channel_unregister_command(struct spcom_ioctl_ch *arg)
  2271. {
  2272. struct spcom_channel *ch = NULL;
  2273. const char *ch_name = NULL;
  2274. int ret = 0;
  2275. if (!current_pid()) {
  2276. spcom_pr_err("unknown PID\n");
  2277. return -EINVAL;
  2278. }
  2279. spcom_pr_dbg("unregister channel cmd arg: ch_name[%s]\n", arg->ch_name);
  2280. ch_name = arg->ch_name;
  2281. if (!is_valid_ch_name(ch_name)) {
  2282. spcom_pr_err("invalid channel name\n");
  2283. return -EINVAL;
  2284. }
  2285. if (is_control_channel_name(ch_name)) {
  2286. spcom_pr_err("cannot unregister control channel\n");
  2287. return -EINVAL;
  2288. }
  2289. /* Lock before modifying spcom device global channel list */
  2290. mutex_lock(&spcom_dev->ch_list_lock);
  2291. ch = spcom_find_channel_by_name(ch_name);
  2292. if (!ch) {
  2293. spcom_pr_err("could not find channel[%s]\n", ch_name);
  2294. mutex_unlock(&spcom_dev->ch_list_lock);
  2295. return -ENODEV;
  2296. }
  2297. /* Reset channel context */
  2298. ret = spcom_channel_deinit(ch);
  2299. /* Unlock spcom device global channel list */
  2300. mutex_unlock(&spcom_dev->ch_list_lock);
  2301. spcom_pr_dbg("spcom unregister ch[%s] is done, ret[%d]\n", ch_name, ret);
  2302. return ret;
  2303. }
  2304. /**
  2305. * spcom_ioctl_handle_is_channel_connected
  2306. *
  2307. * @brief Handle check if SPCOM channel is connected IOCTL command from user space
  2308. *
  2309. * @arg[in] IOCTL command arguments
  2310. *
  2311. * @return zero if not connected, positive value if connected, negative value otherwise.
  2312. */
  2313. static int spcom_ioctl_handle_is_channel_connected(struct spcom_ioctl_ch *arg)
  2314. {
  2315. const char *ch_name = arg->ch_name;
  2316. struct spcom_channel *ch = NULL;
  2317. int ret = 0;
  2318. spcom_pr_dbg("Is channel connected cmd arg: ch_name[%s]\n", arg->ch_name);
  2319. if (!is_valid_ch_name(ch_name)) {
  2320. spcom_pr_err("invalid channel name\n");
  2321. return -EINVAL;
  2322. }
  2323. if (is_control_channel_name(ch_name)) {
  2324. spcom_pr_err("invalid control device: %s\n", ch_name);
  2325. return -EINVAL;
  2326. }
  2327. ch = spcom_find_channel_by_name(ch_name);
  2328. if (!ch) {
  2329. spcom_pr_err("could not find channel[%s]\n", ch_name);
  2330. return -EINVAL;
  2331. }
  2332. mutex_lock(&ch->lock);
  2333. /* rpdev is set during spcom_rpdev_probe when remote app is loaded */
  2334. ret = (ch->rpdev != NULL) ? 1 : 0;
  2335. mutex_unlock(&ch->lock);
  2336. return ret;
  2337. }
  2338. /**
  2339. * spcom_ioctl_handle_lock_dmabuf_commnad
  2340. *
  2341. * @brief Handle DMA buffer lock IOCTL command from user space
  2342. *
  2343. * @param[in] arg IOCTL command arguments
  2344. *
  2345. * @return zero on successful operation, negative value otherwise.
  2346. */
  2347. static int spcom_ioctl_handle_lock_dmabuf_command(struct spcom_ioctl_dmabuf_lock *arg)
  2348. {
  2349. struct spcom_channel *ch = NULL;
  2350. struct dma_buf *dma_buf = NULL;
  2351. const char *ch_name = NULL;
  2352. uint32_t pid = current_pid();
  2353. int fd = 0;
  2354. int i = 0;
  2355. spcom_pr_dbg("Lock dmabuf cmd arg: ch_name[%s], fd[%d], padding[%u], PID[%ld]\n",
  2356. arg->ch_name, arg->fd, arg->padding, current_pid());
  2357. ch_name = arg->ch_name;
  2358. if (!is_valid_ch_name(ch_name)) {
  2359. spcom_pr_err("invalid channel name\n");
  2360. return -EINVAL;
  2361. }
  2362. fd = arg->fd;
  2363. if (!pid) {
  2364. spcom_pr_err("unknown PID\n");
  2365. return -EINVAL;
  2366. }
  2367. if (fd > (unsigned int)INT_MAX) {
  2368. spcom_pr_err("int overflow [%u]\n", fd);
  2369. return -EINVAL;
  2370. }
  2371. ch = spcom_find_channel_by_name(ch_name);
  2372. if (!ch) {
  2373. spcom_pr_err("could not find channel[%s]\n", ch_name);
  2374. return -ENODEV;
  2375. }
  2376. dma_buf = dma_buf_get(fd);
  2377. if (IS_ERR_OR_NULL(dma_buf)) {
  2378. spcom_pr_err("fail to get dma buf handle\n");
  2379. return -EINVAL;
  2380. }
  2381. /* DMA buffer lock doesn't involve any Rx/Tx data to remote edge */
  2382. mutex_lock(&ch->lock);
  2383. /* Check if channel is open */
  2384. if (!spcom_is_channel_open(ch)) {
  2385. spcom_pr_err("Channel [%s] is closed\n", ch_name);
  2386. mutex_unlock(&ch->lock);
  2387. dma_buf_put(dma_buf);
  2388. return -EINVAL;
  2389. }
  2390. /* Check if this shared buffer is already locked */
  2391. for (i = 0 ; i < ARRAY_SIZE(ch->dmabuf_array); i++) {
  2392. if (ch->dmabuf_array[i].handle == dma_buf) {
  2393. spcom_pr_dbg("fd [%d] shared buf is already locked\n", fd);
  2394. mutex_unlock(&ch->lock);
  2395. dma_buf_put(dma_buf); /* decrement back the ref count */
  2396. return -EINVAL;
  2397. }
  2398. }
  2399. /* Store the dma_buf handle */
  2400. for (i = 0 ; i < ARRAY_SIZE(ch->dmabuf_array); i++) {
  2401. struct dma_buf_info *curr_buf = &ch->dmabuf_array[i];
  2402. if (curr_buf->handle == NULL) {
  2403. curr_buf->handle = dma_buf;
  2404. curr_buf->fd = fd;
  2405. curr_buf->owner_pid = pid;
  2406. spcom_pr_dbg("ch [%s] locked dma buf #%d fd [%d] dma_buf=0x%pK pid #%d\n",
  2407. ch_name, i, curr_buf->fd, curr_buf->handle, curr_buf->owner_pid);
  2408. mutex_unlock(&ch->lock);
  2409. return 0;
  2410. }
  2411. }
  2412. mutex_unlock(&ch->lock);
  2413. /* decrement back the ref count */
  2414. dma_buf_put(dma_buf);
  2415. spcom_pr_err("No free entry to store dmabuf handle of fd [%d] on ch [%s]\n", fd, ch_name);
  2416. return -EFAULT;
  2417. }
  2418. /**
  2419. * spcom_ioctl_handle_unlock_dmabuf_commnad
  2420. *
  2421. * @brief Handle DMA buffer unlock IOCTL command from user space
  2422. *
  2423. * @param[in] arg IOCTL command arguments
  2424. *
  2425. * @return zero on success, negative value otherwise.
  2426. */
  2427. static int spcom_ioctl_handle_unlock_dmabuf_command(struct spcom_ioctl_dmabuf_lock *arg)
  2428. {
  2429. struct spcom_channel *ch = NULL;
  2430. struct dma_buf *dma_buf = NULL;
  2431. const char *ch_name = NULL;
  2432. struct dma_buf *curr_handle = NULL;
  2433. bool found = false;
  2434. int fd = 0;
  2435. int i = 0;
  2436. int ret = 0;
  2437. bool unlock_all = false;
  2438. spcom_pr_dbg("Unlock dmabuf cmd arg: ch_name[%s], fd[%d], padding[%u], PID[%ld]\n",
  2439. arg->ch_name, arg->fd, arg->padding, current_pid());
  2440. ch_name = arg->ch_name;
  2441. if (!is_valid_ch_name(ch_name))
  2442. return -EINVAL;
  2443. fd = arg->fd;
  2444. if (fd > (unsigned int)INT_MAX) {
  2445. spcom_pr_err("int overflow [%u]\n", fd);
  2446. return -EINVAL;
  2447. }
  2448. if (fd == (int) SPCOM_DMABUF_FD_UNLOCK_ALL) {
  2449. spcom_pr_dbg("unlock all FDs of PID [%d]\n", current_pid());
  2450. unlock_all = true;
  2451. }
  2452. ch = spcom_find_channel_by_name(ch_name);
  2453. if (!ch)
  2454. return -ENODEV;
  2455. dma_buf = dma_buf_get(fd);
  2456. if (IS_ERR_OR_NULL(dma_buf)) {
  2457. spcom_pr_err("Failed to get dma buf handle, fd [%d]\n", fd);
  2458. return -EINVAL;
  2459. }
  2460. dma_buf_put(dma_buf);
  2461. mutex_lock(&ch->lock);
  2462. if (unlock_all) { /* Unlock all buffers of current PID on channel */
  2463. for (i = 0; i < ARRAY_SIZE(ch->dmabuf_array); i++) {
  2464. if (spcom_dmabuf_unlock(&ch->dmabuf_array[i], true) == 0)
  2465. found = true;
  2466. }
  2467. } else { /* Unlock specific buffer if owned by current PID */
  2468. for (i = 0; i < ARRAY_SIZE(ch->dmabuf_array); i++) {
  2469. curr_handle = ch->dmabuf_array[i].handle;
  2470. if (curr_handle && curr_handle == dma_buf) {
  2471. ret = spcom_dmabuf_unlock(&ch->dmabuf_array[i], true);
  2472. found = true;
  2473. break;
  2474. }
  2475. }
  2476. }
  2477. mutex_unlock(&ch->lock);
  2478. if (!found) {
  2479. spcom_pr_err("Buffer fd [%d] was not found for PID [%u] on channel [%s]\n",
  2480. fd, current_pid(), ch_name);
  2481. return -ENODEV;
  2482. }
  2483. return ret;
  2484. }
  2485. /**
  2486. * spcom_ioctl_handle_get_message
  2487. *
  2488. * @brief Handle get message (request or response) IOCTL command from user space
  2489. *
  2490. * @param[in] arg IOCTL command arguments
  2491. * @param[out] user_buffer user space buffer to copy message to
  2492. *
  2493. * @return size in bytes on success, negative value on failure.
  2494. */
  2495. static int spcom_ioctl_handle_get_message(struct spcom_ioctl_message *arg, void *user_buffer)
  2496. {
  2497. struct spcom_channel *ch = NULL;
  2498. struct spcom_msg_hdr *hdr = NULL;
  2499. const char *ch_name = NULL;
  2500. void *rx_buf = NULL;
  2501. int rx_buf_size = 0;
  2502. uint32_t msg_sz = arg->buffer_size;
  2503. uint32_t timeout_msec = 0; /* client only */
  2504. int ret = 0;
  2505. spcom_pr_dbg("Get message cmd arg: ch_name[%s], timeout_msec [%u], buffer size[%u]\n",
  2506. arg->ch_name, arg->timeout_msec, arg->buffer_size);
  2507. ch_name = arg->ch_name;
  2508. if (!is_valid_ch_name(ch_name)) {
  2509. spcom_pr_err("invalid channel name\n");
  2510. return -EINVAL;
  2511. }
  2512. /* DEVICE_NAME name is reserved for control channel */
  2513. if (is_control_channel_name(ch_name)) {
  2514. spcom_pr_err("cannot send message on management channel %s\n", ch_name);
  2515. return -EFAULT;
  2516. }
  2517. ch = spcom_find_channel_by_name(ch_name);
  2518. if (!ch)
  2519. return -ENODEV;
  2520. /* Check if remote side connect */
  2521. if (!spcom_is_channel_connected(ch)) {
  2522. spcom_pr_err("ch [%s] remote side not connect\n", ch_name);
  2523. ret = -ENOTCONN;
  2524. goto get_message_out;
  2525. }
  2526. /* Check param validity */
  2527. if (msg_sz > SPCOM_MAX_RESPONSE_SIZE) {
  2528. spcom_pr_err("ch [%s] invalid size [%d]\n", ch_name, msg_sz);
  2529. ret = -EINVAL;
  2530. goto get_message_out;
  2531. }
  2532. spcom_pr_dbg("waiting for incoming message, ch[%s], size[%u]\n", ch_name, msg_sz);
  2533. /* Allocate Buffers*/
  2534. rx_buf_size = sizeof(*hdr) + msg_sz;
  2535. rx_buf = kzalloc(rx_buf_size, GFP_KERNEL);
  2536. if (!rx_buf) {
  2537. ret = -ENOMEM;
  2538. goto get_message_out;
  2539. }
  2540. /* Client response timeout depends on the request handling time on the remote side
  2541. * Server send response to remote edge and return immediately, timeout isn't needed
  2542. */
  2543. if (!ch->is_server) {
  2544. timeout_msec = ch->response_timeout_msec;
  2545. spcom_pr_dbg("response timeout_msec [%d]\n", (int) timeout_msec);
  2546. }
  2547. ret = spcom_rx(ch, rx_buf, rx_buf_size, timeout_msec);
  2548. if (ret < 0) {
  2549. spcom_pr_err("rx error %d\n", ret);
  2550. goto get_message_out;
  2551. }
  2552. msg_sz = ret; /* actual_rx_size */
  2553. hdr = rx_buf;
  2554. if (ch->is_server) {
  2555. ch->txn_id = hdr->txn_id; /* SPCOM server sets the request tnx_id */
  2556. spcom_pr_dbg("ch[%s]: request txn_id [0x%x]\n", ch_name, ch->txn_id);
  2557. }
  2558. /* Verify incoming message size */
  2559. if (msg_sz <= sizeof(*hdr)) {
  2560. spcom_pr_err("rx size [%d] too small\n", msg_sz);
  2561. ret = -EFAULT;
  2562. goto get_message_out;
  2563. }
  2564. /* Copy message to user */
  2565. msg_sz -= sizeof(*hdr);
  2566. spcom_pr_dbg("copying message to user space, size: [%d]\n", msg_sz);
  2567. ret = copy_to_user(user_buffer, hdr->buf, msg_sz);
  2568. if (ret) {
  2569. spcom_pr_err("failed to copy to user, ret [%d]\n", ret);
  2570. ret = -EFAULT;
  2571. goto get_message_out;
  2572. }
  2573. ret = msg_sz;
  2574. spcom_pr_dbg("get message done, msg size[%d]\n", msg_sz);
  2575. get_message_out:
  2576. if (ch->active_pid == current_pid()) {
  2577. ch->active_pid = 0;
  2578. mutex_unlock(&ch->shared_sync_lock);
  2579. }
  2580. kfree(rx_buf);
  2581. /* close pm awake window for spcom client get response */
  2582. mutex_lock(&ch->lock);
  2583. if (!ch->is_server) {
  2584. __pm_relax(spcom_dev->ws);
  2585. spcom_pr_dbg("ch[%s]:pm_relax() called for server, after tx\n",
  2586. ch->name);
  2587. }
  2588. mutex_unlock(&ch->lock);
  2589. return ret;
  2590. }
  2591. /**
  2592. * spcom_ioctl_handle_poll_event
  2593. *
  2594. * @brief Handle SPCOM event poll ioctl command from user space
  2595. *
  2596. * @param[in] arg IOCTL command arguments
  2597. * @param[out] user_retval user space address of poll return value
  2598. *
  2599. * @return Zero on success, negative value on failure.
  2600. */
  2601. static int spcom_ioctl_handle_poll_event(struct spcom_ioctl_poll_event *arg, int32_t *user_retval)
  2602. {
  2603. int ret = 0;
  2604. uint32_t link_state = 0;
  2605. spcom_pr_dbg("Handle poll event cmd args: event_id[%d], wait[%u], retval[%d], padding[%d]\n",
  2606. arg->event_id, arg->wait, arg->retval, arg->padding);
  2607. switch (arg->event_id) {
  2608. case SPCOM_EVENT_LINK_STATE:
  2609. {
  2610. if (arg->wait) {
  2611. reinit_completion(&spcom_dev->rpmsg_state_change);
  2612. ret = wait_for_completion_interruptible(
  2613. &spcom_dev->rpmsg_state_change);
  2614. if (ret) {/* wait was interrupted */
  2615. spcom_pr_info("Wait for link state change interrupted, ret[%d]\n",
  2616. ret);
  2617. return -EINTR;
  2618. }
  2619. }
  2620. if (atomic_read(&spcom_dev->rpmsg_dev_count) > 0)
  2621. link_state = 1;
  2622. spcom_pr_dbg("SPCOM link state change: Signaled [%d], PID [%d]\n",
  2623. link_state, current_pid());
  2624. ret = put_user(link_state, user_retval);
  2625. if (ret) {
  2626. spcom_pr_err("unable to copy link state to user [%d]\n", ret);
  2627. return -EFAULT;
  2628. }
  2629. return 0;
  2630. }
  2631. default:
  2632. spcom_pr_err("SPCOM handle poll unsupported event id [%u]\n", arg->event_id);
  2633. return -EINVAL;
  2634. }
  2635. return -EBADRQC;
  2636. }
  2637. /**
  2638. * spcom_ioctl_handle_get_next_req_msg_size
  2639. *
  2640. * @brief Handle user space get next request message size IOCTL command from user space
  2641. *
  2642. * @param arg[in] IOCTL command arguments
  2643. * @param user_req_size[out] user space next request size pointer
  2644. *
  2645. * @Return size in bytes on success, negative value on failure.
  2646. */
  2647. static int spcom_ioctl_handle_get_next_req_msg_size(struct spcom_ioctl_next_request_size *arg,
  2648. uint32_t *user_size)
  2649. {
  2650. struct spcom_channel *ch = NULL;
  2651. const char *ch_name = NULL;
  2652. int ret = 0;
  2653. spcom_pr_dbg("Get next request msg size cmd arg: ch_name[%s], size[%u], padding[%d]\n",
  2654. arg->ch_name, arg->size, arg->padding);
  2655. ch_name = arg->ch_name;
  2656. if (!is_valid_ch_name(ch_name))
  2657. return -EINVAL;
  2658. ch = spcom_find_channel_by_name(ch_name);
  2659. if (!ch)
  2660. return -ENODEV;
  2661. ret = spcom_get_next_request_size(ch);
  2662. if (ret < 0)
  2663. return ret;
  2664. spcom_pr_dbg("Channel[%s], next request size[%d]\n", ch_name, ret);
  2665. /* Copy next request size to user space */
  2666. ret = put_user(ret, user_size);
  2667. if (ret) {
  2668. spcom_pr_err("unable to copy to user [%d]\n", ret);
  2669. return -EFAULT;
  2670. }
  2671. return 0;
  2672. }
  2673. /**
  2674. * spcom_ioctl_handle_copy_and_send_message
  2675. *
  2676. * @brief Handle SPCOM send message (request or response) IOCTL command from user space
  2677. *
  2678. * @param[in] arg IOCTL command arguments
  2679. * @param[in] user_msg_buffer user message buffer
  2680. * @param[in] is_modified flag to indicate if this is a modified message or regular message
  2681. *
  2682. * @return: zero on success, negative value otherwise.
  2683. */
  2684. static int spcom_ioctl_handle_copy_and_send_message(void *arg, void *user_msg_buffer,
  2685. bool is_modified)
  2686. {
  2687. struct spcom_ioctl_modified_message *mod_msg = NULL;
  2688. struct spcom_ioctl_message *msg = NULL;
  2689. void *msg_buffer_copy = NULL;
  2690. uint32_t buffer_size = 0;
  2691. int ret = 0;
  2692. if (is_modified) {
  2693. mod_msg = (struct spcom_ioctl_modified_message *)arg;
  2694. buffer_size = mod_msg->buffer_size;
  2695. } else {
  2696. msg = (struct spcom_ioctl_message *)arg;
  2697. buffer_size = msg->buffer_size;
  2698. }
  2699. msg_buffer_copy = kzalloc(buffer_size, GFP_KERNEL);
  2700. if (!msg_buffer_copy)
  2701. return -ENOMEM;
  2702. spcom_pr_dbg("copying message buffer from user space, size[%u]\n", buffer_size);
  2703. ret = copy_from_user(msg_buffer_copy, user_msg_buffer, buffer_size);
  2704. if (ret) {
  2705. spcom_pr_err("failed to copy from user, ret [%d]\n", ret);
  2706. kfree(msg_buffer_copy);
  2707. return -EFAULT;
  2708. }
  2709. /* Send SPCOM message to remote edge */
  2710. ret = spcom_send_message(arg, msg_buffer_copy, is_modified);
  2711. kfree(msg_buffer_copy);
  2712. return ret;
  2713. }
  2714. /**
  2715. * spcom_ioctl_copy_user_arg
  2716. *
  2717. * Helper function to copy user arguments of IOCTL commands
  2718. *
  2719. * @user_arg: user IOCTL command arguments pointer
  2720. * @arg_copy: internal copy of user arguments
  2721. * @size: size of user arguments struct
  2722. *
  2723. * @return: zero on success, negative value otherwise.
  2724. */
  2725. static inline int spcom_ioctl_copy_user_arg(void *user_arg, void *arg_copy, uint32_t size)
  2726. {
  2727. int ret = 0;
  2728. if (!user_arg) {
  2729. spcom_pr_err("user arg is NULL\n");
  2730. return -EINVAL;
  2731. }
  2732. ret = copy_from_user(arg_copy, user_arg, size);
  2733. if (ret) {
  2734. spcom_pr_err("copy from user failed, size [%u], ret[%d]\n", size, ret);
  2735. return -EFAULT;
  2736. }
  2737. return 0;
  2738. }
  2739. bool is_arg_size_expected(unsigned int cmd, uint32_t arg_size)
  2740. {
  2741. uint32_t expected_size = 0;
  2742. switch (cmd) {
  2743. case SPCOM_POLL_STATE:
  2744. expected_size = sizeof(struct spcom_poll_param);
  2745. break;
  2746. case SPCOM_IOCTL_STATE_POLL:
  2747. expected_size = sizeof(struct spcom_ioctl_poll_event);
  2748. break;
  2749. case SPCOM_IOCTL_SEND_MSG:
  2750. case SPCOM_IOCTL_GET_MSG:
  2751. expected_size = sizeof(struct spcom_ioctl_message);
  2752. break;
  2753. case SPCOM_IOCTL_SEND_MOD_MSG:
  2754. expected_size = sizeof(struct spcom_ioctl_modified_message);
  2755. break;
  2756. case SPCOM_IOCTL_GET_NEXT_REQ_SZ:
  2757. expected_size = sizeof(struct spcom_ioctl_next_request_size);
  2758. break;
  2759. case SPCOM_IOCTL_SHARED_CH_CREATE:
  2760. case SPCOM_IOCTL_CH_REGISTER:
  2761. case SPCOM_IOCTL_CH_UNREGISTER:
  2762. case SPCOM_IOCTL_CH_IS_CONNECTED:
  2763. expected_size = sizeof(struct spcom_ioctl_ch);
  2764. break;
  2765. case SPCOM_IOCTL_DMABUF_LOCK:
  2766. case SPCOM_IOCTL_DMABUF_UNLOCK:
  2767. expected_size = sizeof(struct spcom_ioctl_dmabuf_lock);
  2768. break;
  2769. default:
  2770. spcom_pr_err("No userspace data for ioctl cmd[%ld]\n", cmd);
  2771. return false;
  2772. }
  2773. if (arg_size != expected_size) {
  2774. spcom_pr_err("Invalid cmd size: cmd[%ld], arg size[%u], expected[%u]\n",
  2775. cmd, arg_size, expected_size);
  2776. return false;
  2777. }
  2778. return true;
  2779. }
  2780. static long spcom_device_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2781. {
  2782. void __user *user_arg = (void __user *)arg;
  2783. union spcom_ioctl_arg arg_copy = {0};
  2784. uint32_t arg_size = 0;
  2785. int ret = 0;
  2786. spcom_pr_dbg("ioctl cmd [%u], PID [%d]\n", _IOC_NR(cmd), current_pid());
  2787. if (atomic_read(&spcom_dev->remove_in_progress)) {
  2788. spcom_pr_err("module remove in progress\n");
  2789. return -ENODEV;
  2790. }
  2791. if (!is_control_channel(file) && cmd != SPCOM_POLL_STATE) {
  2792. spcom_pr_err("ioctl is supported only for control channel\n");
  2793. return -EINVAL;
  2794. }
  2795. if ((_IOC_DIR(cmd) & _IOC_WRITE)) {
  2796. arg_size = _IOC_SIZE(cmd);
  2797. if (!is_arg_size_expected(cmd, arg_size))
  2798. return -EFAULT;
  2799. ret = spcom_ioctl_copy_user_arg(user_arg, &arg_copy, arg_size);
  2800. if (ret)
  2801. return ret;
  2802. }
  2803. switch (cmd) {
  2804. case SPCOM_POLL_STATE:
  2805. return handle_poll(file, &(arg_copy.poll),
  2806. &((struct spcom_poll_param *)user_arg)->retval);
  2807. case SPCOM_GET_RMB_ERROR:
  2808. return put_user(spcom_dev->rmb_error, (uint32_t *)arg);
  2809. case SPCOM_IOCTL_STATE_POLL:
  2810. return spcom_ioctl_handle_poll_event(
  2811. &(arg_copy.poll_event),
  2812. &((struct spcom_ioctl_poll_event *)user_arg)->retval);
  2813. case SPCOM_IOCTL_SEND_MSG:
  2814. return spcom_ioctl_handle_copy_and_send_message(&(arg_copy.message),
  2815. ((struct spcom_ioctl_message *)user_arg)->buffer, false);
  2816. case SPCOM_IOCTL_SEND_MOD_MSG:
  2817. return spcom_ioctl_handle_copy_and_send_message(&(arg_copy.message),
  2818. ((struct spcom_ioctl_modified_message *)user_arg)->buffer, true);
  2819. case SPCOM_IOCTL_GET_NEXT_REQ_SZ:
  2820. return spcom_ioctl_handle_get_next_req_msg_size(&(arg_copy.next_req_size),
  2821. &((struct spcom_ioctl_next_request_size *)user_arg)->size);
  2822. case SPCOM_IOCTL_GET_MSG:
  2823. return spcom_ioctl_handle_get_message(&(arg_copy.message),
  2824. ((struct spcom_ioctl_message *)user_arg)->buffer);
  2825. case SPCOM_IOCTL_SHARED_CH_CREATE:
  2826. return spcom_ioctl_handle_create_shared_ch_command(&(arg_copy.channel));
  2827. case SPCOM_IOCTL_CH_REGISTER:
  2828. return spcom_ioctl_handle_channel_register_command(&(arg_copy.channel));
  2829. case SPCOM_IOCTL_CH_UNREGISTER:
  2830. return spcom_ioctl_handle_channel_unregister_command(&(arg_copy.channel));
  2831. case SPCOM_IOCTL_CH_IS_CONNECTED:
  2832. return spcom_ioctl_handle_is_channel_connected(&(arg_copy.channel));
  2833. case SPCOM_IOCTL_DMABUF_LOCK:
  2834. return spcom_ioctl_handle_lock_dmabuf_command(&(arg_copy.dmabuf_lock));
  2835. case SPCOM_IOCTL_DMABUF_UNLOCK:
  2836. return spcom_ioctl_handle_unlock_dmabuf_command(&(arg_copy.dmabuf_lock));
  2837. case SPCOM_IOCTL_RESTART_SPU:
  2838. return spcom_ioctl_handle_restart_spu_command();
  2839. case SPCOM_IOCTL_ENABLE_SSR:
  2840. return spcom_handle_enable_ssr_command();
  2841. default:
  2842. spcom_pr_err("ioctl cmd[%d] is not supported\n", cmd);
  2843. }
  2844. return -ENOIOCTLCMD;
  2845. }
  2846. /* file operation supported from user space */
  2847. static const struct file_operations fops = {
  2848. .read = spcom_device_read,
  2849. .write = spcom_device_write,
  2850. .open = spcom_device_open,
  2851. .release = spcom_device_release,
  2852. .unlocked_ioctl = spcom_device_ioctl,
  2853. };
  2854. /**
  2855. * spcom_create_channel_chardev() - Create a channel char-dev node file
  2856. * for user space interface
  2857. */
  2858. static int spcom_create_channel_chardev(const char *name, bool is_sharable)
  2859. {
  2860. int ret;
  2861. struct device *dev;
  2862. struct spcom_channel *ch;
  2863. dev_t devt;
  2864. struct class *cls = spcom_dev->driver_class;
  2865. struct device *parent = spcom_dev->class_dev;
  2866. void *priv;
  2867. struct cdev *cdev;
  2868. if (!name || strnlen(name, SPCOM_CHANNEL_NAME_SIZE) ==
  2869. SPCOM_CHANNEL_NAME_SIZE) {
  2870. spcom_pr_err("invalid channel name\n");
  2871. return -EINVAL;
  2872. }
  2873. spcom_pr_dbg("creating channel [%s]\n", name);
  2874. ch = spcom_find_channel_by_name(name);
  2875. if (ch) {
  2876. spcom_pr_err("channel [%s] already exist\n", name);
  2877. return -EBUSY;
  2878. }
  2879. ch = spcom_find_channel_by_name(""); /* find reserved channel */
  2880. if (!ch) {
  2881. spcom_pr_err("no free channel\n");
  2882. return -ENODEV;
  2883. }
  2884. ret = spcom_init_channel(ch, is_sharable, name);
  2885. if (ret < 0) {
  2886. spcom_pr_err("can't init channel %d\n", ret);
  2887. return ret;
  2888. }
  2889. ret = spcom_register_rpmsg_drv(ch);
  2890. if (ret < 0) {
  2891. spcom_pr_err("register rpmsg driver failed %d\n", ret);
  2892. goto exit_destroy_channel;
  2893. }
  2894. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  2895. if (!cdev) {
  2896. ret = -ENOMEM;
  2897. goto exit_unregister_drv;
  2898. }
  2899. devt = spcom_dev->device_no + spcom_dev->chdev_count;
  2900. priv = ch;
  2901. /*
  2902. * Pass channel name as formatted string to avoid abuse by using a
  2903. * formatted string as channel name
  2904. */
  2905. dev = device_create(cls, parent, devt, priv, "%s", name);
  2906. if (IS_ERR(dev)) {
  2907. spcom_pr_err("device_create failed\n");
  2908. ret = -ENODEV;
  2909. goto exit_free_cdev;
  2910. }
  2911. cdev_init(cdev, &fops);
  2912. cdev->owner = THIS_MODULE;
  2913. ret = cdev_add(cdev, devt, 1);
  2914. if (ret < 0) {
  2915. spcom_pr_err("cdev_add failed %d\n", ret);
  2916. ret = -ENODEV;
  2917. goto exit_destroy_device;
  2918. }
  2919. spcom_dev->chdev_count++;
  2920. mutex_lock(&ch->lock);
  2921. ch->cdev = cdev;
  2922. ch->dev = dev;
  2923. ch->devt = devt;
  2924. mutex_unlock(&ch->lock);
  2925. return 0;
  2926. exit_destroy_device:
  2927. device_destroy(spcom_dev->driver_class, devt);
  2928. exit_free_cdev:
  2929. kfree(cdev);
  2930. exit_unregister_drv:
  2931. ret = spcom_unregister_rpmsg_drv(ch);
  2932. if (ret != 0)
  2933. spcom_pr_err("can't unregister rpmsg drv %d\n", ret);
  2934. exit_destroy_channel:
  2935. /* empty channel leaves free slot for next time*/
  2936. mutex_lock(&ch->lock);
  2937. memset(ch->name, 0, SPCOM_CHANNEL_NAME_SIZE);
  2938. mutex_unlock(&ch->lock);
  2939. return ret;
  2940. }
  2941. // TODO: error handling
  2942. static int spcom_destroy_channel_chardev(const char *name)
  2943. {
  2944. int ret;
  2945. struct spcom_channel *ch;
  2946. spcom_pr_err("destroy channel [%s]\n", name);
  2947. ch = spcom_find_channel_by_name(name);
  2948. if (!ch) {
  2949. spcom_pr_err("channel [%s] not exist\n", name);
  2950. return -EINVAL;
  2951. }
  2952. ret = spcom_unregister_rpmsg_drv(ch);
  2953. if (ret < 0)
  2954. spcom_pr_err("unregister rpmsg driver failed %d\n", ret);
  2955. mutex_lock(&ch->lock);
  2956. device_destroy(spcom_dev->driver_class, ch->devt);
  2957. kfree(ch->cdev);
  2958. mutex_unlock(&ch->lock);
  2959. mutex_lock(&spcom_dev->chdev_count_lock);
  2960. spcom_dev->chdev_count--;
  2961. mutex_unlock(&spcom_dev->chdev_count_lock);
  2962. return 0;
  2963. }
  2964. static int spcom_register_chardev(void)
  2965. {
  2966. int ret;
  2967. unsigned int baseminor = 0;
  2968. unsigned int count = 1;
  2969. ret = alloc_chrdev_region(&spcom_dev->device_no, baseminor, count,
  2970. DEVICE_NAME);
  2971. if (ret < 0) {
  2972. spcom_pr_err("alloc_chrdev_region failed %d\n", ret);
  2973. return ret;
  2974. }
  2975. spcom_dev->driver_class = class_create(THIS_MODULE, DEVICE_NAME);
  2976. if (IS_ERR(spcom_dev->driver_class)) {
  2977. ret = -ENOMEM;
  2978. spcom_pr_err("class_create failed %d\n", ret);
  2979. goto exit_unreg_chrdev_region;
  2980. }
  2981. spcom_dev->class_dev = device_create(spcom_dev->driver_class, NULL,
  2982. spcom_dev->device_no, spcom_dev, DEVICE_NAME);
  2983. if (IS_ERR(spcom_dev->class_dev)) {
  2984. spcom_pr_err("class_device_create failed %d\n", ret);
  2985. ret = -ENOMEM;
  2986. goto exit_destroy_class;
  2987. }
  2988. cdev_init(&spcom_dev->cdev, &fops);
  2989. spcom_dev->cdev.owner = THIS_MODULE;
  2990. ret = cdev_add(&spcom_dev->cdev,
  2991. MKDEV(MAJOR(spcom_dev->device_no), 0),
  2992. SPCOM_MAX_CHANNELS);
  2993. if (ret < 0) {
  2994. spcom_pr_err("cdev_add failed %d\n", ret);
  2995. goto exit_destroy_device;
  2996. }
  2997. spcom_pr_dbg("char device created\n");
  2998. return 0;
  2999. exit_destroy_device:
  3000. device_destroy(spcom_dev->driver_class, spcom_dev->device_no);
  3001. exit_destroy_class:
  3002. class_destroy(spcom_dev->driver_class);
  3003. exit_unreg_chrdev_region:
  3004. unregister_chrdev_region(spcom_dev->device_no, 1);
  3005. return ret;
  3006. }
  3007. static void spcom_unregister_chrdev(void)
  3008. {
  3009. cdev_del(&spcom_dev->cdev);
  3010. device_destroy(spcom_dev->driver_class, spcom_dev->device_no);
  3011. class_destroy(spcom_dev->driver_class);
  3012. mutex_lock(&spcom_dev->chdev_count_lock);
  3013. unregister_chrdev_region(spcom_dev->device_no, spcom_dev->chdev_count);
  3014. mutex_unlock(&spcom_dev->chdev_count_lock);
  3015. spcom_pr_dbg("control spcom device removed\n");
  3016. }
  3017. static int spcom_parse_dt(struct device_node *np)
  3018. {
  3019. int ret;
  3020. const char *propname = "qcom,spcom-ch-names";
  3021. int num_ch;
  3022. int i;
  3023. const char *name;
  3024. /* Get predefined channels info */
  3025. num_ch = of_property_count_strings(np, propname);
  3026. if (num_ch < 0) {
  3027. spcom_pr_err("wrong format of predefined channels definition [%d]\n",
  3028. num_ch);
  3029. return num_ch;
  3030. }
  3031. if (num_ch > ARRAY_SIZE(spcom_dev->predefined_ch_name)) {
  3032. spcom_pr_err("too many predefined channels [%d]\n", num_ch);
  3033. return -EINVAL;
  3034. }
  3035. spcom_pr_dbg("num of predefined channels [%d]\n", num_ch);
  3036. for (i = 0; i < num_ch; i++) {
  3037. ret = of_property_read_string_index(np, propname, i, &name);
  3038. if (ret) {
  3039. spcom_pr_err("failed to read DT ch#%d name\n", i);
  3040. return -EFAULT;
  3041. }
  3042. strscpy(spcom_dev->predefined_ch_name[i],
  3043. name,
  3044. sizeof(spcom_dev->predefined_ch_name[i]));
  3045. spcom_pr_dbg("found ch [%s]\n", name);
  3046. }
  3047. return num_ch;
  3048. }
  3049. /*
  3050. * the function is running on system workqueue context,
  3051. * processes delayed (by rpmsg rx callback) packets:
  3052. * each packet belong to its destination spcom channel ch
  3053. */
  3054. static void spcom_signal_rx_done(struct work_struct *ignored)
  3055. {
  3056. struct spcom_channel *ch;
  3057. struct rx_buff_list *rx_item;
  3058. struct spcom_msg_hdr *hdr;
  3059. unsigned long flags;
  3060. spin_lock_irqsave(&spcom_dev->rx_lock, flags);
  3061. while (!list_empty(&spcom_dev->rx_list_head)) {
  3062. /* detach last entry */
  3063. rx_item = list_last_entry(&spcom_dev->rx_list_head,
  3064. struct rx_buff_list, list);
  3065. list_del(&rx_item->list);
  3066. spin_unlock_irqrestore(&spcom_dev->rx_lock, flags);
  3067. if (!rx_item) {
  3068. spcom_pr_err("empty entry in pending rx list\n");
  3069. spin_lock_irqsave(&spcom_dev->rx_lock, flags);
  3070. continue;
  3071. }
  3072. ch = rx_item->ch;
  3073. hdr = (struct spcom_msg_hdr *)rx_item->rpmsg_rx_buf;
  3074. mutex_lock(&ch->lock);
  3075. if (ch->comm_role_undefined) {
  3076. ch->comm_role_undefined = false;
  3077. ch->is_server = true;
  3078. ch->txn_id = hdr->txn_id;
  3079. spcom_pr_dbg("ch [%s] first packet txn_id=%d, it is server\n",
  3080. ch->name, ch->txn_id);
  3081. }
  3082. if (ch->rpmsg_abort) {
  3083. if (ch->rpmsg_rx_buf) {
  3084. spcom_pr_dbg("ch [%s] rx aborted free %zd bytes\n",
  3085. ch->name, ch->actual_rx_size);
  3086. kfree(ch->rpmsg_rx_buf);
  3087. ch->actual_rx_size = 0;
  3088. }
  3089. goto rx_aborted;
  3090. }
  3091. if (ch->rpmsg_rx_buf) {
  3092. spcom_pr_err("ch [%s] previous buffer not consumed %zd bytes\n",
  3093. ch->name, ch->actual_rx_size);
  3094. kfree(ch->rpmsg_rx_buf);
  3095. ch->rpmsg_rx_buf = NULL;
  3096. ch->actual_rx_size = 0;
  3097. }
  3098. if (!ch->is_server && (hdr->txn_id != ch->txn_id)) {
  3099. spcom_pr_err("ch [%s] client: rx dropped txn_id %d, ch->txn_id %d\n",
  3100. ch->name, hdr->txn_id, ch->txn_id);
  3101. goto rx_aborted;
  3102. }
  3103. spcom_pr_dbg("ch[%s] rx txn_id %d, ch->txn_id %d, size=%d\n",
  3104. ch->name, hdr->txn_id, ch->txn_id,
  3105. rx_item->rx_buf_size);
  3106. ch->rpmsg_rx_buf = rx_item->rpmsg_rx_buf;
  3107. ch->actual_rx_size = rx_item->rx_buf_size;
  3108. ch->rx_buf_txn_id = ch->txn_id;
  3109. complete_all(&ch->rx_done);
  3110. mutex_unlock(&ch->lock);
  3111. kfree(rx_item);
  3112. /* lock for the next list entry */
  3113. spin_lock_irqsave(&spcom_dev->rx_lock, flags);
  3114. }
  3115. spin_unlock_irqrestore(&spcom_dev->rx_lock, flags);
  3116. return;
  3117. rx_aborted:
  3118. mutex_unlock(&ch->lock);
  3119. kfree(rx_item->rpmsg_rx_buf);
  3120. kfree(rx_item);
  3121. }
  3122. static int spcom_rpdev_cb(struct rpmsg_device *rpdev,
  3123. void *data, int len, void *priv, u32 src)
  3124. {
  3125. struct spcom_channel *ch;
  3126. static DECLARE_WORK(rpmsg_rx_consumer, spcom_signal_rx_done);
  3127. struct rx_buff_list *rx_item;
  3128. unsigned long flags;
  3129. if (!rpdev || !data) {
  3130. spcom_pr_err("rpdev or data is NULL\n");
  3131. return -EINVAL;
  3132. }
  3133. ch = dev_get_drvdata(&rpdev->dev);
  3134. if (!ch) {
  3135. spcom_pr_err("%s: invalid ch\n", ch->name);
  3136. return -EINVAL;
  3137. }
  3138. if (len > SPCOM_RX_BUF_SIZE || len <= 0) {
  3139. spcom_pr_err("got msg size %d, max allowed %d\n",
  3140. len, SPCOM_RX_BUF_SIZE);
  3141. return -EINVAL;
  3142. }
  3143. rx_item = kzalloc(sizeof(*rx_item), GFP_ATOMIC);
  3144. if (!rx_item)
  3145. return -ENOMEM;
  3146. rx_item->rpmsg_rx_buf = kmemdup(data, len, GFP_ATOMIC);
  3147. if (!rx_item->rpmsg_rx_buf)
  3148. return -ENOMEM;
  3149. rx_item->rx_buf_size = len;
  3150. rx_item->ch = ch;
  3151. pm_wakeup_ws_event(spcom_dev->ws, SPCOM_PM_PACKET_HANDLE_TIMEOUT, true);
  3152. spcom_pr_dbg("%s:got new packet, wakeup requested\n", ch->name);
  3153. spin_lock_irqsave(&spcom_dev->rx_lock, flags);
  3154. list_add(&rx_item->list, &spcom_dev->rx_list_head);
  3155. spin_unlock_irqrestore(&spcom_dev->rx_lock, flags);
  3156. schedule_work(&rpmsg_rx_consumer);
  3157. return 0;
  3158. }
  3159. static int spcom_rpdev_probe(struct rpmsg_device *rpdev)
  3160. {
  3161. const char *name;
  3162. struct spcom_channel *ch;
  3163. if (!rpdev) {
  3164. spcom_pr_err("rpdev is NULL\n");
  3165. return -EINVAL;
  3166. }
  3167. name = rpdev->id.name;
  3168. /* module exiting */
  3169. if (atomic_read(&spcom_dev->remove_in_progress)) {
  3170. spcom_pr_warn("remove in progress, ignore rpmsg probe for ch %s\n",
  3171. name);
  3172. return 0;
  3173. }
  3174. spcom_pr_dbg("new channel %s rpmsg_device arrived\n", name);
  3175. ch = spcom_find_channel_by_name(name);
  3176. if (!ch) {
  3177. spcom_pr_err("channel %s not found\n", name);
  3178. return -ENODEV;
  3179. }
  3180. mutex_lock(&ch->lock);
  3181. ch->rpdev = rpdev;
  3182. ch->rpmsg_abort = false;
  3183. ch->txn_id = INITIAL_TXN_ID;
  3184. complete_all(&ch->connect);
  3185. mutex_unlock(&ch->lock);
  3186. dev_set_drvdata(&rpdev->dev, ch);
  3187. /* used to evaluate underlying transport link up/down */
  3188. atomic_inc(&spcom_dev->rpmsg_dev_count);
  3189. if (atomic_read(&spcom_dev->rpmsg_dev_count) == 1) {
  3190. spcom_pr_info("Signal link up\n");
  3191. complete_all(&spcom_dev->rpmsg_state_change);
  3192. }
  3193. return 0;
  3194. }
  3195. static void spcom_rpdev_remove(struct rpmsg_device *rpdev)
  3196. {
  3197. struct spcom_channel *ch;
  3198. int i;
  3199. if (!rpdev) {
  3200. spcom_pr_err("rpdev is NULL\n");
  3201. return;
  3202. }
  3203. dev_info(&rpdev->dev, "rpmsg device %s removed\n", rpdev->id.name);
  3204. ch = dev_get_drvdata(&rpdev->dev);
  3205. if (!ch) {
  3206. spcom_pr_err("channel %s not found\n", rpdev->id.name);
  3207. return;
  3208. }
  3209. mutex_lock(&ch->lock);
  3210. /* unlock all ion buffers of sp_kernel channel*/
  3211. if (strcmp(ch->name, "sp_kernel") == 0) {
  3212. for (i = 0; i < ARRAY_SIZE(ch->dmabuf_array); i++)
  3213. if (ch->dmabuf_array[i].handle)
  3214. spcom_dmabuf_unlock(&ch->dmabuf_array[i], false);
  3215. }
  3216. ch->rpdev = NULL;
  3217. ch->rpmsg_abort = true;
  3218. ch->txn_id = 0;
  3219. complete_all(&ch->rx_done);
  3220. mutex_unlock(&ch->lock);
  3221. /* used to evaluate underlying transport link up/down */
  3222. if (atomic_dec_and_test(&spcom_dev->rpmsg_dev_count)) {
  3223. spcom_pr_err("Signal link down\n");
  3224. complete_all(&spcom_dev->rpmsg_state_change);
  3225. }
  3226. }
  3227. /* register rpmsg driver to match with channel ch_name */
  3228. static int spcom_register_rpmsg_drv(struct spcom_channel *ch)
  3229. {
  3230. struct rpmsg_driver *rpdrv;
  3231. struct rpmsg_device_id *match;
  3232. char *drv_name;
  3233. int ret;
  3234. if (ch->rpdrv) {
  3235. spcom_pr_err("ch:%s, rpmsg driver %s already registered\n",
  3236. ch->name, ch->rpdrv->id_table->name);
  3237. return -ENODEV;
  3238. }
  3239. rpdrv = kzalloc(sizeof(*rpdrv), GFP_KERNEL);
  3240. if (!rpdrv)
  3241. return -ENOMEM;
  3242. /* zalloc array of two to NULL terminate the match list */
  3243. match = kzalloc(2 * sizeof(*match), GFP_KERNEL);
  3244. if (!match) {
  3245. kfree(rpdrv);
  3246. return -ENOMEM;
  3247. }
  3248. snprintf(match->name, RPMSG_NAME_SIZE, "%s", ch->name);
  3249. drv_name = kasprintf(GFP_KERNEL, "%s_%s", "spcom_rpmsg_drv", ch->name);
  3250. if (!drv_name) {
  3251. spcom_pr_err("can't allocate drv_name for %s\n", ch->name);
  3252. kfree(rpdrv);
  3253. kfree(match);
  3254. return -ENOMEM;
  3255. }
  3256. rpdrv->probe = spcom_rpdev_probe;
  3257. rpdrv->remove = spcom_rpdev_remove;
  3258. rpdrv->callback = spcom_rpdev_cb;
  3259. rpdrv->id_table = match;
  3260. rpdrv->drv.name = drv_name;
  3261. ret = register_rpmsg_driver(rpdrv);
  3262. if (ret) {
  3263. spcom_pr_err("can't register rpmsg_driver for %s\n", ch->name);
  3264. kfree(rpdrv);
  3265. kfree(match);
  3266. kfree(drv_name);
  3267. return ret;
  3268. }
  3269. mutex_lock(&ch->lock);
  3270. ch->rpdrv = rpdrv;
  3271. ch->rpmsg_abort = false;
  3272. mutex_unlock(&ch->lock);
  3273. return 0;
  3274. }
  3275. static int spcom_unregister_rpmsg_drv(struct spcom_channel *ch)
  3276. {
  3277. if (!ch->rpdrv) {
  3278. spcom_pr_err("rpdev is NULL, can't unregister rpmsg drv\n");
  3279. return -ENODEV;
  3280. }
  3281. unregister_rpmsg_driver(ch->rpdrv);
  3282. mutex_lock(&ch->lock);
  3283. kfree(ch->rpdrv->drv.name);
  3284. kfree((void *)ch->rpdrv->id_table);
  3285. kfree(ch->rpdrv);
  3286. ch->rpdrv = NULL;
  3287. ch->rpmsg_abort = true; /* will unblock spcom_rx() */
  3288. mutex_unlock(&ch->lock);
  3289. return 0;
  3290. }
  3291. static int spcom_probe(struct platform_device *pdev)
  3292. {
  3293. int ret;
  3294. struct spcom_device *dev = NULL;
  3295. struct device_node *np;
  3296. struct property *prop;
  3297. if (!pdev) {
  3298. pr_err("invalid pdev\n");
  3299. return -ENODEV;
  3300. }
  3301. np = pdev->dev.of_node;
  3302. if (!np) {
  3303. pr_err("invalid DT node\n");
  3304. return -EINVAL;
  3305. }
  3306. prop = of_find_property(np, "qcom,rproc-handle", NULL);
  3307. if (!prop) {
  3308. spcom_pr_err("can't find qcom,rproc-hable property");
  3309. return -EINVAL;
  3310. }
  3311. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  3312. if (dev == NULL)
  3313. return -ENOMEM;
  3314. spcom_dev = dev;
  3315. spcom_dev->pdev = pdev;
  3316. spcom_dev->rproc_prop = prop;
  3317. /* start counting exposed channel char devices from 1 */
  3318. spcom_dev->chdev_count = 1;
  3319. mutex_init(&spcom_dev->chdev_count_lock);
  3320. init_completion(&spcom_dev->rpmsg_state_change);
  3321. atomic_set(&spcom_dev->rpmsg_dev_count, 0);
  3322. atomic_set(&spcom_dev->remove_in_progress, 0);
  3323. INIT_LIST_HEAD(&spcom_dev->rx_list_head);
  3324. spin_lock_init(&spcom_dev->rx_lock);
  3325. spcom_dev->nvm_ion_fd = -1;
  3326. spcom_dev->rmb_error = 0;
  3327. mutex_init(&spcom_dev->ch_list_lock);
  3328. // register wakeup source
  3329. spcom_dev->ws =
  3330. wakeup_source_register(&spcom_dev->pdev->dev, "spcom_wakeup");
  3331. if (!spcom_dev->ws) {
  3332. pr_err("failed to register wakeup source\n");
  3333. ret = -ENOMEM;
  3334. goto fail_while_chardev_reg;
  3335. }
  3336. ret = spcom_register_chardev();
  3337. if (ret) {
  3338. pr_err("create character device failed\n");
  3339. goto fail_while_chardev_reg;
  3340. }
  3341. ret = spcom_parse_dt(np);
  3342. if (ret < 0)
  3343. goto fail_reg_chardev;
  3344. if (of_property_read_bool(np, "qcom,boot-enabled"))
  3345. atomic_set(&dev->subsys_req, 1);
  3346. ret = spcom_create_predefined_channels_chardev();
  3347. if (ret < 0) {
  3348. pr_err("create character device failed (%d)\n", ret);
  3349. goto fail_reg_chardev;
  3350. }
  3351. spcom_ipc_log_context = ipc_log_context_create(SPCOM_LOG_PAGE_CNT,
  3352. "spcom", 0);
  3353. if (!spcom_ipc_log_context)
  3354. pr_err("Unable to create IPC log context\n");
  3355. spcom_pr_info("Driver Initialization completed ok\n");
  3356. return 0;
  3357. fail_reg_chardev:
  3358. pr_err("failed to init driver\n");
  3359. spcom_unregister_chrdev();
  3360. fail_while_chardev_reg:
  3361. kfree(dev);
  3362. spcom_dev = NULL;
  3363. return -ENODEV;
  3364. }
  3365. static int spcom_remove(struct platform_device *pdev)
  3366. {
  3367. int ret;
  3368. struct rx_buff_list *rx_item;
  3369. unsigned long flags;
  3370. int i;
  3371. atomic_inc(&spcom_dev->remove_in_progress);
  3372. if (spcom_dev->spss_rproc) {
  3373. spcom_pr_info("shutdown spss\n");
  3374. rproc_shutdown(spcom_dev->spss_rproc);
  3375. spcom_dev->spss_rproc = NULL;
  3376. }
  3377. /* destroy existing channel char devices */
  3378. for (i = 0; i < SPCOM_MAX_CHANNELS; i++) {
  3379. const char *name = spcom_dev->channels[i].name;
  3380. if (name[0] == 0)
  3381. break;
  3382. ret = spcom_destroy_channel_chardev(name);
  3383. if (ret) {
  3384. spcom_pr_err("failed to destroy chardev [%s], ret [%d]\n",
  3385. name, ret);
  3386. return -EFAULT;
  3387. }
  3388. spcom_pr_dbg("destroyed channel %s", name);
  3389. }
  3390. /* destroy control char device */
  3391. spcom_unregister_chrdev();
  3392. /* release uncompleted rx */
  3393. spin_lock_irqsave(&spcom_dev->rx_lock, flags);
  3394. while (!list_empty(&spcom_dev->rx_list_head)) {
  3395. /* detach last entry */
  3396. rx_item = list_last_entry(&spcom_dev->rx_list_head,
  3397. struct rx_buff_list, list);
  3398. list_del(&rx_item->list);
  3399. if (!rx_item) {
  3400. spcom_pr_err("empty entry in pending rx list\n");
  3401. spin_lock_irqsave(&spcom_dev->rx_lock, flags);
  3402. continue;
  3403. }
  3404. kfree(rx_item);
  3405. }
  3406. spin_unlock_irqrestore(&spcom_dev->rx_lock, flags);
  3407. wakeup_source_unregister(spcom_dev->ws);
  3408. if (spcom_ipc_log_context)
  3409. ipc_log_context_destroy(spcom_ipc_log_context);
  3410. /* free global device struct */
  3411. kfree(spcom_dev);
  3412. spcom_dev = NULL;
  3413. pr_info("successfully released all module resources\n");
  3414. return 0;
  3415. }
  3416. static void spcom_release_all_channels_of_process(u32 pid)
  3417. {
  3418. u32 i;
  3419. /* Iterate over all channels and release all channels that belong to
  3420. * the given process
  3421. */
  3422. for (i = 0; i < SPCOM_MAX_CHANNELS; i++) {
  3423. struct spcom_channel *ch = &spcom_dev->channels[i];
  3424. if (ch->name[0] != '\0') {
  3425. u32 j;
  3426. /* Check if the given process is a client of the current channel, and
  3427. * if so release the channel
  3428. */
  3429. for (j = 0; j < SPCOM_MAX_CHANNEL_CLIENTS; j++) {
  3430. if (ch->pid[j] == pid) {
  3431. mutex_lock(&ch->lock);
  3432. spcom_channel_deinit_locked(ch, pid);
  3433. mutex_unlock(&ch->lock);
  3434. break;
  3435. }
  3436. }
  3437. }
  3438. }
  3439. }
  3440. static int spom_control_channel_add_client(u32 pid)
  3441. {
  3442. u32 i;
  3443. int free_index;
  3444. struct spcom_control_channel_info *ch_info;
  3445. mutex_lock(&spcom_dev->ch_list_lock);
  3446. for (i = 0, free_index = -1; i < SPCOM_MAX_CONTROL_CHANNELS; i++) {
  3447. ch_info = &spcom_dev->control_channels[i];
  3448. /* A process may open only a single control channel */
  3449. if (ch_info->pid == pid) {
  3450. ch_info->ref_cnt++;
  3451. spcom_pr_dbg("Control channel for pid %u already exists, ref_cnt=%u\n",
  3452. pid, ch_info->ref_cnt);
  3453. mutex_unlock(&spcom_dev->ch_list_lock);
  3454. return 0;
  3455. }
  3456. /* Remember the first free entry */
  3457. if (free_index < 0 && ch_info->pid == 0)
  3458. free_index = i;
  3459. }
  3460. /* If no free entry was found then the control channel can't be opened */
  3461. if (free_index < 0) {
  3462. mutex_unlock(&spcom_dev->ch_list_lock);
  3463. spcom_pr_err("Too many open control channels\n");
  3464. return -EMFILE;
  3465. }
  3466. /* Add the process opening the control channel in the free entry */
  3467. ch_info = &spcom_dev->control_channels[free_index];
  3468. ch_info->pid = pid;
  3469. ch_info->ref_cnt = 1;
  3470. spcom_pr_dbg("Add pid %u at index %u\n", pid, free_index);
  3471. mutex_unlock(&spcom_dev->ch_list_lock);
  3472. return 0;
  3473. }
  3474. static int spom_control_channel_remove_client(u32 pid)
  3475. {
  3476. u32 i;
  3477. int ret = -ESRCH;
  3478. mutex_lock(&spcom_dev->ch_list_lock);
  3479. for (i = 0; i < SPCOM_MAX_CONTROL_CHANNELS; i++) {
  3480. struct spcom_control_channel_info *ch_info = &spcom_dev->control_channels[i];
  3481. /* When a process closes the control channel we release all its channels
  3482. * to allow re-registration if another instance of the process will be created
  3483. */
  3484. if (ch_info->pid == pid) {
  3485. ch_info->ref_cnt--;
  3486. spcom_pr_dbg("Remove pid %u from index %u, ref_cnt=%u\n",
  3487. pid, i, ch_info->ref_cnt);
  3488. if (ch_info->ref_cnt == 0) {
  3489. ch_info->pid = 0;
  3490. spcom_release_all_channels_of_process(pid);
  3491. }
  3492. ret = 0;
  3493. break;
  3494. }
  3495. }
  3496. mutex_unlock(&spcom_dev->ch_list_lock);
  3497. return ret;
  3498. }
  3499. static const struct of_device_id spcom_match_table[] = {
  3500. { .compatible = "qcom,spcom", },
  3501. { },
  3502. };
  3503. MODULE_DEVICE_TABLE(of, spcom_match_table);
  3504. static struct platform_driver spcom_driver = {
  3505. .probe = spcom_probe,
  3506. .remove = spcom_remove,
  3507. .driver = {
  3508. .name = DEVICE_NAME,
  3509. .of_match_table = of_match_ptr(spcom_match_table),
  3510. },
  3511. };
  3512. module_platform_driver(spcom_driver);
  3513. MODULE_SOFTDEP("pre: spss_utils");
  3514. MODULE_IMPORT_NS(DMA_BUF);
  3515. MODULE_LICENSE("GPL v2");
  3516. MODULE_DESCRIPTION("Secure Processor Communication");