spcom.c 103 KB

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