nicvf_queues.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. /*
  2. * Copyright (C) 2015 Cavium, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License
  6. * as published by the Free Software Foundation.
  7. */
  8. #include <linux/pci.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/ip.h>
  11. #include <linux/etherdevice.h>
  12. #include <net/ip.h>
  13. #include <net/tso.h>
  14. #include "nic_reg.h"
  15. #include "nic.h"
  16. #include "q_struct.h"
  17. #include "nicvf_queues.h"
  18. static void nicvf_get_page(struct nicvf *nic)
  19. {
  20. if (!nic->rb_pageref || !nic->rb_page)
  21. return;
  22. page_ref_add(nic->rb_page, nic->rb_pageref);
  23. nic->rb_pageref = 0;
  24. }
  25. /* Poll a register for a specific value */
  26. static int nicvf_poll_reg(struct nicvf *nic, int qidx,
  27. u64 reg, int bit_pos, int bits, int val)
  28. {
  29. u64 bit_mask;
  30. u64 reg_val;
  31. int timeout = 10;
  32. bit_mask = (1ULL << bits) - 1;
  33. bit_mask = (bit_mask << bit_pos);
  34. while (timeout) {
  35. reg_val = nicvf_queue_reg_read(nic, reg, qidx);
  36. if (((reg_val & bit_mask) >> bit_pos) == val)
  37. return 0;
  38. usleep_range(1000, 2000);
  39. timeout--;
  40. }
  41. netdev_err(nic->netdev, "Poll on reg 0x%llx failed\n", reg);
  42. return 1;
  43. }
  44. /* Allocate memory for a queue's descriptors */
  45. static int nicvf_alloc_q_desc_mem(struct nicvf *nic, struct q_desc_mem *dmem,
  46. int q_len, int desc_size, int align_bytes)
  47. {
  48. dmem->q_len = q_len;
  49. dmem->size = (desc_size * q_len) + align_bytes;
  50. /* Save address, need it while freeing */
  51. dmem->unalign_base = dma_zalloc_coherent(&nic->pdev->dev, dmem->size,
  52. &dmem->dma, GFP_KERNEL);
  53. if (!dmem->unalign_base)
  54. return -ENOMEM;
  55. /* Align memory address for 'align_bytes' */
  56. dmem->phys_base = NICVF_ALIGNED_ADDR((u64)dmem->dma, align_bytes);
  57. dmem->base = dmem->unalign_base + (dmem->phys_base - dmem->dma);
  58. return 0;
  59. }
  60. /* Free queue's descriptor memory */
  61. static void nicvf_free_q_desc_mem(struct nicvf *nic, struct q_desc_mem *dmem)
  62. {
  63. if (!dmem)
  64. return;
  65. dma_free_coherent(&nic->pdev->dev, dmem->size,
  66. dmem->unalign_base, dmem->dma);
  67. dmem->unalign_base = NULL;
  68. dmem->base = NULL;
  69. }
  70. /* Allocate buffer for packet reception
  71. * HW returns memory address where packet is DMA'ed but not a pointer
  72. * into RBDR ring, so save buffer address at the start of fragment and
  73. * align the start address to a cache aligned address
  74. */
  75. static inline int nicvf_alloc_rcv_buffer(struct nicvf *nic, gfp_t gfp,
  76. u32 buf_len, u64 **rbuf)
  77. {
  78. int order = (PAGE_SIZE <= 4096) ? PAGE_ALLOC_COSTLY_ORDER : 0;
  79. /* Check if request can be accomodated in previous allocated page */
  80. if (nic->rb_page &&
  81. ((nic->rb_page_offset + buf_len) < (PAGE_SIZE << order))) {
  82. nic->rb_pageref++;
  83. goto ret;
  84. }
  85. nicvf_get_page(nic);
  86. nic->rb_page = NULL;
  87. /* Allocate a new page */
  88. if (!nic->rb_page) {
  89. nic->rb_page = alloc_pages(gfp | __GFP_COMP | __GFP_NOWARN,
  90. order);
  91. if (!nic->rb_page) {
  92. this_cpu_inc(nic->pnicvf->drv_stats->
  93. rcv_buffer_alloc_failures);
  94. return -ENOMEM;
  95. }
  96. nic->rb_page_offset = 0;
  97. }
  98. ret:
  99. *rbuf = (u64 *)((u64)page_address(nic->rb_page) + nic->rb_page_offset);
  100. nic->rb_page_offset += buf_len;
  101. return 0;
  102. }
  103. /* Build skb around receive buffer */
  104. static struct sk_buff *nicvf_rb_ptr_to_skb(struct nicvf *nic,
  105. u64 rb_ptr, int len)
  106. {
  107. void *data;
  108. struct sk_buff *skb;
  109. data = phys_to_virt(rb_ptr);
  110. /* Now build an skb to give to stack */
  111. skb = build_skb(data, RCV_FRAG_LEN);
  112. if (!skb) {
  113. put_page(virt_to_page(data));
  114. return NULL;
  115. }
  116. prefetch(skb->data);
  117. return skb;
  118. }
  119. /* Allocate RBDR ring and populate receive buffers */
  120. static int nicvf_init_rbdr(struct nicvf *nic, struct rbdr *rbdr,
  121. int ring_len, int buf_size)
  122. {
  123. int idx;
  124. u64 *rbuf;
  125. struct rbdr_entry_t *desc;
  126. int err;
  127. err = nicvf_alloc_q_desc_mem(nic, &rbdr->dmem, ring_len,
  128. sizeof(struct rbdr_entry_t),
  129. NICVF_RCV_BUF_ALIGN_BYTES);
  130. if (err)
  131. return err;
  132. rbdr->desc = rbdr->dmem.base;
  133. /* Buffer size has to be in multiples of 128 bytes */
  134. rbdr->dma_size = buf_size;
  135. rbdr->enable = true;
  136. rbdr->thresh = RBDR_THRESH;
  137. nic->rb_page = NULL;
  138. for (idx = 0; idx < ring_len; idx++) {
  139. err = nicvf_alloc_rcv_buffer(nic, GFP_KERNEL, RCV_FRAG_LEN,
  140. &rbuf);
  141. if (err)
  142. return err;
  143. desc = GET_RBDR_DESC(rbdr, idx);
  144. desc->buf_addr = virt_to_phys(rbuf) >> NICVF_RCV_BUF_ALIGN;
  145. }
  146. nicvf_get_page(nic);
  147. return 0;
  148. }
  149. /* Free RBDR ring and its receive buffers */
  150. static void nicvf_free_rbdr(struct nicvf *nic, struct rbdr *rbdr)
  151. {
  152. int head, tail;
  153. u64 buf_addr;
  154. struct rbdr_entry_t *desc;
  155. if (!rbdr)
  156. return;
  157. rbdr->enable = false;
  158. if (!rbdr->dmem.base)
  159. return;
  160. head = rbdr->head;
  161. tail = rbdr->tail;
  162. /* Free SKBs */
  163. while (head != tail) {
  164. desc = GET_RBDR_DESC(rbdr, head);
  165. buf_addr = desc->buf_addr << NICVF_RCV_BUF_ALIGN;
  166. put_page(virt_to_page(phys_to_virt(buf_addr)));
  167. head++;
  168. head &= (rbdr->dmem.q_len - 1);
  169. }
  170. /* Free SKB of tail desc */
  171. desc = GET_RBDR_DESC(rbdr, tail);
  172. buf_addr = desc->buf_addr << NICVF_RCV_BUF_ALIGN;
  173. put_page(virt_to_page(phys_to_virt(buf_addr)));
  174. /* Free RBDR ring */
  175. nicvf_free_q_desc_mem(nic, &rbdr->dmem);
  176. }
  177. /* Refill receive buffer descriptors with new buffers.
  178. */
  179. static void nicvf_refill_rbdr(struct nicvf *nic, gfp_t gfp)
  180. {
  181. struct queue_set *qs = nic->qs;
  182. int rbdr_idx = qs->rbdr_cnt;
  183. int tail, qcount;
  184. int refill_rb_cnt;
  185. struct rbdr *rbdr;
  186. struct rbdr_entry_t *desc;
  187. u64 *rbuf;
  188. int new_rb = 0;
  189. refill:
  190. if (!rbdr_idx)
  191. return;
  192. rbdr_idx--;
  193. rbdr = &qs->rbdr[rbdr_idx];
  194. /* Check if it's enabled */
  195. if (!rbdr->enable)
  196. goto next_rbdr;
  197. /* Get no of desc's to be refilled */
  198. qcount = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_STATUS0, rbdr_idx);
  199. qcount &= 0x7FFFF;
  200. /* Doorbell can be ringed with a max of ring size minus 1 */
  201. if (qcount >= (qs->rbdr_len - 1))
  202. goto next_rbdr;
  203. else
  204. refill_rb_cnt = qs->rbdr_len - qcount - 1;
  205. /* Start filling descs from tail */
  206. tail = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_TAIL, rbdr_idx) >> 3;
  207. while (refill_rb_cnt) {
  208. tail++;
  209. tail &= (rbdr->dmem.q_len - 1);
  210. if (nicvf_alloc_rcv_buffer(nic, gfp, RCV_FRAG_LEN, &rbuf))
  211. break;
  212. desc = GET_RBDR_DESC(rbdr, tail);
  213. desc->buf_addr = virt_to_phys(rbuf) >> NICVF_RCV_BUF_ALIGN;
  214. refill_rb_cnt--;
  215. new_rb++;
  216. }
  217. nicvf_get_page(nic);
  218. /* make sure all memory stores are done before ringing doorbell */
  219. smp_wmb();
  220. /* Check if buffer allocation failed */
  221. if (refill_rb_cnt)
  222. nic->rb_alloc_fail = true;
  223. else
  224. nic->rb_alloc_fail = false;
  225. /* Notify HW */
  226. nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_DOOR,
  227. rbdr_idx, new_rb);
  228. next_rbdr:
  229. /* Re-enable RBDR interrupts only if buffer allocation is success */
  230. if (!nic->rb_alloc_fail && rbdr->enable &&
  231. netif_running(nic->pnicvf->netdev))
  232. nicvf_enable_intr(nic, NICVF_INTR_RBDR, rbdr_idx);
  233. if (rbdr_idx)
  234. goto refill;
  235. }
  236. /* Alloc rcv buffers in non-atomic mode for better success */
  237. void nicvf_rbdr_work(struct work_struct *work)
  238. {
  239. struct nicvf *nic = container_of(work, struct nicvf, rbdr_work.work);
  240. nicvf_refill_rbdr(nic, GFP_KERNEL);
  241. if (nic->rb_alloc_fail)
  242. schedule_delayed_work(&nic->rbdr_work, msecs_to_jiffies(10));
  243. else
  244. nic->rb_work_scheduled = false;
  245. }
  246. /* In Softirq context, alloc rcv buffers in atomic mode */
  247. void nicvf_rbdr_task(unsigned long data)
  248. {
  249. struct nicvf *nic = (struct nicvf *)data;
  250. nicvf_refill_rbdr(nic, GFP_ATOMIC);
  251. if (nic->rb_alloc_fail) {
  252. nic->rb_work_scheduled = true;
  253. schedule_delayed_work(&nic->rbdr_work, msecs_to_jiffies(10));
  254. }
  255. }
  256. /* Initialize completion queue */
  257. static int nicvf_init_cmp_queue(struct nicvf *nic,
  258. struct cmp_queue *cq, int q_len)
  259. {
  260. int err;
  261. err = nicvf_alloc_q_desc_mem(nic, &cq->dmem, q_len, CMP_QUEUE_DESC_SIZE,
  262. NICVF_CQ_BASE_ALIGN_BYTES);
  263. if (err)
  264. return err;
  265. cq->desc = cq->dmem.base;
  266. cq->thresh = pass1_silicon(nic->pdev) ? 0 : CMP_QUEUE_CQE_THRESH;
  267. nic->cq_coalesce_usecs = (CMP_QUEUE_TIMER_THRESH * 0.05) - 1;
  268. return 0;
  269. }
  270. static void nicvf_free_cmp_queue(struct nicvf *nic, struct cmp_queue *cq)
  271. {
  272. if (!cq)
  273. return;
  274. if (!cq->dmem.base)
  275. return;
  276. nicvf_free_q_desc_mem(nic, &cq->dmem);
  277. }
  278. /* Initialize transmit queue */
  279. static int nicvf_init_snd_queue(struct nicvf *nic,
  280. struct snd_queue *sq, int q_len)
  281. {
  282. int err;
  283. err = nicvf_alloc_q_desc_mem(nic, &sq->dmem, q_len, SND_QUEUE_DESC_SIZE,
  284. NICVF_SQ_BASE_ALIGN_BYTES);
  285. if (err)
  286. return err;
  287. sq->desc = sq->dmem.base;
  288. sq->skbuff = kcalloc(q_len, sizeof(u64), GFP_KERNEL);
  289. if (!sq->skbuff)
  290. return -ENOMEM;
  291. sq->head = 0;
  292. sq->tail = 0;
  293. atomic_set(&sq->free_cnt, q_len - 1);
  294. sq->thresh = SND_QUEUE_THRESH;
  295. /* Preallocate memory for TSO segment's header */
  296. sq->tso_hdrs = dma_alloc_coherent(&nic->pdev->dev,
  297. q_len * TSO_HEADER_SIZE,
  298. &sq->tso_hdrs_phys, GFP_KERNEL);
  299. if (!sq->tso_hdrs)
  300. return -ENOMEM;
  301. return 0;
  302. }
  303. static void nicvf_free_snd_queue(struct nicvf *nic, struct snd_queue *sq)
  304. {
  305. struct sk_buff *skb;
  306. if (!sq)
  307. return;
  308. if (!sq->dmem.base)
  309. return;
  310. if (sq->tso_hdrs)
  311. dma_free_coherent(&nic->pdev->dev,
  312. sq->dmem.q_len * TSO_HEADER_SIZE,
  313. sq->tso_hdrs, sq->tso_hdrs_phys);
  314. /* Free pending skbs in the queue */
  315. smp_rmb();
  316. while (sq->head != sq->tail) {
  317. skb = (struct sk_buff *)sq->skbuff[sq->head];
  318. if (skb)
  319. dev_kfree_skb_any(skb);
  320. sq->head++;
  321. sq->head &= (sq->dmem.q_len - 1);
  322. }
  323. kfree(sq->skbuff);
  324. nicvf_free_q_desc_mem(nic, &sq->dmem);
  325. }
  326. static void nicvf_reclaim_snd_queue(struct nicvf *nic,
  327. struct queue_set *qs, int qidx)
  328. {
  329. /* Disable send queue */
  330. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, 0);
  331. /* Check if SQ is stopped */
  332. if (nicvf_poll_reg(nic, qidx, NIC_QSET_SQ_0_7_STATUS, 21, 1, 0x01))
  333. return;
  334. /* Reset send queue */
  335. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, NICVF_SQ_RESET);
  336. }
  337. static void nicvf_reclaim_rcv_queue(struct nicvf *nic,
  338. struct queue_set *qs, int qidx)
  339. {
  340. union nic_mbx mbx = {};
  341. /* Make sure all packets in the pipeline are written back into mem */
  342. mbx.msg.msg = NIC_MBOX_MSG_RQ_SW_SYNC;
  343. nicvf_send_msg_to_pf(nic, &mbx);
  344. }
  345. static void nicvf_reclaim_cmp_queue(struct nicvf *nic,
  346. struct queue_set *qs, int qidx)
  347. {
  348. /* Disable timer threshold (doesn't get reset upon CQ reset */
  349. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG2, qidx, 0);
  350. /* Disable completion queue */
  351. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, 0);
  352. /* Reset completion queue */
  353. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, NICVF_CQ_RESET);
  354. }
  355. static void nicvf_reclaim_rbdr(struct nicvf *nic,
  356. struct rbdr *rbdr, int qidx)
  357. {
  358. u64 tmp, fifo_state;
  359. int timeout = 10;
  360. /* Save head and tail pointers for feeing up buffers */
  361. rbdr->head = nicvf_queue_reg_read(nic,
  362. NIC_QSET_RBDR_0_1_HEAD,
  363. qidx) >> 3;
  364. rbdr->tail = nicvf_queue_reg_read(nic,
  365. NIC_QSET_RBDR_0_1_TAIL,
  366. qidx) >> 3;
  367. /* If RBDR FIFO is in 'FAIL' state then do a reset first
  368. * before relaiming.
  369. */
  370. fifo_state = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_STATUS0, qidx);
  371. if (((fifo_state >> 62) & 0x03) == 0x3)
  372. nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
  373. qidx, NICVF_RBDR_RESET);
  374. /* Disable RBDR */
  375. nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG, qidx, 0);
  376. if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x00))
  377. return;
  378. while (1) {
  379. tmp = nicvf_queue_reg_read(nic,
  380. NIC_QSET_RBDR_0_1_PREFETCH_STATUS,
  381. qidx);
  382. if ((tmp & 0xFFFFFFFF) == ((tmp >> 32) & 0xFFFFFFFF))
  383. break;
  384. usleep_range(1000, 2000);
  385. timeout--;
  386. if (!timeout) {
  387. netdev_err(nic->netdev,
  388. "Failed polling on prefetch status\n");
  389. return;
  390. }
  391. }
  392. nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
  393. qidx, NICVF_RBDR_RESET);
  394. if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x02))
  395. return;
  396. nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG, qidx, 0x00);
  397. if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x00))
  398. return;
  399. }
  400. void nicvf_config_vlan_stripping(struct nicvf *nic, netdev_features_t features)
  401. {
  402. u64 rq_cfg;
  403. int sqs;
  404. rq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_RQ_GEN_CFG, 0);
  405. /* Enable first VLAN stripping */
  406. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  407. rq_cfg |= (1ULL << 25);
  408. else
  409. rq_cfg &= ~(1ULL << 25);
  410. nicvf_queue_reg_write(nic, NIC_QSET_RQ_GEN_CFG, 0, rq_cfg);
  411. /* Configure Secondary Qsets, if any */
  412. for (sqs = 0; sqs < nic->sqs_count; sqs++)
  413. if (nic->snicvf[sqs])
  414. nicvf_queue_reg_write(nic->snicvf[sqs],
  415. NIC_QSET_RQ_GEN_CFG, 0, rq_cfg);
  416. }
  417. static void nicvf_reset_rcv_queue_stats(struct nicvf *nic)
  418. {
  419. union nic_mbx mbx = {};
  420. /* Reset all RQ/SQ and VF stats */
  421. mbx.reset_stat.msg = NIC_MBOX_MSG_RESET_STAT_COUNTER;
  422. mbx.reset_stat.rx_stat_mask = 0x3FFF;
  423. mbx.reset_stat.tx_stat_mask = 0x1F;
  424. mbx.reset_stat.rq_stat_mask = 0xFFFF;
  425. mbx.reset_stat.sq_stat_mask = 0xFFFF;
  426. nicvf_send_msg_to_pf(nic, &mbx);
  427. }
  428. /* Configures receive queue */
  429. static void nicvf_rcv_queue_config(struct nicvf *nic, struct queue_set *qs,
  430. int qidx, bool enable)
  431. {
  432. union nic_mbx mbx = {};
  433. struct rcv_queue *rq;
  434. struct rq_cfg rq_cfg;
  435. rq = &qs->rq[qidx];
  436. rq->enable = enable;
  437. /* Disable receive queue */
  438. nicvf_queue_reg_write(nic, NIC_QSET_RQ_0_7_CFG, qidx, 0);
  439. if (!rq->enable) {
  440. nicvf_reclaim_rcv_queue(nic, qs, qidx);
  441. return;
  442. }
  443. rq->cq_qs = qs->vnic_id;
  444. rq->cq_idx = qidx;
  445. rq->start_rbdr_qs = qs->vnic_id;
  446. rq->start_qs_rbdr_idx = qs->rbdr_cnt - 1;
  447. rq->cont_rbdr_qs = qs->vnic_id;
  448. rq->cont_qs_rbdr_idx = qs->rbdr_cnt - 1;
  449. /* all writes of RBDR data to be loaded into L2 Cache as well*/
  450. rq->caching = 1;
  451. /* Send a mailbox msg to PF to config RQ */
  452. mbx.rq.msg = NIC_MBOX_MSG_RQ_CFG;
  453. mbx.rq.qs_num = qs->vnic_id;
  454. mbx.rq.rq_num = qidx;
  455. mbx.rq.cfg = (rq->caching << 26) | (rq->cq_qs << 19) |
  456. (rq->cq_idx << 16) | (rq->cont_rbdr_qs << 9) |
  457. (rq->cont_qs_rbdr_idx << 8) |
  458. (rq->start_rbdr_qs << 1) | (rq->start_qs_rbdr_idx);
  459. nicvf_send_msg_to_pf(nic, &mbx);
  460. mbx.rq.msg = NIC_MBOX_MSG_RQ_BP_CFG;
  461. mbx.rq.cfg = BIT_ULL(63) | BIT_ULL(62) |
  462. (RQ_PASS_RBDR_LVL << 16) | (RQ_PASS_CQ_LVL << 8) |
  463. (qs->vnic_id << 0);
  464. nicvf_send_msg_to_pf(nic, &mbx);
  465. /* RQ drop config
  466. * Enable CQ drop to reserve sufficient CQEs for all tx packets
  467. */
  468. mbx.rq.msg = NIC_MBOX_MSG_RQ_DROP_CFG;
  469. mbx.rq.cfg = BIT_ULL(63) | BIT_ULL(62) |
  470. (RQ_PASS_RBDR_LVL << 40) | (RQ_DROP_RBDR_LVL << 32) |
  471. (RQ_PASS_CQ_LVL << 16) | (RQ_DROP_CQ_LVL << 8);
  472. nicvf_send_msg_to_pf(nic, &mbx);
  473. if (!nic->sqs_mode && (qidx == 0)) {
  474. /* Enable checking L3/L4 length and TCP/UDP checksums */
  475. nicvf_queue_reg_write(nic, NIC_QSET_RQ_GEN_CFG, 0,
  476. (BIT(24) | BIT(23) | BIT(21)));
  477. nicvf_config_vlan_stripping(nic, nic->netdev->features);
  478. }
  479. /* Enable Receive queue */
  480. memset(&rq_cfg, 0, sizeof(struct rq_cfg));
  481. rq_cfg.ena = 1;
  482. rq_cfg.tcp_ena = 0;
  483. nicvf_queue_reg_write(nic, NIC_QSET_RQ_0_7_CFG, qidx, *(u64 *)&rq_cfg);
  484. }
  485. /* Configures completion queue */
  486. void nicvf_cmp_queue_config(struct nicvf *nic, struct queue_set *qs,
  487. int qidx, bool enable)
  488. {
  489. struct cmp_queue *cq;
  490. struct cq_cfg cq_cfg;
  491. cq = &qs->cq[qidx];
  492. cq->enable = enable;
  493. if (!cq->enable) {
  494. nicvf_reclaim_cmp_queue(nic, qs, qidx);
  495. return;
  496. }
  497. /* Reset completion queue */
  498. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, NICVF_CQ_RESET);
  499. if (!cq->enable)
  500. return;
  501. spin_lock_init(&cq->lock);
  502. /* Set completion queue base address */
  503. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_BASE,
  504. qidx, (u64)(cq->dmem.phys_base));
  505. /* Enable Completion queue */
  506. memset(&cq_cfg, 0, sizeof(struct cq_cfg));
  507. cq_cfg.ena = 1;
  508. cq_cfg.reset = 0;
  509. cq_cfg.caching = 0;
  510. cq_cfg.qsize = CMP_QSIZE;
  511. cq_cfg.avg_con = 0;
  512. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, *(u64 *)&cq_cfg);
  513. /* Set threshold value for interrupt generation */
  514. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_THRESH, qidx, cq->thresh);
  515. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG2,
  516. qidx, CMP_QUEUE_TIMER_THRESH);
  517. }
  518. /* Configures transmit queue */
  519. static void nicvf_snd_queue_config(struct nicvf *nic, struct queue_set *qs,
  520. int qidx, bool enable)
  521. {
  522. union nic_mbx mbx = {};
  523. struct snd_queue *sq;
  524. struct sq_cfg sq_cfg;
  525. sq = &qs->sq[qidx];
  526. sq->enable = enable;
  527. if (!sq->enable) {
  528. nicvf_reclaim_snd_queue(nic, qs, qidx);
  529. return;
  530. }
  531. /* Reset send queue */
  532. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, NICVF_SQ_RESET);
  533. sq->cq_qs = qs->vnic_id;
  534. sq->cq_idx = qidx;
  535. /* Send a mailbox msg to PF to config SQ */
  536. mbx.sq.msg = NIC_MBOX_MSG_SQ_CFG;
  537. mbx.sq.qs_num = qs->vnic_id;
  538. mbx.sq.sq_num = qidx;
  539. mbx.sq.sqs_mode = nic->sqs_mode;
  540. mbx.sq.cfg = (sq->cq_qs << 3) | sq->cq_idx;
  541. nicvf_send_msg_to_pf(nic, &mbx);
  542. /* Set queue base address */
  543. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_BASE,
  544. qidx, (u64)(sq->dmem.phys_base));
  545. /* Enable send queue & set queue size */
  546. memset(&sq_cfg, 0, sizeof(struct sq_cfg));
  547. sq_cfg.ena = 1;
  548. sq_cfg.reset = 0;
  549. sq_cfg.ldwb = 0;
  550. sq_cfg.qsize = SND_QSIZE;
  551. sq_cfg.tstmp_bgx_intf = 0;
  552. sq_cfg.cq_limit = 0;
  553. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, *(u64 *)&sq_cfg);
  554. /* Set threshold value for interrupt generation */
  555. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_THRESH, qidx, sq->thresh);
  556. /* Set queue:cpu affinity for better load distribution */
  557. if (cpu_online(qidx)) {
  558. cpumask_set_cpu(qidx, &sq->affinity_mask);
  559. netif_set_xps_queue(nic->netdev,
  560. &sq->affinity_mask, qidx);
  561. }
  562. }
  563. /* Configures receive buffer descriptor ring */
  564. static void nicvf_rbdr_config(struct nicvf *nic, struct queue_set *qs,
  565. int qidx, bool enable)
  566. {
  567. struct rbdr *rbdr;
  568. struct rbdr_cfg rbdr_cfg;
  569. rbdr = &qs->rbdr[qidx];
  570. nicvf_reclaim_rbdr(nic, rbdr, qidx);
  571. if (!enable)
  572. return;
  573. /* Set descriptor base address */
  574. nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_BASE,
  575. qidx, (u64)(rbdr->dmem.phys_base));
  576. /* Enable RBDR & set queue size */
  577. /* Buffer size should be in multiples of 128 bytes */
  578. memset(&rbdr_cfg, 0, sizeof(struct rbdr_cfg));
  579. rbdr_cfg.ena = 1;
  580. rbdr_cfg.reset = 0;
  581. rbdr_cfg.ldwb = 0;
  582. rbdr_cfg.qsize = RBDR_SIZE;
  583. rbdr_cfg.avg_con = 0;
  584. rbdr_cfg.lines = rbdr->dma_size / 128;
  585. nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
  586. qidx, *(u64 *)&rbdr_cfg);
  587. /* Notify HW */
  588. nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_DOOR,
  589. qidx, qs->rbdr_len - 1);
  590. /* Set threshold value for interrupt generation */
  591. nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_THRESH,
  592. qidx, rbdr->thresh - 1);
  593. }
  594. /* Requests PF to assign and enable Qset */
  595. void nicvf_qset_config(struct nicvf *nic, bool enable)
  596. {
  597. union nic_mbx mbx = {};
  598. struct queue_set *qs = nic->qs;
  599. struct qs_cfg *qs_cfg;
  600. if (!qs) {
  601. netdev_warn(nic->netdev,
  602. "Qset is still not allocated, don't init queues\n");
  603. return;
  604. }
  605. qs->enable = enable;
  606. qs->vnic_id = nic->vf_id;
  607. /* Send a mailbox msg to PF to config Qset */
  608. mbx.qs.msg = NIC_MBOX_MSG_QS_CFG;
  609. mbx.qs.num = qs->vnic_id;
  610. mbx.qs.sqs_count = nic->sqs_count;
  611. mbx.qs.cfg = 0;
  612. qs_cfg = (struct qs_cfg *)&mbx.qs.cfg;
  613. if (qs->enable) {
  614. qs_cfg->ena = 1;
  615. #ifdef __BIG_ENDIAN
  616. qs_cfg->be = 1;
  617. #endif
  618. qs_cfg->vnic = qs->vnic_id;
  619. }
  620. nicvf_send_msg_to_pf(nic, &mbx);
  621. }
  622. static void nicvf_free_resources(struct nicvf *nic)
  623. {
  624. int qidx;
  625. struct queue_set *qs = nic->qs;
  626. /* Free receive buffer descriptor ring */
  627. for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
  628. nicvf_free_rbdr(nic, &qs->rbdr[qidx]);
  629. /* Free completion queue */
  630. for (qidx = 0; qidx < qs->cq_cnt; qidx++)
  631. nicvf_free_cmp_queue(nic, &qs->cq[qidx]);
  632. /* Free send queue */
  633. for (qidx = 0; qidx < qs->sq_cnt; qidx++)
  634. nicvf_free_snd_queue(nic, &qs->sq[qidx]);
  635. }
  636. static int nicvf_alloc_resources(struct nicvf *nic)
  637. {
  638. int qidx;
  639. struct queue_set *qs = nic->qs;
  640. /* Alloc receive buffer descriptor ring */
  641. for (qidx = 0; qidx < qs->rbdr_cnt; qidx++) {
  642. if (nicvf_init_rbdr(nic, &qs->rbdr[qidx], qs->rbdr_len,
  643. DMA_BUFFER_LEN))
  644. goto alloc_fail;
  645. }
  646. /* Alloc send queue */
  647. for (qidx = 0; qidx < qs->sq_cnt; qidx++) {
  648. if (nicvf_init_snd_queue(nic, &qs->sq[qidx], qs->sq_len))
  649. goto alloc_fail;
  650. }
  651. /* Alloc completion queue */
  652. for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
  653. if (nicvf_init_cmp_queue(nic, &qs->cq[qidx], qs->cq_len))
  654. goto alloc_fail;
  655. }
  656. return 0;
  657. alloc_fail:
  658. nicvf_free_resources(nic);
  659. return -ENOMEM;
  660. }
  661. int nicvf_set_qset_resources(struct nicvf *nic)
  662. {
  663. struct queue_set *qs;
  664. qs = devm_kzalloc(&nic->pdev->dev, sizeof(*qs), GFP_KERNEL);
  665. if (!qs)
  666. return -ENOMEM;
  667. nic->qs = qs;
  668. /* Set count of each queue */
  669. qs->rbdr_cnt = DEFAULT_RBDR_CNT;
  670. qs->rq_cnt = min_t(u8, MAX_RCV_QUEUES_PER_QS, num_online_cpus());
  671. qs->sq_cnt = min_t(u8, MAX_SND_QUEUES_PER_QS, num_online_cpus());
  672. qs->cq_cnt = max_t(u8, qs->rq_cnt, qs->sq_cnt);
  673. /* Set queue lengths */
  674. qs->rbdr_len = RCV_BUF_COUNT;
  675. qs->sq_len = SND_QUEUE_LEN;
  676. qs->cq_len = CMP_QUEUE_LEN;
  677. nic->rx_queues = qs->rq_cnt;
  678. nic->tx_queues = qs->sq_cnt;
  679. return 0;
  680. }
  681. int nicvf_config_data_transfer(struct nicvf *nic, bool enable)
  682. {
  683. bool disable = false;
  684. struct queue_set *qs = nic->qs;
  685. int qidx;
  686. if (!qs)
  687. return 0;
  688. if (enable) {
  689. if (nicvf_alloc_resources(nic))
  690. return -ENOMEM;
  691. for (qidx = 0; qidx < qs->sq_cnt; qidx++)
  692. nicvf_snd_queue_config(nic, qs, qidx, enable);
  693. for (qidx = 0; qidx < qs->cq_cnt; qidx++)
  694. nicvf_cmp_queue_config(nic, qs, qidx, enable);
  695. for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
  696. nicvf_rbdr_config(nic, qs, qidx, enable);
  697. for (qidx = 0; qidx < qs->rq_cnt; qidx++)
  698. nicvf_rcv_queue_config(nic, qs, qidx, enable);
  699. } else {
  700. for (qidx = 0; qidx < qs->rq_cnt; qidx++)
  701. nicvf_rcv_queue_config(nic, qs, qidx, disable);
  702. for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
  703. nicvf_rbdr_config(nic, qs, qidx, disable);
  704. for (qidx = 0; qidx < qs->sq_cnt; qidx++)
  705. nicvf_snd_queue_config(nic, qs, qidx, disable);
  706. for (qidx = 0; qidx < qs->cq_cnt; qidx++)
  707. nicvf_cmp_queue_config(nic, qs, qidx, disable);
  708. nicvf_free_resources(nic);
  709. }
  710. /* Reset RXQ's stats.
  711. * SQ's stats will get reset automatically once SQ is reset.
  712. */
  713. nicvf_reset_rcv_queue_stats(nic);
  714. return 0;
  715. }
  716. /* Get a free desc from SQ
  717. * returns descriptor ponter & descriptor number
  718. */
  719. static inline int nicvf_get_sq_desc(struct snd_queue *sq, int desc_cnt)
  720. {
  721. int qentry;
  722. qentry = sq->tail;
  723. atomic_sub(desc_cnt, &sq->free_cnt);
  724. sq->tail += desc_cnt;
  725. sq->tail &= (sq->dmem.q_len - 1);
  726. return qentry;
  727. }
  728. /* Free descriptor back to SQ for future use */
  729. void nicvf_put_sq_desc(struct snd_queue *sq, int desc_cnt)
  730. {
  731. atomic_add(desc_cnt, &sq->free_cnt);
  732. sq->head += desc_cnt;
  733. sq->head &= (sq->dmem.q_len - 1);
  734. }
  735. static inline int nicvf_get_nxt_sqentry(struct snd_queue *sq, int qentry)
  736. {
  737. qentry++;
  738. qentry &= (sq->dmem.q_len - 1);
  739. return qentry;
  740. }
  741. void nicvf_sq_enable(struct nicvf *nic, struct snd_queue *sq, int qidx)
  742. {
  743. u64 sq_cfg;
  744. sq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, qidx);
  745. sq_cfg |= NICVF_SQ_EN;
  746. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, sq_cfg);
  747. /* Ring doorbell so that H/W restarts processing SQEs */
  748. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_DOOR, qidx, 0);
  749. }
  750. void nicvf_sq_disable(struct nicvf *nic, int qidx)
  751. {
  752. u64 sq_cfg;
  753. sq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, qidx);
  754. sq_cfg &= ~NICVF_SQ_EN;
  755. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, sq_cfg);
  756. }
  757. void nicvf_sq_free_used_descs(struct net_device *netdev, struct snd_queue *sq,
  758. int qidx)
  759. {
  760. u64 head, tail;
  761. struct sk_buff *skb;
  762. struct nicvf *nic = netdev_priv(netdev);
  763. struct sq_hdr_subdesc *hdr;
  764. head = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_HEAD, qidx) >> 4;
  765. tail = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_TAIL, qidx) >> 4;
  766. while (sq->head != head) {
  767. hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, sq->head);
  768. if (hdr->subdesc_type != SQ_DESC_TYPE_HEADER) {
  769. nicvf_put_sq_desc(sq, 1);
  770. continue;
  771. }
  772. skb = (struct sk_buff *)sq->skbuff[sq->head];
  773. if (skb)
  774. dev_kfree_skb_any(skb);
  775. atomic64_add(1, (atomic64_t *)&netdev->stats.tx_packets);
  776. atomic64_add(hdr->tot_len,
  777. (atomic64_t *)&netdev->stats.tx_bytes);
  778. nicvf_put_sq_desc(sq, hdr->subdesc_cnt + 1);
  779. }
  780. }
  781. /* Calculate no of SQ subdescriptors needed to transmit all
  782. * segments of this TSO packet.
  783. * Taken from 'Tilera network driver' with a minor modification.
  784. */
  785. static int nicvf_tso_count_subdescs(struct sk_buff *skb)
  786. {
  787. struct skb_shared_info *sh = skb_shinfo(skb);
  788. unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  789. unsigned int data_len = skb->len - sh_len;
  790. unsigned int p_len = sh->gso_size;
  791. long f_id = -1; /* id of the current fragment */
  792. long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
  793. long f_used = 0; /* bytes used from the current fragment */
  794. long n; /* size of the current piece of payload */
  795. int num_edescs = 0;
  796. int segment;
  797. for (segment = 0; segment < sh->gso_segs; segment++) {
  798. unsigned int p_used = 0;
  799. /* One edesc for header and for each piece of the payload. */
  800. for (num_edescs++; p_used < p_len; num_edescs++) {
  801. /* Advance as needed. */
  802. while (f_used >= f_size) {
  803. f_id++;
  804. f_size = skb_frag_size(&sh->frags[f_id]);
  805. f_used = 0;
  806. }
  807. /* Use bytes from the current fragment. */
  808. n = p_len - p_used;
  809. if (n > f_size - f_used)
  810. n = f_size - f_used;
  811. f_used += n;
  812. p_used += n;
  813. }
  814. /* The last segment may be less than gso_size. */
  815. data_len -= p_len;
  816. if (data_len < p_len)
  817. p_len = data_len;
  818. }
  819. /* '+ gso_segs' for SQ_HDR_SUDESCs for each segment */
  820. return num_edescs + sh->gso_segs;
  821. }
  822. #define POST_CQE_DESC_COUNT 2
  823. /* Get the number of SQ descriptors needed to xmit this skb */
  824. static int nicvf_sq_subdesc_required(struct nicvf *nic, struct sk_buff *skb)
  825. {
  826. int subdesc_cnt = MIN_SQ_DESC_PER_PKT_XMIT;
  827. if (skb_shinfo(skb)->gso_size && !nic->hw_tso) {
  828. subdesc_cnt = nicvf_tso_count_subdescs(skb);
  829. return subdesc_cnt;
  830. }
  831. /* Dummy descriptors to get TSO pkt completion notification */
  832. if (nic->t88 && nic->hw_tso && skb_shinfo(skb)->gso_size)
  833. subdesc_cnt += POST_CQE_DESC_COUNT;
  834. if (skb_shinfo(skb)->nr_frags)
  835. subdesc_cnt += skb_shinfo(skb)->nr_frags;
  836. return subdesc_cnt;
  837. }
  838. /* Add SQ HEADER subdescriptor.
  839. * First subdescriptor for every send descriptor.
  840. */
  841. static inline void
  842. nicvf_sq_add_hdr_subdesc(struct nicvf *nic, struct snd_queue *sq, int qentry,
  843. int subdesc_cnt, struct sk_buff *skb, int len)
  844. {
  845. int proto;
  846. struct sq_hdr_subdesc *hdr;
  847. hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, qentry);
  848. memset(hdr, 0, SND_QUEUE_DESC_SIZE);
  849. hdr->subdesc_type = SQ_DESC_TYPE_HEADER;
  850. if (nic->t88 && nic->hw_tso && skb_shinfo(skb)->gso_size) {
  851. /* post_cqe = 0, to avoid HW posting a CQE for every TSO
  852. * segment transmitted on 88xx.
  853. */
  854. hdr->subdesc_cnt = subdesc_cnt - POST_CQE_DESC_COUNT;
  855. } else {
  856. sq->skbuff[qentry] = (u64)skb;
  857. /* Enable notification via CQE after processing SQE */
  858. hdr->post_cqe = 1;
  859. /* No of subdescriptors following this */
  860. hdr->subdesc_cnt = subdesc_cnt;
  861. }
  862. hdr->tot_len = len;
  863. /* Offload checksum calculation to HW */
  864. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  865. hdr->csum_l3 = 1; /* Enable IP csum calculation */
  866. hdr->l3_offset = skb_network_offset(skb);
  867. hdr->l4_offset = skb_transport_offset(skb);
  868. proto = ip_hdr(skb)->protocol;
  869. switch (proto) {
  870. case IPPROTO_TCP:
  871. hdr->csum_l4 = SEND_L4_CSUM_TCP;
  872. break;
  873. case IPPROTO_UDP:
  874. hdr->csum_l4 = SEND_L4_CSUM_UDP;
  875. break;
  876. case IPPROTO_SCTP:
  877. hdr->csum_l4 = SEND_L4_CSUM_SCTP;
  878. break;
  879. }
  880. }
  881. if (nic->hw_tso && skb_shinfo(skb)->gso_size) {
  882. hdr->tso = 1;
  883. hdr->tso_start = skb_transport_offset(skb) + tcp_hdrlen(skb);
  884. hdr->tso_max_paysize = skb_shinfo(skb)->gso_size;
  885. /* For non-tunneled pkts, point this to L2 ethertype */
  886. hdr->inner_l3_offset = skb_network_offset(skb) - 2;
  887. this_cpu_inc(nic->pnicvf->drv_stats->tx_tso);
  888. }
  889. }
  890. /* SQ GATHER subdescriptor
  891. * Must follow HDR descriptor
  892. */
  893. static inline void nicvf_sq_add_gather_subdesc(struct snd_queue *sq, int qentry,
  894. int size, u64 data)
  895. {
  896. struct sq_gather_subdesc *gather;
  897. qentry &= (sq->dmem.q_len - 1);
  898. gather = (struct sq_gather_subdesc *)GET_SQ_DESC(sq, qentry);
  899. memset(gather, 0, SND_QUEUE_DESC_SIZE);
  900. gather->subdesc_type = SQ_DESC_TYPE_GATHER;
  901. gather->ld_type = NIC_SEND_LD_TYPE_E_LDD;
  902. gather->size = size;
  903. gather->addr = data;
  904. }
  905. /* Add HDR + IMMEDIATE subdescriptors right after descriptors of a TSO
  906. * packet so that a CQE is posted as a notifation for transmission of
  907. * TSO packet.
  908. */
  909. static inline void nicvf_sq_add_cqe_subdesc(struct snd_queue *sq, int qentry,
  910. int tso_sqe, struct sk_buff *skb)
  911. {
  912. struct sq_imm_subdesc *imm;
  913. struct sq_hdr_subdesc *hdr;
  914. sq->skbuff[qentry] = (u64)skb;
  915. hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, qentry);
  916. memset(hdr, 0, SND_QUEUE_DESC_SIZE);
  917. hdr->subdesc_type = SQ_DESC_TYPE_HEADER;
  918. /* Enable notification via CQE after processing SQE */
  919. hdr->post_cqe = 1;
  920. /* There is no packet to transmit here */
  921. hdr->dont_send = 1;
  922. hdr->subdesc_cnt = POST_CQE_DESC_COUNT - 1;
  923. hdr->tot_len = 1;
  924. /* Actual TSO header SQE index, needed for cleanup */
  925. hdr->rsvd2 = tso_sqe;
  926. qentry = nicvf_get_nxt_sqentry(sq, qentry);
  927. imm = (struct sq_imm_subdesc *)GET_SQ_DESC(sq, qentry);
  928. memset(imm, 0, SND_QUEUE_DESC_SIZE);
  929. imm->subdesc_type = SQ_DESC_TYPE_IMMEDIATE;
  930. imm->len = 1;
  931. }
  932. static inline void nicvf_sq_doorbell(struct nicvf *nic, struct sk_buff *skb,
  933. int sq_num, int desc_cnt)
  934. {
  935. struct netdev_queue *txq;
  936. txq = netdev_get_tx_queue(nic->pnicvf->netdev,
  937. skb_get_queue_mapping(skb));
  938. netdev_tx_sent_queue(txq, skb->len);
  939. /* make sure all memory stores are done before ringing doorbell */
  940. smp_wmb();
  941. /* Inform HW to xmit all TSO segments */
  942. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_DOOR,
  943. sq_num, desc_cnt);
  944. }
  945. /* Segment a TSO packet into 'gso_size' segments and append
  946. * them to SQ for transfer
  947. */
  948. static int nicvf_sq_append_tso(struct nicvf *nic, struct snd_queue *sq,
  949. int sq_num, int qentry, struct sk_buff *skb)
  950. {
  951. struct tso_t tso;
  952. int seg_subdescs = 0, desc_cnt = 0;
  953. int seg_len, total_len, data_left;
  954. int hdr_qentry = qentry;
  955. int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  956. tso_start(skb, &tso);
  957. total_len = skb->len - hdr_len;
  958. while (total_len > 0) {
  959. char *hdr;
  960. /* Save Qentry for adding HDR_SUBDESC at the end */
  961. hdr_qentry = qentry;
  962. data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
  963. total_len -= data_left;
  964. /* Add segment's header */
  965. qentry = nicvf_get_nxt_sqentry(sq, qentry);
  966. hdr = sq->tso_hdrs + qentry * TSO_HEADER_SIZE;
  967. tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
  968. nicvf_sq_add_gather_subdesc(sq, qentry, hdr_len,
  969. sq->tso_hdrs_phys +
  970. qentry * TSO_HEADER_SIZE);
  971. /* HDR_SUDESC + GATHER */
  972. seg_subdescs = 2;
  973. seg_len = hdr_len;
  974. /* Add segment's payload fragments */
  975. while (data_left > 0) {
  976. int size;
  977. size = min_t(int, tso.size, data_left);
  978. qentry = nicvf_get_nxt_sqentry(sq, qentry);
  979. nicvf_sq_add_gather_subdesc(sq, qentry, size,
  980. virt_to_phys(tso.data));
  981. seg_subdescs++;
  982. seg_len += size;
  983. data_left -= size;
  984. tso_build_data(skb, &tso, size);
  985. }
  986. nicvf_sq_add_hdr_subdesc(nic, sq, hdr_qentry,
  987. seg_subdescs - 1, skb, seg_len);
  988. sq->skbuff[hdr_qentry] = (u64)NULL;
  989. qentry = nicvf_get_nxt_sqentry(sq, qentry);
  990. desc_cnt += seg_subdescs;
  991. }
  992. /* Save SKB in the last segment for freeing */
  993. sq->skbuff[hdr_qentry] = (u64)skb;
  994. nicvf_sq_doorbell(nic, skb, sq_num, desc_cnt);
  995. this_cpu_inc(nic->pnicvf->drv_stats->tx_tso);
  996. return 1;
  997. }
  998. /* Append an skb to a SQ for packet transfer. */
  999. int nicvf_sq_append_skb(struct nicvf *nic, struct snd_queue *sq,
  1000. struct sk_buff *skb, u8 sq_num)
  1001. {
  1002. int i, size;
  1003. int subdesc_cnt, tso_sqe = 0;
  1004. int qentry;
  1005. subdesc_cnt = nicvf_sq_subdesc_required(nic, skb);
  1006. if (subdesc_cnt > atomic_read(&sq->free_cnt))
  1007. goto append_fail;
  1008. qentry = nicvf_get_sq_desc(sq, subdesc_cnt);
  1009. /* Check if its a TSO packet */
  1010. if (skb_shinfo(skb)->gso_size && !nic->hw_tso)
  1011. return nicvf_sq_append_tso(nic, sq, sq_num, qentry, skb);
  1012. /* Add SQ header subdesc */
  1013. nicvf_sq_add_hdr_subdesc(nic, sq, qentry, subdesc_cnt - 1,
  1014. skb, skb->len);
  1015. tso_sqe = qentry;
  1016. /* Add SQ gather subdescs */
  1017. qentry = nicvf_get_nxt_sqentry(sq, qentry);
  1018. size = skb_is_nonlinear(skb) ? skb_headlen(skb) : skb->len;
  1019. nicvf_sq_add_gather_subdesc(sq, qentry, size, virt_to_phys(skb->data));
  1020. /* Check for scattered buffer */
  1021. if (!skb_is_nonlinear(skb))
  1022. goto doorbell;
  1023. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1024. const struct skb_frag_struct *frag;
  1025. frag = &skb_shinfo(skb)->frags[i];
  1026. qentry = nicvf_get_nxt_sqentry(sq, qentry);
  1027. size = skb_frag_size(frag);
  1028. nicvf_sq_add_gather_subdesc(sq, qentry, size,
  1029. virt_to_phys(
  1030. skb_frag_address(frag)));
  1031. }
  1032. doorbell:
  1033. if (nic->t88 && skb_shinfo(skb)->gso_size) {
  1034. qentry = nicvf_get_nxt_sqentry(sq, qentry);
  1035. nicvf_sq_add_cqe_subdesc(sq, qentry, tso_sqe, skb);
  1036. }
  1037. nicvf_sq_doorbell(nic, skb, sq_num, subdesc_cnt);
  1038. return 1;
  1039. append_fail:
  1040. /* Use original PCI dev for debug log */
  1041. nic = nic->pnicvf;
  1042. netdev_dbg(nic->netdev, "Not enough SQ descriptors to xmit pkt\n");
  1043. return 0;
  1044. }
  1045. static inline unsigned frag_num(unsigned i)
  1046. {
  1047. #ifdef __BIG_ENDIAN
  1048. return (i & ~3) + 3 - (i & 3);
  1049. #else
  1050. return i;
  1051. #endif
  1052. }
  1053. /* Returns SKB for a received packet */
  1054. struct sk_buff *nicvf_get_rcv_skb(struct nicvf *nic, struct cqe_rx_t *cqe_rx)
  1055. {
  1056. int frag;
  1057. int payload_len = 0;
  1058. struct sk_buff *skb = NULL;
  1059. struct page *page;
  1060. int offset;
  1061. u16 *rb_lens = NULL;
  1062. u64 *rb_ptrs = NULL;
  1063. rb_lens = (void *)cqe_rx + (3 * sizeof(u64));
  1064. /* Except 88xx pass1 on all other chips CQE_RX2_S is added to
  1065. * CQE_RX at word6, hence buffer pointers move by word
  1066. *
  1067. * Use existing 'hw_tso' flag which will be set for all chips
  1068. * except 88xx pass1 instead of a additional cache line
  1069. * access (or miss) by using pci dev's revision.
  1070. */
  1071. if (!nic->hw_tso)
  1072. rb_ptrs = (void *)cqe_rx + (6 * sizeof(u64));
  1073. else
  1074. rb_ptrs = (void *)cqe_rx + (7 * sizeof(u64));
  1075. netdev_dbg(nic->netdev, "%s rb_cnt %d rb0_ptr %llx rb0_sz %d\n",
  1076. __func__, cqe_rx->rb_cnt, cqe_rx->rb0_ptr, cqe_rx->rb0_sz);
  1077. for (frag = 0; frag < cqe_rx->rb_cnt; frag++) {
  1078. payload_len = rb_lens[frag_num(frag)];
  1079. if (!frag) {
  1080. /* First fragment */
  1081. skb = nicvf_rb_ptr_to_skb(nic,
  1082. *rb_ptrs - cqe_rx->align_pad,
  1083. payload_len);
  1084. if (!skb)
  1085. return NULL;
  1086. skb_reserve(skb, cqe_rx->align_pad);
  1087. skb_put(skb, payload_len);
  1088. } else {
  1089. /* Add fragments */
  1090. page = virt_to_page(phys_to_virt(*rb_ptrs));
  1091. offset = phys_to_virt(*rb_ptrs) - page_address(page);
  1092. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
  1093. offset, payload_len, RCV_FRAG_LEN);
  1094. }
  1095. /* Next buffer pointer */
  1096. rb_ptrs++;
  1097. }
  1098. return skb;
  1099. }
  1100. static u64 nicvf_int_type_to_mask(int int_type, int q_idx)
  1101. {
  1102. u64 reg_val;
  1103. switch (int_type) {
  1104. case NICVF_INTR_CQ:
  1105. reg_val = ((1ULL << q_idx) << NICVF_INTR_CQ_SHIFT);
  1106. break;
  1107. case NICVF_INTR_SQ:
  1108. reg_val = ((1ULL << q_idx) << NICVF_INTR_SQ_SHIFT);
  1109. break;
  1110. case NICVF_INTR_RBDR:
  1111. reg_val = ((1ULL << q_idx) << NICVF_INTR_RBDR_SHIFT);
  1112. break;
  1113. case NICVF_INTR_PKT_DROP:
  1114. reg_val = (1ULL << NICVF_INTR_PKT_DROP_SHIFT);
  1115. break;
  1116. case NICVF_INTR_TCP_TIMER:
  1117. reg_val = (1ULL << NICVF_INTR_TCP_TIMER_SHIFT);
  1118. break;
  1119. case NICVF_INTR_MBOX:
  1120. reg_val = (1ULL << NICVF_INTR_MBOX_SHIFT);
  1121. break;
  1122. case NICVF_INTR_QS_ERR:
  1123. reg_val = (1ULL << NICVF_INTR_QS_ERR_SHIFT);
  1124. break;
  1125. default:
  1126. reg_val = 0;
  1127. }
  1128. return reg_val;
  1129. }
  1130. /* Enable interrupt */
  1131. void nicvf_enable_intr(struct nicvf *nic, int int_type, int q_idx)
  1132. {
  1133. u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
  1134. if (!mask) {
  1135. netdev_dbg(nic->netdev,
  1136. "Failed to enable interrupt: unknown type\n");
  1137. return;
  1138. }
  1139. nicvf_reg_write(nic, NIC_VF_ENA_W1S,
  1140. nicvf_reg_read(nic, NIC_VF_ENA_W1S) | mask);
  1141. }
  1142. /* Disable interrupt */
  1143. void nicvf_disable_intr(struct nicvf *nic, int int_type, int q_idx)
  1144. {
  1145. u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
  1146. if (!mask) {
  1147. netdev_dbg(nic->netdev,
  1148. "Failed to disable interrupt: unknown type\n");
  1149. return;
  1150. }
  1151. nicvf_reg_write(nic, NIC_VF_ENA_W1C, mask);
  1152. }
  1153. /* Clear interrupt */
  1154. void nicvf_clear_intr(struct nicvf *nic, int int_type, int q_idx)
  1155. {
  1156. u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
  1157. if (!mask) {
  1158. netdev_dbg(nic->netdev,
  1159. "Failed to clear interrupt: unknown type\n");
  1160. return;
  1161. }
  1162. nicvf_reg_write(nic, NIC_VF_INT, mask);
  1163. }
  1164. /* Check if interrupt is enabled */
  1165. int nicvf_is_intr_enabled(struct nicvf *nic, int int_type, int q_idx)
  1166. {
  1167. u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
  1168. /* If interrupt type is unknown, we treat it disabled. */
  1169. if (!mask) {
  1170. netdev_dbg(nic->netdev,
  1171. "Failed to check interrupt enable: unknown type\n");
  1172. return 0;
  1173. }
  1174. return mask & nicvf_reg_read(nic, NIC_VF_ENA_W1S);
  1175. }
  1176. void nicvf_update_rq_stats(struct nicvf *nic, int rq_idx)
  1177. {
  1178. struct rcv_queue *rq;
  1179. #define GET_RQ_STATS(reg) \
  1180. nicvf_reg_read(nic, NIC_QSET_RQ_0_7_STAT_0_1 |\
  1181. (rq_idx << NIC_Q_NUM_SHIFT) | (reg << 3))
  1182. rq = &nic->qs->rq[rq_idx];
  1183. rq->stats.bytes = GET_RQ_STATS(RQ_SQ_STATS_OCTS);
  1184. rq->stats.pkts = GET_RQ_STATS(RQ_SQ_STATS_PKTS);
  1185. }
  1186. void nicvf_update_sq_stats(struct nicvf *nic, int sq_idx)
  1187. {
  1188. struct snd_queue *sq;
  1189. #define GET_SQ_STATS(reg) \
  1190. nicvf_reg_read(nic, NIC_QSET_SQ_0_7_STAT_0_1 |\
  1191. (sq_idx << NIC_Q_NUM_SHIFT) | (reg << 3))
  1192. sq = &nic->qs->sq[sq_idx];
  1193. sq->stats.bytes = GET_SQ_STATS(RQ_SQ_STATS_OCTS);
  1194. sq->stats.pkts = GET_SQ_STATS(RQ_SQ_STATS_PKTS);
  1195. }
  1196. /* Check for errors in the receive cmp.queue entry */
  1197. int nicvf_check_cqe_rx_errs(struct nicvf *nic, struct cqe_rx_t *cqe_rx)
  1198. {
  1199. if (!cqe_rx->err_level && !cqe_rx->err_opcode)
  1200. return 0;
  1201. if (netif_msg_rx_err(nic))
  1202. netdev_err(nic->netdev,
  1203. "%s: RX error CQE err_level 0x%x err_opcode 0x%x\n",
  1204. nic->netdev->name,
  1205. cqe_rx->err_level, cqe_rx->err_opcode);
  1206. switch (cqe_rx->err_opcode) {
  1207. case CQ_RX_ERROP_RE_PARTIAL:
  1208. this_cpu_inc(nic->drv_stats->rx_bgx_truncated_pkts);
  1209. break;
  1210. case CQ_RX_ERROP_RE_JABBER:
  1211. this_cpu_inc(nic->drv_stats->rx_jabber_errs);
  1212. break;
  1213. case CQ_RX_ERROP_RE_FCS:
  1214. this_cpu_inc(nic->drv_stats->rx_fcs_errs);
  1215. break;
  1216. case CQ_RX_ERROP_RE_RX_CTL:
  1217. this_cpu_inc(nic->drv_stats->rx_bgx_errs);
  1218. break;
  1219. case CQ_RX_ERROP_PREL2_ERR:
  1220. this_cpu_inc(nic->drv_stats->rx_prel2_errs);
  1221. break;
  1222. case CQ_RX_ERROP_L2_MAL:
  1223. this_cpu_inc(nic->drv_stats->rx_l2_hdr_malformed);
  1224. break;
  1225. case CQ_RX_ERROP_L2_OVERSIZE:
  1226. this_cpu_inc(nic->drv_stats->rx_oversize);
  1227. break;
  1228. case CQ_RX_ERROP_L2_UNDERSIZE:
  1229. this_cpu_inc(nic->drv_stats->rx_undersize);
  1230. break;
  1231. case CQ_RX_ERROP_L2_LENMISM:
  1232. this_cpu_inc(nic->drv_stats->rx_l2_len_mismatch);
  1233. break;
  1234. case CQ_RX_ERROP_L2_PCLP:
  1235. this_cpu_inc(nic->drv_stats->rx_l2_pclp);
  1236. break;
  1237. case CQ_RX_ERROP_IP_NOT:
  1238. this_cpu_inc(nic->drv_stats->rx_ip_ver_errs);
  1239. break;
  1240. case CQ_RX_ERROP_IP_CSUM_ERR:
  1241. this_cpu_inc(nic->drv_stats->rx_ip_csum_errs);
  1242. break;
  1243. case CQ_RX_ERROP_IP_MAL:
  1244. this_cpu_inc(nic->drv_stats->rx_ip_hdr_malformed);
  1245. break;
  1246. case CQ_RX_ERROP_IP_MALD:
  1247. this_cpu_inc(nic->drv_stats->rx_ip_payload_malformed);
  1248. break;
  1249. case CQ_RX_ERROP_IP_HOP:
  1250. this_cpu_inc(nic->drv_stats->rx_ip_ttl_errs);
  1251. break;
  1252. case CQ_RX_ERROP_L3_PCLP:
  1253. this_cpu_inc(nic->drv_stats->rx_l3_pclp);
  1254. break;
  1255. case CQ_RX_ERROP_L4_MAL:
  1256. this_cpu_inc(nic->drv_stats->rx_l4_malformed);
  1257. break;
  1258. case CQ_RX_ERROP_L4_CHK:
  1259. this_cpu_inc(nic->drv_stats->rx_l4_csum_errs);
  1260. break;
  1261. case CQ_RX_ERROP_UDP_LEN:
  1262. this_cpu_inc(nic->drv_stats->rx_udp_len_errs);
  1263. break;
  1264. case CQ_RX_ERROP_L4_PORT:
  1265. this_cpu_inc(nic->drv_stats->rx_l4_port_errs);
  1266. break;
  1267. case CQ_RX_ERROP_TCP_FLAG:
  1268. this_cpu_inc(nic->drv_stats->rx_tcp_flag_errs);
  1269. break;
  1270. case CQ_RX_ERROP_TCP_OFFSET:
  1271. this_cpu_inc(nic->drv_stats->rx_tcp_offset_errs);
  1272. break;
  1273. case CQ_RX_ERROP_L4_PCLP:
  1274. this_cpu_inc(nic->drv_stats->rx_l4_pclp);
  1275. break;
  1276. case CQ_RX_ERROP_RBDR_TRUNC:
  1277. this_cpu_inc(nic->drv_stats->rx_truncated_pkts);
  1278. break;
  1279. }
  1280. return 1;
  1281. }
  1282. /* Check for errors in the send cmp.queue entry */
  1283. int nicvf_check_cqe_tx_errs(struct nicvf *nic, struct cqe_send_t *cqe_tx)
  1284. {
  1285. switch (cqe_tx->send_status) {
  1286. case CQ_TX_ERROP_GOOD:
  1287. return 0;
  1288. case CQ_TX_ERROP_DESC_FAULT:
  1289. this_cpu_inc(nic->drv_stats->tx_desc_fault);
  1290. break;
  1291. case CQ_TX_ERROP_HDR_CONS_ERR:
  1292. this_cpu_inc(nic->drv_stats->tx_hdr_cons_err);
  1293. break;
  1294. case CQ_TX_ERROP_SUBDC_ERR:
  1295. this_cpu_inc(nic->drv_stats->tx_subdesc_err);
  1296. break;
  1297. case CQ_TX_ERROP_MAX_SIZE_VIOL:
  1298. this_cpu_inc(nic->drv_stats->tx_max_size_exceeded);
  1299. break;
  1300. case CQ_TX_ERROP_IMM_SIZE_OFLOW:
  1301. this_cpu_inc(nic->drv_stats->tx_imm_size_oflow);
  1302. break;
  1303. case CQ_TX_ERROP_DATA_SEQUENCE_ERR:
  1304. this_cpu_inc(nic->drv_stats->tx_data_seq_err);
  1305. break;
  1306. case CQ_TX_ERROP_MEM_SEQUENCE_ERR:
  1307. this_cpu_inc(nic->drv_stats->tx_mem_seq_err);
  1308. break;
  1309. case CQ_TX_ERROP_LOCK_VIOL:
  1310. this_cpu_inc(nic->drv_stats->tx_lock_viol);
  1311. break;
  1312. case CQ_TX_ERROP_DATA_FAULT:
  1313. this_cpu_inc(nic->drv_stats->tx_data_fault);
  1314. break;
  1315. case CQ_TX_ERROP_TSTMP_CONFLICT:
  1316. this_cpu_inc(nic->drv_stats->tx_tstmp_conflict);
  1317. break;
  1318. case CQ_TX_ERROP_TSTMP_TIMEOUT:
  1319. this_cpu_inc(nic->drv_stats->tx_tstmp_timeout);
  1320. break;
  1321. case CQ_TX_ERROP_MEM_FAULT:
  1322. this_cpu_inc(nic->drv_stats->tx_mem_fault);
  1323. break;
  1324. case CQ_TX_ERROP_CK_OVERLAP:
  1325. this_cpu_inc(nic->drv_stats->tx_csum_overlap);
  1326. break;
  1327. case CQ_TX_ERROP_CK_OFLOW:
  1328. this_cpu_inc(nic->drv_stats->tx_csum_overflow);
  1329. break;
  1330. }
  1331. return 1;
  1332. }