trusty-ipc.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  1. /*
  2. * Copyright (C) 2015 Google, Inc.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/cdev.h>
  17. #include <linux/slab.h>
  18. #include <linux/fs.h>
  19. #include <linux/poll.h>
  20. #include <linux/idr.h>
  21. #include <linux/completion.h>
  22. #include <linux/sched.h>
  23. #include <linux/compat.h>
  24. #include <linux/virtio.h>
  25. #include <linux/virtio_ids.h>
  26. #include <linux/virtio_config.h>
  27. #include <linux/trusty/smcall.h>
  28. #include <linux/trusty/trusty.h>
  29. #include <linux/trusty/trusty_ipc.h>
  30. #define MAX_DEVICES 4
  31. #define REPLY_TIMEOUT 5000
  32. #define TXBUF_TIMEOUT 15000
  33. #define MAX_SRV_NAME_LEN 256
  34. #define MAX_DEV_NAME_LEN 32
  35. #define DEFAULT_MSG_BUF_SIZE PAGE_SIZE
  36. #define DEFAULT_MSG_BUF_ALIGN PAGE_SIZE
  37. #define TIPC_CTRL_ADDR 53
  38. #define TIPC_ANY_ADDR 0xFFFFFFFF
  39. #define TIPC_MIN_LOCAL_ADDR 1024
  40. #define TIPC_IOC_MAGIC 'r'
  41. #define TIPC_IOC_CONNECT _IOW(TIPC_IOC_MAGIC, 0x80, char *)
  42. #if defined(CONFIG_COMPAT)
  43. #define TIPC_IOC_CONNECT_COMPAT _IOW(TIPC_IOC_MAGIC, 0x80, \
  44. compat_uptr_t)
  45. #endif
  46. struct tipc_virtio_dev;
  47. struct tipc_dev_config {
  48. u32 msg_buf_max_size;
  49. u32 msg_buf_alignment;
  50. char dev_name[MAX_DEV_NAME_LEN];
  51. } __packed;
  52. struct tipc_msg_hdr {
  53. u32 src;
  54. u32 dst;
  55. u32 reserved;
  56. u16 len;
  57. u16 flags;
  58. u8 data[0];
  59. } __packed;
  60. enum tipc_ctrl_msg_types {
  61. TIPC_CTRL_MSGTYPE_GO_ONLINE = 1,
  62. TIPC_CTRL_MSGTYPE_GO_OFFLINE,
  63. TIPC_CTRL_MSGTYPE_CONN_REQ,
  64. TIPC_CTRL_MSGTYPE_CONN_RSP,
  65. TIPC_CTRL_MSGTYPE_DISC_REQ,
  66. };
  67. struct tipc_ctrl_msg {
  68. u32 type;
  69. u32 body_len;
  70. u8 body[0];
  71. } __packed;
  72. struct tipc_conn_req_body {
  73. char name[MAX_SRV_NAME_LEN];
  74. } __packed;
  75. struct tipc_conn_rsp_body {
  76. u32 target;
  77. u32 status;
  78. u32 remote;
  79. u32 max_msg_size;
  80. u32 max_msg_cnt;
  81. } __packed;
  82. struct tipc_disc_req_body {
  83. u32 target;
  84. } __packed;
  85. struct tipc_cdev_node {
  86. struct cdev cdev;
  87. struct device *dev;
  88. unsigned int minor;
  89. };
  90. enum tipc_device_state {
  91. VDS_OFFLINE = 0,
  92. VDS_ONLINE,
  93. VDS_DEAD,
  94. };
  95. struct tipc_virtio_dev {
  96. struct kref refcount;
  97. struct mutex lock; /* protects access to this device */
  98. struct virtio_device *vdev;
  99. struct virtqueue *rxvq;
  100. struct virtqueue *txvq;
  101. uint msg_buf_cnt;
  102. uint msg_buf_max_cnt;
  103. size_t msg_buf_max_sz;
  104. uint free_msg_buf_cnt;
  105. struct list_head free_buf_list;
  106. wait_queue_head_t sendq;
  107. struct idr addr_idr;
  108. enum tipc_device_state state;
  109. struct tipc_cdev_node cdev_node;
  110. char cdev_name[MAX_DEV_NAME_LEN];
  111. };
  112. enum tipc_chan_state {
  113. TIPC_DISCONNECTED = 0,
  114. TIPC_CONNECTING,
  115. TIPC_CONNECTED,
  116. TIPC_STALE,
  117. };
  118. struct tipc_chan {
  119. struct mutex lock; /* protects channel state */
  120. struct kref refcount;
  121. enum tipc_chan_state state;
  122. struct tipc_virtio_dev *vds;
  123. const struct tipc_chan_ops *ops;
  124. void *ops_arg;
  125. u32 remote;
  126. u32 local;
  127. u32 max_msg_size;
  128. u32 max_msg_cnt;
  129. char srv_name[MAX_SRV_NAME_LEN];
  130. };
  131. static struct class *tipc_class;
  132. static unsigned int tipc_major;
  133. static DEFINE_IDR(tipc_devices);
  134. static DEFINE_MUTEX(tipc_devices_lock);
  135. static int _match_any(int id, void *p, void *data)
  136. {
  137. return id;
  138. }
  139. static int _match_data(int id, void *p, void *data)
  140. {
  141. return (p == data);
  142. }
  143. static void *_alloc_shareable_mem(size_t sz, phys_addr_t *ppa, gfp_t gfp)
  144. {
  145. return alloc_pages_exact(sz, gfp);
  146. }
  147. static void _free_shareable_mem(size_t sz, void *va, phys_addr_t pa)
  148. {
  149. free_pages_exact(va, sz);
  150. }
  151. static struct tipc_msg_buf *_alloc_msg_buf(size_t sz)
  152. {
  153. struct tipc_msg_buf *mb;
  154. /* allocate tracking structure */
  155. mb = kzalloc(sizeof(struct tipc_msg_buf), GFP_KERNEL);
  156. if (!mb)
  157. return NULL;
  158. /* allocate buffer that can be shared with secure world */
  159. mb->buf_va = _alloc_shareable_mem(sz, &mb->buf_pa, GFP_KERNEL);
  160. if (!mb->buf_va)
  161. goto err_alloc;
  162. mb->buf_sz = sz;
  163. return mb;
  164. err_alloc:
  165. kfree(mb);
  166. return NULL;
  167. }
  168. static void _free_msg_buf(struct tipc_msg_buf *mb)
  169. {
  170. _free_shareable_mem(mb->buf_sz, mb->buf_va, mb->buf_pa);
  171. kfree(mb);
  172. }
  173. static void _free_msg_buf_list(struct list_head *list)
  174. {
  175. struct tipc_msg_buf *mb = NULL;
  176. mb = list_first_entry_or_null(list, struct tipc_msg_buf, node);
  177. while (mb) {
  178. list_del(&mb->node);
  179. _free_msg_buf(mb);
  180. mb = list_first_entry_or_null(list, struct tipc_msg_buf, node);
  181. }
  182. }
  183. static inline void mb_reset(struct tipc_msg_buf *mb)
  184. {
  185. mb->wpos = 0;
  186. mb->rpos = 0;
  187. }
  188. static void _free_chan(struct kref *kref)
  189. {
  190. struct tipc_chan *ch = container_of(kref, struct tipc_chan, refcount);
  191. kfree(ch);
  192. }
  193. static void _free_vds(struct kref *kref)
  194. {
  195. struct tipc_virtio_dev *vds =
  196. container_of(kref, struct tipc_virtio_dev, refcount);
  197. kfree(vds);
  198. }
  199. static struct tipc_msg_buf *vds_alloc_msg_buf(struct tipc_virtio_dev *vds)
  200. {
  201. return _alloc_msg_buf(vds->msg_buf_max_sz);
  202. }
  203. static void vds_free_msg_buf(struct tipc_virtio_dev *vds,
  204. struct tipc_msg_buf *mb)
  205. {
  206. _free_msg_buf(mb);
  207. }
  208. static bool _put_txbuf_locked(struct tipc_virtio_dev *vds,
  209. struct tipc_msg_buf *mb)
  210. {
  211. list_add_tail(&mb->node, &vds->free_buf_list);
  212. return vds->free_msg_buf_cnt++ == 0;
  213. }
  214. static struct tipc_msg_buf *_get_txbuf_locked(struct tipc_virtio_dev *vds)
  215. {
  216. struct tipc_msg_buf *mb;
  217. if (vds->state != VDS_ONLINE)
  218. return ERR_PTR(-ENODEV);
  219. if (vds->free_msg_buf_cnt) {
  220. /* take it out of free list */
  221. mb = list_first_entry(&vds->free_buf_list,
  222. struct tipc_msg_buf, node);
  223. list_del(&mb->node);
  224. vds->free_msg_buf_cnt--;
  225. } else {
  226. if (vds->msg_buf_cnt >= vds->msg_buf_max_cnt)
  227. return ERR_PTR(-EAGAIN);
  228. /* try to allocate it */
  229. mb = _alloc_msg_buf(vds->msg_buf_max_sz);
  230. if (!mb)
  231. return ERR_PTR(-ENOMEM);
  232. vds->msg_buf_cnt++;
  233. }
  234. return mb;
  235. }
  236. static struct tipc_msg_buf *_vds_get_txbuf(struct tipc_virtio_dev *vds)
  237. {
  238. struct tipc_msg_buf *mb;
  239. mutex_lock(&vds->lock);
  240. mb = _get_txbuf_locked(vds);
  241. mutex_unlock(&vds->lock);
  242. return mb;
  243. }
  244. static void vds_put_txbuf(struct tipc_virtio_dev *vds, struct tipc_msg_buf *mb)
  245. {
  246. if (!vds)
  247. return;
  248. mutex_lock(&vds->lock);
  249. _put_txbuf_locked(vds, mb);
  250. wake_up_interruptible(&vds->sendq);
  251. mutex_unlock(&vds->lock);
  252. }
  253. static struct tipc_msg_buf *vds_get_txbuf(struct tipc_virtio_dev *vds,
  254. long timeout)
  255. {
  256. struct tipc_msg_buf *mb;
  257. if (!vds)
  258. return ERR_PTR(-EINVAL);
  259. mb = _vds_get_txbuf(vds);
  260. if ((PTR_ERR(mb) == -EAGAIN) && timeout) {
  261. int rc = wait_event_interruptible_timeout(vds->sendq,
  262. PTR_ERR(mb = _vds_get_txbuf(vds)) != -EAGAIN,
  263. msecs_to_jiffies(timeout));
  264. if (rc < 0)
  265. return ERR_PTR(rc);
  266. if (rc == 0)
  267. return ERR_PTR(-ETIMEDOUT);
  268. }
  269. if (IS_ERR(mb))
  270. return mb;
  271. BUG_ON(!mb);
  272. /* reset and reserve space for message header */
  273. mb_reset(mb);
  274. mb_put_data(mb, sizeof(struct tipc_msg_hdr));
  275. return mb;
  276. }
  277. static int vds_queue_txbuf(struct tipc_virtio_dev *vds,
  278. struct tipc_msg_buf *mb)
  279. {
  280. int err;
  281. struct scatterlist sg;
  282. bool need_notify = false;
  283. if (!vds)
  284. return -EINVAL;
  285. mutex_lock(&vds->lock);
  286. if (vds->state == VDS_ONLINE) {
  287. sg_init_one(&sg, mb->buf_va, mb->wpos);
  288. err = virtqueue_add_outbuf(vds->txvq, &sg, 1, mb, GFP_KERNEL);
  289. need_notify = virtqueue_kick_prepare(vds->txvq);
  290. } else {
  291. err = -ENODEV;
  292. }
  293. mutex_unlock(&vds->lock);
  294. if (need_notify)
  295. virtqueue_notify(vds->txvq);
  296. return err;
  297. }
  298. static int vds_add_channel(struct tipc_virtio_dev *vds,
  299. struct tipc_chan *chan)
  300. {
  301. int ret;
  302. mutex_lock(&vds->lock);
  303. if (vds->state == VDS_ONLINE) {
  304. ret = idr_alloc(&vds->addr_idr, chan,
  305. TIPC_MIN_LOCAL_ADDR, TIPC_ANY_ADDR - 1,
  306. GFP_KERNEL);
  307. if (ret > 0) {
  308. chan->local = ret;
  309. kref_get(&chan->refcount);
  310. ret = 0;
  311. }
  312. } else {
  313. ret = -EINVAL;
  314. }
  315. mutex_unlock(&vds->lock);
  316. return ret;
  317. }
  318. static void vds_del_channel(struct tipc_virtio_dev *vds,
  319. struct tipc_chan *chan)
  320. {
  321. mutex_lock(&vds->lock);
  322. if (chan->local) {
  323. idr_remove(&vds->addr_idr, chan->local);
  324. chan->local = 0;
  325. chan->remote = 0;
  326. kref_put(&chan->refcount, _free_chan);
  327. }
  328. mutex_unlock(&vds->lock);
  329. }
  330. static struct tipc_chan *vds_lookup_channel(struct tipc_virtio_dev *vds,
  331. u32 addr)
  332. {
  333. int id;
  334. struct tipc_chan *chan = NULL;
  335. mutex_lock(&vds->lock);
  336. if (addr == TIPC_ANY_ADDR) {
  337. id = idr_for_each(&vds->addr_idr, _match_any, NULL);
  338. if (id > 0)
  339. chan = idr_find(&vds->addr_idr, id);
  340. } else {
  341. chan = idr_find(&vds->addr_idr, addr);
  342. }
  343. if (chan)
  344. kref_get(&chan->refcount);
  345. mutex_unlock(&vds->lock);
  346. return chan;
  347. }
  348. static struct tipc_chan *vds_create_channel(struct tipc_virtio_dev *vds,
  349. const struct tipc_chan_ops *ops,
  350. void *ops_arg)
  351. {
  352. int ret;
  353. struct tipc_chan *chan = NULL;
  354. if (!vds)
  355. return ERR_PTR(-ENOENT);
  356. if (!ops)
  357. return ERR_PTR(-EINVAL);
  358. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  359. if (!chan)
  360. return ERR_PTR(-ENOMEM);
  361. kref_get(&vds->refcount);
  362. chan->vds = vds;
  363. chan->ops = ops;
  364. chan->ops_arg = ops_arg;
  365. mutex_init(&chan->lock);
  366. kref_init(&chan->refcount);
  367. chan->state = TIPC_DISCONNECTED;
  368. ret = vds_add_channel(vds, chan);
  369. if (ret) {
  370. kfree(chan);
  371. kref_put(&vds->refcount, _free_vds);
  372. return ERR_PTR(ret);
  373. }
  374. return chan;
  375. }
  376. static void fill_msg_hdr(struct tipc_msg_buf *mb, u32 src, u32 dst)
  377. {
  378. struct tipc_msg_hdr *hdr = mb_get_data(mb, sizeof(*hdr));
  379. hdr->src = src;
  380. hdr->dst = dst;
  381. hdr->len = mb_avail_data(mb);
  382. hdr->flags = 0;
  383. hdr->reserved = 0;
  384. }
  385. /*****************************************************************************/
  386. struct tipc_chan *tipc_create_channel(struct device *dev,
  387. const struct tipc_chan_ops *ops,
  388. void *ops_arg)
  389. {
  390. struct virtio_device *vd =
  391. container_of(dev, struct virtio_device, dev);
  392. return vds_create_channel(vd->priv, ops, ops_arg);
  393. }
  394. EXPORT_SYMBOL(tipc_create_channel);
  395. struct tipc_msg_buf *tipc_chan_get_rxbuf(struct tipc_chan *chan)
  396. {
  397. return vds_alloc_msg_buf(chan->vds);
  398. }
  399. EXPORT_SYMBOL(tipc_chan_get_rxbuf);
  400. void tipc_chan_put_rxbuf(struct tipc_chan *chan, struct tipc_msg_buf *mb)
  401. {
  402. vds_free_msg_buf(chan->vds, mb);
  403. }
  404. EXPORT_SYMBOL(tipc_chan_put_rxbuf);
  405. struct tipc_msg_buf *tipc_chan_get_txbuf_timeout(struct tipc_chan *chan,
  406. long timeout)
  407. {
  408. return vds_get_txbuf(chan->vds, timeout);
  409. }
  410. EXPORT_SYMBOL(tipc_chan_get_txbuf_timeout);
  411. void tipc_chan_put_txbuf(struct tipc_chan *chan, struct tipc_msg_buf *mb)
  412. {
  413. vds_put_txbuf(chan->vds, mb);
  414. }
  415. EXPORT_SYMBOL(tipc_chan_put_txbuf);
  416. int tipc_chan_queue_msg(struct tipc_chan *chan, struct tipc_msg_buf *mb)
  417. {
  418. int err;
  419. mutex_lock(&chan->lock);
  420. switch (chan->state) {
  421. case TIPC_CONNECTED:
  422. fill_msg_hdr(mb, chan->local, chan->remote);
  423. err = vds_queue_txbuf(chan->vds, mb);
  424. if (err) {
  425. /* this should never happen */
  426. pr_err("%s: failed to queue tx buffer (%d)\n",
  427. __func__, err);
  428. }
  429. break;
  430. case TIPC_DISCONNECTED:
  431. case TIPC_CONNECTING:
  432. err = -ENOTCONN;
  433. break;
  434. case TIPC_STALE:
  435. err = -ESHUTDOWN;
  436. break;
  437. default:
  438. err = -EBADFD;
  439. pr_err("%s: unexpected channel state %d\n",
  440. __func__, chan->state);
  441. }
  442. mutex_unlock(&chan->lock);
  443. return err;
  444. }
  445. EXPORT_SYMBOL(tipc_chan_queue_msg);
  446. int tipc_chan_connect(struct tipc_chan *chan, const char *name)
  447. {
  448. int err;
  449. struct tipc_ctrl_msg *msg;
  450. struct tipc_conn_req_body *body;
  451. struct tipc_msg_buf *txbuf;
  452. txbuf = vds_get_txbuf(chan->vds, TXBUF_TIMEOUT);
  453. if (IS_ERR(txbuf))
  454. return PTR_ERR(txbuf);
  455. /* reserve space for connection request control message */
  456. msg = mb_put_data(txbuf, sizeof(*msg) + sizeof(*body));
  457. body = (struct tipc_conn_req_body *)msg->body;
  458. /* fill message */
  459. msg->type = TIPC_CTRL_MSGTYPE_CONN_REQ;
  460. msg->body_len = sizeof(*body);
  461. strncpy(body->name, name, sizeof(body->name));
  462. body->name[sizeof(body->name)-1] = '\0';
  463. mutex_lock(&chan->lock);
  464. switch (chan->state) {
  465. case TIPC_DISCONNECTED:
  466. /* save service name we are connecting to */
  467. strcpy(chan->srv_name, body->name);
  468. fill_msg_hdr(txbuf, chan->local, TIPC_CTRL_ADDR);
  469. err = vds_queue_txbuf(chan->vds, txbuf);
  470. if (err) {
  471. /* this should never happen */
  472. pr_err("%s: failed to queue tx buffer (%d)\n",
  473. __func__, err);
  474. } else {
  475. chan->state = TIPC_CONNECTING;
  476. txbuf = NULL; /* prevents discarding buffer */
  477. }
  478. break;
  479. case TIPC_CONNECTED:
  480. case TIPC_CONNECTING:
  481. /* check if we are trying to connect to the same service */
  482. if (strcmp(chan->srv_name, body->name) == 0)
  483. err = 0;
  484. else
  485. if (chan->state == TIPC_CONNECTING)
  486. err = -EALREADY; /* in progress */
  487. else
  488. err = -EISCONN; /* already connected */
  489. break;
  490. case TIPC_STALE:
  491. err = -ESHUTDOWN;
  492. break;
  493. default:
  494. err = -EBADFD;
  495. pr_err("%s: unexpected channel state %d\n",
  496. __func__, chan->state);
  497. break;
  498. }
  499. mutex_unlock(&chan->lock);
  500. if (txbuf)
  501. tipc_chan_put_txbuf(chan, txbuf); /* discard it */
  502. return err;
  503. }
  504. EXPORT_SYMBOL(tipc_chan_connect);
  505. int tipc_chan_shutdown(struct tipc_chan *chan)
  506. {
  507. int err;
  508. struct tipc_ctrl_msg *msg;
  509. struct tipc_disc_req_body *body;
  510. struct tipc_msg_buf *txbuf = NULL;
  511. /* get tx buffer */
  512. txbuf = vds_get_txbuf(chan->vds, TXBUF_TIMEOUT);
  513. if (IS_ERR(txbuf))
  514. return PTR_ERR(txbuf);
  515. mutex_lock(&chan->lock);
  516. if (chan->state == TIPC_CONNECTED || chan->state == TIPC_CONNECTING) {
  517. /* reserve space for disconnect request control message */
  518. msg = mb_put_data(txbuf, sizeof(*msg) + sizeof(*body));
  519. body = (struct tipc_disc_req_body *)msg->body;
  520. msg->type = TIPC_CTRL_MSGTYPE_DISC_REQ;
  521. msg->body_len = sizeof(*body);
  522. body->target = chan->remote;
  523. fill_msg_hdr(txbuf, chan->local, TIPC_CTRL_ADDR);
  524. err = vds_queue_txbuf(chan->vds, txbuf);
  525. if (err) {
  526. /* this should never happen */
  527. pr_err("%s: failed to queue tx buffer (%d)\n",
  528. __func__, err);
  529. }
  530. } else {
  531. err = -ENOTCONN;
  532. }
  533. chan->state = TIPC_STALE;
  534. mutex_unlock(&chan->lock);
  535. if (err) {
  536. /* release buffer */
  537. tipc_chan_put_txbuf(chan, txbuf);
  538. }
  539. return err;
  540. }
  541. EXPORT_SYMBOL(tipc_chan_shutdown);
  542. void tipc_chan_destroy(struct tipc_chan *chan)
  543. {
  544. mutex_lock(&chan->lock);
  545. if (chan->vds) {
  546. vds_del_channel(chan->vds, chan);
  547. kref_put(&chan->vds->refcount, _free_vds);
  548. chan->vds = NULL;
  549. }
  550. mutex_unlock(&chan->lock);
  551. kref_put(&chan->refcount, _free_chan);
  552. }
  553. EXPORT_SYMBOL(tipc_chan_destroy);
  554. /***************************************************************************/
  555. struct tipc_dn_chan {
  556. int state;
  557. struct mutex lock; /* protects rx_msg_queue list and channel state */
  558. struct tipc_chan *chan;
  559. wait_queue_head_t readq;
  560. struct completion reply_comp;
  561. struct list_head rx_msg_queue;
  562. };
  563. static int dn_wait_for_reply(struct tipc_dn_chan *dn, int timeout)
  564. {
  565. int ret;
  566. ret = wait_for_completion_interruptible_timeout(&dn->reply_comp,
  567. msecs_to_jiffies(timeout));
  568. if (ret < 0)
  569. return ret;
  570. mutex_lock(&dn->lock);
  571. if (!ret) {
  572. /* no reply from remote */
  573. dn->state = TIPC_STALE;
  574. ret = -ETIMEDOUT;
  575. } else {
  576. /* got reply */
  577. if (dn->state == TIPC_CONNECTED)
  578. ret = 0;
  579. else if (dn->state == TIPC_DISCONNECTED)
  580. if (!list_empty(&dn->rx_msg_queue))
  581. ret = 0;
  582. else
  583. ret = -ENOTCONN;
  584. else
  585. ret = -EIO;
  586. }
  587. mutex_unlock(&dn->lock);
  588. return ret;
  589. }
  590. struct tipc_msg_buf *dn_handle_msg(void *data, struct tipc_msg_buf *rxbuf)
  591. {
  592. struct tipc_dn_chan *dn = data;
  593. struct tipc_msg_buf *newbuf = rxbuf;
  594. mutex_lock(&dn->lock);
  595. if (dn->state == TIPC_CONNECTED) {
  596. /* get new buffer */
  597. newbuf = tipc_chan_get_rxbuf(dn->chan);
  598. if (newbuf) {
  599. /* queue an old buffer and return a new one */
  600. list_add_tail(&rxbuf->node, &dn->rx_msg_queue);
  601. wake_up_interruptible(&dn->readq);
  602. } else {
  603. /*
  604. * return an old buffer effectively discarding
  605. * incoming message
  606. */
  607. pr_err("%s: discard incoming message\n", __func__);
  608. }
  609. }
  610. mutex_unlock(&dn->lock);
  611. return newbuf;
  612. }
  613. static void dn_connected(struct tipc_dn_chan *dn)
  614. {
  615. mutex_lock(&dn->lock);
  616. dn->state = TIPC_CONNECTED;
  617. /* complete all pending */
  618. complete(&dn->reply_comp);
  619. mutex_unlock(&dn->lock);
  620. }
  621. static void dn_disconnected(struct tipc_dn_chan *dn)
  622. {
  623. mutex_lock(&dn->lock);
  624. dn->state = TIPC_DISCONNECTED;
  625. /* complete all pending */
  626. complete(&dn->reply_comp);
  627. /* wakeup all readers */
  628. wake_up_interruptible_all(&dn->readq);
  629. mutex_unlock(&dn->lock);
  630. }
  631. static void dn_shutdown(struct tipc_dn_chan *dn)
  632. {
  633. mutex_lock(&dn->lock);
  634. /* set state to STALE */
  635. dn->state = TIPC_STALE;
  636. /* complete all pending */
  637. complete(&dn->reply_comp);
  638. /* wakeup all readers */
  639. wake_up_interruptible_all(&dn->readq);
  640. mutex_unlock(&dn->lock);
  641. }
  642. static void dn_handle_event(void *data, int event)
  643. {
  644. struct tipc_dn_chan *dn = data;
  645. switch (event) {
  646. case TIPC_CHANNEL_SHUTDOWN:
  647. dn_shutdown(dn);
  648. break;
  649. case TIPC_CHANNEL_DISCONNECTED:
  650. dn_disconnected(dn);
  651. break;
  652. case TIPC_CHANNEL_CONNECTED:
  653. dn_connected(dn);
  654. break;
  655. default:
  656. pr_err("%s: unhandled event %d\n", __func__, event);
  657. break;
  658. }
  659. }
  660. static struct tipc_chan_ops _dn_ops = {
  661. .handle_msg = dn_handle_msg,
  662. .handle_event = dn_handle_event,
  663. };
  664. #define cdev_to_cdn(c) container_of((c), struct tipc_cdev_node, cdev)
  665. #define cdn_to_vds(cdn) container_of((cdn), struct tipc_virtio_dev, cdev_node)
  666. static struct tipc_virtio_dev *_dn_lookup_vds(struct tipc_cdev_node *cdn)
  667. {
  668. int ret;
  669. struct tipc_virtio_dev *vds = NULL;
  670. mutex_lock(&tipc_devices_lock);
  671. ret = idr_for_each(&tipc_devices, _match_data, cdn);
  672. if (ret) {
  673. vds = cdn_to_vds(cdn);
  674. kref_get(&vds->refcount);
  675. }
  676. mutex_unlock(&tipc_devices_lock);
  677. return vds;
  678. }
  679. static int tipc_open(struct inode *inode, struct file *filp)
  680. {
  681. int ret;
  682. struct tipc_virtio_dev *vds;
  683. struct tipc_dn_chan *dn;
  684. struct tipc_cdev_node *cdn = cdev_to_cdn(inode->i_cdev);
  685. vds = _dn_lookup_vds(cdn);
  686. if (!vds) {
  687. ret = -ENOENT;
  688. goto err_vds_lookup;
  689. }
  690. dn = kzalloc(sizeof(*dn), GFP_KERNEL);
  691. if (!dn) {
  692. ret = -ENOMEM;
  693. goto err_alloc_chan;
  694. }
  695. mutex_init(&dn->lock);
  696. init_waitqueue_head(&dn->readq);
  697. init_completion(&dn->reply_comp);
  698. INIT_LIST_HEAD(&dn->rx_msg_queue);
  699. dn->state = TIPC_DISCONNECTED;
  700. dn->chan = vds_create_channel(vds, &_dn_ops, dn);
  701. if (IS_ERR(dn->chan)) {
  702. ret = PTR_ERR(dn->chan);
  703. goto err_create_chan;
  704. }
  705. filp->private_data = dn;
  706. kref_put(&vds->refcount, _free_vds);
  707. return 0;
  708. err_create_chan:
  709. kfree(dn);
  710. err_alloc_chan:
  711. kref_put(&vds->refcount, _free_vds);
  712. err_vds_lookup:
  713. return ret;
  714. }
  715. static int dn_connect_ioctl(struct tipc_dn_chan *dn, char __user *usr_name)
  716. {
  717. int err;
  718. char name[MAX_SRV_NAME_LEN];
  719. /* copy in service name from user space */
  720. err = strncpy_from_user(name, usr_name, sizeof(name));
  721. if (err < 0) {
  722. pr_err("%s: copy_from_user (%p) failed (%d)\n",
  723. __func__, usr_name, err);
  724. return err;
  725. }
  726. name[sizeof(name)-1] = '\0';
  727. /* send connect request */
  728. err = tipc_chan_connect(dn->chan, name);
  729. if (err)
  730. return err;
  731. /* and wait for reply */
  732. return dn_wait_for_reply(dn, REPLY_TIMEOUT);
  733. }
  734. static long tipc_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  735. {
  736. int ret;
  737. struct tipc_dn_chan *dn = filp->private_data;
  738. if (_IOC_TYPE(cmd) != TIPC_IOC_MAGIC)
  739. return -EINVAL;
  740. switch (cmd) {
  741. case TIPC_IOC_CONNECT:
  742. ret = dn_connect_ioctl(dn, (char __user *)arg);
  743. if (ret) {
  744. pr_err("%s: TIPC_IOC_CONNECT error (%d)!\n", __func__, ret);
  745. trusty_fast_call32(dn->chan->vds->vdev->dev.parent->parent,
  746. MT_SMC_FC_THREADS, 0, 0, 0);
  747. trusty_std_call32(dn->chan->vds->vdev->dev.parent->parent,
  748. SMC_SC_NOP, 0, 0, 0);
  749. }
  750. break;
  751. default:
  752. pr_warn("%s: Unhandled ioctl cmd: 0x%x\n",
  753. __func__, cmd);
  754. ret = -EINVAL;
  755. }
  756. return ret;
  757. }
  758. #if defined(CONFIG_COMPAT)
  759. static long tipc_compat_ioctl(struct file *filp,
  760. unsigned int cmd, unsigned long arg)
  761. {
  762. int ret;
  763. struct tipc_dn_chan *dn = filp->private_data;
  764. void __user *user_req = compat_ptr(arg);
  765. if (_IOC_TYPE(cmd) != TIPC_IOC_MAGIC)
  766. return -EINVAL;
  767. switch (cmd) {
  768. case TIPC_IOC_CONNECT_COMPAT:
  769. ret = dn_connect_ioctl(dn, user_req);
  770. break;
  771. default:
  772. pr_warn("%s: Unhandled ioctl cmd: 0x%x\n",
  773. __func__, cmd);
  774. ret = -EINVAL;
  775. }
  776. return ret;
  777. }
  778. #endif
  779. static inline bool _got_rx(struct tipc_dn_chan *dn)
  780. {
  781. if (dn->state != TIPC_CONNECTED)
  782. return true;
  783. if (!list_empty(&dn->rx_msg_queue))
  784. return true;
  785. return false;
  786. }
  787. static ssize_t tipc_read(struct file *filp, char __user *buf, size_t buf_len,
  788. loff_t *offp)
  789. {
  790. ssize_t ret;
  791. size_t data_len;
  792. struct tipc_msg_buf *mb;
  793. struct tipc_dn_chan *dn = filp->private_data;
  794. mutex_lock(&dn->lock);
  795. while (list_empty(&dn->rx_msg_queue)) {
  796. if (dn->state != TIPC_CONNECTED) {
  797. if (dn->state == TIPC_CONNECTING)
  798. ret = -ENOTCONN;
  799. else if (dn->state == TIPC_DISCONNECTED)
  800. ret = -ENOTCONN;
  801. else if (dn->state == TIPC_STALE)
  802. ret = -ESHUTDOWN;
  803. else
  804. ret = -EBADFD;
  805. goto out;
  806. }
  807. mutex_unlock(&dn->lock);
  808. if (filp->f_flags & O_NONBLOCK)
  809. return -EAGAIN;
  810. if (wait_event_interruptible(dn->readq, _got_rx(dn)))
  811. return -ERESTARTSYS;
  812. mutex_lock(&dn->lock);
  813. }
  814. mb = list_first_entry(&dn->rx_msg_queue, struct tipc_msg_buf, node);
  815. data_len = mb_avail_data(mb);
  816. if (data_len > buf_len) {
  817. ret = -EMSGSIZE;
  818. goto out;
  819. }
  820. if (copy_to_user(buf, mb_get_data(mb, data_len), data_len)) {
  821. ret = -EFAULT;
  822. goto out;
  823. }
  824. ret = data_len;
  825. list_del(&mb->node);
  826. tipc_chan_put_rxbuf(dn->chan, mb);
  827. out:
  828. mutex_unlock(&dn->lock);
  829. return ret;
  830. }
  831. static ssize_t tipc_write(struct file *filp, const char __user *ubuf,
  832. size_t len, loff_t *offp)
  833. {
  834. ssize_t ret;
  835. long timeout = TXBUF_TIMEOUT;
  836. struct tipc_msg_buf *txbuf = NULL;
  837. struct tipc_dn_chan *dn = filp->private_data;
  838. if (filp->f_flags & O_NONBLOCK)
  839. timeout = 0;
  840. txbuf = tipc_chan_get_txbuf_timeout(dn->chan, timeout);
  841. if (IS_ERR(txbuf))
  842. return PTR_ERR(txbuf);
  843. /* check available space */
  844. if (len > mb_avail_space(txbuf)) {
  845. ret = -EMSGSIZE;
  846. goto err_out;
  847. }
  848. /* copy in message data */
  849. if (copy_from_user(mb_put_data(txbuf, len), ubuf, len)) {
  850. ret = -EFAULT;
  851. goto err_out;
  852. }
  853. /* queue message */
  854. ret = tipc_chan_queue_msg(dn->chan, txbuf);
  855. if (ret)
  856. goto err_out;
  857. return len;
  858. err_out:
  859. tipc_chan_put_txbuf(dn->chan, txbuf);
  860. return ret;
  861. }
  862. static unsigned int tipc_poll(struct file *filp, poll_table *wait)
  863. {
  864. unsigned int mask = 0;
  865. struct tipc_dn_chan *dn = filp->private_data;
  866. mutex_lock(&dn->lock);
  867. poll_wait(filp, &dn->readq, wait);
  868. /* Writes always succeed for now */
  869. mask |= POLLOUT | POLLWRNORM;
  870. if (!list_empty(&dn->rx_msg_queue))
  871. mask |= POLLIN | POLLRDNORM;
  872. if (dn->state != TIPC_CONNECTED)
  873. mask |= POLLERR;
  874. mutex_unlock(&dn->lock);
  875. return mask;
  876. }
  877. static int tipc_release(struct inode *inode, struct file *filp)
  878. {
  879. struct tipc_dn_chan *dn = filp->private_data;
  880. dn_shutdown(dn);
  881. /* free all pending buffers */
  882. _free_msg_buf_list(&dn->rx_msg_queue);
  883. /* shutdown channel */
  884. tipc_chan_shutdown(dn->chan);
  885. /* and destroy it */
  886. tipc_chan_destroy(dn->chan);
  887. kfree(dn);
  888. return 0;
  889. }
  890. static const struct file_operations tipc_fops = {
  891. .open = tipc_open,
  892. .release = tipc_release,
  893. .unlocked_ioctl = tipc_ioctl,
  894. #if defined(CONFIG_COMPAT)
  895. .compat_ioctl = tipc_compat_ioctl,
  896. #endif
  897. .read = tipc_read,
  898. .write = tipc_write,
  899. .poll = tipc_poll,
  900. .owner = THIS_MODULE,
  901. };
  902. /*****************************************************************************/
  903. static void chan_trigger_event(struct tipc_chan *chan, int event)
  904. {
  905. if (!event)
  906. return;
  907. chan->ops->handle_event(chan->ops_arg, event);
  908. }
  909. static void _cleanup_vq(struct virtqueue *vq)
  910. {
  911. struct tipc_msg_buf *mb;
  912. while ((mb = virtqueue_detach_unused_buf(vq)) != NULL)
  913. _free_msg_buf(mb);
  914. }
  915. static int _create_cdev_node(struct device *parent,
  916. struct tipc_cdev_node *cdn,
  917. const char *name)
  918. {
  919. int ret;
  920. dev_t devt;
  921. if (!name) {
  922. dev_dbg(parent, "%s: cdev name has to be provided\n",
  923. __func__);
  924. return -EINVAL;
  925. }
  926. /* allocate minor */
  927. ret = idr_alloc(&tipc_devices, cdn, 0, MAX_DEVICES-1, GFP_KERNEL);
  928. if (ret < 0) {
  929. dev_dbg(parent, "%s: failed (%d) to get id\n",
  930. __func__, ret);
  931. return ret;
  932. }
  933. cdn->minor = ret;
  934. cdev_init(&cdn->cdev, &tipc_fops);
  935. cdn->cdev.owner = THIS_MODULE;
  936. /* Add character device */
  937. devt = MKDEV(tipc_major, cdn->minor);
  938. ret = cdev_add(&cdn->cdev, devt, 1);
  939. if (ret) {
  940. dev_dbg(parent, "%s: cdev_add failed (%d)\n",
  941. __func__, ret);
  942. goto err_add_cdev;
  943. }
  944. /* Create a device node */
  945. cdn->dev = device_create(tipc_class, parent,
  946. devt, NULL, "trusty-ipc-%s", name);
  947. if (IS_ERR(cdn->dev)) {
  948. ret = PTR_ERR(cdn->dev);
  949. dev_dbg(parent, "%s: device_create failed: %d\n",
  950. __func__, ret);
  951. goto err_device_create;
  952. }
  953. return 0;
  954. err_device_create:
  955. cdn->dev = NULL;
  956. cdev_del(&cdn->cdev);
  957. err_add_cdev:
  958. idr_remove(&tipc_devices, cdn->minor);
  959. return ret;
  960. }
  961. static void create_cdev_node(struct tipc_virtio_dev *vds,
  962. struct tipc_cdev_node *cdn)
  963. {
  964. int err;
  965. mutex_lock(&tipc_devices_lock);
  966. if (vds->cdev_name[0] && !cdn->dev) {
  967. kref_get(&vds->refcount);
  968. err = _create_cdev_node(&vds->vdev->dev, cdn, vds->cdev_name);
  969. if (err) {
  970. dev_err(&vds->vdev->dev,
  971. "failed (%d) to create cdev node\n", err);
  972. kref_put(&vds->refcount, _free_vds);
  973. }
  974. }
  975. mutex_unlock(&tipc_devices_lock);
  976. }
  977. static void destroy_cdev_node(struct tipc_virtio_dev *vds,
  978. struct tipc_cdev_node *cdn)
  979. {
  980. mutex_lock(&tipc_devices_lock);
  981. if (cdn->dev) {
  982. device_destroy(tipc_class, MKDEV(tipc_major, cdn->minor));
  983. cdev_del(&cdn->cdev);
  984. idr_remove(&tipc_devices, cdn->minor);
  985. cdn->dev = NULL;
  986. kref_put(&vds->refcount, _free_vds);
  987. }
  988. mutex_unlock(&tipc_devices_lock);
  989. }
  990. static void _go_online(struct tipc_virtio_dev *vds)
  991. {
  992. mutex_lock(&vds->lock);
  993. if (vds->state == VDS_OFFLINE)
  994. vds->state = VDS_ONLINE;
  995. mutex_unlock(&vds->lock);
  996. create_cdev_node(vds, &vds->cdev_node);
  997. dev_info(&vds->vdev->dev, "is online\n");
  998. }
  999. static void _go_offline(struct tipc_virtio_dev *vds)
  1000. {
  1001. struct tipc_chan *chan;
  1002. /* change state to OFFLINE */
  1003. mutex_lock(&vds->lock);
  1004. if (vds->state != VDS_ONLINE) {
  1005. mutex_unlock(&vds->lock);
  1006. return;
  1007. }
  1008. vds->state = VDS_OFFLINE;
  1009. mutex_unlock(&vds->lock);
  1010. /* wakeup all waiters */
  1011. wake_up_interruptible_all(&vds->sendq);
  1012. /* shutdown all channels */
  1013. while ((chan = vds_lookup_channel(vds, TIPC_ANY_ADDR))) {
  1014. mutex_lock(&chan->lock);
  1015. chan->state = TIPC_STALE;
  1016. chan->remote = 0;
  1017. chan_trigger_event(chan, TIPC_CHANNEL_SHUTDOWN);
  1018. mutex_unlock(&chan->lock);
  1019. kref_put(&chan->refcount, _free_chan);
  1020. }
  1021. /* shutdown device node */
  1022. destroy_cdev_node(vds, &vds->cdev_node);
  1023. dev_info(&vds->vdev->dev, "is offline\n");
  1024. }
  1025. static void _handle_conn_rsp(struct tipc_virtio_dev *vds,
  1026. struct tipc_conn_rsp_body *rsp, size_t len)
  1027. {
  1028. struct tipc_chan *chan;
  1029. if (sizeof(*rsp) != len) {
  1030. dev_err(&vds->vdev->dev, "%s: Invalid response length %zd\n",
  1031. __func__, len);
  1032. return;
  1033. }
  1034. dev_dbg(&vds->vdev->dev,
  1035. "%s: connection response: for addr 0x%x: "
  1036. "status %d remote addr 0x%x\n",
  1037. __func__, rsp->target, rsp->status, rsp->remote);
  1038. /* Lookup channel */
  1039. chan = vds_lookup_channel(vds, rsp->target);
  1040. if (chan) {
  1041. mutex_lock(&chan->lock);
  1042. if (chan->state == TIPC_CONNECTING) {
  1043. if (!rsp->status) {
  1044. chan->state = TIPC_CONNECTED;
  1045. chan->remote = rsp->remote;
  1046. chan->max_msg_cnt = rsp->max_msg_cnt;
  1047. chan->max_msg_size = rsp->max_msg_size;
  1048. chan_trigger_event(chan,
  1049. TIPC_CHANNEL_CONNECTED);
  1050. } else {
  1051. chan->state = TIPC_DISCONNECTED;
  1052. chan->remote = 0;
  1053. chan_trigger_event(chan,
  1054. TIPC_CHANNEL_DISCONNECTED);
  1055. }
  1056. }
  1057. mutex_unlock(&chan->lock);
  1058. kref_put(&chan->refcount, _free_chan);
  1059. }
  1060. }
  1061. static void _handle_disc_req(struct tipc_virtio_dev *vds,
  1062. struct tipc_disc_req_body *req, size_t len)
  1063. {
  1064. struct tipc_chan *chan;
  1065. if (sizeof(*req) != len) {
  1066. dev_err(&vds->vdev->dev, "%s: Invalid request length %zd\n",
  1067. __func__, len);
  1068. return;
  1069. }
  1070. dev_dbg(&vds->vdev->dev, "%s: disconnect request: for addr 0x%x\n",
  1071. __func__, req->target);
  1072. chan = vds_lookup_channel(vds, req->target);
  1073. if (chan) {
  1074. mutex_lock(&chan->lock);
  1075. if (chan->state == TIPC_CONNECTED) {
  1076. chan->state = TIPC_DISCONNECTED;
  1077. chan->remote = 0;
  1078. chan_trigger_event(chan, TIPC_CHANNEL_DISCONNECTED);
  1079. }
  1080. mutex_unlock(&chan->lock);
  1081. kref_put(&chan->refcount, _free_chan);
  1082. }
  1083. }
  1084. static void _handle_ctrl_msg(struct tipc_virtio_dev *vds,
  1085. void *data, int len, u32 src)
  1086. {
  1087. struct tipc_ctrl_msg *msg = data;
  1088. if ((len < sizeof(*msg)) || (sizeof(*msg) + msg->body_len != len)) {
  1089. dev_err(&vds->vdev->dev,
  1090. "%s: Invalid message length ( %d vs. %d)\n",
  1091. __func__, (int)(sizeof(*msg) + msg->body_len), len);
  1092. return;
  1093. }
  1094. dev_dbg(&vds->vdev->dev,
  1095. "%s: Incoming ctrl message: src 0x%x type %d len %d\n",
  1096. __func__, src, msg->type, msg->body_len);
  1097. switch (msg->type) {
  1098. case TIPC_CTRL_MSGTYPE_GO_ONLINE:
  1099. _go_online(vds);
  1100. break;
  1101. case TIPC_CTRL_MSGTYPE_GO_OFFLINE:
  1102. _go_offline(vds);
  1103. break;
  1104. case TIPC_CTRL_MSGTYPE_CONN_RSP:
  1105. _handle_conn_rsp(vds, (struct tipc_conn_rsp_body *)msg->body,
  1106. msg->body_len);
  1107. break;
  1108. case TIPC_CTRL_MSGTYPE_DISC_REQ:
  1109. _handle_disc_req(vds, (struct tipc_disc_req_body *)msg->body,
  1110. msg->body_len);
  1111. break;
  1112. default:
  1113. dev_warn(&vds->vdev->dev,
  1114. "%s: Unexpected message type: %d\n",
  1115. __func__, msg->type);
  1116. }
  1117. }
  1118. static int _handle_rxbuf(struct tipc_virtio_dev *vds,
  1119. struct tipc_msg_buf *rxbuf, size_t rxlen)
  1120. {
  1121. int err;
  1122. struct scatterlist sg;
  1123. struct tipc_msg_hdr *msg;
  1124. struct device *dev = &vds->vdev->dev;
  1125. /* message sanity check */
  1126. if (rxlen > rxbuf->buf_sz) {
  1127. dev_warn(dev, "inbound msg is too big: %zd\n", rxlen);
  1128. goto drop_it;
  1129. }
  1130. if (rxlen < sizeof(*msg)) {
  1131. dev_warn(dev, "inbound msg is too short: %zd\n", rxlen);
  1132. goto drop_it;
  1133. }
  1134. /* reset buffer and put data */
  1135. mb_reset(rxbuf);
  1136. mb_put_data(rxbuf, rxlen);
  1137. /* get message header */
  1138. msg = mb_get_data(rxbuf, sizeof(*msg));
  1139. if (mb_avail_data(rxbuf) != msg->len) {
  1140. dev_warn(dev, "inbound msg length mismatch: (%d vs. %d)\n",
  1141. (uint) mb_avail_data(rxbuf), (uint)msg->len);
  1142. goto drop_it;
  1143. }
  1144. dev_dbg(dev, "From: %d, To: %d, Len: %d, Flags: 0x%x, Reserved: %d\n",
  1145. msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
  1146. /* message directed to control endpoint is a special case */
  1147. if (msg->dst == TIPC_CTRL_ADDR) {
  1148. _handle_ctrl_msg(vds, msg->data, msg->len, msg->src);
  1149. } else {
  1150. struct tipc_chan *chan = NULL;
  1151. /* Lookup channel */
  1152. chan = vds_lookup_channel(vds, msg->dst);
  1153. if (chan) {
  1154. /* handle it */
  1155. rxbuf = chan->ops->handle_msg(chan->ops_arg, rxbuf);
  1156. BUG_ON(!rxbuf);
  1157. kref_put(&chan->refcount, _free_chan);
  1158. }
  1159. }
  1160. drop_it:
  1161. /* add the buffer back to the virtqueue */
  1162. sg_init_one(&sg, rxbuf->buf_va, rxbuf->buf_sz);
  1163. err = virtqueue_add_inbuf(vds->rxvq, &sg, 1, rxbuf, GFP_KERNEL);
  1164. if (err < 0) {
  1165. dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
  1166. return err;
  1167. }
  1168. return 0;
  1169. }
  1170. static void _rxvq_cb(struct virtqueue *rxvq)
  1171. {
  1172. unsigned int len;
  1173. struct tipc_msg_buf *mb;
  1174. unsigned int msg_cnt = 0;
  1175. struct tipc_virtio_dev *vds = rxvq->vdev->priv;
  1176. while ((mb = virtqueue_get_buf(rxvq, &len)) != NULL) {
  1177. if (_handle_rxbuf(vds, mb, len))
  1178. break;
  1179. msg_cnt++;
  1180. }
  1181. /* tell the other size that we added rx buffers */
  1182. if (msg_cnt)
  1183. virtqueue_kick(rxvq);
  1184. }
  1185. static void _txvq_cb(struct virtqueue *txvq)
  1186. {
  1187. unsigned int len;
  1188. struct tipc_msg_buf *mb;
  1189. bool need_wakeup = false;
  1190. struct tipc_virtio_dev *vds = txvq->vdev->priv;
  1191. dev_dbg(&txvq->vdev->dev, "%s\n", __func__);
  1192. /* detach all buffers */
  1193. mutex_lock(&vds->lock);
  1194. while ((mb = virtqueue_get_buf(txvq, &len)) != NULL)
  1195. need_wakeup |= _put_txbuf_locked(vds, mb);
  1196. mutex_unlock(&vds->lock);
  1197. if (need_wakeup) {
  1198. /* wake up potential senders waiting for a tx buffer */
  1199. wake_up_interruptible_all(&vds->sendq);
  1200. }
  1201. }
  1202. static int tipc_virtio_probe(struct virtio_device *vdev)
  1203. {
  1204. int err, i;
  1205. struct tipc_virtio_dev *vds;
  1206. struct tipc_dev_config config;
  1207. struct virtqueue *vqs[2];
  1208. vq_callback_t *vq_cbs[] = {_rxvq_cb, _txvq_cb};
  1209. const char *vq_names[] = { "rx", "tx" };
  1210. dev_dbg(&vdev->dev, "%s:\n", __func__);
  1211. vds = kzalloc(sizeof(*vds), GFP_KERNEL);
  1212. if (!vds)
  1213. return -ENOMEM;
  1214. vds->vdev = vdev;
  1215. mutex_init(&vds->lock);
  1216. kref_init(&vds->refcount);
  1217. init_waitqueue_head(&vds->sendq);
  1218. INIT_LIST_HEAD(&vds->free_buf_list);
  1219. idr_init(&vds->addr_idr);
  1220. /* set default max message size and alignment */
  1221. memset(&config, 0, sizeof(config));
  1222. config.msg_buf_max_size = DEFAULT_MSG_BUF_SIZE;
  1223. config.msg_buf_alignment = DEFAULT_MSG_BUF_ALIGN;
  1224. /* get configuration if present */
  1225. vdev->config->get(vdev, 0, &config, sizeof(config));
  1226. /* copy dev name */
  1227. strncpy(vds->cdev_name, config.dev_name, sizeof(vds->cdev_name));
  1228. vds->cdev_name[sizeof(vds->cdev_name)-1] = '\0';
  1229. /* find tx virtqueues (rx and tx and in this order) */
  1230. err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, vq_names);
  1231. if (err)
  1232. goto err_find_vqs;
  1233. vds->rxvq = vqs[0];
  1234. vds->txvq = vqs[1];
  1235. /* save max buffer size and count */
  1236. vds->msg_buf_max_sz = config.msg_buf_max_size;
  1237. vds->msg_buf_max_cnt = virtqueue_get_vring_size(vds->txvq);
  1238. /* set up the receive buffers */
  1239. for (i = 0; i < virtqueue_get_vring_size(vds->rxvq); i++) {
  1240. struct scatterlist sg;
  1241. struct tipc_msg_buf *rxbuf;
  1242. rxbuf = _alloc_msg_buf(vds->msg_buf_max_sz);
  1243. if (!rxbuf) {
  1244. dev_err(&vdev->dev, "failed to allocate rx buffer\n");
  1245. err = -ENOMEM;
  1246. goto err_free_rx_buffers;
  1247. }
  1248. sg_init_one(&sg, rxbuf->buf_va, rxbuf->buf_sz);
  1249. err = virtqueue_add_inbuf(vds->rxvq, &sg, 1, rxbuf, GFP_KERNEL);
  1250. WARN_ON(err); /* sanity check; this can't really happen */
  1251. }
  1252. vdev->priv = vds;
  1253. vds->state = VDS_OFFLINE;
  1254. dev_dbg(&vdev->dev, "%s: done\n", __func__);
  1255. return 0;
  1256. err_free_rx_buffers:
  1257. _cleanup_vq(vds->rxvq);
  1258. err_find_vqs:
  1259. kref_put(&vds->refcount, _free_vds);
  1260. return err;
  1261. }
  1262. static void tipc_virtio_remove(struct virtio_device *vdev)
  1263. {
  1264. struct tipc_virtio_dev *vds = vdev->priv;
  1265. _go_offline(vds);
  1266. mutex_lock(&vds->lock);
  1267. vds->state = VDS_DEAD;
  1268. vds->vdev = NULL;
  1269. mutex_unlock(&vds->lock);
  1270. vdev->config->reset(vdev);
  1271. idr_destroy(&vds->addr_idr);
  1272. _cleanup_vq(vds->rxvq);
  1273. _cleanup_vq(vds->txvq);
  1274. _free_msg_buf_list(&vds->free_buf_list);
  1275. vdev->config->del_vqs(vds->vdev);
  1276. kref_put(&vds->refcount, _free_vds);
  1277. }
  1278. static struct virtio_device_id tipc_virtio_id_table[] = {
  1279. { VIRTIO_ID_TRUSTY_IPC, VIRTIO_DEV_ANY_ID },
  1280. { 0 },
  1281. };
  1282. static unsigned int features[] = {
  1283. 0,
  1284. };
  1285. static struct virtio_driver virtio_tipc_driver = {
  1286. .feature_table = features,
  1287. .feature_table_size = ARRAY_SIZE(features),
  1288. .driver.name = KBUILD_MODNAME,
  1289. .driver.owner = THIS_MODULE,
  1290. .id_table = tipc_virtio_id_table,
  1291. .probe = tipc_virtio_probe,
  1292. .remove = tipc_virtio_remove,
  1293. };
  1294. static int __init tipc_init(void)
  1295. {
  1296. int ret;
  1297. dev_t dev;
  1298. ret = alloc_chrdev_region(&dev, 0, MAX_DEVICES, KBUILD_MODNAME);
  1299. if (ret) {
  1300. pr_err("%s: alloc_chrdev_region failed: %d\n", __func__, ret);
  1301. return ret;
  1302. }
  1303. tipc_major = MAJOR(dev);
  1304. tipc_class = class_create(THIS_MODULE, KBUILD_MODNAME);
  1305. if (IS_ERR(tipc_class)) {
  1306. ret = PTR_ERR(tipc_class);
  1307. pr_err("%s: class_create failed: %d\n", __func__, ret);
  1308. goto err_class_create;
  1309. }
  1310. ret = register_virtio_driver(&virtio_tipc_driver);
  1311. if (ret) {
  1312. pr_err("failed to register virtio driver: %d\n", ret);
  1313. goto err_register_virtio_drv;
  1314. }
  1315. return 0;
  1316. err_register_virtio_drv:
  1317. class_destroy(tipc_class);
  1318. err_class_create:
  1319. unregister_chrdev_region(dev, MAX_DEVICES);
  1320. return ret;
  1321. }
  1322. static void __exit tipc_exit(void)
  1323. {
  1324. unregister_virtio_driver(&virtio_tipc_driver);
  1325. class_destroy(tipc_class);
  1326. unregister_chrdev_region(MKDEV(tipc_major, 0), MAX_DEVICES);
  1327. }
  1328. /* We need to init this early */
  1329. subsys_initcall(tipc_init);
  1330. module_exit(tipc_exit);
  1331. MODULE_DEVICE_TABLE(tipc, tipc_virtio_id_table);
  1332. MODULE_DESCRIPTION("Trusty IPC driver");
  1333. MODULE_LICENSE("GPL v2");