nicvf_queues.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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 = (1ULL << 63) | (1ULL << 62) | (qs->vnic_id << 0);
  462. nicvf_send_msg_to_pf(nic, &mbx);
  463. /* RQ drop config
  464. * Enable CQ drop to reserve sufficient CQEs for all tx packets
  465. */
  466. mbx.rq.msg = NIC_MBOX_MSG_RQ_DROP_CFG;
  467. mbx.rq.cfg = (1ULL << 62) | (RQ_CQ_DROP << 8);
  468. nicvf_send_msg_to_pf(nic, &mbx);
  469. if (!nic->sqs_mode && (qidx == 0)) {
  470. /* Enable checking L3/L4 length and TCP/UDP checksums */
  471. nicvf_queue_reg_write(nic, NIC_QSET_RQ_GEN_CFG, 0,
  472. (BIT(24) | BIT(23) | BIT(21)));
  473. nicvf_config_vlan_stripping(nic, nic->netdev->features);
  474. }
  475. /* Enable Receive queue */
  476. memset(&rq_cfg, 0, sizeof(struct rq_cfg));
  477. rq_cfg.ena = 1;
  478. rq_cfg.tcp_ena = 0;
  479. nicvf_queue_reg_write(nic, NIC_QSET_RQ_0_7_CFG, qidx, *(u64 *)&rq_cfg);
  480. }
  481. /* Configures completion queue */
  482. void nicvf_cmp_queue_config(struct nicvf *nic, struct queue_set *qs,
  483. int qidx, bool enable)
  484. {
  485. struct cmp_queue *cq;
  486. struct cq_cfg cq_cfg;
  487. cq = &qs->cq[qidx];
  488. cq->enable = enable;
  489. if (!cq->enable) {
  490. nicvf_reclaim_cmp_queue(nic, qs, qidx);
  491. return;
  492. }
  493. /* Reset completion queue */
  494. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, NICVF_CQ_RESET);
  495. if (!cq->enable)
  496. return;
  497. spin_lock_init(&cq->lock);
  498. /* Set completion queue base address */
  499. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_BASE,
  500. qidx, (u64)(cq->dmem.phys_base));
  501. /* Enable Completion queue */
  502. memset(&cq_cfg, 0, sizeof(struct cq_cfg));
  503. cq_cfg.ena = 1;
  504. cq_cfg.reset = 0;
  505. cq_cfg.caching = 0;
  506. cq_cfg.qsize = CMP_QSIZE;
  507. cq_cfg.avg_con = 0;
  508. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, *(u64 *)&cq_cfg);
  509. /* Set threshold value for interrupt generation */
  510. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_THRESH, qidx, cq->thresh);
  511. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG2,
  512. qidx, CMP_QUEUE_TIMER_THRESH);
  513. }
  514. /* Configures transmit queue */
  515. static void nicvf_snd_queue_config(struct nicvf *nic, struct queue_set *qs,
  516. int qidx, bool enable)
  517. {
  518. union nic_mbx mbx = {};
  519. struct snd_queue *sq;
  520. struct sq_cfg sq_cfg;
  521. sq = &qs->sq[qidx];
  522. sq->enable = enable;
  523. if (!sq->enable) {
  524. nicvf_reclaim_snd_queue(nic, qs, qidx);
  525. return;
  526. }
  527. /* Reset send queue */
  528. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, NICVF_SQ_RESET);
  529. sq->cq_qs = qs->vnic_id;
  530. sq->cq_idx = qidx;
  531. /* Send a mailbox msg to PF to config SQ */
  532. mbx.sq.msg = NIC_MBOX_MSG_SQ_CFG;
  533. mbx.sq.qs_num = qs->vnic_id;
  534. mbx.sq.sq_num = qidx;
  535. mbx.sq.sqs_mode = nic->sqs_mode;
  536. mbx.sq.cfg = (sq->cq_qs << 3) | sq->cq_idx;
  537. nicvf_send_msg_to_pf(nic, &mbx);
  538. /* Set queue base address */
  539. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_BASE,
  540. qidx, (u64)(sq->dmem.phys_base));
  541. /* Enable send queue & set queue size */
  542. memset(&sq_cfg, 0, sizeof(struct sq_cfg));
  543. sq_cfg.ena = 1;
  544. sq_cfg.reset = 0;
  545. sq_cfg.ldwb = 0;
  546. sq_cfg.qsize = SND_QSIZE;
  547. sq_cfg.tstmp_bgx_intf = 0;
  548. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, *(u64 *)&sq_cfg);
  549. /* Set threshold value for interrupt generation */
  550. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_THRESH, qidx, sq->thresh);
  551. /* Set queue:cpu affinity for better load distribution */
  552. if (cpu_online(qidx)) {
  553. cpumask_set_cpu(qidx, &sq->affinity_mask);
  554. netif_set_xps_queue(nic->netdev,
  555. &sq->affinity_mask, qidx);
  556. }
  557. }
  558. /* Configures receive buffer descriptor ring */
  559. static void nicvf_rbdr_config(struct nicvf *nic, struct queue_set *qs,
  560. int qidx, bool enable)
  561. {
  562. struct rbdr *rbdr;
  563. struct rbdr_cfg rbdr_cfg;
  564. rbdr = &qs->rbdr[qidx];
  565. nicvf_reclaim_rbdr(nic, rbdr, qidx);
  566. if (!enable)
  567. return;
  568. /* Set descriptor base address */
  569. nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_BASE,
  570. qidx, (u64)(rbdr->dmem.phys_base));
  571. /* Enable RBDR & set queue size */
  572. /* Buffer size should be in multiples of 128 bytes */
  573. memset(&rbdr_cfg, 0, sizeof(struct rbdr_cfg));
  574. rbdr_cfg.ena = 1;
  575. rbdr_cfg.reset = 0;
  576. rbdr_cfg.ldwb = 0;
  577. rbdr_cfg.qsize = RBDR_SIZE;
  578. rbdr_cfg.avg_con = 0;
  579. rbdr_cfg.lines = rbdr->dma_size / 128;
  580. nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
  581. qidx, *(u64 *)&rbdr_cfg);
  582. /* Notify HW */
  583. nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_DOOR,
  584. qidx, qs->rbdr_len - 1);
  585. /* Set threshold value for interrupt generation */
  586. nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_THRESH,
  587. qidx, rbdr->thresh - 1);
  588. }
  589. /* Requests PF to assign and enable Qset */
  590. void nicvf_qset_config(struct nicvf *nic, bool enable)
  591. {
  592. union nic_mbx mbx = {};
  593. struct queue_set *qs = nic->qs;
  594. struct qs_cfg *qs_cfg;
  595. if (!qs) {
  596. netdev_warn(nic->netdev,
  597. "Qset is still not allocated, don't init queues\n");
  598. return;
  599. }
  600. qs->enable = enable;
  601. qs->vnic_id = nic->vf_id;
  602. /* Send a mailbox msg to PF to config Qset */
  603. mbx.qs.msg = NIC_MBOX_MSG_QS_CFG;
  604. mbx.qs.num = qs->vnic_id;
  605. mbx.qs.sqs_count = nic->sqs_count;
  606. mbx.qs.cfg = 0;
  607. qs_cfg = (struct qs_cfg *)&mbx.qs.cfg;
  608. if (qs->enable) {
  609. qs_cfg->ena = 1;
  610. #ifdef __BIG_ENDIAN
  611. qs_cfg->be = 1;
  612. #endif
  613. qs_cfg->vnic = qs->vnic_id;
  614. }
  615. nicvf_send_msg_to_pf(nic, &mbx);
  616. }
  617. static void nicvf_free_resources(struct nicvf *nic)
  618. {
  619. int qidx;
  620. struct queue_set *qs = nic->qs;
  621. /* Free receive buffer descriptor ring */
  622. for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
  623. nicvf_free_rbdr(nic, &qs->rbdr[qidx]);
  624. /* Free completion queue */
  625. for (qidx = 0; qidx < qs->cq_cnt; qidx++)
  626. nicvf_free_cmp_queue(nic, &qs->cq[qidx]);
  627. /* Free send queue */
  628. for (qidx = 0; qidx < qs->sq_cnt; qidx++)
  629. nicvf_free_snd_queue(nic, &qs->sq[qidx]);
  630. }
  631. static int nicvf_alloc_resources(struct nicvf *nic)
  632. {
  633. int qidx;
  634. struct queue_set *qs = nic->qs;
  635. /* Alloc receive buffer descriptor ring */
  636. for (qidx = 0; qidx < qs->rbdr_cnt; qidx++) {
  637. if (nicvf_init_rbdr(nic, &qs->rbdr[qidx], qs->rbdr_len,
  638. DMA_BUFFER_LEN))
  639. goto alloc_fail;
  640. }
  641. /* Alloc send queue */
  642. for (qidx = 0; qidx < qs->sq_cnt; qidx++) {
  643. if (nicvf_init_snd_queue(nic, &qs->sq[qidx], qs->sq_len))
  644. goto alloc_fail;
  645. }
  646. /* Alloc completion queue */
  647. for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
  648. if (nicvf_init_cmp_queue(nic, &qs->cq[qidx], qs->cq_len))
  649. goto alloc_fail;
  650. }
  651. return 0;
  652. alloc_fail:
  653. nicvf_free_resources(nic);
  654. return -ENOMEM;
  655. }
  656. int nicvf_set_qset_resources(struct nicvf *nic)
  657. {
  658. struct queue_set *qs;
  659. qs = devm_kzalloc(&nic->pdev->dev, sizeof(*qs), GFP_KERNEL);
  660. if (!qs)
  661. return -ENOMEM;
  662. nic->qs = qs;
  663. /* Set count of each queue */
  664. qs->rbdr_cnt = DEFAULT_RBDR_CNT;
  665. qs->rq_cnt = min_t(u8, MAX_RCV_QUEUES_PER_QS, num_online_cpus());
  666. qs->sq_cnt = min_t(u8, MAX_SND_QUEUES_PER_QS, num_online_cpus());
  667. qs->cq_cnt = max_t(u8, qs->rq_cnt, qs->sq_cnt);
  668. /* Set queue lengths */
  669. qs->rbdr_len = RCV_BUF_COUNT;
  670. qs->sq_len = SND_QUEUE_LEN;
  671. qs->cq_len = CMP_QUEUE_LEN;
  672. nic->rx_queues = qs->rq_cnt;
  673. nic->tx_queues = qs->sq_cnt;
  674. return 0;
  675. }
  676. int nicvf_config_data_transfer(struct nicvf *nic, bool enable)
  677. {
  678. bool disable = false;
  679. struct queue_set *qs = nic->qs;
  680. int qidx;
  681. if (!qs)
  682. return 0;
  683. if (enable) {
  684. if (nicvf_alloc_resources(nic))
  685. return -ENOMEM;
  686. for (qidx = 0; qidx < qs->sq_cnt; qidx++)
  687. nicvf_snd_queue_config(nic, qs, qidx, enable);
  688. for (qidx = 0; qidx < qs->cq_cnt; qidx++)
  689. nicvf_cmp_queue_config(nic, qs, qidx, enable);
  690. for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
  691. nicvf_rbdr_config(nic, qs, qidx, enable);
  692. for (qidx = 0; qidx < qs->rq_cnt; qidx++)
  693. nicvf_rcv_queue_config(nic, qs, qidx, enable);
  694. } else {
  695. for (qidx = 0; qidx < qs->rq_cnt; qidx++)
  696. nicvf_rcv_queue_config(nic, qs, qidx, disable);
  697. for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
  698. nicvf_rbdr_config(nic, qs, qidx, disable);
  699. for (qidx = 0; qidx < qs->sq_cnt; qidx++)
  700. nicvf_snd_queue_config(nic, qs, qidx, disable);
  701. for (qidx = 0; qidx < qs->cq_cnt; qidx++)
  702. nicvf_cmp_queue_config(nic, qs, qidx, disable);
  703. nicvf_free_resources(nic);
  704. }
  705. /* Reset RXQ's stats.
  706. * SQ's stats will get reset automatically once SQ is reset.
  707. */
  708. nicvf_reset_rcv_queue_stats(nic);
  709. return 0;
  710. }
  711. /* Get a free desc from SQ
  712. * returns descriptor ponter & descriptor number
  713. */
  714. static inline int nicvf_get_sq_desc(struct snd_queue *sq, int desc_cnt)
  715. {
  716. int qentry;
  717. qentry = sq->tail;
  718. atomic_sub(desc_cnt, &sq->free_cnt);
  719. sq->tail += desc_cnt;
  720. sq->tail &= (sq->dmem.q_len - 1);
  721. return qentry;
  722. }
  723. /* Free descriptor back to SQ for future use */
  724. void nicvf_put_sq_desc(struct snd_queue *sq, int desc_cnt)
  725. {
  726. atomic_add(desc_cnt, &sq->free_cnt);
  727. sq->head += desc_cnt;
  728. sq->head &= (sq->dmem.q_len - 1);
  729. }
  730. static inline int nicvf_get_nxt_sqentry(struct snd_queue *sq, int qentry)
  731. {
  732. qentry++;
  733. qentry &= (sq->dmem.q_len - 1);
  734. return qentry;
  735. }
  736. void nicvf_sq_enable(struct nicvf *nic, struct snd_queue *sq, int qidx)
  737. {
  738. u64 sq_cfg;
  739. sq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, qidx);
  740. sq_cfg |= NICVF_SQ_EN;
  741. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, sq_cfg);
  742. /* Ring doorbell so that H/W restarts processing SQEs */
  743. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_DOOR, qidx, 0);
  744. }
  745. void nicvf_sq_disable(struct nicvf *nic, int qidx)
  746. {
  747. u64 sq_cfg;
  748. sq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, qidx);
  749. sq_cfg &= ~NICVF_SQ_EN;
  750. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, sq_cfg);
  751. }
  752. void nicvf_sq_free_used_descs(struct net_device *netdev, struct snd_queue *sq,
  753. int qidx)
  754. {
  755. u64 head, tail;
  756. struct sk_buff *skb;
  757. struct nicvf *nic = netdev_priv(netdev);
  758. struct sq_hdr_subdesc *hdr;
  759. head = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_HEAD, qidx) >> 4;
  760. tail = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_TAIL, qidx) >> 4;
  761. while (sq->head != head) {
  762. hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, sq->head);
  763. if (hdr->subdesc_type != SQ_DESC_TYPE_HEADER) {
  764. nicvf_put_sq_desc(sq, 1);
  765. continue;
  766. }
  767. skb = (struct sk_buff *)sq->skbuff[sq->head];
  768. if (skb)
  769. dev_kfree_skb_any(skb);
  770. atomic64_add(1, (atomic64_t *)&netdev->stats.tx_packets);
  771. atomic64_add(hdr->tot_len,
  772. (atomic64_t *)&netdev->stats.tx_bytes);
  773. nicvf_put_sq_desc(sq, hdr->subdesc_cnt + 1);
  774. }
  775. }
  776. /* Calculate no of SQ subdescriptors needed to transmit all
  777. * segments of this TSO packet.
  778. * Taken from 'Tilera network driver' with a minor modification.
  779. */
  780. static int nicvf_tso_count_subdescs(struct sk_buff *skb)
  781. {
  782. struct skb_shared_info *sh = skb_shinfo(skb);
  783. unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  784. unsigned int data_len = skb->len - sh_len;
  785. unsigned int p_len = sh->gso_size;
  786. long f_id = -1; /* id of the current fragment */
  787. long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
  788. long f_used = 0; /* bytes used from the current fragment */
  789. long n; /* size of the current piece of payload */
  790. int num_edescs = 0;
  791. int segment;
  792. for (segment = 0; segment < sh->gso_segs; segment++) {
  793. unsigned int p_used = 0;
  794. /* One edesc for header and for each piece of the payload. */
  795. for (num_edescs++; p_used < p_len; num_edescs++) {
  796. /* Advance as needed. */
  797. while (f_used >= f_size) {
  798. f_id++;
  799. f_size = skb_frag_size(&sh->frags[f_id]);
  800. f_used = 0;
  801. }
  802. /* Use bytes from the current fragment. */
  803. n = p_len - p_used;
  804. if (n > f_size - f_used)
  805. n = f_size - f_used;
  806. f_used += n;
  807. p_used += n;
  808. }
  809. /* The last segment may be less than gso_size. */
  810. data_len -= p_len;
  811. if (data_len < p_len)
  812. p_len = data_len;
  813. }
  814. /* '+ gso_segs' for SQ_HDR_SUDESCs for each segment */
  815. return num_edescs + sh->gso_segs;
  816. }
  817. #define POST_CQE_DESC_COUNT 2
  818. /* Get the number of SQ descriptors needed to xmit this skb */
  819. static int nicvf_sq_subdesc_required(struct nicvf *nic, struct sk_buff *skb)
  820. {
  821. int subdesc_cnt = MIN_SQ_DESC_PER_PKT_XMIT;
  822. if (skb_shinfo(skb)->gso_size && !nic->hw_tso) {
  823. subdesc_cnt = nicvf_tso_count_subdescs(skb);
  824. return subdesc_cnt;
  825. }
  826. /* Dummy descriptors to get TSO pkt completion notification */
  827. if (nic->t88 && nic->hw_tso && skb_shinfo(skb)->gso_size)
  828. subdesc_cnt += POST_CQE_DESC_COUNT;
  829. if (skb_shinfo(skb)->nr_frags)
  830. subdesc_cnt += skb_shinfo(skb)->nr_frags;
  831. return subdesc_cnt;
  832. }
  833. /* Add SQ HEADER subdescriptor.
  834. * First subdescriptor for every send descriptor.
  835. */
  836. static inline void
  837. nicvf_sq_add_hdr_subdesc(struct nicvf *nic, struct snd_queue *sq, int qentry,
  838. int subdesc_cnt, struct sk_buff *skb, int len)
  839. {
  840. int proto;
  841. struct sq_hdr_subdesc *hdr;
  842. hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, qentry);
  843. memset(hdr, 0, SND_QUEUE_DESC_SIZE);
  844. hdr->subdesc_type = SQ_DESC_TYPE_HEADER;
  845. if (nic->t88 && nic->hw_tso && skb_shinfo(skb)->gso_size) {
  846. /* post_cqe = 0, to avoid HW posting a CQE for every TSO
  847. * segment transmitted on 88xx.
  848. */
  849. hdr->subdesc_cnt = subdesc_cnt - POST_CQE_DESC_COUNT;
  850. } else {
  851. sq->skbuff[qentry] = (u64)skb;
  852. /* Enable notification via CQE after processing SQE */
  853. hdr->post_cqe = 1;
  854. /* No of subdescriptors following this */
  855. hdr->subdesc_cnt = subdesc_cnt;
  856. }
  857. hdr->tot_len = len;
  858. /* Offload checksum calculation to HW */
  859. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  860. hdr->csum_l3 = 1; /* Enable IP csum calculation */
  861. hdr->l3_offset = skb_network_offset(skb);
  862. hdr->l4_offset = skb_transport_offset(skb);
  863. proto = ip_hdr(skb)->protocol;
  864. switch (proto) {
  865. case IPPROTO_TCP:
  866. hdr->csum_l4 = SEND_L4_CSUM_TCP;
  867. break;
  868. case IPPROTO_UDP:
  869. hdr->csum_l4 = SEND_L4_CSUM_UDP;
  870. break;
  871. case IPPROTO_SCTP:
  872. hdr->csum_l4 = SEND_L4_CSUM_SCTP;
  873. break;
  874. }
  875. }
  876. if (nic->hw_tso && skb_shinfo(skb)->gso_size) {
  877. hdr->tso = 1;
  878. hdr->tso_start = skb_transport_offset(skb) + tcp_hdrlen(skb);
  879. hdr->tso_max_paysize = skb_shinfo(skb)->gso_size;
  880. /* For non-tunneled pkts, point this to L2 ethertype */
  881. hdr->inner_l3_offset = skb_network_offset(skb) - 2;
  882. this_cpu_inc(nic->pnicvf->drv_stats->tx_tso);
  883. }
  884. }
  885. /* SQ GATHER subdescriptor
  886. * Must follow HDR descriptor
  887. */
  888. static inline void nicvf_sq_add_gather_subdesc(struct snd_queue *sq, int qentry,
  889. int size, u64 data)
  890. {
  891. struct sq_gather_subdesc *gather;
  892. qentry &= (sq->dmem.q_len - 1);
  893. gather = (struct sq_gather_subdesc *)GET_SQ_DESC(sq, qentry);
  894. memset(gather, 0, SND_QUEUE_DESC_SIZE);
  895. gather->subdesc_type = SQ_DESC_TYPE_GATHER;
  896. gather->ld_type = NIC_SEND_LD_TYPE_E_LDD;
  897. gather->size = size;
  898. gather->addr = data;
  899. }
  900. /* Add HDR + IMMEDIATE subdescriptors right after descriptors of a TSO
  901. * packet so that a CQE is posted as a notifation for transmission of
  902. * TSO packet.
  903. */
  904. static inline void nicvf_sq_add_cqe_subdesc(struct snd_queue *sq, int qentry,
  905. int tso_sqe, struct sk_buff *skb)
  906. {
  907. struct sq_imm_subdesc *imm;
  908. struct sq_hdr_subdesc *hdr;
  909. sq->skbuff[qentry] = (u64)skb;
  910. hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, qentry);
  911. memset(hdr, 0, SND_QUEUE_DESC_SIZE);
  912. hdr->subdesc_type = SQ_DESC_TYPE_HEADER;
  913. /* Enable notification via CQE after processing SQE */
  914. hdr->post_cqe = 1;
  915. /* There is no packet to transmit here */
  916. hdr->dont_send = 1;
  917. hdr->subdesc_cnt = POST_CQE_DESC_COUNT - 1;
  918. hdr->tot_len = 1;
  919. /* Actual TSO header SQE index, needed for cleanup */
  920. hdr->rsvd2 = tso_sqe;
  921. qentry = nicvf_get_nxt_sqentry(sq, qentry);
  922. imm = (struct sq_imm_subdesc *)GET_SQ_DESC(sq, qentry);
  923. memset(imm, 0, SND_QUEUE_DESC_SIZE);
  924. imm->subdesc_type = SQ_DESC_TYPE_IMMEDIATE;
  925. imm->len = 1;
  926. }
  927. static inline void nicvf_sq_doorbell(struct nicvf *nic, struct sk_buff *skb,
  928. int sq_num, int desc_cnt)
  929. {
  930. struct netdev_queue *txq;
  931. txq = netdev_get_tx_queue(nic->pnicvf->netdev,
  932. skb_get_queue_mapping(skb));
  933. netdev_tx_sent_queue(txq, skb->len);
  934. /* make sure all memory stores are done before ringing doorbell */
  935. smp_wmb();
  936. /* Inform HW to xmit all TSO segments */
  937. nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_DOOR,
  938. sq_num, desc_cnt);
  939. }
  940. /* Segment a TSO packet into 'gso_size' segments and append
  941. * them to SQ for transfer
  942. */
  943. static int nicvf_sq_append_tso(struct nicvf *nic, struct snd_queue *sq,
  944. int sq_num, int qentry, struct sk_buff *skb)
  945. {
  946. struct tso_t tso;
  947. int seg_subdescs = 0, desc_cnt = 0;
  948. int seg_len, total_len, data_left;
  949. int hdr_qentry = qentry;
  950. int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  951. tso_start(skb, &tso);
  952. total_len = skb->len - hdr_len;
  953. while (total_len > 0) {
  954. char *hdr;
  955. /* Save Qentry for adding HDR_SUBDESC at the end */
  956. hdr_qentry = qentry;
  957. data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
  958. total_len -= data_left;
  959. /* Add segment's header */
  960. qentry = nicvf_get_nxt_sqentry(sq, qentry);
  961. hdr = sq->tso_hdrs + qentry * TSO_HEADER_SIZE;
  962. tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
  963. nicvf_sq_add_gather_subdesc(sq, qentry, hdr_len,
  964. sq->tso_hdrs_phys +
  965. qentry * TSO_HEADER_SIZE);
  966. /* HDR_SUDESC + GATHER */
  967. seg_subdescs = 2;
  968. seg_len = hdr_len;
  969. /* Add segment's payload fragments */
  970. while (data_left > 0) {
  971. int size;
  972. size = min_t(int, tso.size, data_left);
  973. qentry = nicvf_get_nxt_sqentry(sq, qentry);
  974. nicvf_sq_add_gather_subdesc(sq, qentry, size,
  975. virt_to_phys(tso.data));
  976. seg_subdescs++;
  977. seg_len += size;
  978. data_left -= size;
  979. tso_build_data(skb, &tso, size);
  980. }
  981. nicvf_sq_add_hdr_subdesc(nic, sq, hdr_qentry,
  982. seg_subdescs - 1, skb, seg_len);
  983. sq->skbuff[hdr_qentry] = (u64)NULL;
  984. qentry = nicvf_get_nxt_sqentry(sq, qentry);
  985. desc_cnt += seg_subdescs;
  986. }
  987. /* Save SKB in the last segment for freeing */
  988. sq->skbuff[hdr_qentry] = (u64)skb;
  989. nicvf_sq_doorbell(nic, skb, sq_num, desc_cnt);
  990. this_cpu_inc(nic->pnicvf->drv_stats->tx_tso);
  991. return 1;
  992. }
  993. /* Append an skb to a SQ for packet transfer. */
  994. int nicvf_sq_append_skb(struct nicvf *nic, struct sk_buff *skb)
  995. {
  996. int i, size;
  997. int subdesc_cnt, tso_sqe = 0;
  998. int sq_num, qentry;
  999. struct queue_set *qs;
  1000. struct snd_queue *sq;
  1001. sq_num = skb_get_queue_mapping(skb);
  1002. if (sq_num >= MAX_SND_QUEUES_PER_QS) {
  1003. /* Get secondary Qset's SQ structure */
  1004. i = sq_num / MAX_SND_QUEUES_PER_QS;
  1005. if (!nic->snicvf[i - 1]) {
  1006. netdev_warn(nic->netdev,
  1007. "Secondary Qset#%d's ptr not initialized\n",
  1008. i - 1);
  1009. return 1;
  1010. }
  1011. nic = (struct nicvf *)nic->snicvf[i - 1];
  1012. sq_num = sq_num % MAX_SND_QUEUES_PER_QS;
  1013. }
  1014. qs = nic->qs;
  1015. sq = &qs->sq[sq_num];
  1016. subdesc_cnt = nicvf_sq_subdesc_required(nic, skb);
  1017. if (subdesc_cnt > atomic_read(&sq->free_cnt))
  1018. goto append_fail;
  1019. qentry = nicvf_get_sq_desc(sq, subdesc_cnt);
  1020. /* Check if its a TSO packet */
  1021. if (skb_shinfo(skb)->gso_size && !nic->hw_tso)
  1022. return nicvf_sq_append_tso(nic, sq, sq_num, qentry, skb);
  1023. /* Add SQ header subdesc */
  1024. nicvf_sq_add_hdr_subdesc(nic, sq, qentry, subdesc_cnt - 1,
  1025. skb, skb->len);
  1026. tso_sqe = qentry;
  1027. /* Add SQ gather subdescs */
  1028. qentry = nicvf_get_nxt_sqentry(sq, qentry);
  1029. size = skb_is_nonlinear(skb) ? skb_headlen(skb) : skb->len;
  1030. nicvf_sq_add_gather_subdesc(sq, qentry, size, virt_to_phys(skb->data));
  1031. /* Check for scattered buffer */
  1032. if (!skb_is_nonlinear(skb))
  1033. goto doorbell;
  1034. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1035. const struct skb_frag_struct *frag;
  1036. frag = &skb_shinfo(skb)->frags[i];
  1037. qentry = nicvf_get_nxt_sqentry(sq, qentry);
  1038. size = skb_frag_size(frag);
  1039. nicvf_sq_add_gather_subdesc(sq, qentry, size,
  1040. virt_to_phys(
  1041. skb_frag_address(frag)));
  1042. }
  1043. doorbell:
  1044. if (nic->t88 && skb_shinfo(skb)->gso_size) {
  1045. qentry = nicvf_get_nxt_sqentry(sq, qentry);
  1046. nicvf_sq_add_cqe_subdesc(sq, qentry, tso_sqe, skb);
  1047. }
  1048. nicvf_sq_doorbell(nic, skb, sq_num, subdesc_cnt);
  1049. return 1;
  1050. append_fail:
  1051. /* Use original PCI dev for debug log */
  1052. nic = nic->pnicvf;
  1053. netdev_dbg(nic->netdev, "Not enough SQ descriptors to xmit pkt\n");
  1054. return 0;
  1055. }
  1056. static inline unsigned frag_num(unsigned i)
  1057. {
  1058. #ifdef __BIG_ENDIAN
  1059. return (i & ~3) + 3 - (i & 3);
  1060. #else
  1061. return i;
  1062. #endif
  1063. }
  1064. /* Returns SKB for a received packet */
  1065. struct sk_buff *nicvf_get_rcv_skb(struct nicvf *nic, struct cqe_rx_t *cqe_rx)
  1066. {
  1067. int frag;
  1068. int payload_len = 0;
  1069. struct sk_buff *skb = NULL;
  1070. struct page *page;
  1071. int offset;
  1072. u16 *rb_lens = NULL;
  1073. u64 *rb_ptrs = NULL;
  1074. rb_lens = (void *)cqe_rx + (3 * sizeof(u64));
  1075. /* Except 88xx pass1 on all other chips CQE_RX2_S is added to
  1076. * CQE_RX at word6, hence buffer pointers move by word
  1077. *
  1078. * Use existing 'hw_tso' flag which will be set for all chips
  1079. * except 88xx pass1 instead of a additional cache line
  1080. * access (or miss) by using pci dev's revision.
  1081. */
  1082. if (!nic->hw_tso)
  1083. rb_ptrs = (void *)cqe_rx + (6 * sizeof(u64));
  1084. else
  1085. rb_ptrs = (void *)cqe_rx + (7 * sizeof(u64));
  1086. netdev_dbg(nic->netdev, "%s rb_cnt %d rb0_ptr %llx rb0_sz %d\n",
  1087. __func__, cqe_rx->rb_cnt, cqe_rx->rb0_ptr, cqe_rx->rb0_sz);
  1088. for (frag = 0; frag < cqe_rx->rb_cnt; frag++) {
  1089. payload_len = rb_lens[frag_num(frag)];
  1090. if (!frag) {
  1091. /* First fragment */
  1092. skb = nicvf_rb_ptr_to_skb(nic,
  1093. *rb_ptrs - cqe_rx->align_pad,
  1094. payload_len);
  1095. if (!skb)
  1096. return NULL;
  1097. skb_reserve(skb, cqe_rx->align_pad);
  1098. skb_put(skb, payload_len);
  1099. } else {
  1100. /* Add fragments */
  1101. page = virt_to_page(phys_to_virt(*rb_ptrs));
  1102. offset = phys_to_virt(*rb_ptrs) - page_address(page);
  1103. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
  1104. offset, payload_len, RCV_FRAG_LEN);
  1105. }
  1106. /* Next buffer pointer */
  1107. rb_ptrs++;
  1108. }
  1109. return skb;
  1110. }
  1111. static u64 nicvf_int_type_to_mask(int int_type, int q_idx)
  1112. {
  1113. u64 reg_val;
  1114. switch (int_type) {
  1115. case NICVF_INTR_CQ:
  1116. reg_val = ((1ULL << q_idx) << NICVF_INTR_CQ_SHIFT);
  1117. break;
  1118. case NICVF_INTR_SQ:
  1119. reg_val = ((1ULL << q_idx) << NICVF_INTR_SQ_SHIFT);
  1120. break;
  1121. case NICVF_INTR_RBDR:
  1122. reg_val = ((1ULL << q_idx) << NICVF_INTR_RBDR_SHIFT);
  1123. break;
  1124. case NICVF_INTR_PKT_DROP:
  1125. reg_val = (1ULL << NICVF_INTR_PKT_DROP_SHIFT);
  1126. break;
  1127. case NICVF_INTR_TCP_TIMER:
  1128. reg_val = (1ULL << NICVF_INTR_TCP_TIMER_SHIFT);
  1129. break;
  1130. case NICVF_INTR_MBOX:
  1131. reg_val = (1ULL << NICVF_INTR_MBOX_SHIFT);
  1132. break;
  1133. case NICVF_INTR_QS_ERR:
  1134. reg_val = (1ULL << NICVF_INTR_QS_ERR_SHIFT);
  1135. break;
  1136. default:
  1137. reg_val = 0;
  1138. }
  1139. return reg_val;
  1140. }
  1141. /* Enable interrupt */
  1142. void nicvf_enable_intr(struct nicvf *nic, int int_type, int q_idx)
  1143. {
  1144. u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
  1145. if (!mask) {
  1146. netdev_dbg(nic->netdev,
  1147. "Failed to enable interrupt: unknown type\n");
  1148. return;
  1149. }
  1150. nicvf_reg_write(nic, NIC_VF_ENA_W1S,
  1151. nicvf_reg_read(nic, NIC_VF_ENA_W1S) | mask);
  1152. }
  1153. /* Disable interrupt */
  1154. void nicvf_disable_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 disable interrupt: unknown type\n");
  1160. return;
  1161. }
  1162. nicvf_reg_write(nic, NIC_VF_ENA_W1C, mask);
  1163. }
  1164. /* Clear interrupt */
  1165. void nicvf_clear_intr(struct nicvf *nic, int int_type, int q_idx)
  1166. {
  1167. u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
  1168. if (!mask) {
  1169. netdev_dbg(nic->netdev,
  1170. "Failed to clear interrupt: unknown type\n");
  1171. return;
  1172. }
  1173. nicvf_reg_write(nic, NIC_VF_INT, mask);
  1174. }
  1175. /* Check if interrupt is enabled */
  1176. int nicvf_is_intr_enabled(struct nicvf *nic, int int_type, int q_idx)
  1177. {
  1178. u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
  1179. /* If interrupt type is unknown, we treat it disabled. */
  1180. if (!mask) {
  1181. netdev_dbg(nic->netdev,
  1182. "Failed to check interrupt enable: unknown type\n");
  1183. return 0;
  1184. }
  1185. return mask & nicvf_reg_read(nic, NIC_VF_ENA_W1S);
  1186. }
  1187. void nicvf_update_rq_stats(struct nicvf *nic, int rq_idx)
  1188. {
  1189. struct rcv_queue *rq;
  1190. #define GET_RQ_STATS(reg) \
  1191. nicvf_reg_read(nic, NIC_QSET_RQ_0_7_STAT_0_1 |\
  1192. (rq_idx << NIC_Q_NUM_SHIFT) | (reg << 3))
  1193. rq = &nic->qs->rq[rq_idx];
  1194. rq->stats.bytes = GET_RQ_STATS(RQ_SQ_STATS_OCTS);
  1195. rq->stats.pkts = GET_RQ_STATS(RQ_SQ_STATS_PKTS);
  1196. }
  1197. void nicvf_update_sq_stats(struct nicvf *nic, int sq_idx)
  1198. {
  1199. struct snd_queue *sq;
  1200. #define GET_SQ_STATS(reg) \
  1201. nicvf_reg_read(nic, NIC_QSET_SQ_0_7_STAT_0_1 |\
  1202. (sq_idx << NIC_Q_NUM_SHIFT) | (reg << 3))
  1203. sq = &nic->qs->sq[sq_idx];
  1204. sq->stats.bytes = GET_SQ_STATS(RQ_SQ_STATS_OCTS);
  1205. sq->stats.pkts = GET_SQ_STATS(RQ_SQ_STATS_PKTS);
  1206. }
  1207. /* Check for errors in the receive cmp.queue entry */
  1208. int nicvf_check_cqe_rx_errs(struct nicvf *nic, struct cqe_rx_t *cqe_rx)
  1209. {
  1210. if (!cqe_rx->err_level && !cqe_rx->err_opcode)
  1211. return 0;
  1212. if (netif_msg_rx_err(nic))
  1213. netdev_err(nic->netdev,
  1214. "%s: RX error CQE err_level 0x%x err_opcode 0x%x\n",
  1215. nic->netdev->name,
  1216. cqe_rx->err_level, cqe_rx->err_opcode);
  1217. switch (cqe_rx->err_opcode) {
  1218. case CQ_RX_ERROP_RE_PARTIAL:
  1219. this_cpu_inc(nic->drv_stats->rx_bgx_truncated_pkts);
  1220. break;
  1221. case CQ_RX_ERROP_RE_JABBER:
  1222. this_cpu_inc(nic->drv_stats->rx_jabber_errs);
  1223. break;
  1224. case CQ_RX_ERROP_RE_FCS:
  1225. this_cpu_inc(nic->drv_stats->rx_fcs_errs);
  1226. break;
  1227. case CQ_RX_ERROP_RE_RX_CTL:
  1228. this_cpu_inc(nic->drv_stats->rx_bgx_errs);
  1229. break;
  1230. case CQ_RX_ERROP_PREL2_ERR:
  1231. this_cpu_inc(nic->drv_stats->rx_prel2_errs);
  1232. break;
  1233. case CQ_RX_ERROP_L2_MAL:
  1234. this_cpu_inc(nic->drv_stats->rx_l2_hdr_malformed);
  1235. break;
  1236. case CQ_RX_ERROP_L2_OVERSIZE:
  1237. this_cpu_inc(nic->drv_stats->rx_oversize);
  1238. break;
  1239. case CQ_RX_ERROP_L2_UNDERSIZE:
  1240. this_cpu_inc(nic->drv_stats->rx_undersize);
  1241. break;
  1242. case CQ_RX_ERROP_L2_LENMISM:
  1243. this_cpu_inc(nic->drv_stats->rx_l2_len_mismatch);
  1244. break;
  1245. case CQ_RX_ERROP_L2_PCLP:
  1246. this_cpu_inc(nic->drv_stats->rx_l2_pclp);
  1247. break;
  1248. case CQ_RX_ERROP_IP_NOT:
  1249. this_cpu_inc(nic->drv_stats->rx_ip_ver_errs);
  1250. break;
  1251. case CQ_RX_ERROP_IP_CSUM_ERR:
  1252. this_cpu_inc(nic->drv_stats->rx_ip_csum_errs);
  1253. break;
  1254. case CQ_RX_ERROP_IP_MAL:
  1255. this_cpu_inc(nic->drv_stats->rx_ip_hdr_malformed);
  1256. break;
  1257. case CQ_RX_ERROP_IP_MALD:
  1258. this_cpu_inc(nic->drv_stats->rx_ip_payload_malformed);
  1259. break;
  1260. case CQ_RX_ERROP_IP_HOP:
  1261. this_cpu_inc(nic->drv_stats->rx_ip_ttl_errs);
  1262. break;
  1263. case CQ_RX_ERROP_L3_PCLP:
  1264. this_cpu_inc(nic->drv_stats->rx_l3_pclp);
  1265. break;
  1266. case CQ_RX_ERROP_L4_MAL:
  1267. this_cpu_inc(nic->drv_stats->rx_l4_malformed);
  1268. break;
  1269. case CQ_RX_ERROP_L4_CHK:
  1270. this_cpu_inc(nic->drv_stats->rx_l4_csum_errs);
  1271. break;
  1272. case CQ_RX_ERROP_UDP_LEN:
  1273. this_cpu_inc(nic->drv_stats->rx_udp_len_errs);
  1274. break;
  1275. case CQ_RX_ERROP_L4_PORT:
  1276. this_cpu_inc(nic->drv_stats->rx_l4_port_errs);
  1277. break;
  1278. case CQ_RX_ERROP_TCP_FLAG:
  1279. this_cpu_inc(nic->drv_stats->rx_tcp_flag_errs);
  1280. break;
  1281. case CQ_RX_ERROP_TCP_OFFSET:
  1282. this_cpu_inc(nic->drv_stats->rx_tcp_offset_errs);
  1283. break;
  1284. case CQ_RX_ERROP_L4_PCLP:
  1285. this_cpu_inc(nic->drv_stats->rx_l4_pclp);
  1286. break;
  1287. case CQ_RX_ERROP_RBDR_TRUNC:
  1288. this_cpu_inc(nic->drv_stats->rx_truncated_pkts);
  1289. break;
  1290. }
  1291. return 1;
  1292. }
  1293. /* Check for errors in the send cmp.queue entry */
  1294. int nicvf_check_cqe_tx_errs(struct nicvf *nic, struct cqe_send_t *cqe_tx)
  1295. {
  1296. switch (cqe_tx->send_status) {
  1297. case CQ_TX_ERROP_GOOD:
  1298. return 0;
  1299. case CQ_TX_ERROP_DESC_FAULT:
  1300. this_cpu_inc(nic->drv_stats->tx_desc_fault);
  1301. break;
  1302. case CQ_TX_ERROP_HDR_CONS_ERR:
  1303. this_cpu_inc(nic->drv_stats->tx_hdr_cons_err);
  1304. break;
  1305. case CQ_TX_ERROP_SUBDC_ERR:
  1306. this_cpu_inc(nic->drv_stats->tx_subdesc_err);
  1307. break;
  1308. case CQ_TX_ERROP_MAX_SIZE_VIOL:
  1309. this_cpu_inc(nic->drv_stats->tx_max_size_exceeded);
  1310. break;
  1311. case CQ_TX_ERROP_IMM_SIZE_OFLOW:
  1312. this_cpu_inc(nic->drv_stats->tx_imm_size_oflow);
  1313. break;
  1314. case CQ_TX_ERROP_DATA_SEQUENCE_ERR:
  1315. this_cpu_inc(nic->drv_stats->tx_data_seq_err);
  1316. break;
  1317. case CQ_TX_ERROP_MEM_SEQUENCE_ERR:
  1318. this_cpu_inc(nic->drv_stats->tx_mem_seq_err);
  1319. break;
  1320. case CQ_TX_ERROP_LOCK_VIOL:
  1321. this_cpu_inc(nic->drv_stats->tx_lock_viol);
  1322. break;
  1323. case CQ_TX_ERROP_DATA_FAULT:
  1324. this_cpu_inc(nic->drv_stats->tx_data_fault);
  1325. break;
  1326. case CQ_TX_ERROP_TSTMP_CONFLICT:
  1327. this_cpu_inc(nic->drv_stats->tx_tstmp_conflict);
  1328. break;
  1329. case CQ_TX_ERROP_TSTMP_TIMEOUT:
  1330. this_cpu_inc(nic->drv_stats->tx_tstmp_timeout);
  1331. break;
  1332. case CQ_TX_ERROP_MEM_FAULT:
  1333. this_cpu_inc(nic->drv_stats->tx_mem_fault);
  1334. break;
  1335. case CQ_TX_ERROP_CK_OVERLAP:
  1336. this_cpu_inc(nic->drv_stats->tx_csum_overlap);
  1337. break;
  1338. case CQ_TX_ERROP_CK_OFLOW:
  1339. this_cpu_inc(nic->drv_stats->tx_csum_overflow);
  1340. break;
  1341. }
  1342. return 1;
  1343. }