1 /**************************************************************************/
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) */
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. */
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. */
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 */
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. */
28 /**************************************************************************/
31 - remove frag processing code - no longer needed
32 - add support for sysfs
33 - possibly remove procfs support
36 #include <linux/module.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>
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>
54 #include <asm/uaccess.h>
55 #include <linux/seq_file.h>
61 #define ibmveth_printk(fmt, args...) \
62 printk(KERN_DEBUG "%s: " fmt, __FILE__, ## args)
64 #define ibmveth_error_printk(fmt, args...) \
65 printk(KERN_ERR "(%s:%3.3d ua:%x) ERROR: " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
68 #define ibmveth_debug_printk_no_adapter(fmt, args...) \
69 printk(KERN_DEBUG "(%s:%3.3d): " fmt, __FILE__, __LINE__ , ## args)
70 #define ibmveth_debug_printk(fmt, args...) \
71 printk(KERN_DEBUG "(%s:%3.3d ua:%x): " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
72 #define ibmveth_assert(expr) \
74 printk(KERN_DEBUG "assertion failed (%s:%3.3d ua:%x): %s\n", __FILE__, __LINE__, adapter->vdev->unit_address, #expr); \
78 #define ibmveth_debug_printk_no_adapter(fmt, args...)
79 #define ibmveth_debug_printk(fmt, args...)
80 #define ibmveth_assert(expr)
83 static int ibmveth_open(struct net_device *dev);
84 static int ibmveth_close(struct net_device *dev);
85 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
86 static int ibmveth_poll(struct net_device *dev, int *budget);
87 static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *dev);
88 static struct net_device_stats *ibmveth_get_stats(struct net_device *dev);
89 static void ibmveth_set_multicast_list(struct net_device *dev);
90 static int ibmveth_change_mtu(struct net_device *dev, int new_mtu);
91 static void ibmveth_proc_register_driver(void);
92 static void ibmveth_proc_unregister_driver(void);
93 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter);
94 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter);
95 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
96 static inline void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
97 static struct kobj_type ktype_veth_pool;
100 #define IBMVETH_PROC_DIR "net/ibmveth"
101 static struct proc_dir_entry *ibmveth_proc_dir;
104 static const char ibmveth_driver_name[] = "ibmveth";
105 static const char ibmveth_driver_string[] = "IBM i/pSeries Virtual Ethernet Driver";
106 #define ibmveth_driver_version "1.03"
108 MODULE_AUTHOR("Santiago Leon <santil@us.ibm.com>");
109 MODULE_DESCRIPTION("IBM i/pSeries Virtual Ethernet Driver");
110 MODULE_LICENSE("GPL");
111 MODULE_VERSION(ibmveth_driver_version);
113 /* simple methods of getting data from the current rxq entry */
114 static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
116 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].toggle == adapter->rx_queue.toggle);
119 static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
121 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].valid);
124 static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
126 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].offset);
129 static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
131 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
134 /* setup the initial settings for a buffer pool */
135 static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool, u32 pool_index, u32 pool_size, u32 buff_size, u32 pool_active)
137 pool->size = pool_size;
138 pool->index = pool_index;
139 pool->buff_size = buff_size;
140 pool->threshold = pool_size / 2;
141 pool->active = pool_active;
144 /* allocate and setup an buffer pool - called during open */
145 static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
149 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
151 if(!pool->free_map) {
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;
162 pool->skbuff = kmalloc(sizeof(void*) * pool->size, GFP_KERNEL);
165 kfree(pool->dma_addr);
166 pool->dma_addr = NULL;
168 kfree(pool->free_map);
169 pool->free_map = NULL;
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;
187 /* replenish the buffers for a pool. note that we don't need to
188 * skb_reserve these since they are used for incoming...
190 static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
193 u32 count = pool->size - atomic_read(&pool->available);
194 u32 buffers_added = 0;
198 for(i = 0; i < count; ++i) {
200 unsigned int free_index, index;
202 union ibmveth_buf_desc desc;
203 unsigned long lpar_rc;
206 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
209 ibmveth_debug_printk("replenish: unable to allocate skb\n");
210 adapter->replenish_no_mem++;
214 free_index = pool->consumer_index;
215 pool->consumer_index = (pool->consumer_index + 1) % 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;
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 if (pool->consumer_index == 0)
242 pool->consumer_index = pool->size - 1;
244 pool->consumer_index--;
245 dma_unmap_single(&adapter->vdev->dev,
246 pool->dma_addr[index], pool->buff_size,
248 dev_kfree_skb_any(skb);
249 adapter->replenish_add_buff_failure++;
253 adapter->replenish_add_buff_success++;
258 atomic_add(buffers_added, &(pool->available));
261 /* replenish routine */
262 static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
266 adapter->replenish_task_cycles++;
268 for(i = 0; i < IbmVethNumBufferPools; i++)
269 if(adapter->rx_buff_pool[i].active)
270 ibmveth_replenish_buffer_pool(adapter,
271 &adapter->rx_buff_pool[i]);
273 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
276 /* empty and free ana buffer pool - also used to do cleanup in error paths */
277 static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
281 kfree(pool->free_map);
282 pool->free_map = NULL;
284 if(pool->skbuff && pool->dma_addr) {
285 for(i = 0; i < pool->size; ++i) {
286 struct sk_buff *skb = pool->skbuff[i];
288 dma_unmap_single(&adapter->vdev->dev,
292 dev_kfree_skb_any(skb);
293 pool->skbuff[i] = NULL;
299 kfree(pool->dma_addr);
300 pool->dma_addr = NULL;
309 /* remove a buffer from a pool */
310 static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter, u64 correlator)
312 unsigned int pool = correlator >> 32;
313 unsigned int index = correlator & 0xffffffffUL;
314 unsigned int free_index;
317 ibmveth_assert(pool < IbmVethNumBufferPools);
318 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
320 skb = adapter->rx_buff_pool[pool].skbuff[index];
322 ibmveth_assert(skb != NULL);
324 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
326 dma_unmap_single(&adapter->vdev->dev,
327 adapter->rx_buff_pool[pool].dma_addr[index],
328 adapter->rx_buff_pool[pool].buff_size,
331 free_index = adapter->rx_buff_pool[pool].producer_index;
332 adapter->rx_buff_pool[pool].producer_index
333 = (adapter->rx_buff_pool[pool].producer_index + 1)
334 % adapter->rx_buff_pool[pool].size;
335 adapter->rx_buff_pool[pool].free_map[free_index] = index;
339 atomic_dec(&(adapter->rx_buff_pool[pool].available));
342 /* get the current buffer on the rx queue */
343 static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
345 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
346 unsigned int pool = correlator >> 32;
347 unsigned int index = correlator & 0xffffffffUL;
349 ibmveth_assert(pool < IbmVethNumBufferPools);
350 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
352 return adapter->rx_buff_pool[pool].skbuff[index];
355 /* recycle the current buffer on the rx queue */
356 static void ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
358 u32 q_index = adapter->rx_queue.index;
359 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
360 unsigned int pool = correlator >> 32;
361 unsigned int index = correlator & 0xffffffffUL;
362 union ibmveth_buf_desc desc;
363 unsigned long lpar_rc;
365 ibmveth_assert(pool < IbmVethNumBufferPools);
366 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
368 if(!adapter->rx_buff_pool[pool].active) {
369 ibmveth_rxq_harvest_buffer(adapter);
370 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
375 desc.fields.valid = 1;
376 desc.fields.length = adapter->rx_buff_pool[pool].buff_size;
377 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
379 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
381 if(lpar_rc != H_SUCCESS) {
382 ibmveth_debug_printk("h_add_logical_lan_buffer failed during recycle rc=%ld", lpar_rc);
383 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
386 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
387 adapter->rx_queue.index = 0;
388 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
392 static inline void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
394 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
396 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
397 adapter->rx_queue.index = 0;
398 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
402 static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
406 if(adapter->buffer_list_addr != NULL) {
407 if(!dma_mapping_error(adapter->buffer_list_dma)) {
408 dma_unmap_single(&adapter->vdev->dev,
409 adapter->buffer_list_dma, 4096,
411 adapter->buffer_list_dma = DMA_ERROR_CODE;
413 free_page((unsigned long)adapter->buffer_list_addr);
414 adapter->buffer_list_addr = NULL;
417 if(adapter->filter_list_addr != NULL) {
418 if(!dma_mapping_error(adapter->filter_list_dma)) {
419 dma_unmap_single(&adapter->vdev->dev,
420 adapter->filter_list_dma, 4096,
422 adapter->filter_list_dma = DMA_ERROR_CODE;
424 free_page((unsigned long)adapter->filter_list_addr);
425 adapter->filter_list_addr = NULL;
428 if(adapter->rx_queue.queue_addr != NULL) {
429 if(!dma_mapping_error(adapter->rx_queue.queue_dma)) {
430 dma_unmap_single(&adapter->vdev->dev,
431 adapter->rx_queue.queue_dma,
432 adapter->rx_queue.queue_len,
434 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
436 kfree(adapter->rx_queue.queue_addr);
437 adapter->rx_queue.queue_addr = NULL;
440 for(i = 0; i<IbmVethNumBufferPools; i++)
441 if (adapter->rx_buff_pool[i].active)
442 ibmveth_free_buffer_pool(adapter,
443 &adapter->rx_buff_pool[i]);
446 static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
447 union ibmveth_buf_desc rxq_desc, u64 mac_address)
449 int rc, try_again = 1;
451 /* After a kexec the adapter will still be open, so our attempt to
452 * open it will fail. So if we get a failure we free the adapter and
453 * try again, but only once. */
455 rc = h_register_logical_lan(adapter->vdev->unit_address,
456 adapter->buffer_list_dma, rxq_desc.desc,
457 adapter->filter_list_dma, mac_address);
459 if (rc != H_SUCCESS && try_again) {
461 rc = h_free_logical_lan(adapter->vdev->unit_address);
462 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
471 static int ibmveth_open(struct net_device *netdev)
473 struct ibmveth_adapter *adapter = netdev->priv;
476 unsigned long lpar_rc;
478 union ibmveth_buf_desc rxq_desc;
481 ibmveth_debug_printk("open starting\n");
483 for(i = 0; i<IbmVethNumBufferPools; i++)
484 rxq_entries += adapter->rx_buff_pool[i].size;
486 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
487 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
489 if(!adapter->buffer_list_addr || !adapter->filter_list_addr) {
490 ibmveth_error_printk("unable to allocate filter or buffer list pages\n");
491 ibmveth_cleanup(adapter);
495 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) * rxq_entries;
496 adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len, GFP_KERNEL);
498 if(!adapter->rx_queue.queue_addr) {
499 ibmveth_error_printk("unable to allocate rx queue pages\n");
500 ibmveth_cleanup(adapter);
504 adapter->buffer_list_dma = dma_map_single(&adapter->vdev->dev,
505 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
506 adapter->filter_list_dma = dma_map_single(&adapter->vdev->dev,
507 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
508 adapter->rx_queue.queue_dma = dma_map_single(&adapter->vdev->dev,
509 adapter->rx_queue.queue_addr,
510 adapter->rx_queue.queue_len, DMA_BIDIRECTIONAL);
512 if((dma_mapping_error(adapter->buffer_list_dma) ) ||
513 (dma_mapping_error(adapter->filter_list_dma)) ||
514 (dma_mapping_error(adapter->rx_queue.queue_dma))) {
515 ibmveth_error_printk("unable to map filter or buffer list pages\n");
516 ibmveth_cleanup(adapter);
520 adapter->rx_queue.index = 0;
521 adapter->rx_queue.num_slots = rxq_entries;
522 adapter->rx_queue.toggle = 1;
524 memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
525 mac_address = mac_address >> 16;
528 rxq_desc.fields.valid = 1;
529 rxq_desc.fields.length = adapter->rx_queue.queue_len;
530 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
532 ibmveth_debug_printk("buffer list @ 0x%p\n", adapter->buffer_list_addr);
533 ibmveth_debug_printk("filter list @ 0x%p\n", adapter->filter_list_addr);
534 ibmveth_debug_printk("receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
536 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
538 lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
540 if(lpar_rc != H_SUCCESS) {
541 ibmveth_error_printk("h_register_logical_lan failed with %ld\n", lpar_rc);
542 ibmveth_error_printk("buffer TCE:0x%lx filter TCE:0x%lx rxq desc:0x%lx MAC:0x%lx\n",
543 adapter->buffer_list_dma,
544 adapter->filter_list_dma,
547 ibmveth_cleanup(adapter);
551 for(i = 0; i<IbmVethNumBufferPools; i++) {
552 if(!adapter->rx_buff_pool[i].active)
554 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
555 ibmveth_error_printk("unable to alloc pool\n");
556 adapter->rx_buff_pool[i].active = 0;
557 ibmveth_cleanup(adapter);
562 ibmveth_debug_printk("registering irq 0x%x\n", netdev->irq);
563 if((rc = request_irq(netdev->irq, &ibmveth_interrupt, 0, netdev->name, netdev)) != 0) {
564 ibmveth_error_printk("unable to request irq 0x%x, rc %d\n", netdev->irq, rc);
566 rc = h_free_logical_lan(adapter->vdev->unit_address);
567 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
569 ibmveth_cleanup(adapter);
573 ibmveth_debug_printk("initial replenish cycle\n");
574 ibmveth_interrupt(netdev->irq, netdev);
576 netif_start_queue(netdev);
578 ibmveth_debug_printk("open complete\n");
583 static int ibmveth_close(struct net_device *netdev)
585 struct ibmveth_adapter *adapter = netdev->priv;
588 ibmveth_debug_printk("close starting\n");
590 if (!adapter->pool_config)
591 netif_stop_queue(netdev);
593 free_irq(netdev->irq, netdev);
596 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
597 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
599 if(lpar_rc != H_SUCCESS)
601 ibmveth_error_printk("h_free_logical_lan failed with %lx, continuing with close\n",
605 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
607 ibmveth_cleanup(adapter);
609 ibmveth_debug_printk("close complete\n");
614 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
615 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
616 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | ADVERTISED_FIBRE);
617 cmd->speed = SPEED_1000;
618 cmd->duplex = DUPLEX_FULL;
619 cmd->port = PORT_FIBRE;
620 cmd->phy_address = 0;
621 cmd->transceiver = XCVR_INTERNAL;
622 cmd->autoneg = AUTONEG_ENABLE;
628 static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info) {
629 strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
630 strncpy(info->version, ibmveth_driver_version, sizeof(info->version) - 1);
633 static u32 netdev_get_link(struct net_device *dev) {
637 static const struct ethtool_ops netdev_ethtool_ops = {
638 .get_drvinfo = netdev_get_drvinfo,
639 .get_settings = netdev_get_settings,
640 .get_link = netdev_get_link,
641 .get_sg = ethtool_op_get_sg,
642 .get_tx_csum = ethtool_op_get_tx_csum,
645 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
650 #define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
652 static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *netdev)
654 struct ibmveth_adapter *adapter = netdev->priv;
655 union ibmveth_buf_desc desc[IbmVethMaxSendFrags];
656 unsigned long lpar_rc;
657 int nfrags = 0, curfrag;
658 unsigned long correlator;
660 unsigned int retry_count;
661 unsigned int tx_dropped = 0;
662 unsigned int tx_bytes = 0;
663 unsigned int tx_packets = 0;
664 unsigned int tx_send_failed = 0;
665 unsigned int tx_map_failed = 0;
668 if ((skb_shinfo(skb)->nr_frags + 1) > IbmVethMaxSendFrags) {
673 memset(&desc, 0, sizeof(desc));
675 /* nfrags = number of frags after the initial fragment */
676 nfrags = skb_shinfo(skb)->nr_frags;
679 adapter->tx_multidesc_send++;
681 /* map the initial fragment */
682 desc[0].fields.length = nfrags ? skb->len - skb->data_len : skb->len;
683 desc[0].fields.address = dma_map_single(&adapter->vdev->dev, skb->data,
684 desc[0].fields.length, DMA_TO_DEVICE);
685 desc[0].fields.valid = 1;
687 if(dma_mapping_error(desc[0].fields.address)) {
688 ibmveth_error_printk("tx: unable to map initial fragment\n");
696 /* map fragments past the initial portion if there are any */
698 skb_frag_t *frag = &skb_shinfo(skb)->frags[curfrag];
699 desc[curfrag+1].fields.address
700 = dma_map_single(&adapter->vdev->dev,
701 page_address(frag->page) + frag->page_offset,
702 frag->size, DMA_TO_DEVICE);
703 desc[curfrag+1].fields.length = frag->size;
704 desc[curfrag+1].fields.valid = 1;
706 if(dma_mapping_error(desc[curfrag+1].fields.address)) {
707 ibmveth_error_printk("tx: unable to map fragment %d\n", curfrag);
710 /* Free all the mappings we just created */
711 while(curfrag < nfrags) {
712 dma_unmap_single(&adapter->vdev->dev,
713 desc[curfrag+1].fields.address,
714 desc[curfrag+1].fields.length,
722 /* send the frame. Arbitrarily set retrycount to 1024 */
726 lpar_rc = h_send_logical_lan(adapter->vdev->unit_address,
735 } while ((lpar_rc == H_BUSY) && (retry_count--));
737 if(lpar_rc != H_SUCCESS && lpar_rc != H_DROPPED) {
739 ibmveth_error_printk("tx: h_send_logical_lan failed with rc=%ld\n", lpar_rc);
740 for(i = 0; i < 6; i++) {
741 ibmveth_error_printk("tx: desc[%i] valid=%d, len=%d, address=0x%d\n", i,
742 desc[i].fields.valid, desc[i].fields.length, desc[i].fields.address);
748 tx_bytes += skb->len;
749 netdev->trans_start = jiffies;
753 dma_unmap_single(&adapter->vdev->dev,
754 desc[nfrags].fields.address,
755 desc[nfrags].fields.length, DMA_TO_DEVICE);
756 } while(--nfrags >= 0);
758 out: spin_lock_irqsave(&adapter->stats_lock, flags);
759 adapter->stats.tx_dropped += tx_dropped;
760 adapter->stats.tx_bytes += tx_bytes;
761 adapter->stats.tx_packets += tx_packets;
762 adapter->tx_send_failed += tx_send_failed;
763 adapter->tx_map_failed += tx_map_failed;
764 spin_unlock_irqrestore(&adapter->stats_lock, flags);
770 static int ibmveth_poll(struct net_device *netdev, int *budget)
772 struct ibmveth_adapter *adapter = netdev->priv;
773 int max_frames_to_process = netdev->quota;
774 int frames_processed = 0;
776 unsigned long lpar_rc;
780 struct net_device *netdev = adapter->netdev;
782 if(ibmveth_rxq_pending_buffer(adapter)) {
787 if(!ibmveth_rxq_buffer_valid(adapter)) {
788 wmb(); /* suggested by larson1 */
789 adapter->rx_invalid_buffer++;
790 ibmveth_debug_printk("recycling invalid buffer\n");
791 ibmveth_rxq_recycle_buffer(adapter);
793 int length = ibmveth_rxq_frame_length(adapter);
794 int offset = ibmveth_rxq_frame_offset(adapter);
795 skb = ibmveth_rxq_get_buffer(adapter);
797 ibmveth_rxq_harvest_buffer(adapter);
799 skb_reserve(skb, offset);
800 skb_put(skb, length);
802 skb->protocol = eth_type_trans(skb, netdev);
804 netif_receive_skb(skb); /* send it up */
806 adapter->stats.rx_packets++;
807 adapter->stats.rx_bytes += length;
809 netdev->last_rx = jiffies;
814 } while(more_work && (frames_processed < max_frames_to_process));
816 ibmveth_replenish_task(adapter);
819 /* more work to do - return that we are not done yet */
820 netdev->quota -= frames_processed;
821 *budget -= frames_processed;
825 /* we think we are done - reenable interrupts, then check once more to make sure we are done */
826 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_ENABLE);
828 ibmveth_assert(lpar_rc == H_SUCCESS);
830 netif_rx_complete(netdev);
832 if(ibmveth_rxq_pending_buffer(adapter) && netif_rx_reschedule(netdev, frames_processed))
834 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
835 ibmveth_assert(lpar_rc == H_SUCCESS);
840 netdev->quota -= frames_processed;
841 *budget -= frames_processed;
843 /* we really are done */
847 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
849 struct net_device *netdev = dev_instance;
850 struct ibmveth_adapter *adapter = netdev->priv;
851 unsigned long lpar_rc;
853 if(netif_rx_schedule_prep(netdev)) {
854 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
855 ibmveth_assert(lpar_rc == H_SUCCESS);
856 __netif_rx_schedule(netdev);
861 static struct net_device_stats *ibmveth_get_stats(struct net_device *dev)
863 struct ibmveth_adapter *adapter = dev->priv;
864 return &adapter->stats;
867 static void ibmveth_set_multicast_list(struct net_device *netdev)
869 struct ibmveth_adapter *adapter = netdev->priv;
870 unsigned long lpar_rc;
872 if((netdev->flags & IFF_PROMISC) || (netdev->mc_count > adapter->mcastFilterSize)) {
873 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
874 IbmVethMcastEnableRecv |
875 IbmVethMcastDisableFiltering,
877 if(lpar_rc != H_SUCCESS) {
878 ibmveth_error_printk("h_multicast_ctrl rc=%ld when entering promisc mode\n", lpar_rc);
881 struct dev_mc_list *mclist = netdev->mc_list;
883 /* clear the filter table & disable filtering */
884 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
885 IbmVethMcastEnableRecv |
886 IbmVethMcastDisableFiltering |
887 IbmVethMcastClearFilterTable,
889 if(lpar_rc != H_SUCCESS) {
890 ibmveth_error_printk("h_multicast_ctrl rc=%ld when attempting to clear filter table\n", lpar_rc);
892 /* add the addresses to the filter table */
893 for(i = 0; i < netdev->mc_count; ++i, mclist = mclist->next) {
894 // add the multicast address to the filter table
895 unsigned long mcast_addr = 0;
896 memcpy(((char *)&mcast_addr)+2, mclist->dmi_addr, 6);
897 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
898 IbmVethMcastAddFilter,
900 if(lpar_rc != H_SUCCESS) {
901 ibmveth_error_printk("h_multicast_ctrl rc=%ld when adding an entry to the filter table\n", lpar_rc);
905 /* re-enable filtering */
906 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
907 IbmVethMcastEnableFiltering,
909 if(lpar_rc != H_SUCCESS) {
910 ibmveth_error_printk("h_multicast_ctrl rc=%ld when enabling filtering\n", lpar_rc);
915 static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
917 struct ibmveth_adapter *adapter = dev->priv;
918 int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
921 if (new_mtu < IBMVETH_MAX_MTU)
924 /* Look for an active buffer pool that can hold the new MTU */
925 for(i = 0; i<IbmVethNumBufferPools; i++) {
926 if (!adapter->rx_buff_pool[i].active)
928 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size) {
936 #ifdef CONFIG_NET_POLL_CONTROLLER
937 static void ibmveth_poll_controller(struct net_device *dev)
939 ibmveth_replenish_task(dev->priv);
940 ibmveth_interrupt(dev->irq, dev);
944 static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
947 struct net_device *netdev;
948 struct ibmveth_adapter *adapter = NULL;
950 unsigned char *mac_addr_p;
951 unsigned int *mcastFilterSize_p;
954 ibmveth_debug_printk_no_adapter("entering ibmveth_probe for UA 0x%x\n",
957 mac_addr_p = (unsigned char *) vio_get_attribute(dev, VETH_MAC_ADDR, 0);
959 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find VETH_MAC_ADDR "
960 "attribute\n", __FILE__, __LINE__);
964 mcastFilterSize_p= (unsigned int *) vio_get_attribute(dev, VETH_MCAST_FILTER_SIZE, 0);
965 if(!mcastFilterSize_p) {
966 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find "
967 "VETH_MCAST_FILTER_SIZE attribute\n",
972 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
977 SET_MODULE_OWNER(netdev);
979 adapter = netdev->priv;
980 memset(adapter, 0, sizeof(adapter));
981 dev->dev.driver_data = netdev;
984 adapter->netdev = netdev;
985 adapter->mcastFilterSize= *mcastFilterSize_p;
986 adapter->pool_config = 0;
988 /* Some older boxes running PHYP non-natively have an OF that
989 returns a 8-byte local-mac-address field (and the first
990 2 bytes have to be ignored) while newer boxes' OF return
991 a 6-byte field. Note that IEEE 1275 specifies that
992 local-mac-address must be a 6-byte field.
993 The RPA doc specifies that the first byte must be 10b, so
994 we'll just look for it to solve this 8 vs. 6 byte field issue */
996 if ((*mac_addr_p & 0x3) != 0x02)
999 adapter->mac_addr = 0;
1000 memcpy(&adapter->mac_addr, mac_addr_p, 6);
1002 netdev->irq = dev->irq;
1003 netdev->open = ibmveth_open;
1004 netdev->poll = ibmveth_poll;
1005 netdev->weight = 16;
1006 netdev->stop = ibmveth_close;
1007 netdev->hard_start_xmit = ibmveth_start_xmit;
1008 netdev->get_stats = ibmveth_get_stats;
1009 netdev->set_multicast_list = ibmveth_set_multicast_list;
1010 netdev->do_ioctl = ibmveth_ioctl;
1011 netdev->ethtool_ops = &netdev_ethtool_ops;
1012 netdev->change_mtu = ibmveth_change_mtu;
1013 SET_NETDEV_DEV(netdev, &dev->dev);
1014 #ifdef CONFIG_NET_POLL_CONTROLLER
1015 netdev->poll_controller = ibmveth_poll_controller;
1017 netdev->features |= NETIF_F_LLTX;
1018 spin_lock_init(&adapter->stats_lock);
1020 memcpy(&netdev->dev_addr, &adapter->mac_addr, netdev->addr_len);
1022 for(i = 0; i<IbmVethNumBufferPools; i++) {
1023 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
1024 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1025 pool_count[i], pool_size[i],
1027 kobj->parent = &dev->dev.kobj;
1028 sprintf(kobj->name, "pool%d", i);
1029 kobj->ktype = &ktype_veth_pool;
1030 kobject_register(kobj);
1033 ibmveth_debug_printk("adapter @ 0x%p\n", adapter);
1035 adapter->buffer_list_dma = DMA_ERROR_CODE;
1036 adapter->filter_list_dma = DMA_ERROR_CODE;
1037 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1039 ibmveth_debug_printk("registering netdev...\n");
1041 rc = register_netdev(netdev);
1044 ibmveth_debug_printk("failed to register netdev rc=%d\n", rc);
1045 free_netdev(netdev);
1049 ibmveth_debug_printk("registered\n");
1051 ibmveth_proc_register_adapter(adapter);
1056 static int __devexit ibmveth_remove(struct vio_dev *dev)
1058 struct net_device *netdev = dev->dev.driver_data;
1059 struct ibmveth_adapter *adapter = netdev->priv;
1062 for(i = 0; i<IbmVethNumBufferPools; i++)
1063 kobject_unregister(&adapter->rx_buff_pool[i].kobj);
1065 unregister_netdev(netdev);
1067 ibmveth_proc_unregister_adapter(adapter);
1069 free_netdev(netdev);
1073 #ifdef CONFIG_PROC_FS
1074 static void ibmveth_proc_register_driver(void)
1076 ibmveth_proc_dir = proc_mkdir(IBMVETH_PROC_DIR, NULL);
1077 if (ibmveth_proc_dir) {
1078 SET_MODULE_OWNER(ibmveth_proc_dir);
1082 static void ibmveth_proc_unregister_driver(void)
1084 remove_proc_entry(IBMVETH_PROC_DIR, NULL);
1087 static void *ibmveth_seq_start(struct seq_file *seq, loff_t *pos)
1096 static void *ibmveth_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1102 static void ibmveth_seq_stop(struct seq_file *seq, void *v)
1106 static int ibmveth_seq_show(struct seq_file *seq, void *v)
1108 struct ibmveth_adapter *adapter = seq->private;
1109 char *current_mac = ((char*) &adapter->netdev->dev_addr);
1110 char *firmware_mac = ((char*) &adapter->mac_addr) ;
1112 seq_printf(seq, "%s %s\n\n", ibmveth_driver_string, ibmveth_driver_version);
1114 seq_printf(seq, "Unit Address: 0x%x\n", adapter->vdev->unit_address);
1115 seq_printf(seq, "Current MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
1116 current_mac[0], current_mac[1], current_mac[2],
1117 current_mac[3], current_mac[4], current_mac[5]);
1118 seq_printf(seq, "Firmware MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
1119 firmware_mac[0], firmware_mac[1], firmware_mac[2],
1120 firmware_mac[3], firmware_mac[4], firmware_mac[5]);
1122 seq_printf(seq, "\nAdapter Statistics:\n");
1123 seq_printf(seq, " TX: skbuffs linearized: %ld\n", adapter->tx_linearized);
1124 seq_printf(seq, " multi-descriptor sends: %ld\n", adapter->tx_multidesc_send);
1125 seq_printf(seq, " skb_linearize failures: %ld\n", adapter->tx_linearize_failed);
1126 seq_printf(seq, " vio_map_single failres: %ld\n", adapter->tx_map_failed);
1127 seq_printf(seq, " send failures: %ld\n", adapter->tx_send_failed);
1128 seq_printf(seq, " RX: replenish task cycles: %ld\n", adapter->replenish_task_cycles);
1129 seq_printf(seq, " alloc_skb_failures: %ld\n", adapter->replenish_no_mem);
1130 seq_printf(seq, " add buffer failures: %ld\n", adapter->replenish_add_buff_failure);
1131 seq_printf(seq, " invalid buffers: %ld\n", adapter->rx_invalid_buffer);
1132 seq_printf(seq, " no buffers: %ld\n", adapter->rx_no_buffer);
1136 static struct seq_operations ibmveth_seq_ops = {
1137 .start = ibmveth_seq_start,
1138 .next = ibmveth_seq_next,
1139 .stop = ibmveth_seq_stop,
1140 .show = ibmveth_seq_show,
1143 static int ibmveth_proc_open(struct inode *inode, struct file *file)
1145 struct seq_file *seq;
1146 struct proc_dir_entry *proc;
1149 rc = seq_open(file, &ibmveth_seq_ops);
1151 /* recover the pointer buried in proc_dir_entry data */
1152 seq = file->private_data;
1154 seq->private = proc->data;
1159 static struct file_operations ibmveth_proc_fops = {
1160 .owner = THIS_MODULE,
1161 .open = ibmveth_proc_open,
1163 .llseek = seq_lseek,
1164 .release = seq_release,
1167 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1169 struct proc_dir_entry *entry;
1170 if (ibmveth_proc_dir) {
1172 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1173 entry = create_proc_entry(u_addr, S_IFREG, ibmveth_proc_dir);
1175 ibmveth_error_printk("Cannot create adapter proc entry");
1177 entry->data = (void *) adapter;
1178 entry->proc_fops = &ibmveth_proc_fops;
1179 SET_MODULE_OWNER(entry);
1185 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1187 if (ibmveth_proc_dir) {
1189 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1190 remove_proc_entry(u_addr, ibmveth_proc_dir);
1194 #else /* CONFIG_PROC_FS */
1195 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1199 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1202 static void ibmveth_proc_register_driver(void)
1206 static void ibmveth_proc_unregister_driver(void)
1209 #endif /* CONFIG_PROC_FS */
1211 static struct attribute veth_active_attr;
1212 static struct attribute veth_num_attr;
1213 static struct attribute veth_size_attr;
1215 static ssize_t veth_pool_show(struct kobject * kobj,
1216 struct attribute * attr, char * buf)
1218 struct ibmveth_buff_pool *pool = container_of(kobj,
1219 struct ibmveth_buff_pool,
1222 if (attr == &veth_active_attr)
1223 return sprintf(buf, "%d\n", pool->active);
1224 else if (attr == &veth_num_attr)
1225 return sprintf(buf, "%d\n", pool->size);
1226 else if (attr == &veth_size_attr)
1227 return sprintf(buf, "%d\n", pool->buff_size);
1231 static ssize_t veth_pool_store(struct kobject * kobj, struct attribute * attr,
1232 const char * buf, size_t count)
1234 struct ibmveth_buff_pool *pool = container_of(kobj,
1235 struct ibmveth_buff_pool,
1237 struct net_device *netdev =
1238 container_of(kobj->parent, struct device, kobj)->driver_data;
1239 struct ibmveth_adapter *adapter = netdev->priv;
1240 long value = simple_strtol(buf, NULL, 10);
1243 if (attr == &veth_active_attr) {
1244 if (value && !pool->active) {
1245 if(ibmveth_alloc_buffer_pool(pool)) {
1246 ibmveth_error_printk("unable to alloc pool\n");
1250 adapter->pool_config = 1;
1251 ibmveth_close(netdev);
1252 adapter->pool_config = 0;
1253 if ((rc = ibmveth_open(netdev)))
1255 } else if (!value && pool->active) {
1256 int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1258 /* Make sure there is a buffer pool with buffers that
1259 can hold a packet of the size of the MTU */
1260 for(i = 0; i<IbmVethNumBufferPools; i++) {
1261 if (pool == &adapter->rx_buff_pool[i])
1263 if (!adapter->rx_buff_pool[i].active)
1265 if (mtu < adapter->rx_buff_pool[i].buff_size) {
1267 h_free_logical_lan_buffer(adapter->
1275 ibmveth_error_printk("no active pool >= MTU\n");
1279 } else if (attr == &veth_num_attr) {
1280 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT)
1283 adapter->pool_config = 1;
1284 ibmveth_close(netdev);
1285 adapter->pool_config = 0;
1287 if ((rc = ibmveth_open(netdev)))
1290 } else if (attr == &veth_size_attr) {
1291 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE)
1294 adapter->pool_config = 1;
1295 ibmveth_close(netdev);
1296 adapter->pool_config = 0;
1297 pool->buff_size = value;
1298 if ((rc = ibmveth_open(netdev)))
1303 /* kick the interrupt handler to allocate/deallocate pools */
1304 ibmveth_interrupt(netdev->irq, netdev);
1309 #define ATTR(_name, _mode) \
1310 struct attribute veth_##_name##_attr = { \
1311 .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE \
1314 static ATTR(active, 0644);
1315 static ATTR(num, 0644);
1316 static ATTR(size, 0644);
1318 static struct attribute * veth_pool_attrs[] = {
1325 static struct sysfs_ops veth_pool_ops = {
1326 .show = veth_pool_show,
1327 .store = veth_pool_store,
1330 static struct kobj_type ktype_veth_pool = {
1332 .sysfs_ops = &veth_pool_ops,
1333 .default_attrs = veth_pool_attrs,
1337 static struct vio_device_id ibmveth_device_table[] __devinitdata= {
1338 { "network", "IBM,l-lan"},
1341 MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1343 static struct vio_driver ibmveth_driver = {
1344 .id_table = ibmveth_device_table,
1345 .probe = ibmveth_probe,
1346 .remove = ibmveth_remove,
1348 .name = ibmveth_driver_name,
1349 .owner = THIS_MODULE,
1353 static int __init ibmveth_module_init(void)
1355 ibmveth_printk("%s: %s %s\n", ibmveth_driver_name, ibmveth_driver_string, ibmveth_driver_version);
1357 ibmveth_proc_register_driver();
1359 return vio_register_driver(&ibmveth_driver);
1362 static void __exit ibmveth_module_exit(void)
1364 vio_unregister_driver(&ibmveth_driver);
1365 ibmveth_proc_unregister_driver();
1368 module_init(ibmveth_module_init);
1369 module_exit(ibmveth_module_exit);