nic_main.c 38 KB

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