blob: cd7675ac5bf9ed8b8658996d2d27190c6b20245f [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";
61#define ibmveth_driver_version "1.04"
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) },
Brian Kingddbb4de2007-08-17 09:16:43 -0500103};
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/* simple methods of getting data from the current rxq entry */
Brian King79ef4a42007-08-17 09:16:56 -0500106static inline u32 ibmveth_rxq_flags(struct ibmveth_adapter *adapter)
107{
Anton Blanchard0b536be2013-09-03 09:55:32 +1000108 return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].flags_off);
Brian King79ef4a42007-08-17 09:16:56 -0500109}
110
111static inline int ibmveth_rxq_toggle(struct ibmveth_adapter *adapter)
112{
Santiago Leonf148f612010-09-03 18:29:30 +0000113 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_TOGGLE) >>
114 IBMVETH_RXQ_TOGGLE_SHIFT;
Brian King79ef4a42007-08-17 09:16:56 -0500115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
118{
Santiago Leonf148f612010-09-03 18:29:30 +0000119 return ibmveth_rxq_toggle(adapter) == adapter->rx_queue.toggle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
123{
Santiago Leonf148f612010-09-03 18:29:30 +0000124 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
128{
Santiago Leonf148f612010-09-03 18:29:30 +0000129 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_OFF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
133{
Anton Blanchard0b536be2013-09-03 09:55:32 +1000134 return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Brian Kingf4ff2872007-09-15 13:36:07 -0700137static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
138{
Santiago Leonf148f612010-09-03 18:29:30 +0000139 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_CSUM_GOOD;
Brian Kingf4ff2872007-09-15 13:36:07 -0700140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/* setup the initial settings for a buffer pool */
Santiago Leonf148f612010-09-03 18:29:30 +0000143static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool,
144 u32 pool_index, u32 pool_size,
145 u32 buff_size, u32 pool_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 pool->size = pool_size;
148 pool->index = pool_index;
149 pool->buff_size = buff_size;
Santiago Leonc033a6d2010-09-03 18:28:09 +0000150 pool->threshold = pool_size * 7 / 8;
Santiago Leon860f2422006-04-25 11:19:59 -0500151 pool->active = pool_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
154/* allocate and setup an buffer pool - called during open */
155static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
156{
157 int i;
158
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400159 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Santiago Leonf148f612010-09-03 18:29:30 +0000161 if (!pool->free_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400164 pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
Santiago Leonf148f612010-09-03 18:29:30 +0000165 if (!pool->dma_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 kfree(pool->free_map);
167 pool->free_map = NULL;
168 return -1;
169 }
170
Julia Lawalla05abcb2010-05-13 10:06:01 +0000171 pool->skbuff = kcalloc(pool->size, sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Santiago Leonf148f612010-09-03 18:29:30 +0000173 if (!pool->skbuff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 kfree(pool->dma_addr);
175 pool->dma_addr = NULL;
176
177 kfree(pool->free_map);
178 pool->free_map = NULL;
179 return -1;
180 }
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
183
Santiago Leonf148f612010-09-03 18:29:30 +0000184 for (i = 0; i < pool->size; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 pool->free_map[i] = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 atomic_set(&pool->available, 0);
188 pool->producer_index = 0;
189 pool->consumer_index = 0;
190
191 return 0;
192}
193
Santiago Leon0c26b672010-09-03 18:28:41 +0000194static inline void ibmveth_flush_buffer(void *addr, unsigned long length)
195{
196 unsigned long offset;
197
198 for (offset = 0; offset < length; offset += SMP_CACHE_BYTES)
199 asm("dcbfl %0,%1" :: "b" (addr), "r" (offset));
200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/* replenish the buffers for a pool. note that we don't need to
203 * skb_reserve these since they are used for incoming...
204 */
Santiago Leonf148f612010-09-03 18:29:30 +0000205static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter,
206 struct ibmveth_buff_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 u32 i;
209 u32 count = pool->size - atomic_read(&pool->available);
210 u32 buffers_added = 0;
Robert Jennings1096d632008-07-24 04:34:52 +1000211 struct sk_buff *skb;
212 unsigned int free_index, index;
213 u64 correlator;
214 unsigned long lpar_rc;
215 dma_addr_t dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 mb();
218
Santiago Leonf148f612010-09-03 18:29:30 +0000219 for (i = 0; i < count; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 union ibmveth_buf_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Santiago Leon003212c2010-09-03 18:29:03 +0000222 skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Santiago Leonf148f612010-09-03 18:29:30 +0000224 if (!skb) {
Santiago Leonc43ced12010-09-03 18:29:14 +0000225 netdev_dbg(adapter->netdev,
226 "replenish: unable to allocate skb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 adapter->replenish_no_mem++;
228 break;
229 }
230
David Gibson047a66d2006-10-21 10:24:13 -0700231 free_index = pool->consumer_index;
Santiago Leona613f582010-09-03 18:28:04 +0000232 pool->consumer_index++;
233 if (pool->consumer_index >= pool->size)
234 pool->consumer_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 index = pool->free_map[free_index];
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400236
Santiago Leon64859112010-09-03 18:29:41 +0000237 BUG_ON(index == IBM_VETH_INVALID_MAP);
238 BUG_ON(pool->skbuff[index] != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
241 pool->buff_size, DMA_FROM_DEVICE);
242
Stephen Rothwellc713e7c2008-07-28 02:14:24 +1000243 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
Robert Jennings1096d632008-07-24 04:34:52 +1000244 goto failure;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
247 pool->dma_addr[index] = dma_addr;
248 pool->skbuff[index] = skb;
249
250 correlator = ((u64)pool->index << 32) | index;
Santiago Leonf148f612010-09-03 18:29:30 +0000251 *(u64 *)skb->data = correlator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Brian King79ef4a42007-08-17 09:16:56 -0500253 desc.fields.flags_len = IBMVETH_BUF_VALID | pool->buff_size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400254 desc.fields.address = dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Santiago Leon0c26b672010-09-03 18:28:41 +0000256 if (rx_flush) {
257 unsigned int len = min(pool->buff_size,
258 adapter->netdev->mtu +
259 IBMVETH_BUFF_OH);
260 ibmveth_flush_buffer(skb->data, len);
261 }
Santiago Leonf148f612010-09-03 18:29:30 +0000262 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address,
263 desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400264
Santiago Leonf148f612010-09-03 18:29:30 +0000265 if (lpar_rc != H_SUCCESS) {
Robert Jennings1096d632008-07-24 04:34:52 +1000266 goto failure;
Santiago Leonf148f612010-09-03 18:29:30 +0000267 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 buffers_added++;
269 adapter->replenish_add_buff_success++;
270 }
271 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 mb();
274 atomic_add(buffers_added, &(pool->available));
Robert Jennings1096d632008-07-24 04:34:52 +1000275 return;
276
277failure:
278 pool->free_map[free_index] = index;
279 pool->skbuff[index] = NULL;
280 if (pool->consumer_index == 0)
281 pool->consumer_index = pool->size - 1;
282 else
283 pool->consumer_index--;
Stephen Rothwellc713e7c2008-07-28 02:14:24 +1000284 if (!dma_mapping_error(&adapter->vdev->dev, dma_addr))
Robert Jennings1096d632008-07-24 04:34:52 +1000285 dma_unmap_single(&adapter->vdev->dev,
286 pool->dma_addr[index], pool->buff_size,
287 DMA_FROM_DEVICE);
288 dev_kfree_skb_any(skb);
289 adapter->replenish_add_buff_failure++;
290
291 mb();
292 atomic_add(buffers_added, &(pool->available));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Anton Blanchardcbd52282014-08-22 11:36:52 +1000295/*
296 * The final 8 bytes of the buffer list is a counter of frames dropped
297 * because there was not a buffer in the buffer list capable of holding
298 * the frame.
299 */
300static void ibmveth_update_rx_no_buffer(struct ibmveth_adapter *adapter)
301{
302 __be64 *p = adapter->buffer_list_addr + 4096 - 8;
303
304 adapter->rx_no_buffer = be64_to_cpup(p);
305}
306
Santiago Leone2adbcb2005-10-26 10:47:08 -0600307/* replenish routine */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400308static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Santiago Leonb6d35182005-10-26 10:47:01 -0600310 int i;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 adapter->replenish_task_cycles++;
313
Santiago Leon517e80e2010-09-03 18:29:25 +0000314 for (i = (IBMVETH_NUM_BUFF_POOLS - 1); i >= 0; i--) {
Santiago Leonc033a6d2010-09-03 18:28:09 +0000315 struct ibmveth_buff_pool *pool = &adapter->rx_buff_pool[i];
316
317 if (pool->active &&
318 (atomic_read(&pool->available) < pool->threshold))
319 ibmveth_replenish_buffer_pool(adapter, pool);
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Anton Blanchardcbd52282014-08-22 11:36:52 +1000322 ibmveth_update_rx_no_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325/* empty and free ana buffer pool - also used to do cleanup in error paths */
Santiago Leonf148f612010-09-03 18:29:30 +0000326static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter,
327 struct ibmveth_buff_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 int i;
330
Jesper Juhlb4558ea2005-10-28 16:53:13 -0400331 kfree(pool->free_map);
332 pool->free_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Santiago Leonf148f612010-09-03 18:29:30 +0000334 if (pool->skbuff && pool->dma_addr) {
335 for (i = 0; i < pool->size; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 struct sk_buff *skb = pool->skbuff[i];
Santiago Leonf148f612010-09-03 18:29:30 +0000337 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 dma_unmap_single(&adapter->vdev->dev,
339 pool->dma_addr[i],
340 pool->buff_size,
341 DMA_FROM_DEVICE);
342 dev_kfree_skb_any(skb);
343 pool->skbuff[i] = NULL;
344 }
345 }
346 }
347
Santiago Leonf148f612010-09-03 18:29:30 +0000348 if (pool->dma_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 kfree(pool->dma_addr);
350 pool->dma_addr = NULL;
351 }
352
Santiago Leonf148f612010-09-03 18:29:30 +0000353 if (pool->skbuff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 kfree(pool->skbuff);
355 pool->skbuff = NULL;
356 }
357}
358
359/* remove a buffer from a pool */
Santiago Leonf148f612010-09-03 18:29:30 +0000360static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter,
361 u64 correlator)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 unsigned int pool = correlator >> 32;
364 unsigned int index = correlator & 0xffffffffUL;
365 unsigned int free_index;
366 struct sk_buff *skb;
367
Santiago Leon64859112010-09-03 18:29:41 +0000368 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
369 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 skb = adapter->rx_buff_pool[pool].skbuff[index];
372
Santiago Leon64859112010-09-03 18:29:41 +0000373 BUG_ON(skb == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
376
377 dma_unmap_single(&adapter->vdev->dev,
378 adapter->rx_buff_pool[pool].dma_addr[index],
379 adapter->rx_buff_pool[pool].buff_size,
380 DMA_FROM_DEVICE);
381
David Gibson047a66d2006-10-21 10:24:13 -0700382 free_index = adapter->rx_buff_pool[pool].producer_index;
Santiago Leona613f582010-09-03 18:28:04 +0000383 adapter->rx_buff_pool[pool].producer_index++;
384 if (adapter->rx_buff_pool[pool].producer_index >=
385 adapter->rx_buff_pool[pool].size)
386 adapter->rx_buff_pool[pool].producer_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 adapter->rx_buff_pool[pool].free_map[free_index] = index;
388
389 mb();
390
391 atomic_dec(&(adapter->rx_buff_pool[pool].available));
392}
393
394/* get the current buffer on the rx queue */
395static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
396{
397 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
398 unsigned int pool = correlator >> 32;
399 unsigned int index = correlator & 0xffffffffUL;
400
Santiago Leon64859112010-09-03 18:29:41 +0000401 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
402 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 return adapter->rx_buff_pool[pool].skbuff[index];
405}
406
407/* recycle the current buffer on the rx queue */
David S. Miller8decf862011-09-22 03:23:13 -0400408static int ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 u32 q_index = adapter->rx_queue.index;
411 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
412 unsigned int pool = correlator >> 32;
413 unsigned int index = correlator & 0xffffffffUL;
414 union ibmveth_buf_desc desc;
415 unsigned long lpar_rc;
David S. Miller8decf862011-09-22 03:23:13 -0400416 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Santiago Leon64859112010-09-03 18:29:41 +0000418 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
419 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Santiago Leonf148f612010-09-03 18:29:30 +0000421 if (!adapter->rx_buff_pool[pool].active) {
Santiago Leonb6d35182005-10-26 10:47:01 -0600422 ibmveth_rxq_harvest_buffer(adapter);
423 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
David S. Miller8decf862011-09-22 03:23:13 -0400424 goto out;
Santiago Leonb6d35182005-10-26 10:47:01 -0600425 }
426
Brian King79ef4a42007-08-17 09:16:56 -0500427 desc.fields.flags_len = IBMVETH_BUF_VALID |
428 adapter->rx_buff_pool[pool].buff_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
430
431 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400432
Santiago Leonf148f612010-09-03 18:29:30 +0000433 if (lpar_rc != H_SUCCESS) {
Santiago Leonc43ced12010-09-03 18:29:14 +0000434 netdev_dbg(adapter->netdev, "h_add_logical_lan_buffer failed "
435 "during recycle rc=%ld", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
David S. Miller8decf862011-09-22 03:23:13 -0400437 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
Santiago Leonf148f612010-09-03 18:29:30 +0000440 if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 adapter->rx_queue.index = 0;
442 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
443 }
David S. Miller8decf862011-09-22 03:23:13 -0400444
445out:
446 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Michael Ellerman493a6842007-04-17 13:12:55 +1000449static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
452
Santiago Leonf148f612010-09-03 18:29:30 +0000453 if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 adapter->rx_queue.index = 0;
455 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
456 }
457}
458
459static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
460{
Santiago Leonb6d35182005-10-26 10:47:01 -0600461 int i;
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700462 struct device *dev = &adapter->vdev->dev;
Santiago Leonb6d35182005-10-26 10:47:01 -0600463
Santiago Leonf148f612010-09-03 18:29:30 +0000464 if (adapter->buffer_list_addr != NULL) {
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700465 if (!dma_mapping_error(dev, adapter->buffer_list_dma)) {
466 dma_unmap_single(dev, adapter->buffer_list_dma, 4096,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 DMA_BIDIRECTIONAL);
468 adapter->buffer_list_dma = DMA_ERROR_CODE;
469 }
470 free_page((unsigned long)adapter->buffer_list_addr);
471 adapter->buffer_list_addr = NULL;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Santiago Leonf148f612010-09-03 18:29:30 +0000474 if (adapter->filter_list_addr != NULL) {
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700475 if (!dma_mapping_error(dev, adapter->filter_list_dma)) {
476 dma_unmap_single(dev, adapter->filter_list_dma, 4096,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 DMA_BIDIRECTIONAL);
478 adapter->filter_list_dma = DMA_ERROR_CODE;
479 }
480 free_page((unsigned long)adapter->filter_list_addr);
481 adapter->filter_list_addr = NULL;
482 }
483
Santiago Leonf148f612010-09-03 18:29:30 +0000484 if (adapter->rx_queue.queue_addr != NULL) {
Santiago Leond90c92f2012-09-04 14:41:37 +0000485 dma_free_coherent(dev, adapter->rx_queue.queue_len,
486 adapter->rx_queue.queue_addr,
487 adapter->rx_queue.queue_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 adapter->rx_queue.queue_addr = NULL;
489 }
490
Santiago Leonf148f612010-09-03 18:29:30 +0000491 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Santiago Leon860f2422006-04-25 11:19:59 -0500492 if (adapter->rx_buff_pool[i].active)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400493 ibmveth_free_buffer_pool(adapter,
Santiago Leon860f2422006-04-25 11:19:59 -0500494 &adapter->rx_buff_pool[i]);
Robert Jennings1096d632008-07-24 04:34:52 +1000495
496 if (adapter->bounce_buffer != NULL) {
Stephen Rothwellc713e7c2008-07-28 02:14:24 +1000497 if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
Robert Jennings1096d632008-07-24 04:34:52 +1000498 dma_unmap_single(&adapter->vdev->dev,
499 adapter->bounce_buffer_dma,
500 adapter->netdev->mtu + IBMVETH_BUFF_OH,
501 DMA_BIDIRECTIONAL);
502 adapter->bounce_buffer_dma = DMA_ERROR_CODE;
503 }
504 kfree(adapter->bounce_buffer);
505 adapter->bounce_buffer = NULL;
506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500509static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
510 union ibmveth_buf_desc rxq_desc, u64 mac_address)
511{
512 int rc, try_again = 1;
513
Santiago Leonf148f612010-09-03 18:29:30 +0000514 /*
515 * After a kexec the adapter will still be open, so our attempt to
516 * open it will fail. So if we get a failure we free the adapter and
517 * try again, but only once.
518 */
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500519retry:
520 rc = h_register_logical_lan(adapter->vdev->unit_address,
521 adapter->buffer_list_dma, rxq_desc.desc,
522 adapter->filter_list_dma, mac_address);
523
524 if (rc != H_SUCCESS && try_again) {
525 do {
526 rc = h_free_logical_lan(adapter->vdev->unit_address);
527 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
528
529 try_again = 0;
530 goto retry;
531 }
532
533 return rc;
534}
535
Anton Blanchardd746ca92014-03-05 14:51:37 +1100536static u64 ibmveth_encode_mac_addr(u8 *mac)
537{
538 int i;
539 u64 encoded = 0;
540
541 for (i = 0; i < ETH_ALEN; i++)
542 encoded = (encoded << 8) | mac[i];
543
544 return encoded;
545}
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547static int ibmveth_open(struct net_device *netdev)
548{
Wang Chen4cf16532008-11-12 23:38:14 -0800549 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Anton Blanchardd746ca92014-03-05 14:51:37 +1100550 u64 mac_address;
Santiago Leonb6d35182005-10-26 10:47:01 -0600551 int rxq_entries = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 unsigned long lpar_rc;
553 int rc;
554 union ibmveth_buf_desc rxq_desc;
Santiago Leonb6d35182005-10-26 10:47:01 -0600555 int i;
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700556 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Santiago Leonc43ced12010-09-03 18:29:14 +0000558 netdev_dbg(netdev, "open starting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700560 napi_enable(&adapter->napi);
561
Santiago Leon517e80e2010-09-03 18:29:25 +0000562 for(i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Santiago Leonb6d35182005-10-26 10:47:01 -0600563 rxq_entries += adapter->rx_buff_pool[i].size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
566 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400567
Santiago Leonf148f612010-09-03 18:29:30 +0000568 if (!adapter->buffer_list_addr || !adapter->filter_list_addr) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000569 netdev_err(netdev, "unable to allocate filter or buffer list "
570 "pages\n");
Denis Kirjanov88426f22010-10-20 04:21:13 +0000571 rc = -ENOMEM;
572 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
Santiago Leond90c92f2012-09-04 14:41:37 +0000575 dev = &adapter->vdev->dev;
576
Santiago Leonf148f612010-09-03 18:29:30 +0000577 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) *
578 rxq_entries;
Santiago Leond90c92f2012-09-04 14:41:37 +0000579 adapter->rx_queue.queue_addr =
Joe Perchesd0320f72013-03-14 13:07:21 +0000580 dma_alloc_coherent(dev, adapter->rx_queue.queue_len,
581 &adapter->rx_queue.queue_dma, GFP_KERNEL);
Santiago Leonf148f612010-09-03 18:29:30 +0000582 if (!adapter->rx_queue.queue_addr) {
Denis Kirjanov88426f22010-10-20 04:21:13 +0000583 rc = -ENOMEM;
584 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700587 adapter->buffer_list_dma = dma_map_single(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700589 adapter->filter_list_dma = dma_map_single(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700592 if ((dma_mapping_error(dev, adapter->buffer_list_dma)) ||
Santiago Leond90c92f2012-09-04 14:41:37 +0000593 (dma_mapping_error(dev, adapter->filter_list_dma))) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000594 netdev_err(netdev, "unable to map filter or buffer list "
595 "pages\n");
Denis Kirjanov88426f22010-10-20 04:21:13 +0000596 rc = -ENOMEM;
597 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
600 adapter->rx_queue.index = 0;
601 adapter->rx_queue.num_slots = rxq_entries;
602 adapter->rx_queue.toggle = 1;
603
Anton Blanchardd746ca92014-03-05 14:51:37 +1100604 mac_address = ibmveth_encode_mac_addr(netdev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Santiago Leonf148f612010-09-03 18:29:30 +0000606 rxq_desc.fields.flags_len = IBMVETH_BUF_VALID |
607 adapter->rx_queue.queue_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
609
Santiago Leonc43ced12010-09-03 18:29:14 +0000610 netdev_dbg(netdev, "buffer list @ 0x%p\n", adapter->buffer_list_addr);
611 netdev_dbg(netdev, "filter list @ 0x%p\n", adapter->filter_list_addr);
612 netdev_dbg(netdev, "receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Santiago Leon4347ef12006-10-03 12:24:34 -0500614 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
615
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500616 lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Santiago Leonf148f612010-09-03 18:29:30 +0000618 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000619 netdev_err(netdev, "h_register_logical_lan failed with %ld\n",
620 lpar_rc);
621 netdev_err(netdev, "buffer TCE:0x%llx filter TCE:0x%llx rxq "
622 "desc:0x%llx MAC:0x%llx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 adapter->buffer_list_dma,
624 adapter->filter_list_dma,
625 rxq_desc.desc,
626 mac_address);
Denis Kirjanov88426f22010-10-20 04:21:13 +0000627 rc = -ENONET;
628 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630
Santiago Leonf148f612010-09-03 18:29:30 +0000631 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
632 if (!adapter->rx_buff_pool[i].active)
Santiago Leon860f2422006-04-25 11:19:59 -0500633 continue;
634 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000635 netdev_err(netdev, "unable to alloc pool\n");
Santiago Leon860f2422006-04-25 11:19:59 -0500636 adapter->rx_buff_pool[i].active = 0;
Denis Kirjanov88426f22010-10-20 04:21:13 +0000637 rc = -ENOMEM;
638 goto err_out;
Santiago Leon860f2422006-04-25 11:19:59 -0500639 }
640 }
641
Santiago Leonc43ced12010-09-03 18:29:14 +0000642 netdev_dbg(netdev, "registering irq 0x%x\n", netdev->irq);
Santiago Leonf148f612010-09-03 18:29:30 +0000643 rc = request_irq(netdev->irq, ibmveth_interrupt, 0, netdev->name,
644 netdev);
645 if (rc != 0) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000646 netdev_err(netdev, "unable to request irq 0x%x, rc %d\n",
647 netdev->irq, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 do {
David S. Miller88c51002011-10-07 13:38:43 -0400649 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
650 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Denis Kirjanov88426f22010-10-20 04:21:13 +0000652 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654
Robert Jennings1096d632008-07-24 04:34:52 +1000655 adapter->bounce_buffer =
656 kmalloc(netdev->mtu + IBMVETH_BUFF_OH, GFP_KERNEL);
657 if (!adapter->bounce_buffer) {
Denis Kirjanov88426f22010-10-20 04:21:13 +0000658 rc = -ENOMEM;
Denis Kirjanove0e8ab52010-10-20 04:21:51 +0000659 goto err_out_free_irq;
Robert Jennings1096d632008-07-24 04:34:52 +1000660 }
661 adapter->bounce_buffer_dma =
662 dma_map_single(&adapter->vdev->dev, adapter->bounce_buffer,
663 netdev->mtu + IBMVETH_BUFF_OH, DMA_BIDIRECTIONAL);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700664 if (dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000665 netdev_err(netdev, "unable to map bounce buffer\n");
Denis Kirjanov88426f22010-10-20 04:21:13 +0000666 rc = -ENOMEM;
Denis Kirjanove0e8ab52010-10-20 04:21:51 +0000667 goto err_out_free_irq;
Robert Jennings1096d632008-07-24 04:34:52 +1000668 }
669
Santiago Leonc43ced12010-09-03 18:29:14 +0000670 netdev_dbg(netdev, "initial replenish cycle\n");
David Howells7d12e782006-10-05 14:55:46 +0100671 ibmveth_interrupt(netdev->irq, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Santiago Leone2adbcb2005-10-26 10:47:08 -0600673 netif_start_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Santiago Leonc43ced12010-09-03 18:29:14 +0000675 netdev_dbg(netdev, "open complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 return 0;
Denis Kirjanov88426f22010-10-20 04:21:13 +0000678
Denis Kirjanove0e8ab52010-10-20 04:21:51 +0000679err_out_free_irq:
680 free_irq(netdev->irq, netdev);
Denis Kirjanov88426f22010-10-20 04:21:13 +0000681err_out:
682 ibmveth_cleanup(adapter);
683 napi_disable(&adapter->napi);
684 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687static int ibmveth_close(struct net_device *netdev)
688{
Wang Chen4cf16532008-11-12 23:38:14 -0800689 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 long lpar_rc;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400691
Santiago Leonc43ced12010-09-03 18:29:14 +0000692 netdev_dbg(netdev, "close starting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700694 napi_disable(&adapter->napi);
695
Santiago Leon860f2422006-04-25 11:19:59 -0500696 if (!adapter->pool_config)
697 netif_stop_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Robert Jenningsee2e6112010-07-16 04:57:25 +0000699 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 do {
702 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200703 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Santiago Leonf148f612010-09-03 18:29:30 +0000705 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000706 netdev_err(netdev, "h_free_logical_lan failed with %lx, "
707 "continuing with close\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
Robert Jenningsee2e6112010-07-16 04:57:25 +0000710 free_irq(netdev->irq, netdev);
711
Anton Blanchardcbd52282014-08-22 11:36:52 +1000712 ibmveth_update_rx_no_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 ibmveth_cleanup(adapter);
715
Santiago Leonc43ced12010-09-03 18:29:14 +0000716 netdev_dbg(netdev, "close complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 return 0;
719}
720
Santiago Leonf148f612010-09-03 18:29:30 +0000721static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
722{
723 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
724 SUPPORTED_FIBRE);
725 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
726 ADVERTISED_FIBRE);
David Decotigny70739492011-04-27 18:32:40 +0000727 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 cmd->duplex = DUPLEX_FULL;
729 cmd->port = PORT_FIBRE;
730 cmd->phy_address = 0;
731 cmd->transceiver = XCVR_INTERNAL;
732 cmd->autoneg = AUTONEG_ENABLE;
733 cmd->maxtxpkt = 0;
734 cmd->maxrxpkt = 1;
735 return 0;
736}
737
Santiago Leonf148f612010-09-03 18:29:30 +0000738static void netdev_get_drvinfo(struct net_device *dev,
739 struct ethtool_drvinfo *info)
740{
Jiri Pirko7826d432013-01-06 00:44:26 +0000741 strlcpy(info->driver, ibmveth_driver_name, sizeof(info->driver));
742 strlcpy(info->version, ibmveth_driver_version, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000745static netdev_features_t ibmveth_fix_features(struct net_device *dev,
746 netdev_features_t features)
Brian King5fc7e012007-08-17 09:16:31 -0500747{
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000748 /*
749 * Since the ibmveth firmware interface does not have the
750 * concept of separate tx/rx checksum offload enable, if rx
751 * checksum is disabled we also have to disable tx checksum
752 * offload. Once we disable rx checksum offload, we are no
753 * longer allowed to send tx buffers that are not properly
754 * checksummed.
755 */
Brian King5fc7e012007-08-17 09:16:31 -0500756
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000757 if (!(features & NETIF_F_RXCSUM))
758 features &= ~NETIF_F_ALL_CSUM;
759
760 return features;
Brian King5fc7e012007-08-17 09:16:31 -0500761}
762
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000763static int ibmveth_set_csum_offload(struct net_device *dev, u32 data)
Brian King5fc7e012007-08-17 09:16:31 -0500764{
Wang Chen4cf16532008-11-12 23:38:14 -0800765 struct ibmveth_adapter *adapter = netdev_priv(dev);
Stephen Rothwellff5bfc32009-01-06 10:47:44 -0800766 unsigned long set_attr, clr_attr, ret_attr;
Santiago Leonab78df72010-09-03 18:28:52 +0000767 unsigned long set_attr6, clr_attr6;
David S. Miller8decf862011-09-22 03:23:13 -0400768 long ret, ret4, ret6;
Brian King5fc7e012007-08-17 09:16:31 -0500769 int rc1 = 0, rc2 = 0;
770 int restart = 0;
771
772 if (netif_running(dev)) {
773 restart = 1;
774 adapter->pool_config = 1;
775 ibmveth_close(dev);
776 adapter->pool_config = 0;
777 }
778
Brian King79ef4a42007-08-17 09:16:56 -0500779 set_attr = 0;
780 clr_attr = 0;
David S. Miller8decf862011-09-22 03:23:13 -0400781 set_attr6 = 0;
782 clr_attr6 = 0;
Brian King5fc7e012007-08-17 09:16:31 -0500783
Santiago Leonab78df72010-09-03 18:28:52 +0000784 if (data) {
Brian King79ef4a42007-08-17 09:16:56 -0500785 set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Santiago Leonab78df72010-09-03 18:28:52 +0000786 set_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
787 } else {
Brian King79ef4a42007-08-17 09:16:56 -0500788 clr_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Santiago Leonab78df72010-09-03 18:28:52 +0000789 clr_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
790 }
Brian King5fc7e012007-08-17 09:16:31 -0500791
Brian King79ef4a42007-08-17 09:16:56 -0500792 ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500793
Brian King79ef4a42007-08-17 09:16:56 -0500794 if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
795 !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
796 (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
David S. Miller8decf862011-09-22 03:23:13 -0400797 ret4 = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
Brian King79ef4a42007-08-17 09:16:56 -0500798 set_attr, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500799
David S. Miller8decf862011-09-22 03:23:13 -0400800 if (ret4 != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000801 netdev_err(dev, "unable to change IPv4 checksum "
802 "offload settings. %d rc=%ld\n",
David S. Miller8decf862011-09-22 03:23:13 -0400803 data, ret4);
Brian King5fc7e012007-08-17 09:16:31 -0500804
David S. Miller8decf862011-09-22 03:23:13 -0400805 h_illan_attributes(adapter->vdev->unit_address,
806 set_attr, clr_attr, &ret_attr);
807
808 if (data == 1)
809 dev->features &= ~NETIF_F_IP_CSUM;
810
Santiago Leonf148f612010-09-03 18:29:30 +0000811 } else {
Santiago Leonab78df72010-09-03 18:28:52 +0000812 adapter->fw_ipv4_csum_support = data;
Santiago Leonf148f612010-09-03 18:29:30 +0000813 }
Santiago Leonab78df72010-09-03 18:28:52 +0000814
815 ret6 = h_illan_attributes(adapter->vdev->unit_address,
816 clr_attr6, set_attr6, &ret_attr);
817
818 if (ret6 != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000819 netdev_err(dev, "unable to change IPv6 checksum "
820 "offload settings. %d rc=%ld\n",
David S. Miller8decf862011-09-22 03:23:13 -0400821 data, ret6);
Santiago Leonab78df72010-09-03 18:28:52 +0000822
David S. Miller8decf862011-09-22 03:23:13 -0400823 h_illan_attributes(adapter->vdev->unit_address,
824 set_attr6, clr_attr6, &ret_attr);
825
826 if (data == 1)
827 dev->features &= ~NETIF_F_IPV6_CSUM;
828
Santiago Leonab78df72010-09-03 18:28:52 +0000829 } else
830 adapter->fw_ipv6_csum_support = data;
831
David S. Miller8decf862011-09-22 03:23:13 -0400832 if (ret4 == H_SUCCESS || ret6 == H_SUCCESS)
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000833 adapter->rx_csum = data;
Santiago Leonab78df72010-09-03 18:28:52 +0000834 else
835 rc1 = -EIO;
Brian King5fc7e012007-08-17 09:16:31 -0500836 } else {
837 rc1 = -EIO;
Santiago Leon21c2dec2010-09-03 18:29:19 +0000838 netdev_err(dev, "unable to change checksum offload settings."
839 " %d rc=%ld ret_attr=%lx\n", data, ret,
840 ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500841 }
842
843 if (restart)
844 rc2 = ibmveth_open(dev);
845
846 return rc1 ? rc1 : rc2;
847}
848
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000849static int ibmveth_set_features(struct net_device *dev,
850 netdev_features_t features)
Brian King5fc7e012007-08-17 09:16:31 -0500851{
Wang Chen4cf16532008-11-12 23:38:14 -0800852 struct ibmveth_adapter *adapter = netdev_priv(dev);
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000853 int rx_csum = !!(features & NETIF_F_RXCSUM);
854 int rc;
Brian King5fc7e012007-08-17 09:16:31 -0500855
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000856 if (rx_csum == adapter->rx_csum)
Brian King5fc7e012007-08-17 09:16:31 -0500857 return 0;
858
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000859 rc = ibmveth_set_csum_offload(dev, rx_csum);
860 if (rc && !adapter->rx_csum)
861 dev->features = features & ~(NETIF_F_ALL_CSUM | NETIF_F_RXCSUM);
Brian King5fc7e012007-08-17 09:16:31 -0500862
863 return rc;
864}
865
Brian Kingddbb4de2007-08-17 09:16:43 -0500866static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
867{
868 int i;
869
870 if (stringset != ETH_SS_STATS)
871 return;
872
873 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
874 memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
875}
876
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700877static int ibmveth_get_sset_count(struct net_device *dev, int sset)
Brian Kingddbb4de2007-08-17 09:16:43 -0500878{
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700879 switch (sset) {
880 case ETH_SS_STATS:
881 return ARRAY_SIZE(ibmveth_stats);
882 default:
883 return -EOPNOTSUPP;
884 }
Brian Kingddbb4de2007-08-17 09:16:43 -0500885}
886
887static void ibmveth_get_ethtool_stats(struct net_device *dev,
888 struct ethtool_stats *stats, u64 *data)
889{
890 int i;
Wang Chen4cf16532008-11-12 23:38:14 -0800891 struct ibmveth_adapter *adapter = netdev_priv(dev);
Brian Kingddbb4de2007-08-17 09:16:43 -0500892
893 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
894 data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
895}
896
Jeff Garzik7282d492006-09-13 14:30:00 -0400897static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 .get_drvinfo = netdev_get_drvinfo,
899 .get_settings = netdev_get_settings,
Ben Hutchingsed4ba4b2010-12-09 12:10:25 +0000900 .get_link = ethtool_op_get_link,
Brian Kingddbb4de2007-08-17 09:16:43 -0500901 .get_strings = ibmveth_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700902 .get_sset_count = ibmveth_get_sset_count,
Brian Kingddbb4de2007-08-17 09:16:43 -0500903 .get_ethtool_stats = ibmveth_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904};
905
906static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
907{
908 return -EOPNOTSUPP;
909}
910
911#define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
912
Santiago Leon6e8ab302010-09-03 18:28:36 +0000913static int ibmveth_send(struct ibmveth_adapter *adapter,
914 union ibmveth_buf_desc *descs)
915{
916 unsigned long correlator;
917 unsigned int retry_count;
918 unsigned long ret;
919
920 /*
921 * The retry count sets a maximum for the number of broadcast and
922 * multicast destinations within the system.
923 */
924 retry_count = 1024;
925 correlator = 0;
926 do {
927 ret = h_send_logical_lan(adapter->vdev->unit_address,
928 descs[0].desc, descs[1].desc,
929 descs[2].desc, descs[3].desc,
930 descs[4].desc, descs[5].desc,
931 correlator, &correlator);
932 } while ((ret == H_BUSY) && (retry_count--));
933
934 if (ret != H_SUCCESS && ret != H_DROPPED) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000935 netdev_err(adapter->netdev, "tx: h_send_logical_lan failed "
936 "with rc=%ld\n", ret);
Santiago Leon6e8ab302010-09-03 18:28:36 +0000937 return 1;
938 }
939
940 return 0;
941}
942
Stephen Hemminger613573252009-08-31 19:50:58 +0000943static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb,
944 struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Wang Chen4cf16532008-11-12 23:38:14 -0800946 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Santiago Leon6e8ab302010-09-03 18:28:36 +0000947 unsigned int desc_flags;
948 union ibmveth_buf_desc descs[6];
949 int last, i;
950 int force_bounce = 0;
David S. Miller8decf862011-09-22 03:23:13 -0400951 dma_addr_t dma_addr;
Santiago Leon60296d92005-10-26 10:47:16 -0600952
Santiago Leon6e8ab302010-09-03 18:28:36 +0000953 /*
954 * veth handles a maximum of 6 segments including the header, so
955 * we have to linearize the skb if there are more than this.
956 */
957 if (skb_shinfo(skb)->nr_frags > 5 && __skb_linearize(skb)) {
958 netdev->stats.tx_dropped++;
Brian Kingf4ff2872007-09-15 13:36:07 -0700959 goto out;
960 }
961
Santiago Leon6e8ab302010-09-03 18:28:36 +0000962 /* veth can't checksum offload UDP */
963 if (skb->ip_summed == CHECKSUM_PARTIAL &&
Santiago Leonab78df72010-09-03 18:28:52 +0000964 ((skb->protocol == htons(ETH_P_IP) &&
965 ip_hdr(skb)->protocol != IPPROTO_TCP) ||
966 (skb->protocol == htons(ETH_P_IPV6) &&
967 ipv6_hdr(skb)->nexthdr != IPPROTO_TCP)) &&
968 skb_checksum_help(skb)) {
969
Santiago Leon21c2dec2010-09-03 18:29:19 +0000970 netdev_err(netdev, "tx: failed to checksum packet\n");
Santiago Leon6e8ab302010-09-03 18:28:36 +0000971 netdev->stats.tx_dropped++;
972 goto out;
973 }
Brian Kingf4ff2872007-09-15 13:36:07 -0700974
Santiago Leon6e8ab302010-09-03 18:28:36 +0000975 desc_flags = IBMVETH_BUF_VALID;
976
977 if (skb->ip_summed == CHECKSUM_PARTIAL) {
978 unsigned char *buf = skb_transport_header(skb) +
979 skb->csum_offset;
980
981 desc_flags |= (IBMVETH_BUF_NO_CSUM | IBMVETH_BUF_CSUM_GOOD);
Brian Kingf4ff2872007-09-15 13:36:07 -0700982
983 /* Need to zero out the checksum */
984 buf[0] = 0;
985 buf[1] = 0;
986 }
987
Santiago Leon6e8ab302010-09-03 18:28:36 +0000988retry_bounce:
989 memset(descs, 0, sizeof(descs));
Santiago Leonc08cc3c2010-09-03 18:28:20 +0000990
Santiago Leon6e8ab302010-09-03 18:28:36 +0000991 /*
992 * If a linear packet is below the rx threshold then
993 * copy it into the static bounce buffer. This avoids the
994 * cost of a TCE insert and remove.
995 */
996 if (force_bounce || (!skb_is_nonlinear(skb) &&
997 (skb->len < tx_copybreak))) {
Robert Jennings1096d632008-07-24 04:34:52 +1000998 skb_copy_from_linear_data(skb, adapter->bounce_buffer,
999 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Santiago Leon6e8ab302010-09-03 18:28:36 +00001001 descs[0].fields.flags_len = desc_flags | skb->len;
1002 descs[0].fields.address = adapter->bounce_buffer_dma;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001003
Santiago Leon6e8ab302010-09-03 18:28:36 +00001004 if (ibmveth_send(adapter, descs)) {
1005 adapter->tx_send_failed++;
1006 netdev->stats.tx_dropped++;
1007 } else {
1008 netdev->stats.tx_packets++;
1009 netdev->stats.tx_bytes += skb->len;
1010 }
1011
1012 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
Santiago Leon6e8ab302010-09-03 18:28:36 +00001015 /* Map the header */
David S. Miller8decf862011-09-22 03:23:13 -04001016 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
1017 skb_headlen(skb), DMA_TO_DEVICE);
1018 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
Santiago Leon6e8ab302010-09-03 18:28:36 +00001019 goto map_failed;
1020
1021 descs[0].fields.flags_len = desc_flags | skb_headlen(skb);
David S. Miller8decf862011-09-22 03:23:13 -04001022 descs[0].fields.address = dma_addr;
Santiago Leon6e8ab302010-09-03 18:28:36 +00001023
1024 /* Map the frags */
1025 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001026 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Santiago Leon6e8ab302010-09-03 18:28:36 +00001027
Ian Campbell8838a532011-08-31 00:46:53 +00001028 dma_addr = skb_frag_dma_map(&adapter->vdev->dev, frag, 0,
Eric Dumazet9e903e02011-10-18 21:00:24 +00001029 skb_frag_size(frag), DMA_TO_DEVICE);
Santiago Leon6e8ab302010-09-03 18:28:36 +00001030
1031 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
1032 goto map_failed_frags;
1033
Eric Dumazet9e903e02011-10-18 21:00:24 +00001034 descs[i+1].fields.flags_len = desc_flags | skb_frag_size(frag);
Santiago Leon6e8ab302010-09-03 18:28:36 +00001035 descs[i+1].fields.address = dma_addr;
1036 }
1037
1038 if (ibmveth_send(adapter, descs)) {
1039 adapter->tx_send_failed++;
1040 netdev->stats.tx_dropped++;
1041 } else {
1042 netdev->stats.tx_packets++;
1043 netdev->stats.tx_bytes += skb->len;
1044 }
1045
David S. Miller8decf862011-09-22 03:23:13 -04001046 dma_unmap_single(&adapter->vdev->dev,
1047 descs[0].fields.address,
1048 descs[0].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1049 DMA_TO_DEVICE);
1050
1051 for (i = 1; i < skb_shinfo(skb)->nr_frags + 1; i++)
Santiago Leon6e8ab302010-09-03 18:28:36 +00001052 dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
1053 descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1054 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Santiago Leone8cb7eb2010-09-03 18:28:15 +00001056out:
Eric W. Biederman26faa9d2014-03-15 17:29:34 -07001057 dev_consume_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001058 return NETDEV_TX_OK;
Santiago Leon6e8ab302010-09-03 18:28:36 +00001059
1060map_failed_frags:
1061 last = i+1;
1062 for (i = 0; i < last; i++)
1063 dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
1064 descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1065 DMA_TO_DEVICE);
1066
1067map_failed:
1068 if (!firmware_has_feature(FW_FEATURE_CMO))
Santiago Leon21c2dec2010-09-03 18:29:19 +00001069 netdev_err(netdev, "tx: unable to map xmit buffer\n");
Santiago Leon6e8ab302010-09-03 18:28:36 +00001070 adapter->tx_map_failed++;
1071 skb_linearize(skb);
1072 force_bounce = 1;
1073 goto retry_bounce;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001076static int ibmveth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Santiago Leonf148f612010-09-03 18:29:30 +00001078 struct ibmveth_adapter *adapter =
1079 container_of(napi, struct ibmveth_adapter, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001080 struct net_device *netdev = adapter->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 int frames_processed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 unsigned long lpar_rc;
1083
Santiago Leonf148f612010-09-03 18:29:30 +00001084restart_poll:
Eric W. Biedermancb013ea2014-03-14 18:03:50 -07001085 while (frames_processed < budget) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001086 if (!ibmveth_rxq_pending_buffer(adapter))
1087 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Anton Blanchardf89e49e2010-09-06 18:21:41 -07001089 smp_rmb();
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001090 if (!ibmveth_rxq_buffer_valid(adapter)) {
1091 wmb(); /* suggested by larson1 */
1092 adapter->rx_invalid_buffer++;
Santiago Leonc43ced12010-09-03 18:29:14 +00001093 netdev_dbg(netdev, "recycling invalid buffer\n");
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001094 ibmveth_rxq_recycle_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 } else {
Santiago Leon8d86c612010-09-03 18:28:25 +00001096 struct sk_buff *skb, *new_skb;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001097 int length = ibmveth_rxq_frame_length(adapter);
1098 int offset = ibmveth_rxq_frame_offset(adapter);
Brian Kingf4ff2872007-09-15 13:36:07 -07001099 int csum_good = ibmveth_rxq_csum_good(adapter);
1100
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001101 skb = ibmveth_rxq_get_buffer(adapter);
1102
Santiago Leon8d86c612010-09-03 18:28:25 +00001103 new_skb = NULL;
1104 if (length < rx_copybreak)
1105 new_skb = netdev_alloc_skb(netdev, length);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001106
Santiago Leon8d86c612010-09-03 18:28:25 +00001107 if (new_skb) {
1108 skb_copy_to_linear_data(new_skb,
1109 skb->data + offset,
1110 length);
Santiago Leon0c26b672010-09-03 18:28:41 +00001111 if (rx_flush)
1112 ibmveth_flush_buffer(skb->data,
1113 length + offset);
David S. Miller8decf862011-09-22 03:23:13 -04001114 if (!ibmveth_rxq_recycle_buffer(adapter))
1115 kfree_skb(skb);
Santiago Leon8d86c612010-09-03 18:28:25 +00001116 skb = new_skb;
Santiago Leon8d86c612010-09-03 18:28:25 +00001117 } else {
1118 ibmveth_rxq_harvest_buffer(adapter);
1119 skb_reserve(skb, offset);
1120 }
1121
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001122 skb_put(skb, length);
1123 skb->protocol = eth_type_trans(skb, netdev);
1124
Santiago Leon8d86c612010-09-03 18:28:25 +00001125 if (csum_good)
1126 skb->ip_summed = CHECKSUM_UNNECESSARY;
1127
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001128 netif_receive_skb(skb); /* send it up */
1129
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001130 netdev->stats.rx_packets++;
1131 netdev->stats.rx_bytes += length;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001132 frames_processed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
Eric W. Biedermancb013ea2014-03-14 18:03:50 -07001134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Santiago Leone2adbcb2005-10-26 10:47:08 -06001136 ibmveth_replenish_task(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001138 if (frames_processed < budget) {
Yongbae Park4736edc2015-03-10 11:15:39 +09001139 napi_complete(napi);
1140
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001141 /* We think we are done - reenable interrupts,
1142 * then check once more to make sure we are done.
1143 */
1144 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1145 VIO_IRQ_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Santiago Leon64859112010-09-03 18:29:41 +00001147 BUG_ON(lpar_rc != H_SUCCESS);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001148
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001149 if (ibmveth_rxq_pending_buffer(adapter) &&
Ben Hutchings288379f2009-01-19 16:43:59 -08001150 napi_reschedule(napi)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001151 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1152 VIO_IRQ_DISABLE);
1153 goto restart_poll;
1154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001157 return frames_processed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
David Howells7d12e782006-10-05 14:55:46 +01001160static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001161{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct net_device *netdev = dev_instance;
Wang Chen4cf16532008-11-12 23:38:14 -08001163 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 unsigned long lpar_rc;
1165
Ben Hutchings288379f2009-01-19 16:43:59 -08001166 if (napi_schedule_prep(&adapter->napi)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001167 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1168 VIO_IRQ_DISABLE);
Santiago Leon64859112010-09-03 18:29:41 +00001169 BUG_ON(lpar_rc != H_SUCCESS);
Ben Hutchings288379f2009-01-19 16:43:59 -08001170 __napi_schedule(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172 return IRQ_HANDLED;
1173}
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175static void ibmveth_set_multicast_list(struct net_device *netdev)
1176{
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
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001180 if ((netdev->flags & IFF_PROMISC) ||
1181 (netdev_mc_count(netdev) > adapter->mcastFilterSize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1183 IbmVethMcastEnableRecv |
1184 IbmVethMcastDisableFiltering,
1185 0);
Santiago Leonf148f612010-09-03 18:29:30 +00001186 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001187 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1188 "entering promisc mode\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001191 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 /* clear the filter table & disable filtering */
1193 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1194 IbmVethMcastEnableRecv |
1195 IbmVethMcastDisableFiltering |
1196 IbmVethMcastClearFilterTable,
1197 0);
Santiago Leonf148f612010-09-03 18:29:30 +00001198 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001199 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1200 "attempting to clear filter table\n",
1201 lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203 /* add the addresses to the filter table */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001204 netdev_for_each_mc_addr(ha, netdev) {
Santiago Leonf148f612010-09-03 18:29:30 +00001205 /* add the multicast address to the filter table */
Anton Blanchardd746ca92014-03-05 14:51:37 +11001206 u64 mcast_addr;
1207 mcast_addr = ibmveth_encode_mac_addr(ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1209 IbmVethMcastAddFilter,
1210 mcast_addr);
Santiago Leonf148f612010-09-03 18:29:30 +00001211 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001212 netdev_err(netdev, "h_multicast_ctrl rc=%ld "
1213 "when adding an entry to the filter "
1214 "table\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 /* re-enable filtering */
1219 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1220 IbmVethMcastEnableFiltering,
1221 0);
Santiago Leonf148f612010-09-03 18:29:30 +00001222 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001223 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1224 "enabling filtering\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226 }
1227}
1228
1229static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
1230{
Wang Chen4cf16532008-11-12 23:38:14 -08001231 struct ibmveth_adapter *adapter = netdev_priv(dev);
Robert Jennings1096d632008-07-24 04:34:52 +10001232 struct vio_dev *viodev = adapter->vdev;
Santiago Leon860f2422006-04-25 11:19:59 -05001233 int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
Robert Jennings0645bab2010-08-17 09:15:45 +00001234 int i, rc;
1235 int need_restart = 0;
Santiago Leonb6d35182005-10-26 10:47:01 -06001236
Santiago Leon517e80e2010-09-03 18:29:25 +00001237 if (new_mtu < IBMVETH_MIN_MTU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return -EINVAL;
Santiago Leonb6d35182005-10-26 10:47:01 -06001239
Santiago Leon517e80e2010-09-03 18:29:25 +00001240 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Brian Kingce6eea52007-06-08 14:05:17 -05001241 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size)
1242 break;
1243
Santiago Leon517e80e2010-09-03 18:29:25 +00001244 if (i == IBMVETH_NUM_BUFF_POOLS)
Brian Kingce6eea52007-06-08 14:05:17 -05001245 return -EINVAL;
1246
Santiago Leonea866e62008-07-24 04:34:23 +10001247 /* Deactivate all the buffer pools so that the next loop can activate
1248 only the buffer pools necessary to hold the new MTU */
Robert Jennings0645bab2010-08-17 09:15:45 +00001249 if (netif_running(adapter->netdev)) {
1250 need_restart = 1;
1251 adapter->pool_config = 1;
1252 ibmveth_close(adapter->netdev);
1253 adapter->pool_config = 0;
1254 }
Brian Kingce6eea52007-06-08 14:05:17 -05001255
Santiago Leonea866e62008-07-24 04:34:23 +10001256 /* Look for an active buffer pool that can hold the new MTU */
Santiago Leonf148f612010-09-03 18:29:30 +00001257 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Santiago Leonea866e62008-07-24 04:34:23 +10001258 adapter->rx_buff_pool[i].active = 1;
1259
Santiago Leon860f2422006-04-25 11:19:59 -05001260 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size) {
Robert Jennings1096d632008-07-24 04:34:52 +10001261 dev->mtu = new_mtu;
1262 vio_cmo_set_dev_desired(viodev,
1263 ibmveth_get_desired_dma
1264 (viodev));
Robert Jennings0645bab2010-08-17 09:15:45 +00001265 if (need_restart) {
1266 return ibmveth_open(adapter->netdev);
1267 }
Santiago Leon860f2422006-04-25 11:19:59 -05001268 return 0;
Santiago Leonb6d35182005-10-26 10:47:01 -06001269 }
Santiago Leonb6d35182005-10-26 10:47:01 -06001270 }
Robert Jennings0645bab2010-08-17 09:15:45 +00001271
1272 if (need_restart && (rc = ibmveth_open(adapter->netdev)))
1273 return rc;
1274
Santiago Leon860f2422006-04-25 11:19:59 -05001275 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
Santiago Leon6b422372006-10-03 12:24:28 -05001278#ifdef CONFIG_NET_POLL_CONTROLLER
1279static void ibmveth_poll_controller(struct net_device *dev)
1280{
Wang Chen4cf16532008-11-12 23:38:14 -08001281 ibmveth_replenish_task(netdev_priv(dev));
Andrew Morton5f771132006-10-10 14:33:30 -07001282 ibmveth_interrupt(dev->irq, dev);
Santiago Leon6b422372006-10-03 12:24:28 -05001283}
1284#endif
1285
Robert Jennings1096d632008-07-24 04:34:52 +10001286/**
1287 * ibmveth_get_desired_dma - Calculate IO memory desired by the driver
1288 *
1289 * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
1290 *
1291 * Return value:
1292 * Number of bytes of IO data the driver will need to perform well.
1293 */
1294static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev)
1295{
1296 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
1297 struct ibmveth_adapter *adapter;
Alistair Poppled0847752013-12-09 18:17:03 +11001298 struct iommu_table *tbl;
Robert Jennings1096d632008-07-24 04:34:52 +10001299 unsigned long ret;
1300 int i;
1301 int rxqentries = 1;
1302
Alistair Poppled0847752013-12-09 18:17:03 +11001303 tbl = get_iommu_table_base(&vdev->dev);
1304
Robert Jennings1096d632008-07-24 04:34:52 +10001305 /* netdev inits at probe time along with the structures we need below*/
1306 if (netdev == NULL)
Alistair Poppled0847752013-12-09 18:17:03 +11001307 return IOMMU_PAGE_ALIGN(IBMVETH_IO_ENTITLEMENT_DEFAULT, tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001308
1309 adapter = netdev_priv(netdev);
1310
1311 ret = IBMVETH_BUFF_LIST_SIZE + IBMVETH_FILT_LIST_SIZE;
Alistair Poppled0847752013-12-09 18:17:03 +11001312 ret += IOMMU_PAGE_ALIGN(netdev->mtu, tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001313
Santiago Leon517e80e2010-09-03 18:29:25 +00001314 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Robert Jennings1096d632008-07-24 04:34:52 +10001315 /* add the size of the active receive buffers */
1316 if (adapter->rx_buff_pool[i].active)
1317 ret +=
1318 adapter->rx_buff_pool[i].size *
1319 IOMMU_PAGE_ALIGN(adapter->rx_buff_pool[i].
Alistair Poppled0847752013-12-09 18:17:03 +11001320 buff_size, tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001321 rxqentries += adapter->rx_buff_pool[i].size;
1322 }
1323 /* add the size of the receive queue entries */
Alistair Poppled0847752013-12-09 18:17:03 +11001324 ret += IOMMU_PAGE_ALIGN(
1325 rxqentries * sizeof(struct ibmveth_rx_q_entry), tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001326
1327 return ret;
1328}
1329
Thomas Falconc77c7612015-03-02 11:56:12 -06001330static int ibmveth_set_mac_addr(struct net_device *dev, void *p)
1331{
1332 struct ibmveth_adapter *adapter = netdev_priv(dev);
1333 struct sockaddr *addr = p;
1334 u64 mac_address;
1335 int rc;
1336
1337 if (!is_valid_ether_addr(addr->sa_data))
1338 return -EADDRNOTAVAIL;
1339
1340 mac_address = ibmveth_encode_mac_addr(addr->sa_data);
1341 rc = h_change_logical_lan_mac(adapter->vdev->unit_address, mac_address);
1342 if (rc) {
1343 netdev_err(adapter->netdev, "h_change_logical_lan_mac failed with rc=%d\n", rc);
1344 return rc;
1345 }
1346
1347 ether_addr_copy(dev->dev_addr, addr->sa_data);
1348
1349 return 0;
1350}
1351
Alexander Beregalove186d172009-04-15 12:52:39 +00001352static const struct net_device_ops ibmveth_netdev_ops = {
1353 .ndo_open = ibmveth_open,
1354 .ndo_stop = ibmveth_close,
1355 .ndo_start_xmit = ibmveth_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001356 .ndo_set_rx_mode = ibmveth_set_multicast_list,
Alexander Beregalove186d172009-04-15 12:52:39 +00001357 .ndo_do_ioctl = ibmveth_ioctl,
1358 .ndo_change_mtu = ibmveth_change_mtu,
Michał Mirosławb9367bf2011-04-19 02:14:25 +00001359 .ndo_fix_features = ibmveth_fix_features,
1360 .ndo_set_features = ibmveth_set_features,
Alexander Beregalove186d172009-04-15 12:52:39 +00001361 .ndo_validate_addr = eth_validate_addr,
Thomas Falconc77c7612015-03-02 11:56:12 -06001362 .ndo_set_mac_address = ibmveth_set_mac_addr,
Alexander Beregalove186d172009-04-15 12:52:39 +00001363#ifdef CONFIG_NET_POLL_CONTROLLER
1364 .ndo_poll_controller = ibmveth_poll_controller,
1365#endif
1366};
1367
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001368static int ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Benjamin Herrenschmidt13f85202013-05-03 17:19:01 +00001370 int rc, i, mac_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 struct net_device *netdev;
Mariusz Kozlowski9dc83af2007-08-06 23:44:03 +02001372 struct ibmveth_adapter *adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 unsigned char *mac_addr_p;
1374 unsigned int *mcastFilterSize_p;
1375
Santiago Leonc43ced12010-09-03 18:29:14 +00001376 dev_dbg(&dev->dev, "entering ibmveth_probe for UA 0x%x\n",
1377 dev->unit_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Santiago Leonf148f612010-09-03 18:29:30 +00001379 mac_addr_p = (unsigned char *)vio_get_attribute(dev, VETH_MAC_ADDR,
Benjamin Herrenschmidt13f85202013-05-03 17:19:01 +00001380 &mac_len);
Santiago Leonf148f612010-09-03 18:29:30 +00001381 if (!mac_addr_p) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001382 dev_err(&dev->dev, "Can't find VETH_MAC_ADDR attribute\n");
Santiago Leonbe35ae92010-09-03 18:29:36 +00001383 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Benjamin Herrenschmidt13f85202013-05-03 17:19:01 +00001385 /* Workaround for old/broken pHyp */
1386 if (mac_len == 8)
1387 mac_addr_p += 2;
1388 else if (mac_len != 6) {
1389 dev_err(&dev->dev, "VETH_MAC_ADDR attribute wrong len %d\n",
1390 mac_len);
1391 return -EINVAL;
1392 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001393
Santiago Leonf148f612010-09-03 18:29:30 +00001394 mcastFilterSize_p = (unsigned int *)vio_get_attribute(dev,
Michael Ellerman493a6842007-04-17 13:12:55 +10001395 VETH_MCAST_FILTER_SIZE, NULL);
Santiago Leonf148f612010-09-03 18:29:30 +00001396 if (!mcastFilterSize_p) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001397 dev_err(&dev->dev, "Can't find VETH_MCAST_FILTER_SIZE "
1398 "attribute\n");
Santiago Leonbe35ae92010-09-03 18:29:36 +00001399 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
1403
Santiago Leonf148f612010-09-03 18:29:30 +00001404 if (!netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return -ENOMEM;
1406
Wang Chen4cf16532008-11-12 23:38:14 -08001407 adapter = netdev_priv(netdev);
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07001408 dev_set_drvdata(&dev->dev, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 adapter->vdev = dev;
1411 adapter->netdev = netdev;
Santiago Leonf148f612010-09-03 18:29:30 +00001412 adapter->mcastFilterSize = *mcastFilterSize_p;
Santiago Leon860f2422006-04-25 11:19:59 -05001413 adapter->pool_config = 0;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001414
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001415 netif_napi_add(netdev, &adapter->napi, ibmveth_poll, 16);
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 netdev->irq = dev->irq;
Alexander Beregalove186d172009-04-15 12:52:39 +00001418 netdev->netdev_ops = &ibmveth_netdev_ops;
1419 netdev->ethtool_ops = &netdev_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 SET_NETDEV_DEV(netdev, &dev->dev);
Michał Mirosławb9367bf2011-04-19 02:14:25 +00001421 netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
1422 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1423 netdev->features |= netdev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Anton Blanchardd746ca92014-03-05 14:51:37 +11001425 memcpy(netdev->dev_addr, mac_addr_p, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Santiago Leonf148f612010-09-03 18:29:30 +00001427 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Santiago Leon860f2422006-04-25 11:19:59 -05001428 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
Greg Kroah-Hartman8dde2a92007-12-17 15:54:39 -04001429 int error;
1430
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001431 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1432 pool_count[i], pool_size[i],
Santiago Leon860f2422006-04-25 11:19:59 -05001433 pool_active[i]);
Greg Kroah-Hartman8dde2a92007-12-17 15:54:39 -04001434 error = kobject_init_and_add(kobj, &ktype_veth_pool,
1435 &dev->dev.kobj, "pool%d", i);
1436 if (!error)
1437 kobject_uevent(kobj, KOBJ_ADD);
Santiago Leon860f2422006-04-25 11:19:59 -05001438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Santiago Leonc43ced12010-09-03 18:29:14 +00001440 netdev_dbg(netdev, "adapter @ 0x%p\n", adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 adapter->buffer_list_dma = DMA_ERROR_CODE;
1443 adapter->filter_list_dma = DMA_ERROR_CODE;
1444 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1445
Santiago Leonc43ced12010-09-03 18:29:14 +00001446 netdev_dbg(netdev, "registering netdev...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Michał Mirosławb801a4e2011-04-28 11:59:15 +10001448 ibmveth_set_features(netdev, netdev->features);
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 rc = register_netdev(netdev);
1451
Santiago Leonf148f612010-09-03 18:29:30 +00001452 if (rc) {
Santiago Leonc43ced12010-09-03 18:29:14 +00001453 netdev_dbg(netdev, "failed to register netdev rc=%d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 free_netdev(netdev);
1455 return rc;
1456 }
1457
Santiago Leonc43ced12010-09-03 18:29:14 +00001458 netdev_dbg(netdev, "registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return 0;
1461}
1462
Bill Pembertone11787a2012-12-03 09:23:12 -05001463static int ibmveth_remove(struct vio_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07001465 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Wang Chen4cf16532008-11-12 23:38:14 -08001466 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001467 int i;
1468
Santiago Leonf148f612010-09-03 18:29:30 +00001469 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001470 kobject_put(&adapter->rx_buff_pool[i].kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 unregister_netdev(netdev);
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 free_netdev(netdev);
Robert Jennings1096d632008-07-24 04:34:52 +10001475 dev_set_drvdata(&dev->dev, NULL);
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return 0;
1478}
1479
Santiago Leon860f2422006-04-25 11:19:59 -05001480static struct attribute veth_active_attr;
1481static struct attribute veth_num_attr;
1482static struct attribute veth_size_attr;
1483
Santiago Leonf148f612010-09-03 18:29:30 +00001484static ssize_t veth_pool_show(struct kobject *kobj,
1485 struct attribute *attr, char *buf)
Santiago Leon860f2422006-04-25 11:19:59 -05001486{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001487 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001488 struct ibmveth_buff_pool,
1489 kobj);
1490
1491 if (attr == &veth_active_attr)
1492 return sprintf(buf, "%d\n", pool->active);
1493 else if (attr == &veth_num_attr)
1494 return sprintf(buf, "%d\n", pool->size);
1495 else if (attr == &veth_size_attr)
1496 return sprintf(buf, "%d\n", pool->buff_size);
1497 return 0;
1498}
1499
Santiago Leonf148f612010-09-03 18:29:30 +00001500static ssize_t veth_pool_store(struct kobject *kobj, struct attribute *attr,
1501 const char *buf, size_t count)
Santiago Leon860f2422006-04-25 11:19:59 -05001502{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001503 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001504 struct ibmveth_buff_pool,
1505 kobj);
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07001506 struct net_device *netdev = dev_get_drvdata(
1507 container_of(kobj->parent, struct device, kobj));
Wang Chen4cf16532008-11-12 23:38:14 -08001508 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001509 long value = simple_strtol(buf, NULL, 10);
1510 long rc;
1511
1512 if (attr == &veth_active_attr) {
1513 if (value && !pool->active) {
Brian King4aa9c932007-06-08 14:05:16 -05001514 if (netif_running(netdev)) {
Santiago Leonf148f612010-09-03 18:29:30 +00001515 if (ibmveth_alloc_buffer_pool(pool)) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001516 netdev_err(netdev,
1517 "unable to alloc pool\n");
Brian King4aa9c932007-06-08 14:05:16 -05001518 return -ENOMEM;
1519 }
1520 pool->active = 1;
1521 adapter->pool_config = 1;
1522 ibmveth_close(netdev);
1523 adapter->pool_config = 0;
1524 if ((rc = ibmveth_open(netdev)))
1525 return rc;
Santiago Leonf148f612010-09-03 18:29:30 +00001526 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001527 pool->active = 1;
Santiago Leonf148f612010-09-03 18:29:30 +00001528 }
Santiago Leon860f2422006-04-25 11:19:59 -05001529 } else if (!value && pool->active) {
1530 int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1531 int i;
1532 /* Make sure there is a buffer pool with buffers that
1533 can hold a packet of the size of the MTU */
Santiago Leon517e80e2010-09-03 18:29:25 +00001534 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Santiago Leon860f2422006-04-25 11:19:59 -05001535 if (pool == &adapter->rx_buff_pool[i])
1536 continue;
1537 if (!adapter->rx_buff_pool[i].active)
1538 continue;
Brian King76b9cfc2007-08-03 13:55:19 +10001539 if (mtu <= adapter->rx_buff_pool[i].buff_size)
1540 break;
Santiago Leon860f2422006-04-25 11:19:59 -05001541 }
Brian King76b9cfc2007-08-03 13:55:19 +10001542
Santiago Leon517e80e2010-09-03 18:29:25 +00001543 if (i == IBMVETH_NUM_BUFF_POOLS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001544 netdev_err(netdev, "no active pool >= MTU\n");
Santiago Leon860f2422006-04-25 11:19:59 -05001545 return -EPERM;
1546 }
Brian King76b9cfc2007-08-03 13:55:19 +10001547
Brian King76b9cfc2007-08-03 13:55:19 +10001548 if (netif_running(netdev)) {
1549 adapter->pool_config = 1;
1550 ibmveth_close(netdev);
Santiago Leonea866e62008-07-24 04:34:23 +10001551 pool->active = 0;
Brian King76b9cfc2007-08-03 13:55:19 +10001552 adapter->pool_config = 0;
1553 if ((rc = ibmveth_open(netdev)))
1554 return rc;
1555 }
Santiago Leonea866e62008-07-24 04:34:23 +10001556 pool->active = 0;
Santiago Leon860f2422006-04-25 11:19:59 -05001557 }
1558 } else if (attr == &veth_num_attr) {
Santiago Leonf148f612010-09-03 18:29:30 +00001559 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT) {
Santiago Leon860f2422006-04-25 11:19:59 -05001560 return -EINVAL;
Santiago Leonf148f612010-09-03 18:29:30 +00001561 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001562 if (netif_running(netdev)) {
1563 adapter->pool_config = 1;
1564 ibmveth_close(netdev);
1565 adapter->pool_config = 0;
1566 pool->size = value;
1567 if ((rc = ibmveth_open(netdev)))
1568 return rc;
Santiago Leonf148f612010-09-03 18:29:30 +00001569 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001570 pool->size = value;
Santiago Leonf148f612010-09-03 18:29:30 +00001571 }
Santiago Leon860f2422006-04-25 11:19:59 -05001572 }
1573 } else if (attr == &veth_size_attr) {
Santiago Leonf148f612010-09-03 18:29:30 +00001574 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE) {
Santiago Leon860f2422006-04-25 11:19:59 -05001575 return -EINVAL;
Santiago Leonf148f612010-09-03 18:29:30 +00001576 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001577 if (netif_running(netdev)) {
1578 adapter->pool_config = 1;
1579 ibmveth_close(netdev);
1580 adapter->pool_config = 0;
1581 pool->buff_size = value;
1582 if ((rc = ibmveth_open(netdev)))
1583 return rc;
Santiago Leonf148f612010-09-03 18:29:30 +00001584 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001585 pool->buff_size = value;
Santiago Leonf148f612010-09-03 18:29:30 +00001586 }
Santiago Leon860f2422006-04-25 11:19:59 -05001587 }
1588 }
1589
1590 /* kick the interrupt handler to allocate/deallocate pools */
David Howells7d12e782006-10-05 14:55:46 +01001591 ibmveth_interrupt(netdev->irq, netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001592 return count;
1593}
1594
1595
Santiago Leonf148f612010-09-03 18:29:30 +00001596#define ATTR(_name, _mode) \
1597 struct attribute veth_##_name##_attr = { \
1598 .name = __stringify(_name), .mode = _mode, \
1599 };
Santiago Leon860f2422006-04-25 11:19:59 -05001600
1601static ATTR(active, 0644);
1602static ATTR(num, 0644);
1603static ATTR(size, 0644);
1604
Santiago Leonf148f612010-09-03 18:29:30 +00001605static struct attribute *veth_pool_attrs[] = {
Santiago Leon860f2422006-04-25 11:19:59 -05001606 &veth_active_attr,
1607 &veth_num_attr,
1608 &veth_size_attr,
1609 NULL,
1610};
1611
Emese Revfy52cf25d2010-01-19 02:58:23 +01001612static const struct sysfs_ops veth_pool_ops = {
Santiago Leon860f2422006-04-25 11:19:59 -05001613 .show = veth_pool_show,
1614 .store = veth_pool_store,
1615};
1616
1617static struct kobj_type ktype_veth_pool = {
1618 .release = NULL,
1619 .sysfs_ops = &veth_pool_ops,
1620 .default_attrs = veth_pool_attrs,
1621};
1622
Brian Kinge7a3af52010-05-07 08:56:08 +00001623static int ibmveth_resume(struct device *dev)
1624{
1625 struct net_device *netdev = dev_get_drvdata(dev);
1626 ibmveth_interrupt(netdev->irq, netdev);
1627 return 0;
1628}
Santiago Leon860f2422006-04-25 11:19:59 -05001629
Bill Pembertone11787a2012-12-03 09:23:12 -05001630static struct vio_device_id ibmveth_device_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 { "network", "IBM,l-lan"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +10001632 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1635
Brian Kinge7a3af52010-05-07 08:56:08 +00001636static struct dev_pm_ops ibmveth_pm_ops = {
1637 .resume = ibmveth_resume
1638};
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640static struct vio_driver ibmveth_driver = {
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001641 .id_table = ibmveth_device_table,
1642 .probe = ibmveth_probe,
1643 .remove = ibmveth_remove,
Robert Jennings1096d632008-07-24 04:34:52 +10001644 .get_desired_dma = ibmveth_get_desired_dma,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +00001645 .name = ibmveth_driver_name,
1646 .pm = &ibmveth_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647};
1648
1649static int __init ibmveth_module_init(void)
1650{
Santiago Leon21c2dec2010-09-03 18:29:19 +00001651 printk(KERN_DEBUG "%s: %s %s\n", ibmveth_driver_name,
1652 ibmveth_driver_string, ibmveth_driver_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return vio_register_driver(&ibmveth_driver);
1655}
1656
1657static void __exit ibmveth_module_exit(void)
1658{
1659 vio_unregister_driver(&ibmveth_driver);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001660}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662module_init(ibmveth_module_init);
1663module_exit(ibmveth_module_exit);