ntb_netdev: correct skb leak
[cascardo/linux.git] / drivers / net / ntb_netdev.c
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  *
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
54 #define NTB_NETDEV_VER  "0.6"
55
56 MODULE_DESCRIPTION(KBUILD_MODNAME);
57 MODULE_VERSION(NTB_NETDEV_VER);
58 MODULE_LICENSE("Dual BSD/GPL");
59 MODULE_AUTHOR("Intel Corporation");
60
61 struct 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
71 static LIST_HEAD(dev_list);
72
73 static 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
88 static 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) {
122                 dev_kfree_skb(skb);
123                 ndev->stats.rx_errors++;
124                 ndev->stats.rx_fifo_errors++;
125         }
126 }
127
128 static 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);
147
148         if (netif_queue_stopped(ndev))
149                 netif_wake_queue(ndev);
150 }
151
152 static netdev_tx_t ntb_netdev_start_xmit(struct sk_buff *skb,
153                                          struct net_device *ndev)
154 {
155         struct ntb_netdev *dev = netdev_priv(ndev);
156         int rc;
157
158         netdev_dbg(ndev, "ntb_transport_tx_enqueue\n");
159
160         rc = ntb_transport_tx_enqueue(dev->qp, skb, skb->data, skb->len);
161         if (rc)
162                 goto err;
163
164         return NETDEV_TX_OK;
165
166 err:
167         ndev->stats.tx_dropped++;
168         ndev->stats.tx_errors++;
169         netif_stop_queue(ndev);
170         return NETDEV_TX_BUSY;
171 }
172
173 static int ntb_netdev_open(struct net_device *ndev)
174 {
175         struct ntb_netdev *dev = netdev_priv(ndev);
176         struct sk_buff *skb;
177         int rc, i, len;
178
179         /* Add some empty rx bufs */
180         for (i = 0; i < NTB_RXQ_SIZE; i++) {
181                 skb = netdev_alloc_skb(ndev, ndev->mtu + ETH_HLEN);
182                 if (!skb) {
183                         rc = -ENOMEM;
184                         goto err;
185                 }
186
187                 rc = ntb_transport_rx_enqueue(dev->qp, skb, skb->data,
188                                               ndev->mtu + ETH_HLEN);
189                 if (rc == -EINVAL)
190                         goto err;
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 void ntb_netdev_tx_timeout(struct net_device *ndev)
274 {
275         if (netif_running(ndev))
276                 netif_wake_queue(ndev);
277 }
278
279 static const struct net_device_ops ntb_netdev_ops = {
280         .ndo_open = ntb_netdev_open,
281         .ndo_stop = ntb_netdev_close,
282         .ndo_start_xmit = ntb_netdev_start_xmit,
283         .ndo_change_mtu = ntb_netdev_change_mtu,
284         .ndo_tx_timeout = ntb_netdev_tx_timeout,
285         .ndo_set_mac_address = eth_mac_addr,
286 };
287
288 static 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
298 static int ntb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
299 {
300         cmd->supported = SUPPORTED_Backplane;
301         cmd->advertising = ADVERTISED_Backplane;
302         cmd->speed = SPEED_UNKNOWN;
303         ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
304         cmd->duplex = DUPLEX_FULL;
305         cmd->port = PORT_OTHER;
306         cmd->phy_address = 0;
307         cmd->transceiver = XCVR_DUMMY1;
308         cmd->autoneg = AUTONEG_ENABLE;
309         cmd->maxtxpkt = 0;
310         cmd->maxrxpkt = 0;
311
312         return 0;
313 }
314
315 static const struct ethtool_ops ntb_ethtool_ops = {
316         .get_drvinfo = ntb_get_drvinfo,
317         .get_link = ethtool_op_get_link,
318         .get_settings = ntb_get_settings,
319 };
320
321 static const struct ntb_queue_handlers ntb_netdev_handlers = {
322         .tx_handler = ntb_netdev_tx_handler,
323         .rx_handler = ntb_netdev_rx_handler,
324         .event_handler = ntb_netdev_event_handler,
325 };
326
327 static int ntb_netdev_probe(struct pci_dev *pdev)
328 {
329         struct net_device *ndev;
330         struct ntb_netdev *dev;
331         int rc;
332
333         ndev = alloc_etherdev(sizeof(struct ntb_netdev));
334         if (!ndev)
335                 return -ENOMEM;
336
337         dev = netdev_priv(ndev);
338         dev->ndev = ndev;
339         dev->pdev = pdev;
340         BUG_ON(!dev->pdev);
341         ndev->features = NETIF_F_HIGHDMA;
342
343         ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
344
345         ndev->hw_features = ndev->features;
346         ndev->watchdog_timeo = msecs_to_jiffies(NTB_TX_TIMEOUT_MS);
347
348         random_ether_addr(ndev->perm_addr);
349         memcpy(ndev->dev_addr, ndev->perm_addr, ndev->addr_len);
350
351         ndev->netdev_ops = &ntb_netdev_ops;
352         SET_ETHTOOL_OPS(ndev, &ntb_ethtool_ops);
353
354         dev->qp = ntb_transport_create_queue(ndev, pdev, &ntb_netdev_handlers);
355         if (!dev->qp) {
356                 rc = -EIO;
357                 goto err;
358         }
359
360         ndev->mtu = ntb_transport_max_size(dev->qp) - ETH_HLEN;
361
362         rc = register_netdev(ndev);
363         if (rc)
364                 goto err1;
365
366         list_add(&dev->list, &dev_list);
367         pr_info("%s: %s created\n", KBUILD_MODNAME, ndev->name);
368         return 0;
369
370 err1:
371         ntb_transport_free_queue(dev->qp);
372 err:
373         free_netdev(ndev);
374         return rc;
375 }
376
377 static void ntb_netdev_remove(struct pci_dev *pdev)
378 {
379         struct net_device *ndev;
380         struct ntb_netdev *dev;
381
382         list_for_each_entry(dev, &dev_list, list) {
383                 if (dev->pdev == pdev)
384                         break;
385         }
386         if (dev == NULL)
387                 return;
388
389         ndev = dev->ndev;
390
391         unregister_netdev(ndev);
392         ntb_transport_free_queue(dev->qp);
393         free_netdev(ndev);
394 }
395
396 static struct ntb_client ntb_netdev_client = {
397         .driver.name = KBUILD_MODNAME,
398         .driver.owner = THIS_MODULE,
399         .probe = ntb_netdev_probe,
400         .remove = ntb_netdev_remove,
401 };
402
403 static int __init ntb_netdev_init_module(void)
404 {
405         int rc;
406
407         rc = ntb_register_client_dev(KBUILD_MODNAME);
408         if (rc)
409                 return rc;
410         return ntb_register_client(&ntb_netdev_client);
411 }
412 module_init(ntb_netdev_init_module);
413
414 static void __exit ntb_netdev_exit_module(void)
415 {
416         ntb_unregister_client(&ntb_netdev_client);
417         ntb_unregister_client_dev(KBUILD_MODNAME);
418         pr_info("%s: Driver removed\n", KBUILD_MODNAME);
419 }
420 module_exit(ntb_netdev_exit_module);