nic_main.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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/etherdevice.h>
  12. #include <linux/of.h>
  13. #include <linux/if_vlan.h>
  14. #include "nic_reg.h"
  15. #include "nic.h"
  16. #include "q_struct.h"
  17. #include "thunder_bgx.h"
  18. #define DRV_NAME "nicpf"
  19. #define DRV_VERSION "1.0"
  20. #define NIC_VF_PER_MBX_REG 64
  21. struct hw_info {
  22. u8 bgx_cnt;
  23. u8 chans_per_lmac;
  24. u8 chans_per_bgx; /* Rx/Tx chans */
  25. u8 chans_per_rgx;
  26. u8 chans_per_lbk;
  27. u16 cpi_cnt;
  28. u16 rssi_cnt;
  29. u16 rss_ind_tbl_size;
  30. u16 tl4_cnt;
  31. u16 tl3_cnt;
  32. u8 tl2_cnt;
  33. u8 tl1_cnt;
  34. bool tl1_per_bgx; /* TL1 per BGX or per LMAC */
  35. };
  36. struct nicpf {
  37. struct pci_dev *pdev;
  38. struct hw_info *hw;
  39. u8 node;
  40. unsigned int flags;
  41. u8 num_vf_en; /* No of VF enabled */
  42. bool vf_enabled[MAX_NUM_VFS_SUPPORTED];
  43. void __iomem *reg_base; /* Register start address */
  44. u8 num_sqs_en; /* Secondary qsets enabled */
  45. u64 nicvf[MAX_NUM_VFS_SUPPORTED];
  46. u8 vf_sqs[MAX_NUM_VFS_SUPPORTED][MAX_SQS_PER_VF];
  47. u8 pqs_vf[MAX_NUM_VFS_SUPPORTED];
  48. bool sqs_used[MAX_NUM_VFS_SUPPORTED];
  49. struct pkind_cfg pkind;
  50. #define NIC_SET_VF_LMAC_MAP(bgx, lmac) (((bgx & 0xF) << 4) | (lmac & 0xF))
  51. #define NIC_GET_BGX_FROM_VF_LMAC_MAP(map) ((map >> 4) & 0xF)
  52. #define NIC_GET_LMAC_FROM_VF_LMAC_MAP(map) (map & 0xF)
  53. u8 *vf_lmac_map;
  54. struct delayed_work dwork;
  55. struct workqueue_struct *check_link;
  56. u8 *link;
  57. u8 *duplex;
  58. u32 *speed;
  59. u16 cpi_base[MAX_NUM_VFS_SUPPORTED];
  60. u16 rssi_base[MAX_NUM_VFS_SUPPORTED];
  61. bool mbx_lock[MAX_NUM_VFS_SUPPORTED];
  62. /* MSI-X */
  63. u8 num_vec;
  64. bool irq_allocated[NIC_PF_MSIX_VECTORS];
  65. char irq_name[NIC_PF_MSIX_VECTORS][20];
  66. };
  67. /* Supported devices */
  68. static const struct pci_device_id nic_id_table[] = {
  69. { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_NIC_PF) },
  70. { 0, } /* end of table */
  71. };
  72. MODULE_AUTHOR("Sunil Goutham");
  73. MODULE_DESCRIPTION("Cavium Thunder NIC Physical Function Driver");
  74. MODULE_LICENSE("GPL v2");
  75. MODULE_VERSION(DRV_VERSION);
  76. MODULE_DEVICE_TABLE(pci, nic_id_table);
  77. /* The Cavium ThunderX network controller can *only* be found in SoCs
  78. * containing the ThunderX ARM64 CPU implementation. All accesses to the device
  79. * registers on this platform are implicitly strongly ordered with respect
  80. * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
  81. * with no memory barriers in this driver. The readq()/writeq() functions add
  82. * explicit ordering operation which in this case are redundant, and only
  83. * add overhead.
  84. */
  85. /* Register read/write APIs */
  86. static void nic_reg_write(struct nicpf *nic, u64 offset, u64 val)
  87. {
  88. writeq_relaxed(val, nic->reg_base + offset);
  89. }
  90. static u64 nic_reg_read(struct nicpf *nic, u64 offset)
  91. {
  92. return readq_relaxed(nic->reg_base + offset);
  93. }
  94. /* PF -> VF mailbox communication APIs */
  95. static void nic_enable_mbx_intr(struct nicpf *nic)
  96. {
  97. int vf_cnt = pci_sriov_get_totalvfs(nic->pdev);
  98. #define INTR_MASK(vfs) ((vfs < 64) ? (BIT_ULL(vfs) - 1) : (~0ull))
  99. /* Clear it, to avoid spurious interrupts (if any) */
  100. nic_reg_write(nic, NIC_PF_MAILBOX_INT, INTR_MASK(vf_cnt));
  101. /* Enable mailbox interrupt for all VFs */
  102. nic_reg_write(nic, NIC_PF_MAILBOX_ENA_W1S, INTR_MASK(vf_cnt));
  103. /* One mailbox intr enable reg per 64 VFs */
  104. if (vf_cnt > 64) {
  105. nic_reg_write(nic, NIC_PF_MAILBOX_INT + sizeof(u64),
  106. INTR_MASK(vf_cnt - 64));
  107. nic_reg_write(nic, NIC_PF_MAILBOX_ENA_W1S + sizeof(u64),
  108. INTR_MASK(vf_cnt - 64));
  109. }
  110. }
  111. static void nic_clear_mbx_intr(struct nicpf *nic, int vf, int mbx_reg)
  112. {
  113. nic_reg_write(nic, NIC_PF_MAILBOX_INT + (mbx_reg << 3), BIT_ULL(vf));
  114. }
  115. static u64 nic_get_mbx_addr(int vf)
  116. {
  117. return NIC_PF_VF_0_127_MAILBOX_0_1 + (vf << NIC_VF_NUM_SHIFT);
  118. }
  119. /* Send a mailbox message to VF
  120. * @vf: vf to which this message to be sent
  121. * @mbx: Message to be sent
  122. */
  123. static void nic_send_msg_to_vf(struct nicpf *nic, int vf, union nic_mbx *mbx)
  124. {
  125. void __iomem *mbx_addr = nic->reg_base + nic_get_mbx_addr(vf);
  126. u64 *msg = (u64 *)mbx;
  127. /* In first revision HW, mbox interrupt is triggerred
  128. * when PF writes to MBOX(1), in next revisions when
  129. * PF writes to MBOX(0)
  130. */
  131. if (pass1_silicon(nic->pdev)) {
  132. /* see the comment for nic_reg_write()/nic_reg_read()
  133. * functions above
  134. */
  135. writeq_relaxed(msg[0], mbx_addr);
  136. writeq_relaxed(msg[1], mbx_addr + 8);
  137. } else {
  138. writeq_relaxed(msg[1], mbx_addr + 8);
  139. writeq_relaxed(msg[0], mbx_addr);
  140. }
  141. }
  142. /* Responds to VF's READY message with VF's
  143. * ID, node, MAC address e.t.c
  144. * @vf: VF which sent READY message
  145. */
  146. static void nic_mbx_send_ready(struct nicpf *nic, int vf)
  147. {
  148. union nic_mbx mbx = {};
  149. int bgx_idx, lmac;
  150. const char *mac;
  151. mbx.nic_cfg.msg = NIC_MBOX_MSG_READY;
  152. mbx.nic_cfg.vf_id = vf;
  153. mbx.nic_cfg.tns_mode = NIC_TNS_BYPASS_MODE;
  154. if (vf < nic->num_vf_en) {
  155. bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  156. lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  157. mac = bgx_get_lmac_mac(nic->node, bgx_idx, lmac);
  158. if (mac)
  159. ether_addr_copy((u8 *)&mbx.nic_cfg.mac_addr, mac);
  160. }
  161. mbx.nic_cfg.sqs_mode = (vf >= nic->num_vf_en) ? true : false;
  162. mbx.nic_cfg.node_id = nic->node;
  163. mbx.nic_cfg.loopback_supported = vf < nic->num_vf_en;
  164. nic_send_msg_to_vf(nic, vf, &mbx);
  165. }
  166. /* ACKs VF's mailbox message
  167. * @vf: VF to which ACK to be sent
  168. */
  169. static void nic_mbx_send_ack(struct nicpf *nic, int vf)
  170. {
  171. union nic_mbx mbx = {};
  172. mbx.msg.msg = NIC_MBOX_MSG_ACK;
  173. nic_send_msg_to_vf(nic, vf, &mbx);
  174. }
  175. /* NACKs VF's mailbox message that PF is not able to
  176. * complete the action
  177. * @vf: VF to which ACK to be sent
  178. */
  179. static void nic_mbx_send_nack(struct nicpf *nic, int vf)
  180. {
  181. union nic_mbx mbx = {};
  182. mbx.msg.msg = NIC_MBOX_MSG_NACK;
  183. nic_send_msg_to_vf(nic, vf, &mbx);
  184. }
  185. /* Flush all in flight receive packets to memory and
  186. * bring down an active RQ
  187. */
  188. static int nic_rcv_queue_sw_sync(struct nicpf *nic)
  189. {
  190. u16 timeout = ~0x00;
  191. nic_reg_write(nic, NIC_PF_SW_SYNC_RX, 0x01);
  192. /* Wait till sync cycle is finished */
  193. while (timeout) {
  194. if (nic_reg_read(nic, NIC_PF_SW_SYNC_RX_DONE) & 0x1)
  195. break;
  196. timeout--;
  197. }
  198. nic_reg_write(nic, NIC_PF_SW_SYNC_RX, 0x00);
  199. if (!timeout) {
  200. dev_err(&nic->pdev->dev, "Receive queue software sync failed");
  201. return 1;
  202. }
  203. return 0;
  204. }
  205. /* Get BGX Rx/Tx stats and respond to VF's request */
  206. static void nic_get_bgx_stats(struct nicpf *nic, struct bgx_stats_msg *bgx)
  207. {
  208. int bgx_idx, lmac;
  209. union nic_mbx mbx = {};
  210. bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[bgx->vf_id]);
  211. lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[bgx->vf_id]);
  212. mbx.bgx_stats.msg = NIC_MBOX_MSG_BGX_STATS;
  213. mbx.bgx_stats.vf_id = bgx->vf_id;
  214. mbx.bgx_stats.rx = bgx->rx;
  215. mbx.bgx_stats.idx = bgx->idx;
  216. if (bgx->rx)
  217. mbx.bgx_stats.stats = bgx_get_rx_stats(nic->node, bgx_idx,
  218. lmac, bgx->idx);
  219. else
  220. mbx.bgx_stats.stats = bgx_get_tx_stats(nic->node, bgx_idx,
  221. lmac, bgx->idx);
  222. nic_send_msg_to_vf(nic, bgx->vf_id, &mbx);
  223. }
  224. /* Update hardware min/max frame size */
  225. static int nic_update_hw_frs(struct nicpf *nic, int new_frs, int vf)
  226. {
  227. int bgx, lmac, lmac_cnt;
  228. u64 lmac_credits;
  229. if ((new_frs > NIC_HW_MAX_FRS) || (new_frs < NIC_HW_MIN_FRS))
  230. return 1;
  231. bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  232. lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  233. lmac += bgx * MAX_LMAC_PER_BGX;
  234. new_frs += VLAN_ETH_HLEN + ETH_FCS_LEN + 4;
  235. /* Update corresponding LMAC credits */
  236. lmac_cnt = bgx_get_lmac_count(nic->node, bgx);
  237. lmac_credits = nic_reg_read(nic, NIC_PF_LMAC_0_7_CREDIT + (lmac * 8));
  238. lmac_credits &= ~(0xFFFFFULL << 12);
  239. lmac_credits |= (((((48 * 1024) / lmac_cnt) - new_frs) / 16) << 12);
  240. nic_reg_write(nic, NIC_PF_LMAC_0_7_CREDIT + (lmac * 8), lmac_credits);
  241. /* Enforce MTU in HW
  242. * This config is supported only from 88xx pass 2.0 onwards.
  243. */
  244. if (!pass1_silicon(nic->pdev))
  245. nic_reg_write(nic,
  246. NIC_PF_LMAC_0_7_CFG2 + (lmac * 8), new_frs);
  247. return 0;
  248. }
  249. /* Set minimum transmit packet size */
  250. static void nic_set_tx_pkt_pad(struct nicpf *nic, int size)
  251. {
  252. int lmac, max_lmac;
  253. u16 sdevid;
  254. u64 lmac_cfg;
  255. /* There is a issue in HW where-in while sending GSO sized
  256. * pkts as part of TSO, if pkt len falls below this size
  257. * NIC will zero PAD packet and also updates IP total length.
  258. * Hence set this value to lessthan min pkt size of MAC+IP+TCP
  259. * headers, BGX will do the padding to transmit 64 byte pkt.
  260. */
  261. if (size > 52)
  262. size = 52;
  263. pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid);
  264. /* 81xx's RGX has only one LMAC */
  265. if (sdevid == PCI_SUBSYS_DEVID_81XX_NIC_PF)
  266. max_lmac = ((nic->hw->bgx_cnt - 1) * MAX_LMAC_PER_BGX) + 1;
  267. else
  268. max_lmac = nic->hw->bgx_cnt * MAX_LMAC_PER_BGX;
  269. for (lmac = 0; lmac < max_lmac; lmac++) {
  270. lmac_cfg = nic_reg_read(nic, NIC_PF_LMAC_0_7_CFG | (lmac << 3));
  271. lmac_cfg &= ~(0xF << 2);
  272. lmac_cfg |= ((size / 4) << 2);
  273. nic_reg_write(nic, NIC_PF_LMAC_0_7_CFG | (lmac << 3), lmac_cfg);
  274. }
  275. }
  276. /* Function to check number of LMACs present and set VF::LMAC mapping.
  277. * Mapping will be used while initializing channels.
  278. */
  279. static void nic_set_lmac_vf_mapping(struct nicpf *nic)
  280. {
  281. unsigned bgx_map = bgx_get_map(nic->node);
  282. int bgx, next_bgx_lmac = 0;
  283. int lmac, lmac_cnt = 0;
  284. u64 lmac_credit;
  285. nic->num_vf_en = 0;
  286. for (bgx = 0; bgx < nic->hw->bgx_cnt; bgx++) {
  287. if (!(bgx_map & (1 << bgx)))
  288. continue;
  289. lmac_cnt = bgx_get_lmac_count(nic->node, bgx);
  290. for (lmac = 0; lmac < lmac_cnt; lmac++)
  291. nic->vf_lmac_map[next_bgx_lmac++] =
  292. NIC_SET_VF_LMAC_MAP(bgx, lmac);
  293. nic->num_vf_en += lmac_cnt;
  294. /* Program LMAC credits */
  295. lmac_credit = (1ull << 1); /* channel credit enable */
  296. lmac_credit |= (0x1ff << 2); /* Max outstanding pkt count */
  297. /* 48KB BGX Tx buffer size, each unit is of size 16bytes */
  298. lmac_credit |= (((((48 * 1024) / lmac_cnt) -
  299. NIC_HW_MAX_FRS) / 16) << 12);
  300. lmac = bgx * MAX_LMAC_PER_BGX;
  301. for (; lmac < lmac_cnt + (bgx * MAX_LMAC_PER_BGX); lmac++)
  302. nic_reg_write(nic,
  303. NIC_PF_LMAC_0_7_CREDIT + (lmac * 8),
  304. lmac_credit);
  305. /* On CN81XX there are only 8 VFs but max possible no of
  306. * interfaces are 9.
  307. */
  308. if (nic->num_vf_en >= pci_sriov_get_totalvfs(nic->pdev)) {
  309. nic->num_vf_en = pci_sriov_get_totalvfs(nic->pdev);
  310. break;
  311. }
  312. }
  313. }
  314. static void nic_get_hw_info(struct nicpf *nic)
  315. {
  316. u16 sdevid;
  317. struct hw_info *hw = nic->hw;
  318. pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid);
  319. switch (sdevid) {
  320. case PCI_SUBSYS_DEVID_88XX_NIC_PF:
  321. hw->bgx_cnt = MAX_BGX_PER_CN88XX;
  322. hw->chans_per_lmac = 16;
  323. hw->chans_per_bgx = 128;
  324. hw->cpi_cnt = 2048;
  325. hw->rssi_cnt = 4096;
  326. hw->rss_ind_tbl_size = NIC_MAX_RSS_IDR_TBL_SIZE;
  327. hw->tl3_cnt = 256;
  328. hw->tl2_cnt = 64;
  329. hw->tl1_cnt = 2;
  330. hw->tl1_per_bgx = true;
  331. break;
  332. case PCI_SUBSYS_DEVID_81XX_NIC_PF:
  333. hw->bgx_cnt = MAX_BGX_PER_CN81XX;
  334. hw->chans_per_lmac = 8;
  335. hw->chans_per_bgx = 32;
  336. hw->chans_per_rgx = 8;
  337. hw->chans_per_lbk = 24;
  338. hw->cpi_cnt = 512;
  339. hw->rssi_cnt = 256;
  340. hw->rss_ind_tbl_size = 32; /* Max RSSI / Max interfaces */
  341. hw->tl3_cnt = 64;
  342. hw->tl2_cnt = 16;
  343. hw->tl1_cnt = 10;
  344. hw->tl1_per_bgx = false;
  345. break;
  346. case PCI_SUBSYS_DEVID_83XX_NIC_PF:
  347. hw->bgx_cnt = MAX_BGX_PER_CN83XX;
  348. hw->chans_per_lmac = 8;
  349. hw->chans_per_bgx = 32;
  350. hw->chans_per_lbk = 64;
  351. hw->cpi_cnt = 2048;
  352. hw->rssi_cnt = 1024;
  353. hw->rss_ind_tbl_size = 64; /* Max RSSI / Max interfaces */
  354. hw->tl3_cnt = 256;
  355. hw->tl2_cnt = 64;
  356. hw->tl1_cnt = 18;
  357. hw->tl1_per_bgx = false;
  358. break;
  359. }
  360. hw->tl4_cnt = MAX_QUEUES_PER_QSET * pci_sriov_get_totalvfs(nic->pdev);
  361. }
  362. #define BGX0_BLOCK 8
  363. #define BGX1_BLOCK 9
  364. static void nic_init_hw(struct nicpf *nic)
  365. {
  366. int i;
  367. u64 cqm_cfg;
  368. /* Enable NIC HW block */
  369. nic_reg_write(nic, NIC_PF_CFG, 0x3);
  370. /* Enable backpressure */
  371. nic_reg_write(nic, NIC_PF_BP_CFG, (1ULL << 6) | 0x03);
  372. /* TNS and TNS bypass modes are present only on 88xx
  373. * Also offset of this CSR has changed in 81xx and 83xx.
  374. */
  375. if (nic->pdev->subsystem_device == PCI_SUBSYS_DEVID_88XX_NIC_PF) {
  376. /* Disable TNS mode on both interfaces */
  377. nic_reg_write(nic, NIC_PF_INTF_0_1_SEND_CFG,
  378. (NIC_TNS_BYPASS_MODE << 7) |
  379. BGX0_BLOCK | (1ULL << 16));
  380. nic_reg_write(nic, NIC_PF_INTF_0_1_SEND_CFG | (1 << 8),
  381. (NIC_TNS_BYPASS_MODE << 7) |
  382. BGX1_BLOCK | (1ULL << 16));
  383. } else {
  384. /* Configure timestamp generation timeout to 10us */
  385. for (i = 0; i < nic->hw->bgx_cnt; i++)
  386. nic_reg_write(nic, NIC_PF_INTFX_SEND_CFG | (i << 3),
  387. (1ULL << 16));
  388. }
  389. nic_reg_write(nic, NIC_PF_INTF_0_1_BP_CFG,
  390. (1ULL << 63) | BGX0_BLOCK);
  391. nic_reg_write(nic, NIC_PF_INTF_0_1_BP_CFG + (1 << 8),
  392. (1ULL << 63) | BGX1_BLOCK);
  393. /* PKIND configuration */
  394. nic->pkind.minlen = 0;
  395. nic->pkind.maxlen = NIC_HW_MAX_FRS + VLAN_ETH_HLEN + ETH_FCS_LEN + 4;
  396. nic->pkind.lenerr_en = 1;
  397. nic->pkind.rx_hdr = 0;
  398. nic->pkind.hdr_sl = 0;
  399. for (i = 0; i < NIC_MAX_PKIND; i++)
  400. nic_reg_write(nic, NIC_PF_PKIND_0_15_CFG | (i << 3),
  401. *(u64 *)&nic->pkind);
  402. nic_set_tx_pkt_pad(nic, NIC_HW_MIN_FRS);
  403. /* Timer config */
  404. nic_reg_write(nic, NIC_PF_INTR_TIMER_CFG, NICPF_CLK_PER_INT_TICK);
  405. /* Enable VLAN ethertype matching and stripping */
  406. nic_reg_write(nic, NIC_PF_RX_ETYPE_0_7,
  407. (2 << 19) | (ETYPE_ALG_VLAN_STRIP << 16) | ETH_P_8021Q);
  408. /* Check if HW expected value is higher (could be in future chips) */
  409. cqm_cfg = nic_reg_read(nic, NIC_PF_CQM_CFG);
  410. if (cqm_cfg < NICPF_CQM_MIN_DROP_LEVEL)
  411. nic_reg_write(nic, NIC_PF_CQM_CFG, NICPF_CQM_MIN_DROP_LEVEL);
  412. }
  413. /* Channel parse index configuration */
  414. static void nic_config_cpi(struct nicpf *nic, struct cpi_cfg_msg *cfg)
  415. {
  416. struct hw_info *hw = nic->hw;
  417. u32 vnic, bgx, lmac, chan;
  418. u32 padd, cpi_count = 0;
  419. u64 cpi_base, cpi, rssi_base, rssi;
  420. u8 qset, rq_idx = 0;
  421. vnic = cfg->vf_id;
  422. bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vnic]);
  423. lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vnic]);
  424. chan = (lmac * hw->chans_per_lmac) + (bgx * hw->chans_per_bgx);
  425. cpi_base = vnic * NIC_MAX_CPI_PER_LMAC;
  426. rssi_base = vnic * hw->rss_ind_tbl_size;
  427. /* Rx channel configuration */
  428. nic_reg_write(nic, NIC_PF_CHAN_0_255_RX_BP_CFG | (chan << 3),
  429. (1ull << 63) | (vnic << 0));
  430. nic_reg_write(nic, NIC_PF_CHAN_0_255_RX_CFG | (chan << 3),
  431. ((u64)cfg->cpi_alg << 62) | (cpi_base << 48));
  432. if (cfg->cpi_alg == CPI_ALG_NONE)
  433. cpi_count = 1;
  434. else if (cfg->cpi_alg == CPI_ALG_VLAN) /* 3 bits of PCP */
  435. cpi_count = 8;
  436. else if (cfg->cpi_alg == CPI_ALG_VLAN16) /* 3 bits PCP + DEI */
  437. cpi_count = 16;
  438. else if (cfg->cpi_alg == CPI_ALG_DIFF) /* 6bits DSCP */
  439. cpi_count = NIC_MAX_CPI_PER_LMAC;
  440. /* RSS Qset, Qidx mapping */
  441. qset = cfg->vf_id;
  442. rssi = rssi_base;
  443. for (; rssi < (rssi_base + cfg->rq_cnt); rssi++) {
  444. nic_reg_write(nic, NIC_PF_RSSI_0_4097_RQ | (rssi << 3),
  445. (qset << 3) | rq_idx);
  446. rq_idx++;
  447. }
  448. rssi = 0;
  449. cpi = cpi_base;
  450. for (; cpi < (cpi_base + cpi_count); cpi++) {
  451. /* Determine port to channel adder */
  452. if (cfg->cpi_alg != CPI_ALG_DIFF)
  453. padd = cpi % cpi_count;
  454. else
  455. padd = cpi % 8; /* 3 bits CS out of 6bits DSCP */
  456. /* Leave RSS_SIZE as '0' to disable RSS */
  457. if (pass1_silicon(nic->pdev)) {
  458. nic_reg_write(nic, NIC_PF_CPI_0_2047_CFG | (cpi << 3),
  459. (vnic << 24) | (padd << 16) |
  460. (rssi_base + rssi));
  461. } else {
  462. /* Set MPI_ALG to '0' to disable MCAM parsing */
  463. nic_reg_write(nic, NIC_PF_CPI_0_2047_CFG | (cpi << 3),
  464. (padd << 16));
  465. /* MPI index is same as CPI if MPI_ALG is not enabled */
  466. nic_reg_write(nic, NIC_PF_MPI_0_2047_CFG | (cpi << 3),
  467. (vnic << 24) | (rssi_base + rssi));
  468. }
  469. if ((rssi + 1) >= cfg->rq_cnt)
  470. continue;
  471. if (cfg->cpi_alg == CPI_ALG_VLAN)
  472. rssi++;
  473. else if (cfg->cpi_alg == CPI_ALG_VLAN16)
  474. rssi = ((cpi - cpi_base) & 0xe) >> 1;
  475. else if (cfg->cpi_alg == CPI_ALG_DIFF)
  476. rssi = ((cpi - cpi_base) & 0x38) >> 3;
  477. }
  478. nic->cpi_base[cfg->vf_id] = cpi_base;
  479. nic->rssi_base[cfg->vf_id] = rssi_base;
  480. }
  481. /* Responsds to VF with its RSS indirection table size */
  482. static void nic_send_rss_size(struct nicpf *nic, int vf)
  483. {
  484. union nic_mbx mbx = {};
  485. mbx.rss_size.msg = NIC_MBOX_MSG_RSS_SIZE;
  486. mbx.rss_size.ind_tbl_size = nic->hw->rss_ind_tbl_size;
  487. nic_send_msg_to_vf(nic, vf, &mbx);
  488. }
  489. /* Receive side scaling configuration
  490. * configure:
  491. * - RSS index
  492. * - indir table i.e hash::RQ mapping
  493. * - no of hash bits to consider
  494. */
  495. static void nic_config_rss(struct nicpf *nic, struct rss_cfg_msg *cfg)
  496. {
  497. u8 qset, idx = 0;
  498. u64 cpi_cfg, cpi_base, rssi_base, rssi;
  499. u64 idx_addr;
  500. rssi_base = nic->rssi_base[cfg->vf_id] + cfg->tbl_offset;
  501. rssi = rssi_base;
  502. for (; rssi < (rssi_base + cfg->tbl_len); rssi++) {
  503. u8 svf = cfg->ind_tbl[idx] >> 3;
  504. if (svf)
  505. qset = nic->vf_sqs[cfg->vf_id][svf - 1];
  506. else
  507. qset = cfg->vf_id;
  508. nic_reg_write(nic, NIC_PF_RSSI_0_4097_RQ | (rssi << 3),
  509. (qset << 3) | (cfg->ind_tbl[idx] & 0x7));
  510. idx++;
  511. }
  512. cpi_base = nic->cpi_base[cfg->vf_id];
  513. if (pass1_silicon(nic->pdev))
  514. idx_addr = NIC_PF_CPI_0_2047_CFG;
  515. else
  516. idx_addr = NIC_PF_MPI_0_2047_CFG;
  517. cpi_cfg = nic_reg_read(nic, idx_addr | (cpi_base << 3));
  518. cpi_cfg &= ~(0xFULL << 20);
  519. cpi_cfg |= (cfg->hash_bits << 20);
  520. nic_reg_write(nic, idx_addr | (cpi_base << 3), cpi_cfg);
  521. }
  522. /* 4 level transmit side scheduler configutation
  523. * for TNS bypass mode
  524. *
  525. * Sample configuration for SQ0 on 88xx
  526. * VNIC0-SQ0 -> TL4(0) -> TL3[0] -> TL2[0] -> TL1[0] -> BGX0
  527. * VNIC1-SQ0 -> TL4(8) -> TL3[2] -> TL2[0] -> TL1[0] -> BGX0
  528. * VNIC2-SQ0 -> TL4(16) -> TL3[4] -> TL2[1] -> TL1[0] -> BGX0
  529. * VNIC3-SQ0 -> TL4(24) -> TL3[6] -> TL2[1] -> TL1[0] -> BGX0
  530. * VNIC4-SQ0 -> TL4(512) -> TL3[128] -> TL2[32] -> TL1[1] -> BGX1
  531. * VNIC5-SQ0 -> TL4(520) -> TL3[130] -> TL2[32] -> TL1[1] -> BGX1
  532. * VNIC6-SQ0 -> TL4(528) -> TL3[132] -> TL2[33] -> TL1[1] -> BGX1
  533. * VNIC7-SQ0 -> TL4(536) -> TL3[134] -> TL2[33] -> TL1[1] -> BGX1
  534. */
  535. static void nic_tx_channel_cfg(struct nicpf *nic, u8 vnic,
  536. struct sq_cfg_msg *sq)
  537. {
  538. struct hw_info *hw = nic->hw;
  539. u32 bgx, lmac, chan;
  540. u32 tl2, tl3, tl4;
  541. u32 rr_quantum;
  542. u8 sq_idx = sq->sq_num;
  543. u8 pqs_vnic;
  544. int svf;
  545. if (sq->sqs_mode)
  546. pqs_vnic = nic->pqs_vf[vnic];
  547. else
  548. pqs_vnic = vnic;
  549. bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[pqs_vnic]);
  550. lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[pqs_vnic]);
  551. /* 24 bytes for FCS, IPG and preamble */
  552. rr_quantum = ((NIC_HW_MAX_FRS + 24) / 4);
  553. /* For 88xx 0-511 TL4 transmits via BGX0 and
  554. * 512-1023 TL4s transmit via BGX1.
  555. */
  556. if (hw->tl1_per_bgx) {
  557. tl4 = bgx * (hw->tl4_cnt / hw->bgx_cnt);
  558. if (!sq->sqs_mode) {
  559. tl4 += (lmac * MAX_QUEUES_PER_QSET);
  560. } else {
  561. for (svf = 0; svf < MAX_SQS_PER_VF; svf++) {
  562. if (nic->vf_sqs[pqs_vnic][svf] == vnic)
  563. break;
  564. }
  565. tl4 += (MAX_LMAC_PER_BGX * MAX_QUEUES_PER_QSET);
  566. tl4 += (lmac * MAX_QUEUES_PER_QSET * MAX_SQS_PER_VF);
  567. tl4 += (svf * MAX_QUEUES_PER_QSET);
  568. }
  569. } else {
  570. tl4 = (vnic * MAX_QUEUES_PER_QSET);
  571. }
  572. tl4 += sq_idx;
  573. tl3 = tl4 / (hw->tl4_cnt / hw->tl3_cnt);
  574. nic_reg_write(nic, NIC_PF_QSET_0_127_SQ_0_7_CFG2 |
  575. ((u64)vnic << NIC_QS_ID_SHIFT) |
  576. ((u32)sq_idx << NIC_Q_NUM_SHIFT), tl4);
  577. nic_reg_write(nic, NIC_PF_TL4_0_1023_CFG | (tl4 << 3),
  578. ((u64)vnic << 27) | ((u32)sq_idx << 24) | rr_quantum);
  579. nic_reg_write(nic, NIC_PF_TL3_0_255_CFG | (tl3 << 3), rr_quantum);
  580. /* On 88xx 0-127 channels are for BGX0 and
  581. * 127-255 channels for BGX1.
  582. *
  583. * On 81xx/83xx TL3_CHAN reg should be configured with channel
  584. * within LMAC i.e 0-7 and not the actual channel number like on 88xx
  585. */
  586. chan = (lmac * hw->chans_per_lmac) + (bgx * hw->chans_per_bgx);
  587. if (hw->tl1_per_bgx)
  588. nic_reg_write(nic, NIC_PF_TL3_0_255_CHAN | (tl3 << 3), chan);
  589. else
  590. nic_reg_write(nic, NIC_PF_TL3_0_255_CHAN | (tl3 << 3), 0);
  591. /* Enable backpressure on the channel */
  592. nic_reg_write(nic, NIC_PF_CHAN_0_255_TX_CFG | (chan << 3), 1);
  593. tl2 = tl3 >> 2;
  594. nic_reg_write(nic, NIC_PF_TL3A_0_63_CFG | (tl2 << 3), tl2);
  595. nic_reg_write(nic, NIC_PF_TL2_0_63_CFG | (tl2 << 3), rr_quantum);
  596. /* No priorities as of now */
  597. nic_reg_write(nic, NIC_PF_TL2_0_63_PRI | (tl2 << 3), 0x00);
  598. /* Unlike 88xx where TL2s 0-31 transmits to TL1 '0' and rest to TL1 '1'
  599. * on 81xx/83xx TL2 needs to be configured to transmit to one of the
  600. * possible LMACs.
  601. *
  602. * This register doesn't exist on 88xx.
  603. */
  604. if (!hw->tl1_per_bgx)
  605. nic_reg_write(nic, NIC_PF_TL2_LMAC | (tl2 << 3),
  606. lmac + (bgx * MAX_LMAC_PER_BGX));
  607. }
  608. /* Send primary nicvf pointer to secondary QS's VF */
  609. static void nic_send_pnicvf(struct nicpf *nic, int sqs)
  610. {
  611. union nic_mbx mbx = {};
  612. mbx.nicvf.msg = NIC_MBOX_MSG_PNICVF_PTR;
  613. mbx.nicvf.nicvf = nic->nicvf[nic->pqs_vf[sqs]];
  614. nic_send_msg_to_vf(nic, sqs, &mbx);
  615. }
  616. /* Send SQS's nicvf pointer to primary QS's VF */
  617. static void nic_send_snicvf(struct nicpf *nic, struct nicvf_ptr *nicvf)
  618. {
  619. union nic_mbx mbx = {};
  620. int sqs_id = nic->vf_sqs[nicvf->vf_id][nicvf->sqs_id];
  621. mbx.nicvf.msg = NIC_MBOX_MSG_SNICVF_PTR;
  622. mbx.nicvf.sqs_id = nicvf->sqs_id;
  623. mbx.nicvf.nicvf = nic->nicvf[sqs_id];
  624. nic_send_msg_to_vf(nic, nicvf->vf_id, &mbx);
  625. }
  626. /* Find next available Qset that can be assigned as a
  627. * secondary Qset to a VF.
  628. */
  629. static int nic_nxt_avail_sqs(struct nicpf *nic)
  630. {
  631. int sqs;
  632. for (sqs = 0; sqs < nic->num_sqs_en; sqs++) {
  633. if (!nic->sqs_used[sqs])
  634. nic->sqs_used[sqs] = true;
  635. else
  636. continue;
  637. return sqs + nic->num_vf_en;
  638. }
  639. return -1;
  640. }
  641. /* Allocate additional Qsets for requested VF */
  642. static void nic_alloc_sqs(struct nicpf *nic, struct sqs_alloc *sqs)
  643. {
  644. union nic_mbx mbx = {};
  645. int idx, alloc_qs = 0;
  646. int sqs_id;
  647. if (!nic->num_sqs_en)
  648. goto send_mbox;
  649. for (idx = 0; idx < sqs->qs_count; idx++) {
  650. sqs_id = nic_nxt_avail_sqs(nic);
  651. if (sqs_id < 0)
  652. break;
  653. nic->vf_sqs[sqs->vf_id][idx] = sqs_id;
  654. nic->pqs_vf[sqs_id] = sqs->vf_id;
  655. alloc_qs++;
  656. }
  657. send_mbox:
  658. mbx.sqs_alloc.msg = NIC_MBOX_MSG_ALLOC_SQS;
  659. mbx.sqs_alloc.vf_id = sqs->vf_id;
  660. mbx.sqs_alloc.qs_count = alloc_qs;
  661. nic_send_msg_to_vf(nic, sqs->vf_id, &mbx);
  662. }
  663. static int nic_config_loopback(struct nicpf *nic, struct set_loopback *lbk)
  664. {
  665. int bgx_idx, lmac_idx;
  666. if (lbk->vf_id >= nic->num_vf_en)
  667. return -1;
  668. bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lbk->vf_id]);
  669. lmac_idx = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lbk->vf_id]);
  670. bgx_lmac_internal_loopback(nic->node, bgx_idx, lmac_idx, lbk->enable);
  671. /* Enable moving average calculation.
  672. * Keep the LVL/AVG delay to HW enforced minimum so that, not too many
  673. * packets sneek in between average calculations.
  674. */
  675. nic_reg_write(nic, NIC_PF_CQ_AVG_CFG,
  676. (BIT_ULL(20) | 0x2ull << 14 | 0x1));
  677. nic_reg_write(nic, NIC_PF_RRM_AVG_CFG,
  678. (BIT_ULL(20) | 0x3ull << 14 | 0x1));
  679. return 0;
  680. }
  681. /* Reset statistics counters */
  682. static int nic_reset_stat_counters(struct nicpf *nic,
  683. int vf, struct reset_stat_cfg *cfg)
  684. {
  685. int i, stat, qnum;
  686. u64 reg_addr;
  687. for (i = 0; i < RX_STATS_ENUM_LAST; i++) {
  688. if (cfg->rx_stat_mask & BIT(i)) {
  689. reg_addr = NIC_PF_VNIC_0_127_RX_STAT_0_13 |
  690. (vf << NIC_QS_ID_SHIFT) |
  691. (i << 3);
  692. nic_reg_write(nic, reg_addr, 0);
  693. }
  694. }
  695. for (i = 0; i < TX_STATS_ENUM_LAST; i++) {
  696. if (cfg->tx_stat_mask & BIT(i)) {
  697. reg_addr = NIC_PF_VNIC_0_127_TX_STAT_0_4 |
  698. (vf << NIC_QS_ID_SHIFT) |
  699. (i << 3);
  700. nic_reg_write(nic, reg_addr, 0);
  701. }
  702. }
  703. for (i = 0; i <= 15; i++) {
  704. qnum = i >> 1;
  705. stat = i & 1 ? 1 : 0;
  706. reg_addr = (vf << NIC_QS_ID_SHIFT) |
  707. (qnum << NIC_Q_NUM_SHIFT) | (stat << 3);
  708. if (cfg->rq_stat_mask & BIT(i)) {
  709. reg_addr |= NIC_PF_QSET_0_127_RQ_0_7_STAT_0_1;
  710. nic_reg_write(nic, reg_addr, 0);
  711. }
  712. if (cfg->sq_stat_mask & BIT(i)) {
  713. reg_addr |= NIC_PF_QSET_0_127_SQ_0_7_STAT_0_1;
  714. nic_reg_write(nic, reg_addr, 0);
  715. }
  716. }
  717. return 0;
  718. }
  719. static void nic_enable_tunnel_parsing(struct nicpf *nic, int vf)
  720. {
  721. u64 prot_def = (IPV6_PROT << 32) | (IPV4_PROT << 16) | ET_PROT;
  722. u64 vxlan_prot_def = (IPV6_PROT_DEF << 32) |
  723. (IPV4_PROT_DEF) << 16 | ET_PROT_DEF;
  724. /* Configure tunnel parsing parameters */
  725. nic_reg_write(nic, NIC_PF_RX_GENEVE_DEF,
  726. (1ULL << 63 | UDP_GENEVE_PORT_NUM));
  727. nic_reg_write(nic, NIC_PF_RX_GENEVE_PROT_DEF,
  728. ((7ULL << 61) | prot_def));
  729. nic_reg_write(nic, NIC_PF_RX_NVGRE_PROT_DEF,
  730. ((7ULL << 61) | prot_def));
  731. nic_reg_write(nic, NIC_PF_RX_VXLAN_DEF_0_1,
  732. ((1ULL << 63) | UDP_VXLAN_PORT_NUM));
  733. nic_reg_write(nic, NIC_PF_RX_VXLAN_PROT_DEF,
  734. ((0xfULL << 60) | vxlan_prot_def));
  735. }
  736. static void nic_enable_vf(struct nicpf *nic, int vf, bool enable)
  737. {
  738. int bgx, lmac;
  739. nic->vf_enabled[vf] = enable;
  740. if (vf >= nic->num_vf_en)
  741. return;
  742. bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  743. lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  744. bgx_lmac_rx_tx_enable(nic->node, bgx, lmac, enable);
  745. }
  746. static void nic_pause_frame(struct nicpf *nic, int vf, struct pfc *cfg)
  747. {
  748. int bgx, lmac;
  749. struct pfc pfc;
  750. union nic_mbx mbx = {};
  751. if (vf >= nic->num_vf_en)
  752. return;
  753. bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  754. lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  755. if (cfg->get) {
  756. bgx_lmac_get_pfc(nic->node, bgx, lmac, &pfc);
  757. mbx.pfc.msg = NIC_MBOX_MSG_PFC;
  758. mbx.pfc.autoneg = pfc.autoneg;
  759. mbx.pfc.fc_rx = pfc.fc_rx;
  760. mbx.pfc.fc_tx = pfc.fc_tx;
  761. nic_send_msg_to_vf(nic, vf, &mbx);
  762. } else {
  763. bgx_lmac_set_pfc(nic->node, bgx, lmac, cfg);
  764. nic_mbx_send_ack(nic, vf);
  765. }
  766. }
  767. /* Enable or disable HW timestamping by BGX for pkts received on a LMAC */
  768. static void nic_config_timestamp(struct nicpf *nic, int vf, struct set_ptp *ptp)
  769. {
  770. struct pkind_cfg *pkind;
  771. u8 lmac, bgx_idx;
  772. u64 pkind_val, pkind_idx;
  773. if (vf >= nic->num_vf_en)
  774. return;
  775. bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  776. lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  777. pkind_idx = lmac + bgx_idx * MAX_LMAC_PER_BGX;
  778. pkind_val = nic_reg_read(nic, NIC_PF_PKIND_0_15_CFG | (pkind_idx << 3));
  779. pkind = (struct pkind_cfg *)&pkind_val;
  780. if (ptp->enable && !pkind->hdr_sl) {
  781. /* Skiplen to exclude 8byte timestamp while parsing pkt
  782. * If not configured, will result in L2 errors.
  783. */
  784. pkind->hdr_sl = 4;
  785. /* Adjust max packet length allowed */
  786. pkind->maxlen += (pkind->hdr_sl * 2);
  787. bgx_config_timestamping(nic->node, bgx_idx, lmac, true);
  788. nic_reg_write(nic, NIC_PF_RX_ETYPE_0_7 | (1 << 3),
  789. (ETYPE_ALG_ENDPARSE << 16) | ETH_P_1588);
  790. } else if (!ptp->enable && pkind->hdr_sl) {
  791. pkind->maxlen -= (pkind->hdr_sl * 2);
  792. pkind->hdr_sl = 0;
  793. bgx_config_timestamping(nic->node, bgx_idx, lmac, false);
  794. nic_reg_write(nic, NIC_PF_RX_ETYPE_0_7 | (1 << 3),
  795. (ETYPE_ALG_SKIP << 16) | ETH_P_8021Q);
  796. }
  797. nic_reg_write(nic, NIC_PF_PKIND_0_15_CFG | (pkind_idx << 3), pkind_val);
  798. }
  799. /* Interrupt handler to handle mailbox messages from VFs */
  800. static void nic_handle_mbx_intr(struct nicpf *nic, int vf)
  801. {
  802. union nic_mbx mbx = {};
  803. u64 *mbx_data;
  804. u64 mbx_addr;
  805. u64 reg_addr;
  806. u64 cfg;
  807. int bgx, lmac;
  808. int i;
  809. int ret = 0;
  810. nic->mbx_lock[vf] = true;
  811. mbx_addr = nic_get_mbx_addr(vf);
  812. mbx_data = (u64 *)&mbx;
  813. for (i = 0; i < NIC_PF_VF_MAILBOX_SIZE; i++) {
  814. *mbx_data = nic_reg_read(nic, mbx_addr);
  815. mbx_data++;
  816. mbx_addr += sizeof(u64);
  817. }
  818. dev_dbg(&nic->pdev->dev, "%s: Mailbox msg 0x%02x from VF%d\n",
  819. __func__, mbx.msg.msg, vf);
  820. switch (mbx.msg.msg) {
  821. case NIC_MBOX_MSG_READY:
  822. nic_mbx_send_ready(nic, vf);
  823. if (vf < nic->num_vf_en) {
  824. nic->link[vf] = 0;
  825. nic->duplex[vf] = 0;
  826. nic->speed[vf] = 0;
  827. }
  828. goto unlock;
  829. case NIC_MBOX_MSG_QS_CFG:
  830. reg_addr = NIC_PF_QSET_0_127_CFG |
  831. (mbx.qs.num << NIC_QS_ID_SHIFT);
  832. cfg = mbx.qs.cfg;
  833. /* Check if its a secondary Qset */
  834. if (vf >= nic->num_vf_en) {
  835. cfg = cfg & (~0x7FULL);
  836. /* Assign this Qset to primary Qset's VF */
  837. cfg |= nic->pqs_vf[vf];
  838. }
  839. nic_reg_write(nic, reg_addr, cfg);
  840. break;
  841. case NIC_MBOX_MSG_RQ_CFG:
  842. reg_addr = NIC_PF_QSET_0_127_RQ_0_7_CFG |
  843. (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
  844. (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
  845. nic_reg_write(nic, reg_addr, mbx.rq.cfg);
  846. /* Enable CQE_RX2_S extension in CQE_RX descriptor.
  847. * This gets appended by default on 81xx/83xx chips,
  848. * for consistency enabling the same on 88xx pass2
  849. * where this is introduced.
  850. */
  851. if (pass2_silicon(nic->pdev))
  852. nic_reg_write(nic, NIC_PF_RX_CFG, 0x01);
  853. if (!pass1_silicon(nic->pdev))
  854. nic_enable_tunnel_parsing(nic, vf);
  855. break;
  856. case NIC_MBOX_MSG_RQ_BP_CFG:
  857. reg_addr = NIC_PF_QSET_0_127_RQ_0_7_BP_CFG |
  858. (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
  859. (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
  860. nic_reg_write(nic, reg_addr, mbx.rq.cfg);
  861. break;
  862. case NIC_MBOX_MSG_RQ_SW_SYNC:
  863. ret = nic_rcv_queue_sw_sync(nic);
  864. break;
  865. case NIC_MBOX_MSG_RQ_DROP_CFG:
  866. reg_addr = NIC_PF_QSET_0_127_RQ_0_7_DROP_CFG |
  867. (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
  868. (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
  869. nic_reg_write(nic, reg_addr, mbx.rq.cfg);
  870. break;
  871. case NIC_MBOX_MSG_SQ_CFG:
  872. reg_addr = NIC_PF_QSET_0_127_SQ_0_7_CFG |
  873. (mbx.sq.qs_num << NIC_QS_ID_SHIFT) |
  874. (mbx.sq.sq_num << NIC_Q_NUM_SHIFT);
  875. nic_reg_write(nic, reg_addr, mbx.sq.cfg);
  876. nic_tx_channel_cfg(nic, mbx.qs.num, &mbx.sq);
  877. break;
  878. case NIC_MBOX_MSG_SET_MAC:
  879. if (vf >= nic->num_vf_en) {
  880. ret = -1; /* NACK */
  881. break;
  882. }
  883. lmac = mbx.mac.vf_id;
  884. bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lmac]);
  885. lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lmac]);
  886. bgx_set_lmac_mac(nic->node, bgx, lmac, mbx.mac.mac_addr);
  887. break;
  888. case NIC_MBOX_MSG_SET_MAX_FRS:
  889. ret = nic_update_hw_frs(nic, mbx.frs.max_frs,
  890. mbx.frs.vf_id);
  891. break;
  892. case NIC_MBOX_MSG_CPI_CFG:
  893. nic_config_cpi(nic, &mbx.cpi_cfg);
  894. break;
  895. case NIC_MBOX_MSG_RSS_SIZE:
  896. nic_send_rss_size(nic, vf);
  897. goto unlock;
  898. case NIC_MBOX_MSG_RSS_CFG:
  899. case NIC_MBOX_MSG_RSS_CFG_CONT:
  900. nic_config_rss(nic, &mbx.rss_cfg);
  901. break;
  902. case NIC_MBOX_MSG_CFG_DONE:
  903. /* Last message of VF config msg sequence */
  904. nic_enable_vf(nic, vf, true);
  905. goto unlock;
  906. case NIC_MBOX_MSG_SHUTDOWN:
  907. /* First msg in VF teardown sequence */
  908. if (vf >= nic->num_vf_en)
  909. nic->sqs_used[vf - nic->num_vf_en] = false;
  910. nic->pqs_vf[vf] = 0;
  911. nic_enable_vf(nic, vf, false);
  912. break;
  913. case NIC_MBOX_MSG_ALLOC_SQS:
  914. nic_alloc_sqs(nic, &mbx.sqs_alloc);
  915. goto unlock;
  916. case NIC_MBOX_MSG_NICVF_PTR:
  917. nic->nicvf[vf] = mbx.nicvf.nicvf;
  918. break;
  919. case NIC_MBOX_MSG_PNICVF_PTR:
  920. nic_send_pnicvf(nic, vf);
  921. goto unlock;
  922. case NIC_MBOX_MSG_SNICVF_PTR:
  923. nic_send_snicvf(nic, &mbx.nicvf);
  924. goto unlock;
  925. case NIC_MBOX_MSG_BGX_STATS:
  926. nic_get_bgx_stats(nic, &mbx.bgx_stats);
  927. goto unlock;
  928. case NIC_MBOX_MSG_LOOPBACK:
  929. ret = nic_config_loopback(nic, &mbx.lbk);
  930. break;
  931. case NIC_MBOX_MSG_RESET_STAT_COUNTER:
  932. ret = nic_reset_stat_counters(nic, vf, &mbx.reset_stat);
  933. break;
  934. case NIC_MBOX_MSG_PFC:
  935. nic_pause_frame(nic, vf, &mbx.pfc);
  936. goto unlock;
  937. case NIC_MBOX_MSG_PTP_CFG:
  938. nic_config_timestamp(nic, vf, &mbx.ptp);
  939. break;
  940. case NIC_MBOX_MSG_RESET_XCAST:
  941. if (vf >= nic->num_vf_en) {
  942. ret = -1; /* NACK */
  943. break;
  944. }
  945. bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  946. lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  947. bgx_reset_xcast_mode(nic->node, bgx, lmac,
  948. vf < NIC_VF_PER_MBX_REG ? vf :
  949. vf - NIC_VF_PER_MBX_REG);
  950. break;
  951. case NIC_MBOX_MSG_ADD_MCAST:
  952. if (vf >= nic->num_vf_en) {
  953. ret = -1; /* NACK */
  954. break;
  955. }
  956. bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  957. lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  958. bgx_set_dmac_cam_filter(nic->node, bgx, lmac,
  959. mbx.xcast.data.mac,
  960. vf < NIC_VF_PER_MBX_REG ? vf :
  961. vf - NIC_VF_PER_MBX_REG);
  962. break;
  963. case NIC_MBOX_MSG_SET_XCAST:
  964. if (vf >= nic->num_vf_en) {
  965. ret = -1; /* NACK */
  966. break;
  967. }
  968. bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  969. lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  970. bgx_set_xcast_mode(nic->node, bgx, lmac, mbx.xcast.data.mode);
  971. break;
  972. default:
  973. dev_err(&nic->pdev->dev,
  974. "Invalid msg from VF%d, msg 0x%x\n", vf, mbx.msg.msg);
  975. break;
  976. }
  977. if (!ret) {
  978. nic_mbx_send_ack(nic, vf);
  979. } else if (mbx.msg.msg != NIC_MBOX_MSG_READY) {
  980. dev_err(&nic->pdev->dev, "NACK for MBOX 0x%02x from VF %d\n",
  981. mbx.msg.msg, vf);
  982. nic_mbx_send_nack(nic, vf);
  983. }
  984. unlock:
  985. nic->mbx_lock[vf] = false;
  986. }
  987. static irqreturn_t nic_mbx_intr_handler(int irq, void *nic_irq)
  988. {
  989. struct nicpf *nic = (struct nicpf *)nic_irq;
  990. int mbx;
  991. u64 intr;
  992. u8 vf;
  993. if (irq == pci_irq_vector(nic->pdev, NIC_PF_INTR_ID_MBOX0))
  994. mbx = 0;
  995. else
  996. mbx = 1;
  997. intr = nic_reg_read(nic, NIC_PF_MAILBOX_INT + (mbx << 3));
  998. dev_dbg(&nic->pdev->dev, "PF interrupt Mbox%d 0x%llx\n", mbx, intr);
  999. for (vf = 0; vf < NIC_VF_PER_MBX_REG; vf++) {
  1000. if (intr & (1ULL << vf)) {
  1001. dev_dbg(&nic->pdev->dev, "Intr from VF %d\n",
  1002. vf + (mbx * NIC_VF_PER_MBX_REG));
  1003. nic_handle_mbx_intr(nic, vf +
  1004. (mbx * NIC_VF_PER_MBX_REG));
  1005. nic_clear_mbx_intr(nic, vf, mbx);
  1006. }
  1007. }
  1008. return IRQ_HANDLED;
  1009. }
  1010. static void nic_free_all_interrupts(struct nicpf *nic)
  1011. {
  1012. int irq;
  1013. for (irq = 0; irq < nic->num_vec; irq++) {
  1014. if (nic->irq_allocated[irq])
  1015. free_irq(pci_irq_vector(nic->pdev, irq), nic);
  1016. nic->irq_allocated[irq] = false;
  1017. }
  1018. }
  1019. static int nic_register_interrupts(struct nicpf *nic)
  1020. {
  1021. int i, ret;
  1022. nic->num_vec = pci_msix_vec_count(nic->pdev);
  1023. /* Enable MSI-X */
  1024. ret = pci_alloc_irq_vectors(nic->pdev, nic->num_vec, nic->num_vec,
  1025. PCI_IRQ_MSIX);
  1026. if (ret < 0) {
  1027. dev_err(&nic->pdev->dev,
  1028. "Request for #%d msix vectors failed, returned %d\n",
  1029. nic->num_vec, ret);
  1030. return 1;
  1031. }
  1032. /* Register mailbox interrupt handler */
  1033. for (i = NIC_PF_INTR_ID_MBOX0; i < nic->num_vec; i++) {
  1034. sprintf(nic->irq_name[i],
  1035. "NICPF Mbox%d", (i - NIC_PF_INTR_ID_MBOX0));
  1036. ret = request_irq(pci_irq_vector(nic->pdev, i),
  1037. nic_mbx_intr_handler, 0,
  1038. nic->irq_name[i], nic);
  1039. if (ret)
  1040. goto fail;
  1041. nic->irq_allocated[i] = true;
  1042. }
  1043. /* Enable mailbox interrupt */
  1044. nic_enable_mbx_intr(nic);
  1045. return 0;
  1046. fail:
  1047. dev_err(&nic->pdev->dev, "Request irq failed\n");
  1048. nic_free_all_interrupts(nic);
  1049. pci_free_irq_vectors(nic->pdev);
  1050. nic->num_vec = 0;
  1051. return ret;
  1052. }
  1053. static void nic_unregister_interrupts(struct nicpf *nic)
  1054. {
  1055. nic_free_all_interrupts(nic);
  1056. pci_free_irq_vectors(nic->pdev);
  1057. nic->num_vec = 0;
  1058. }
  1059. static int nic_num_sqs_en(struct nicpf *nic, int vf_en)
  1060. {
  1061. int pos, sqs_per_vf = MAX_SQS_PER_VF_SINGLE_NODE;
  1062. u16 total_vf;
  1063. /* Secondary Qsets are needed only if CPU count is
  1064. * morethan MAX_QUEUES_PER_QSET.
  1065. */
  1066. if (num_online_cpus() <= MAX_QUEUES_PER_QSET)
  1067. return 0;
  1068. /* Check if its a multi-node environment */
  1069. if (nr_node_ids > 1)
  1070. sqs_per_vf = MAX_SQS_PER_VF;
  1071. pos = pci_find_ext_capability(nic->pdev, PCI_EXT_CAP_ID_SRIOV);
  1072. pci_read_config_word(nic->pdev, (pos + PCI_SRIOV_TOTAL_VF), &total_vf);
  1073. return min(total_vf - vf_en, vf_en * sqs_per_vf);
  1074. }
  1075. static int nic_sriov_init(struct pci_dev *pdev, struct nicpf *nic)
  1076. {
  1077. int pos = 0;
  1078. int vf_en;
  1079. int err;
  1080. u16 total_vf_cnt;
  1081. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
  1082. if (!pos) {
  1083. dev_err(&pdev->dev, "SRIOV capability is not found in PCIe config space\n");
  1084. return -ENODEV;
  1085. }
  1086. pci_read_config_word(pdev, (pos + PCI_SRIOV_TOTAL_VF), &total_vf_cnt);
  1087. if (total_vf_cnt < nic->num_vf_en)
  1088. nic->num_vf_en = total_vf_cnt;
  1089. if (!total_vf_cnt)
  1090. return 0;
  1091. vf_en = nic->num_vf_en;
  1092. nic->num_sqs_en = nic_num_sqs_en(nic, nic->num_vf_en);
  1093. vf_en += nic->num_sqs_en;
  1094. err = pci_enable_sriov(pdev, vf_en);
  1095. if (err) {
  1096. dev_err(&pdev->dev, "SRIOV enable failed, num VF is %d\n",
  1097. vf_en);
  1098. nic->num_vf_en = 0;
  1099. return err;
  1100. }
  1101. dev_info(&pdev->dev, "SRIOV enabled, number of VF available %d\n",
  1102. vf_en);
  1103. nic->flags |= NIC_SRIOV_ENABLED;
  1104. return 0;
  1105. }
  1106. /* Poll for BGX LMAC link status and update corresponding VF
  1107. * if there is a change, valid only if internal L2 switch
  1108. * is not present otherwise VF link is always treated as up
  1109. */
  1110. static void nic_poll_for_link(struct work_struct *work)
  1111. {
  1112. union nic_mbx mbx = {};
  1113. struct nicpf *nic;
  1114. struct bgx_link_status link;
  1115. u8 vf, bgx, lmac;
  1116. nic = container_of(work, struct nicpf, dwork.work);
  1117. mbx.link_status.msg = NIC_MBOX_MSG_BGX_LINK_CHANGE;
  1118. for (vf = 0; vf < nic->num_vf_en; vf++) {
  1119. /* Poll only if VF is UP */
  1120. if (!nic->vf_enabled[vf])
  1121. continue;
  1122. /* Get BGX, LMAC indices for the VF */
  1123. bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  1124. lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
  1125. /* Get interface link status */
  1126. bgx_get_lmac_link_state(nic->node, bgx, lmac, &link);
  1127. /* Inform VF only if link status changed */
  1128. if (nic->link[vf] == link.link_up)
  1129. continue;
  1130. if (!nic->mbx_lock[vf]) {
  1131. nic->link[vf] = link.link_up;
  1132. nic->duplex[vf] = link.duplex;
  1133. nic->speed[vf] = link.speed;
  1134. /* Send a mbox message to VF with current link status */
  1135. mbx.link_status.link_up = link.link_up;
  1136. mbx.link_status.duplex = link.duplex;
  1137. mbx.link_status.speed = link.speed;
  1138. mbx.link_status.mac_type = link.mac_type;
  1139. nic_send_msg_to_vf(nic, vf, &mbx);
  1140. }
  1141. }
  1142. queue_delayed_work(nic->check_link, &nic->dwork, HZ * 2);
  1143. }
  1144. static int nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1145. {
  1146. struct device *dev = &pdev->dev;
  1147. struct nicpf *nic;
  1148. u8 max_lmac;
  1149. int err;
  1150. BUILD_BUG_ON(sizeof(union nic_mbx) > 16);
  1151. nic = devm_kzalloc(dev, sizeof(*nic), GFP_KERNEL);
  1152. if (!nic)
  1153. return -ENOMEM;
  1154. nic->hw = devm_kzalloc(dev, sizeof(struct hw_info), GFP_KERNEL);
  1155. if (!nic->hw)
  1156. return -ENOMEM;
  1157. pci_set_drvdata(pdev, nic);
  1158. nic->pdev = pdev;
  1159. err = pci_enable_device(pdev);
  1160. if (err) {
  1161. dev_err(dev, "Failed to enable PCI device\n");
  1162. pci_set_drvdata(pdev, NULL);
  1163. return err;
  1164. }
  1165. err = pci_request_regions(pdev, DRV_NAME);
  1166. if (err) {
  1167. dev_err(dev, "PCI request regions failed 0x%x\n", err);
  1168. goto err_disable_device;
  1169. }
  1170. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(48));
  1171. if (err) {
  1172. dev_err(dev, "Unable to get usable DMA configuration\n");
  1173. goto err_release_regions;
  1174. }
  1175. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(48));
  1176. if (err) {
  1177. dev_err(dev, "Unable to get 48-bit DMA for consistent allocations\n");
  1178. goto err_release_regions;
  1179. }
  1180. /* MAP PF's configuration registers */
  1181. nic->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
  1182. if (!nic->reg_base) {
  1183. dev_err(dev, "Cannot map config register space, aborting\n");
  1184. err = -ENOMEM;
  1185. goto err_release_regions;
  1186. }
  1187. nic->node = nic_get_node_id(pdev);
  1188. /* Get HW capability info */
  1189. nic_get_hw_info(nic);
  1190. /* Allocate memory for LMAC tracking elements */
  1191. err = -ENOMEM;
  1192. max_lmac = nic->hw->bgx_cnt * MAX_LMAC_PER_BGX;
  1193. nic->vf_lmac_map = devm_kmalloc_array(dev, max_lmac, sizeof(u8),
  1194. GFP_KERNEL);
  1195. if (!nic->vf_lmac_map)
  1196. goto err_release_regions;
  1197. nic->link = devm_kmalloc_array(dev, max_lmac, sizeof(u8), GFP_KERNEL);
  1198. if (!nic->link)
  1199. goto err_release_regions;
  1200. nic->duplex = devm_kmalloc_array(dev, max_lmac, sizeof(u8), GFP_KERNEL);
  1201. if (!nic->duplex)
  1202. goto err_release_regions;
  1203. nic->speed = devm_kmalloc_array(dev, max_lmac, sizeof(u32), GFP_KERNEL);
  1204. if (!nic->speed)
  1205. goto err_release_regions;
  1206. /* Initialize hardware */
  1207. nic_init_hw(nic);
  1208. nic_set_lmac_vf_mapping(nic);
  1209. /* Register interrupts */
  1210. err = nic_register_interrupts(nic);
  1211. if (err)
  1212. goto err_release_regions;
  1213. /* Configure SRIOV */
  1214. err = nic_sriov_init(pdev, nic);
  1215. if (err)
  1216. goto err_unregister_interrupts;
  1217. /* Register a physical link status poll fn() */
  1218. nic->check_link = alloc_workqueue("check_link_status",
  1219. WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  1220. if (!nic->check_link) {
  1221. err = -ENOMEM;
  1222. goto err_disable_sriov;
  1223. }
  1224. INIT_DELAYED_WORK(&nic->dwork, nic_poll_for_link);
  1225. queue_delayed_work(nic->check_link, &nic->dwork, 0);
  1226. return 0;
  1227. err_disable_sriov:
  1228. if (nic->flags & NIC_SRIOV_ENABLED)
  1229. pci_disable_sriov(pdev);
  1230. err_unregister_interrupts:
  1231. nic_unregister_interrupts(nic);
  1232. err_release_regions:
  1233. pci_release_regions(pdev);
  1234. err_disable_device:
  1235. pci_disable_device(pdev);
  1236. pci_set_drvdata(pdev, NULL);
  1237. return err;
  1238. }
  1239. static void nic_remove(struct pci_dev *pdev)
  1240. {
  1241. struct nicpf *nic = pci_get_drvdata(pdev);
  1242. if (nic->flags & NIC_SRIOV_ENABLED)
  1243. pci_disable_sriov(pdev);
  1244. if (nic->check_link) {
  1245. /* Destroy work Queue */
  1246. cancel_delayed_work_sync(&nic->dwork);
  1247. destroy_workqueue(nic->check_link);
  1248. }
  1249. nic_unregister_interrupts(nic);
  1250. pci_release_regions(pdev);
  1251. pci_disable_device(pdev);
  1252. pci_set_drvdata(pdev, NULL);
  1253. }
  1254. static struct pci_driver nic_driver = {
  1255. .name = DRV_NAME,
  1256. .id_table = nic_id_table,
  1257. .probe = nic_probe,
  1258. .remove = nic_remove,
  1259. };
  1260. static int __init nic_init_module(void)
  1261. {
  1262. pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
  1263. return pci_register_driver(&nic_driver);
  1264. }
  1265. static void __exit nic_cleanup_module(void)
  1266. {
  1267. pci_unregister_driver(&nic_driver);
  1268. }
  1269. module_init(nic_init_module);
  1270. module_exit(nic_cleanup_module);