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>
53 #include <asm/iommu.h>
55 #include <asm/uaccess.h>
56 #include <linux/seq_file.h>
62 #define ibmveth_printk(fmt, args...) \
63 printk(KERN_DEBUG "%s: " fmt, __FILE__, ## args)
65 #define ibmveth_error_printk(fmt, args...) \
66 printk(KERN_ERR "(%s:%3.3d ua:%x) ERROR: " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
69 #define ibmveth_debug_printk_no_adapter(fmt, args...) \
70 printk(KERN_DEBUG "(%s:%3.3d): " fmt, __FILE__, __LINE__ , ## args)
71 #define ibmveth_debug_printk(fmt, args...) \
72 printk(KERN_DEBUG "(%s:%3.3d ua:%x): " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
73 #define ibmveth_assert(expr) \
75 printk(KERN_DEBUG "assertion failed (%s:%3.3d ua:%x): %s\n", __FILE__, __LINE__, adapter->vdev->unit_address, #expr); \
79 #define ibmveth_debug_printk_no_adapter(fmt, args...)
80 #define ibmveth_debug_printk(fmt, args...)
81 #define ibmveth_assert(expr)
84 static int ibmveth_open(struct net_device *dev);
85 static int ibmveth_close(struct net_device *dev);
86 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
87 static int ibmveth_poll(struct net_device *dev, int *budget);
88 static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *dev);
89 static struct net_device_stats *ibmveth_get_stats(struct net_device *dev);
90 static void ibmveth_set_multicast_list(struct net_device *dev);
91 static int ibmveth_change_mtu(struct net_device *dev, int new_mtu);
92 static void ibmveth_proc_register_driver(void);
93 static void ibmveth_proc_unregister_driver(void);
94 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter);
95 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter);
96 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
97 static inline void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
98 static struct kobj_type ktype_veth_pool;
100 #ifdef CONFIG_PROC_FS
101 #define IBMVETH_PROC_DIR "net/ibmveth"
102 static struct proc_dir_entry *ibmveth_proc_dir;
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, u32 pool_active)
138 pool->size = pool_size;
139 pool->index = pool_index;
140 pool->buff_size = buff_size;
141 pool->threshold = pool_size / 2;
142 pool->active = pool_active;
145 /* allocate and setup an buffer pool - called during open */
146 static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
150 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
152 if(!pool->free_map) {
156 pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
157 if(!pool->dma_addr) {
158 kfree(pool->free_map);
159 pool->free_map = NULL;
163 pool->skbuff = kmalloc(sizeof(void*) * pool->size, GFP_KERNEL);
166 kfree(pool->dma_addr);
167 pool->dma_addr = NULL;
169 kfree(pool->free_map);
170 pool->free_map = NULL;
174 memset(pool->skbuff, 0, sizeof(void*) * pool->size);
175 memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
177 for(i = 0; i < pool->size; ++i) {
178 pool->free_map[i] = i;
181 atomic_set(&pool->available, 0);
182 pool->producer_index = 0;
183 pool->consumer_index = 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)
194 u32 count = pool->size - atomic_read(&pool->available);
195 u32 buffers_added = 0;
199 for(i = 0; i < count; ++i) {
201 unsigned int free_index, index;
203 union ibmveth_buf_desc desc;
204 unsigned long lpar_rc;
207 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
210 ibmveth_debug_printk("replenish: unable to allocate skb\n");
211 adapter->replenish_no_mem++;
215 free_index = pool->consumer_index;
216 pool->consumer_index = (pool->consumer_index + 1) % pool->size;
217 index = pool->free_map[free_index];
219 ibmveth_assert(index != IBM_VETH_INVALID_MAP);
220 ibmveth_assert(pool->skbuff[index] == NULL);
222 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
223 pool->buff_size, DMA_FROM_DEVICE);
225 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
226 pool->dma_addr[index] = dma_addr;
227 pool->skbuff[index] = skb;
229 correlator = ((u64)pool->index << 32) | index;
230 *(u64*)skb->data = correlator;
233 desc.fields.valid = 1;
234 desc.fields.length = pool->buff_size;
235 desc.fields.address = dma_addr;
237 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
239 if(lpar_rc != H_SUCCESS) {
240 pool->free_map[free_index] = index;
241 pool->skbuff[index] = NULL;
242 if (pool->consumer_index == 0)
243 pool->consumer_index = pool->size - 1;
245 pool->consumer_index--;
246 dma_unmap_single(&adapter->vdev->dev,
247 pool->dma_addr[index], pool->buff_size,
249 dev_kfree_skb_any(skb);
250 adapter->replenish_add_buff_failure++;
254 adapter->replenish_add_buff_success++;
259 atomic_add(buffers_added, &(pool->available));
262 /* replenish routine */
263 static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
267 adapter->replenish_task_cycles++;
269 for(i = 0; i < IbmVethNumBufferPools; i++)
270 if(adapter->rx_buff_pool[i].active)
271 ibmveth_replenish_buffer_pool(adapter,
272 &adapter->rx_buff_pool[i]);
274 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
277 /* empty and free ana buffer pool - also used to do cleanup in error paths */
278 static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
282 kfree(pool->free_map);
283 pool->free_map = NULL;
285 if(pool->skbuff && pool->dma_addr) {
286 for(i = 0; i < pool->size; ++i) {
287 struct sk_buff *skb = pool->skbuff[i];
289 dma_unmap_single(&adapter->vdev->dev,
293 dev_kfree_skb_any(skb);
294 pool->skbuff[i] = NULL;
300 kfree(pool->dma_addr);
301 pool->dma_addr = NULL;
310 /* remove a buffer from a pool */
311 static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter, u64 correlator)
313 unsigned int pool = correlator >> 32;
314 unsigned int index = correlator & 0xffffffffUL;
315 unsigned int free_index;
318 ibmveth_assert(pool < IbmVethNumBufferPools);
319 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
321 skb = adapter->rx_buff_pool[pool].skbuff[index];
323 ibmveth_assert(skb != NULL);
325 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
327 dma_unmap_single(&adapter->vdev->dev,
328 adapter->rx_buff_pool[pool].dma_addr[index],
329 adapter->rx_buff_pool[pool].buff_size,
332 free_index = adapter->rx_buff_pool[pool].producer_index;
333 adapter->rx_buff_pool[pool].producer_index
334 = (adapter->rx_buff_pool[pool].producer_index + 1)
335 % adapter->rx_buff_pool[pool].size;
336 adapter->rx_buff_pool[pool].free_map[free_index] = index;
340 atomic_dec(&(adapter->rx_buff_pool[pool].available));
343 /* get the current buffer on the rx queue */
344 static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
346 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
347 unsigned int pool = correlator >> 32;
348 unsigned int index = correlator & 0xffffffffUL;
350 ibmveth_assert(pool < IbmVethNumBufferPools);
351 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
353 return adapter->rx_buff_pool[pool].skbuff[index];
356 /* recycle the current buffer on the rx queue */
357 static void ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
359 u32 q_index = adapter->rx_queue.index;
360 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
361 unsigned int pool = correlator >> 32;
362 unsigned int index = correlator & 0xffffffffUL;
363 union ibmveth_buf_desc desc;
364 unsigned long lpar_rc;
366 ibmveth_assert(pool < IbmVethNumBufferPools);
367 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
369 if(!adapter->rx_buff_pool[pool].active) {
370 ibmveth_rxq_harvest_buffer(adapter);
371 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
376 desc.fields.valid = 1;
377 desc.fields.length = adapter->rx_buff_pool[pool].buff_size;
378 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
380 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
382 if(lpar_rc != H_SUCCESS) {
383 ibmveth_debug_printk("h_add_logical_lan_buffer failed during recycle rc=%ld", lpar_rc);
384 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
387 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
388 adapter->rx_queue.index = 0;
389 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
393 static inline void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
395 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
397 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
398 adapter->rx_queue.index = 0;
399 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
403 static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
407 if(adapter->buffer_list_addr != NULL) {
408 if(!dma_mapping_error(adapter->buffer_list_dma)) {
409 dma_unmap_single(&adapter->vdev->dev,
410 adapter->buffer_list_dma, 4096,
412 adapter->buffer_list_dma = DMA_ERROR_CODE;
414 free_page((unsigned long)adapter->buffer_list_addr);
415 adapter->buffer_list_addr = NULL;
418 if(adapter->filter_list_addr != NULL) {
419 if(!dma_mapping_error(adapter->filter_list_dma)) {
420 dma_unmap_single(&adapter->vdev->dev,
421 adapter->filter_list_dma, 4096,
423 adapter->filter_list_dma = DMA_ERROR_CODE;
425 free_page((unsigned long)adapter->filter_list_addr);
426 adapter->filter_list_addr = NULL;
429 if(adapter->rx_queue.queue_addr != NULL) {
430 if(!dma_mapping_error(adapter->rx_queue.queue_dma)) {
431 dma_unmap_single(&adapter->vdev->dev,
432 adapter->rx_queue.queue_dma,
433 adapter->rx_queue.queue_len,
435 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
437 kfree(adapter->rx_queue.queue_addr);
438 adapter->rx_queue.queue_addr = NULL;
441 for(i = 0; i<IbmVethNumBufferPools; i++)
442 if (adapter->rx_buff_pool[i].active)
443 ibmveth_free_buffer_pool(adapter,
444 &adapter->rx_buff_pool[i]);
447 static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
448 union ibmveth_buf_desc rxq_desc, u64 mac_address)
450 int rc, try_again = 1;
452 /* After a kexec the adapter will still be open, so our attempt to
453 * open it will fail. So if we get a failure we free the adapter and
454 * try again, but only once. */
456 rc = h_register_logical_lan(adapter->vdev->unit_address,
457 adapter->buffer_list_dma, rxq_desc.desc,
458 adapter->filter_list_dma, mac_address);
460 if (rc != H_SUCCESS && try_again) {
462 rc = h_free_logical_lan(adapter->vdev->unit_address);
463 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
472 static int ibmveth_open(struct net_device *netdev)
474 struct ibmveth_adapter *adapter = netdev->priv;
477 unsigned long lpar_rc;
479 union ibmveth_buf_desc rxq_desc;
482 ibmveth_debug_printk("open starting\n");
484 for(i = 0; i<IbmVethNumBufferPools; i++)
485 rxq_entries += adapter->rx_buff_pool[i].size;
487 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
488 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
490 if(!adapter->buffer_list_addr || !adapter->filter_list_addr) {
491 ibmveth_error_printk("unable to allocate filter or buffer list pages\n");
492 ibmveth_cleanup(adapter);
496 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) * rxq_entries;
497 adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len, GFP_KERNEL);
499 if(!adapter->rx_queue.queue_addr) {
500 ibmveth_error_printk("unable to allocate rx queue pages\n");
501 ibmveth_cleanup(adapter);
505 adapter->buffer_list_dma = dma_map_single(&adapter->vdev->dev,
506 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
507 adapter->filter_list_dma = dma_map_single(&adapter->vdev->dev,
508 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
509 adapter->rx_queue.queue_dma = dma_map_single(&adapter->vdev->dev,
510 adapter->rx_queue.queue_addr,
511 adapter->rx_queue.queue_len, DMA_BIDIRECTIONAL);
513 if((dma_mapping_error(adapter->buffer_list_dma) ) ||
514 (dma_mapping_error(adapter->filter_list_dma)) ||
515 (dma_mapping_error(adapter->rx_queue.queue_dma))) {
516 ibmveth_error_printk("unable to map filter or buffer list pages\n");
517 ibmveth_cleanup(adapter);
521 adapter->rx_queue.index = 0;
522 adapter->rx_queue.num_slots = rxq_entries;
523 adapter->rx_queue.toggle = 1;
525 memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
526 mac_address = mac_address >> 16;
529 rxq_desc.fields.valid = 1;
530 rxq_desc.fields.length = adapter->rx_queue.queue_len;
531 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
533 ibmveth_debug_printk("buffer list @ 0x%p\n", adapter->buffer_list_addr);
534 ibmveth_debug_printk("filter list @ 0x%p\n", adapter->filter_list_addr);
535 ibmveth_debug_printk("receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
537 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
539 lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
541 if(lpar_rc != H_SUCCESS) {
542 ibmveth_error_printk("h_register_logical_lan failed with %ld\n", lpar_rc);
543 ibmveth_error_printk("buffer TCE:0x%lx filter TCE:0x%lx rxq desc:0x%lx MAC:0x%lx\n",
544 adapter->buffer_list_dma,
545 adapter->filter_list_dma,
548 ibmveth_cleanup(adapter);
552 for(i = 0; i<IbmVethNumBufferPools; i++) {
553 if(!adapter->rx_buff_pool[i].active)
555 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
556 ibmveth_error_printk("unable to alloc pool\n");
557 adapter->rx_buff_pool[i].active = 0;
558 ibmveth_cleanup(adapter);
563 ibmveth_debug_printk("registering irq 0x%x\n", netdev->irq);
564 if((rc = request_irq(netdev->irq, &ibmveth_interrupt, 0, netdev->name, netdev)) != 0) {
565 ibmveth_error_printk("unable to request irq 0x%x, rc %d\n", netdev->irq, rc);
567 rc = h_free_logical_lan(adapter->vdev->unit_address);
568 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
570 ibmveth_cleanup(adapter);
574 ibmveth_debug_printk("initial replenish cycle\n");
575 ibmveth_interrupt(netdev->irq, netdev);
577 netif_start_queue(netdev);
579 ibmveth_debug_printk("open complete\n");
584 static int ibmveth_close(struct net_device *netdev)
586 struct ibmveth_adapter *adapter = netdev->priv;
589 ibmveth_debug_printk("close starting\n");
591 if (!adapter->pool_config)
592 netif_stop_queue(netdev);
594 free_irq(netdev->irq, netdev);
597 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
598 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
600 if(lpar_rc != H_SUCCESS)
602 ibmveth_error_printk("h_free_logical_lan failed with %lx, continuing with close\n",
606 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
608 ibmveth_cleanup(adapter);
610 ibmveth_debug_printk("close complete\n");
615 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
616 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
617 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | ADVERTISED_FIBRE);
618 cmd->speed = SPEED_1000;
619 cmd->duplex = DUPLEX_FULL;
620 cmd->port = PORT_FIBRE;
621 cmd->phy_address = 0;
622 cmd->transceiver = XCVR_INTERNAL;
623 cmd->autoneg = AUTONEG_ENABLE;
629 static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info) {
630 strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
631 strncpy(info->version, ibmveth_driver_version, sizeof(info->version) - 1);
634 static u32 netdev_get_link(struct net_device *dev) {
638 static const struct ethtool_ops netdev_ethtool_ops = {
639 .get_drvinfo = netdev_get_drvinfo,
640 .get_settings = netdev_get_settings,
641 .get_link = netdev_get_link,
642 .get_sg = ethtool_op_get_sg,
643 .get_tx_csum = ethtool_op_get_tx_csum,
646 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
651 #define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
653 static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *netdev)
655 struct ibmveth_adapter *adapter = netdev->priv;
656 union ibmveth_buf_desc desc[IbmVethMaxSendFrags];
657 unsigned long lpar_rc;
658 int nfrags = 0, curfrag;
659 unsigned long correlator;
661 unsigned int retry_count;
662 unsigned int tx_dropped = 0;
663 unsigned int tx_bytes = 0;
664 unsigned int tx_packets = 0;
665 unsigned int tx_send_failed = 0;
666 unsigned int tx_map_failed = 0;
669 if ((skb_shinfo(skb)->nr_frags + 1) > IbmVethMaxSendFrags) {
674 memset(&desc, 0, sizeof(desc));
676 /* nfrags = number of frags after the initial fragment */
677 nfrags = skb_shinfo(skb)->nr_frags;
680 adapter->tx_multidesc_send++;
682 /* map the initial fragment */
683 desc[0].fields.length = nfrags ? skb->len - skb->data_len : skb->len;
684 desc[0].fields.address = dma_map_single(&adapter->vdev->dev, skb->data,
685 desc[0].fields.length, DMA_TO_DEVICE);
686 desc[0].fields.valid = 1;
688 if(dma_mapping_error(desc[0].fields.address)) {
689 ibmveth_error_printk("tx: unable to map initial fragment\n");
697 /* map fragments past the initial portion if there are any */
699 skb_frag_t *frag = &skb_shinfo(skb)->frags[curfrag];
700 desc[curfrag+1].fields.address
701 = dma_map_single(&adapter->vdev->dev,
702 page_address(frag->page) + frag->page_offset,
703 frag->size, DMA_TO_DEVICE);
704 desc[curfrag+1].fields.length = frag->size;
705 desc[curfrag+1].fields.valid = 1;
707 if(dma_mapping_error(desc[curfrag+1].fields.address)) {
708 ibmveth_error_printk("tx: unable to map fragment %d\n", curfrag);
711 /* Free all the mappings we just created */
712 while(curfrag < nfrags) {
713 dma_unmap_single(&adapter->vdev->dev,
714 desc[curfrag+1].fields.address,
715 desc[curfrag+1].fields.length,
723 /* send the frame. Arbitrarily set retrycount to 1024 */
727 lpar_rc = h_send_logical_lan(adapter->vdev->unit_address,
736 } while ((lpar_rc == H_BUSY) && (retry_count--));
738 if(lpar_rc != H_SUCCESS && lpar_rc != H_DROPPED) {
740 ibmveth_error_printk("tx: h_send_logical_lan failed with rc=%ld\n", lpar_rc);
741 for(i = 0; i < 6; i++) {
742 ibmveth_error_printk("tx: desc[%i] valid=%d, len=%d, address=0x%d\n", i,
743 desc[i].fields.valid, desc[i].fields.length, desc[i].fields.address);
749 tx_bytes += skb->len;
750 netdev->trans_start = jiffies;
754 dma_unmap_single(&adapter->vdev->dev,
755 desc[nfrags].fields.address,
756 desc[nfrags].fields.length, DMA_TO_DEVICE);
757 } while(--nfrags >= 0);
759 out: spin_lock_irqsave(&adapter->stats_lock, flags);
760 adapter->stats.tx_dropped += tx_dropped;
761 adapter->stats.tx_bytes += tx_bytes;
762 adapter->stats.tx_packets += tx_packets;
763 adapter->tx_send_failed += tx_send_failed;
764 adapter->tx_map_failed += tx_map_failed;
765 spin_unlock_irqrestore(&adapter->stats_lock, flags);
771 static int ibmveth_poll(struct net_device *netdev, int *budget)
773 struct ibmveth_adapter *adapter = netdev->priv;
774 int max_frames_to_process = netdev->quota;
775 int frames_processed = 0;
777 unsigned long lpar_rc;
781 struct net_device *netdev = adapter->netdev;
783 if(ibmveth_rxq_pending_buffer(adapter)) {
788 if(!ibmveth_rxq_buffer_valid(adapter)) {
789 wmb(); /* suggested by larson1 */
790 adapter->rx_invalid_buffer++;
791 ibmveth_debug_printk("recycling invalid buffer\n");
792 ibmveth_rxq_recycle_buffer(adapter);
794 int length = ibmveth_rxq_frame_length(adapter);
795 int offset = ibmveth_rxq_frame_offset(adapter);
796 skb = ibmveth_rxq_get_buffer(adapter);
798 ibmveth_rxq_harvest_buffer(adapter);
800 skb_reserve(skb, offset);
801 skb_put(skb, length);
803 skb->protocol = eth_type_trans(skb, netdev);
805 netif_receive_skb(skb); /* send it up */
807 adapter->stats.rx_packets++;
808 adapter->stats.rx_bytes += length;
810 netdev->last_rx = jiffies;
815 } while(more_work && (frames_processed < max_frames_to_process));
817 ibmveth_replenish_task(adapter);
820 /* more work to do - return that we are not done yet */
821 netdev->quota -= frames_processed;
822 *budget -= frames_processed;
826 /* we think we are done - reenable interrupts, then check once more to make sure we are done */
827 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_ENABLE);
829 ibmveth_assert(lpar_rc == H_SUCCESS);
831 netif_rx_complete(netdev);
833 if(ibmveth_rxq_pending_buffer(adapter) && netif_rx_reschedule(netdev, frames_processed))
835 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
836 ibmveth_assert(lpar_rc == H_SUCCESS);
841 netdev->quota -= frames_processed;
842 *budget -= frames_processed;
844 /* we really are done */
848 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
850 struct net_device *netdev = dev_instance;
851 struct ibmveth_adapter *adapter = netdev->priv;
852 unsigned long lpar_rc;
854 if(netif_rx_schedule_prep(netdev)) {
855 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
856 ibmveth_assert(lpar_rc == H_SUCCESS);
857 __netif_rx_schedule(netdev);
862 static struct net_device_stats *ibmveth_get_stats(struct net_device *dev)
864 struct ibmveth_adapter *adapter = dev->priv;
865 return &adapter->stats;
868 static void ibmveth_set_multicast_list(struct net_device *netdev)
870 struct ibmveth_adapter *adapter = netdev->priv;
871 unsigned long lpar_rc;
873 if((netdev->flags & IFF_PROMISC) || (netdev->mc_count > adapter->mcastFilterSize)) {
874 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
875 IbmVethMcastEnableRecv |
876 IbmVethMcastDisableFiltering,
878 if(lpar_rc != H_SUCCESS) {
879 ibmveth_error_printk("h_multicast_ctrl rc=%ld when entering promisc mode\n", lpar_rc);
882 struct dev_mc_list *mclist = netdev->mc_list;
884 /* clear the filter table & disable filtering */
885 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
886 IbmVethMcastEnableRecv |
887 IbmVethMcastDisableFiltering |
888 IbmVethMcastClearFilterTable,
890 if(lpar_rc != H_SUCCESS) {
891 ibmveth_error_printk("h_multicast_ctrl rc=%ld when attempting to clear filter table\n", lpar_rc);
893 /* add the addresses to the filter table */
894 for(i = 0; i < netdev->mc_count; ++i, mclist = mclist->next) {
895 // add the multicast address to the filter table
896 unsigned long mcast_addr = 0;
897 memcpy(((char *)&mcast_addr)+2, mclist->dmi_addr, 6);
898 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
899 IbmVethMcastAddFilter,
901 if(lpar_rc != H_SUCCESS) {
902 ibmveth_error_printk("h_multicast_ctrl rc=%ld when adding an entry to the filter table\n", lpar_rc);
906 /* re-enable filtering */
907 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
908 IbmVethMcastEnableFiltering,
910 if(lpar_rc != H_SUCCESS) {
911 ibmveth_error_printk("h_multicast_ctrl rc=%ld when enabling filtering\n", lpar_rc);
916 static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
918 struct ibmveth_adapter *adapter = dev->priv;
919 int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
922 if (new_mtu < IBMVETH_MAX_MTU)
925 /* Look for an active buffer pool that can hold the new MTU */
926 for(i = 0; i<IbmVethNumBufferPools; i++) {
927 if (!adapter->rx_buff_pool[i].active)
929 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size) {
937 #ifdef CONFIG_NET_POLL_CONTROLLER
938 static void ibmveth_poll_controller(struct net_device *dev)
940 ibmveth_replenish_task(dev->priv);
941 ibmveth_interrupt(dev->irq, dev);
945 static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
948 struct net_device *netdev;
949 struct ibmveth_adapter *adapter = NULL;
951 unsigned char *mac_addr_p;
952 unsigned int *mcastFilterSize_p;
955 ibmveth_debug_printk_no_adapter("entering ibmveth_probe for UA 0x%x\n",
958 mac_addr_p = (unsigned char *) vio_get_attribute(dev, VETH_MAC_ADDR, 0);
960 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find VETH_MAC_ADDR "
961 "attribute\n", __FILE__, __LINE__);
965 mcastFilterSize_p= (unsigned int *) vio_get_attribute(dev, VETH_MCAST_FILTER_SIZE, 0);
966 if(!mcastFilterSize_p) {
967 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find "
968 "VETH_MCAST_FILTER_SIZE attribute\n",
973 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
978 SET_MODULE_OWNER(netdev);
980 adapter = netdev->priv;
981 memset(adapter, 0, sizeof(adapter));
982 dev->dev.driver_data = netdev;
985 adapter->netdev = netdev;
986 adapter->mcastFilterSize= *mcastFilterSize_p;
987 adapter->pool_config = 0;
989 /* Some older boxes running PHYP non-natively have an OF that
990 returns a 8-byte local-mac-address field (and the first
991 2 bytes have to be ignored) while newer boxes' OF return
992 a 6-byte field. Note that IEEE 1275 specifies that
993 local-mac-address must be a 6-byte field.
994 The RPA doc specifies that the first byte must be 10b, so
995 we'll just look for it to solve this 8 vs. 6 byte field issue */
997 if ((*mac_addr_p & 0x3) != 0x02)
1000 adapter->mac_addr = 0;
1001 memcpy(&adapter->mac_addr, mac_addr_p, 6);
1003 adapter->liobn = dev->iommu_table->it_index;
1005 netdev->irq = dev->irq;
1006 netdev->open = ibmveth_open;
1007 netdev->poll = ibmveth_poll;
1008 netdev->weight = 16;
1009 netdev->stop = ibmveth_close;
1010 netdev->hard_start_xmit = ibmveth_start_xmit;
1011 netdev->get_stats = ibmveth_get_stats;
1012 netdev->set_multicast_list = ibmveth_set_multicast_list;
1013 netdev->do_ioctl = ibmveth_ioctl;
1014 netdev->ethtool_ops = &netdev_ethtool_ops;
1015 netdev->change_mtu = ibmveth_change_mtu;
1016 SET_NETDEV_DEV(netdev, &dev->dev);
1017 #ifdef CONFIG_NET_POLL_CONTROLLER
1018 netdev->poll_controller = ibmveth_poll_controller;
1020 netdev->features |= NETIF_F_LLTX;
1021 spin_lock_init(&adapter->stats_lock);
1023 memcpy(&netdev->dev_addr, &adapter->mac_addr, netdev->addr_len);
1025 for(i = 0; i<IbmVethNumBufferPools; i++) {
1026 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
1027 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1028 pool_count[i], pool_size[i],
1030 kobj->parent = &dev->dev.kobj;
1031 sprintf(kobj->name, "pool%d", i);
1032 kobj->ktype = &ktype_veth_pool;
1033 kobject_register(kobj);
1036 ibmveth_debug_printk("adapter @ 0x%p\n", adapter);
1038 adapter->buffer_list_dma = DMA_ERROR_CODE;
1039 adapter->filter_list_dma = DMA_ERROR_CODE;
1040 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1042 ibmveth_debug_printk("registering netdev...\n");
1044 rc = register_netdev(netdev);
1047 ibmveth_debug_printk("failed to register netdev rc=%d\n", rc);
1048 free_netdev(netdev);
1052 ibmveth_debug_printk("registered\n");
1054 ibmveth_proc_register_adapter(adapter);
1059 static int __devexit ibmveth_remove(struct vio_dev *dev)
1061 struct net_device *netdev = dev->dev.driver_data;
1062 struct ibmveth_adapter *adapter = netdev->priv;
1065 for(i = 0; i<IbmVethNumBufferPools; i++)
1066 kobject_unregister(&adapter->rx_buff_pool[i].kobj);
1068 unregister_netdev(netdev);
1070 ibmveth_proc_unregister_adapter(adapter);
1072 free_netdev(netdev);
1076 #ifdef CONFIG_PROC_FS
1077 static void ibmveth_proc_register_driver(void)
1079 ibmveth_proc_dir = proc_mkdir(IBMVETH_PROC_DIR, NULL);
1080 if (ibmveth_proc_dir) {
1081 SET_MODULE_OWNER(ibmveth_proc_dir);
1085 static void ibmveth_proc_unregister_driver(void)
1087 remove_proc_entry(IBMVETH_PROC_DIR, NULL);
1090 static void *ibmveth_seq_start(struct seq_file *seq, loff_t *pos)
1099 static void *ibmveth_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1105 static void ibmveth_seq_stop(struct seq_file *seq, void *v)
1109 static int ibmveth_seq_show(struct seq_file *seq, void *v)
1111 struct ibmveth_adapter *adapter = seq->private;
1112 char *current_mac = ((char*) &adapter->netdev->dev_addr);
1113 char *firmware_mac = ((char*) &adapter->mac_addr) ;
1115 seq_printf(seq, "%s %s\n\n", ibmveth_driver_string, ibmveth_driver_version);
1117 seq_printf(seq, "Unit Address: 0x%x\n", adapter->vdev->unit_address);
1118 seq_printf(seq, "LIOBN: 0x%lx\n", adapter->liobn);
1119 seq_printf(seq, "Current MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
1120 current_mac[0], current_mac[1], current_mac[2],
1121 current_mac[3], current_mac[4], current_mac[5]);
1122 seq_printf(seq, "Firmware MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
1123 firmware_mac[0], firmware_mac[1], firmware_mac[2],
1124 firmware_mac[3], firmware_mac[4], firmware_mac[5]);
1126 seq_printf(seq, "\nAdapter Statistics:\n");
1127 seq_printf(seq, " TX: skbuffs linearized: %ld\n", adapter->tx_linearized);
1128 seq_printf(seq, " multi-descriptor sends: %ld\n", adapter->tx_multidesc_send);
1129 seq_printf(seq, " skb_linearize failures: %ld\n", adapter->tx_linearize_failed);
1130 seq_printf(seq, " vio_map_single failres: %ld\n", adapter->tx_map_failed);
1131 seq_printf(seq, " send failures: %ld\n", adapter->tx_send_failed);
1132 seq_printf(seq, " RX: replenish task cycles: %ld\n", adapter->replenish_task_cycles);
1133 seq_printf(seq, " alloc_skb_failures: %ld\n", adapter->replenish_no_mem);
1134 seq_printf(seq, " add buffer failures: %ld\n", adapter->replenish_add_buff_failure);
1135 seq_printf(seq, " invalid buffers: %ld\n", adapter->rx_invalid_buffer);
1136 seq_printf(seq, " no buffers: %ld\n", adapter->rx_no_buffer);
1140 static struct seq_operations ibmveth_seq_ops = {
1141 .start = ibmveth_seq_start,
1142 .next = ibmveth_seq_next,
1143 .stop = ibmveth_seq_stop,
1144 .show = ibmveth_seq_show,
1147 static int ibmveth_proc_open(struct inode *inode, struct file *file)
1149 struct seq_file *seq;
1150 struct proc_dir_entry *proc;
1153 rc = seq_open(file, &ibmveth_seq_ops);
1155 /* recover the pointer buried in proc_dir_entry data */
1156 seq = file->private_data;
1158 seq->private = proc->data;
1163 static struct file_operations ibmveth_proc_fops = {
1164 .owner = THIS_MODULE,
1165 .open = ibmveth_proc_open,
1167 .llseek = seq_lseek,
1168 .release = seq_release,
1171 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1173 struct proc_dir_entry *entry;
1174 if (ibmveth_proc_dir) {
1176 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1177 entry = create_proc_entry(u_addr, S_IFREG, ibmveth_proc_dir);
1179 ibmveth_error_printk("Cannot create adapter proc entry");
1181 entry->data = (void *) adapter;
1182 entry->proc_fops = &ibmveth_proc_fops;
1183 SET_MODULE_OWNER(entry);
1189 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1191 if (ibmveth_proc_dir) {
1193 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1194 remove_proc_entry(u_addr, ibmveth_proc_dir);
1198 #else /* CONFIG_PROC_FS */
1199 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1203 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1206 static void ibmveth_proc_register_driver(void)
1210 static void ibmveth_proc_unregister_driver(void)
1213 #endif /* CONFIG_PROC_FS */
1215 static struct attribute veth_active_attr;
1216 static struct attribute veth_num_attr;
1217 static struct attribute veth_size_attr;
1219 static ssize_t veth_pool_show(struct kobject * kobj,
1220 struct attribute * attr, char * buf)
1222 struct ibmveth_buff_pool *pool = container_of(kobj,
1223 struct ibmveth_buff_pool,
1226 if (attr == &veth_active_attr)
1227 return sprintf(buf, "%d\n", pool->active);
1228 else if (attr == &veth_num_attr)
1229 return sprintf(buf, "%d\n", pool->size);
1230 else if (attr == &veth_size_attr)
1231 return sprintf(buf, "%d\n", pool->buff_size);
1235 static ssize_t veth_pool_store(struct kobject * kobj, struct attribute * attr,
1236 const char * buf, size_t count)
1238 struct ibmveth_buff_pool *pool = container_of(kobj,
1239 struct ibmveth_buff_pool,
1241 struct net_device *netdev =
1242 container_of(kobj->parent, struct device, kobj)->driver_data;
1243 struct ibmveth_adapter *adapter = netdev->priv;
1244 long value = simple_strtol(buf, NULL, 10);
1247 if (attr == &veth_active_attr) {
1248 if (value && !pool->active) {
1249 if(ibmveth_alloc_buffer_pool(pool)) {
1250 ibmveth_error_printk("unable to alloc pool\n");
1254 adapter->pool_config = 1;
1255 ibmveth_close(netdev);
1256 adapter->pool_config = 0;
1257 if ((rc = ibmveth_open(netdev)))
1259 } else if (!value && pool->active) {
1260 int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1262 /* Make sure there is a buffer pool with buffers that
1263 can hold a packet of the size of the MTU */
1264 for(i = 0; i<IbmVethNumBufferPools; i++) {
1265 if (pool == &adapter->rx_buff_pool[i])
1267 if (!adapter->rx_buff_pool[i].active)
1269 if (mtu < adapter->rx_buff_pool[i].buff_size) {
1271 h_free_logical_lan_buffer(adapter->
1279 ibmveth_error_printk("no active pool >= MTU\n");
1283 } else if (attr == &veth_num_attr) {
1284 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT)
1287 adapter->pool_config = 1;
1288 ibmveth_close(netdev);
1289 adapter->pool_config = 0;
1291 if ((rc = ibmveth_open(netdev)))
1294 } else if (attr == &veth_size_attr) {
1295 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE)
1298 adapter->pool_config = 1;
1299 ibmveth_close(netdev);
1300 adapter->pool_config = 0;
1301 pool->buff_size = value;
1302 if ((rc = ibmveth_open(netdev)))
1307 /* kick the interrupt handler to allocate/deallocate pools */
1308 ibmveth_interrupt(netdev->irq, netdev);
1313 #define ATTR(_name, _mode) \
1314 struct attribute veth_##_name##_attr = { \
1315 .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE \
1318 static ATTR(active, 0644);
1319 static ATTR(num, 0644);
1320 static ATTR(size, 0644);
1322 static struct attribute * veth_pool_attrs[] = {
1329 static struct sysfs_ops veth_pool_ops = {
1330 .show = veth_pool_show,
1331 .store = veth_pool_store,
1334 static struct kobj_type ktype_veth_pool = {
1336 .sysfs_ops = &veth_pool_ops,
1337 .default_attrs = veth_pool_attrs,
1341 static struct vio_device_id ibmveth_device_table[] __devinitdata= {
1342 { "network", "IBM,l-lan"},
1345 MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1347 static struct vio_driver ibmveth_driver = {
1348 .id_table = ibmveth_device_table,
1349 .probe = ibmveth_probe,
1350 .remove = ibmveth_remove,
1352 .name = ibmveth_driver_name,
1353 .owner = THIS_MODULE,
1357 static int __init ibmveth_module_init(void)
1359 ibmveth_printk("%s: %s %s\n", ibmveth_driver_name, ibmveth_driver_string, ibmveth_driver_version);
1361 ibmveth_proc_register_driver();
1363 return vio_register_driver(&ibmveth_driver);
1366 static void __exit ibmveth_module_exit(void)
1368 vio_unregister_driver(&ibmveth_driver);
1369 ibmveth_proc_unregister_driver();
1372 module_init(ibmveth_module_init);
1373 module_exit(ibmveth_module_exit);