x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / drivers / net / ibmveth.c
blob5862282ab2fef6eb3b90b2db059b126d138b2d73
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. */
27 /* */
28 /**************************************************************************/
30 TODO:
31 - add support for sysfs
32 - possibly remove procfs support
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
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 <linux/in.h>
51 #include <linux/ip.h>
52 #include <net/net_namespace.h>
53 #include <asm/hvcall.h>
54 #include <asm/atomic.h>
55 #include <asm/vio.h>
56 #include <asm/iommu.h>
57 #include <asm/uaccess.h>
58 #include <asm/firmware.h>
59 #include <linux/seq_file.h>
61 #include "ibmveth.h"
63 #undef DEBUG
65 #define ibmveth_printk(fmt, args...) \
66 printk(KERN_DEBUG "%s: " fmt, __FILE__, ## args)
68 #define ibmveth_error_printk(fmt, args...) \
69 printk(KERN_ERR "(%s:%3.3d ua:%x) ERROR: " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
71 #ifdef DEBUG
72 #define ibmveth_debug_printk_no_adapter(fmt, args...) \
73 printk(KERN_DEBUG "(%s:%3.3d): " fmt, __FILE__, __LINE__ , ## args)
74 #define ibmveth_debug_printk(fmt, args...) \
75 printk(KERN_DEBUG "(%s:%3.3d ua:%x): " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
76 #define ibmveth_assert(expr) \
77 if(!(expr)) { \
78 printk(KERN_DEBUG "assertion failed (%s:%3.3d ua:%x): %s\n", __FILE__, __LINE__, adapter->vdev->unit_address, #expr); \
79 BUG(); \
81 #else
82 #define ibmveth_debug_printk_no_adapter(fmt, args...)
83 #define ibmveth_debug_printk(fmt, args...)
84 #define ibmveth_assert(expr)
85 #endif
87 static int ibmveth_open(struct net_device *dev);
88 static int ibmveth_close(struct net_device *dev);
89 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
90 static int ibmveth_poll(struct napi_struct *napi, int budget);
91 static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *dev);
92 static void ibmveth_set_multicast_list(struct net_device *dev);
93 static int ibmveth_change_mtu(struct net_device *dev, int new_mtu);
94 static void ibmveth_proc_register_driver(void);
95 static void ibmveth_proc_unregister_driver(void);
96 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter);
97 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter);
98 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
99 static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
100 static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev);
101 static struct kobj_type ktype_veth_pool;
104 #ifdef CONFIG_PROC_FS
105 #define IBMVETH_PROC_DIR "ibmveth"
106 static struct proc_dir_entry *ibmveth_proc_dir;
107 #endif
109 static const char ibmveth_driver_name[] = "ibmveth";
110 static const char ibmveth_driver_string[] = "IBM i/pSeries Virtual Ethernet Driver";
111 #define ibmveth_driver_version "1.03"
113 MODULE_AUTHOR("Santiago Leon <santil@us.ibm.com>");
114 MODULE_DESCRIPTION("IBM i/pSeries Virtual Ethernet Driver");
115 MODULE_LICENSE("GPL");
116 MODULE_VERSION(ibmveth_driver_version);
118 struct ibmveth_stat {
119 char name[ETH_GSTRING_LEN];
120 int offset;
123 #define IBMVETH_STAT_OFF(stat) offsetof(struct ibmveth_adapter, stat)
124 #define IBMVETH_GET_STAT(a, off) *((u64 *)(((unsigned long)(a)) + off))
126 struct ibmveth_stat ibmveth_stats[] = {
127 { "replenish_task_cycles", IBMVETH_STAT_OFF(replenish_task_cycles) },
128 { "replenish_no_mem", IBMVETH_STAT_OFF(replenish_no_mem) },
129 { "replenish_add_buff_failure", IBMVETH_STAT_OFF(replenish_add_buff_failure) },
130 { "replenish_add_buff_success", IBMVETH_STAT_OFF(replenish_add_buff_success) },
131 { "rx_invalid_buffer", IBMVETH_STAT_OFF(rx_invalid_buffer) },
132 { "rx_no_buffer", IBMVETH_STAT_OFF(rx_no_buffer) },
133 { "tx_map_failed", IBMVETH_STAT_OFF(tx_map_failed) },
134 { "tx_send_failed", IBMVETH_STAT_OFF(tx_send_failed) },
137 /* simple methods of getting data from the current rxq entry */
138 static inline u32 ibmveth_rxq_flags(struct ibmveth_adapter *adapter)
140 return adapter->rx_queue.queue_addr[adapter->rx_queue.index].flags_off;
143 static inline int ibmveth_rxq_toggle(struct ibmveth_adapter *adapter)
145 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_TOGGLE) >> IBMVETH_RXQ_TOGGLE_SHIFT;
148 static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
150 return (ibmveth_rxq_toggle(adapter) == adapter->rx_queue.toggle);
153 static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
155 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_VALID);
158 static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
160 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_OFF_MASK);
163 static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
165 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
168 static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
170 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_CSUM_GOOD);
173 /* setup the initial settings for a buffer pool */
174 static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool, u32 pool_index, u32 pool_size, u32 buff_size, u32 pool_active)
176 pool->size = pool_size;
177 pool->index = pool_index;
178 pool->buff_size = buff_size;
179 pool->threshold = pool_size / 2;
180 pool->active = pool_active;
183 /* allocate and setup an buffer pool - called during open */
184 static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
186 int i;
188 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
190 if(!pool->free_map) {
191 return -1;
194 pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
195 if(!pool->dma_addr) {
196 kfree(pool->free_map);
197 pool->free_map = NULL;
198 return -1;
201 pool->skbuff = kmalloc(sizeof(void*) * pool->size, GFP_KERNEL);
203 if(!pool->skbuff) {
204 kfree(pool->dma_addr);
205 pool->dma_addr = NULL;
207 kfree(pool->free_map);
208 pool->free_map = NULL;
209 return -1;
212 memset(pool->skbuff, 0, sizeof(void*) * pool->size);
213 memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
215 for(i = 0; i < pool->size; ++i) {
216 pool->free_map[i] = i;
219 atomic_set(&pool->available, 0);
220 pool->producer_index = 0;
221 pool->consumer_index = 0;
223 return 0;
226 /* replenish the buffers for a pool. note that we don't need to
227 * skb_reserve these since they are used for incoming...
229 static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
231 u32 i;
232 u32 count = pool->size - atomic_read(&pool->available);
233 u32 buffers_added = 0;
234 struct sk_buff *skb;
235 unsigned int free_index, index;
236 u64 correlator;
237 unsigned long lpar_rc;
238 dma_addr_t dma_addr;
240 mb();
242 for(i = 0; i < count; ++i) {
243 union ibmveth_buf_desc desc;
245 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
247 if(!skb) {
248 ibmveth_debug_printk("replenish: unable to allocate skb\n");
249 adapter->replenish_no_mem++;
250 break;
253 free_index = pool->consumer_index;
254 pool->consumer_index = (pool->consumer_index + 1) % pool->size;
255 index = pool->free_map[free_index];
257 ibmveth_assert(index != IBM_VETH_INVALID_MAP);
258 ibmveth_assert(pool->skbuff[index] == NULL);
260 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
261 pool->buff_size, DMA_FROM_DEVICE);
263 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
264 goto failure;
266 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
267 pool->dma_addr[index] = dma_addr;
268 pool->skbuff[index] = skb;
270 correlator = ((u64)pool->index << 32) | index;
271 *(u64*)skb->data = correlator;
273 desc.fields.flags_len = IBMVETH_BUF_VALID | pool->buff_size;
274 desc.fields.address = dma_addr;
276 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
278 if (lpar_rc != H_SUCCESS)
279 goto failure;
280 else {
281 buffers_added++;
282 adapter->replenish_add_buff_success++;
286 mb();
287 atomic_add(buffers_added, &(pool->available));
288 return;
290 failure:
291 pool->free_map[free_index] = index;
292 pool->skbuff[index] = NULL;
293 if (pool->consumer_index == 0)
294 pool->consumer_index = pool->size - 1;
295 else
296 pool->consumer_index--;
297 if (!dma_mapping_error(&adapter->vdev->dev, dma_addr))
298 dma_unmap_single(&adapter->vdev->dev,
299 pool->dma_addr[index], pool->buff_size,
300 DMA_FROM_DEVICE);
301 dev_kfree_skb_any(skb);
302 adapter->replenish_add_buff_failure++;
304 mb();
305 atomic_add(buffers_added, &(pool->available));
308 /* replenish routine */
309 static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
311 int i;
313 adapter->replenish_task_cycles++;
315 for (i = (IbmVethNumBufferPools - 1); i >= 0; i--)
316 if(adapter->rx_buff_pool[i].active)
317 ibmveth_replenish_buffer_pool(adapter,
318 &adapter->rx_buff_pool[i]);
320 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
323 /* empty and free ana buffer pool - also used to do cleanup in error paths */
324 static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
326 int i;
328 kfree(pool->free_map);
329 pool->free_map = NULL;
331 if(pool->skbuff && pool->dma_addr) {
332 for(i = 0; i < pool->size; ++i) {
333 struct sk_buff *skb = pool->skbuff[i];
334 if(skb) {
335 dma_unmap_single(&adapter->vdev->dev,
336 pool->dma_addr[i],
337 pool->buff_size,
338 DMA_FROM_DEVICE);
339 dev_kfree_skb_any(skb);
340 pool->skbuff[i] = NULL;
345 if(pool->dma_addr) {
346 kfree(pool->dma_addr);
347 pool->dma_addr = NULL;
350 if(pool->skbuff) {
351 kfree(pool->skbuff);
352 pool->skbuff = NULL;
356 /* remove a buffer from a pool */
357 static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter, u64 correlator)
359 unsigned int pool = correlator >> 32;
360 unsigned int index = correlator & 0xffffffffUL;
361 unsigned int free_index;
362 struct sk_buff *skb;
364 ibmveth_assert(pool < IbmVethNumBufferPools);
365 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
367 skb = adapter->rx_buff_pool[pool].skbuff[index];
369 ibmveth_assert(skb != NULL);
371 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
373 dma_unmap_single(&adapter->vdev->dev,
374 adapter->rx_buff_pool[pool].dma_addr[index],
375 adapter->rx_buff_pool[pool].buff_size,
376 DMA_FROM_DEVICE);
378 free_index = adapter->rx_buff_pool[pool].producer_index;
379 adapter->rx_buff_pool[pool].producer_index
380 = (adapter->rx_buff_pool[pool].producer_index + 1)
381 % adapter->rx_buff_pool[pool].size;
382 adapter->rx_buff_pool[pool].free_map[free_index] = index;
384 mb();
386 atomic_dec(&(adapter->rx_buff_pool[pool].available));
389 /* get the current buffer on the rx queue */
390 static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
392 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
393 unsigned int pool = correlator >> 32;
394 unsigned int index = correlator & 0xffffffffUL;
396 ibmveth_assert(pool < IbmVethNumBufferPools);
397 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
399 return adapter->rx_buff_pool[pool].skbuff[index];
402 /* recycle the current buffer on the rx queue */
403 static void ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
405 u32 q_index = adapter->rx_queue.index;
406 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
407 unsigned int pool = correlator >> 32;
408 unsigned int index = correlator & 0xffffffffUL;
409 union ibmveth_buf_desc desc;
410 unsigned long lpar_rc;
412 ibmveth_assert(pool < IbmVethNumBufferPools);
413 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
415 if(!adapter->rx_buff_pool[pool].active) {
416 ibmveth_rxq_harvest_buffer(adapter);
417 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
418 return;
421 desc.fields.flags_len = IBMVETH_BUF_VALID |
422 adapter->rx_buff_pool[pool].buff_size;
423 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
425 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
427 if(lpar_rc != H_SUCCESS) {
428 ibmveth_debug_printk("h_add_logical_lan_buffer failed during recycle rc=%ld", lpar_rc);
429 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
432 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
433 adapter->rx_queue.index = 0;
434 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
438 static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
440 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
442 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
443 adapter->rx_queue.index = 0;
444 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
448 static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
450 int i;
451 struct device *dev = &adapter->vdev->dev;
453 if(adapter->buffer_list_addr != NULL) {
454 if (!dma_mapping_error(dev, adapter->buffer_list_dma)) {
455 dma_unmap_single(dev, adapter->buffer_list_dma, 4096,
456 DMA_BIDIRECTIONAL);
457 adapter->buffer_list_dma = DMA_ERROR_CODE;
459 free_page((unsigned long)adapter->buffer_list_addr);
460 adapter->buffer_list_addr = NULL;
463 if(adapter->filter_list_addr != NULL) {
464 if (!dma_mapping_error(dev, adapter->filter_list_dma)) {
465 dma_unmap_single(dev, adapter->filter_list_dma, 4096,
466 DMA_BIDIRECTIONAL);
467 adapter->filter_list_dma = DMA_ERROR_CODE;
469 free_page((unsigned long)adapter->filter_list_addr);
470 adapter->filter_list_addr = NULL;
473 if(adapter->rx_queue.queue_addr != NULL) {
474 if (!dma_mapping_error(dev, adapter->rx_queue.queue_dma)) {
475 dma_unmap_single(dev,
476 adapter->rx_queue.queue_dma,
477 adapter->rx_queue.queue_len,
478 DMA_BIDIRECTIONAL);
479 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
481 kfree(adapter->rx_queue.queue_addr);
482 adapter->rx_queue.queue_addr = NULL;
485 for(i = 0; i<IbmVethNumBufferPools; i++)
486 if (adapter->rx_buff_pool[i].active)
487 ibmveth_free_buffer_pool(adapter,
488 &adapter->rx_buff_pool[i]);
490 if (adapter->bounce_buffer != NULL) {
491 if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
492 dma_unmap_single(&adapter->vdev->dev,
493 adapter->bounce_buffer_dma,
494 adapter->netdev->mtu + IBMVETH_BUFF_OH,
495 DMA_BIDIRECTIONAL);
496 adapter->bounce_buffer_dma = DMA_ERROR_CODE;
498 kfree(adapter->bounce_buffer);
499 adapter->bounce_buffer = NULL;
503 static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
504 union ibmveth_buf_desc rxq_desc, u64 mac_address)
506 int rc, try_again = 1;
508 /* After a kexec the adapter will still be open, so our attempt to
509 * open it will fail. So if we get a failure we free the adapter and
510 * try again, but only once. */
511 retry:
512 rc = h_register_logical_lan(adapter->vdev->unit_address,
513 adapter->buffer_list_dma, rxq_desc.desc,
514 adapter->filter_list_dma, mac_address);
516 if (rc != H_SUCCESS && try_again) {
517 do {
518 rc = h_free_logical_lan(adapter->vdev->unit_address);
519 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
521 try_again = 0;
522 goto retry;
525 return rc;
528 static int ibmveth_open(struct net_device *netdev)
530 struct ibmveth_adapter *adapter = netdev_priv(netdev);
531 u64 mac_address = 0;
532 int rxq_entries = 1;
533 unsigned long lpar_rc;
534 int rc;
535 union ibmveth_buf_desc rxq_desc;
536 int i;
537 struct device *dev;
539 ibmveth_debug_printk("open starting\n");
541 napi_enable(&adapter->napi);
543 for(i = 0; i<IbmVethNumBufferPools; i++)
544 rxq_entries += adapter->rx_buff_pool[i].size;
546 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
547 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
549 if(!adapter->buffer_list_addr || !adapter->filter_list_addr) {
550 ibmveth_error_printk("unable to allocate filter or buffer list pages\n");
551 ibmveth_cleanup(adapter);
552 napi_disable(&adapter->napi);
553 return -ENOMEM;
556 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) * rxq_entries;
557 adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len, GFP_KERNEL);
559 if(!adapter->rx_queue.queue_addr) {
560 ibmveth_error_printk("unable to allocate rx queue pages\n");
561 ibmveth_cleanup(adapter);
562 napi_disable(&adapter->napi);
563 return -ENOMEM;
566 dev = &adapter->vdev->dev;
568 adapter->buffer_list_dma = dma_map_single(dev,
569 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
570 adapter->filter_list_dma = dma_map_single(dev,
571 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
572 adapter->rx_queue.queue_dma = dma_map_single(dev,
573 adapter->rx_queue.queue_addr,
574 adapter->rx_queue.queue_len, DMA_BIDIRECTIONAL);
576 if ((dma_mapping_error(dev, adapter->buffer_list_dma)) ||
577 (dma_mapping_error(dev, adapter->filter_list_dma)) ||
578 (dma_mapping_error(dev, adapter->rx_queue.queue_dma))) {
579 ibmveth_error_printk("unable to map filter or buffer list pages\n");
580 ibmveth_cleanup(adapter);
581 napi_disable(&adapter->napi);
582 return -ENOMEM;
585 adapter->rx_queue.index = 0;
586 adapter->rx_queue.num_slots = rxq_entries;
587 adapter->rx_queue.toggle = 1;
589 memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
590 mac_address = mac_address >> 16;
592 rxq_desc.fields.flags_len = IBMVETH_BUF_VALID | adapter->rx_queue.queue_len;
593 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
595 ibmveth_debug_printk("buffer list @ 0x%p\n", adapter->buffer_list_addr);
596 ibmveth_debug_printk("filter list @ 0x%p\n", adapter->filter_list_addr);
597 ibmveth_debug_printk("receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
599 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
601 lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
603 if(lpar_rc != H_SUCCESS) {
604 ibmveth_error_printk("h_register_logical_lan failed with %ld\n", lpar_rc);
605 ibmveth_error_printk("buffer TCE:0x%llx filter TCE:0x%llx rxq desc:0x%llx MAC:0x%llx\n",
606 adapter->buffer_list_dma,
607 adapter->filter_list_dma,
608 rxq_desc.desc,
609 mac_address);
610 ibmveth_cleanup(adapter);
611 napi_disable(&adapter->napi);
612 return -ENONET;
615 for(i = 0; i<IbmVethNumBufferPools; i++) {
616 if(!adapter->rx_buff_pool[i].active)
617 continue;
618 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
619 ibmveth_error_printk("unable to alloc pool\n");
620 adapter->rx_buff_pool[i].active = 0;
621 ibmveth_cleanup(adapter);
622 napi_disable(&adapter->napi);
623 return -ENOMEM ;
627 ibmveth_debug_printk("registering irq 0x%x\n", netdev->irq);
628 if((rc = request_irq(netdev->irq, &ibmveth_interrupt, 0, netdev->name, netdev)) != 0) {
629 ibmveth_error_printk("unable to request irq 0x%x, rc %d\n", netdev->irq, rc);
630 do {
631 rc = h_free_logical_lan(adapter->vdev->unit_address);
632 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
634 ibmveth_cleanup(adapter);
635 napi_disable(&adapter->napi);
636 return rc;
639 adapter->bounce_buffer =
640 kmalloc(netdev->mtu + IBMVETH_BUFF_OH, GFP_KERNEL);
641 if (!adapter->bounce_buffer) {
642 ibmveth_error_printk("unable to allocate bounce buffer\n");
643 ibmveth_cleanup(adapter);
644 napi_disable(&adapter->napi);
645 return -ENOMEM;
647 adapter->bounce_buffer_dma =
648 dma_map_single(&adapter->vdev->dev, adapter->bounce_buffer,
649 netdev->mtu + IBMVETH_BUFF_OH, DMA_BIDIRECTIONAL);
650 if (dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
651 ibmveth_error_printk("unable to map bounce buffer\n");
652 ibmveth_cleanup(adapter);
653 napi_disable(&adapter->napi);
654 return -ENOMEM;
657 ibmveth_debug_printk("initial replenish cycle\n");
658 ibmveth_interrupt(netdev->irq, netdev);
660 netif_start_queue(netdev);
662 ibmveth_debug_printk("open complete\n");
664 return 0;
667 static int ibmveth_close(struct net_device *netdev)
669 struct ibmveth_adapter *adapter = netdev_priv(netdev);
670 long lpar_rc;
672 ibmveth_debug_printk("close starting\n");
674 napi_disable(&adapter->napi);
676 if (!adapter->pool_config)
677 netif_stop_queue(netdev);
679 free_irq(netdev->irq, netdev);
681 do {
682 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
683 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
685 if(lpar_rc != H_SUCCESS)
687 ibmveth_error_printk("h_free_logical_lan failed with %lx, continuing with close\n",
688 lpar_rc);
691 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
693 ibmveth_cleanup(adapter);
695 ibmveth_debug_printk("close complete\n");
697 return 0;
700 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
701 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
702 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | ADVERTISED_FIBRE);
703 cmd->speed = SPEED_1000;
704 cmd->duplex = DUPLEX_FULL;
705 cmd->port = PORT_FIBRE;
706 cmd->phy_address = 0;
707 cmd->transceiver = XCVR_INTERNAL;
708 cmd->autoneg = AUTONEG_ENABLE;
709 cmd->maxtxpkt = 0;
710 cmd->maxrxpkt = 1;
711 return 0;
714 static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info) {
715 strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
716 strncpy(info->version, ibmveth_driver_version, sizeof(info->version) - 1);
719 static u32 netdev_get_link(struct net_device *dev) {
720 return 1;
723 static void ibmveth_set_rx_csum_flags(struct net_device *dev, u32 data)
725 struct ibmveth_adapter *adapter = netdev_priv(dev);
727 if (data)
728 adapter->rx_csum = 1;
729 else {
731 * Since the ibmveth firmware interface does not have the concept of
732 * separate tx/rx checksum offload enable, if rx checksum is disabled
733 * we also have to disable tx checksum offload. Once we disable rx
734 * checksum offload, we are no longer allowed to send tx buffers that
735 * are not properly checksummed.
737 adapter->rx_csum = 0;
738 dev->features &= ~NETIF_F_IP_CSUM;
742 static void ibmveth_set_tx_csum_flags(struct net_device *dev, u32 data)
744 struct ibmveth_adapter *adapter = netdev_priv(dev);
746 if (data) {
747 dev->features |= NETIF_F_IP_CSUM;
748 adapter->rx_csum = 1;
749 } else
750 dev->features &= ~NETIF_F_IP_CSUM;
753 static int ibmveth_set_csum_offload(struct net_device *dev, u32 data,
754 void (*done) (struct net_device *, u32))
756 struct ibmveth_adapter *adapter = netdev_priv(dev);
757 unsigned long set_attr, clr_attr, ret_attr;
758 long ret;
759 int rc1 = 0, rc2 = 0;
760 int restart = 0;
762 if (netif_running(dev)) {
763 restart = 1;
764 adapter->pool_config = 1;
765 ibmveth_close(dev);
766 adapter->pool_config = 0;
769 set_attr = 0;
770 clr_attr = 0;
772 if (data)
773 set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
774 else
775 clr_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
777 ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
779 if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
780 !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
781 (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
782 ret = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
783 set_attr, &ret_attr);
785 if (ret != H_SUCCESS) {
786 rc1 = -EIO;
787 ibmveth_error_printk("unable to change checksum offload settings."
788 " %d rc=%ld\n", data, ret);
790 ret = h_illan_attributes(adapter->vdev->unit_address,
791 set_attr, clr_attr, &ret_attr);
792 } else
793 done(dev, data);
794 } else {
795 rc1 = -EIO;
796 ibmveth_error_printk("unable to change checksum offload settings."
797 " %d rc=%ld ret_attr=%lx\n", data, ret, ret_attr);
800 if (restart)
801 rc2 = ibmveth_open(dev);
803 return rc1 ? rc1 : rc2;
806 static int ibmveth_set_rx_csum(struct net_device *dev, u32 data)
808 struct ibmveth_adapter *adapter = netdev_priv(dev);
810 if ((data && adapter->rx_csum) || (!data && !adapter->rx_csum))
811 return 0;
813 return ibmveth_set_csum_offload(dev, data, ibmveth_set_rx_csum_flags);
816 static int ibmveth_set_tx_csum(struct net_device *dev, u32 data)
818 struct ibmveth_adapter *adapter = netdev_priv(dev);
819 int rc = 0;
821 if (data && (dev->features & NETIF_F_IP_CSUM))
822 return 0;
823 if (!data && !(dev->features & NETIF_F_IP_CSUM))
824 return 0;
826 if (data && !adapter->rx_csum)
827 rc = ibmveth_set_csum_offload(dev, data, ibmveth_set_tx_csum_flags);
828 else
829 ibmveth_set_tx_csum_flags(dev, data);
831 return rc;
834 static u32 ibmveth_get_rx_csum(struct net_device *dev)
836 struct ibmveth_adapter *adapter = netdev_priv(dev);
837 return adapter->rx_csum;
840 static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
842 int i;
844 if (stringset != ETH_SS_STATS)
845 return;
847 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
848 memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
851 static int ibmveth_get_sset_count(struct net_device *dev, int sset)
853 switch (sset) {
854 case ETH_SS_STATS:
855 return ARRAY_SIZE(ibmveth_stats);
856 default:
857 return -EOPNOTSUPP;
861 static void ibmveth_get_ethtool_stats(struct net_device *dev,
862 struct ethtool_stats *stats, u64 *data)
864 int i;
865 struct ibmveth_adapter *adapter = netdev_priv(dev);
867 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
868 data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
871 static const struct ethtool_ops netdev_ethtool_ops = {
872 .get_drvinfo = netdev_get_drvinfo,
873 .get_settings = netdev_get_settings,
874 .get_link = netdev_get_link,
875 .set_tx_csum = ibmveth_set_tx_csum,
876 .get_rx_csum = ibmveth_get_rx_csum,
877 .set_rx_csum = ibmveth_set_rx_csum,
878 .get_strings = ibmveth_get_strings,
879 .get_sset_count = ibmveth_get_sset_count,
880 .get_ethtool_stats = ibmveth_get_ethtool_stats,
883 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
885 return -EOPNOTSUPP;
888 #define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
890 static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb,
891 struct net_device *netdev)
893 struct ibmveth_adapter *adapter = netdev_priv(netdev);
894 union ibmveth_buf_desc desc;
895 unsigned long lpar_rc;
896 unsigned long correlator;
897 unsigned long flags;
898 unsigned int retry_count;
899 unsigned int tx_dropped = 0;
900 unsigned int tx_bytes = 0;
901 unsigned int tx_packets = 0;
902 unsigned int tx_send_failed = 0;
903 unsigned int tx_map_failed = 0;
904 int used_bounce = 0;
905 unsigned long data_dma_addr;
907 desc.fields.flags_len = IBMVETH_BUF_VALID | skb->len;
909 if (skb->ip_summed == CHECKSUM_PARTIAL &&
910 ip_hdr(skb)->protocol != IPPROTO_TCP && skb_checksum_help(skb)) {
911 ibmveth_error_printk("tx: failed to checksum packet\n");
912 tx_dropped++;
913 goto out;
916 if (skb->ip_summed == CHECKSUM_PARTIAL) {
917 unsigned char *buf = skb_transport_header(skb) + skb->csum_offset;
919 desc.fields.flags_len |= (IBMVETH_BUF_NO_CSUM | IBMVETH_BUF_CSUM_GOOD);
921 /* Need to zero out the checksum */
922 buf[0] = 0;
923 buf[1] = 0;
926 data_dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
927 skb->len, DMA_TO_DEVICE);
928 if (dma_mapping_error(&adapter->vdev->dev, data_dma_addr)) {
929 if (!firmware_has_feature(FW_FEATURE_CMO))
930 ibmveth_error_printk("tx: unable to map xmit buffer\n");
931 skb_copy_from_linear_data(skb, adapter->bounce_buffer,
932 skb->len);
933 desc.fields.address = adapter->bounce_buffer_dma;
934 tx_map_failed++;
935 used_bounce = 1;
936 wmb();
937 } else
938 desc.fields.address = data_dma_addr;
940 /* send the frame. Arbitrarily set retrycount to 1024 */
941 correlator = 0;
942 retry_count = 1024;
943 do {
944 lpar_rc = h_send_logical_lan(adapter->vdev->unit_address,
945 desc.desc, 0, 0, 0, 0, 0,
946 correlator, &correlator);
947 } while ((lpar_rc == H_BUSY) && (retry_count--));
949 if(lpar_rc != H_SUCCESS && lpar_rc != H_DROPPED) {
950 ibmveth_error_printk("tx: h_send_logical_lan failed with rc=%ld\n", lpar_rc);
951 ibmveth_error_printk("tx: valid=%d, len=%d, address=0x%08x\n",
952 (desc.fields.flags_len & IBMVETH_BUF_VALID) ? 1 : 0,
953 skb->len, desc.fields.address);
954 tx_send_failed++;
955 tx_dropped++;
956 } else {
957 tx_packets++;
958 tx_bytes += skb->len;
959 netdev->trans_start = jiffies;
962 if (!used_bounce)
963 dma_unmap_single(&adapter->vdev->dev, data_dma_addr,
964 skb->len, DMA_TO_DEVICE);
966 out: spin_lock_irqsave(&adapter->stats_lock, flags);
967 netdev->stats.tx_dropped += tx_dropped;
968 netdev->stats.tx_bytes += tx_bytes;
969 netdev->stats.tx_packets += tx_packets;
970 adapter->tx_send_failed += tx_send_failed;
971 adapter->tx_map_failed += tx_map_failed;
972 spin_unlock_irqrestore(&adapter->stats_lock, flags);
974 dev_kfree_skb(skb);
975 return NETDEV_TX_OK;
978 static int ibmveth_poll(struct napi_struct *napi, int budget)
980 struct ibmveth_adapter *adapter = container_of(napi, struct ibmveth_adapter, napi);
981 struct net_device *netdev = adapter->netdev;
982 int frames_processed = 0;
983 unsigned long lpar_rc;
985 restart_poll:
986 do {
987 struct sk_buff *skb;
989 if (!ibmveth_rxq_pending_buffer(adapter))
990 break;
992 rmb();
993 if (!ibmveth_rxq_buffer_valid(adapter)) {
994 wmb(); /* suggested by larson1 */
995 adapter->rx_invalid_buffer++;
996 ibmveth_debug_printk("recycling invalid buffer\n");
997 ibmveth_rxq_recycle_buffer(adapter);
998 } else {
999 int length = ibmveth_rxq_frame_length(adapter);
1000 int offset = ibmveth_rxq_frame_offset(adapter);
1001 int csum_good = ibmveth_rxq_csum_good(adapter);
1003 skb = ibmveth_rxq_get_buffer(adapter);
1004 if (csum_good)
1005 skb->ip_summed = CHECKSUM_UNNECESSARY;
1007 ibmveth_rxq_harvest_buffer(adapter);
1009 skb_reserve(skb, offset);
1010 skb_put(skb, length);
1011 skb->protocol = eth_type_trans(skb, netdev);
1013 netif_receive_skb(skb); /* send it up */
1015 netdev->stats.rx_packets++;
1016 netdev->stats.rx_bytes += length;
1017 frames_processed++;
1019 } while (frames_processed < budget);
1021 ibmveth_replenish_task(adapter);
1023 if (frames_processed < budget) {
1024 /* We think we are done - reenable interrupts,
1025 * then check once more to make sure we are done.
1027 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1028 VIO_IRQ_ENABLE);
1030 ibmveth_assert(lpar_rc == H_SUCCESS);
1032 napi_complete(napi);
1034 if (ibmveth_rxq_pending_buffer(adapter) &&
1035 napi_reschedule(napi)) {
1036 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1037 VIO_IRQ_DISABLE);
1038 goto restart_poll;
1042 return frames_processed;
1045 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
1047 struct net_device *netdev = dev_instance;
1048 struct ibmveth_adapter *adapter = netdev_priv(netdev);
1049 unsigned long lpar_rc;
1051 if (napi_schedule_prep(&adapter->napi)) {
1052 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1053 VIO_IRQ_DISABLE);
1054 ibmveth_assert(lpar_rc == H_SUCCESS);
1055 __napi_schedule(&adapter->napi);
1057 return IRQ_HANDLED;
1060 static void ibmveth_set_multicast_list(struct net_device *netdev)
1062 struct ibmveth_adapter *adapter = netdev_priv(netdev);
1063 unsigned long lpar_rc;
1065 if((netdev->flags & IFF_PROMISC) || (netdev->mc_count > adapter->mcastFilterSize)) {
1066 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1067 IbmVethMcastEnableRecv |
1068 IbmVethMcastDisableFiltering,
1070 if(lpar_rc != H_SUCCESS) {
1071 ibmveth_error_printk("h_multicast_ctrl rc=%ld when entering promisc mode\n", lpar_rc);
1073 } else {
1074 struct dev_mc_list *mclist = netdev->mc_list;
1075 int i;
1076 /* clear the filter table & disable filtering */
1077 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1078 IbmVethMcastEnableRecv |
1079 IbmVethMcastDisableFiltering |
1080 IbmVethMcastClearFilterTable,
1082 if(lpar_rc != H_SUCCESS) {
1083 ibmveth_error_printk("h_multicast_ctrl rc=%ld when attempting to clear filter table\n", lpar_rc);
1085 /* add the addresses to the filter table */
1086 for(i = 0; i < netdev->mc_count; ++i, mclist = mclist->next) {
1087 // add the multicast address to the filter table
1088 unsigned long mcast_addr = 0;
1089 memcpy(((char *)&mcast_addr)+2, mclist->dmi_addr, 6);
1090 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1091 IbmVethMcastAddFilter,
1092 mcast_addr);
1093 if(lpar_rc != H_SUCCESS) {
1094 ibmveth_error_printk("h_multicast_ctrl rc=%ld when adding an entry to the filter table\n", lpar_rc);
1098 /* re-enable filtering */
1099 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1100 IbmVethMcastEnableFiltering,
1102 if(lpar_rc != H_SUCCESS) {
1103 ibmveth_error_printk("h_multicast_ctrl rc=%ld when enabling filtering\n", lpar_rc);
1108 static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
1110 struct ibmveth_adapter *adapter = netdev_priv(dev);
1111 struct vio_dev *viodev = adapter->vdev;
1112 int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
1113 int i;
1115 if (new_mtu < IBMVETH_MAX_MTU)
1116 return -EINVAL;
1118 for (i = 0; i < IbmVethNumBufferPools; i++)
1119 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size)
1120 break;
1122 if (i == IbmVethNumBufferPools)
1123 return -EINVAL;
1125 /* Deactivate all the buffer pools so that the next loop can activate
1126 only the buffer pools necessary to hold the new MTU */
1127 for (i = 0; i < IbmVethNumBufferPools; i++)
1128 if (adapter->rx_buff_pool[i].active) {
1129 ibmveth_free_buffer_pool(adapter,
1130 &adapter->rx_buff_pool[i]);
1131 adapter->rx_buff_pool[i].active = 0;
1134 /* Look for an active buffer pool that can hold the new MTU */
1135 for(i = 0; i<IbmVethNumBufferPools; i++) {
1136 adapter->rx_buff_pool[i].active = 1;
1138 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size) {
1139 if (netif_running(adapter->netdev)) {
1140 adapter->pool_config = 1;
1141 ibmveth_close(adapter->netdev);
1142 adapter->pool_config = 0;
1143 dev->mtu = new_mtu;
1144 vio_cmo_set_dev_desired(viodev,
1145 ibmveth_get_desired_dma
1146 (viodev));
1147 return ibmveth_open(adapter->netdev);
1149 dev->mtu = new_mtu;
1150 vio_cmo_set_dev_desired(viodev,
1151 ibmveth_get_desired_dma
1152 (viodev));
1153 return 0;
1156 return -EINVAL;
1159 #ifdef CONFIG_NET_POLL_CONTROLLER
1160 static void ibmveth_poll_controller(struct net_device *dev)
1162 ibmveth_replenish_task(netdev_priv(dev));
1163 ibmveth_interrupt(dev->irq, dev);
1165 #endif
1168 * ibmveth_get_desired_dma - Calculate IO memory desired by the driver
1170 * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
1172 * Return value:
1173 * Number of bytes of IO data the driver will need to perform well.
1175 static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev)
1177 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
1178 struct ibmveth_adapter *adapter;
1179 unsigned long ret;
1180 int i;
1181 int rxqentries = 1;
1183 /* netdev inits at probe time along with the structures we need below*/
1184 if (netdev == NULL)
1185 return IOMMU_PAGE_ALIGN(IBMVETH_IO_ENTITLEMENT_DEFAULT);
1187 adapter = netdev_priv(netdev);
1189 ret = IBMVETH_BUFF_LIST_SIZE + IBMVETH_FILT_LIST_SIZE;
1190 ret += IOMMU_PAGE_ALIGN(netdev->mtu);
1192 for (i = 0; i < IbmVethNumBufferPools; i++) {
1193 /* add the size of the active receive buffers */
1194 if (adapter->rx_buff_pool[i].active)
1195 ret +=
1196 adapter->rx_buff_pool[i].size *
1197 IOMMU_PAGE_ALIGN(adapter->rx_buff_pool[i].
1198 buff_size);
1199 rxqentries += adapter->rx_buff_pool[i].size;
1201 /* add the size of the receive queue entries */
1202 ret += IOMMU_PAGE_ALIGN(rxqentries * sizeof(struct ibmveth_rx_q_entry));
1204 return ret;
1207 static const struct net_device_ops ibmveth_netdev_ops = {
1208 .ndo_open = ibmveth_open,
1209 .ndo_stop = ibmveth_close,
1210 .ndo_start_xmit = ibmveth_start_xmit,
1211 .ndo_set_multicast_list = ibmveth_set_multicast_list,
1212 .ndo_do_ioctl = ibmveth_ioctl,
1213 .ndo_change_mtu = ibmveth_change_mtu,
1214 .ndo_validate_addr = eth_validate_addr,
1215 .ndo_set_mac_address = eth_mac_addr,
1216 #ifdef CONFIG_NET_POLL_CONTROLLER
1217 .ndo_poll_controller = ibmveth_poll_controller,
1218 #endif
1221 static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
1223 int rc, i;
1224 long ret;
1225 struct net_device *netdev;
1226 struct ibmveth_adapter *adapter;
1227 unsigned long set_attr, ret_attr;
1229 unsigned char *mac_addr_p;
1230 unsigned int *mcastFilterSize_p;
1233 ibmveth_debug_printk_no_adapter("entering ibmveth_probe for UA 0x%x\n",
1234 dev->unit_address);
1236 mac_addr_p = (unsigned char *) vio_get_attribute(dev,
1237 VETH_MAC_ADDR, NULL);
1238 if(!mac_addr_p) {
1239 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find VETH_MAC_ADDR "
1240 "attribute\n", __FILE__, __LINE__);
1241 return 0;
1244 mcastFilterSize_p = (unsigned int *) vio_get_attribute(dev,
1245 VETH_MCAST_FILTER_SIZE, NULL);
1246 if(!mcastFilterSize_p) {
1247 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find "
1248 "VETH_MCAST_FILTER_SIZE attribute\n",
1249 __FILE__, __LINE__);
1250 return 0;
1253 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
1255 if(!netdev)
1256 return -ENOMEM;
1258 adapter = netdev_priv(netdev);
1259 dev_set_drvdata(&dev->dev, netdev);
1261 adapter->vdev = dev;
1262 adapter->netdev = netdev;
1263 adapter->mcastFilterSize= *mcastFilterSize_p;
1264 adapter->pool_config = 0;
1266 netif_napi_add(netdev, &adapter->napi, ibmveth_poll, 16);
1268 /* Some older boxes running PHYP non-natively have an OF that
1269 returns a 8-byte local-mac-address field (and the first
1270 2 bytes have to be ignored) while newer boxes' OF return
1271 a 6-byte field. Note that IEEE 1275 specifies that
1272 local-mac-address must be a 6-byte field.
1273 The RPA doc specifies that the first byte must be 10b, so
1274 we'll just look for it to solve this 8 vs. 6 byte field issue */
1276 if ((*mac_addr_p & 0x3) != 0x02)
1277 mac_addr_p += 2;
1279 adapter->mac_addr = 0;
1280 memcpy(&adapter->mac_addr, mac_addr_p, 6);
1282 netdev->irq = dev->irq;
1283 netdev->netdev_ops = &ibmveth_netdev_ops;
1284 netdev->ethtool_ops = &netdev_ethtool_ops;
1285 SET_NETDEV_DEV(netdev, &dev->dev);
1286 netdev->features |= NETIF_F_LLTX;
1287 spin_lock_init(&adapter->stats_lock);
1289 memcpy(netdev->dev_addr, &adapter->mac_addr, netdev->addr_len);
1291 for(i = 0; i<IbmVethNumBufferPools; i++) {
1292 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
1293 int error;
1295 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1296 pool_count[i], pool_size[i],
1297 pool_active[i]);
1298 error = kobject_init_and_add(kobj, &ktype_veth_pool,
1299 &dev->dev.kobj, "pool%d", i);
1300 if (!error)
1301 kobject_uevent(kobj, KOBJ_ADD);
1304 ibmveth_debug_printk("adapter @ 0x%p\n", adapter);
1306 adapter->buffer_list_dma = DMA_ERROR_CODE;
1307 adapter->filter_list_dma = DMA_ERROR_CODE;
1308 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1310 ibmveth_debug_printk("registering netdev...\n");
1312 ret = h_illan_attributes(dev->unit_address, 0, 0, &ret_attr);
1314 if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
1315 !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
1316 (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
1317 set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
1319 ret = h_illan_attributes(dev->unit_address, 0, set_attr, &ret_attr);
1321 if (ret == H_SUCCESS) {
1322 adapter->rx_csum = 1;
1323 netdev->features |= NETIF_F_IP_CSUM;
1324 } else
1325 ret = h_illan_attributes(dev->unit_address, set_attr, 0, &ret_attr);
1328 rc = register_netdev(netdev);
1330 if(rc) {
1331 ibmveth_debug_printk("failed to register netdev rc=%d\n", rc);
1332 free_netdev(netdev);
1333 return rc;
1336 ibmveth_debug_printk("registered\n");
1338 ibmveth_proc_register_adapter(adapter);
1340 return 0;
1343 static int __devexit ibmveth_remove(struct vio_dev *dev)
1345 struct net_device *netdev = dev_get_drvdata(&dev->dev);
1346 struct ibmveth_adapter *adapter = netdev_priv(netdev);
1347 int i;
1349 for(i = 0; i<IbmVethNumBufferPools; i++)
1350 kobject_put(&adapter->rx_buff_pool[i].kobj);
1352 unregister_netdev(netdev);
1354 ibmveth_proc_unregister_adapter(adapter);
1356 free_netdev(netdev);
1357 dev_set_drvdata(&dev->dev, NULL);
1359 return 0;
1362 #ifdef CONFIG_PROC_FS
1363 static void ibmveth_proc_register_driver(void)
1365 ibmveth_proc_dir = proc_mkdir(IBMVETH_PROC_DIR, init_net.proc_net);
1366 if (ibmveth_proc_dir) {
1370 static void ibmveth_proc_unregister_driver(void)
1372 remove_proc_entry(IBMVETH_PROC_DIR, init_net.proc_net);
1375 static int ibmveth_show(struct seq_file *seq, void *v)
1377 struct ibmveth_adapter *adapter = seq->private;
1378 char *current_mac = (char *) adapter->netdev->dev_addr;
1379 char *firmware_mac = (char *) &adapter->mac_addr;
1381 seq_printf(seq, "%s %s\n\n", ibmveth_driver_string, ibmveth_driver_version);
1383 seq_printf(seq, "Unit Address: 0x%x\n", adapter->vdev->unit_address);
1384 seq_printf(seq, "Current MAC: %pM\n", current_mac);
1385 seq_printf(seq, "Firmware MAC: %pM\n", firmware_mac);
1387 seq_printf(seq, "\nAdapter Statistics:\n");
1388 seq_printf(seq, " TX: vio_map_single failres: %lld\n", adapter->tx_map_failed);
1389 seq_printf(seq, " send failures: %lld\n", adapter->tx_send_failed);
1390 seq_printf(seq, " RX: replenish task cycles: %lld\n", adapter->replenish_task_cycles);
1391 seq_printf(seq, " alloc_skb_failures: %lld\n", adapter->replenish_no_mem);
1392 seq_printf(seq, " add buffer failures: %lld\n", adapter->replenish_add_buff_failure);
1393 seq_printf(seq, " invalid buffers: %lld\n", adapter->rx_invalid_buffer);
1394 seq_printf(seq, " no buffers: %lld\n", adapter->rx_no_buffer);
1396 return 0;
1399 static int ibmveth_proc_open(struct inode *inode, struct file *file)
1401 return single_open(file, ibmveth_show, PDE(inode)->data);
1404 static const struct file_operations ibmveth_proc_fops = {
1405 .owner = THIS_MODULE,
1406 .open = ibmveth_proc_open,
1407 .read = seq_read,
1408 .llseek = seq_lseek,
1409 .release = single_release,
1412 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1414 struct proc_dir_entry *entry;
1415 if (ibmveth_proc_dir) {
1416 char u_addr[10];
1417 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1418 entry = proc_create_data(u_addr, S_IFREG, ibmveth_proc_dir,
1419 &ibmveth_proc_fops, adapter);
1420 if (!entry)
1421 ibmveth_error_printk("Cannot create adapter proc entry");
1423 return;
1426 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1428 if (ibmveth_proc_dir) {
1429 char u_addr[10];
1430 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1431 remove_proc_entry(u_addr, ibmveth_proc_dir);
1435 #else /* CONFIG_PROC_FS */
1436 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1440 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1443 static void ibmveth_proc_register_driver(void)
1447 static void ibmveth_proc_unregister_driver(void)
1450 #endif /* CONFIG_PROC_FS */
1452 static struct attribute veth_active_attr;
1453 static struct attribute veth_num_attr;
1454 static struct attribute veth_size_attr;
1456 static ssize_t veth_pool_show(struct kobject * kobj,
1457 struct attribute * attr, char * buf)
1459 struct ibmveth_buff_pool *pool = container_of(kobj,
1460 struct ibmveth_buff_pool,
1461 kobj);
1463 if (attr == &veth_active_attr)
1464 return sprintf(buf, "%d\n", pool->active);
1465 else if (attr == &veth_num_attr)
1466 return sprintf(buf, "%d\n", pool->size);
1467 else if (attr == &veth_size_attr)
1468 return sprintf(buf, "%d\n", pool->buff_size);
1469 return 0;
1472 static ssize_t veth_pool_store(struct kobject * kobj, struct attribute * attr,
1473 const char * buf, size_t count)
1475 struct ibmveth_buff_pool *pool = container_of(kobj,
1476 struct ibmveth_buff_pool,
1477 kobj);
1478 struct net_device *netdev = dev_get_drvdata(
1479 container_of(kobj->parent, struct device, kobj));
1480 struct ibmveth_adapter *adapter = netdev_priv(netdev);
1481 long value = simple_strtol(buf, NULL, 10);
1482 long rc;
1484 if (attr == &veth_active_attr) {
1485 if (value && !pool->active) {
1486 if (netif_running(netdev)) {
1487 if(ibmveth_alloc_buffer_pool(pool)) {
1488 ibmveth_error_printk("unable to alloc pool\n");
1489 return -ENOMEM;
1491 pool->active = 1;
1492 adapter->pool_config = 1;
1493 ibmveth_close(netdev);
1494 adapter->pool_config = 0;
1495 if ((rc = ibmveth_open(netdev)))
1496 return rc;
1497 } else
1498 pool->active = 1;
1499 } else if (!value && pool->active) {
1500 int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1501 int i;
1502 /* Make sure there is a buffer pool with buffers that
1503 can hold a packet of the size of the MTU */
1504 for (i = 0; i < IbmVethNumBufferPools; i++) {
1505 if (pool == &adapter->rx_buff_pool[i])
1506 continue;
1507 if (!adapter->rx_buff_pool[i].active)
1508 continue;
1509 if (mtu <= adapter->rx_buff_pool[i].buff_size)
1510 break;
1513 if (i == IbmVethNumBufferPools) {
1514 ibmveth_error_printk("no active pool >= MTU\n");
1515 return -EPERM;
1518 if (netif_running(netdev)) {
1519 adapter->pool_config = 1;
1520 ibmveth_close(netdev);
1521 pool->active = 0;
1522 adapter->pool_config = 0;
1523 if ((rc = ibmveth_open(netdev)))
1524 return rc;
1526 pool->active = 0;
1528 } else if (attr == &veth_num_attr) {
1529 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT)
1530 return -EINVAL;
1531 else {
1532 if (netif_running(netdev)) {
1533 adapter->pool_config = 1;
1534 ibmveth_close(netdev);
1535 adapter->pool_config = 0;
1536 pool->size = value;
1537 if ((rc = ibmveth_open(netdev)))
1538 return rc;
1539 } else
1540 pool->size = value;
1542 } else if (attr == &veth_size_attr) {
1543 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE)
1544 return -EINVAL;
1545 else {
1546 if (netif_running(netdev)) {
1547 adapter->pool_config = 1;
1548 ibmveth_close(netdev);
1549 adapter->pool_config = 0;
1550 pool->buff_size = value;
1551 if ((rc = ibmveth_open(netdev)))
1552 return rc;
1553 } else
1554 pool->buff_size = value;
1558 /* kick the interrupt handler to allocate/deallocate pools */
1559 ibmveth_interrupt(netdev->irq, netdev);
1560 return count;
1564 #define ATTR(_name, _mode) \
1565 struct attribute veth_##_name##_attr = { \
1566 .name = __stringify(_name), .mode = _mode, \
1569 static ATTR(active, 0644);
1570 static ATTR(num, 0644);
1571 static ATTR(size, 0644);
1573 static struct attribute * veth_pool_attrs[] = {
1574 &veth_active_attr,
1575 &veth_num_attr,
1576 &veth_size_attr,
1577 NULL,
1580 static struct sysfs_ops veth_pool_ops = {
1581 .show = veth_pool_show,
1582 .store = veth_pool_store,
1585 static struct kobj_type ktype_veth_pool = {
1586 .release = NULL,
1587 .sysfs_ops = &veth_pool_ops,
1588 .default_attrs = veth_pool_attrs,
1592 static struct vio_device_id ibmveth_device_table[] __devinitdata= {
1593 { "network", "IBM,l-lan"},
1594 { "", "" }
1596 MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1598 static struct vio_driver ibmveth_driver = {
1599 .id_table = ibmveth_device_table,
1600 .probe = ibmveth_probe,
1601 .remove = ibmveth_remove,
1602 .get_desired_dma = ibmveth_get_desired_dma,
1603 .driver = {
1604 .name = ibmveth_driver_name,
1605 .owner = THIS_MODULE,
1609 static int __init ibmveth_module_init(void)
1611 ibmveth_printk("%s: %s %s\n", ibmveth_driver_name, ibmveth_driver_string, ibmveth_driver_version);
1613 ibmveth_proc_register_driver();
1615 return vio_register_driver(&ibmveth_driver);
1618 static void __exit ibmveth_module_exit(void)
1620 vio_unregister_driver(&ibmveth_driver);
1621 ibmveth_proc_unregister_driver();
1624 module_init(ibmveth_module_init);
1625 module_exit(ibmveth_module_exit);