blob: 29bbb628d712b38e8b17529626b85c3532d7942f [file] [log] [blame]
Santiago Leonf148f612010-09-03 18:29:30 +00001/*
Santiago Leon9d348af2010-09-03 18:29:53 +00002 * IBM Power Virtual Ethernet Device Driver
Santiago Leonf148f612010-09-03 18:29:30 +00003 *
Santiago Leon9d348af2010-09-03 18:29:53 +00004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Santiago Leonf148f612010-09-03 18:29:30 +00008 *
Santiago Leon9d348af2010-09-03 18:29:53 +00009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Santiago Leonf148f612010-09-03 18:29:30 +000013 *
Santiago Leon9d348af2010-09-03 18:29:53 +000014 * You should have received a copy of the GNU General Public License
Jeff Kirsher0ab75ae2013-12-06 06:28:43 -080015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Santiago Leonf148f612010-09-03 18:29:30 +000016 *
Santiago Leon9d348af2010-09-03 18:29:53 +000017 * Copyright (C) IBM Corporation, 2003, 2010
18 *
19 * Authors: Dave Larson <larson1@us.ibm.com>
20 * Santiago Leon <santil@linux.vnet.ibm.com>
21 * Brian King <brking@linux.vnet.ibm.com>
22 * Robert Jennings <rcj@linux.vnet.ibm.com>
23 * Anton Blanchard <anton@au.ibm.com>
Santiago Leonf148f612010-09-03 18:29:30 +000024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
Robert Jennings1096d632008-07-24 04:34:52 +100027#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/types.h>
29#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/dma-mapping.h>
31#include <linux/kernel.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000036#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mm.h>
Brian Kinge7a3af52010-05-07 08:56:08 +000038#include <linux/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/ethtool.h>
Brian Kingf4ff2872007-09-15 13:36:07 -070040#include <linux/in.h>
41#include <linux/ip.h>
Santiago Leonab78df72010-09-03 18:28:52 +000042#include <linux/ipv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/hvcall.h>
Arun Sharma600634972011-07-26 16:09:06 -070045#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/vio.h>
Robert Jennings1096d632008-07-24 04:34:52 +100047#include <asm/iommu.h>
Robert Jennings1096d632008-07-24 04:34:52 +100048#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include "ibmveth.h"
51
David Howells7d12e782006-10-05 14:55:46 +010052static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
Michael Ellerman493a6842007-04-17 13:12:55 +100053static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
Robert Jennings1096d632008-07-24 04:34:52 +100054static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev);
Santiago Leone295fe82010-09-03 18:29:08 +000055
Santiago Leon860f2422006-04-25 11:19:59 -050056static struct kobj_type ktype_veth_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Robert Jennings1096d632008-07-24 04:34:52 +100058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static const char ibmveth_driver_name[] = "ibmveth";
Santiago Leon9d348af2010-09-03 18:29:53 +000060static const char ibmveth_driver_string[] = "IBM Power Virtual Ethernet Driver";
Thomas Falcon8641dd82015-04-29 16:25:45 -050061#define ibmveth_driver_version "1.05"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Santiago Leon9d348af2010-09-03 18:29:53 +000063MODULE_AUTHOR("Santiago Leon <santil@linux.vnet.ibm.com>");
64MODULE_DESCRIPTION("IBM Power Virtual Ethernet Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065MODULE_LICENSE("GPL");
66MODULE_VERSION(ibmveth_driver_version);
67
Santiago Leonc08cc3c2010-09-03 18:28:20 +000068static unsigned int tx_copybreak __read_mostly = 128;
69module_param(tx_copybreak, uint, 0644);
70MODULE_PARM_DESC(tx_copybreak,
71 "Maximum size of packet that is copied to a new buffer on transmit");
72
Santiago Leon8d86c612010-09-03 18:28:25 +000073static unsigned int rx_copybreak __read_mostly = 128;
74module_param(rx_copybreak, uint, 0644);
75MODULE_PARM_DESC(rx_copybreak,
76 "Maximum size of packet that is copied to a new buffer on receive");
77
Santiago Leon0c26b672010-09-03 18:28:41 +000078static unsigned int rx_flush __read_mostly = 0;
79module_param(rx_flush, uint, 0644);
80MODULE_PARM_DESC(rx_flush, "Flush receive buffers before use");
81
Brian Kingddbb4de2007-08-17 09:16:43 -050082struct ibmveth_stat {
83 char name[ETH_GSTRING_LEN];
84 int offset;
85};
86
87#define IBMVETH_STAT_OFF(stat) offsetof(struct ibmveth_adapter, stat)
88#define IBMVETH_GET_STAT(a, off) *((u64 *)(((unsigned long)(a)) + off))
89
90struct ibmveth_stat ibmveth_stats[] = {
91 { "replenish_task_cycles", IBMVETH_STAT_OFF(replenish_task_cycles) },
92 { "replenish_no_mem", IBMVETH_STAT_OFF(replenish_no_mem) },
Santiago Leonf148f612010-09-03 18:29:30 +000093 { "replenish_add_buff_failure",
94 IBMVETH_STAT_OFF(replenish_add_buff_failure) },
95 { "replenish_add_buff_success",
96 IBMVETH_STAT_OFF(replenish_add_buff_success) },
Brian Kingddbb4de2007-08-17 09:16:43 -050097 { "rx_invalid_buffer", IBMVETH_STAT_OFF(rx_invalid_buffer) },
98 { "rx_no_buffer", IBMVETH_STAT_OFF(rx_no_buffer) },
Brian Kingddbb4de2007-08-17 09:16:43 -050099 { "tx_map_failed", IBMVETH_STAT_OFF(tx_map_failed) },
100 { "tx_send_failed", IBMVETH_STAT_OFF(tx_send_failed) },
Santiago Leonab78df72010-09-03 18:28:52 +0000101 { "fw_enabled_ipv4_csum", IBMVETH_STAT_OFF(fw_ipv4_csum_support) },
102 { "fw_enabled_ipv6_csum", IBMVETH_STAT_OFF(fw_ipv6_csum_support) },
Thomas Falcon8641dd82015-04-29 16:25:45 -0500103 { "tx_large_packets", IBMVETH_STAT_OFF(tx_large_packets) },
Thomas Falcon9c7e8bc2015-04-29 16:25:47 -0500104 { "rx_large_packets", IBMVETH_STAT_OFF(rx_large_packets) }
Brian Kingddbb4de2007-08-17 09:16:43 -0500105};
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/* simple methods of getting data from the current rxq entry */
Brian King79ef4a42007-08-17 09:16:56 -0500108static inline u32 ibmveth_rxq_flags(struct ibmveth_adapter *adapter)
109{
Anton Blanchard0b536be2013-09-03 09:55:32 +1000110 return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].flags_off);
Brian King79ef4a42007-08-17 09:16:56 -0500111}
112
113static inline int ibmveth_rxq_toggle(struct ibmveth_adapter *adapter)
114{
Santiago Leonf148f612010-09-03 18:29:30 +0000115 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_TOGGLE) >>
116 IBMVETH_RXQ_TOGGLE_SHIFT;
Brian King79ef4a42007-08-17 09:16:56 -0500117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
120{
Santiago Leonf148f612010-09-03 18:29:30 +0000121 return ibmveth_rxq_toggle(adapter) == adapter->rx_queue.toggle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
125{
Santiago Leonf148f612010-09-03 18:29:30 +0000126 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
130{
Santiago Leonf148f612010-09-03 18:29:30 +0000131 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_OFF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
135{
Anton Blanchard0b536be2013-09-03 09:55:32 +1000136 return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Brian Kingf4ff2872007-09-15 13:36:07 -0700139static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
140{
Santiago Leonf148f612010-09-03 18:29:30 +0000141 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_CSUM_GOOD;
Brian Kingf4ff2872007-09-15 13:36:07 -0700142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* setup the initial settings for a buffer pool */
Santiago Leonf148f612010-09-03 18:29:30 +0000145static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool,
146 u32 pool_index, u32 pool_size,
147 u32 buff_size, u32 pool_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 pool->size = pool_size;
150 pool->index = pool_index;
151 pool->buff_size = buff_size;
Santiago Leonc033a6d2010-09-03 18:28:09 +0000152 pool->threshold = pool_size * 7 / 8;
Santiago Leon860f2422006-04-25 11:19:59 -0500153 pool->active = pool_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156/* allocate and setup an buffer pool - called during open */
157static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
158{
159 int i;
160
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400161 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Santiago Leonf148f612010-09-03 18:29:30 +0000163 if (!pool->free_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400166 pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
Santiago Leonf148f612010-09-03 18:29:30 +0000167 if (!pool->dma_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 kfree(pool->free_map);
169 pool->free_map = NULL;
170 return -1;
171 }
172
Julia Lawalla05abcb2010-05-13 10:06:01 +0000173 pool->skbuff = kcalloc(pool->size, sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Santiago Leonf148f612010-09-03 18:29:30 +0000175 if (!pool->skbuff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 kfree(pool->dma_addr);
177 pool->dma_addr = NULL;
178
179 kfree(pool->free_map);
180 pool->free_map = NULL;
181 return -1;
182 }
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
185
Santiago Leonf148f612010-09-03 18:29:30 +0000186 for (i = 0; i < pool->size; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 pool->free_map[i] = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 atomic_set(&pool->available, 0);
190 pool->producer_index = 0;
191 pool->consumer_index = 0;
192
193 return 0;
194}
195
Santiago Leon0c26b672010-09-03 18:28:41 +0000196static inline void ibmveth_flush_buffer(void *addr, unsigned long length)
197{
198 unsigned long offset;
199
200 for (offset = 0; offset < length; offset += SMP_CACHE_BYTES)
201 asm("dcbfl %0,%1" :: "b" (addr), "r" (offset));
202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/* replenish the buffers for a pool. note that we don't need to
205 * skb_reserve these since they are used for incoming...
206 */
Santiago Leonf148f612010-09-03 18:29:30 +0000207static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter,
208 struct ibmveth_buff_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 u32 i;
211 u32 count = pool->size - atomic_read(&pool->available);
212 u32 buffers_added = 0;
Robert Jennings1096d632008-07-24 04:34:52 +1000213 struct sk_buff *skb;
214 unsigned int free_index, index;
215 u64 correlator;
216 unsigned long lpar_rc;
217 dma_addr_t dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 mb();
220
Santiago Leonf148f612010-09-03 18:29:30 +0000221 for (i = 0; i < count; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 union ibmveth_buf_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Santiago Leon003212c2010-09-03 18:29:03 +0000224 skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Santiago Leonf148f612010-09-03 18:29:30 +0000226 if (!skb) {
Santiago Leonc43ced12010-09-03 18:29:14 +0000227 netdev_dbg(adapter->netdev,
228 "replenish: unable to allocate skb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 adapter->replenish_no_mem++;
230 break;
231 }
232
David Gibson047a66d2006-10-21 10:24:13 -0700233 free_index = pool->consumer_index;
Santiago Leona613f582010-09-03 18:28:04 +0000234 pool->consumer_index++;
235 if (pool->consumer_index >= pool->size)
236 pool->consumer_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 index = pool->free_map[free_index];
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400238
Santiago Leon64859112010-09-03 18:29:41 +0000239 BUG_ON(index == IBM_VETH_INVALID_MAP);
240 BUG_ON(pool->skbuff[index] != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
243 pool->buff_size, DMA_FROM_DEVICE);
244
Stephen Rothwellc713e7c2008-07-28 02:14:24 +1000245 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
Robert Jennings1096d632008-07-24 04:34:52 +1000246 goto failure;
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
249 pool->dma_addr[index] = dma_addr;
250 pool->skbuff[index] = skb;
251
252 correlator = ((u64)pool->index << 32) | index;
Santiago Leonf148f612010-09-03 18:29:30 +0000253 *(u64 *)skb->data = correlator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Brian King79ef4a42007-08-17 09:16:56 -0500255 desc.fields.flags_len = IBMVETH_BUF_VALID | pool->buff_size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400256 desc.fields.address = dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Santiago Leon0c26b672010-09-03 18:28:41 +0000258 if (rx_flush) {
259 unsigned int len = min(pool->buff_size,
260 adapter->netdev->mtu +
261 IBMVETH_BUFF_OH);
262 ibmveth_flush_buffer(skb->data, len);
263 }
Santiago Leonf148f612010-09-03 18:29:30 +0000264 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address,
265 desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400266
Santiago Leonf148f612010-09-03 18:29:30 +0000267 if (lpar_rc != H_SUCCESS) {
Robert Jennings1096d632008-07-24 04:34:52 +1000268 goto failure;
Santiago Leonf148f612010-09-03 18:29:30 +0000269 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 buffers_added++;
271 adapter->replenish_add_buff_success++;
272 }
273 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 mb();
276 atomic_add(buffers_added, &(pool->available));
Robert Jennings1096d632008-07-24 04:34:52 +1000277 return;
278
279failure:
280 pool->free_map[free_index] = index;
281 pool->skbuff[index] = NULL;
282 if (pool->consumer_index == 0)
283 pool->consumer_index = pool->size - 1;
284 else
285 pool->consumer_index--;
Stephen Rothwellc713e7c2008-07-28 02:14:24 +1000286 if (!dma_mapping_error(&adapter->vdev->dev, dma_addr))
Robert Jennings1096d632008-07-24 04:34:52 +1000287 dma_unmap_single(&adapter->vdev->dev,
288 pool->dma_addr[index], pool->buff_size,
289 DMA_FROM_DEVICE);
290 dev_kfree_skb_any(skb);
291 adapter->replenish_add_buff_failure++;
292
293 mb();
294 atomic_add(buffers_added, &(pool->available));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Anton Blanchardcbd52282014-08-22 11:36:52 +1000297/*
298 * The final 8 bytes of the buffer list is a counter of frames dropped
299 * because there was not a buffer in the buffer list capable of holding
300 * the frame.
301 */
302static void ibmveth_update_rx_no_buffer(struct ibmveth_adapter *adapter)
303{
304 __be64 *p = adapter->buffer_list_addr + 4096 - 8;
305
306 adapter->rx_no_buffer = be64_to_cpup(p);
307}
308
Santiago Leone2adbcb2005-10-26 10:47:08 -0600309/* replenish routine */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400310static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Santiago Leonb6d35182005-10-26 10:47:01 -0600312 int i;
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 adapter->replenish_task_cycles++;
315
Santiago Leon517e80e2010-09-03 18:29:25 +0000316 for (i = (IBMVETH_NUM_BUFF_POOLS - 1); i >= 0; i--) {
Santiago Leonc033a6d2010-09-03 18:28:09 +0000317 struct ibmveth_buff_pool *pool = &adapter->rx_buff_pool[i];
318
319 if (pool->active &&
320 (atomic_read(&pool->available) < pool->threshold))
321 ibmveth_replenish_buffer_pool(adapter, pool);
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Anton Blanchardcbd52282014-08-22 11:36:52 +1000324 ibmveth_update_rx_no_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327/* empty and free ana buffer pool - also used to do cleanup in error paths */
Santiago Leonf148f612010-09-03 18:29:30 +0000328static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter,
329 struct ibmveth_buff_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 int i;
332
Jesper Juhlb4558ea2005-10-28 16:53:13 -0400333 kfree(pool->free_map);
334 pool->free_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Santiago Leonf148f612010-09-03 18:29:30 +0000336 if (pool->skbuff && pool->dma_addr) {
337 for (i = 0; i < pool->size; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct sk_buff *skb = pool->skbuff[i];
Santiago Leonf148f612010-09-03 18:29:30 +0000339 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 dma_unmap_single(&adapter->vdev->dev,
341 pool->dma_addr[i],
342 pool->buff_size,
343 DMA_FROM_DEVICE);
344 dev_kfree_skb_any(skb);
345 pool->skbuff[i] = NULL;
346 }
347 }
348 }
349
Santiago Leonf148f612010-09-03 18:29:30 +0000350 if (pool->dma_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 kfree(pool->dma_addr);
352 pool->dma_addr = NULL;
353 }
354
Santiago Leonf148f612010-09-03 18:29:30 +0000355 if (pool->skbuff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 kfree(pool->skbuff);
357 pool->skbuff = NULL;
358 }
359}
360
361/* remove a buffer from a pool */
Santiago Leonf148f612010-09-03 18:29:30 +0000362static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter,
363 u64 correlator)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 unsigned int pool = correlator >> 32;
366 unsigned int index = correlator & 0xffffffffUL;
367 unsigned int free_index;
368 struct sk_buff *skb;
369
Santiago Leon64859112010-09-03 18:29:41 +0000370 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
371 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 skb = adapter->rx_buff_pool[pool].skbuff[index];
374
Santiago Leon64859112010-09-03 18:29:41 +0000375 BUG_ON(skb == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
378
379 dma_unmap_single(&adapter->vdev->dev,
380 adapter->rx_buff_pool[pool].dma_addr[index],
381 adapter->rx_buff_pool[pool].buff_size,
382 DMA_FROM_DEVICE);
383
David Gibson047a66d2006-10-21 10:24:13 -0700384 free_index = adapter->rx_buff_pool[pool].producer_index;
Santiago Leona613f582010-09-03 18:28:04 +0000385 adapter->rx_buff_pool[pool].producer_index++;
386 if (adapter->rx_buff_pool[pool].producer_index >=
387 adapter->rx_buff_pool[pool].size)
388 adapter->rx_buff_pool[pool].producer_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 adapter->rx_buff_pool[pool].free_map[free_index] = index;
390
391 mb();
392
393 atomic_dec(&(adapter->rx_buff_pool[pool].available));
394}
395
396/* get the current buffer on the rx queue */
397static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
398{
399 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
400 unsigned int pool = correlator >> 32;
401 unsigned int index = correlator & 0xffffffffUL;
402
Santiago Leon64859112010-09-03 18:29:41 +0000403 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
404 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 return adapter->rx_buff_pool[pool].skbuff[index];
407}
408
409/* recycle the current buffer on the rx queue */
David S. Miller8decf862011-09-22 03:23:13 -0400410static int ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 u32 q_index = adapter->rx_queue.index;
413 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
414 unsigned int pool = correlator >> 32;
415 unsigned int index = correlator & 0xffffffffUL;
416 union ibmveth_buf_desc desc;
417 unsigned long lpar_rc;
David S. Miller8decf862011-09-22 03:23:13 -0400418 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Santiago Leon64859112010-09-03 18:29:41 +0000420 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
421 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Santiago Leonf148f612010-09-03 18:29:30 +0000423 if (!adapter->rx_buff_pool[pool].active) {
Santiago Leonb6d35182005-10-26 10:47:01 -0600424 ibmveth_rxq_harvest_buffer(adapter);
425 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
David S. Miller8decf862011-09-22 03:23:13 -0400426 goto out;
Santiago Leonb6d35182005-10-26 10:47:01 -0600427 }
428
Brian King79ef4a42007-08-17 09:16:56 -0500429 desc.fields.flags_len = IBMVETH_BUF_VALID |
430 adapter->rx_buff_pool[pool].buff_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
432
433 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400434
Santiago Leonf148f612010-09-03 18:29:30 +0000435 if (lpar_rc != H_SUCCESS) {
Santiago Leonc43ced12010-09-03 18:29:14 +0000436 netdev_dbg(adapter->netdev, "h_add_logical_lan_buffer failed "
437 "during recycle rc=%ld", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
David S. Miller8decf862011-09-22 03:23:13 -0400439 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Santiago Leonf148f612010-09-03 18:29:30 +0000442 if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 adapter->rx_queue.index = 0;
444 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
445 }
David S. Miller8decf862011-09-22 03:23:13 -0400446
447out:
448 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Michael Ellerman493a6842007-04-17 13:12:55 +1000451static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
454
Santiago Leonf148f612010-09-03 18:29:30 +0000455 if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 adapter->rx_queue.index = 0;
457 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
458 }
459}
460
461static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
462{
Santiago Leonb6d35182005-10-26 10:47:01 -0600463 int i;
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700464 struct device *dev = &adapter->vdev->dev;
Santiago Leonb6d35182005-10-26 10:47:01 -0600465
Santiago Leonf148f612010-09-03 18:29:30 +0000466 if (adapter->buffer_list_addr != NULL) {
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700467 if (!dma_mapping_error(dev, adapter->buffer_list_dma)) {
468 dma_unmap_single(dev, adapter->buffer_list_dma, 4096,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 DMA_BIDIRECTIONAL);
470 adapter->buffer_list_dma = DMA_ERROR_CODE;
471 }
472 free_page((unsigned long)adapter->buffer_list_addr);
473 adapter->buffer_list_addr = NULL;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Santiago Leonf148f612010-09-03 18:29:30 +0000476 if (adapter->filter_list_addr != NULL) {
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700477 if (!dma_mapping_error(dev, adapter->filter_list_dma)) {
478 dma_unmap_single(dev, adapter->filter_list_dma, 4096,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 DMA_BIDIRECTIONAL);
480 adapter->filter_list_dma = DMA_ERROR_CODE;
481 }
482 free_page((unsigned long)adapter->filter_list_addr);
483 adapter->filter_list_addr = NULL;
484 }
485
Santiago Leonf148f612010-09-03 18:29:30 +0000486 if (adapter->rx_queue.queue_addr != NULL) {
Santiago Leond90c92f2012-09-04 14:41:37 +0000487 dma_free_coherent(dev, adapter->rx_queue.queue_len,
488 adapter->rx_queue.queue_addr,
489 adapter->rx_queue.queue_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 adapter->rx_queue.queue_addr = NULL;
491 }
492
Santiago Leonf148f612010-09-03 18:29:30 +0000493 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Santiago Leon860f2422006-04-25 11:19:59 -0500494 if (adapter->rx_buff_pool[i].active)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400495 ibmveth_free_buffer_pool(adapter,
Santiago Leon860f2422006-04-25 11:19:59 -0500496 &adapter->rx_buff_pool[i]);
Robert Jennings1096d632008-07-24 04:34:52 +1000497
498 if (adapter->bounce_buffer != NULL) {
Stephen Rothwellc713e7c2008-07-28 02:14:24 +1000499 if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
Robert Jennings1096d632008-07-24 04:34:52 +1000500 dma_unmap_single(&adapter->vdev->dev,
501 adapter->bounce_buffer_dma,
502 adapter->netdev->mtu + IBMVETH_BUFF_OH,
503 DMA_BIDIRECTIONAL);
504 adapter->bounce_buffer_dma = DMA_ERROR_CODE;
505 }
506 kfree(adapter->bounce_buffer);
507 adapter->bounce_buffer = NULL;
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500511static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
512 union ibmveth_buf_desc rxq_desc, u64 mac_address)
513{
514 int rc, try_again = 1;
515
Santiago Leonf148f612010-09-03 18:29:30 +0000516 /*
517 * After a kexec the adapter will still be open, so our attempt to
518 * open it will fail. So if we get a failure we free the adapter and
519 * try again, but only once.
520 */
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500521retry:
522 rc = h_register_logical_lan(adapter->vdev->unit_address,
523 adapter->buffer_list_dma, rxq_desc.desc,
524 adapter->filter_list_dma, mac_address);
525
526 if (rc != H_SUCCESS && try_again) {
527 do {
528 rc = h_free_logical_lan(adapter->vdev->unit_address);
529 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
530
531 try_again = 0;
532 goto retry;
533 }
534
535 return rc;
536}
537
Anton Blanchardd746ca92014-03-05 14:51:37 +1100538static u64 ibmveth_encode_mac_addr(u8 *mac)
539{
540 int i;
541 u64 encoded = 0;
542
543 for (i = 0; i < ETH_ALEN; i++)
544 encoded = (encoded << 8) | mac[i];
545
546 return encoded;
547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549static int ibmveth_open(struct net_device *netdev)
550{
Wang Chen4cf16532008-11-12 23:38:14 -0800551 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Anton Blanchardd746ca92014-03-05 14:51:37 +1100552 u64 mac_address;
Santiago Leonb6d35182005-10-26 10:47:01 -0600553 int rxq_entries = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 unsigned long lpar_rc;
555 int rc;
556 union ibmveth_buf_desc rxq_desc;
Santiago Leonb6d35182005-10-26 10:47:01 -0600557 int i;
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700558 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Santiago Leonc43ced12010-09-03 18:29:14 +0000560 netdev_dbg(netdev, "open starting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700562 napi_enable(&adapter->napi);
563
Santiago Leon517e80e2010-09-03 18:29:25 +0000564 for(i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Santiago Leonb6d35182005-10-26 10:47:01 -0600565 rxq_entries += adapter->rx_buff_pool[i].size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
568 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400569
Santiago Leonf148f612010-09-03 18:29:30 +0000570 if (!adapter->buffer_list_addr || !adapter->filter_list_addr) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000571 netdev_err(netdev, "unable to allocate filter or buffer list "
572 "pages\n");
Denis Kirjanov88426f22010-10-20 04:21:13 +0000573 rc = -ENOMEM;
574 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
Santiago Leond90c92f2012-09-04 14:41:37 +0000577 dev = &adapter->vdev->dev;
578
Santiago Leonf148f612010-09-03 18:29:30 +0000579 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) *
580 rxq_entries;
Santiago Leond90c92f2012-09-04 14:41:37 +0000581 adapter->rx_queue.queue_addr =
Joe Perchesd0320f72013-03-14 13:07:21 +0000582 dma_alloc_coherent(dev, adapter->rx_queue.queue_len,
583 &adapter->rx_queue.queue_dma, GFP_KERNEL);
Santiago Leonf148f612010-09-03 18:29:30 +0000584 if (!adapter->rx_queue.queue_addr) {
Denis Kirjanov88426f22010-10-20 04:21:13 +0000585 rc = -ENOMEM;
586 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700589 adapter->buffer_list_dma = dma_map_single(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700591 adapter->filter_list_dma = dma_map_single(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700594 if ((dma_mapping_error(dev, adapter->buffer_list_dma)) ||
Santiago Leond90c92f2012-09-04 14:41:37 +0000595 (dma_mapping_error(dev, adapter->filter_list_dma))) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000596 netdev_err(netdev, "unable to map filter or buffer list "
597 "pages\n");
Denis Kirjanov88426f22010-10-20 04:21:13 +0000598 rc = -ENOMEM;
599 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
602 adapter->rx_queue.index = 0;
603 adapter->rx_queue.num_slots = rxq_entries;
604 adapter->rx_queue.toggle = 1;
605
Anton Blanchardd746ca92014-03-05 14:51:37 +1100606 mac_address = ibmveth_encode_mac_addr(netdev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Santiago Leonf148f612010-09-03 18:29:30 +0000608 rxq_desc.fields.flags_len = IBMVETH_BUF_VALID |
609 adapter->rx_queue.queue_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
611
Santiago Leonc43ced12010-09-03 18:29:14 +0000612 netdev_dbg(netdev, "buffer list @ 0x%p\n", adapter->buffer_list_addr);
613 netdev_dbg(netdev, "filter list @ 0x%p\n", adapter->filter_list_addr);
614 netdev_dbg(netdev, "receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Santiago Leon4347ef12006-10-03 12:24:34 -0500616 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
617
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500618 lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Santiago Leonf148f612010-09-03 18:29:30 +0000620 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000621 netdev_err(netdev, "h_register_logical_lan failed with %ld\n",
622 lpar_rc);
623 netdev_err(netdev, "buffer TCE:0x%llx filter TCE:0x%llx rxq "
624 "desc:0x%llx MAC:0x%llx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 adapter->buffer_list_dma,
626 adapter->filter_list_dma,
627 rxq_desc.desc,
628 mac_address);
Denis Kirjanov88426f22010-10-20 04:21:13 +0000629 rc = -ENONET;
630 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
Santiago Leonf148f612010-09-03 18:29:30 +0000633 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
634 if (!adapter->rx_buff_pool[i].active)
Santiago Leon860f2422006-04-25 11:19:59 -0500635 continue;
636 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000637 netdev_err(netdev, "unable to alloc pool\n");
Santiago Leon860f2422006-04-25 11:19:59 -0500638 adapter->rx_buff_pool[i].active = 0;
Denis Kirjanov88426f22010-10-20 04:21:13 +0000639 rc = -ENOMEM;
640 goto err_out;
Santiago Leon860f2422006-04-25 11:19:59 -0500641 }
642 }
643
Santiago Leonc43ced12010-09-03 18:29:14 +0000644 netdev_dbg(netdev, "registering irq 0x%x\n", netdev->irq);
Santiago Leonf148f612010-09-03 18:29:30 +0000645 rc = request_irq(netdev->irq, ibmveth_interrupt, 0, netdev->name,
646 netdev);
647 if (rc != 0) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000648 netdev_err(netdev, "unable to request irq 0x%x, rc %d\n",
649 netdev->irq, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 do {
David S. Miller88c51002011-10-07 13:38:43 -0400651 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
652 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Denis Kirjanov88426f22010-10-20 04:21:13 +0000654 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656
Robert Jennings1096d632008-07-24 04:34:52 +1000657 adapter->bounce_buffer =
658 kmalloc(netdev->mtu + IBMVETH_BUFF_OH, GFP_KERNEL);
659 if (!adapter->bounce_buffer) {
Denis Kirjanov88426f22010-10-20 04:21:13 +0000660 rc = -ENOMEM;
Denis Kirjanove0e8ab52010-10-20 04:21:51 +0000661 goto err_out_free_irq;
Robert Jennings1096d632008-07-24 04:34:52 +1000662 }
663 adapter->bounce_buffer_dma =
664 dma_map_single(&adapter->vdev->dev, adapter->bounce_buffer,
665 netdev->mtu + IBMVETH_BUFF_OH, DMA_BIDIRECTIONAL);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700666 if (dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000667 netdev_err(netdev, "unable to map bounce buffer\n");
Denis Kirjanov88426f22010-10-20 04:21:13 +0000668 rc = -ENOMEM;
Denis Kirjanove0e8ab52010-10-20 04:21:51 +0000669 goto err_out_free_irq;
Robert Jennings1096d632008-07-24 04:34:52 +1000670 }
671
Santiago Leonc43ced12010-09-03 18:29:14 +0000672 netdev_dbg(netdev, "initial replenish cycle\n");
David Howells7d12e782006-10-05 14:55:46 +0100673 ibmveth_interrupt(netdev->irq, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Santiago Leone2adbcb2005-10-26 10:47:08 -0600675 netif_start_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Santiago Leonc43ced12010-09-03 18:29:14 +0000677 netdev_dbg(netdev, "open complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 return 0;
Denis Kirjanov88426f22010-10-20 04:21:13 +0000680
Denis Kirjanove0e8ab52010-10-20 04:21:51 +0000681err_out_free_irq:
682 free_irq(netdev->irq, netdev);
Denis Kirjanov88426f22010-10-20 04:21:13 +0000683err_out:
684 ibmveth_cleanup(adapter);
685 napi_disable(&adapter->napi);
686 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689static int ibmveth_close(struct net_device *netdev)
690{
Wang Chen4cf16532008-11-12 23:38:14 -0800691 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 long lpar_rc;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400693
Santiago Leonc43ced12010-09-03 18:29:14 +0000694 netdev_dbg(netdev, "close starting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700696 napi_disable(&adapter->napi);
697
Santiago Leon860f2422006-04-25 11:19:59 -0500698 if (!adapter->pool_config)
699 netif_stop_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Robert Jenningsee2e6112010-07-16 04:57:25 +0000701 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 do {
704 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200705 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Santiago Leonf148f612010-09-03 18:29:30 +0000707 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000708 netdev_err(netdev, "h_free_logical_lan failed with %lx, "
709 "continuing with close\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
Robert Jenningsee2e6112010-07-16 04:57:25 +0000712 free_irq(netdev->irq, netdev);
713
Anton Blanchardcbd52282014-08-22 11:36:52 +1000714 ibmveth_update_rx_no_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 ibmveth_cleanup(adapter);
717
Santiago Leonc43ced12010-09-03 18:29:14 +0000718 netdev_dbg(netdev, "close complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 return 0;
721}
722
Santiago Leonf148f612010-09-03 18:29:30 +0000723static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
724{
725 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
726 SUPPORTED_FIBRE);
727 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
728 ADVERTISED_FIBRE);
David Decotigny70739492011-04-27 18:32:40 +0000729 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 cmd->duplex = DUPLEX_FULL;
731 cmd->port = PORT_FIBRE;
732 cmd->phy_address = 0;
733 cmd->transceiver = XCVR_INTERNAL;
734 cmd->autoneg = AUTONEG_ENABLE;
735 cmd->maxtxpkt = 0;
736 cmd->maxrxpkt = 1;
737 return 0;
738}
739
Santiago Leonf148f612010-09-03 18:29:30 +0000740static void netdev_get_drvinfo(struct net_device *dev,
741 struct ethtool_drvinfo *info)
742{
Jiri Pirko7826d432013-01-06 00:44:26 +0000743 strlcpy(info->driver, ibmveth_driver_name, sizeof(info->driver));
744 strlcpy(info->version, ibmveth_driver_version, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000747static netdev_features_t ibmveth_fix_features(struct net_device *dev,
748 netdev_features_t features)
Brian King5fc7e012007-08-17 09:16:31 -0500749{
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000750 /*
751 * Since the ibmveth firmware interface does not have the
752 * concept of separate tx/rx checksum offload enable, if rx
753 * checksum is disabled we also have to disable tx checksum
754 * offload. Once we disable rx checksum offload, we are no
755 * longer allowed to send tx buffers that are not properly
756 * checksummed.
757 */
Brian King5fc7e012007-08-17 09:16:31 -0500758
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000759 if (!(features & NETIF_F_RXCSUM))
760 features &= ~NETIF_F_ALL_CSUM;
761
762 return features;
Brian King5fc7e012007-08-17 09:16:31 -0500763}
764
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000765static int ibmveth_set_csum_offload(struct net_device *dev, u32 data)
Brian King5fc7e012007-08-17 09:16:31 -0500766{
Wang Chen4cf16532008-11-12 23:38:14 -0800767 struct ibmveth_adapter *adapter = netdev_priv(dev);
Stephen Rothwellff5bfc32009-01-06 10:47:44 -0800768 unsigned long set_attr, clr_attr, ret_attr;
Santiago Leonab78df72010-09-03 18:28:52 +0000769 unsigned long set_attr6, clr_attr6;
David S. Miller8decf862011-09-22 03:23:13 -0400770 long ret, ret4, ret6;
Brian King5fc7e012007-08-17 09:16:31 -0500771 int rc1 = 0, rc2 = 0;
772 int restart = 0;
773
774 if (netif_running(dev)) {
775 restart = 1;
776 adapter->pool_config = 1;
777 ibmveth_close(dev);
778 adapter->pool_config = 0;
779 }
780
Brian King79ef4a42007-08-17 09:16:56 -0500781 set_attr = 0;
782 clr_attr = 0;
David S. Miller8decf862011-09-22 03:23:13 -0400783 set_attr6 = 0;
784 clr_attr6 = 0;
Brian King5fc7e012007-08-17 09:16:31 -0500785
Santiago Leonab78df72010-09-03 18:28:52 +0000786 if (data) {
Brian King79ef4a42007-08-17 09:16:56 -0500787 set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Santiago Leonab78df72010-09-03 18:28:52 +0000788 set_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
789 } else {
Brian King79ef4a42007-08-17 09:16:56 -0500790 clr_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Santiago Leonab78df72010-09-03 18:28:52 +0000791 clr_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
792 }
Brian King5fc7e012007-08-17 09:16:31 -0500793
Brian King79ef4a42007-08-17 09:16:56 -0500794 ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500795
Brian King79ef4a42007-08-17 09:16:56 -0500796 if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
797 !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
798 (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
David S. Miller8decf862011-09-22 03:23:13 -0400799 ret4 = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
Brian King79ef4a42007-08-17 09:16:56 -0500800 set_attr, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500801
David S. Miller8decf862011-09-22 03:23:13 -0400802 if (ret4 != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000803 netdev_err(dev, "unable to change IPv4 checksum "
804 "offload settings. %d rc=%ld\n",
David S. Miller8decf862011-09-22 03:23:13 -0400805 data, ret4);
Brian King5fc7e012007-08-17 09:16:31 -0500806
David S. Miller8decf862011-09-22 03:23:13 -0400807 h_illan_attributes(adapter->vdev->unit_address,
808 set_attr, clr_attr, &ret_attr);
809
810 if (data == 1)
811 dev->features &= ~NETIF_F_IP_CSUM;
812
Santiago Leonf148f612010-09-03 18:29:30 +0000813 } else {
Santiago Leonab78df72010-09-03 18:28:52 +0000814 adapter->fw_ipv4_csum_support = data;
Santiago Leonf148f612010-09-03 18:29:30 +0000815 }
Santiago Leonab78df72010-09-03 18:28:52 +0000816
817 ret6 = h_illan_attributes(adapter->vdev->unit_address,
818 clr_attr6, set_attr6, &ret_attr);
819
820 if (ret6 != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000821 netdev_err(dev, "unable to change IPv6 checksum "
822 "offload settings. %d rc=%ld\n",
David S. Miller8decf862011-09-22 03:23:13 -0400823 data, ret6);
Santiago Leonab78df72010-09-03 18:28:52 +0000824
David S. Miller8decf862011-09-22 03:23:13 -0400825 h_illan_attributes(adapter->vdev->unit_address,
826 set_attr6, clr_attr6, &ret_attr);
827
828 if (data == 1)
829 dev->features &= ~NETIF_F_IPV6_CSUM;
830
Santiago Leonab78df72010-09-03 18:28:52 +0000831 } else
832 adapter->fw_ipv6_csum_support = data;
833
David S. Miller8decf862011-09-22 03:23:13 -0400834 if (ret4 == H_SUCCESS || ret6 == H_SUCCESS)
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000835 adapter->rx_csum = data;
Santiago Leonab78df72010-09-03 18:28:52 +0000836 else
837 rc1 = -EIO;
Brian King5fc7e012007-08-17 09:16:31 -0500838 } else {
839 rc1 = -EIO;
Santiago Leon21c2dec2010-09-03 18:29:19 +0000840 netdev_err(dev, "unable to change checksum offload settings."
841 " %d rc=%ld ret_attr=%lx\n", data, ret,
842 ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500843 }
844
845 if (restart)
846 rc2 = ibmveth_open(dev);
847
848 return rc1 ? rc1 : rc2;
849}
850
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000851static int ibmveth_set_features(struct net_device *dev,
852 netdev_features_t features)
Brian King5fc7e012007-08-17 09:16:31 -0500853{
Wang Chen4cf16532008-11-12 23:38:14 -0800854 struct ibmveth_adapter *adapter = netdev_priv(dev);
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000855 int rx_csum = !!(features & NETIF_F_RXCSUM);
856 int rc;
Thomas Falcon8641dd82015-04-29 16:25:45 -0500857 netdev_features_t changed = features ^ dev->features;
858
859 if (features & NETIF_F_TSO & changed)
860 netdev_info(dev, "TSO feature requires all partitions to have updated driver");
Brian King5fc7e012007-08-17 09:16:31 -0500861
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000862 if (rx_csum == adapter->rx_csum)
Brian King5fc7e012007-08-17 09:16:31 -0500863 return 0;
864
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000865 rc = ibmveth_set_csum_offload(dev, rx_csum);
866 if (rc && !adapter->rx_csum)
867 dev->features = features & ~(NETIF_F_ALL_CSUM | NETIF_F_RXCSUM);
Brian King5fc7e012007-08-17 09:16:31 -0500868
869 return rc;
870}
871
Brian Kingddbb4de2007-08-17 09:16:43 -0500872static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
873{
874 int i;
875
876 if (stringset != ETH_SS_STATS)
877 return;
878
879 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
880 memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
881}
882
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700883static int ibmveth_get_sset_count(struct net_device *dev, int sset)
Brian Kingddbb4de2007-08-17 09:16:43 -0500884{
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700885 switch (sset) {
886 case ETH_SS_STATS:
887 return ARRAY_SIZE(ibmveth_stats);
888 default:
889 return -EOPNOTSUPP;
890 }
Brian Kingddbb4de2007-08-17 09:16:43 -0500891}
892
893static void ibmveth_get_ethtool_stats(struct net_device *dev,
894 struct ethtool_stats *stats, u64 *data)
895{
896 int i;
Wang Chen4cf16532008-11-12 23:38:14 -0800897 struct ibmveth_adapter *adapter = netdev_priv(dev);
Brian Kingddbb4de2007-08-17 09:16:43 -0500898
899 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
900 data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
901}
902
Jeff Garzik7282d492006-09-13 14:30:00 -0400903static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 .get_drvinfo = netdev_get_drvinfo,
905 .get_settings = netdev_get_settings,
Ben Hutchingsed4ba4b2010-12-09 12:10:25 +0000906 .get_link = ethtool_op_get_link,
Brian Kingddbb4de2007-08-17 09:16:43 -0500907 .get_strings = ibmveth_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700908 .get_sset_count = ibmveth_get_sset_count,
Brian Kingddbb4de2007-08-17 09:16:43 -0500909 .get_ethtool_stats = ibmveth_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910};
911
912static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
913{
914 return -EOPNOTSUPP;
915}
916
917#define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
918
Santiago Leon6e8ab302010-09-03 18:28:36 +0000919static int ibmveth_send(struct ibmveth_adapter *adapter,
920 union ibmveth_buf_desc *descs)
921{
922 unsigned long correlator;
923 unsigned int retry_count;
924 unsigned long ret;
925
926 /*
927 * The retry count sets a maximum for the number of broadcast and
928 * multicast destinations within the system.
929 */
930 retry_count = 1024;
931 correlator = 0;
932 do {
933 ret = h_send_logical_lan(adapter->vdev->unit_address,
934 descs[0].desc, descs[1].desc,
935 descs[2].desc, descs[3].desc,
936 descs[4].desc, descs[5].desc,
937 correlator, &correlator);
938 } while ((ret == H_BUSY) && (retry_count--));
939
940 if (ret != H_SUCCESS && ret != H_DROPPED) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000941 netdev_err(adapter->netdev, "tx: h_send_logical_lan failed "
942 "with rc=%ld\n", ret);
Santiago Leon6e8ab302010-09-03 18:28:36 +0000943 return 1;
944 }
945
946 return 0;
947}
948
Stephen Hemminger613573252009-08-31 19:50:58 +0000949static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb,
950 struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Wang Chen4cf16532008-11-12 23:38:14 -0800952 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Santiago Leon6e8ab302010-09-03 18:28:36 +0000953 unsigned int desc_flags;
954 union ibmveth_buf_desc descs[6];
955 int last, i;
956 int force_bounce = 0;
David S. Miller8decf862011-09-22 03:23:13 -0400957 dma_addr_t dma_addr;
Santiago Leon60296d92005-10-26 10:47:16 -0600958
Santiago Leon6e8ab302010-09-03 18:28:36 +0000959 /*
960 * veth handles a maximum of 6 segments including the header, so
961 * we have to linearize the skb if there are more than this.
962 */
963 if (skb_shinfo(skb)->nr_frags > 5 && __skb_linearize(skb)) {
964 netdev->stats.tx_dropped++;
Brian Kingf4ff2872007-09-15 13:36:07 -0700965 goto out;
966 }
967
Santiago Leon6e8ab302010-09-03 18:28:36 +0000968 /* veth can't checksum offload UDP */
969 if (skb->ip_summed == CHECKSUM_PARTIAL &&
Santiago Leonab78df72010-09-03 18:28:52 +0000970 ((skb->protocol == htons(ETH_P_IP) &&
971 ip_hdr(skb)->protocol != IPPROTO_TCP) ||
972 (skb->protocol == htons(ETH_P_IPV6) &&
973 ipv6_hdr(skb)->nexthdr != IPPROTO_TCP)) &&
974 skb_checksum_help(skb)) {
975
Santiago Leon21c2dec2010-09-03 18:29:19 +0000976 netdev_err(netdev, "tx: failed to checksum packet\n");
Santiago Leon6e8ab302010-09-03 18:28:36 +0000977 netdev->stats.tx_dropped++;
978 goto out;
979 }
Brian Kingf4ff2872007-09-15 13:36:07 -0700980
Santiago Leon6e8ab302010-09-03 18:28:36 +0000981 desc_flags = IBMVETH_BUF_VALID;
982
983 if (skb->ip_summed == CHECKSUM_PARTIAL) {
984 unsigned char *buf = skb_transport_header(skb) +
985 skb->csum_offset;
986
987 desc_flags |= (IBMVETH_BUF_NO_CSUM | IBMVETH_BUF_CSUM_GOOD);
Brian Kingf4ff2872007-09-15 13:36:07 -0700988
989 /* Need to zero out the checksum */
990 buf[0] = 0;
991 buf[1] = 0;
992 }
993
Santiago Leon6e8ab302010-09-03 18:28:36 +0000994retry_bounce:
995 memset(descs, 0, sizeof(descs));
Santiago Leonc08cc3c2010-09-03 18:28:20 +0000996
Santiago Leon6e8ab302010-09-03 18:28:36 +0000997 /*
998 * If a linear packet is below the rx threshold then
999 * copy it into the static bounce buffer. This avoids the
1000 * cost of a TCE insert and remove.
1001 */
1002 if (force_bounce || (!skb_is_nonlinear(skb) &&
1003 (skb->len < tx_copybreak))) {
Robert Jennings1096d632008-07-24 04:34:52 +10001004 skb_copy_from_linear_data(skb, adapter->bounce_buffer,
1005 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Santiago Leon6e8ab302010-09-03 18:28:36 +00001007 descs[0].fields.flags_len = desc_flags | skb->len;
1008 descs[0].fields.address = adapter->bounce_buffer_dma;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001009
Santiago Leon6e8ab302010-09-03 18:28:36 +00001010 if (ibmveth_send(adapter, descs)) {
1011 adapter->tx_send_failed++;
1012 netdev->stats.tx_dropped++;
1013 } else {
1014 netdev->stats.tx_packets++;
1015 netdev->stats.tx_bytes += skb->len;
1016 }
1017
1018 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020
Santiago Leon6e8ab302010-09-03 18:28:36 +00001021 /* Map the header */
David S. Miller8decf862011-09-22 03:23:13 -04001022 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
1023 skb_headlen(skb), DMA_TO_DEVICE);
1024 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
Santiago Leon6e8ab302010-09-03 18:28:36 +00001025 goto map_failed;
1026
1027 descs[0].fields.flags_len = desc_flags | skb_headlen(skb);
David S. Miller8decf862011-09-22 03:23:13 -04001028 descs[0].fields.address = dma_addr;
Santiago Leon6e8ab302010-09-03 18:28:36 +00001029
1030 /* Map the frags */
1031 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001032 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Santiago Leon6e8ab302010-09-03 18:28:36 +00001033
Ian Campbell8838a532011-08-31 00:46:53 +00001034 dma_addr = skb_frag_dma_map(&adapter->vdev->dev, frag, 0,
Eric Dumazet9e903e02011-10-18 21:00:24 +00001035 skb_frag_size(frag), DMA_TO_DEVICE);
Santiago Leon6e8ab302010-09-03 18:28:36 +00001036
1037 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
1038 goto map_failed_frags;
1039
Eric Dumazet9e903e02011-10-18 21:00:24 +00001040 descs[i+1].fields.flags_len = desc_flags | skb_frag_size(frag);
Santiago Leon6e8ab302010-09-03 18:28:36 +00001041 descs[i+1].fields.address = dma_addr;
1042 }
1043
Thomas Falcon8641dd82015-04-29 16:25:45 -05001044 if (skb_is_gso(skb) && !skb_is_gso_v6(skb)) {
1045 /* Put -1 in the IP checksum to tell phyp it
1046 * is a largesend packet and put the mss in the TCP checksum.
1047 */
1048 ip_hdr(skb)->check = 0xffff;
1049 tcp_hdr(skb)->check = cpu_to_be16(skb_shinfo(skb)->gso_size);
1050 adapter->tx_large_packets++;
1051 }
1052
Santiago Leon6e8ab302010-09-03 18:28:36 +00001053 if (ibmveth_send(adapter, descs)) {
1054 adapter->tx_send_failed++;
1055 netdev->stats.tx_dropped++;
1056 } else {
1057 netdev->stats.tx_packets++;
1058 netdev->stats.tx_bytes += skb->len;
1059 }
1060
David S. Miller8decf862011-09-22 03:23:13 -04001061 dma_unmap_single(&adapter->vdev->dev,
1062 descs[0].fields.address,
1063 descs[0].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1064 DMA_TO_DEVICE);
1065
1066 for (i = 1; i < skb_shinfo(skb)->nr_frags + 1; i++)
Santiago Leon6e8ab302010-09-03 18:28:36 +00001067 dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
1068 descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1069 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Santiago Leone8cb7eb2010-09-03 18:28:15 +00001071out:
Eric W. Biederman26faa9d2014-03-15 17:29:34 -07001072 dev_consume_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001073 return NETDEV_TX_OK;
Santiago Leon6e8ab302010-09-03 18:28:36 +00001074
1075map_failed_frags:
1076 last = i+1;
1077 for (i = 0; i < last; i++)
1078 dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
1079 descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1080 DMA_TO_DEVICE);
1081
1082map_failed:
1083 if (!firmware_has_feature(FW_FEATURE_CMO))
Santiago Leon21c2dec2010-09-03 18:29:19 +00001084 netdev_err(netdev, "tx: unable to map xmit buffer\n");
Santiago Leon6e8ab302010-09-03 18:28:36 +00001085 adapter->tx_map_failed++;
1086 skb_linearize(skb);
1087 force_bounce = 1;
1088 goto retry_bounce;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001091static int ibmveth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Santiago Leonf148f612010-09-03 18:29:30 +00001093 struct ibmveth_adapter *adapter =
1094 container_of(napi, struct ibmveth_adapter, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001095 struct net_device *netdev = adapter->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 int frames_processed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 unsigned long lpar_rc;
Thomas Falcon9c7e8bc2015-04-29 16:25:47 -05001098 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Santiago Leonf148f612010-09-03 18:29:30 +00001100restart_poll:
Eric W. Biedermancb013ea2014-03-14 18:03:50 -07001101 while (frames_processed < budget) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001102 if (!ibmveth_rxq_pending_buffer(adapter))
1103 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Anton Blanchardf89e49e2010-09-06 18:21:41 -07001105 smp_rmb();
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001106 if (!ibmveth_rxq_buffer_valid(adapter)) {
1107 wmb(); /* suggested by larson1 */
1108 adapter->rx_invalid_buffer++;
Santiago Leonc43ced12010-09-03 18:29:14 +00001109 netdev_dbg(netdev, "recycling invalid buffer\n");
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001110 ibmveth_rxq_recycle_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 } else {
Santiago Leon8d86c612010-09-03 18:28:25 +00001112 struct sk_buff *skb, *new_skb;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001113 int length = ibmveth_rxq_frame_length(adapter);
1114 int offset = ibmveth_rxq_frame_offset(adapter);
Brian Kingf4ff2872007-09-15 13:36:07 -07001115 int csum_good = ibmveth_rxq_csum_good(adapter);
1116
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001117 skb = ibmveth_rxq_get_buffer(adapter);
1118
Santiago Leon8d86c612010-09-03 18:28:25 +00001119 new_skb = NULL;
1120 if (length < rx_copybreak)
1121 new_skb = netdev_alloc_skb(netdev, length);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001122
Santiago Leon8d86c612010-09-03 18:28:25 +00001123 if (new_skb) {
1124 skb_copy_to_linear_data(new_skb,
1125 skb->data + offset,
1126 length);
Santiago Leon0c26b672010-09-03 18:28:41 +00001127 if (rx_flush)
1128 ibmveth_flush_buffer(skb->data,
1129 length + offset);
David S. Miller8decf862011-09-22 03:23:13 -04001130 if (!ibmveth_rxq_recycle_buffer(adapter))
1131 kfree_skb(skb);
Santiago Leon8d86c612010-09-03 18:28:25 +00001132 skb = new_skb;
Santiago Leon8d86c612010-09-03 18:28:25 +00001133 } else {
1134 ibmveth_rxq_harvest_buffer(adapter);
1135 skb_reserve(skb, offset);
1136 }
1137
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001138 skb_put(skb, length);
1139 skb->protocol = eth_type_trans(skb, netdev);
1140
Thomas Falcon9c7e8bc2015-04-29 16:25:47 -05001141 if (csum_good) {
Santiago Leon8d86c612010-09-03 18:28:25 +00001142 skb->ip_summed = CHECKSUM_UNNECESSARY;
Thomas Falcon9c7e8bc2015-04-29 16:25:47 -05001143 if (be16_to_cpu(skb->protocol) == ETH_P_IP) {
1144 iph = (struct iphdr *)skb->data;
1145
1146 /* If the IP checksum is not offloaded and if the packet
1147 * is large send, the checksum must be rebuilt.
1148 */
1149 if (iph->check == 0xffff) {
1150 iph->check = 0;
1151 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1152 adapter->rx_large_packets++;
1153 }
1154 }
1155 }
Santiago Leon8d86c612010-09-03 18:28:25 +00001156
Thomas Falcon92ec8272015-04-29 16:25:46 -05001157 napi_gro_receive(napi, skb); /* send it up */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001158
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001159 netdev->stats.rx_packets++;
1160 netdev->stats.rx_bytes += length;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001161 frames_processed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
Eric W. Biedermancb013ea2014-03-14 18:03:50 -07001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Santiago Leone2adbcb2005-10-26 10:47:08 -06001165 ibmveth_replenish_task(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001167 if (frames_processed < budget) {
Yongbae Park4736edc2015-03-10 11:15:39 +09001168 napi_complete(napi);
1169
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001170 /* We think we are done - reenable interrupts,
1171 * then check once more to make sure we are done.
1172 */
1173 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1174 VIO_IRQ_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Santiago Leon64859112010-09-03 18:29:41 +00001176 BUG_ON(lpar_rc != H_SUCCESS);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001177
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001178 if (ibmveth_rxq_pending_buffer(adapter) &&
Ben Hutchings288379f2009-01-19 16:43:59 -08001179 napi_reschedule(napi)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001180 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1181 VIO_IRQ_DISABLE);
1182 goto restart_poll;
1183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001186 return frames_processed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
David Howells7d12e782006-10-05 14:55:46 +01001189static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001190{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 struct net_device *netdev = dev_instance;
Wang Chen4cf16532008-11-12 23:38:14 -08001192 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 unsigned long lpar_rc;
1194
Ben Hutchings288379f2009-01-19 16:43:59 -08001195 if (napi_schedule_prep(&adapter->napi)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001196 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1197 VIO_IRQ_DISABLE);
Santiago Leon64859112010-09-03 18:29:41 +00001198 BUG_ON(lpar_rc != H_SUCCESS);
Ben Hutchings288379f2009-01-19 16:43:59 -08001199 __napi_schedule(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201 return IRQ_HANDLED;
1202}
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204static void ibmveth_set_multicast_list(struct net_device *netdev)
1205{
Wang Chen4cf16532008-11-12 23:38:14 -08001206 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 unsigned long lpar_rc;
1208
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001209 if ((netdev->flags & IFF_PROMISC) ||
1210 (netdev_mc_count(netdev) > adapter->mcastFilterSize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1212 IbmVethMcastEnableRecv |
1213 IbmVethMcastDisableFiltering,
1214 0);
Santiago Leonf148f612010-09-03 18:29:30 +00001215 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001216 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1217 "entering promisc mode\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001220 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 /* clear the filter table & disable filtering */
1222 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1223 IbmVethMcastEnableRecv |
1224 IbmVethMcastDisableFiltering |
1225 IbmVethMcastClearFilterTable,
1226 0);
Santiago Leonf148f612010-09-03 18:29:30 +00001227 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001228 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1229 "attempting to clear filter table\n",
1230 lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232 /* add the addresses to the filter table */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001233 netdev_for_each_mc_addr(ha, netdev) {
Santiago Leonf148f612010-09-03 18:29:30 +00001234 /* add the multicast address to the filter table */
Anton Blanchardd746ca92014-03-05 14:51:37 +11001235 u64 mcast_addr;
1236 mcast_addr = ibmveth_encode_mac_addr(ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1238 IbmVethMcastAddFilter,
1239 mcast_addr);
Santiago Leonf148f612010-09-03 18:29:30 +00001240 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001241 netdev_err(netdev, "h_multicast_ctrl rc=%ld "
1242 "when adding an entry to the filter "
1243 "table\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 /* re-enable filtering */
1248 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1249 IbmVethMcastEnableFiltering,
1250 0);
Santiago Leonf148f612010-09-03 18:29:30 +00001251 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001252 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1253 "enabling filtering\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255 }
1256}
1257
1258static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
1259{
Wang Chen4cf16532008-11-12 23:38:14 -08001260 struct ibmveth_adapter *adapter = netdev_priv(dev);
Robert Jennings1096d632008-07-24 04:34:52 +10001261 struct vio_dev *viodev = adapter->vdev;
Santiago Leon860f2422006-04-25 11:19:59 -05001262 int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
Robert Jennings0645bab2010-08-17 09:15:45 +00001263 int i, rc;
1264 int need_restart = 0;
Santiago Leonb6d35182005-10-26 10:47:01 -06001265
Santiago Leon517e80e2010-09-03 18:29:25 +00001266 if (new_mtu < IBMVETH_MIN_MTU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return -EINVAL;
Santiago Leonb6d35182005-10-26 10:47:01 -06001268
Santiago Leon517e80e2010-09-03 18:29:25 +00001269 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
David Gibson4fce1482015-04-23 14:43:05 +10001270 if (new_mtu_oh <= adapter->rx_buff_pool[i].buff_size)
Brian Kingce6eea52007-06-08 14:05:17 -05001271 break;
1272
Santiago Leon517e80e2010-09-03 18:29:25 +00001273 if (i == IBMVETH_NUM_BUFF_POOLS)
Brian Kingce6eea52007-06-08 14:05:17 -05001274 return -EINVAL;
1275
Santiago Leonea866e62008-07-24 04:34:23 +10001276 /* Deactivate all the buffer pools so that the next loop can activate
1277 only the buffer pools necessary to hold the new MTU */
Robert Jennings0645bab2010-08-17 09:15:45 +00001278 if (netif_running(adapter->netdev)) {
1279 need_restart = 1;
1280 adapter->pool_config = 1;
1281 ibmveth_close(adapter->netdev);
1282 adapter->pool_config = 0;
1283 }
Brian Kingce6eea52007-06-08 14:05:17 -05001284
Santiago Leonea866e62008-07-24 04:34:23 +10001285 /* Look for an active buffer pool that can hold the new MTU */
Santiago Leonf148f612010-09-03 18:29:30 +00001286 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Santiago Leonea866e62008-07-24 04:34:23 +10001287 adapter->rx_buff_pool[i].active = 1;
1288
David Gibson4fce1482015-04-23 14:43:05 +10001289 if (new_mtu_oh <= adapter->rx_buff_pool[i].buff_size) {
Robert Jennings1096d632008-07-24 04:34:52 +10001290 dev->mtu = new_mtu;
1291 vio_cmo_set_dev_desired(viodev,
1292 ibmveth_get_desired_dma
1293 (viodev));
Robert Jennings0645bab2010-08-17 09:15:45 +00001294 if (need_restart) {
1295 return ibmveth_open(adapter->netdev);
1296 }
Santiago Leon860f2422006-04-25 11:19:59 -05001297 return 0;
Santiago Leonb6d35182005-10-26 10:47:01 -06001298 }
Santiago Leonb6d35182005-10-26 10:47:01 -06001299 }
Robert Jennings0645bab2010-08-17 09:15:45 +00001300
1301 if (need_restart && (rc = ibmveth_open(adapter->netdev)))
1302 return rc;
1303
Santiago Leon860f2422006-04-25 11:19:59 -05001304 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
Santiago Leon6b422372006-10-03 12:24:28 -05001307#ifdef CONFIG_NET_POLL_CONTROLLER
1308static void ibmveth_poll_controller(struct net_device *dev)
1309{
Wang Chen4cf16532008-11-12 23:38:14 -08001310 ibmveth_replenish_task(netdev_priv(dev));
Andrew Morton5f771132006-10-10 14:33:30 -07001311 ibmveth_interrupt(dev->irq, dev);
Santiago Leon6b422372006-10-03 12:24:28 -05001312}
1313#endif
1314
Robert Jennings1096d632008-07-24 04:34:52 +10001315/**
1316 * ibmveth_get_desired_dma - Calculate IO memory desired by the driver
1317 *
1318 * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
1319 *
1320 * Return value:
1321 * Number of bytes of IO data the driver will need to perform well.
1322 */
1323static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev)
1324{
1325 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
1326 struct ibmveth_adapter *adapter;
Alistair Poppled0847752013-12-09 18:17:03 +11001327 struct iommu_table *tbl;
Robert Jennings1096d632008-07-24 04:34:52 +10001328 unsigned long ret;
1329 int i;
1330 int rxqentries = 1;
1331
Alistair Poppled0847752013-12-09 18:17:03 +11001332 tbl = get_iommu_table_base(&vdev->dev);
1333
Robert Jennings1096d632008-07-24 04:34:52 +10001334 /* netdev inits at probe time along with the structures we need below*/
1335 if (netdev == NULL)
Alistair Poppled0847752013-12-09 18:17:03 +11001336 return IOMMU_PAGE_ALIGN(IBMVETH_IO_ENTITLEMENT_DEFAULT, tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001337
1338 adapter = netdev_priv(netdev);
1339
1340 ret = IBMVETH_BUFF_LIST_SIZE + IBMVETH_FILT_LIST_SIZE;
Alistair Poppled0847752013-12-09 18:17:03 +11001341 ret += IOMMU_PAGE_ALIGN(netdev->mtu, tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001342
Santiago Leon517e80e2010-09-03 18:29:25 +00001343 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Robert Jennings1096d632008-07-24 04:34:52 +10001344 /* add the size of the active receive buffers */
1345 if (adapter->rx_buff_pool[i].active)
1346 ret +=
1347 adapter->rx_buff_pool[i].size *
1348 IOMMU_PAGE_ALIGN(adapter->rx_buff_pool[i].
Alistair Poppled0847752013-12-09 18:17:03 +11001349 buff_size, tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001350 rxqentries += adapter->rx_buff_pool[i].size;
1351 }
1352 /* add the size of the receive queue entries */
Alistair Poppled0847752013-12-09 18:17:03 +11001353 ret += IOMMU_PAGE_ALIGN(
1354 rxqentries * sizeof(struct ibmveth_rx_q_entry), tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001355
1356 return ret;
1357}
1358
Thomas Falconc77c7612015-03-02 11:56:12 -06001359static int ibmveth_set_mac_addr(struct net_device *dev, void *p)
1360{
1361 struct ibmveth_adapter *adapter = netdev_priv(dev);
1362 struct sockaddr *addr = p;
1363 u64 mac_address;
1364 int rc;
1365
1366 if (!is_valid_ether_addr(addr->sa_data))
1367 return -EADDRNOTAVAIL;
1368
1369 mac_address = ibmveth_encode_mac_addr(addr->sa_data);
1370 rc = h_change_logical_lan_mac(adapter->vdev->unit_address, mac_address);
1371 if (rc) {
1372 netdev_err(adapter->netdev, "h_change_logical_lan_mac failed with rc=%d\n", rc);
1373 return rc;
1374 }
1375
1376 ether_addr_copy(dev->dev_addr, addr->sa_data);
1377
1378 return 0;
1379}
1380
Alexander Beregalove186d172009-04-15 12:52:39 +00001381static const struct net_device_ops ibmveth_netdev_ops = {
1382 .ndo_open = ibmveth_open,
1383 .ndo_stop = ibmveth_close,
1384 .ndo_start_xmit = ibmveth_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001385 .ndo_set_rx_mode = ibmveth_set_multicast_list,
Alexander Beregalove186d172009-04-15 12:52:39 +00001386 .ndo_do_ioctl = ibmveth_ioctl,
1387 .ndo_change_mtu = ibmveth_change_mtu,
Michał Mirosławb9367bf2011-04-19 02:14:25 +00001388 .ndo_fix_features = ibmveth_fix_features,
1389 .ndo_set_features = ibmveth_set_features,
Alexander Beregalove186d172009-04-15 12:52:39 +00001390 .ndo_validate_addr = eth_validate_addr,
Thomas Falconc77c7612015-03-02 11:56:12 -06001391 .ndo_set_mac_address = ibmveth_set_mac_addr,
Alexander Beregalove186d172009-04-15 12:52:39 +00001392#ifdef CONFIG_NET_POLL_CONTROLLER
1393 .ndo_poll_controller = ibmveth_poll_controller,
1394#endif
1395};
1396
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001397static int ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Benjamin Herrenschmidt13f85202013-05-03 17:19:01 +00001399 int rc, i, mac_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 struct net_device *netdev;
Mariusz Kozlowski9dc83af2007-08-06 23:44:03 +02001401 struct ibmveth_adapter *adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 unsigned char *mac_addr_p;
1403 unsigned int *mcastFilterSize_p;
1404
Santiago Leonc43ced12010-09-03 18:29:14 +00001405 dev_dbg(&dev->dev, "entering ibmveth_probe for UA 0x%x\n",
1406 dev->unit_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Santiago Leonf148f612010-09-03 18:29:30 +00001408 mac_addr_p = (unsigned char *)vio_get_attribute(dev, VETH_MAC_ADDR,
Benjamin Herrenschmidt13f85202013-05-03 17:19:01 +00001409 &mac_len);
Santiago Leonf148f612010-09-03 18:29:30 +00001410 if (!mac_addr_p) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001411 dev_err(&dev->dev, "Can't find VETH_MAC_ADDR attribute\n");
Santiago Leonbe35ae92010-09-03 18:29:36 +00001412 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
Benjamin Herrenschmidt13f85202013-05-03 17:19:01 +00001414 /* Workaround for old/broken pHyp */
1415 if (mac_len == 8)
1416 mac_addr_p += 2;
1417 else if (mac_len != 6) {
1418 dev_err(&dev->dev, "VETH_MAC_ADDR attribute wrong len %d\n",
1419 mac_len);
1420 return -EINVAL;
1421 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001422
Santiago Leonf148f612010-09-03 18:29:30 +00001423 mcastFilterSize_p = (unsigned int *)vio_get_attribute(dev,
Michael Ellerman493a6842007-04-17 13:12:55 +10001424 VETH_MCAST_FILTER_SIZE, NULL);
Santiago Leonf148f612010-09-03 18:29:30 +00001425 if (!mcastFilterSize_p) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001426 dev_err(&dev->dev, "Can't find VETH_MCAST_FILTER_SIZE "
1427 "attribute\n");
Santiago Leonbe35ae92010-09-03 18:29:36 +00001428 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
1432
Santiago Leonf148f612010-09-03 18:29:30 +00001433 if (!netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return -ENOMEM;
1435
Wang Chen4cf16532008-11-12 23:38:14 -08001436 adapter = netdev_priv(netdev);
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07001437 dev_set_drvdata(&dev->dev, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 adapter->vdev = dev;
1440 adapter->netdev = netdev;
Santiago Leonf148f612010-09-03 18:29:30 +00001441 adapter->mcastFilterSize = *mcastFilterSize_p;
Santiago Leon860f2422006-04-25 11:19:59 -05001442 adapter->pool_config = 0;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001443
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001444 netif_napi_add(netdev, &adapter->napi, ibmveth_poll, 16);
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 netdev->irq = dev->irq;
Alexander Beregalove186d172009-04-15 12:52:39 +00001447 netdev->netdev_ops = &ibmveth_netdev_ops;
1448 netdev->ethtool_ops = &netdev_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 SET_NETDEV_DEV(netdev, &dev->dev);
Michał Mirosławb9367bf2011-04-19 02:14:25 +00001450 netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
1451 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1452 netdev->features |= netdev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Thomas Falcon8641dd82015-04-29 16:25:45 -05001454 /* TSO is disabled by default */
1455 netdev->hw_features |= NETIF_F_TSO;
1456
Anton Blanchardd746ca92014-03-05 14:51:37 +11001457 memcpy(netdev->dev_addr, mac_addr_p, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Thomas Falconcd7c7ec2015-04-29 16:25:44 -05001459 if (firmware_has_feature(FW_FEATURE_CMO))
1460 memcpy(pool_count, pool_count_cmo, sizeof(pool_count));
1461
Santiago Leonf148f612010-09-03 18:29:30 +00001462 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Santiago Leon860f2422006-04-25 11:19:59 -05001463 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
Greg Kroah-Hartman8dde2a92007-12-17 15:54:39 -04001464 int error;
1465
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001466 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1467 pool_count[i], pool_size[i],
Santiago Leon860f2422006-04-25 11:19:59 -05001468 pool_active[i]);
Greg Kroah-Hartman8dde2a92007-12-17 15:54:39 -04001469 error = kobject_init_and_add(kobj, &ktype_veth_pool,
1470 &dev->dev.kobj, "pool%d", i);
1471 if (!error)
1472 kobject_uevent(kobj, KOBJ_ADD);
Santiago Leon860f2422006-04-25 11:19:59 -05001473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Santiago Leonc43ced12010-09-03 18:29:14 +00001475 netdev_dbg(netdev, "adapter @ 0x%p\n", adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 adapter->buffer_list_dma = DMA_ERROR_CODE;
1478 adapter->filter_list_dma = DMA_ERROR_CODE;
1479 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1480
Santiago Leonc43ced12010-09-03 18:29:14 +00001481 netdev_dbg(netdev, "registering netdev...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Michał Mirosławb801a4e2011-04-28 11:59:15 +10001483 ibmveth_set_features(netdev, netdev->features);
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 rc = register_netdev(netdev);
1486
Santiago Leonf148f612010-09-03 18:29:30 +00001487 if (rc) {
Santiago Leonc43ced12010-09-03 18:29:14 +00001488 netdev_dbg(netdev, "failed to register netdev rc=%d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 free_netdev(netdev);
1490 return rc;
1491 }
1492
Santiago Leonc43ced12010-09-03 18:29:14 +00001493 netdev_dbg(netdev, "registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return 0;
1496}
1497
Bill Pembertone11787a2012-12-03 09:23:12 -05001498static int ibmveth_remove(struct vio_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07001500 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Wang Chen4cf16532008-11-12 23:38:14 -08001501 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001502 int i;
1503
Santiago Leonf148f612010-09-03 18:29:30 +00001504 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001505 kobject_put(&adapter->rx_buff_pool[i].kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 unregister_netdev(netdev);
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 free_netdev(netdev);
Robert Jennings1096d632008-07-24 04:34:52 +10001510 dev_set_drvdata(&dev->dev, NULL);
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return 0;
1513}
1514
Santiago Leon860f2422006-04-25 11:19:59 -05001515static struct attribute veth_active_attr;
1516static struct attribute veth_num_attr;
1517static struct attribute veth_size_attr;
1518
Santiago Leonf148f612010-09-03 18:29:30 +00001519static ssize_t veth_pool_show(struct kobject *kobj,
1520 struct attribute *attr, char *buf)
Santiago Leon860f2422006-04-25 11:19:59 -05001521{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001522 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001523 struct ibmveth_buff_pool,
1524 kobj);
1525
1526 if (attr == &veth_active_attr)
1527 return sprintf(buf, "%d\n", pool->active);
1528 else if (attr == &veth_num_attr)
1529 return sprintf(buf, "%d\n", pool->size);
1530 else if (attr == &veth_size_attr)
1531 return sprintf(buf, "%d\n", pool->buff_size);
1532 return 0;
1533}
1534
Santiago Leonf148f612010-09-03 18:29:30 +00001535static ssize_t veth_pool_store(struct kobject *kobj, struct attribute *attr,
1536 const char *buf, size_t count)
Santiago Leon860f2422006-04-25 11:19:59 -05001537{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001538 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001539 struct ibmveth_buff_pool,
1540 kobj);
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07001541 struct net_device *netdev = dev_get_drvdata(
1542 container_of(kobj->parent, struct device, kobj));
Wang Chen4cf16532008-11-12 23:38:14 -08001543 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001544 long value = simple_strtol(buf, NULL, 10);
1545 long rc;
1546
1547 if (attr == &veth_active_attr) {
1548 if (value && !pool->active) {
Brian King4aa9c932007-06-08 14:05:16 -05001549 if (netif_running(netdev)) {
Santiago Leonf148f612010-09-03 18:29:30 +00001550 if (ibmveth_alloc_buffer_pool(pool)) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001551 netdev_err(netdev,
1552 "unable to alloc pool\n");
Brian King4aa9c932007-06-08 14:05:16 -05001553 return -ENOMEM;
1554 }
1555 pool->active = 1;
1556 adapter->pool_config = 1;
1557 ibmveth_close(netdev);
1558 adapter->pool_config = 0;
1559 if ((rc = ibmveth_open(netdev)))
1560 return rc;
Santiago Leonf148f612010-09-03 18:29:30 +00001561 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001562 pool->active = 1;
Santiago Leonf148f612010-09-03 18:29:30 +00001563 }
Santiago Leon860f2422006-04-25 11:19:59 -05001564 } else if (!value && pool->active) {
1565 int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1566 int i;
1567 /* Make sure there is a buffer pool with buffers that
1568 can hold a packet of the size of the MTU */
Santiago Leon517e80e2010-09-03 18:29:25 +00001569 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Santiago Leon860f2422006-04-25 11:19:59 -05001570 if (pool == &adapter->rx_buff_pool[i])
1571 continue;
1572 if (!adapter->rx_buff_pool[i].active)
1573 continue;
Brian King76b9cfc2007-08-03 13:55:19 +10001574 if (mtu <= adapter->rx_buff_pool[i].buff_size)
1575 break;
Santiago Leon860f2422006-04-25 11:19:59 -05001576 }
Brian King76b9cfc2007-08-03 13:55:19 +10001577
Santiago Leon517e80e2010-09-03 18:29:25 +00001578 if (i == IBMVETH_NUM_BUFF_POOLS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001579 netdev_err(netdev, "no active pool >= MTU\n");
Santiago Leon860f2422006-04-25 11:19:59 -05001580 return -EPERM;
1581 }
Brian King76b9cfc2007-08-03 13:55:19 +10001582
Brian King76b9cfc2007-08-03 13:55:19 +10001583 if (netif_running(netdev)) {
1584 adapter->pool_config = 1;
1585 ibmveth_close(netdev);
Santiago Leonea866e62008-07-24 04:34:23 +10001586 pool->active = 0;
Brian King76b9cfc2007-08-03 13:55:19 +10001587 adapter->pool_config = 0;
1588 if ((rc = ibmveth_open(netdev)))
1589 return rc;
1590 }
Santiago Leonea866e62008-07-24 04:34:23 +10001591 pool->active = 0;
Santiago Leon860f2422006-04-25 11:19:59 -05001592 }
1593 } else if (attr == &veth_num_attr) {
Santiago Leonf148f612010-09-03 18:29:30 +00001594 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT) {
Santiago Leon860f2422006-04-25 11:19:59 -05001595 return -EINVAL;
Santiago Leonf148f612010-09-03 18:29:30 +00001596 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001597 if (netif_running(netdev)) {
1598 adapter->pool_config = 1;
1599 ibmveth_close(netdev);
1600 adapter->pool_config = 0;
1601 pool->size = value;
1602 if ((rc = ibmveth_open(netdev)))
1603 return rc;
Santiago Leonf148f612010-09-03 18:29:30 +00001604 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001605 pool->size = value;
Santiago Leonf148f612010-09-03 18:29:30 +00001606 }
Santiago Leon860f2422006-04-25 11:19:59 -05001607 }
1608 } else if (attr == &veth_size_attr) {
Santiago Leonf148f612010-09-03 18:29:30 +00001609 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE) {
Santiago Leon860f2422006-04-25 11:19:59 -05001610 return -EINVAL;
Santiago Leonf148f612010-09-03 18:29:30 +00001611 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001612 if (netif_running(netdev)) {
1613 adapter->pool_config = 1;
1614 ibmveth_close(netdev);
1615 adapter->pool_config = 0;
1616 pool->buff_size = value;
1617 if ((rc = ibmveth_open(netdev)))
1618 return rc;
Santiago Leonf148f612010-09-03 18:29:30 +00001619 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001620 pool->buff_size = value;
Santiago Leonf148f612010-09-03 18:29:30 +00001621 }
Santiago Leon860f2422006-04-25 11:19:59 -05001622 }
1623 }
1624
1625 /* kick the interrupt handler to allocate/deallocate pools */
David Howells7d12e782006-10-05 14:55:46 +01001626 ibmveth_interrupt(netdev->irq, netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001627 return count;
1628}
1629
1630
Santiago Leonf148f612010-09-03 18:29:30 +00001631#define ATTR(_name, _mode) \
1632 struct attribute veth_##_name##_attr = { \
1633 .name = __stringify(_name), .mode = _mode, \
1634 };
Santiago Leon860f2422006-04-25 11:19:59 -05001635
1636static ATTR(active, 0644);
1637static ATTR(num, 0644);
1638static ATTR(size, 0644);
1639
Santiago Leonf148f612010-09-03 18:29:30 +00001640static struct attribute *veth_pool_attrs[] = {
Santiago Leon860f2422006-04-25 11:19:59 -05001641 &veth_active_attr,
1642 &veth_num_attr,
1643 &veth_size_attr,
1644 NULL,
1645};
1646
Emese Revfy52cf25d2010-01-19 02:58:23 +01001647static const struct sysfs_ops veth_pool_ops = {
Santiago Leon860f2422006-04-25 11:19:59 -05001648 .show = veth_pool_show,
1649 .store = veth_pool_store,
1650};
1651
1652static struct kobj_type ktype_veth_pool = {
1653 .release = NULL,
1654 .sysfs_ops = &veth_pool_ops,
1655 .default_attrs = veth_pool_attrs,
1656};
1657
Brian Kinge7a3af52010-05-07 08:56:08 +00001658static int ibmveth_resume(struct device *dev)
1659{
1660 struct net_device *netdev = dev_get_drvdata(dev);
1661 ibmveth_interrupt(netdev->irq, netdev);
1662 return 0;
1663}
Santiago Leon860f2422006-04-25 11:19:59 -05001664
Bill Pembertone11787a2012-12-03 09:23:12 -05001665static struct vio_device_id ibmveth_device_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 { "network", "IBM,l-lan"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +10001667 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1670
Brian Kinge7a3af52010-05-07 08:56:08 +00001671static struct dev_pm_ops ibmveth_pm_ops = {
1672 .resume = ibmveth_resume
1673};
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675static struct vio_driver ibmveth_driver = {
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001676 .id_table = ibmveth_device_table,
1677 .probe = ibmveth_probe,
1678 .remove = ibmveth_remove,
Robert Jennings1096d632008-07-24 04:34:52 +10001679 .get_desired_dma = ibmveth_get_desired_dma,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +00001680 .name = ibmveth_driver_name,
1681 .pm = &ibmveth_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682};
1683
1684static int __init ibmveth_module_init(void)
1685{
Santiago Leon21c2dec2010-09-03 18:29:19 +00001686 printk(KERN_DEBUG "%s: %s %s\n", ibmveth_driver_name,
1687 ibmveth_driver_string, ibmveth_driver_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return vio_register_driver(&ibmveth_driver);
1690}
1691
1692static void __exit ibmveth_module_exit(void)
1693{
1694 vio_unregister_driver(&ibmveth_driver);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001695}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697module_init(ibmveth_module_init);
1698module_exit(ibmveth_module_exit);