blob: ebe60719e489cd1fbdc12248ee48eed6d0979fd4 [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
Thomas Falcon07e6a972015-07-14 10:51:51 -050082static bool old_large_send __read_mostly;
83module_param(old_large_send, bool, S_IRUGO);
84MODULE_PARM_DESC(old_large_send,
85 "Use old large send method on firmware that supports the new method");
86
Brian Kingddbb4de2007-08-17 09:16:43 -050087struct ibmveth_stat {
88 char name[ETH_GSTRING_LEN];
89 int offset;
90};
91
92#define IBMVETH_STAT_OFF(stat) offsetof(struct ibmveth_adapter, stat)
93#define IBMVETH_GET_STAT(a, off) *((u64 *)(((unsigned long)(a)) + off))
94
95struct ibmveth_stat ibmveth_stats[] = {
96 { "replenish_task_cycles", IBMVETH_STAT_OFF(replenish_task_cycles) },
97 { "replenish_no_mem", IBMVETH_STAT_OFF(replenish_no_mem) },
Santiago Leonf148f612010-09-03 18:29:30 +000098 { "replenish_add_buff_failure",
99 IBMVETH_STAT_OFF(replenish_add_buff_failure) },
100 { "replenish_add_buff_success",
101 IBMVETH_STAT_OFF(replenish_add_buff_success) },
Brian Kingddbb4de2007-08-17 09:16:43 -0500102 { "rx_invalid_buffer", IBMVETH_STAT_OFF(rx_invalid_buffer) },
103 { "rx_no_buffer", IBMVETH_STAT_OFF(rx_no_buffer) },
Brian Kingddbb4de2007-08-17 09:16:43 -0500104 { "tx_map_failed", IBMVETH_STAT_OFF(tx_map_failed) },
105 { "tx_send_failed", IBMVETH_STAT_OFF(tx_send_failed) },
Santiago Leonab78df72010-09-03 18:28:52 +0000106 { "fw_enabled_ipv4_csum", IBMVETH_STAT_OFF(fw_ipv4_csum_support) },
107 { "fw_enabled_ipv6_csum", IBMVETH_STAT_OFF(fw_ipv6_csum_support) },
Thomas Falcon8641dd82015-04-29 16:25:45 -0500108 { "tx_large_packets", IBMVETH_STAT_OFF(tx_large_packets) },
Thomas Falcon07e6a972015-07-14 10:51:51 -0500109 { "rx_large_packets", IBMVETH_STAT_OFF(rx_large_packets) },
110 { "fw_enabled_large_send", IBMVETH_STAT_OFF(fw_large_send_support) }
Brian Kingddbb4de2007-08-17 09:16:43 -0500111};
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/* simple methods of getting data from the current rxq entry */
Brian King79ef4a42007-08-17 09:16:56 -0500114static inline u32 ibmveth_rxq_flags(struct ibmveth_adapter *adapter)
115{
Anton Blanchard0b536be2013-09-03 09:55:32 +1000116 return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].flags_off);
Brian King79ef4a42007-08-17 09:16:56 -0500117}
118
119static inline int ibmveth_rxq_toggle(struct ibmveth_adapter *adapter)
120{
Santiago Leonf148f612010-09-03 18:29:30 +0000121 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_TOGGLE) >>
122 IBMVETH_RXQ_TOGGLE_SHIFT;
Brian King79ef4a42007-08-17 09:16:56 -0500123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
126{
Santiago Leonf148f612010-09-03 18:29:30 +0000127 return ibmveth_rxq_toggle(adapter) == adapter->rx_queue.toggle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
131{
Santiago Leonf148f612010-09-03 18:29:30 +0000132 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
136{
Santiago Leonf148f612010-09-03 18:29:30 +0000137 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_OFF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
141{
Anton Blanchard0b536be2013-09-03 09:55:32 +1000142 return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Brian Kingf4ff2872007-09-15 13:36:07 -0700145static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
146{
Santiago Leonf148f612010-09-03 18:29:30 +0000147 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_CSUM_GOOD;
Brian Kingf4ff2872007-09-15 13:36:07 -0700148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* setup the initial settings for a buffer pool */
Santiago Leonf148f612010-09-03 18:29:30 +0000151static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool,
152 u32 pool_index, u32 pool_size,
153 u32 buff_size, u32 pool_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 pool->size = pool_size;
156 pool->index = pool_index;
157 pool->buff_size = buff_size;
Santiago Leonc033a6d2010-09-03 18:28:09 +0000158 pool->threshold = pool_size * 7 / 8;
Santiago Leon860f2422006-04-25 11:19:59 -0500159 pool->active = pool_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162/* allocate and setup an buffer pool - called during open */
163static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
164{
165 int i;
166
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400167 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Santiago Leonf148f612010-09-03 18:29:30 +0000169 if (!pool->free_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Nicholas Mc Guire076ef442015-12-20 15:06:18 +0100172 pool->dma_addr = kcalloc(pool->size, sizeof(dma_addr_t), GFP_KERNEL);
Santiago Leonf148f612010-09-03 18:29:30 +0000173 if (!pool->dma_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 kfree(pool->free_map);
175 pool->free_map = NULL;
176 return -1;
177 }
178
Julia Lawalla05abcb2010-05-13 10:06:01 +0000179 pool->skbuff = kcalloc(pool->size, sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Santiago Leonf148f612010-09-03 18:29:30 +0000181 if (!pool->skbuff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 kfree(pool->dma_addr);
183 pool->dma_addr = NULL;
184
185 kfree(pool->free_map);
186 pool->free_map = NULL;
187 return -1;
188 }
189
Santiago Leonf148f612010-09-03 18:29:30 +0000190 for (i = 0; i < pool->size; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 pool->free_map[i] = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 atomic_set(&pool->available, 0);
194 pool->producer_index = 0;
195 pool->consumer_index = 0;
196
197 return 0;
198}
199
Santiago Leon0c26b672010-09-03 18:28:41 +0000200static inline void ibmveth_flush_buffer(void *addr, unsigned long length)
201{
202 unsigned long offset;
203
204 for (offset = 0; offset < length; offset += SMP_CACHE_BYTES)
205 asm("dcbfl %0,%1" :: "b" (addr), "r" (offset));
206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/* replenish the buffers for a pool. note that we don't need to
209 * skb_reserve these since they are used for incoming...
210 */
Santiago Leonf148f612010-09-03 18:29:30 +0000211static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter,
212 struct ibmveth_buff_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 u32 i;
215 u32 count = pool->size - atomic_read(&pool->available);
216 u32 buffers_added = 0;
Robert Jennings1096d632008-07-24 04:34:52 +1000217 struct sk_buff *skb;
218 unsigned int free_index, index;
219 u64 correlator;
220 unsigned long lpar_rc;
221 dma_addr_t dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 mb();
224
Santiago Leonf148f612010-09-03 18:29:30 +0000225 for (i = 0; i < count; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 union ibmveth_buf_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Santiago Leon003212c2010-09-03 18:29:03 +0000228 skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Santiago Leonf148f612010-09-03 18:29:30 +0000230 if (!skb) {
Santiago Leonc43ced12010-09-03 18:29:14 +0000231 netdev_dbg(adapter->netdev,
232 "replenish: unable to allocate skb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 adapter->replenish_no_mem++;
234 break;
235 }
236
David Gibson047a66d2006-10-21 10:24:13 -0700237 free_index = pool->consumer_index;
Santiago Leona613f582010-09-03 18:28:04 +0000238 pool->consumer_index++;
239 if (pool->consumer_index >= pool->size)
240 pool->consumer_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 index = pool->free_map[free_index];
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400242
Santiago Leon64859112010-09-03 18:29:41 +0000243 BUG_ON(index == IBM_VETH_INVALID_MAP);
244 BUG_ON(pool->skbuff[index] != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
247 pool->buff_size, DMA_FROM_DEVICE);
248
Stephen Rothwellc713e7c2008-07-28 02:14:24 +1000249 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
Robert Jennings1096d632008-07-24 04:34:52 +1000250 goto failure;
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
253 pool->dma_addr[index] = dma_addr;
254 pool->skbuff[index] = skb;
255
256 correlator = ((u64)pool->index << 32) | index;
Santiago Leonf148f612010-09-03 18:29:30 +0000257 *(u64 *)skb->data = correlator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Brian King79ef4a42007-08-17 09:16:56 -0500259 desc.fields.flags_len = IBMVETH_BUF_VALID | pool->buff_size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400260 desc.fields.address = dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Santiago Leon0c26b672010-09-03 18:28:41 +0000262 if (rx_flush) {
263 unsigned int len = min(pool->buff_size,
264 adapter->netdev->mtu +
265 IBMVETH_BUFF_OH);
266 ibmveth_flush_buffer(skb->data, len);
267 }
Santiago Leonf148f612010-09-03 18:29:30 +0000268 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address,
269 desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400270
Santiago Leonf148f612010-09-03 18:29:30 +0000271 if (lpar_rc != H_SUCCESS) {
Robert Jennings1096d632008-07-24 04:34:52 +1000272 goto failure;
Santiago Leonf148f612010-09-03 18:29:30 +0000273 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 buffers_added++;
275 adapter->replenish_add_buff_success++;
276 }
277 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 mb();
280 atomic_add(buffers_added, &(pool->available));
Robert Jennings1096d632008-07-24 04:34:52 +1000281 return;
282
283failure:
284 pool->free_map[free_index] = index;
285 pool->skbuff[index] = NULL;
286 if (pool->consumer_index == 0)
287 pool->consumer_index = pool->size - 1;
288 else
289 pool->consumer_index--;
Stephen Rothwellc713e7c2008-07-28 02:14:24 +1000290 if (!dma_mapping_error(&adapter->vdev->dev, dma_addr))
Robert Jennings1096d632008-07-24 04:34:52 +1000291 dma_unmap_single(&adapter->vdev->dev,
292 pool->dma_addr[index], pool->buff_size,
293 DMA_FROM_DEVICE);
294 dev_kfree_skb_any(skb);
295 adapter->replenish_add_buff_failure++;
296
297 mb();
298 atomic_add(buffers_added, &(pool->available));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Anton Blanchardcbd52282014-08-22 11:36:52 +1000301/*
302 * The final 8 bytes of the buffer list is a counter of frames dropped
303 * because there was not a buffer in the buffer list capable of holding
304 * the frame.
305 */
306static void ibmveth_update_rx_no_buffer(struct ibmveth_adapter *adapter)
307{
308 __be64 *p = adapter->buffer_list_addr + 4096 - 8;
309
310 adapter->rx_no_buffer = be64_to_cpup(p);
311}
312
Santiago Leone2adbcb2005-10-26 10:47:08 -0600313/* replenish routine */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400314static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Santiago Leonb6d35182005-10-26 10:47:01 -0600316 int i;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 adapter->replenish_task_cycles++;
319
Santiago Leon517e80e2010-09-03 18:29:25 +0000320 for (i = (IBMVETH_NUM_BUFF_POOLS - 1); i >= 0; i--) {
Santiago Leonc033a6d2010-09-03 18:28:09 +0000321 struct ibmveth_buff_pool *pool = &adapter->rx_buff_pool[i];
322
323 if (pool->active &&
324 (atomic_read(&pool->available) < pool->threshold))
325 ibmveth_replenish_buffer_pool(adapter, pool);
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Anton Blanchardcbd52282014-08-22 11:36:52 +1000328 ibmveth_update_rx_no_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/* empty and free ana buffer pool - also used to do cleanup in error paths */
Santiago Leonf148f612010-09-03 18:29:30 +0000332static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter,
333 struct ibmveth_buff_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 int i;
336
Jesper Juhlb4558ea2005-10-28 16:53:13 -0400337 kfree(pool->free_map);
338 pool->free_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Santiago Leonf148f612010-09-03 18:29:30 +0000340 if (pool->skbuff && pool->dma_addr) {
341 for (i = 0; i < pool->size; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct sk_buff *skb = pool->skbuff[i];
Santiago Leonf148f612010-09-03 18:29:30 +0000343 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 dma_unmap_single(&adapter->vdev->dev,
345 pool->dma_addr[i],
346 pool->buff_size,
347 DMA_FROM_DEVICE);
348 dev_kfree_skb_any(skb);
349 pool->skbuff[i] = NULL;
350 }
351 }
352 }
353
Santiago Leonf148f612010-09-03 18:29:30 +0000354 if (pool->dma_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 kfree(pool->dma_addr);
356 pool->dma_addr = NULL;
357 }
358
Santiago Leonf148f612010-09-03 18:29:30 +0000359 if (pool->skbuff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 kfree(pool->skbuff);
361 pool->skbuff = NULL;
362 }
363}
364
365/* remove a buffer from a pool */
Santiago Leonf148f612010-09-03 18:29:30 +0000366static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter,
367 u64 correlator)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 unsigned int pool = correlator >> 32;
370 unsigned int index = correlator & 0xffffffffUL;
371 unsigned int free_index;
372 struct sk_buff *skb;
373
Santiago Leon64859112010-09-03 18:29:41 +0000374 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
375 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 skb = adapter->rx_buff_pool[pool].skbuff[index];
378
Santiago Leon64859112010-09-03 18:29:41 +0000379 BUG_ON(skb == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
382
383 dma_unmap_single(&adapter->vdev->dev,
384 adapter->rx_buff_pool[pool].dma_addr[index],
385 adapter->rx_buff_pool[pool].buff_size,
386 DMA_FROM_DEVICE);
387
David Gibson047a66d2006-10-21 10:24:13 -0700388 free_index = adapter->rx_buff_pool[pool].producer_index;
Santiago Leona613f582010-09-03 18:28:04 +0000389 adapter->rx_buff_pool[pool].producer_index++;
390 if (adapter->rx_buff_pool[pool].producer_index >=
391 adapter->rx_buff_pool[pool].size)
392 adapter->rx_buff_pool[pool].producer_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 adapter->rx_buff_pool[pool].free_map[free_index] = index;
394
395 mb();
396
397 atomic_dec(&(adapter->rx_buff_pool[pool].available));
398}
399
400/* get the current buffer on the rx queue */
401static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
402{
403 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
404 unsigned int pool = correlator >> 32;
405 unsigned int index = correlator & 0xffffffffUL;
406
Santiago Leon64859112010-09-03 18:29:41 +0000407 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
408 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 return adapter->rx_buff_pool[pool].skbuff[index];
411}
412
413/* recycle the current buffer on the rx queue */
David S. Miller8decf862011-09-22 03:23:13 -0400414static int ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 u32 q_index = adapter->rx_queue.index;
417 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
418 unsigned int pool = correlator >> 32;
419 unsigned int index = correlator & 0xffffffffUL;
420 union ibmveth_buf_desc desc;
421 unsigned long lpar_rc;
David S. Miller8decf862011-09-22 03:23:13 -0400422 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Santiago Leon64859112010-09-03 18:29:41 +0000424 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
425 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Santiago Leonf148f612010-09-03 18:29:30 +0000427 if (!adapter->rx_buff_pool[pool].active) {
Santiago Leonb6d35182005-10-26 10:47:01 -0600428 ibmveth_rxq_harvest_buffer(adapter);
429 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
David S. Miller8decf862011-09-22 03:23:13 -0400430 goto out;
Santiago Leonb6d35182005-10-26 10:47:01 -0600431 }
432
Brian King79ef4a42007-08-17 09:16:56 -0500433 desc.fields.flags_len = IBMVETH_BUF_VALID |
434 adapter->rx_buff_pool[pool].buff_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
436
437 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400438
Santiago Leonf148f612010-09-03 18:29:30 +0000439 if (lpar_rc != H_SUCCESS) {
Santiago Leonc43ced12010-09-03 18:29:14 +0000440 netdev_dbg(adapter->netdev, "h_add_logical_lan_buffer failed "
441 "during recycle rc=%ld", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
David S. Miller8decf862011-09-22 03:23:13 -0400443 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
Santiago Leonf148f612010-09-03 18:29:30 +0000446 if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 adapter->rx_queue.index = 0;
448 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
449 }
David S. Miller8decf862011-09-22 03:23:13 -0400450
451out:
452 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Michael Ellerman493a6842007-04-17 13:12:55 +1000455static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
458
Santiago Leonf148f612010-09-03 18:29:30 +0000459 if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 adapter->rx_queue.index = 0;
461 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
462 }
463}
464
465static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
466{
Santiago Leonb6d35182005-10-26 10:47:01 -0600467 int i;
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700468 struct device *dev = &adapter->vdev->dev;
Santiago Leonb6d35182005-10-26 10:47:01 -0600469
Santiago Leonf148f612010-09-03 18:29:30 +0000470 if (adapter->buffer_list_addr != NULL) {
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700471 if (!dma_mapping_error(dev, adapter->buffer_list_dma)) {
472 dma_unmap_single(dev, adapter->buffer_list_dma, 4096,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 DMA_BIDIRECTIONAL);
474 adapter->buffer_list_dma = DMA_ERROR_CODE;
475 }
476 free_page((unsigned long)adapter->buffer_list_addr);
477 adapter->buffer_list_addr = NULL;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Santiago Leonf148f612010-09-03 18:29:30 +0000480 if (adapter->filter_list_addr != NULL) {
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700481 if (!dma_mapping_error(dev, adapter->filter_list_dma)) {
482 dma_unmap_single(dev, adapter->filter_list_dma, 4096,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 DMA_BIDIRECTIONAL);
484 adapter->filter_list_dma = DMA_ERROR_CODE;
485 }
486 free_page((unsigned long)adapter->filter_list_addr);
487 adapter->filter_list_addr = NULL;
488 }
489
Santiago Leonf148f612010-09-03 18:29:30 +0000490 if (adapter->rx_queue.queue_addr != NULL) {
Santiago Leond90c92f2012-09-04 14:41:37 +0000491 dma_free_coherent(dev, adapter->rx_queue.queue_len,
492 adapter->rx_queue.queue_addr,
493 adapter->rx_queue.queue_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 adapter->rx_queue.queue_addr = NULL;
495 }
496
Santiago Leonf148f612010-09-03 18:29:30 +0000497 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Santiago Leon860f2422006-04-25 11:19:59 -0500498 if (adapter->rx_buff_pool[i].active)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400499 ibmveth_free_buffer_pool(adapter,
Santiago Leon860f2422006-04-25 11:19:59 -0500500 &adapter->rx_buff_pool[i]);
Robert Jennings1096d632008-07-24 04:34:52 +1000501
502 if (adapter->bounce_buffer != NULL) {
Stephen Rothwellc713e7c2008-07-28 02:14:24 +1000503 if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
Robert Jennings1096d632008-07-24 04:34:52 +1000504 dma_unmap_single(&adapter->vdev->dev,
505 adapter->bounce_buffer_dma,
506 adapter->netdev->mtu + IBMVETH_BUFF_OH,
507 DMA_BIDIRECTIONAL);
508 adapter->bounce_buffer_dma = DMA_ERROR_CODE;
509 }
510 kfree(adapter->bounce_buffer);
511 adapter->bounce_buffer = NULL;
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500515static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
516 union ibmveth_buf_desc rxq_desc, u64 mac_address)
517{
518 int rc, try_again = 1;
519
Santiago Leonf148f612010-09-03 18:29:30 +0000520 /*
521 * After a kexec the adapter will still be open, so our attempt to
522 * open it will fail. So if we get a failure we free the adapter and
523 * try again, but only once.
524 */
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500525retry:
526 rc = h_register_logical_lan(adapter->vdev->unit_address,
527 adapter->buffer_list_dma, rxq_desc.desc,
528 adapter->filter_list_dma, mac_address);
529
530 if (rc != H_SUCCESS && try_again) {
531 do {
532 rc = h_free_logical_lan(adapter->vdev->unit_address);
533 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
534
535 try_again = 0;
536 goto retry;
537 }
538
539 return rc;
540}
541
Anton Blanchardd746ca92014-03-05 14:51:37 +1100542static u64 ibmveth_encode_mac_addr(u8 *mac)
543{
544 int i;
545 u64 encoded = 0;
546
547 for (i = 0; i < ETH_ALEN; i++)
548 encoded = (encoded << 8) | mac[i];
549
550 return encoded;
551}
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553static int ibmveth_open(struct net_device *netdev)
554{
Wang Chen4cf16532008-11-12 23:38:14 -0800555 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Anton Blanchardd746ca92014-03-05 14:51:37 +1100556 u64 mac_address;
Santiago Leonb6d35182005-10-26 10:47:01 -0600557 int rxq_entries = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 unsigned long lpar_rc;
559 int rc;
560 union ibmveth_buf_desc rxq_desc;
Santiago Leonb6d35182005-10-26 10:47:01 -0600561 int i;
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700562 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Santiago Leonc43ced12010-09-03 18:29:14 +0000564 netdev_dbg(netdev, "open starting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700566 napi_enable(&adapter->napi);
567
Santiago Leon517e80e2010-09-03 18:29:25 +0000568 for(i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Santiago Leonb6d35182005-10-26 10:47:01 -0600569 rxq_entries += adapter->rx_buff_pool[i].size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
572 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400573
Santiago Leonf148f612010-09-03 18:29:30 +0000574 if (!adapter->buffer_list_addr || !adapter->filter_list_addr) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000575 netdev_err(netdev, "unable to allocate filter or buffer list "
576 "pages\n");
Denis Kirjanov88426f22010-10-20 04:21:13 +0000577 rc = -ENOMEM;
578 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
Santiago Leond90c92f2012-09-04 14:41:37 +0000581 dev = &adapter->vdev->dev;
582
Santiago Leonf148f612010-09-03 18:29:30 +0000583 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) *
584 rxq_entries;
Santiago Leond90c92f2012-09-04 14:41:37 +0000585 adapter->rx_queue.queue_addr =
Joe Perchesd0320f72013-03-14 13:07:21 +0000586 dma_alloc_coherent(dev, adapter->rx_queue.queue_len,
587 &adapter->rx_queue.queue_dma, GFP_KERNEL);
Santiago Leonf148f612010-09-03 18:29:30 +0000588 if (!adapter->rx_queue.queue_addr) {
Denis Kirjanov88426f22010-10-20 04:21:13 +0000589 rc = -ENOMEM;
590 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700593 adapter->buffer_list_dma = dma_map_single(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700595 adapter->filter_list_dma = dma_map_single(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700598 if ((dma_mapping_error(dev, adapter->buffer_list_dma)) ||
Santiago Leond90c92f2012-09-04 14:41:37 +0000599 (dma_mapping_error(dev, adapter->filter_list_dma))) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000600 netdev_err(netdev, "unable to map filter or buffer list "
601 "pages\n");
Denis Kirjanov88426f22010-10-20 04:21:13 +0000602 rc = -ENOMEM;
603 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
606 adapter->rx_queue.index = 0;
607 adapter->rx_queue.num_slots = rxq_entries;
608 adapter->rx_queue.toggle = 1;
609
Anton Blanchardd746ca92014-03-05 14:51:37 +1100610 mac_address = ibmveth_encode_mac_addr(netdev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Santiago Leonf148f612010-09-03 18:29:30 +0000612 rxq_desc.fields.flags_len = IBMVETH_BUF_VALID |
613 adapter->rx_queue.queue_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
615
Santiago Leonc43ced12010-09-03 18:29:14 +0000616 netdev_dbg(netdev, "buffer list @ 0x%p\n", adapter->buffer_list_addr);
617 netdev_dbg(netdev, "filter list @ 0x%p\n", adapter->filter_list_addr);
618 netdev_dbg(netdev, "receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Santiago Leon4347ef12006-10-03 12:24:34 -0500620 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
621
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500622 lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Santiago Leonf148f612010-09-03 18:29:30 +0000624 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000625 netdev_err(netdev, "h_register_logical_lan failed with %ld\n",
626 lpar_rc);
627 netdev_err(netdev, "buffer TCE:0x%llx filter TCE:0x%llx rxq "
628 "desc:0x%llx MAC:0x%llx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 adapter->buffer_list_dma,
630 adapter->filter_list_dma,
631 rxq_desc.desc,
632 mac_address);
Denis Kirjanov88426f22010-10-20 04:21:13 +0000633 rc = -ENONET;
634 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
Santiago Leonf148f612010-09-03 18:29:30 +0000637 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
638 if (!adapter->rx_buff_pool[i].active)
Santiago Leon860f2422006-04-25 11:19:59 -0500639 continue;
640 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000641 netdev_err(netdev, "unable to alloc pool\n");
Santiago Leon860f2422006-04-25 11:19:59 -0500642 adapter->rx_buff_pool[i].active = 0;
Denis Kirjanov88426f22010-10-20 04:21:13 +0000643 rc = -ENOMEM;
644 goto err_out;
Santiago Leon860f2422006-04-25 11:19:59 -0500645 }
646 }
647
Santiago Leonc43ced12010-09-03 18:29:14 +0000648 netdev_dbg(netdev, "registering irq 0x%x\n", netdev->irq);
Santiago Leonf148f612010-09-03 18:29:30 +0000649 rc = request_irq(netdev->irq, ibmveth_interrupt, 0, netdev->name,
650 netdev);
651 if (rc != 0) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000652 netdev_err(netdev, "unable to request irq 0x%x, rc %d\n",
653 netdev->irq, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 do {
David S. Miller88c51002011-10-07 13:38:43 -0400655 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
656 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Denis Kirjanov88426f22010-10-20 04:21:13 +0000658 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Robert Jennings1096d632008-07-24 04:34:52 +1000661 adapter->bounce_buffer =
662 kmalloc(netdev->mtu + IBMVETH_BUFF_OH, GFP_KERNEL);
663 if (!adapter->bounce_buffer) {
Denis Kirjanov88426f22010-10-20 04:21:13 +0000664 rc = -ENOMEM;
Denis Kirjanove0e8ab52010-10-20 04:21:51 +0000665 goto err_out_free_irq;
Robert Jennings1096d632008-07-24 04:34:52 +1000666 }
667 adapter->bounce_buffer_dma =
668 dma_map_single(&adapter->vdev->dev, adapter->bounce_buffer,
669 netdev->mtu + IBMVETH_BUFF_OH, DMA_BIDIRECTIONAL);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700670 if (dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000671 netdev_err(netdev, "unable to map bounce buffer\n");
Denis Kirjanov88426f22010-10-20 04:21:13 +0000672 rc = -ENOMEM;
Denis Kirjanove0e8ab52010-10-20 04:21:51 +0000673 goto err_out_free_irq;
Robert Jennings1096d632008-07-24 04:34:52 +1000674 }
675
Santiago Leonc43ced12010-09-03 18:29:14 +0000676 netdev_dbg(netdev, "initial replenish cycle\n");
David Howells7d12e782006-10-05 14:55:46 +0100677 ibmveth_interrupt(netdev->irq, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Santiago Leone2adbcb2005-10-26 10:47:08 -0600679 netif_start_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Santiago Leonc43ced12010-09-03 18:29:14 +0000681 netdev_dbg(netdev, "open complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 return 0;
Denis Kirjanov88426f22010-10-20 04:21:13 +0000684
Denis Kirjanove0e8ab52010-10-20 04:21:51 +0000685err_out_free_irq:
686 free_irq(netdev->irq, netdev);
Denis Kirjanov88426f22010-10-20 04:21:13 +0000687err_out:
688 ibmveth_cleanup(adapter);
689 napi_disable(&adapter->napi);
690 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
693static int ibmveth_close(struct net_device *netdev)
694{
Wang Chen4cf16532008-11-12 23:38:14 -0800695 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 long lpar_rc;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400697
Santiago Leonc43ced12010-09-03 18:29:14 +0000698 netdev_dbg(netdev, "close starting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700700 napi_disable(&adapter->napi);
701
Santiago Leon860f2422006-04-25 11:19:59 -0500702 if (!adapter->pool_config)
703 netif_stop_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Robert Jenningsee2e6112010-07-16 04:57:25 +0000705 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 do {
708 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200709 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Santiago Leonf148f612010-09-03 18:29:30 +0000711 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000712 netdev_err(netdev, "h_free_logical_lan failed with %lx, "
713 "continuing with close\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715
Robert Jenningsee2e6112010-07-16 04:57:25 +0000716 free_irq(netdev->irq, netdev);
717
Anton Blanchardcbd52282014-08-22 11:36:52 +1000718 ibmveth_update_rx_no_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 ibmveth_cleanup(adapter);
721
Santiago Leonc43ced12010-09-03 18:29:14 +0000722 netdev_dbg(netdev, "close complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 return 0;
725}
726
Santiago Leonf148f612010-09-03 18:29:30 +0000727static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
728{
729 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
730 SUPPORTED_FIBRE);
731 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
732 ADVERTISED_FIBRE);
David Decotigny70739492011-04-27 18:32:40 +0000733 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 cmd->duplex = DUPLEX_FULL;
735 cmd->port = PORT_FIBRE;
736 cmd->phy_address = 0;
737 cmd->transceiver = XCVR_INTERNAL;
738 cmd->autoneg = AUTONEG_ENABLE;
739 cmd->maxtxpkt = 0;
740 cmd->maxrxpkt = 1;
741 return 0;
742}
743
Santiago Leonf148f612010-09-03 18:29:30 +0000744static void netdev_get_drvinfo(struct net_device *dev,
745 struct ethtool_drvinfo *info)
746{
Jiri Pirko7826d432013-01-06 00:44:26 +0000747 strlcpy(info->driver, ibmveth_driver_name, sizeof(info->driver));
748 strlcpy(info->version, ibmveth_driver_version, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000751static netdev_features_t ibmveth_fix_features(struct net_device *dev,
752 netdev_features_t features)
Brian King5fc7e012007-08-17 09:16:31 -0500753{
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000754 /*
755 * Since the ibmveth firmware interface does not have the
756 * concept of separate tx/rx checksum offload enable, if rx
757 * checksum is disabled we also have to disable tx checksum
758 * offload. Once we disable rx checksum offload, we are no
759 * longer allowed to send tx buffers that are not properly
760 * checksummed.
761 */
Brian King5fc7e012007-08-17 09:16:31 -0500762
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000763 if (!(features & NETIF_F_RXCSUM))
Tom Herberta1882222015-12-14 11:19:43 -0800764 features &= ~NETIF_F_CSUM_MASK;
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000765
766 return features;
Brian King5fc7e012007-08-17 09:16:31 -0500767}
768
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000769static int ibmveth_set_csum_offload(struct net_device *dev, u32 data)
Brian King5fc7e012007-08-17 09:16:31 -0500770{
Wang Chen4cf16532008-11-12 23:38:14 -0800771 struct ibmveth_adapter *adapter = netdev_priv(dev);
Stephen Rothwellff5bfc32009-01-06 10:47:44 -0800772 unsigned long set_attr, clr_attr, ret_attr;
Santiago Leonab78df72010-09-03 18:28:52 +0000773 unsigned long set_attr6, clr_attr6;
David S. Miller8decf862011-09-22 03:23:13 -0400774 long ret, ret4, ret6;
Brian King5fc7e012007-08-17 09:16:31 -0500775 int rc1 = 0, rc2 = 0;
776 int restart = 0;
777
778 if (netif_running(dev)) {
779 restart = 1;
780 adapter->pool_config = 1;
781 ibmveth_close(dev);
782 adapter->pool_config = 0;
783 }
784
Brian King79ef4a42007-08-17 09:16:56 -0500785 set_attr = 0;
786 clr_attr = 0;
David S. Miller8decf862011-09-22 03:23:13 -0400787 set_attr6 = 0;
788 clr_attr6 = 0;
Brian King5fc7e012007-08-17 09:16:31 -0500789
Santiago Leonab78df72010-09-03 18:28:52 +0000790 if (data) {
Brian King79ef4a42007-08-17 09:16:56 -0500791 set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Santiago Leonab78df72010-09-03 18:28:52 +0000792 set_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
793 } else {
Brian King79ef4a42007-08-17 09:16:56 -0500794 clr_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Santiago Leonab78df72010-09-03 18:28:52 +0000795 clr_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
796 }
Brian King5fc7e012007-08-17 09:16:31 -0500797
Brian King79ef4a42007-08-17 09:16:56 -0500798 ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500799
Brian King79ef4a42007-08-17 09:16:56 -0500800 if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
801 !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
802 (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
David S. Miller8decf862011-09-22 03:23:13 -0400803 ret4 = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
Brian King79ef4a42007-08-17 09:16:56 -0500804 set_attr, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500805
David S. Miller8decf862011-09-22 03:23:13 -0400806 if (ret4 != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000807 netdev_err(dev, "unable to change IPv4 checksum "
808 "offload settings. %d rc=%ld\n",
David S. Miller8decf862011-09-22 03:23:13 -0400809 data, ret4);
Brian King5fc7e012007-08-17 09:16:31 -0500810
David S. Miller8decf862011-09-22 03:23:13 -0400811 h_illan_attributes(adapter->vdev->unit_address,
812 set_attr, clr_attr, &ret_attr);
813
814 if (data == 1)
815 dev->features &= ~NETIF_F_IP_CSUM;
816
Santiago Leonf148f612010-09-03 18:29:30 +0000817 } else {
Santiago Leonab78df72010-09-03 18:28:52 +0000818 adapter->fw_ipv4_csum_support = data;
Santiago Leonf148f612010-09-03 18:29:30 +0000819 }
Santiago Leonab78df72010-09-03 18:28:52 +0000820
821 ret6 = h_illan_attributes(adapter->vdev->unit_address,
822 clr_attr6, set_attr6, &ret_attr);
823
824 if (ret6 != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +0000825 netdev_err(dev, "unable to change IPv6 checksum "
826 "offload settings. %d rc=%ld\n",
David S. Miller8decf862011-09-22 03:23:13 -0400827 data, ret6);
Santiago Leonab78df72010-09-03 18:28:52 +0000828
David S. Miller8decf862011-09-22 03:23:13 -0400829 h_illan_attributes(adapter->vdev->unit_address,
830 set_attr6, clr_attr6, &ret_attr);
831
832 if (data == 1)
833 dev->features &= ~NETIF_F_IPV6_CSUM;
834
Santiago Leonab78df72010-09-03 18:28:52 +0000835 } else
836 adapter->fw_ipv6_csum_support = data;
837
David S. Miller8decf862011-09-22 03:23:13 -0400838 if (ret4 == H_SUCCESS || ret6 == H_SUCCESS)
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000839 adapter->rx_csum = data;
Santiago Leonab78df72010-09-03 18:28:52 +0000840 else
841 rc1 = -EIO;
Brian King5fc7e012007-08-17 09:16:31 -0500842 } else {
843 rc1 = -EIO;
Santiago Leon21c2dec2010-09-03 18:29:19 +0000844 netdev_err(dev, "unable to change checksum offload settings."
845 " %d rc=%ld ret_attr=%lx\n", data, ret,
846 ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500847 }
848
849 if (restart)
850 rc2 = ibmveth_open(dev);
851
852 return rc1 ? rc1 : rc2;
853}
854
Thomas Falcon07e6a972015-07-14 10:51:51 -0500855static int ibmveth_set_tso(struct net_device *dev, u32 data)
856{
857 struct ibmveth_adapter *adapter = netdev_priv(dev);
858 unsigned long set_attr, clr_attr, ret_attr;
859 long ret1, ret2;
860 int rc1 = 0, rc2 = 0;
861 int restart = 0;
862
863 if (netif_running(dev)) {
864 restart = 1;
865 adapter->pool_config = 1;
866 ibmveth_close(dev);
867 adapter->pool_config = 0;
868 }
869
870 set_attr = 0;
871 clr_attr = 0;
872
873 if (data)
874 set_attr = IBMVETH_ILLAN_LRG_SR_ENABLED;
875 else
876 clr_attr = IBMVETH_ILLAN_LRG_SR_ENABLED;
877
878 ret1 = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
879
880 if (ret1 == H_SUCCESS && (ret_attr & IBMVETH_ILLAN_LRG_SND_SUPPORT) &&
881 !old_large_send) {
882 ret2 = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
883 set_attr, &ret_attr);
884
885 if (ret2 != H_SUCCESS) {
886 netdev_err(dev, "unable to change tso settings. %d rc=%ld\n",
887 data, ret2);
888
889 h_illan_attributes(adapter->vdev->unit_address,
890 set_attr, clr_attr, &ret_attr);
891
892 if (data == 1)
893 dev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
894 rc1 = -EIO;
895
896 } else {
897 adapter->fw_large_send_support = data;
898 adapter->large_send = data;
899 }
900 } else {
901 /* Older firmware version of large send offload does not
902 * support tcp6/ipv6
903 */
904 if (data == 1) {
905 dev->features &= ~NETIF_F_TSO6;
906 netdev_info(dev, "TSO feature requires all partitions to have updated driver");
907 }
908 adapter->large_send = data;
909 }
910
911 if (restart)
912 rc2 = ibmveth_open(dev);
913
914 return rc1 ? rc1 : rc2;
915}
916
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000917static int ibmveth_set_features(struct net_device *dev,
918 netdev_features_t features)
Brian King5fc7e012007-08-17 09:16:31 -0500919{
Wang Chen4cf16532008-11-12 23:38:14 -0800920 struct ibmveth_adapter *adapter = netdev_priv(dev);
Michał Mirosławb9367bf2011-04-19 02:14:25 +0000921 int rx_csum = !!(features & NETIF_F_RXCSUM);
Thomas Falcon07e6a972015-07-14 10:51:51 -0500922 int large_send = !!(features & (NETIF_F_TSO | NETIF_F_TSO6));
923 int rc1 = 0, rc2 = 0;
Thomas Falcon8641dd82015-04-29 16:25:45 -0500924
Thomas Falcon07e6a972015-07-14 10:51:51 -0500925 if (rx_csum != adapter->rx_csum) {
926 rc1 = ibmveth_set_csum_offload(dev, rx_csum);
927 if (rc1 && !adapter->rx_csum)
928 dev->features =
Tom Herberta1882222015-12-14 11:19:43 -0800929 features & ~(NETIF_F_CSUM_MASK |
930 NETIF_F_RXCSUM);
Thomas Falcon07e6a972015-07-14 10:51:51 -0500931 }
Brian King5fc7e012007-08-17 09:16:31 -0500932
Thomas Falcon07e6a972015-07-14 10:51:51 -0500933 if (large_send != adapter->large_send) {
934 rc2 = ibmveth_set_tso(dev, large_send);
935 if (rc2 && !adapter->large_send)
936 dev->features =
937 features & ~(NETIF_F_TSO | NETIF_F_TSO6);
938 }
Brian King5fc7e012007-08-17 09:16:31 -0500939
Thomas Falcon07e6a972015-07-14 10:51:51 -0500940 return rc1 ? rc1 : rc2;
Brian King5fc7e012007-08-17 09:16:31 -0500941}
942
Brian Kingddbb4de2007-08-17 09:16:43 -0500943static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
944{
945 int i;
946
947 if (stringset != ETH_SS_STATS)
948 return;
949
950 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
951 memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
952}
953
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700954static int ibmveth_get_sset_count(struct net_device *dev, int sset)
Brian Kingddbb4de2007-08-17 09:16:43 -0500955{
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700956 switch (sset) {
957 case ETH_SS_STATS:
958 return ARRAY_SIZE(ibmveth_stats);
959 default:
960 return -EOPNOTSUPP;
961 }
Brian Kingddbb4de2007-08-17 09:16:43 -0500962}
963
964static void ibmveth_get_ethtool_stats(struct net_device *dev,
965 struct ethtool_stats *stats, u64 *data)
966{
967 int i;
Wang Chen4cf16532008-11-12 23:38:14 -0800968 struct ibmveth_adapter *adapter = netdev_priv(dev);
Brian Kingddbb4de2007-08-17 09:16:43 -0500969
970 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
971 data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
972}
973
Jeff Garzik7282d492006-09-13 14:30:00 -0400974static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 .get_drvinfo = netdev_get_drvinfo,
976 .get_settings = netdev_get_settings,
Ben Hutchingsed4ba4b2010-12-09 12:10:25 +0000977 .get_link = ethtool_op_get_link,
Brian Kingddbb4de2007-08-17 09:16:43 -0500978 .get_strings = ibmveth_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700979 .get_sset_count = ibmveth_get_sset_count,
Brian Kingddbb4de2007-08-17 09:16:43 -0500980 .get_ethtool_stats = ibmveth_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981};
982
983static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
984{
985 return -EOPNOTSUPP;
986}
987
988#define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
989
Santiago Leon6e8ab302010-09-03 18:28:36 +0000990static int ibmveth_send(struct ibmveth_adapter *adapter,
Thomas Falcon07e6a972015-07-14 10:51:51 -0500991 union ibmveth_buf_desc *descs, unsigned long mss)
Santiago Leon6e8ab302010-09-03 18:28:36 +0000992{
993 unsigned long correlator;
994 unsigned int retry_count;
995 unsigned long ret;
996
997 /*
998 * The retry count sets a maximum for the number of broadcast and
999 * multicast destinations within the system.
1000 */
1001 retry_count = 1024;
1002 correlator = 0;
1003 do {
1004 ret = h_send_logical_lan(adapter->vdev->unit_address,
1005 descs[0].desc, descs[1].desc,
1006 descs[2].desc, descs[3].desc,
1007 descs[4].desc, descs[5].desc,
Thomas Falcon07e6a972015-07-14 10:51:51 -05001008 correlator, &correlator, mss,
1009 adapter->fw_large_send_support);
Santiago Leon6e8ab302010-09-03 18:28:36 +00001010 } while ((ret == H_BUSY) && (retry_count--));
1011
1012 if (ret != H_SUCCESS && ret != H_DROPPED) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001013 netdev_err(adapter->netdev, "tx: h_send_logical_lan failed "
1014 "with rc=%ld\n", ret);
Santiago Leon6e8ab302010-09-03 18:28:36 +00001015 return 1;
1016 }
1017
1018 return 0;
1019}
1020
Stephen Hemminger613573252009-08-31 19:50:58 +00001021static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb,
1022 struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Wang Chen4cf16532008-11-12 23:38:14 -08001024 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Santiago Leon6e8ab302010-09-03 18:28:36 +00001025 unsigned int desc_flags;
1026 union ibmveth_buf_desc descs[6];
1027 int last, i;
1028 int force_bounce = 0;
David S. Miller8decf862011-09-22 03:23:13 -04001029 dma_addr_t dma_addr;
Thomas Falcon07e6a972015-07-14 10:51:51 -05001030 unsigned long mss = 0;
Santiago Leon60296d92005-10-26 10:47:16 -06001031
Santiago Leon6e8ab302010-09-03 18:28:36 +00001032 /*
1033 * veth handles a maximum of 6 segments including the header, so
1034 * we have to linearize the skb if there are more than this.
1035 */
1036 if (skb_shinfo(skb)->nr_frags > 5 && __skb_linearize(skb)) {
1037 netdev->stats.tx_dropped++;
Brian Kingf4ff2872007-09-15 13:36:07 -07001038 goto out;
1039 }
1040
Santiago Leon6e8ab302010-09-03 18:28:36 +00001041 /* veth can't checksum offload UDP */
1042 if (skb->ip_summed == CHECKSUM_PARTIAL &&
Santiago Leonab78df72010-09-03 18:28:52 +00001043 ((skb->protocol == htons(ETH_P_IP) &&
1044 ip_hdr(skb)->protocol != IPPROTO_TCP) ||
1045 (skb->protocol == htons(ETH_P_IPV6) &&
1046 ipv6_hdr(skb)->nexthdr != IPPROTO_TCP)) &&
1047 skb_checksum_help(skb)) {
1048
Santiago Leon21c2dec2010-09-03 18:29:19 +00001049 netdev_err(netdev, "tx: failed to checksum packet\n");
Santiago Leon6e8ab302010-09-03 18:28:36 +00001050 netdev->stats.tx_dropped++;
1051 goto out;
1052 }
Brian Kingf4ff2872007-09-15 13:36:07 -07001053
Santiago Leon6e8ab302010-09-03 18:28:36 +00001054 desc_flags = IBMVETH_BUF_VALID;
1055
Thomas Falcon07e6a972015-07-14 10:51:51 -05001056 if (skb_is_gso(skb) && adapter->fw_large_send_support)
1057 desc_flags |= IBMVETH_BUF_LRG_SND;
1058
Santiago Leon6e8ab302010-09-03 18:28:36 +00001059 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1060 unsigned char *buf = skb_transport_header(skb) +
1061 skb->csum_offset;
1062
1063 desc_flags |= (IBMVETH_BUF_NO_CSUM | IBMVETH_BUF_CSUM_GOOD);
Brian Kingf4ff2872007-09-15 13:36:07 -07001064
1065 /* Need to zero out the checksum */
1066 buf[0] = 0;
1067 buf[1] = 0;
1068 }
1069
Santiago Leon6e8ab302010-09-03 18:28:36 +00001070retry_bounce:
1071 memset(descs, 0, sizeof(descs));
Santiago Leonc08cc3c2010-09-03 18:28:20 +00001072
Santiago Leon6e8ab302010-09-03 18:28:36 +00001073 /*
1074 * If a linear packet is below the rx threshold then
1075 * copy it into the static bounce buffer. This avoids the
1076 * cost of a TCE insert and remove.
1077 */
1078 if (force_bounce || (!skb_is_nonlinear(skb) &&
1079 (skb->len < tx_copybreak))) {
Robert Jennings1096d632008-07-24 04:34:52 +10001080 skb_copy_from_linear_data(skb, adapter->bounce_buffer,
1081 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Santiago Leon6e8ab302010-09-03 18:28:36 +00001083 descs[0].fields.flags_len = desc_flags | skb->len;
1084 descs[0].fields.address = adapter->bounce_buffer_dma;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001085
Thomas Falcon07e6a972015-07-14 10:51:51 -05001086 if (ibmveth_send(adapter, descs, 0)) {
Santiago Leon6e8ab302010-09-03 18:28:36 +00001087 adapter->tx_send_failed++;
1088 netdev->stats.tx_dropped++;
1089 } else {
1090 netdev->stats.tx_packets++;
1091 netdev->stats.tx_bytes += skb->len;
1092 }
1093
1094 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096
Santiago Leon6e8ab302010-09-03 18:28:36 +00001097 /* Map the header */
David S. Miller8decf862011-09-22 03:23:13 -04001098 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
1099 skb_headlen(skb), DMA_TO_DEVICE);
1100 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
Santiago Leon6e8ab302010-09-03 18:28:36 +00001101 goto map_failed;
1102
1103 descs[0].fields.flags_len = desc_flags | skb_headlen(skb);
David S. Miller8decf862011-09-22 03:23:13 -04001104 descs[0].fields.address = dma_addr;
Santiago Leon6e8ab302010-09-03 18:28:36 +00001105
1106 /* Map the frags */
1107 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001108 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Santiago Leon6e8ab302010-09-03 18:28:36 +00001109
Ian Campbell8838a532011-08-31 00:46:53 +00001110 dma_addr = skb_frag_dma_map(&adapter->vdev->dev, frag, 0,
Eric Dumazet9e903e02011-10-18 21:00:24 +00001111 skb_frag_size(frag), DMA_TO_DEVICE);
Santiago Leon6e8ab302010-09-03 18:28:36 +00001112
1113 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
1114 goto map_failed_frags;
1115
Eric Dumazet9e903e02011-10-18 21:00:24 +00001116 descs[i+1].fields.flags_len = desc_flags | skb_frag_size(frag);
Santiago Leon6e8ab302010-09-03 18:28:36 +00001117 descs[i+1].fields.address = dma_addr;
1118 }
1119
Thomas Falcon07e6a972015-07-14 10:51:51 -05001120 if (skb_is_gso(skb)) {
1121 if (adapter->fw_large_send_support) {
1122 mss = (unsigned long)skb_shinfo(skb)->gso_size;
1123 adapter->tx_large_packets++;
1124 } else if (!skb_is_gso_v6(skb)) {
1125 /* Put -1 in the IP checksum to tell phyp it
1126 * is a largesend packet. Put the mss in
1127 * the TCP checksum.
1128 */
1129 ip_hdr(skb)->check = 0xffff;
1130 tcp_hdr(skb)->check =
1131 cpu_to_be16(skb_shinfo(skb)->gso_size);
1132 adapter->tx_large_packets++;
1133 }
Thomas Falcon8641dd82015-04-29 16:25:45 -05001134 }
1135
Thomas Falcon07e6a972015-07-14 10:51:51 -05001136 if (ibmveth_send(adapter, descs, mss)) {
Santiago Leon6e8ab302010-09-03 18:28:36 +00001137 adapter->tx_send_failed++;
1138 netdev->stats.tx_dropped++;
1139 } else {
1140 netdev->stats.tx_packets++;
1141 netdev->stats.tx_bytes += skb->len;
1142 }
1143
David S. Miller8decf862011-09-22 03:23:13 -04001144 dma_unmap_single(&adapter->vdev->dev,
1145 descs[0].fields.address,
1146 descs[0].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1147 DMA_TO_DEVICE);
1148
1149 for (i = 1; i < skb_shinfo(skb)->nr_frags + 1; i++)
Santiago Leon6e8ab302010-09-03 18:28:36 +00001150 dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
1151 descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1152 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Santiago Leone8cb7eb2010-09-03 18:28:15 +00001154out:
Eric W. Biederman26faa9d2014-03-15 17:29:34 -07001155 dev_consume_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001156 return NETDEV_TX_OK;
Santiago Leon6e8ab302010-09-03 18:28:36 +00001157
1158map_failed_frags:
1159 last = i+1;
1160 for (i = 0; i < last; i++)
1161 dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
1162 descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1163 DMA_TO_DEVICE);
1164
1165map_failed:
1166 if (!firmware_has_feature(FW_FEATURE_CMO))
Santiago Leon21c2dec2010-09-03 18:29:19 +00001167 netdev_err(netdev, "tx: unable to map xmit buffer\n");
Santiago Leon6e8ab302010-09-03 18:28:36 +00001168 adapter->tx_map_failed++;
Thomas Falcon2c42bf42016-03-03 15:22:36 -06001169 if (skb_linearize(skb)) {
1170 netdev->stats.tx_dropped++;
1171 goto out;
1172 }
Santiago Leon6e8ab302010-09-03 18:28:36 +00001173 force_bounce = 1;
1174 goto retry_bounce;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001177static int ibmveth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Santiago Leonf148f612010-09-03 18:29:30 +00001179 struct ibmveth_adapter *adapter =
1180 container_of(napi, struct ibmveth_adapter, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001181 struct net_device *netdev = adapter->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 int frames_processed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 unsigned long lpar_rc;
Thomas Falcon9c7e8bc2015-04-29 16:25:47 -05001184 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Santiago Leonf148f612010-09-03 18:29:30 +00001186restart_poll:
Eric W. Biedermancb013ea2014-03-14 18:03:50 -07001187 while (frames_processed < budget) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001188 if (!ibmveth_rxq_pending_buffer(adapter))
1189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Anton Blanchardf89e49e2010-09-06 18:21:41 -07001191 smp_rmb();
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001192 if (!ibmveth_rxq_buffer_valid(adapter)) {
1193 wmb(); /* suggested by larson1 */
1194 adapter->rx_invalid_buffer++;
Santiago Leonc43ced12010-09-03 18:29:14 +00001195 netdev_dbg(netdev, "recycling invalid buffer\n");
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001196 ibmveth_rxq_recycle_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 } else {
Santiago Leon8d86c612010-09-03 18:28:25 +00001198 struct sk_buff *skb, *new_skb;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001199 int length = ibmveth_rxq_frame_length(adapter);
1200 int offset = ibmveth_rxq_frame_offset(adapter);
Brian Kingf4ff2872007-09-15 13:36:07 -07001201 int csum_good = ibmveth_rxq_csum_good(adapter);
1202
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001203 skb = ibmveth_rxq_get_buffer(adapter);
1204
Santiago Leon8d86c612010-09-03 18:28:25 +00001205 new_skb = NULL;
1206 if (length < rx_copybreak)
1207 new_skb = netdev_alloc_skb(netdev, length);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001208
Santiago Leon8d86c612010-09-03 18:28:25 +00001209 if (new_skb) {
1210 skb_copy_to_linear_data(new_skb,
1211 skb->data + offset,
1212 length);
Santiago Leon0c26b672010-09-03 18:28:41 +00001213 if (rx_flush)
1214 ibmveth_flush_buffer(skb->data,
1215 length + offset);
David S. Miller8decf862011-09-22 03:23:13 -04001216 if (!ibmveth_rxq_recycle_buffer(adapter))
1217 kfree_skb(skb);
Santiago Leon8d86c612010-09-03 18:28:25 +00001218 skb = new_skb;
Santiago Leon8d86c612010-09-03 18:28:25 +00001219 } else {
1220 ibmveth_rxq_harvest_buffer(adapter);
1221 skb_reserve(skb, offset);
1222 }
1223
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001224 skb_put(skb, length);
1225 skb->protocol = eth_type_trans(skb, netdev);
1226
Thomas Falcon9c7e8bc2015-04-29 16:25:47 -05001227 if (csum_good) {
Santiago Leon8d86c612010-09-03 18:28:25 +00001228 skb->ip_summed = CHECKSUM_UNNECESSARY;
Thomas Falcon9c7e8bc2015-04-29 16:25:47 -05001229 if (be16_to_cpu(skb->protocol) == ETH_P_IP) {
1230 iph = (struct iphdr *)skb->data;
1231
1232 /* If the IP checksum is not offloaded and if the packet
1233 * is large send, the checksum must be rebuilt.
1234 */
1235 if (iph->check == 0xffff) {
1236 iph->check = 0;
1237 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1238 adapter->rx_large_packets++;
1239 }
1240 }
1241 }
Santiago Leon8d86c612010-09-03 18:28:25 +00001242
Thomas Falcon92ec8272015-04-29 16:25:46 -05001243 napi_gro_receive(napi, skb); /* send it up */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001244
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001245 netdev->stats.rx_packets++;
1246 netdev->stats.rx_bytes += length;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001247 frames_processed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
Eric W. Biedermancb013ea2014-03-14 18:03:50 -07001249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Santiago Leone2adbcb2005-10-26 10:47:08 -06001251 ibmveth_replenish_task(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001253 if (frames_processed < budget) {
Yongbae Park4736edc2015-03-10 11:15:39 +09001254 napi_complete(napi);
1255
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001256 /* We think we are done - reenable interrupts,
1257 * then check once more to make sure we are done.
1258 */
1259 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1260 VIO_IRQ_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Santiago Leon64859112010-09-03 18:29:41 +00001262 BUG_ON(lpar_rc != H_SUCCESS);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001263
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001264 if (ibmveth_rxq_pending_buffer(adapter) &&
Ben Hutchings288379f2009-01-19 16:43:59 -08001265 napi_reschedule(napi)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001266 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1267 VIO_IRQ_DISABLE);
1268 goto restart_poll;
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001272 return frames_processed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
David Howells7d12e782006-10-05 14:55:46 +01001275static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001276{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 struct net_device *netdev = dev_instance;
Wang Chen4cf16532008-11-12 23:38:14 -08001278 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 unsigned long lpar_rc;
1280
Ben Hutchings288379f2009-01-19 16:43:59 -08001281 if (napi_schedule_prep(&adapter->napi)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001282 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1283 VIO_IRQ_DISABLE);
Santiago Leon64859112010-09-03 18:29:41 +00001284 BUG_ON(lpar_rc != H_SUCCESS);
Ben Hutchings288379f2009-01-19 16:43:59 -08001285 __napi_schedule(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287 return IRQ_HANDLED;
1288}
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290static void ibmveth_set_multicast_list(struct net_device *netdev)
1291{
Wang Chen4cf16532008-11-12 23:38:14 -08001292 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 unsigned long lpar_rc;
1294
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001295 if ((netdev->flags & IFF_PROMISC) ||
1296 (netdev_mc_count(netdev) > adapter->mcastFilterSize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1298 IbmVethMcastEnableRecv |
1299 IbmVethMcastDisableFiltering,
1300 0);
Santiago Leonf148f612010-09-03 18:29:30 +00001301 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001302 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1303 "entering promisc mode\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001306 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 /* clear the filter table & disable filtering */
1308 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1309 IbmVethMcastEnableRecv |
1310 IbmVethMcastDisableFiltering |
1311 IbmVethMcastClearFilterTable,
1312 0);
Santiago Leonf148f612010-09-03 18:29:30 +00001313 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001314 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1315 "attempting to clear filter table\n",
1316 lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318 /* add the addresses to the filter table */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001319 netdev_for_each_mc_addr(ha, netdev) {
Santiago Leonf148f612010-09-03 18:29:30 +00001320 /* add the multicast address to the filter table */
Anton Blanchardd746ca92014-03-05 14:51:37 +11001321 u64 mcast_addr;
1322 mcast_addr = ibmveth_encode_mac_addr(ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1324 IbmVethMcastAddFilter,
1325 mcast_addr);
Santiago Leonf148f612010-09-03 18:29:30 +00001326 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001327 netdev_err(netdev, "h_multicast_ctrl rc=%ld "
1328 "when adding an entry to the filter "
1329 "table\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /* re-enable filtering */
1334 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1335 IbmVethMcastEnableFiltering,
1336 0);
Santiago Leonf148f612010-09-03 18:29:30 +00001337 if (lpar_rc != H_SUCCESS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001338 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1339 "enabling filtering\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 }
1342}
1343
1344static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
1345{
Wang Chen4cf16532008-11-12 23:38:14 -08001346 struct ibmveth_adapter *adapter = netdev_priv(dev);
Robert Jennings1096d632008-07-24 04:34:52 +10001347 struct vio_dev *viodev = adapter->vdev;
Santiago Leon860f2422006-04-25 11:19:59 -05001348 int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
Robert Jennings0645bab2010-08-17 09:15:45 +00001349 int i, rc;
1350 int need_restart = 0;
Santiago Leonb6d35182005-10-26 10:47:01 -06001351
Santiago Leon517e80e2010-09-03 18:29:25 +00001352 if (new_mtu < IBMVETH_MIN_MTU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -EINVAL;
Santiago Leonb6d35182005-10-26 10:47:01 -06001354
Santiago Leon517e80e2010-09-03 18:29:25 +00001355 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
David Gibson4fce1482015-04-23 14:43:05 +10001356 if (new_mtu_oh <= adapter->rx_buff_pool[i].buff_size)
Brian Kingce6eea52007-06-08 14:05:17 -05001357 break;
1358
Santiago Leon517e80e2010-09-03 18:29:25 +00001359 if (i == IBMVETH_NUM_BUFF_POOLS)
Brian Kingce6eea52007-06-08 14:05:17 -05001360 return -EINVAL;
1361
Santiago Leonea866e62008-07-24 04:34:23 +10001362 /* Deactivate all the buffer pools so that the next loop can activate
1363 only the buffer pools necessary to hold the new MTU */
Robert Jennings0645bab2010-08-17 09:15:45 +00001364 if (netif_running(adapter->netdev)) {
1365 need_restart = 1;
1366 adapter->pool_config = 1;
1367 ibmveth_close(adapter->netdev);
1368 adapter->pool_config = 0;
1369 }
Brian Kingce6eea52007-06-08 14:05:17 -05001370
Santiago Leonea866e62008-07-24 04:34:23 +10001371 /* Look for an active buffer pool that can hold the new MTU */
Santiago Leonf148f612010-09-03 18:29:30 +00001372 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Santiago Leonea866e62008-07-24 04:34:23 +10001373 adapter->rx_buff_pool[i].active = 1;
1374
David Gibson4fce1482015-04-23 14:43:05 +10001375 if (new_mtu_oh <= adapter->rx_buff_pool[i].buff_size) {
Robert Jennings1096d632008-07-24 04:34:52 +10001376 dev->mtu = new_mtu;
1377 vio_cmo_set_dev_desired(viodev,
1378 ibmveth_get_desired_dma
1379 (viodev));
Robert Jennings0645bab2010-08-17 09:15:45 +00001380 if (need_restart) {
1381 return ibmveth_open(adapter->netdev);
1382 }
Santiago Leon860f2422006-04-25 11:19:59 -05001383 return 0;
Santiago Leonb6d35182005-10-26 10:47:01 -06001384 }
Santiago Leonb6d35182005-10-26 10:47:01 -06001385 }
Robert Jennings0645bab2010-08-17 09:15:45 +00001386
1387 if (need_restart && (rc = ibmveth_open(adapter->netdev)))
1388 return rc;
1389
Santiago Leon860f2422006-04-25 11:19:59 -05001390 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
Santiago Leon6b422372006-10-03 12:24:28 -05001393#ifdef CONFIG_NET_POLL_CONTROLLER
1394static void ibmveth_poll_controller(struct net_device *dev)
1395{
Wang Chen4cf16532008-11-12 23:38:14 -08001396 ibmveth_replenish_task(netdev_priv(dev));
Andrew Morton5f771132006-10-10 14:33:30 -07001397 ibmveth_interrupt(dev->irq, dev);
Santiago Leon6b422372006-10-03 12:24:28 -05001398}
1399#endif
1400
Robert Jennings1096d632008-07-24 04:34:52 +10001401/**
1402 * ibmveth_get_desired_dma - Calculate IO memory desired by the driver
1403 *
1404 * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
1405 *
1406 * Return value:
1407 * Number of bytes of IO data the driver will need to perform well.
1408 */
1409static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev)
1410{
1411 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
1412 struct ibmveth_adapter *adapter;
Alistair Poppled0847752013-12-09 18:17:03 +11001413 struct iommu_table *tbl;
Robert Jennings1096d632008-07-24 04:34:52 +10001414 unsigned long ret;
1415 int i;
1416 int rxqentries = 1;
1417
Alistair Poppled0847752013-12-09 18:17:03 +11001418 tbl = get_iommu_table_base(&vdev->dev);
1419
Robert Jennings1096d632008-07-24 04:34:52 +10001420 /* netdev inits at probe time along with the structures we need below*/
1421 if (netdev == NULL)
Alistair Poppled0847752013-12-09 18:17:03 +11001422 return IOMMU_PAGE_ALIGN(IBMVETH_IO_ENTITLEMENT_DEFAULT, tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001423
1424 adapter = netdev_priv(netdev);
1425
1426 ret = IBMVETH_BUFF_LIST_SIZE + IBMVETH_FILT_LIST_SIZE;
Alistair Poppled0847752013-12-09 18:17:03 +11001427 ret += IOMMU_PAGE_ALIGN(netdev->mtu, tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001428
Santiago Leon517e80e2010-09-03 18:29:25 +00001429 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Robert Jennings1096d632008-07-24 04:34:52 +10001430 /* add the size of the active receive buffers */
1431 if (adapter->rx_buff_pool[i].active)
1432 ret +=
1433 adapter->rx_buff_pool[i].size *
1434 IOMMU_PAGE_ALIGN(adapter->rx_buff_pool[i].
Alistair Poppled0847752013-12-09 18:17:03 +11001435 buff_size, tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001436 rxqentries += adapter->rx_buff_pool[i].size;
1437 }
1438 /* add the size of the receive queue entries */
Alistair Poppled0847752013-12-09 18:17:03 +11001439 ret += IOMMU_PAGE_ALIGN(
1440 rxqentries * sizeof(struct ibmveth_rx_q_entry), tbl);
Robert Jennings1096d632008-07-24 04:34:52 +10001441
1442 return ret;
1443}
1444
Thomas Falconc77c7612015-03-02 11:56:12 -06001445static int ibmveth_set_mac_addr(struct net_device *dev, void *p)
1446{
1447 struct ibmveth_adapter *adapter = netdev_priv(dev);
1448 struct sockaddr *addr = p;
1449 u64 mac_address;
1450 int rc;
1451
1452 if (!is_valid_ether_addr(addr->sa_data))
1453 return -EADDRNOTAVAIL;
1454
1455 mac_address = ibmveth_encode_mac_addr(addr->sa_data);
1456 rc = h_change_logical_lan_mac(adapter->vdev->unit_address, mac_address);
1457 if (rc) {
1458 netdev_err(adapter->netdev, "h_change_logical_lan_mac failed with rc=%d\n", rc);
1459 return rc;
1460 }
1461
1462 ether_addr_copy(dev->dev_addr, addr->sa_data);
1463
1464 return 0;
1465}
1466
Alexander Beregalove186d172009-04-15 12:52:39 +00001467static const struct net_device_ops ibmveth_netdev_ops = {
1468 .ndo_open = ibmveth_open,
1469 .ndo_stop = ibmveth_close,
1470 .ndo_start_xmit = ibmveth_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001471 .ndo_set_rx_mode = ibmveth_set_multicast_list,
Alexander Beregalove186d172009-04-15 12:52:39 +00001472 .ndo_do_ioctl = ibmveth_ioctl,
1473 .ndo_change_mtu = ibmveth_change_mtu,
Michał Mirosławb9367bf2011-04-19 02:14:25 +00001474 .ndo_fix_features = ibmveth_fix_features,
1475 .ndo_set_features = ibmveth_set_features,
Alexander Beregalove186d172009-04-15 12:52:39 +00001476 .ndo_validate_addr = eth_validate_addr,
Thomas Falconc77c7612015-03-02 11:56:12 -06001477 .ndo_set_mac_address = ibmveth_set_mac_addr,
Alexander Beregalove186d172009-04-15 12:52:39 +00001478#ifdef CONFIG_NET_POLL_CONTROLLER
1479 .ndo_poll_controller = ibmveth_poll_controller,
1480#endif
1481};
1482
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001483static int ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Benjamin Herrenschmidt13f85202013-05-03 17:19:01 +00001485 int rc, i, mac_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 struct net_device *netdev;
Mariusz Kozlowski9dc83af2007-08-06 23:44:03 +02001487 struct ibmveth_adapter *adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 unsigned char *mac_addr_p;
1489 unsigned int *mcastFilterSize_p;
Thomas Falcon07e6a972015-07-14 10:51:51 -05001490 long ret;
1491 unsigned long ret_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Santiago Leonc43ced12010-09-03 18:29:14 +00001493 dev_dbg(&dev->dev, "entering ibmveth_probe for UA 0x%x\n",
1494 dev->unit_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Santiago Leonf148f612010-09-03 18:29:30 +00001496 mac_addr_p = (unsigned char *)vio_get_attribute(dev, VETH_MAC_ADDR,
Benjamin Herrenschmidt13f85202013-05-03 17:19:01 +00001497 &mac_len);
Santiago Leonf148f612010-09-03 18:29:30 +00001498 if (!mac_addr_p) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001499 dev_err(&dev->dev, "Can't find VETH_MAC_ADDR attribute\n");
Santiago Leonbe35ae92010-09-03 18:29:36 +00001500 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
Benjamin Herrenschmidt13f85202013-05-03 17:19:01 +00001502 /* Workaround for old/broken pHyp */
1503 if (mac_len == 8)
1504 mac_addr_p += 2;
1505 else if (mac_len != 6) {
1506 dev_err(&dev->dev, "VETH_MAC_ADDR attribute wrong len %d\n",
1507 mac_len);
1508 return -EINVAL;
1509 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001510
Santiago Leonf148f612010-09-03 18:29:30 +00001511 mcastFilterSize_p = (unsigned int *)vio_get_attribute(dev,
Michael Ellerman493a6842007-04-17 13:12:55 +10001512 VETH_MCAST_FILTER_SIZE, NULL);
Santiago Leonf148f612010-09-03 18:29:30 +00001513 if (!mcastFilterSize_p) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001514 dev_err(&dev->dev, "Can't find VETH_MCAST_FILTER_SIZE "
1515 "attribute\n");
Santiago Leonbe35ae92010-09-03 18:29:36 +00001516 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
1520
Santiago Leonf148f612010-09-03 18:29:30 +00001521 if (!netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return -ENOMEM;
1523
Wang Chen4cf16532008-11-12 23:38:14 -08001524 adapter = netdev_priv(netdev);
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07001525 dev_set_drvdata(&dev->dev, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 adapter->vdev = dev;
1528 adapter->netdev = netdev;
Santiago Leonf148f612010-09-03 18:29:30 +00001529 adapter->mcastFilterSize = *mcastFilterSize_p;
Santiago Leon860f2422006-04-25 11:19:59 -05001530 adapter->pool_config = 0;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001531
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001532 netif_napi_add(netdev, &adapter->napi, ibmveth_poll, 16);
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 netdev->irq = dev->irq;
Alexander Beregalove186d172009-04-15 12:52:39 +00001535 netdev->netdev_ops = &ibmveth_netdev_ops;
1536 netdev->ethtool_ops = &netdev_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 SET_NETDEV_DEV(netdev, &dev->dev);
Michał Mirosławb9367bf2011-04-19 02:14:25 +00001538 netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
1539 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
Thomas Falcon07e6a972015-07-14 10:51:51 -05001540
Michał Mirosławb9367bf2011-04-19 02:14:25 +00001541 netdev->features |= netdev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Thomas Falcon07e6a972015-07-14 10:51:51 -05001543 ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
1544
1545 /* If running older firmware, TSO should not be enabled by default */
1546 if (ret == H_SUCCESS && (ret_attr & IBMVETH_ILLAN_LRG_SND_SUPPORT) &&
1547 !old_large_send) {
1548 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1549 netdev->features |= netdev->hw_features;
1550 } else {
1551 netdev->hw_features |= NETIF_F_TSO;
1552 }
Thomas Falcon8641dd82015-04-29 16:25:45 -05001553
Anton Blanchardd746ca92014-03-05 14:51:37 +11001554 memcpy(netdev->dev_addr, mac_addr_p, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Thomas Falconcd7c7ec2015-04-29 16:25:44 -05001556 if (firmware_has_feature(FW_FEATURE_CMO))
1557 memcpy(pool_count, pool_count_cmo, sizeof(pool_count));
1558
Santiago Leonf148f612010-09-03 18:29:30 +00001559 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Santiago Leon860f2422006-04-25 11:19:59 -05001560 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
Greg Kroah-Hartman8dde2a92007-12-17 15:54:39 -04001561 int error;
1562
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001563 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1564 pool_count[i], pool_size[i],
Santiago Leon860f2422006-04-25 11:19:59 -05001565 pool_active[i]);
Greg Kroah-Hartman8dde2a92007-12-17 15:54:39 -04001566 error = kobject_init_and_add(kobj, &ktype_veth_pool,
1567 &dev->dev.kobj, "pool%d", i);
1568 if (!error)
1569 kobject_uevent(kobj, KOBJ_ADD);
Santiago Leon860f2422006-04-25 11:19:59 -05001570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Santiago Leonc43ced12010-09-03 18:29:14 +00001572 netdev_dbg(netdev, "adapter @ 0x%p\n", adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 adapter->buffer_list_dma = DMA_ERROR_CODE;
1575 adapter->filter_list_dma = DMA_ERROR_CODE;
1576 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1577
Santiago Leonc43ced12010-09-03 18:29:14 +00001578 netdev_dbg(netdev, "registering netdev...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Michał Mirosławb801a4e2011-04-28 11:59:15 +10001580 ibmveth_set_features(netdev, netdev->features);
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 rc = register_netdev(netdev);
1583
Santiago Leonf148f612010-09-03 18:29:30 +00001584 if (rc) {
Santiago Leonc43ced12010-09-03 18:29:14 +00001585 netdev_dbg(netdev, "failed to register netdev rc=%d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 free_netdev(netdev);
1587 return rc;
1588 }
1589
Santiago Leonc43ced12010-09-03 18:29:14 +00001590 netdev_dbg(netdev, "registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return 0;
1593}
1594
Bill Pembertone11787a2012-12-03 09:23:12 -05001595static int ibmveth_remove(struct vio_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07001597 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Wang Chen4cf16532008-11-12 23:38:14 -08001598 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001599 int i;
1600
Santiago Leonf148f612010-09-03 18:29:30 +00001601 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001602 kobject_put(&adapter->rx_buff_pool[i].kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 unregister_netdev(netdev);
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 free_netdev(netdev);
Robert Jennings1096d632008-07-24 04:34:52 +10001607 dev_set_drvdata(&dev->dev, NULL);
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return 0;
1610}
1611
Santiago Leon860f2422006-04-25 11:19:59 -05001612static struct attribute veth_active_attr;
1613static struct attribute veth_num_attr;
1614static struct attribute veth_size_attr;
1615
Santiago Leonf148f612010-09-03 18:29:30 +00001616static ssize_t veth_pool_show(struct kobject *kobj,
1617 struct attribute *attr, char *buf)
Santiago Leon860f2422006-04-25 11:19:59 -05001618{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001619 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001620 struct ibmveth_buff_pool,
1621 kobj);
1622
1623 if (attr == &veth_active_attr)
1624 return sprintf(buf, "%d\n", pool->active);
1625 else if (attr == &veth_num_attr)
1626 return sprintf(buf, "%d\n", pool->size);
1627 else if (attr == &veth_size_attr)
1628 return sprintf(buf, "%d\n", pool->buff_size);
1629 return 0;
1630}
1631
Santiago Leonf148f612010-09-03 18:29:30 +00001632static ssize_t veth_pool_store(struct kobject *kobj, struct attribute *attr,
1633 const char *buf, size_t count)
Santiago Leon860f2422006-04-25 11:19:59 -05001634{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001635 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001636 struct ibmveth_buff_pool,
1637 kobj);
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07001638 struct net_device *netdev = dev_get_drvdata(
1639 container_of(kobj->parent, struct device, kobj));
Wang Chen4cf16532008-11-12 23:38:14 -08001640 struct ibmveth_adapter *adapter = netdev_priv(netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001641 long value = simple_strtol(buf, NULL, 10);
1642 long rc;
1643
1644 if (attr == &veth_active_attr) {
1645 if (value && !pool->active) {
Brian King4aa9c932007-06-08 14:05:16 -05001646 if (netif_running(netdev)) {
Santiago Leonf148f612010-09-03 18:29:30 +00001647 if (ibmveth_alloc_buffer_pool(pool)) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001648 netdev_err(netdev,
1649 "unable to alloc pool\n");
Brian King4aa9c932007-06-08 14:05:16 -05001650 return -ENOMEM;
1651 }
1652 pool->active = 1;
1653 adapter->pool_config = 1;
1654 ibmveth_close(netdev);
1655 adapter->pool_config = 0;
1656 if ((rc = ibmveth_open(netdev)))
1657 return rc;
Santiago Leonf148f612010-09-03 18:29:30 +00001658 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001659 pool->active = 1;
Santiago Leonf148f612010-09-03 18:29:30 +00001660 }
Santiago Leon860f2422006-04-25 11:19:59 -05001661 } else if (!value && pool->active) {
1662 int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1663 int i;
1664 /* Make sure there is a buffer pool with buffers that
1665 can hold a packet of the size of the MTU */
Santiago Leon517e80e2010-09-03 18:29:25 +00001666 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
Santiago Leon860f2422006-04-25 11:19:59 -05001667 if (pool == &adapter->rx_buff_pool[i])
1668 continue;
1669 if (!adapter->rx_buff_pool[i].active)
1670 continue;
Brian King76b9cfc2007-08-03 13:55:19 +10001671 if (mtu <= adapter->rx_buff_pool[i].buff_size)
1672 break;
Santiago Leon860f2422006-04-25 11:19:59 -05001673 }
Brian King76b9cfc2007-08-03 13:55:19 +10001674
Santiago Leon517e80e2010-09-03 18:29:25 +00001675 if (i == IBMVETH_NUM_BUFF_POOLS) {
Santiago Leon21c2dec2010-09-03 18:29:19 +00001676 netdev_err(netdev, "no active pool >= MTU\n");
Santiago Leon860f2422006-04-25 11:19:59 -05001677 return -EPERM;
1678 }
Brian King76b9cfc2007-08-03 13:55:19 +10001679
Brian King76b9cfc2007-08-03 13:55:19 +10001680 if (netif_running(netdev)) {
1681 adapter->pool_config = 1;
1682 ibmveth_close(netdev);
Santiago Leonea866e62008-07-24 04:34:23 +10001683 pool->active = 0;
Brian King76b9cfc2007-08-03 13:55:19 +10001684 adapter->pool_config = 0;
1685 if ((rc = ibmveth_open(netdev)))
1686 return rc;
1687 }
Santiago Leonea866e62008-07-24 04:34:23 +10001688 pool->active = 0;
Santiago Leon860f2422006-04-25 11:19:59 -05001689 }
1690 } else if (attr == &veth_num_attr) {
Santiago Leonf148f612010-09-03 18:29:30 +00001691 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT) {
Santiago Leon860f2422006-04-25 11:19:59 -05001692 return -EINVAL;
Santiago Leonf148f612010-09-03 18:29:30 +00001693 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001694 if (netif_running(netdev)) {
1695 adapter->pool_config = 1;
1696 ibmveth_close(netdev);
1697 adapter->pool_config = 0;
1698 pool->size = value;
1699 if ((rc = ibmveth_open(netdev)))
1700 return rc;
Santiago Leonf148f612010-09-03 18:29:30 +00001701 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001702 pool->size = value;
Santiago Leonf148f612010-09-03 18:29:30 +00001703 }
Santiago Leon860f2422006-04-25 11:19:59 -05001704 }
1705 } else if (attr == &veth_size_attr) {
Santiago Leonf148f612010-09-03 18:29:30 +00001706 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE) {
Santiago Leon860f2422006-04-25 11:19:59 -05001707 return -EINVAL;
Santiago Leonf148f612010-09-03 18:29:30 +00001708 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001709 if (netif_running(netdev)) {
1710 adapter->pool_config = 1;
1711 ibmveth_close(netdev);
1712 adapter->pool_config = 0;
1713 pool->buff_size = value;
1714 if ((rc = ibmveth_open(netdev)))
1715 return rc;
Santiago Leonf148f612010-09-03 18:29:30 +00001716 } else {
Brian King4aa9c932007-06-08 14:05:16 -05001717 pool->buff_size = value;
Santiago Leonf148f612010-09-03 18:29:30 +00001718 }
Santiago Leon860f2422006-04-25 11:19:59 -05001719 }
1720 }
1721
1722 /* kick the interrupt handler to allocate/deallocate pools */
David Howells7d12e782006-10-05 14:55:46 +01001723 ibmveth_interrupt(netdev->irq, netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001724 return count;
1725}
1726
1727
Santiago Leonf148f612010-09-03 18:29:30 +00001728#define ATTR(_name, _mode) \
1729 struct attribute veth_##_name##_attr = { \
1730 .name = __stringify(_name), .mode = _mode, \
1731 };
Santiago Leon860f2422006-04-25 11:19:59 -05001732
1733static ATTR(active, 0644);
1734static ATTR(num, 0644);
1735static ATTR(size, 0644);
1736
Santiago Leonf148f612010-09-03 18:29:30 +00001737static struct attribute *veth_pool_attrs[] = {
Santiago Leon860f2422006-04-25 11:19:59 -05001738 &veth_active_attr,
1739 &veth_num_attr,
1740 &veth_size_attr,
1741 NULL,
1742};
1743
Emese Revfy52cf25d2010-01-19 02:58:23 +01001744static const struct sysfs_ops veth_pool_ops = {
Santiago Leon860f2422006-04-25 11:19:59 -05001745 .show = veth_pool_show,
1746 .store = veth_pool_store,
1747};
1748
1749static struct kobj_type ktype_veth_pool = {
1750 .release = NULL,
1751 .sysfs_ops = &veth_pool_ops,
1752 .default_attrs = veth_pool_attrs,
1753};
1754
Brian Kinge7a3af52010-05-07 08:56:08 +00001755static int ibmveth_resume(struct device *dev)
1756{
1757 struct net_device *netdev = dev_get_drvdata(dev);
1758 ibmveth_interrupt(netdev->irq, netdev);
1759 return 0;
1760}
Santiago Leon860f2422006-04-25 11:19:59 -05001761
Bill Pembertone11787a2012-12-03 09:23:12 -05001762static struct vio_device_id ibmveth_device_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 { "network", "IBM,l-lan"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +10001764 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1767
Brian Kinge7a3af52010-05-07 08:56:08 +00001768static struct dev_pm_ops ibmveth_pm_ops = {
1769 .resume = ibmveth_resume
1770};
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772static struct vio_driver ibmveth_driver = {
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001773 .id_table = ibmveth_device_table,
1774 .probe = ibmveth_probe,
1775 .remove = ibmveth_remove,
Robert Jennings1096d632008-07-24 04:34:52 +10001776 .get_desired_dma = ibmveth_get_desired_dma,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +00001777 .name = ibmveth_driver_name,
1778 .pm = &ibmveth_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779};
1780
1781static int __init ibmveth_module_init(void)
1782{
Santiago Leon21c2dec2010-09-03 18:29:19 +00001783 printk(KERN_DEBUG "%s: %s %s\n", ibmveth_driver_name,
1784 ibmveth_driver_string, ibmveth_driver_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return vio_register_driver(&ibmveth_driver);
1787}
1788
1789static void __exit ibmveth_module_exit(void)
1790{
1791 vio_unregister_driver(&ibmveth_driver);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001792}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794module_init(ibmveth_module_init);
1795module_exit(ibmveth_module_exit);