blob: d8757bf9ad755ed6a3114d9d0a9664e59618744a [file] [log] [blame]
Jon Mason548c2372012-11-16 19:27:13 -07001/*
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.
Allen Hubbee26a5842015-04-09 10:33:20 -04008 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
Jon Mason548c2372012-11-16 19:27:13 -07009 *
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.
Allen Hubbee26a5842015-04-09 10:33:20 -040017 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
Jon Mason548c2372012-11-16 19:27:13 -070018 *
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 *
Allen Hubbee26a5842015-04-09 10:33:20 -040045 * PCIe NTB Network Linux driver
Jon Mason548c2372012-11-16 19:27:13 -070046 *
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>
Allen Hubbee26a5842015-04-09 10:33:20 -040054#include <linux/ntb.h>
Allen Hubbeec110bc2015-05-07 06:45:21 -040055#include <linux/ntb_transport.h>
Jon Mason548c2372012-11-16 19:27:13 -070056
Jon Mason24208bb2013-01-19 02:02:35 -070057#define NTB_NETDEV_VER "0.7"
Jon Mason548c2372012-11-16 19:27:13 -070058
59MODULE_DESCRIPTION(KBUILD_MODNAME);
60MODULE_VERSION(NTB_NETDEV_VER);
61MODULE_LICENSE("Dual BSD/GPL");
62MODULE_AUTHOR("Intel Corporation");
63
64struct 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
74static LIST_HEAD(dev_list);
75
Allen Hubbee26a5842015-04-09 10:33:20 -040076static void ntb_netdev_event_handler(void *data, int link_is_up)
Jon Mason548c2372012-11-16 19:27:13 -070077{
78 struct net_device *ndev = data;
79 struct ntb_netdev *dev = netdev_priv(ndev);
80
Allen Hubbee26a5842015-04-09 10:33:20 -040081 netdev_dbg(ndev, "Event %x, Link %x\n", link_is_up,
Jon Mason548c2372012-11-16 19:27:13 -070082 ntb_transport_link_query(dev->qp));
83
Allen Hubbee26a5842015-04-09 10:33:20 -040084 if (link_is_up) {
85 if (ntb_transport_link_query(dev->qp))
86 netif_carrier_on(ndev);
87 } else {
Jon Mason548c2372012-11-16 19:27:13 -070088 netif_carrier_off(ndev);
Jon Mason403c63c2013-07-29 16:31:18 -070089 }
Jon Mason548c2372012-11-16 19:27:13 -070090}
91
92static 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
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400105 if (len < 0) {
106 ndev->stats.rx_errors++;
107 ndev->stats.rx_length_errors++;
108 goto enqueue_again;
109 }
110
Jon Mason548c2372012-11-16 19:27:13 -0700111 skb_put(skb, len);
112 skb->protocol = eth_type_trans(skb, ndev);
113 skb->ip_summed = CHECKSUM_NONE;
114
115 if (netif_rx(skb) == NET_RX_DROP) {
116 ndev->stats.rx_errors++;
117 ndev->stats.rx_dropped++;
118 } else {
119 ndev->stats.rx_packets++;
120 ndev->stats.rx_bytes += len;
121 }
122
123 skb = netdev_alloc_skb(ndev, ndev->mtu + ETH_HLEN);
124 if (!skb) {
125 ndev->stats.rx_errors++;
126 ndev->stats.rx_frame_errors++;
127 return;
128 }
129
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400130enqueue_again:
Jon Mason548c2372012-11-16 19:27:13 -0700131 rc = ntb_transport_rx_enqueue(qp, skb, skb->data, ndev->mtu + ETH_HLEN);
132 if (rc) {
Jon Mason765ccc72013-01-19 02:02:31 -0700133 dev_kfree_skb(skb);
Jon Mason548c2372012-11-16 19:27:13 -0700134 ndev->stats.rx_errors++;
135 ndev->stats.rx_fifo_errors++;
136 }
137}
138
139static void ntb_netdev_tx_handler(struct ntb_transport_qp *qp, void *qp_data,
140 void *data, int len)
141{
142 struct net_device *ndev = qp_data;
143 struct sk_buff *skb;
144
145 skb = data;
146 if (!skb || !ndev)
147 return;
148
149 if (len > 0) {
150 ndev->stats.tx_packets++;
151 ndev->stats.tx_bytes += skb->len;
152 } else {
153 ndev->stats.tx_errors++;
154 ndev->stats.tx_aborted_errors++;
155 }
156
157 dev_kfree_skb(skb);
Jon Mason548c2372012-11-16 19:27:13 -0700158}
159
160static netdev_tx_t ntb_netdev_start_xmit(struct sk_buff *skb,
161 struct net_device *ndev)
162{
163 struct ntb_netdev *dev = netdev_priv(ndev);
164 int rc;
165
Jon Mason548c2372012-11-16 19:27:13 -0700166 rc = ntb_transport_tx_enqueue(dev->qp, skb, skb->data, skb->len);
167 if (rc)
168 goto err;
169
170 return NETDEV_TX_OK;
171
172err:
173 ndev->stats.tx_dropped++;
174 ndev->stats.tx_errors++;
Jon Mason548c2372012-11-16 19:27:13 -0700175 return NETDEV_TX_BUSY;
176}
177
178static int ntb_netdev_open(struct net_device *ndev)
179{
180 struct ntb_netdev *dev = netdev_priv(ndev);
181 struct sk_buff *skb;
182 int rc, i, len;
183
184 /* Add some empty rx bufs */
185 for (i = 0; i < NTB_RXQ_SIZE; i++) {
186 skb = netdev_alloc_skb(ndev, ndev->mtu + ETH_HLEN);
187 if (!skb) {
188 rc = -ENOMEM;
189 goto err;
190 }
191
192 rc = ntb_transport_rx_enqueue(dev->qp, skb, skb->data,
193 ndev->mtu + ETH_HLEN);
Dave Jiangda4eb272015-07-13 08:07:10 -0400194 if (rc) {
Jon Masone8bc2ebd2013-11-22 16:50:57 -0700195 dev_kfree_skb(skb);
Jon Mason548c2372012-11-16 19:27:13 -0700196 goto err;
Jon Masone8bc2ebd2013-11-22 16:50:57 -0700197 }
Jon Mason548c2372012-11-16 19:27:13 -0700198 }
199
200 netif_carrier_off(ndev);
201 ntb_transport_link_up(dev->qp);
202
203 return 0;
204
205err:
206 while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
207 dev_kfree_skb(skb);
208 return rc;
209}
210
211static int ntb_netdev_close(struct net_device *ndev)
212{
213 struct ntb_netdev *dev = netdev_priv(ndev);
214 struct sk_buff *skb;
215 int len;
216
217 ntb_transport_link_down(dev->qp);
218
219 while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
220 dev_kfree_skb(skb);
221
222 return 0;
223}
224
225static int ntb_netdev_change_mtu(struct net_device *ndev, int new_mtu)
226{
227 struct ntb_netdev *dev = netdev_priv(ndev);
228 struct sk_buff *skb;
229 int len, rc;
230
231 if (new_mtu > ntb_transport_max_size(dev->qp) - ETH_HLEN)
232 return -EINVAL;
233
234 if (!netif_running(ndev)) {
235 ndev->mtu = new_mtu;
236 return 0;
237 }
238
239 /* Bring down the link and dispose of posted rx entries */
240 ntb_transport_link_down(dev->qp);
241
242 if (ndev->mtu < new_mtu) {
243 int i;
244
245 for (i = 0; (skb = ntb_transport_rx_remove(dev->qp, &len)); i++)
246 dev_kfree_skb(skb);
247
248 for (; i; i--) {
249 skb = netdev_alloc_skb(ndev, new_mtu + ETH_HLEN);
250 if (!skb) {
251 rc = -ENOMEM;
252 goto err;
253 }
254
255 rc = ntb_transport_rx_enqueue(dev->qp, skb, skb->data,
256 new_mtu + ETH_HLEN);
257 if (rc) {
258 dev_kfree_skb(skb);
259 goto err;
260 }
261 }
262 }
263
264 ndev->mtu = new_mtu;
265
266 ntb_transport_link_up(dev->qp);
267
268 return 0;
269
270err:
271 ntb_transport_link_down(dev->qp);
272
273 while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
274 dev_kfree_skb(skb);
275
276 netdev_err(ndev, "Error changing MTU, device inoperable\n");
277 return rc;
278}
279
Jon Mason548c2372012-11-16 19:27:13 -0700280static const struct net_device_ops ntb_netdev_ops = {
281 .ndo_open = ntb_netdev_open,
282 .ndo_stop = ntb_netdev_close,
283 .ndo_start_xmit = ntb_netdev_start_xmit,
284 .ndo_change_mtu = ntb_netdev_change_mtu,
Jon Mason548c2372012-11-16 19:27:13 -0700285 .ndo_set_mac_address = eth_mac_addr,
286};
287
288static void ntb_get_drvinfo(struct net_device *ndev,
289 struct ethtool_drvinfo *info)
290{
291 struct ntb_netdev *dev = netdev_priv(ndev);
292
293 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
294 strlcpy(info->version, NTB_NETDEV_VER, sizeof(info->version));
295 strlcpy(info->bus_info, pci_name(dev->pdev), sizeof(info->bus_info));
296}
297
298static int ntb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
299{
300 cmd->supported = SUPPORTED_Backplane;
301 cmd->advertising = ADVERTISED_Backplane;
Jon Mason548c2372012-11-16 19:27:13 -0700302 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
303 cmd->duplex = DUPLEX_FULL;
304 cmd->port = PORT_OTHER;
305 cmd->phy_address = 0;
306 cmd->transceiver = XCVR_DUMMY1;
307 cmd->autoneg = AUTONEG_ENABLE;
308 cmd->maxtxpkt = 0;
309 cmd->maxrxpkt = 0;
310
311 return 0;
312}
313
314static const struct ethtool_ops ntb_ethtool_ops = {
315 .get_drvinfo = ntb_get_drvinfo,
316 .get_link = ethtool_op_get_link,
317 .get_settings = ntb_get_settings,
318};
319
320static const struct ntb_queue_handlers ntb_netdev_handlers = {
321 .tx_handler = ntb_netdev_tx_handler,
322 .rx_handler = ntb_netdev_rx_handler,
323 .event_handler = ntb_netdev_event_handler,
324};
325
Allen Hubbee26a5842015-04-09 10:33:20 -0400326static int ntb_netdev_probe(struct device *client_dev)
Jon Mason548c2372012-11-16 19:27:13 -0700327{
Allen Hubbee26a5842015-04-09 10:33:20 -0400328 struct ntb_dev *ntb;
Jon Mason548c2372012-11-16 19:27:13 -0700329 struct net_device *ndev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400330 struct pci_dev *pdev;
Jon Mason548c2372012-11-16 19:27:13 -0700331 struct ntb_netdev *dev;
332 int rc;
333
Allen Hubbee26a5842015-04-09 10:33:20 -0400334 ntb = dev_ntb(client_dev->parent);
335 pdev = ntb->pdev;
336 if (!pdev)
337 return -ENODEV;
338
339 ndev = alloc_etherdev(sizeof(*dev));
Jon Mason548c2372012-11-16 19:27:13 -0700340 if (!ndev)
341 return -ENOMEM;
342
343 dev = netdev_priv(ndev);
344 dev->ndev = ndev;
345 dev->pdev = pdev;
Jon Mason548c2372012-11-16 19:27:13 -0700346 ndev->features = NETIF_F_HIGHDMA;
347
348 ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
349
350 ndev->hw_features = ndev->features;
351 ndev->watchdog_timeo = msecs_to_jiffies(NTB_TX_TIMEOUT_MS);
352
353 random_ether_addr(ndev->perm_addr);
354 memcpy(ndev->dev_addr, ndev->perm_addr, ndev->addr_len);
355
356 ndev->netdev_ops = &ntb_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000357 ndev->ethtool_ops = &ntb_ethtool_ops;
Jon Mason548c2372012-11-16 19:27:13 -0700358
Allen Hubbee26a5842015-04-09 10:33:20 -0400359 dev->qp = ntb_transport_create_queue(ndev, client_dev,
360 &ntb_netdev_handlers);
Jon Mason548c2372012-11-16 19:27:13 -0700361 if (!dev->qp) {
362 rc = -EIO;
363 goto err;
364 }
365
366 ndev->mtu = ntb_transport_max_size(dev->qp) - ETH_HLEN;
367
368 rc = register_netdev(ndev);
369 if (rc)
370 goto err1;
371
372 list_add(&dev->list, &dev_list);
Jon Mason7bcd2b12013-01-19 02:02:34 -0700373 dev_info(&pdev->dev, "%s created\n", ndev->name);
Jon Mason548c2372012-11-16 19:27:13 -0700374 return 0;
375
376err1:
377 ntb_transport_free_queue(dev->qp);
378err:
379 free_netdev(ndev);
380 return rc;
381}
382
Allen Hubbee26a5842015-04-09 10:33:20 -0400383static void ntb_netdev_remove(struct device *client_dev)
Jon Mason548c2372012-11-16 19:27:13 -0700384{
Allen Hubbee26a5842015-04-09 10:33:20 -0400385 struct ntb_dev *ntb;
Jon Mason548c2372012-11-16 19:27:13 -0700386 struct net_device *ndev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400387 struct pci_dev *pdev;
Jon Mason548c2372012-11-16 19:27:13 -0700388 struct ntb_netdev *dev;
Jon Mason53ca4fe2013-11-26 11:21:50 -0700389 bool found = false;
Jon Mason548c2372012-11-16 19:27:13 -0700390
Allen Hubbee26a5842015-04-09 10:33:20 -0400391 ntb = dev_ntb(client_dev->parent);
392 pdev = ntb->pdev;
393
Jon Mason548c2372012-11-16 19:27:13 -0700394 list_for_each_entry(dev, &dev_list, list) {
Jon Masonfea903e2013-11-22 16:44:13 -0700395 if (dev->pdev == pdev) {
396 found = true;
Jon Mason548c2372012-11-16 19:27:13 -0700397 break;
Jon Masonfea903e2013-11-22 16:44:13 -0700398 }
Jon Mason548c2372012-11-16 19:27:13 -0700399 }
Jon Masonfea903e2013-11-22 16:44:13 -0700400 if (!found)
Jon Mason548c2372012-11-16 19:27:13 -0700401 return;
402
Jon Mason904435c2013-04-18 13:36:43 -0700403 list_del(&dev->list);
404
Jon Mason548c2372012-11-16 19:27:13 -0700405 ndev = dev->ndev;
406
407 unregister_netdev(ndev);
408 ntb_transport_free_queue(dev->qp);
409 free_netdev(ndev);
410}
411
Allen Hubbee26a5842015-04-09 10:33:20 -0400412static struct ntb_transport_client ntb_netdev_client = {
Jon Mason548c2372012-11-16 19:27:13 -0700413 .driver.name = KBUILD_MODNAME,
414 .driver.owner = THIS_MODULE,
415 .probe = ntb_netdev_probe,
416 .remove = ntb_netdev_remove,
417};
418
419static int __init ntb_netdev_init_module(void)
420{
421 int rc;
422
Allen Hubbee26a5842015-04-09 10:33:20 -0400423 rc = ntb_transport_register_client_dev(KBUILD_MODNAME);
Jon Mason548c2372012-11-16 19:27:13 -0700424 if (rc)
425 return rc;
Allen Hubbeec110bc2015-05-07 06:45:21 -0400426 return ntb_transport_register_client(&ntb_netdev_client);
Jon Mason548c2372012-11-16 19:27:13 -0700427}
428module_init(ntb_netdev_init_module);
429
430static void __exit ntb_netdev_exit_module(void)
431{
Allen Hubbeec110bc2015-05-07 06:45:21 -0400432 ntb_transport_unregister_client(&ntb_netdev_client);
Allen Hubbee26a5842015-04-09 10:33:20 -0400433 ntb_transport_unregister_client_dev(KBUILD_MODNAME);
Jon Mason548c2372012-11-16 19:27:13 -0700434}
435module_exit(ntb_netdev_exit_module);