1 /*************************************************************************
2 SPDX-License-Identifier: BSD-3-Clause
4 Copyright (c) 2003-2007 Cavium Networks (support@cavium.com). All rights
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are
12 * Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above
16 copyright notice, this list of conditions and the following
17 disclaimer in the documentation and/or other materials provided
18 with the distribution.
20 * Neither the name of Cavium Networks nor the names of
21 its contributors may be used to endorse or promote products
22 derived from this software without specific prior written
25 This Software, including technical data, may be subject to U.S. export control laws, including the U.S. Export Administration Act and its associated regulations, and may be subject to export or import regulations in other countries.
27 TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
28 AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
30 *************************************************************************/
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
41 #include <sys/socket.h>
43 #include <net/ethernet.h>
45 #include <net/if_var.h>
47 #include "wrapper-cvmx-includes.h"
48 #include "ethernet-headers.h"
50 static uint64_t cvm_oct_mac_addr = 0;
51 static uint32_t cvm_oct_mac_addr_offset = 0;
54 * Set the multicast list. Currently unimplemented.
56 * @param dev Device to work on
58 void cvm_oct_common_set_multicast_list(struct ifnet *ifp)
60 cvmx_gmxx_prtx_cfg_t gmx_cfg;
61 cvm_oct_private_t *priv = (cvm_oct_private_t *)ifp->if_softc;
62 int interface = INTERFACE(priv->port);
63 int index = INDEX(priv->port);
65 if ((interface < 2) && (cvmx_helper_interface_get_mode(interface) != CVMX_HELPER_INTERFACE_MODE_SPI)) {
66 cvmx_gmxx_rxx_adr_ctl_t control;
68 control.s.bcst = 1; /* Allow broadcast MAC addresses */
70 if (/*ifp->mc_list || */(ifp->if_flags&IFF_ALLMULTI) ||
71 (ifp->if_flags & IFF_PROMISC))
72 control.s.mcst = 2; /* Force accept multicast packets */
74 control.s.mcst = 1; /* Force reject multicat packets */
76 if (ifp->if_flags & IFF_PROMISC)
77 control.s.cam_mode = 0; /* Reject matches if promisc. Since CAM is shut off, should accept everything */
79 control.s.cam_mode = 1; /* Filter packets based on the CAM */
81 gmx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
82 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64 & ~1ull);
84 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CTL(index, interface), control.u64);
85 if (ifp->if_flags&IFF_PROMISC)
86 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM_EN(index, interface), 0);
88 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM_EN(index, interface), 1);
90 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
96 * Assign a MAC addres from the pool of available MAC addresses
97 * Can return as either a 64-bit value and/or 6 octets.
99 * @param macp Filled in with the assigned address if non-NULL
100 * @param octets Filled in with the assigned address if non-NULL
101 * @return Zero on success
103 int cvm_assign_mac_address(uint64_t *macp, uint8_t *octets)
105 /* Initialize from global MAC address base; fail if not set */
106 if (cvm_oct_mac_addr == 0) {
107 memcpy((uint8_t *)&cvm_oct_mac_addr + 2,
108 cvmx_sysinfo_get()->mac_addr_base, 6);
110 if (cvm_oct_mac_addr == 0)
113 cvm_oct_mac_addr_offset = cvmx_mgmt_port_num_ports();
114 cvm_oct_mac_addr += cvm_oct_mac_addr_offset;
117 if (cvm_oct_mac_addr_offset >= cvmx_sysinfo_get()->mac_addr_count)
118 return ENXIO; /* Out of addresses to assign */
121 *macp = cvm_oct_mac_addr;
123 memcpy(octets, (u_int8_t *)&cvm_oct_mac_addr + 2, 6);
126 cvm_oct_mac_addr_offset++;
132 * Set the hardware MAC address for a device
134 * @param dev Device to change the MAC address for
135 * @param addr Address structure to change it too.
137 void cvm_oct_common_set_mac_address(struct ifnet *ifp, const void *addr)
139 cvm_oct_private_t *priv = (cvm_oct_private_t *)ifp->if_softc;
140 cvmx_gmxx_prtx_cfg_t gmx_cfg;
141 int interface = INTERFACE(priv->port);
142 int index = INDEX(priv->port);
144 memcpy(priv->mac, addr, 6);
146 if ((interface < 2) && (cvmx_helper_interface_get_mode(interface) != CVMX_HELPER_INTERFACE_MODE_SPI)) {
148 const uint8_t *ptr = addr;
150 for (i = 0; i < 6; i++)
151 mac = (mac<<8) | (uint64_t)(ptr[i]);
153 gmx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
154 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64 & ~1ull);
156 cvmx_write_csr(CVMX_GMXX_SMACX(index, interface), mac);
157 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM0(index, interface), ptr[0]);
158 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM1(index, interface), ptr[1]);
159 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM2(index, interface), ptr[2]);
160 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM3(index, interface), ptr[3]);
161 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM4(index, interface), ptr[4]);
162 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM5(index, interface), ptr[5]);
163 cvm_oct_common_set_multicast_list(ifp);
164 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
170 * Change the link MTU. Unimplemented
172 * @param dev Device to change
173 * @param new_mtu The new MTU
174 * @return Zero on success
176 int cvm_oct_common_change_mtu(struct ifnet *ifp, int new_mtu)
178 cvm_oct_private_t *priv = (cvm_oct_private_t *)ifp->if_softc;
179 int interface = INTERFACE(priv->port);
180 int index = INDEX(priv->port);
183 /* Limit the MTU to make sure the ethernet packets are between 64 bytes
185 if ((new_mtu + 14 + 4 + vlan_bytes < 64) || (new_mtu + 14 + 4 + vlan_bytes > 65392)) {
186 printf("MTU must be between %d and %d.\n", 64-14-4-vlan_bytes, 65392-14-4-vlan_bytes);
189 ifp->if_mtu = new_mtu;
191 if ((interface < 2) && (cvmx_helper_interface_get_mode(interface) != CVMX_HELPER_INTERFACE_MODE_SPI)) {
192 int max_packet = new_mtu + 14 + 4 + vlan_bytes; /* Add ethernet header and FCS, and VLAN if configured. */
194 if (OCTEON_IS_MODEL(OCTEON_CN3XXX) || OCTEON_IS_MODEL(OCTEON_CN58XX)) {
195 /* Signal errors on packets larger than the MTU */
196 cvmx_write_csr(CVMX_GMXX_RXX_FRM_MAX(index, interface), max_packet);
198 /* Set the hardware to truncate packets larger than the MTU and
199 smaller the 64 bytes */
200 cvmx_pip_frm_len_chkx_t frm_len_chk;
202 frm_len_chk.s.minlen = 64;
203 frm_len_chk.s.maxlen = max_packet;
204 cvmx_write_csr(CVMX_PIP_FRM_LEN_CHKX(interface), frm_len_chk.u64);
206 /* Set the hardware to truncate packets larger than the MTU. The
207 jabber register must be set to a multiple of 8 bytes, so round up */
208 cvmx_write_csr(CVMX_GMXX_RXX_JABBER(index, interface), (max_packet + 7) & ~7u);
217 int cvm_oct_common_open(struct ifnet *ifp)
219 cvmx_gmxx_prtx_cfg_t gmx_cfg;
220 cvm_oct_private_t *priv = (cvm_oct_private_t *)ifp->if_softc;
221 int interface = INTERFACE(priv->port);
222 int index = INDEX(priv->port);
223 cvmx_helper_link_info_t link_info;
225 gmx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
227 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
230 * Set the link state unless we are using MII.
232 if (cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_SIM && priv->miibus == NULL) {
233 link_info = cvmx_helper_link_get(priv->port);
234 if (!link_info.s.link_up)
235 if_link_state_change(ifp, LINK_STATE_DOWN);
237 if_link_state_change(ifp, LINK_STATE_UP);
247 int cvm_oct_common_stop(struct ifnet *ifp)
249 cvmx_gmxx_prtx_cfg_t gmx_cfg;
250 cvm_oct_private_t *priv = (cvm_oct_private_t *)ifp->if_softc;
251 int interface = INTERFACE(priv->port);
252 int index = INDEX(priv->port);
254 gmx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
256 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
261 * Poll for link status change.
263 void cvm_oct_common_poll(struct ifnet *ifp)
265 cvm_oct_private_t *priv = (cvm_oct_private_t *)ifp->if_softc;
266 cvmx_helper_link_info_t link_info;
269 * If this is a simulation, do nothing.
271 if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM)
275 * If there is a device-specific poll method, use it.
277 if (priv->poll != NULL) {
283 * If an MII bus is attached, don't use the Simple Executive's link
286 if (priv->miibus != NULL)
290 * Use the Simple Executive's link state routines.
292 link_info = cvmx_helper_link_get(priv->port);
293 if (link_info.u64 == priv->link_info)
296 link_info = cvmx_helper_link_autoconf(priv->port);
297 priv->link_info = link_info.u64;
298 priv->need_link_update = 1;
303 * Per network device initialization
305 * @param dev Device to initialize
306 * @return Zero on success
308 int cvm_oct_common_init(struct ifnet *ifp)
311 cvm_oct_private_t *priv = (cvm_oct_private_t *)ifp->if_softc;
313 if (cvm_assign_mac_address(NULL, mac) != 0)
316 ifp->if_mtu = ETHERMTU;
318 cvm_oct_mdio_setup_device(ifp);
320 cvm_oct_common_set_mac_address(ifp, mac);
321 cvm_oct_common_change_mtu(ifp, ifp->if_mtu);
324 * Do any last-minute board-specific initialization.
326 switch (cvmx_sysinfo_get()->board_type) {
327 #if defined(OCTEON_VENDOR_LANNER)
328 case CVMX_BOARD_TYPE_CUST_LANNER_MR320:
329 case CVMX_BOARD_TYPE_CUST_LANNER_MR321X:
330 if (priv->phy_id == 16)
331 cvm_oct_mv88e61xx_setup_device(ifp);
338 device_attach(priv->dev);
343 void cvm_oct_common_uninit(struct ifnet *ifp)
345 /* Currently nothing to do */