cptvf_main.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * Copyright (C) 2016 Cavium, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of version 2 of the GNU General Public License
  6. * as published by the Free Software Foundation.
  7. */
  8. #include <linux/interrupt.h>
  9. #include <linux/module.h>
  10. #include "cptvf.h"
  11. #define DRV_NAME "thunder-cptvf"
  12. #define DRV_VERSION "1.0"
  13. struct cptvf_wqe {
  14. struct tasklet_struct twork;
  15. void *cptvf;
  16. u32 qno;
  17. };
  18. struct cptvf_wqe_info {
  19. struct cptvf_wqe vq_wqe[CPT_NUM_QS_PER_VF];
  20. };
  21. static void vq_work_handler(unsigned long data)
  22. {
  23. struct cptvf_wqe_info *cwqe_info = (struct cptvf_wqe_info *)data;
  24. struct cptvf_wqe *cwqe = &cwqe_info->vq_wqe[0];
  25. vq_post_process(cwqe->cptvf, cwqe->qno);
  26. }
  27. static int init_worker_threads(struct cpt_vf *cptvf)
  28. {
  29. struct pci_dev *pdev = cptvf->pdev;
  30. struct cptvf_wqe_info *cwqe_info;
  31. int i;
  32. cwqe_info = kzalloc(sizeof(*cwqe_info), GFP_KERNEL);
  33. if (!cwqe_info)
  34. return -ENOMEM;
  35. if (cptvf->nr_queues) {
  36. dev_info(&pdev->dev, "Creating VQ worker threads (%d)\n",
  37. cptvf->nr_queues);
  38. }
  39. for (i = 0; i < cptvf->nr_queues; i++) {
  40. tasklet_init(&cwqe_info->vq_wqe[i].twork, vq_work_handler,
  41. (u64)cwqe_info);
  42. cwqe_info->vq_wqe[i].qno = i;
  43. cwqe_info->vq_wqe[i].cptvf = cptvf;
  44. }
  45. cptvf->wqe_info = cwqe_info;
  46. return 0;
  47. }
  48. static void cleanup_worker_threads(struct cpt_vf *cptvf)
  49. {
  50. struct cptvf_wqe_info *cwqe_info;
  51. struct pci_dev *pdev = cptvf->pdev;
  52. int i;
  53. cwqe_info = (struct cptvf_wqe_info *)cptvf->wqe_info;
  54. if (!cwqe_info)
  55. return;
  56. if (cptvf->nr_queues) {
  57. dev_info(&pdev->dev, "Cleaning VQ worker threads (%u)\n",
  58. cptvf->nr_queues);
  59. }
  60. for (i = 0; i < cptvf->nr_queues; i++)
  61. tasklet_kill(&cwqe_info->vq_wqe[i].twork);
  62. kzfree(cwqe_info);
  63. cptvf->wqe_info = NULL;
  64. }
  65. static void free_pending_queues(struct pending_qinfo *pqinfo)
  66. {
  67. int i;
  68. struct pending_queue *queue;
  69. for_each_pending_queue(pqinfo, queue, i) {
  70. if (!queue->head)
  71. continue;
  72. /* free single queue */
  73. kzfree((queue->head));
  74. queue->front = 0;
  75. queue->rear = 0;
  76. return;
  77. }
  78. pqinfo->qlen = 0;
  79. pqinfo->nr_queues = 0;
  80. }
  81. static int alloc_pending_queues(struct pending_qinfo *pqinfo, u32 qlen,
  82. u32 nr_queues)
  83. {
  84. u32 i;
  85. size_t size;
  86. int ret;
  87. struct pending_queue *queue = NULL;
  88. pqinfo->nr_queues = nr_queues;
  89. pqinfo->qlen = qlen;
  90. size = (qlen * sizeof(struct pending_entry));
  91. for_each_pending_queue(pqinfo, queue, i) {
  92. queue->head = kzalloc((size), GFP_KERNEL);
  93. if (!queue->head) {
  94. ret = -ENOMEM;
  95. goto pending_qfail;
  96. }
  97. queue->front = 0;
  98. queue->rear = 0;
  99. atomic64_set((&queue->pending_count), (0));
  100. /* init queue spin lock */
  101. spin_lock_init(&queue->lock);
  102. }
  103. return 0;
  104. pending_qfail:
  105. free_pending_queues(pqinfo);
  106. return ret;
  107. }
  108. static int init_pending_queues(struct cpt_vf *cptvf, u32 qlen, u32 nr_queues)
  109. {
  110. struct pci_dev *pdev = cptvf->pdev;
  111. int ret;
  112. if (!nr_queues)
  113. return 0;
  114. ret = alloc_pending_queues(&cptvf->pqinfo, qlen, nr_queues);
  115. if (ret) {
  116. dev_err(&pdev->dev, "failed to setup pending queues (%u)\n",
  117. nr_queues);
  118. return ret;
  119. }
  120. return 0;
  121. }
  122. static void cleanup_pending_queues(struct cpt_vf *cptvf)
  123. {
  124. struct pci_dev *pdev = cptvf->pdev;
  125. if (!cptvf->nr_queues)
  126. return;
  127. dev_info(&pdev->dev, "Cleaning VQ pending queue (%u)\n",
  128. cptvf->nr_queues);
  129. free_pending_queues(&cptvf->pqinfo);
  130. }
  131. static void free_command_queues(struct cpt_vf *cptvf,
  132. struct command_qinfo *cqinfo)
  133. {
  134. int i;
  135. struct command_queue *queue = NULL;
  136. struct command_chunk *chunk = NULL;
  137. struct pci_dev *pdev = cptvf->pdev;
  138. struct hlist_node *node;
  139. /* clean up for each queue */
  140. for (i = 0; i < cptvf->nr_queues; i++) {
  141. queue = &cqinfo->queue[i];
  142. if (hlist_empty(&cqinfo->queue[i].chead))
  143. continue;
  144. hlist_for_each_entry_safe(chunk, node, &cqinfo->queue[i].chead,
  145. nextchunk) {
  146. dma_free_coherent(&pdev->dev, chunk->size,
  147. chunk->head,
  148. chunk->dma_addr);
  149. chunk->head = NULL;
  150. chunk->dma_addr = 0;
  151. hlist_del(&chunk->nextchunk);
  152. kzfree(chunk);
  153. }
  154. queue->nchunks = 0;
  155. queue->idx = 0;
  156. }
  157. /* common cleanup */
  158. cqinfo->cmd_size = 0;
  159. }
  160. static int alloc_command_queues(struct cpt_vf *cptvf,
  161. struct command_qinfo *cqinfo, size_t cmd_size,
  162. u32 qlen)
  163. {
  164. int i;
  165. size_t q_size;
  166. struct command_queue *queue = NULL;
  167. struct pci_dev *pdev = cptvf->pdev;
  168. /* common init */
  169. cqinfo->cmd_size = cmd_size;
  170. /* Qsize in dwords, needed for SADDR config, 1-next chunk pointer */
  171. cptvf->qsize = min(qlen, cqinfo->qchunksize) *
  172. CPT_NEXT_CHUNK_PTR_SIZE + 1;
  173. /* Qsize in bytes to create space for alignment */
  174. q_size = qlen * cqinfo->cmd_size;
  175. /* per queue initialization */
  176. for (i = 0; i < cptvf->nr_queues; i++) {
  177. size_t c_size = 0;
  178. size_t rem_q_size = q_size;
  179. struct command_chunk *curr = NULL, *first = NULL, *last = NULL;
  180. u32 qcsize_bytes = cqinfo->qchunksize * cqinfo->cmd_size;
  181. queue = &cqinfo->queue[i];
  182. INIT_HLIST_HEAD(&cqinfo->queue[i].chead);
  183. do {
  184. curr = kzalloc(sizeof(*curr), GFP_KERNEL);
  185. if (!curr)
  186. goto cmd_qfail;
  187. c_size = (rem_q_size > qcsize_bytes) ? qcsize_bytes :
  188. rem_q_size;
  189. curr->head = (u8 *)dma_zalloc_coherent(&pdev->dev,
  190. c_size + CPT_NEXT_CHUNK_PTR_SIZE,
  191. &curr->dma_addr, GFP_KERNEL);
  192. if (!curr->head) {
  193. dev_err(&pdev->dev, "Command Q (%d) chunk (%d) allocation failed\n",
  194. i, queue->nchunks);
  195. kfree(curr);
  196. goto cmd_qfail;
  197. }
  198. curr->size = c_size;
  199. if (queue->nchunks == 0) {
  200. hlist_add_head(&curr->nextchunk,
  201. &cqinfo->queue[i].chead);
  202. first = curr;
  203. } else {
  204. hlist_add_behind(&curr->nextchunk,
  205. &last->nextchunk);
  206. }
  207. queue->nchunks++;
  208. rem_q_size -= c_size;
  209. if (last)
  210. *((u64 *)(&last->head[last->size])) = (u64)curr->dma_addr;
  211. last = curr;
  212. } while (rem_q_size);
  213. /* Make the queue circular */
  214. /* Tie back last chunk entry to head */
  215. curr = first;
  216. *((u64 *)(&last->head[last->size])) = (u64)curr->dma_addr;
  217. queue->qhead = curr;
  218. spin_lock_init(&queue->lock);
  219. }
  220. return 0;
  221. cmd_qfail:
  222. free_command_queues(cptvf, cqinfo);
  223. return -ENOMEM;
  224. }
  225. static int init_command_queues(struct cpt_vf *cptvf, u32 qlen)
  226. {
  227. struct pci_dev *pdev = cptvf->pdev;
  228. int ret;
  229. /* setup AE command queues */
  230. ret = alloc_command_queues(cptvf, &cptvf->cqinfo, CPT_INST_SIZE,
  231. qlen);
  232. if (ret) {
  233. dev_err(&pdev->dev, "failed to allocate AE command queues (%u)\n",
  234. cptvf->nr_queues);
  235. return ret;
  236. }
  237. return ret;
  238. }
  239. static void cleanup_command_queues(struct cpt_vf *cptvf)
  240. {
  241. struct pci_dev *pdev = cptvf->pdev;
  242. if (!cptvf->nr_queues)
  243. return;
  244. dev_info(&pdev->dev, "Cleaning VQ command queue (%u)\n",
  245. cptvf->nr_queues);
  246. free_command_queues(cptvf, &cptvf->cqinfo);
  247. }
  248. static void cptvf_sw_cleanup(struct cpt_vf *cptvf)
  249. {
  250. cleanup_worker_threads(cptvf);
  251. cleanup_pending_queues(cptvf);
  252. cleanup_command_queues(cptvf);
  253. }
  254. static int cptvf_sw_init(struct cpt_vf *cptvf, u32 qlen, u32 nr_queues)
  255. {
  256. struct pci_dev *pdev = cptvf->pdev;
  257. int ret = 0;
  258. u32 max_dev_queues = 0;
  259. max_dev_queues = CPT_NUM_QS_PER_VF;
  260. /* possible cpus */
  261. nr_queues = min_t(u32, nr_queues, max_dev_queues);
  262. cptvf->nr_queues = nr_queues;
  263. ret = init_command_queues(cptvf, qlen);
  264. if (ret) {
  265. dev_err(&pdev->dev, "Failed to setup command queues (%u)\n",
  266. nr_queues);
  267. return ret;
  268. }
  269. ret = init_pending_queues(cptvf, qlen, nr_queues);
  270. if (ret) {
  271. dev_err(&pdev->dev, "Failed to setup pending queues (%u)\n",
  272. nr_queues);
  273. goto setup_pqfail;
  274. }
  275. /* Create worker threads for BH processing */
  276. ret = init_worker_threads(cptvf);
  277. if (ret) {
  278. dev_err(&pdev->dev, "Failed to setup worker threads\n");
  279. goto init_work_fail;
  280. }
  281. return 0;
  282. init_work_fail:
  283. cleanup_worker_threads(cptvf);
  284. cleanup_pending_queues(cptvf);
  285. setup_pqfail:
  286. cleanup_command_queues(cptvf);
  287. return ret;
  288. }
  289. static void cptvf_free_irq_affinity(struct cpt_vf *cptvf, int vec)
  290. {
  291. irq_set_affinity_hint(pci_irq_vector(cptvf->pdev, vec), NULL);
  292. free_cpumask_var(cptvf->affinity_mask[vec]);
  293. }
  294. static void cptvf_write_vq_ctl(struct cpt_vf *cptvf, bool val)
  295. {
  296. union cptx_vqx_ctl vqx_ctl;
  297. vqx_ctl.u = cpt_read_csr64(cptvf->reg_base, CPTX_VQX_CTL(0, 0));
  298. vqx_ctl.s.ena = val;
  299. cpt_write_csr64(cptvf->reg_base, CPTX_VQX_CTL(0, 0), vqx_ctl.u);
  300. }
  301. void cptvf_write_vq_doorbell(struct cpt_vf *cptvf, u32 val)
  302. {
  303. union cptx_vqx_doorbell vqx_dbell;
  304. vqx_dbell.u = cpt_read_csr64(cptvf->reg_base,
  305. CPTX_VQX_DOORBELL(0, 0));
  306. vqx_dbell.s.dbell_cnt = val * 8; /* Num of Instructions * 8 words */
  307. cpt_write_csr64(cptvf->reg_base, CPTX_VQX_DOORBELL(0, 0),
  308. vqx_dbell.u);
  309. }
  310. static void cptvf_write_vq_inprog(struct cpt_vf *cptvf, u8 val)
  311. {
  312. union cptx_vqx_inprog vqx_inprg;
  313. vqx_inprg.u = cpt_read_csr64(cptvf->reg_base, CPTX_VQX_INPROG(0, 0));
  314. vqx_inprg.s.inflight = val;
  315. cpt_write_csr64(cptvf->reg_base, CPTX_VQX_INPROG(0, 0), vqx_inprg.u);
  316. }
  317. static void cptvf_write_vq_done_numwait(struct cpt_vf *cptvf, u32 val)
  318. {
  319. union cptx_vqx_done_wait vqx_dwait;
  320. vqx_dwait.u = cpt_read_csr64(cptvf->reg_base,
  321. CPTX_VQX_DONE_WAIT(0, 0));
  322. vqx_dwait.s.num_wait = val;
  323. cpt_write_csr64(cptvf->reg_base, CPTX_VQX_DONE_WAIT(0, 0),
  324. vqx_dwait.u);
  325. }
  326. static void cptvf_write_vq_done_timewait(struct cpt_vf *cptvf, u16 time)
  327. {
  328. union cptx_vqx_done_wait vqx_dwait;
  329. vqx_dwait.u = cpt_read_csr64(cptvf->reg_base,
  330. CPTX_VQX_DONE_WAIT(0, 0));
  331. vqx_dwait.s.time_wait = time;
  332. cpt_write_csr64(cptvf->reg_base, CPTX_VQX_DONE_WAIT(0, 0),
  333. vqx_dwait.u);
  334. }
  335. static void cptvf_enable_swerr_interrupts(struct cpt_vf *cptvf)
  336. {
  337. union cptx_vqx_misc_ena_w1s vqx_misc_ena;
  338. vqx_misc_ena.u = cpt_read_csr64(cptvf->reg_base,
  339. CPTX_VQX_MISC_ENA_W1S(0, 0));
  340. /* Set mbox(0) interupts for the requested vf */
  341. vqx_misc_ena.s.swerr = 1;
  342. cpt_write_csr64(cptvf->reg_base, CPTX_VQX_MISC_ENA_W1S(0, 0),
  343. vqx_misc_ena.u);
  344. }
  345. static void cptvf_enable_mbox_interrupts(struct cpt_vf *cptvf)
  346. {
  347. union cptx_vqx_misc_ena_w1s vqx_misc_ena;
  348. vqx_misc_ena.u = cpt_read_csr64(cptvf->reg_base,
  349. CPTX_VQX_MISC_ENA_W1S(0, 0));
  350. /* Set mbox(0) interupts for the requested vf */
  351. vqx_misc_ena.s.mbox = 1;
  352. cpt_write_csr64(cptvf->reg_base, CPTX_VQX_MISC_ENA_W1S(0, 0),
  353. vqx_misc_ena.u);
  354. }
  355. static void cptvf_enable_done_interrupts(struct cpt_vf *cptvf)
  356. {
  357. union cptx_vqx_done_ena_w1s vqx_done_ena;
  358. vqx_done_ena.u = cpt_read_csr64(cptvf->reg_base,
  359. CPTX_VQX_DONE_ENA_W1S(0, 0));
  360. /* Set DONE interrupt for the requested vf */
  361. vqx_done_ena.s.done = 1;
  362. cpt_write_csr64(cptvf->reg_base, CPTX_VQX_DONE_ENA_W1S(0, 0),
  363. vqx_done_ena.u);
  364. }
  365. static void cptvf_clear_dovf_intr(struct cpt_vf *cptvf)
  366. {
  367. union cptx_vqx_misc_int vqx_misc_int;
  368. vqx_misc_int.u = cpt_read_csr64(cptvf->reg_base,
  369. CPTX_VQX_MISC_INT(0, 0));
  370. /* W1C for the VF */
  371. vqx_misc_int.s.dovf = 1;
  372. cpt_write_csr64(cptvf->reg_base, CPTX_VQX_MISC_INT(0, 0),
  373. vqx_misc_int.u);
  374. }
  375. static void cptvf_clear_irde_intr(struct cpt_vf *cptvf)
  376. {
  377. union cptx_vqx_misc_int vqx_misc_int;
  378. vqx_misc_int.u = cpt_read_csr64(cptvf->reg_base,
  379. CPTX_VQX_MISC_INT(0, 0));
  380. /* W1C for the VF */
  381. vqx_misc_int.s.irde = 1;
  382. cpt_write_csr64(cptvf->reg_base, CPTX_VQX_MISC_INT(0, 0),
  383. vqx_misc_int.u);
  384. }
  385. static void cptvf_clear_nwrp_intr(struct cpt_vf *cptvf)
  386. {
  387. union cptx_vqx_misc_int vqx_misc_int;
  388. vqx_misc_int.u = cpt_read_csr64(cptvf->reg_base,
  389. CPTX_VQX_MISC_INT(0, 0));
  390. /* W1C for the VF */
  391. vqx_misc_int.s.nwrp = 1;
  392. cpt_write_csr64(cptvf->reg_base,
  393. CPTX_VQX_MISC_INT(0, 0), vqx_misc_int.u);
  394. }
  395. static void cptvf_clear_mbox_intr(struct cpt_vf *cptvf)
  396. {
  397. union cptx_vqx_misc_int vqx_misc_int;
  398. vqx_misc_int.u = cpt_read_csr64(cptvf->reg_base,
  399. CPTX_VQX_MISC_INT(0, 0));
  400. /* W1C for the VF */
  401. vqx_misc_int.s.mbox = 1;
  402. cpt_write_csr64(cptvf->reg_base, CPTX_VQX_MISC_INT(0, 0),
  403. vqx_misc_int.u);
  404. }
  405. static void cptvf_clear_swerr_intr(struct cpt_vf *cptvf)
  406. {
  407. union cptx_vqx_misc_int vqx_misc_int;
  408. vqx_misc_int.u = cpt_read_csr64(cptvf->reg_base,
  409. CPTX_VQX_MISC_INT(0, 0));
  410. /* W1C for the VF */
  411. vqx_misc_int.s.swerr = 1;
  412. cpt_write_csr64(cptvf->reg_base, CPTX_VQX_MISC_INT(0, 0),
  413. vqx_misc_int.u);
  414. }
  415. static u64 cptvf_read_vf_misc_intr_status(struct cpt_vf *cptvf)
  416. {
  417. return cpt_read_csr64(cptvf->reg_base, CPTX_VQX_MISC_INT(0, 0));
  418. }
  419. static irqreturn_t cptvf_misc_intr_handler(int irq, void *cptvf_irq)
  420. {
  421. struct cpt_vf *cptvf = (struct cpt_vf *)cptvf_irq;
  422. struct pci_dev *pdev = cptvf->pdev;
  423. u64 intr;
  424. intr = cptvf_read_vf_misc_intr_status(cptvf);
  425. /*Check for MISC interrupt types*/
  426. if (likely(intr & CPT_VF_INTR_MBOX_MASK)) {
  427. dev_dbg(&pdev->dev, "Mailbox interrupt 0x%llx on CPT VF %d\n",
  428. intr, cptvf->vfid);
  429. cptvf_handle_mbox_intr(cptvf);
  430. cptvf_clear_mbox_intr(cptvf);
  431. } else if (unlikely(intr & CPT_VF_INTR_DOVF_MASK)) {
  432. cptvf_clear_dovf_intr(cptvf);
  433. /*Clear doorbell count*/
  434. cptvf_write_vq_doorbell(cptvf, 0);
  435. dev_err(&pdev->dev, "Doorbell overflow error interrupt 0x%llx on CPT VF %d\n",
  436. intr, cptvf->vfid);
  437. } else if (unlikely(intr & CPT_VF_INTR_IRDE_MASK)) {
  438. cptvf_clear_irde_intr(cptvf);
  439. dev_err(&pdev->dev, "Instruction NCB read error interrupt 0x%llx on CPT VF %d\n",
  440. intr, cptvf->vfid);
  441. } else if (unlikely(intr & CPT_VF_INTR_NWRP_MASK)) {
  442. cptvf_clear_nwrp_intr(cptvf);
  443. dev_err(&pdev->dev, "NCB response write error interrupt 0x%llx on CPT VF %d\n",
  444. intr, cptvf->vfid);
  445. } else if (unlikely(intr & CPT_VF_INTR_SERR_MASK)) {
  446. cptvf_clear_swerr_intr(cptvf);
  447. dev_err(&pdev->dev, "Software error interrupt 0x%llx on CPT VF %d\n",
  448. intr, cptvf->vfid);
  449. } else {
  450. dev_err(&pdev->dev, "Unhandled interrupt in CPT VF %d\n",
  451. cptvf->vfid);
  452. }
  453. return IRQ_HANDLED;
  454. }
  455. static inline struct cptvf_wqe *get_cptvf_vq_wqe(struct cpt_vf *cptvf,
  456. int qno)
  457. {
  458. struct cptvf_wqe_info *nwqe_info;
  459. if (unlikely(qno >= cptvf->nr_queues))
  460. return NULL;
  461. nwqe_info = (struct cptvf_wqe_info *)cptvf->wqe_info;
  462. return &nwqe_info->vq_wqe[qno];
  463. }
  464. static inline u32 cptvf_read_vq_done_count(struct cpt_vf *cptvf)
  465. {
  466. union cptx_vqx_done vqx_done;
  467. vqx_done.u = cpt_read_csr64(cptvf->reg_base, CPTX_VQX_DONE(0, 0));
  468. return vqx_done.s.done;
  469. }
  470. static inline void cptvf_write_vq_done_ack(struct cpt_vf *cptvf,
  471. u32 ackcnt)
  472. {
  473. union cptx_vqx_done_ack vqx_dack_cnt;
  474. vqx_dack_cnt.u = cpt_read_csr64(cptvf->reg_base,
  475. CPTX_VQX_DONE_ACK(0, 0));
  476. vqx_dack_cnt.s.done_ack = ackcnt;
  477. cpt_write_csr64(cptvf->reg_base, CPTX_VQX_DONE_ACK(0, 0),
  478. vqx_dack_cnt.u);
  479. }
  480. static irqreturn_t cptvf_done_intr_handler(int irq, void *cptvf_irq)
  481. {
  482. struct cpt_vf *cptvf = (struct cpt_vf *)cptvf_irq;
  483. struct pci_dev *pdev = cptvf->pdev;
  484. /* Read the number of completions */
  485. u32 intr = cptvf_read_vq_done_count(cptvf);
  486. if (intr) {
  487. struct cptvf_wqe *wqe;
  488. /* Acknowledge the number of
  489. * scheduled completions for processing
  490. */
  491. cptvf_write_vq_done_ack(cptvf, intr);
  492. wqe = get_cptvf_vq_wqe(cptvf, 0);
  493. if (unlikely(!wqe)) {
  494. dev_err(&pdev->dev, "No work to schedule for VF (%d)",
  495. cptvf->vfid);
  496. return IRQ_NONE;
  497. }
  498. tasklet_hi_schedule(&wqe->twork);
  499. }
  500. return IRQ_HANDLED;
  501. }
  502. static void cptvf_set_irq_affinity(struct cpt_vf *cptvf, int vec)
  503. {
  504. struct pci_dev *pdev = cptvf->pdev;
  505. int cpu;
  506. if (!zalloc_cpumask_var(&cptvf->affinity_mask[vec],
  507. GFP_KERNEL)) {
  508. dev_err(&pdev->dev, "Allocation failed for affinity_mask for VF %d",
  509. cptvf->vfid);
  510. return;
  511. }
  512. cpu = cptvf->vfid % num_online_cpus();
  513. cpumask_set_cpu(cpumask_local_spread(cpu, cptvf->node),
  514. cptvf->affinity_mask[vec]);
  515. irq_set_affinity_hint(pci_irq_vector(pdev, vec),
  516. cptvf->affinity_mask[vec]);
  517. }
  518. static void cptvf_write_vq_saddr(struct cpt_vf *cptvf, u64 val)
  519. {
  520. union cptx_vqx_saddr vqx_saddr;
  521. vqx_saddr.u = val;
  522. cpt_write_csr64(cptvf->reg_base, CPTX_VQX_SADDR(0, 0), vqx_saddr.u);
  523. }
  524. void cptvf_device_init(struct cpt_vf *cptvf)
  525. {
  526. u64 base_addr = 0;
  527. /* Disable the VQ */
  528. cptvf_write_vq_ctl(cptvf, 0);
  529. /* Reset the doorbell */
  530. cptvf_write_vq_doorbell(cptvf, 0);
  531. /* Clear inflight */
  532. cptvf_write_vq_inprog(cptvf, 0);
  533. /* Write VQ SADDR */
  534. /* TODO: for now only one queue, so hard coded */
  535. base_addr = (u64)(cptvf->cqinfo.queue[0].qhead->dma_addr);
  536. cptvf_write_vq_saddr(cptvf, base_addr);
  537. /* Configure timerhold / coalescence */
  538. cptvf_write_vq_done_timewait(cptvf, CPT_TIMER_THOLD);
  539. cptvf_write_vq_done_numwait(cptvf, 1);
  540. /* Enable the VQ */
  541. cptvf_write_vq_ctl(cptvf, 1);
  542. /* Flag the VF ready */
  543. cptvf->flags |= CPT_FLAG_DEVICE_READY;
  544. }
  545. static int cptvf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  546. {
  547. struct device *dev = &pdev->dev;
  548. struct cpt_vf *cptvf;
  549. int err;
  550. cptvf = devm_kzalloc(dev, sizeof(*cptvf), GFP_KERNEL);
  551. if (!cptvf)
  552. return -ENOMEM;
  553. pci_set_drvdata(pdev, cptvf);
  554. cptvf->pdev = pdev;
  555. err = pci_enable_device(pdev);
  556. if (err) {
  557. dev_err(dev, "Failed to enable PCI device\n");
  558. pci_set_drvdata(pdev, NULL);
  559. return err;
  560. }
  561. err = pci_request_regions(pdev, DRV_NAME);
  562. if (err) {
  563. dev_err(dev, "PCI request regions failed 0x%x\n", err);
  564. goto cptvf_err_disable_device;
  565. }
  566. /* Mark as VF driver */
  567. cptvf->flags |= CPT_FLAG_VF_DRIVER;
  568. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(48));
  569. if (err) {
  570. dev_err(dev, "Unable to get usable DMA configuration\n");
  571. goto cptvf_err_release_regions;
  572. }
  573. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(48));
  574. if (err) {
  575. dev_err(dev, "Unable to get 48-bit DMA for consistent allocations\n");
  576. goto cptvf_err_release_regions;
  577. }
  578. /* MAP PF's configuration registers */
  579. cptvf->reg_base = pcim_iomap(pdev, 0, 0);
  580. if (!cptvf->reg_base) {
  581. dev_err(dev, "Cannot map config register space, aborting\n");
  582. err = -ENOMEM;
  583. goto cptvf_err_release_regions;
  584. }
  585. cptvf->node = dev_to_node(&pdev->dev);
  586. err = pci_alloc_irq_vectors(pdev, CPT_VF_MSIX_VECTORS,
  587. CPT_VF_MSIX_VECTORS, PCI_IRQ_MSIX);
  588. if (err < 0) {
  589. dev_err(dev, "Request for #%d msix vectors failed\n",
  590. CPT_VF_MSIX_VECTORS);
  591. goto cptvf_err_release_regions;
  592. }
  593. err = request_irq(pci_irq_vector(pdev, CPT_VF_INT_VEC_E_MISC),
  594. cptvf_misc_intr_handler, 0, "CPT VF misc intr",
  595. cptvf);
  596. if (err) {
  597. dev_err(dev, "Request misc irq failed");
  598. goto cptvf_free_vectors;
  599. }
  600. /* Enable mailbox interrupt */
  601. cptvf_enable_mbox_interrupts(cptvf);
  602. cptvf_enable_swerr_interrupts(cptvf);
  603. /* Check ready with PF */
  604. /* Gets chip ID / device Id from PF if ready */
  605. err = cptvf_check_pf_ready(cptvf);
  606. if (err) {
  607. dev_err(dev, "PF not responding to READY msg");
  608. goto cptvf_free_misc_irq;
  609. }
  610. /* CPT VF software resources initialization */
  611. cptvf->cqinfo.qchunksize = CPT_CMD_QCHUNK_SIZE;
  612. err = cptvf_sw_init(cptvf, CPT_CMD_QLEN, CPT_NUM_QS_PER_VF);
  613. if (err) {
  614. dev_err(dev, "cptvf_sw_init() failed");
  615. goto cptvf_free_misc_irq;
  616. }
  617. /* Convey VQ LEN to PF */
  618. err = cptvf_send_vq_size_msg(cptvf);
  619. if (err) {
  620. dev_err(dev, "PF not responding to QLEN msg");
  621. goto cptvf_free_misc_irq;
  622. }
  623. /* CPT VF device initialization */
  624. cptvf_device_init(cptvf);
  625. /* Send msg to PF to assign currnet Q to required group */
  626. cptvf->vfgrp = 1;
  627. err = cptvf_send_vf_to_grp_msg(cptvf);
  628. if (err) {
  629. dev_err(dev, "PF not responding to VF_GRP msg");
  630. goto cptvf_free_misc_irq;
  631. }
  632. cptvf->priority = 1;
  633. err = cptvf_send_vf_priority_msg(cptvf);
  634. if (err) {
  635. dev_err(dev, "PF not responding to VF_PRIO msg");
  636. goto cptvf_free_misc_irq;
  637. }
  638. err = request_irq(pci_irq_vector(pdev, CPT_VF_INT_VEC_E_DONE),
  639. cptvf_done_intr_handler, 0, "CPT VF done intr",
  640. cptvf);
  641. if (err) {
  642. dev_err(dev, "Request done irq failed\n");
  643. goto cptvf_free_misc_irq;
  644. }
  645. /* Enable mailbox interrupt */
  646. cptvf_enable_done_interrupts(cptvf);
  647. /* Set irq affinity masks */
  648. cptvf_set_irq_affinity(cptvf, CPT_VF_INT_VEC_E_MISC);
  649. cptvf_set_irq_affinity(cptvf, CPT_VF_INT_VEC_E_DONE);
  650. err = cptvf_send_vf_up(cptvf);
  651. if (err) {
  652. dev_err(dev, "PF not responding to UP msg");
  653. goto cptvf_free_irq_affinity;
  654. }
  655. err = cvm_crypto_init(cptvf);
  656. if (err) {
  657. dev_err(dev, "Algorithm register failed\n");
  658. goto cptvf_free_irq_affinity;
  659. }
  660. return 0;
  661. cptvf_free_irq_affinity:
  662. cptvf_free_irq_affinity(cptvf, CPT_VF_INT_VEC_E_DONE);
  663. cptvf_free_irq_affinity(cptvf, CPT_VF_INT_VEC_E_MISC);
  664. cptvf_free_misc_irq:
  665. free_irq(pci_irq_vector(pdev, CPT_VF_INT_VEC_E_MISC), cptvf);
  666. cptvf_free_vectors:
  667. pci_free_irq_vectors(cptvf->pdev);
  668. cptvf_err_release_regions:
  669. pci_release_regions(pdev);
  670. cptvf_err_disable_device:
  671. pci_disable_device(pdev);
  672. pci_set_drvdata(pdev, NULL);
  673. return err;
  674. }
  675. static void cptvf_remove(struct pci_dev *pdev)
  676. {
  677. struct cpt_vf *cptvf = pci_get_drvdata(pdev);
  678. if (!cptvf) {
  679. dev_err(&pdev->dev, "Invalid CPT-VF device\n");
  680. return;
  681. }
  682. /* Convey DOWN to PF */
  683. if (cptvf_send_vf_down(cptvf)) {
  684. dev_err(&pdev->dev, "PF not responding to DOWN msg");
  685. } else {
  686. cptvf_free_irq_affinity(cptvf, CPT_VF_INT_VEC_E_DONE);
  687. cptvf_free_irq_affinity(cptvf, CPT_VF_INT_VEC_E_MISC);
  688. free_irq(pci_irq_vector(pdev, CPT_VF_INT_VEC_E_DONE), cptvf);
  689. free_irq(pci_irq_vector(pdev, CPT_VF_INT_VEC_E_MISC), cptvf);
  690. pci_free_irq_vectors(cptvf->pdev);
  691. cptvf_sw_cleanup(cptvf);
  692. pci_set_drvdata(pdev, NULL);
  693. pci_release_regions(pdev);
  694. pci_disable_device(pdev);
  695. cvm_crypto_exit();
  696. }
  697. }
  698. static void cptvf_shutdown(struct pci_dev *pdev)
  699. {
  700. cptvf_remove(pdev);
  701. }
  702. /* Supported devices */
  703. static const struct pci_device_id cptvf_id_table[] = {
  704. {PCI_VDEVICE(CAVIUM, CPT_81XX_PCI_VF_DEVICE_ID), 0},
  705. { 0, } /* end of table */
  706. };
  707. static struct pci_driver cptvf_pci_driver = {
  708. .name = DRV_NAME,
  709. .id_table = cptvf_id_table,
  710. .probe = cptvf_probe,
  711. .remove = cptvf_remove,
  712. .shutdown = cptvf_shutdown,
  713. };
  714. module_pci_driver(cptvf_pci_driver);
  715. MODULE_AUTHOR("George Cherian <george.cherian@cavium.com>");
  716. MODULE_DESCRIPTION("Cavium Thunder CPT Virtual Function Driver");
  717. MODULE_LICENSE("GPL v2");
  718. MODULE_VERSION(DRV_VERSION);
  719. MODULE_DEVICE_TABLE(pci, cptvf_id_table);