1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * aQuantia Corporation Network Driver 4 * Copyright (C) 2014-2019 aQuantia Corporation. All rights reserved 5 */ 6 7 /* File aq_main.c: Main file for aQuantia Linux driver. */ 8 9 #include "aq_main.h" 10 #include "aq_nic.h" 11 #include "aq_pci_func.h" 12 #include "aq_ethtool.h" 13 #include "aq_ptp.h" 14 #include "aq_filters.h" 15 16 #include <linux/netdevice.h> 17 #include <linux/module.h> 18 #include <linux/ip.h> 19 #include <linux/udp.h> 20 21 MODULE_LICENSE("GPL v2"); 22 MODULE_AUTHOR(AQ_CFG_DRV_AUTHOR); 23 MODULE_DESCRIPTION(AQ_CFG_DRV_DESC); 24 25 static const char aq_ndev_driver_name[] = AQ_CFG_DRV_NAME; 26 27 static const struct net_device_ops aq_ndev_ops; 28 29 static struct workqueue_struct *aq_ndev_wq; 30 31 void aq_ndev_schedule_work(struct work_struct *work) 32 { 33 queue_work(aq_ndev_wq, work); 34 } 35 36 struct net_device *aq_ndev_alloc(void) 37 { 38 struct net_device *ndev = NULL; 39 struct aq_nic_s *aq_nic = NULL; 40 41 ndev = alloc_etherdev_mq(sizeof(struct aq_nic_s), AQ_CFG_VECS_MAX); 42 if (!ndev) 43 return NULL; 44 45 aq_nic = netdev_priv(ndev); 46 aq_nic->ndev = ndev; 47 ndev->netdev_ops = &aq_ndev_ops; 48 ndev->ethtool_ops = &aq_ethtool_ops; 49 50 return ndev; 51 } 52 53 static int aq_ndev_open(struct net_device *ndev) 54 { 55 struct aq_nic_s *aq_nic = netdev_priv(ndev); 56 int err = 0; 57 58 err = aq_nic_init(aq_nic); 59 if (err < 0) 60 goto err_exit; 61 62 err = aq_reapply_rxnfc_all_rules(aq_nic); 63 if (err < 0) 64 goto err_exit; 65 66 err = aq_filters_vlans_update(aq_nic); 67 if (err < 0) 68 goto err_exit; 69 70 err = aq_nic_start(aq_nic); 71 if (err < 0) 72 goto err_exit; 73 74 err_exit: 75 if (err < 0) 76 aq_nic_deinit(aq_nic, true); 77 78 return err; 79 } 80 81 static int aq_ndev_close(struct net_device *ndev) 82 { 83 struct aq_nic_s *aq_nic = netdev_priv(ndev); 84 int err = 0; 85 86 err = aq_nic_stop(aq_nic); 87 if (err < 0) 88 goto err_exit; 89 aq_nic_deinit(aq_nic, true); 90 91 err_exit: 92 return err; 93 } 94 95 static int aq_ndev_start_xmit(struct sk_buff *skb, struct net_device *ndev) 96 { 97 struct aq_nic_s *aq_nic = netdev_priv(ndev); 98 99 if (unlikely(aq_utils_obj_test(&aq_nic->flags, AQ_NIC_PTP_DPATH_UP))) { 100 /* Hardware adds the Timestamp for PTPv2 802.AS1 101 * and PTPv2 IPv4 UDP. 102 * We have to push even general 320 port messages to the ptp 103 * queue explicitly. This is a limitation of current firmware 104 * and hardware PTP design of the chip. Otherwise ptp stream 105 * will fail to sync 106 */ 107 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) || 108 unlikely((ip_hdr(skb)->version == 4) && 109 (ip_hdr(skb)->protocol == IPPROTO_UDP) && 110 ((udp_hdr(skb)->dest == htons(319)) || 111 (udp_hdr(skb)->dest == htons(320)))) || 112 unlikely(eth_hdr(skb)->h_proto == htons(ETH_P_1588))) 113 return aq_ptp_xmit(aq_nic, skb); 114 } 115 116 skb_tx_timestamp(skb); 117 return aq_nic_xmit(aq_nic, skb); 118 } 119 120 static int aq_ndev_change_mtu(struct net_device *ndev, int new_mtu) 121 { 122 struct aq_nic_s *aq_nic = netdev_priv(ndev); 123 int err; 124 125 err = aq_nic_set_mtu(aq_nic, new_mtu + ETH_HLEN); 126 127 if (err < 0) 128 goto err_exit; 129 ndev->mtu = new_mtu; 130 131 err_exit: 132 return err; 133 } 134 135 static int aq_ndev_set_features(struct net_device *ndev, 136 netdev_features_t features) 137 { 138 bool is_vlan_tx_insert = !!(features & NETIF_F_HW_VLAN_CTAG_TX); 139 bool is_vlan_rx_strip = !!(features & NETIF_F_HW_VLAN_CTAG_RX); 140 struct aq_nic_s *aq_nic = netdev_priv(ndev); 141 bool need_ndev_restart = false; 142 struct aq_nic_cfg_s *aq_cfg; 143 bool is_lro = false; 144 int err = 0; 145 146 aq_cfg = aq_nic_get_cfg(aq_nic); 147 148 if (!(features & NETIF_F_NTUPLE)) { 149 if (aq_nic->ndev->features & NETIF_F_NTUPLE) { 150 err = aq_clear_rxnfc_all_rules(aq_nic); 151 if (unlikely(err)) 152 goto err_exit; 153 } 154 } 155 if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER)) { 156 if (aq_nic->ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) { 157 err = aq_filters_vlan_offload_off(aq_nic); 158 if (unlikely(err)) 159 goto err_exit; 160 } 161 } 162 163 aq_cfg->features = features; 164 165 if (aq_cfg->aq_hw_caps->hw_features & NETIF_F_LRO) { 166 is_lro = features & NETIF_F_LRO; 167 168 if (aq_cfg->is_lro != is_lro) { 169 aq_cfg->is_lro = is_lro; 170 need_ndev_restart = true; 171 } 172 } 173 174 if ((aq_nic->ndev->features ^ features) & NETIF_F_RXCSUM) { 175 err = aq_nic->aq_hw_ops->hw_set_offload(aq_nic->aq_hw, 176 aq_cfg); 177 178 if (unlikely(err)) 179 goto err_exit; 180 } 181 182 if (aq_cfg->is_vlan_rx_strip != is_vlan_rx_strip) { 183 aq_cfg->is_vlan_rx_strip = is_vlan_rx_strip; 184 need_ndev_restart = true; 185 } 186 if (aq_cfg->is_vlan_tx_insert != is_vlan_tx_insert) { 187 aq_cfg->is_vlan_tx_insert = is_vlan_tx_insert; 188 need_ndev_restart = true; 189 } 190 191 if (need_ndev_restart && netif_running(ndev)) { 192 aq_ndev_close(ndev); 193 aq_ndev_open(ndev); 194 } 195 196 err_exit: 197 return err; 198 } 199 200 static int aq_ndev_set_mac_address(struct net_device *ndev, void *addr) 201 { 202 struct aq_nic_s *aq_nic = netdev_priv(ndev); 203 int err = 0; 204 205 err = eth_mac_addr(ndev, addr); 206 if (err < 0) 207 goto err_exit; 208 err = aq_nic_set_mac(aq_nic, ndev); 209 if (err < 0) 210 goto err_exit; 211 212 err_exit: 213 return err; 214 } 215 216 static void aq_ndev_set_multicast_settings(struct net_device *ndev) 217 { 218 struct aq_nic_s *aq_nic = netdev_priv(ndev); 219 220 (void)aq_nic_set_multicast_list(aq_nic, ndev); 221 } 222 223 static int aq_ndev_config_hwtstamp(struct aq_nic_s *aq_nic, 224 struct hwtstamp_config *config) 225 { 226 if (config->flags) 227 return -EINVAL; 228 229 switch (config->tx_type) { 230 case HWTSTAMP_TX_OFF: 231 case HWTSTAMP_TX_ON: 232 break; 233 default: 234 return -ERANGE; 235 } 236 237 switch (config->rx_filter) { 238 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 239 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 240 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 241 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 242 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 243 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 244 case HWTSTAMP_FILTER_PTP_V2_SYNC: 245 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 246 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 247 break; 248 case HWTSTAMP_FILTER_PTP_V2_EVENT: 249 case HWTSTAMP_FILTER_NONE: 250 break; 251 default: 252 return -ERANGE; 253 } 254 255 return aq_ptp_hwtstamp_config_set(aq_nic->aq_ptp, config); 256 } 257 258 static int aq_ndev_hwtstamp_set(struct aq_nic_s *aq_nic, struct ifreq *ifr) 259 { 260 struct hwtstamp_config config; 261 int ret_val; 262 263 if (!aq_nic->aq_ptp) 264 return -EOPNOTSUPP; 265 266 if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 267 return -EFAULT; 268 269 ret_val = aq_ndev_config_hwtstamp(aq_nic, &config); 270 if (ret_val) 271 return ret_val; 272 273 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 274 -EFAULT : 0; 275 } 276 277 static int aq_ndev_hwtstamp_get(struct aq_nic_s *aq_nic, struct ifreq *ifr) 278 { 279 struct hwtstamp_config config; 280 281 if (!aq_nic->aq_ptp) 282 return -EOPNOTSUPP; 283 284 aq_ptp_hwtstamp_config_get(aq_nic->aq_ptp, &config); 285 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 286 -EFAULT : 0; 287 } 288 289 static int aq_ndev_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd) 290 { 291 struct aq_nic_s *aq_nic = netdev_priv(netdev); 292 293 switch (cmd) { 294 case SIOCSHWTSTAMP: 295 return aq_ndev_hwtstamp_set(aq_nic, ifr); 296 297 case SIOCGHWTSTAMP: 298 return aq_ndev_hwtstamp_get(aq_nic, ifr); 299 } 300 301 return -EOPNOTSUPP; 302 } 303 304 static int aq_ndo_vlan_rx_add_vid(struct net_device *ndev, __be16 proto, 305 u16 vid) 306 { 307 struct aq_nic_s *aq_nic = netdev_priv(ndev); 308 309 if (!aq_nic->aq_hw_ops->hw_filter_vlan_set) 310 return -EOPNOTSUPP; 311 312 set_bit(vid, aq_nic->active_vlans); 313 314 return aq_filters_vlans_update(aq_nic); 315 } 316 317 static int aq_ndo_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto, 318 u16 vid) 319 { 320 struct aq_nic_s *aq_nic = netdev_priv(ndev); 321 322 if (!aq_nic->aq_hw_ops->hw_filter_vlan_set) 323 return -EOPNOTSUPP; 324 325 clear_bit(vid, aq_nic->active_vlans); 326 327 if (-ENOENT == aq_del_fvlan_by_vlan(aq_nic, vid)) 328 return aq_filters_vlans_update(aq_nic); 329 330 return 0; 331 } 332 333 static const struct net_device_ops aq_ndev_ops = { 334 .ndo_open = aq_ndev_open, 335 .ndo_stop = aq_ndev_close, 336 .ndo_start_xmit = aq_ndev_start_xmit, 337 .ndo_set_rx_mode = aq_ndev_set_multicast_settings, 338 .ndo_change_mtu = aq_ndev_change_mtu, 339 .ndo_set_mac_address = aq_ndev_set_mac_address, 340 .ndo_set_features = aq_ndev_set_features, 341 .ndo_do_ioctl = aq_ndev_ioctl, 342 .ndo_vlan_rx_add_vid = aq_ndo_vlan_rx_add_vid, 343 .ndo_vlan_rx_kill_vid = aq_ndo_vlan_rx_kill_vid, 344 }; 345 346 static int __init aq_ndev_init_module(void) 347 { 348 int ret; 349 350 aq_ndev_wq = create_singlethread_workqueue(aq_ndev_driver_name); 351 if (!aq_ndev_wq) { 352 pr_err("Failed to create workqueue\n"); 353 return -ENOMEM; 354 } 355 356 ret = aq_pci_func_register_driver(); 357 if (ret) { 358 destroy_workqueue(aq_ndev_wq); 359 return ret; 360 } 361 362 return 0; 363 } 364 365 static void __exit aq_ndev_exit_module(void) 366 { 367 aq_pci_func_unregister_driver(); 368 369 if (aq_ndev_wq) { 370 destroy_workqueue(aq_ndev_wq); 371 aq_ndev_wq = NULL; 372 } 373 } 374 375 module_init(aq_ndev_init_module); 376 module_exit(aq_ndev_exit_module); 377