blob: 00527805e4f170a8a1bdb6888aa2a30829210d6b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**************************************************************************/
2/* */
3/* IBM eServer i/pSeries Virtual Ethernet Device Driver */
4/* Copyright (C) 2003 IBM Corp. */
5/* Originally written by Dave Larson (larson1@us.ibm.com) */
6/* Maintained by Santiago Leon (santil@us.ibm.com) */
7/* */
8/* This program is free software; you can redistribute it and/or modify */
9/* it under the terms of the GNU General Public License as published by */
10/* the Free Software Foundation; either version 2 of the License, or */
11/* (at your option) any later version. */
12/* */
13/* This program is distributed in the hope that it will be useful, */
14/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16/* GNU General Public License for more details. */
17/* */
18/* You should have received a copy of the GNU General Public License */
19/* along with this program; if not, write to the Free Software */
20/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 */
21/* USA */
22/* */
23/* This module contains the implementation of a virtual ethernet device */
24/* for use with IBM i/pSeries LPAR Linux. It utilizes the logical LAN */
25/* option of the RS/6000 Platform Architechture to interface with virtual */
26/* ethernet NICs that are presented to the partition by the hypervisor. */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -040027/* */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/**************************************************************************/
29/*
30 TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 - add support for sysfs
32 - possibly remove procfs support
33*/
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/types.h>
37#include <linux/errno.h>
38#include <linux/ioport.h>
39#include <linux/dma-mapping.h>
40#include <linux/kernel.h>
41#include <linux/netdevice.h>
42#include <linux/etherdevice.h>
43#include <linux/skbuff.h>
44#include <linux/init.h>
45#include <linux/delay.h>
46#include <linux/mm.h>
47#include <linux/ethtool.h>
48#include <linux/proc_fs.h>
Brian Kingf4ff2872007-09-15 13:36:07 -070049#include <linux/in.h>
50#include <linux/ip.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020051#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/hvcall.h>
53#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/vio.h>
55#include <asm/uaccess.h>
56#include <linux/seq_file.h>
57
58#include "ibmveth.h"
59
Anton Blanchard6af37fa2005-11-11 14:02:04 +110060#undef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#define ibmveth_printk(fmt, args...) \
Olof Johanssonf98baff2006-04-12 15:22:29 -050063 printk(KERN_DEBUG "%s: " fmt, __FILE__, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#define ibmveth_error_printk(fmt, args...) \
66 printk(KERN_ERR "(%s:%3.3d ua:%x) ERROR: " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
67
68#ifdef DEBUG
69#define ibmveth_debug_printk_no_adapter(fmt, args...) \
70 printk(KERN_DEBUG "(%s:%3.3d): " fmt, __FILE__, __LINE__ , ## args)
71#define ibmveth_debug_printk(fmt, args...) \
72 printk(KERN_DEBUG "(%s:%3.3d ua:%x): " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
73#define ibmveth_assert(expr) \
74 if(!(expr)) { \
75 printk(KERN_DEBUG "assertion failed (%s:%3.3d ua:%x): %s\n", __FILE__, __LINE__, adapter->vdev->unit_address, #expr); \
76 BUG(); \
77 }
78#else
79#define ibmveth_debug_printk_no_adapter(fmt, args...)
80#define ibmveth_debug_printk(fmt, args...)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -040081#define ibmveth_assert(expr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif
83
84static int ibmveth_open(struct net_device *dev);
85static int ibmveth_close(struct net_device *dev);
86static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070087static int ibmveth_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static void ibmveth_set_multicast_list(struct net_device *dev);
90static int ibmveth_change_mtu(struct net_device *dev, int new_mtu);
91static void ibmveth_proc_register_driver(void);
92static void ibmveth_proc_unregister_driver(void);
93static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter);
94static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter);
David Howells7d12e782006-10-05 14:55:46 +010095static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
Michael Ellerman493a6842007-04-17 13:12:55 +100096static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
Santiago Leon860f2422006-04-25 11:19:59 -050097static struct kobj_type ktype_veth_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99#ifdef CONFIG_PROC_FS
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200100#define IBMVETH_PROC_DIR "ibmveth"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static struct proc_dir_entry *ibmveth_proc_dir;
102#endif
103
104static const char ibmveth_driver_name[] = "ibmveth";
105static const char ibmveth_driver_string[] = "IBM i/pSeries Virtual Ethernet Driver";
106#define ibmveth_driver_version "1.03"
107
108MODULE_AUTHOR("Santiago Leon <santil@us.ibm.com>");
109MODULE_DESCRIPTION("IBM i/pSeries Virtual Ethernet Driver");
110MODULE_LICENSE("GPL");
111MODULE_VERSION(ibmveth_driver_version);
112
Brian Kingddbb4de2007-08-17 09:16:43 -0500113struct ibmveth_stat {
114 char name[ETH_GSTRING_LEN];
115 int offset;
116};
117
118#define IBMVETH_STAT_OFF(stat) offsetof(struct ibmveth_adapter, stat)
119#define IBMVETH_GET_STAT(a, off) *((u64 *)(((unsigned long)(a)) + off))
120
121struct ibmveth_stat ibmveth_stats[] = {
122 { "replenish_task_cycles", IBMVETH_STAT_OFF(replenish_task_cycles) },
123 { "replenish_no_mem", IBMVETH_STAT_OFF(replenish_no_mem) },
124 { "replenish_add_buff_failure", IBMVETH_STAT_OFF(replenish_add_buff_failure) },
125 { "replenish_add_buff_success", IBMVETH_STAT_OFF(replenish_add_buff_success) },
126 { "rx_invalid_buffer", IBMVETH_STAT_OFF(rx_invalid_buffer) },
127 { "rx_no_buffer", IBMVETH_STAT_OFF(rx_no_buffer) },
Brian Kingddbb4de2007-08-17 09:16:43 -0500128 { "tx_map_failed", IBMVETH_STAT_OFF(tx_map_failed) },
129 { "tx_send_failed", IBMVETH_STAT_OFF(tx_send_failed) },
130};
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/* simple methods of getting data from the current rxq entry */
Brian King79ef4a42007-08-17 09:16:56 -0500133static inline u32 ibmveth_rxq_flags(struct ibmveth_adapter *adapter)
134{
135 return adapter->rx_queue.queue_addr[adapter->rx_queue.index].flags_off;
136}
137
138static inline int ibmveth_rxq_toggle(struct ibmveth_adapter *adapter)
139{
140 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_TOGGLE) >> IBMVETH_RXQ_TOGGLE_SHIFT;
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
144{
Brian King79ef4a42007-08-17 09:16:56 -0500145 return (ibmveth_rxq_toggle(adapter) == adapter->rx_queue.toggle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
149{
Brian King79ef4a42007-08-17 09:16:56 -0500150 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
154{
Brian King79ef4a42007-08-17 09:16:56 -0500155 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_OFF_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
159{
160 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
161}
162
Brian Kingf4ff2872007-09-15 13:36:07 -0700163static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
164{
Brian King79ef4a42007-08-17 09:16:56 -0500165 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_CSUM_GOOD);
Brian Kingf4ff2872007-09-15 13:36:07 -0700166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* setup the initial settings for a buffer pool */
Santiago Leon860f2422006-04-25 11:19:59 -0500169static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool, u32 pool_index, u32 pool_size, u32 buff_size, u32 pool_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 pool->size = pool_size;
172 pool->index = pool_index;
173 pool->buff_size = buff_size;
174 pool->threshold = pool_size / 2;
Santiago Leon860f2422006-04-25 11:19:59 -0500175 pool->active = pool_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178/* allocate and setup an buffer pool - called during open */
179static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
180{
181 int i;
182
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400183 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if(!pool->free_map) {
186 return -1;
187 }
188
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400189 pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if(!pool->dma_addr) {
191 kfree(pool->free_map);
192 pool->free_map = NULL;
193 return -1;
194 }
195
196 pool->skbuff = kmalloc(sizeof(void*) * pool->size, GFP_KERNEL);
197
198 if(!pool->skbuff) {
199 kfree(pool->dma_addr);
200 pool->dma_addr = NULL;
201
202 kfree(pool->free_map);
203 pool->free_map = NULL;
204 return -1;
205 }
206
207 memset(pool->skbuff, 0, sizeof(void*) * pool->size);
208 memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
209
210 for(i = 0; i < pool->size; ++i) {
211 pool->free_map[i] = i;
212 }
213
214 atomic_set(&pool->available, 0);
215 pool->producer_index = 0;
216 pool->consumer_index = 0;
217
218 return 0;
219}
220
221/* replenish the buffers for a pool. note that we don't need to
222 * skb_reserve these since they are used for incoming...
223 */
224static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
225{
226 u32 i;
227 u32 count = pool->size - atomic_read(&pool->available);
228 u32 buffers_added = 0;
229
230 mb();
231
232 for(i = 0; i < count; ++i) {
233 struct sk_buff *skb;
234 unsigned int free_index, index;
235 u64 correlator;
236 union ibmveth_buf_desc desc;
237 unsigned long lpar_rc;
238 dma_addr_t dma_addr;
239
240 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
241
242 if(!skb) {
243 ibmveth_debug_printk("replenish: unable to allocate skb\n");
244 adapter->replenish_no_mem++;
245 break;
246 }
247
David Gibson047a66d2006-10-21 10:24:13 -0700248 free_index = pool->consumer_index;
249 pool->consumer_index = (pool->consumer_index + 1) % pool->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 index = pool->free_map[free_index];
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 ibmveth_assert(index != IBM_VETH_INVALID_MAP);
253 ibmveth_assert(pool->skbuff[index] == NULL);
254
255 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
256 pool->buff_size, DMA_FROM_DEVICE);
257
258 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
259 pool->dma_addr[index] = dma_addr;
260 pool->skbuff[index] = skb;
261
262 correlator = ((u64)pool->index << 32) | index;
263 *(u64*)skb->data = correlator;
264
Brian King79ef4a42007-08-17 09:16:56 -0500265 desc.fields.flags_len = IBMVETH_BUF_VALID | pool->buff_size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400266 desc.fields.address = dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400269
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200270 if(lpar_rc != H_SUCCESS) {
Santiago Leon82702d32005-10-26 10:47:23 -0600271 pool->free_map[free_index] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 pool->skbuff[index] = NULL;
Santiago Leon751ae212006-10-03 12:24:45 -0500273 if (pool->consumer_index == 0)
274 pool->consumer_index = pool->size - 1;
275 else
276 pool->consumer_index--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 dma_unmap_single(&adapter->vdev->dev,
278 pool->dma_addr[index], pool->buff_size,
279 DMA_FROM_DEVICE);
280 dev_kfree_skb_any(skb);
281 adapter->replenish_add_buff_failure++;
282 break;
283 } else {
284 buffers_added++;
285 adapter->replenish_add_buff_success++;
286 }
287 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 mb();
290 atomic_add(buffers_added, &(pool->available));
291}
292
Santiago Leone2adbcb2005-10-26 10:47:08 -0600293/* replenish routine */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400294static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Santiago Leonb6d35182005-10-26 10:47:01 -0600296 int i;
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 adapter->replenish_task_cycles++;
299
Santiago Leonb6d35182005-10-26 10:47:01 -0600300 for(i = 0; i < IbmVethNumBufferPools; i++)
301 if(adapter->rx_buff_pool[i].active)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400302 ibmveth_replenish_buffer_pool(adapter,
Santiago Leonb6d35182005-10-26 10:47:01 -0600303 &adapter->rx_buff_pool[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308/* empty and free ana buffer pool - also used to do cleanup in error paths */
309static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
310{
311 int i;
312
Jesper Juhlb4558ea2005-10-28 16:53:13 -0400313 kfree(pool->free_map);
314 pool->free_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 if(pool->skbuff && pool->dma_addr) {
317 for(i = 0; i < pool->size; ++i) {
318 struct sk_buff *skb = pool->skbuff[i];
319 if(skb) {
320 dma_unmap_single(&adapter->vdev->dev,
321 pool->dma_addr[i],
322 pool->buff_size,
323 DMA_FROM_DEVICE);
324 dev_kfree_skb_any(skb);
325 pool->skbuff[i] = NULL;
326 }
327 }
328 }
329
330 if(pool->dma_addr) {
331 kfree(pool->dma_addr);
332 pool->dma_addr = NULL;
333 }
334
335 if(pool->skbuff) {
336 kfree(pool->skbuff);
337 pool->skbuff = NULL;
338 }
339}
340
341/* remove a buffer from a pool */
342static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter, u64 correlator)
343{
344 unsigned int pool = correlator >> 32;
345 unsigned int index = correlator & 0xffffffffUL;
346 unsigned int free_index;
347 struct sk_buff *skb;
348
349 ibmveth_assert(pool < IbmVethNumBufferPools);
350 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
351
352 skb = adapter->rx_buff_pool[pool].skbuff[index];
353
354 ibmveth_assert(skb != NULL);
355
356 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
357
358 dma_unmap_single(&adapter->vdev->dev,
359 adapter->rx_buff_pool[pool].dma_addr[index],
360 adapter->rx_buff_pool[pool].buff_size,
361 DMA_FROM_DEVICE);
362
David Gibson047a66d2006-10-21 10:24:13 -0700363 free_index = adapter->rx_buff_pool[pool].producer_index;
364 adapter->rx_buff_pool[pool].producer_index
365 = (adapter->rx_buff_pool[pool].producer_index + 1)
366 % adapter->rx_buff_pool[pool].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 adapter->rx_buff_pool[pool].free_map[free_index] = index;
368
369 mb();
370
371 atomic_dec(&(adapter->rx_buff_pool[pool].available));
372}
373
374/* get the current buffer on the rx queue */
375static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
376{
377 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
378 unsigned int pool = correlator >> 32;
379 unsigned int index = correlator & 0xffffffffUL;
380
381 ibmveth_assert(pool < IbmVethNumBufferPools);
382 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
383
384 return adapter->rx_buff_pool[pool].skbuff[index];
385}
386
387/* recycle the current buffer on the rx queue */
388static void ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
389{
390 u32 q_index = adapter->rx_queue.index;
391 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
392 unsigned int pool = correlator >> 32;
393 unsigned int index = correlator & 0xffffffffUL;
394 union ibmveth_buf_desc desc;
395 unsigned long lpar_rc;
396
397 ibmveth_assert(pool < IbmVethNumBufferPools);
398 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
399
Santiago Leonb6d35182005-10-26 10:47:01 -0600400 if(!adapter->rx_buff_pool[pool].active) {
401 ibmveth_rxq_harvest_buffer(adapter);
402 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
403 return;
404 }
405
Brian King79ef4a42007-08-17 09:16:56 -0500406 desc.fields.flags_len = IBMVETH_BUF_VALID |
407 adapter->rx_buff_pool[pool].buff_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
409
410 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400411
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200412 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ibmveth_debug_printk("h_add_logical_lan_buffer failed during recycle rc=%ld", lpar_rc);
414 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
415 }
416
417 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
418 adapter->rx_queue.index = 0;
419 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
420 }
421}
422
Michael Ellerman493a6842007-04-17 13:12:55 +1000423static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
426
427 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
428 adapter->rx_queue.index = 0;
429 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
430 }
431}
432
433static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
434{
Santiago Leonb6d35182005-10-26 10:47:01 -0600435 int i;
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if(adapter->buffer_list_addr != NULL) {
438 if(!dma_mapping_error(adapter->buffer_list_dma)) {
439 dma_unmap_single(&adapter->vdev->dev,
440 adapter->buffer_list_dma, 4096,
441 DMA_BIDIRECTIONAL);
442 adapter->buffer_list_dma = DMA_ERROR_CODE;
443 }
444 free_page((unsigned long)adapter->buffer_list_addr);
445 adapter->buffer_list_addr = NULL;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if(adapter->filter_list_addr != NULL) {
449 if(!dma_mapping_error(adapter->filter_list_dma)) {
450 dma_unmap_single(&adapter->vdev->dev,
451 adapter->filter_list_dma, 4096,
452 DMA_BIDIRECTIONAL);
453 adapter->filter_list_dma = DMA_ERROR_CODE;
454 }
455 free_page((unsigned long)adapter->filter_list_addr);
456 adapter->filter_list_addr = NULL;
457 }
458
459 if(adapter->rx_queue.queue_addr != NULL) {
460 if(!dma_mapping_error(adapter->rx_queue.queue_dma)) {
461 dma_unmap_single(&adapter->vdev->dev,
462 adapter->rx_queue.queue_dma,
463 adapter->rx_queue.queue_len,
464 DMA_BIDIRECTIONAL);
465 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
466 }
467 kfree(adapter->rx_queue.queue_addr);
468 adapter->rx_queue.queue_addr = NULL;
469 }
470
Santiago Leonb6d35182005-10-26 10:47:01 -0600471 for(i = 0; i<IbmVethNumBufferPools; i++)
Santiago Leon860f2422006-04-25 11:19:59 -0500472 if (adapter->rx_buff_pool[i].active)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400473 ibmveth_free_buffer_pool(adapter,
Santiago Leon860f2422006-04-25 11:19:59 -0500474 &adapter->rx_buff_pool[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500477static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
478 union ibmveth_buf_desc rxq_desc, u64 mac_address)
479{
480 int rc, try_again = 1;
481
482 /* After a kexec the adapter will still be open, so our attempt to
483 * open it will fail. So if we get a failure we free the adapter and
484 * try again, but only once. */
485retry:
486 rc = h_register_logical_lan(adapter->vdev->unit_address,
487 adapter->buffer_list_dma, rxq_desc.desc,
488 adapter->filter_list_dma, mac_address);
489
490 if (rc != H_SUCCESS && try_again) {
491 do {
492 rc = h_free_logical_lan(adapter->vdev->unit_address);
493 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
494
495 try_again = 0;
496 goto retry;
497 }
498
499 return rc;
500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502static int ibmveth_open(struct net_device *netdev)
503{
504 struct ibmveth_adapter *adapter = netdev->priv;
505 u64 mac_address = 0;
Santiago Leonb6d35182005-10-26 10:47:01 -0600506 int rxq_entries = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 unsigned long lpar_rc;
508 int rc;
509 union ibmveth_buf_desc rxq_desc;
Santiago Leonb6d35182005-10-26 10:47:01 -0600510 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 ibmveth_debug_printk("open starting\n");
513
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700514 napi_enable(&adapter->napi);
515
Santiago Leonb6d35182005-10-26 10:47:01 -0600516 for(i = 0; i<IbmVethNumBufferPools; i++)
517 rxq_entries += adapter->rx_buff_pool[i].size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
520 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if(!adapter->buffer_list_addr || !adapter->filter_list_addr) {
523 ibmveth_error_printk("unable to allocate filter or buffer list pages\n");
524 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700525 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return -ENOMEM;
527 }
528
529 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) * rxq_entries;
530 adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len, GFP_KERNEL);
531
532 if(!adapter->rx_queue.queue_addr) {
533 ibmveth_error_printk("unable to allocate rx queue pages\n");
534 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700535 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return -ENOMEM;
537 }
538
539 adapter->buffer_list_dma = dma_map_single(&adapter->vdev->dev,
540 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
541 adapter->filter_list_dma = dma_map_single(&adapter->vdev->dev,
542 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
543 adapter->rx_queue.queue_dma = dma_map_single(&adapter->vdev->dev,
544 adapter->rx_queue.queue_addr,
545 adapter->rx_queue.queue_len, DMA_BIDIRECTIONAL);
546
547 if((dma_mapping_error(adapter->buffer_list_dma) ) ||
548 (dma_mapping_error(adapter->filter_list_dma)) ||
549 (dma_mapping_error(adapter->rx_queue.queue_dma))) {
550 ibmveth_error_printk("unable to map filter or buffer list pages\n");
551 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700552 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return -ENOMEM;
554 }
555
556 adapter->rx_queue.index = 0;
557 adapter->rx_queue.num_slots = rxq_entries;
558 adapter->rx_queue.toggle = 1;
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
561 mac_address = mac_address >> 16;
562
Brian King79ef4a42007-08-17 09:16:56 -0500563 rxq_desc.fields.flags_len = IBMVETH_BUF_VALID | adapter->rx_queue.queue_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
565
566 ibmveth_debug_printk("buffer list @ 0x%p\n", adapter->buffer_list_addr);
567 ibmveth_debug_printk("filter list @ 0x%p\n", adapter->filter_list_addr);
568 ibmveth_debug_printk("receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
569
Santiago Leon4347ef12006-10-03 12:24:34 -0500570 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
571
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500572 lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200574 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 ibmveth_error_printk("h_register_logical_lan failed with %ld\n", lpar_rc);
Stephen Rothwell8168f902005-10-25 16:56:43 +1000576 ibmveth_error_printk("buffer TCE:0x%lx filter TCE:0x%lx rxq desc:0x%lx MAC:0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 adapter->buffer_list_dma,
578 adapter->filter_list_dma,
579 rxq_desc.desc,
580 mac_address);
581 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700582 napi_disable(&adapter->napi);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400583 return -ENONET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
Santiago Leon860f2422006-04-25 11:19:59 -0500586 for(i = 0; i<IbmVethNumBufferPools; i++) {
587 if(!adapter->rx_buff_pool[i].active)
588 continue;
589 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
590 ibmveth_error_printk("unable to alloc pool\n");
591 adapter->rx_buff_pool[i].active = 0;
592 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700593 napi_disable(&adapter->napi);
Santiago Leon860f2422006-04-25 11:19:59 -0500594 return -ENOMEM ;
595 }
596 }
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 ibmveth_debug_printk("registering irq 0x%x\n", netdev->irq);
599 if((rc = request_irq(netdev->irq, &ibmveth_interrupt, 0, netdev->name, netdev)) != 0) {
600 ibmveth_error_printk("unable to request irq 0x%x, rc %d\n", netdev->irq, rc);
601 do {
602 rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200603 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700606 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return rc;
608 }
609
Santiago Leone2adbcb2005-10-26 10:47:08 -0600610 ibmveth_debug_printk("initial replenish cycle\n");
David Howells7d12e782006-10-05 14:55:46 +0100611 ibmveth_interrupt(netdev->irq, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Santiago Leone2adbcb2005-10-26 10:47:08 -0600613 netif_start_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 ibmveth_debug_printk("open complete\n");
616
617 return 0;
618}
619
620static int ibmveth_close(struct net_device *netdev)
621{
622 struct ibmveth_adapter *adapter = netdev->priv;
623 long lpar_rc;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 ibmveth_debug_printk("close starting\n");
626
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700627 napi_disable(&adapter->napi);
628
Santiago Leon860f2422006-04-25 11:19:59 -0500629 if (!adapter->pool_config)
630 netif_stop_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 free_irq(netdev->irq, netdev);
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 do {
635 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200636 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200638 if(lpar_rc != H_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 {
640 ibmveth_error_printk("h_free_logical_lan failed with %lx, continuing with close\n",
641 lpar_rc);
642 }
643
644 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
645
646 ibmveth_cleanup(adapter);
647
648 ibmveth_debug_printk("close complete\n");
649
650 return 0;
651}
652
653static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
654 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
655 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | ADVERTISED_FIBRE);
656 cmd->speed = SPEED_1000;
657 cmd->duplex = DUPLEX_FULL;
658 cmd->port = PORT_FIBRE;
659 cmd->phy_address = 0;
660 cmd->transceiver = XCVR_INTERNAL;
661 cmd->autoneg = AUTONEG_ENABLE;
662 cmd->maxtxpkt = 0;
663 cmd->maxrxpkt = 1;
664 return 0;
665}
666
667static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info) {
668 strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
669 strncpy(info->version, ibmveth_driver_version, sizeof(info->version) - 1);
670}
671
672static u32 netdev_get_link(struct net_device *dev) {
673 return 1;
674}
675
Brian King5fc7e012007-08-17 09:16:31 -0500676static void ibmveth_set_rx_csum_flags(struct net_device *dev, u32 data)
677{
678 struct ibmveth_adapter *adapter = dev->priv;
679
680 if (data)
681 adapter->rx_csum = 1;
682 else {
683 /*
684 * Since the ibmveth firmware interface does not have the concept of
685 * separate tx/rx checksum offload enable, if rx checksum is disabled
686 * we also have to disable tx checksum offload. Once we disable rx
687 * checksum offload, we are no longer allowed to send tx buffers that
688 * are not properly checksummed.
689 */
690 adapter->rx_csum = 0;
691 dev->features &= ~NETIF_F_IP_CSUM;
692 }
693}
694
695static void ibmveth_set_tx_csum_flags(struct net_device *dev, u32 data)
696{
697 struct ibmveth_adapter *adapter = dev->priv;
698
699 if (data) {
700 dev->features |= NETIF_F_IP_CSUM;
701 adapter->rx_csum = 1;
702 } else
703 dev->features &= ~NETIF_F_IP_CSUM;
704}
705
706static int ibmveth_set_csum_offload(struct net_device *dev, u32 data,
707 void (*done) (struct net_device *, u32))
708{
709 struct ibmveth_adapter *adapter = dev->priv;
Brian King79ef4a42007-08-17 09:16:56 -0500710 u64 set_attr, clr_attr, ret_attr;
Brian King5fc7e012007-08-17 09:16:31 -0500711 long ret;
712 int rc1 = 0, rc2 = 0;
713 int restart = 0;
714
715 if (netif_running(dev)) {
716 restart = 1;
717 adapter->pool_config = 1;
718 ibmveth_close(dev);
719 adapter->pool_config = 0;
720 }
721
Brian King79ef4a42007-08-17 09:16:56 -0500722 set_attr = 0;
723 clr_attr = 0;
Brian King5fc7e012007-08-17 09:16:31 -0500724
725 if (data)
Brian King79ef4a42007-08-17 09:16:56 -0500726 set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Brian King5fc7e012007-08-17 09:16:31 -0500727 else
Brian King79ef4a42007-08-17 09:16:56 -0500728 clr_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Brian King5fc7e012007-08-17 09:16:31 -0500729
Brian King79ef4a42007-08-17 09:16:56 -0500730 ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500731
Brian King79ef4a42007-08-17 09:16:56 -0500732 if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
733 !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
734 (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
735 ret = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
736 set_attr, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500737
738 if (ret != H_SUCCESS) {
739 rc1 = -EIO;
740 ibmveth_error_printk("unable to change checksum offload settings."
741 " %d rc=%ld\n", data, ret);
742
743 ret = h_illan_attributes(adapter->vdev->unit_address,
Brian King79ef4a42007-08-17 09:16:56 -0500744 set_attr, clr_attr, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500745 } else
746 done(dev, data);
747 } else {
748 rc1 = -EIO;
749 ibmveth_error_printk("unable to change checksum offload settings."
Brian King79ef4a42007-08-17 09:16:56 -0500750 " %d rc=%ld ret_attr=%lx\n", data, ret, ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500751 }
752
753 if (restart)
754 rc2 = ibmveth_open(dev);
755
756 return rc1 ? rc1 : rc2;
757}
758
759static int ibmveth_set_rx_csum(struct net_device *dev, u32 data)
760{
761 struct ibmveth_adapter *adapter = dev->priv;
762
763 if ((data && adapter->rx_csum) || (!data && !adapter->rx_csum))
764 return 0;
765
766 return ibmveth_set_csum_offload(dev, data, ibmveth_set_rx_csum_flags);
767}
768
769static int ibmveth_set_tx_csum(struct net_device *dev, u32 data)
770{
771 struct ibmveth_adapter *adapter = dev->priv;
772 int rc = 0;
773
774 if (data && (dev->features & NETIF_F_IP_CSUM))
775 return 0;
776 if (!data && !(dev->features & NETIF_F_IP_CSUM))
777 return 0;
778
779 if (data && !adapter->rx_csum)
780 rc = ibmveth_set_csum_offload(dev, data, ibmveth_set_tx_csum_flags);
781 else
782 ibmveth_set_tx_csum_flags(dev, data);
783
784 return rc;
785}
786
787static u32 ibmveth_get_rx_csum(struct net_device *dev)
788{
789 struct ibmveth_adapter *adapter = dev->priv;
790 return adapter->rx_csum;
791}
792
Brian Kingddbb4de2007-08-17 09:16:43 -0500793static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
794{
795 int i;
796
797 if (stringset != ETH_SS_STATS)
798 return;
799
800 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
801 memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
802}
803
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700804static int ibmveth_get_sset_count(struct net_device *dev, int sset)
Brian Kingddbb4de2007-08-17 09:16:43 -0500805{
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700806 switch (sset) {
807 case ETH_SS_STATS:
808 return ARRAY_SIZE(ibmveth_stats);
809 default:
810 return -EOPNOTSUPP;
811 }
Brian Kingddbb4de2007-08-17 09:16:43 -0500812}
813
814static void ibmveth_get_ethtool_stats(struct net_device *dev,
815 struct ethtool_stats *stats, u64 *data)
816{
817 int i;
818 struct ibmveth_adapter *adapter = dev->priv;
819
820 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
821 data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
822}
823
Jeff Garzik7282d492006-09-13 14:30:00 -0400824static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 .get_drvinfo = netdev_get_drvinfo,
826 .get_settings = netdev_get_settings,
827 .get_link = netdev_get_link,
Brian King5fc7e012007-08-17 09:16:31 -0500828 .set_tx_csum = ibmveth_set_tx_csum,
829 .get_rx_csum = ibmveth_get_rx_csum,
830 .set_rx_csum = ibmveth_set_rx_csum,
Brian Kingddbb4de2007-08-17 09:16:43 -0500831 .get_strings = ibmveth_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700832 .get_sset_count = ibmveth_get_sset_count,
Brian Kingddbb4de2007-08-17 09:16:43 -0500833 .get_ethtool_stats = ibmveth_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834};
835
836static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
837{
838 return -EOPNOTSUPP;
839}
840
841#define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
842
843static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *netdev)
844{
845 struct ibmveth_adapter *adapter = netdev->priv;
Brian King3449a2a2007-08-17 09:16:49 -0500846 union ibmveth_buf_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 unsigned long lpar_rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 unsigned long correlator;
Santiago Leon60296d92005-10-26 10:47:16 -0600849 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 unsigned int retry_count;
Santiago Leon60296d92005-10-26 10:47:16 -0600851 unsigned int tx_dropped = 0;
852 unsigned int tx_bytes = 0;
853 unsigned int tx_packets = 0;
854 unsigned int tx_send_failed = 0;
855 unsigned int tx_map_failed = 0;
856
Brian King79ef4a42007-08-17 09:16:56 -0500857 desc.fields.flags_len = IBMVETH_BUF_VALID | skb->len;
Brian King3449a2a2007-08-17 09:16:49 -0500858 desc.fields.address = dma_map_single(&adapter->vdev->dev, skb->data,
Brian King79ef4a42007-08-17 09:16:56 -0500859 skb->len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Brian Kingf4ff2872007-09-15 13:36:07 -0700861 if (skb->ip_summed == CHECKSUM_PARTIAL &&
862 ip_hdr(skb)->protocol != IPPROTO_TCP && skb_checksum_help(skb)) {
863 ibmveth_error_printk("tx: failed to checksum packet\n");
864 tx_dropped++;
865 goto out;
866 }
867
868 if (skb->ip_summed == CHECKSUM_PARTIAL) {
869 unsigned char *buf = skb_transport_header(skb) + skb->csum_offset;
870
Brian King79ef4a42007-08-17 09:16:56 -0500871 desc.fields.flags_len |= (IBMVETH_BUF_NO_CSUM | IBMVETH_BUF_CSUM_GOOD);
Brian Kingf4ff2872007-09-15 13:36:07 -0700872
873 /* Need to zero out the checksum */
874 buf[0] = 0;
875 buf[1] = 0;
876 }
877
Brian King3449a2a2007-08-17 09:16:49 -0500878 if (dma_mapping_error(desc.fields.address)) {
879 ibmveth_error_printk("tx: unable to map xmit buffer\n");
Santiago Leon60296d92005-10-26 10:47:16 -0600880 tx_map_failed++;
881 tx_dropped++;
882 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 /* send the frame. Arbitrarily set retrycount to 1024 */
886 correlator = 0;
887 retry_count = 1024;
888 do {
889 lpar_rc = h_send_logical_lan(adapter->vdev->unit_address,
Brian King3449a2a2007-08-17 09:16:49 -0500890 desc.desc, 0, 0, 0, 0, 0,
891 correlator, &correlator);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200892 } while ((lpar_rc == H_BUSY) && (retry_count--));
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400893
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200894 if(lpar_rc != H_SUCCESS && lpar_rc != H_DROPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 ibmveth_error_printk("tx: h_send_logical_lan failed with rc=%ld\n", lpar_rc);
Brian King3449a2a2007-08-17 09:16:49 -0500896 ibmveth_error_printk("tx: valid=%d, len=%d, address=0x%08x\n",
Brian King79ef4a42007-08-17 09:16:56 -0500897 (desc.fields.flags_len & IBMVETH_BUF_VALID) ? 1 : 0,
898 skb->len, desc.fields.address);
Santiago Leon60296d92005-10-26 10:47:16 -0600899 tx_send_failed++;
900 tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 } else {
Santiago Leon60296d92005-10-26 10:47:16 -0600902 tx_packets++;
903 tx_bytes += skb->len;
Santiago Leon0abe7912005-10-26 10:46:53 -0600904 netdev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906
Brian King3449a2a2007-08-17 09:16:49 -0500907 dma_unmap_single(&adapter->vdev->dev, desc.fields.address,
Brian King79ef4a42007-08-17 09:16:56 -0500908 skb->len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Santiago Leon60296d92005-10-26 10:47:16 -0600910out: spin_lock_irqsave(&adapter->stats_lock, flags);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700911 netdev->stats.tx_dropped += tx_dropped;
912 netdev->stats.tx_bytes += tx_bytes;
913 netdev->stats.tx_packets += tx_packets;
Santiago Leon60296d92005-10-26 10:47:16 -0600914 adapter->tx_send_failed += tx_send_failed;
915 adapter->tx_map_failed += tx_map_failed;
916 spin_unlock_irqrestore(&adapter->stats_lock, flags);
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 dev_kfree_skb(skb);
919 return 0;
920}
921
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700922static int ibmveth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700924 struct ibmveth_adapter *adapter = container_of(napi, struct ibmveth_adapter, napi);
925 struct net_device *netdev = adapter->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 int frames_processed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 unsigned long lpar_rc;
928
929 restart_poll:
930 do {
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700931 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700933 if (!ibmveth_rxq_pending_buffer(adapter))
934 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700936 rmb();
937 if (!ibmveth_rxq_buffer_valid(adapter)) {
938 wmb(); /* suggested by larson1 */
939 adapter->rx_invalid_buffer++;
940 ibmveth_debug_printk("recycling invalid buffer\n");
941 ibmveth_rxq_recycle_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 } else {
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700943 int length = ibmveth_rxq_frame_length(adapter);
944 int offset = ibmveth_rxq_frame_offset(adapter);
Brian Kingf4ff2872007-09-15 13:36:07 -0700945 int csum_good = ibmveth_rxq_csum_good(adapter);
946
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700947 skb = ibmveth_rxq_get_buffer(adapter);
Brian Kingf4ff2872007-09-15 13:36:07 -0700948 if (csum_good)
949 skb->ip_summed = CHECKSUM_UNNECESSARY;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700950
951 ibmveth_rxq_harvest_buffer(adapter);
952
953 skb_reserve(skb, offset);
954 skb_put(skb, length);
955 skb->protocol = eth_type_trans(skb, netdev);
956
957 netif_receive_skb(skb); /* send it up */
958
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700959 netdev->stats.rx_packets++;
960 netdev->stats.rx_bytes += length;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700961 frames_processed++;
962 netdev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700964 } while (frames_processed < budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Santiago Leone2adbcb2005-10-26 10:47:08 -0600966 ibmveth_replenish_task(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700968 if (frames_processed < budget) {
969 /* We think we are done - reenable interrupts,
970 * then check once more to make sure we are done.
971 */
972 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
973 VIO_IRQ_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200975 ibmveth_assert(lpar_rc == H_SUCCESS);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700976
977 netif_rx_complete(netdev, napi);
978
979 if (ibmveth_rxq_pending_buffer(adapter) &&
980 netif_rx_reschedule(netdev, napi)) {
981 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
982 VIO_IRQ_DISABLE);
983 goto restart_poll;
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700987 return frames_processed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
David Howells7d12e782006-10-05 14:55:46 +0100990static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400991{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 struct net_device *netdev = dev_instance;
993 struct ibmveth_adapter *adapter = netdev->priv;
994 unsigned long lpar_rc;
995
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700996 if (netif_rx_schedule_prep(netdev, &adapter->napi)) {
997 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
998 VIO_IRQ_DISABLE);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200999 ibmveth_assert(lpar_rc == H_SUCCESS);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001000 __netif_rx_schedule(netdev, &adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002 return IRQ_HANDLED;
1003}
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005static void ibmveth_set_multicast_list(struct net_device *netdev)
1006{
1007 struct ibmveth_adapter *adapter = netdev->priv;
1008 unsigned long lpar_rc;
1009
1010 if((netdev->flags & IFF_PROMISC) || (netdev->mc_count > adapter->mcastFilterSize)) {
1011 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1012 IbmVethMcastEnableRecv |
1013 IbmVethMcastDisableFiltering,
1014 0);
Segher Boessenkool706c8c92006-03-30 14:49:40 +02001015 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 ibmveth_error_printk("h_multicast_ctrl rc=%ld when entering promisc mode\n", lpar_rc);
1017 }
1018 } else {
1019 struct dev_mc_list *mclist = netdev->mc_list;
1020 int i;
1021 /* clear the filter table & disable filtering */
1022 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1023 IbmVethMcastEnableRecv |
1024 IbmVethMcastDisableFiltering |
1025 IbmVethMcastClearFilterTable,
1026 0);
Segher Boessenkool706c8c92006-03-30 14:49:40 +02001027 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 ibmveth_error_printk("h_multicast_ctrl rc=%ld when attempting to clear filter table\n", lpar_rc);
1029 }
1030 /* add the addresses to the filter table */
1031 for(i = 0; i < netdev->mc_count; ++i, mclist = mclist->next) {
1032 // add the multicast address to the filter table
1033 unsigned long mcast_addr = 0;
1034 memcpy(((char *)&mcast_addr)+2, mclist->dmi_addr, 6);
1035 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1036 IbmVethMcastAddFilter,
1037 mcast_addr);
Segher Boessenkool706c8c92006-03-30 14:49:40 +02001038 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 ibmveth_error_printk("h_multicast_ctrl rc=%ld when adding an entry to the filter table\n", lpar_rc);
1040 }
1041 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /* re-enable filtering */
1044 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1045 IbmVethMcastEnableFiltering,
1046 0);
Segher Boessenkool706c8c92006-03-30 14:49:40 +02001047 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 ibmveth_error_printk("h_multicast_ctrl rc=%ld when enabling filtering\n", lpar_rc);
1049 }
1050 }
1051}
1052
1053static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
1054{
Santiago Leonb6d35182005-10-26 10:47:01 -06001055 struct ibmveth_adapter *adapter = dev->priv;
Santiago Leon860f2422006-04-25 11:19:59 -05001056 int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
Brian Kingce6eea52007-06-08 14:05:17 -05001057 int reinit = 0;
1058 int i, rc;
Santiago Leonb6d35182005-10-26 10:47:01 -06001059
Santiago Leon860f2422006-04-25 11:19:59 -05001060 if (new_mtu < IBMVETH_MAX_MTU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return -EINVAL;
Santiago Leonb6d35182005-10-26 10:47:01 -06001062
Brian Kingce6eea52007-06-08 14:05:17 -05001063 for (i = 0; i < IbmVethNumBufferPools; i++)
1064 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size)
1065 break;
1066
1067 if (i == IbmVethNumBufferPools)
1068 return -EINVAL;
1069
Santiago Leon860f2422006-04-25 11:19:59 -05001070 /* Look for an active buffer pool that can hold the new MTU */
Santiago Leonb6d35182005-10-26 10:47:01 -06001071 for(i = 0; i<IbmVethNumBufferPools; i++) {
Brian Kingce6eea52007-06-08 14:05:17 -05001072 if (!adapter->rx_buff_pool[i].active) {
1073 adapter->rx_buff_pool[i].active = 1;
1074 reinit = 1;
1075 }
1076
Santiago Leon860f2422006-04-25 11:19:59 -05001077 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size) {
Brian Kingce6eea52007-06-08 14:05:17 -05001078 if (reinit && netif_running(adapter->netdev)) {
1079 adapter->pool_config = 1;
1080 ibmveth_close(adapter->netdev);
1081 adapter->pool_config = 0;
1082 dev->mtu = new_mtu;
1083 if ((rc = ibmveth_open(adapter->netdev)))
1084 return rc;
1085 } else
1086 dev->mtu = new_mtu;
Santiago Leon860f2422006-04-25 11:19:59 -05001087 return 0;
Santiago Leonb6d35182005-10-26 10:47:01 -06001088 }
Santiago Leonb6d35182005-10-26 10:47:01 -06001089 }
Santiago Leon860f2422006-04-25 11:19:59 -05001090 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Santiago Leon6b422372006-10-03 12:24:28 -05001093#ifdef CONFIG_NET_POLL_CONTROLLER
1094static void ibmveth_poll_controller(struct net_device *dev)
1095{
1096 ibmveth_replenish_task(dev->priv);
Andrew Morton5f771132006-10-10 14:33:30 -07001097 ibmveth_interrupt(dev->irq, dev);
Santiago Leon6b422372006-10-03 12:24:28 -05001098}
1099#endif
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
1102{
Santiago Leonb6d35182005-10-26 10:47:01 -06001103 int rc, i;
Brian Kingf4ff2872007-09-15 13:36:07 -07001104 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 struct net_device *netdev;
Mariusz Kozlowski9dc83af2007-08-06 23:44:03 +02001106 struct ibmveth_adapter *adapter;
Brian King79ef4a42007-08-17 09:16:56 -05001107 u64 set_attr, ret_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 unsigned char *mac_addr_p;
1110 unsigned int *mcastFilterSize_p;
1111
1112
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001113 ibmveth_debug_printk_no_adapter("entering ibmveth_probe for UA 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 dev->unit_address);
1115
Michael Ellerman493a6842007-04-17 13:12:55 +10001116 mac_addr_p = (unsigned char *) vio_get_attribute(dev,
1117 VETH_MAC_ADDR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if(!mac_addr_p) {
1119 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find VETH_MAC_ADDR "
1120 "attribute\n", __FILE__, __LINE__);
1121 return 0;
1122 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001123
Michael Ellerman493a6842007-04-17 13:12:55 +10001124 mcastFilterSize_p = (unsigned int *) vio_get_attribute(dev,
1125 VETH_MCAST_FILTER_SIZE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if(!mcastFilterSize_p) {
1127 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find "
1128 "VETH_MCAST_FILTER_SIZE attribute\n",
1129 __FILE__, __LINE__);
1130 return 0;
1131 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
1134
1135 if(!netdev)
1136 return -ENOMEM;
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 adapter = netdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 dev->dev.driver_data = netdev;
1140
1141 adapter->vdev = dev;
1142 adapter->netdev = netdev;
1143 adapter->mcastFilterSize= *mcastFilterSize_p;
Santiago Leon860f2422006-04-25 11:19:59 -05001144 adapter->pool_config = 0;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001145
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001146 netif_napi_add(netdev, &adapter->napi, ibmveth_poll, 16);
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /* Some older boxes running PHYP non-natively have an OF that
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001149 returns a 8-byte local-mac-address field (and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 2 bytes have to be ignored) while newer boxes' OF return
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001151 a 6-byte field. Note that IEEE 1275 specifies that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 local-mac-address must be a 6-byte field.
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001153 The RPA doc specifies that the first byte must be 10b, so
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 we'll just look for it to solve this 8 vs. 6 byte field issue */
1155
1156 if ((*mac_addr_p & 0x3) != 0x02)
1157 mac_addr_p += 2;
1158
1159 adapter->mac_addr = 0;
1160 memcpy(&adapter->mac_addr, mac_addr_p, 6);
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 netdev->irq = dev->irq;
1163 netdev->open = ibmveth_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 netdev->stop = ibmveth_close;
1165 netdev->hard_start_xmit = ibmveth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 netdev->set_multicast_list = ibmveth_set_multicast_list;
1167 netdev->do_ioctl = ibmveth_ioctl;
1168 netdev->ethtool_ops = &netdev_ethtool_ops;
1169 netdev->change_mtu = ibmveth_change_mtu;
1170 SET_NETDEV_DEV(netdev, &dev->dev);
Santiago Leon6b422372006-10-03 12:24:28 -05001171#ifdef CONFIG_NET_POLL_CONTROLLER
1172 netdev->poll_controller = ibmveth_poll_controller;
1173#endif
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001174 netdev->features |= NETIF_F_LLTX;
Santiago Leon60296d92005-10-26 10:47:16 -06001175 spin_lock_init(&adapter->stats_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 memcpy(&netdev->dev_addr, &adapter->mac_addr, netdev->addr_len);
1178
Santiago Leon860f2422006-04-25 11:19:59 -05001179 for(i = 0; i<IbmVethNumBufferPools; i++) {
1180 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
Greg Kroah-Hartman8dde2a92007-12-17 15:54:39 -04001181 int error;
1182
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001183 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1184 pool_count[i], pool_size[i],
Santiago Leon860f2422006-04-25 11:19:59 -05001185 pool_active[i]);
Greg Kroah-Hartman8dde2a92007-12-17 15:54:39 -04001186 error = kobject_init_and_add(kobj, &ktype_veth_pool,
1187 &dev->dev.kobj, "pool%d", i);
1188 if (!error)
1189 kobject_uevent(kobj, KOBJ_ADD);
Santiago Leon860f2422006-04-25 11:19:59 -05001190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 ibmveth_debug_printk("adapter @ 0x%p\n", adapter);
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 adapter->buffer_list_dma = DMA_ERROR_CODE;
1195 adapter->filter_list_dma = DMA_ERROR_CODE;
1196 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 ibmveth_debug_printk("registering netdev...\n");
1199
Brian King79ef4a42007-08-17 09:16:56 -05001200 ret = h_illan_attributes(dev->unit_address, 0, 0, &ret_attr);
Brian Kingf4ff2872007-09-15 13:36:07 -07001201
Brian King79ef4a42007-08-17 09:16:56 -05001202 if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
1203 !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
1204 (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
1205 set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Brian Kingf4ff2872007-09-15 13:36:07 -07001206
Brian King79ef4a42007-08-17 09:16:56 -05001207 ret = h_illan_attributes(dev->unit_address, 0, set_attr, &ret_attr);
Brian Kingf4ff2872007-09-15 13:36:07 -07001208
Brian King5fc7e012007-08-17 09:16:31 -05001209 if (ret == H_SUCCESS) {
1210 adapter->rx_csum = 1;
Brian Kingf4ff2872007-09-15 13:36:07 -07001211 netdev->features |= NETIF_F_IP_CSUM;
Brian King5fc7e012007-08-17 09:16:31 -05001212 } else
Brian King79ef4a42007-08-17 09:16:56 -05001213 ret = h_illan_attributes(dev->unit_address, set_attr, 0, &ret_attr);
Brian Kingf4ff2872007-09-15 13:36:07 -07001214 }
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 rc = register_netdev(netdev);
1217
1218 if(rc) {
1219 ibmveth_debug_printk("failed to register netdev rc=%d\n", rc);
1220 free_netdev(netdev);
1221 return rc;
1222 }
1223
1224 ibmveth_debug_printk("registered\n");
1225
1226 ibmveth_proc_register_adapter(adapter);
1227
1228 return 0;
1229}
1230
1231static int __devexit ibmveth_remove(struct vio_dev *dev)
1232{
1233 struct net_device *netdev = dev->dev.driver_data;
1234 struct ibmveth_adapter *adapter = netdev->priv;
Santiago Leon860f2422006-04-25 11:19:59 -05001235 int i;
1236
1237 for(i = 0; i<IbmVethNumBufferPools; i++)
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001238 kobject_put(&adapter->rx_buff_pool[i].kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 unregister_netdev(netdev);
1241
1242 ibmveth_proc_unregister_adapter(adapter);
1243
1244 free_netdev(netdev);
1245 return 0;
1246}
1247
1248#ifdef CONFIG_PROC_FS
1249static void ibmveth_proc_register_driver(void)
1250{
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001251 ibmveth_proc_dir = proc_mkdir(IBMVETH_PROC_DIR, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (ibmveth_proc_dir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254}
1255
1256static void ibmveth_proc_unregister_driver(void)
1257{
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001258 remove_proc_entry(IBMVETH_PROC_DIR, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
Pavel Emelyanov1119d572008-02-18 13:55:10 +03001261static int ibmveth_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 struct ibmveth_adapter *adapter = seq->private;
1264 char *current_mac = ((char*) &adapter->netdev->dev_addr);
1265 char *firmware_mac = ((char*) &adapter->mac_addr) ;
Joe Perches0795af52007-10-03 17:59:30 -07001266 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 seq_printf(seq, "%s %s\n\n", ibmveth_driver_string, ibmveth_driver_version);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 seq_printf(seq, "Unit Address: 0x%x\n", adapter->vdev->unit_address);
Joe Perches0795af52007-10-03 17:59:30 -07001271 seq_printf(seq, "Current MAC: %s\n", print_mac(mac, current_mac));
1272 seq_printf(seq, "Firmware MAC: %s\n", print_mac(mac, firmware_mac));
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 seq_printf(seq, "\nAdapter Statistics:\n");
Brian King3449a2a2007-08-17 09:16:49 -05001275 seq_printf(seq, " TX: vio_map_single failres: %ld\n", adapter->tx_map_failed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 seq_printf(seq, " send failures: %ld\n", adapter->tx_send_failed);
1277 seq_printf(seq, " RX: replenish task cycles: %ld\n", adapter->replenish_task_cycles);
1278 seq_printf(seq, " alloc_skb_failures: %ld\n", adapter->replenish_no_mem);
1279 seq_printf(seq, " add buffer failures: %ld\n", adapter->replenish_add_buff_failure);
1280 seq_printf(seq, " invalid buffers: %ld\n", adapter->rx_invalid_buffer);
1281 seq_printf(seq, " no buffers: %ld\n", adapter->rx_no_buffer);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return 0;
1284}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286static int ibmveth_proc_open(struct inode *inode, struct file *file)
1287{
Pavel Emelyanov1119d572008-02-18 13:55:10 +03001288 return single_open(file, ibmveth_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001291static const struct file_operations ibmveth_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 .owner = THIS_MODULE,
1293 .open = ibmveth_proc_open,
1294 .read = seq_read,
1295 .llseek = seq_lseek,
Pavel Emelyanov1119d572008-02-18 13:55:10 +03001296 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297};
1298
1299static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1300{
1301 struct proc_dir_entry *entry;
1302 if (ibmveth_proc_dir) {
Santiago Leon03a85d02006-10-03 12:24:39 -05001303 char u_addr[10];
1304 sprintf(u_addr, "%x", adapter->vdev->unit_address);
Denis V. Luneva95609c2008-04-29 01:02:29 -07001305 entry = proc_create_data(u_addr, S_IFREG, ibmveth_proc_dir,
1306 &ibmveth_proc_fops, adapter);
1307 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 ibmveth_error_printk("Cannot create adapter proc entry");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310 return;
1311}
1312
1313static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1314{
1315 if (ibmveth_proc_dir) {
Santiago Leon03a85d02006-10-03 12:24:39 -05001316 char u_addr[10];
1317 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1318 remove_proc_entry(u_addr, ibmveth_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320}
1321
1322#else /* CONFIG_PROC_FS */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001323static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325}
1326
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001327static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329}
1330static void ibmveth_proc_register_driver(void)
1331{
1332}
1333
1334static void ibmveth_proc_unregister_driver(void)
1335{
1336}
1337#endif /* CONFIG_PROC_FS */
1338
Santiago Leon860f2422006-04-25 11:19:59 -05001339static struct attribute veth_active_attr;
1340static struct attribute veth_num_attr;
1341static struct attribute veth_size_attr;
1342
1343static ssize_t veth_pool_show(struct kobject * kobj,
1344 struct attribute * attr, char * buf)
1345{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001346 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001347 struct ibmveth_buff_pool,
1348 kobj);
1349
1350 if (attr == &veth_active_attr)
1351 return sprintf(buf, "%d\n", pool->active);
1352 else if (attr == &veth_num_attr)
1353 return sprintf(buf, "%d\n", pool->size);
1354 else if (attr == &veth_size_attr)
1355 return sprintf(buf, "%d\n", pool->buff_size);
1356 return 0;
1357}
1358
1359static ssize_t veth_pool_store(struct kobject * kobj, struct attribute * attr,
1360const char * buf, size_t count)
1361{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001362 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001363 struct ibmveth_buff_pool,
1364 kobj);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001365 struct net_device *netdev =
Santiago Leon860f2422006-04-25 11:19:59 -05001366 container_of(kobj->parent, struct device, kobj)->driver_data;
1367 struct ibmveth_adapter *adapter = netdev->priv;
1368 long value = simple_strtol(buf, NULL, 10);
1369 long rc;
1370
1371 if (attr == &veth_active_attr) {
1372 if (value && !pool->active) {
Brian King4aa9c932007-06-08 14:05:16 -05001373 if (netif_running(netdev)) {
1374 if(ibmveth_alloc_buffer_pool(pool)) {
1375 ibmveth_error_printk("unable to alloc pool\n");
1376 return -ENOMEM;
1377 }
1378 pool->active = 1;
1379 adapter->pool_config = 1;
1380 ibmveth_close(netdev);
1381 adapter->pool_config = 0;
1382 if ((rc = ibmveth_open(netdev)))
1383 return rc;
1384 } else
1385 pool->active = 1;
Santiago Leon860f2422006-04-25 11:19:59 -05001386 } else if (!value && pool->active) {
1387 int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1388 int i;
1389 /* Make sure there is a buffer pool with buffers that
1390 can hold a packet of the size of the MTU */
Brian King76b9cfc2007-08-03 13:55:19 +10001391 for (i = 0; i < IbmVethNumBufferPools; i++) {
Santiago Leon860f2422006-04-25 11:19:59 -05001392 if (pool == &adapter->rx_buff_pool[i])
1393 continue;
1394 if (!adapter->rx_buff_pool[i].active)
1395 continue;
Brian King76b9cfc2007-08-03 13:55:19 +10001396 if (mtu <= adapter->rx_buff_pool[i].buff_size)
1397 break;
Santiago Leon860f2422006-04-25 11:19:59 -05001398 }
Brian King76b9cfc2007-08-03 13:55:19 +10001399
1400 if (i == IbmVethNumBufferPools) {
Santiago Leon860f2422006-04-25 11:19:59 -05001401 ibmveth_error_printk("no active pool >= MTU\n");
1402 return -EPERM;
1403 }
Brian King76b9cfc2007-08-03 13:55:19 +10001404
1405 pool->active = 0;
1406 if (netif_running(netdev)) {
1407 adapter->pool_config = 1;
1408 ibmveth_close(netdev);
1409 adapter->pool_config = 0;
1410 if ((rc = ibmveth_open(netdev)))
1411 return rc;
1412 }
Santiago Leon860f2422006-04-25 11:19:59 -05001413 }
1414 } else if (attr == &veth_num_attr) {
1415 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT)
1416 return -EINVAL;
1417 else {
Brian King4aa9c932007-06-08 14:05:16 -05001418 if (netif_running(netdev)) {
1419 adapter->pool_config = 1;
1420 ibmveth_close(netdev);
1421 adapter->pool_config = 0;
1422 pool->size = value;
1423 if ((rc = ibmveth_open(netdev)))
1424 return rc;
1425 } else
1426 pool->size = value;
Santiago Leon860f2422006-04-25 11:19:59 -05001427 }
1428 } else if (attr == &veth_size_attr) {
1429 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE)
1430 return -EINVAL;
1431 else {
Brian King4aa9c932007-06-08 14:05:16 -05001432 if (netif_running(netdev)) {
1433 adapter->pool_config = 1;
1434 ibmveth_close(netdev);
1435 adapter->pool_config = 0;
1436 pool->buff_size = value;
1437 if ((rc = ibmveth_open(netdev)))
1438 return rc;
1439 } else
1440 pool->buff_size = value;
Santiago Leon860f2422006-04-25 11:19:59 -05001441 }
1442 }
1443
1444 /* kick the interrupt handler to allocate/deallocate pools */
David Howells7d12e782006-10-05 14:55:46 +01001445 ibmveth_interrupt(netdev->irq, netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001446 return count;
1447}
1448
1449
1450#define ATTR(_name, _mode) \
1451 struct attribute veth_##_name##_attr = { \
Tejun Heo7b595752007-06-14 03:45:17 +09001452 .name = __stringify(_name), .mode = _mode, \
Santiago Leon860f2422006-04-25 11:19:59 -05001453 };
1454
1455static ATTR(active, 0644);
1456static ATTR(num, 0644);
1457static ATTR(size, 0644);
1458
1459static struct attribute * veth_pool_attrs[] = {
1460 &veth_active_attr,
1461 &veth_num_attr,
1462 &veth_size_attr,
1463 NULL,
1464};
1465
1466static struct sysfs_ops veth_pool_ops = {
1467 .show = veth_pool_show,
1468 .store = veth_pool_store,
1469};
1470
1471static struct kobj_type ktype_veth_pool = {
1472 .release = NULL,
1473 .sysfs_ops = &veth_pool_ops,
1474 .default_attrs = veth_pool_attrs,
1475};
1476
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478static struct vio_device_id ibmveth_device_table[] __devinitdata= {
1479 { "network", "IBM,l-lan"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +10001480 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1483
1484static struct vio_driver ibmveth_driver = {
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001485 .id_table = ibmveth_device_table,
1486 .probe = ibmveth_probe,
1487 .remove = ibmveth_remove,
1488 .driver = {
1489 .name = ibmveth_driver_name,
Stephen Rothwell915124d2005-10-24 15:12:22 +10001490 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492};
1493
1494static int __init ibmveth_module_init(void)
1495{
1496 ibmveth_printk("%s: %s %s\n", ibmveth_driver_name, ibmveth_driver_string, ibmveth_driver_version);
1497
1498 ibmveth_proc_register_driver();
1499
1500 return vio_register_driver(&ibmveth_driver);
1501}
1502
1503static void __exit ibmveth_module_exit(void)
1504{
1505 vio_unregister_driver(&ibmveth_driver);
1506 ibmveth_proc_unregister_driver();
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001507}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509module_init(ibmveth_module_init);
1510module_exit(ibmveth_module_exit);