blob: 33ffd349fb054f3b930a76115419a9e09a2f1ca0 [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) },
Brian Kingddbb4de2007-08-17 09:16:43 -0500104};
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* simple methods of getting data from the current rxq entry */
Brian King79ef4a42007-08-17 09:16:56 -0500107static inline u32 ibmveth_rxq_flags(struct ibmveth_adapter *adapter)
108{
Anton Blanchard0b536be2013-09-03 09:55:32 +1000109 return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].flags_off);
Brian King79ef4a42007-08-17 09:16:56 -0500110}
111
112static inline int ibmveth_rxq_toggle(struct ibmveth_adapter *adapter)
113{
Santiago Leonf148f612010-09-03 18:29:30 +0000114 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_TOGGLE) >>
115 IBMVETH_RXQ_TOGGLE_SHIFT;
Brian King79ef4a42007-08-17 09:16:56 -0500116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
119{
Santiago Leonf148f612010-09-03 18:29:30 +0000120 return ibmveth_rxq_toggle(adapter) == adapter->rx_queue.toggle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
124{
Santiago Leonf148f612010-09-03 18:29:30 +0000125 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
129{
Santiago Leonf148f612010-09-03 18:29:30 +0000130 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_OFF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
134{
Anton Blanchard0b536be2013-09-03 09:55:32 +1000135 return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Brian Kingf4ff2872007-09-15 13:36:07 -0700138static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
139{
Santiago Leonf148f612010-09-03 18:29:30 +0000140 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_CSUM_GOOD;
Brian Kingf4ff2872007-09-15 13:36:07 -0700141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/* setup the initial settings for a buffer pool */
Santiago Leonf148f612010-09-03 18:29:30 +0000144static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool,
145 u32 pool_index, u32 pool_size,
146 u32 buff_size, u32 pool_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 pool->size = pool_size;
149 pool->index = pool_index;
150 pool->buff_size = buff_size;
Santiago Leonc033a6d2010-09-03 18:28:09 +0000151 pool->threshold = pool_size * 7 / 8;
Santiago Leon860f2422006-04-25 11:19:59 -0500152 pool->active = pool_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155/* allocate and setup an buffer pool - called during open */
156static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
157{
158 int i;
159
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400160 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Santiago Leonf148f612010-09-03 18:29:30 +0000162 if (!pool->free_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400165 pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
Santiago Leonf148f612010-09-03 18:29:30 +0000166 if (!pool->dma_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 kfree(pool->free_map);
168 pool->free_map = NULL;
169 return -1;
170 }
171
Julia Lawalla05abcb2010-05-13 10:06:01 +0000172 pool->skbuff = kcalloc(pool->size, sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Santiago Leonf148f612010-09-03 18:29:30 +0000174 if (!pool->skbuff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 kfree(pool->dma_addr);
176 pool->dma_addr = NULL;
177
178 kfree(pool->free_map);
179 pool->free_map = NULL;
180 return -1;
181 }
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
184
Santiago Leonf148f612010-09-03 18:29:30 +0000185 for (i = 0; i < pool->size; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 pool->free_map[i] = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 atomic_set(&pool->available, 0);
189 pool->producer_index = 0;
190 pool->consumer_index = 0;
191
192 return 0;
193}
194
Santiago Leon0c26b672010-09-03 18:28:41 +0000195static inline void ibmveth_flush_buffer(void *addr, unsigned long length)
196{
197 unsigned long offset;
198
199 for (offset = 0; offset < length; offset += SMP_CACHE_BYTES)
200 asm("dcbfl %0,%1" :: "b" (addr), "r" (offset));
201}
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/* replenish the buffers for a pool. note that we don't need to
204 * skb_reserve these since they are used for incoming...
205 */
Santiago Leonf148f612010-09-03 18:29:30 +0000206static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter,
207 struct ibmveth_buff_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 u32 i;
210 u32 count = pool->size - atomic_read(&pool->available);
211 u32 buffers_added = 0;
Robert Jennings1096d632008-07-24 04:34:52 +1000212 struct sk_buff *skb;
213 unsigned int free_index, index;
214 u64 correlator;
215 unsigned long lpar_rc;
216 dma_addr_t dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 mb();
219
Santiago Leonf148f612010-09-03 18:29:30 +0000220 for (i = 0; i < count; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 union ibmveth_buf_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Santiago Leon003212c2010-09-03 18:29:03 +0000223 skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Santiago Leonf148f612010-09-03 18:29:30 +0000225 if (!skb) {
Santiago Leonc43ced12010-09-03 18:29:14 +0000226 netdev_dbg(adapter->netdev,
227 "replenish: unable to allocate skb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 adapter->replenish_no_mem++;
229 break;
230 }
231
David Gibson047a66d2006-10-21 10:24:13 -0700232 free_index = pool->consumer_index;
Santiago Leona613f582010-09-03 18:28:04 +0000233 pool->consumer_index++;
234 if (pool->consumer_index >= pool->size)
235 pool->consumer_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 index = pool->free_map[free_index];
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400237
Santiago Leon64859112010-09-03 18:29:41 +0000238 BUG_ON(index == IBM_VETH_INVALID_MAP);
239 BUG_ON(pool->skbuff[index] != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
242 pool->buff_size, DMA_FROM_DEVICE);
243
Stephen Rothwellc713e7c2008-07-28 02:14:24 +1000244 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
Robert Jennings1096d632008-07-24 04:34:52 +1000245 goto failure;
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
248 pool->dma_addr[index] = dma_addr;
249 pool->skbuff[index] = skb;
250
251 correlator = ((u64)pool->index << 32) | index;
Santiago Leonf148f612010-09-03 18:29:30 +0000252 *(u64 *)skb->data = correlator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Brian King79ef4a42007-08-17 09:16:56 -0500254 desc.fields.flags_len = IBMVETH_BUF_VALID | pool->buff_size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400255 desc.fields.address = dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Santiago Leon0c26b672010-09-03 18:28:41 +0000257 if (rx_flush) {
258 unsigned int len = min(pool->buff_size,
259 adapter->netdev->mtu +
260 IBMVETH_BUFF_OH);
261 ibmveth_flush_buffer(skb->data, len);
262 }
Santiago Leonf148f612010-09-03 18:29:30 +0000263 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address,
264 desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400265
Santiago Leonf148f612010-09-03 18:29:30 +0000266 if (lpar_rc != H_SUCCESS) {
Robert Jennings1096d632008-07-24 04:34:52 +1000267 goto failure;
Santiago Leonf148f612010-09-03 18:29:30 +0000268 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 buffers_added++;
270 adapter->replenish_add_buff_success++;
271 }
272 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 mb();
275 atomic_add(buffers_added, &(pool->available));
Robert Jennings1096d632008-07-24 04:34:52 +1000276 return;
277
278failure:
279 pool->free_map[free_index] = index;
280 pool->skbuff[index] = NULL;
281 if (pool->consumer_index == 0)
282 pool->consumer_index = pool->size - 1;
283 else
284 pool->consumer_index--;
Stephen Rothwellc713e7c2008-07-28 02:14:24 +1000285 if (!dma_mapping_error(&adapter->vdev->dev, dma_addr))
Robert Jennings1096d632008-07-24 04:34:52 +1000286 dma_unmap_single(&adapter->vdev->dev,
287 pool->dma_addr[index], pool->buff_size,
288 DMA_FROM_DEVICE);
289 dev_kfree_skb_any(skb);
290 adapter->replenish_add_buff_failure++;
291
292 mb();
293 atomic_add(buffers_added, &(pool->available));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Anton Blanchardcbd52282014-08-22 11:36:52 +1000296/*
297 * The final 8 bytes of the buffer list is a counter of frames dropped
298 * because there was not a buffer in the buffer list capable of holding
299 * the frame.
300 */
301static void ibmveth_update_rx_no_buffer(struct ibmveth_adapter *adapter)
302{
303 __be64 *p = adapter->buffer_list_addr + 4096 - 8;
304
305 adapter->rx_no_buffer = be64_to_cpup(p);
306}
307
Santiago Leone2adbcb2005-10-26 10:47:08 -0600308/* replenish routine */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400309static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Santiago Leonb6d35182005-10-26 10:47:01 -0600311 int i;
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 adapter->replenish_task_cycles++;
314
Santiago Leon517e80e2010-09-03 18:29:25 +0000315 for (i = (IBMVETH_NUM_BUFF_POOLS - 1); i >= 0; i--) {
Santiago Leonc033a6d2010-09-03 18:28:09 +0000316 struct ibmveth_buff_pool *pool = &adapter->rx_buff_pool[i];
317
318 if (pool->active &&
319 (atomic_read(&pool->available) < pool->threshold))
320 ibmveth_replenish_buffer_pool(adapter, pool);
321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Anton Blanchardcbd52282014-08-22 11:36:52 +1000323 ibmveth_update_rx_no_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326/* empty and free ana buffer pool - also used to do cleanup in error paths */
Santiago Leonf148f612010-09-03 18:29:30 +0000327static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter,
328 struct ibmveth_buff_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 int i;
331
Jesper Juhlb4558ea2005-10-28 16:53:13 -0400332 kfree(pool->free_map);
333 pool->free_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Santiago Leonf148f612010-09-03 18:29:30 +0000335 if (pool->skbuff && pool->dma_addr) {
336 for (i = 0; i < pool->size; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct sk_buff *skb = pool->skbuff[i];
Santiago Leonf148f612010-09-03 18:29:30 +0000338 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 dma_unmap_single(&adapter->vdev->dev,
340 pool->dma_addr[i],
341 pool->buff_size,
342 DMA_FROM_DEVICE);
343 dev_kfree_skb_any(skb);
344 pool->skbuff[i] = NULL;
345 }
346 }
347 }
348
Santiago Leonf148f612010-09-03 18:29:30 +0000349 if (pool->dma_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 kfree(pool->dma_addr);
351 pool->dma_addr = NULL;
352 }
353
Santiago Leonf148f612010-09-03 18:29:30 +0000354 if (pool->skbuff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 kfree(pool->skbuff);
356 pool->skbuff = NULL;
357 }
358}
359
360/* remove a buffer from a pool */
Santiago Leonf148f612010-09-03 18:29:30 +0000361static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter,
362 u64 correlator)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 unsigned int pool = correlator >> 32;
365 unsigned int index = correlator & 0xffffffffUL;
366 unsigned int free_index;
367 struct sk_buff *skb;
368
Santiago Leon64859112010-09-03 18:29:41 +0000369 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
370 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 skb = adapter->rx_buff_pool[pool].skbuff[index];
373
Santiago Leon64859112010-09-03 18:29:41 +0000374 BUG_ON(skb == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
377
378 dma_unmap_single(&adapter->vdev->dev,
379 adapter->rx_buff_pool[pool].dma_addr[index],
380 adapter->rx_buff_pool[pool].buff_size,
381 DMA_FROM_DEVICE);
382
David Gibson047a66d2006-10-21 10:24:13 -0700383 free_index = adapter->rx_buff_pool[pool].producer_index;
Santiago Leona613f582010-09-03 18:28:04 +0000384 adapter->rx_buff_pool[pool].producer_index++;
385 if (adapter->rx_buff_pool[pool].producer_index >=
386 adapter->rx_buff_pool[pool].size)
387 adapter->rx_buff_pool[pool].producer_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 adapter->rx_buff_pool[pool].free_map[free_index] = index;
389
390 mb();
391
392 atomic_dec(&(adapter->rx_buff_pool[pool].available));
393}
394
395/* get the current buffer on the rx queue */
396static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
397{
398 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
399 unsigned int pool = correlator >> 32;
400 unsigned int index = correlator & 0xffffffffUL;
401
Santiago Leon64859112010-09-03 18:29:41 +0000402 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
403 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 return adapter->rx_buff_pool[pool].skbuff[index];
406}
407
408/* recycle the current buffer on the rx queue */
David S. Miller8decf862011-09-22 03:23:13 -0400409static int ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 u32 q_index = adapter->rx_queue.index;
412 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
413 unsigned int pool = correlator >> 32;
414 unsigned int index = correlator & 0xffffffffUL;
415 union ibmveth_buf_desc desc;
416 unsigned long lpar_rc;
David S. Miller8decf862011-09-22 03:23:13 -0400417 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Santiago Leon64859112010-09-03 18:29:41 +0000419 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
420 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Santiago Leonf148f612010-09-03 18:29:30 +0000422 if (!adapter->rx_buff_pool[pool].active) {
Santiago Leonb6d35182005-10-26 10:47:01 -0600423 ibmveth_rxq_harvest_buffer(adapter);
424 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
David S. Miller8decf862011-09-22 03:23:13 -0400425 goto out;
Santiago Leonb6d35182005-10-26 10:47:01 -0600426 }
427
Brian King79ef4a42007-08-17 09:16:56 -0500428 desc.fields.flags_len = IBMVETH_BUF_VALID |
429 adapter->rx_buff_pool[pool].buff_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
431
432 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400433
Santiago Leonf148f612010-09-03 18:29:30 +0000434 if (lpar_rc != H_SUCCESS) {
Santiago Leonc43ced12010-09-03 18:29:14 +0000435 netdev_dbg(adapter->netdev, "h_add_logical_lan_buffer failed "
436 "during recycle rc=%ld", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
David S. Miller8decf862011-09-22 03:23:13 -0400438 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
Santiago Leonf148f612010-09-03 18:29:30 +0000441 if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 adapter->rx_queue.index = 0;
443 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
444 }
David S. Miller8decf862011-09-22 03:23:13 -0400445
446out:
447 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Michael Ellerman493a6842007-04-17 13:12:55 +1000450static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
453
Santiago Leonf148f612010-09-03 18:29:30 +0000454 if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 adapter->rx_queue.index = 0;
456 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
457 }
458}
459
460static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
461{
Santiago Leonb6d35182005-10-26 10:47:01 -0600462 int i;
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700463 struct device *dev = &adapter->vdev->dev;
Santiago Leonb6d35182005-10-26 10:47:01 -0600464
Santiago Leonf148f612010-09-03 18:29:30 +0000465 if (adapter->buffer_list_addr != NULL) {
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700466 if (!dma_mapping_error(dev, adapter->buffer_list_dma)) {
467 dma_unmap_single(dev, adapter->buffer_list_dma, 4096,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 DMA_BIDIRECTIONAL);
469 adapter->buffer_list_dma = DMA_ERROR_CODE;
470 }
471 free_page((unsigned long)adapter->buffer_list_addr);
472 adapter->buffer_list_addr = NULL;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Santiago Leonf148f612010-09-03 18:29:30 +0000475 if (adapter->filter_list_addr != NULL) {
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700476 if (!dma_mapping_error(dev, adapter->filter_list_dma)) {
477 dma_unmap_single(dev, adapter->filter_list_dma, 4096,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 DMA_BIDIRECTIONAL);
479 adapter->filter_list_dma = DMA_ERROR_CODE;
480 }
481 free_page((unsigned long)adapter->filter_list_addr);
482 adapter->filter_list_addr = NULL;
483 }
484
Santiago Leonf148f612010-09-03 18:29:30 +0000485 if (adapter->rx_queue.queue_addr != NULL) {
Santiago Leond90c92f2012-09-04 14:41:37 +0000486 dma_free_coherent(dev, adapter->rx_queue.queue_len,
487 adapter->rx_queue.queue_addr,
488 adapter->rx_queue.queue_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 adapter->rx_queue.queue_addr = NULL;
490 }
491
Santiago Leonf148f612010-09-03 18:29:30 +0000492 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Santiago Leon860f2422006-04-25 11:19:59 -0500493 if (adapter->rx_buff_pool[i].active)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400494 ibmveth_free_buffer_pool(adapter,
Santiago Leon860f2422006-04-25 11:19:59 -0500495 &adapter->rx_buff_pool[i]);
Robert Jennings1096d632008-07-24 04:34:52 +1000496
497 if (adapter->bounce_buffer != NULL) {
Stephen Rothwellc713e7c2008-07-28 02:14:24 +1000498 if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
Robert Jennings1096d632008-07-24 04:34:52 +1000499 dma_unmap_single(&adapter->vdev->dev,
500 adapter->bounce_buffer_dma,
501 adapter->netdev->mtu + IBMVETH_BUFF_OH,
502 DMA_BIDIRECTIONAL);
503 adapter->bounce_buffer_dma = DMA_ERROR_CODE;
504 }
505 kfree(adapter->bounce_buffer);
506 adapter->bounce_buffer = NULL;
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500510static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
511 union ibmveth_buf_desc rxq_desc, u64 mac_address)
512{
513 int rc, try_again = 1;
514
Santiago Leonf148f612010-09-03 18:29:30 +0000515 /*
516 * After a kexec the adapter will still be open, so our attempt to
517 * open it will fail. So if we get a failure we free the adapter and
518 * try again, but only once.
519 */
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500520retry:
521 rc = h_register_logical_lan(adapter->vdev->unit_address,
522 adapter->buffer_list_dma, rxq_desc.desc,
523 adapter->filter_list_dma, mac_address);
524
525 if (rc != H_SUCCESS && try_again) {
526 do {
527 rc = h_free_logical_lan(adapter->vdev->unit_address);
528 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
529
530 try_again = 0;
531 goto retry;
532 }
533
534 return rc;
535}
536
Anton Blanchardd746ca92014-03-05 14:51:37 +1100537static u64 ibmveth_encode_mac_addr(u8 *mac)
538{
539 int i;
540 u64 encoded = 0;
541
542 for (i = 0; i < ETH_ALEN; i++)
543 encoded = (encoded << 8) | mac[i];
544
545 return encoded;
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548static int ibmveth_open(struct net_device *netdev)
549{
Wang Chen4cf16532008-11-12 23:38:14 -0800550 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Anton Blanchardd746ca92014-03-05 14:51:37 +1100551 u64 mac_address;
Santiago Leonb6d35182005-10-26 10:47:01 -0600552 int rxq_entries = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 unsigned long lpar_rc;
554 int rc;
555 union ibmveth_buf_desc rxq_desc;
Santiago Leonb6d35182005-10-26 10:47:01 -0600556 int i;
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700557 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Santiago Leonc43ced12010-09-03 18:29:14 +0000559 netdev_dbg(netdev, "open starting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700561 napi_enable(&adapter->napi);
562
Santiago Leon517e80e2010-09-03 18:29:25 +0000563 for(i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Santiago Leonb6d35182005-10-26 10:47:01 -0600564 rxq_entries += adapter->rx_buff_pool[i].size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
567 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400568
Santiago Leonf148f612010-09-03 18:29:30 +0000569 if (!adapter->buffer_list_addr || !adapter->filter_list_addr) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000570 netdev_err(netdev, "unable to allocate filter or buffer list "
571 "pages\n");
Denis Kirjanov88426f22010-10-20 04:21:13 +0000572 rc = -ENOMEM;
573 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
Santiago Leond90c92f2012-09-04 14:41:37 +0000576 dev = &adapter->vdev->dev;
577
Santiago Leonf148f612010-09-03 18:29:30 +0000578 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) *
579 rxq_entries;
Santiago Leond90c92f2012-09-04 14:41:37 +0000580 adapter->rx_queue.queue_addr =
Joe Perchesd0320f72013-03-14 13:07:21 +0000581 dma_alloc_coherent(dev, adapter->rx_queue.queue_len,
582 &adapter->rx_queue.queue_dma, GFP_KERNEL);
Santiago Leonf148f612010-09-03 18:29:30 +0000583 if (!adapter->rx_queue.queue_addr) {
Denis Kirjanov88426f22010-10-20 04:21:13 +0000584 rc = -ENOMEM;
585 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700588 adapter->buffer_list_dma = dma_map_single(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700590 adapter->filter_list_dma = dma_map_single(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700593 if ((dma_mapping_error(dev, adapter->buffer_list_dma)) ||
Santiago Leond90c92f2012-09-04 14:41:37 +0000594 (dma_mapping_error(dev, adapter->filter_list_dma))) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000595 netdev_err(netdev, "unable to map filter or buffer list "
596 "pages\n");
Denis Kirjanov88426f22010-10-20 04:21:13 +0000597 rc = -ENOMEM;
598 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600
601 adapter->rx_queue.index = 0;
602 adapter->rx_queue.num_slots = rxq_entries;
603 adapter->rx_queue.toggle = 1;
604
Anton Blanchardd746ca92014-03-05 14:51:37 +1100605 mac_address = ibmveth_encode_mac_addr(netdev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Santiago Leonf148f612010-09-03 18:29:30 +0000607 rxq_desc.fields.flags_len = IBMVETH_BUF_VALID |
608 adapter->rx_queue.queue_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
610
Santiago Leonc43ced12010-09-03 18:29:14 +0000611 netdev_dbg(netdev, "buffer list @ 0x%p\n", adapter->buffer_list_addr);
612 netdev_dbg(netdev, "filter list @ 0x%p\n", adapter->filter_list_addr);
613 netdev_dbg(netdev, "receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Santiago Leon4347ef12006-10-03 12:24:34 -0500615 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
616
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500617 lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Santiago Leonf148f612010-09-03 18:29:30 +0000619 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000620 netdev_err(netdev, "h_register_logical_lan failed with %ld\n",
621 lpar_rc);
622 netdev_err(netdev, "buffer TCE:0x%llx filter TCE:0x%llx rxq "
623 "desc:0x%llx MAC:0x%llx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 adapter->buffer_list_dma,
625 adapter->filter_list_dma,
626 rxq_desc.desc,
627 mac_address);
Denis Kirjanov88426f22010-10-20 04:21:13 +0000628 rc = -ENONET;
629 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
Santiago Leonf148f612010-09-03 18:29:30 +0000632 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
633 if (!adapter->rx_buff_pool[i].active)
Santiago Leon860f2422006-04-25 11:19:59 -0500634 continue;
635 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000636 netdev_err(netdev, "unable to alloc pool\n");
Santiago Leon860f2422006-04-25 11:19:59 -0500637 adapter->rx_buff_pool[i].active = 0;
Denis Kirjanov88426f22010-10-20 04:21:13 +0000638 rc = -ENOMEM;
639 goto err_out;
Santiago Leon860f2422006-04-25 11:19:59 -0500640 }
641 }
642
Santiago Leonc43ced12010-09-03 18:29:14 +0000643 netdev_dbg(netdev, "registering irq 0x%x\n", netdev->irq);
Santiago Leonf148f612010-09-03 18:29:30 +0000644 rc = request_irq(netdev->irq, ibmveth_interrupt, 0, netdev->name,
645 netdev);
646 if (rc != 0) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000647 netdev_err(netdev, "unable to request irq 0x%x, rc %d\n",
648 netdev->irq, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 do {
David S. Miller88c51002011-10-07 13:38:43 -0400650 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
651 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Denis Kirjanov88426f22010-10-20 04:21:13 +0000653 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655
Robert Jennings1096d632008-07-24 04:34:52 +1000656 adapter->bounce_buffer =
657 kmalloc(netdev->mtu + IBMVETH_BUFF_OH, GFP_KERNEL);
658 if (!adapter->bounce_buffer) {
Denis Kirjanov88426f22010-10-20 04:21:13 +0000659 rc = -ENOMEM;
Denis Kirjanove0e8ab52010-10-20 04:21:51 +0000660 goto err_out_free_irq;
Robert Jennings1096d632008-07-24 04:34:52 +1000661 }
662 adapter->bounce_buffer_dma =
663 dma_map_single(&adapter->vdev->dev, adapter->bounce_buffer,
664 netdev->mtu + IBMVETH_BUFF_OH, DMA_BIDIRECTIONAL);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700665 if (dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000666 netdev_err(netdev, "unable to map bounce buffer\n");
Denis Kirjanov88426f22010-10-20 04:21:13 +0000667 rc = -ENOMEM;
Denis Kirjanove0e8ab52010-10-20 04:21:51 +0000668 goto err_out_free_irq;
Robert Jennings1096d632008-07-24 04:34:52 +1000669 }
670
Santiago Leonc43ced12010-09-03 18:29:14 +0000671 netdev_dbg(netdev, "initial replenish cycle\n");
David Howells7d12e782006-10-05 14:55:46 +0100672 ibmveth_interrupt(netdev->irq, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Santiago Leone2adbcb2005-10-26 10:47:08 -0600674 netif_start_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Santiago Leonc43ced12010-09-03 18:29:14 +0000676 netdev_dbg(netdev, "open complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 return 0;
Denis Kirjanov88426f22010-10-20 04:21:13 +0000679
Denis Kirjanove0e8ab52010-10-20 04:21:51 +0000680err_out_free_irq:
681 free_irq(netdev->irq, netdev);
Denis Kirjanov88426f22010-10-20 04:21:13 +0000682err_out:
683 ibmveth_cleanup(adapter);
684 napi_disable(&adapter->napi);
685 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688static int ibmveth_close(struct net_device *netdev)
689{
Wang Chen4cf16532008-11-12 23:38:14 -0800690 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 long lpar_rc;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400692
Santiago Leonc43ced12010-09-03 18:29:14 +0000693 netdev_dbg(netdev, "close starting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700695 napi_disable(&adapter->napi);
696
Santiago Leon860f2422006-04-25 11:19:59 -0500697 if (!adapter->pool_config)
698 netif_stop_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Robert Jenningsee2e6112010-07-16 04:57:25 +0000700 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 do {
703 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200704 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Santiago Leonf148f612010-09-03 18:29:30 +0000706 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000707 netdev_err(netdev, "h_free_logical_lan failed with %lx, "
708 "continuing with close\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
Robert Jenningsee2e6112010-07-16 04:57:25 +0000711 free_irq(netdev->irq, netdev);
712
Anton Blanchardcbd52282014-08-22 11:36:52 +1000713 ibmveth_update_rx_no_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 ibmveth_cleanup(adapter);
716
Santiago Leonc43ced12010-09-03 18:29:14 +0000717 netdev_dbg(netdev, "close complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 return 0;
720}
721
Santiago Leonf148f612010-09-03 18:29:30 +0000722static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
723{
724 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
725 SUPPORTED_FIBRE);
726 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
727 ADVERTISED_FIBRE);
David Decotigny70739492011-04-27 18:32:40 +0000728 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 cmd->duplex = DUPLEX_FULL;
730 cmd->port = PORT_FIBRE;
731 cmd->phy_address = 0;
732 cmd->transceiver = XCVR_INTERNAL;
733 cmd->autoneg = AUTONEG_ENABLE;
734 cmd->maxtxpkt = 0;
735 cmd->maxrxpkt = 1;
736 return 0;
737}
738
Santiago Leonf148f612010-09-03 18:29:30 +0000739static void netdev_get_drvinfo(struct net_device *dev,
740 struct ethtool_drvinfo *info)
741{
Jiri Pirko7826d432013-01-06 00:44:26 +0000742 strlcpy(info->driver, ibmveth_driver_name, sizeof(info->driver));
743 strlcpy(info->version, ibmveth_driver_version, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000746static netdev_features_t ibmveth_fix_features(struct net_device *dev,
747 netdev_features_t features)
Brian King5fc7e012007-08-17 09:16:31 -0500748{
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000749 /*
750 * Since the ibmveth firmware interface does not have the
751 * concept of separate tx/rx checksum offload enable, if rx
752 * checksum is disabled we also have to disable tx checksum
753 * offload. Once we disable rx checksum offload, we are no
754 * longer allowed to send tx buffers that are not properly
755 * checksummed.
756 */
Brian King5fc7e012007-08-17 09:16:31 -0500757
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000758 if (!(features & NETIF_F_RXCSUM))
759 features &= ~NETIF_F_ALL_CSUM;
760
761 return features;
Brian King5fc7e012007-08-17 09:16:31 -0500762}
763
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000764static int ibmveth_set_csum_offload(struct net_device *dev, u32 data)
Brian King5fc7e012007-08-17 09:16:31 -0500765{
Wang Chen4cf16532008-11-12 23:38:14 -0800766 struct ibmveth_adapter *adapter = netdev_priv(dev);
Stephen Rothwellff5bfc32009-01-06 10:47:44 -0800767 unsigned long set_attr, clr_attr, ret_attr;
Santiago Leonab78df72010-09-03 18:28:52 +0000768 unsigned long set_attr6, clr_attr6;
David S. Miller8decf862011-09-22 03:23:13 -0400769 long ret, ret4, ret6;
Brian King5fc7e012007-08-17 09:16:31 -0500770 int rc1 = 0, rc2 = 0;
771 int restart = 0;
772
773 if (netif_running(dev)) {
774 restart = 1;
775 adapter->pool_config = 1;
776 ibmveth_close(dev);
777 adapter->pool_config = 0;
778 }
779
Brian King79ef4a42007-08-17 09:16:56 -0500780 set_attr = 0;
781 clr_attr = 0;
David S. Miller8decf862011-09-22 03:23:13 -0400782 set_attr6 = 0;
783 clr_attr6 = 0;
Brian King5fc7e012007-08-17 09:16:31 -0500784
Santiago Leonab78df72010-09-03 18:28:52 +0000785 if (data) {
Brian King79ef4a42007-08-17 09:16:56 -0500786 set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Santiago Leonab78df72010-09-03 18:28:52 +0000787 set_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
788 } else {
Brian King79ef4a42007-08-17 09:16:56 -0500789 clr_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Santiago Leonab78df72010-09-03 18:28:52 +0000790 clr_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
791 }
Brian King5fc7e012007-08-17 09:16:31 -0500792
Brian King79ef4a42007-08-17 09:16:56 -0500793 ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500794
Brian King79ef4a42007-08-17 09:16:56 -0500795 if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
796 !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
797 (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
David S. Miller8decf862011-09-22 03:23:13 -0400798 ret4 = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
Brian King79ef4a42007-08-17 09:16:56 -0500799 set_attr, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500800
David S. Miller8decf862011-09-22 03:23:13 -0400801 if (ret4 != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000802 netdev_err(dev, "unable to change IPv4 checksum "
803 "offload settings. %d rc=%ld\n",
David S. Miller8decf862011-09-22 03:23:13 -0400804 data, ret4);
Brian King5fc7e012007-08-17 09:16:31 -0500805
David S. Miller8decf862011-09-22 03:23:13 -0400806 h_illan_attributes(adapter->vdev->unit_address,
807 set_attr, clr_attr, &ret_attr);
808
809 if (data == 1)
810 dev->features &= ~NETIF_F_IP_CSUM;
811
Santiago Leonf148f612010-09-03 18:29:30 +0000812 } else {
Santiago Leonab78df72010-09-03 18:28:52 +0000813 adapter->fw_ipv4_csum_support = data;
Santiago Leonf148f612010-09-03 18:29:30 +0000814 }
Santiago Leonab78df72010-09-03 18:28:52 +0000815
816 ret6 = h_illan_attributes(adapter->vdev->unit_address,
817 clr_attr6, set_attr6, &ret_attr);
818
819 if (ret6 != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000820 netdev_err(dev, "unable to change IPv6 checksum "
821 "offload settings. %d rc=%ld\n",
David S. Miller8decf862011-09-22 03:23:13 -0400822 data, ret6);
Santiago Leonab78df72010-09-03 18:28:52 +0000823
David S. Miller8decf862011-09-22 03:23:13 -0400824 h_illan_attributes(adapter->vdev->unit_address,
825 set_attr6, clr_attr6, &ret_attr);
826
827 if (data == 1)
828 dev->features &= ~NETIF_F_IPV6_CSUM;
829
Santiago Leonab78df72010-09-03 18:28:52 +0000830 } else
831 adapter->fw_ipv6_csum_support = data;
832
David S. Miller8decf862011-09-22 03:23:13 -0400833 if (ret4 == H_SUCCESS || ret6 == H_SUCCESS)
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000834 adapter->rx_csum = data;
Santiago Leonab78df72010-09-03 18:28:52 +0000835 else
836 rc1 = -EIO;
Brian King5fc7e012007-08-17 09:16:31 -0500837 } else {
838 rc1 = -EIO;
Santiago Leon21c2dec2010-09-03 18:29:19 +0000839 netdev_err(dev, "unable to change checksum offload settings."
840 " %d rc=%ld ret_attr=%lx\n", data, ret,
841 ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500842 }
843
844 if (restart)
845 rc2 = ibmveth_open(dev);
846
847 return rc1 ? rc1 : rc2;
848}
849
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000850static int ibmveth_set_features(struct net_device *dev,
851 netdev_features_t features)
Brian King5fc7e012007-08-17 09:16:31 -0500852{
Wang Chen4cf16532008-11-12 23:38:14 -0800853 struct ibmveth_adapter *adapter = netdev_priv(dev);
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000854 int rx_csum = !!(features & NETIF_F_RXCSUM);
855 int rc;
Thomas Falcon8641dd82015-04-29 16:25:45 -0500856 netdev_features_t changed = features ^ dev->features;
857
858 if (features & NETIF_F_TSO & changed)
859 netdev_info(dev, "TSO feature requires all partitions to have updated driver");
Brian King5fc7e012007-08-17 09:16:31 -0500860
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000861 if (rx_csum == adapter->rx_csum)
Brian King5fc7e012007-08-17 09:16:31 -0500862 return 0;
863
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000864 rc = ibmveth_set_csum_offload(dev, rx_csum);
865 if (rc && !adapter->rx_csum)
866 dev->features = features & ~(NETIF_F_ALL_CSUM | NETIF_F_RXCSUM);
Brian King5fc7e012007-08-17 09:16:31 -0500867
868 return rc;
869}
870
Brian Kingddbb4de2007-08-17 09:16:43 -0500871static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
872{
873 int i;
874
875 if (stringset != ETH_SS_STATS)
876 return;
877
878 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
879 memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
880}
881
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700882static int ibmveth_get_sset_count(struct net_device *dev, int sset)
Brian Kingddbb4de2007-08-17 09:16:43 -0500883{
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700884 switch (sset) {
885 case ETH_SS_STATS:
886 return ARRAY_SIZE(ibmveth_stats);
887 default:
888 return -EOPNOTSUPP;
889 }
Brian Kingddbb4de2007-08-17 09:16:43 -0500890}
891
892static void ibmveth_get_ethtool_stats(struct net_device *dev,
893 struct ethtool_stats *stats, u64 *data)
894{
895 int i;
Wang Chen4cf16532008-11-12 23:38:14 -0800896 struct ibmveth_adapter *adapter = netdev_priv(dev);
Brian Kingddbb4de2007-08-17 09:16:43 -0500897
898 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
899 data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
900}
901
Jeff Garzik7282d492006-09-13 14:30:00 -0400902static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 .get_drvinfo = netdev_get_drvinfo,
904 .get_settings = netdev_get_settings,
Ben Hutchingsed4ba4b2010-12-09 12:10:25 +0000905 .get_link = ethtool_op_get_link,
Brian Kingddbb4de2007-08-17 09:16:43 -0500906 .get_strings = ibmveth_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700907 .get_sset_count = ibmveth_get_sset_count,
Brian Kingddbb4de2007-08-17 09:16:43 -0500908 .get_ethtool_stats = ibmveth_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909};
910
911static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
912{
913 return -EOPNOTSUPP;
914}
915
916#define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
917
Santiago Leon6e8ab302010-09-03 18:28:36 +0000918static int ibmveth_send(struct ibmveth_adapter *adapter,
919 union ibmveth_buf_desc *descs)
920{
921 unsigned long correlator;
922 unsigned int retry_count;
923 unsigned long ret;
924
925 /*
926 * The retry count sets a maximum for the number of broadcast and
927 * multicast destinations within the system.
928 */
929 retry_count = 1024;
930 correlator = 0;
931 do {
932 ret = h_send_logical_lan(adapter->vdev->unit_address,
933 descs[0].desc, descs[1].desc,
934 descs[2].desc, descs[3].desc,
935 descs[4].desc, descs[5].desc,
936 correlator, &correlator);
937 } while ((ret == H_BUSY) && (retry_count--));
938
939 if (ret != H_SUCCESS && ret != H_DROPPED) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000940 netdev_err(adapter->netdev, "tx: h_send_logical_lan failed "
941 "with rc=%ld\n", ret);
Santiago Leon6e8ab302010-09-03 18:28:36 +0000942 return 1;
943 }
944
945 return 0;
946}
947
Stephen Hemminger613573252009-08-31 19:50:58 +0000948static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb,
949 struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Wang Chen4cf16532008-11-12 23:38:14 -0800951 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Santiago Leon6e8ab302010-09-03 18:28:36 +0000952 unsigned int desc_flags;
953 union ibmveth_buf_desc descs[6];
954 int last, i;
955 int force_bounce = 0;
David S. Miller8decf862011-09-22 03:23:13 -0400956 dma_addr_t dma_addr;
Santiago Leon60296d92005-10-26 10:47:16 -0600957
Santiago Leon6e8ab302010-09-03 18:28:36 +0000958 /*
959 * veth handles a maximum of 6 segments including the header, so
960 * we have to linearize the skb if there are more than this.
961 */
962 if (skb_shinfo(skb)->nr_frags > 5 && __skb_linearize(skb)) {
963 netdev->stats.tx_dropped++;
Brian Kingf4ff2872007-09-15 13:36:07 -0700964 goto out;
965 }
966
Santiago Leon6e8ab302010-09-03 18:28:36 +0000967 /* veth can't checksum offload UDP */
968 if (skb->ip_summed == CHECKSUM_PARTIAL &&
Santiago Leonab78df72010-09-03 18:28:52 +0000969 ((skb->protocol == htons(ETH_P_IP) &&
970 ip_hdr(skb)->protocol != IPPROTO_TCP) ||
971 (skb->protocol == htons(ETH_P_IPV6) &&
972 ipv6_hdr(skb)->nexthdr != IPPROTO_TCP)) &&
973 skb_checksum_help(skb)) {
974
Santiago Leon21c2dec2010-09-03 18:29:19 +0000975 netdev_err(netdev, "tx: failed to checksum packet\n");
Santiago Leon6e8ab302010-09-03 18:28:36 +0000976 netdev->stats.tx_dropped++;
977 goto out;
978 }
Brian Kingf4ff2872007-09-15 13:36:07 -0700979
Santiago Leon6e8ab302010-09-03 18:28:36 +0000980 desc_flags = IBMVETH_BUF_VALID;
981
982 if (skb->ip_summed == CHECKSUM_PARTIAL) {
983 unsigned char *buf = skb_transport_header(skb) +
984 skb->csum_offset;
985
986 desc_flags |= (IBMVETH_BUF_NO_CSUM | IBMVETH_BUF_CSUM_GOOD);
Brian Kingf4ff2872007-09-15 13:36:07 -0700987
988 /* Need to zero out the checksum */
989 buf[0] = 0;
990 buf[1] = 0;
991 }
992
Santiago Leon6e8ab302010-09-03 18:28:36 +0000993retry_bounce:
994 memset(descs, 0, sizeof(descs));
Santiago Leonc08cc3c2010-09-03 18:28:20 +0000995
Santiago Leon6e8ab302010-09-03 18:28:36 +0000996 /*
997 * If a linear packet is below the rx threshold then
998 * copy it into the static bounce buffer. This avoids the
999 * cost of a TCE insert and remove.
1000 */
1001 if (force_bounce || (!skb_is_nonlinear(skb) &&
1002 (skb->len < tx_copybreak))) {
Robert Jennings1096d632008-07-24 04:34:52 +10001003 skb_copy_from_linear_data(skb, adapter->bounce_buffer,
1004 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Santiago Leon6e8ab302010-09-03 18:28:36 +00001006 descs[0].fields.flags_len = desc_flags | skb->len;
1007 descs[0].fields.address = adapter->bounce_buffer_dma;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001008
Santiago Leon6e8ab302010-09-03 18:28:36 +00001009 if (ibmveth_send(adapter, descs)) {
1010 adapter->tx_send_failed++;
1011 netdev->stats.tx_dropped++;
1012 } else {
1013 netdev->stats.tx_packets++;
1014 netdev->stats.tx_bytes += skb->len;
1015 }
1016
1017 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019
Santiago Leon6e8ab302010-09-03 18:28:36 +00001020 /* Map the header */
David S. Miller8decf862011-09-22 03:23:13 -04001021 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
1022 skb_headlen(skb), DMA_TO_DEVICE);
1023 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
Santiago Leon6e8ab302010-09-03 18:28:36 +00001024 goto map_failed;
1025
1026 descs[0].fields.flags_len = desc_flags | skb_headlen(skb);
David S. Miller8decf862011-09-22 03:23:13 -04001027 descs[0].fields.address = dma_addr;
Santiago Leon6e8ab302010-09-03 18:28:36 +00001028
1029 /* Map the frags */
1030 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001031 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Santiago Leon6e8ab302010-09-03 18:28:36 +00001032
Ian Campbell8838a532011-08-31 00:46:53 +00001033 dma_addr = skb_frag_dma_map(&adapter->vdev->dev, frag, 0,
Eric Dumazet9e903e02011-10-18 21:00:24 +00001034 skb_frag_size(frag), DMA_TO_DEVICE);
Santiago Leon6e8ab302010-09-03 18:28:36 +00001035
1036 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
1037 goto map_failed_frags;
1038
Eric Dumazet9e903e02011-10-18 21:00:24 +00001039 descs[i+1].fields.flags_len = desc_flags | skb_frag_size(frag);
Santiago Leon6e8ab302010-09-03 18:28:36 +00001040 descs[i+1].fields.address = dma_addr;
1041 }
1042
Thomas Falcon8641dd82015-04-29 16:25:45 -05001043 if (skb_is_gso(skb) && !skb_is_gso_v6(skb)) {
1044 /* Put -1 in the IP checksum to tell phyp it
1045 * is a largesend packet and put the mss in the TCP checksum.
1046 */
1047 ip_hdr(skb)->check = 0xffff;
1048 tcp_hdr(skb)->check = cpu_to_be16(skb_shinfo(skb)->gso_size);
1049 adapter->tx_large_packets++;
1050 }
1051
Santiago Leon6e8ab302010-09-03 18:28:36 +00001052 if (ibmveth_send(adapter, descs)) {
1053 adapter->tx_send_failed++;
1054 netdev->stats.tx_dropped++;
1055 } else {
1056 netdev->stats.tx_packets++;
1057 netdev->stats.tx_bytes += skb->len;
1058 }
1059
David S. Miller8decf862011-09-22 03:23:13 -04001060 dma_unmap_single(&adapter->vdev->dev,
1061 descs[0].fields.address,
1062 descs[0].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1063 DMA_TO_DEVICE);
1064
1065 for (i = 1; i < skb_shinfo(skb)->nr_frags + 1; i++)
Santiago Leon6e8ab302010-09-03 18:28:36 +00001066 dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
1067 descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1068 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Santiago Leone8cb7eb2010-09-03 18:28:15 +00001070out:
Eric W. Biederman26faa9d2014-03-15 17:29:34 -07001071 dev_consume_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001072 return NETDEV_TX_OK;
Santiago Leon6e8ab302010-09-03 18:28:36 +00001073
1074map_failed_frags:
1075 last = i+1;
1076 for (i = 0; i < last; i++)
1077 dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
1078 descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1079 DMA_TO_DEVICE);
1080
1081map_failed:
1082 if (!firmware_has_feature(FW_FEATURE_CMO))
Santiago Leon21c2dec2010-09-03 18:29:19 +00001083 netdev_err(netdev, "tx: unable to map xmit buffer\n");
Santiago Leon6e8ab302010-09-03 18:28:36 +00001084 adapter->tx_map_failed++;
1085 skb_linearize(skb);
1086 force_bounce = 1;
1087 goto retry_bounce;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001090static int ibmveth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Santiago Leonf148f612010-09-03 18:29:30 +00001092 struct ibmveth_adapter *adapter =
1093 container_of(napi, struct ibmveth_adapter, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001094 struct net_device *netdev = adapter->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 int frames_processed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 unsigned long lpar_rc;
1097
Santiago Leonf148f612010-09-03 18:29:30 +00001098restart_poll:
Eric W. Biedermancb013ea2014-03-14 18:03:50 -07001099 while (frames_processed < budget) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001100 if (!ibmveth_rxq_pending_buffer(adapter))
1101 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Anton Blanchardf89e49e2010-09-06 18:21:41 -07001103 smp_rmb();
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001104 if (!ibmveth_rxq_buffer_valid(adapter)) {
1105 wmb(); /* suggested by larson1 */
1106 adapter->rx_invalid_buffer++;
Santiago Leonc43ced12010-09-03 18:29:14 +00001107 netdev_dbg(netdev, "recycling invalid buffer\n");
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001108 ibmveth_rxq_recycle_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 } else {
Santiago Leon8d86c612010-09-03 18:28:25 +00001110 struct sk_buff *skb, *new_skb;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001111 int length = ibmveth_rxq_frame_length(adapter);
1112 int offset = ibmveth_rxq_frame_offset(adapter);
Brian Kingf4ff2872007-09-15 13:36:07 -07001113 int csum_good = ibmveth_rxq_csum_good(adapter);
1114
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001115 skb = ibmveth_rxq_get_buffer(adapter);
1116
Santiago Leon8d86c612010-09-03 18:28:25 +00001117 new_skb = NULL;
1118 if (length < rx_copybreak)
1119 new_skb = netdev_alloc_skb(netdev, length);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001120
Santiago Leon8d86c612010-09-03 18:28:25 +00001121 if (new_skb) {
1122 skb_copy_to_linear_data(new_skb,
1123 skb->data + offset,
1124 length);
Santiago Leon0c26b672010-09-03 18:28:41 +00001125 if (rx_flush)
1126 ibmveth_flush_buffer(skb->data,
1127 length + offset);
David S. Miller8decf862011-09-22 03:23:13 -04001128 if (!ibmveth_rxq_recycle_buffer(adapter))
1129 kfree_skb(skb);
Santiago Leon8d86c612010-09-03 18:28:25 +00001130 skb = new_skb;
Santiago Leon8d86c612010-09-03 18:28:25 +00001131 } else {
1132 ibmveth_rxq_harvest_buffer(adapter);
1133 skb_reserve(skb, offset);
1134 }
1135
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001136 skb_put(skb, length);
1137 skb->protocol = eth_type_trans(skb, netdev);
1138
Santiago Leon8d86c612010-09-03 18:28:25 +00001139 if (csum_good)
1140 skb->ip_summed = CHECKSUM_UNNECESSARY;
1141
Thomas Falcon92ec8272015-04-29 16:25:46 -05001142 napi_gro_receive(napi, skb); /* send it up */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001143
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001144 netdev->stats.rx_packets++;
1145 netdev->stats.rx_bytes += length;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001146 frames_processed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
Eric W. Biedermancb013ea2014-03-14 18:03:50 -07001148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Santiago Leone2adbcb2005-10-26 10:47:08 -06001150 ibmveth_replenish_task(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001152 if (frames_processed < budget) {
Yongbae Park4736edc2015-03-10 11:15:39 +09001153 napi_complete(napi);
1154
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001155 /* We think we are done - reenable interrupts,
1156 * then check once more to make sure we are done.
1157 */
1158 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1159 VIO_IRQ_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Santiago Leon64859112010-09-03 18:29:41 +00001161 BUG_ON(lpar_rc != H_SUCCESS);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001162
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001163 if (ibmveth_rxq_pending_buffer(adapter) &&
Ben Hutchings288379f2009-01-19 16:43:59 -08001164 napi_reschedule(napi)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001165 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1166 VIO_IRQ_DISABLE);
1167 goto restart_poll;
1168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
1170
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001171 return frames_processed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
David Howells7d12e782006-10-05 14:55:46 +01001174static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001175{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 struct net_device *netdev = dev_instance;
Wang Chen4cf16532008-11-12 23:38:14 -08001177 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 unsigned long lpar_rc;
1179
Ben Hutchings288379f2009-01-19 16:43:59 -08001180 if (napi_schedule_prep(&adapter->napi)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001181 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1182 VIO_IRQ_DISABLE);
Santiago Leon64859112010-09-03 18:29:41 +00001183 BUG_ON(lpar_rc != H_SUCCESS);
Ben Hutchings288379f2009-01-19 16:43:59 -08001184 __napi_schedule(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186 return IRQ_HANDLED;
1187}
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189static void ibmveth_set_multicast_list(struct net_device *netdev)
1190{
Wang Chen4cf16532008-11-12 23:38:14 -08001191 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 unsigned long lpar_rc;
1193
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001194 if ((netdev->flags & IFF_PROMISC) ||
1195 (netdev_mc_count(netdev) > adapter->mcastFilterSize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1197 IbmVethMcastEnableRecv |
1198 IbmVethMcastDisableFiltering,
1199 0);
Santiago Leonf148f612010-09-03 18:29:30 +00001200 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001201 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1202 "entering promisc mode\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001205 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* clear the filter table & disable filtering */
1207 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1208 IbmVethMcastEnableRecv |
1209 IbmVethMcastDisableFiltering |
1210 IbmVethMcastClearFilterTable,
1211 0);
Santiago Leonf148f612010-09-03 18:29:30 +00001212 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001213 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1214 "attempting to clear filter table\n",
1215 lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217 /* add the addresses to the filter table */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001218 netdev_for_each_mc_addr(ha, netdev) {
Santiago Leonf148f612010-09-03 18:29:30 +00001219 /* add the multicast address to the filter table */
Anton Blanchardd746ca92014-03-05 14:51:37 +11001220 u64 mcast_addr;
1221 mcast_addr = ibmveth_encode_mac_addr(ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1223 IbmVethMcastAddFilter,
1224 mcast_addr);
Santiago Leonf148f612010-09-03 18:29:30 +00001225 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001226 netdev_err(netdev, "h_multicast_ctrl rc=%ld "
1227 "when adding an entry to the filter "
1228 "table\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /* re-enable filtering */
1233 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1234 IbmVethMcastEnableFiltering,
1235 0);
Santiago Leonf148f612010-09-03 18:29:30 +00001236 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001237 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1238 "enabling filtering\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 }
1241}
1242
1243static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
1244{
Wang Chen4cf16532008-11-12 23:38:14 -08001245 struct ibmveth_adapter *adapter = netdev_priv(dev);
Robert Jennings1096d632008-07-24 04:34:52 +10001246 struct vio_dev *viodev = adapter->vdev;
Santiago Leon860f2422006-04-25 11:19:59 -05001247 int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
Robert Jennings0645bab2010-08-17 09:15:45 +00001248 int i, rc;
1249 int need_restart = 0;
Santiago Leonb6d35182005-10-26 10:47:01 -06001250
Santiago Leon517e80e2010-09-03 18:29:25 +00001251 if (new_mtu < IBMVETH_MIN_MTU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return -EINVAL;
Santiago Leonb6d35182005-10-26 10:47:01 -06001253
Santiago Leon517e80e2010-09-03 18:29:25 +00001254 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
David Gibson4fce1482015-04-23 14:43:05 +10001255 if (new_mtu_oh <= adapter->rx_buff_pool[i].buff_size)
Brian Kingce6eea52007-06-08 14:05:17 -05001256 break;
1257
Santiago Leon517e80e2010-09-03 18:29:25 +00001258 if (i == IBMVETH_NUM_BUFF_POOLS)
Brian Kingce6eea52007-06-08 14:05:17 -05001259 return -EINVAL;
1260
Santiago Leonea866e62008-07-24 04:34:23 +10001261 /* Deactivate all the buffer pools so that the next loop can activate
1262 only the buffer pools necessary to hold the new MTU */
Robert Jennings0645bab2010-08-17 09:15:45 +00001263 if (netif_running(adapter->netdev)) {
1264 need_restart = 1;
1265 adapter->pool_config = 1;
1266 ibmveth_close(adapter->netdev);
1267 adapter->pool_config = 0;
1268 }
Brian Kingce6eea52007-06-08 14:05:17 -05001269
Santiago Leonea866e62008-07-24 04:34:23 +10001270 /* Look for an active buffer pool that can hold the new MTU */
Santiago Leonf148f612010-09-03 18:29:30 +00001271 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Santiago Leonea866e62008-07-24 04:34:23 +10001272 adapter->rx_buff_pool[i].active = 1;
1273
David Gibson4fce1482015-04-23 14:43:05 +10001274 if (new_mtu_oh <= adapter->rx_buff_pool[i].buff_size) {
Robert Jennings1096d632008-07-24 04:34:52 +10001275 dev->mtu = new_mtu;
1276 vio_cmo_set_dev_desired(viodev,
1277 ibmveth_get_desired_dma
1278 (viodev));
Robert Jennings0645bab2010-08-17 09:15:45 +00001279 if (need_restart) {
1280 return ibmveth_open(adapter->netdev);
1281 }
Santiago Leon860f2422006-04-25 11:19:59 -05001282 return 0;
Santiago Leonb6d35182005-10-26 10:47:01 -06001283 }
Santiago Leonb6d35182005-10-26 10:47:01 -06001284 }
Robert Jennings0645bab2010-08-17 09:15:45 +00001285
1286 if (need_restart && (rc = ibmveth_open(adapter->netdev)))
1287 return rc;
1288
Santiago Leon860f2422006-04-25 11:19:59 -05001289 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Santiago Leon6b422372006-10-03 12:24:28 -05001292#ifdef CONFIG_NET_POLL_CONTROLLER
1293static void ibmveth_poll_controller(struct net_device *dev)
1294{
Wang Chen4cf16532008-11-12 23:38:14 -08001295 ibmveth_replenish_task(netdev_priv(dev));
Andrew Morton5f771132006-10-10 14:33:30 -07001296 ibmveth_interrupt(dev->irq, dev);
Santiago Leon6b422372006-10-03 12:24:28 -05001297}
1298#endif
1299
Robert Jennings1096d632008-07-24 04:34:52 +10001300/**
1301 * ibmveth_get_desired_dma - Calculate IO memory desired by the driver
1302 *
1303 * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
1304 *
1305 * Return value:
1306 * Number of bytes of IO data the driver will need to perform well.
1307 */
1308static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev)
1309{
1310 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
1311 struct ibmveth_adapter *adapter;
Alistair Poppled0847752013-12-09 18:17:03 +11001312 struct iommu_table *tbl;
Robert Jennings1096d632008-07-24 04:34:52 +10001313 unsigned long ret;
1314 int i;
1315 int rxqentries = 1;
1316
Alistair Poppled0847752013-12-09 18:17:03 +11001317 tbl = get_iommu_table_base(&vdev->dev);
1318
Robert Jennings1096d632008-07-24 04:34:52 +10001319 /* netdev inits at probe time along with the structures we need below*/
1320 if (netdev == NULL)
Alistair Poppled0847752013-12-09 18:17:03 +11001321 return IOMMU_PAGE_ALIGN(IBMVETH_IO_ENTITLEMENT_DEFAULT, tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001322
1323 adapter = netdev_priv(netdev);
1324
1325 ret = IBMVETH_BUFF_LIST_SIZE + IBMVETH_FILT_LIST_SIZE;
Alistair Poppled0847752013-12-09 18:17:03 +11001326 ret += IOMMU_PAGE_ALIGN(netdev->mtu, tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001327
Santiago Leon517e80e2010-09-03 18:29:25 +00001328 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Robert Jennings1096d632008-07-24 04:34:52 +10001329 /* add the size of the active receive buffers */
1330 if (adapter->rx_buff_pool[i].active)
1331 ret +=
1332 adapter->rx_buff_pool[i].size *
1333 IOMMU_PAGE_ALIGN(adapter->rx_buff_pool[i].
Alistair Poppled0847752013-12-09 18:17:03 +11001334 buff_size, tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001335 rxqentries += adapter->rx_buff_pool[i].size;
1336 }
1337 /* add the size of the receive queue entries */
Alistair Poppled0847752013-12-09 18:17:03 +11001338 ret += IOMMU_PAGE_ALIGN(
1339 rxqentries * sizeof(struct ibmveth_rx_q_entry), tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001340
1341 return ret;
1342}
1343
Thomas Falconc77c7612015-03-02 11:56:12 -06001344static int ibmveth_set_mac_addr(struct net_device *dev, void *p)
1345{
1346 struct ibmveth_adapter *adapter = netdev_priv(dev);
1347 struct sockaddr *addr = p;
1348 u64 mac_address;
1349 int rc;
1350
1351 if (!is_valid_ether_addr(addr->sa_data))
1352 return -EADDRNOTAVAIL;
1353
1354 mac_address = ibmveth_encode_mac_addr(addr->sa_data);
1355 rc = h_change_logical_lan_mac(adapter->vdev->unit_address, mac_address);
1356 if (rc) {
1357 netdev_err(adapter->netdev, "h_change_logical_lan_mac failed with rc=%d\n", rc);
1358 return rc;
1359 }
1360
1361 ether_addr_copy(dev->dev_addr, addr->sa_data);
1362
1363 return 0;
1364}
1365
Alexander Beregalove186d172009-04-15 12:52:39 +00001366static const struct net_device_ops ibmveth_netdev_ops = {
1367 .ndo_open = ibmveth_open,
1368 .ndo_stop = ibmveth_close,
1369 .ndo_start_xmit = ibmveth_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001370 .ndo_set_rx_mode = ibmveth_set_multicast_list,
Alexander Beregalove186d172009-04-15 12:52:39 +00001371 .ndo_do_ioctl = ibmveth_ioctl,
1372 .ndo_change_mtu = ibmveth_change_mtu,
Michał Mirosławb9367bf2011-04-19 02:14:25 +00001373 .ndo_fix_features = ibmveth_fix_features,
1374 .ndo_set_features = ibmveth_set_features,
Alexander Beregalove186d172009-04-15 12:52:39 +00001375 .ndo_validate_addr = eth_validate_addr,
Thomas Falconc77c7612015-03-02 11:56:12 -06001376 .ndo_set_mac_address = ibmveth_set_mac_addr,
Alexander Beregalove186d172009-04-15 12:52:39 +00001377#ifdef CONFIG_NET_POLL_CONTROLLER
1378 .ndo_poll_controller = ibmveth_poll_controller,
1379#endif
1380};
1381
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001382static int ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Benjamin Herrenschmidt13f85202013-05-03 17:19:01 +00001384 int rc, i, mac_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 struct net_device *netdev;
Mariusz Kozlowski9dc83af2007-08-06 23:44:03 +02001386 struct ibmveth_adapter *adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 unsigned char *mac_addr_p;
1388 unsigned int *mcastFilterSize_p;
1389
Santiago Leonc43ced12010-09-03 18:29:14 +00001390 dev_dbg(&dev->dev, "entering ibmveth_probe for UA 0x%x\n",
1391 dev->unit_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Santiago Leonf148f612010-09-03 18:29:30 +00001393 mac_addr_p = (unsigned char *)vio_get_attribute(dev, VETH_MAC_ADDR,
Benjamin Herrenschmidt13f85202013-05-03 17:19:01 +00001394 &mac_len);
Santiago Leonf148f612010-09-03 18:29:30 +00001395 if (!mac_addr_p) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001396 dev_err(&dev->dev, "Can't find VETH_MAC_ADDR attribute\n");
Santiago Leonbe35ae92010-09-03 18:29:36 +00001397 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
Benjamin Herrenschmidt13f85202013-05-03 17:19:01 +00001399 /* Workaround for old/broken pHyp */
1400 if (mac_len == 8)
1401 mac_addr_p += 2;
1402 else if (mac_len != 6) {
1403 dev_err(&dev->dev, "VETH_MAC_ADDR attribute wrong len %d\n",
1404 mac_len);
1405 return -EINVAL;
1406 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001407
Santiago Leonf148f612010-09-03 18:29:30 +00001408 mcastFilterSize_p = (unsigned int *)vio_get_attribute(dev,
Michael Ellerman493a6842007-04-17 13:12:55 +10001409 VETH_MCAST_FILTER_SIZE, NULL);
Santiago Leonf148f612010-09-03 18:29:30 +00001410 if (!mcastFilterSize_p) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001411 dev_err(&dev->dev, "Can't find VETH_MCAST_FILTER_SIZE "
1412 "attribute\n");
Santiago Leonbe35ae92010-09-03 18:29:36 +00001413 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
1417
Santiago Leonf148f612010-09-03 18:29:30 +00001418 if (!netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return -ENOMEM;
1420
Wang Chen4cf16532008-11-12 23:38:14 -08001421 adapter = netdev_priv(netdev);
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07001422 dev_set_drvdata(&dev->dev, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 adapter->vdev = dev;
1425 adapter->netdev = netdev;
Santiago Leonf148f612010-09-03 18:29:30 +00001426 adapter->mcastFilterSize = *mcastFilterSize_p;
Santiago Leon860f2422006-04-25 11:19:59 -05001427 adapter->pool_config = 0;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001428
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001429 netif_napi_add(netdev, &adapter->napi, ibmveth_poll, 16);
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 netdev->irq = dev->irq;
Alexander Beregalove186d172009-04-15 12:52:39 +00001432 netdev->netdev_ops = &ibmveth_netdev_ops;
1433 netdev->ethtool_ops = &netdev_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 SET_NETDEV_DEV(netdev, &dev->dev);
Michał Mirosławb9367bf2011-04-19 02:14:25 +00001435 netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
1436 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1437 netdev->features |= netdev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Thomas Falcon8641dd82015-04-29 16:25:45 -05001439 /* TSO is disabled by default */
1440 netdev->hw_features |= NETIF_F_TSO;
1441
Anton Blanchardd746ca92014-03-05 14:51:37 +11001442 memcpy(netdev->dev_addr, mac_addr_p, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Thomas Falconcd7c7ec2015-04-29 16:25:44 -05001444 if (firmware_has_feature(FW_FEATURE_CMO))
1445 memcpy(pool_count, pool_count_cmo, sizeof(pool_count));
1446
Santiago Leonf148f612010-09-03 18:29:30 +00001447 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Santiago Leon860f2422006-04-25 11:19:59 -05001448 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
Greg Kroah-Hartman8dde2a92007-12-17 15:54:39 -04001449 int error;
1450
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001451 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1452 pool_count[i], pool_size[i],
Santiago Leon860f2422006-04-25 11:19:59 -05001453 pool_active[i]);
Greg Kroah-Hartman8dde2a92007-12-17 15:54:39 -04001454 error = kobject_init_and_add(kobj, &ktype_veth_pool,
1455 &dev->dev.kobj, "pool%d", i);
1456 if (!error)
1457 kobject_uevent(kobj, KOBJ_ADD);
Santiago Leon860f2422006-04-25 11:19:59 -05001458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Santiago Leonc43ced12010-09-03 18:29:14 +00001460 netdev_dbg(netdev, "adapter @ 0x%p\n", adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 adapter->buffer_list_dma = DMA_ERROR_CODE;
1463 adapter->filter_list_dma = DMA_ERROR_CODE;
1464 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1465
Santiago Leonc43ced12010-09-03 18:29:14 +00001466 netdev_dbg(netdev, "registering netdev...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Michał Mirosławb801a4e2011-04-28 11:59:15 +10001468 ibmveth_set_features(netdev, netdev->features);
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 rc = register_netdev(netdev);
1471
Santiago Leonf148f612010-09-03 18:29:30 +00001472 if (rc) {
Santiago Leonc43ced12010-09-03 18:29:14 +00001473 netdev_dbg(netdev, "failed to register netdev rc=%d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 free_netdev(netdev);
1475 return rc;
1476 }
1477
Santiago Leonc43ced12010-09-03 18:29:14 +00001478 netdev_dbg(netdev, "registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return 0;
1481}
1482
Bill Pembertone11787a2012-12-03 09:23:12 -05001483static int ibmveth_remove(struct vio_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07001485 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Wang Chen4cf16532008-11-12 23:38:14 -08001486 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001487 int i;
1488
Santiago Leonf148f612010-09-03 18:29:30 +00001489 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001490 kobject_put(&adapter->rx_buff_pool[i].kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 unregister_netdev(netdev);
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 free_netdev(netdev);
Robert Jennings1096d632008-07-24 04:34:52 +10001495 dev_set_drvdata(&dev->dev, NULL);
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return 0;
1498}
1499
Santiago Leon860f2422006-04-25 11:19:59 -05001500static struct attribute veth_active_attr;
1501static struct attribute veth_num_attr;
1502static struct attribute veth_size_attr;
1503
Santiago Leonf148f612010-09-03 18:29:30 +00001504static ssize_t veth_pool_show(struct kobject *kobj,
1505 struct attribute *attr, char *buf)
Santiago Leon860f2422006-04-25 11:19:59 -05001506{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001507 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001508 struct ibmveth_buff_pool,
1509 kobj);
1510
1511 if (attr == &veth_active_attr)
1512 return sprintf(buf, "%d\n", pool->active);
1513 else if (attr == &veth_num_attr)
1514 return sprintf(buf, "%d\n", pool->size);
1515 else if (attr == &veth_size_attr)
1516 return sprintf(buf, "%d\n", pool->buff_size);
1517 return 0;
1518}
1519
Santiago Leonf148f612010-09-03 18:29:30 +00001520static ssize_t veth_pool_store(struct kobject *kobj, struct attribute *attr,
1521 const char *buf, size_t count)
Santiago Leon860f2422006-04-25 11:19:59 -05001522{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001523 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001524 struct ibmveth_buff_pool,
1525 kobj);
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07001526 struct net_device *netdev = dev_get_drvdata(
1527 container_of(kobj->parent, struct device, kobj));
Wang Chen4cf16532008-11-12 23:38:14 -08001528 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001529 long value = simple_strtol(buf, NULL, 10);
1530 long rc;
1531
1532 if (attr == &veth_active_attr) {
1533 if (value && !pool->active) {
Brian King4aa9c932007-06-08 14:05:16 -05001534 if (netif_running(netdev)) {
Santiago Leonf148f612010-09-03 18:29:30 +00001535 if (ibmveth_alloc_buffer_pool(pool)) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001536 netdev_err(netdev,
1537 "unable to alloc pool\n");
Brian King4aa9c932007-06-08 14:05:16 -05001538 return -ENOMEM;
1539 }
1540 pool->active = 1;
1541 adapter->pool_config = 1;
1542 ibmveth_close(netdev);
1543 adapter->pool_config = 0;
1544 if ((rc = ibmveth_open(netdev)))
1545 return rc;
Santiago Leonf148f612010-09-03 18:29:30 +00001546 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001547 pool->active = 1;
Santiago Leonf148f612010-09-03 18:29:30 +00001548 }
Santiago Leon860f2422006-04-25 11:19:59 -05001549 } else if (!value && pool->active) {
1550 int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1551 int i;
1552 /* Make sure there is a buffer pool with buffers that
1553 can hold a packet of the size of the MTU */
Santiago Leon517e80e2010-09-03 18:29:25 +00001554 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Santiago Leon860f2422006-04-25 11:19:59 -05001555 if (pool == &adapter->rx_buff_pool[i])
1556 continue;
1557 if (!adapter->rx_buff_pool[i].active)
1558 continue;
Brian King76b9cfc2007-08-03 13:55:19 +10001559 if (mtu <= adapter->rx_buff_pool[i].buff_size)
1560 break;
Santiago Leon860f2422006-04-25 11:19:59 -05001561 }
Brian King76b9cfc2007-08-03 13:55:19 +10001562
Santiago Leon517e80e2010-09-03 18:29:25 +00001563 if (i == IBMVETH_NUM_BUFF_POOLS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001564 netdev_err(netdev, "no active pool >= MTU\n");
Santiago Leon860f2422006-04-25 11:19:59 -05001565 return -EPERM;
1566 }
Brian King76b9cfc2007-08-03 13:55:19 +10001567
Brian King76b9cfc2007-08-03 13:55:19 +10001568 if (netif_running(netdev)) {
1569 adapter->pool_config = 1;
1570 ibmveth_close(netdev);
Santiago Leonea866e62008-07-24 04:34:23 +10001571 pool->active = 0;
Brian King76b9cfc2007-08-03 13:55:19 +10001572 adapter->pool_config = 0;
1573 if ((rc = ibmveth_open(netdev)))
1574 return rc;
1575 }
Santiago Leonea866e62008-07-24 04:34:23 +10001576 pool->active = 0;
Santiago Leon860f2422006-04-25 11:19:59 -05001577 }
1578 } else if (attr == &veth_num_attr) {
Santiago Leonf148f612010-09-03 18:29:30 +00001579 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT) {
Santiago Leon860f2422006-04-25 11:19:59 -05001580 return -EINVAL;
Santiago Leonf148f612010-09-03 18:29:30 +00001581 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001582 if (netif_running(netdev)) {
1583 adapter->pool_config = 1;
1584 ibmveth_close(netdev);
1585 adapter->pool_config = 0;
1586 pool->size = value;
1587 if ((rc = ibmveth_open(netdev)))
1588 return rc;
Santiago Leonf148f612010-09-03 18:29:30 +00001589 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001590 pool->size = value;
Santiago Leonf148f612010-09-03 18:29:30 +00001591 }
Santiago Leon860f2422006-04-25 11:19:59 -05001592 }
1593 } else if (attr == &veth_size_attr) {
Santiago Leonf148f612010-09-03 18:29:30 +00001594 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE) {
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->buff_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->buff_size = value;
Santiago Leonf148f612010-09-03 18:29:30 +00001606 }
Santiago Leon860f2422006-04-25 11:19:59 -05001607 }
1608 }
1609
1610 /* kick the interrupt handler to allocate/deallocate pools */
David Howells7d12e782006-10-05 14:55:46 +01001611 ibmveth_interrupt(netdev->irq, netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001612 return count;
1613}
1614
1615
Santiago Leonf148f612010-09-03 18:29:30 +00001616#define ATTR(_name, _mode) \
1617 struct attribute veth_##_name##_attr = { \
1618 .name = __stringify(_name), .mode = _mode, \
1619 };
Santiago Leon860f2422006-04-25 11:19:59 -05001620
1621static ATTR(active, 0644);
1622static ATTR(num, 0644);
1623static ATTR(size, 0644);
1624
Santiago Leonf148f612010-09-03 18:29:30 +00001625static struct attribute *veth_pool_attrs[] = {
Santiago Leon860f2422006-04-25 11:19:59 -05001626 &veth_active_attr,
1627 &veth_num_attr,
1628 &veth_size_attr,
1629 NULL,
1630};
1631
Emese Revfy52cf25d2010-01-19 02:58:23 +01001632static const struct sysfs_ops veth_pool_ops = {
Santiago Leon860f2422006-04-25 11:19:59 -05001633 .show = veth_pool_show,
1634 .store = veth_pool_store,
1635};
1636
1637static struct kobj_type ktype_veth_pool = {
1638 .release = NULL,
1639 .sysfs_ops = &veth_pool_ops,
1640 .default_attrs = veth_pool_attrs,
1641};
1642
Brian Kinge7a3af52010-05-07 08:56:08 +00001643static int ibmveth_resume(struct device *dev)
1644{
1645 struct net_device *netdev = dev_get_drvdata(dev);
1646 ibmveth_interrupt(netdev->irq, netdev);
1647 return 0;
1648}
Santiago Leon860f2422006-04-25 11:19:59 -05001649
Bill Pembertone11787a2012-12-03 09:23:12 -05001650static struct vio_device_id ibmveth_device_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 { "network", "IBM,l-lan"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +10001652 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1655
Brian Kinge7a3af52010-05-07 08:56:08 +00001656static struct dev_pm_ops ibmveth_pm_ops = {
1657 .resume = ibmveth_resume
1658};
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660static struct vio_driver ibmveth_driver = {
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001661 .id_table = ibmveth_device_table,
1662 .probe = ibmveth_probe,
1663 .remove = ibmveth_remove,
Robert Jennings1096d632008-07-24 04:34:52 +10001664 .get_desired_dma = ibmveth_get_desired_dma,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +00001665 .name = ibmveth_driver_name,
1666 .pm = &ibmveth_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667};
1668
1669static int __init ibmveth_module_init(void)
1670{
Santiago Leon21c2dec2010-09-03 18:29:19 +00001671 printk(KERN_DEBUG "%s: %s %s\n", ibmveth_driver_name,
1672 ibmveth_driver_string, ibmveth_driver_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return vio_register_driver(&ibmveth_driver);
1675}
1676
1677static void __exit ibmveth_module_exit(void)
1678{
1679 vio_unregister_driver(&ibmveth_driver);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001680}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682module_init(ibmveth_module_init);
1683module_exit(ibmveth_module_exit);