e1000: FIX: enable hw TSO for IPV6
[linux-2.6-block.git] / drivers / net / ibmveth.c
CommitLineData
1da177e4
LT
1/**************************************************************************/
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. */
d7fbeba6 27/* */
1da177e4
LT
28/**************************************************************************/
29/*
30 TODO:
31 - remove frag processing code - no longer needed
32 - add support for sysfs
33 - possibly remove procfs support
34*/
35
1da177e4 36#include <linux/module.h>
1da177e4
LT
37#include <linux/types.h>
38#include <linux/errno.h>
39#include <linux/ioport.h>
40#include <linux/dma-mapping.h>
41#include <linux/kernel.h>
42#include <linux/netdevice.h>
43#include <linux/etherdevice.h>
44#include <linux/skbuff.h>
45#include <linux/init.h>
46#include <linux/delay.h>
47#include <linux/mm.h>
48#include <linux/ethtool.h>
49#include <linux/proc_fs.h>
50#include <asm/semaphore.h>
51#include <asm/hvcall.h>
52#include <asm/atomic.h>
53#include <asm/iommu.h>
54#include <asm/vio.h>
55#include <asm/uaccess.h>
56#include <linux/seq_file.h>
57
58#include "ibmveth.h"
59
6af37fa9 60#undef DEBUG
1da177e4
LT
61
62#define ibmveth_printk(fmt, args...) \
f98baffb 63 printk(KERN_DEBUG "%s: " fmt, __FILE__, ## args)
1da177e4
LT
64
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...)
d7fbeba6 81#define ibmveth_assert(expr)
1da177e4
LT
82#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);
87static int ibmveth_poll(struct net_device *dev, int *budget);
88static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *dev);
89static struct net_device_stats *ibmveth_get_stats(struct net_device *dev);
90static 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);
7d12e780 96static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
b6d35182 97static inline void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
860f242e 98static struct kobj_type ktype_veth_pool;
1da177e4
LT
99
100#ifdef CONFIG_PROC_FS
66600221 101#define IBMVETH_PROC_DIR "net/ibmveth"
1da177e4
LT
102static 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
114/* simple methods of getting data from the current rxq entry */
115static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
116{
117 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].toggle == adapter->rx_queue.toggle);
118}
119
120static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
121{
122 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].valid);
123}
124
125static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
126{
127 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].offset);
128}
129
130static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
131{
132 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
133}
134
135/* setup the initial settings for a buffer pool */
860f242e 136static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool, u32 pool_index, u32 pool_size, u32 buff_size, u32 pool_active)
1da177e4
LT
137{
138 pool->size = pool_size;
139 pool->index = pool_index;
140 pool->buff_size = buff_size;
141 pool->threshold = pool_size / 2;
860f242e 142 pool->active = pool_active;
1da177e4
LT
143}
144
145/* allocate and setup an buffer pool - called during open */
146static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
147{
148 int i;
149
d7fbeba6 150 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
1da177e4
LT
151
152 if(!pool->free_map) {
153 return -1;
154 }
155
d7fbeba6 156 pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
1da177e4
LT
157 if(!pool->dma_addr) {
158 kfree(pool->free_map);
159 pool->free_map = NULL;
160 return -1;
161 }
162
163 pool->skbuff = kmalloc(sizeof(void*) * pool->size, GFP_KERNEL);
164
165 if(!pool->skbuff) {
166 kfree(pool->dma_addr);
167 pool->dma_addr = NULL;
168
169 kfree(pool->free_map);
170 pool->free_map = NULL;
171 return -1;
172 }
173
174 memset(pool->skbuff, 0, sizeof(void*) * pool->size);
175 memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
176
177 for(i = 0; i < pool->size; ++i) {
178 pool->free_map[i] = i;
179 }
180
181 atomic_set(&pool->available, 0);
182 pool->producer_index = 0;
183 pool->consumer_index = 0;
184
185 return 0;
186}
187
188/* replenish the buffers for a pool. note that we don't need to
189 * skb_reserve these since they are used for incoming...
190 */
191static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
192{
193 u32 i;
194 u32 count = pool->size - atomic_read(&pool->available);
195 u32 buffers_added = 0;
196
197 mb();
198
199 for(i = 0; i < count; ++i) {
200 struct sk_buff *skb;
201 unsigned int free_index, index;
202 u64 correlator;
203 union ibmveth_buf_desc desc;
204 unsigned long lpar_rc;
205 dma_addr_t dma_addr;
206
207 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
208
209 if(!skb) {
210 ibmveth_debug_printk("replenish: unable to allocate skb\n");
211 adapter->replenish_no_mem++;
212 break;
213 }
214
047a66d4
DG
215 free_index = pool->consumer_index;
216 pool->consumer_index = (pool->consumer_index + 1) % pool->size;
1da177e4 217 index = pool->free_map[free_index];
d7fbeba6 218
1da177e4
LT
219 ibmveth_assert(index != IBM_VETH_INVALID_MAP);
220 ibmveth_assert(pool->skbuff[index] == NULL);
221
222 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
223 pool->buff_size, DMA_FROM_DEVICE);
224
225 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
226 pool->dma_addr[index] = dma_addr;
227 pool->skbuff[index] = skb;
228
229 correlator = ((u64)pool->index << 32) | index;
230 *(u64*)skb->data = correlator;
231
232 desc.desc = 0;
233 desc.fields.valid = 1;
234 desc.fields.length = pool->buff_size;
d7fbeba6 235 desc.fields.address = dma_addr;
1da177e4
LT
236
237 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
d7fbeba6 238
706c8c93 239 if(lpar_rc != H_SUCCESS) {
82702d37 240 pool->free_map[free_index] = index;
1da177e4 241 pool->skbuff[index] = NULL;
751ae21c
SL
242 if (pool->consumer_index == 0)
243 pool->consumer_index = pool->size - 1;
244 else
245 pool->consumer_index--;
1da177e4
LT
246 dma_unmap_single(&adapter->vdev->dev,
247 pool->dma_addr[index], pool->buff_size,
248 DMA_FROM_DEVICE);
249 dev_kfree_skb_any(skb);
250 adapter->replenish_add_buff_failure++;
251 break;
252 } else {
253 buffers_added++;
254 adapter->replenish_add_buff_success++;
255 }
256 }
d7fbeba6 257
1da177e4
LT
258 mb();
259 atomic_add(buffers_added, &(pool->available));
260}
261
e2adbcb4 262/* replenish routine */
d7fbeba6 263static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
1da177e4 264{
b6d35182
SL
265 int i;
266
1da177e4
LT
267 adapter->replenish_task_cycles++;
268
b6d35182
SL
269 for(i = 0; i < IbmVethNumBufferPools; i++)
270 if(adapter->rx_buff_pool[i].active)
d7fbeba6 271 ibmveth_replenish_buffer_pool(adapter,
b6d35182 272 &adapter->rx_buff_pool[i]);
1da177e4
LT
273
274 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
1da177e4
LT
275}
276
277/* empty and free ana buffer pool - also used to do cleanup in error paths */
278static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
279{
280 int i;
281
b4558ea9
JJ
282 kfree(pool->free_map);
283 pool->free_map = NULL;
1da177e4
LT
284
285 if(pool->skbuff && pool->dma_addr) {
286 for(i = 0; i < pool->size; ++i) {
287 struct sk_buff *skb = pool->skbuff[i];
288 if(skb) {
289 dma_unmap_single(&adapter->vdev->dev,
290 pool->dma_addr[i],
291 pool->buff_size,
292 DMA_FROM_DEVICE);
293 dev_kfree_skb_any(skb);
294 pool->skbuff[i] = NULL;
295 }
296 }
297 }
298
299 if(pool->dma_addr) {
300 kfree(pool->dma_addr);
301 pool->dma_addr = NULL;
302 }
303
304 if(pool->skbuff) {
305 kfree(pool->skbuff);
306 pool->skbuff = NULL;
307 }
308}
309
310/* remove a buffer from a pool */
311static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter, u64 correlator)
312{
313 unsigned int pool = correlator >> 32;
314 unsigned int index = correlator & 0xffffffffUL;
315 unsigned int free_index;
316 struct sk_buff *skb;
317
318 ibmveth_assert(pool < IbmVethNumBufferPools);
319 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
320
321 skb = adapter->rx_buff_pool[pool].skbuff[index];
322
323 ibmveth_assert(skb != NULL);
324
325 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
326
327 dma_unmap_single(&adapter->vdev->dev,
328 adapter->rx_buff_pool[pool].dma_addr[index],
329 adapter->rx_buff_pool[pool].buff_size,
330 DMA_FROM_DEVICE);
331
047a66d4
DG
332 free_index = adapter->rx_buff_pool[pool].producer_index;
333 adapter->rx_buff_pool[pool].producer_index
334 = (adapter->rx_buff_pool[pool].producer_index + 1)
335 % adapter->rx_buff_pool[pool].size;
1da177e4
LT
336 adapter->rx_buff_pool[pool].free_map[free_index] = index;
337
338 mb();
339
340 atomic_dec(&(adapter->rx_buff_pool[pool].available));
341}
342
343/* get the current buffer on the rx queue */
344static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
345{
346 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
347 unsigned int pool = correlator >> 32;
348 unsigned int index = correlator & 0xffffffffUL;
349
350 ibmveth_assert(pool < IbmVethNumBufferPools);
351 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
352
353 return adapter->rx_buff_pool[pool].skbuff[index];
354}
355
356/* recycle the current buffer on the rx queue */
357static void ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
358{
359 u32 q_index = adapter->rx_queue.index;
360 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
361 unsigned int pool = correlator >> 32;
362 unsigned int index = correlator & 0xffffffffUL;
363 union ibmveth_buf_desc desc;
364 unsigned long lpar_rc;
365
366 ibmveth_assert(pool < IbmVethNumBufferPools);
367 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
368
b6d35182
SL
369 if(!adapter->rx_buff_pool[pool].active) {
370 ibmveth_rxq_harvest_buffer(adapter);
371 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
372 return;
373 }
374
1da177e4
LT
375 desc.desc = 0;
376 desc.fields.valid = 1;
377 desc.fields.length = adapter->rx_buff_pool[pool].buff_size;
378 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
379
380 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
d7fbeba6 381
706c8c93 382 if(lpar_rc != H_SUCCESS) {
1da177e4
LT
383 ibmveth_debug_printk("h_add_logical_lan_buffer failed during recycle rc=%ld", lpar_rc);
384 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
385 }
386
387 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
388 adapter->rx_queue.index = 0;
389 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
390 }
391}
392
393static inline void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
394{
395 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
396
397 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
398 adapter->rx_queue.index = 0;
399 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
400 }
401}
402
403static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
404{
b6d35182
SL
405 int i;
406
1da177e4
LT
407 if(adapter->buffer_list_addr != NULL) {
408 if(!dma_mapping_error(adapter->buffer_list_dma)) {
409 dma_unmap_single(&adapter->vdev->dev,
410 adapter->buffer_list_dma, 4096,
411 DMA_BIDIRECTIONAL);
412 adapter->buffer_list_dma = DMA_ERROR_CODE;
413 }
414 free_page((unsigned long)adapter->buffer_list_addr);
415 adapter->buffer_list_addr = NULL;
d7fbeba6 416 }
1da177e4
LT
417
418 if(adapter->filter_list_addr != NULL) {
419 if(!dma_mapping_error(adapter->filter_list_dma)) {
420 dma_unmap_single(&adapter->vdev->dev,
421 adapter->filter_list_dma, 4096,
422 DMA_BIDIRECTIONAL);
423 adapter->filter_list_dma = DMA_ERROR_CODE;
424 }
425 free_page((unsigned long)adapter->filter_list_addr);
426 adapter->filter_list_addr = NULL;
427 }
428
429 if(adapter->rx_queue.queue_addr != NULL) {
430 if(!dma_mapping_error(adapter->rx_queue.queue_dma)) {
431 dma_unmap_single(&adapter->vdev->dev,
432 adapter->rx_queue.queue_dma,
433 adapter->rx_queue.queue_len,
434 DMA_BIDIRECTIONAL);
435 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
436 }
437 kfree(adapter->rx_queue.queue_addr);
438 adapter->rx_queue.queue_addr = NULL;
439 }
440
b6d35182 441 for(i = 0; i<IbmVethNumBufferPools; i++)
860f242e 442 if (adapter->rx_buff_pool[i].active)
d7fbeba6 443 ibmveth_free_buffer_pool(adapter,
860f242e 444 &adapter->rx_buff_pool[i]);
1da177e4
LT
445}
446
bbedefcc
ME
447static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
448 union ibmveth_buf_desc rxq_desc, u64 mac_address)
449{
450 int rc, try_again = 1;
451
452 /* After a kexec the adapter will still be open, so our attempt to
453 * open it will fail. So if we get a failure we free the adapter and
454 * try again, but only once. */
455retry:
456 rc = h_register_logical_lan(adapter->vdev->unit_address,
457 adapter->buffer_list_dma, rxq_desc.desc,
458 adapter->filter_list_dma, mac_address);
459
460 if (rc != H_SUCCESS && try_again) {
461 do {
462 rc = h_free_logical_lan(adapter->vdev->unit_address);
463 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
464
465 try_again = 0;
466 goto retry;
467 }
468
469 return rc;
470}
471
1da177e4
LT
472static int ibmveth_open(struct net_device *netdev)
473{
474 struct ibmveth_adapter *adapter = netdev->priv;
475 u64 mac_address = 0;
b6d35182 476 int rxq_entries = 1;
1da177e4
LT
477 unsigned long lpar_rc;
478 int rc;
479 union ibmveth_buf_desc rxq_desc;
b6d35182 480 int i;
1da177e4
LT
481
482 ibmveth_debug_printk("open starting\n");
483
b6d35182
SL
484 for(i = 0; i<IbmVethNumBufferPools; i++)
485 rxq_entries += adapter->rx_buff_pool[i].size;
d7fbeba6 486
1da177e4
LT
487 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
488 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
d7fbeba6 489
1da177e4
LT
490 if(!adapter->buffer_list_addr || !adapter->filter_list_addr) {
491 ibmveth_error_printk("unable to allocate filter or buffer list pages\n");
492 ibmveth_cleanup(adapter);
493 return -ENOMEM;
494 }
495
496 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) * rxq_entries;
497 adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len, GFP_KERNEL);
498
499 if(!adapter->rx_queue.queue_addr) {
500 ibmveth_error_printk("unable to allocate rx queue pages\n");
501 ibmveth_cleanup(adapter);
502 return -ENOMEM;
503 }
504
505 adapter->buffer_list_dma = dma_map_single(&adapter->vdev->dev,
506 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
507 adapter->filter_list_dma = dma_map_single(&adapter->vdev->dev,
508 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
509 adapter->rx_queue.queue_dma = dma_map_single(&adapter->vdev->dev,
510 adapter->rx_queue.queue_addr,
511 adapter->rx_queue.queue_len, DMA_BIDIRECTIONAL);
512
513 if((dma_mapping_error(adapter->buffer_list_dma) ) ||
514 (dma_mapping_error(adapter->filter_list_dma)) ||
515 (dma_mapping_error(adapter->rx_queue.queue_dma))) {
516 ibmveth_error_printk("unable to map filter or buffer list pages\n");
517 ibmveth_cleanup(adapter);
518 return -ENOMEM;
519 }
520
521 adapter->rx_queue.index = 0;
522 adapter->rx_queue.num_slots = rxq_entries;
523 adapter->rx_queue.toggle = 1;
524
1da177e4
LT
525 memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
526 mac_address = mac_address >> 16;
527
528 rxq_desc.desc = 0;
529 rxq_desc.fields.valid = 1;
530 rxq_desc.fields.length = adapter->rx_queue.queue_len;
531 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
532
533 ibmveth_debug_printk("buffer list @ 0x%p\n", adapter->buffer_list_addr);
534 ibmveth_debug_printk("filter list @ 0x%p\n", adapter->filter_list_addr);
535 ibmveth_debug_printk("receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
536
4347ef15
SL
537 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
538
bbedefcc 539 lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
1da177e4 540
706c8c93 541 if(lpar_rc != H_SUCCESS) {
1da177e4 542 ibmveth_error_printk("h_register_logical_lan failed with %ld\n", lpar_rc);
8168f902 543 ibmveth_error_printk("buffer TCE:0x%lx filter TCE:0x%lx rxq desc:0x%lx MAC:0x%lx\n",
1da177e4
LT
544 adapter->buffer_list_dma,
545 adapter->filter_list_dma,
546 rxq_desc.desc,
547 mac_address);
548 ibmveth_cleanup(adapter);
d7fbeba6 549 return -ENONET;
1da177e4
LT
550 }
551
860f242e
SL
552 for(i = 0; i<IbmVethNumBufferPools; i++) {
553 if(!adapter->rx_buff_pool[i].active)
554 continue;
555 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
556 ibmveth_error_printk("unable to alloc pool\n");
557 adapter->rx_buff_pool[i].active = 0;
558 ibmveth_cleanup(adapter);
559 return -ENOMEM ;
560 }
561 }
562
1da177e4
LT
563 ibmveth_debug_printk("registering irq 0x%x\n", netdev->irq);
564 if((rc = request_irq(netdev->irq, &ibmveth_interrupt, 0, netdev->name, netdev)) != 0) {
565 ibmveth_error_printk("unable to request irq 0x%x, rc %d\n", netdev->irq, rc);
566 do {
567 rc = h_free_logical_lan(adapter->vdev->unit_address);
706c8c93 568 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
1da177e4
LT
569
570 ibmveth_cleanup(adapter);
571 return rc;
572 }
573
e2adbcb4 574 ibmveth_debug_printk("initial replenish cycle\n");
7d12e780 575 ibmveth_interrupt(netdev->irq, netdev);
1da177e4 576
e2adbcb4 577 netif_start_queue(netdev);
1da177e4
LT
578
579 ibmveth_debug_printk("open complete\n");
580
581 return 0;
582}
583
584static int ibmveth_close(struct net_device *netdev)
585{
586 struct ibmveth_adapter *adapter = netdev->priv;
587 long lpar_rc;
d7fbeba6 588
1da177e4
LT
589 ibmveth_debug_printk("close starting\n");
590
860f242e
SL
591 if (!adapter->pool_config)
592 netif_stop_queue(netdev);
1da177e4
LT
593
594 free_irq(netdev->irq, netdev);
595
1da177e4
LT
596 do {
597 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
706c8c93 598 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
1da177e4 599
706c8c93 600 if(lpar_rc != H_SUCCESS)
1da177e4
LT
601 {
602 ibmveth_error_printk("h_free_logical_lan failed with %lx, continuing with close\n",
603 lpar_rc);
604 }
605
606 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
607
608 ibmveth_cleanup(adapter);
609
610 ibmveth_debug_printk("close complete\n");
611
612 return 0;
613}
614
615static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
616 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
617 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | ADVERTISED_FIBRE);
618 cmd->speed = SPEED_1000;
619 cmd->duplex = DUPLEX_FULL;
620 cmd->port = PORT_FIBRE;
621 cmd->phy_address = 0;
622 cmd->transceiver = XCVR_INTERNAL;
623 cmd->autoneg = AUTONEG_ENABLE;
624 cmd->maxtxpkt = 0;
625 cmd->maxrxpkt = 1;
626 return 0;
627}
628
629static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info) {
630 strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
631 strncpy(info->version, ibmveth_driver_version, sizeof(info->version) - 1);
632}
633
634static u32 netdev_get_link(struct net_device *dev) {
635 return 1;
636}
637
7282d491 638static const struct ethtool_ops netdev_ethtool_ops = {
1da177e4
LT
639 .get_drvinfo = netdev_get_drvinfo,
640 .get_settings = netdev_get_settings,
641 .get_link = netdev_get_link,
642 .get_sg = ethtool_op_get_sg,
643 .get_tx_csum = ethtool_op_get_tx_csum,
644};
645
646static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
647{
648 return -EOPNOTSUPP;
649}
650
651#define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
652
653static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *netdev)
654{
655 struct ibmveth_adapter *adapter = netdev->priv;
656 union ibmveth_buf_desc desc[IbmVethMaxSendFrags];
657 unsigned long lpar_rc;
658 int nfrags = 0, curfrag;
659 unsigned long correlator;
60296d9e 660 unsigned long flags;
1da177e4 661 unsigned int retry_count;
60296d9e
SL
662 unsigned int tx_dropped = 0;
663 unsigned int tx_bytes = 0;
664 unsigned int tx_packets = 0;
665 unsigned int tx_send_failed = 0;
666 unsigned int tx_map_failed = 0;
667
1da177e4
LT
668
669 if ((skb_shinfo(skb)->nr_frags + 1) > IbmVethMaxSendFrags) {
60296d9e
SL
670 tx_dropped++;
671 goto out;
1da177e4
LT
672 }
673
674 memset(&desc, 0, sizeof(desc));
675
676 /* nfrags = number of frags after the initial fragment */
677 nfrags = skb_shinfo(skb)->nr_frags;
678
679 if(nfrags)
680 adapter->tx_multidesc_send++;
681
682 /* map the initial fragment */
683 desc[0].fields.length = nfrags ? skb->len - skb->data_len : skb->len;
684 desc[0].fields.address = dma_map_single(&adapter->vdev->dev, skb->data,
685 desc[0].fields.length, DMA_TO_DEVICE);
686 desc[0].fields.valid = 1;
687
688 if(dma_mapping_error(desc[0].fields.address)) {
689 ibmveth_error_printk("tx: unable to map initial fragment\n");
60296d9e
SL
690 tx_map_failed++;
691 tx_dropped++;
692 goto out;
1da177e4
LT
693 }
694
695 curfrag = nfrags;
696
697 /* map fragments past the initial portion if there are any */
698 while(curfrag--) {
699 skb_frag_t *frag = &skb_shinfo(skb)->frags[curfrag];
700 desc[curfrag+1].fields.address
701 = dma_map_single(&adapter->vdev->dev,
702 page_address(frag->page) + frag->page_offset,
703 frag->size, DMA_TO_DEVICE);
704 desc[curfrag+1].fields.length = frag->size;
705 desc[curfrag+1].fields.valid = 1;
706
707 if(dma_mapping_error(desc[curfrag+1].fields.address)) {
708 ibmveth_error_printk("tx: unable to map fragment %d\n", curfrag);
60296d9e
SL
709 tx_map_failed++;
710 tx_dropped++;
1da177e4
LT
711 /* Free all the mappings we just created */
712 while(curfrag < nfrags) {
713 dma_unmap_single(&adapter->vdev->dev,
714 desc[curfrag+1].fields.address,
715 desc[curfrag+1].fields.length,
716 DMA_TO_DEVICE);
717 curfrag++;
718 }
60296d9e 719 goto out;
1da177e4
LT
720 }
721 }
722
723 /* send the frame. Arbitrarily set retrycount to 1024 */
724 correlator = 0;
725 retry_count = 1024;
726 do {
727 lpar_rc = h_send_logical_lan(adapter->vdev->unit_address,
728 desc[0].desc,
729 desc[1].desc,
730 desc[2].desc,
731 desc[3].desc,
732 desc[4].desc,
733 desc[5].desc,
b9377ffc
AB
734 correlator,
735 &correlator);
706c8c93 736 } while ((lpar_rc == H_BUSY) && (retry_count--));
d7fbeba6 737
706c8c93 738 if(lpar_rc != H_SUCCESS && lpar_rc != H_DROPPED) {
1da177e4
LT
739 int i;
740 ibmveth_error_printk("tx: h_send_logical_lan failed with rc=%ld\n", lpar_rc);
741 for(i = 0; i < 6; i++) {
742 ibmveth_error_printk("tx: desc[%i] valid=%d, len=%d, address=0x%d\n", i,
743 desc[i].fields.valid, desc[i].fields.length, desc[i].fields.address);
744 }
60296d9e
SL
745 tx_send_failed++;
746 tx_dropped++;
1da177e4 747 } else {
60296d9e
SL
748 tx_packets++;
749 tx_bytes += skb->len;
0abe791e 750 netdev->trans_start = jiffies;
1da177e4
LT
751 }
752
753 do {
754 dma_unmap_single(&adapter->vdev->dev,
755 desc[nfrags].fields.address,
756 desc[nfrags].fields.length, DMA_TO_DEVICE);
757 } while(--nfrags >= 0);
758
60296d9e
SL
759out: spin_lock_irqsave(&adapter->stats_lock, flags);
760 adapter->stats.tx_dropped += tx_dropped;
761 adapter->stats.tx_bytes += tx_bytes;
762 adapter->stats.tx_packets += tx_packets;
763 adapter->tx_send_failed += tx_send_failed;
764 adapter->tx_map_failed += tx_map_failed;
765 spin_unlock_irqrestore(&adapter->stats_lock, flags);
766
1da177e4
LT
767 dev_kfree_skb(skb);
768 return 0;
769}
770
771static int ibmveth_poll(struct net_device *netdev, int *budget)
772{
773 struct ibmveth_adapter *adapter = netdev->priv;
774 int max_frames_to_process = netdev->quota;
775 int frames_processed = 0;
776 int more_work = 1;
777 unsigned long lpar_rc;
778
779 restart_poll:
780 do {
781 struct net_device *netdev = adapter->netdev;
782
783 if(ibmveth_rxq_pending_buffer(adapter)) {
784 struct sk_buff *skb;
785
786 rmb();
787
788 if(!ibmveth_rxq_buffer_valid(adapter)) {
789 wmb(); /* suggested by larson1 */
790 adapter->rx_invalid_buffer++;
791 ibmveth_debug_printk("recycling invalid buffer\n");
792 ibmveth_rxq_recycle_buffer(adapter);
793 } else {
794 int length = ibmveth_rxq_frame_length(adapter);
795 int offset = ibmveth_rxq_frame_offset(adapter);
796 skb = ibmveth_rxq_get_buffer(adapter);
797
798 ibmveth_rxq_harvest_buffer(adapter);
799
800 skb_reserve(skb, offset);
801 skb_put(skb, length);
802 skb->dev = netdev;
803 skb->protocol = eth_type_trans(skb, netdev);
804
805 netif_receive_skb(skb); /* send it up */
806
807 adapter->stats.rx_packets++;
808 adapter->stats.rx_bytes += length;
809 frames_processed++;
0abe791e 810 netdev->last_rx = jiffies;
1da177e4
LT
811 }
812 } else {
813 more_work = 0;
814 }
815 } while(more_work && (frames_processed < max_frames_to_process));
816
e2adbcb4 817 ibmveth_replenish_task(adapter);
1da177e4
LT
818
819 if(more_work) {
820 /* more work to do - return that we are not done yet */
821 netdev->quota -= frames_processed;
822 *budget -= frames_processed;
d7fbeba6 823 return 1;
1da177e4
LT
824 }
825
826 /* we think we are done - reenable interrupts, then check once more to make sure we are done */
827 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_ENABLE);
828
706c8c93 829 ibmveth_assert(lpar_rc == H_SUCCESS);
1da177e4
LT
830
831 netif_rx_complete(netdev);
832
833 if(ibmveth_rxq_pending_buffer(adapter) && netif_rx_reschedule(netdev, frames_processed))
834 {
835 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
706c8c93 836 ibmveth_assert(lpar_rc == H_SUCCESS);
1da177e4
LT
837 more_work = 1;
838 goto restart_poll;
839 }
840
841 netdev->quota -= frames_processed;
842 *budget -= frames_processed;
843
844 /* we really are done */
845 return 0;
846}
847
7d12e780 848static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
d7fbeba6 849{
1da177e4
LT
850 struct net_device *netdev = dev_instance;
851 struct ibmveth_adapter *adapter = netdev->priv;
852 unsigned long lpar_rc;
853
854 if(netif_rx_schedule_prep(netdev)) {
855 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
706c8c93 856 ibmveth_assert(lpar_rc == H_SUCCESS);
1da177e4
LT
857 __netif_rx_schedule(netdev);
858 }
859 return IRQ_HANDLED;
860}
861
862static struct net_device_stats *ibmveth_get_stats(struct net_device *dev)
863{
864 struct ibmveth_adapter *adapter = dev->priv;
865 return &adapter->stats;
866}
867
868static void ibmveth_set_multicast_list(struct net_device *netdev)
869{
870 struct ibmveth_adapter *adapter = netdev->priv;
871 unsigned long lpar_rc;
872
873 if((netdev->flags & IFF_PROMISC) || (netdev->mc_count > adapter->mcastFilterSize)) {
874 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
875 IbmVethMcastEnableRecv |
876 IbmVethMcastDisableFiltering,
877 0);
706c8c93 878 if(lpar_rc != H_SUCCESS) {
1da177e4
LT
879 ibmveth_error_printk("h_multicast_ctrl rc=%ld when entering promisc mode\n", lpar_rc);
880 }
881 } else {
882 struct dev_mc_list *mclist = netdev->mc_list;
883 int i;
884 /* clear the filter table & disable filtering */
885 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
886 IbmVethMcastEnableRecv |
887 IbmVethMcastDisableFiltering |
888 IbmVethMcastClearFilterTable,
889 0);
706c8c93 890 if(lpar_rc != H_SUCCESS) {
1da177e4
LT
891 ibmveth_error_printk("h_multicast_ctrl rc=%ld when attempting to clear filter table\n", lpar_rc);
892 }
893 /* add the addresses to the filter table */
894 for(i = 0; i < netdev->mc_count; ++i, mclist = mclist->next) {
895 // add the multicast address to the filter table
896 unsigned long mcast_addr = 0;
897 memcpy(((char *)&mcast_addr)+2, mclist->dmi_addr, 6);
898 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
899 IbmVethMcastAddFilter,
900 mcast_addr);
706c8c93 901 if(lpar_rc != H_SUCCESS) {
1da177e4
LT
902 ibmveth_error_printk("h_multicast_ctrl rc=%ld when adding an entry to the filter table\n", lpar_rc);
903 }
904 }
d7fbeba6 905
1da177e4
LT
906 /* re-enable filtering */
907 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
908 IbmVethMcastEnableFiltering,
909 0);
706c8c93 910 if(lpar_rc != H_SUCCESS) {
1da177e4
LT
911 ibmveth_error_printk("h_multicast_ctrl rc=%ld when enabling filtering\n", lpar_rc);
912 }
913 }
914}
915
916static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
917{
b6d35182 918 struct ibmveth_adapter *adapter = dev->priv;
860f242e 919 int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
b6d35182 920 int i;
b6d35182 921
860f242e 922 if (new_mtu < IBMVETH_MAX_MTU)
1da177e4 923 return -EINVAL;
b6d35182 924
860f242e 925 /* Look for an active buffer pool that can hold the new MTU */
b6d35182 926 for(i = 0; i<IbmVethNumBufferPools; i++) {
860f242e
SL
927 if (!adapter->rx_buff_pool[i].active)
928 continue;
929 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size) {
930 dev->mtu = new_mtu;
931 return 0;
b6d35182 932 }
b6d35182 933 }
860f242e 934 return -EINVAL;
1da177e4
LT
935}
936
6b422374
SL
937#ifdef CONFIG_NET_POLL_CONTROLLER
938static void ibmveth_poll_controller(struct net_device *dev)
939{
940 ibmveth_replenish_task(dev->priv);
5f77113c 941 ibmveth_interrupt(dev->irq, dev);
6b422374
SL
942}
943#endif
944
1da177e4
LT
945static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
946{
b6d35182 947 int rc, i;
1da177e4 948 struct net_device *netdev;
b6d35182 949 struct ibmveth_adapter *adapter = NULL;
1da177e4
LT
950
951 unsigned char *mac_addr_p;
952 unsigned int *mcastFilterSize_p;
953
954
d7fbeba6 955 ibmveth_debug_printk_no_adapter("entering ibmveth_probe for UA 0x%x\n",
1da177e4
LT
956 dev->unit_address);
957
958 mac_addr_p = (unsigned char *) vio_get_attribute(dev, VETH_MAC_ADDR, 0);
959 if(!mac_addr_p) {
960 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find VETH_MAC_ADDR "
961 "attribute\n", __FILE__, __LINE__);
962 return 0;
963 }
d7fbeba6 964
1da177e4
LT
965 mcastFilterSize_p= (unsigned int *) vio_get_attribute(dev, VETH_MCAST_FILTER_SIZE, 0);
966 if(!mcastFilterSize_p) {
967 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find "
968 "VETH_MCAST_FILTER_SIZE attribute\n",
969 __FILE__, __LINE__);
970 return 0;
971 }
d7fbeba6 972
1da177e4
LT
973 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
974
975 if(!netdev)
976 return -ENOMEM;
977
978 SET_MODULE_OWNER(netdev);
979
980 adapter = netdev->priv;
981 memset(adapter, 0, sizeof(adapter));
982 dev->dev.driver_data = netdev;
983
984 adapter->vdev = dev;
985 adapter->netdev = netdev;
986 adapter->mcastFilterSize= *mcastFilterSize_p;
860f242e 987 adapter->pool_config = 0;
d7fbeba6 988
1da177e4 989 /* Some older boxes running PHYP non-natively have an OF that
d7fbeba6 990 returns a 8-byte local-mac-address field (and the first
1da177e4 991 2 bytes have to be ignored) while newer boxes' OF return
d7fbeba6 992 a 6-byte field. Note that IEEE 1275 specifies that
1da177e4 993 local-mac-address must be a 6-byte field.
d7fbeba6 994 The RPA doc specifies that the first byte must be 10b, so
1da177e4
LT
995 we'll just look for it to solve this 8 vs. 6 byte field issue */
996
997 if ((*mac_addr_p & 0x3) != 0x02)
998 mac_addr_p += 2;
999
1000 adapter->mac_addr = 0;
1001 memcpy(&adapter->mac_addr, mac_addr_p, 6);
1002
1003 adapter->liobn = dev->iommu_table->it_index;
d7fbeba6 1004
1da177e4
LT
1005 netdev->irq = dev->irq;
1006 netdev->open = ibmveth_open;
1007 netdev->poll = ibmveth_poll;
1008 netdev->weight = 16;
1009 netdev->stop = ibmveth_close;
1010 netdev->hard_start_xmit = ibmveth_start_xmit;
1011 netdev->get_stats = ibmveth_get_stats;
1012 netdev->set_multicast_list = ibmveth_set_multicast_list;
1013 netdev->do_ioctl = ibmveth_ioctl;
1014 netdev->ethtool_ops = &netdev_ethtool_ops;
1015 netdev->change_mtu = ibmveth_change_mtu;
1016 SET_NETDEV_DEV(netdev, &dev->dev);
6b422374
SL
1017#ifdef CONFIG_NET_POLL_CONTROLLER
1018 netdev->poll_controller = ibmveth_poll_controller;
1019#endif
d7fbeba6 1020 netdev->features |= NETIF_F_LLTX;
60296d9e 1021 spin_lock_init(&adapter->stats_lock);
1da177e4
LT
1022
1023 memcpy(&netdev->dev_addr, &adapter->mac_addr, netdev->addr_len);
1024
860f242e
SL
1025 for(i = 0; i<IbmVethNumBufferPools; i++) {
1026 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
d7fbeba6
JG
1027 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1028 pool_count[i], pool_size[i],
860f242e
SL
1029 pool_active[i]);
1030 kobj->parent = &dev->dev.kobj;
1031 sprintf(kobj->name, "pool%d", i);
1032 kobj->ktype = &ktype_veth_pool;
1033 kobject_register(kobj);
1034 }
1da177e4
LT
1035
1036 ibmveth_debug_printk("adapter @ 0x%p\n", adapter);
1037
1da177e4
LT
1038 adapter->buffer_list_dma = DMA_ERROR_CODE;
1039 adapter->filter_list_dma = DMA_ERROR_CODE;
1040 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1041
1da177e4
LT
1042 ibmveth_debug_printk("registering netdev...\n");
1043
1044 rc = register_netdev(netdev);
1045
1046 if(rc) {
1047 ibmveth_debug_printk("failed to register netdev rc=%d\n", rc);
1048 free_netdev(netdev);
1049 return rc;
1050 }
1051
1052 ibmveth_debug_printk("registered\n");
1053
1054 ibmveth_proc_register_adapter(adapter);
1055
1056 return 0;
1057}
1058
1059static int __devexit ibmveth_remove(struct vio_dev *dev)
1060{
1061 struct net_device *netdev = dev->dev.driver_data;
1062 struct ibmveth_adapter *adapter = netdev->priv;
860f242e
SL
1063 int i;
1064
1065 for(i = 0; i<IbmVethNumBufferPools; i++)
1066 kobject_unregister(&adapter->rx_buff_pool[i].kobj);
1da177e4
LT
1067
1068 unregister_netdev(netdev);
1069
1070 ibmveth_proc_unregister_adapter(adapter);
1071
1072 free_netdev(netdev);
1073 return 0;
1074}
1075
1076#ifdef CONFIG_PROC_FS
1077static void ibmveth_proc_register_driver(void)
1078{
66600221 1079 ibmveth_proc_dir = proc_mkdir(IBMVETH_PROC_DIR, NULL);
1da177e4
LT
1080 if (ibmveth_proc_dir) {
1081 SET_MODULE_OWNER(ibmveth_proc_dir);
1082 }
1083}
1084
1085static void ibmveth_proc_unregister_driver(void)
1086{
66600221 1087 remove_proc_entry(IBMVETH_PROC_DIR, NULL);
1da177e4
LT
1088}
1089
d7fbeba6 1090static void *ibmveth_seq_start(struct seq_file *seq, loff_t *pos)
1da177e4
LT
1091{
1092 if (*pos == 0) {
1093 return (void *)1;
1094 } else {
1095 return NULL;
1096 }
1097}
1098
1099static void *ibmveth_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1100{
1101 ++*pos;
1102 return NULL;
1103}
1104
d7fbeba6 1105static void ibmveth_seq_stop(struct seq_file *seq, void *v)
1da177e4
LT
1106{
1107}
1108
d7fbeba6 1109static int ibmveth_seq_show(struct seq_file *seq, void *v)
1da177e4
LT
1110{
1111 struct ibmveth_adapter *adapter = seq->private;
1112 char *current_mac = ((char*) &adapter->netdev->dev_addr);
1113 char *firmware_mac = ((char*) &adapter->mac_addr) ;
1114
1115 seq_printf(seq, "%s %s\n\n", ibmveth_driver_string, ibmveth_driver_version);
d7fbeba6 1116
1da177e4
LT
1117 seq_printf(seq, "Unit Address: 0x%x\n", adapter->vdev->unit_address);
1118 seq_printf(seq, "LIOBN: 0x%lx\n", adapter->liobn);
1119 seq_printf(seq, "Current MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
1120 current_mac[0], current_mac[1], current_mac[2],
1121 current_mac[3], current_mac[4], current_mac[5]);
1122 seq_printf(seq, "Firmware MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
1123 firmware_mac[0], firmware_mac[1], firmware_mac[2],
1124 firmware_mac[3], firmware_mac[4], firmware_mac[5]);
d7fbeba6 1125
1da177e4
LT
1126 seq_printf(seq, "\nAdapter Statistics:\n");
1127 seq_printf(seq, " TX: skbuffs linearized: %ld\n", adapter->tx_linearized);
1128 seq_printf(seq, " multi-descriptor sends: %ld\n", adapter->tx_multidesc_send);
1129 seq_printf(seq, " skb_linearize failures: %ld\n", adapter->tx_linearize_failed);
1130 seq_printf(seq, " vio_map_single failres: %ld\n", adapter->tx_map_failed);
1131 seq_printf(seq, " send failures: %ld\n", adapter->tx_send_failed);
1132 seq_printf(seq, " RX: replenish task cycles: %ld\n", adapter->replenish_task_cycles);
1133 seq_printf(seq, " alloc_skb_failures: %ld\n", adapter->replenish_no_mem);
1134 seq_printf(seq, " add buffer failures: %ld\n", adapter->replenish_add_buff_failure);
1135 seq_printf(seq, " invalid buffers: %ld\n", adapter->rx_invalid_buffer);
1136 seq_printf(seq, " no buffers: %ld\n", adapter->rx_no_buffer);
d7fbeba6 1137
1da177e4
LT
1138 return 0;
1139}
1140static struct seq_operations ibmveth_seq_ops = {
1141 .start = ibmveth_seq_start,
1142 .next = ibmveth_seq_next,
1143 .stop = ibmveth_seq_stop,
1144 .show = ibmveth_seq_show,
1145};
1146
1147static int ibmveth_proc_open(struct inode *inode, struct file *file)
1148{
1149 struct seq_file *seq;
1150 struct proc_dir_entry *proc;
1151 int rc;
1152
1153 rc = seq_open(file, &ibmveth_seq_ops);
1154 if (!rc) {
1155 /* recover the pointer buried in proc_dir_entry data */
1156 seq = file->private_data;
1157 proc = PDE(inode);
1158 seq->private = proc->data;
1159 }
1160 return rc;
1161}
1162
1163static struct file_operations ibmveth_proc_fops = {
1164 .owner = THIS_MODULE,
1165 .open = ibmveth_proc_open,
1166 .read = seq_read,
1167 .llseek = seq_lseek,
1168 .release = seq_release,
1169};
1170
1171static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1172{
1173 struct proc_dir_entry *entry;
1174 if (ibmveth_proc_dir) {
03a85d09
SL
1175 char u_addr[10];
1176 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1177 entry = create_proc_entry(u_addr, S_IFREG, ibmveth_proc_dir);
1da177e4
LT
1178 if (!entry) {
1179 ibmveth_error_printk("Cannot create adapter proc entry");
1180 } else {
1181 entry->data = (void *) adapter;
1182 entry->proc_fops = &ibmveth_proc_fops;
1183 SET_MODULE_OWNER(entry);
1184 }
1185 }
1186 return;
1187}
1188
1189static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1190{
1191 if (ibmveth_proc_dir) {
03a85d09
SL
1192 char u_addr[10];
1193 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1194 remove_proc_entry(u_addr, ibmveth_proc_dir);
1da177e4
LT
1195 }
1196}
1197
1198#else /* CONFIG_PROC_FS */
d7fbeba6 1199static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1da177e4
LT
1200{
1201}
1202
d7fbeba6 1203static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1da177e4
LT
1204{
1205}
1206static void ibmveth_proc_register_driver(void)
1207{
1208}
1209
1210static void ibmveth_proc_unregister_driver(void)
1211{
1212}
1213#endif /* CONFIG_PROC_FS */
1214
860f242e
SL
1215static struct attribute veth_active_attr;
1216static struct attribute veth_num_attr;
1217static struct attribute veth_size_attr;
1218
1219static ssize_t veth_pool_show(struct kobject * kobj,
1220 struct attribute * attr, char * buf)
1221{
d7fbeba6 1222 struct ibmveth_buff_pool *pool = container_of(kobj,
860f242e
SL
1223 struct ibmveth_buff_pool,
1224 kobj);
1225
1226 if (attr == &veth_active_attr)
1227 return sprintf(buf, "%d\n", pool->active);
1228 else if (attr == &veth_num_attr)
1229 return sprintf(buf, "%d\n", pool->size);
1230 else if (attr == &veth_size_attr)
1231 return sprintf(buf, "%d\n", pool->buff_size);
1232 return 0;
1233}
1234
1235static ssize_t veth_pool_store(struct kobject * kobj, struct attribute * attr,
1236const char * buf, size_t count)
1237{
d7fbeba6 1238 struct ibmveth_buff_pool *pool = container_of(kobj,
860f242e
SL
1239 struct ibmveth_buff_pool,
1240 kobj);
d7fbeba6 1241 struct net_device *netdev =
860f242e
SL
1242 container_of(kobj->parent, struct device, kobj)->driver_data;
1243 struct ibmveth_adapter *adapter = netdev->priv;
1244 long value = simple_strtol(buf, NULL, 10);
1245 long rc;
1246
1247 if (attr == &veth_active_attr) {
1248 if (value && !pool->active) {
1249 if(ibmveth_alloc_buffer_pool(pool)) {
1250 ibmveth_error_printk("unable to alloc pool\n");
1251 return -ENOMEM;
1252 }
1253 pool->active = 1;
1254 adapter->pool_config = 1;
1255 ibmveth_close(netdev);
1256 adapter->pool_config = 0;
1257 if ((rc = ibmveth_open(netdev)))
1258 return rc;
1259 } else if (!value && pool->active) {
1260 int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1261 int i;
1262 /* Make sure there is a buffer pool with buffers that
1263 can hold a packet of the size of the MTU */
1264 for(i = 0; i<IbmVethNumBufferPools; i++) {
1265 if (pool == &adapter->rx_buff_pool[i])
1266 continue;
1267 if (!adapter->rx_buff_pool[i].active)
1268 continue;
1269 if (mtu < adapter->rx_buff_pool[i].buff_size) {
1270 pool->active = 0;
1271 h_free_logical_lan_buffer(adapter->
1272 vdev->
1273 unit_address,
1274 pool->
1275 buff_size);
1276 }
1277 }
1278 if (pool->active) {
1279 ibmveth_error_printk("no active pool >= MTU\n");
1280 return -EPERM;
1281 }
1282 }
1283 } else if (attr == &veth_num_attr) {
1284 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT)
1285 return -EINVAL;
1286 else {
1287 adapter->pool_config = 1;
1288 ibmveth_close(netdev);
1289 adapter->pool_config = 0;
1290 pool->size = value;
1291 if ((rc = ibmveth_open(netdev)))
1292 return rc;
1293 }
1294 } else if (attr == &veth_size_attr) {
1295 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE)
1296 return -EINVAL;
1297 else {
1298 adapter->pool_config = 1;
1299 ibmveth_close(netdev);
1300 adapter->pool_config = 0;
1301 pool->buff_size = value;
1302 if ((rc = ibmveth_open(netdev)))
1303 return rc;
1304 }
1305 }
1306
1307 /* kick the interrupt handler to allocate/deallocate pools */
7d12e780 1308 ibmveth_interrupt(netdev->irq, netdev);
860f242e
SL
1309 return count;
1310}
1311
1312
1313#define ATTR(_name, _mode) \
1314 struct attribute veth_##_name##_attr = { \
1315 .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE \
1316 };
1317
1318static ATTR(active, 0644);
1319static ATTR(num, 0644);
1320static ATTR(size, 0644);
1321
1322static struct attribute * veth_pool_attrs[] = {
1323 &veth_active_attr,
1324 &veth_num_attr,
1325 &veth_size_attr,
1326 NULL,
1327};
1328
1329static struct sysfs_ops veth_pool_ops = {
1330 .show = veth_pool_show,
1331 .store = veth_pool_store,
1332};
1333
1334static struct kobj_type ktype_veth_pool = {
1335 .release = NULL,
1336 .sysfs_ops = &veth_pool_ops,
1337 .default_attrs = veth_pool_attrs,
1338};
1339
1340
1da177e4
LT
1341static struct vio_device_id ibmveth_device_table[] __devinitdata= {
1342 { "network", "IBM,l-lan"},
fb120da6 1343 { "", "" }
1da177e4 1344};
1da177e4
LT
1345MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1346
1347static struct vio_driver ibmveth_driver = {
6fdf5392
SR
1348 .id_table = ibmveth_device_table,
1349 .probe = ibmveth_probe,
1350 .remove = ibmveth_remove,
1351 .driver = {
1352 .name = ibmveth_driver_name,
915124d8 1353 .owner = THIS_MODULE,
6fdf5392 1354 }
1da177e4
LT
1355};
1356
1357static int __init ibmveth_module_init(void)
1358{
1359 ibmveth_printk("%s: %s %s\n", ibmveth_driver_name, ibmveth_driver_string, ibmveth_driver_version);
1360
1361 ibmveth_proc_register_driver();
1362
1363 return vio_register_driver(&ibmveth_driver);
1364}
1365
1366static void __exit ibmveth_module_exit(void)
1367{
1368 vio_unregister_driver(&ibmveth_driver);
1369 ibmveth_proc_unregister_driver();
d7fbeba6 1370}
1da177e4
LT
1371
1372module_init(ibmveth_module_init);
1373module_exit(ibmveth_module_exit);