thunder_bgx.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  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/acpi.h>
  9. #include <linux/module.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/pci.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/phy.h>
  15. #include <linux/of.h>
  16. #include <linux/of_mdio.h>
  17. #include <linux/of_net.h>
  18. #include "nic_reg.h"
  19. #include "nic.h"
  20. #include "thunder_bgx.h"
  21. #define DRV_NAME "thunder_bgx"
  22. #define DRV_VERSION "1.0"
  23. /* RX_DMAC_CTL configuration */
  24. enum MCAST_MODE {
  25. MCAST_MODE_REJECT = 0x0,
  26. MCAST_MODE_ACCEPT = 0x1,
  27. MCAST_MODE_CAM_FILTER = 0x2,
  28. RSVD = 0x3
  29. };
  30. #define BCAST_ACCEPT BIT(0)
  31. #define CAM_ACCEPT BIT(3)
  32. #define MCAST_MODE_MASK 0x3
  33. #define BGX_MCAST_MODE(x) (x << 1)
  34. struct dmac_map {
  35. u64 vf_map;
  36. u64 dmac;
  37. };
  38. struct lmac {
  39. struct bgx *bgx;
  40. /* actual number of DMACs configured */
  41. u8 dmacs_cfg;
  42. /* overal number of possible DMACs could be configured per LMAC */
  43. u8 dmacs_count;
  44. struct dmac_map *dmacs; /* DMAC:VFs tracking filter array */
  45. u8 mac[ETH_ALEN];
  46. u8 lmac_type;
  47. u8 lane_to_sds;
  48. bool use_training;
  49. bool autoneg;
  50. bool link_up;
  51. int lmacid; /* ID within BGX */
  52. int lmacid_bd; /* ID on board */
  53. struct net_device netdev;
  54. struct phy_device *phydev;
  55. unsigned int last_duplex;
  56. unsigned int last_link;
  57. unsigned int last_speed;
  58. bool is_sgmii;
  59. struct delayed_work dwork;
  60. struct workqueue_struct *check_link;
  61. };
  62. struct bgx {
  63. u8 bgx_id;
  64. struct lmac lmac[MAX_LMAC_PER_BGX];
  65. u8 lmac_count;
  66. u8 max_lmac;
  67. u8 acpi_lmac_idx;
  68. void __iomem *reg_base;
  69. struct pci_dev *pdev;
  70. bool is_dlm;
  71. bool is_rgx;
  72. };
  73. static struct bgx *bgx_vnic[MAX_BGX_THUNDER];
  74. static int lmac_count; /* Total no of LMACs in system */
  75. static int bgx_xaui_check_link(struct lmac *lmac);
  76. /* Supported devices */
  77. static const struct pci_device_id bgx_id_table[] = {
  78. { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_BGX) },
  79. { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_RGX) },
  80. { 0, } /* end of table */
  81. };
  82. MODULE_AUTHOR("Cavium Inc");
  83. MODULE_DESCRIPTION("Cavium Thunder BGX/MAC Driver");
  84. MODULE_LICENSE("GPL v2");
  85. MODULE_VERSION(DRV_VERSION);
  86. MODULE_DEVICE_TABLE(pci, bgx_id_table);
  87. /* The Cavium ThunderX network controller can *only* be found in SoCs
  88. * containing the ThunderX ARM64 CPU implementation. All accesses to the device
  89. * registers on this platform are implicitly strongly ordered with respect
  90. * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
  91. * with no memory barriers in this driver. The readq()/writeq() functions add
  92. * explicit ordering operation which in this case are redundant, and only
  93. * add overhead.
  94. */
  95. /* Register read/write APIs */
  96. static u64 bgx_reg_read(struct bgx *bgx, u8 lmac, u64 offset)
  97. {
  98. void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset;
  99. return readq_relaxed(addr);
  100. }
  101. static void bgx_reg_write(struct bgx *bgx, u8 lmac, u64 offset, u64 val)
  102. {
  103. void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset;
  104. writeq_relaxed(val, addr);
  105. }
  106. static void bgx_reg_modify(struct bgx *bgx, u8 lmac, u64 offset, u64 val)
  107. {
  108. void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset;
  109. writeq_relaxed(val | readq_relaxed(addr), addr);
  110. }
  111. static int bgx_poll_reg(struct bgx *bgx, u8 lmac, u64 reg, u64 mask, bool zero)
  112. {
  113. int timeout = 100;
  114. u64 reg_val;
  115. while (timeout) {
  116. reg_val = bgx_reg_read(bgx, lmac, reg);
  117. if (zero && !(reg_val & mask))
  118. return 0;
  119. if (!zero && (reg_val & mask))
  120. return 0;
  121. usleep_range(1000, 2000);
  122. timeout--;
  123. }
  124. return 1;
  125. }
  126. static int max_bgx_per_node;
  127. static void set_max_bgx_per_node(struct pci_dev *pdev)
  128. {
  129. u16 sdevid;
  130. if (max_bgx_per_node)
  131. return;
  132. pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &sdevid);
  133. switch (sdevid) {
  134. case PCI_SUBSYS_DEVID_81XX_BGX:
  135. case PCI_SUBSYS_DEVID_81XX_RGX:
  136. max_bgx_per_node = MAX_BGX_PER_CN81XX;
  137. break;
  138. case PCI_SUBSYS_DEVID_83XX_BGX:
  139. max_bgx_per_node = MAX_BGX_PER_CN83XX;
  140. break;
  141. case PCI_SUBSYS_DEVID_88XX_BGX:
  142. default:
  143. max_bgx_per_node = MAX_BGX_PER_CN88XX;
  144. break;
  145. }
  146. }
  147. static struct bgx *get_bgx(int node, int bgx_idx)
  148. {
  149. int idx = (node * max_bgx_per_node) + bgx_idx;
  150. return bgx_vnic[idx];
  151. }
  152. /* Return number of BGX present in HW */
  153. unsigned bgx_get_map(int node)
  154. {
  155. int i;
  156. unsigned map = 0;
  157. for (i = 0; i < max_bgx_per_node; i++) {
  158. if (bgx_vnic[(node * max_bgx_per_node) + i])
  159. map |= (1 << i);
  160. }
  161. return map;
  162. }
  163. EXPORT_SYMBOL(bgx_get_map);
  164. /* Return number of LMAC configured for this BGX */
  165. int bgx_get_lmac_count(int node, int bgx_idx)
  166. {
  167. struct bgx *bgx;
  168. bgx = get_bgx(node, bgx_idx);
  169. if (bgx)
  170. return bgx->lmac_count;
  171. return 0;
  172. }
  173. EXPORT_SYMBOL(bgx_get_lmac_count);
  174. /* Returns the current link status of LMAC */
  175. void bgx_get_lmac_link_state(int node, int bgx_idx, int lmacid, void *status)
  176. {
  177. struct bgx_link_status *link = (struct bgx_link_status *)status;
  178. struct bgx *bgx;
  179. struct lmac *lmac;
  180. bgx = get_bgx(node, bgx_idx);
  181. if (!bgx)
  182. return;
  183. lmac = &bgx->lmac[lmacid];
  184. link->mac_type = lmac->lmac_type;
  185. link->link_up = lmac->link_up;
  186. link->duplex = lmac->last_duplex;
  187. link->speed = lmac->last_speed;
  188. }
  189. EXPORT_SYMBOL(bgx_get_lmac_link_state);
  190. const u8 *bgx_get_lmac_mac(int node, int bgx_idx, int lmacid)
  191. {
  192. struct bgx *bgx = get_bgx(node, bgx_idx);
  193. if (bgx)
  194. return bgx->lmac[lmacid].mac;
  195. return NULL;
  196. }
  197. EXPORT_SYMBOL(bgx_get_lmac_mac);
  198. void bgx_set_lmac_mac(int node, int bgx_idx, int lmacid, const u8 *mac)
  199. {
  200. struct bgx *bgx = get_bgx(node, bgx_idx);
  201. if (!bgx)
  202. return;
  203. ether_addr_copy(bgx->lmac[lmacid].mac, mac);
  204. }
  205. EXPORT_SYMBOL(bgx_set_lmac_mac);
  206. static void bgx_flush_dmac_cam_filter(struct bgx *bgx, int lmacid)
  207. {
  208. struct lmac *lmac = NULL;
  209. u8 idx = 0;
  210. lmac = &bgx->lmac[lmacid];
  211. /* reset CAM filters */
  212. for (idx = 0; idx < lmac->dmacs_count; idx++)
  213. bgx_reg_write(bgx, 0, BGX_CMR_RX_DMACX_CAM +
  214. ((lmacid * lmac->dmacs_count) + idx) *
  215. sizeof(u64), 0);
  216. }
  217. static void bgx_lmac_remove_filters(struct lmac *lmac, u8 vf_id)
  218. {
  219. int i = 0;
  220. if (!lmac)
  221. return;
  222. /* We've got reset filters request from some of attached VF, while the
  223. * others might want to keep their configuration. So in this case lets
  224. * iterate over all of configured filters and decrease number of
  225. * referencies. if some addresses get zero refs remove them from list
  226. */
  227. for (i = lmac->dmacs_cfg - 1; i >= 0; i--) {
  228. lmac->dmacs[i].vf_map &= ~BIT_ULL(vf_id);
  229. if (!lmac->dmacs[i].vf_map) {
  230. lmac->dmacs_cfg--;
  231. lmac->dmacs[i].dmac = 0;
  232. lmac->dmacs[i].vf_map = 0;
  233. }
  234. }
  235. }
  236. static int bgx_lmac_save_filter(struct lmac *lmac, u64 dmac, u8 vf_id)
  237. {
  238. u8 i = 0;
  239. if (!lmac)
  240. return -1;
  241. /* At the same time we could have several VFs 'attached' to some
  242. * particular LMAC, and each VF is represented as network interface
  243. * for kernel. So from user perspective it should be possible to
  244. * manipulate with its' (VF) receive modes. However from PF
  245. * driver perspective we need to keep track of filter configurations
  246. * for different VFs to prevent filter values dupes
  247. */
  248. for (i = 0; i < lmac->dmacs_cfg; i++) {
  249. if (lmac->dmacs[i].dmac == dmac) {
  250. lmac->dmacs[i].vf_map |= BIT_ULL(vf_id);
  251. return -1;
  252. }
  253. }
  254. if (!(lmac->dmacs_cfg < lmac->dmacs_count))
  255. return -1;
  256. /* keep it for further tracking */
  257. lmac->dmacs[lmac->dmacs_cfg].dmac = dmac;
  258. lmac->dmacs[lmac->dmacs_cfg].vf_map = BIT_ULL(vf_id);
  259. lmac->dmacs_cfg++;
  260. return 0;
  261. }
  262. static int bgx_set_dmac_cam_filter_mac(struct bgx *bgx, int lmacid,
  263. u64 cam_dmac, u8 idx)
  264. {
  265. struct lmac *lmac = NULL;
  266. u64 cfg = 0;
  267. /* skip zero addresses as meaningless */
  268. if (!cam_dmac || !bgx)
  269. return -1;
  270. lmac = &bgx->lmac[lmacid];
  271. /* configure DCAM filtering for designated LMAC */
  272. cfg = RX_DMACX_CAM_LMACID(lmacid & LMAC_ID_MASK) |
  273. RX_DMACX_CAM_EN | cam_dmac;
  274. bgx_reg_write(bgx, 0, BGX_CMR_RX_DMACX_CAM +
  275. ((lmacid * lmac->dmacs_count) + idx) * sizeof(u64), cfg);
  276. return 0;
  277. }
  278. void bgx_set_dmac_cam_filter(int node, int bgx_idx, int lmacid,
  279. u64 cam_dmac, u8 vf_id)
  280. {
  281. struct bgx *bgx = get_bgx(node, bgx_idx);
  282. struct lmac *lmac = NULL;
  283. if (!bgx)
  284. return;
  285. lmac = &bgx->lmac[lmacid];
  286. if (!cam_dmac)
  287. cam_dmac = ether_addr_to_u64(lmac->mac);
  288. /* since we might have several VFs attached to particular LMAC
  289. * and kernel could call mcast config for each of them with the
  290. * same MAC, check if requested MAC is already in filtering list and
  291. * updare/prepare list of MACs to be applied later to HW filters
  292. */
  293. bgx_lmac_save_filter(lmac, cam_dmac, vf_id);
  294. }
  295. EXPORT_SYMBOL(bgx_set_dmac_cam_filter);
  296. void bgx_set_xcast_mode(int node, int bgx_idx, int lmacid, u8 mode)
  297. {
  298. struct bgx *bgx = get_bgx(node, bgx_idx);
  299. struct lmac *lmac = NULL;
  300. u64 cfg = 0;
  301. u8 i = 0;
  302. if (!bgx)
  303. return;
  304. lmac = &bgx->lmac[lmacid];
  305. cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_RX_DMAC_CTL);
  306. if (mode & BGX_XCAST_BCAST_ACCEPT)
  307. cfg |= BCAST_ACCEPT;
  308. else
  309. cfg &= ~BCAST_ACCEPT;
  310. /* disable all MCASTs and DMAC filtering */
  311. cfg &= ~(CAM_ACCEPT | BGX_MCAST_MODE(MCAST_MODE_MASK));
  312. /* check requested bits and set filtergin mode appropriately */
  313. if (mode & (BGX_XCAST_MCAST_ACCEPT)) {
  314. cfg |= (BGX_MCAST_MODE(MCAST_MODE_ACCEPT));
  315. } else if (mode & BGX_XCAST_MCAST_FILTER) {
  316. cfg |= (BGX_MCAST_MODE(MCAST_MODE_CAM_FILTER) | CAM_ACCEPT);
  317. for (i = 0; i < lmac->dmacs_cfg; i++)
  318. bgx_set_dmac_cam_filter_mac(bgx, lmacid,
  319. lmac->dmacs[i].dmac, i);
  320. }
  321. bgx_reg_write(bgx, lmacid, BGX_CMRX_RX_DMAC_CTL, cfg);
  322. }
  323. EXPORT_SYMBOL(bgx_set_xcast_mode);
  324. void bgx_reset_xcast_mode(int node, int bgx_idx, int lmacid, u8 vf_id)
  325. {
  326. struct bgx *bgx = get_bgx(node, bgx_idx);
  327. if (!bgx)
  328. return;
  329. bgx_lmac_remove_filters(&bgx->lmac[lmacid], vf_id);
  330. bgx_flush_dmac_cam_filter(bgx, lmacid);
  331. bgx_set_xcast_mode(node, bgx_idx, lmacid,
  332. (BGX_XCAST_BCAST_ACCEPT | BGX_XCAST_MCAST_ACCEPT));
  333. }
  334. EXPORT_SYMBOL(bgx_reset_xcast_mode);
  335. void bgx_lmac_rx_tx_enable(int node, int bgx_idx, int lmacid, bool enable)
  336. {
  337. struct bgx *bgx = get_bgx(node, bgx_idx);
  338. struct lmac *lmac;
  339. u64 cfg;
  340. if (!bgx)
  341. return;
  342. lmac = &bgx->lmac[lmacid];
  343. cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
  344. if (enable)
  345. cfg |= CMR_PKT_RX_EN | CMR_PKT_TX_EN;
  346. else
  347. cfg &= ~(CMR_PKT_RX_EN | CMR_PKT_TX_EN);
  348. bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
  349. if (bgx->is_rgx)
  350. xcv_setup_link(enable ? lmac->link_up : 0, lmac->last_speed);
  351. }
  352. EXPORT_SYMBOL(bgx_lmac_rx_tx_enable);
  353. /* Enables or disables timestamp insertion by BGX for Rx packets */
  354. void bgx_config_timestamping(int node, int bgx_idx, int lmacid, bool enable)
  355. {
  356. struct bgx *bgx = get_bgx(node, bgx_idx);
  357. struct lmac *lmac;
  358. u64 csr_offset, cfg;
  359. if (!bgx)
  360. return;
  361. lmac = &bgx->lmac[lmacid];
  362. if (lmac->lmac_type == BGX_MODE_SGMII ||
  363. lmac->lmac_type == BGX_MODE_QSGMII ||
  364. lmac->lmac_type == BGX_MODE_RGMII)
  365. csr_offset = BGX_GMP_GMI_RXX_FRM_CTL;
  366. else
  367. csr_offset = BGX_SMUX_RX_FRM_CTL;
  368. cfg = bgx_reg_read(bgx, lmacid, csr_offset);
  369. if (enable)
  370. cfg |= BGX_PKT_RX_PTP_EN;
  371. else
  372. cfg &= ~BGX_PKT_RX_PTP_EN;
  373. bgx_reg_write(bgx, lmacid, csr_offset, cfg);
  374. }
  375. EXPORT_SYMBOL(bgx_config_timestamping);
  376. void bgx_lmac_get_pfc(int node, int bgx_idx, int lmacid, void *pause)
  377. {
  378. struct pfc *pfc = (struct pfc *)pause;
  379. struct bgx *bgx = get_bgx(node, bgx_idx);
  380. struct lmac *lmac;
  381. u64 cfg;
  382. if (!bgx)
  383. return;
  384. lmac = &bgx->lmac[lmacid];
  385. if (lmac->is_sgmii)
  386. return;
  387. cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_CBFC_CTL);
  388. pfc->fc_rx = cfg & RX_EN;
  389. pfc->fc_tx = cfg & TX_EN;
  390. pfc->autoneg = 0;
  391. }
  392. EXPORT_SYMBOL(bgx_lmac_get_pfc);
  393. void bgx_lmac_set_pfc(int node, int bgx_idx, int lmacid, void *pause)
  394. {
  395. struct pfc *pfc = (struct pfc *)pause;
  396. struct bgx *bgx = get_bgx(node, bgx_idx);
  397. struct lmac *lmac;
  398. u64 cfg;
  399. if (!bgx)
  400. return;
  401. lmac = &bgx->lmac[lmacid];
  402. if (lmac->is_sgmii)
  403. return;
  404. cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_CBFC_CTL);
  405. cfg &= ~(RX_EN | TX_EN);
  406. cfg |= (pfc->fc_rx ? RX_EN : 0x00);
  407. cfg |= (pfc->fc_tx ? TX_EN : 0x00);
  408. bgx_reg_write(bgx, lmacid, BGX_SMUX_CBFC_CTL, cfg);
  409. }
  410. EXPORT_SYMBOL(bgx_lmac_set_pfc);
  411. static void bgx_sgmii_change_link_state(struct lmac *lmac)
  412. {
  413. struct bgx *bgx = lmac->bgx;
  414. u64 cmr_cfg;
  415. u64 port_cfg = 0;
  416. u64 misc_ctl = 0;
  417. bool tx_en, rx_en;
  418. cmr_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_CMRX_CFG);
  419. tx_en = cmr_cfg & CMR_PKT_TX_EN;
  420. rx_en = cmr_cfg & CMR_PKT_RX_EN;
  421. cmr_cfg &= ~(CMR_PKT_RX_EN | CMR_PKT_TX_EN);
  422. bgx_reg_write(bgx, lmac->lmacid, BGX_CMRX_CFG, cmr_cfg);
  423. /* Wait for BGX RX to be idle */
  424. if (bgx_poll_reg(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG,
  425. GMI_PORT_CFG_RX_IDLE, false)) {
  426. dev_err(&bgx->pdev->dev, "BGX%d LMAC%d GMI RX not idle\n",
  427. bgx->bgx_id, lmac->lmacid);
  428. return;
  429. }
  430. /* Wait for BGX TX to be idle */
  431. if (bgx_poll_reg(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG,
  432. GMI_PORT_CFG_TX_IDLE, false)) {
  433. dev_err(&bgx->pdev->dev, "BGX%d LMAC%d GMI TX not idle\n",
  434. bgx->bgx_id, lmac->lmacid);
  435. return;
  436. }
  437. port_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG);
  438. misc_ctl = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_PCS_MISCX_CTL);
  439. if (lmac->link_up) {
  440. misc_ctl &= ~PCS_MISC_CTL_GMX_ENO;
  441. port_cfg &= ~GMI_PORT_CFG_DUPLEX;
  442. port_cfg |= (lmac->last_duplex << 2);
  443. } else {
  444. misc_ctl |= PCS_MISC_CTL_GMX_ENO;
  445. }
  446. switch (lmac->last_speed) {
  447. case 10:
  448. port_cfg &= ~GMI_PORT_CFG_SPEED; /* speed 0 */
  449. port_cfg |= GMI_PORT_CFG_SPEED_MSB; /* speed_msb 1 */
  450. port_cfg &= ~GMI_PORT_CFG_SLOT_TIME; /* slottime 0 */
  451. misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK;
  452. misc_ctl |= 50; /* samp_pt */
  453. bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 64);
  454. bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_BURST, 0);
  455. break;
  456. case 100:
  457. port_cfg &= ~GMI_PORT_CFG_SPEED; /* speed 0 */
  458. port_cfg &= ~GMI_PORT_CFG_SPEED_MSB; /* speed_msb 0 */
  459. port_cfg &= ~GMI_PORT_CFG_SLOT_TIME; /* slottime 0 */
  460. misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK;
  461. misc_ctl |= 5; /* samp_pt */
  462. bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 64);
  463. bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_BURST, 0);
  464. break;
  465. case 1000:
  466. port_cfg |= GMI_PORT_CFG_SPEED; /* speed 1 */
  467. port_cfg &= ~GMI_PORT_CFG_SPEED_MSB; /* speed_msb 0 */
  468. port_cfg |= GMI_PORT_CFG_SLOT_TIME; /* slottime 1 */
  469. misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK;
  470. misc_ctl |= 1; /* samp_pt */
  471. bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 512);
  472. if (lmac->last_duplex)
  473. bgx_reg_write(bgx, lmac->lmacid,
  474. BGX_GMP_GMI_TXX_BURST, 0);
  475. else
  476. bgx_reg_write(bgx, lmac->lmacid,
  477. BGX_GMP_GMI_TXX_BURST, 8192);
  478. break;
  479. default:
  480. break;
  481. }
  482. bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_PCS_MISCX_CTL, misc_ctl);
  483. bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG, port_cfg);
  484. /* Restore CMR config settings */
  485. cmr_cfg |= (rx_en ? CMR_PKT_RX_EN : 0) | (tx_en ? CMR_PKT_TX_EN : 0);
  486. bgx_reg_write(bgx, lmac->lmacid, BGX_CMRX_CFG, cmr_cfg);
  487. if (bgx->is_rgx && (cmr_cfg & (CMR_PKT_RX_EN | CMR_PKT_TX_EN)))
  488. xcv_setup_link(lmac->link_up, lmac->last_speed);
  489. }
  490. static void bgx_lmac_handler(struct net_device *netdev)
  491. {
  492. struct lmac *lmac = container_of(netdev, struct lmac, netdev);
  493. struct phy_device *phydev;
  494. int link_changed = 0;
  495. if (!lmac)
  496. return;
  497. phydev = lmac->phydev;
  498. if (!phydev->link && lmac->last_link)
  499. link_changed = -1;
  500. if (phydev->link &&
  501. (lmac->last_duplex != phydev->duplex ||
  502. lmac->last_link != phydev->link ||
  503. lmac->last_speed != phydev->speed)) {
  504. link_changed = 1;
  505. }
  506. lmac->last_link = phydev->link;
  507. lmac->last_speed = phydev->speed;
  508. lmac->last_duplex = phydev->duplex;
  509. if (!link_changed)
  510. return;
  511. if (link_changed > 0)
  512. lmac->link_up = true;
  513. else
  514. lmac->link_up = false;
  515. if (lmac->is_sgmii)
  516. bgx_sgmii_change_link_state(lmac);
  517. else
  518. bgx_xaui_check_link(lmac);
  519. }
  520. u64 bgx_get_rx_stats(int node, int bgx_idx, int lmac, int idx)
  521. {
  522. struct bgx *bgx;
  523. bgx = get_bgx(node, bgx_idx);
  524. if (!bgx)
  525. return 0;
  526. if (idx > 8)
  527. lmac = 0;
  528. return bgx_reg_read(bgx, lmac, BGX_CMRX_RX_STAT0 + (idx * 8));
  529. }
  530. EXPORT_SYMBOL(bgx_get_rx_stats);
  531. u64 bgx_get_tx_stats(int node, int bgx_idx, int lmac, int idx)
  532. {
  533. struct bgx *bgx;
  534. bgx = get_bgx(node, bgx_idx);
  535. if (!bgx)
  536. return 0;
  537. return bgx_reg_read(bgx, lmac, BGX_CMRX_TX_STAT0 + (idx * 8));
  538. }
  539. EXPORT_SYMBOL(bgx_get_tx_stats);
  540. /* Configure BGX LMAC in internal loopback mode */
  541. void bgx_lmac_internal_loopback(int node, int bgx_idx,
  542. int lmac_idx, bool enable)
  543. {
  544. struct bgx *bgx;
  545. struct lmac *lmac;
  546. u64 cfg;
  547. bgx = get_bgx(node, bgx_idx);
  548. if (!bgx)
  549. return;
  550. lmac = &bgx->lmac[lmac_idx];
  551. if (lmac->is_sgmii) {
  552. cfg = bgx_reg_read(bgx, lmac_idx, BGX_GMP_PCS_MRX_CTL);
  553. if (enable)
  554. cfg |= PCS_MRX_CTL_LOOPBACK1;
  555. else
  556. cfg &= ~PCS_MRX_CTL_LOOPBACK1;
  557. bgx_reg_write(bgx, lmac_idx, BGX_GMP_PCS_MRX_CTL, cfg);
  558. } else {
  559. cfg = bgx_reg_read(bgx, lmac_idx, BGX_SPUX_CONTROL1);
  560. if (enable)
  561. cfg |= SPU_CTL_LOOPBACK;
  562. else
  563. cfg &= ~SPU_CTL_LOOPBACK;
  564. bgx_reg_write(bgx, lmac_idx, BGX_SPUX_CONTROL1, cfg);
  565. }
  566. }
  567. EXPORT_SYMBOL(bgx_lmac_internal_loopback);
  568. static int bgx_lmac_sgmii_init(struct bgx *bgx, struct lmac *lmac)
  569. {
  570. int lmacid = lmac->lmacid;
  571. u64 cfg;
  572. bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_TXX_THRESH, 0x30);
  573. /* max packet size */
  574. bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_RXX_JABBER, MAX_FRAME_SIZE);
  575. /* Disable frame alignment if using preamble */
  576. cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND);
  577. if (cfg & 1)
  578. bgx_reg_write(bgx, lmacid, BGX_GMP_GMI_TXX_SGMII_CTL, 0);
  579. /* Enable lmac */
  580. bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN);
  581. /* PCS reset */
  582. bgx_reg_modify(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, PCS_MRX_CTL_RESET);
  583. if (bgx_poll_reg(bgx, lmacid, BGX_GMP_PCS_MRX_CTL,
  584. PCS_MRX_CTL_RESET, true)) {
  585. dev_err(&bgx->pdev->dev, "BGX PCS reset not completed\n");
  586. return -1;
  587. }
  588. /* power down, reset autoneg, autoneg enable */
  589. cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_PCS_MRX_CTL);
  590. cfg &= ~PCS_MRX_CTL_PWR_DN;
  591. cfg |= PCS_MRX_CTL_RST_AN;
  592. if (lmac->phydev) {
  593. cfg |= PCS_MRX_CTL_AN_EN;
  594. } else {
  595. /* In scenarios where PHY driver is not present or it's a
  596. * non-standard PHY, FW sets AN_EN to inform Linux driver
  597. * to do auto-neg and link polling or not.
  598. */
  599. if (cfg & PCS_MRX_CTL_AN_EN)
  600. lmac->autoneg = true;
  601. }
  602. bgx_reg_write(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, cfg);
  603. if (lmac->lmac_type == BGX_MODE_QSGMII) {
  604. /* Disable disparity check for QSGMII */
  605. cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_PCS_MISCX_CTL);
  606. cfg &= ~PCS_MISC_CTL_DISP_EN;
  607. bgx_reg_write(bgx, lmacid, BGX_GMP_PCS_MISCX_CTL, cfg);
  608. return 0;
  609. }
  610. if ((lmac->lmac_type == BGX_MODE_SGMII) && lmac->phydev) {
  611. if (bgx_poll_reg(bgx, lmacid, BGX_GMP_PCS_MRX_STATUS,
  612. PCS_MRX_STATUS_AN_CPT, false)) {
  613. dev_err(&bgx->pdev->dev, "BGX AN_CPT not completed\n");
  614. return -1;
  615. }
  616. }
  617. return 0;
  618. }
  619. static int bgx_lmac_xaui_init(struct bgx *bgx, struct lmac *lmac)
  620. {
  621. u64 cfg;
  622. int lmacid = lmac->lmacid;
  623. /* Reset SPU */
  624. bgx_reg_modify(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET);
  625. if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET, true)) {
  626. dev_err(&bgx->pdev->dev, "BGX SPU reset not completed\n");
  627. return -1;
  628. }
  629. /* Disable LMAC */
  630. cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
  631. cfg &= ~CMR_EN;
  632. bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
  633. bgx_reg_modify(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_LOW_POWER);
  634. /* Set interleaved running disparity for RXAUI */
  635. if (lmac->lmac_type == BGX_MODE_RXAUI)
  636. bgx_reg_modify(bgx, lmacid, BGX_SPUX_MISC_CONTROL,
  637. SPU_MISC_CTL_INTLV_RDISP);
  638. /* Clear receive packet disable */
  639. cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_MISC_CONTROL);
  640. cfg &= ~SPU_MISC_CTL_RX_DIS;
  641. bgx_reg_write(bgx, lmacid, BGX_SPUX_MISC_CONTROL, cfg);
  642. /* clear all interrupts */
  643. cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_RX_INT);
  644. bgx_reg_write(bgx, lmacid, BGX_SMUX_RX_INT, cfg);
  645. cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_INT);
  646. bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_INT, cfg);
  647. cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT);
  648. bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg);
  649. if (lmac->use_training) {
  650. bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LP_CUP, 0x00);
  651. bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LD_CUP, 0x00);
  652. bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LD_REP, 0x00);
  653. /* training enable */
  654. bgx_reg_modify(bgx, lmacid,
  655. BGX_SPUX_BR_PMD_CRTL, SPU_PMD_CRTL_TRAIN_EN);
  656. }
  657. /* Append FCS to each packet */
  658. bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_APPEND, SMU_TX_APPEND_FCS_D);
  659. /* Disable forward error correction */
  660. cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_FEC_CONTROL);
  661. cfg &= ~SPU_FEC_CTL_FEC_EN;
  662. bgx_reg_write(bgx, lmacid, BGX_SPUX_FEC_CONTROL, cfg);
  663. /* Disable autoneg */
  664. cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_AN_CONTROL);
  665. cfg = cfg & ~(SPU_AN_CTL_AN_EN | SPU_AN_CTL_XNP_EN);
  666. bgx_reg_write(bgx, lmacid, BGX_SPUX_AN_CONTROL, cfg);
  667. cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_AN_ADV);
  668. if (lmac->lmac_type == BGX_MODE_10G_KR)
  669. cfg |= (1 << 23);
  670. else if (lmac->lmac_type == BGX_MODE_40G_KR)
  671. cfg |= (1 << 24);
  672. else
  673. cfg &= ~((1 << 23) | (1 << 24));
  674. cfg = cfg & (~((1ULL << 25) | (1ULL << 22) | (1ULL << 12)));
  675. bgx_reg_write(bgx, lmacid, BGX_SPUX_AN_ADV, cfg);
  676. cfg = bgx_reg_read(bgx, 0, BGX_SPU_DBG_CONTROL);
  677. cfg &= ~SPU_DBG_CTL_AN_ARB_LINK_CHK_EN;
  678. bgx_reg_write(bgx, 0, BGX_SPU_DBG_CONTROL, cfg);
  679. /* Enable lmac */
  680. bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN);
  681. cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_CONTROL1);
  682. cfg &= ~SPU_CTL_LOW_POWER;
  683. bgx_reg_write(bgx, lmacid, BGX_SPUX_CONTROL1, cfg);
  684. cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_CTL);
  685. cfg &= ~SMU_TX_CTL_UNI_EN;
  686. cfg |= SMU_TX_CTL_DIC_EN;
  687. bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_CTL, cfg);
  688. /* Enable receive and transmission of pause frames */
  689. bgx_reg_write(bgx, lmacid, BGX_SMUX_CBFC_CTL, ((0xffffULL << 32) |
  690. BCK_EN | DRP_EN | TX_EN | RX_EN));
  691. /* Configure pause time and interval */
  692. bgx_reg_write(bgx, lmacid,
  693. BGX_SMUX_TX_PAUSE_PKT_TIME, DEFAULT_PAUSE_TIME);
  694. cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_PAUSE_PKT_INTERVAL);
  695. cfg &= ~0xFFFFull;
  696. bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_PAUSE_PKT_INTERVAL,
  697. cfg | (DEFAULT_PAUSE_TIME - 0x1000));
  698. bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_PAUSE_ZERO, 0x01);
  699. /* take lmac_count into account */
  700. bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_THRESH, (0x100 - 1));
  701. /* max packet size */
  702. bgx_reg_modify(bgx, lmacid, BGX_SMUX_RX_JABBER, MAX_FRAME_SIZE);
  703. return 0;
  704. }
  705. static int bgx_xaui_check_link(struct lmac *lmac)
  706. {
  707. struct bgx *bgx = lmac->bgx;
  708. int lmacid = lmac->lmacid;
  709. int lmac_type = lmac->lmac_type;
  710. u64 cfg;
  711. if (lmac->use_training) {
  712. cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT);
  713. if (!(cfg & (1ull << 13))) {
  714. cfg = (1ull << 13) | (1ull << 14);
  715. bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg);
  716. cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL);
  717. cfg |= (1ull << 0);
  718. bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL, cfg);
  719. return -1;
  720. }
  721. }
  722. /* wait for PCS to come out of reset */
  723. if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET, true)) {
  724. dev_err(&bgx->pdev->dev, "BGX SPU reset not completed\n");
  725. return -1;
  726. }
  727. if ((lmac_type == BGX_MODE_10G_KR) || (lmac_type == BGX_MODE_XFI) ||
  728. (lmac_type == BGX_MODE_40G_KR) || (lmac_type == BGX_MODE_XLAUI)) {
  729. if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_BR_STATUS1,
  730. SPU_BR_STATUS_BLK_LOCK, false)) {
  731. dev_err(&bgx->pdev->dev,
  732. "SPU_BR_STATUS_BLK_LOCK not completed\n");
  733. return -1;
  734. }
  735. } else {
  736. if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_BX_STATUS,
  737. SPU_BX_STATUS_RX_ALIGN, false)) {
  738. dev_err(&bgx->pdev->dev,
  739. "SPU_BX_STATUS_RX_ALIGN not completed\n");
  740. return -1;
  741. }
  742. }
  743. /* Clear rcvflt bit (latching high) and read it back */
  744. if (bgx_reg_read(bgx, lmacid, BGX_SPUX_STATUS2) & SPU_STATUS2_RCVFLT)
  745. bgx_reg_modify(bgx, lmacid,
  746. BGX_SPUX_STATUS2, SPU_STATUS2_RCVFLT);
  747. if (bgx_reg_read(bgx, lmacid, BGX_SPUX_STATUS2) & SPU_STATUS2_RCVFLT) {
  748. dev_err(&bgx->pdev->dev, "Receive fault, retry training\n");
  749. if (lmac->use_training) {
  750. cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT);
  751. if (!(cfg & (1ull << 13))) {
  752. cfg = (1ull << 13) | (1ull << 14);
  753. bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg);
  754. cfg = bgx_reg_read(bgx, lmacid,
  755. BGX_SPUX_BR_PMD_CRTL);
  756. cfg |= (1ull << 0);
  757. bgx_reg_write(bgx, lmacid,
  758. BGX_SPUX_BR_PMD_CRTL, cfg);
  759. return -1;
  760. }
  761. }
  762. return -1;
  763. }
  764. /* Wait for BGX RX to be idle */
  765. if (bgx_poll_reg(bgx, lmacid, BGX_SMUX_CTL, SMU_CTL_RX_IDLE, false)) {
  766. dev_err(&bgx->pdev->dev, "SMU RX not idle\n");
  767. return -1;
  768. }
  769. /* Wait for BGX TX to be idle */
  770. if (bgx_poll_reg(bgx, lmacid, BGX_SMUX_CTL, SMU_CTL_TX_IDLE, false)) {
  771. dev_err(&bgx->pdev->dev, "SMU TX not idle\n");
  772. return -1;
  773. }
  774. /* Check for MAC RX faults */
  775. cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_RX_CTL);
  776. /* 0 - Link is okay, 1 - Local fault, 2 - Remote fault */
  777. cfg &= SMU_RX_CTL_STATUS;
  778. if (!cfg)
  779. return 0;
  780. /* Rx local/remote fault seen.
  781. * Do lmac reinit to see if condition recovers
  782. */
  783. bgx_lmac_xaui_init(bgx, lmac);
  784. return -1;
  785. }
  786. static void bgx_poll_for_sgmii_link(struct lmac *lmac)
  787. {
  788. u64 pcs_link, an_result;
  789. u8 speed;
  790. pcs_link = bgx_reg_read(lmac->bgx, lmac->lmacid,
  791. BGX_GMP_PCS_MRX_STATUS);
  792. /*Link state bit is sticky, read it again*/
  793. if (!(pcs_link & PCS_MRX_STATUS_LINK))
  794. pcs_link = bgx_reg_read(lmac->bgx, lmac->lmacid,
  795. BGX_GMP_PCS_MRX_STATUS);
  796. if (bgx_poll_reg(lmac->bgx, lmac->lmacid, BGX_GMP_PCS_MRX_STATUS,
  797. PCS_MRX_STATUS_AN_CPT, false)) {
  798. lmac->link_up = false;
  799. lmac->last_speed = SPEED_UNKNOWN;
  800. lmac->last_duplex = DUPLEX_UNKNOWN;
  801. goto next_poll;
  802. }
  803. lmac->link_up = ((pcs_link & PCS_MRX_STATUS_LINK) != 0) ? true : false;
  804. an_result = bgx_reg_read(lmac->bgx, lmac->lmacid,
  805. BGX_GMP_PCS_ANX_AN_RESULTS);
  806. speed = (an_result >> 3) & 0x3;
  807. lmac->last_duplex = (an_result >> 1) & 0x1;
  808. switch (speed) {
  809. case 0:
  810. lmac->last_speed = 10;
  811. break;
  812. case 1:
  813. lmac->last_speed = 100;
  814. break;
  815. case 2:
  816. lmac->last_speed = 1000;
  817. break;
  818. default:
  819. lmac->link_up = false;
  820. lmac->last_speed = SPEED_UNKNOWN;
  821. lmac->last_duplex = DUPLEX_UNKNOWN;
  822. break;
  823. }
  824. next_poll:
  825. if (lmac->last_link != lmac->link_up) {
  826. if (lmac->link_up)
  827. bgx_sgmii_change_link_state(lmac);
  828. lmac->last_link = lmac->link_up;
  829. }
  830. queue_delayed_work(lmac->check_link, &lmac->dwork, HZ * 3);
  831. }
  832. static void bgx_poll_for_link(struct work_struct *work)
  833. {
  834. struct lmac *lmac;
  835. u64 spu_link, smu_link;
  836. lmac = container_of(work, struct lmac, dwork.work);
  837. if (lmac->is_sgmii) {
  838. bgx_poll_for_sgmii_link(lmac);
  839. return;
  840. }
  841. /* Receive link is latching low. Force it high and verify it */
  842. bgx_reg_modify(lmac->bgx, lmac->lmacid,
  843. BGX_SPUX_STATUS1, SPU_STATUS1_RCV_LNK);
  844. bgx_poll_reg(lmac->bgx, lmac->lmacid, BGX_SPUX_STATUS1,
  845. SPU_STATUS1_RCV_LNK, false);
  846. spu_link = bgx_reg_read(lmac->bgx, lmac->lmacid, BGX_SPUX_STATUS1);
  847. smu_link = bgx_reg_read(lmac->bgx, lmac->lmacid, BGX_SMUX_RX_CTL);
  848. if ((spu_link & SPU_STATUS1_RCV_LNK) &&
  849. !(smu_link & SMU_RX_CTL_STATUS)) {
  850. lmac->link_up = 1;
  851. if (lmac->lmac_type == BGX_MODE_XLAUI)
  852. lmac->last_speed = 40000;
  853. else
  854. lmac->last_speed = 10000;
  855. lmac->last_duplex = 1;
  856. } else {
  857. lmac->link_up = 0;
  858. lmac->last_speed = SPEED_UNKNOWN;
  859. lmac->last_duplex = DUPLEX_UNKNOWN;
  860. }
  861. if (lmac->last_link != lmac->link_up) {
  862. if (lmac->link_up) {
  863. if (bgx_xaui_check_link(lmac)) {
  864. /* Errors, clear link_up state */
  865. lmac->link_up = 0;
  866. lmac->last_speed = SPEED_UNKNOWN;
  867. lmac->last_duplex = DUPLEX_UNKNOWN;
  868. }
  869. }
  870. lmac->last_link = lmac->link_up;
  871. }
  872. queue_delayed_work(lmac->check_link, &lmac->dwork, HZ * 2);
  873. }
  874. static int phy_interface_mode(u8 lmac_type)
  875. {
  876. if (lmac_type == BGX_MODE_QSGMII)
  877. return PHY_INTERFACE_MODE_QSGMII;
  878. if (lmac_type == BGX_MODE_RGMII)
  879. return PHY_INTERFACE_MODE_RGMII;
  880. return PHY_INTERFACE_MODE_SGMII;
  881. }
  882. static int bgx_lmac_enable(struct bgx *bgx, u8 lmacid)
  883. {
  884. struct lmac *lmac;
  885. u64 cfg;
  886. lmac = &bgx->lmac[lmacid];
  887. lmac->bgx = bgx;
  888. if ((lmac->lmac_type == BGX_MODE_SGMII) ||
  889. (lmac->lmac_type == BGX_MODE_QSGMII) ||
  890. (lmac->lmac_type == BGX_MODE_RGMII)) {
  891. lmac->is_sgmii = 1;
  892. if (bgx_lmac_sgmii_init(bgx, lmac))
  893. return -1;
  894. } else {
  895. lmac->is_sgmii = 0;
  896. if (bgx_lmac_xaui_init(bgx, lmac))
  897. return -1;
  898. }
  899. if (lmac->is_sgmii) {
  900. cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND);
  901. cfg |= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */
  902. bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND, cfg);
  903. bgx_reg_write(bgx, lmacid, BGX_GMP_GMI_TXX_MIN_PKT, 60 - 1);
  904. } else {
  905. cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_APPEND);
  906. cfg |= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */
  907. bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_APPEND, cfg);
  908. bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_MIN_PKT, 60 + 4);
  909. }
  910. /* actual number of filters available to exact LMAC */
  911. lmac->dmacs_count = (RX_DMAC_COUNT / bgx->lmac_count);
  912. lmac->dmacs = kcalloc(lmac->dmacs_count, sizeof(*lmac->dmacs),
  913. GFP_KERNEL);
  914. if (!lmac->dmacs)
  915. return -ENOMEM;
  916. /* Enable lmac */
  917. bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN);
  918. /* Restore default cfg, incase low level firmware changed it */
  919. bgx_reg_write(bgx, lmacid, BGX_CMRX_RX_DMAC_CTL, 0x03);
  920. if ((lmac->lmac_type != BGX_MODE_XFI) &&
  921. (lmac->lmac_type != BGX_MODE_XLAUI) &&
  922. (lmac->lmac_type != BGX_MODE_40G_KR) &&
  923. (lmac->lmac_type != BGX_MODE_10G_KR)) {
  924. if (!lmac->phydev) {
  925. if (lmac->autoneg) {
  926. bgx_reg_write(bgx, lmacid,
  927. BGX_GMP_PCS_LINKX_TIMER,
  928. PCS_LINKX_TIMER_COUNT);
  929. goto poll;
  930. } else {
  931. /* Default to below link speed and duplex */
  932. lmac->link_up = true;
  933. lmac->last_speed = 1000;
  934. lmac->last_duplex = 1;
  935. bgx_sgmii_change_link_state(lmac);
  936. return 0;
  937. }
  938. }
  939. lmac->phydev->dev_flags = 0;
  940. if (phy_connect_direct(&lmac->netdev, lmac->phydev,
  941. bgx_lmac_handler,
  942. phy_interface_mode(lmac->lmac_type)))
  943. return -ENODEV;
  944. phy_start_aneg(lmac->phydev);
  945. return 0;
  946. }
  947. poll:
  948. lmac->check_link = alloc_workqueue("check_link", WQ_UNBOUND |
  949. WQ_MEM_RECLAIM, 1);
  950. if (!lmac->check_link)
  951. return -ENOMEM;
  952. INIT_DELAYED_WORK(&lmac->dwork, bgx_poll_for_link);
  953. queue_delayed_work(lmac->check_link, &lmac->dwork, 0);
  954. return 0;
  955. }
  956. static void bgx_lmac_disable(struct bgx *bgx, u8 lmacid)
  957. {
  958. struct lmac *lmac;
  959. u64 cfg;
  960. lmac = &bgx->lmac[lmacid];
  961. if (lmac->check_link) {
  962. /* Destroy work queue */
  963. cancel_delayed_work_sync(&lmac->dwork);
  964. destroy_workqueue(lmac->check_link);
  965. }
  966. /* Disable packet reception */
  967. cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
  968. cfg &= ~CMR_PKT_RX_EN;
  969. bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
  970. /* Give chance for Rx/Tx FIFO to get drained */
  971. bgx_poll_reg(bgx, lmacid, BGX_CMRX_RX_FIFO_LEN, (u64)0x1FFF, true);
  972. bgx_poll_reg(bgx, lmacid, BGX_CMRX_TX_FIFO_LEN, (u64)0x3FFF, true);
  973. /* Disable packet transmission */
  974. cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
  975. cfg &= ~CMR_PKT_TX_EN;
  976. bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
  977. /* Disable serdes lanes */
  978. if (!lmac->is_sgmii)
  979. bgx_reg_modify(bgx, lmacid,
  980. BGX_SPUX_CONTROL1, SPU_CTL_LOW_POWER);
  981. else
  982. bgx_reg_modify(bgx, lmacid,
  983. BGX_GMP_PCS_MRX_CTL, PCS_MRX_CTL_PWR_DN);
  984. /* Disable LMAC */
  985. cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
  986. cfg &= ~CMR_EN;
  987. bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
  988. bgx_flush_dmac_cam_filter(bgx, lmacid);
  989. kfree(lmac->dmacs);
  990. if ((lmac->lmac_type != BGX_MODE_XFI) &&
  991. (lmac->lmac_type != BGX_MODE_XLAUI) &&
  992. (lmac->lmac_type != BGX_MODE_40G_KR) &&
  993. (lmac->lmac_type != BGX_MODE_10G_KR) && lmac->phydev)
  994. phy_disconnect(lmac->phydev);
  995. lmac->phydev = NULL;
  996. }
  997. static void bgx_init_hw(struct bgx *bgx)
  998. {
  999. int i;
  1000. struct lmac *lmac;
  1001. bgx_reg_modify(bgx, 0, BGX_CMR_GLOBAL_CFG, CMR_GLOBAL_CFG_FCS_STRIP);
  1002. if (bgx_reg_read(bgx, 0, BGX_CMR_BIST_STATUS))
  1003. dev_err(&bgx->pdev->dev, "BGX%d BIST failed\n", bgx->bgx_id);
  1004. /* Set lmac type and lane2serdes mapping */
  1005. for (i = 0; i < bgx->lmac_count; i++) {
  1006. lmac = &bgx->lmac[i];
  1007. bgx_reg_write(bgx, i, BGX_CMRX_CFG,
  1008. (lmac->lmac_type << 8) | lmac->lane_to_sds);
  1009. bgx->lmac[i].lmacid_bd = lmac_count;
  1010. lmac_count++;
  1011. }
  1012. bgx_reg_write(bgx, 0, BGX_CMR_TX_LMACS, bgx->lmac_count);
  1013. bgx_reg_write(bgx, 0, BGX_CMR_RX_LMACS, bgx->lmac_count);
  1014. /* Set the backpressure AND mask */
  1015. for (i = 0; i < bgx->lmac_count; i++)
  1016. bgx_reg_modify(bgx, 0, BGX_CMR_CHAN_MSK_AND,
  1017. ((1ULL << MAX_BGX_CHANS_PER_LMAC) - 1) <<
  1018. (i * MAX_BGX_CHANS_PER_LMAC));
  1019. /* Disable all MAC filtering */
  1020. for (i = 0; i < RX_DMAC_COUNT; i++)
  1021. bgx_reg_write(bgx, 0, BGX_CMR_RX_DMACX_CAM + (i * 8), 0x00);
  1022. /* Disable MAC steering (NCSI traffic) */
  1023. for (i = 0; i < RX_TRAFFIC_STEER_RULE_COUNT; i++)
  1024. bgx_reg_write(bgx, 0, BGX_CMR_RX_STREERING + (i * 8), 0x00);
  1025. }
  1026. static u8 bgx_get_lane2sds_cfg(struct bgx *bgx, struct lmac *lmac)
  1027. {
  1028. return (u8)(bgx_reg_read(bgx, lmac->lmacid, BGX_CMRX_CFG) & 0xFF);
  1029. }
  1030. static void bgx_print_qlm_mode(struct bgx *bgx, u8 lmacid)
  1031. {
  1032. struct device *dev = &bgx->pdev->dev;
  1033. struct lmac *lmac;
  1034. char str[27];
  1035. if (!bgx->is_dlm && lmacid)
  1036. return;
  1037. lmac = &bgx->lmac[lmacid];
  1038. if (!bgx->is_dlm)
  1039. sprintf(str, "BGX%d QLM mode", bgx->bgx_id);
  1040. else
  1041. sprintf(str, "BGX%d LMAC%d mode", bgx->bgx_id, lmacid);
  1042. switch (lmac->lmac_type) {
  1043. case BGX_MODE_SGMII:
  1044. dev_info(dev, "%s: SGMII\n", (char *)str);
  1045. break;
  1046. case BGX_MODE_XAUI:
  1047. dev_info(dev, "%s: XAUI\n", (char *)str);
  1048. break;
  1049. case BGX_MODE_RXAUI:
  1050. dev_info(dev, "%s: RXAUI\n", (char *)str);
  1051. break;
  1052. case BGX_MODE_XFI:
  1053. if (!lmac->use_training)
  1054. dev_info(dev, "%s: XFI\n", (char *)str);
  1055. else
  1056. dev_info(dev, "%s: 10G_KR\n", (char *)str);
  1057. break;
  1058. case BGX_MODE_XLAUI:
  1059. if (!lmac->use_training)
  1060. dev_info(dev, "%s: XLAUI\n", (char *)str);
  1061. else
  1062. dev_info(dev, "%s: 40G_KR4\n", (char *)str);
  1063. break;
  1064. case BGX_MODE_QSGMII:
  1065. dev_info(dev, "%s: QSGMII\n", (char *)str);
  1066. break;
  1067. case BGX_MODE_RGMII:
  1068. dev_info(dev, "%s: RGMII\n", (char *)str);
  1069. break;
  1070. case BGX_MODE_INVALID:
  1071. /* Nothing to do */
  1072. break;
  1073. }
  1074. }
  1075. static void lmac_set_lane2sds(struct bgx *bgx, struct lmac *lmac)
  1076. {
  1077. switch (lmac->lmac_type) {
  1078. case BGX_MODE_SGMII:
  1079. case BGX_MODE_XFI:
  1080. lmac->lane_to_sds = lmac->lmacid;
  1081. break;
  1082. case BGX_MODE_XAUI:
  1083. case BGX_MODE_XLAUI:
  1084. case BGX_MODE_RGMII:
  1085. lmac->lane_to_sds = 0xE4;
  1086. break;
  1087. case BGX_MODE_RXAUI:
  1088. lmac->lane_to_sds = (lmac->lmacid) ? 0xE : 0x4;
  1089. break;
  1090. case BGX_MODE_QSGMII:
  1091. /* There is no way to determine if DLM0/2 is QSGMII or
  1092. * DLM1/3 is configured to QSGMII as bootloader will
  1093. * configure all LMACs, so take whatever is configured
  1094. * by low level firmware.
  1095. */
  1096. lmac->lane_to_sds = bgx_get_lane2sds_cfg(bgx, lmac);
  1097. break;
  1098. default:
  1099. lmac->lane_to_sds = 0;
  1100. break;
  1101. }
  1102. }
  1103. static void lmac_set_training(struct bgx *bgx, struct lmac *lmac, int lmacid)
  1104. {
  1105. if ((lmac->lmac_type != BGX_MODE_10G_KR) &&
  1106. (lmac->lmac_type != BGX_MODE_40G_KR)) {
  1107. lmac->use_training = 0;
  1108. return;
  1109. }
  1110. lmac->use_training = bgx_reg_read(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL) &
  1111. SPU_PMD_CRTL_TRAIN_EN;
  1112. }
  1113. static void bgx_set_lmac_config(struct bgx *bgx, u8 idx)
  1114. {
  1115. struct lmac *lmac;
  1116. u64 cmr_cfg;
  1117. u8 lmac_type;
  1118. u8 lane_to_sds;
  1119. lmac = &bgx->lmac[idx];
  1120. if (!bgx->is_dlm || bgx->is_rgx) {
  1121. /* Read LMAC0 type to figure out QLM mode
  1122. * This is configured by low level firmware
  1123. */
  1124. cmr_cfg = bgx_reg_read(bgx, 0, BGX_CMRX_CFG);
  1125. lmac->lmac_type = (cmr_cfg >> 8) & 0x07;
  1126. if (bgx->is_rgx)
  1127. lmac->lmac_type = BGX_MODE_RGMII;
  1128. lmac_set_training(bgx, lmac, 0);
  1129. lmac_set_lane2sds(bgx, lmac);
  1130. return;
  1131. }
  1132. /* For DLMs or SLMs on 80/81/83xx so many lane configurations
  1133. * are possible and vary across boards. Also Kernel doesn't have
  1134. * any way to identify board type/info and since firmware does,
  1135. * just take lmac type and serdes lane config as is.
  1136. */
  1137. cmr_cfg = bgx_reg_read(bgx, idx, BGX_CMRX_CFG);
  1138. lmac_type = (u8)((cmr_cfg >> 8) & 0x07);
  1139. lane_to_sds = (u8)(cmr_cfg & 0xFF);
  1140. /* Check if config is reset value */
  1141. if ((lmac_type == 0) && (lane_to_sds == 0xE4))
  1142. lmac->lmac_type = BGX_MODE_INVALID;
  1143. else
  1144. lmac->lmac_type = lmac_type;
  1145. lmac->lane_to_sds = lane_to_sds;
  1146. lmac_set_training(bgx, lmac, lmac->lmacid);
  1147. }
  1148. static void bgx_get_qlm_mode(struct bgx *bgx)
  1149. {
  1150. struct lmac *lmac;
  1151. u8 idx;
  1152. /* Init all LMAC's type to invalid */
  1153. for (idx = 0; idx < bgx->max_lmac; idx++) {
  1154. lmac = &bgx->lmac[idx];
  1155. lmac->lmacid = idx;
  1156. lmac->lmac_type = BGX_MODE_INVALID;
  1157. lmac->use_training = false;
  1158. }
  1159. /* It is assumed that low level firmware sets this value */
  1160. bgx->lmac_count = bgx_reg_read(bgx, 0, BGX_CMR_RX_LMACS) & 0x7;
  1161. if (bgx->lmac_count > bgx->max_lmac)
  1162. bgx->lmac_count = bgx->max_lmac;
  1163. for (idx = 0; idx < bgx->lmac_count; idx++) {
  1164. bgx_set_lmac_config(bgx, idx);
  1165. bgx_print_qlm_mode(bgx, idx);
  1166. }
  1167. }
  1168. #ifdef CONFIG_ACPI
  1169. static int acpi_get_mac_address(struct device *dev, struct acpi_device *adev,
  1170. u8 *dst)
  1171. {
  1172. u8 mac[ETH_ALEN];
  1173. int ret;
  1174. ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
  1175. "mac-address", mac, ETH_ALEN);
  1176. if (ret)
  1177. goto out;
  1178. if (!is_valid_ether_addr(mac)) {
  1179. dev_err(dev, "MAC address invalid: %pM\n", mac);
  1180. ret = -EINVAL;
  1181. goto out;
  1182. }
  1183. dev_info(dev, "MAC address set to: %pM\n", mac);
  1184. memcpy(dst, mac, ETH_ALEN);
  1185. out:
  1186. return ret;
  1187. }
  1188. /* Currently only sets the MAC address. */
  1189. static acpi_status bgx_acpi_register_phy(acpi_handle handle,
  1190. u32 lvl, void *context, void **rv)
  1191. {
  1192. struct bgx *bgx = context;
  1193. struct device *dev = &bgx->pdev->dev;
  1194. struct acpi_device *adev;
  1195. if (acpi_bus_get_device(handle, &adev))
  1196. goto out;
  1197. acpi_get_mac_address(dev, adev, bgx->lmac[bgx->acpi_lmac_idx].mac);
  1198. SET_NETDEV_DEV(&bgx->lmac[bgx->acpi_lmac_idx].netdev, dev);
  1199. bgx->lmac[bgx->acpi_lmac_idx].lmacid = bgx->acpi_lmac_idx;
  1200. bgx->acpi_lmac_idx++; /* move to next LMAC */
  1201. out:
  1202. return AE_OK;
  1203. }
  1204. static acpi_status bgx_acpi_match_id(acpi_handle handle, u32 lvl,
  1205. void *context, void **ret_val)
  1206. {
  1207. struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
  1208. struct bgx *bgx = context;
  1209. char bgx_sel[5];
  1210. snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
  1211. if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &string))) {
  1212. pr_warn("Invalid link device\n");
  1213. return AE_OK;
  1214. }
  1215. if (strncmp(string.pointer, bgx_sel, 4))
  1216. return AE_OK;
  1217. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  1218. bgx_acpi_register_phy, NULL, bgx, NULL);
  1219. kfree(string.pointer);
  1220. return AE_CTRL_TERMINATE;
  1221. }
  1222. static int bgx_init_acpi_phy(struct bgx *bgx)
  1223. {
  1224. acpi_get_devices(NULL, bgx_acpi_match_id, bgx, (void **)NULL);
  1225. return 0;
  1226. }
  1227. #else
  1228. static int bgx_init_acpi_phy(struct bgx *bgx)
  1229. {
  1230. return -ENODEV;
  1231. }
  1232. #endif /* CONFIG_ACPI */
  1233. #if IS_ENABLED(CONFIG_OF_MDIO)
  1234. static int bgx_init_of_phy(struct bgx *bgx)
  1235. {
  1236. struct fwnode_handle *fwn;
  1237. struct device_node *node = NULL;
  1238. u8 lmac = 0;
  1239. device_for_each_child_node(&bgx->pdev->dev, fwn) {
  1240. struct phy_device *pd;
  1241. struct device_node *phy_np;
  1242. const char *mac;
  1243. /* Should always be an OF node. But if it is not, we
  1244. * cannot handle it, so exit the loop.
  1245. */
  1246. node = to_of_node(fwn);
  1247. if (!node)
  1248. break;
  1249. mac = of_get_mac_address(node);
  1250. if (mac)
  1251. ether_addr_copy(bgx->lmac[lmac].mac, mac);
  1252. SET_NETDEV_DEV(&bgx->lmac[lmac].netdev, &bgx->pdev->dev);
  1253. bgx->lmac[lmac].lmacid = lmac;
  1254. phy_np = of_parse_phandle(node, "phy-handle", 0);
  1255. /* If there is no phy or defective firmware presents
  1256. * this cortina phy, for which there is no driver
  1257. * support, ignore it.
  1258. */
  1259. if (phy_np &&
  1260. !of_device_is_compatible(phy_np, "cortina,cs4223-slice")) {
  1261. /* Wait until the phy drivers are available */
  1262. pd = of_phy_find_device(phy_np);
  1263. if (!pd)
  1264. goto defer;
  1265. bgx->lmac[lmac].phydev = pd;
  1266. }
  1267. lmac++;
  1268. if (lmac == bgx->max_lmac) {
  1269. of_node_put(node);
  1270. break;
  1271. }
  1272. }
  1273. return 0;
  1274. defer:
  1275. /* We are bailing out, try not to leak device reference counts
  1276. * for phy devices we may have already found.
  1277. */
  1278. while (lmac) {
  1279. if (bgx->lmac[lmac].phydev) {
  1280. put_device(&bgx->lmac[lmac].phydev->mdio.dev);
  1281. bgx->lmac[lmac].phydev = NULL;
  1282. }
  1283. lmac--;
  1284. }
  1285. of_node_put(node);
  1286. return -EPROBE_DEFER;
  1287. }
  1288. #else
  1289. static int bgx_init_of_phy(struct bgx *bgx)
  1290. {
  1291. return -ENODEV;
  1292. }
  1293. #endif /* CONFIG_OF_MDIO */
  1294. static int bgx_init_phy(struct bgx *bgx)
  1295. {
  1296. if (!acpi_disabled)
  1297. return bgx_init_acpi_phy(bgx);
  1298. return bgx_init_of_phy(bgx);
  1299. }
  1300. static int bgx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1301. {
  1302. int err;
  1303. struct device *dev = &pdev->dev;
  1304. struct bgx *bgx = NULL;
  1305. u8 lmac;
  1306. u16 sdevid;
  1307. bgx = devm_kzalloc(dev, sizeof(*bgx), GFP_KERNEL);
  1308. if (!bgx)
  1309. return -ENOMEM;
  1310. bgx->pdev = pdev;
  1311. pci_set_drvdata(pdev, bgx);
  1312. err = pci_enable_device(pdev);
  1313. if (err) {
  1314. dev_err(dev, "Failed to enable PCI device\n");
  1315. pci_set_drvdata(pdev, NULL);
  1316. return err;
  1317. }
  1318. err = pci_request_regions(pdev, DRV_NAME);
  1319. if (err) {
  1320. dev_err(dev, "PCI request regions failed 0x%x\n", err);
  1321. goto err_disable_device;
  1322. }
  1323. /* MAP configuration registers */
  1324. bgx->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
  1325. if (!bgx->reg_base) {
  1326. dev_err(dev, "BGX: Cannot map CSR memory space, aborting\n");
  1327. err = -ENOMEM;
  1328. goto err_release_regions;
  1329. }
  1330. set_max_bgx_per_node(pdev);
  1331. pci_read_config_word(pdev, PCI_DEVICE_ID, &sdevid);
  1332. if (sdevid != PCI_DEVICE_ID_THUNDER_RGX) {
  1333. bgx->bgx_id = (pci_resource_start(pdev,
  1334. PCI_CFG_REG_BAR_NUM) >> 24) & BGX_ID_MASK;
  1335. bgx->bgx_id += nic_get_node_id(pdev) * max_bgx_per_node;
  1336. bgx->max_lmac = MAX_LMAC_PER_BGX;
  1337. bgx_vnic[bgx->bgx_id] = bgx;
  1338. } else {
  1339. bgx->is_rgx = true;
  1340. bgx->max_lmac = 1;
  1341. bgx->bgx_id = MAX_BGX_PER_CN81XX - 1;
  1342. bgx_vnic[bgx->bgx_id] = bgx;
  1343. xcv_init_hw();
  1344. }
  1345. /* On 81xx all are DLMs and on 83xx there are 3 BGX QLMs and one
  1346. * BGX i.e BGX2 can be split across 2 DLMs.
  1347. */
  1348. pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &sdevid);
  1349. if ((sdevid == PCI_SUBSYS_DEVID_81XX_BGX) ||
  1350. ((sdevid == PCI_SUBSYS_DEVID_83XX_BGX) && (bgx->bgx_id == 2)))
  1351. bgx->is_dlm = true;
  1352. bgx_get_qlm_mode(bgx);
  1353. err = bgx_init_phy(bgx);
  1354. if (err)
  1355. goto err_enable;
  1356. bgx_init_hw(bgx);
  1357. /* Enable all LMACs */
  1358. for (lmac = 0; lmac < bgx->lmac_count; lmac++) {
  1359. err = bgx_lmac_enable(bgx, lmac);
  1360. if (err) {
  1361. dev_err(dev, "BGX%d failed to enable lmac%d\n",
  1362. bgx->bgx_id, lmac);
  1363. while (lmac)
  1364. bgx_lmac_disable(bgx, --lmac);
  1365. goto err_enable;
  1366. }
  1367. }
  1368. return 0;
  1369. err_enable:
  1370. bgx_vnic[bgx->bgx_id] = NULL;
  1371. err_release_regions:
  1372. pci_release_regions(pdev);
  1373. err_disable_device:
  1374. pci_disable_device(pdev);
  1375. pci_set_drvdata(pdev, NULL);
  1376. return err;
  1377. }
  1378. static void bgx_remove(struct pci_dev *pdev)
  1379. {
  1380. struct bgx *bgx = pci_get_drvdata(pdev);
  1381. u8 lmac;
  1382. /* Disable all LMACs */
  1383. for (lmac = 0; lmac < bgx->lmac_count; lmac++)
  1384. bgx_lmac_disable(bgx, lmac);
  1385. bgx_vnic[bgx->bgx_id] = NULL;
  1386. pci_release_regions(pdev);
  1387. pci_disable_device(pdev);
  1388. pci_set_drvdata(pdev, NULL);
  1389. }
  1390. static struct pci_driver bgx_driver = {
  1391. .name = DRV_NAME,
  1392. .id_table = bgx_id_table,
  1393. .probe = bgx_probe,
  1394. .remove = bgx_remove,
  1395. };
  1396. static int __init bgx_init_module(void)
  1397. {
  1398. pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
  1399. return pci_register_driver(&bgx_driver);
  1400. }
  1401. static void __exit bgx_cleanup_module(void)
  1402. {
  1403. pci_unregister_driver(&bgx_driver);
  1404. }
  1405. module_init(bgx_init_module);
  1406. module_exit(bgx_cleanup_module);