1 /*- 2 * Copyright (c) 2016 Solarflare Communications Inc. 3 * All rights reserved. 4 * 5 * This software was jointly developed between OKTET Labs (under contract 6 * for Solarflare) and Solarflare Communications, Inc. 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 notice, 12 * this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright notice, 14 * this list of conditions and the following disclaimer in the documentation 15 * and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 27 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include "efx.h" 31 32 #include "sfc.h" 33 #include "sfc_log.h" 34 35 /** 36 * Update MAC statistics in the buffer. 37 * 38 * @param sa Adapter 39 * 40 * @return Status code 41 * @retval 0 Success 42 * @retval EAGAIN Try again 43 * @retval ENOMEM Memory allocation failure 44 */ 45 int 46 sfc_port_update_mac_stats(struct sfc_adapter *sa) 47 { 48 struct sfc_port *port = &sa->port; 49 int rc; 50 51 SFC_ASSERT(rte_spinlock_is_locked(&port->mac_stats_lock)); 52 53 if (sa->state != SFC_ADAPTER_STARTED) 54 return EINVAL; 55 56 rc = efx_mac_stats_update(sa->nic, &port->mac_stats_dma_mem, 57 port->mac_stats_buf, NULL); 58 if (rc != 0) 59 return rc; 60 61 return 0; 62 } 63 64 int 65 sfc_port_start(struct sfc_adapter *sa) 66 { 67 struct sfc_port *port = &sa->port; 68 int rc; 69 70 sfc_log_init(sa, "entry"); 71 72 sfc_log_init(sa, "init filters"); 73 rc = efx_filter_init(sa->nic); 74 if (rc != 0) 75 goto fail_filter_init; 76 77 sfc_log_init(sa, "init port"); 78 rc = efx_port_init(sa->nic); 79 if (rc != 0) 80 goto fail_port_init; 81 82 sfc_log_init(sa, "set flow control to %#x autoneg=%u", 83 port->flow_ctrl, port->flow_ctrl_autoneg); 84 rc = efx_mac_fcntl_set(sa->nic, port->flow_ctrl, 85 port->flow_ctrl_autoneg); 86 if (rc != 0) 87 goto fail_mac_fcntl_set; 88 89 sfc_log_init(sa, "set phy adv caps to %#x", port->phy_adv_cap); 90 rc = efx_phy_adv_cap_set(sa->nic, port->phy_adv_cap); 91 if (rc != 0) 92 goto fail_phy_adv_cap_set; 93 94 sfc_log_init(sa, "set MAC PDU %u", (unsigned int)port->pdu); 95 rc = efx_mac_pdu_set(sa->nic, port->pdu); 96 if (rc != 0) 97 goto fail_mac_pdu_set; 98 99 sfc_log_init(sa, "set MAC address"); 100 rc = efx_mac_addr_set(sa->nic, 101 sa->eth_dev->data->mac_addrs[0].addr_bytes); 102 if (rc != 0) 103 goto fail_mac_addr_set; 104 105 sfc_log_init(sa, "set MAC filters"); 106 port->promisc = (sa->eth_dev->data->promiscuous != 0) ? 107 B_TRUE : B_FALSE; 108 port->allmulti = (sa->eth_dev->data->all_multicast != 0) ? 109 B_TRUE : B_FALSE; 110 rc = sfc_set_rx_mode(sa); 111 if (rc != 0) 112 goto fail_mac_filter_set; 113 114 efx_mac_stats_get_mask(sa->nic, port->mac_stats_mask, 115 sizeof(port->mac_stats_mask)); 116 117 /* Update MAC stats using periodic DMA. 118 * Common code always uses 1000ms update period, so period_ms 119 * parameter only needs to be non-zero to start updates. 120 */ 121 sfc_log_init(sa, "request MAC stats DMA'ing"); 122 rc = efx_mac_stats_periodic(sa->nic, &port->mac_stats_dma_mem, 123 1000, B_FALSE); 124 if (rc != 0) 125 goto fail_mac_stats_periodic; 126 127 sfc_log_init(sa, "disable MAC drain"); 128 rc = efx_mac_drain(sa->nic, B_FALSE); 129 if (rc != 0) 130 goto fail_mac_drain; 131 132 sfc_log_init(sa, "done"); 133 return 0; 134 135 fail_mac_drain: 136 (void)efx_mac_stats_periodic(sa->nic, &port->mac_stats_dma_mem, 137 0, B_FALSE); 138 139 fail_mac_stats_periodic: 140 fail_mac_filter_set: 141 fail_mac_addr_set: 142 fail_mac_pdu_set: 143 fail_phy_adv_cap_set: 144 fail_mac_fcntl_set: 145 efx_port_fini(sa->nic); 146 147 fail_port_init: 148 efx_filter_fini(sa->nic); 149 150 fail_filter_init: 151 sfc_log_init(sa, "failed %d", rc); 152 return rc; 153 } 154 155 void 156 sfc_port_stop(struct sfc_adapter *sa) 157 { 158 sfc_log_init(sa, "entry"); 159 160 efx_mac_drain(sa->nic, B_TRUE); 161 162 (void)efx_mac_stats_periodic(sa->nic, &sa->port.mac_stats_dma_mem, 163 0, B_FALSE); 164 165 efx_port_fini(sa->nic); 166 efx_filter_fini(sa->nic); 167 168 sfc_log_init(sa, "done"); 169 } 170 171 int 172 sfc_port_init(struct sfc_adapter *sa) 173 { 174 const struct rte_eth_dev_data *dev_data = sa->eth_dev->data; 175 struct sfc_port *port = &sa->port; 176 int rc; 177 178 sfc_log_init(sa, "entry"); 179 180 /* Enable flow control by default */ 181 port->flow_ctrl = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE; 182 port->flow_ctrl_autoneg = B_TRUE; 183 184 if (dev_data->dev_conf.rxmode.jumbo_frame) 185 port->pdu = dev_data->dev_conf.rxmode.max_rx_pkt_len; 186 else 187 port->pdu = EFX_MAC_PDU(dev_data->mtu); 188 189 rte_spinlock_init(&port->mac_stats_lock); 190 191 rc = ENOMEM; 192 port->mac_stats_buf = rte_calloc_socket("mac_stats_buf", EFX_MAC_NSTATS, 193 sizeof(uint64_t), 0, 194 sa->socket_id); 195 if (port->mac_stats_buf == NULL) 196 goto fail_mac_stats_buf_alloc; 197 198 rc = sfc_dma_alloc(sa, "mac_stats", 0, EFX_MAC_STATS_SIZE, 199 sa->socket_id, &port->mac_stats_dma_mem); 200 if (rc != 0) 201 goto fail_mac_stats_dma_alloc; 202 203 sfc_log_init(sa, "done"); 204 return 0; 205 206 fail_mac_stats_dma_alloc: 207 rte_free(port->mac_stats_buf); 208 fail_mac_stats_buf_alloc: 209 sfc_log_init(sa, "failed %d", rc); 210 return rc; 211 } 212 213 void 214 sfc_port_fini(struct sfc_adapter *sa) 215 { 216 struct sfc_port *port = &sa->port; 217 218 sfc_log_init(sa, "entry"); 219 220 sfc_dma_free(sa, &port->mac_stats_dma_mem); 221 rte_free(port->mac_stats_buf); 222 223 sfc_log_init(sa, "done"); 224 } 225 226 int 227 sfc_set_rx_mode(struct sfc_adapter *sa) 228 { 229 struct sfc_port *port = &sa->port; 230 int rc; 231 232 rc = efx_mac_filter_set(sa->nic, port->promisc, B_TRUE, 233 port->promisc || port->allmulti, B_TRUE); 234 235 return rc; 236 } 237 238 void 239 sfc_port_link_mode_to_info(efx_link_mode_t link_mode, 240 struct rte_eth_link *link_info) 241 { 242 SFC_ASSERT(link_mode < EFX_LINK_NMODES); 243 244 memset(link_info, 0, sizeof(*link_info)); 245 if ((link_mode == EFX_LINK_DOWN) || (link_mode == EFX_LINK_UNKNOWN)) 246 link_info->link_status = ETH_LINK_DOWN; 247 else 248 link_info->link_status = ETH_LINK_UP; 249 250 switch (link_mode) { 251 case EFX_LINK_10HDX: 252 link_info->link_speed = ETH_SPEED_NUM_10M; 253 link_info->link_duplex = ETH_LINK_HALF_DUPLEX; 254 break; 255 case EFX_LINK_10FDX: 256 link_info->link_speed = ETH_SPEED_NUM_10M; 257 link_info->link_duplex = ETH_LINK_FULL_DUPLEX; 258 break; 259 case EFX_LINK_100HDX: 260 link_info->link_speed = ETH_SPEED_NUM_100M; 261 link_info->link_duplex = ETH_LINK_HALF_DUPLEX; 262 break; 263 case EFX_LINK_100FDX: 264 link_info->link_speed = ETH_SPEED_NUM_100M; 265 link_info->link_duplex = ETH_LINK_FULL_DUPLEX; 266 break; 267 case EFX_LINK_1000HDX: 268 link_info->link_speed = ETH_SPEED_NUM_1G; 269 link_info->link_duplex = ETH_LINK_HALF_DUPLEX; 270 break; 271 case EFX_LINK_1000FDX: 272 link_info->link_speed = ETH_SPEED_NUM_1G; 273 link_info->link_duplex = ETH_LINK_FULL_DUPLEX; 274 break; 275 case EFX_LINK_10000FDX: 276 link_info->link_speed = ETH_SPEED_NUM_10G; 277 link_info->link_duplex = ETH_LINK_FULL_DUPLEX; 278 break; 279 case EFX_LINK_40000FDX: 280 link_info->link_speed = ETH_SPEED_NUM_40G; 281 link_info->link_duplex = ETH_LINK_FULL_DUPLEX; 282 break; 283 default: 284 SFC_ASSERT(B_FALSE); 285 /* FALLTHROUGH */ 286 case EFX_LINK_UNKNOWN: 287 case EFX_LINK_DOWN: 288 link_info->link_speed = ETH_SPEED_NUM_NONE; 289 link_info->link_duplex = 0; 290 break; 291 } 292 293 link_info->link_autoneg = ETH_LINK_AUTONEG; 294 } 295