virtio_eavb.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2024, Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include<linux/init.h>
  6. #include<linux/kernel.h>
  7. #include<linux/module.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/virtio.h>
  10. #include <linux/virtio_config.h>
  11. #include <linux/virtio_ids.h>
  12. #include <linux/cdev.h>
  13. #include <linux/delay.h>
  14. #include <linux/slab.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/sched/clock.h>
  17. #include <soc/qcom/boot_stats.h>
  18. #include <uapi/linux/eavb_shared.h>
  19. #include "vio_eavb.h"
  20. /* Virtio ID of eavb : 0xC006 */
  21. #define VIRTIO_ID_EAVB 49158
  22. /* Virtio ID of eavb for Backward compatibility : 0x24 */
  23. #define VIRTIO_ID_EAVB_BC 36
  24. /* support feature */
  25. #define VIRTIO_EAVB_F_SHMEM 1
  26. #define MINOR_NUM_DEV 0
  27. #define DEVICE_NAME "virt-eavb"
  28. #define DEVICE_NUM 1
  29. #define LEVEL_DEBUG 1
  30. #define LEVEL_INFO 2
  31. #define LEVEL_ERR 3
  32. static unsigned int log_level = LEVEL_INFO;
  33. static char *prix[] = {"", "debug", "info", "error"};
  34. static void log_eavb(int level, const char *fmt, ...)
  35. {
  36. va_list args;
  37. if ((level) >= log_level) {
  38. va_start(args, fmt);
  39. vprintk(fmt, args);
  40. va_end(args);
  41. }
  42. }
  43. #define LOG_EAVB(level, format, args...) \
  44. log_eavb(level, "eavb: pid %.8x: %s: %s(%d) "format, \
  45. current->pid, prix[0x3 & (level)], __func__, __LINE__, ## args)
  46. #define ASSERT(x) \
  47. do { \
  48. if (unlikely(!(x))) { \
  49. pr_err("Assertion failed! %s %s:%d\n", \
  50. __func__, __FILE__, __LINE__); \
  51. dump_stack(); \
  52. } \
  53. } while (0)
  54. static unsigned int timeout_msec = 5000; /* default 5s */
  55. /*
  56. * device_priv (struct virtio_eavb_priv)
  57. * |
  58. * |-- file_priv (struct eavb_file)
  59. * | |
  60. * | |-- stream0 (struct stream)
  61. * | |-- stream1
  62. * | |-- stream2
  63. */
  64. struct fe_msg;
  65. struct virtio_eavb_priv {
  66. char *name;
  67. struct virtio_device *vdev;
  68. struct virtqueue *svq;
  69. struct virtqueue *rvq;
  70. spinlock_t rvqlock;
  71. bool has_shmem;
  72. struct mutex lock;
  73. struct device *dev;
  74. struct cdev cdev;
  75. struct class *class;
  76. dev_t dev_no;
  77. int file_index;
  78. spinlock_t msglock;
  79. #define FE_MSG_MAX 256 /* 1<<8 */
  80. #define mod(index) ((index) & 0xff) /* ((index) % FE_MSG_MAX) */
  81. struct fe_msg *msgtable[FE_MSG_MAX];
  82. #define RX_BUF_MAX_LEN 1024
  83. void *rxbufs[FE_MSG_MAX];
  84. struct work_struct reclaim_work;
  85. #ifdef EAVB_DEBUGFS
  86. struct dentry *debugfs_root;
  87. struct rx_record {
  88. u32 msgid;
  89. u64 ts;
  90. } rxrecords[32];
  91. int next_rxrecords_index;
  92. #endif
  93. u64 crf_ts;
  94. struct completion crf_ts_update;
  95. };
  96. struct mapping {
  97. struct list_head list;
  98. u64 va; /* kernel virtual address */
  99. u64 ua; /* user address */
  100. u64 pa; /* phy address */
  101. size_t size;
  102. };
  103. struct eavb_file;
  104. struct stream {
  105. int index;
  106. struct eavb_file *fl;
  107. u64 hdl;
  108. u32 idx;
  109. enum stream_status {
  110. UNCREATED = 0,
  111. CREATED = 1,
  112. CONNECTED = 2,
  113. DISCONNECTED = 3,
  114. DESTROYED = 4,
  115. } status;
  116. #ifdef EAVB_DEBUGFS
  117. struct dentry *debugfs;
  118. struct tx_record {
  119. u32 msgid;
  120. u64 begin;
  121. u64 end;
  122. #define RECORD_COUNT (1<<4)
  123. } records[RECORD_COUNT];
  124. int next_records_index;
  125. spinlock_t recordslock;
  126. #endif
  127. };
  128. struct eavb_file {
  129. int index;
  130. struct virtio_eavb_priv *priv;
  131. #define MAPPING_MAX 8
  132. int mapping_count;
  133. struct mapping mapping[MAPPING_MAX];
  134. spinlock_t mappinglock;
  135. struct stream streams[MAX_STREAM_NUM];
  136. int stream_count;
  137. spinlock_t streamlock;
  138. };
  139. static inline const char *cmd2str(uint32_t cmd)
  140. {
  141. switch (cmd) {
  142. case VIRTIO_EAVB_T_CREATE_STREAM:
  143. return "VIRTIO_EAVB_T_CREATE_STREAM";
  144. case VIRTIO_EAVB_T_GET_STREAM_INFO:
  145. return "VIRTIO_EAVB_T_GET_STREAM_INFO";
  146. case VIRTIO_EAVB_T_CONNECT_STREAM:
  147. return "VIRTIO_EAVB_T_CONNECT_STREAM";
  148. case VIRTIO_EAVB_T_RECEIVE:
  149. return "VIRTIO_EAVB_T_RECEIVE";
  150. case VIRTIO_EAVB_T_TRANSMIT:
  151. return "VIRTIO_EAVB_T_TRANSMIT";
  152. case VIRTIO_EAVB_T_DISCONNECT_STREAM:
  153. return "VIRTIO_EAVB_T_DISCONNECT_STREAM";
  154. case VIRTIO_EAVB_T_DESTROY_STREAM:
  155. return "VIRTIO_EAVB_T_DESTROY_STREAM";
  156. case VIRTIO_EAVB_T_CREATE_STREAM_PATH:
  157. return "VIRTIO_EAVB_T_CREATE_STREAM_PATH";
  158. case VIRTIO_EAVB_T_MMAP:
  159. return "VIRTIO_EAVB_T_MMAP";
  160. case VIRTIO_EAVB_T_MUNMAP:
  161. return "VIRTIO_EAVB_T_MUNMAP";
  162. case VIRTIO_EAVB_T_UPDATE_CLK:
  163. return "VIRTIO_EAVB_T_UPDATE_CLK";
  164. default:
  165. return "not supported";
  166. }
  167. }
  168. #ifdef EAVB_DEBUGFS
  169. void update_crf_ts(struct virtio_eavb_priv *priv, u64 value);
  170. static ssize_t log_write(struct file *file,
  171. const char __user *buf,
  172. size_t count, loff_t *ppos)
  173. {
  174. char mybuffer[32] = {0};
  175. size_t len = min(count, sizeof(mybuffer));
  176. long input;
  177. if (copy_from_user(mybuffer, buf, len)) {
  178. LOG_EAVB(LEVEL_ERR, "copy_from_user failed\n");
  179. return -EFAULT;
  180. }
  181. if (kstrtol(mybuffer, 0, &input) == 0) {
  182. log_level = input & 0x3;
  183. LOG_EAVB(LEVEL_INFO, "update log_level to %d\n", log_level);
  184. }
  185. return len;
  186. }
  187. static int do_rxrecords_snapshoot(struct virtio_eavb_priv *priv,
  188. char *buffer, size_t size)
  189. {
  190. int i, index, len;
  191. struct rx_record records[32];
  192. memcpy(records, priv->rxrecords, sizeof(records));
  193. index = priv->next_rxrecords_index - 1;
  194. len = 0;
  195. for (i = 0; i < 32; i++) {
  196. u32 msgid;
  197. u64 ts, rem_nsec;
  198. int l;
  199. if (index < 0)
  200. break;
  201. msgid = records[index & 0x1f].msgid;
  202. ts = records[index & 0x1f].ts;
  203. rem_nsec = do_div(ts, 1000000000);
  204. l = scnprintf(&buffer[len], size - len, "%llu.%06llu: %d\n",
  205. ts, rem_nsec/1000, msgid);
  206. if (l <= 0)
  207. break;
  208. if (l + len > size)
  209. break;
  210. len += l;
  211. index--;
  212. }
  213. return len;
  214. }
  215. static ssize_t log_read(struct file *file,
  216. char __user *userbuf,
  217. size_t count, loff_t *ppos)
  218. {
  219. struct virtio_eavb_priv *priv = file->private_data;
  220. char *buffer;
  221. int len;
  222. ssize_t return_count;
  223. if (*ppos > 0)
  224. return 0;
  225. #define BUF_SIZE 512
  226. buffer = kzalloc(BUF_SIZE, GFP_KERNEL);
  227. if (!buffer)
  228. return 0;
  229. len = do_rxrecords_snapshoot(priv, buffer, BUF_SIZE);
  230. #undef BUF_SIZE
  231. return_count = simple_read_from_buffer(userbuf, count,
  232. ppos, buffer, len);
  233. kfree(buffer);
  234. return return_count;
  235. }
  236. static ssize_t timeout_write(struct file *file,
  237. const char __user *buf,
  238. size_t count, loff_t *ppos)
  239. {
  240. char mybuffer[32] = {0};
  241. size_t len = min(count, sizeof(mybuffer));
  242. long input;
  243. if (copy_from_user(mybuffer, buf, len)) {
  244. LOG_EAVB(LEVEL_ERR, "copy_from_user failed\n");
  245. return -EFAULT;
  246. }
  247. if (kstrtol(mybuffer, 0, &input) == 0) {
  248. timeout_msec = input;
  249. LOG_EAVB(LEVEL_INFO, "set timeout to %d ms\n", timeout_msec);
  250. }
  251. return len;
  252. }
  253. static ssize_t timeout_read(struct file *file,
  254. char __user *userbuf,
  255. size_t count, loff_t *ppos)
  256. {
  257. char *buffer;
  258. int len;
  259. ssize_t return_count;
  260. if (*ppos > 0)
  261. return 0;
  262. #define BUF_SIZE 512
  263. buffer = kzalloc(BUF_SIZE, GFP_KERNEL);
  264. if (!buffer)
  265. return 0;
  266. len = scnprintf(buffer, BUF_SIZE, "%d msec\n", timeout_msec);
  267. #undef BUF_SIZE
  268. return_count = simple_read_from_buffer(userbuf, count,
  269. ppos, buffer, len);
  270. kfree(buffer);
  271. return return_count;
  272. }
  273. static int do_stream_snapshoot(struct stream *stream, char *buffer, size_t size)
  274. {
  275. struct tx_record records[RECORD_COUNT];
  276. int current_records_index, save_index, i, len;
  277. u64 total_taken_us = 0;
  278. spin_lock(&stream->recordslock);
  279. memcpy(records, stream->records, sizeof(records));
  280. current_records_index = stream->next_records_index - 1;
  281. spin_unlock(&stream->recordslock);
  282. len = scnprintf(buffer, size,
  283. "stream%d: status(%d)\n",
  284. stream->index, stream->status);
  285. for (i = 0; i < RECORD_COUNT; i++) {
  286. u64 taken_us;
  287. int l;
  288. struct tx_record *record;
  289. if (current_records_index < 0)
  290. break;
  291. save_index = current_records_index & (RECORD_COUNT - 1);
  292. record = &records[save_index];
  293. taken_us = (record->end - record->begin)/1000;
  294. l = scnprintf(&buffer[len], size - len,
  295. "[%d]msgid %d, %lld us\n",
  296. current_records_index, record->msgid, taken_us);
  297. if (l <= 0)
  298. break;
  299. if (l + len > size)
  300. break;
  301. len += l;
  302. current_records_index--;
  303. total_taken_us += taken_us;
  304. }
  305. if (i > 0 && len < size) {
  306. /* average */
  307. int l = snprintf(&buffer[len], size - len,
  308. "average %lld us\n", total_taken_us / i);
  309. if (l > 0 && l + len <= size)
  310. len += l;
  311. }
  312. return len;
  313. }
  314. static ssize_t stream_read(struct file *file,
  315. char __user *userbuf,
  316. size_t count, loff_t *ppos)
  317. {
  318. struct stream *stream = file->private_data;
  319. char *buffer;
  320. int len;
  321. ssize_t return_count;
  322. if (*ppos > 0)
  323. return 0;
  324. #define BUF_SIZE 512
  325. buffer = kzalloc(BUF_SIZE, GFP_KERNEL);
  326. if (!buffer)
  327. return 0;
  328. len = do_stream_snapshoot(stream, buffer, BUF_SIZE);
  329. #undef BUF_SIZE
  330. return_count = simple_read_from_buffer(userbuf, count,
  331. ppos, buffer, len);
  332. kfree(buffer);
  333. return return_count;
  334. }
  335. static const struct file_operations fops_debugfs_log = {
  336. .open = simple_open,
  337. .write = log_write,
  338. .read = log_read,
  339. .owner = THIS_MODULE,
  340. };
  341. static const struct file_operations fops_debugfs_timeout = {
  342. .open = simple_open,
  343. .write = timeout_write,
  344. .read = timeout_read,
  345. .owner = THIS_MODULE,
  346. };
  347. static const struct file_operations fops_debugfs_stream = {
  348. .open = simple_open,
  349. .read = stream_read,
  350. .owner = THIS_MODULE,
  351. };
  352. #endif
  353. static struct stream *getStream(struct eavb_file *fl, u64 hdl)
  354. {
  355. int i;
  356. spin_lock(&fl->streamlock);
  357. for (i = 0; i < MAX_STREAM_NUM; i++) {
  358. struct stream *stream = &fl->streams[i];
  359. if (stream->hdl == hdl) {
  360. if (hdl == 0) {
  361. stream->index = i;
  362. stream->hdl = 0xffffffffffffffff;
  363. }
  364. spin_unlock(&fl->streamlock);
  365. return stream;
  366. }
  367. }
  368. spin_unlock(&fl->streamlock);
  369. return NULL;
  370. }
  371. static void reset_stream(struct stream *stream)
  372. {
  373. stream->hdl = 0;
  374. stream->idx = 0;
  375. }
  376. #ifdef EAVB_DEBUGFS
  377. static void create_dbugfs_stream(struct stream *stream)
  378. {
  379. struct eavb_file *fl = stream->fl;
  380. struct virtio_eavb_priv *priv = fl->priv;
  381. char name[16] = {0};
  382. snprintf(name, sizeof(name), "%d-stream%d", fl->index, stream->index);
  383. stream->debugfs = debugfs_create_dir(name, priv->debugfs_root);
  384. debugfs_create_file("status", 00400 | 00200,
  385. stream->debugfs, stream,
  386. &fops_debugfs_stream);
  387. stream->next_records_index = 0;
  388. spin_lock_init(&stream->recordslock);
  389. }
  390. static void destroy_dbugfs_stream(struct stream *stream)
  391. {
  392. debugfs_remove_recursive(stream->debugfs);
  393. }
  394. static void records(struct stream *stream, u32 msgid, u64 begin, u64 end)
  395. {
  396. int index;
  397. spin_lock(&stream->recordslock);
  398. index = (stream->next_records_index++) & 0x0f;
  399. spin_unlock(&stream->recordslock);
  400. stream->records[index].msgid = msgid;
  401. stream->records[index].begin = begin;
  402. stream->records[index].end = end;
  403. }
  404. #else
  405. #define create_dbugfs_stream(stream)
  406. #define destroy_dbugfs_stream(stream)
  407. #define records(stream, cmd, begin, end)
  408. #endif
  409. static struct mapping *get_file_mapping(struct eavb_file *fl, u64 addr, u32 len)
  410. {
  411. u64 start, end;
  412. int i;
  413. for (i = 0; i < MAPPING_MAX; i++) {
  414. start = fl->mapping[i].ua;
  415. end = fl->mapping[i].ua + fl->mapping[i].size;
  416. if (addr >= start && addr + len <= end)
  417. return &fl->mapping[i];
  418. }
  419. return NULL;
  420. }
  421. static u64 get_mapping_phyaddr(struct mapping *mapping, u64 ua)
  422. {
  423. return mapping->pa + (ua - mapping->ua);
  424. }
  425. static int add_mapping_info(struct eavb_file *fl,
  426. u64 va, u64 ua, u64 pa, size_t size)
  427. {
  428. int i;
  429. for (i = 0; i < MAPPING_MAX; i++) {
  430. if (fl->mapping[i].va == 0) {
  431. fl->mapping[i].size = size;
  432. fl->mapping[i].va = va;
  433. fl->mapping[i].ua = ua;
  434. fl->mapping[i].pa = pa;
  435. break;
  436. }
  437. }
  438. return i;
  439. }
  440. struct fe_msg {
  441. u16 msgid;
  442. struct completion work;
  443. void *txbuf;
  444. void *rxbuf;
  445. u32 txbuf_size;
  446. u32 rxbuf_used;
  447. u64 ts_begin;
  448. u64 ts_end;
  449. };
  450. static struct fe_msg *virt_alloc_msg(struct virtio_eavb_priv *priv,
  451. int tsize, int rsize)
  452. {
  453. struct fe_msg *msg;
  454. unsigned long flags;
  455. u8 *buf;
  456. int i;
  457. static int next; /* default 0 */
  458. ASSERT(rsize < RX_BUF_MAX_LEN);
  459. /* kzalloc needn't memset 0 */
  460. msg = kzalloc(sizeof(*msg) + tsize, GFP_KERNEL);
  461. if (!msg) {
  462. LOG_EAVB(LEVEL_ERR, "cannot alloc msg memory\n");
  463. return NULL;
  464. }
  465. buf = kzalloc(tsize, GFP_KERNEL);
  466. if (!buf) {
  467. LOG_EAVB(LEVEL_ERR, "cannot alloc txbuf memory\n");
  468. kfree(msg);
  469. return NULL;
  470. }
  471. spin_lock_irqsave(&priv->msglock, flags);
  472. for (i = 0; i < FE_MSG_MAX; i++) {
  473. int index = mod(next + i);
  474. if (!priv->msgtable[index]) {
  475. priv->msgtable[index] = msg;
  476. msg->msgid = index;
  477. next = mod(index + 1);
  478. break;
  479. }
  480. }
  481. spin_unlock_irqrestore(&priv->msglock, flags);
  482. if (i == FE_MSG_MAX) {
  483. LOG_EAVB(LEVEL_ERR, "fe message queue is full\n");
  484. kfree(buf);
  485. kfree(msg);
  486. return NULL;
  487. }
  488. msg->txbuf = buf;
  489. msg->txbuf_size = tsize;
  490. init_completion(&msg->work);
  491. return msg;
  492. }
  493. static void reclaim_rxbuf(struct virtio_eavb_priv *priv, void *rxbuf)
  494. {
  495. struct scatterlist sg;
  496. unsigned long flags;
  497. sg_init_one(&sg, rxbuf, RX_BUF_MAX_LEN);
  498. spin_lock_irqsave(&priv->rvqlock, flags);
  499. virtqueue_add_inbuf(priv->rvq, &sg, 1, rxbuf, GFP_KERNEL);
  500. virtqueue_kick(priv->rvq);
  501. spin_unlock_irqrestore(&priv->rvqlock, flags);
  502. }
  503. static void virt_free_msg(struct virtio_eavb_priv *priv, struct fe_msg *msg)
  504. {
  505. unsigned long flags;
  506. if (msg->rxbuf_used)
  507. reclaim_rxbuf(priv, msg->rxbuf);
  508. spin_lock_irqsave(&priv->msglock, flags);
  509. if (priv->msgtable[msg->msgid] == msg)
  510. priv->msgtable[msg->msgid] = NULL;
  511. else
  512. LOG_EAVB(LEVEL_ERR, "can't find msg %d in table\n",
  513. msg->msgid);
  514. spin_unlock_irqrestore(&priv->msglock, flags);
  515. kfree(msg);
  516. schedule_work(&priv->reclaim_work);
  517. }
  518. static void reclaim_function(struct work_struct *work)
  519. {
  520. struct virtio_eavb_priv *priv;
  521. priv = container_of(work, struct virtio_eavb_priv, reclaim_work);
  522. while (1) {
  523. struct vio_msg_hdr *vhdr;
  524. unsigned int len;
  525. mutex_lock(&priv->lock);
  526. /* remove a used txbuf */
  527. vhdr = virtqueue_get_buf(priv->svq, &len);
  528. mutex_unlock(&priv->lock);
  529. if (!vhdr)
  530. break;
  531. ASSERT(vhdr->len == len);
  532. LOG_EAVB(LEVEL_DEBUG, "msgid %d txbuf used\n", vhdr->msgid);
  533. kfree(vhdr);
  534. }
  535. }
  536. #define fill_vmsg_hdr(msg, stream, cmdtype) \
  537. do { \
  538. struct vio_msg_hdr *vhdr; \
  539. vhdr = (struct vio_msg_hdr *)msg->txbuf; \
  540. vhdr->cmd = cmdtype; \
  541. vhdr->len = msg->txbuf_size; \
  542. vhdr->streamctx_hdl = stream->hdl; \
  543. vhdr->stream_idx = stream->idx; \
  544. } while (0)
  545. static int send_msg(struct virtio_eavb_priv *priv, struct fe_msg *msg)
  546. {
  547. struct vio_msg_hdr *req, *rsp = NULL;
  548. struct scatterlist sg[1];
  549. int ret = 0;
  550. int rsv;
  551. uint32_t cmd_req, cmd_rsp = 0;
  552. int32_t stream_idx_req, stream_idx_rsp = 0;
  553. uint16_t msgid_req, msgid_rsp = 0;
  554. req = (struct vio_msg_hdr *)msg->txbuf;
  555. msgid_req = msg->msgid;
  556. cmd_req = req->cmd;
  557. stream_idx_req = req->stream_idx;
  558. msg->rxbuf = NULL;
  559. if ((cmd_req != VIRTIO_EAVB_T_RECEIVE) && (cmd_req != VIRTIO_EAVB_T_TRANSMIT))
  560. LOG_EAVB(LEVEL_INFO, "[eavb#%d] request: msgid %d, cmd %s\n",
  561. stream_idx_req, msgid_req, cmd2str(cmd_req));
  562. mutex_lock(&priv->lock);
  563. req->msgid = msg->msgid;
  564. sg_init_one(sg, req, req->len);
  565. ret = virtqueue_add_outbuf(priv->svq, sg, 1, req, GFP_KERNEL);
  566. if (ret) {
  567. LOG_EAVB(LEVEL_ERR, "fail to add output buffer, return %d\n",
  568. ret);
  569. mutex_unlock(&priv->lock);
  570. kfree(req);
  571. msg->txbuf = NULL;
  572. goto error;
  573. }
  574. virtqueue_kick(priv->svq);
  575. mutex_unlock(&priv->lock);
  576. rsv = wait_for_completion_timeout(&msg->work, msecs_to_jiffies(timeout_msec));
  577. if (rsv == 0) {
  578. LOG_EAVB(LEVEL_ERR, "msgid %d timeout\n", msg->msgid);
  579. ret = -ETIMEDOUT;
  580. goto error;
  581. }
  582. rsp = (struct vio_msg_hdr *)msg->rxbuf;
  583. msgid_rsp = msg->msgid;
  584. if (msgid_req != msgid_rsp) {
  585. LOG_EAVB(LEVEL_ERR, "msgid mismatch, msgid_req %d, msgid_rsp %d\n",
  586. msgid_req, msgid_rsp);
  587. ret = -EFAULT;
  588. goto error;
  589. }
  590. if (rsp) {
  591. cmd_rsp = rsp->cmd;
  592. stream_idx_rsp = rsp->stream_idx;
  593. if (rsp->result) {
  594. LOG_EAVB(LEVEL_ERR, "[eavb#%d] unexpected rsp: msgid %d, cmd %s, ret %d\n",
  595. stream_idx_req, msgid_req, cmd2str(cmd_req), rsp->result);
  596. ret = -EINVAL;
  597. }
  598. } else {
  599. LOG_EAVB(LEVEL_ERR, "fail to get rsp buffer, msgid %d\n",
  600. msgid_rsp);
  601. ret = -ENOMEM;
  602. goto error;
  603. }
  604. if ((ret != 0) ||
  605. ((cmd_rsp != VIRTIO_EAVB_T_RECEIVE) && (cmd_rsp != VIRTIO_EAVB_T_TRANSMIT)))
  606. LOG_EAVB(LEVEL_INFO, "[eavb#%d] response: msgid %d, cmd %s, ret %d\n",
  607. stream_idx_rsp, msgid_rsp, cmd2str(cmd_rsp), ret);
  608. return ret;
  609. error:
  610. LOG_EAVB(LEVEL_ERR, "[eavb#%d] REQ/RSP ERROR: msgid %d, cmd %s, ret %d\n",
  611. stream_idx_req, msgid_req, cmd2str(cmd_req), ret);
  612. return ret;
  613. }
  614. static int vio_eavb_disconnect(struct eavb_file *fl, struct stream *stream)
  615. {
  616. struct virtio_eavb_priv *priv = fl->priv;
  617. struct fe_msg *msg;
  618. struct vio_msg_hdr *vhdr;
  619. int tsize, rsize;
  620. int ret;
  621. tsize = rsize = sizeof(struct vio_disconnect_stream_msg);
  622. msg = virt_alloc_msg(priv, tsize, rsize);
  623. if (!msg) {
  624. LOG_EAVB(LEVEL_ERR, "stream%d alloc msg fail!\n",
  625. stream->index);
  626. return -ENOMEM;
  627. }
  628. fill_vmsg_hdr(msg, stream, VIRTIO_EAVB_T_DISCONNECT_STREAM);
  629. vhdr = (struct vio_msg_hdr *)msg->txbuf;
  630. LOG_EAVB(LEVEL_DEBUG, "(%d:%d:%d)\n",
  631. fl->index, stream->index, msg->msgid);
  632. LOG_EAVB(LEVEL_DEBUG, "stream%d (ctx 0x%llx, idx %d)\n",
  633. stream->index, vhdr->streamctx_hdl, vhdr->stream_idx);
  634. ret = send_msg(priv, msg);
  635. LOG_EAVB(LEVEL_DEBUG, "(%d:%d:%d) ret=%d\n",
  636. fl->index, stream->index, msg->msgid, ret);
  637. vhdr = (struct vio_msg_hdr *)msg->rxbuf;
  638. if (!ret && vhdr) {
  639. ret = vhdr->result;
  640. stream->status = DISCONNECTED;
  641. }
  642. virt_free_msg(priv, msg);
  643. return 0;
  644. }
  645. static int vio_eavb_destroy(struct eavb_file *fl, struct stream *stream)
  646. {
  647. struct virtio_eavb_priv *priv = fl->priv;
  648. struct fe_msg *msg;
  649. struct vio_msg_hdr *vhdr;
  650. int tsize, rsize;
  651. int ret;
  652. tsize = rsize = sizeof(struct vio_destroy_stream_msg);
  653. msg = virt_alloc_msg(priv, tsize, rsize);
  654. if (!msg) {
  655. LOG_EAVB(LEVEL_ERR, "stream%d alloc msg fail!\n",
  656. stream->index);
  657. return -ENOMEM;
  658. }
  659. fill_vmsg_hdr(msg, stream, VIRTIO_EAVB_T_DESTROY_STREAM);
  660. vhdr = (struct vio_msg_hdr *)msg->txbuf;
  661. LOG_EAVB(LEVEL_DEBUG, "(%d:%d:%d)\n",
  662. fl->index, stream->index, msg->msgid);
  663. ret = send_msg(priv, msg);
  664. LOG_EAVB(LEVEL_DEBUG, "(%d:%d:%d) ret=%d\n",
  665. fl->index, stream->index, msg->msgid, ret);
  666. vhdr = (struct vio_msg_hdr *)msg->rxbuf;
  667. if (!ret && vhdr) {
  668. ret = vhdr->result;
  669. destroy_dbugfs_stream(stream);
  670. reset_stream(stream);
  671. stream->status = DESTROYED;
  672. }
  673. virt_free_msg(priv, msg);
  674. return 0;
  675. }
  676. static int virtio_eavb_open(struct inode *inode, struct file *file)
  677. {
  678. struct cdev *cdev = inode->i_cdev;
  679. struct virtio_eavb_priv *priv;
  680. struct eavb_file *fl;
  681. priv = container_of(cdev, struct virtio_eavb_priv, cdev);
  682. fl = kzalloc(sizeof(*fl), GFP_KERNEL);
  683. if (!fl) {
  684. LOG_EAVB(LEVEL_ERR, "malloc fail\n");
  685. return -ENOMEM;
  686. }
  687. memset(fl, 0, sizeof(*fl));
  688. fl->priv = priv;
  689. file->private_data = fl;
  690. spin_lock_init(&fl->mappinglock);
  691. spin_lock_init(&fl->streamlock);
  692. fl->index = priv->file_index++;
  693. LOG_EAVB(LEVEL_INFO, "fl->index=%d\n", fl->index);
  694. return 0;
  695. }
  696. static int virtio_eavb_release(struct inode *inode, struct file *file)
  697. {
  698. struct eavb_file *fl = file->private_data;
  699. int i;
  700. LOG_EAVB(LEVEL_INFO, "fl->index=%d\n", fl->index);
  701. for (i = 0; i < MAX_STREAM_NUM; i++) {
  702. struct stream *stream = &fl->streams[i];
  703. LOG_EAVB(LEVEL_INFO, "[%d], status %d\n",
  704. i, stream->status);
  705. if (stream->status == CONNECTED)
  706. vio_eavb_disconnect(fl, stream);
  707. if (stream->status == DISCONNECTED
  708. || stream->status == CREATED) {
  709. vio_eavb_destroy(fl, stream);
  710. }
  711. }
  712. file->private_data = NULL;
  713. for (i = 0; i < MAPPING_MAX; i++) {
  714. if (fl->mapping[i].va)
  715. kfree((void *)fl->mapping[i].va);
  716. }
  717. memset(fl->mapping, 0, sizeof(fl->mapping));
  718. fl->mapping_count = 0;
  719. LOG_EAVB(LEVEL_INFO, "fl->index=%d\n", fl->index);
  720. kfree(fl);
  721. return 0;
  722. }
  723. static int qavb_create_stream(struct eavb_file *fl, void __user *buf)
  724. {
  725. struct virtio_eavb_priv *priv = fl->priv;
  726. struct fe_msg *msg;
  727. struct vio_msg_hdr *vhdr;
  728. struct vio_create_stream_msg *vmsg;
  729. struct eavb_ioctl_create_stream create;
  730. int tsize, rsize;
  731. struct stream *stream;
  732. int ret;
  733. if (copy_from_user(&create, buf, sizeof(create))) {
  734. LOG_EAVB(LEVEL_ERR, "copy_from_user failed\n");
  735. return -EFAULT;
  736. }
  737. LOG_EAVB(LEVEL_INFO, "fl->index=%d\n", fl->index);
  738. stream = getStream(fl, 0);
  739. if (!stream) {
  740. LOG_EAVB(LEVEL_ERR, "stream full\n");
  741. return -EINVAL;
  742. }
  743. tsize = rsize = sizeof(struct vio_create_stream_msg);
  744. msg = virt_alloc_msg(priv, tsize, rsize);
  745. if (!msg) {
  746. LOG_EAVB(LEVEL_ERR, "stream%d alloc msg fail!\n",
  747. stream->index);
  748. reset_stream(stream);
  749. return -ENOMEM;
  750. }
  751. vhdr = (struct vio_msg_hdr *)msg->txbuf;
  752. vhdr->cmd = VIRTIO_EAVB_T_CREATE_STREAM;
  753. vhdr->len = msg->txbuf_size;
  754. ASSERT(sizeof(vmsg->cfg) == sizeof(create.config));
  755. vmsg = (struct vio_create_stream_msg *)vhdr;
  756. memcpy(&vmsg->cfg, &create.config, sizeof(vmsg->cfg));
  757. LOG_EAVB(LEVEL_DEBUG, "(%d:%d:%d)\n",
  758. fl->index, stream->index, msg->msgid);
  759. ret = send_msg(priv, msg);
  760. LOG_EAVB(LEVEL_DEBUG, "(%d:%d:%d) ret=%d\n",
  761. fl->index, stream->index, msg->msgid, ret);
  762. vhdr = (struct vio_msg_hdr *)msg->rxbuf;
  763. if (!ret && vhdr) {
  764. ret = vhdr->result;
  765. create.hdr.streamCtx = vhdr->streamctx_hdl;
  766. stream->hdl = vhdr->streamctx_hdl;
  767. stream->idx = vhdr->stream_idx;
  768. stream->status = CREATED;
  769. stream->fl = fl;
  770. LOG_EAVB(LEVEL_INFO, "stream%d (ctx 0x%llx, idx %d)\n",
  771. stream->index, vhdr->streamctx_hdl, vhdr->stream_idx);
  772. create_dbugfs_stream(stream);
  773. } else {
  774. reset_stream(stream);
  775. }
  776. virt_free_msg(priv, msg);
  777. if (copy_to_user(buf, &create, sizeof(create))) {
  778. LOG_EAVB(LEVEL_ERR, "copy_to_user failed\n");
  779. return -EFAULT;
  780. }
  781. return ret;
  782. }
  783. static int qavb_create_stream_with_path(struct eavb_file *fl, void __user *buf)
  784. {
  785. struct virtio_eavb_priv *priv = fl->priv;
  786. struct fe_msg *msg;
  787. struct vio_msg_hdr *vhdr;
  788. struct vio_create_stream_path_msg *vmsg;
  789. struct eavb_ioctl_create_stream_with_path create;
  790. int tsize, rsize;
  791. struct stream *stream;
  792. int ret;
  793. if (copy_from_user(&create, buf, sizeof(create))) {
  794. LOG_EAVB(LEVEL_ERR, "copy_from_user failed\n");
  795. return -EFAULT;
  796. }
  797. LOG_EAVB(LEVEL_INFO, "fl->index=%d\n", fl->index);
  798. LOG_EAVB(LEVEL_INFO, "M - DRIVER EAVB FE create stream\n");
  799. stream = getStream(fl, 0);
  800. if (!stream) {
  801. LOG_EAVB(LEVEL_ERR, "stream full\n");
  802. return -EINVAL;
  803. }
  804. tsize = rsize = sizeof(struct vio_create_stream_path_msg);
  805. msg = virt_alloc_msg(priv, tsize, rsize);
  806. if (!msg) {
  807. LOG_EAVB(LEVEL_ERR, "stream%d alloc msg fail!\n",
  808. stream->index);
  809. reset_stream(stream);
  810. return -ENOMEM;
  811. }
  812. vhdr = (struct vio_msg_hdr *)msg->txbuf;
  813. vhdr->cmd = VIRTIO_EAVB_T_CREATE_STREAM_PATH;
  814. vhdr->len = msg->txbuf_size;
  815. vmsg = (struct vio_create_stream_path_msg *)vhdr;
  816. memcpy(vmsg->path, create.path, MAX_CONFIG_FILE_PATH);
  817. LOG_EAVB(LEVEL_DEBUG, "(%d:%d:%d)\n",
  818. fl->index, stream->index, msg->msgid);
  819. ret = send_msg(priv, msg);
  820. LOG_EAVB(LEVEL_DEBUG, "(%d:%d:%d) ret=%d\n",
  821. fl->index, stream->index, msg->msgid, ret);
  822. vhdr = (struct vio_msg_hdr *)msg->rxbuf;
  823. if (!ret && vhdr) {
  824. ret = vhdr->result;
  825. create.hdr.streamCtx = vhdr->streamctx_hdl;
  826. stream->hdl = vhdr->streamctx_hdl;
  827. stream->idx = vhdr->stream_idx;
  828. stream->status = CREATED;
  829. stream->fl = fl;
  830. LOG_EAVB(LEVEL_INFO, "stream%d (ctx 0x%llx, idx %d)\n",
  831. stream->index, vhdr->streamctx_hdl, vhdr->stream_idx);
  832. LOG_EAVB(LEVEL_INFO, "M - DRIVER EAVB FE create stream success\n");
  833. create_dbugfs_stream(stream);
  834. } else {
  835. reset_stream(stream);
  836. }
  837. virt_free_msg(priv, msg);
  838. if (copy_to_user(buf, &create, sizeof(create))) {
  839. LOG_EAVB(LEVEL_ERR, "copy_to_user failed\n");
  840. return -EFAULT;
  841. }
  842. return ret;
  843. }
  844. static int qavb_destroy_stream(struct eavb_file *fl, void __user *buf)
  845. {
  846. struct eavb_ioctl_destroy_stream destroy;
  847. struct stream *stream;
  848. int ret;
  849. if (copy_from_user(&destroy, buf, sizeof(destroy))) {
  850. LOG_EAVB(LEVEL_ERR, "copy_from_user failed\n");
  851. return -EFAULT;
  852. }
  853. stream = getStream(fl, destroy.hdr.streamCtx);
  854. if (!stream) {
  855. LOG_EAVB(LEVEL_ERR, "invalid streamCtx=0x%llx\n",
  856. destroy.hdr.streamCtx);
  857. return -EINVAL;
  858. }
  859. ret = vio_eavb_destroy(fl, stream);
  860. return ret;
  861. }
  862. static int qavb_get_stream_info(struct eavb_file *fl, void __user *buf)
  863. {
  864. struct virtio_eavb_priv *priv = fl->priv;
  865. struct fe_msg *msg;
  866. struct vio_msg_hdr *vhdr;
  867. struct eavb_ioctl_get_stream_info get_info;
  868. int tsize, rsize;
  869. struct stream *stream;
  870. int ret;
  871. if (copy_from_user(&get_info, buf, sizeof(get_info))) {
  872. LOG_EAVB(LEVEL_ERR, "copy_from_user failed\n");
  873. return -EFAULT;
  874. }
  875. LOG_EAVB(LEVEL_INFO, "streamCtx=0x%llx\n", get_info.hdr.streamCtx);
  876. LOG_EAVB(LEVEL_INFO, "M - DRIVER EAVB FE get stream info\n");
  877. stream = getStream(fl, get_info.hdr.streamCtx);
  878. if (!stream) {
  879. LOG_EAVB(LEVEL_ERR, "invalid streamCtx=0x%llx\n",
  880. get_info.hdr.streamCtx);
  881. return -EINVAL;
  882. }
  883. tsize = rsize = sizeof(struct vio_get_stream_info_msg);
  884. msg = virt_alloc_msg(priv, tsize, rsize);
  885. if (!msg) {
  886. LOG_EAVB(LEVEL_ERR, "stream%d alloc msg fail!\n",
  887. stream->index);
  888. return -ENOMEM;
  889. }
  890. fill_vmsg_hdr(msg, stream, VIRTIO_EAVB_T_GET_STREAM_INFO);
  891. vhdr = (struct vio_msg_hdr *)msg->txbuf;
  892. LOG_EAVB(LEVEL_DEBUG, "(%d:%d:%d)\n",
  893. fl->index, stream->index, msg->msgid);
  894. ret = send_msg(priv, msg);
  895. LOG_EAVB(LEVEL_DEBUG, "(%d:%d:%d) ret=%d\n",
  896. fl->index, stream->index, msg->msgid, ret);
  897. vhdr = (struct vio_msg_hdr *)msg->rxbuf;
  898. if (!ret && vhdr) {
  899. struct eavb_stream_info *info;
  900. ret = vhdr->result;
  901. info = (struct eavb_stream_info *)(vhdr + 1);
  902. ASSERT(sizeof(get_info.info) == sizeof(*info));
  903. memcpy(&get_info.info, info, sizeof(*info));
  904. LOG_EAVB(LEVEL_INFO, "M - DRIVER EAVB FE get stream info success\n");
  905. }
  906. virt_free_msg(priv, msg);
  907. if (copy_to_user(buf, &get_info, sizeof(get_info))) {
  908. LOG_EAVB(LEVEL_ERR, "copy_to_user failed\n");
  909. return -EFAULT;
  910. }
  911. return ret;
  912. }
  913. static int qavb_connect_stream(struct eavb_file *fl, void __user *buf)
  914. {
  915. struct virtio_eavb_priv *priv = fl->priv;
  916. struct fe_msg *msg;
  917. struct vio_msg_hdr *vhdr;
  918. struct eavb_ioctl_connect_stream connect;
  919. int tsize, rsize;
  920. struct stream *stream;
  921. int ret;
  922. if (copy_from_user(&connect, buf, sizeof(connect))) {
  923. LOG_EAVB(LEVEL_ERR, "copy_from_user failed\n");
  924. return -EFAULT;
  925. }
  926. LOG_EAVB(LEVEL_INFO, "streamCtx=0x%llx\n", connect.hdr.streamCtx);
  927. LOG_EAVB(LEVEL_INFO, "M - DRIVER EAVB FE connect stream\n");
  928. stream = getStream(fl, connect.hdr.streamCtx);
  929. if (!stream) {
  930. LOG_EAVB(LEVEL_ERR, "invalid streamCtx=0x%llx\n",
  931. connect.hdr.streamCtx);
  932. return -EINVAL;
  933. }
  934. tsize = rsize = sizeof(struct vio_connect_stream_msg);
  935. msg = virt_alloc_msg(priv, tsize, rsize);
  936. if (!msg) {
  937. LOG_EAVB(LEVEL_ERR, "stream%d alloc msg fail!\n",
  938. stream->index);
  939. return -ENOMEM;
  940. }
  941. fill_vmsg_hdr(msg, stream, VIRTIO_EAVB_T_CONNECT_STREAM);
  942. vhdr = (struct vio_msg_hdr *)msg->txbuf;
  943. LOG_EAVB(LEVEL_DEBUG, "(%d:%d:%d)\n",
  944. fl->index, stream->index, msg->msgid);
  945. LOG_EAVB(LEVEL_DEBUG, "stream%d (ctx 0x%llx, idx %d)\n",
  946. stream->index, vhdr->streamctx_hdl, vhdr->stream_idx);
  947. ret = send_msg(priv, msg);
  948. LOG_EAVB(LEVEL_DEBUG, "(%d:%d:%d) ret=%d\n",
  949. fl->index, stream->index, msg->msgid, ret);
  950. vhdr = (struct vio_msg_hdr *)msg->rxbuf;
  951. if (!ret && vhdr) {
  952. ret = vhdr->result;
  953. stream->status = CONNECTED;
  954. LOG_EAVB(LEVEL_INFO, "M - DRIVER EAVB FE connect stream success\n");
  955. }
  956. virt_free_msg(priv, msg);
  957. return ret;
  958. }
  959. static int qavb_disconnect_stream(struct eavb_file *fl, void __user *buf)
  960. {
  961. struct eavb_ioctl_disconnect_stream disconnect;
  962. struct stream *stream;
  963. int ret;
  964. if (copy_from_user(&disconnect, buf, sizeof(disconnect))) {
  965. LOG_EAVB(LEVEL_ERR, "copy_from_user failed\n");
  966. return -EFAULT;
  967. }
  968. LOG_EAVB(LEVEL_INFO, "streamCtx=0x%llx\n", disconnect.hdr.streamCtx);
  969. stream = getStream(fl, disconnect.hdr.streamCtx);
  970. if (!stream) {
  971. LOG_EAVB(LEVEL_ERR, "invalid streamCtx=0x%llx\n",
  972. disconnect.hdr.streamCtx);
  973. return -EINVAL;
  974. }
  975. ret = vio_eavb_disconnect(fl, stream);
  976. return ret;
  977. }
  978. static int qavb_receive(struct eavb_file *fl, void __user *buf)
  979. {
  980. struct virtio_eavb_priv *priv = fl->priv;
  981. struct fe_msg *msg;
  982. struct vio_msg_hdr *vhdr;
  983. struct vio_receive_msg *vmsg;
  984. struct eavb_ioctl_receive receive;
  985. int tsize, rsize;
  986. struct mapping *mapping;
  987. struct stream *stream;
  988. int ret;
  989. if (copy_from_user(&receive, buf, sizeof(receive))) {
  990. LOG_EAVB(LEVEL_ERR, "copy_from_user failed\n");
  991. return -EFAULT;
  992. }
  993. mapping = get_file_mapping(fl, receive.data.pbuf,
  994. receive.data.hdr.payload_size);
  995. if (!mapping) {
  996. LOG_EAVB(LEVEL_ERR, "invalid buf address 0x%llx\n",
  997. receive.data.pbuf);
  998. return -EINVAL;
  999. }
  1000. stream = getStream(fl, receive.hdr.streamCtx);
  1001. if (!stream) {
  1002. LOG_EAVB(LEVEL_ERR, "invalid streamCtx=0x%llx\n",
  1003. receive.hdr.streamCtx);
  1004. return -EINVAL;
  1005. }
  1006. tsize = rsize = sizeof(struct vio_receive_msg);
  1007. msg = virt_alloc_msg(priv, tsize, rsize);
  1008. if (!msg) {
  1009. LOG_EAVB(LEVEL_ERR, "stream%d alloc msg fail!\n",
  1010. stream->index);
  1011. return -ENOMEM;
  1012. }
  1013. fill_vmsg_hdr(msg, stream, VIRTIO_EAVB_T_RECEIVE);
  1014. vhdr = (struct vio_msg_hdr *)msg->txbuf;
  1015. vmsg = (struct vio_receive_msg *)vhdr;
  1016. ASSERT(sizeof(vmsg->data) == sizeof(receive.data));
  1017. memcpy(&vmsg->data, &receive.data, sizeof(vmsg->data));
  1018. vmsg->data.gpa = get_mapping_phyaddr(mapping, receive.data.pbuf);
  1019. LOG_EAVB(LEVEL_DEBUG, "pass phy address 0x%llx\n", vmsg->data.gpa);
  1020. LOG_EAVB(LEVEL_DEBUG, "stream%d (ctx 0x%llx, idx %d)\n",
  1021. stream->index, vhdr->streamctx_hdl, vhdr->stream_idx);
  1022. msg->ts_begin = local_clock();
  1023. ret = send_msg(priv, msg);
  1024. msg->ts_end = local_clock();
  1025. records(stream, msg->msgid, msg->ts_begin, msg->ts_end);
  1026. vhdr = (struct vio_msg_hdr *)msg->rxbuf;
  1027. if (!ret && vhdr) {
  1028. ret = vhdr->result;
  1029. vmsg = (struct vio_receive_msg *)vhdr;
  1030. receive.received = vmsg->received;
  1031. memcpy(&receive.data, &vmsg->data,
  1032. sizeof(struct eavb_buf_data));
  1033. if (receive.received)
  1034. LOG_EAVB(LEVEL_DEBUG, "M - DRIVER EAVB FE First received data\n");
  1035. }
  1036. virt_free_msg(priv, msg);
  1037. if (copy_to_user(buf, &receive, sizeof(receive))) {
  1038. LOG_EAVB(LEVEL_ERR, "copy_to_user failed\n");
  1039. return -EFAULT;
  1040. }
  1041. return ret;
  1042. }
  1043. static int qavb_recv_done(struct eavb_file *fl, void __user *buf)
  1044. {
  1045. return 0;
  1046. }
  1047. static int qavb_transmit(struct eavb_file *fl, void __user *buf)
  1048. {
  1049. struct virtio_eavb_priv *priv = fl->priv;
  1050. struct fe_msg *msg;
  1051. struct vio_msg_hdr *vhdr;
  1052. struct vio_transmit_msg *vmsg;
  1053. struct eavb_ioctl_transmit transmit;
  1054. int tsize, rsize;
  1055. struct mapping *mapping;
  1056. struct stream *stream;
  1057. int ret;
  1058. if (copy_from_user(&transmit, buf, sizeof(transmit))) {
  1059. LOG_EAVB(LEVEL_ERR, "copy_from_user failed\n");
  1060. return -EFAULT;
  1061. }
  1062. mapping = get_file_mapping(fl, transmit.data.pbuf,
  1063. transmit.data.hdr.payload_size);
  1064. if (!mapping) {
  1065. LOG_EAVB(LEVEL_ERR, "invalid buf address 0x%llx\n",
  1066. transmit.data.pbuf);
  1067. return -EINVAL;
  1068. }
  1069. stream = getStream(fl, transmit.hdr.streamCtx);
  1070. if (!stream) {
  1071. LOG_EAVB(LEVEL_ERR, "invalid streamCtx=0x%llx\n",
  1072. transmit.hdr.streamCtx);
  1073. return -EINVAL;
  1074. }
  1075. tsize = rsize = sizeof(struct vio_transmit_msg);
  1076. msg = virt_alloc_msg(priv, tsize, rsize);
  1077. if (!msg) {
  1078. LOG_EAVB(LEVEL_ERR, "stream%d alloc msg fail!\n",
  1079. stream->index);
  1080. return -ENOMEM;
  1081. }
  1082. fill_vmsg_hdr(msg, stream, VIRTIO_EAVB_T_TRANSMIT);
  1083. vhdr = (struct vio_msg_hdr *)msg->txbuf;
  1084. vmsg = (struct vio_transmit_msg *)vhdr;
  1085. ASSERT(sizeof(vmsg->data) == sizeof(transmit.data));
  1086. memcpy(&vmsg->data, &transmit.data, sizeof(vmsg->data));
  1087. vmsg->mapping_size = mapping->size;
  1088. vmsg->data.gpa = get_mapping_phyaddr(mapping, transmit.data.pbuf);
  1089. LOG_EAVB(LEVEL_DEBUG, "pass phy address 0x%llx\n", vmsg->data.gpa);
  1090. LOG_EAVB(LEVEL_DEBUG, "stream%d (ctx 0x%llx, idx %d)\n",
  1091. stream->index, vhdr->streamctx_hdl, vhdr->stream_idx);
  1092. ret = send_msg(priv, msg);
  1093. vhdr = (struct vio_msg_hdr *)msg->rxbuf;
  1094. if (!ret && vhdr) {
  1095. ret = vhdr->result;
  1096. vmsg = (struct vio_transmit_msg *)vhdr;
  1097. transmit.written = vmsg->written;
  1098. }
  1099. virt_free_msg(priv, msg);
  1100. if (copy_to_user(buf, &transmit, sizeof(transmit))) {
  1101. LOG_EAVB(LEVEL_ERR, "copy_to_user failed\n");
  1102. return -EFAULT;
  1103. }
  1104. return ret;
  1105. }
  1106. static int qavb_get_crf_ts(struct eavb_file *fl, void __user *buf)
  1107. {
  1108. struct virtio_eavb_priv *priv = fl->priv;
  1109. u64 value;
  1110. init_completion(&priv->crf_ts_update);
  1111. wait_for_completion(&priv->crf_ts_update);
  1112. value = priv->crf_ts;
  1113. if (copy_to_user(buf, &value, sizeof(value))) {
  1114. LOG_EAVB(LEVEL_ERR, "copy_to_user failed\n");
  1115. return -EFAULT;
  1116. }
  1117. return 0;
  1118. }
  1119. void update_crf_ts(struct virtio_eavb_priv *priv, u64 value)
  1120. {
  1121. LOG_EAVB(LEVEL_DEBUG, "update crf clock %llu\n", value);
  1122. priv->crf_ts = value;
  1123. complete_all(&priv->crf_ts_update);
  1124. }
  1125. static long virtio_eavb_ioctl(struct file *file, unsigned int ioctl_cmd,
  1126. unsigned long ioctl_param)
  1127. {
  1128. struct eavb_file *fl = file->private_data;
  1129. void __user *buf = (void __user *)ioctl_param;
  1130. int ret = 0;
  1131. switch (ioctl_cmd) {
  1132. case EAVB_IOCTL_CREATE_STREAM:
  1133. ret = qavb_create_stream(fl, buf);
  1134. break;
  1135. case EAVB_IOCTL_CREATE_STREAM_WITH_PATH:
  1136. ret = qavb_create_stream_with_path(fl, buf);
  1137. break;
  1138. case EAVB_IOCTL_GET_STREAM_INFO:
  1139. ret = qavb_get_stream_info(fl, buf);
  1140. break;
  1141. case EAVB_IOCTL_CONNECT_STREAM:
  1142. ret = qavb_connect_stream(fl, buf);
  1143. break;
  1144. case EAVB_IOCTL_RECEIVE:
  1145. ret = qavb_receive(fl, buf);
  1146. break;
  1147. case EAVB_IOCTL_RECV_DONE:
  1148. ret = qavb_recv_done(fl, buf);
  1149. break;
  1150. case EAVB_IOCTL_TRANSMIT:
  1151. ret = qavb_transmit(fl, buf);
  1152. break;
  1153. case EAVB_IOCTL_DISCONNECT_STREAM:
  1154. ret = qavb_disconnect_stream(fl, buf);
  1155. break;
  1156. case EAVB_IOCTL_DESTROY_STREAM:
  1157. ret = qavb_destroy_stream(fl, buf);
  1158. break;
  1159. case EAVB_IOCTL_GET_CRF_TS:
  1160. ret = qavb_get_crf_ts(fl, buf);
  1161. break;
  1162. default:
  1163. ret = -ENOTTY;
  1164. LOG_EAVB(LEVEL_ERR, "unsupported ioctl 0x%x\n", ioctl_cmd);
  1165. }
  1166. return ret;
  1167. }
  1168. static long virtio_eavb_compat_ioctl(struct file *file,
  1169. unsigned int ioctl_cmd,
  1170. unsigned long ioctl_param)
  1171. {
  1172. return -EOPNOTSUPP;
  1173. }
  1174. static int virtio_eavb_mmap(struct file *file, struct vm_area_struct *vma)
  1175. {
  1176. struct eavb_file *fl = file->private_data;
  1177. int ret = 0;
  1178. unsigned long vsize;
  1179. u8 *buf;
  1180. spin_lock(&fl->mappinglock);
  1181. if (fl->mapping_count < MAPPING_MAX)
  1182. fl->mapping_count++;
  1183. else
  1184. ret = -EBUSY;
  1185. spin_unlock(&fl->mappinglock);
  1186. if (ret) {
  1187. LOG_EAVB(LEVEL_ERR, "Too many mapping (%d):fl->index %d\n",
  1188. fl->mapping_count, fl->index);
  1189. return ret;
  1190. }
  1191. vsize = vma->vm_end - vma->vm_start;
  1192. LOG_EAVB(LEVEL_DEBUG, "vsize = %ld(0x%lx~0x%lx)\n",
  1193. vsize, vma->vm_start, vma->vm_end);
  1194. buf = kmalloc(vsize, GFP_KERNEL);
  1195. if (!buf) {
  1196. spin_lock(&fl->mappinglock);
  1197. fl->mapping_count--;
  1198. spin_unlock(&fl->mappinglock);
  1199. LOG_EAVB(LEVEL_ERR, "kmalloc failed\n");
  1200. return -ENOMEM;
  1201. }
  1202. vm_flags_set(vma, VM_IO);
  1203. vm_flags_set(vma, VM_LOCKED);
  1204. if (remap_pfn_range(vma, vma->vm_start,
  1205. virt_to_phys(buf)>>PAGE_SHIFT, vsize, vma->vm_page_prot)) {
  1206. LOG_EAVB(LEVEL_ERR, "remap pfn range failed\n");
  1207. spin_lock(&fl->mappinglock);
  1208. fl->mapping_count--;
  1209. spin_unlock(&fl->mappinglock);
  1210. ret = -EAGAIN;
  1211. }
  1212. memset(buf, 0, vsize);
  1213. add_mapping_info(fl, (u64)buf, vma->vm_start, virt_to_phys(buf), vsize);
  1214. return ret;
  1215. }
  1216. static const struct file_operations fops = {
  1217. .open = virtio_eavb_open,
  1218. .release = virtio_eavb_release,
  1219. .unlocked_ioctl = virtio_eavb_ioctl,
  1220. .compat_ioctl = virtio_eavb_compat_ioctl,
  1221. .mmap = virtio_eavb_mmap,
  1222. };
  1223. static void fe_recv_done(struct virtqueue *rvq)
  1224. {
  1225. struct virtio_eavb_priv *priv = rvq->vdev->priv;
  1226. struct vio_msg_hdr *rxvhdr, *rsp;
  1227. struct fe_msg *msg;
  1228. unsigned int len;
  1229. unsigned long flags;
  1230. #ifdef EAVB_DEBUGFS
  1231. int index;
  1232. #endif
  1233. while (1) {
  1234. spin_lock_irqsave(&priv->rvqlock, flags);
  1235. rxvhdr = virtqueue_get_buf(rvq, &len);
  1236. spin_unlock_irqrestore(&priv->rvqlock, flags);
  1237. if (!rxvhdr)
  1238. break;
  1239. ASSERT(len == RX_BUF_MAX_LEN);
  1240. if (len < rxvhdr->len) {
  1241. LOG_EAVB(LEVEL_ERR,
  1242. "rxvhdr msgid %d len %d > received len %d!\n",
  1243. rxvhdr->msgid, rxvhdr->len, len);
  1244. continue;
  1245. }
  1246. if (rxvhdr->cmd == VIRTIO_EAVB_T_UPDATE_CLK) {
  1247. struct vio_update_clk_msg *vmsg;
  1248. vmsg = (struct vio_update_clk_msg *)rxvhdr;
  1249. update_crf_ts(priv, vmsg->clk);
  1250. reclaim_rxbuf(priv, rxvhdr);
  1251. continue;
  1252. }
  1253. rsp = rxvhdr;
  1254. if (rsp->msgid > FE_MSG_MAX - 1) {
  1255. LOG_EAVB(LEVEL_ERR, "rsp msgid %d is invalid!\n",
  1256. rsp->msgid);
  1257. continue;
  1258. }
  1259. msg = priv->msgtable[rsp->msgid];
  1260. if (!msg) {
  1261. LOG_EAVB(LEVEL_ERR, "rsp msgid %d found no msg!\n",
  1262. rsp->msgid);
  1263. continue;
  1264. }
  1265. if (msg->msgid != rsp->msgid) {
  1266. LOG_EAVB(LEVEL_ERR, "rsp msgid %d mismatch %d!\n",
  1267. rsp->msgid, msg->msgid);
  1268. continue;
  1269. }
  1270. msg->rxbuf = (void *)rsp;
  1271. msg->rxbuf_used = 1;
  1272. LOG_EAVB(LEVEL_DEBUG, "msgid %d rxbuf used\n", msg->msgid);
  1273. #ifdef EAVB_DEBUGFS
  1274. index = priv->next_rxrecords_index & 0x1f;
  1275. priv->rxrecords[index].msgid = msg->msgid;
  1276. priv->rxrecords[index].ts = local_clock();
  1277. priv->next_rxrecords_index++;
  1278. #endif
  1279. complete(&msg->work);
  1280. }
  1281. }
  1282. static int init_vqs(struct virtio_eavb_priv *priv)
  1283. {
  1284. struct virtqueue *vqs[2];
  1285. static const char *const names[] = { "eavb_tx", "eavb_rx" };
  1286. vq_callback_t *cbs[] = {NULL, fe_recv_done};
  1287. int ret;
  1288. ret = virtio_find_vqs(priv->vdev, 2, vqs, cbs, names, NULL);
  1289. if (ret) {
  1290. LOG_EAVB(LEVEL_ERR, "virtio_find_vqs fail\n");
  1291. return ret;
  1292. }
  1293. priv->svq = vqs[0];
  1294. priv->rvq = vqs[1];
  1295. return 0;
  1296. }
  1297. static int virtio_eavb_probe(struct virtio_device *vdev)
  1298. {
  1299. int ret;
  1300. struct device *dev = NULL;
  1301. struct virtio_eavb_priv *priv;
  1302. int i;
  1303. LOG_EAVB(LEVEL_DEBUG, "\n");
  1304. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1305. if (!priv) {
  1306. ret = -ENOMEM;
  1307. goto fail;
  1308. }
  1309. if (virtio_has_feature(vdev, VIRTIO_EAVB_F_SHMEM))
  1310. priv->has_shmem = true;
  1311. mutex_init(&priv->lock);
  1312. spin_lock_init(&priv->msglock);
  1313. spin_lock_init(&priv->rvqlock);
  1314. INIT_WORK(&priv->reclaim_work, reclaim_function);
  1315. init_completion(&priv->crf_ts_update);
  1316. vdev->priv = priv;
  1317. priv->vdev = vdev;
  1318. priv->dev = vdev->dev.parent;
  1319. priv->name = "eavb";
  1320. ret = init_vqs(priv);
  1321. if (ret)
  1322. goto free_priv;
  1323. virtio_device_ready(vdev);
  1324. ret = alloc_chrdev_region(&priv->dev_no, 0, DEVICE_NUM, DEVICE_NAME);
  1325. if (ret)
  1326. goto free_priv;
  1327. cdev_init(&priv->cdev, &fops);
  1328. priv->cdev.owner = THIS_MODULE;
  1329. ret = cdev_add(&priv->cdev, MKDEV(MAJOR(priv->dev_no), 0), DEVICE_NUM);
  1330. if (ret)
  1331. goto free_chrdev;
  1332. priv->class = class_create(THIS_MODULE, "virt-eavb");
  1333. if (IS_ERR(priv->class))
  1334. goto class_create_fail;
  1335. dev = device_create(priv->class, NULL,
  1336. MKDEV(MAJOR(priv->dev_no), MINOR_NUM_DEV),
  1337. NULL, DEVICE_NAME);
  1338. if (IS_ERR_OR_NULL(dev))
  1339. goto device_create_fail;
  1340. priv->rxbufs[0] = kmalloc(FE_MSG_MAX * RX_BUF_MAX_LEN, GFP_KERNEL);
  1341. if (!priv->rxbufs[0]) {
  1342. ret = -ENOMEM;
  1343. goto alloc_rxbufs_fail;
  1344. }
  1345. for (i = 0; i < FE_MSG_MAX; i++) {
  1346. struct scatterlist sg;
  1347. u8 *rxbuf;
  1348. rxbuf = priv->rxbufs[0] + i * RX_BUF_MAX_LEN;
  1349. priv->rxbufs[i] = rxbuf;
  1350. sg_init_one(&sg, rxbuf, RX_BUF_MAX_LEN);
  1351. ret = virtqueue_add_inbuf(priv->rvq, &sg, 1, rxbuf, GFP_KERNEL);
  1352. WARN_ON(ret);
  1353. }
  1354. virtqueue_disable_cb(priv->svq);
  1355. virtqueue_enable_cb(priv->rvq);
  1356. virtqueue_kick(priv->rvq);
  1357. #ifdef EAVB_DEBUGFS
  1358. priv->debugfs_root = debugfs_create_dir("eavb", NULL);
  1359. debugfs_create_file("log", 00400 | 00200,
  1360. priv->debugfs_root, priv,
  1361. &fops_debugfs_log);
  1362. debugfs_create_file("timeout", 00400 | 00200,
  1363. priv->debugfs_root, NULL,
  1364. &fops_debugfs_timeout);
  1365. #endif
  1366. LOG_EAVB(LEVEL_INFO, "M - DRIVER EAVB FE Ready\n");
  1367. return 0;
  1368. alloc_rxbufs_fail:
  1369. device_destroy(priv->class, MKDEV(MAJOR(priv->dev_no), MINOR_NUM_DEV));
  1370. device_create_fail:
  1371. if (!IS_ERR_OR_NULL(dev))
  1372. device_destroy(priv->class, MKDEV(MAJOR(priv->dev_no),
  1373. MINOR_NUM_DEV));
  1374. class_destroy(priv->class);
  1375. class_create_fail:
  1376. cdev_del(&priv->cdev);
  1377. free_chrdev:
  1378. unregister_chrdev_region(priv->dev_no, DEVICE_NUM);
  1379. free_priv:
  1380. kfree(priv);
  1381. fail:
  1382. return ret;
  1383. }
  1384. static void virtio_eavb_remove(struct virtio_device *vdev)
  1385. {
  1386. struct virtio_eavb_priv *priv;
  1387. LOG_EAVB(LEVEL_DEBUG, "\n");
  1388. priv = vdev->priv;
  1389. #ifdef EAVB_DEBUGFS
  1390. debugfs_remove_recursive(priv->debugfs_root);
  1391. #endif
  1392. device_destroy(priv->class, MKDEV(MAJOR(priv->dev_no), MINOR_NUM_DEV));
  1393. class_destroy(priv->class);
  1394. cdev_del(&priv->cdev);
  1395. unregister_chrdev_region(priv->dev_no, DEVICE_NUM);
  1396. vdev->config->reset(vdev);
  1397. vdev->config->del_vqs(vdev);
  1398. kfree(priv->rxbufs[0]);
  1399. kfree(priv);
  1400. }
  1401. static const struct virtio_device_id id_table[] = {
  1402. { VIRTIO_ID_EAVB, VIRTIO_DEV_ANY_ID },
  1403. { VIRTIO_ID_EAVB_BC, VIRTIO_DEV_ANY_ID },
  1404. { 0 },
  1405. };
  1406. static unsigned int features[] = {
  1407. VIRTIO_EAVB_F_SHMEM,
  1408. };
  1409. static struct virtio_driver virtio_eavb_driver = {
  1410. .feature_table = features,
  1411. .feature_table_size = ARRAY_SIZE(features),
  1412. .driver.name = KBUILD_MODNAME,
  1413. .driver.owner = THIS_MODULE,
  1414. .id_table = id_table,
  1415. .probe = virtio_eavb_probe,
  1416. .remove = virtio_eavb_remove,
  1417. };
  1418. static int __init virtio_eavb_init(void)
  1419. {
  1420. LOG_EAVB(LEVEL_INFO, "M - DRIVER EAVB FE Init\n");
  1421. return register_virtio_driver(&virtio_eavb_driver);
  1422. }
  1423. static void __exit virtio_eavb_exit(void)
  1424. {
  1425. unregister_virtio_driver(&virtio_eavb_driver);
  1426. }
  1427. module_init(virtio_eavb_init);
  1428. module_exit(virtio_eavb_exit);
  1429. MODULE_DESCRIPTION("Virtio eavb driver");
  1430. MODULE_LICENSE("GPL");