xref: /linux/drivers/net/ethernet/freescale/enetc/enetc_vf.c (revision fcc79e1714e8c2b8e216dc3149812edd37884eef)
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 	eth_hw_addr_set(ndev, saddr->sa_data);
91 
92 	return 0;
93 }
94 
95 static int enetc_vf_set_features(struct net_device *ndev,
96 				 netdev_features_t features)
97 {
98 	enetc_set_features(ndev, features);
99 
100 	return 0;
101 }
102 
103 static int enetc_vf_setup_tc(struct net_device *ndev, enum tc_setup_type type,
104 			     void *type_data)
105 {
106 	switch (type) {
107 	case TC_SETUP_QDISC_MQPRIO:
108 		return enetc_setup_tc_mqprio(ndev, type_data);
109 	default:
110 		return -EOPNOTSUPP;
111 	}
112 }
113 
114 /* Probing/ Init */
115 static const struct net_device_ops enetc_ndev_ops = {
116 	.ndo_open		= enetc_open,
117 	.ndo_stop		= enetc_close,
118 	.ndo_start_xmit		= enetc_xmit,
119 	.ndo_get_stats		= enetc_get_stats,
120 	.ndo_set_mac_address	= enetc_vf_set_mac_addr,
121 	.ndo_set_features	= enetc_vf_set_features,
122 	.ndo_eth_ioctl		= enetc_ioctl,
123 	.ndo_setup_tc		= enetc_vf_setup_tc,
124 };
125 
126 static void enetc_vf_netdev_setup(struct enetc_si *si, struct net_device *ndev,
127 				  const struct net_device_ops *ndev_ops)
128 {
129 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
130 
131 	SET_NETDEV_DEV(ndev, &si->pdev->dev);
132 	priv->ndev = ndev;
133 	priv->si = si;
134 	priv->dev = &si->pdev->dev;
135 	si->ndev = ndev;
136 
137 	priv->msg_enable = (NETIF_MSG_IFUP << 1) - 1;
138 	priv->sysclk_freq = si->drvdata->sysclk_freq;
139 	ndev->netdev_ops = ndev_ops;
140 	enetc_set_ethtool_ops(ndev);
141 	ndev->watchdog_timeo = 5 * HZ;
142 	ndev->max_mtu = ENETC_MAX_MTU;
143 
144 	ndev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
145 			    NETIF_F_HW_VLAN_CTAG_TX |
146 			    NETIF_F_HW_VLAN_CTAG_RX |
147 			    NETIF_F_HW_CSUM | NETIF_F_TSO | NETIF_F_TSO6;
148 	ndev->features = NETIF_F_HIGHDMA | NETIF_F_SG | NETIF_F_RXCSUM |
149 			 NETIF_F_HW_VLAN_CTAG_TX |
150 			 NETIF_F_HW_VLAN_CTAG_RX |
151 			 NETIF_F_HW_CSUM | NETIF_F_TSO | NETIF_F_TSO6;
152 	ndev->vlan_features = NETIF_F_SG | NETIF_F_HW_CSUM |
153 			      NETIF_F_TSO | NETIF_F_TSO6;
154 
155 	if (si->num_rss)
156 		ndev->hw_features |= NETIF_F_RXHASH;
157 
158 	/* pick up primary MAC address from SI */
159 	enetc_load_primary_mac_addr(&si->hw, ndev);
160 }
161 
162 static int enetc_vf_probe(struct pci_dev *pdev,
163 			  const struct pci_device_id *ent)
164 {
165 	struct enetc_ndev_priv *priv;
166 	struct net_device *ndev;
167 	struct enetc_si *si;
168 	int err;
169 
170 	err = enetc_pci_probe(pdev, KBUILD_MODNAME, 0);
171 	if (err)
172 		return dev_err_probe(&pdev->dev, err, "PCI probing failed\n");
173 
174 	si = pci_get_drvdata(pdev);
175 	si->revision = ENETC_REV_1_0;
176 	err = enetc_get_driver_data(si);
177 	if (err) {
178 		dev_err_probe(&pdev->dev, err,
179 			      "Could not get VF driver data\n");
180 		goto err_alloc_netdev;
181 	}
182 
183 	enetc_get_si_caps(si);
184 
185 	ndev = alloc_etherdev_mq(sizeof(*priv), ENETC_MAX_NUM_TXQS);
186 	if (!ndev) {
187 		err = -ENOMEM;
188 		dev_err(&pdev->dev, "netdev creation failed\n");
189 		goto err_alloc_netdev;
190 	}
191 
192 	enetc_vf_netdev_setup(si, ndev, &enetc_ndev_ops);
193 
194 	priv = netdev_priv(ndev);
195 
196 	enetc_init_si_rings_params(priv);
197 
198 	err = enetc_setup_cbdr(priv->dev, &si->hw, ENETC_CBDR_DEFAULT_SIZE,
199 			       &si->cbd_ring);
200 	if (err)
201 		goto err_setup_cbdr;
202 
203 	err = enetc_alloc_si_resources(priv);
204 	if (err) {
205 		dev_err(&pdev->dev, "SI resource alloc failed\n");
206 		goto err_alloc_si_res;
207 	}
208 
209 	err = enetc_configure_si(priv);
210 	if (err) {
211 		dev_err(&pdev->dev, "Failed to configure SI\n");
212 		goto err_config_si;
213 	}
214 
215 	err = enetc_alloc_msix(priv);
216 	if (err) {
217 		dev_err(&pdev->dev, "MSIX alloc failed\n");
218 		goto err_alloc_msix;
219 	}
220 
221 	err = register_netdev(ndev);
222 	if (err)
223 		goto err_reg_netdev;
224 
225 	netif_carrier_off(ndev);
226 
227 	return 0;
228 
229 err_reg_netdev:
230 	enetc_free_msix(priv);
231 err_config_si:
232 err_alloc_msix:
233 	enetc_free_si_resources(priv);
234 err_alloc_si_res:
235 	enetc_teardown_cbdr(&si->cbd_ring);
236 err_setup_cbdr:
237 	si->ndev = NULL;
238 	free_netdev(ndev);
239 err_alloc_netdev:
240 	enetc_pci_remove(pdev);
241 
242 	return err;
243 }
244 
245 static void enetc_vf_remove(struct pci_dev *pdev)
246 {
247 	struct enetc_si *si = pci_get_drvdata(pdev);
248 	struct enetc_ndev_priv *priv;
249 
250 	priv = netdev_priv(si->ndev);
251 	unregister_netdev(si->ndev);
252 
253 	enetc_free_msix(priv);
254 
255 	enetc_free_si_resources(priv);
256 	enetc_teardown_cbdr(&si->cbd_ring);
257 
258 	free_netdev(si->ndev);
259 
260 	enetc_pci_remove(pdev);
261 }
262 
263 static const struct pci_device_id enetc_vf_id_table[] = {
264 	{ PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, ENETC_DEV_ID_VF) },
265 	{ 0, } /* End of table. */
266 };
267 MODULE_DEVICE_TABLE(pci, enetc_vf_id_table);
268 
269 static struct pci_driver enetc_vf_driver = {
270 	.name = KBUILD_MODNAME,
271 	.id_table = enetc_vf_id_table,
272 	.probe = enetc_vf_probe,
273 	.remove = enetc_vf_remove,
274 };
275 module_pci_driver(enetc_vf_driver);
276 
277 MODULE_DESCRIPTION(ENETC_DRV_NAME_STR);
278 MODULE_LICENSE("Dual BSD/GPL");
279