bcm_vk_msg.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2018-2020 Broadcom.
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/fs.h>
  7. #include <linux/hash.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/list.h>
  10. #include <linux/module.h>
  11. #include <linux/poll.h>
  12. #include <linux/sizes.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/timer.h>
  15. #include "bcm_vk.h"
  16. #include "bcm_vk_msg.h"
  17. #include "bcm_vk_sg.h"
  18. /* functions to manipulate the transport id in msg block */
  19. #define BCM_VK_MSG_Q_SHIFT 4
  20. #define BCM_VK_MSG_Q_MASK 0xF
  21. #define BCM_VK_MSG_ID_MASK 0xFFF
  22. #define BCM_VK_DMA_DRAIN_MAX_MS 2000
  23. /* number x q_size will be the max number of msg processed per loop */
  24. #define BCM_VK_MSG_PROC_MAX_LOOP 2
  25. /* module parameter */
  26. static bool hb_mon = true;
  27. module_param(hb_mon, bool, 0444);
  28. MODULE_PARM_DESC(hb_mon, "Monitoring heartbeat continuously.\n");
  29. static int batch_log = 1;
  30. module_param(batch_log, int, 0444);
  31. MODULE_PARM_DESC(batch_log, "Max num of logs per batch operation.\n");
  32. static bool hb_mon_is_on(void)
  33. {
  34. return hb_mon;
  35. }
  36. static u32 get_q_num(const struct vk_msg_blk *msg)
  37. {
  38. u32 q_num = msg->trans_id & BCM_VK_MSG_Q_MASK;
  39. if (q_num >= VK_MSGQ_PER_CHAN_MAX)
  40. q_num = VK_MSGQ_NUM_DEFAULT;
  41. return q_num;
  42. }
  43. static void set_q_num(struct vk_msg_blk *msg, u32 q_num)
  44. {
  45. u32 trans_q;
  46. if (q_num >= VK_MSGQ_PER_CHAN_MAX)
  47. trans_q = VK_MSGQ_NUM_DEFAULT;
  48. else
  49. trans_q = q_num;
  50. msg->trans_id = (msg->trans_id & ~BCM_VK_MSG_Q_MASK) | trans_q;
  51. }
  52. static u32 get_msg_id(const struct vk_msg_blk *msg)
  53. {
  54. return ((msg->trans_id >> BCM_VK_MSG_Q_SHIFT) & BCM_VK_MSG_ID_MASK);
  55. }
  56. static void set_msg_id(struct vk_msg_blk *msg, u32 val)
  57. {
  58. msg->trans_id = (val << BCM_VK_MSG_Q_SHIFT) | get_q_num(msg);
  59. }
  60. static u32 msgq_inc(const struct bcm_vk_sync_qinfo *qinfo, u32 idx, u32 inc)
  61. {
  62. return ((idx + inc) & qinfo->q_mask);
  63. }
  64. static
  65. struct vk_msg_blk __iomem *msgq_blk_addr(const struct bcm_vk_sync_qinfo *qinfo,
  66. u32 idx)
  67. {
  68. return qinfo->q_start + (VK_MSGQ_BLK_SIZE * idx);
  69. }
  70. static u32 msgq_occupied(const struct bcm_vk_msgq __iomem *msgq,
  71. const struct bcm_vk_sync_qinfo *qinfo)
  72. {
  73. u32 wr_idx, rd_idx;
  74. wr_idx = readl_relaxed(&msgq->wr_idx);
  75. rd_idx = readl_relaxed(&msgq->rd_idx);
  76. return ((wr_idx - rd_idx) & qinfo->q_mask);
  77. }
  78. static
  79. u32 msgq_avail_space(const struct bcm_vk_msgq __iomem *msgq,
  80. const struct bcm_vk_sync_qinfo *qinfo)
  81. {
  82. return (qinfo->q_size - msgq_occupied(msgq, qinfo) - 1);
  83. }
  84. /* number of retries when enqueue message fails before returning EAGAIN */
  85. #define BCM_VK_H2VK_ENQ_RETRY 10
  86. #define BCM_VK_H2VK_ENQ_RETRY_DELAY_MS 50
  87. bool bcm_vk_drv_access_ok(struct bcm_vk *vk)
  88. {
  89. return (!!atomic_read(&vk->msgq_inited));
  90. }
  91. void bcm_vk_set_host_alert(struct bcm_vk *vk, u32 bit_mask)
  92. {
  93. struct bcm_vk_alert *alert = &vk->host_alert;
  94. unsigned long flags;
  95. /* use irqsave version as this maybe called inside timer interrupt */
  96. spin_lock_irqsave(&vk->host_alert_lock, flags);
  97. alert->notfs |= bit_mask;
  98. spin_unlock_irqrestore(&vk->host_alert_lock, flags);
  99. if (test_and_set_bit(BCM_VK_WQ_NOTF_PEND, vk->wq_offload) == 0)
  100. queue_work(vk->wq_thread, &vk->wq_work);
  101. }
  102. /*
  103. * Heartbeat related defines
  104. * The heartbeat from host is a last resort. If stuck condition happens
  105. * on the card, firmware is supposed to detect it. Therefore, the heartbeat
  106. * values used will be more relaxed on the driver, which need to be bigger
  107. * than the watchdog timeout on the card. The watchdog timeout on the card
  108. * is 20s, with a jitter of 2s => 22s. We use a value of 27s here.
  109. */
  110. #define BCM_VK_HB_TIMER_S 3
  111. #define BCM_VK_HB_TIMER_VALUE (BCM_VK_HB_TIMER_S * HZ)
  112. #define BCM_VK_HB_LOST_MAX (27 / BCM_VK_HB_TIMER_S)
  113. static void bcm_vk_hb_poll(struct work_struct *work)
  114. {
  115. u32 uptime_s;
  116. struct bcm_vk_hb_ctrl *hb = container_of(to_delayed_work(work), struct bcm_vk_hb_ctrl,
  117. work);
  118. struct bcm_vk *vk = container_of(hb, struct bcm_vk, hb_ctrl);
  119. if (bcm_vk_drv_access_ok(vk) && hb_mon_is_on()) {
  120. /* read uptime from register and compare */
  121. uptime_s = vkread32(vk, BAR_0, BAR_OS_UPTIME);
  122. if (uptime_s == hb->last_uptime)
  123. hb->lost_cnt++;
  124. else /* reset to avoid accumulation */
  125. hb->lost_cnt = 0;
  126. dev_dbg(&vk->pdev->dev, "Last uptime %d current %d, lost %d\n",
  127. hb->last_uptime, uptime_s, hb->lost_cnt);
  128. /*
  129. * if the interface goes down without any activity, a value
  130. * of 0xFFFFFFFF will be continuously read, and the detection
  131. * will be happened eventually.
  132. */
  133. hb->last_uptime = uptime_s;
  134. } else {
  135. /* reset heart beat lost cnt */
  136. hb->lost_cnt = 0;
  137. }
  138. /* next, check if heartbeat exceeds limit */
  139. if (hb->lost_cnt > BCM_VK_HB_LOST_MAX) {
  140. dev_err(&vk->pdev->dev, "Heartbeat Misses %d times, %d s!\n",
  141. BCM_VK_HB_LOST_MAX,
  142. BCM_VK_HB_LOST_MAX * BCM_VK_HB_TIMER_S);
  143. bcm_vk_blk_drv_access(vk);
  144. bcm_vk_set_host_alert(vk, ERR_LOG_HOST_HB_FAIL);
  145. }
  146. /* re-arm timer */
  147. schedule_delayed_work(&hb->work, BCM_VK_HB_TIMER_VALUE);
  148. }
  149. void bcm_vk_hb_init(struct bcm_vk *vk)
  150. {
  151. struct bcm_vk_hb_ctrl *hb = &vk->hb_ctrl;
  152. INIT_DELAYED_WORK(&hb->work, bcm_vk_hb_poll);
  153. schedule_delayed_work(&hb->work, BCM_VK_HB_TIMER_VALUE);
  154. }
  155. void bcm_vk_hb_deinit(struct bcm_vk *vk)
  156. {
  157. struct bcm_vk_hb_ctrl *hb = &vk->hb_ctrl;
  158. cancel_delayed_work_sync(&hb->work);
  159. }
  160. static void bcm_vk_msgid_bitmap_clear(struct bcm_vk *vk,
  161. unsigned int start,
  162. unsigned int nbits)
  163. {
  164. spin_lock(&vk->msg_id_lock);
  165. bitmap_clear(vk->bmap, start, nbits);
  166. spin_unlock(&vk->msg_id_lock);
  167. }
  168. /*
  169. * allocate a ctx per file struct
  170. */
  171. static struct bcm_vk_ctx *bcm_vk_get_ctx(struct bcm_vk *vk, const pid_t pid)
  172. {
  173. u32 i;
  174. struct bcm_vk_ctx *ctx = NULL;
  175. u32 hash_idx = hash_32(pid, VK_PID_HT_SHIFT_BIT);
  176. spin_lock(&vk->ctx_lock);
  177. /* check if it is in reset, if so, don't allow */
  178. if (vk->reset_pid) {
  179. dev_err(&vk->pdev->dev,
  180. "No context allowed during reset by pid %d\n",
  181. vk->reset_pid);
  182. goto in_reset_exit;
  183. }
  184. for (i = 0; i < ARRAY_SIZE(vk->ctx); i++) {
  185. if (!vk->ctx[i].in_use) {
  186. vk->ctx[i].in_use = true;
  187. ctx = &vk->ctx[i];
  188. break;
  189. }
  190. }
  191. if (!ctx) {
  192. dev_err(&vk->pdev->dev, "All context in use\n");
  193. goto all_in_use_exit;
  194. }
  195. /* set the pid and insert it to hash table */
  196. ctx->pid = pid;
  197. ctx->hash_idx = hash_idx;
  198. list_add_tail(&ctx->node, &vk->pid_ht[hash_idx].head);
  199. /* increase kref */
  200. kref_get(&vk->kref);
  201. /* clear counter */
  202. atomic_set(&ctx->pend_cnt, 0);
  203. atomic_set(&ctx->dma_cnt, 0);
  204. init_waitqueue_head(&ctx->rd_wq);
  205. all_in_use_exit:
  206. in_reset_exit:
  207. spin_unlock(&vk->ctx_lock);
  208. return ctx;
  209. }
  210. static u16 bcm_vk_get_msg_id(struct bcm_vk *vk)
  211. {
  212. u16 rc = VK_MSG_ID_OVERFLOW;
  213. u16 test_bit_count = 0;
  214. spin_lock(&vk->msg_id_lock);
  215. while (test_bit_count < (VK_MSG_ID_BITMAP_SIZE - 1)) {
  216. /*
  217. * first time come in this loop, msg_id will be 0
  218. * and the first one tested will be 1. We skip
  219. * VK_SIMPLEX_MSG_ID (0) for one way host2vk
  220. * communication
  221. */
  222. vk->msg_id++;
  223. if (vk->msg_id == VK_MSG_ID_BITMAP_SIZE)
  224. vk->msg_id = 1;
  225. if (test_bit(vk->msg_id, vk->bmap)) {
  226. test_bit_count++;
  227. continue;
  228. }
  229. rc = vk->msg_id;
  230. bitmap_set(vk->bmap, vk->msg_id, 1);
  231. break;
  232. }
  233. spin_unlock(&vk->msg_id_lock);
  234. return rc;
  235. }
  236. static int bcm_vk_free_ctx(struct bcm_vk *vk, struct bcm_vk_ctx *ctx)
  237. {
  238. u32 idx;
  239. u32 hash_idx;
  240. pid_t pid;
  241. struct bcm_vk_ctx *entry;
  242. int count = 0;
  243. if (!ctx) {
  244. dev_err(&vk->pdev->dev, "NULL context detected\n");
  245. return -EINVAL;
  246. }
  247. idx = ctx->idx;
  248. pid = ctx->pid;
  249. spin_lock(&vk->ctx_lock);
  250. if (!vk->ctx[idx].in_use) {
  251. dev_err(&vk->pdev->dev, "context[%d] not in use!\n", idx);
  252. } else {
  253. vk->ctx[idx].in_use = false;
  254. vk->ctx[idx].miscdev = NULL;
  255. /* Remove it from hash list and see if it is the last one. */
  256. list_del(&ctx->node);
  257. hash_idx = ctx->hash_idx;
  258. list_for_each_entry(entry, &vk->pid_ht[hash_idx].head, node) {
  259. if (entry->pid == pid)
  260. count++;
  261. }
  262. }
  263. spin_unlock(&vk->ctx_lock);
  264. return count;
  265. }
  266. static void bcm_vk_free_wkent(struct device *dev, struct bcm_vk_wkent *entry)
  267. {
  268. int proc_cnt;
  269. bcm_vk_sg_free(dev, entry->dma, VK_DMA_MAX_ADDRS, &proc_cnt);
  270. if (proc_cnt)
  271. atomic_dec(&entry->ctx->dma_cnt);
  272. kfree(entry->to_h_msg);
  273. kfree(entry);
  274. }
  275. static void bcm_vk_drain_all_pend(struct device *dev,
  276. struct bcm_vk_msg_chan *chan,
  277. struct bcm_vk_ctx *ctx)
  278. {
  279. u32 num;
  280. struct bcm_vk_wkent *entry, *tmp;
  281. struct bcm_vk *vk;
  282. struct list_head del_q;
  283. if (ctx)
  284. vk = container_of(ctx->miscdev, struct bcm_vk, miscdev);
  285. INIT_LIST_HEAD(&del_q);
  286. spin_lock(&chan->pendq_lock);
  287. for (num = 0; num < chan->q_nr; num++) {
  288. list_for_each_entry_safe(entry, tmp, &chan->pendq[num], node) {
  289. if ((!ctx) || (entry->ctx->idx == ctx->idx)) {
  290. list_move_tail(&entry->node, &del_q);
  291. }
  292. }
  293. }
  294. spin_unlock(&chan->pendq_lock);
  295. /* batch clean up */
  296. num = 0;
  297. list_for_each_entry_safe(entry, tmp, &del_q, node) {
  298. list_del(&entry->node);
  299. num++;
  300. if (ctx) {
  301. struct vk_msg_blk *msg;
  302. int bit_set;
  303. bool responded;
  304. u32 msg_id;
  305. /* if it is specific ctx, log for any stuck */
  306. msg = entry->to_v_msg;
  307. msg_id = get_msg_id(msg);
  308. bit_set = test_bit(msg_id, vk->bmap);
  309. responded = entry->to_h_msg ? true : false;
  310. if (num <= batch_log)
  311. dev_info(dev,
  312. "Drained: fid %u size %u msg 0x%x(seq-%x) ctx 0x%x[fd-%d] args:[0x%x 0x%x] resp %s, bmap %d\n",
  313. msg->function_id, msg->size,
  314. msg_id, entry->seq_num,
  315. msg->context_id, entry->ctx->idx,
  316. msg->cmd, msg->arg,
  317. responded ? "T" : "F", bit_set);
  318. if (responded)
  319. atomic_dec(&ctx->pend_cnt);
  320. else if (bit_set)
  321. bcm_vk_msgid_bitmap_clear(vk, msg_id, 1);
  322. }
  323. bcm_vk_free_wkent(dev, entry);
  324. }
  325. if (num && ctx)
  326. dev_info(dev, "Total drained items %d [fd-%d]\n",
  327. num, ctx->idx);
  328. }
  329. void bcm_vk_drain_msg_on_reset(struct bcm_vk *vk)
  330. {
  331. bcm_vk_drain_all_pend(&vk->pdev->dev, &vk->to_v_msg_chan, NULL);
  332. bcm_vk_drain_all_pend(&vk->pdev->dev, &vk->to_h_msg_chan, NULL);
  333. }
  334. /*
  335. * Function to sync up the messages queue info that is provided by BAR1
  336. */
  337. int bcm_vk_sync_msgq(struct bcm_vk *vk, bool force_sync)
  338. {
  339. struct bcm_vk_msgq __iomem *msgq;
  340. struct device *dev = &vk->pdev->dev;
  341. u32 msgq_off;
  342. u32 num_q;
  343. struct bcm_vk_msg_chan *chan_list[] = {&vk->to_v_msg_chan,
  344. &vk->to_h_msg_chan};
  345. struct bcm_vk_msg_chan *chan;
  346. int i, j;
  347. int ret = 0;
  348. /*
  349. * If the driver is loaded at startup where vk OS is not up yet,
  350. * the msgq-info may not be available until a later time. In
  351. * this case, we skip and the sync function is supposed to be
  352. * called again.
  353. */
  354. if (!bcm_vk_msgq_marker_valid(vk)) {
  355. dev_info(dev, "BAR1 msgq marker not initialized.\n");
  356. return -EAGAIN;
  357. }
  358. msgq_off = vkread32(vk, BAR_1, VK_BAR1_MSGQ_CTRL_OFF);
  359. /* each side is always half the total */
  360. num_q = vkread32(vk, BAR_1, VK_BAR1_MSGQ_NR) / 2;
  361. if (!num_q || (num_q > VK_MSGQ_PER_CHAN_MAX)) {
  362. dev_err(dev,
  363. "Advertised msgq %d error - max %d allowed\n",
  364. num_q, VK_MSGQ_PER_CHAN_MAX);
  365. return -EINVAL;
  366. }
  367. vk->to_v_msg_chan.q_nr = num_q;
  368. vk->to_h_msg_chan.q_nr = num_q;
  369. /* first msgq location */
  370. msgq = vk->bar[BAR_1] + msgq_off;
  371. /*
  372. * if this function is called when it is already inited,
  373. * something is wrong
  374. */
  375. if (bcm_vk_drv_access_ok(vk) && !force_sync) {
  376. dev_err(dev, "Msgq info already in sync\n");
  377. return -EPERM;
  378. }
  379. for (i = 0; i < ARRAY_SIZE(chan_list); i++) {
  380. chan = chan_list[i];
  381. memset(chan->sync_qinfo, 0, sizeof(chan->sync_qinfo));
  382. for (j = 0; j < num_q; j++) {
  383. struct bcm_vk_sync_qinfo *qinfo;
  384. u32 msgq_start;
  385. u32 msgq_size;
  386. u32 msgq_nxt;
  387. u32 msgq_db_offset, q_db_offset;
  388. chan->msgq[j] = msgq;
  389. msgq_start = readl_relaxed(&msgq->start);
  390. msgq_size = readl_relaxed(&msgq->size);
  391. msgq_nxt = readl_relaxed(&msgq->nxt);
  392. msgq_db_offset = readl_relaxed(&msgq->db_offset);
  393. q_db_offset = (msgq_db_offset & ((1 << DB_SHIFT) - 1));
  394. if (q_db_offset == (~msgq_db_offset >> DB_SHIFT))
  395. msgq_db_offset = q_db_offset;
  396. else
  397. /* fall back to default */
  398. msgq_db_offset = VK_BAR0_Q_DB_BASE(j);
  399. dev_info(dev,
  400. "MsgQ[%d] type %d num %d, @ 0x%x, db_offset 0x%x rd_idx %d wr_idx %d, size %d, nxt 0x%x\n",
  401. j,
  402. readw_relaxed(&msgq->type),
  403. readw_relaxed(&msgq->num),
  404. msgq_start,
  405. msgq_db_offset,
  406. readl_relaxed(&msgq->rd_idx),
  407. readl_relaxed(&msgq->wr_idx),
  408. msgq_size,
  409. msgq_nxt);
  410. qinfo = &chan->sync_qinfo[j];
  411. /* formulate and record static info */
  412. qinfo->q_start = vk->bar[BAR_1] + msgq_start;
  413. qinfo->q_size = msgq_size;
  414. /* set low threshold as 50% or 1/2 */
  415. qinfo->q_low = qinfo->q_size >> 1;
  416. qinfo->q_mask = qinfo->q_size - 1;
  417. qinfo->q_db_offset = msgq_db_offset;
  418. msgq++;
  419. }
  420. }
  421. atomic_set(&vk->msgq_inited, 1);
  422. return ret;
  423. }
  424. static int bcm_vk_msg_chan_init(struct bcm_vk_msg_chan *chan)
  425. {
  426. u32 i;
  427. mutex_init(&chan->msgq_mutex);
  428. spin_lock_init(&chan->pendq_lock);
  429. for (i = 0; i < VK_MSGQ_MAX_NR; i++)
  430. INIT_LIST_HEAD(&chan->pendq[i]);
  431. return 0;
  432. }
  433. static void bcm_vk_append_pendq(struct bcm_vk_msg_chan *chan, u16 q_num,
  434. struct bcm_vk_wkent *entry)
  435. {
  436. struct bcm_vk_ctx *ctx;
  437. spin_lock(&chan->pendq_lock);
  438. list_add_tail(&entry->node, &chan->pendq[q_num]);
  439. if (entry->to_h_msg) {
  440. ctx = entry->ctx;
  441. atomic_inc(&ctx->pend_cnt);
  442. wake_up_interruptible(&ctx->rd_wq);
  443. }
  444. spin_unlock(&chan->pendq_lock);
  445. }
  446. static u32 bcm_vk_append_ib_sgl(struct bcm_vk *vk,
  447. struct bcm_vk_wkent *entry,
  448. struct _vk_data *data,
  449. unsigned int num_planes)
  450. {
  451. unsigned int i;
  452. unsigned int item_cnt = 0;
  453. struct device *dev = &vk->pdev->dev;
  454. struct bcm_vk_msg_chan *chan = &vk->to_v_msg_chan;
  455. struct vk_msg_blk *msg = &entry->to_v_msg[0];
  456. struct bcm_vk_msgq __iomem *msgq;
  457. struct bcm_vk_sync_qinfo *qinfo;
  458. u32 ib_sgl_size = 0;
  459. u8 *buf = (u8 *)&entry->to_v_msg[entry->to_v_blks];
  460. u32 avail;
  461. u32 q_num;
  462. /* check if high watermark is hit, and if so, skip */
  463. q_num = get_q_num(msg);
  464. msgq = chan->msgq[q_num];
  465. qinfo = &chan->sync_qinfo[q_num];
  466. avail = msgq_avail_space(msgq, qinfo);
  467. if (avail < qinfo->q_low) {
  468. dev_dbg(dev, "Skip inserting inband SGL, [0x%x/0x%x]\n",
  469. avail, qinfo->q_size);
  470. return 0;
  471. }
  472. for (i = 0; i < num_planes; i++) {
  473. if (data[i].address &&
  474. (ib_sgl_size + data[i].size) <= vk->ib_sgl_size) {
  475. item_cnt++;
  476. memcpy(buf, entry->dma[i].sglist, data[i].size);
  477. ib_sgl_size += data[i].size;
  478. buf += data[i].size;
  479. }
  480. }
  481. dev_dbg(dev, "Num %u sgl items appended, size 0x%x, room 0x%x\n",
  482. item_cnt, ib_sgl_size, vk->ib_sgl_size);
  483. /* round up size */
  484. ib_sgl_size = (ib_sgl_size + VK_MSGQ_BLK_SIZE - 1)
  485. >> VK_MSGQ_BLK_SZ_SHIFT;
  486. return ib_sgl_size;
  487. }
  488. void bcm_to_v_q_doorbell(struct bcm_vk *vk, u32 q_num, u32 db_val)
  489. {
  490. struct bcm_vk_msg_chan *chan = &vk->to_v_msg_chan;
  491. struct bcm_vk_sync_qinfo *qinfo = &chan->sync_qinfo[q_num];
  492. vkwrite32(vk, db_val, BAR_0, qinfo->q_db_offset);
  493. }
  494. static int bcm_to_v_msg_enqueue(struct bcm_vk *vk, struct bcm_vk_wkent *entry)
  495. {
  496. static u32 seq_num;
  497. struct bcm_vk_msg_chan *chan = &vk->to_v_msg_chan;
  498. struct device *dev = &vk->pdev->dev;
  499. struct vk_msg_blk *src = &entry->to_v_msg[0];
  500. struct vk_msg_blk __iomem *dst;
  501. struct bcm_vk_msgq __iomem *msgq;
  502. struct bcm_vk_sync_qinfo *qinfo;
  503. u32 q_num = get_q_num(src);
  504. u32 wr_idx; /* local copy */
  505. u32 i;
  506. u32 avail;
  507. u32 retry;
  508. if (entry->to_v_blks != src->size + 1) {
  509. dev_err(dev, "number of blks %d not matching %d MsgId[0x%x]: func %d ctx 0x%x\n",
  510. entry->to_v_blks,
  511. src->size + 1,
  512. get_msg_id(src),
  513. src->function_id,
  514. src->context_id);
  515. return -EMSGSIZE;
  516. }
  517. msgq = chan->msgq[q_num];
  518. qinfo = &chan->sync_qinfo[q_num];
  519. mutex_lock(&chan->msgq_mutex);
  520. avail = msgq_avail_space(msgq, qinfo);
  521. /* if not enough space, return EAGAIN and let app handles it */
  522. retry = 0;
  523. while ((avail < entry->to_v_blks) &&
  524. (retry++ < BCM_VK_H2VK_ENQ_RETRY)) {
  525. mutex_unlock(&chan->msgq_mutex);
  526. msleep(BCM_VK_H2VK_ENQ_RETRY_DELAY_MS);
  527. mutex_lock(&chan->msgq_mutex);
  528. avail = msgq_avail_space(msgq, qinfo);
  529. }
  530. if (retry > BCM_VK_H2VK_ENQ_RETRY) {
  531. mutex_unlock(&chan->msgq_mutex);
  532. return -EAGAIN;
  533. }
  534. /* at this point, mutex is taken and there is enough space */
  535. entry->seq_num = seq_num++; /* update debug seq number */
  536. wr_idx = readl_relaxed(&msgq->wr_idx);
  537. if (wr_idx >= qinfo->q_size) {
  538. dev_crit(dev, "Invalid wr_idx 0x%x => max 0x%x!",
  539. wr_idx, qinfo->q_size);
  540. bcm_vk_blk_drv_access(vk);
  541. bcm_vk_set_host_alert(vk, ERR_LOG_HOST_PCIE_DWN);
  542. goto idx_err;
  543. }
  544. dst = msgq_blk_addr(qinfo, wr_idx);
  545. for (i = 0; i < entry->to_v_blks; i++) {
  546. memcpy_toio(dst, src, sizeof(*dst));
  547. src++;
  548. wr_idx = msgq_inc(qinfo, wr_idx, 1);
  549. dst = msgq_blk_addr(qinfo, wr_idx);
  550. }
  551. /* flush the write pointer */
  552. writel(wr_idx, &msgq->wr_idx);
  553. /* log new info for debugging */
  554. dev_dbg(dev,
  555. "MsgQ[%d] [Rd Wr] = [%d %d] blks inserted %d - Q = [u-%d a-%d]/%d\n",
  556. readl_relaxed(&msgq->num),
  557. readl_relaxed(&msgq->rd_idx),
  558. wr_idx,
  559. entry->to_v_blks,
  560. msgq_occupied(msgq, qinfo),
  561. msgq_avail_space(msgq, qinfo),
  562. readl_relaxed(&msgq->size));
  563. /*
  564. * press door bell based on queue number. 1 is added to the wr_idx
  565. * to avoid the value of 0 appearing on the VK side to distinguish
  566. * from initial value.
  567. */
  568. bcm_to_v_q_doorbell(vk, q_num, wr_idx + 1);
  569. idx_err:
  570. mutex_unlock(&chan->msgq_mutex);
  571. return 0;
  572. }
  573. int bcm_vk_send_shutdown_msg(struct bcm_vk *vk, u32 shut_type,
  574. const pid_t pid, const u32 q_num)
  575. {
  576. int rc = 0;
  577. struct bcm_vk_wkent *entry;
  578. struct device *dev = &vk->pdev->dev;
  579. /*
  580. * check if the marker is still good. Sometimes, the PCIe interface may
  581. * have gone done, and if so and we ship down thing based on broken
  582. * values, kernel may panic.
  583. */
  584. if (!bcm_vk_msgq_marker_valid(vk)) {
  585. dev_info(dev, "PCIe comm chan - invalid marker (0x%x)!\n",
  586. vkread32(vk, BAR_1, VK_BAR1_MSGQ_DEF_RDY));
  587. return -EINVAL;
  588. }
  589. entry = kzalloc_flex(*entry, to_v_msg, 1);
  590. if (!entry)
  591. return -ENOMEM;
  592. entry->to_v_blks = 1; /* always 1 block */
  593. /* fill up necessary data */
  594. entry->to_v_msg[0].function_id = VK_FID_SHUTDOWN;
  595. set_q_num(&entry->to_v_msg[0], q_num);
  596. set_msg_id(&entry->to_v_msg[0], VK_SIMPLEX_MSG_ID);
  597. entry->to_v_msg[0].cmd = shut_type;
  598. entry->to_v_msg[0].arg = pid;
  599. rc = bcm_to_v_msg_enqueue(vk, entry);
  600. if (rc)
  601. dev_err(dev,
  602. "Sending shutdown message to q %d for pid %d fails.\n",
  603. get_q_num(&entry->to_v_msg[0]), pid);
  604. kfree(entry);
  605. return rc;
  606. }
  607. static int bcm_vk_handle_last_sess(struct bcm_vk *vk, const pid_t pid,
  608. const u32 q_num)
  609. {
  610. int rc = 0;
  611. struct device *dev = &vk->pdev->dev;
  612. /*
  613. * don't send down or do anything if message queue is not initialized
  614. * and if it is the reset session, clear it.
  615. */
  616. if (!bcm_vk_drv_access_ok(vk)) {
  617. if (vk->reset_pid == pid)
  618. vk->reset_pid = 0;
  619. return -EPERM;
  620. }
  621. dev_dbg(dev, "No more sessions, shut down pid %d\n", pid);
  622. /* only need to do it if it is not the reset process */
  623. if (vk->reset_pid != pid)
  624. rc = bcm_vk_send_shutdown_msg(vk, VK_SHUTDOWN_PID, pid, q_num);
  625. else
  626. /* put reset_pid to 0 if it is exiting last session */
  627. vk->reset_pid = 0;
  628. return rc;
  629. }
  630. static struct bcm_vk_wkent *bcm_vk_dequeue_pending(struct bcm_vk *vk,
  631. struct bcm_vk_msg_chan *chan,
  632. u16 q_num,
  633. u16 msg_id)
  634. {
  635. struct bcm_vk_wkent *entry = NULL, *iter;
  636. spin_lock(&chan->pendq_lock);
  637. list_for_each_entry(iter, &chan->pendq[q_num], node) {
  638. if (get_msg_id(&iter->to_v_msg[0]) == msg_id) {
  639. list_del(&iter->node);
  640. entry = iter;
  641. bcm_vk_msgid_bitmap_clear(vk, msg_id, 1);
  642. break;
  643. }
  644. }
  645. spin_unlock(&chan->pendq_lock);
  646. return entry;
  647. }
  648. s32 bcm_to_h_msg_dequeue(struct bcm_vk *vk)
  649. {
  650. struct device *dev = &vk->pdev->dev;
  651. struct bcm_vk_msg_chan *chan = &vk->to_h_msg_chan;
  652. struct vk_msg_blk *data;
  653. struct vk_msg_blk __iomem *src;
  654. struct vk_msg_blk *dst;
  655. struct bcm_vk_msgq __iomem *msgq;
  656. struct bcm_vk_sync_qinfo *qinfo;
  657. struct bcm_vk_wkent *entry;
  658. u32 rd_idx, wr_idx;
  659. u32 q_num, msg_id, j;
  660. u32 num_blks;
  661. s32 total = 0;
  662. int cnt = 0;
  663. int msg_processed = 0;
  664. int max_msg_to_process;
  665. bool exit_loop;
  666. /*
  667. * drain all the messages from the queues, and find its pending
  668. * entry in the to_v queue, based on msg_id & q_num, and move the
  669. * entry to the to_h pending queue, waiting for user space
  670. * program to extract
  671. */
  672. mutex_lock(&chan->msgq_mutex);
  673. for (q_num = 0; q_num < chan->q_nr; q_num++) {
  674. msgq = chan->msgq[q_num];
  675. qinfo = &chan->sync_qinfo[q_num];
  676. max_msg_to_process = BCM_VK_MSG_PROC_MAX_LOOP * qinfo->q_size;
  677. rd_idx = readl_relaxed(&msgq->rd_idx);
  678. wr_idx = readl_relaxed(&msgq->wr_idx);
  679. msg_processed = 0;
  680. exit_loop = false;
  681. while ((rd_idx != wr_idx) && !exit_loop) {
  682. u8 src_size;
  683. /*
  684. * Make a local copy and get pointer to src blk
  685. * The rd_idx is masked before getting the pointer to
  686. * avoid out of bound access in case the interface goes
  687. * down. It will end up pointing to the last block in
  688. * the buffer, but subsequent src->size check would be
  689. * able to catch this.
  690. */
  691. src = msgq_blk_addr(qinfo, rd_idx & qinfo->q_mask);
  692. src_size = readb(&src->size);
  693. if ((rd_idx >= qinfo->q_size) ||
  694. (src_size > (qinfo->q_size - 1))) {
  695. dev_crit(dev,
  696. "Invalid rd_idx 0x%x or size 0x%x => max 0x%x!",
  697. rd_idx, src_size, qinfo->q_size);
  698. bcm_vk_blk_drv_access(vk);
  699. bcm_vk_set_host_alert(vk,
  700. ERR_LOG_HOST_PCIE_DWN);
  701. goto idx_err;
  702. }
  703. num_blks = src_size + 1;
  704. data = kzalloc(num_blks * VK_MSGQ_BLK_SIZE, GFP_KERNEL);
  705. if (data) {
  706. /* copy messages and linearize it */
  707. dst = data;
  708. for (j = 0; j < num_blks; j++) {
  709. memcpy_fromio(dst, src, sizeof(*dst));
  710. dst++;
  711. rd_idx = msgq_inc(qinfo, rd_idx, 1);
  712. src = msgq_blk_addr(qinfo, rd_idx);
  713. }
  714. total++;
  715. } else {
  716. /*
  717. * if we could not allocate memory in kernel,
  718. * that is fatal.
  719. */
  720. dev_crit(dev, "Kernel mem allocation failure.\n");
  721. total = -ENOMEM;
  722. goto idx_err;
  723. }
  724. /* flush rd pointer after a message is dequeued */
  725. writel(rd_idx, &msgq->rd_idx);
  726. /* log new info for debugging */
  727. dev_dbg(dev,
  728. "MsgQ[%d] [Rd Wr] = [%d %d] blks extracted %d - Q = [u-%d a-%d]/%d\n",
  729. readl_relaxed(&msgq->num),
  730. rd_idx,
  731. wr_idx,
  732. num_blks,
  733. msgq_occupied(msgq, qinfo),
  734. msgq_avail_space(msgq, qinfo),
  735. readl_relaxed(&msgq->size));
  736. /*
  737. * No need to search if it is an autonomous one-way
  738. * message from driver, as these messages do not bear
  739. * a to_v pending item. Currently, only the shutdown
  740. * message falls into this category.
  741. */
  742. if (data->function_id == VK_FID_SHUTDOWN) {
  743. kfree(data);
  744. continue;
  745. }
  746. msg_id = get_msg_id(data);
  747. /* lookup original message in to_v direction */
  748. entry = bcm_vk_dequeue_pending(vk,
  749. &vk->to_v_msg_chan,
  750. q_num,
  751. msg_id);
  752. /*
  753. * if there is message to does not have prior send,
  754. * this is the location to add here
  755. */
  756. if (entry) {
  757. entry->to_h_blks = num_blks;
  758. entry->to_h_msg = data;
  759. bcm_vk_append_pendq(&vk->to_h_msg_chan,
  760. q_num, entry);
  761. } else {
  762. if (cnt++ < batch_log)
  763. dev_info(dev,
  764. "Could not find MsgId[0x%x] for resp func %d bmap %d\n",
  765. msg_id, data->function_id,
  766. test_bit(msg_id, vk->bmap));
  767. kfree(data);
  768. }
  769. /* Fetch wr_idx to handle more back-to-back events */
  770. wr_idx = readl(&msgq->wr_idx);
  771. /*
  772. * cap the max so that even we try to handle more back-to-back events,
  773. * so that it won't hold CPU too long or in case rd/wr idexes are
  774. * corrupted which triggers infinite looping.
  775. */
  776. if (++msg_processed >= max_msg_to_process) {
  777. dev_warn(dev, "Q[%d] Per loop processing exceeds %d\n",
  778. q_num, max_msg_to_process);
  779. exit_loop = true;
  780. }
  781. }
  782. }
  783. idx_err:
  784. mutex_unlock(&chan->msgq_mutex);
  785. dev_dbg(dev, "total %d drained from queues\n", total);
  786. return total;
  787. }
  788. /*
  789. * init routine for all required data structures
  790. */
  791. static int bcm_vk_data_init(struct bcm_vk *vk)
  792. {
  793. int i;
  794. spin_lock_init(&vk->ctx_lock);
  795. for (i = 0; i < ARRAY_SIZE(vk->ctx); i++) {
  796. vk->ctx[i].in_use = false;
  797. vk->ctx[i].idx = i; /* self identity */
  798. vk->ctx[i].miscdev = NULL;
  799. }
  800. spin_lock_init(&vk->msg_id_lock);
  801. spin_lock_init(&vk->host_alert_lock);
  802. vk->msg_id = 0;
  803. /* initialize hash table */
  804. for (i = 0; i < VK_PID_HT_SZ; i++)
  805. INIT_LIST_HEAD(&vk->pid_ht[i].head);
  806. return 0;
  807. }
  808. irqreturn_t bcm_vk_msgq_irqhandler(int irq, void *dev_id)
  809. {
  810. struct bcm_vk *vk = dev_id;
  811. if (!bcm_vk_drv_access_ok(vk)) {
  812. dev_err(&vk->pdev->dev,
  813. "Interrupt %d received when msgq not inited\n", irq);
  814. goto skip_schedule_work;
  815. }
  816. queue_work(vk->wq_thread, &vk->wq_work);
  817. skip_schedule_work:
  818. return IRQ_HANDLED;
  819. }
  820. int bcm_vk_open(struct inode *inode, struct file *p_file)
  821. {
  822. struct bcm_vk_ctx *ctx;
  823. struct miscdevice *miscdev = (struct miscdevice *)p_file->private_data;
  824. struct bcm_vk *vk = container_of(miscdev, struct bcm_vk, miscdev);
  825. struct device *dev = &vk->pdev->dev;
  826. int rc = 0;
  827. /* get a context and set it up for file */
  828. ctx = bcm_vk_get_ctx(vk, task_tgid_nr(current));
  829. if (!ctx) {
  830. dev_err(dev, "Error allocating context\n");
  831. rc = -ENOMEM;
  832. } else {
  833. /*
  834. * set up context and replace private data with context for
  835. * other methods to use. Reason for the context is because
  836. * it is allowed for multiple sessions to open the sysfs, and
  837. * for each file open, when upper layer query the response,
  838. * only those that are tied to a specific open should be
  839. * returned. The context->idx will be used for such binding
  840. */
  841. ctx->miscdev = miscdev;
  842. p_file->private_data = ctx;
  843. dev_dbg(dev, "ctx_returned with idx %d, pid %d\n",
  844. ctx->idx, ctx->pid);
  845. }
  846. return rc;
  847. }
  848. ssize_t bcm_vk_read(struct file *p_file,
  849. char __user *buf,
  850. size_t count,
  851. loff_t *f_pos)
  852. {
  853. ssize_t rc = -ENOMSG;
  854. struct bcm_vk_ctx *ctx = p_file->private_data;
  855. struct bcm_vk *vk = container_of(ctx->miscdev, struct bcm_vk,
  856. miscdev);
  857. struct device *dev = &vk->pdev->dev;
  858. struct bcm_vk_msg_chan *chan = &vk->to_h_msg_chan;
  859. struct bcm_vk_wkent *entry = NULL, *iter;
  860. struct vk_msg_blk tmp_msg;
  861. u32 tmp_usr_msg_id;
  862. u32 tmp_blks;
  863. u32 q_num;
  864. u32 rsp_length;
  865. if (!bcm_vk_drv_access_ok(vk))
  866. return -EPERM;
  867. dev_dbg(dev, "Buf count %zu\n", count);
  868. /*
  869. * search through the pendq on the to_h chan, and return only those
  870. * that belongs to the same context. Search is always from the high to
  871. * the low priority queues
  872. */
  873. spin_lock(&chan->pendq_lock);
  874. for (q_num = 0; q_num < chan->q_nr; q_num++) {
  875. list_for_each_entry(iter, &chan->pendq[q_num], node) {
  876. if (iter->ctx->idx == ctx->idx) {
  877. if (count >=
  878. (iter->to_h_blks * VK_MSGQ_BLK_SIZE)) {
  879. list_del(&iter->node);
  880. atomic_dec(&ctx->pend_cnt);
  881. entry = iter;
  882. } else {
  883. /* buffer not big enough */
  884. tmp_msg = iter->to_h_msg[0];
  885. tmp_usr_msg_id = iter->usr_msg_id;
  886. tmp_blks = iter->to_h_blks;
  887. rc = -EMSGSIZE;
  888. }
  889. goto read_loop_exit;
  890. }
  891. }
  892. }
  893. read_loop_exit:
  894. spin_unlock(&chan->pendq_lock);
  895. if (entry) {
  896. /* retrieve the passed down msg_id */
  897. set_msg_id(&entry->to_h_msg[0], entry->usr_msg_id);
  898. rsp_length = entry->to_h_blks * VK_MSGQ_BLK_SIZE;
  899. if (copy_to_user(buf, entry->to_h_msg, rsp_length) == 0)
  900. rc = rsp_length;
  901. bcm_vk_free_wkent(dev, entry);
  902. } else if (rc == -EMSGSIZE) {
  903. /*
  904. * in this case, return just the first block, so
  905. * that app knows what size it is looking for.
  906. */
  907. set_msg_id(&tmp_msg, tmp_usr_msg_id);
  908. tmp_msg.size = tmp_blks - 1;
  909. if (copy_to_user(buf, &tmp_msg, VK_MSGQ_BLK_SIZE) != 0) {
  910. dev_err(dev, "Error return 1st block in -EMSGSIZE\n");
  911. rc = -EFAULT;
  912. }
  913. }
  914. return rc;
  915. }
  916. ssize_t bcm_vk_write(struct file *p_file,
  917. const char __user *buf,
  918. size_t count,
  919. loff_t *f_pos)
  920. {
  921. ssize_t rc;
  922. struct bcm_vk_ctx *ctx = p_file->private_data;
  923. struct bcm_vk *vk = container_of(ctx->miscdev, struct bcm_vk,
  924. miscdev);
  925. struct bcm_vk_msgq __iomem *msgq;
  926. struct device *dev = &vk->pdev->dev;
  927. struct bcm_vk_wkent *entry;
  928. u32 sgl_extra_blks;
  929. u32 q_num;
  930. u32 msg_size;
  931. u32 msgq_size;
  932. if (!bcm_vk_drv_access_ok(vk))
  933. return -EPERM;
  934. dev_dbg(dev, "Msg count %zu\n", count);
  935. /* first, do sanity check where count should be multiple of basic blk */
  936. if (count & (VK_MSGQ_BLK_SIZE - 1)) {
  937. dev_err(dev, "Failure with size %zu not multiple of %zu\n",
  938. count, VK_MSGQ_BLK_SIZE);
  939. rc = -EINVAL;
  940. goto write_err;
  941. }
  942. /* allocate the work entry + buffer for size count and inband sgl */
  943. entry = kzalloc(sizeof(*entry) + count + vk->ib_sgl_size,
  944. GFP_KERNEL);
  945. if (!entry) {
  946. rc = -ENOMEM;
  947. goto write_err;
  948. }
  949. /* now copy msg from user space, and then formulate the work entry */
  950. if (copy_from_user(&entry->to_v_msg[0], buf, count)) {
  951. rc = -EFAULT;
  952. goto write_free_ent;
  953. }
  954. entry->to_v_blks = count >> VK_MSGQ_BLK_SZ_SHIFT;
  955. entry->ctx = ctx;
  956. /* do a check on the blk size which could not exceed queue space */
  957. q_num = get_q_num(&entry->to_v_msg[0]);
  958. msgq = vk->to_v_msg_chan.msgq[q_num];
  959. msgq_size = readl_relaxed(&msgq->size);
  960. if (entry->to_v_blks + (vk->ib_sgl_size >> VK_MSGQ_BLK_SZ_SHIFT)
  961. > (msgq_size - 1)) {
  962. dev_err(dev, "Blk size %d exceed max queue size allowed %d\n",
  963. entry->to_v_blks, msgq_size - 1);
  964. rc = -EINVAL;
  965. goto write_free_ent;
  966. }
  967. /* Use internal message id */
  968. entry->usr_msg_id = get_msg_id(&entry->to_v_msg[0]);
  969. rc = bcm_vk_get_msg_id(vk);
  970. if (rc == VK_MSG_ID_OVERFLOW) {
  971. dev_err(dev, "msg_id overflow\n");
  972. rc = -EOVERFLOW;
  973. goto write_free_ent;
  974. }
  975. set_msg_id(&entry->to_v_msg[0], rc);
  976. ctx->q_num = q_num;
  977. dev_dbg(dev,
  978. "[Q-%d]Message ctx id %d, usr_msg_id 0x%x sent msg_id 0x%x\n",
  979. ctx->q_num, ctx->idx, entry->usr_msg_id,
  980. get_msg_id(&entry->to_v_msg[0]));
  981. if (entry->to_v_msg[0].function_id == VK_FID_TRANS_BUF) {
  982. /* Convert any pointers to sg list */
  983. unsigned int num_planes;
  984. int dir;
  985. struct _vk_data *data;
  986. /*
  987. * check if we are in reset, if so, no buffer transfer is
  988. * allowed and return error.
  989. */
  990. if (vk->reset_pid) {
  991. dev_dbg(dev, "No Transfer allowed during reset, pid %d.\n",
  992. ctx->pid);
  993. rc = -EACCES;
  994. goto write_free_msgid;
  995. }
  996. num_planes = entry->to_v_msg[0].cmd & VK_CMD_PLANES_MASK;
  997. if ((entry->to_v_msg[0].cmd & VK_CMD_MASK) == VK_CMD_DOWNLOAD)
  998. dir = DMA_FROM_DEVICE;
  999. else
  1000. dir = DMA_TO_DEVICE;
  1001. /* Calculate vk_data location */
  1002. /* Go to end of the message */
  1003. msg_size = entry->to_v_msg[0].size;
  1004. if (msg_size > entry->to_v_blks) {
  1005. rc = -EMSGSIZE;
  1006. goto write_free_msgid;
  1007. }
  1008. data = (struct _vk_data *)&entry->to_v_msg[msg_size + 1];
  1009. /* Now back up to the start of the pointers */
  1010. data -= num_planes;
  1011. /* Convert user addresses to DMA SG List */
  1012. rc = bcm_vk_sg_alloc(dev, entry->dma, dir, data, num_planes);
  1013. if (rc)
  1014. goto write_free_msgid;
  1015. atomic_inc(&ctx->dma_cnt);
  1016. /* try to embed inband sgl */
  1017. sgl_extra_blks = bcm_vk_append_ib_sgl(vk, entry, data,
  1018. num_planes);
  1019. entry->to_v_blks += sgl_extra_blks;
  1020. entry->to_v_msg[0].size += sgl_extra_blks;
  1021. } else if (entry->to_v_msg[0].function_id == VK_FID_INIT &&
  1022. entry->to_v_msg[0].context_id == VK_NEW_CTX) {
  1023. /*
  1024. * Init happens in 2 stages, only the first stage contains the
  1025. * pid that needs translating.
  1026. */
  1027. pid_t org_pid, pid;
  1028. /*
  1029. * translate the pid into the unique host space as user
  1030. * may run sessions inside containers or process
  1031. * namespaces.
  1032. */
  1033. #define VK_MSG_PID_MASK 0xffffff00
  1034. #define VK_MSG_PID_SH 8
  1035. org_pid = (entry->to_v_msg[0].arg & VK_MSG_PID_MASK)
  1036. >> VK_MSG_PID_SH;
  1037. pid = task_tgid_nr(current);
  1038. entry->to_v_msg[0].arg =
  1039. (entry->to_v_msg[0].arg & ~VK_MSG_PID_MASK) |
  1040. (pid << VK_MSG_PID_SH);
  1041. if (org_pid != pid)
  1042. dev_dbg(dev, "In PID 0x%x(%d), converted PID 0x%x(%d)\n",
  1043. org_pid, org_pid, pid, pid);
  1044. }
  1045. /*
  1046. * store work entry to pending queue until a response is received.
  1047. * This needs to be done before enqueuing the message
  1048. */
  1049. bcm_vk_append_pendq(&vk->to_v_msg_chan, q_num, entry);
  1050. rc = bcm_to_v_msg_enqueue(vk, entry);
  1051. if (rc) {
  1052. dev_err(dev, "Fail to enqueue msg to to_v queue\n");
  1053. /* remove message from pending list */
  1054. entry = bcm_vk_dequeue_pending
  1055. (vk,
  1056. &vk->to_v_msg_chan,
  1057. q_num,
  1058. get_msg_id(&entry->to_v_msg[0]));
  1059. goto write_free_ent;
  1060. }
  1061. return count;
  1062. write_free_msgid:
  1063. bcm_vk_msgid_bitmap_clear(vk, get_msg_id(&entry->to_v_msg[0]), 1);
  1064. write_free_ent:
  1065. kfree(entry);
  1066. write_err:
  1067. return rc;
  1068. }
  1069. __poll_t bcm_vk_poll(struct file *p_file, struct poll_table_struct *wait)
  1070. {
  1071. __poll_t ret = 0;
  1072. int cnt;
  1073. struct bcm_vk_ctx *ctx = p_file->private_data;
  1074. struct bcm_vk *vk = container_of(ctx->miscdev, struct bcm_vk, miscdev);
  1075. struct device *dev = &vk->pdev->dev;
  1076. poll_wait(p_file, &ctx->rd_wq, wait);
  1077. cnt = atomic_read(&ctx->pend_cnt);
  1078. if (cnt) {
  1079. ret = (__force __poll_t)(POLLIN | POLLRDNORM);
  1080. if (cnt < 0) {
  1081. dev_err(dev, "Error cnt %d, setting back to 0", cnt);
  1082. atomic_set(&ctx->pend_cnt, 0);
  1083. }
  1084. }
  1085. return ret;
  1086. }
  1087. int bcm_vk_release(struct inode *inode, struct file *p_file)
  1088. {
  1089. int ret;
  1090. struct bcm_vk_ctx *ctx = p_file->private_data;
  1091. struct bcm_vk *vk = container_of(ctx->miscdev, struct bcm_vk, miscdev);
  1092. struct device *dev = &vk->pdev->dev;
  1093. pid_t pid = ctx->pid;
  1094. int dma_cnt;
  1095. unsigned long timeout, start_time;
  1096. /*
  1097. * if there are outstanding DMA transactions, need to delay long enough
  1098. * to ensure that the card side would have stopped touching the host buffer
  1099. * and its SGL list. A race condition could happen if the host app is killed
  1100. * abruptly, eg kill -9, while some DMA transfer orders are still inflight.
  1101. * Nothing could be done except for a delay as host side is running in a
  1102. * completely async fashion.
  1103. */
  1104. start_time = jiffies;
  1105. timeout = start_time + msecs_to_jiffies(BCM_VK_DMA_DRAIN_MAX_MS);
  1106. do {
  1107. if (time_after(jiffies, timeout)) {
  1108. dev_warn(dev, "%d dma still pending for [fd-%d] pid %d\n",
  1109. dma_cnt, ctx->idx, pid);
  1110. break;
  1111. }
  1112. dma_cnt = atomic_read(&ctx->dma_cnt);
  1113. cpu_relax();
  1114. cond_resched();
  1115. } while (dma_cnt);
  1116. dev_dbg(dev, "Draining for [fd-%d] pid %d - delay %d ms\n",
  1117. ctx->idx, pid, jiffies_to_msecs(jiffies - start_time));
  1118. bcm_vk_drain_all_pend(&vk->pdev->dev, &vk->to_v_msg_chan, ctx);
  1119. bcm_vk_drain_all_pend(&vk->pdev->dev, &vk->to_h_msg_chan, ctx);
  1120. ret = bcm_vk_free_ctx(vk, ctx);
  1121. if (ret == 0)
  1122. ret = bcm_vk_handle_last_sess(vk, pid, ctx->q_num);
  1123. else
  1124. ret = 0;
  1125. kref_put(&vk->kref, bcm_vk_release_data);
  1126. return ret;
  1127. }
  1128. int bcm_vk_msg_init(struct bcm_vk *vk)
  1129. {
  1130. struct device *dev = &vk->pdev->dev;
  1131. int ret;
  1132. if (bcm_vk_data_init(vk)) {
  1133. dev_err(dev, "Error initializing internal data structures\n");
  1134. return -EINVAL;
  1135. }
  1136. if (bcm_vk_msg_chan_init(&vk->to_v_msg_chan) ||
  1137. bcm_vk_msg_chan_init(&vk->to_h_msg_chan)) {
  1138. dev_err(dev, "Error initializing communication channel\n");
  1139. return -EIO;
  1140. }
  1141. /* read msgq info if ready */
  1142. ret = bcm_vk_sync_msgq(vk, false);
  1143. if (ret && (ret != -EAGAIN)) {
  1144. dev_err(dev, "Error reading comm msg Q info\n");
  1145. return -EIO;
  1146. }
  1147. return 0;
  1148. }
  1149. void bcm_vk_msg_remove(struct bcm_vk *vk)
  1150. {
  1151. bcm_vk_blk_drv_access(vk);
  1152. /* drain all pending items */
  1153. bcm_vk_drain_all_pend(&vk->pdev->dev, &vk->to_v_msg_chan, NULL);
  1154. bcm_vk_drain_all_pend(&vk->pdev->dev, &vk->to_h_msg_chan, NULL);
  1155. }