nic_main.c 37 KB

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