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