switchx2.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  1. /*
  2. * drivers/net/ethernet/mellanox/mlxsw/switchx2.c
  3. * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com>
  5. * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com>
  6. * Copyright (c) 2015 Elad Raz <eladr@mellanox.com>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. #include <linux/types.h>
  39. #include <linux/netdevice.h>
  40. #include <linux/etherdevice.h>
  41. #include <linux/slab.h>
  42. #include <linux/device.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/if_vlan.h>
  45. #include <net/switchdev.h>
  46. #include <generated/utsrelease.h>
  47. #include "core.h"
  48. #include "reg.h"
  49. #include "port.h"
  50. #include "trap.h"
  51. #include "txheader.h"
  52. static const char mlxsw_sx_driver_name[] = "mlxsw_switchx2";
  53. static const char mlxsw_sx_driver_version[] = "1.0";
  54. struct mlxsw_sx_port;
  55. struct mlxsw_sx {
  56. struct mlxsw_sx_port **ports;
  57. struct mlxsw_core *core;
  58. const struct mlxsw_bus_info *bus_info;
  59. u8 hw_id[ETH_ALEN];
  60. };
  61. struct mlxsw_sx_port_pcpu_stats {
  62. u64 rx_packets;
  63. u64 rx_bytes;
  64. u64 tx_packets;
  65. u64 tx_bytes;
  66. struct u64_stats_sync syncp;
  67. u32 tx_dropped;
  68. };
  69. struct mlxsw_sx_port {
  70. struct net_device *dev;
  71. struct mlxsw_sx_port_pcpu_stats __percpu *pcpu_stats;
  72. struct mlxsw_sx *mlxsw_sx;
  73. u8 local_port;
  74. };
  75. /* tx_hdr_version
  76. * Tx header version.
  77. * Must be set to 0.
  78. */
  79. MLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4);
  80. /* tx_hdr_ctl
  81. * Packet control type.
  82. * 0 - Ethernet control (e.g. EMADs, LACP)
  83. * 1 - Ethernet data
  84. */
  85. MLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2);
  86. /* tx_hdr_proto
  87. * Packet protocol type. Must be set to 1 (Ethernet).
  88. */
  89. MLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3);
  90. /* tx_hdr_etclass
  91. * Egress TClass to be used on the egress device on the egress port.
  92. * The MSB is specified in the 'ctclass3' field.
  93. * Range is 0-15, where 15 is the highest priority.
  94. */
  95. MLXSW_ITEM32(tx, hdr, etclass, 0x00, 18, 3);
  96. /* tx_hdr_swid
  97. * Switch partition ID.
  98. */
  99. MLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3);
  100. /* tx_hdr_port_mid
  101. * Destination local port for unicast packets.
  102. * Destination multicast ID for multicast packets.
  103. *
  104. * Control packets are directed to a specific egress port, while data
  105. * packets are transmitted through the CPU port (0) into the switch partition,
  106. * where forwarding rules are applied.
  107. */
  108. MLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16);
  109. /* tx_hdr_ctclass3
  110. * See field 'etclass'.
  111. */
  112. MLXSW_ITEM32(tx, hdr, ctclass3, 0x04, 14, 1);
  113. /* tx_hdr_rdq
  114. * RDQ for control packets sent to remote CPU.
  115. * Must be set to 0x1F for EMADs, otherwise 0.
  116. */
  117. MLXSW_ITEM32(tx, hdr, rdq, 0x04, 9, 5);
  118. /* tx_hdr_cpu_sig
  119. * Signature control for packets going to CPU. Must be set to 0.
  120. */
  121. MLXSW_ITEM32(tx, hdr, cpu_sig, 0x04, 0, 9);
  122. /* tx_hdr_sig
  123. * Stacking protocl signature. Must be set to 0xE0E0.
  124. */
  125. MLXSW_ITEM32(tx, hdr, sig, 0x0C, 16, 16);
  126. /* tx_hdr_stclass
  127. * Stacking TClass.
  128. */
  129. MLXSW_ITEM32(tx, hdr, stclass, 0x0C, 13, 3);
  130. /* tx_hdr_emad
  131. * EMAD bit. Must be set for EMADs.
  132. */
  133. MLXSW_ITEM32(tx, hdr, emad, 0x0C, 5, 1);
  134. /* tx_hdr_type
  135. * 0 - Data packets
  136. * 6 - Control packets
  137. */
  138. MLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4);
  139. static void mlxsw_sx_txhdr_construct(struct sk_buff *skb,
  140. const struct mlxsw_tx_info *tx_info)
  141. {
  142. char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN);
  143. bool is_emad = tx_info->is_emad;
  144. memset(txhdr, 0, MLXSW_TXHDR_LEN);
  145. /* We currently set default values for the egress tclass (QoS). */
  146. mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_0);
  147. mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL);
  148. mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH);
  149. mlxsw_tx_hdr_etclass_set(txhdr, is_emad ? MLXSW_TXHDR_ETCLASS_6 :
  150. MLXSW_TXHDR_ETCLASS_5);
  151. mlxsw_tx_hdr_swid_set(txhdr, 0);
  152. mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port);
  153. mlxsw_tx_hdr_ctclass3_set(txhdr, MLXSW_TXHDR_CTCLASS3);
  154. mlxsw_tx_hdr_rdq_set(txhdr, is_emad ? MLXSW_TXHDR_RDQ_EMAD :
  155. MLXSW_TXHDR_RDQ_OTHER);
  156. mlxsw_tx_hdr_cpu_sig_set(txhdr, MLXSW_TXHDR_CPU_SIG);
  157. mlxsw_tx_hdr_sig_set(txhdr, MLXSW_TXHDR_SIG);
  158. mlxsw_tx_hdr_stclass_set(txhdr, MLXSW_TXHDR_STCLASS_NONE);
  159. mlxsw_tx_hdr_emad_set(txhdr, is_emad ? MLXSW_TXHDR_EMAD :
  160. MLXSW_TXHDR_NOT_EMAD);
  161. mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL);
  162. }
  163. static int mlxsw_sx_port_admin_status_set(struct mlxsw_sx_port *mlxsw_sx_port,
  164. bool is_up)
  165. {
  166. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  167. char paos_pl[MLXSW_REG_PAOS_LEN];
  168. mlxsw_reg_paos_pack(paos_pl, mlxsw_sx_port->local_port,
  169. is_up ? MLXSW_PORT_ADMIN_STATUS_UP :
  170. MLXSW_PORT_ADMIN_STATUS_DOWN);
  171. return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(paos), paos_pl);
  172. }
  173. static int mlxsw_sx_port_oper_status_get(struct mlxsw_sx_port *mlxsw_sx_port,
  174. bool *p_is_up)
  175. {
  176. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  177. char paos_pl[MLXSW_REG_PAOS_LEN];
  178. u8 oper_status;
  179. int err;
  180. mlxsw_reg_paos_pack(paos_pl, mlxsw_sx_port->local_port, 0);
  181. err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(paos), paos_pl);
  182. if (err)
  183. return err;
  184. oper_status = mlxsw_reg_paos_oper_status_get(paos_pl);
  185. *p_is_up = oper_status == MLXSW_PORT_ADMIN_STATUS_UP ? true : false;
  186. return 0;
  187. }
  188. static int mlxsw_sx_port_mtu_set(struct mlxsw_sx_port *mlxsw_sx_port, u16 mtu)
  189. {
  190. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  191. char pmtu_pl[MLXSW_REG_PMTU_LEN];
  192. int max_mtu;
  193. int err;
  194. mtu += MLXSW_TXHDR_LEN + ETH_HLEN;
  195. mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sx_port->local_port, 0);
  196. err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(pmtu), pmtu_pl);
  197. if (err)
  198. return err;
  199. max_mtu = mlxsw_reg_pmtu_max_mtu_get(pmtu_pl);
  200. if (mtu > max_mtu)
  201. return -EINVAL;
  202. mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sx_port->local_port, mtu);
  203. return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(pmtu), pmtu_pl);
  204. }
  205. static int mlxsw_sx_port_swid_set(struct mlxsw_sx_port *mlxsw_sx_port, u8 swid)
  206. {
  207. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  208. char pspa_pl[MLXSW_REG_PSPA_LEN];
  209. mlxsw_reg_pspa_pack(pspa_pl, swid, mlxsw_sx_port->local_port);
  210. return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(pspa), pspa_pl);
  211. }
  212. static int
  213. mlxsw_sx_port_system_port_mapping_set(struct mlxsw_sx_port *mlxsw_sx_port)
  214. {
  215. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  216. char sspr_pl[MLXSW_REG_SSPR_LEN];
  217. mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sx_port->local_port);
  218. return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sspr), sspr_pl);
  219. }
  220. static int mlxsw_sx_port_module_check(struct mlxsw_sx_port *mlxsw_sx_port,
  221. bool *p_usable)
  222. {
  223. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  224. char pmlp_pl[MLXSW_REG_PMLP_LEN];
  225. int err;
  226. mlxsw_reg_pmlp_pack(pmlp_pl, mlxsw_sx_port->local_port);
  227. err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(pmlp), pmlp_pl);
  228. if (err)
  229. return err;
  230. *p_usable = mlxsw_reg_pmlp_width_get(pmlp_pl) ? true : false;
  231. return 0;
  232. }
  233. static int mlxsw_sx_port_open(struct net_device *dev)
  234. {
  235. struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
  236. int err;
  237. err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, true);
  238. if (err)
  239. return err;
  240. netif_start_queue(dev);
  241. return 0;
  242. }
  243. static int mlxsw_sx_port_stop(struct net_device *dev)
  244. {
  245. struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
  246. netif_stop_queue(dev);
  247. return mlxsw_sx_port_admin_status_set(mlxsw_sx_port, false);
  248. }
  249. static netdev_tx_t mlxsw_sx_port_xmit(struct sk_buff *skb,
  250. struct net_device *dev)
  251. {
  252. struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
  253. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  254. struct mlxsw_sx_port_pcpu_stats *pcpu_stats;
  255. const struct mlxsw_tx_info tx_info = {
  256. .local_port = mlxsw_sx_port->local_port,
  257. .is_emad = false,
  258. };
  259. u64 len;
  260. int err;
  261. if (mlxsw_core_skb_transmit_busy(mlxsw_sx, &tx_info))
  262. return NETDEV_TX_BUSY;
  263. if (unlikely(skb_headroom(skb) < MLXSW_TXHDR_LEN)) {
  264. struct sk_buff *skb_orig = skb;
  265. skb = skb_realloc_headroom(skb, MLXSW_TXHDR_LEN);
  266. if (!skb) {
  267. this_cpu_inc(mlxsw_sx_port->pcpu_stats->tx_dropped);
  268. dev_kfree_skb_any(skb_orig);
  269. return NETDEV_TX_OK;
  270. }
  271. dev_consume_skb_any(skb_orig);
  272. }
  273. mlxsw_sx_txhdr_construct(skb, &tx_info);
  274. len = skb->len;
  275. /* Due to a race we might fail here because of a full queue. In that
  276. * unlikely case we simply drop the packet.
  277. */
  278. err = mlxsw_core_skb_transmit(mlxsw_sx, skb, &tx_info);
  279. if (!err) {
  280. pcpu_stats = this_cpu_ptr(mlxsw_sx_port->pcpu_stats);
  281. u64_stats_update_begin(&pcpu_stats->syncp);
  282. pcpu_stats->tx_packets++;
  283. pcpu_stats->tx_bytes += len;
  284. u64_stats_update_end(&pcpu_stats->syncp);
  285. } else {
  286. this_cpu_inc(mlxsw_sx_port->pcpu_stats->tx_dropped);
  287. dev_kfree_skb_any(skb);
  288. }
  289. return NETDEV_TX_OK;
  290. }
  291. static int mlxsw_sx_port_change_mtu(struct net_device *dev, int mtu)
  292. {
  293. struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
  294. int err;
  295. err = mlxsw_sx_port_mtu_set(mlxsw_sx_port, mtu);
  296. if (err)
  297. return err;
  298. dev->mtu = mtu;
  299. return 0;
  300. }
  301. static struct rtnl_link_stats64 *
  302. mlxsw_sx_port_get_stats64(struct net_device *dev,
  303. struct rtnl_link_stats64 *stats)
  304. {
  305. struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
  306. struct mlxsw_sx_port_pcpu_stats *p;
  307. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  308. u32 tx_dropped = 0;
  309. unsigned int start;
  310. int i;
  311. for_each_possible_cpu(i) {
  312. p = per_cpu_ptr(mlxsw_sx_port->pcpu_stats, i);
  313. do {
  314. start = u64_stats_fetch_begin_irq(&p->syncp);
  315. rx_packets = p->rx_packets;
  316. rx_bytes = p->rx_bytes;
  317. tx_packets = p->tx_packets;
  318. tx_bytes = p->tx_bytes;
  319. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  320. stats->rx_packets += rx_packets;
  321. stats->rx_bytes += rx_bytes;
  322. stats->tx_packets += tx_packets;
  323. stats->tx_bytes += tx_bytes;
  324. /* tx_dropped is u32, updated without syncp protection. */
  325. tx_dropped += p->tx_dropped;
  326. }
  327. stats->tx_dropped = tx_dropped;
  328. return stats;
  329. }
  330. static const struct net_device_ops mlxsw_sx_port_netdev_ops = {
  331. .ndo_open = mlxsw_sx_port_open,
  332. .ndo_stop = mlxsw_sx_port_stop,
  333. .ndo_start_xmit = mlxsw_sx_port_xmit,
  334. .ndo_change_mtu = mlxsw_sx_port_change_mtu,
  335. .ndo_get_stats64 = mlxsw_sx_port_get_stats64,
  336. };
  337. static void mlxsw_sx_port_get_drvinfo(struct net_device *dev,
  338. struct ethtool_drvinfo *drvinfo)
  339. {
  340. struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
  341. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  342. strlcpy(drvinfo->driver, mlxsw_sx_driver_name, sizeof(drvinfo->driver));
  343. strlcpy(drvinfo->version, mlxsw_sx_driver_version,
  344. sizeof(drvinfo->version));
  345. snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
  346. "%d.%d.%d",
  347. mlxsw_sx->bus_info->fw_rev.major,
  348. mlxsw_sx->bus_info->fw_rev.minor,
  349. mlxsw_sx->bus_info->fw_rev.subminor);
  350. strlcpy(drvinfo->bus_info, mlxsw_sx->bus_info->device_name,
  351. sizeof(drvinfo->bus_info));
  352. }
  353. struct mlxsw_sx_port_hw_stats {
  354. char str[ETH_GSTRING_LEN];
  355. u64 (*getter)(char *payload);
  356. };
  357. static const struct mlxsw_sx_port_hw_stats mlxsw_sx_port_hw_stats[] = {
  358. {
  359. .str = "a_frames_transmitted_ok",
  360. .getter = mlxsw_reg_ppcnt_a_frames_transmitted_ok_get,
  361. },
  362. {
  363. .str = "a_frames_received_ok",
  364. .getter = mlxsw_reg_ppcnt_a_frames_received_ok_get,
  365. },
  366. {
  367. .str = "a_frame_check_sequence_errors",
  368. .getter = mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get,
  369. },
  370. {
  371. .str = "a_alignment_errors",
  372. .getter = mlxsw_reg_ppcnt_a_alignment_errors_get,
  373. },
  374. {
  375. .str = "a_octets_transmitted_ok",
  376. .getter = mlxsw_reg_ppcnt_a_octets_transmitted_ok_get,
  377. },
  378. {
  379. .str = "a_octets_received_ok",
  380. .getter = mlxsw_reg_ppcnt_a_octets_received_ok_get,
  381. },
  382. {
  383. .str = "a_multicast_frames_xmitted_ok",
  384. .getter = mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get,
  385. },
  386. {
  387. .str = "a_broadcast_frames_xmitted_ok",
  388. .getter = mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get,
  389. },
  390. {
  391. .str = "a_multicast_frames_received_ok",
  392. .getter = mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get,
  393. },
  394. {
  395. .str = "a_broadcast_frames_received_ok",
  396. .getter = mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get,
  397. },
  398. {
  399. .str = "a_in_range_length_errors",
  400. .getter = mlxsw_reg_ppcnt_a_in_range_length_errors_get,
  401. },
  402. {
  403. .str = "a_out_of_range_length_field",
  404. .getter = mlxsw_reg_ppcnt_a_out_of_range_length_field_get,
  405. },
  406. {
  407. .str = "a_frame_too_long_errors",
  408. .getter = mlxsw_reg_ppcnt_a_frame_too_long_errors_get,
  409. },
  410. {
  411. .str = "a_symbol_error_during_carrier",
  412. .getter = mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get,
  413. },
  414. {
  415. .str = "a_mac_control_frames_transmitted",
  416. .getter = mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get,
  417. },
  418. {
  419. .str = "a_mac_control_frames_received",
  420. .getter = mlxsw_reg_ppcnt_a_mac_control_frames_received_get,
  421. },
  422. {
  423. .str = "a_unsupported_opcodes_received",
  424. .getter = mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get,
  425. },
  426. {
  427. .str = "a_pause_mac_ctrl_frames_received",
  428. .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get,
  429. },
  430. {
  431. .str = "a_pause_mac_ctrl_frames_xmitted",
  432. .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get,
  433. },
  434. };
  435. #define MLXSW_SX_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sx_port_hw_stats)
  436. static void mlxsw_sx_port_get_strings(struct net_device *dev,
  437. u32 stringset, u8 *data)
  438. {
  439. u8 *p = data;
  440. int i;
  441. switch (stringset) {
  442. case ETH_SS_STATS:
  443. for (i = 0; i < MLXSW_SX_PORT_HW_STATS_LEN; i++) {
  444. memcpy(p, mlxsw_sx_port_hw_stats[i].str,
  445. ETH_GSTRING_LEN);
  446. p += ETH_GSTRING_LEN;
  447. }
  448. break;
  449. }
  450. }
  451. static void mlxsw_sx_port_get_stats(struct net_device *dev,
  452. struct ethtool_stats *stats, u64 *data)
  453. {
  454. struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
  455. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  456. char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
  457. int i;
  458. int err;
  459. mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sx_port->local_port);
  460. err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ppcnt), ppcnt_pl);
  461. for (i = 0; i < MLXSW_SX_PORT_HW_STATS_LEN; i++)
  462. data[i] = !err ? mlxsw_sx_port_hw_stats[i].getter(ppcnt_pl) : 0;
  463. }
  464. static int mlxsw_sx_port_get_sset_count(struct net_device *dev, int sset)
  465. {
  466. switch (sset) {
  467. case ETH_SS_STATS:
  468. return MLXSW_SX_PORT_HW_STATS_LEN;
  469. default:
  470. return -EOPNOTSUPP;
  471. }
  472. }
  473. struct mlxsw_sx_port_link_mode {
  474. u32 mask;
  475. u32 supported;
  476. u32 advertised;
  477. u32 speed;
  478. };
  479. static const struct mlxsw_sx_port_link_mode mlxsw_sx_port_link_mode[] = {
  480. {
  481. .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_T,
  482. .supported = SUPPORTED_100baseT_Full,
  483. .advertised = ADVERTISED_100baseT_Full,
  484. .speed = 100,
  485. },
  486. {
  487. .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_TX,
  488. .speed = 100,
  489. },
  490. {
  491. .mask = MLXSW_REG_PTYS_ETH_SPEED_SGMII |
  492. MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX,
  493. .supported = SUPPORTED_1000baseKX_Full,
  494. .advertised = ADVERTISED_1000baseKX_Full,
  495. .speed = 1000,
  496. },
  497. {
  498. .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_T,
  499. .supported = SUPPORTED_10000baseT_Full,
  500. .advertised = ADVERTISED_10000baseT_Full,
  501. .speed = 10000,
  502. },
  503. {
  504. .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4 |
  505. MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4,
  506. .supported = SUPPORTED_10000baseKX4_Full,
  507. .advertised = ADVERTISED_10000baseKX4_Full,
  508. .speed = 10000,
  509. },
  510. {
  511. .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
  512. MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
  513. MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
  514. MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR,
  515. .supported = SUPPORTED_10000baseKR_Full,
  516. .advertised = ADVERTISED_10000baseKR_Full,
  517. .speed = 10000,
  518. },
  519. {
  520. .mask = MLXSW_REG_PTYS_ETH_SPEED_20GBASE_KR2,
  521. .supported = SUPPORTED_20000baseKR2_Full,
  522. .advertised = ADVERTISED_20000baseKR2_Full,
  523. .speed = 20000,
  524. },
  525. {
  526. .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4,
  527. .supported = SUPPORTED_40000baseCR4_Full,
  528. .advertised = ADVERTISED_40000baseCR4_Full,
  529. .speed = 40000,
  530. },
  531. {
  532. .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4,
  533. .supported = SUPPORTED_40000baseKR4_Full,
  534. .advertised = ADVERTISED_40000baseKR4_Full,
  535. .speed = 40000,
  536. },
  537. {
  538. .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4,
  539. .supported = SUPPORTED_40000baseSR4_Full,
  540. .advertised = ADVERTISED_40000baseSR4_Full,
  541. .speed = 40000,
  542. },
  543. {
  544. .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4,
  545. .supported = SUPPORTED_40000baseLR4_Full,
  546. .advertised = ADVERTISED_40000baseLR4_Full,
  547. .speed = 40000,
  548. },
  549. {
  550. .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR |
  551. MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR |
  552. MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR,
  553. .speed = 25000,
  554. },
  555. {
  556. .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR4 |
  557. MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2 |
  558. MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2,
  559. .speed = 50000,
  560. },
  561. {
  562. .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
  563. .supported = SUPPORTED_56000baseKR4_Full,
  564. .advertised = ADVERTISED_56000baseKR4_Full,
  565. .speed = 56000,
  566. },
  567. {
  568. .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4 |
  569. MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
  570. MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
  571. MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4,
  572. .speed = 100000,
  573. },
  574. };
  575. #define MLXSW_SX_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sx_port_link_mode)
  576. static u32 mlxsw_sx_from_ptys_supported_port(u32 ptys_eth_proto)
  577. {
  578. if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
  579. MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
  580. MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
  581. MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
  582. MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
  583. MLXSW_REG_PTYS_ETH_SPEED_SGMII))
  584. return SUPPORTED_FIBRE;
  585. if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
  586. MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
  587. MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
  588. MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
  589. MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX))
  590. return SUPPORTED_Backplane;
  591. return 0;
  592. }
  593. static u32 mlxsw_sx_from_ptys_supported_link(u32 ptys_eth_proto)
  594. {
  595. u32 modes = 0;
  596. int i;
  597. for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
  598. if (ptys_eth_proto & mlxsw_sx_port_link_mode[i].mask)
  599. modes |= mlxsw_sx_port_link_mode[i].supported;
  600. }
  601. return modes;
  602. }
  603. static u32 mlxsw_sx_from_ptys_advert_link(u32 ptys_eth_proto)
  604. {
  605. u32 modes = 0;
  606. int i;
  607. for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
  608. if (ptys_eth_proto & mlxsw_sx_port_link_mode[i].mask)
  609. modes |= mlxsw_sx_port_link_mode[i].advertised;
  610. }
  611. return modes;
  612. }
  613. static void mlxsw_sx_from_ptys_speed_duplex(bool carrier_ok, u32 ptys_eth_proto,
  614. struct ethtool_cmd *cmd)
  615. {
  616. u32 speed = SPEED_UNKNOWN;
  617. u8 duplex = DUPLEX_UNKNOWN;
  618. int i;
  619. if (!carrier_ok)
  620. goto out;
  621. for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
  622. if (ptys_eth_proto & mlxsw_sx_port_link_mode[i].mask) {
  623. speed = mlxsw_sx_port_link_mode[i].speed;
  624. duplex = DUPLEX_FULL;
  625. break;
  626. }
  627. }
  628. out:
  629. ethtool_cmd_speed_set(cmd, speed);
  630. cmd->duplex = duplex;
  631. }
  632. static u8 mlxsw_sx_port_connector_port(u32 ptys_eth_proto)
  633. {
  634. if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
  635. MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
  636. MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
  637. MLXSW_REG_PTYS_ETH_SPEED_SGMII))
  638. return PORT_FIBRE;
  639. if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
  640. MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
  641. MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4))
  642. return PORT_DA;
  643. if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
  644. MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
  645. MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
  646. MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4))
  647. return PORT_NONE;
  648. return PORT_OTHER;
  649. }
  650. static int mlxsw_sx_port_get_settings(struct net_device *dev,
  651. struct ethtool_cmd *cmd)
  652. {
  653. struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
  654. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  655. char ptys_pl[MLXSW_REG_PTYS_LEN];
  656. u32 eth_proto_cap;
  657. u32 eth_proto_admin;
  658. u32 eth_proto_oper;
  659. int err;
  660. mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sx_port->local_port, 0);
  661. err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
  662. if (err) {
  663. netdev_err(dev, "Failed to get proto");
  664. return err;
  665. }
  666. mlxsw_reg_ptys_unpack(ptys_pl, &eth_proto_cap,
  667. &eth_proto_admin, &eth_proto_oper);
  668. cmd->supported = mlxsw_sx_from_ptys_supported_port(eth_proto_cap) |
  669. mlxsw_sx_from_ptys_supported_link(eth_proto_cap) |
  670. SUPPORTED_Pause | SUPPORTED_Asym_Pause;
  671. cmd->advertising = mlxsw_sx_from_ptys_advert_link(eth_proto_admin);
  672. mlxsw_sx_from_ptys_speed_duplex(netif_carrier_ok(dev),
  673. eth_proto_oper, cmd);
  674. eth_proto_oper = eth_proto_oper ? eth_proto_oper : eth_proto_cap;
  675. cmd->port = mlxsw_sx_port_connector_port(eth_proto_oper);
  676. cmd->lp_advertising = mlxsw_sx_from_ptys_advert_link(eth_proto_oper);
  677. cmd->transceiver = XCVR_INTERNAL;
  678. return 0;
  679. }
  680. static u32 mlxsw_sx_to_ptys_advert_link(u32 advertising)
  681. {
  682. u32 ptys_proto = 0;
  683. int i;
  684. for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
  685. if (advertising & mlxsw_sx_port_link_mode[i].advertised)
  686. ptys_proto |= mlxsw_sx_port_link_mode[i].mask;
  687. }
  688. return ptys_proto;
  689. }
  690. static u32 mlxsw_sx_to_ptys_speed(u32 speed)
  691. {
  692. u32 ptys_proto = 0;
  693. int i;
  694. for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
  695. if (speed == mlxsw_sx_port_link_mode[i].speed)
  696. ptys_proto |= mlxsw_sx_port_link_mode[i].mask;
  697. }
  698. return ptys_proto;
  699. }
  700. static int mlxsw_sx_port_set_settings(struct net_device *dev,
  701. struct ethtool_cmd *cmd)
  702. {
  703. struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
  704. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  705. char ptys_pl[MLXSW_REG_PTYS_LEN];
  706. u32 speed;
  707. u32 eth_proto_new;
  708. u32 eth_proto_cap;
  709. u32 eth_proto_admin;
  710. bool is_up;
  711. int err;
  712. speed = ethtool_cmd_speed(cmd);
  713. eth_proto_new = cmd->autoneg == AUTONEG_ENABLE ?
  714. mlxsw_sx_to_ptys_advert_link(cmd->advertising) :
  715. mlxsw_sx_to_ptys_speed(speed);
  716. mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sx_port->local_port, 0);
  717. err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
  718. if (err) {
  719. netdev_err(dev, "Failed to get proto");
  720. return err;
  721. }
  722. mlxsw_reg_ptys_unpack(ptys_pl, &eth_proto_cap, &eth_proto_admin, NULL);
  723. eth_proto_new = eth_proto_new & eth_proto_cap;
  724. if (!eth_proto_new) {
  725. netdev_err(dev, "Not supported proto admin requested");
  726. return -EINVAL;
  727. }
  728. if (eth_proto_new == eth_proto_admin)
  729. return 0;
  730. mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sx_port->local_port, eth_proto_new);
  731. err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
  732. if (err) {
  733. netdev_err(dev, "Failed to set proto admin");
  734. return err;
  735. }
  736. err = mlxsw_sx_port_oper_status_get(mlxsw_sx_port, &is_up);
  737. if (err) {
  738. netdev_err(dev, "Failed to get oper status");
  739. return err;
  740. }
  741. if (!is_up)
  742. return 0;
  743. err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, false);
  744. if (err) {
  745. netdev_err(dev, "Failed to set admin status");
  746. return err;
  747. }
  748. err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, true);
  749. if (err) {
  750. netdev_err(dev, "Failed to set admin status");
  751. return err;
  752. }
  753. return 0;
  754. }
  755. static const struct ethtool_ops mlxsw_sx_port_ethtool_ops = {
  756. .get_drvinfo = mlxsw_sx_port_get_drvinfo,
  757. .get_link = ethtool_op_get_link,
  758. .get_strings = mlxsw_sx_port_get_strings,
  759. .get_ethtool_stats = mlxsw_sx_port_get_stats,
  760. .get_sset_count = mlxsw_sx_port_get_sset_count,
  761. .get_settings = mlxsw_sx_port_get_settings,
  762. .set_settings = mlxsw_sx_port_set_settings,
  763. };
  764. static int mlxsw_sx_port_attr_get(struct net_device *dev,
  765. struct switchdev_attr *attr)
  766. {
  767. struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
  768. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  769. switch (attr->id) {
  770. case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
  771. attr->u.ppid.id_len = sizeof(mlxsw_sx->hw_id);
  772. memcpy(&attr->u.ppid.id, &mlxsw_sx->hw_id, attr->u.ppid.id_len);
  773. break;
  774. default:
  775. return -EOPNOTSUPP;
  776. }
  777. return 0;
  778. }
  779. static const struct switchdev_ops mlxsw_sx_port_switchdev_ops = {
  780. .switchdev_port_attr_get = mlxsw_sx_port_attr_get,
  781. };
  782. static int mlxsw_sx_hw_id_get(struct mlxsw_sx *mlxsw_sx)
  783. {
  784. char spad_pl[MLXSW_REG_SPAD_LEN];
  785. int err;
  786. err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(spad), spad_pl);
  787. if (err)
  788. return err;
  789. mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sx->hw_id);
  790. return 0;
  791. }
  792. static int mlxsw_sx_port_dev_addr_get(struct mlxsw_sx_port *mlxsw_sx_port)
  793. {
  794. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  795. struct net_device *dev = mlxsw_sx_port->dev;
  796. char ppad_pl[MLXSW_REG_PPAD_LEN];
  797. int err;
  798. mlxsw_reg_ppad_pack(ppad_pl, false, 0);
  799. err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ppad), ppad_pl);
  800. if (err)
  801. return err;
  802. mlxsw_reg_ppad_mac_memcpy_from(ppad_pl, dev->dev_addr);
  803. /* The last byte value in base mac address is guaranteed
  804. * to be such it does not overflow when adding local_port
  805. * value.
  806. */
  807. dev->dev_addr[ETH_ALEN - 1] += mlxsw_sx_port->local_port;
  808. return 0;
  809. }
  810. static int mlxsw_sx_port_stp_state_set(struct mlxsw_sx_port *mlxsw_sx_port,
  811. u16 vid, enum mlxsw_reg_spms_state state)
  812. {
  813. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  814. char *spms_pl;
  815. int err;
  816. spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
  817. if (!spms_pl)
  818. return -ENOMEM;
  819. mlxsw_reg_spms_pack(spms_pl, mlxsw_sx_port->local_port);
  820. mlxsw_reg_spms_vid_pack(spms_pl, vid, state);
  821. err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(spms), spms_pl);
  822. kfree(spms_pl);
  823. return err;
  824. }
  825. static int mlxsw_sx_port_speed_set(struct mlxsw_sx_port *mlxsw_sx_port,
  826. u32 speed)
  827. {
  828. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  829. char ptys_pl[MLXSW_REG_PTYS_LEN];
  830. mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sx_port->local_port, speed);
  831. return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
  832. }
  833. static int
  834. mlxsw_sx_port_mac_learning_mode_set(struct mlxsw_sx_port *mlxsw_sx_port,
  835. enum mlxsw_reg_spmlr_learn_mode mode)
  836. {
  837. struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
  838. char spmlr_pl[MLXSW_REG_SPMLR_LEN];
  839. mlxsw_reg_spmlr_pack(spmlr_pl, mlxsw_sx_port->local_port, mode);
  840. return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(spmlr), spmlr_pl);
  841. }
  842. static int mlxsw_sx_port_create(struct mlxsw_sx *mlxsw_sx, u8 local_port)
  843. {
  844. struct mlxsw_sx_port *mlxsw_sx_port;
  845. struct net_device *dev;
  846. bool usable;
  847. int err;
  848. dev = alloc_etherdev(sizeof(struct mlxsw_sx_port));
  849. if (!dev)
  850. return -ENOMEM;
  851. mlxsw_sx_port = netdev_priv(dev);
  852. mlxsw_sx_port->dev = dev;
  853. mlxsw_sx_port->mlxsw_sx = mlxsw_sx;
  854. mlxsw_sx_port->local_port = local_port;
  855. mlxsw_sx_port->pcpu_stats =
  856. netdev_alloc_pcpu_stats(struct mlxsw_sx_port_pcpu_stats);
  857. if (!mlxsw_sx_port->pcpu_stats) {
  858. err = -ENOMEM;
  859. goto err_alloc_stats;
  860. }
  861. dev->netdev_ops = &mlxsw_sx_port_netdev_ops;
  862. dev->ethtool_ops = &mlxsw_sx_port_ethtool_ops;
  863. dev->switchdev_ops = &mlxsw_sx_port_switchdev_ops;
  864. err = mlxsw_sx_port_dev_addr_get(mlxsw_sx_port);
  865. if (err) {
  866. dev_err(mlxsw_sx->bus_info->dev, "Port %d: Unable to get port mac address\n",
  867. mlxsw_sx_port->local_port);
  868. goto err_dev_addr_get;
  869. }
  870. netif_carrier_off(dev);
  871. dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_LLTX | NETIF_F_SG |
  872. NETIF_F_VLAN_CHALLENGED;
  873. /* Each packet needs to have a Tx header (metadata) on top all other
  874. * headers.
  875. */
  876. dev->hard_header_len += MLXSW_TXHDR_LEN;
  877. err = mlxsw_sx_port_module_check(mlxsw_sx_port, &usable);
  878. if (err) {
  879. dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to check module\n",
  880. mlxsw_sx_port->local_port);
  881. goto err_port_module_check;
  882. }
  883. if (!usable) {
  884. dev_dbg(mlxsw_sx->bus_info->dev, "Port %d: Not usable, skipping initialization\n",
  885. mlxsw_sx_port->local_port);
  886. goto port_not_usable;
  887. }
  888. err = mlxsw_sx_port_system_port_mapping_set(mlxsw_sx_port);
  889. if (err) {
  890. dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set system port mapping\n",
  891. mlxsw_sx_port->local_port);
  892. goto err_port_system_port_mapping_set;
  893. }
  894. err = mlxsw_sx_port_swid_set(mlxsw_sx_port, 0);
  895. if (err) {
  896. dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set SWID\n",
  897. mlxsw_sx_port->local_port);
  898. goto err_port_swid_set;
  899. }
  900. err = mlxsw_sx_port_speed_set(mlxsw_sx_port,
  901. MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4);
  902. if (err) {
  903. dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set speed\n",
  904. mlxsw_sx_port->local_port);
  905. goto err_port_speed_set;
  906. }
  907. err = mlxsw_sx_port_mtu_set(mlxsw_sx_port, ETH_DATA_LEN);
  908. if (err) {
  909. dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set MTU\n",
  910. mlxsw_sx_port->local_port);
  911. goto err_port_mtu_set;
  912. }
  913. err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, false);
  914. if (err)
  915. goto err_port_admin_status_set;
  916. err = mlxsw_sx_port_stp_state_set(mlxsw_sx_port,
  917. MLXSW_PORT_DEFAULT_VID,
  918. MLXSW_REG_SPMS_STATE_FORWARDING);
  919. if (err) {
  920. dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set STP state\n",
  921. mlxsw_sx_port->local_port);
  922. goto err_port_stp_state_set;
  923. }
  924. err = mlxsw_sx_port_mac_learning_mode_set(mlxsw_sx_port,
  925. MLXSW_REG_SPMLR_LEARN_MODE_DISABLE);
  926. if (err) {
  927. dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set MAC learning mode\n",
  928. mlxsw_sx_port->local_port);
  929. goto err_port_mac_learning_mode_set;
  930. }
  931. err = register_netdev(dev);
  932. if (err) {
  933. dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to register netdev\n",
  934. mlxsw_sx_port->local_port);
  935. goto err_register_netdev;
  936. }
  937. mlxsw_sx->ports[local_port] = mlxsw_sx_port;
  938. return 0;
  939. err_register_netdev:
  940. err_port_mac_learning_mode_set:
  941. err_port_stp_state_set:
  942. err_port_admin_status_set:
  943. err_port_mtu_set:
  944. err_port_speed_set:
  945. err_port_swid_set:
  946. err_port_system_port_mapping_set:
  947. port_not_usable:
  948. err_port_module_check:
  949. err_dev_addr_get:
  950. free_percpu(mlxsw_sx_port->pcpu_stats);
  951. err_alloc_stats:
  952. free_netdev(dev);
  953. return err;
  954. }
  955. static void mlxsw_sx_port_remove(struct mlxsw_sx *mlxsw_sx, u8 local_port)
  956. {
  957. struct mlxsw_sx_port *mlxsw_sx_port = mlxsw_sx->ports[local_port];
  958. if (!mlxsw_sx_port)
  959. return;
  960. unregister_netdev(mlxsw_sx_port->dev); /* This calls ndo_stop */
  961. mlxsw_sx_port_swid_set(mlxsw_sx_port, MLXSW_PORT_SWID_DISABLED_PORT);
  962. free_percpu(mlxsw_sx_port->pcpu_stats);
  963. free_netdev(mlxsw_sx_port->dev);
  964. }
  965. static void mlxsw_sx_ports_remove(struct mlxsw_sx *mlxsw_sx)
  966. {
  967. int i;
  968. for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++)
  969. mlxsw_sx_port_remove(mlxsw_sx, i);
  970. kfree(mlxsw_sx->ports);
  971. }
  972. static int mlxsw_sx_ports_create(struct mlxsw_sx *mlxsw_sx)
  973. {
  974. size_t alloc_size;
  975. int i;
  976. int err;
  977. alloc_size = sizeof(struct mlxsw_sx_port *) * MLXSW_PORT_MAX_PORTS;
  978. mlxsw_sx->ports = kzalloc(alloc_size, GFP_KERNEL);
  979. if (!mlxsw_sx->ports)
  980. return -ENOMEM;
  981. for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++) {
  982. err = mlxsw_sx_port_create(mlxsw_sx, i);
  983. if (err)
  984. goto err_port_create;
  985. }
  986. return 0;
  987. err_port_create:
  988. for (i--; i >= 1; i--)
  989. mlxsw_sx_port_remove(mlxsw_sx, i);
  990. kfree(mlxsw_sx->ports);
  991. return err;
  992. }
  993. static void mlxsw_sx_pude_event_func(const struct mlxsw_reg_info *reg,
  994. char *pude_pl, void *priv)
  995. {
  996. struct mlxsw_sx *mlxsw_sx = priv;
  997. struct mlxsw_sx_port *mlxsw_sx_port;
  998. enum mlxsw_reg_pude_oper_status status;
  999. u8 local_port;
  1000. local_port = mlxsw_reg_pude_local_port_get(pude_pl);
  1001. mlxsw_sx_port = mlxsw_sx->ports[local_port];
  1002. if (!mlxsw_sx_port) {
  1003. dev_warn(mlxsw_sx->bus_info->dev, "Port %d: Link event received for non-existent port\n",
  1004. local_port);
  1005. return;
  1006. }
  1007. status = mlxsw_reg_pude_oper_status_get(pude_pl);
  1008. if (status == MLXSW_PORT_OPER_STATUS_UP) {
  1009. netdev_info(mlxsw_sx_port->dev, "link up\n");
  1010. netif_carrier_on(mlxsw_sx_port->dev);
  1011. } else {
  1012. netdev_info(mlxsw_sx_port->dev, "link down\n");
  1013. netif_carrier_off(mlxsw_sx_port->dev);
  1014. }
  1015. }
  1016. static struct mlxsw_event_listener mlxsw_sx_pude_event = {
  1017. .func = mlxsw_sx_pude_event_func,
  1018. .trap_id = MLXSW_TRAP_ID_PUDE,
  1019. };
  1020. static int mlxsw_sx_event_register(struct mlxsw_sx *mlxsw_sx,
  1021. enum mlxsw_event_trap_id trap_id)
  1022. {
  1023. struct mlxsw_event_listener *el;
  1024. char hpkt_pl[MLXSW_REG_HPKT_LEN];
  1025. int err;
  1026. switch (trap_id) {
  1027. case MLXSW_TRAP_ID_PUDE:
  1028. el = &mlxsw_sx_pude_event;
  1029. break;
  1030. }
  1031. err = mlxsw_core_event_listener_register(mlxsw_sx->core, el, mlxsw_sx);
  1032. if (err)
  1033. return err;
  1034. mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_FORWARD, trap_id);
  1035. err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(hpkt), hpkt_pl);
  1036. if (err)
  1037. goto err_event_trap_set;
  1038. return 0;
  1039. err_event_trap_set:
  1040. mlxsw_core_event_listener_unregister(mlxsw_sx->core, el, mlxsw_sx);
  1041. return err;
  1042. }
  1043. static void mlxsw_sx_event_unregister(struct mlxsw_sx *mlxsw_sx,
  1044. enum mlxsw_event_trap_id trap_id)
  1045. {
  1046. struct mlxsw_event_listener *el;
  1047. switch (trap_id) {
  1048. case MLXSW_TRAP_ID_PUDE:
  1049. el = &mlxsw_sx_pude_event;
  1050. break;
  1051. }
  1052. mlxsw_core_event_listener_unregister(mlxsw_sx->core, el, mlxsw_sx);
  1053. }
  1054. static void mlxsw_sx_rx_listener_func(struct sk_buff *skb, u8 local_port,
  1055. void *priv)
  1056. {
  1057. struct mlxsw_sx *mlxsw_sx = priv;
  1058. struct mlxsw_sx_port *mlxsw_sx_port = mlxsw_sx->ports[local_port];
  1059. struct mlxsw_sx_port_pcpu_stats *pcpu_stats;
  1060. if (unlikely(!mlxsw_sx_port)) {
  1061. dev_warn_ratelimited(mlxsw_sx->bus_info->dev, "Port %d: skb received for non-existent port\n",
  1062. local_port);
  1063. return;
  1064. }
  1065. skb->dev = mlxsw_sx_port->dev;
  1066. pcpu_stats = this_cpu_ptr(mlxsw_sx_port->pcpu_stats);
  1067. u64_stats_update_begin(&pcpu_stats->syncp);
  1068. pcpu_stats->rx_packets++;
  1069. pcpu_stats->rx_bytes += skb->len;
  1070. u64_stats_update_end(&pcpu_stats->syncp);
  1071. skb->protocol = eth_type_trans(skb, skb->dev);
  1072. netif_receive_skb(skb);
  1073. }
  1074. static const struct mlxsw_rx_listener mlxsw_sx_rx_listener[] = {
  1075. {
  1076. .func = mlxsw_sx_rx_listener_func,
  1077. .local_port = MLXSW_PORT_DONT_CARE,
  1078. .trap_id = MLXSW_TRAP_ID_FDB_MC,
  1079. },
  1080. /* Traps for specific L2 packet types, not trapped as FDB MC */
  1081. {
  1082. .func = mlxsw_sx_rx_listener_func,
  1083. .local_port = MLXSW_PORT_DONT_CARE,
  1084. .trap_id = MLXSW_TRAP_ID_STP,
  1085. },
  1086. {
  1087. .func = mlxsw_sx_rx_listener_func,
  1088. .local_port = MLXSW_PORT_DONT_CARE,
  1089. .trap_id = MLXSW_TRAP_ID_LACP,
  1090. },
  1091. {
  1092. .func = mlxsw_sx_rx_listener_func,
  1093. .local_port = MLXSW_PORT_DONT_CARE,
  1094. .trap_id = MLXSW_TRAP_ID_EAPOL,
  1095. },
  1096. {
  1097. .func = mlxsw_sx_rx_listener_func,
  1098. .local_port = MLXSW_PORT_DONT_CARE,
  1099. .trap_id = MLXSW_TRAP_ID_LLDP,
  1100. },
  1101. {
  1102. .func = mlxsw_sx_rx_listener_func,
  1103. .local_port = MLXSW_PORT_DONT_CARE,
  1104. .trap_id = MLXSW_TRAP_ID_MMRP,
  1105. },
  1106. {
  1107. .func = mlxsw_sx_rx_listener_func,
  1108. .local_port = MLXSW_PORT_DONT_CARE,
  1109. .trap_id = MLXSW_TRAP_ID_MVRP,
  1110. },
  1111. {
  1112. .func = mlxsw_sx_rx_listener_func,
  1113. .local_port = MLXSW_PORT_DONT_CARE,
  1114. .trap_id = MLXSW_TRAP_ID_RPVST,
  1115. },
  1116. {
  1117. .func = mlxsw_sx_rx_listener_func,
  1118. .local_port = MLXSW_PORT_DONT_CARE,
  1119. .trap_id = MLXSW_TRAP_ID_DHCP,
  1120. },
  1121. {
  1122. .func = mlxsw_sx_rx_listener_func,
  1123. .local_port = MLXSW_PORT_DONT_CARE,
  1124. .trap_id = MLXSW_TRAP_ID_IGMP_QUERY,
  1125. },
  1126. {
  1127. .func = mlxsw_sx_rx_listener_func,
  1128. .local_port = MLXSW_PORT_DONT_CARE,
  1129. .trap_id = MLXSW_TRAP_ID_IGMP_V1_REPORT,
  1130. },
  1131. {
  1132. .func = mlxsw_sx_rx_listener_func,
  1133. .local_port = MLXSW_PORT_DONT_CARE,
  1134. .trap_id = MLXSW_TRAP_ID_IGMP_V2_REPORT,
  1135. },
  1136. {
  1137. .func = mlxsw_sx_rx_listener_func,
  1138. .local_port = MLXSW_PORT_DONT_CARE,
  1139. .trap_id = MLXSW_TRAP_ID_IGMP_V2_LEAVE,
  1140. },
  1141. {
  1142. .func = mlxsw_sx_rx_listener_func,
  1143. .local_port = MLXSW_PORT_DONT_CARE,
  1144. .trap_id = MLXSW_TRAP_ID_IGMP_V3_REPORT,
  1145. },
  1146. };
  1147. static int mlxsw_sx_traps_init(struct mlxsw_sx *mlxsw_sx)
  1148. {
  1149. char htgt_pl[MLXSW_REG_HTGT_LEN];
  1150. char hpkt_pl[MLXSW_REG_HPKT_LEN];
  1151. int i;
  1152. int err;
  1153. mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_RX);
  1154. err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(htgt), htgt_pl);
  1155. if (err)
  1156. return err;
  1157. mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_CTRL);
  1158. err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(htgt), htgt_pl);
  1159. if (err)
  1160. return err;
  1161. for (i = 0; i < ARRAY_SIZE(mlxsw_sx_rx_listener); i++) {
  1162. err = mlxsw_core_rx_listener_register(mlxsw_sx->core,
  1163. &mlxsw_sx_rx_listener[i],
  1164. mlxsw_sx);
  1165. if (err)
  1166. goto err_rx_listener_register;
  1167. mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_TRAP_TO_CPU,
  1168. mlxsw_sx_rx_listener[i].trap_id);
  1169. err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(hpkt), hpkt_pl);
  1170. if (err)
  1171. goto err_rx_trap_set;
  1172. }
  1173. return 0;
  1174. err_rx_trap_set:
  1175. mlxsw_core_rx_listener_unregister(mlxsw_sx->core,
  1176. &mlxsw_sx_rx_listener[i],
  1177. mlxsw_sx);
  1178. err_rx_listener_register:
  1179. for (i--; i >= 0; i--) {
  1180. mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_FORWARD,
  1181. mlxsw_sx_rx_listener[i].trap_id);
  1182. mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(hpkt), hpkt_pl);
  1183. mlxsw_core_rx_listener_unregister(mlxsw_sx->core,
  1184. &mlxsw_sx_rx_listener[i],
  1185. mlxsw_sx);
  1186. }
  1187. return err;
  1188. }
  1189. static void mlxsw_sx_traps_fini(struct mlxsw_sx *mlxsw_sx)
  1190. {
  1191. char hpkt_pl[MLXSW_REG_HPKT_LEN];
  1192. int i;
  1193. for (i = 0; i < ARRAY_SIZE(mlxsw_sx_rx_listener); i++) {
  1194. mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_FORWARD,
  1195. mlxsw_sx_rx_listener[i].trap_id);
  1196. mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(hpkt), hpkt_pl);
  1197. mlxsw_core_rx_listener_unregister(mlxsw_sx->core,
  1198. &mlxsw_sx_rx_listener[i],
  1199. mlxsw_sx);
  1200. }
  1201. }
  1202. static int mlxsw_sx_flood_init(struct mlxsw_sx *mlxsw_sx)
  1203. {
  1204. char sfgc_pl[MLXSW_REG_SFGC_LEN];
  1205. char sgcr_pl[MLXSW_REG_SGCR_LEN];
  1206. char *sftr_pl;
  1207. int err;
  1208. /* Configure a flooding table, which includes only CPU port. */
  1209. sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
  1210. if (!sftr_pl)
  1211. return -ENOMEM;
  1212. mlxsw_reg_sftr_pack(sftr_pl, 0, 0, MLXSW_REG_SFGC_TABLE_TYPE_SINGLE, 0,
  1213. MLXSW_PORT_CPU_PORT, true);
  1214. err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sftr), sftr_pl);
  1215. kfree(sftr_pl);
  1216. if (err)
  1217. return err;
  1218. /* Flood different packet types using the flooding table. */
  1219. mlxsw_reg_sfgc_pack(sfgc_pl,
  1220. MLXSW_REG_SFGC_TYPE_UNKNOWN_UNICAST,
  1221. MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
  1222. MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
  1223. 0);
  1224. err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
  1225. if (err)
  1226. return err;
  1227. mlxsw_reg_sfgc_pack(sfgc_pl,
  1228. MLXSW_REG_SFGC_TYPE_BROADCAST,
  1229. MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
  1230. MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
  1231. 0);
  1232. err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
  1233. if (err)
  1234. return err;
  1235. mlxsw_reg_sfgc_pack(sfgc_pl,
  1236. MLXSW_REG_SFGC_TYPE_UNREGISTERED_MULTICAST_NON_IP,
  1237. MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
  1238. MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
  1239. 0);
  1240. err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
  1241. if (err)
  1242. return err;
  1243. mlxsw_reg_sfgc_pack(sfgc_pl,
  1244. MLXSW_REG_SFGC_TYPE_UNREGISTERED_MULTICAST_IPV6,
  1245. MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
  1246. MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
  1247. 0);
  1248. err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
  1249. if (err)
  1250. return err;
  1251. mlxsw_reg_sfgc_pack(sfgc_pl,
  1252. MLXSW_REG_SFGC_TYPE_UNREGISTERED_MULTICAST_IPV4,
  1253. MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
  1254. MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
  1255. 0);
  1256. err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
  1257. if (err)
  1258. return err;
  1259. mlxsw_reg_sgcr_pack(sgcr_pl, true);
  1260. return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sgcr), sgcr_pl);
  1261. }
  1262. static int mlxsw_sx_init(void *priv, struct mlxsw_core *mlxsw_core,
  1263. const struct mlxsw_bus_info *mlxsw_bus_info)
  1264. {
  1265. struct mlxsw_sx *mlxsw_sx = priv;
  1266. int err;
  1267. mlxsw_sx->core = mlxsw_core;
  1268. mlxsw_sx->bus_info = mlxsw_bus_info;
  1269. err = mlxsw_sx_hw_id_get(mlxsw_sx);
  1270. if (err) {
  1271. dev_err(mlxsw_sx->bus_info->dev, "Failed to get switch HW ID\n");
  1272. return err;
  1273. }
  1274. err = mlxsw_sx_ports_create(mlxsw_sx);
  1275. if (err) {
  1276. dev_err(mlxsw_sx->bus_info->dev, "Failed to create ports\n");
  1277. return err;
  1278. }
  1279. err = mlxsw_sx_event_register(mlxsw_sx, MLXSW_TRAP_ID_PUDE);
  1280. if (err) {
  1281. dev_err(mlxsw_sx->bus_info->dev, "Failed to register for PUDE events\n");
  1282. goto err_event_register;
  1283. }
  1284. err = mlxsw_sx_traps_init(mlxsw_sx);
  1285. if (err) {
  1286. dev_err(mlxsw_sx->bus_info->dev, "Failed to set traps for RX\n");
  1287. goto err_rx_listener_register;
  1288. }
  1289. err = mlxsw_sx_flood_init(mlxsw_sx);
  1290. if (err) {
  1291. dev_err(mlxsw_sx->bus_info->dev, "Failed to initialize flood tables\n");
  1292. goto err_flood_init;
  1293. }
  1294. return 0;
  1295. err_flood_init:
  1296. mlxsw_sx_traps_fini(mlxsw_sx);
  1297. err_rx_listener_register:
  1298. mlxsw_sx_event_unregister(mlxsw_sx, MLXSW_TRAP_ID_PUDE);
  1299. err_event_register:
  1300. mlxsw_sx_ports_remove(mlxsw_sx);
  1301. return err;
  1302. }
  1303. static void mlxsw_sx_fini(void *priv)
  1304. {
  1305. struct mlxsw_sx *mlxsw_sx = priv;
  1306. mlxsw_sx_traps_fini(mlxsw_sx);
  1307. mlxsw_sx_event_unregister(mlxsw_sx, MLXSW_TRAP_ID_PUDE);
  1308. mlxsw_sx_ports_remove(mlxsw_sx);
  1309. }
  1310. static struct mlxsw_config_profile mlxsw_sx_config_profile = {
  1311. .used_max_vepa_channels = 1,
  1312. .max_vepa_channels = 0,
  1313. .used_max_lag = 1,
  1314. .max_lag = 64,
  1315. .used_max_port_per_lag = 1,
  1316. .max_port_per_lag = 16,
  1317. .used_max_mid = 1,
  1318. .max_mid = 7000,
  1319. .used_max_pgt = 1,
  1320. .max_pgt = 0,
  1321. .used_max_system_port = 1,
  1322. .max_system_port = 48000,
  1323. .used_max_vlan_groups = 1,
  1324. .max_vlan_groups = 127,
  1325. .used_max_regions = 1,
  1326. .max_regions = 400,
  1327. .used_flood_tables = 1,
  1328. .max_flood_tables = 2,
  1329. .max_vid_flood_tables = 1,
  1330. .used_flood_mode = 1,
  1331. .flood_mode = 3,
  1332. .used_max_ib_mc = 1,
  1333. .max_ib_mc = 0,
  1334. .used_max_pkey = 1,
  1335. .max_pkey = 0,
  1336. .swid_config = {
  1337. {
  1338. .used_type = 1,
  1339. .type = MLXSW_PORT_SWID_TYPE_ETH,
  1340. }
  1341. },
  1342. };
  1343. static struct mlxsw_driver mlxsw_sx_driver = {
  1344. .kind = MLXSW_DEVICE_KIND_SWITCHX2,
  1345. .owner = THIS_MODULE,
  1346. .priv_size = sizeof(struct mlxsw_sx),
  1347. .init = mlxsw_sx_init,
  1348. .fini = mlxsw_sx_fini,
  1349. .txhdr_construct = mlxsw_sx_txhdr_construct,
  1350. .txhdr_len = MLXSW_TXHDR_LEN,
  1351. .profile = &mlxsw_sx_config_profile,
  1352. };
  1353. static int __init mlxsw_sx_module_init(void)
  1354. {
  1355. return mlxsw_core_driver_register(&mlxsw_sx_driver);
  1356. }
  1357. static void __exit mlxsw_sx_module_exit(void)
  1358. {
  1359. mlxsw_core_driver_unregister(&mlxsw_sx_driver);
  1360. }
  1361. module_init(mlxsw_sx_module_init);
  1362. module_exit(mlxsw_sx_module_exit);
  1363. MODULE_LICENSE("Dual BSD/GPL");
  1364. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  1365. MODULE_DESCRIPTION("Mellanox SwitchX-2 driver");
  1366. MODULE_MLXSW_DRIVER_ALIAS(MLXSW_DEVICE_KIND_SWITCHX2);