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