blob: 57772bebff56336cb50c3bde81c0823d13210787 [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/semaphore.h>
53#include <asm/hvcall.h>
54#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/vio.h>
56#include <asm/uaccess.h>
57#include <linux/seq_file.h>
58
59#include "ibmveth.h"
60
Anton Blanchard6af37fa2005-11-11 14:02:04 +110061#undef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#define ibmveth_printk(fmt, args...) \
Olof Johanssonf98baff2006-04-12 15:22:29 -050064 printk(KERN_DEBUG "%s: " fmt, __FILE__, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#define ibmveth_error_printk(fmt, args...) \
67 printk(KERN_ERR "(%s:%3.3d ua:%x) ERROR: " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
68
69#ifdef DEBUG
70#define ibmveth_debug_printk_no_adapter(fmt, args...) \
71 printk(KERN_DEBUG "(%s:%3.3d): " fmt, __FILE__, __LINE__ , ## args)
72#define ibmveth_debug_printk(fmt, args...) \
73 printk(KERN_DEBUG "(%s:%3.3d ua:%x): " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
74#define ibmveth_assert(expr) \
75 if(!(expr)) { \
76 printk(KERN_DEBUG "assertion failed (%s:%3.3d ua:%x): %s\n", __FILE__, __LINE__, adapter->vdev->unit_address, #expr); \
77 BUG(); \
78 }
79#else
80#define ibmveth_debug_printk_no_adapter(fmt, args...)
81#define ibmveth_debug_printk(fmt, args...)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -040082#define ibmveth_assert(expr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#endif
84
85static int ibmveth_open(struct net_device *dev);
86static int ibmveth_close(struct net_device *dev);
87static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070088static int ibmveth_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void ibmveth_set_multicast_list(struct net_device *dev);
91static int ibmveth_change_mtu(struct net_device *dev, int new_mtu);
92static void ibmveth_proc_register_driver(void);
93static void ibmveth_proc_unregister_driver(void);
94static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter);
95static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter);
David Howells7d12e782006-10-05 14:55:46 +010096static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
Michael Ellerman493a6842007-04-17 13:12:55 +100097static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
Santiago Leon860f2422006-04-25 11:19:59 -050098static struct kobj_type ktype_veth_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#ifdef CONFIG_PROC_FS
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200101#define IBMVETH_PROC_DIR "ibmveth"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static struct proc_dir_entry *ibmveth_proc_dir;
103#endif
104
105static const char ibmveth_driver_name[] = "ibmveth";
106static const char ibmveth_driver_string[] = "IBM i/pSeries Virtual Ethernet Driver";
107#define ibmveth_driver_version "1.03"
108
109MODULE_AUTHOR("Santiago Leon <santil@us.ibm.com>");
110MODULE_DESCRIPTION("IBM i/pSeries Virtual Ethernet Driver");
111MODULE_LICENSE("GPL");
112MODULE_VERSION(ibmveth_driver_version);
113
Brian Kingddbb4de2007-08-17 09:16:43 -0500114struct ibmveth_stat {
115 char name[ETH_GSTRING_LEN];
116 int offset;
117};
118
119#define IBMVETH_STAT_OFF(stat) offsetof(struct ibmveth_adapter, stat)
120#define IBMVETH_GET_STAT(a, off) *((u64 *)(((unsigned long)(a)) + off))
121
122struct ibmveth_stat ibmveth_stats[] = {
123 { "replenish_task_cycles", IBMVETH_STAT_OFF(replenish_task_cycles) },
124 { "replenish_no_mem", IBMVETH_STAT_OFF(replenish_no_mem) },
125 { "replenish_add_buff_failure", IBMVETH_STAT_OFF(replenish_add_buff_failure) },
126 { "replenish_add_buff_success", IBMVETH_STAT_OFF(replenish_add_buff_success) },
127 { "rx_invalid_buffer", IBMVETH_STAT_OFF(rx_invalid_buffer) },
128 { "rx_no_buffer", IBMVETH_STAT_OFF(rx_no_buffer) },
Brian Kingddbb4de2007-08-17 09:16:43 -0500129 { "tx_map_failed", IBMVETH_STAT_OFF(tx_map_failed) },
130 { "tx_send_failed", IBMVETH_STAT_OFF(tx_send_failed) },
131};
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/* simple methods of getting data from the current rxq entry */
Brian King79ef4a42007-08-17 09:16:56 -0500134static inline u32 ibmveth_rxq_flags(struct ibmveth_adapter *adapter)
135{
136 return adapter->rx_queue.queue_addr[adapter->rx_queue.index].flags_off;
137}
138
139static inline int ibmveth_rxq_toggle(struct ibmveth_adapter *adapter)
140{
141 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_TOGGLE) >> IBMVETH_RXQ_TOGGLE_SHIFT;
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
145{
Brian King79ef4a42007-08-17 09:16:56 -0500146 return (ibmveth_rxq_toggle(adapter) == adapter->rx_queue.toggle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
150{
Brian King79ef4a42007-08-17 09:16:56 -0500151 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
154static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
155{
Brian King79ef4a42007-08-17 09:16:56 -0500156 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_OFF_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
160{
161 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
162}
163
Brian Kingf4ff2872007-09-15 13:36:07 -0700164static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
165{
Brian King79ef4a42007-08-17 09:16:56 -0500166 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_CSUM_GOOD);
Brian Kingf4ff2872007-09-15 13:36:07 -0700167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/* setup the initial settings for a buffer pool */
Santiago Leon860f2422006-04-25 11:19:59 -0500170static 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 -0700171{
172 pool->size = pool_size;
173 pool->index = pool_index;
174 pool->buff_size = buff_size;
175 pool->threshold = pool_size / 2;
Santiago Leon860f2422006-04-25 11:19:59 -0500176 pool->active = pool_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179/* allocate and setup an buffer pool - called during open */
180static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
181{
182 int i;
183
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400184 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 if(!pool->free_map) {
187 return -1;
188 }
189
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400190 pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if(!pool->dma_addr) {
192 kfree(pool->free_map);
193 pool->free_map = NULL;
194 return -1;
195 }
196
197 pool->skbuff = kmalloc(sizeof(void*) * pool->size, GFP_KERNEL);
198
199 if(!pool->skbuff) {
200 kfree(pool->dma_addr);
201 pool->dma_addr = NULL;
202
203 kfree(pool->free_map);
204 pool->free_map = NULL;
205 return -1;
206 }
207
208 memset(pool->skbuff, 0, sizeof(void*) * pool->size);
209 memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
210
211 for(i = 0; i < pool->size; ++i) {
212 pool->free_map[i] = i;
213 }
214
215 atomic_set(&pool->available, 0);
216 pool->producer_index = 0;
217 pool->consumer_index = 0;
218
219 return 0;
220}
221
222/* replenish the buffers for a pool. note that we don't need to
223 * skb_reserve these since they are used for incoming...
224 */
225static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
226{
227 u32 i;
228 u32 count = pool->size - atomic_read(&pool->available);
229 u32 buffers_added = 0;
230
231 mb();
232
233 for(i = 0; i < count; ++i) {
234 struct sk_buff *skb;
235 unsigned int free_index, index;
236 u64 correlator;
237 union ibmveth_buf_desc desc;
238 unsigned long lpar_rc;
239 dma_addr_t dma_addr;
240
241 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
242
243 if(!skb) {
244 ibmveth_debug_printk("replenish: unable to allocate skb\n");
245 adapter->replenish_no_mem++;
246 break;
247 }
248
David Gibson047a66d2006-10-21 10:24:13 -0700249 free_index = pool->consumer_index;
250 pool->consumer_index = (pool->consumer_index + 1) % pool->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 index = pool->free_map[free_index];
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 ibmveth_assert(index != IBM_VETH_INVALID_MAP);
254 ibmveth_assert(pool->skbuff[index] == NULL);
255
256 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
257 pool->buff_size, DMA_FROM_DEVICE);
258
259 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
260 pool->dma_addr[index] = dma_addr;
261 pool->skbuff[index] = skb;
262
263 correlator = ((u64)pool->index << 32) | index;
264 *(u64*)skb->data = correlator;
265
Brian King79ef4a42007-08-17 09:16:56 -0500266 desc.fields.flags_len = IBMVETH_BUF_VALID | pool->buff_size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400267 desc.fields.address = dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400270
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200271 if(lpar_rc != H_SUCCESS) {
Santiago Leon82702d32005-10-26 10:47:23 -0600272 pool->free_map[free_index] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 pool->skbuff[index] = NULL;
Santiago Leon751ae212006-10-03 12:24:45 -0500274 if (pool->consumer_index == 0)
275 pool->consumer_index = pool->size - 1;
276 else
277 pool->consumer_index--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 dma_unmap_single(&adapter->vdev->dev,
279 pool->dma_addr[index], pool->buff_size,
280 DMA_FROM_DEVICE);
281 dev_kfree_skb_any(skb);
282 adapter->replenish_add_buff_failure++;
283 break;
284 } else {
285 buffers_added++;
286 adapter->replenish_add_buff_success++;
287 }
288 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 mb();
291 atomic_add(buffers_added, &(pool->available));
292}
293
Santiago Leone2adbcb2005-10-26 10:47:08 -0600294/* replenish routine */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400295static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Santiago Leonb6d35182005-10-26 10:47:01 -0600297 int i;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 adapter->replenish_task_cycles++;
300
Santiago Leonb6d35182005-10-26 10:47:01 -0600301 for(i = 0; i < IbmVethNumBufferPools; i++)
302 if(adapter->rx_buff_pool[i].active)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400303 ibmveth_replenish_buffer_pool(adapter,
Santiago Leonb6d35182005-10-26 10:47:01 -0600304 &adapter->rx_buff_pool[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309/* empty and free ana buffer pool - also used to do cleanup in error paths */
310static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
311{
312 int i;
313
Jesper Juhlb4558ea2005-10-28 16:53:13 -0400314 kfree(pool->free_map);
315 pool->free_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 if(pool->skbuff && pool->dma_addr) {
318 for(i = 0; i < pool->size; ++i) {
319 struct sk_buff *skb = pool->skbuff[i];
320 if(skb) {
321 dma_unmap_single(&adapter->vdev->dev,
322 pool->dma_addr[i],
323 pool->buff_size,
324 DMA_FROM_DEVICE);
325 dev_kfree_skb_any(skb);
326 pool->skbuff[i] = NULL;
327 }
328 }
329 }
330
331 if(pool->dma_addr) {
332 kfree(pool->dma_addr);
333 pool->dma_addr = NULL;
334 }
335
336 if(pool->skbuff) {
337 kfree(pool->skbuff);
338 pool->skbuff = NULL;
339 }
340}
341
342/* remove a buffer from a pool */
343static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter, u64 correlator)
344{
345 unsigned int pool = correlator >> 32;
346 unsigned int index = correlator & 0xffffffffUL;
347 unsigned int free_index;
348 struct sk_buff *skb;
349
350 ibmveth_assert(pool < IbmVethNumBufferPools);
351 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
352
353 skb = adapter->rx_buff_pool[pool].skbuff[index];
354
355 ibmveth_assert(skb != NULL);
356
357 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
358
359 dma_unmap_single(&adapter->vdev->dev,
360 adapter->rx_buff_pool[pool].dma_addr[index],
361 adapter->rx_buff_pool[pool].buff_size,
362 DMA_FROM_DEVICE);
363
David Gibson047a66d2006-10-21 10:24:13 -0700364 free_index = adapter->rx_buff_pool[pool].producer_index;
365 adapter->rx_buff_pool[pool].producer_index
366 = (adapter->rx_buff_pool[pool].producer_index + 1)
367 % adapter->rx_buff_pool[pool].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 adapter->rx_buff_pool[pool].free_map[free_index] = index;
369
370 mb();
371
372 atomic_dec(&(adapter->rx_buff_pool[pool].available));
373}
374
375/* get the current buffer on the rx queue */
376static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
377{
378 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
379 unsigned int pool = correlator >> 32;
380 unsigned int index = correlator & 0xffffffffUL;
381
382 ibmveth_assert(pool < IbmVethNumBufferPools);
383 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
384
385 return adapter->rx_buff_pool[pool].skbuff[index];
386}
387
388/* recycle the current buffer on the rx queue */
389static void ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
390{
391 u32 q_index = adapter->rx_queue.index;
392 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
393 unsigned int pool = correlator >> 32;
394 unsigned int index = correlator & 0xffffffffUL;
395 union ibmveth_buf_desc desc;
396 unsigned long lpar_rc;
397
398 ibmveth_assert(pool < IbmVethNumBufferPools);
399 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
400
Santiago Leonb6d35182005-10-26 10:47:01 -0600401 if(!adapter->rx_buff_pool[pool].active) {
402 ibmveth_rxq_harvest_buffer(adapter);
403 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
404 return;
405 }
406
Brian King79ef4a42007-08-17 09:16:56 -0500407 desc.fields.flags_len = IBMVETH_BUF_VALID |
408 adapter->rx_buff_pool[pool].buff_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
410
411 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400412
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200413 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 ibmveth_debug_printk("h_add_logical_lan_buffer failed during recycle rc=%ld", lpar_rc);
415 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
416 }
417
418 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
419 adapter->rx_queue.index = 0;
420 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
421 }
422}
423
Michael Ellerman493a6842007-04-17 13:12:55 +1000424static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
427
428 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
429 adapter->rx_queue.index = 0;
430 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
431 }
432}
433
434static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
435{
Santiago Leonb6d35182005-10-26 10:47:01 -0600436 int i;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if(adapter->buffer_list_addr != NULL) {
439 if(!dma_mapping_error(adapter->buffer_list_dma)) {
440 dma_unmap_single(&adapter->vdev->dev,
441 adapter->buffer_list_dma, 4096,
442 DMA_BIDIRECTIONAL);
443 adapter->buffer_list_dma = DMA_ERROR_CODE;
444 }
445 free_page((unsigned long)adapter->buffer_list_addr);
446 adapter->buffer_list_addr = NULL;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 if(adapter->filter_list_addr != NULL) {
450 if(!dma_mapping_error(adapter->filter_list_dma)) {
451 dma_unmap_single(&adapter->vdev->dev,
452 adapter->filter_list_dma, 4096,
453 DMA_BIDIRECTIONAL);
454 adapter->filter_list_dma = DMA_ERROR_CODE;
455 }
456 free_page((unsigned long)adapter->filter_list_addr);
457 adapter->filter_list_addr = NULL;
458 }
459
460 if(adapter->rx_queue.queue_addr != NULL) {
461 if(!dma_mapping_error(adapter->rx_queue.queue_dma)) {
462 dma_unmap_single(&adapter->vdev->dev,
463 adapter->rx_queue.queue_dma,
464 adapter->rx_queue.queue_len,
465 DMA_BIDIRECTIONAL);
466 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
467 }
468 kfree(adapter->rx_queue.queue_addr);
469 adapter->rx_queue.queue_addr = NULL;
470 }
471
Santiago Leonb6d35182005-10-26 10:47:01 -0600472 for(i = 0; i<IbmVethNumBufferPools; i++)
Santiago Leon860f2422006-04-25 11:19:59 -0500473 if (adapter->rx_buff_pool[i].active)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400474 ibmveth_free_buffer_pool(adapter,
Santiago Leon860f2422006-04-25 11:19:59 -0500475 &adapter->rx_buff_pool[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500478static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
479 union ibmveth_buf_desc rxq_desc, u64 mac_address)
480{
481 int rc, try_again = 1;
482
483 /* After a kexec the adapter will still be open, so our attempt to
484 * open it will fail. So if we get a failure we free the adapter and
485 * try again, but only once. */
486retry:
487 rc = h_register_logical_lan(adapter->vdev->unit_address,
488 adapter->buffer_list_dma, rxq_desc.desc,
489 adapter->filter_list_dma, mac_address);
490
491 if (rc != H_SUCCESS && try_again) {
492 do {
493 rc = h_free_logical_lan(adapter->vdev->unit_address);
494 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
495
496 try_again = 0;
497 goto retry;
498 }
499
500 return rc;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static int ibmveth_open(struct net_device *netdev)
504{
505 struct ibmveth_adapter *adapter = netdev->priv;
506 u64 mac_address = 0;
Santiago Leonb6d35182005-10-26 10:47:01 -0600507 int rxq_entries = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 unsigned long lpar_rc;
509 int rc;
510 union ibmveth_buf_desc rxq_desc;
Santiago Leonb6d35182005-10-26 10:47:01 -0600511 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 ibmveth_debug_printk("open starting\n");
514
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700515 napi_enable(&adapter->napi);
516
Santiago Leonb6d35182005-10-26 10:47:01 -0600517 for(i = 0; i<IbmVethNumBufferPools; i++)
518 rxq_entries += adapter->rx_buff_pool[i].size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
521 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if(!adapter->buffer_list_addr || !adapter->filter_list_addr) {
524 ibmveth_error_printk("unable to allocate filter or buffer list pages\n");
525 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700526 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return -ENOMEM;
528 }
529
530 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) * rxq_entries;
531 adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len, GFP_KERNEL);
532
533 if(!adapter->rx_queue.queue_addr) {
534 ibmveth_error_printk("unable to allocate rx queue pages\n");
535 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700536 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return -ENOMEM;
538 }
539
540 adapter->buffer_list_dma = dma_map_single(&adapter->vdev->dev,
541 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
542 adapter->filter_list_dma = dma_map_single(&adapter->vdev->dev,
543 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
544 adapter->rx_queue.queue_dma = dma_map_single(&adapter->vdev->dev,
545 adapter->rx_queue.queue_addr,
546 adapter->rx_queue.queue_len, DMA_BIDIRECTIONAL);
547
548 if((dma_mapping_error(adapter->buffer_list_dma) ) ||
549 (dma_mapping_error(adapter->filter_list_dma)) ||
550 (dma_mapping_error(adapter->rx_queue.queue_dma))) {
551 ibmveth_error_printk("unable to map filter or buffer list pages\n");
552 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700553 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return -ENOMEM;
555 }
556
557 adapter->rx_queue.index = 0;
558 adapter->rx_queue.num_slots = rxq_entries;
559 adapter->rx_queue.toggle = 1;
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
562 mac_address = mac_address >> 16;
563
Brian King79ef4a42007-08-17 09:16:56 -0500564 rxq_desc.fields.flags_len = IBMVETH_BUF_VALID | adapter->rx_queue.queue_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
566
567 ibmveth_debug_printk("buffer list @ 0x%p\n", adapter->buffer_list_addr);
568 ibmveth_debug_printk("filter list @ 0x%p\n", adapter->filter_list_addr);
569 ibmveth_debug_printk("receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
570
Santiago Leon4347ef12006-10-03 12:24:34 -0500571 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
572
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500573 lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200575 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 ibmveth_error_printk("h_register_logical_lan failed with %ld\n", lpar_rc);
Stephen Rothwell8168f902005-10-25 16:56:43 +1000577 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 -0700578 adapter->buffer_list_dma,
579 adapter->filter_list_dma,
580 rxq_desc.desc,
581 mac_address);
582 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700583 napi_disable(&adapter->napi);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400584 return -ENONET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586
Santiago Leon860f2422006-04-25 11:19:59 -0500587 for(i = 0; i<IbmVethNumBufferPools; i++) {
588 if(!adapter->rx_buff_pool[i].active)
589 continue;
590 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
591 ibmveth_error_printk("unable to alloc pool\n");
592 adapter->rx_buff_pool[i].active = 0;
593 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700594 napi_disable(&adapter->napi);
Santiago Leon860f2422006-04-25 11:19:59 -0500595 return -ENOMEM ;
596 }
597 }
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 ibmveth_debug_printk("registering irq 0x%x\n", netdev->irq);
600 if((rc = request_irq(netdev->irq, &ibmveth_interrupt, 0, netdev->name, netdev)) != 0) {
601 ibmveth_error_printk("unable to request irq 0x%x, rc %d\n", netdev->irq, rc);
602 do {
603 rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200604 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 ibmveth_cleanup(adapter);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700607 napi_disable(&adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return rc;
609 }
610
Santiago Leone2adbcb2005-10-26 10:47:08 -0600611 ibmveth_debug_printk("initial replenish cycle\n");
David Howells7d12e782006-10-05 14:55:46 +0100612 ibmveth_interrupt(netdev->irq, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Santiago Leone2adbcb2005-10-26 10:47:08 -0600614 netif_start_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 ibmveth_debug_printk("open complete\n");
617
618 return 0;
619}
620
621static int ibmveth_close(struct net_device *netdev)
622{
623 struct ibmveth_adapter *adapter = netdev->priv;
624 long lpar_rc;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 ibmveth_debug_printk("close starting\n");
627
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700628 napi_disable(&adapter->napi);
629
Santiago Leon860f2422006-04-25 11:19:59 -0500630 if (!adapter->pool_config)
631 netif_stop_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 free_irq(netdev->irq, netdev);
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 do {
636 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200637 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200639 if(lpar_rc != H_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 {
641 ibmveth_error_printk("h_free_logical_lan failed with %lx, continuing with close\n",
642 lpar_rc);
643 }
644
645 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
646
647 ibmveth_cleanup(adapter);
648
649 ibmveth_debug_printk("close complete\n");
650
651 return 0;
652}
653
654static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
655 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
656 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | ADVERTISED_FIBRE);
657 cmd->speed = SPEED_1000;
658 cmd->duplex = DUPLEX_FULL;
659 cmd->port = PORT_FIBRE;
660 cmd->phy_address = 0;
661 cmd->transceiver = XCVR_INTERNAL;
662 cmd->autoneg = AUTONEG_ENABLE;
663 cmd->maxtxpkt = 0;
664 cmd->maxrxpkt = 1;
665 return 0;
666}
667
668static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info) {
669 strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
670 strncpy(info->version, ibmveth_driver_version, sizeof(info->version) - 1);
671}
672
673static u32 netdev_get_link(struct net_device *dev) {
674 return 1;
675}
676
Brian King5fc7e012007-08-17 09:16:31 -0500677static void ibmveth_set_rx_csum_flags(struct net_device *dev, u32 data)
678{
679 struct ibmveth_adapter *adapter = dev->priv;
680
681 if (data)
682 adapter->rx_csum = 1;
683 else {
684 /*
685 * Since the ibmveth firmware interface does not have the concept of
686 * separate tx/rx checksum offload enable, if rx checksum is disabled
687 * we also have to disable tx checksum offload. Once we disable rx
688 * checksum offload, we are no longer allowed to send tx buffers that
689 * are not properly checksummed.
690 */
691 adapter->rx_csum = 0;
692 dev->features &= ~NETIF_F_IP_CSUM;
693 }
694}
695
696static void ibmveth_set_tx_csum_flags(struct net_device *dev, u32 data)
697{
698 struct ibmveth_adapter *adapter = dev->priv;
699
700 if (data) {
701 dev->features |= NETIF_F_IP_CSUM;
702 adapter->rx_csum = 1;
703 } else
704 dev->features &= ~NETIF_F_IP_CSUM;
705}
706
707static int ibmveth_set_csum_offload(struct net_device *dev, u32 data,
708 void (*done) (struct net_device *, u32))
709{
710 struct ibmveth_adapter *adapter = dev->priv;
Brian King79ef4a42007-08-17 09:16:56 -0500711 u64 set_attr, clr_attr, ret_attr;
Brian King5fc7e012007-08-17 09:16:31 -0500712 long ret;
713 int rc1 = 0, rc2 = 0;
714 int restart = 0;
715
716 if (netif_running(dev)) {
717 restart = 1;
718 adapter->pool_config = 1;
719 ibmveth_close(dev);
720 adapter->pool_config = 0;
721 }
722
Brian King79ef4a42007-08-17 09:16:56 -0500723 set_attr = 0;
724 clr_attr = 0;
Brian King5fc7e012007-08-17 09:16:31 -0500725
726 if (data)
Brian King79ef4a42007-08-17 09:16:56 -0500727 set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Brian King5fc7e012007-08-17 09:16:31 -0500728 else
Brian King79ef4a42007-08-17 09:16:56 -0500729 clr_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Brian King5fc7e012007-08-17 09:16:31 -0500730
Brian King79ef4a42007-08-17 09:16:56 -0500731 ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500732
Brian King79ef4a42007-08-17 09:16:56 -0500733 if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
734 !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
735 (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
736 ret = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
737 set_attr, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500738
739 if (ret != H_SUCCESS) {
740 rc1 = -EIO;
741 ibmveth_error_printk("unable to change checksum offload settings."
742 " %d rc=%ld\n", data, ret);
743
744 ret = h_illan_attributes(adapter->vdev->unit_address,
Brian King79ef4a42007-08-17 09:16:56 -0500745 set_attr, clr_attr, &ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500746 } else
747 done(dev, data);
748 } else {
749 rc1 = -EIO;
750 ibmveth_error_printk("unable to change checksum offload settings."
Brian King79ef4a42007-08-17 09:16:56 -0500751 " %d rc=%ld ret_attr=%lx\n", data, ret, ret_attr);
Brian King5fc7e012007-08-17 09:16:31 -0500752 }
753
754 if (restart)
755 rc2 = ibmveth_open(dev);
756
757 return rc1 ? rc1 : rc2;
758}
759
760static int ibmveth_set_rx_csum(struct net_device *dev, u32 data)
761{
762 struct ibmveth_adapter *adapter = dev->priv;
763
764 if ((data && adapter->rx_csum) || (!data && !adapter->rx_csum))
765 return 0;
766
767 return ibmveth_set_csum_offload(dev, data, ibmveth_set_rx_csum_flags);
768}
769
770static int ibmveth_set_tx_csum(struct net_device *dev, u32 data)
771{
772 struct ibmveth_adapter *adapter = dev->priv;
773 int rc = 0;
774
775 if (data && (dev->features & NETIF_F_IP_CSUM))
776 return 0;
777 if (!data && !(dev->features & NETIF_F_IP_CSUM))
778 return 0;
779
780 if (data && !adapter->rx_csum)
781 rc = ibmveth_set_csum_offload(dev, data, ibmveth_set_tx_csum_flags);
782 else
783 ibmveth_set_tx_csum_flags(dev, data);
784
785 return rc;
786}
787
788static u32 ibmveth_get_rx_csum(struct net_device *dev)
789{
790 struct ibmveth_adapter *adapter = dev->priv;
791 return adapter->rx_csum;
792}
793
Brian Kingddbb4de2007-08-17 09:16:43 -0500794static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
795{
796 int i;
797
798 if (stringset != ETH_SS_STATS)
799 return;
800
801 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
802 memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
803}
804
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700805static int ibmveth_get_sset_count(struct net_device *dev, int sset)
Brian Kingddbb4de2007-08-17 09:16:43 -0500806{
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700807 switch (sset) {
808 case ETH_SS_STATS:
809 return ARRAY_SIZE(ibmveth_stats);
810 default:
811 return -EOPNOTSUPP;
812 }
Brian Kingddbb4de2007-08-17 09:16:43 -0500813}
814
815static void ibmveth_get_ethtool_stats(struct net_device *dev,
816 struct ethtool_stats *stats, u64 *data)
817{
818 int i;
819 struct ibmveth_adapter *adapter = dev->priv;
820
821 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
822 data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
823}
824
Jeff Garzik7282d492006-09-13 14:30:00 -0400825static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 .get_drvinfo = netdev_get_drvinfo,
827 .get_settings = netdev_get_settings,
828 .get_link = netdev_get_link,
Brian King5fc7e012007-08-17 09:16:31 -0500829 .set_tx_csum = ibmveth_set_tx_csum,
830 .get_rx_csum = ibmveth_get_rx_csum,
831 .set_rx_csum = ibmveth_set_rx_csum,
Brian Kingddbb4de2007-08-17 09:16:43 -0500832 .get_strings = ibmveth_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700833 .get_sset_count = ibmveth_get_sset_count,
Brian Kingddbb4de2007-08-17 09:16:43 -0500834 .get_ethtool_stats = ibmveth_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835};
836
837static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
838{
839 return -EOPNOTSUPP;
840}
841
842#define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
843
844static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *netdev)
845{
846 struct ibmveth_adapter *adapter = netdev->priv;
Brian King3449a2a2007-08-17 09:16:49 -0500847 union ibmveth_buf_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 unsigned long lpar_rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 unsigned long correlator;
Santiago Leon60296d92005-10-26 10:47:16 -0600850 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 unsigned int retry_count;
Santiago Leon60296d92005-10-26 10:47:16 -0600852 unsigned int tx_dropped = 0;
853 unsigned int tx_bytes = 0;
854 unsigned int tx_packets = 0;
855 unsigned int tx_send_failed = 0;
856 unsigned int tx_map_failed = 0;
857
Brian King79ef4a42007-08-17 09:16:56 -0500858 desc.fields.flags_len = IBMVETH_BUF_VALID | skb->len;
Brian King3449a2a2007-08-17 09:16:49 -0500859 desc.fields.address = dma_map_single(&adapter->vdev->dev, skb->data,
Brian King79ef4a42007-08-17 09:16:56 -0500860 skb->len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Brian Kingf4ff2872007-09-15 13:36:07 -0700862 if (skb->ip_summed == CHECKSUM_PARTIAL &&
863 ip_hdr(skb)->protocol != IPPROTO_TCP && skb_checksum_help(skb)) {
864 ibmveth_error_printk("tx: failed to checksum packet\n");
865 tx_dropped++;
866 goto out;
867 }
868
869 if (skb->ip_summed == CHECKSUM_PARTIAL) {
870 unsigned char *buf = skb_transport_header(skb) + skb->csum_offset;
871
Brian King79ef4a42007-08-17 09:16:56 -0500872 desc.fields.flags_len |= (IBMVETH_BUF_NO_CSUM | IBMVETH_BUF_CSUM_GOOD);
Brian Kingf4ff2872007-09-15 13:36:07 -0700873
874 /* Need to zero out the checksum */
875 buf[0] = 0;
876 buf[1] = 0;
877 }
878
Brian King3449a2a2007-08-17 09:16:49 -0500879 if (dma_mapping_error(desc.fields.address)) {
880 ibmveth_error_printk("tx: unable to map xmit buffer\n");
Santiago Leon60296d92005-10-26 10:47:16 -0600881 tx_map_failed++;
882 tx_dropped++;
883 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /* send the frame. Arbitrarily set retrycount to 1024 */
887 correlator = 0;
888 retry_count = 1024;
889 do {
890 lpar_rc = h_send_logical_lan(adapter->vdev->unit_address,
Brian King3449a2a2007-08-17 09:16:49 -0500891 desc.desc, 0, 0, 0, 0, 0,
892 correlator, &correlator);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200893 } while ((lpar_rc == H_BUSY) && (retry_count--));
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400894
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200895 if(lpar_rc != H_SUCCESS && lpar_rc != H_DROPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 ibmveth_error_printk("tx: h_send_logical_lan failed with rc=%ld\n", lpar_rc);
Brian King3449a2a2007-08-17 09:16:49 -0500897 ibmveth_error_printk("tx: valid=%d, len=%d, address=0x%08x\n",
Brian King79ef4a42007-08-17 09:16:56 -0500898 (desc.fields.flags_len & IBMVETH_BUF_VALID) ? 1 : 0,
899 skb->len, desc.fields.address);
Santiago Leon60296d92005-10-26 10:47:16 -0600900 tx_send_failed++;
901 tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 } else {
Santiago Leon60296d92005-10-26 10:47:16 -0600903 tx_packets++;
904 tx_bytes += skb->len;
Santiago Leon0abe7912005-10-26 10:46:53 -0600905 netdev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907
Brian King3449a2a2007-08-17 09:16:49 -0500908 dma_unmap_single(&adapter->vdev->dev, desc.fields.address,
Brian King79ef4a42007-08-17 09:16:56 -0500909 skb->len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Santiago Leon60296d92005-10-26 10:47:16 -0600911out: spin_lock_irqsave(&adapter->stats_lock, flags);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700912 netdev->stats.tx_dropped += tx_dropped;
913 netdev->stats.tx_bytes += tx_bytes;
914 netdev->stats.tx_packets += tx_packets;
Santiago Leon60296d92005-10-26 10:47:16 -0600915 adapter->tx_send_failed += tx_send_failed;
916 adapter->tx_map_failed += tx_map_failed;
917 spin_unlock_irqrestore(&adapter->stats_lock, flags);
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 dev_kfree_skb(skb);
920 return 0;
921}
922
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700923static int ibmveth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700925 struct ibmveth_adapter *adapter = container_of(napi, struct ibmveth_adapter, napi);
926 struct net_device *netdev = adapter->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int frames_processed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 unsigned long lpar_rc;
929
930 restart_poll:
931 do {
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700932 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700934 if (!ibmveth_rxq_pending_buffer(adapter))
935 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700937 rmb();
938 if (!ibmveth_rxq_buffer_valid(adapter)) {
939 wmb(); /* suggested by larson1 */
940 adapter->rx_invalid_buffer++;
941 ibmveth_debug_printk("recycling invalid buffer\n");
942 ibmveth_rxq_recycle_buffer(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 } else {
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700944 int length = ibmveth_rxq_frame_length(adapter);
945 int offset = ibmveth_rxq_frame_offset(adapter);
Brian Kingf4ff2872007-09-15 13:36:07 -0700946 int csum_good = ibmveth_rxq_csum_good(adapter);
947
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700948 skb = ibmveth_rxq_get_buffer(adapter);
Brian Kingf4ff2872007-09-15 13:36:07 -0700949 if (csum_good)
950 skb->ip_summed = CHECKSUM_UNNECESSARY;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700951
952 ibmveth_rxq_harvest_buffer(adapter);
953
954 skb_reserve(skb, offset);
955 skb_put(skb, length);
956 skb->protocol = eth_type_trans(skb, netdev);
957
958 netif_receive_skb(skb); /* send it up */
959
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700960 netdev->stats.rx_packets++;
961 netdev->stats.rx_bytes += length;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700962 frames_processed++;
963 netdev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700965 } while (frames_processed < budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Santiago Leone2adbcb2005-10-26 10:47:08 -0600967 ibmveth_replenish_task(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700969 if (frames_processed < budget) {
970 /* We think we are done - reenable interrupts,
971 * then check once more to make sure we are done.
972 */
973 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
974 VIO_IRQ_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200976 ibmveth_assert(lpar_rc == H_SUCCESS);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700977
978 netif_rx_complete(netdev, napi);
979
980 if (ibmveth_rxq_pending_buffer(adapter) &&
981 netif_rx_reschedule(netdev, napi)) {
982 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
983 VIO_IRQ_DISABLE);
984 goto restart_poll;
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700988 return frames_processed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
David Howells7d12e782006-10-05 14:55:46 +0100991static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400992{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct net_device *netdev = dev_instance;
994 struct ibmveth_adapter *adapter = netdev->priv;
995 unsigned long lpar_rc;
996
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700997 if (netif_rx_schedule_prep(netdev, &adapter->napi)) {
998 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
999 VIO_IRQ_DISABLE);
Segher Boessenkool706c8c92006-03-30 14:49:40 +02001000 ibmveth_assert(lpar_rc == H_SUCCESS);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001001 __netif_rx_schedule(netdev, &adapter->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003 return IRQ_HANDLED;
1004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006static void ibmveth_set_multicast_list(struct net_device *netdev)
1007{
1008 struct ibmveth_adapter *adapter = netdev->priv;
1009 unsigned long lpar_rc;
1010
1011 if((netdev->flags & IFF_PROMISC) || (netdev->mc_count > adapter->mcastFilterSize)) {
1012 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1013 IbmVethMcastEnableRecv |
1014 IbmVethMcastDisableFiltering,
1015 0);
Segher Boessenkool706c8c92006-03-30 14:49:40 +02001016 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 ibmveth_error_printk("h_multicast_ctrl rc=%ld when entering promisc mode\n", lpar_rc);
1018 }
1019 } else {
1020 struct dev_mc_list *mclist = netdev->mc_list;
1021 int i;
1022 /* clear the filter table & disable filtering */
1023 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1024 IbmVethMcastEnableRecv |
1025 IbmVethMcastDisableFiltering |
1026 IbmVethMcastClearFilterTable,
1027 0);
Segher Boessenkool706c8c92006-03-30 14:49:40 +02001028 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 ibmveth_error_printk("h_multicast_ctrl rc=%ld when attempting to clear filter table\n", lpar_rc);
1030 }
1031 /* add the addresses to the filter table */
1032 for(i = 0; i < netdev->mc_count; ++i, mclist = mclist->next) {
1033 // add the multicast address to the filter table
1034 unsigned long mcast_addr = 0;
1035 memcpy(((char *)&mcast_addr)+2, mclist->dmi_addr, 6);
1036 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1037 IbmVethMcastAddFilter,
1038 mcast_addr);
Segher Boessenkool706c8c92006-03-30 14:49:40 +02001039 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 ibmveth_error_printk("h_multicast_ctrl rc=%ld when adding an entry to the filter table\n", lpar_rc);
1041 }
1042 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /* re-enable filtering */
1045 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1046 IbmVethMcastEnableFiltering,
1047 0);
Segher Boessenkool706c8c92006-03-30 14:49:40 +02001048 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ibmveth_error_printk("h_multicast_ctrl rc=%ld when enabling filtering\n", lpar_rc);
1050 }
1051 }
1052}
1053
1054static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
1055{
Santiago Leonb6d35182005-10-26 10:47:01 -06001056 struct ibmveth_adapter *adapter = dev->priv;
Santiago Leon860f2422006-04-25 11:19:59 -05001057 int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
Brian Kingce6eea52007-06-08 14:05:17 -05001058 int reinit = 0;
1059 int i, rc;
Santiago Leonb6d35182005-10-26 10:47:01 -06001060
Santiago Leon860f2422006-04-25 11:19:59 -05001061 if (new_mtu < IBMVETH_MAX_MTU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return -EINVAL;
Santiago Leonb6d35182005-10-26 10:47:01 -06001063
Brian Kingce6eea52007-06-08 14:05:17 -05001064 for (i = 0; i < IbmVethNumBufferPools; i++)
1065 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size)
1066 break;
1067
1068 if (i == IbmVethNumBufferPools)
1069 return -EINVAL;
1070
Santiago Leon860f2422006-04-25 11:19:59 -05001071 /* Look for an active buffer pool that can hold the new MTU */
Santiago Leonb6d35182005-10-26 10:47:01 -06001072 for(i = 0; i<IbmVethNumBufferPools; i++) {
Brian Kingce6eea52007-06-08 14:05:17 -05001073 if (!adapter->rx_buff_pool[i].active) {
1074 adapter->rx_buff_pool[i].active = 1;
1075 reinit = 1;
1076 }
1077
Santiago Leon860f2422006-04-25 11:19:59 -05001078 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size) {
Brian Kingce6eea52007-06-08 14:05:17 -05001079 if (reinit && netif_running(adapter->netdev)) {
1080 adapter->pool_config = 1;
1081 ibmveth_close(adapter->netdev);
1082 adapter->pool_config = 0;
1083 dev->mtu = new_mtu;
1084 if ((rc = ibmveth_open(adapter->netdev)))
1085 return rc;
1086 } else
1087 dev->mtu = new_mtu;
Santiago Leon860f2422006-04-25 11:19:59 -05001088 return 0;
Santiago Leonb6d35182005-10-26 10:47:01 -06001089 }
Santiago Leonb6d35182005-10-26 10:47:01 -06001090 }
Santiago Leon860f2422006-04-25 11:19:59 -05001091 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Santiago Leon6b422372006-10-03 12:24:28 -05001094#ifdef CONFIG_NET_POLL_CONTROLLER
1095static void ibmveth_poll_controller(struct net_device *dev)
1096{
1097 ibmveth_replenish_task(dev->priv);
Andrew Morton5f771132006-10-10 14:33:30 -07001098 ibmveth_interrupt(dev->irq, dev);
Santiago Leon6b422372006-10-03 12:24:28 -05001099}
1100#endif
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
1103{
Santiago Leonb6d35182005-10-26 10:47:01 -06001104 int rc, i;
Brian Kingf4ff2872007-09-15 13:36:07 -07001105 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct net_device *netdev;
Mariusz Kozlowski9dc83af2007-08-06 23:44:03 +02001107 struct ibmveth_adapter *adapter;
Brian King79ef4a42007-08-17 09:16:56 -05001108 u64 set_attr, ret_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 unsigned char *mac_addr_p;
1111 unsigned int *mcastFilterSize_p;
1112
1113
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001114 ibmveth_debug_printk_no_adapter("entering ibmveth_probe for UA 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 dev->unit_address);
1116
Michael Ellerman493a6842007-04-17 13:12:55 +10001117 mac_addr_p = (unsigned char *) vio_get_attribute(dev,
1118 VETH_MAC_ADDR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if(!mac_addr_p) {
1120 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find VETH_MAC_ADDR "
1121 "attribute\n", __FILE__, __LINE__);
1122 return 0;
1123 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001124
Michael Ellerman493a6842007-04-17 13:12:55 +10001125 mcastFilterSize_p = (unsigned int *) vio_get_attribute(dev,
1126 VETH_MCAST_FILTER_SIZE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if(!mcastFilterSize_p) {
1128 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find "
1129 "VETH_MCAST_FILTER_SIZE attribute\n",
1130 __FILE__, __LINE__);
1131 return 0;
1132 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
1135
1136 if(!netdev)
1137 return -ENOMEM;
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 adapter = netdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 dev->dev.driver_data = netdev;
1141
1142 adapter->vdev = dev;
1143 adapter->netdev = netdev;
1144 adapter->mcastFilterSize= *mcastFilterSize_p;
Santiago Leon860f2422006-04-25 11:19:59 -05001145 adapter->pool_config = 0;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001146
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001147 netif_napi_add(netdev, &adapter->napi, ibmveth_poll, 16);
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /* Some older boxes running PHYP non-natively have an OF that
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001150 returns a 8-byte local-mac-address field (and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 2 bytes have to be ignored) while newer boxes' OF return
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001152 a 6-byte field. Note that IEEE 1275 specifies that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 local-mac-address must be a 6-byte field.
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001154 The RPA doc specifies that the first byte must be 10b, so
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 we'll just look for it to solve this 8 vs. 6 byte field issue */
1156
1157 if ((*mac_addr_p & 0x3) != 0x02)
1158 mac_addr_p += 2;
1159
1160 adapter->mac_addr = 0;
1161 memcpy(&adapter->mac_addr, mac_addr_p, 6);
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 netdev->irq = dev->irq;
1164 netdev->open = ibmveth_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 netdev->stop = ibmveth_close;
1166 netdev->hard_start_xmit = ibmveth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 netdev->set_multicast_list = ibmveth_set_multicast_list;
1168 netdev->do_ioctl = ibmveth_ioctl;
1169 netdev->ethtool_ops = &netdev_ethtool_ops;
1170 netdev->change_mtu = ibmveth_change_mtu;
1171 SET_NETDEV_DEV(netdev, &dev->dev);
Santiago Leon6b422372006-10-03 12:24:28 -05001172#ifdef CONFIG_NET_POLL_CONTROLLER
1173 netdev->poll_controller = ibmveth_poll_controller;
1174#endif
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001175 netdev->features |= NETIF_F_LLTX;
Santiago Leon60296d92005-10-26 10:47:16 -06001176 spin_lock_init(&adapter->stats_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 memcpy(&netdev->dev_addr, &adapter->mac_addr, netdev->addr_len);
1179
Santiago Leon860f2422006-04-25 11:19:59 -05001180 for(i = 0; i<IbmVethNumBufferPools; i++) {
1181 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
Greg Kroah-Hartman8dde2a92007-12-17 15:54:39 -04001182 int error;
1183
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001184 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1185 pool_count[i], pool_size[i],
Santiago Leon860f2422006-04-25 11:19:59 -05001186 pool_active[i]);
Greg Kroah-Hartman8dde2a92007-12-17 15:54:39 -04001187 error = kobject_init_and_add(kobj, &ktype_veth_pool,
1188 &dev->dev.kobj, "pool%d", i);
1189 if (!error)
1190 kobject_uevent(kobj, KOBJ_ADD);
Santiago Leon860f2422006-04-25 11:19:59 -05001191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 ibmveth_debug_printk("adapter @ 0x%p\n", adapter);
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 adapter->buffer_list_dma = DMA_ERROR_CODE;
1196 adapter->filter_list_dma = DMA_ERROR_CODE;
1197 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 ibmveth_debug_printk("registering netdev...\n");
1200
Brian King79ef4a42007-08-17 09:16:56 -05001201 ret = h_illan_attributes(dev->unit_address, 0, 0, &ret_attr);
Brian Kingf4ff2872007-09-15 13:36:07 -07001202
Brian King79ef4a42007-08-17 09:16:56 -05001203 if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
1204 !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
1205 (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
1206 set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
Brian Kingf4ff2872007-09-15 13:36:07 -07001207
Brian King79ef4a42007-08-17 09:16:56 -05001208 ret = h_illan_attributes(dev->unit_address, 0, set_attr, &ret_attr);
Brian Kingf4ff2872007-09-15 13:36:07 -07001209
Brian King5fc7e012007-08-17 09:16:31 -05001210 if (ret == H_SUCCESS) {
1211 adapter->rx_csum = 1;
Brian Kingf4ff2872007-09-15 13:36:07 -07001212 netdev->features |= NETIF_F_IP_CSUM;
Brian King5fc7e012007-08-17 09:16:31 -05001213 } else
Brian King79ef4a42007-08-17 09:16:56 -05001214 ret = h_illan_attributes(dev->unit_address, set_attr, 0, &ret_attr);
Brian Kingf4ff2872007-09-15 13:36:07 -07001215 }
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 rc = register_netdev(netdev);
1218
1219 if(rc) {
1220 ibmveth_debug_printk("failed to register netdev rc=%d\n", rc);
1221 free_netdev(netdev);
1222 return rc;
1223 }
1224
1225 ibmveth_debug_printk("registered\n");
1226
1227 ibmveth_proc_register_adapter(adapter);
1228
1229 return 0;
1230}
1231
1232static int __devexit ibmveth_remove(struct vio_dev *dev)
1233{
1234 struct net_device *netdev = dev->dev.driver_data;
1235 struct ibmveth_adapter *adapter = netdev->priv;
Santiago Leon860f2422006-04-25 11:19:59 -05001236 int i;
1237
1238 for(i = 0; i<IbmVethNumBufferPools; i++)
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001239 kobject_put(&adapter->rx_buff_pool[i].kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 unregister_netdev(netdev);
1242
1243 ibmveth_proc_unregister_adapter(adapter);
1244
1245 free_netdev(netdev);
1246 return 0;
1247}
1248
1249#ifdef CONFIG_PROC_FS
1250static void ibmveth_proc_register_driver(void)
1251{
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001252 ibmveth_proc_dir = proc_mkdir(IBMVETH_PROC_DIR, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (ibmveth_proc_dir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255}
1256
1257static void ibmveth_proc_unregister_driver(void)
1258{
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001259 remove_proc_entry(IBMVETH_PROC_DIR, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001262static void *ibmveth_seq_start(struct seq_file *seq, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 if (*pos == 0) {
1265 return (void *)1;
1266 } else {
1267 return NULL;
1268 }
1269}
1270
1271static void *ibmveth_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1272{
1273 ++*pos;
1274 return NULL;
1275}
1276
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001277static void ibmveth_seq_stop(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279}
1280
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001281static int ibmveth_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 struct ibmveth_adapter *adapter = seq->private;
1284 char *current_mac = ((char*) &adapter->netdev->dev_addr);
1285 char *firmware_mac = ((char*) &adapter->mac_addr) ;
Joe Perches0795af52007-10-03 17:59:30 -07001286 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 seq_printf(seq, "%s %s\n\n", ibmveth_driver_string, ibmveth_driver_version);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 seq_printf(seq, "Unit Address: 0x%x\n", adapter->vdev->unit_address);
Joe Perches0795af52007-10-03 17:59:30 -07001291 seq_printf(seq, "Current MAC: %s\n", print_mac(mac, current_mac));
1292 seq_printf(seq, "Firmware MAC: %s\n", print_mac(mac, firmware_mac));
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 seq_printf(seq, "\nAdapter Statistics:\n");
Brian King3449a2a2007-08-17 09:16:49 -05001295 seq_printf(seq, " TX: vio_map_single failres: %ld\n", adapter->tx_map_failed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 seq_printf(seq, " send failures: %ld\n", adapter->tx_send_failed);
1297 seq_printf(seq, " RX: replenish task cycles: %ld\n", adapter->replenish_task_cycles);
1298 seq_printf(seq, " alloc_skb_failures: %ld\n", adapter->replenish_no_mem);
1299 seq_printf(seq, " add buffer failures: %ld\n", adapter->replenish_add_buff_failure);
1300 seq_printf(seq, " invalid buffers: %ld\n", adapter->rx_invalid_buffer);
1301 seq_printf(seq, " no buffers: %ld\n", adapter->rx_no_buffer);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return 0;
1304}
1305static struct seq_operations ibmveth_seq_ops = {
1306 .start = ibmveth_seq_start,
1307 .next = ibmveth_seq_next,
1308 .stop = ibmveth_seq_stop,
1309 .show = ibmveth_seq_show,
1310};
1311
1312static int ibmveth_proc_open(struct inode *inode, struct file *file)
1313{
1314 struct seq_file *seq;
1315 struct proc_dir_entry *proc;
1316 int rc;
1317
1318 rc = seq_open(file, &ibmveth_seq_ops);
1319 if (!rc) {
1320 /* recover the pointer buried in proc_dir_entry data */
1321 seq = file->private_data;
1322 proc = PDE(inode);
1323 seq->private = proc->data;
1324 }
1325 return rc;
1326}
1327
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001328static const struct file_operations ibmveth_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 .owner = THIS_MODULE,
1330 .open = ibmveth_proc_open,
1331 .read = seq_read,
1332 .llseek = seq_lseek,
1333 .release = seq_release,
1334};
1335
1336static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1337{
1338 struct proc_dir_entry *entry;
1339 if (ibmveth_proc_dir) {
Santiago Leon03a85d02006-10-03 12:24:39 -05001340 char u_addr[10];
1341 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1342 entry = create_proc_entry(u_addr, S_IFREG, ibmveth_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (!entry) {
1344 ibmveth_error_printk("Cannot create adapter proc entry");
1345 } else {
1346 entry->data = (void *) adapter;
1347 entry->proc_fops = &ibmveth_proc_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349 }
1350 return;
1351}
1352
1353static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1354{
1355 if (ibmveth_proc_dir) {
Santiago Leon03a85d02006-10-03 12:24:39 -05001356 char u_addr[10];
1357 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1358 remove_proc_entry(u_addr, ibmveth_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
1360}
1361
1362#else /* CONFIG_PROC_FS */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001363static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365}
1366
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001367static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369}
1370static void ibmveth_proc_register_driver(void)
1371{
1372}
1373
1374static void ibmveth_proc_unregister_driver(void)
1375{
1376}
1377#endif /* CONFIG_PROC_FS */
1378
Santiago Leon860f2422006-04-25 11:19:59 -05001379static struct attribute veth_active_attr;
1380static struct attribute veth_num_attr;
1381static struct attribute veth_size_attr;
1382
1383static ssize_t veth_pool_show(struct kobject * kobj,
1384 struct attribute * attr, char * buf)
1385{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001386 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001387 struct ibmveth_buff_pool,
1388 kobj);
1389
1390 if (attr == &veth_active_attr)
1391 return sprintf(buf, "%d\n", pool->active);
1392 else if (attr == &veth_num_attr)
1393 return sprintf(buf, "%d\n", pool->size);
1394 else if (attr == &veth_size_attr)
1395 return sprintf(buf, "%d\n", pool->buff_size);
1396 return 0;
1397}
1398
1399static ssize_t veth_pool_store(struct kobject * kobj, struct attribute * attr,
1400const char * buf, size_t count)
1401{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001402 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001403 struct ibmveth_buff_pool,
1404 kobj);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001405 struct net_device *netdev =
Santiago Leon860f2422006-04-25 11:19:59 -05001406 container_of(kobj->parent, struct device, kobj)->driver_data;
1407 struct ibmveth_adapter *adapter = netdev->priv;
1408 long value = simple_strtol(buf, NULL, 10);
1409 long rc;
1410
1411 if (attr == &veth_active_attr) {
1412 if (value && !pool->active) {
Brian King4aa9c932007-06-08 14:05:16 -05001413 if (netif_running(netdev)) {
1414 if(ibmveth_alloc_buffer_pool(pool)) {
1415 ibmveth_error_printk("unable to alloc pool\n");
1416 return -ENOMEM;
1417 }
1418 pool->active = 1;
1419 adapter->pool_config = 1;
1420 ibmveth_close(netdev);
1421 adapter->pool_config = 0;
1422 if ((rc = ibmveth_open(netdev)))
1423 return rc;
1424 } else
1425 pool->active = 1;
Santiago Leon860f2422006-04-25 11:19:59 -05001426 } else if (!value && pool->active) {
1427 int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1428 int i;
1429 /* Make sure there is a buffer pool with buffers that
1430 can hold a packet of the size of the MTU */
Brian King76b9cfc2007-08-03 13:55:19 +10001431 for (i = 0; i < IbmVethNumBufferPools; i++) {
Santiago Leon860f2422006-04-25 11:19:59 -05001432 if (pool == &adapter->rx_buff_pool[i])
1433 continue;
1434 if (!adapter->rx_buff_pool[i].active)
1435 continue;
Brian King76b9cfc2007-08-03 13:55:19 +10001436 if (mtu <= adapter->rx_buff_pool[i].buff_size)
1437 break;
Santiago Leon860f2422006-04-25 11:19:59 -05001438 }
Brian King76b9cfc2007-08-03 13:55:19 +10001439
1440 if (i == IbmVethNumBufferPools) {
Santiago Leon860f2422006-04-25 11:19:59 -05001441 ibmveth_error_printk("no active pool >= MTU\n");
1442 return -EPERM;
1443 }
Brian King76b9cfc2007-08-03 13:55:19 +10001444
1445 pool->active = 0;
1446 if (netif_running(netdev)) {
1447 adapter->pool_config = 1;
1448 ibmveth_close(netdev);
1449 adapter->pool_config = 0;
1450 if ((rc = ibmveth_open(netdev)))
1451 return rc;
1452 }
Santiago Leon860f2422006-04-25 11:19:59 -05001453 }
1454 } else if (attr == &veth_num_attr) {
1455 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT)
1456 return -EINVAL;
1457 else {
Brian King4aa9c932007-06-08 14:05:16 -05001458 if (netif_running(netdev)) {
1459 adapter->pool_config = 1;
1460 ibmveth_close(netdev);
1461 adapter->pool_config = 0;
1462 pool->size = value;
1463 if ((rc = ibmveth_open(netdev)))
1464 return rc;
1465 } else
1466 pool->size = value;
Santiago Leon860f2422006-04-25 11:19:59 -05001467 }
1468 } else if (attr == &veth_size_attr) {
1469 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE)
1470 return -EINVAL;
1471 else {
Brian King4aa9c932007-06-08 14:05:16 -05001472 if (netif_running(netdev)) {
1473 adapter->pool_config = 1;
1474 ibmveth_close(netdev);
1475 adapter->pool_config = 0;
1476 pool->buff_size = value;
1477 if ((rc = ibmveth_open(netdev)))
1478 return rc;
1479 } else
1480 pool->buff_size = value;
Santiago Leon860f2422006-04-25 11:19:59 -05001481 }
1482 }
1483
1484 /* kick the interrupt handler to allocate/deallocate pools */
David Howells7d12e782006-10-05 14:55:46 +01001485 ibmveth_interrupt(netdev->irq, netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001486 return count;
1487}
1488
1489
1490#define ATTR(_name, _mode) \
1491 struct attribute veth_##_name##_attr = { \
Tejun Heo7b595752007-06-14 03:45:17 +09001492 .name = __stringify(_name), .mode = _mode, \
Santiago Leon860f2422006-04-25 11:19:59 -05001493 };
1494
1495static ATTR(active, 0644);
1496static ATTR(num, 0644);
1497static ATTR(size, 0644);
1498
1499static struct attribute * veth_pool_attrs[] = {
1500 &veth_active_attr,
1501 &veth_num_attr,
1502 &veth_size_attr,
1503 NULL,
1504};
1505
1506static struct sysfs_ops veth_pool_ops = {
1507 .show = veth_pool_show,
1508 .store = veth_pool_store,
1509};
1510
1511static struct kobj_type ktype_veth_pool = {
1512 .release = NULL,
1513 .sysfs_ops = &veth_pool_ops,
1514 .default_attrs = veth_pool_attrs,
1515};
1516
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518static struct vio_device_id ibmveth_device_table[] __devinitdata= {
1519 { "network", "IBM,l-lan"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +10001520 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1523
1524static struct vio_driver ibmveth_driver = {
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001525 .id_table = ibmveth_device_table,
1526 .probe = ibmveth_probe,
1527 .remove = ibmveth_remove,
1528 .driver = {
1529 .name = ibmveth_driver_name,
Stephen Rothwell915124d2005-10-24 15:12:22 +10001530 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532};
1533
1534static int __init ibmveth_module_init(void)
1535{
1536 ibmveth_printk("%s: %s %s\n", ibmveth_driver_name, ibmveth_driver_string, ibmveth_driver_version);
1537
1538 ibmveth_proc_register_driver();
1539
1540 return vio_register_driver(&ibmveth_driver);
1541}
1542
1543static void __exit ibmveth_module_exit(void)
1544{
1545 vio_unregister_driver(&ibmveth_driver);
1546 ibmveth_proc_unregister_driver();
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001547}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549module_init(ibmveth_module_init);
1550module_exit(ibmveth_module_exit);