nicvf_main.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  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/module.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/pci.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/if_vlan.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/ethtool.h>
  15. #include <linux/log2.h>
  16. #include <linux/prefetch.h>
  17. #include <linux/irq.h>
  18. #include <linux/iommu.h>
  19. #include <linux/bpf.h>
  20. #include <linux/bpf_trace.h>
  21. #include <linux/filter.h>
  22. #include "nic_reg.h"
  23. #include "nic.h"
  24. #include "nicvf_queues.h"
  25. #include "thunder_bgx.h"
  26. #define DRV_NAME "thunder-nicvf"
  27. #define DRV_VERSION "1.0"
  28. /* Supported devices */
  29. static const struct pci_device_id nicvf_id_table[] = {
  30. { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
  31. PCI_DEVICE_ID_THUNDER_NIC_VF,
  32. PCI_VENDOR_ID_CAVIUM,
  33. PCI_SUBSYS_DEVID_88XX_NIC_VF) },
  34. { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
  35. PCI_DEVICE_ID_THUNDER_PASS1_NIC_VF,
  36. PCI_VENDOR_ID_CAVIUM,
  37. PCI_SUBSYS_DEVID_88XX_PASS1_NIC_VF) },
  38. { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
  39. PCI_DEVICE_ID_THUNDER_NIC_VF,
  40. PCI_VENDOR_ID_CAVIUM,
  41. PCI_SUBSYS_DEVID_81XX_NIC_VF) },
  42. { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
  43. PCI_DEVICE_ID_THUNDER_NIC_VF,
  44. PCI_VENDOR_ID_CAVIUM,
  45. PCI_SUBSYS_DEVID_83XX_NIC_VF) },
  46. { 0, } /* end of table */
  47. };
  48. MODULE_AUTHOR("Sunil Goutham");
  49. MODULE_DESCRIPTION("Cavium Thunder NIC Virtual Function Driver");
  50. MODULE_LICENSE("GPL v2");
  51. MODULE_VERSION(DRV_VERSION);
  52. MODULE_DEVICE_TABLE(pci, nicvf_id_table);
  53. static int debug = 0x00;
  54. module_param(debug, int, 0644);
  55. MODULE_PARM_DESC(debug, "Debug message level bitmap");
  56. static int cpi_alg = CPI_ALG_NONE;
  57. module_param(cpi_alg, int, S_IRUGO);
  58. MODULE_PARM_DESC(cpi_alg,
  59. "PFC algorithm (0=none, 1=VLAN, 2=VLAN16, 3=IP Diffserv)");
  60. static inline u8 nicvf_netdev_qidx(struct nicvf *nic, u8 qidx)
  61. {
  62. if (nic->sqs_mode)
  63. return qidx + ((nic->sqs_id + 1) * MAX_CMP_QUEUES_PER_QS);
  64. else
  65. return qidx;
  66. }
  67. /* The Cavium ThunderX network controller can *only* be found in SoCs
  68. * containing the ThunderX ARM64 CPU implementation. All accesses to the device
  69. * registers on this platform are implicitly strongly ordered with respect
  70. * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
  71. * with no memory barriers in this driver. The readq()/writeq() functions add
  72. * explicit ordering operation which in this case are redundant, and only
  73. * add overhead.
  74. */
  75. /* Register read/write APIs */
  76. void nicvf_reg_write(struct nicvf *nic, u64 offset, u64 val)
  77. {
  78. writeq_relaxed(val, nic->reg_base + offset);
  79. }
  80. u64 nicvf_reg_read(struct nicvf *nic, u64 offset)
  81. {
  82. return readq_relaxed(nic->reg_base + offset);
  83. }
  84. void nicvf_queue_reg_write(struct nicvf *nic, u64 offset,
  85. u64 qidx, u64 val)
  86. {
  87. void __iomem *addr = nic->reg_base + offset;
  88. writeq_relaxed(val, addr + (qidx << NIC_Q_NUM_SHIFT));
  89. }
  90. u64 nicvf_queue_reg_read(struct nicvf *nic, u64 offset, u64 qidx)
  91. {
  92. void __iomem *addr = nic->reg_base + offset;
  93. return readq_relaxed(addr + (qidx << NIC_Q_NUM_SHIFT));
  94. }
  95. /* VF -> PF mailbox communication */
  96. static void nicvf_write_to_mbx(struct nicvf *nic, union nic_mbx *mbx)
  97. {
  98. u64 *msg = (u64 *)mbx;
  99. nicvf_reg_write(nic, NIC_VF_PF_MAILBOX_0_1 + 0, msg[0]);
  100. nicvf_reg_write(nic, NIC_VF_PF_MAILBOX_0_1 + 8, msg[1]);
  101. }
  102. int nicvf_send_msg_to_pf(struct nicvf *nic, union nic_mbx *mbx)
  103. {
  104. int timeout = NIC_MBOX_MSG_TIMEOUT;
  105. int sleep = 10;
  106. nic->pf_acked = false;
  107. nic->pf_nacked = false;
  108. nicvf_write_to_mbx(nic, mbx);
  109. /* Wait for previous message to be acked, timeout 2sec */
  110. while (!nic->pf_acked) {
  111. if (nic->pf_nacked) {
  112. netdev_err(nic->netdev,
  113. "PF NACK to mbox msg 0x%02x from VF%d\n",
  114. (mbx->msg.msg & 0xFF), nic->vf_id);
  115. return -EINVAL;
  116. }
  117. msleep(sleep);
  118. if (nic->pf_acked)
  119. break;
  120. timeout -= sleep;
  121. if (!timeout) {
  122. netdev_err(nic->netdev,
  123. "PF didn't ACK to mbox msg 0x%02x from VF%d\n",
  124. (mbx->msg.msg & 0xFF), nic->vf_id);
  125. return -EBUSY;
  126. }
  127. }
  128. return 0;
  129. }
  130. /* Checks if VF is able to comminicate with PF
  131. * and also gets the VNIC number this VF is associated to.
  132. */
  133. static int nicvf_check_pf_ready(struct nicvf *nic)
  134. {
  135. union nic_mbx mbx = {};
  136. mbx.msg.msg = NIC_MBOX_MSG_READY;
  137. if (nicvf_send_msg_to_pf(nic, &mbx)) {
  138. netdev_err(nic->netdev,
  139. "PF didn't respond to READY msg\n");
  140. return 0;
  141. }
  142. return 1;
  143. }
  144. static void nicvf_read_bgx_stats(struct nicvf *nic, struct bgx_stats_msg *bgx)
  145. {
  146. if (bgx->rx)
  147. nic->bgx_stats.rx_stats[bgx->idx] = bgx->stats;
  148. else
  149. nic->bgx_stats.tx_stats[bgx->idx] = bgx->stats;
  150. }
  151. static void nicvf_handle_mbx_intr(struct nicvf *nic)
  152. {
  153. union nic_mbx mbx = {};
  154. u64 *mbx_data;
  155. u64 mbx_addr;
  156. int i;
  157. mbx_addr = NIC_VF_PF_MAILBOX_0_1;
  158. mbx_data = (u64 *)&mbx;
  159. for (i = 0; i < NIC_PF_VF_MAILBOX_SIZE; i++) {
  160. *mbx_data = nicvf_reg_read(nic, mbx_addr);
  161. mbx_data++;
  162. mbx_addr += sizeof(u64);
  163. }
  164. netdev_dbg(nic->netdev, "Mbox message: msg: 0x%x\n", mbx.msg.msg);
  165. switch (mbx.msg.msg) {
  166. case NIC_MBOX_MSG_READY:
  167. nic->pf_acked = true;
  168. nic->vf_id = mbx.nic_cfg.vf_id & 0x7F;
  169. nic->tns_mode = mbx.nic_cfg.tns_mode & 0x7F;
  170. nic->node = mbx.nic_cfg.node_id;
  171. if (!nic->set_mac_pending)
  172. ether_addr_copy(nic->netdev->dev_addr,
  173. mbx.nic_cfg.mac_addr);
  174. nic->sqs_mode = mbx.nic_cfg.sqs_mode;
  175. nic->loopback_supported = mbx.nic_cfg.loopback_supported;
  176. nic->link_up = false;
  177. nic->duplex = 0;
  178. nic->speed = 0;
  179. break;
  180. case NIC_MBOX_MSG_ACK:
  181. nic->pf_acked = true;
  182. break;
  183. case NIC_MBOX_MSG_NACK:
  184. nic->pf_nacked = true;
  185. break;
  186. case NIC_MBOX_MSG_RSS_SIZE:
  187. nic->rss_info.rss_size = mbx.rss_size.ind_tbl_size;
  188. nic->pf_acked = true;
  189. break;
  190. case NIC_MBOX_MSG_BGX_STATS:
  191. nicvf_read_bgx_stats(nic, &mbx.bgx_stats);
  192. nic->pf_acked = true;
  193. break;
  194. case NIC_MBOX_MSG_BGX_LINK_CHANGE:
  195. nic->pf_acked = true;
  196. nic->link_up = mbx.link_status.link_up;
  197. nic->duplex = mbx.link_status.duplex;
  198. nic->speed = mbx.link_status.speed;
  199. nic->mac_type = mbx.link_status.mac_type;
  200. if (nic->link_up) {
  201. netdev_info(nic->netdev, "%s: Link is Up %d Mbps %s\n",
  202. nic->netdev->name, nic->speed,
  203. nic->duplex == DUPLEX_FULL ?
  204. "Full duplex" : "Half duplex");
  205. netif_carrier_on(nic->netdev);
  206. netif_tx_start_all_queues(nic->netdev);
  207. } else {
  208. netdev_info(nic->netdev, "%s: Link is Down\n",
  209. nic->netdev->name);
  210. netif_carrier_off(nic->netdev);
  211. netif_tx_stop_all_queues(nic->netdev);
  212. }
  213. break;
  214. case NIC_MBOX_MSG_ALLOC_SQS:
  215. nic->sqs_count = mbx.sqs_alloc.qs_count;
  216. nic->pf_acked = true;
  217. break;
  218. case NIC_MBOX_MSG_SNICVF_PTR:
  219. /* Primary VF: make note of secondary VF's pointer
  220. * to be used while packet transmission.
  221. */
  222. nic->snicvf[mbx.nicvf.sqs_id] =
  223. (struct nicvf *)mbx.nicvf.nicvf;
  224. nic->pf_acked = true;
  225. break;
  226. case NIC_MBOX_MSG_PNICVF_PTR:
  227. /* Secondary VF/Qset: make note of primary VF's pointer
  228. * to be used while packet reception, to handover packet
  229. * to primary VF's netdev.
  230. */
  231. nic->pnicvf = (struct nicvf *)mbx.nicvf.nicvf;
  232. nic->pf_acked = true;
  233. break;
  234. case NIC_MBOX_MSG_PFC:
  235. nic->pfc.autoneg = mbx.pfc.autoneg;
  236. nic->pfc.fc_rx = mbx.pfc.fc_rx;
  237. nic->pfc.fc_tx = mbx.pfc.fc_tx;
  238. nic->pf_acked = true;
  239. break;
  240. default:
  241. netdev_err(nic->netdev,
  242. "Invalid message from PF, msg 0x%x\n", mbx.msg.msg);
  243. break;
  244. }
  245. nicvf_clear_intr(nic, NICVF_INTR_MBOX, 0);
  246. }
  247. static int nicvf_hw_set_mac_addr(struct nicvf *nic, struct net_device *netdev)
  248. {
  249. union nic_mbx mbx = {};
  250. mbx.mac.msg = NIC_MBOX_MSG_SET_MAC;
  251. mbx.mac.vf_id = nic->vf_id;
  252. ether_addr_copy(mbx.mac.mac_addr, netdev->dev_addr);
  253. return nicvf_send_msg_to_pf(nic, &mbx);
  254. }
  255. static void nicvf_config_cpi(struct nicvf *nic)
  256. {
  257. union nic_mbx mbx = {};
  258. mbx.cpi_cfg.msg = NIC_MBOX_MSG_CPI_CFG;
  259. mbx.cpi_cfg.vf_id = nic->vf_id;
  260. mbx.cpi_cfg.cpi_alg = nic->cpi_alg;
  261. mbx.cpi_cfg.rq_cnt = nic->qs->rq_cnt;
  262. nicvf_send_msg_to_pf(nic, &mbx);
  263. }
  264. static void nicvf_get_rss_size(struct nicvf *nic)
  265. {
  266. union nic_mbx mbx = {};
  267. mbx.rss_size.msg = NIC_MBOX_MSG_RSS_SIZE;
  268. mbx.rss_size.vf_id = nic->vf_id;
  269. nicvf_send_msg_to_pf(nic, &mbx);
  270. }
  271. void nicvf_config_rss(struct nicvf *nic)
  272. {
  273. union nic_mbx mbx = {};
  274. struct nicvf_rss_info *rss = &nic->rss_info;
  275. int ind_tbl_len = rss->rss_size;
  276. int i, nextq = 0;
  277. mbx.rss_cfg.vf_id = nic->vf_id;
  278. mbx.rss_cfg.hash_bits = rss->hash_bits;
  279. while (ind_tbl_len) {
  280. mbx.rss_cfg.tbl_offset = nextq;
  281. mbx.rss_cfg.tbl_len = min(ind_tbl_len,
  282. RSS_IND_TBL_LEN_PER_MBX_MSG);
  283. mbx.rss_cfg.msg = mbx.rss_cfg.tbl_offset ?
  284. NIC_MBOX_MSG_RSS_CFG_CONT : NIC_MBOX_MSG_RSS_CFG;
  285. for (i = 0; i < mbx.rss_cfg.tbl_len; i++)
  286. mbx.rss_cfg.ind_tbl[i] = rss->ind_tbl[nextq++];
  287. nicvf_send_msg_to_pf(nic, &mbx);
  288. ind_tbl_len -= mbx.rss_cfg.tbl_len;
  289. }
  290. }
  291. void nicvf_set_rss_key(struct nicvf *nic)
  292. {
  293. struct nicvf_rss_info *rss = &nic->rss_info;
  294. u64 key_addr = NIC_VNIC_RSS_KEY_0_4;
  295. int idx;
  296. for (idx = 0; idx < RSS_HASH_KEY_SIZE; idx++) {
  297. nicvf_reg_write(nic, key_addr, rss->key[idx]);
  298. key_addr += sizeof(u64);
  299. }
  300. }
  301. static int nicvf_rss_init(struct nicvf *nic)
  302. {
  303. struct nicvf_rss_info *rss = &nic->rss_info;
  304. int idx;
  305. nicvf_get_rss_size(nic);
  306. if (cpi_alg != CPI_ALG_NONE) {
  307. rss->enable = false;
  308. rss->hash_bits = 0;
  309. return 0;
  310. }
  311. rss->enable = true;
  312. netdev_rss_key_fill(rss->key, RSS_HASH_KEY_SIZE * sizeof(u64));
  313. nicvf_set_rss_key(nic);
  314. rss->cfg = RSS_IP_HASH_ENA | RSS_TCP_HASH_ENA | RSS_UDP_HASH_ENA;
  315. nicvf_reg_write(nic, NIC_VNIC_RSS_CFG, rss->cfg);
  316. rss->hash_bits = ilog2(rounddown_pow_of_two(rss->rss_size));
  317. for (idx = 0; idx < rss->rss_size; idx++)
  318. rss->ind_tbl[idx] = ethtool_rxfh_indir_default(idx,
  319. nic->rx_queues);
  320. nicvf_config_rss(nic);
  321. return 1;
  322. }
  323. /* Request PF to allocate additional Qsets */
  324. static void nicvf_request_sqs(struct nicvf *nic)
  325. {
  326. union nic_mbx mbx = {};
  327. int sqs;
  328. int sqs_count = nic->sqs_count;
  329. int rx_queues = 0, tx_queues = 0;
  330. /* Only primary VF should request */
  331. if (nic->sqs_mode || !nic->sqs_count)
  332. return;
  333. mbx.sqs_alloc.msg = NIC_MBOX_MSG_ALLOC_SQS;
  334. mbx.sqs_alloc.vf_id = nic->vf_id;
  335. mbx.sqs_alloc.qs_count = nic->sqs_count;
  336. if (nicvf_send_msg_to_pf(nic, &mbx)) {
  337. /* No response from PF */
  338. nic->sqs_count = 0;
  339. return;
  340. }
  341. /* Return if no Secondary Qsets available */
  342. if (!nic->sqs_count)
  343. return;
  344. if (nic->rx_queues > MAX_RCV_QUEUES_PER_QS)
  345. rx_queues = nic->rx_queues - MAX_RCV_QUEUES_PER_QS;
  346. tx_queues = nic->tx_queues + nic->xdp_tx_queues;
  347. if (tx_queues > MAX_SND_QUEUES_PER_QS)
  348. tx_queues = tx_queues - MAX_SND_QUEUES_PER_QS;
  349. /* Set no of Rx/Tx queues in each of the SQsets */
  350. for (sqs = 0; sqs < nic->sqs_count; sqs++) {
  351. mbx.nicvf.msg = NIC_MBOX_MSG_SNICVF_PTR;
  352. mbx.nicvf.vf_id = nic->vf_id;
  353. mbx.nicvf.sqs_id = sqs;
  354. nicvf_send_msg_to_pf(nic, &mbx);
  355. nic->snicvf[sqs]->sqs_id = sqs;
  356. if (rx_queues > MAX_RCV_QUEUES_PER_QS) {
  357. nic->snicvf[sqs]->qs->rq_cnt = MAX_RCV_QUEUES_PER_QS;
  358. rx_queues -= MAX_RCV_QUEUES_PER_QS;
  359. } else {
  360. nic->snicvf[sqs]->qs->rq_cnt = rx_queues;
  361. rx_queues = 0;
  362. }
  363. if (tx_queues > MAX_SND_QUEUES_PER_QS) {
  364. nic->snicvf[sqs]->qs->sq_cnt = MAX_SND_QUEUES_PER_QS;
  365. tx_queues -= MAX_SND_QUEUES_PER_QS;
  366. } else {
  367. nic->snicvf[sqs]->qs->sq_cnt = tx_queues;
  368. tx_queues = 0;
  369. }
  370. nic->snicvf[sqs]->qs->cq_cnt =
  371. max(nic->snicvf[sqs]->qs->rq_cnt, nic->snicvf[sqs]->qs->sq_cnt);
  372. /* Initialize secondary Qset's queues and its interrupts */
  373. nicvf_open(nic->snicvf[sqs]->netdev);
  374. }
  375. /* Update stack with actual Rx/Tx queue count allocated */
  376. if (sqs_count != nic->sqs_count)
  377. nicvf_set_real_num_queues(nic->netdev,
  378. nic->tx_queues, nic->rx_queues);
  379. }
  380. /* Send this Qset's nicvf pointer to PF.
  381. * PF inturn sends primary VF's nicvf struct to secondary Qsets/VFs
  382. * so that packets received by these Qsets can use primary VF's netdev
  383. */
  384. static void nicvf_send_vf_struct(struct nicvf *nic)
  385. {
  386. union nic_mbx mbx = {};
  387. mbx.nicvf.msg = NIC_MBOX_MSG_NICVF_PTR;
  388. mbx.nicvf.sqs_mode = nic->sqs_mode;
  389. mbx.nicvf.nicvf = (u64)nic;
  390. nicvf_send_msg_to_pf(nic, &mbx);
  391. }
  392. static void nicvf_get_primary_vf_struct(struct nicvf *nic)
  393. {
  394. union nic_mbx mbx = {};
  395. mbx.nicvf.msg = NIC_MBOX_MSG_PNICVF_PTR;
  396. nicvf_send_msg_to_pf(nic, &mbx);
  397. }
  398. int nicvf_set_real_num_queues(struct net_device *netdev,
  399. int tx_queues, int rx_queues)
  400. {
  401. int err = 0;
  402. err = netif_set_real_num_tx_queues(netdev, tx_queues);
  403. if (err) {
  404. netdev_err(netdev,
  405. "Failed to set no of Tx queues: %d\n", tx_queues);
  406. return err;
  407. }
  408. err = netif_set_real_num_rx_queues(netdev, rx_queues);
  409. if (err)
  410. netdev_err(netdev,
  411. "Failed to set no of Rx queues: %d\n", rx_queues);
  412. return err;
  413. }
  414. static int nicvf_init_resources(struct nicvf *nic)
  415. {
  416. int err;
  417. /* Enable Qset */
  418. nicvf_qset_config(nic, true);
  419. /* Initialize queues and HW for data transfer */
  420. err = nicvf_config_data_transfer(nic, true);
  421. if (err) {
  422. netdev_err(nic->netdev,
  423. "Failed to alloc/config VF's QSet resources\n");
  424. return err;
  425. }
  426. return 0;
  427. }
  428. static inline bool nicvf_xdp_rx(struct nicvf *nic, struct bpf_prog *prog,
  429. struct cqe_rx_t *cqe_rx, struct snd_queue *sq,
  430. struct sk_buff **skb)
  431. {
  432. struct xdp_buff xdp;
  433. struct page *page;
  434. u32 action;
  435. u16 len, offset = 0;
  436. u64 dma_addr, cpu_addr;
  437. void *orig_data;
  438. /* Retrieve packet buffer's DMA address and length */
  439. len = *((u16 *)((void *)cqe_rx + (3 * sizeof(u64))));
  440. dma_addr = *((u64 *)((void *)cqe_rx + (7 * sizeof(u64))));
  441. cpu_addr = nicvf_iova_to_phys(nic, dma_addr);
  442. if (!cpu_addr)
  443. return false;
  444. cpu_addr = (u64)phys_to_virt(cpu_addr);
  445. page = virt_to_page((void *)cpu_addr);
  446. xdp.data_hard_start = page_address(page);
  447. xdp.data = (void *)cpu_addr;
  448. xdp.data_end = xdp.data + len;
  449. orig_data = xdp.data;
  450. rcu_read_lock();
  451. action = bpf_prog_run_xdp(prog, &xdp);
  452. rcu_read_unlock();
  453. /* Check if XDP program has changed headers */
  454. if (orig_data != xdp.data) {
  455. len = xdp.data_end - xdp.data;
  456. offset = orig_data - xdp.data;
  457. dma_addr -= offset;
  458. }
  459. switch (action) {
  460. case XDP_PASS:
  461. /* Check if it's a recycled page, if not
  462. * unmap the DMA mapping.
  463. *
  464. * Recycled page holds an extra reference.
  465. */
  466. if (page_ref_count(page) == 1) {
  467. dma_addr &= PAGE_MASK;
  468. dma_unmap_page_attrs(&nic->pdev->dev, dma_addr,
  469. RCV_FRAG_LEN + XDP_PACKET_HEADROOM,
  470. DMA_FROM_DEVICE,
  471. DMA_ATTR_SKIP_CPU_SYNC);
  472. }
  473. /* Build SKB and pass on packet to network stack */
  474. *skb = build_skb(xdp.data,
  475. RCV_FRAG_LEN - cqe_rx->align_pad + offset);
  476. if (!*skb)
  477. put_page(page);
  478. else
  479. skb_put(*skb, len);
  480. return false;
  481. case XDP_TX:
  482. nicvf_xdp_sq_append_pkt(nic, sq, (u64)xdp.data, dma_addr, len);
  483. return true;
  484. default:
  485. bpf_warn_invalid_xdp_action(action);
  486. case XDP_ABORTED:
  487. trace_xdp_exception(nic->netdev, prog, action);
  488. case XDP_DROP:
  489. /* Check if it's a recycled page, if not
  490. * unmap the DMA mapping.
  491. *
  492. * Recycled page holds an extra reference.
  493. */
  494. if (page_ref_count(page) == 1) {
  495. dma_addr &= PAGE_MASK;
  496. dma_unmap_page_attrs(&nic->pdev->dev, dma_addr,
  497. RCV_FRAG_LEN + XDP_PACKET_HEADROOM,
  498. DMA_FROM_DEVICE,
  499. DMA_ATTR_SKIP_CPU_SYNC);
  500. }
  501. put_page(page);
  502. return true;
  503. }
  504. return false;
  505. }
  506. static void nicvf_snd_pkt_handler(struct net_device *netdev,
  507. struct cqe_send_t *cqe_tx,
  508. int budget, int *subdesc_cnt,
  509. unsigned int *tx_pkts, unsigned int *tx_bytes)
  510. {
  511. struct sk_buff *skb = NULL;
  512. struct page *page;
  513. struct nicvf *nic = netdev_priv(netdev);
  514. struct snd_queue *sq;
  515. struct sq_hdr_subdesc *hdr;
  516. struct sq_hdr_subdesc *tso_sqe;
  517. sq = &nic->qs->sq[cqe_tx->sq_idx];
  518. hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, cqe_tx->sqe_ptr);
  519. if (hdr->subdesc_type != SQ_DESC_TYPE_HEADER)
  520. return;
  521. /* Check for errors */
  522. if (cqe_tx->send_status)
  523. nicvf_check_cqe_tx_errs(nic->pnicvf, cqe_tx);
  524. /* Is this a XDP designated Tx queue */
  525. if (sq->is_xdp) {
  526. page = (struct page *)sq->xdp_page[cqe_tx->sqe_ptr];
  527. /* Check if it's recycled page or else unmap DMA mapping */
  528. if (page && (page_ref_count(page) == 1))
  529. nicvf_unmap_sndq_buffers(nic, sq, cqe_tx->sqe_ptr,
  530. hdr->subdesc_cnt);
  531. /* Release page reference for recycling */
  532. if (page)
  533. put_page(page);
  534. sq->xdp_page[cqe_tx->sqe_ptr] = (u64)NULL;
  535. *subdesc_cnt += hdr->subdesc_cnt + 1;
  536. return;
  537. }
  538. skb = (struct sk_buff *)sq->skbuff[cqe_tx->sqe_ptr];
  539. if (skb) {
  540. /* Check for dummy descriptor used for HW TSO offload on 88xx */
  541. if (hdr->dont_send) {
  542. /* Get actual TSO descriptors and free them */
  543. tso_sqe =
  544. (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, hdr->rsvd2);
  545. nicvf_unmap_sndq_buffers(nic, sq, hdr->rsvd2,
  546. tso_sqe->subdesc_cnt);
  547. *subdesc_cnt += tso_sqe->subdesc_cnt + 1;
  548. } else {
  549. nicvf_unmap_sndq_buffers(nic, sq, cqe_tx->sqe_ptr,
  550. hdr->subdesc_cnt);
  551. }
  552. *subdesc_cnt += hdr->subdesc_cnt + 1;
  553. prefetch(skb);
  554. (*tx_pkts)++;
  555. *tx_bytes += skb->len;
  556. napi_consume_skb(skb, budget);
  557. sq->skbuff[cqe_tx->sqe_ptr] = (u64)NULL;
  558. } else {
  559. /* In case of SW TSO on 88xx, only last segment will have
  560. * a SKB attached, so just free SQEs here.
  561. */
  562. if (!nic->hw_tso)
  563. *subdesc_cnt += hdr->subdesc_cnt + 1;
  564. }
  565. }
  566. static inline void nicvf_set_rxhash(struct net_device *netdev,
  567. struct cqe_rx_t *cqe_rx,
  568. struct sk_buff *skb)
  569. {
  570. u8 hash_type;
  571. u32 hash;
  572. if (!(netdev->features & NETIF_F_RXHASH))
  573. return;
  574. switch (cqe_rx->rss_alg) {
  575. case RSS_ALG_TCP_IP:
  576. case RSS_ALG_UDP_IP:
  577. hash_type = PKT_HASH_TYPE_L4;
  578. hash = cqe_rx->rss_tag;
  579. break;
  580. case RSS_ALG_IP:
  581. hash_type = PKT_HASH_TYPE_L3;
  582. hash = cqe_rx->rss_tag;
  583. break;
  584. default:
  585. hash_type = PKT_HASH_TYPE_NONE;
  586. hash = 0;
  587. }
  588. skb_set_hash(skb, hash, hash_type);
  589. }
  590. static void nicvf_rcv_pkt_handler(struct net_device *netdev,
  591. struct napi_struct *napi,
  592. struct cqe_rx_t *cqe_rx, struct snd_queue *sq)
  593. {
  594. struct sk_buff *skb = NULL;
  595. struct nicvf *nic = netdev_priv(netdev);
  596. struct nicvf *snic = nic;
  597. int err = 0;
  598. int rq_idx;
  599. rq_idx = nicvf_netdev_qidx(nic, cqe_rx->rq_idx);
  600. if (nic->sqs_mode) {
  601. /* Use primary VF's 'nicvf' struct */
  602. nic = nic->pnicvf;
  603. netdev = nic->netdev;
  604. }
  605. /* Check for errors */
  606. if (cqe_rx->err_level || cqe_rx->err_opcode) {
  607. err = nicvf_check_cqe_rx_errs(nic, cqe_rx);
  608. if (err && !cqe_rx->rb_cnt)
  609. return;
  610. }
  611. /* For XDP, ignore pkts spanning multiple pages */
  612. if (nic->xdp_prog && (cqe_rx->rb_cnt == 1)) {
  613. /* Packet consumed by XDP */
  614. if (nicvf_xdp_rx(snic, nic->xdp_prog, cqe_rx, sq, &skb))
  615. return;
  616. } else {
  617. skb = nicvf_get_rcv_skb(snic, cqe_rx,
  618. nic->xdp_prog ? true : false);
  619. }
  620. if (!skb)
  621. return;
  622. if (netif_msg_pktdata(nic)) {
  623. netdev_info(nic->netdev, "%s: skb 0x%p, len=%d\n", netdev->name,
  624. skb, skb->len);
  625. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
  626. skb->data, skb->len, true);
  627. }
  628. /* If error packet, drop it here */
  629. if (err) {
  630. dev_kfree_skb_any(skb);
  631. return;
  632. }
  633. nicvf_set_rxhash(netdev, cqe_rx, skb);
  634. skb_record_rx_queue(skb, rq_idx);
  635. if (netdev->hw_features & NETIF_F_RXCSUM) {
  636. /* HW by default verifies TCP/UDP/SCTP checksums */
  637. skb->ip_summed = CHECKSUM_UNNECESSARY;
  638. } else {
  639. skb_checksum_none_assert(skb);
  640. }
  641. skb->protocol = eth_type_trans(skb, netdev);
  642. /* Check for stripped VLAN */
  643. if (cqe_rx->vlan_found && cqe_rx->vlan_stripped)
  644. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  645. ntohs((__force __be16)cqe_rx->vlan_tci));
  646. if (napi && (netdev->features & NETIF_F_GRO))
  647. napi_gro_receive(napi, skb);
  648. else
  649. netif_receive_skb(skb);
  650. }
  651. static int nicvf_cq_intr_handler(struct net_device *netdev, u8 cq_idx,
  652. struct napi_struct *napi, int budget)
  653. {
  654. int processed_cqe, work_done = 0, tx_done = 0;
  655. int cqe_count, cqe_head;
  656. int subdesc_cnt = 0;
  657. struct nicvf *nic = netdev_priv(netdev);
  658. struct queue_set *qs = nic->qs;
  659. struct cmp_queue *cq = &qs->cq[cq_idx];
  660. struct cqe_rx_t *cq_desc;
  661. struct netdev_queue *txq;
  662. struct snd_queue *sq = &qs->sq[cq_idx];
  663. unsigned int tx_pkts = 0, tx_bytes = 0, txq_idx;
  664. spin_lock_bh(&cq->lock);
  665. loop:
  666. processed_cqe = 0;
  667. /* Get no of valid CQ entries to process */
  668. cqe_count = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS, cq_idx);
  669. cqe_count &= CQ_CQE_COUNT;
  670. if (!cqe_count)
  671. goto done;
  672. /* Get head of the valid CQ entries */
  673. cqe_head = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_HEAD, cq_idx) >> 9;
  674. cqe_head &= 0xFFFF;
  675. while (processed_cqe < cqe_count) {
  676. /* Get the CQ descriptor */
  677. cq_desc = (struct cqe_rx_t *)GET_CQ_DESC(cq, cqe_head);
  678. cqe_head++;
  679. cqe_head &= (cq->dmem.q_len - 1);
  680. /* Initiate prefetch for next descriptor */
  681. prefetch((struct cqe_rx_t *)GET_CQ_DESC(cq, cqe_head));
  682. if ((work_done >= budget) && napi &&
  683. (cq_desc->cqe_type != CQE_TYPE_SEND)) {
  684. break;
  685. }
  686. switch (cq_desc->cqe_type) {
  687. case CQE_TYPE_RX:
  688. nicvf_rcv_pkt_handler(netdev, napi, cq_desc, sq);
  689. work_done++;
  690. break;
  691. case CQE_TYPE_SEND:
  692. nicvf_snd_pkt_handler(netdev, (void *)cq_desc,
  693. budget, &subdesc_cnt,
  694. &tx_pkts, &tx_bytes);
  695. tx_done++;
  696. break;
  697. case CQE_TYPE_INVALID:
  698. case CQE_TYPE_RX_SPLIT:
  699. case CQE_TYPE_RX_TCP:
  700. case CQE_TYPE_SEND_PTP:
  701. /* Ignore for now */
  702. break;
  703. }
  704. processed_cqe++;
  705. }
  706. /* Ring doorbell to inform H/W to reuse processed CQEs */
  707. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_DOOR,
  708. cq_idx, processed_cqe);
  709. if ((work_done < budget) && napi)
  710. goto loop;
  711. done:
  712. /* Update SQ's descriptor free count */
  713. if (subdesc_cnt)
  714. nicvf_put_sq_desc(sq, subdesc_cnt);
  715. txq_idx = nicvf_netdev_qidx(nic, cq_idx);
  716. /* Handle XDP TX queues */
  717. if (nic->pnicvf->xdp_prog) {
  718. if (txq_idx < nic->pnicvf->xdp_tx_queues) {
  719. nicvf_xdp_sq_doorbell(nic, sq, cq_idx);
  720. goto out;
  721. }
  722. nic = nic->pnicvf;
  723. txq_idx -= nic->pnicvf->xdp_tx_queues;
  724. }
  725. /* Wakeup TXQ if its stopped earlier due to SQ full */
  726. if (tx_done ||
  727. (atomic_read(&sq->free_cnt) >= MIN_SQ_DESC_PER_PKT_XMIT)) {
  728. netdev = nic->pnicvf->netdev;
  729. txq = netdev_get_tx_queue(netdev, txq_idx);
  730. if (tx_pkts)
  731. netdev_tx_completed_queue(txq, tx_pkts, tx_bytes);
  732. /* To read updated queue and carrier status */
  733. smp_mb();
  734. if (netif_tx_queue_stopped(txq) && netif_carrier_ok(netdev)) {
  735. netif_tx_wake_queue(txq);
  736. nic = nic->pnicvf;
  737. this_cpu_inc(nic->drv_stats->txq_wake);
  738. if (netif_msg_tx_err(nic))
  739. netdev_warn(netdev,
  740. "%s: Transmit queue wakeup SQ%d\n",
  741. netdev->name, txq_idx);
  742. }
  743. }
  744. out:
  745. spin_unlock_bh(&cq->lock);
  746. return work_done;
  747. }
  748. static int nicvf_poll(struct napi_struct *napi, int budget)
  749. {
  750. u64 cq_head;
  751. int work_done = 0;
  752. struct net_device *netdev = napi->dev;
  753. struct nicvf *nic = netdev_priv(netdev);
  754. struct nicvf_cq_poll *cq;
  755. cq = container_of(napi, struct nicvf_cq_poll, napi);
  756. work_done = nicvf_cq_intr_handler(netdev, cq->cq_idx, napi, budget);
  757. if (work_done < budget) {
  758. /* Slow packet rate, exit polling */
  759. napi_complete_done(napi, work_done);
  760. /* Re-enable interrupts */
  761. cq_head = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_HEAD,
  762. cq->cq_idx);
  763. nicvf_clear_intr(nic, NICVF_INTR_CQ, cq->cq_idx);
  764. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_HEAD,
  765. cq->cq_idx, cq_head);
  766. nicvf_enable_intr(nic, NICVF_INTR_CQ, cq->cq_idx);
  767. }
  768. return work_done;
  769. }
  770. /* Qset error interrupt handler
  771. *
  772. * As of now only CQ errors are handled
  773. */
  774. static void nicvf_handle_qs_err(unsigned long data)
  775. {
  776. struct nicvf *nic = (struct nicvf *)data;
  777. struct queue_set *qs = nic->qs;
  778. int qidx;
  779. u64 status;
  780. netif_tx_disable(nic->netdev);
  781. /* Check if it is CQ err */
  782. for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
  783. status = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS,
  784. qidx);
  785. if (!(status & CQ_ERR_MASK))
  786. continue;
  787. /* Process already queued CQEs and reconfig CQ */
  788. nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
  789. nicvf_sq_disable(nic, qidx);
  790. nicvf_cq_intr_handler(nic->netdev, qidx, NULL, 0);
  791. nicvf_cmp_queue_config(nic, qs, qidx, true);
  792. nicvf_sq_free_used_descs(nic->netdev, &qs->sq[qidx], qidx);
  793. nicvf_sq_enable(nic, &qs->sq[qidx], qidx);
  794. nicvf_enable_intr(nic, NICVF_INTR_CQ, qidx);
  795. }
  796. netif_tx_start_all_queues(nic->netdev);
  797. /* Re-enable Qset error interrupt */
  798. nicvf_enable_intr(nic, NICVF_INTR_QS_ERR, 0);
  799. }
  800. static void nicvf_dump_intr_status(struct nicvf *nic)
  801. {
  802. if (netif_msg_intr(nic))
  803. netdev_info(nic->netdev, "%s: interrupt status 0x%llx\n",
  804. nic->netdev->name, nicvf_reg_read(nic, NIC_VF_INT));
  805. }
  806. static irqreturn_t nicvf_misc_intr_handler(int irq, void *nicvf_irq)
  807. {
  808. struct nicvf *nic = (struct nicvf *)nicvf_irq;
  809. u64 intr;
  810. nicvf_dump_intr_status(nic);
  811. intr = nicvf_reg_read(nic, NIC_VF_INT);
  812. /* Check for spurious interrupt */
  813. if (!(intr & NICVF_INTR_MBOX_MASK))
  814. return IRQ_HANDLED;
  815. nicvf_handle_mbx_intr(nic);
  816. return IRQ_HANDLED;
  817. }
  818. static irqreturn_t nicvf_intr_handler(int irq, void *cq_irq)
  819. {
  820. struct nicvf_cq_poll *cq_poll = (struct nicvf_cq_poll *)cq_irq;
  821. struct nicvf *nic = cq_poll->nicvf;
  822. int qidx = cq_poll->cq_idx;
  823. nicvf_dump_intr_status(nic);
  824. /* Disable interrupts */
  825. nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
  826. /* Schedule NAPI */
  827. napi_schedule_irqoff(&cq_poll->napi);
  828. /* Clear interrupt */
  829. nicvf_clear_intr(nic, NICVF_INTR_CQ, qidx);
  830. return IRQ_HANDLED;
  831. }
  832. static irqreturn_t nicvf_rbdr_intr_handler(int irq, void *nicvf_irq)
  833. {
  834. struct nicvf *nic = (struct nicvf *)nicvf_irq;
  835. u8 qidx;
  836. nicvf_dump_intr_status(nic);
  837. /* Disable RBDR interrupt and schedule softirq */
  838. for (qidx = 0; qidx < nic->qs->rbdr_cnt; qidx++) {
  839. if (!nicvf_is_intr_enabled(nic, NICVF_INTR_RBDR, qidx))
  840. continue;
  841. nicvf_disable_intr(nic, NICVF_INTR_RBDR, qidx);
  842. tasklet_hi_schedule(&nic->rbdr_task);
  843. /* Clear interrupt */
  844. nicvf_clear_intr(nic, NICVF_INTR_RBDR, qidx);
  845. }
  846. return IRQ_HANDLED;
  847. }
  848. static irqreturn_t nicvf_qs_err_intr_handler(int irq, void *nicvf_irq)
  849. {
  850. struct nicvf *nic = (struct nicvf *)nicvf_irq;
  851. nicvf_dump_intr_status(nic);
  852. /* Disable Qset err interrupt and schedule softirq */
  853. nicvf_disable_intr(nic, NICVF_INTR_QS_ERR, 0);
  854. tasklet_hi_schedule(&nic->qs_err_task);
  855. nicvf_clear_intr(nic, NICVF_INTR_QS_ERR, 0);
  856. return IRQ_HANDLED;
  857. }
  858. static void nicvf_set_irq_affinity(struct nicvf *nic)
  859. {
  860. int vec, cpu;
  861. for (vec = 0; vec < nic->num_vec; vec++) {
  862. if (!nic->irq_allocated[vec])
  863. continue;
  864. if (!zalloc_cpumask_var(&nic->affinity_mask[vec], GFP_KERNEL))
  865. return;
  866. /* CQ interrupts */
  867. if (vec < NICVF_INTR_ID_SQ)
  868. /* Leave CPU0 for RBDR and other interrupts */
  869. cpu = nicvf_netdev_qidx(nic, vec) + 1;
  870. else
  871. cpu = 0;
  872. cpumask_set_cpu(cpumask_local_spread(cpu, nic->node),
  873. nic->affinity_mask[vec]);
  874. irq_set_affinity_hint(pci_irq_vector(nic->pdev, vec),
  875. nic->affinity_mask[vec]);
  876. }
  877. }
  878. static int nicvf_register_interrupts(struct nicvf *nic)
  879. {
  880. int irq, ret = 0;
  881. for_each_cq_irq(irq)
  882. sprintf(nic->irq_name[irq], "%s-rxtx-%d",
  883. nic->pnicvf->netdev->name,
  884. nicvf_netdev_qidx(nic, irq));
  885. for_each_sq_irq(irq)
  886. sprintf(nic->irq_name[irq], "%s-sq-%d",
  887. nic->pnicvf->netdev->name,
  888. nicvf_netdev_qidx(nic, irq - NICVF_INTR_ID_SQ));
  889. for_each_rbdr_irq(irq)
  890. sprintf(nic->irq_name[irq], "%s-rbdr-%d",
  891. nic->pnicvf->netdev->name,
  892. nic->sqs_mode ? (nic->sqs_id + 1) : 0);
  893. /* Register CQ interrupts */
  894. for (irq = 0; irq < nic->qs->cq_cnt; irq++) {
  895. ret = request_irq(pci_irq_vector(nic->pdev, irq),
  896. nicvf_intr_handler,
  897. 0, nic->irq_name[irq], nic->napi[irq]);
  898. if (ret)
  899. goto err;
  900. nic->irq_allocated[irq] = true;
  901. }
  902. /* Register RBDR interrupt */
  903. for (irq = NICVF_INTR_ID_RBDR;
  904. irq < (NICVF_INTR_ID_RBDR + nic->qs->rbdr_cnt); irq++) {
  905. ret = request_irq(pci_irq_vector(nic->pdev, irq),
  906. nicvf_rbdr_intr_handler,
  907. 0, nic->irq_name[irq], nic);
  908. if (ret)
  909. goto err;
  910. nic->irq_allocated[irq] = true;
  911. }
  912. /* Register QS error interrupt */
  913. sprintf(nic->irq_name[NICVF_INTR_ID_QS_ERR], "%s-qset-err-%d",
  914. nic->pnicvf->netdev->name,
  915. nic->sqs_mode ? (nic->sqs_id + 1) : 0);
  916. irq = NICVF_INTR_ID_QS_ERR;
  917. ret = request_irq(pci_irq_vector(nic->pdev, irq),
  918. nicvf_qs_err_intr_handler,
  919. 0, nic->irq_name[irq], nic);
  920. if (ret)
  921. goto err;
  922. nic->irq_allocated[irq] = true;
  923. /* Set IRQ affinities */
  924. nicvf_set_irq_affinity(nic);
  925. err:
  926. if (ret)
  927. netdev_err(nic->netdev, "request_irq failed, vector %d\n", irq);
  928. return ret;
  929. }
  930. static void nicvf_unregister_interrupts(struct nicvf *nic)
  931. {
  932. struct pci_dev *pdev = nic->pdev;
  933. int irq;
  934. /* Free registered interrupts */
  935. for (irq = 0; irq < nic->num_vec; irq++) {
  936. if (!nic->irq_allocated[irq])
  937. continue;
  938. irq_set_affinity_hint(pci_irq_vector(pdev, irq), NULL);
  939. free_cpumask_var(nic->affinity_mask[irq]);
  940. if (irq < NICVF_INTR_ID_SQ)
  941. free_irq(pci_irq_vector(pdev, irq), nic->napi[irq]);
  942. else
  943. free_irq(pci_irq_vector(pdev, irq), nic);
  944. nic->irq_allocated[irq] = false;
  945. }
  946. /* Disable MSI-X */
  947. pci_free_irq_vectors(pdev);
  948. nic->num_vec = 0;
  949. }
  950. /* Initialize MSIX vectors and register MISC interrupt.
  951. * Send READY message to PF to check if its alive
  952. */
  953. static int nicvf_register_misc_interrupt(struct nicvf *nic)
  954. {
  955. int ret = 0;
  956. int irq = NICVF_INTR_ID_MISC;
  957. /* Return if mailbox interrupt is already registered */
  958. if (nic->pdev->msix_enabled)
  959. return 0;
  960. /* Enable MSI-X */
  961. nic->num_vec = pci_msix_vec_count(nic->pdev);
  962. ret = pci_alloc_irq_vectors(nic->pdev, nic->num_vec, nic->num_vec,
  963. PCI_IRQ_MSIX);
  964. if (ret < 0) {
  965. netdev_err(nic->netdev,
  966. "Req for #%d msix vectors failed\n", nic->num_vec);
  967. return 1;
  968. }
  969. sprintf(nic->irq_name[irq], "%s Mbox", "NICVF");
  970. /* Register Misc interrupt */
  971. ret = request_irq(pci_irq_vector(nic->pdev, irq),
  972. nicvf_misc_intr_handler, 0, nic->irq_name[irq], nic);
  973. if (ret)
  974. return ret;
  975. nic->irq_allocated[irq] = true;
  976. /* Enable mailbox interrupt */
  977. nicvf_enable_intr(nic, NICVF_INTR_MBOX, 0);
  978. /* Check if VF is able to communicate with PF */
  979. if (!nicvf_check_pf_ready(nic)) {
  980. nicvf_disable_intr(nic, NICVF_INTR_MBOX, 0);
  981. nicvf_unregister_interrupts(nic);
  982. return 1;
  983. }
  984. return 0;
  985. }
  986. static netdev_tx_t nicvf_xmit(struct sk_buff *skb, struct net_device *netdev)
  987. {
  988. struct nicvf *nic = netdev_priv(netdev);
  989. int qid = skb_get_queue_mapping(skb);
  990. struct netdev_queue *txq = netdev_get_tx_queue(netdev, qid);
  991. struct nicvf *snic;
  992. struct snd_queue *sq;
  993. int tmp;
  994. /* Check for minimum packet length */
  995. if (skb->len <= ETH_HLEN) {
  996. dev_kfree_skb(skb);
  997. return NETDEV_TX_OK;
  998. }
  999. /* In XDP case, initial HW tx queues are used for XDP,
  1000. * but stack's queue mapping starts at '0', so skip the
  1001. * Tx queues attached to Rx queues for XDP.
  1002. */
  1003. if (nic->xdp_prog)
  1004. qid += nic->xdp_tx_queues;
  1005. snic = nic;
  1006. /* Get secondary Qset's SQ structure */
  1007. if (qid >= MAX_SND_QUEUES_PER_QS) {
  1008. tmp = qid / MAX_SND_QUEUES_PER_QS;
  1009. snic = (struct nicvf *)nic->snicvf[tmp - 1];
  1010. if (!snic) {
  1011. netdev_warn(nic->netdev,
  1012. "Secondary Qset#%d's ptr not initialized\n",
  1013. tmp - 1);
  1014. dev_kfree_skb(skb);
  1015. return NETDEV_TX_OK;
  1016. }
  1017. qid = qid % MAX_SND_QUEUES_PER_QS;
  1018. }
  1019. sq = &snic->qs->sq[qid];
  1020. if (!netif_tx_queue_stopped(txq) &&
  1021. !nicvf_sq_append_skb(snic, sq, skb, qid)) {
  1022. netif_tx_stop_queue(txq);
  1023. /* Barrier, so that stop_queue visible to other cpus */
  1024. smp_mb();
  1025. /* Check again, incase another cpu freed descriptors */
  1026. if (atomic_read(&sq->free_cnt) > MIN_SQ_DESC_PER_PKT_XMIT) {
  1027. netif_tx_wake_queue(txq);
  1028. } else {
  1029. this_cpu_inc(nic->drv_stats->txq_stop);
  1030. if (netif_msg_tx_err(nic))
  1031. netdev_warn(netdev,
  1032. "%s: Transmit ring full, stopping SQ%d\n",
  1033. netdev->name, qid);
  1034. }
  1035. return NETDEV_TX_BUSY;
  1036. }
  1037. return NETDEV_TX_OK;
  1038. }
  1039. static inline void nicvf_free_cq_poll(struct nicvf *nic)
  1040. {
  1041. struct nicvf_cq_poll *cq_poll;
  1042. int qidx;
  1043. for (qidx = 0; qidx < nic->qs->cq_cnt; qidx++) {
  1044. cq_poll = nic->napi[qidx];
  1045. if (!cq_poll)
  1046. continue;
  1047. nic->napi[qidx] = NULL;
  1048. kfree(cq_poll);
  1049. }
  1050. }
  1051. int nicvf_stop(struct net_device *netdev)
  1052. {
  1053. int irq, qidx;
  1054. struct nicvf *nic = netdev_priv(netdev);
  1055. struct queue_set *qs = nic->qs;
  1056. struct nicvf_cq_poll *cq_poll = NULL;
  1057. union nic_mbx mbx = {};
  1058. mbx.msg.msg = NIC_MBOX_MSG_SHUTDOWN;
  1059. nicvf_send_msg_to_pf(nic, &mbx);
  1060. netif_carrier_off(netdev);
  1061. netif_tx_stop_all_queues(nic->netdev);
  1062. nic->link_up = false;
  1063. /* Teardown secondary qsets first */
  1064. if (!nic->sqs_mode) {
  1065. for (qidx = 0; qidx < nic->sqs_count; qidx++) {
  1066. if (!nic->snicvf[qidx])
  1067. continue;
  1068. nicvf_stop(nic->snicvf[qidx]->netdev);
  1069. nic->snicvf[qidx] = NULL;
  1070. }
  1071. }
  1072. /* Disable RBDR & QS error interrupts */
  1073. for (qidx = 0; qidx < qs->rbdr_cnt; qidx++) {
  1074. nicvf_disable_intr(nic, NICVF_INTR_RBDR, qidx);
  1075. nicvf_clear_intr(nic, NICVF_INTR_RBDR, qidx);
  1076. }
  1077. nicvf_disable_intr(nic, NICVF_INTR_QS_ERR, 0);
  1078. nicvf_clear_intr(nic, NICVF_INTR_QS_ERR, 0);
  1079. /* Wait for pending IRQ handlers to finish */
  1080. for (irq = 0; irq < nic->num_vec; irq++)
  1081. synchronize_irq(pci_irq_vector(nic->pdev, irq));
  1082. tasklet_kill(&nic->rbdr_task);
  1083. tasklet_kill(&nic->qs_err_task);
  1084. if (nic->rb_work_scheduled)
  1085. cancel_delayed_work_sync(&nic->rbdr_work);
  1086. for (qidx = 0; qidx < nic->qs->cq_cnt; qidx++) {
  1087. cq_poll = nic->napi[qidx];
  1088. if (!cq_poll)
  1089. continue;
  1090. napi_synchronize(&cq_poll->napi);
  1091. /* CQ intr is enabled while napi_complete,
  1092. * so disable it now
  1093. */
  1094. nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
  1095. nicvf_clear_intr(nic, NICVF_INTR_CQ, qidx);
  1096. napi_disable(&cq_poll->napi);
  1097. netif_napi_del(&cq_poll->napi);
  1098. }
  1099. netif_tx_disable(netdev);
  1100. for (qidx = 0; qidx < netdev->num_tx_queues; qidx++)
  1101. netdev_tx_reset_queue(netdev_get_tx_queue(netdev, qidx));
  1102. /* Free resources */
  1103. nicvf_config_data_transfer(nic, false);
  1104. /* Disable HW Qset */
  1105. nicvf_qset_config(nic, false);
  1106. /* disable mailbox interrupt */
  1107. nicvf_disable_intr(nic, NICVF_INTR_MBOX, 0);
  1108. nicvf_unregister_interrupts(nic);
  1109. nicvf_free_cq_poll(nic);
  1110. /* Clear multiqset info */
  1111. nic->pnicvf = nic;
  1112. return 0;
  1113. }
  1114. static int nicvf_update_hw_max_frs(struct nicvf *nic, int mtu)
  1115. {
  1116. union nic_mbx mbx = {};
  1117. mbx.frs.msg = NIC_MBOX_MSG_SET_MAX_FRS;
  1118. mbx.frs.max_frs = mtu;
  1119. mbx.frs.vf_id = nic->vf_id;
  1120. return nicvf_send_msg_to_pf(nic, &mbx);
  1121. }
  1122. int nicvf_open(struct net_device *netdev)
  1123. {
  1124. int cpu, err, qidx;
  1125. struct nicvf *nic = netdev_priv(netdev);
  1126. struct queue_set *qs = nic->qs;
  1127. struct nicvf_cq_poll *cq_poll = NULL;
  1128. union nic_mbx mbx = {};
  1129. netif_carrier_off(netdev);
  1130. err = nicvf_register_misc_interrupt(nic);
  1131. if (err)
  1132. return err;
  1133. /* Register NAPI handler for processing CQEs */
  1134. for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
  1135. cq_poll = kzalloc(sizeof(*cq_poll), GFP_KERNEL);
  1136. if (!cq_poll) {
  1137. err = -ENOMEM;
  1138. goto napi_del;
  1139. }
  1140. cq_poll->cq_idx = qidx;
  1141. cq_poll->nicvf = nic;
  1142. netif_napi_add(netdev, &cq_poll->napi, nicvf_poll,
  1143. NAPI_POLL_WEIGHT);
  1144. napi_enable(&cq_poll->napi);
  1145. nic->napi[qidx] = cq_poll;
  1146. }
  1147. /* Check if we got MAC address from PF or else generate a radom MAC */
  1148. if (!nic->sqs_mode && is_zero_ether_addr(netdev->dev_addr)) {
  1149. eth_hw_addr_random(netdev);
  1150. nicvf_hw_set_mac_addr(nic, netdev);
  1151. }
  1152. if (nic->set_mac_pending) {
  1153. nic->set_mac_pending = false;
  1154. nicvf_hw_set_mac_addr(nic, netdev);
  1155. }
  1156. /* Init tasklet for handling Qset err interrupt */
  1157. tasklet_init(&nic->qs_err_task, nicvf_handle_qs_err,
  1158. (unsigned long)nic);
  1159. /* Init RBDR tasklet which will refill RBDR */
  1160. tasklet_init(&nic->rbdr_task, nicvf_rbdr_task,
  1161. (unsigned long)nic);
  1162. INIT_DELAYED_WORK(&nic->rbdr_work, nicvf_rbdr_work);
  1163. /* Configure CPI alorithm */
  1164. nic->cpi_alg = cpi_alg;
  1165. if (!nic->sqs_mode)
  1166. nicvf_config_cpi(nic);
  1167. nicvf_request_sqs(nic);
  1168. if (nic->sqs_mode)
  1169. nicvf_get_primary_vf_struct(nic);
  1170. /* Configure receive side scaling and MTU */
  1171. if (!nic->sqs_mode) {
  1172. nicvf_rss_init(nic);
  1173. err = nicvf_update_hw_max_frs(nic, netdev->mtu);
  1174. if (err)
  1175. goto cleanup;
  1176. /* Clear percpu stats */
  1177. for_each_possible_cpu(cpu)
  1178. memset(per_cpu_ptr(nic->drv_stats, cpu), 0,
  1179. sizeof(struct nicvf_drv_stats));
  1180. }
  1181. err = nicvf_register_interrupts(nic);
  1182. if (err)
  1183. goto cleanup;
  1184. /* Initialize the queues */
  1185. err = nicvf_init_resources(nic);
  1186. if (err)
  1187. goto cleanup;
  1188. /* Make sure queue initialization is written */
  1189. wmb();
  1190. nicvf_reg_write(nic, NIC_VF_INT, -1);
  1191. /* Enable Qset err interrupt */
  1192. nicvf_enable_intr(nic, NICVF_INTR_QS_ERR, 0);
  1193. /* Enable completion queue interrupt */
  1194. for (qidx = 0; qidx < qs->cq_cnt; qidx++)
  1195. nicvf_enable_intr(nic, NICVF_INTR_CQ, qidx);
  1196. /* Enable RBDR threshold interrupt */
  1197. for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
  1198. nicvf_enable_intr(nic, NICVF_INTR_RBDR, qidx);
  1199. /* Send VF config done msg to PF */
  1200. mbx.msg.msg = NIC_MBOX_MSG_CFG_DONE;
  1201. nicvf_write_to_mbx(nic, &mbx);
  1202. return 0;
  1203. cleanup:
  1204. nicvf_disable_intr(nic, NICVF_INTR_MBOX, 0);
  1205. nicvf_unregister_interrupts(nic);
  1206. tasklet_kill(&nic->qs_err_task);
  1207. tasklet_kill(&nic->rbdr_task);
  1208. napi_del:
  1209. for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
  1210. cq_poll = nic->napi[qidx];
  1211. if (!cq_poll)
  1212. continue;
  1213. napi_disable(&cq_poll->napi);
  1214. netif_napi_del(&cq_poll->napi);
  1215. }
  1216. nicvf_free_cq_poll(nic);
  1217. return err;
  1218. }
  1219. static int nicvf_change_mtu(struct net_device *netdev, int new_mtu)
  1220. {
  1221. struct nicvf *nic = netdev_priv(netdev);
  1222. int orig_mtu = netdev->mtu;
  1223. netdev->mtu = new_mtu;
  1224. if (!netif_running(netdev))
  1225. return 0;
  1226. if (nicvf_update_hw_max_frs(nic, new_mtu)) {
  1227. netdev->mtu = orig_mtu;
  1228. return -EINVAL;
  1229. }
  1230. return 0;
  1231. }
  1232. static int nicvf_set_mac_address(struct net_device *netdev, void *p)
  1233. {
  1234. struct sockaddr *addr = p;
  1235. struct nicvf *nic = netdev_priv(netdev);
  1236. if (!is_valid_ether_addr(addr->sa_data))
  1237. return -EADDRNOTAVAIL;
  1238. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1239. if (nic->pdev->msix_enabled) {
  1240. if (nicvf_hw_set_mac_addr(nic, netdev))
  1241. return -EBUSY;
  1242. } else {
  1243. nic->set_mac_pending = true;
  1244. }
  1245. return 0;
  1246. }
  1247. void nicvf_update_lmac_stats(struct nicvf *nic)
  1248. {
  1249. int stat = 0;
  1250. union nic_mbx mbx = {};
  1251. if (!netif_running(nic->netdev))
  1252. return;
  1253. mbx.bgx_stats.msg = NIC_MBOX_MSG_BGX_STATS;
  1254. mbx.bgx_stats.vf_id = nic->vf_id;
  1255. /* Rx stats */
  1256. mbx.bgx_stats.rx = 1;
  1257. while (stat < BGX_RX_STATS_COUNT) {
  1258. mbx.bgx_stats.idx = stat;
  1259. if (nicvf_send_msg_to_pf(nic, &mbx))
  1260. return;
  1261. stat++;
  1262. }
  1263. stat = 0;
  1264. /* Tx stats */
  1265. mbx.bgx_stats.rx = 0;
  1266. while (stat < BGX_TX_STATS_COUNT) {
  1267. mbx.bgx_stats.idx = stat;
  1268. if (nicvf_send_msg_to_pf(nic, &mbx))
  1269. return;
  1270. stat++;
  1271. }
  1272. }
  1273. void nicvf_update_stats(struct nicvf *nic)
  1274. {
  1275. int qidx, cpu;
  1276. u64 tmp_stats = 0;
  1277. struct nicvf_hw_stats *stats = &nic->hw_stats;
  1278. struct nicvf_drv_stats *drv_stats;
  1279. struct queue_set *qs = nic->qs;
  1280. #define GET_RX_STATS(reg) \
  1281. nicvf_reg_read(nic, NIC_VNIC_RX_STAT_0_13 | (reg << 3))
  1282. #define GET_TX_STATS(reg) \
  1283. nicvf_reg_read(nic, NIC_VNIC_TX_STAT_0_4 | (reg << 3))
  1284. stats->rx_bytes = GET_RX_STATS(RX_OCTS);
  1285. stats->rx_ucast_frames = GET_RX_STATS(RX_UCAST);
  1286. stats->rx_bcast_frames = GET_RX_STATS(RX_BCAST);
  1287. stats->rx_mcast_frames = GET_RX_STATS(RX_MCAST);
  1288. stats->rx_fcs_errors = GET_RX_STATS(RX_FCS);
  1289. stats->rx_l2_errors = GET_RX_STATS(RX_L2ERR);
  1290. stats->rx_drop_red = GET_RX_STATS(RX_RED);
  1291. stats->rx_drop_red_bytes = GET_RX_STATS(RX_RED_OCTS);
  1292. stats->rx_drop_overrun = GET_RX_STATS(RX_ORUN);
  1293. stats->rx_drop_overrun_bytes = GET_RX_STATS(RX_ORUN_OCTS);
  1294. stats->rx_drop_bcast = GET_RX_STATS(RX_DRP_BCAST);
  1295. stats->rx_drop_mcast = GET_RX_STATS(RX_DRP_MCAST);
  1296. stats->rx_drop_l3_bcast = GET_RX_STATS(RX_DRP_L3BCAST);
  1297. stats->rx_drop_l3_mcast = GET_RX_STATS(RX_DRP_L3MCAST);
  1298. stats->tx_bytes = GET_TX_STATS(TX_OCTS);
  1299. stats->tx_ucast_frames = GET_TX_STATS(TX_UCAST);
  1300. stats->tx_bcast_frames = GET_TX_STATS(TX_BCAST);
  1301. stats->tx_mcast_frames = GET_TX_STATS(TX_MCAST);
  1302. stats->tx_drops = GET_TX_STATS(TX_DROP);
  1303. /* On T88 pass 2.0, the dummy SQE added for TSO notification
  1304. * via CQE has 'dont_send' set. Hence HW drops the pkt pointed
  1305. * pointed by dummy SQE and results in tx_drops counter being
  1306. * incremented. Subtracting it from tx_tso counter will give
  1307. * exact tx_drops counter.
  1308. */
  1309. if (nic->t88 && nic->hw_tso) {
  1310. for_each_possible_cpu(cpu) {
  1311. drv_stats = per_cpu_ptr(nic->drv_stats, cpu);
  1312. tmp_stats += drv_stats->tx_tso;
  1313. }
  1314. stats->tx_drops = tmp_stats - stats->tx_drops;
  1315. }
  1316. stats->tx_frames = stats->tx_ucast_frames +
  1317. stats->tx_bcast_frames +
  1318. stats->tx_mcast_frames;
  1319. stats->rx_frames = stats->rx_ucast_frames +
  1320. stats->rx_bcast_frames +
  1321. stats->rx_mcast_frames;
  1322. stats->rx_drops = stats->rx_drop_red +
  1323. stats->rx_drop_overrun;
  1324. /* Update RQ and SQ stats */
  1325. for (qidx = 0; qidx < qs->rq_cnt; qidx++)
  1326. nicvf_update_rq_stats(nic, qidx);
  1327. for (qidx = 0; qidx < qs->sq_cnt; qidx++)
  1328. nicvf_update_sq_stats(nic, qidx);
  1329. }
  1330. static void nicvf_get_stats64(struct net_device *netdev,
  1331. struct rtnl_link_stats64 *stats)
  1332. {
  1333. struct nicvf *nic = netdev_priv(netdev);
  1334. struct nicvf_hw_stats *hw_stats = &nic->hw_stats;
  1335. nicvf_update_stats(nic);
  1336. stats->rx_bytes = hw_stats->rx_bytes;
  1337. stats->rx_packets = hw_stats->rx_frames;
  1338. stats->rx_dropped = hw_stats->rx_drops;
  1339. stats->multicast = hw_stats->rx_mcast_frames;
  1340. stats->tx_bytes = hw_stats->tx_bytes;
  1341. stats->tx_packets = hw_stats->tx_frames;
  1342. stats->tx_dropped = hw_stats->tx_drops;
  1343. }
  1344. static void nicvf_tx_timeout(struct net_device *dev)
  1345. {
  1346. struct nicvf *nic = netdev_priv(dev);
  1347. if (netif_msg_tx_err(nic))
  1348. netdev_warn(dev, "%s: Transmit timed out, resetting\n",
  1349. dev->name);
  1350. this_cpu_inc(nic->drv_stats->tx_timeout);
  1351. schedule_work(&nic->reset_task);
  1352. }
  1353. static void nicvf_reset_task(struct work_struct *work)
  1354. {
  1355. struct nicvf *nic;
  1356. nic = container_of(work, struct nicvf, reset_task);
  1357. if (!netif_running(nic->netdev))
  1358. return;
  1359. nicvf_stop(nic->netdev);
  1360. nicvf_open(nic->netdev);
  1361. netif_trans_update(nic->netdev);
  1362. }
  1363. static int nicvf_config_loopback(struct nicvf *nic,
  1364. netdev_features_t features)
  1365. {
  1366. union nic_mbx mbx = {};
  1367. mbx.lbk.msg = NIC_MBOX_MSG_LOOPBACK;
  1368. mbx.lbk.vf_id = nic->vf_id;
  1369. mbx.lbk.enable = (features & NETIF_F_LOOPBACK) != 0;
  1370. return nicvf_send_msg_to_pf(nic, &mbx);
  1371. }
  1372. static netdev_features_t nicvf_fix_features(struct net_device *netdev,
  1373. netdev_features_t features)
  1374. {
  1375. struct nicvf *nic = netdev_priv(netdev);
  1376. if ((features & NETIF_F_LOOPBACK) &&
  1377. netif_running(netdev) && !nic->loopback_supported)
  1378. features &= ~NETIF_F_LOOPBACK;
  1379. return features;
  1380. }
  1381. static int nicvf_set_features(struct net_device *netdev,
  1382. netdev_features_t features)
  1383. {
  1384. struct nicvf *nic = netdev_priv(netdev);
  1385. netdev_features_t changed = features ^ netdev->features;
  1386. if (changed & NETIF_F_HW_VLAN_CTAG_RX)
  1387. nicvf_config_vlan_stripping(nic, features);
  1388. if ((changed & NETIF_F_LOOPBACK) && netif_running(netdev))
  1389. return nicvf_config_loopback(nic, features);
  1390. return 0;
  1391. }
  1392. static void nicvf_set_xdp_queues(struct nicvf *nic, bool bpf_attached)
  1393. {
  1394. u8 cq_count, txq_count;
  1395. /* Set XDP Tx queue count same as Rx queue count */
  1396. if (!bpf_attached)
  1397. nic->xdp_tx_queues = 0;
  1398. else
  1399. nic->xdp_tx_queues = nic->rx_queues;
  1400. /* If queue count > MAX_CMP_QUEUES_PER_QS, then additional qsets
  1401. * needs to be allocated, check how many.
  1402. */
  1403. txq_count = nic->xdp_tx_queues + nic->tx_queues;
  1404. cq_count = max(nic->rx_queues, txq_count);
  1405. if (cq_count > MAX_CMP_QUEUES_PER_QS) {
  1406. nic->sqs_count = roundup(cq_count, MAX_CMP_QUEUES_PER_QS);
  1407. nic->sqs_count = (nic->sqs_count / MAX_CMP_QUEUES_PER_QS) - 1;
  1408. } else {
  1409. nic->sqs_count = 0;
  1410. }
  1411. /* Set primary Qset's resources */
  1412. nic->qs->rq_cnt = min_t(u8, nic->rx_queues, MAX_RCV_QUEUES_PER_QS);
  1413. nic->qs->sq_cnt = min_t(u8, txq_count, MAX_SND_QUEUES_PER_QS);
  1414. nic->qs->cq_cnt = max_t(u8, nic->qs->rq_cnt, nic->qs->sq_cnt);
  1415. /* Update stack */
  1416. nicvf_set_real_num_queues(nic->netdev, nic->tx_queues, nic->rx_queues);
  1417. }
  1418. static int nicvf_xdp_setup(struct nicvf *nic, struct bpf_prog *prog)
  1419. {
  1420. struct net_device *dev = nic->netdev;
  1421. bool if_up = netif_running(nic->netdev);
  1422. struct bpf_prog *old_prog;
  1423. bool bpf_attached = false;
  1424. /* For now just support only the usual MTU sized frames */
  1425. if (prog && (dev->mtu > 1500)) {
  1426. netdev_warn(dev, "Jumbo frames not yet supported with XDP, current MTU %d.\n",
  1427. dev->mtu);
  1428. return -EOPNOTSUPP;
  1429. }
  1430. /* ALL SQs attached to CQs i.e same as RQs, are treated as
  1431. * XDP Tx queues and more Tx queues are allocated for
  1432. * network stack to send pkts out.
  1433. *
  1434. * No of Tx queues are either same as Rx queues or whatever
  1435. * is left in max no of queues possible.
  1436. */
  1437. if ((nic->rx_queues + nic->tx_queues) > nic->max_queues) {
  1438. netdev_warn(dev,
  1439. "Failed to attach BPF prog, RXQs + TXQs > Max %d\n",
  1440. nic->max_queues);
  1441. return -ENOMEM;
  1442. }
  1443. if (if_up)
  1444. nicvf_stop(nic->netdev);
  1445. old_prog = xchg(&nic->xdp_prog, prog);
  1446. /* Detach old prog, if any */
  1447. if (old_prog)
  1448. bpf_prog_put(old_prog);
  1449. if (nic->xdp_prog) {
  1450. /* Attach BPF program */
  1451. nic->xdp_prog = bpf_prog_add(nic->xdp_prog, nic->rx_queues - 1);
  1452. if (!IS_ERR(nic->xdp_prog))
  1453. bpf_attached = true;
  1454. }
  1455. /* Calculate Tx queues needed for XDP and network stack */
  1456. nicvf_set_xdp_queues(nic, bpf_attached);
  1457. if (if_up) {
  1458. /* Reinitialize interface, clean slate */
  1459. nicvf_open(nic->netdev);
  1460. netif_trans_update(nic->netdev);
  1461. }
  1462. return 0;
  1463. }
  1464. static int nicvf_xdp(struct net_device *netdev, struct netdev_xdp *xdp)
  1465. {
  1466. struct nicvf *nic = netdev_priv(netdev);
  1467. /* To avoid checks while retrieving buffer address from CQE_RX,
  1468. * do not support XDP for T88 pass1.x silicons which are anyway
  1469. * not in use widely.
  1470. */
  1471. if (pass1_silicon(nic->pdev))
  1472. return -EOPNOTSUPP;
  1473. switch (xdp->command) {
  1474. case XDP_SETUP_PROG:
  1475. return nicvf_xdp_setup(nic, xdp->prog);
  1476. case XDP_QUERY_PROG:
  1477. xdp->prog_attached = !!nic->xdp_prog;
  1478. return 0;
  1479. default:
  1480. return -EINVAL;
  1481. }
  1482. }
  1483. static const struct net_device_ops nicvf_netdev_ops = {
  1484. .ndo_open = nicvf_open,
  1485. .ndo_stop = nicvf_stop,
  1486. .ndo_start_xmit = nicvf_xmit,
  1487. .ndo_change_mtu = nicvf_change_mtu,
  1488. .ndo_set_mac_address = nicvf_set_mac_address,
  1489. .ndo_get_stats64 = nicvf_get_stats64,
  1490. .ndo_tx_timeout = nicvf_tx_timeout,
  1491. .ndo_fix_features = nicvf_fix_features,
  1492. .ndo_set_features = nicvf_set_features,
  1493. .ndo_xdp = nicvf_xdp,
  1494. };
  1495. static int nicvf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1496. {
  1497. struct device *dev = &pdev->dev;
  1498. struct net_device *netdev;
  1499. struct nicvf *nic;
  1500. int err, qcount;
  1501. u16 sdevid;
  1502. err = pci_enable_device(pdev);
  1503. if (err) {
  1504. dev_err(dev, "Failed to enable PCI device\n");
  1505. return err;
  1506. }
  1507. err = pci_request_regions(pdev, DRV_NAME);
  1508. if (err) {
  1509. dev_err(dev, "PCI request regions failed 0x%x\n", err);
  1510. goto err_disable_device;
  1511. }
  1512. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(48));
  1513. if (err) {
  1514. dev_err(dev, "Unable to get usable DMA configuration\n");
  1515. goto err_release_regions;
  1516. }
  1517. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(48));
  1518. if (err) {
  1519. dev_err(dev, "unable to get 48-bit DMA for consistent allocations\n");
  1520. goto err_release_regions;
  1521. }
  1522. qcount = netif_get_num_default_rss_queues();
  1523. /* Restrict multiqset support only for host bound VFs */
  1524. if (pdev->is_virtfn) {
  1525. /* Set max number of queues per VF */
  1526. qcount = min_t(int, num_online_cpus(),
  1527. (MAX_SQS_PER_VF + 1) * MAX_CMP_QUEUES_PER_QS);
  1528. }
  1529. netdev = alloc_etherdev_mqs(sizeof(struct nicvf), qcount, qcount);
  1530. if (!netdev) {
  1531. err = -ENOMEM;
  1532. goto err_release_regions;
  1533. }
  1534. pci_set_drvdata(pdev, netdev);
  1535. SET_NETDEV_DEV(netdev, &pdev->dev);
  1536. nic = netdev_priv(netdev);
  1537. nic->netdev = netdev;
  1538. nic->pdev = pdev;
  1539. nic->pnicvf = nic;
  1540. nic->max_queues = qcount;
  1541. /* MAP VF's configuration registers */
  1542. nic->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
  1543. if (!nic->reg_base) {
  1544. dev_err(dev, "Cannot map config register space, aborting\n");
  1545. err = -ENOMEM;
  1546. goto err_free_netdev;
  1547. }
  1548. nic->drv_stats = netdev_alloc_pcpu_stats(struct nicvf_drv_stats);
  1549. if (!nic->drv_stats) {
  1550. err = -ENOMEM;
  1551. goto err_free_netdev;
  1552. }
  1553. err = nicvf_set_qset_resources(nic);
  1554. if (err)
  1555. goto err_free_netdev;
  1556. /* Check if PF is alive and get MAC address for this VF */
  1557. err = nicvf_register_misc_interrupt(nic);
  1558. if (err)
  1559. goto err_free_netdev;
  1560. nicvf_send_vf_struct(nic);
  1561. if (!pass1_silicon(nic->pdev))
  1562. nic->hw_tso = true;
  1563. /* Get iommu domain for iova to physical addr conversion */
  1564. nic->iommu_domain = iommu_get_domain_for_dev(dev);
  1565. pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid);
  1566. if (sdevid == 0xA134)
  1567. nic->t88 = true;
  1568. /* Check if this VF is in QS only mode */
  1569. if (nic->sqs_mode)
  1570. return 0;
  1571. err = nicvf_set_real_num_queues(netdev, nic->tx_queues, nic->rx_queues);
  1572. if (err)
  1573. goto err_unregister_interrupts;
  1574. netdev->hw_features = (NETIF_F_RXCSUM | NETIF_F_SG |
  1575. NETIF_F_TSO | NETIF_F_GRO | NETIF_F_TSO6 |
  1576. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  1577. NETIF_F_HW_VLAN_CTAG_RX);
  1578. netdev->hw_features |= NETIF_F_RXHASH;
  1579. netdev->features |= netdev->hw_features;
  1580. netdev->hw_features |= NETIF_F_LOOPBACK;
  1581. netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM |
  1582. NETIF_F_IPV6_CSUM | NETIF_F_TSO | NETIF_F_TSO6;
  1583. netdev->netdev_ops = &nicvf_netdev_ops;
  1584. netdev->watchdog_timeo = NICVF_TX_TIMEOUT;
  1585. /* MTU range: 64 - 9200 */
  1586. netdev->min_mtu = NIC_HW_MIN_FRS;
  1587. netdev->max_mtu = NIC_HW_MAX_FRS;
  1588. INIT_WORK(&nic->reset_task, nicvf_reset_task);
  1589. err = register_netdev(netdev);
  1590. if (err) {
  1591. dev_err(dev, "Failed to register netdevice\n");
  1592. goto err_unregister_interrupts;
  1593. }
  1594. nic->msg_enable = debug;
  1595. nicvf_set_ethtool_ops(netdev);
  1596. return 0;
  1597. err_unregister_interrupts:
  1598. nicvf_unregister_interrupts(nic);
  1599. err_free_netdev:
  1600. pci_set_drvdata(pdev, NULL);
  1601. if (nic->drv_stats)
  1602. free_percpu(nic->drv_stats);
  1603. free_netdev(netdev);
  1604. err_release_regions:
  1605. pci_release_regions(pdev);
  1606. err_disable_device:
  1607. pci_disable_device(pdev);
  1608. return err;
  1609. }
  1610. static void nicvf_remove(struct pci_dev *pdev)
  1611. {
  1612. struct net_device *netdev = pci_get_drvdata(pdev);
  1613. struct nicvf *nic;
  1614. struct net_device *pnetdev;
  1615. if (!netdev)
  1616. return;
  1617. nic = netdev_priv(netdev);
  1618. pnetdev = nic->pnicvf->netdev;
  1619. /* Check if this Qset is assigned to different VF.
  1620. * If yes, clean primary and all secondary Qsets.
  1621. */
  1622. if (pnetdev && (pnetdev->reg_state == NETREG_REGISTERED))
  1623. unregister_netdev(pnetdev);
  1624. nicvf_unregister_interrupts(nic);
  1625. pci_set_drvdata(pdev, NULL);
  1626. if (nic->drv_stats)
  1627. free_percpu(nic->drv_stats);
  1628. free_netdev(netdev);
  1629. pci_release_regions(pdev);
  1630. pci_disable_device(pdev);
  1631. }
  1632. static void nicvf_shutdown(struct pci_dev *pdev)
  1633. {
  1634. nicvf_remove(pdev);
  1635. }
  1636. static struct pci_driver nicvf_driver = {
  1637. .name = DRV_NAME,
  1638. .id_table = nicvf_id_table,
  1639. .probe = nicvf_probe,
  1640. .remove = nicvf_remove,
  1641. .shutdown = nicvf_shutdown,
  1642. };
  1643. static int __init nicvf_init_module(void)
  1644. {
  1645. pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
  1646. return pci_register_driver(&nicvf_driver);
  1647. }
  1648. static void __exit nicvf_cleanup_module(void)
  1649. {
  1650. pci_unregister_driver(&nicvf_driver);
  1651. }
  1652. module_init(nicvf_init_module);
  1653. module_exit(nicvf_cleanup_module);