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