nicvf_main.c 50 KB

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