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