Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / net / ibmveth.c
blob52d01027d9e7c236d472af4f25b2f84e993a22bf
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 - remove frag processing code - no longer needed
32 - add support for sysfs
33 - possibly remove procfs support
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/errno.h>
40 #include <linux/ioport.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/kernel.h>
43 #include <linux/netdevice.h>
44 #include <linux/etherdevice.h>
45 #include <linux/skbuff.h>
46 #include <linux/init.h>
47 #include <linux/delay.h>
48 #include <linux/mm.h>
49 #include <linux/ethtool.h>
50 #include <linux/proc_fs.h>
51 #include <asm/semaphore.h>
52 #include <asm/hvcall.h>
53 #include <asm/atomic.h>
54 #include <asm/iommu.h>
55 #include <asm/vio.h>
56 #include <asm/uaccess.h>
57 #include <linux/seq_file.h>
59 #include "ibmveth.h"
61 #undef DEBUG
63 #define ibmveth_printk(fmt, args...) \
64 printk(KERN_INFO "%s: " fmt, __FILE__, ## args)
66 #define ibmveth_error_printk(fmt, args...) \
67 printk(KERN_ERR "(%s:%3.3d ua:%x) ERROR: " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
69 #ifdef DEBUG
70 #define ibmveth_debug_printk_no_adapter(fmt, args...) \
71 printk(KERN_DEBUG "(%s:%3.3d): " fmt, __FILE__, __LINE__ , ## args)
72 #define ibmveth_debug_printk(fmt, args...) \
73 printk(KERN_DEBUG "(%s:%3.3d ua:%x): " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
74 #define ibmveth_assert(expr) \
75 if(!(expr)) { \
76 printk(KERN_DEBUG "assertion failed (%s:%3.3d ua:%x): %s\n", __FILE__, __LINE__, adapter->vdev->unit_address, #expr); \
77 BUG(); \
79 #else
80 #define ibmveth_debug_printk_no_adapter(fmt, args...)
81 #define ibmveth_debug_printk(fmt, args...)
82 #define ibmveth_assert(expr)
83 #endif
85 static int ibmveth_open(struct net_device *dev);
86 static int ibmveth_close(struct net_device *dev);
87 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
88 static int ibmveth_poll(struct net_device *dev, int *budget);
89 static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *dev);
90 static struct net_device_stats *ibmveth_get_stats(struct net_device *dev);
91 static void ibmveth_set_multicast_list(struct net_device *dev);
92 static int ibmveth_change_mtu(struct net_device *dev, int new_mtu);
93 static void ibmveth_proc_register_driver(void);
94 static void ibmveth_proc_unregister_driver(void);
95 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter);
96 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter);
97 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
98 static inline void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
100 #ifdef CONFIG_PROC_FS
101 #define IBMVETH_PROC_DIR "net/ibmveth"
102 static struct proc_dir_entry *ibmveth_proc_dir;
103 #endif
105 static const char ibmveth_driver_name[] = "ibmveth";
106 static const char ibmveth_driver_string[] = "IBM i/pSeries Virtual Ethernet Driver";
107 #define ibmveth_driver_version "1.03"
109 MODULE_AUTHOR("Santiago Leon <santil@us.ibm.com>");
110 MODULE_DESCRIPTION("IBM i/pSeries Virtual Ethernet Driver");
111 MODULE_LICENSE("GPL");
112 MODULE_VERSION(ibmveth_driver_version);
114 /* simple methods of getting data from the current rxq entry */
115 static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
117 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].toggle == adapter->rx_queue.toggle);
120 static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
122 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].valid);
125 static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
127 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].offset);
130 static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
132 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
135 /* setup the initial settings for a buffer pool */
136 static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool, u32 pool_index, u32 pool_size, u32 buff_size)
138 pool->size = pool_size;
139 pool->index = pool_index;
140 pool->buff_size = buff_size;
141 pool->threshold = pool_size / 2;
144 /* allocate and setup an buffer pool - called during open */
145 static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
147 int i;
149 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
151 if(!pool->free_map) {
152 return -1;
155 pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
156 if(!pool->dma_addr) {
157 kfree(pool->free_map);
158 pool->free_map = NULL;
159 return -1;
162 pool->skbuff = kmalloc(sizeof(void*) * pool->size, GFP_KERNEL);
164 if(!pool->skbuff) {
165 kfree(pool->dma_addr);
166 pool->dma_addr = NULL;
168 kfree(pool->free_map);
169 pool->free_map = NULL;
170 return -1;
173 memset(pool->skbuff, 0, sizeof(void*) * pool->size);
174 memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
176 for(i = 0; i < pool->size; ++i) {
177 pool->free_map[i] = i;
180 atomic_set(&pool->available, 0);
181 pool->producer_index = 0;
182 pool->consumer_index = 0;
183 pool->active = 0;
185 return 0;
188 /* replenish the buffers for a pool. note that we don't need to
189 * skb_reserve these since they are used for incoming...
191 static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
193 u32 i;
194 u32 count = pool->size - atomic_read(&pool->available);
195 u32 buffers_added = 0;
197 mb();
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;
207 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
209 if(!skb) {
210 ibmveth_debug_printk("replenish: unable to allocate skb\n");
211 adapter->replenish_no_mem++;
212 break;
215 free_index = pool->consumer_index++ % pool->size;
216 index = pool->free_map[free_index];
218 ibmveth_assert(index != IBM_VETH_INVALID_MAP);
219 ibmveth_assert(pool->skbuff[index] == NULL);
221 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
222 pool->buff_size, DMA_FROM_DEVICE);
224 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
225 pool->dma_addr[index] = dma_addr;
226 pool->skbuff[index] = skb;
228 correlator = ((u64)pool->index << 32) | index;
229 *(u64*)skb->data = correlator;
231 desc.desc = 0;
232 desc.fields.valid = 1;
233 desc.fields.length = pool->buff_size;
234 desc.fields.address = dma_addr;
236 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
238 if(lpar_rc != H_SUCCESS) {
239 pool->free_map[free_index] = index;
240 pool->skbuff[index] = NULL;
241 pool->consumer_index--;
242 dma_unmap_single(&adapter->vdev->dev,
243 pool->dma_addr[index], pool->buff_size,
244 DMA_FROM_DEVICE);
245 dev_kfree_skb_any(skb);
246 adapter->replenish_add_buff_failure++;
247 break;
248 } else {
249 buffers_added++;
250 adapter->replenish_add_buff_success++;
254 mb();
255 atomic_add(buffers_added, &(pool->available));
258 /* replenish routine */
259 static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
261 int i;
263 adapter->replenish_task_cycles++;
265 for(i = 0; i < IbmVethNumBufferPools; i++)
266 if(adapter->rx_buff_pool[i].active)
267 ibmveth_replenish_buffer_pool(adapter,
268 &adapter->rx_buff_pool[i]);
270 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
273 /* empty and free ana buffer pool - also used to do cleanup in error paths */
274 static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
276 int i;
278 kfree(pool->free_map);
279 pool->free_map = NULL;
281 if(pool->skbuff && pool->dma_addr) {
282 for(i = 0; i < pool->size; ++i) {
283 struct sk_buff *skb = pool->skbuff[i];
284 if(skb) {
285 dma_unmap_single(&adapter->vdev->dev,
286 pool->dma_addr[i],
287 pool->buff_size,
288 DMA_FROM_DEVICE);
289 dev_kfree_skb_any(skb);
290 pool->skbuff[i] = NULL;
295 if(pool->dma_addr) {
296 kfree(pool->dma_addr);
297 pool->dma_addr = NULL;
300 if(pool->skbuff) {
301 kfree(pool->skbuff);
302 pool->skbuff = NULL;
304 pool->active = 0;
307 /* remove a buffer from a pool */
308 static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter, u64 correlator)
310 unsigned int pool = correlator >> 32;
311 unsigned int index = correlator & 0xffffffffUL;
312 unsigned int free_index;
313 struct sk_buff *skb;
315 ibmveth_assert(pool < IbmVethNumBufferPools);
316 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
318 skb = adapter->rx_buff_pool[pool].skbuff[index];
320 ibmveth_assert(skb != NULL);
322 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
324 dma_unmap_single(&adapter->vdev->dev,
325 adapter->rx_buff_pool[pool].dma_addr[index],
326 adapter->rx_buff_pool[pool].buff_size,
327 DMA_FROM_DEVICE);
329 free_index = adapter->rx_buff_pool[pool].producer_index++ % adapter->rx_buff_pool[pool].size;
330 adapter->rx_buff_pool[pool].free_map[free_index] = index;
332 mb();
334 atomic_dec(&(adapter->rx_buff_pool[pool].available));
337 /* get the current buffer on the rx queue */
338 static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
340 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
341 unsigned int pool = correlator >> 32;
342 unsigned int index = correlator & 0xffffffffUL;
344 ibmveth_assert(pool < IbmVethNumBufferPools);
345 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
347 return adapter->rx_buff_pool[pool].skbuff[index];
350 /* recycle the current buffer on the rx queue */
351 static void ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
353 u32 q_index = adapter->rx_queue.index;
354 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
355 unsigned int pool = correlator >> 32;
356 unsigned int index = correlator & 0xffffffffUL;
357 union ibmveth_buf_desc desc;
358 unsigned long lpar_rc;
360 ibmveth_assert(pool < IbmVethNumBufferPools);
361 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
363 if(!adapter->rx_buff_pool[pool].active) {
364 ibmveth_rxq_harvest_buffer(adapter);
365 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
366 return;
369 desc.desc = 0;
370 desc.fields.valid = 1;
371 desc.fields.length = adapter->rx_buff_pool[pool].buff_size;
372 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
374 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
376 if(lpar_rc != H_SUCCESS) {
377 ibmveth_debug_printk("h_add_logical_lan_buffer failed during recycle rc=%ld", lpar_rc);
378 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
381 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
382 adapter->rx_queue.index = 0;
383 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
387 static inline void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
389 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
391 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
392 adapter->rx_queue.index = 0;
393 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
397 static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
399 int i;
401 if(adapter->buffer_list_addr != NULL) {
402 if(!dma_mapping_error(adapter->buffer_list_dma)) {
403 dma_unmap_single(&adapter->vdev->dev,
404 adapter->buffer_list_dma, 4096,
405 DMA_BIDIRECTIONAL);
406 adapter->buffer_list_dma = DMA_ERROR_CODE;
408 free_page((unsigned long)adapter->buffer_list_addr);
409 adapter->buffer_list_addr = NULL;
412 if(adapter->filter_list_addr != NULL) {
413 if(!dma_mapping_error(adapter->filter_list_dma)) {
414 dma_unmap_single(&adapter->vdev->dev,
415 adapter->filter_list_dma, 4096,
416 DMA_BIDIRECTIONAL);
417 adapter->filter_list_dma = DMA_ERROR_CODE;
419 free_page((unsigned long)adapter->filter_list_addr);
420 adapter->filter_list_addr = NULL;
423 if(adapter->rx_queue.queue_addr != NULL) {
424 if(!dma_mapping_error(adapter->rx_queue.queue_dma)) {
425 dma_unmap_single(&adapter->vdev->dev,
426 adapter->rx_queue.queue_dma,
427 adapter->rx_queue.queue_len,
428 DMA_BIDIRECTIONAL);
429 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
431 kfree(adapter->rx_queue.queue_addr);
432 adapter->rx_queue.queue_addr = NULL;
435 for(i = 0; i<IbmVethNumBufferPools; i++)
436 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[i]);
439 static int ibmveth_open(struct net_device *netdev)
441 struct ibmveth_adapter *adapter = netdev->priv;
442 u64 mac_address = 0;
443 int rxq_entries = 1;
444 unsigned long lpar_rc;
445 int rc;
446 union ibmveth_buf_desc rxq_desc;
447 int i;
449 ibmveth_debug_printk("open starting\n");
451 for(i = 0; i<IbmVethNumBufferPools; i++)
452 rxq_entries += adapter->rx_buff_pool[i].size;
454 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
455 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
457 if(!adapter->buffer_list_addr || !adapter->filter_list_addr) {
458 ibmveth_error_printk("unable to allocate filter or buffer list pages\n");
459 ibmveth_cleanup(adapter);
460 return -ENOMEM;
463 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) * rxq_entries;
464 adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len, GFP_KERNEL);
466 if(!adapter->rx_queue.queue_addr) {
467 ibmveth_error_printk("unable to allocate rx queue pages\n");
468 ibmveth_cleanup(adapter);
469 return -ENOMEM;
472 adapter->buffer_list_dma = dma_map_single(&adapter->vdev->dev,
473 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
474 adapter->filter_list_dma = dma_map_single(&adapter->vdev->dev,
475 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
476 adapter->rx_queue.queue_dma = dma_map_single(&adapter->vdev->dev,
477 adapter->rx_queue.queue_addr,
478 adapter->rx_queue.queue_len, DMA_BIDIRECTIONAL);
480 if((dma_mapping_error(adapter->buffer_list_dma) ) ||
481 (dma_mapping_error(adapter->filter_list_dma)) ||
482 (dma_mapping_error(adapter->rx_queue.queue_dma))) {
483 ibmveth_error_printk("unable to map filter or buffer list pages\n");
484 ibmveth_cleanup(adapter);
485 return -ENOMEM;
488 adapter->rx_queue.index = 0;
489 adapter->rx_queue.num_slots = rxq_entries;
490 adapter->rx_queue.toggle = 1;
492 /* call change_mtu to init the buffer pools based in initial mtu */
493 ibmveth_change_mtu(netdev, netdev->mtu);
495 memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
496 mac_address = mac_address >> 16;
498 rxq_desc.desc = 0;
499 rxq_desc.fields.valid = 1;
500 rxq_desc.fields.length = adapter->rx_queue.queue_len;
501 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
503 ibmveth_debug_printk("buffer list @ 0x%p\n", adapter->buffer_list_addr);
504 ibmveth_debug_printk("filter list @ 0x%p\n", adapter->filter_list_addr);
505 ibmveth_debug_printk("receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
508 lpar_rc = h_register_logical_lan(adapter->vdev->unit_address,
509 adapter->buffer_list_dma,
510 rxq_desc.desc,
511 adapter->filter_list_dma,
512 mac_address);
514 if(lpar_rc != H_SUCCESS) {
515 ibmveth_error_printk("h_register_logical_lan failed with %ld\n", lpar_rc);
516 ibmveth_error_printk("buffer TCE:0x%lx filter TCE:0x%lx rxq desc:0x%lx MAC:0x%lx\n",
517 adapter->buffer_list_dma,
518 adapter->filter_list_dma,
519 rxq_desc.desc,
520 mac_address);
521 ibmveth_cleanup(adapter);
522 return -ENONET;
525 ibmveth_debug_printk("registering irq 0x%x\n", netdev->irq);
526 if((rc = request_irq(netdev->irq, &ibmveth_interrupt, 0, netdev->name, netdev)) != 0) {
527 ibmveth_error_printk("unable to request irq 0x%x, rc %d\n", netdev->irq, rc);
528 do {
529 rc = h_free_logical_lan(adapter->vdev->unit_address);
530 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
532 ibmveth_cleanup(adapter);
533 return rc;
536 ibmveth_debug_printk("initial replenish cycle\n");
537 ibmveth_interrupt(netdev->irq, netdev, NULL);
539 netif_start_queue(netdev);
541 ibmveth_debug_printk("open complete\n");
543 return 0;
546 static int ibmveth_close(struct net_device *netdev)
548 struct ibmveth_adapter *adapter = netdev->priv;
549 long lpar_rc;
551 ibmveth_debug_printk("close starting\n");
553 netif_stop_queue(netdev);
555 free_irq(netdev->irq, netdev);
557 do {
558 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
559 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
561 if(lpar_rc != H_SUCCESS)
563 ibmveth_error_printk("h_free_logical_lan failed with %lx, continuing with close\n",
564 lpar_rc);
567 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
569 ibmveth_cleanup(adapter);
571 ibmveth_debug_printk("close complete\n");
573 return 0;
576 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
577 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
578 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | ADVERTISED_FIBRE);
579 cmd->speed = SPEED_1000;
580 cmd->duplex = DUPLEX_FULL;
581 cmd->port = PORT_FIBRE;
582 cmd->phy_address = 0;
583 cmd->transceiver = XCVR_INTERNAL;
584 cmd->autoneg = AUTONEG_ENABLE;
585 cmd->maxtxpkt = 0;
586 cmd->maxrxpkt = 1;
587 return 0;
590 static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info) {
591 strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
592 strncpy(info->version, ibmveth_driver_version, sizeof(info->version) - 1);
595 static u32 netdev_get_link(struct net_device *dev) {
596 return 1;
599 static struct ethtool_ops netdev_ethtool_ops = {
600 .get_drvinfo = netdev_get_drvinfo,
601 .get_settings = netdev_get_settings,
602 .get_link = netdev_get_link,
603 .get_sg = ethtool_op_get_sg,
604 .get_tx_csum = ethtool_op_get_tx_csum,
607 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
609 return -EOPNOTSUPP;
612 #define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
614 static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *netdev)
616 struct ibmveth_adapter *adapter = netdev->priv;
617 union ibmveth_buf_desc desc[IbmVethMaxSendFrags];
618 unsigned long lpar_rc;
619 int nfrags = 0, curfrag;
620 unsigned long correlator;
621 unsigned long flags;
622 unsigned int retry_count;
623 unsigned int tx_dropped = 0;
624 unsigned int tx_bytes = 0;
625 unsigned int tx_packets = 0;
626 unsigned int tx_send_failed = 0;
627 unsigned int tx_map_failed = 0;
630 if ((skb_shinfo(skb)->nr_frags + 1) > IbmVethMaxSendFrags) {
631 tx_dropped++;
632 goto out;
635 memset(&desc, 0, sizeof(desc));
637 /* nfrags = number of frags after the initial fragment */
638 nfrags = skb_shinfo(skb)->nr_frags;
640 if(nfrags)
641 adapter->tx_multidesc_send++;
643 /* map the initial fragment */
644 desc[0].fields.length = nfrags ? skb->len - skb->data_len : skb->len;
645 desc[0].fields.address = dma_map_single(&adapter->vdev->dev, skb->data,
646 desc[0].fields.length, DMA_TO_DEVICE);
647 desc[0].fields.valid = 1;
649 if(dma_mapping_error(desc[0].fields.address)) {
650 ibmveth_error_printk("tx: unable to map initial fragment\n");
651 tx_map_failed++;
652 tx_dropped++;
653 goto out;
656 curfrag = nfrags;
658 /* map fragments past the initial portion if there are any */
659 while(curfrag--) {
660 skb_frag_t *frag = &skb_shinfo(skb)->frags[curfrag];
661 desc[curfrag+1].fields.address
662 = dma_map_single(&adapter->vdev->dev,
663 page_address(frag->page) + frag->page_offset,
664 frag->size, DMA_TO_DEVICE);
665 desc[curfrag+1].fields.length = frag->size;
666 desc[curfrag+1].fields.valid = 1;
668 if(dma_mapping_error(desc[curfrag+1].fields.address)) {
669 ibmveth_error_printk("tx: unable to map fragment %d\n", curfrag);
670 tx_map_failed++;
671 tx_dropped++;
672 /* Free all the mappings we just created */
673 while(curfrag < nfrags) {
674 dma_unmap_single(&adapter->vdev->dev,
675 desc[curfrag+1].fields.address,
676 desc[curfrag+1].fields.length,
677 DMA_TO_DEVICE);
678 curfrag++;
680 goto out;
684 /* send the frame. Arbitrarily set retrycount to 1024 */
685 correlator = 0;
686 retry_count = 1024;
687 do {
688 lpar_rc = h_send_logical_lan(adapter->vdev->unit_address,
689 desc[0].desc,
690 desc[1].desc,
691 desc[2].desc,
692 desc[3].desc,
693 desc[4].desc,
694 desc[5].desc,
695 correlator);
696 } while ((lpar_rc == H_BUSY) && (retry_count--));
698 if(lpar_rc != H_SUCCESS && lpar_rc != H_DROPPED) {
699 int i;
700 ibmveth_error_printk("tx: h_send_logical_lan failed with rc=%ld\n", lpar_rc);
701 for(i = 0; i < 6; i++) {
702 ibmveth_error_printk("tx: desc[%i] valid=%d, len=%d, address=0x%d\n", i,
703 desc[i].fields.valid, desc[i].fields.length, desc[i].fields.address);
705 tx_send_failed++;
706 tx_dropped++;
707 } else {
708 tx_packets++;
709 tx_bytes += skb->len;
710 netdev->trans_start = jiffies;
713 do {
714 dma_unmap_single(&adapter->vdev->dev,
715 desc[nfrags].fields.address,
716 desc[nfrags].fields.length, DMA_TO_DEVICE);
717 } while(--nfrags >= 0);
719 out: spin_lock_irqsave(&adapter->stats_lock, flags);
720 adapter->stats.tx_dropped += tx_dropped;
721 adapter->stats.tx_bytes += tx_bytes;
722 adapter->stats.tx_packets += tx_packets;
723 adapter->tx_send_failed += tx_send_failed;
724 adapter->tx_map_failed += tx_map_failed;
725 spin_unlock_irqrestore(&adapter->stats_lock, flags);
727 dev_kfree_skb(skb);
728 return 0;
731 static int ibmveth_poll(struct net_device *netdev, int *budget)
733 struct ibmveth_adapter *adapter = netdev->priv;
734 int max_frames_to_process = netdev->quota;
735 int frames_processed = 0;
736 int more_work = 1;
737 unsigned long lpar_rc;
739 restart_poll:
740 do {
741 struct net_device *netdev = adapter->netdev;
743 if(ibmveth_rxq_pending_buffer(adapter)) {
744 struct sk_buff *skb;
746 rmb();
748 if(!ibmveth_rxq_buffer_valid(adapter)) {
749 wmb(); /* suggested by larson1 */
750 adapter->rx_invalid_buffer++;
751 ibmveth_debug_printk("recycling invalid buffer\n");
752 ibmveth_rxq_recycle_buffer(adapter);
753 } else {
754 int length = ibmveth_rxq_frame_length(adapter);
755 int offset = ibmveth_rxq_frame_offset(adapter);
756 skb = ibmveth_rxq_get_buffer(adapter);
758 ibmveth_rxq_harvest_buffer(adapter);
760 skb_reserve(skb, offset);
761 skb_put(skb, length);
762 skb->dev = netdev;
763 skb->protocol = eth_type_trans(skb, netdev);
765 netif_receive_skb(skb); /* send it up */
767 adapter->stats.rx_packets++;
768 adapter->stats.rx_bytes += length;
769 frames_processed++;
770 netdev->last_rx = jiffies;
772 } else {
773 more_work = 0;
775 } while(more_work && (frames_processed < max_frames_to_process));
777 ibmveth_replenish_task(adapter);
779 if(more_work) {
780 /* more work to do - return that we are not done yet */
781 netdev->quota -= frames_processed;
782 *budget -= frames_processed;
783 return 1;
786 /* we think we are done - reenable interrupts, then check once more to make sure we are done */
787 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_ENABLE);
789 ibmveth_assert(lpar_rc == H_SUCCESS);
791 netif_rx_complete(netdev);
793 if(ibmveth_rxq_pending_buffer(adapter) && netif_rx_reschedule(netdev, frames_processed))
795 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
796 ibmveth_assert(lpar_rc == H_SUCCESS);
797 more_work = 1;
798 goto restart_poll;
801 netdev->quota -= frames_processed;
802 *budget -= frames_processed;
804 /* we really are done */
805 return 0;
808 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
810 struct net_device *netdev = dev_instance;
811 struct ibmveth_adapter *adapter = netdev->priv;
812 unsigned long lpar_rc;
814 if(netif_rx_schedule_prep(netdev)) {
815 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
816 ibmveth_assert(lpar_rc == H_SUCCESS);
817 __netif_rx_schedule(netdev);
819 return IRQ_HANDLED;
822 static struct net_device_stats *ibmveth_get_stats(struct net_device *dev)
824 struct ibmveth_adapter *adapter = dev->priv;
825 return &adapter->stats;
828 static void ibmveth_set_multicast_list(struct net_device *netdev)
830 struct ibmveth_adapter *adapter = netdev->priv;
831 unsigned long lpar_rc;
833 if((netdev->flags & IFF_PROMISC) || (netdev->mc_count > adapter->mcastFilterSize)) {
834 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
835 IbmVethMcastEnableRecv |
836 IbmVethMcastDisableFiltering,
838 if(lpar_rc != H_SUCCESS) {
839 ibmveth_error_printk("h_multicast_ctrl rc=%ld when entering promisc mode\n", lpar_rc);
841 } else {
842 struct dev_mc_list *mclist = netdev->mc_list;
843 int i;
844 /* clear the filter table & disable filtering */
845 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
846 IbmVethMcastEnableRecv |
847 IbmVethMcastDisableFiltering |
848 IbmVethMcastClearFilterTable,
850 if(lpar_rc != H_SUCCESS) {
851 ibmveth_error_printk("h_multicast_ctrl rc=%ld when attempting to clear filter table\n", lpar_rc);
853 /* add the addresses to the filter table */
854 for(i = 0; i < netdev->mc_count; ++i, mclist = mclist->next) {
855 // add the multicast address to the filter table
856 unsigned long mcast_addr = 0;
857 memcpy(((char *)&mcast_addr)+2, mclist->dmi_addr, 6);
858 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
859 IbmVethMcastAddFilter,
860 mcast_addr);
861 if(lpar_rc != H_SUCCESS) {
862 ibmveth_error_printk("h_multicast_ctrl rc=%ld when adding an entry to the filter table\n", lpar_rc);
866 /* re-enable filtering */
867 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
868 IbmVethMcastEnableFiltering,
870 if(lpar_rc != H_SUCCESS) {
871 ibmveth_error_printk("h_multicast_ctrl rc=%ld when enabling filtering\n", lpar_rc);
876 static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
878 struct ibmveth_adapter *adapter = dev->priv;
879 int i;
880 int prev_smaller = 1;
882 if ((new_mtu < 68) ||
883 (new_mtu > (pool_size[IbmVethNumBufferPools-1]) - IBMVETH_BUFF_OH))
884 return -EINVAL;
886 for(i = 0; i<IbmVethNumBufferPools; i++) {
887 int activate = 0;
888 if (new_mtu > (pool_size[i] - IBMVETH_BUFF_OH)) {
889 activate = 1;
890 prev_smaller= 1;
891 } else {
892 if (prev_smaller)
893 activate = 1;
894 prev_smaller= 0;
897 if (activate && !adapter->rx_buff_pool[i].active) {
898 struct ibmveth_buff_pool *pool =
899 &adapter->rx_buff_pool[i];
900 if(ibmveth_alloc_buffer_pool(pool)) {
901 ibmveth_error_printk("unable to alloc pool\n");
902 return -ENOMEM;
904 adapter->rx_buff_pool[i].active = 1;
905 } else if (!activate && adapter->rx_buff_pool[i].active) {
906 adapter->rx_buff_pool[i].active = 0;
907 h_free_logical_lan_buffer(adapter->vdev->unit_address,
908 (u64)pool_size[i]);
913 /* kick the interrupt handler so that the new buffer pools get
914 replenished or deallocated */
915 ibmveth_interrupt(dev->irq, dev, NULL);
917 dev->mtu = new_mtu;
918 return 0;
921 static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
923 int rc, i;
924 struct net_device *netdev;
925 struct ibmveth_adapter *adapter = NULL;
927 unsigned char *mac_addr_p;
928 unsigned int *mcastFilterSize_p;
931 ibmveth_debug_printk_no_adapter("entering ibmveth_probe for UA 0x%x\n",
932 dev->unit_address);
934 mac_addr_p = (unsigned char *) vio_get_attribute(dev, VETH_MAC_ADDR, 0);
935 if(!mac_addr_p) {
936 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find VETH_MAC_ADDR "
937 "attribute\n", __FILE__, __LINE__);
938 return 0;
941 mcastFilterSize_p= (unsigned int *) vio_get_attribute(dev, VETH_MCAST_FILTER_SIZE, 0);
942 if(!mcastFilterSize_p) {
943 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find "
944 "VETH_MCAST_FILTER_SIZE attribute\n",
945 __FILE__, __LINE__);
946 return 0;
949 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
951 if(!netdev)
952 return -ENOMEM;
954 SET_MODULE_OWNER(netdev);
956 adapter = netdev->priv;
957 memset(adapter, 0, sizeof(adapter));
958 dev->dev.driver_data = netdev;
960 adapter->vdev = dev;
961 adapter->netdev = netdev;
962 adapter->mcastFilterSize= *mcastFilterSize_p;
964 /* Some older boxes running PHYP non-natively have an OF that
965 returns a 8-byte local-mac-address field (and the first
966 2 bytes have to be ignored) while newer boxes' OF return
967 a 6-byte field. Note that IEEE 1275 specifies that
968 local-mac-address must be a 6-byte field.
969 The RPA doc specifies that the first byte must be 10b, so
970 we'll just look for it to solve this 8 vs. 6 byte field issue */
972 if ((*mac_addr_p & 0x3) != 0x02)
973 mac_addr_p += 2;
975 adapter->mac_addr = 0;
976 memcpy(&adapter->mac_addr, mac_addr_p, 6);
978 adapter->liobn = dev->iommu_table->it_index;
980 netdev->irq = dev->irq;
981 netdev->open = ibmveth_open;
982 netdev->poll = ibmveth_poll;
983 netdev->weight = 16;
984 netdev->stop = ibmveth_close;
985 netdev->hard_start_xmit = ibmveth_start_xmit;
986 netdev->get_stats = ibmveth_get_stats;
987 netdev->set_multicast_list = ibmveth_set_multicast_list;
988 netdev->do_ioctl = ibmveth_ioctl;
989 netdev->ethtool_ops = &netdev_ethtool_ops;
990 netdev->change_mtu = ibmveth_change_mtu;
991 SET_NETDEV_DEV(netdev, &dev->dev);
992 netdev->features |= NETIF_F_LLTX;
993 spin_lock_init(&adapter->stats_lock);
995 memcpy(&netdev->dev_addr, &adapter->mac_addr, netdev->addr_len);
997 for(i = 0; i<IbmVethNumBufferPools; i++)
998 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
999 pool_count[i], pool_size[i]);
1001 ibmveth_debug_printk("adapter @ 0x%p\n", adapter);
1003 adapter->buffer_list_dma = DMA_ERROR_CODE;
1004 adapter->filter_list_dma = DMA_ERROR_CODE;
1005 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1007 ibmveth_debug_printk("registering netdev...\n");
1009 rc = register_netdev(netdev);
1011 if(rc) {
1012 ibmveth_debug_printk("failed to register netdev rc=%d\n", rc);
1013 free_netdev(netdev);
1014 return rc;
1017 ibmveth_debug_printk("registered\n");
1019 ibmveth_proc_register_adapter(adapter);
1021 return 0;
1024 static int __devexit ibmveth_remove(struct vio_dev *dev)
1026 struct net_device *netdev = dev->dev.driver_data;
1027 struct ibmveth_adapter *adapter = netdev->priv;
1029 unregister_netdev(netdev);
1031 ibmveth_proc_unregister_adapter(adapter);
1033 free_netdev(netdev);
1034 return 0;
1037 #ifdef CONFIG_PROC_FS
1038 static void ibmveth_proc_register_driver(void)
1040 ibmveth_proc_dir = proc_mkdir(IBMVETH_PROC_DIR, NULL);
1041 if (ibmveth_proc_dir) {
1042 SET_MODULE_OWNER(ibmveth_proc_dir);
1046 static void ibmveth_proc_unregister_driver(void)
1048 remove_proc_entry(IBMVETH_PROC_DIR, NULL);
1051 static void *ibmveth_seq_start(struct seq_file *seq, loff_t *pos)
1053 if (*pos == 0) {
1054 return (void *)1;
1055 } else {
1056 return NULL;
1060 static void *ibmveth_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1062 ++*pos;
1063 return NULL;
1066 static void ibmveth_seq_stop(struct seq_file *seq, void *v)
1070 static int ibmveth_seq_show(struct seq_file *seq, void *v)
1072 struct ibmveth_adapter *adapter = seq->private;
1073 char *current_mac = ((char*) &adapter->netdev->dev_addr);
1074 char *firmware_mac = ((char*) &adapter->mac_addr) ;
1076 seq_printf(seq, "%s %s\n\n", ibmveth_driver_string, ibmveth_driver_version);
1078 seq_printf(seq, "Unit Address: 0x%x\n", adapter->vdev->unit_address);
1079 seq_printf(seq, "LIOBN: 0x%lx\n", adapter->liobn);
1080 seq_printf(seq, "Current MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
1081 current_mac[0], current_mac[1], current_mac[2],
1082 current_mac[3], current_mac[4], current_mac[5]);
1083 seq_printf(seq, "Firmware MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
1084 firmware_mac[0], firmware_mac[1], firmware_mac[2],
1085 firmware_mac[3], firmware_mac[4], firmware_mac[5]);
1087 seq_printf(seq, "\nAdapter Statistics:\n");
1088 seq_printf(seq, " TX: skbuffs linearized: %ld\n", adapter->tx_linearized);
1089 seq_printf(seq, " multi-descriptor sends: %ld\n", adapter->tx_multidesc_send);
1090 seq_printf(seq, " skb_linearize failures: %ld\n", adapter->tx_linearize_failed);
1091 seq_printf(seq, " vio_map_single failres: %ld\n", adapter->tx_map_failed);
1092 seq_printf(seq, " send failures: %ld\n", adapter->tx_send_failed);
1093 seq_printf(seq, " RX: replenish task cycles: %ld\n", adapter->replenish_task_cycles);
1094 seq_printf(seq, " alloc_skb_failures: %ld\n", adapter->replenish_no_mem);
1095 seq_printf(seq, " add buffer failures: %ld\n", adapter->replenish_add_buff_failure);
1096 seq_printf(seq, " invalid buffers: %ld\n", adapter->rx_invalid_buffer);
1097 seq_printf(seq, " no buffers: %ld\n", adapter->rx_no_buffer);
1099 return 0;
1101 static struct seq_operations ibmveth_seq_ops = {
1102 .start = ibmveth_seq_start,
1103 .next = ibmveth_seq_next,
1104 .stop = ibmveth_seq_stop,
1105 .show = ibmveth_seq_show,
1108 static int ibmveth_proc_open(struct inode *inode, struct file *file)
1110 struct seq_file *seq;
1111 struct proc_dir_entry *proc;
1112 int rc;
1114 rc = seq_open(file, &ibmveth_seq_ops);
1115 if (!rc) {
1116 /* recover the pointer buried in proc_dir_entry data */
1117 seq = file->private_data;
1118 proc = PDE(inode);
1119 seq->private = proc->data;
1121 return rc;
1124 static struct file_operations ibmveth_proc_fops = {
1125 .owner = THIS_MODULE,
1126 .open = ibmveth_proc_open,
1127 .read = seq_read,
1128 .llseek = seq_lseek,
1129 .release = seq_release,
1132 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1134 struct proc_dir_entry *entry;
1135 if (ibmveth_proc_dir) {
1136 entry = create_proc_entry(adapter->netdev->name, S_IFREG, ibmveth_proc_dir);
1137 if (!entry) {
1138 ibmveth_error_printk("Cannot create adapter proc entry");
1139 } else {
1140 entry->data = (void *) adapter;
1141 entry->proc_fops = &ibmveth_proc_fops;
1142 SET_MODULE_OWNER(entry);
1145 return;
1148 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1150 if (ibmveth_proc_dir) {
1151 remove_proc_entry(adapter->netdev->name, ibmveth_proc_dir);
1155 #else /* CONFIG_PROC_FS */
1156 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1160 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1163 static void ibmveth_proc_register_driver(void)
1167 static void ibmveth_proc_unregister_driver(void)
1170 #endif /* CONFIG_PROC_FS */
1172 static struct vio_device_id ibmveth_device_table[] __devinitdata= {
1173 { "network", "IBM,l-lan"},
1174 { "", "" }
1176 MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1178 static struct vio_driver ibmveth_driver = {
1179 .id_table = ibmveth_device_table,
1180 .probe = ibmveth_probe,
1181 .remove = ibmveth_remove,
1182 .driver = {
1183 .name = ibmveth_driver_name,
1184 .owner = THIS_MODULE,
1188 static int __init ibmveth_module_init(void)
1190 ibmveth_printk("%s: %s %s\n", ibmveth_driver_name, ibmveth_driver_string, ibmveth_driver_version);
1192 ibmveth_proc_register_driver();
1194 return vio_register_driver(&ibmveth_driver);
1197 static void __exit ibmveth_module_exit(void)
1199 vio_unregister_driver(&ibmveth_driver);
1200 ibmveth_proc_unregister_driver();
1203 module_init(ibmveth_module_init);
1204 module_exit(ibmveth_module_exit);