spcom.c 103 KB

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