xref: /openbmc/linux/drivers/net/ntb_netdev.c (revision 93df8a1e)
1 /*
2  * This file is provided under a dual BSD/GPLv2 license.  When using or
3  *   redistributing this file, you may do so under either license.
4  *
5  *   GPL LICENSE SUMMARY
6  *
7  *   Copyright(c) 2012 Intel Corporation. All rights reserved.
8  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of version 2 of the GNU General Public License as
12  *   published by the Free Software Foundation.
13  *
14  *   BSD LICENSE
15  *
16  *   Copyright(c) 2012 Intel Corporation. All rights reserved.
17  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
18  *
19  *   Redistribution and use in source and binary forms, with or without
20  *   modification, are permitted provided that the following conditions
21  *   are met:
22  *
23  *     * Redistributions of source code must retain the above copyright
24  *       notice, this list of conditions and the following disclaimer.
25  *     * Redistributions in binary form must reproduce the above copy
26  *       notice, this list of conditions and the following disclaimer in
27  *       the documentation and/or other materials provided with the
28  *       distribution.
29  *     * Neither the name of Intel Corporation nor the names of its
30  *       contributors may be used to endorse or promote products derived
31  *       from this software without specific prior written permission.
32  *
33  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  *
45  * PCIe NTB Network Linux driver
46  *
47  * Contact Information:
48  * Jon Mason <jon.mason@intel.com>
49  */
50 #include <linux/etherdevice.h>
51 #include <linux/ethtool.h>
52 #include <linux/module.h>
53 #include <linux/pci.h>
54 #include <linux/ntb.h>
55 #include <linux/ntb_transport.h>
56 
57 #define NTB_NETDEV_VER	"0.7"
58 
59 MODULE_DESCRIPTION(KBUILD_MODNAME);
60 MODULE_VERSION(NTB_NETDEV_VER);
61 MODULE_LICENSE("Dual BSD/GPL");
62 MODULE_AUTHOR("Intel Corporation");
63 
64 struct ntb_netdev {
65 	struct list_head list;
66 	struct pci_dev *pdev;
67 	struct net_device *ndev;
68 	struct ntb_transport_qp *qp;
69 };
70 
71 #define	NTB_TX_TIMEOUT_MS	1000
72 #define	NTB_RXQ_SIZE		100
73 
74 static LIST_HEAD(dev_list);
75 
76 static void ntb_netdev_event_handler(void *data, int link_is_up)
77 {
78 	struct net_device *ndev = data;
79 	struct ntb_netdev *dev = netdev_priv(ndev);
80 
81 	netdev_dbg(ndev, "Event %x, Link %x\n", link_is_up,
82 		   ntb_transport_link_query(dev->qp));
83 
84 	if (link_is_up) {
85 		if (ntb_transport_link_query(dev->qp))
86 			netif_carrier_on(ndev);
87 	} else {
88 		netif_carrier_off(ndev);
89 	}
90 }
91 
92 static void ntb_netdev_rx_handler(struct ntb_transport_qp *qp, void *qp_data,
93 				  void *data, int len)
94 {
95 	struct net_device *ndev = qp_data;
96 	struct sk_buff *skb;
97 	int rc;
98 
99 	skb = data;
100 	if (!skb)
101 		return;
102 
103 	netdev_dbg(ndev, "%s: %d byte payload received\n", __func__, len);
104 
105 	skb_put(skb, len);
106 	skb->protocol = eth_type_trans(skb, ndev);
107 	skb->ip_summed = CHECKSUM_NONE;
108 
109 	if (netif_rx(skb) == NET_RX_DROP) {
110 		ndev->stats.rx_errors++;
111 		ndev->stats.rx_dropped++;
112 	} else {
113 		ndev->stats.rx_packets++;
114 		ndev->stats.rx_bytes += len;
115 	}
116 
117 	skb = netdev_alloc_skb(ndev, ndev->mtu + ETH_HLEN);
118 	if (!skb) {
119 		ndev->stats.rx_errors++;
120 		ndev->stats.rx_frame_errors++;
121 		return;
122 	}
123 
124 	rc = ntb_transport_rx_enqueue(qp, skb, skb->data, ndev->mtu + ETH_HLEN);
125 	if (rc) {
126 		dev_kfree_skb(skb);
127 		ndev->stats.rx_errors++;
128 		ndev->stats.rx_fifo_errors++;
129 	}
130 }
131 
132 static void ntb_netdev_tx_handler(struct ntb_transport_qp *qp, void *qp_data,
133 				  void *data, int len)
134 {
135 	struct net_device *ndev = qp_data;
136 	struct sk_buff *skb;
137 
138 	skb = data;
139 	if (!skb || !ndev)
140 		return;
141 
142 	if (len > 0) {
143 		ndev->stats.tx_packets++;
144 		ndev->stats.tx_bytes += skb->len;
145 	} else {
146 		ndev->stats.tx_errors++;
147 		ndev->stats.tx_aborted_errors++;
148 	}
149 
150 	dev_kfree_skb(skb);
151 }
152 
153 static netdev_tx_t ntb_netdev_start_xmit(struct sk_buff *skb,
154 					 struct net_device *ndev)
155 {
156 	struct ntb_netdev *dev = netdev_priv(ndev);
157 	int rc;
158 
159 	rc = ntb_transport_tx_enqueue(dev->qp, skb, skb->data, skb->len);
160 	if (rc)
161 		goto err;
162 
163 	return NETDEV_TX_OK;
164 
165 err:
166 	ndev->stats.tx_dropped++;
167 	ndev->stats.tx_errors++;
168 	return NETDEV_TX_BUSY;
169 }
170 
171 static int ntb_netdev_open(struct net_device *ndev)
172 {
173 	struct ntb_netdev *dev = netdev_priv(ndev);
174 	struct sk_buff *skb;
175 	int rc, i, len;
176 
177 	/* Add some empty rx bufs */
178 	for (i = 0; i < NTB_RXQ_SIZE; i++) {
179 		skb = netdev_alloc_skb(ndev, ndev->mtu + ETH_HLEN);
180 		if (!skb) {
181 			rc = -ENOMEM;
182 			goto err;
183 		}
184 
185 		rc = ntb_transport_rx_enqueue(dev->qp, skb, skb->data,
186 					      ndev->mtu + ETH_HLEN);
187 		if (rc == -EINVAL) {
188 			dev_kfree_skb(skb);
189 			goto err;
190 		}
191 	}
192 
193 	netif_carrier_off(ndev);
194 	ntb_transport_link_up(dev->qp);
195 
196 	return 0;
197 
198 err:
199 	while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
200 		dev_kfree_skb(skb);
201 	return rc;
202 }
203 
204 static int ntb_netdev_close(struct net_device *ndev)
205 {
206 	struct ntb_netdev *dev = netdev_priv(ndev);
207 	struct sk_buff *skb;
208 	int len;
209 
210 	ntb_transport_link_down(dev->qp);
211 
212 	while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
213 		dev_kfree_skb(skb);
214 
215 	return 0;
216 }
217 
218 static int ntb_netdev_change_mtu(struct net_device *ndev, int new_mtu)
219 {
220 	struct ntb_netdev *dev = netdev_priv(ndev);
221 	struct sk_buff *skb;
222 	int len, rc;
223 
224 	if (new_mtu > ntb_transport_max_size(dev->qp) - ETH_HLEN)
225 		return -EINVAL;
226 
227 	if (!netif_running(ndev)) {
228 		ndev->mtu = new_mtu;
229 		return 0;
230 	}
231 
232 	/* Bring down the link and dispose of posted rx entries */
233 	ntb_transport_link_down(dev->qp);
234 
235 	if (ndev->mtu < new_mtu) {
236 		int i;
237 
238 		for (i = 0; (skb = ntb_transport_rx_remove(dev->qp, &len)); i++)
239 			dev_kfree_skb(skb);
240 
241 		for (; i; i--) {
242 			skb = netdev_alloc_skb(ndev, new_mtu + ETH_HLEN);
243 			if (!skb) {
244 				rc = -ENOMEM;
245 				goto err;
246 			}
247 
248 			rc = ntb_transport_rx_enqueue(dev->qp, skb, skb->data,
249 						      new_mtu + ETH_HLEN);
250 			if (rc) {
251 				dev_kfree_skb(skb);
252 				goto err;
253 			}
254 		}
255 	}
256 
257 	ndev->mtu = new_mtu;
258 
259 	ntb_transport_link_up(dev->qp);
260 
261 	return 0;
262 
263 err:
264 	ntb_transport_link_down(dev->qp);
265 
266 	while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
267 		dev_kfree_skb(skb);
268 
269 	netdev_err(ndev, "Error changing MTU, device inoperable\n");
270 	return rc;
271 }
272 
273 static const struct net_device_ops ntb_netdev_ops = {
274 	.ndo_open = ntb_netdev_open,
275 	.ndo_stop = ntb_netdev_close,
276 	.ndo_start_xmit = ntb_netdev_start_xmit,
277 	.ndo_change_mtu = ntb_netdev_change_mtu,
278 	.ndo_set_mac_address = eth_mac_addr,
279 };
280 
281 static void ntb_get_drvinfo(struct net_device *ndev,
282 			    struct ethtool_drvinfo *info)
283 {
284 	struct ntb_netdev *dev = netdev_priv(ndev);
285 
286 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
287 	strlcpy(info->version, NTB_NETDEV_VER, sizeof(info->version));
288 	strlcpy(info->bus_info, pci_name(dev->pdev), sizeof(info->bus_info));
289 }
290 
291 static int ntb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
292 {
293 	cmd->supported = SUPPORTED_Backplane;
294 	cmd->advertising = ADVERTISED_Backplane;
295 	ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
296 	cmd->duplex = DUPLEX_FULL;
297 	cmd->port = PORT_OTHER;
298 	cmd->phy_address = 0;
299 	cmd->transceiver = XCVR_DUMMY1;
300 	cmd->autoneg = AUTONEG_ENABLE;
301 	cmd->maxtxpkt = 0;
302 	cmd->maxrxpkt = 0;
303 
304 	return 0;
305 }
306 
307 static const struct ethtool_ops ntb_ethtool_ops = {
308 	.get_drvinfo = ntb_get_drvinfo,
309 	.get_link = ethtool_op_get_link,
310 	.get_settings = ntb_get_settings,
311 };
312 
313 static const struct ntb_queue_handlers ntb_netdev_handlers = {
314 	.tx_handler = ntb_netdev_tx_handler,
315 	.rx_handler = ntb_netdev_rx_handler,
316 	.event_handler = ntb_netdev_event_handler,
317 };
318 
319 static int ntb_netdev_probe(struct device *client_dev)
320 {
321 	struct ntb_dev *ntb;
322 	struct net_device *ndev;
323 	struct pci_dev *pdev;
324 	struct ntb_netdev *dev;
325 	int rc;
326 
327 	ntb = dev_ntb(client_dev->parent);
328 	pdev = ntb->pdev;
329 	if (!pdev)
330 		return -ENODEV;
331 
332 	ndev = alloc_etherdev(sizeof(*dev));
333 	if (!ndev)
334 		return -ENOMEM;
335 
336 	dev = netdev_priv(ndev);
337 	dev->ndev = ndev;
338 	dev->pdev = pdev;
339 	ndev->features = NETIF_F_HIGHDMA;
340 
341 	ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
342 
343 	ndev->hw_features = ndev->features;
344 	ndev->watchdog_timeo = msecs_to_jiffies(NTB_TX_TIMEOUT_MS);
345 
346 	random_ether_addr(ndev->perm_addr);
347 	memcpy(ndev->dev_addr, ndev->perm_addr, ndev->addr_len);
348 
349 	ndev->netdev_ops = &ntb_netdev_ops;
350 	ndev->ethtool_ops = &ntb_ethtool_ops;
351 
352 	dev->qp = ntb_transport_create_queue(ndev, client_dev,
353 					     &ntb_netdev_handlers);
354 	if (!dev->qp) {
355 		rc = -EIO;
356 		goto err;
357 	}
358 
359 	ndev->mtu = ntb_transport_max_size(dev->qp) - ETH_HLEN;
360 
361 	rc = register_netdev(ndev);
362 	if (rc)
363 		goto err1;
364 
365 	list_add(&dev->list, &dev_list);
366 	dev_info(&pdev->dev, "%s created\n", ndev->name);
367 	return 0;
368 
369 err1:
370 	ntb_transport_free_queue(dev->qp);
371 err:
372 	free_netdev(ndev);
373 	return rc;
374 }
375 
376 static void ntb_netdev_remove(struct device *client_dev)
377 {
378 	struct ntb_dev *ntb;
379 	struct net_device *ndev;
380 	struct pci_dev *pdev;
381 	struct ntb_netdev *dev;
382 	bool found = false;
383 
384 	ntb = dev_ntb(client_dev->parent);
385 	pdev = ntb->pdev;
386 
387 	list_for_each_entry(dev, &dev_list, list) {
388 		if (dev->pdev == pdev) {
389 			found = true;
390 			break;
391 		}
392 	}
393 	if (!found)
394 		return;
395 
396 	list_del(&dev->list);
397 
398 	ndev = dev->ndev;
399 
400 	unregister_netdev(ndev);
401 	ntb_transport_free_queue(dev->qp);
402 	free_netdev(ndev);
403 }
404 
405 static struct ntb_transport_client ntb_netdev_client = {
406 	.driver.name = KBUILD_MODNAME,
407 	.driver.owner = THIS_MODULE,
408 	.probe = ntb_netdev_probe,
409 	.remove = ntb_netdev_remove,
410 };
411 
412 static int __init ntb_netdev_init_module(void)
413 {
414 	int rc;
415 
416 	rc = ntb_transport_register_client_dev(KBUILD_MODNAME);
417 	if (rc)
418 		return rc;
419 	return ntb_transport_register_client(&ntb_netdev_client);
420 }
421 module_init(ntb_netdev_init_module);
422 
423 static void __exit ntb_netdev_exit_module(void)
424 {
425 	ntb_transport_unregister_client(&ntb_netdev_client);
426 	ntb_transport_unregister_client_dev(KBUILD_MODNAME);
427 }
428 module_exit(ntb_netdev_exit_module);
429