1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 2 /* Copyright 2017-2019 NXP */ 3 4 #include <linux/module.h> 5 #include "enetc.h" 6 7 #define ENETC_DRV_NAME_STR "ENETC VF driver" 8 9 /* Messaging */ 10 static void enetc_msg_vsi_write_msg(struct enetc_hw *hw, 11 struct enetc_msg_swbd *msg) 12 { 13 u32 val; 14 15 val = enetc_vsi_set_msize(msg->size) | lower_32_bits(msg->dma); 16 enetc_wr(hw, ENETC_VSIMSGSNDAR1, upper_32_bits(msg->dma)); 17 enetc_wr(hw, ENETC_VSIMSGSNDAR0, val); 18 } 19 20 static int enetc_msg_vsi_send(struct enetc_si *si, struct enetc_msg_swbd *msg) 21 { 22 int timeout = 100; 23 u32 vsimsgsr; 24 25 enetc_msg_vsi_write_msg(&si->hw, msg); 26 27 do { 28 vsimsgsr = enetc_rd(&si->hw, ENETC_VSIMSGSR); 29 if (!(vsimsgsr & ENETC_VSIMSGSR_MB)) 30 break; 31 32 usleep_range(1000, 2000); 33 } while (--timeout); 34 35 if (!timeout) 36 return -ETIMEDOUT; 37 38 /* check for message delivery error */ 39 if (vsimsgsr & ENETC_VSIMSGSR_MS) { 40 dev_err(&si->pdev->dev, "VSI command execute error: %d\n", 41 ENETC_SIMSGSR_GET_MC(vsimsgsr)); 42 return -EIO; 43 } 44 45 return 0; 46 } 47 48 static int enetc_msg_vsi_set_primary_mac_addr(struct enetc_ndev_priv *priv, 49 struct sockaddr *saddr) 50 { 51 struct enetc_msg_cmd_set_primary_mac *cmd; 52 struct enetc_msg_swbd msg; 53 int err; 54 55 msg.size = ALIGN(sizeof(struct enetc_msg_cmd_set_primary_mac), 64); 56 msg.vaddr = dma_alloc_coherent(priv->dev, msg.size, &msg.dma, 57 GFP_KERNEL); 58 if (!msg.vaddr) { 59 dev_err(priv->dev, "Failed to alloc Tx msg (size: %d)\n", 60 msg.size); 61 return -ENOMEM; 62 } 63 64 cmd = (struct enetc_msg_cmd_set_primary_mac *)msg.vaddr; 65 cmd->header.type = ENETC_MSG_CMD_MNG_MAC; 66 cmd->header.id = ENETC_MSG_CMD_MNG_ADD; 67 memcpy(&cmd->mac, saddr, sizeof(struct sockaddr)); 68 69 /* send the command and wait */ 70 err = enetc_msg_vsi_send(priv->si, &msg); 71 72 dma_free_coherent(priv->dev, msg.size, msg.vaddr, msg.dma); 73 74 return err; 75 } 76 77 static int enetc_vf_set_mac_addr(struct net_device *ndev, void *addr) 78 { 79 struct enetc_ndev_priv *priv = netdev_priv(ndev); 80 struct sockaddr *saddr = addr; 81 int err; 82 83 if (!is_valid_ether_addr(saddr->sa_data)) 84 return -EADDRNOTAVAIL; 85 86 err = enetc_msg_vsi_set_primary_mac_addr(priv, saddr); 87 if (err) 88 return err; 89 90 return 0; 91 } 92 93 static int enetc_vf_set_features(struct net_device *ndev, 94 netdev_features_t features) 95 { 96 return enetc_set_features(ndev, features); 97 } 98 99 /* Probing/ Init */ 100 static const struct net_device_ops enetc_ndev_ops = { 101 .ndo_open = enetc_open, 102 .ndo_stop = enetc_close, 103 .ndo_start_xmit = enetc_xmit, 104 .ndo_get_stats = enetc_get_stats, 105 .ndo_set_mac_address = enetc_vf_set_mac_addr, 106 .ndo_set_features = enetc_vf_set_features, 107 .ndo_do_ioctl = enetc_ioctl, 108 .ndo_setup_tc = enetc_setup_tc, 109 }; 110 111 static void enetc_vf_netdev_setup(struct enetc_si *si, struct net_device *ndev, 112 const struct net_device_ops *ndev_ops) 113 { 114 struct enetc_ndev_priv *priv = netdev_priv(ndev); 115 116 SET_NETDEV_DEV(ndev, &si->pdev->dev); 117 priv->ndev = ndev; 118 priv->si = si; 119 priv->dev = &si->pdev->dev; 120 si->ndev = ndev; 121 122 priv->msg_enable = (NETIF_MSG_IFUP << 1) - 1; 123 ndev->netdev_ops = ndev_ops; 124 enetc_set_ethtool_ops(ndev); 125 ndev->watchdog_timeo = 5 * HZ; 126 ndev->max_mtu = ENETC_MAX_MTU; 127 128 ndev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM | NETIF_F_HW_CSUM | 129 NETIF_F_HW_VLAN_CTAG_TX | 130 NETIF_F_HW_VLAN_CTAG_RX; 131 ndev->features = NETIF_F_HIGHDMA | NETIF_F_SG | 132 NETIF_F_RXCSUM | NETIF_F_HW_CSUM | 133 NETIF_F_HW_VLAN_CTAG_TX | 134 NETIF_F_HW_VLAN_CTAG_RX; 135 136 if (si->num_rss) 137 ndev->hw_features |= NETIF_F_RXHASH; 138 139 if (si->errata & ENETC_ERR_TXCSUM) { 140 ndev->hw_features &= ~NETIF_F_HW_CSUM; 141 ndev->features &= ~NETIF_F_HW_CSUM; 142 } 143 144 /* pick up primary MAC address from SI */ 145 enetc_get_primary_mac_addr(&si->hw, ndev->dev_addr); 146 } 147 148 static int enetc_vf_probe(struct pci_dev *pdev, 149 const struct pci_device_id *ent) 150 { 151 struct enetc_ndev_priv *priv; 152 struct net_device *ndev; 153 struct enetc_si *si; 154 int err; 155 156 err = enetc_pci_probe(pdev, KBUILD_MODNAME, 0); 157 if (err) { 158 dev_err(&pdev->dev, "PCI probing failed\n"); 159 return err; 160 } 161 162 si = pci_get_drvdata(pdev); 163 164 enetc_get_si_caps(si); 165 166 ndev = alloc_etherdev_mq(sizeof(*priv), ENETC_MAX_NUM_TXQS); 167 if (!ndev) { 168 err = -ENOMEM; 169 dev_err(&pdev->dev, "netdev creation failed\n"); 170 goto err_alloc_netdev; 171 } 172 173 enetc_vf_netdev_setup(si, ndev, &enetc_ndev_ops); 174 175 priv = netdev_priv(ndev); 176 177 enetc_init_si_rings_params(priv); 178 179 err = enetc_alloc_si_resources(priv); 180 if (err) { 181 dev_err(&pdev->dev, "SI resource alloc failed\n"); 182 goto err_alloc_si_res; 183 } 184 185 err = enetc_alloc_msix(priv); 186 if (err) { 187 dev_err(&pdev->dev, "MSIX alloc failed\n"); 188 goto err_alloc_msix; 189 } 190 191 err = register_netdev(ndev); 192 if (err) 193 goto err_reg_netdev; 194 195 netif_carrier_off(ndev); 196 197 return 0; 198 199 err_reg_netdev: 200 enetc_free_msix(priv); 201 err_alloc_msix: 202 enetc_free_si_resources(priv); 203 err_alloc_si_res: 204 si->ndev = NULL; 205 free_netdev(ndev); 206 err_alloc_netdev: 207 enetc_pci_remove(pdev); 208 209 return err; 210 } 211 212 static void enetc_vf_remove(struct pci_dev *pdev) 213 { 214 struct enetc_si *si = pci_get_drvdata(pdev); 215 struct enetc_ndev_priv *priv; 216 217 priv = netdev_priv(si->ndev); 218 unregister_netdev(si->ndev); 219 220 enetc_free_msix(priv); 221 222 enetc_free_si_resources(priv); 223 224 free_netdev(si->ndev); 225 226 enetc_pci_remove(pdev); 227 } 228 229 static const struct pci_device_id enetc_vf_id_table[] = { 230 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, ENETC_DEV_ID_VF) }, 231 { 0, } /* End of table. */ 232 }; 233 MODULE_DEVICE_TABLE(pci, enetc_vf_id_table); 234 235 static struct pci_driver enetc_vf_driver = { 236 .name = KBUILD_MODNAME, 237 .id_table = enetc_vf_id_table, 238 .probe = enetc_vf_probe, 239 .remove = enetc_vf_remove, 240 }; 241 module_pci_driver(enetc_vf_driver); 242 243 MODULE_DESCRIPTION(ENETC_DRV_NAME_STR); 244 MODULE_LICENSE("Dual BSD/GPL"); 245