blob: d96eb72295488fe486136df191a3a0f8c8b6b96d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**************************************************************************/
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 Garzikd7fbeba2006-05-24 01:31:14 -040027/* */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/**************************************************************************/
29/*
30 TODO:
31 - remove frag processing code - no longer needed
32 - add support for sysfs
33 - possibly remove procfs support
34*/
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/vio.h>
54#include <asm/uaccess.h>
55#include <linux/seq_file.h>
56
57#include "ibmveth.h"
58
Anton Blanchard6af37fa2005-11-11 14:02:04 +110059#undef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#define ibmveth_printk(fmt, args...) \
Olof Johanssonf98baff2006-04-12 15:22:29 -050062 printk(KERN_DEBUG "%s: " fmt, __FILE__, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#define ibmveth_error_printk(fmt, args...) \
65 printk(KERN_ERR "(%s:%3.3d ua:%x) ERROR: " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
66
67#ifdef DEBUG
68#define ibmveth_debug_printk_no_adapter(fmt, args...) \
69 printk(KERN_DEBUG "(%s:%3.3d): " fmt, __FILE__, __LINE__ , ## args)
70#define ibmveth_debug_printk(fmt, args...) \
71 printk(KERN_DEBUG "(%s:%3.3d ua:%x): " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
72#define ibmveth_assert(expr) \
73 if(!(expr)) { \
74 printk(KERN_DEBUG "assertion failed (%s:%3.3d ua:%x): %s\n", __FILE__, __LINE__, adapter->vdev->unit_address, #expr); \
75 BUG(); \
76 }
77#else
78#define ibmveth_debug_printk_no_adapter(fmt, args...)
79#define ibmveth_debug_printk(fmt, args...)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -040080#define ibmveth_assert(expr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#endif
82
83static int ibmveth_open(struct net_device *dev);
84static int ibmveth_close(struct net_device *dev);
85static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
86static int ibmveth_poll(struct net_device *dev, int *budget);
87static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *dev);
88static struct net_device_stats *ibmveth_get_stats(struct net_device *dev);
89static void ibmveth_set_multicast_list(struct net_device *dev);
90static int ibmveth_change_mtu(struct net_device *dev, int new_mtu);
91static void ibmveth_proc_register_driver(void);
92static void ibmveth_proc_unregister_driver(void);
93static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter);
94static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter);
David Howells7d12e782006-10-05 14:55:46 +010095static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
Michael Ellerman493a6842007-04-17 13:12:55 +100096static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
Santiago Leon860f2422006-04-25 11:19:59 -050097static struct kobj_type ktype_veth_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99#ifdef CONFIG_PROC_FS
Al Viro66600222005-09-28 22:32:57 +0100100#define IBMVETH_PROC_DIR "net/ibmveth"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static struct proc_dir_entry *ibmveth_proc_dir;
102#endif
103
104static const char ibmveth_driver_name[] = "ibmveth";
105static const char ibmveth_driver_string[] = "IBM i/pSeries Virtual Ethernet Driver";
106#define ibmveth_driver_version "1.03"
107
108MODULE_AUTHOR("Santiago Leon <santil@us.ibm.com>");
109MODULE_DESCRIPTION("IBM i/pSeries Virtual Ethernet Driver");
110MODULE_LICENSE("GPL");
111MODULE_VERSION(ibmveth_driver_version);
112
113/* simple methods of getting data from the current rxq entry */
114static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
115{
116 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].toggle == adapter->rx_queue.toggle);
117}
118
119static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
120{
121 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].valid);
122}
123
124static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
125{
126 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].offset);
127}
128
129static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
130{
131 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
132}
133
134/* setup the initial settings for a buffer pool */
Santiago Leon860f2422006-04-25 11:19:59 -0500135static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool, u32 pool_index, u32 pool_size, u32 buff_size, u32 pool_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 pool->size = pool_size;
138 pool->index = pool_index;
139 pool->buff_size = buff_size;
140 pool->threshold = pool_size / 2;
Santiago Leon860f2422006-04-25 11:19:59 -0500141 pool->active = pool_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144/* allocate and setup an buffer pool - called during open */
145static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
146{
147 int i;
148
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400149 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 if(!pool->free_map) {
152 return -1;
153 }
154
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400155 pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if(!pool->dma_addr) {
157 kfree(pool->free_map);
158 pool->free_map = NULL;
159 return -1;
160 }
161
162 pool->skbuff = kmalloc(sizeof(void*) * pool->size, GFP_KERNEL);
163
164 if(!pool->skbuff) {
165 kfree(pool->dma_addr);
166 pool->dma_addr = NULL;
167
168 kfree(pool->free_map);
169 pool->free_map = NULL;
170 return -1;
171 }
172
173 memset(pool->skbuff, 0, sizeof(void*) * pool->size);
174 memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
175
176 for(i = 0; i < pool->size; ++i) {
177 pool->free_map[i] = i;
178 }
179
180 atomic_set(&pool->available, 0);
181 pool->producer_index = 0;
182 pool->consumer_index = 0;
183
184 return 0;
185}
186
187/* replenish the buffers for a pool. note that we don't need to
188 * skb_reserve these since they are used for incoming...
189 */
190static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
191{
192 u32 i;
193 u32 count = pool->size - atomic_read(&pool->available);
194 u32 buffers_added = 0;
195
196 mb();
197
198 for(i = 0; i < count; ++i) {
199 struct sk_buff *skb;
200 unsigned int free_index, index;
201 u64 correlator;
202 union ibmveth_buf_desc desc;
203 unsigned long lpar_rc;
204 dma_addr_t dma_addr;
205
206 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
207
208 if(!skb) {
209 ibmveth_debug_printk("replenish: unable to allocate skb\n");
210 adapter->replenish_no_mem++;
211 break;
212 }
213
David Gibson047a66d2006-10-21 10:24:13 -0700214 free_index = pool->consumer_index;
215 pool->consumer_index = (pool->consumer_index + 1) % pool->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 index = pool->free_map[free_index];
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 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 Garzikd7fbeba2006-05-24 01:31:14 -0400234 desc.fields.address = dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400237
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200238 if(lpar_rc != H_SUCCESS) {
Santiago Leon82702d32005-10-26 10:47:23 -0600239 pool->free_map[free_index] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 pool->skbuff[index] = NULL;
Santiago Leon751ae212006-10-03 12:24:45 -0500241 if (pool->consumer_index == 0)
242 pool->consumer_index = pool->size - 1;
243 else
244 pool->consumer_index--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 dma_unmap_single(&adapter->vdev->dev,
246 pool->dma_addr[index], pool->buff_size,
247 DMA_FROM_DEVICE);
248 dev_kfree_skb_any(skb);
249 adapter->replenish_add_buff_failure++;
250 break;
251 } else {
252 buffers_added++;
253 adapter->replenish_add_buff_success++;
254 }
255 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 mb();
258 atomic_add(buffers_added, &(pool->available));
259}
260
Santiago Leone2adbcb2005-10-26 10:47:08 -0600261/* replenish routine */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400262static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Santiago Leonb6d35182005-10-26 10:47:01 -0600264 int i;
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 adapter->replenish_task_cycles++;
267
Santiago Leonb6d35182005-10-26 10:47:01 -0600268 for(i = 0; i < IbmVethNumBufferPools; i++)
269 if(adapter->rx_buff_pool[i].active)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400270 ibmveth_replenish_buffer_pool(adapter,
Santiago Leonb6d35182005-10-26 10:47:01 -0600271 &adapter->rx_buff_pool[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276/* empty and free ana buffer pool - also used to do cleanup in error paths */
277static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
278{
279 int i;
280
Jesper Juhlb4558ea2005-10-28 16:53:13 -0400281 kfree(pool->free_map);
282 pool->free_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if(pool->skbuff && pool->dma_addr) {
285 for(i = 0; i < pool->size; ++i) {
286 struct sk_buff *skb = pool->skbuff[i];
287 if(skb) {
288 dma_unmap_single(&adapter->vdev->dev,
289 pool->dma_addr[i],
290 pool->buff_size,
291 DMA_FROM_DEVICE);
292 dev_kfree_skb_any(skb);
293 pool->skbuff[i] = NULL;
294 }
295 }
296 }
297
298 if(pool->dma_addr) {
299 kfree(pool->dma_addr);
300 pool->dma_addr = NULL;
301 }
302
303 if(pool->skbuff) {
304 kfree(pool->skbuff);
305 pool->skbuff = NULL;
306 }
307}
308
309/* remove a buffer from a pool */
310static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter, u64 correlator)
311{
312 unsigned int pool = correlator >> 32;
313 unsigned int index = correlator & 0xffffffffUL;
314 unsigned int free_index;
315 struct sk_buff *skb;
316
317 ibmveth_assert(pool < IbmVethNumBufferPools);
318 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
319
320 skb = adapter->rx_buff_pool[pool].skbuff[index];
321
322 ibmveth_assert(skb != NULL);
323
324 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
325
326 dma_unmap_single(&adapter->vdev->dev,
327 adapter->rx_buff_pool[pool].dma_addr[index],
328 adapter->rx_buff_pool[pool].buff_size,
329 DMA_FROM_DEVICE);
330
David Gibson047a66d2006-10-21 10:24:13 -0700331 free_index = adapter->rx_buff_pool[pool].producer_index;
332 adapter->rx_buff_pool[pool].producer_index
333 = (adapter->rx_buff_pool[pool].producer_index + 1)
334 % adapter->rx_buff_pool[pool].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 adapter->rx_buff_pool[pool].free_map[free_index] = index;
336
337 mb();
338
339 atomic_dec(&(adapter->rx_buff_pool[pool].available));
340}
341
342/* get the current buffer on the rx queue */
343static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
344{
345 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
346 unsigned int pool = correlator >> 32;
347 unsigned int index = correlator & 0xffffffffUL;
348
349 ibmveth_assert(pool < IbmVethNumBufferPools);
350 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
351
352 return adapter->rx_buff_pool[pool].skbuff[index];
353}
354
355/* recycle the current buffer on the rx queue */
356static void ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
357{
358 u32 q_index = adapter->rx_queue.index;
359 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
360 unsigned int pool = correlator >> 32;
361 unsigned int index = correlator & 0xffffffffUL;
362 union ibmveth_buf_desc desc;
363 unsigned long lpar_rc;
364
365 ibmveth_assert(pool < IbmVethNumBufferPools);
366 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
367
Santiago Leonb6d35182005-10-26 10:47:01 -0600368 if(!adapter->rx_buff_pool[pool].active) {
369 ibmveth_rxq_harvest_buffer(adapter);
370 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
371 return;
372 }
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 desc.desc = 0;
375 desc.fields.valid = 1;
376 desc.fields.length = adapter->rx_buff_pool[pool].buff_size;
377 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
378
379 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400380
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200381 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 ibmveth_debug_printk("h_add_logical_lan_buffer failed during recycle rc=%ld", lpar_rc);
383 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
384 }
385
386 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
387 adapter->rx_queue.index = 0;
388 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
389 }
390}
391
Michael Ellerman493a6842007-04-17 13:12:55 +1000392static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
395
396 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
397 adapter->rx_queue.index = 0;
398 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
399 }
400}
401
402static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
403{
Santiago Leonb6d35182005-10-26 10:47:01 -0600404 int i;
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if(adapter->buffer_list_addr != NULL) {
407 if(!dma_mapping_error(adapter->buffer_list_dma)) {
408 dma_unmap_single(&adapter->vdev->dev,
409 adapter->buffer_list_dma, 4096,
410 DMA_BIDIRECTIONAL);
411 adapter->buffer_list_dma = DMA_ERROR_CODE;
412 }
413 free_page((unsigned long)adapter->buffer_list_addr);
414 adapter->buffer_list_addr = NULL;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if(adapter->filter_list_addr != NULL) {
418 if(!dma_mapping_error(adapter->filter_list_dma)) {
419 dma_unmap_single(&adapter->vdev->dev,
420 adapter->filter_list_dma, 4096,
421 DMA_BIDIRECTIONAL);
422 adapter->filter_list_dma = DMA_ERROR_CODE;
423 }
424 free_page((unsigned long)adapter->filter_list_addr);
425 adapter->filter_list_addr = NULL;
426 }
427
428 if(adapter->rx_queue.queue_addr != NULL) {
429 if(!dma_mapping_error(adapter->rx_queue.queue_dma)) {
430 dma_unmap_single(&adapter->vdev->dev,
431 adapter->rx_queue.queue_dma,
432 adapter->rx_queue.queue_len,
433 DMA_BIDIRECTIONAL);
434 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
435 }
436 kfree(adapter->rx_queue.queue_addr);
437 adapter->rx_queue.queue_addr = NULL;
438 }
439
Santiago Leonb6d35182005-10-26 10:47:01 -0600440 for(i = 0; i<IbmVethNumBufferPools; i++)
Santiago Leon860f2422006-04-25 11:19:59 -0500441 if (adapter->rx_buff_pool[i].active)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400442 ibmveth_free_buffer_pool(adapter,
Santiago Leon860f2422006-04-25 11:19:59 -0500443 &adapter->rx_buff_pool[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500446static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
447 union ibmveth_buf_desc rxq_desc, u64 mac_address)
448{
449 int rc, try_again = 1;
450
451 /* After a kexec the adapter will still be open, so our attempt to
452 * open it will fail. So if we get a failure we free the adapter and
453 * try again, but only once. */
454retry:
455 rc = h_register_logical_lan(adapter->vdev->unit_address,
456 adapter->buffer_list_dma, rxq_desc.desc,
457 adapter->filter_list_dma, mac_address);
458
459 if (rc != H_SUCCESS && try_again) {
460 do {
461 rc = h_free_logical_lan(adapter->vdev->unit_address);
462 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
463
464 try_again = 0;
465 goto retry;
466 }
467
468 return rc;
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471static int ibmveth_open(struct net_device *netdev)
472{
473 struct ibmveth_adapter *adapter = netdev->priv;
474 u64 mac_address = 0;
Santiago Leonb6d35182005-10-26 10:47:01 -0600475 int rxq_entries = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 unsigned long lpar_rc;
477 int rc;
478 union ibmveth_buf_desc rxq_desc;
Santiago Leonb6d35182005-10-26 10:47:01 -0600479 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 ibmveth_debug_printk("open starting\n");
482
Santiago Leonb6d35182005-10-26 10:47:01 -0600483 for(i = 0; i<IbmVethNumBufferPools; i++)
484 rxq_entries += adapter->rx_buff_pool[i].size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
487 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if(!adapter->buffer_list_addr || !adapter->filter_list_addr) {
490 ibmveth_error_printk("unable to allocate filter or buffer list pages\n");
491 ibmveth_cleanup(adapter);
492 return -ENOMEM;
493 }
494
495 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) * rxq_entries;
496 adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len, GFP_KERNEL);
497
498 if(!adapter->rx_queue.queue_addr) {
499 ibmveth_error_printk("unable to allocate rx queue pages\n");
500 ibmveth_cleanup(adapter);
501 return -ENOMEM;
502 }
503
504 adapter->buffer_list_dma = dma_map_single(&adapter->vdev->dev,
505 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
506 adapter->filter_list_dma = dma_map_single(&adapter->vdev->dev,
507 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
508 adapter->rx_queue.queue_dma = dma_map_single(&adapter->vdev->dev,
509 adapter->rx_queue.queue_addr,
510 adapter->rx_queue.queue_len, DMA_BIDIRECTIONAL);
511
512 if((dma_mapping_error(adapter->buffer_list_dma) ) ||
513 (dma_mapping_error(adapter->filter_list_dma)) ||
514 (dma_mapping_error(adapter->rx_queue.queue_dma))) {
515 ibmveth_error_printk("unable to map filter or buffer list pages\n");
516 ibmveth_cleanup(adapter);
517 return -ENOMEM;
518 }
519
520 adapter->rx_queue.index = 0;
521 adapter->rx_queue.num_slots = rxq_entries;
522 adapter->rx_queue.toggle = 1;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
525 mac_address = mac_address >> 16;
526
527 rxq_desc.desc = 0;
528 rxq_desc.fields.valid = 1;
529 rxq_desc.fields.length = adapter->rx_queue.queue_len;
530 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
531
532 ibmveth_debug_printk("buffer list @ 0x%p\n", adapter->buffer_list_addr);
533 ibmveth_debug_printk("filter list @ 0x%p\n", adapter->filter_list_addr);
534 ibmveth_debug_printk("receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
535
Santiago Leon4347ef12006-10-03 12:24:34 -0500536 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
537
Michael Ellermanbbedefc2006-10-03 12:24:23 -0500538 lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200540 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 ibmveth_error_printk("h_register_logical_lan failed with %ld\n", lpar_rc);
Stephen Rothwell8168f902005-10-25 16:56:43 +1000542 ibmveth_error_printk("buffer TCE:0x%lx filter TCE:0x%lx rxq desc:0x%lx MAC:0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 adapter->buffer_list_dma,
544 adapter->filter_list_dma,
545 rxq_desc.desc,
546 mac_address);
547 ibmveth_cleanup(adapter);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400548 return -ENONET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550
Santiago Leon860f2422006-04-25 11:19:59 -0500551 for(i = 0; i<IbmVethNumBufferPools; i++) {
552 if(!adapter->rx_buff_pool[i].active)
553 continue;
554 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
555 ibmveth_error_printk("unable to alloc pool\n");
556 adapter->rx_buff_pool[i].active = 0;
557 ibmveth_cleanup(adapter);
558 return -ENOMEM ;
559 }
560 }
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 ibmveth_debug_printk("registering irq 0x%x\n", netdev->irq);
563 if((rc = request_irq(netdev->irq, &ibmveth_interrupt, 0, netdev->name, netdev)) != 0) {
564 ibmveth_error_printk("unable to request irq 0x%x, rc %d\n", netdev->irq, rc);
565 do {
566 rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200567 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 ibmveth_cleanup(adapter);
570 return rc;
571 }
572
Santiago Leone2adbcb2005-10-26 10:47:08 -0600573 ibmveth_debug_printk("initial replenish cycle\n");
David Howells7d12e782006-10-05 14:55:46 +0100574 ibmveth_interrupt(netdev->irq, netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Santiago Leone2adbcb2005-10-26 10:47:08 -0600576 netif_start_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 ibmveth_debug_printk("open complete\n");
579
580 return 0;
581}
582
583static int ibmveth_close(struct net_device *netdev)
584{
585 struct ibmveth_adapter *adapter = netdev->priv;
586 long lpar_rc;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 ibmveth_debug_printk("close starting\n");
589
Santiago Leon860f2422006-04-25 11:19:59 -0500590 if (!adapter->pool_config)
591 netif_stop_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 free_irq(netdev->irq, netdev);
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 do {
596 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200597 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200599 if(lpar_rc != H_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 {
601 ibmveth_error_printk("h_free_logical_lan failed with %lx, continuing with close\n",
602 lpar_rc);
603 }
604
605 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
606
607 ibmveth_cleanup(adapter);
608
609 ibmveth_debug_printk("close complete\n");
610
611 return 0;
612}
613
614static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
615 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
616 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | ADVERTISED_FIBRE);
617 cmd->speed = SPEED_1000;
618 cmd->duplex = DUPLEX_FULL;
619 cmd->port = PORT_FIBRE;
620 cmd->phy_address = 0;
621 cmd->transceiver = XCVR_INTERNAL;
622 cmd->autoneg = AUTONEG_ENABLE;
623 cmd->maxtxpkt = 0;
624 cmd->maxrxpkt = 1;
625 return 0;
626}
627
628static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info) {
629 strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
630 strncpy(info->version, ibmveth_driver_version, sizeof(info->version) - 1);
631}
632
633static u32 netdev_get_link(struct net_device *dev) {
634 return 1;
635}
636
Jeff Garzik7282d492006-09-13 14:30:00 -0400637static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 .get_drvinfo = netdev_get_drvinfo,
639 .get_settings = netdev_get_settings,
640 .get_link = netdev_get_link,
641 .get_sg = ethtool_op_get_sg,
642 .get_tx_csum = ethtool_op_get_tx_csum,
643};
644
645static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
646{
647 return -EOPNOTSUPP;
648}
649
650#define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
651
652static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *netdev)
653{
654 struct ibmveth_adapter *adapter = netdev->priv;
655 union ibmveth_buf_desc desc[IbmVethMaxSendFrags];
656 unsigned long lpar_rc;
657 int nfrags = 0, curfrag;
658 unsigned long correlator;
Santiago Leon60296d92005-10-26 10:47:16 -0600659 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 unsigned int retry_count;
Santiago Leon60296d92005-10-26 10:47:16 -0600661 unsigned int tx_dropped = 0;
662 unsigned int tx_bytes = 0;
663 unsigned int tx_packets = 0;
664 unsigned int tx_send_failed = 0;
665 unsigned int tx_map_failed = 0;
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 if ((skb_shinfo(skb)->nr_frags + 1) > IbmVethMaxSendFrags) {
Santiago Leon60296d92005-10-26 10:47:16 -0600669 tx_dropped++;
670 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
673 memset(&desc, 0, sizeof(desc));
674
675 /* nfrags = number of frags after the initial fragment */
676 nfrags = skb_shinfo(skb)->nr_frags;
677
678 if(nfrags)
679 adapter->tx_multidesc_send++;
680
681 /* map the initial fragment */
682 desc[0].fields.length = nfrags ? skb->len - skb->data_len : skb->len;
683 desc[0].fields.address = dma_map_single(&adapter->vdev->dev, skb->data,
684 desc[0].fields.length, DMA_TO_DEVICE);
685 desc[0].fields.valid = 1;
686
687 if(dma_mapping_error(desc[0].fields.address)) {
688 ibmveth_error_printk("tx: unable to map initial fragment\n");
Santiago Leon60296d92005-10-26 10:47:16 -0600689 tx_map_failed++;
690 tx_dropped++;
691 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693
694 curfrag = nfrags;
695
696 /* map fragments past the initial portion if there are any */
697 while(curfrag--) {
698 skb_frag_t *frag = &skb_shinfo(skb)->frags[curfrag];
699 desc[curfrag+1].fields.address
700 = dma_map_single(&adapter->vdev->dev,
701 page_address(frag->page) + frag->page_offset,
702 frag->size, DMA_TO_DEVICE);
703 desc[curfrag+1].fields.length = frag->size;
704 desc[curfrag+1].fields.valid = 1;
705
706 if(dma_mapping_error(desc[curfrag+1].fields.address)) {
707 ibmveth_error_printk("tx: unable to map fragment %d\n", curfrag);
Santiago Leon60296d92005-10-26 10:47:16 -0600708 tx_map_failed++;
709 tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* Free all the mappings we just created */
711 while(curfrag < nfrags) {
712 dma_unmap_single(&adapter->vdev->dev,
713 desc[curfrag+1].fields.address,
714 desc[curfrag+1].fields.length,
715 DMA_TO_DEVICE);
716 curfrag++;
717 }
Santiago Leon60296d92005-10-26 10:47:16 -0600718 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720 }
721
722 /* send the frame. Arbitrarily set retrycount to 1024 */
723 correlator = 0;
724 retry_count = 1024;
725 do {
726 lpar_rc = h_send_logical_lan(adapter->vdev->unit_address,
727 desc[0].desc,
728 desc[1].desc,
729 desc[2].desc,
730 desc[3].desc,
731 desc[4].desc,
732 desc[5].desc,
Anton Blanchardb9377ff2006-07-19 08:01:28 +1000733 correlator,
734 &correlator);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200735 } while ((lpar_rc == H_BUSY) && (retry_count--));
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400736
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200737 if(lpar_rc != H_SUCCESS && lpar_rc != H_DROPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 int i;
739 ibmveth_error_printk("tx: h_send_logical_lan failed with rc=%ld\n", lpar_rc);
740 for(i = 0; i < 6; i++) {
741 ibmveth_error_printk("tx: desc[%i] valid=%d, len=%d, address=0x%d\n", i,
742 desc[i].fields.valid, desc[i].fields.length, desc[i].fields.address);
743 }
Santiago Leon60296d92005-10-26 10:47:16 -0600744 tx_send_failed++;
745 tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 } else {
Santiago Leon60296d92005-10-26 10:47:16 -0600747 tx_packets++;
748 tx_bytes += skb->len;
Santiago Leon0abe7912005-10-26 10:46:53 -0600749 netdev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751
752 do {
753 dma_unmap_single(&adapter->vdev->dev,
754 desc[nfrags].fields.address,
755 desc[nfrags].fields.length, DMA_TO_DEVICE);
756 } while(--nfrags >= 0);
757
Santiago Leon60296d92005-10-26 10:47:16 -0600758out: spin_lock_irqsave(&adapter->stats_lock, flags);
759 adapter->stats.tx_dropped += tx_dropped;
760 adapter->stats.tx_bytes += tx_bytes;
761 adapter->stats.tx_packets += tx_packets;
762 adapter->tx_send_failed += tx_send_failed;
763 adapter->tx_map_failed += tx_map_failed;
764 spin_unlock_irqrestore(&adapter->stats_lock, flags);
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 dev_kfree_skb(skb);
767 return 0;
768}
769
770static int ibmveth_poll(struct net_device *netdev, int *budget)
771{
772 struct ibmveth_adapter *adapter = netdev->priv;
773 int max_frames_to_process = netdev->quota;
774 int frames_processed = 0;
775 int more_work = 1;
776 unsigned long lpar_rc;
777
778 restart_poll:
779 do {
780 struct net_device *netdev = adapter->netdev;
781
782 if(ibmveth_rxq_pending_buffer(adapter)) {
783 struct sk_buff *skb;
784
785 rmb();
786
787 if(!ibmveth_rxq_buffer_valid(adapter)) {
788 wmb(); /* suggested by larson1 */
789 adapter->rx_invalid_buffer++;
790 ibmveth_debug_printk("recycling invalid buffer\n");
791 ibmveth_rxq_recycle_buffer(adapter);
792 } else {
793 int length = ibmveth_rxq_frame_length(adapter);
794 int offset = ibmveth_rxq_frame_offset(adapter);
795 skb = ibmveth_rxq_get_buffer(adapter);
796
797 ibmveth_rxq_harvest_buffer(adapter);
798
799 skb_reserve(skb, offset);
800 skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 skb->protocol = eth_type_trans(skb, netdev);
802
803 netif_receive_skb(skb); /* send it up */
804
805 adapter->stats.rx_packets++;
806 adapter->stats.rx_bytes += length;
807 frames_processed++;
Santiago Leon0abe7912005-10-26 10:46:53 -0600808 netdev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810 } else {
811 more_work = 0;
812 }
813 } while(more_work && (frames_processed < max_frames_to_process));
814
Santiago Leone2adbcb2005-10-26 10:47:08 -0600815 ibmveth_replenish_task(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if(more_work) {
818 /* more work to do - return that we are not done yet */
819 netdev->quota -= frames_processed;
820 *budget -= frames_processed;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400821 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
824 /* we think we are done - reenable interrupts, then check once more to make sure we are done */
825 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_ENABLE);
826
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200827 ibmveth_assert(lpar_rc == H_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 netif_rx_complete(netdev);
830
831 if(ibmveth_rxq_pending_buffer(adapter) && netif_rx_reschedule(netdev, frames_processed))
832 {
833 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200834 ibmveth_assert(lpar_rc == H_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 more_work = 1;
836 goto restart_poll;
837 }
838
839 netdev->quota -= frames_processed;
840 *budget -= frames_processed;
841
842 /* we really are done */
843 return 0;
844}
845
David Howells7d12e782006-10-05 14:55:46 +0100846static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400847{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 struct net_device *netdev = dev_instance;
849 struct ibmveth_adapter *adapter = netdev->priv;
850 unsigned long lpar_rc;
851
852 if(netif_rx_schedule_prep(netdev)) {
853 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200854 ibmveth_assert(lpar_rc == H_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 __netif_rx_schedule(netdev);
856 }
857 return IRQ_HANDLED;
858}
859
860static struct net_device_stats *ibmveth_get_stats(struct net_device *dev)
861{
862 struct ibmveth_adapter *adapter = dev->priv;
863 return &adapter->stats;
864}
865
866static void ibmveth_set_multicast_list(struct net_device *netdev)
867{
868 struct ibmveth_adapter *adapter = netdev->priv;
869 unsigned long lpar_rc;
870
871 if((netdev->flags & IFF_PROMISC) || (netdev->mc_count > adapter->mcastFilterSize)) {
872 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
873 IbmVethMcastEnableRecv |
874 IbmVethMcastDisableFiltering,
875 0);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200876 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 ibmveth_error_printk("h_multicast_ctrl rc=%ld when entering promisc mode\n", lpar_rc);
878 }
879 } else {
880 struct dev_mc_list *mclist = netdev->mc_list;
881 int i;
882 /* clear the filter table & disable filtering */
883 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
884 IbmVethMcastEnableRecv |
885 IbmVethMcastDisableFiltering |
886 IbmVethMcastClearFilterTable,
887 0);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200888 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 ibmveth_error_printk("h_multicast_ctrl rc=%ld when attempting to clear filter table\n", lpar_rc);
890 }
891 /* add the addresses to the filter table */
892 for(i = 0; i < netdev->mc_count; ++i, mclist = mclist->next) {
893 // add the multicast address to the filter table
894 unsigned long mcast_addr = 0;
895 memcpy(((char *)&mcast_addr)+2, mclist->dmi_addr, 6);
896 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
897 IbmVethMcastAddFilter,
898 mcast_addr);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200899 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 ibmveth_error_printk("h_multicast_ctrl rc=%ld when adding an entry to the filter table\n", lpar_rc);
901 }
902 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /* re-enable filtering */
905 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
906 IbmVethMcastEnableFiltering,
907 0);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200908 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 ibmveth_error_printk("h_multicast_ctrl rc=%ld when enabling filtering\n", lpar_rc);
910 }
911 }
912}
913
914static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
915{
Santiago Leonb6d35182005-10-26 10:47:01 -0600916 struct ibmveth_adapter *adapter = dev->priv;
Santiago Leon860f2422006-04-25 11:19:59 -0500917 int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
Brian Kingce6eea52007-06-08 14:05:17 -0500918 int reinit = 0;
919 int i, rc;
Santiago Leonb6d35182005-10-26 10:47:01 -0600920
Santiago Leon860f2422006-04-25 11:19:59 -0500921 if (new_mtu < IBMVETH_MAX_MTU)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return -EINVAL;
Santiago Leonb6d35182005-10-26 10:47:01 -0600923
Brian Kingce6eea52007-06-08 14:05:17 -0500924 for (i = 0; i < IbmVethNumBufferPools; i++)
925 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size)
926 break;
927
928 if (i == IbmVethNumBufferPools)
929 return -EINVAL;
930
Santiago Leon860f2422006-04-25 11:19:59 -0500931 /* Look for an active buffer pool that can hold the new MTU */
Santiago Leonb6d35182005-10-26 10:47:01 -0600932 for(i = 0; i<IbmVethNumBufferPools; i++) {
Brian Kingce6eea52007-06-08 14:05:17 -0500933 if (!adapter->rx_buff_pool[i].active) {
934 adapter->rx_buff_pool[i].active = 1;
935 reinit = 1;
936 }
937
Santiago Leon860f2422006-04-25 11:19:59 -0500938 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size) {
Brian Kingce6eea52007-06-08 14:05:17 -0500939 if (reinit && netif_running(adapter->netdev)) {
940 adapter->pool_config = 1;
941 ibmveth_close(adapter->netdev);
942 adapter->pool_config = 0;
943 dev->mtu = new_mtu;
944 if ((rc = ibmveth_open(adapter->netdev)))
945 return rc;
946 } else
947 dev->mtu = new_mtu;
Santiago Leon860f2422006-04-25 11:19:59 -0500948 return 0;
Santiago Leonb6d35182005-10-26 10:47:01 -0600949 }
Santiago Leonb6d35182005-10-26 10:47:01 -0600950 }
Santiago Leon860f2422006-04-25 11:19:59 -0500951 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Santiago Leon6b422372006-10-03 12:24:28 -0500954#ifdef CONFIG_NET_POLL_CONTROLLER
955static void ibmveth_poll_controller(struct net_device *dev)
956{
957 ibmveth_replenish_task(dev->priv);
Andrew Morton5f771132006-10-10 14:33:30 -0700958 ibmveth_interrupt(dev->irq, dev);
Santiago Leon6b422372006-10-03 12:24:28 -0500959}
960#endif
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
963{
Santiago Leonb6d35182005-10-26 10:47:01 -0600964 int rc, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 struct net_device *netdev;
Santiago Leonb6d35182005-10-26 10:47:01 -0600966 struct ibmveth_adapter *adapter = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 unsigned char *mac_addr_p;
969 unsigned int *mcastFilterSize_p;
970
971
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400972 ibmveth_debug_printk_no_adapter("entering ibmveth_probe for UA 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 dev->unit_address);
974
Michael Ellerman493a6842007-04-17 13:12:55 +1000975 mac_addr_p = (unsigned char *) vio_get_attribute(dev,
976 VETH_MAC_ADDR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if(!mac_addr_p) {
978 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find VETH_MAC_ADDR "
979 "attribute\n", __FILE__, __LINE__);
980 return 0;
981 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400982
Michael Ellerman493a6842007-04-17 13:12:55 +1000983 mcastFilterSize_p = (unsigned int *) vio_get_attribute(dev,
984 VETH_MCAST_FILTER_SIZE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if(!mcastFilterSize_p) {
986 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find "
987 "VETH_MCAST_FILTER_SIZE attribute\n",
988 __FILE__, __LINE__);
989 return 0;
990 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
993
994 if(!netdev)
995 return -ENOMEM;
996
997 SET_MODULE_OWNER(netdev);
998
999 adapter = netdev->priv;
1000 memset(adapter, 0, sizeof(adapter));
1001 dev->dev.driver_data = netdev;
1002
1003 adapter->vdev = dev;
1004 adapter->netdev = netdev;
1005 adapter->mcastFilterSize= *mcastFilterSize_p;
Santiago Leon860f2422006-04-25 11:19:59 -05001006 adapter->pool_config = 0;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* Some older boxes running PHYP non-natively have an OF that
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001009 returns a 8-byte local-mac-address field (and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 2 bytes have to be ignored) while newer boxes' OF return
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001011 a 6-byte field. Note that IEEE 1275 specifies that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 local-mac-address must be a 6-byte field.
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001013 The RPA doc specifies that the first byte must be 10b, so
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 we'll just look for it to solve this 8 vs. 6 byte field issue */
1015
1016 if ((*mac_addr_p & 0x3) != 0x02)
1017 mac_addr_p += 2;
1018
1019 adapter->mac_addr = 0;
1020 memcpy(&adapter->mac_addr, mac_addr_p, 6);
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 netdev->irq = dev->irq;
1023 netdev->open = ibmveth_open;
1024 netdev->poll = ibmveth_poll;
1025 netdev->weight = 16;
1026 netdev->stop = ibmveth_close;
1027 netdev->hard_start_xmit = ibmveth_start_xmit;
1028 netdev->get_stats = ibmveth_get_stats;
1029 netdev->set_multicast_list = ibmveth_set_multicast_list;
1030 netdev->do_ioctl = ibmveth_ioctl;
1031 netdev->ethtool_ops = &netdev_ethtool_ops;
1032 netdev->change_mtu = ibmveth_change_mtu;
1033 SET_NETDEV_DEV(netdev, &dev->dev);
Santiago Leon6b422372006-10-03 12:24:28 -05001034#ifdef CONFIG_NET_POLL_CONTROLLER
1035 netdev->poll_controller = ibmveth_poll_controller;
1036#endif
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001037 netdev->features |= NETIF_F_LLTX;
Santiago Leon60296d92005-10-26 10:47:16 -06001038 spin_lock_init(&adapter->stats_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 memcpy(&netdev->dev_addr, &adapter->mac_addr, netdev->addr_len);
1041
Santiago Leon860f2422006-04-25 11:19:59 -05001042 for(i = 0; i<IbmVethNumBufferPools; i++) {
1043 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001044 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1045 pool_count[i], pool_size[i],
Santiago Leon860f2422006-04-25 11:19:59 -05001046 pool_active[i]);
1047 kobj->parent = &dev->dev.kobj;
1048 sprintf(kobj->name, "pool%d", i);
1049 kobj->ktype = &ktype_veth_pool;
1050 kobject_register(kobj);
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 ibmveth_debug_printk("adapter @ 0x%p\n", adapter);
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 adapter->buffer_list_dma = DMA_ERROR_CODE;
1056 adapter->filter_list_dma = DMA_ERROR_CODE;
1057 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ibmveth_debug_printk("registering netdev...\n");
1060
1061 rc = register_netdev(netdev);
1062
1063 if(rc) {
1064 ibmveth_debug_printk("failed to register netdev rc=%d\n", rc);
1065 free_netdev(netdev);
1066 return rc;
1067 }
1068
1069 ibmveth_debug_printk("registered\n");
1070
1071 ibmveth_proc_register_adapter(adapter);
1072
1073 return 0;
1074}
1075
1076static int __devexit ibmveth_remove(struct vio_dev *dev)
1077{
1078 struct net_device *netdev = dev->dev.driver_data;
1079 struct ibmveth_adapter *adapter = netdev->priv;
Santiago Leon860f2422006-04-25 11:19:59 -05001080 int i;
1081
1082 for(i = 0; i<IbmVethNumBufferPools; i++)
1083 kobject_unregister(&adapter->rx_buff_pool[i].kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 unregister_netdev(netdev);
1086
1087 ibmveth_proc_unregister_adapter(adapter);
1088
1089 free_netdev(netdev);
1090 return 0;
1091}
1092
1093#ifdef CONFIG_PROC_FS
1094static void ibmveth_proc_register_driver(void)
1095{
Al Viro66600222005-09-28 22:32:57 +01001096 ibmveth_proc_dir = proc_mkdir(IBMVETH_PROC_DIR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (ibmveth_proc_dir) {
1098 SET_MODULE_OWNER(ibmveth_proc_dir);
1099 }
1100}
1101
1102static void ibmveth_proc_unregister_driver(void)
1103{
Al Viro66600222005-09-28 22:32:57 +01001104 remove_proc_entry(IBMVETH_PROC_DIR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001107static void *ibmveth_seq_start(struct seq_file *seq, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 if (*pos == 0) {
1110 return (void *)1;
1111 } else {
1112 return NULL;
1113 }
1114}
1115
1116static void *ibmveth_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1117{
1118 ++*pos;
1119 return NULL;
1120}
1121
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001122static void ibmveth_seq_stop(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124}
1125
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001126static int ibmveth_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 struct ibmveth_adapter *adapter = seq->private;
1129 char *current_mac = ((char*) &adapter->netdev->dev_addr);
1130 char *firmware_mac = ((char*) &adapter->mac_addr) ;
1131
1132 seq_printf(seq, "%s %s\n\n", ibmveth_driver_string, ibmveth_driver_version);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 seq_printf(seq, "Unit Address: 0x%x\n", adapter->vdev->unit_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 seq_printf(seq, "Current MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
1136 current_mac[0], current_mac[1], current_mac[2],
1137 current_mac[3], current_mac[4], current_mac[5]);
1138 seq_printf(seq, "Firmware MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
1139 firmware_mac[0], firmware_mac[1], firmware_mac[2],
1140 firmware_mac[3], firmware_mac[4], firmware_mac[5]);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 seq_printf(seq, "\nAdapter Statistics:\n");
1143 seq_printf(seq, " TX: skbuffs linearized: %ld\n", adapter->tx_linearized);
1144 seq_printf(seq, " multi-descriptor sends: %ld\n", adapter->tx_multidesc_send);
1145 seq_printf(seq, " skb_linearize failures: %ld\n", adapter->tx_linearize_failed);
1146 seq_printf(seq, " vio_map_single failres: %ld\n", adapter->tx_map_failed);
1147 seq_printf(seq, " send failures: %ld\n", adapter->tx_send_failed);
1148 seq_printf(seq, " RX: replenish task cycles: %ld\n", adapter->replenish_task_cycles);
1149 seq_printf(seq, " alloc_skb_failures: %ld\n", adapter->replenish_no_mem);
1150 seq_printf(seq, " add buffer failures: %ld\n", adapter->replenish_add_buff_failure);
1151 seq_printf(seq, " invalid buffers: %ld\n", adapter->rx_invalid_buffer);
1152 seq_printf(seq, " no buffers: %ld\n", adapter->rx_no_buffer);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return 0;
1155}
1156static struct seq_operations ibmveth_seq_ops = {
1157 .start = ibmveth_seq_start,
1158 .next = ibmveth_seq_next,
1159 .stop = ibmveth_seq_stop,
1160 .show = ibmveth_seq_show,
1161};
1162
1163static int ibmveth_proc_open(struct inode *inode, struct file *file)
1164{
1165 struct seq_file *seq;
1166 struct proc_dir_entry *proc;
1167 int rc;
1168
1169 rc = seq_open(file, &ibmveth_seq_ops);
1170 if (!rc) {
1171 /* recover the pointer buried in proc_dir_entry data */
1172 seq = file->private_data;
1173 proc = PDE(inode);
1174 seq->private = proc->data;
1175 }
1176 return rc;
1177}
1178
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001179static const struct file_operations ibmveth_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 .owner = THIS_MODULE,
1181 .open = ibmveth_proc_open,
1182 .read = seq_read,
1183 .llseek = seq_lseek,
1184 .release = seq_release,
1185};
1186
1187static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1188{
1189 struct proc_dir_entry *entry;
1190 if (ibmveth_proc_dir) {
Santiago Leon03a85d02006-10-03 12:24:39 -05001191 char u_addr[10];
1192 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1193 entry = create_proc_entry(u_addr, S_IFREG, ibmveth_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (!entry) {
1195 ibmveth_error_printk("Cannot create adapter proc entry");
1196 } else {
1197 entry->data = (void *) adapter;
1198 entry->proc_fops = &ibmveth_proc_fops;
1199 SET_MODULE_OWNER(entry);
1200 }
1201 }
1202 return;
1203}
1204
1205static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1206{
1207 if (ibmveth_proc_dir) {
Santiago Leon03a85d02006-10-03 12:24:39 -05001208 char u_addr[10];
1209 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1210 remove_proc_entry(u_addr, ibmveth_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212}
1213
1214#else /* CONFIG_PROC_FS */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001215static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217}
1218
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001219static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221}
1222static void ibmveth_proc_register_driver(void)
1223{
1224}
1225
1226static void ibmveth_proc_unregister_driver(void)
1227{
1228}
1229#endif /* CONFIG_PROC_FS */
1230
Santiago Leon860f2422006-04-25 11:19:59 -05001231static struct attribute veth_active_attr;
1232static struct attribute veth_num_attr;
1233static struct attribute veth_size_attr;
1234
1235static ssize_t veth_pool_show(struct kobject * kobj,
1236 struct attribute * attr, char * buf)
1237{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001238 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001239 struct ibmveth_buff_pool,
1240 kobj);
1241
1242 if (attr == &veth_active_attr)
1243 return sprintf(buf, "%d\n", pool->active);
1244 else if (attr == &veth_num_attr)
1245 return sprintf(buf, "%d\n", pool->size);
1246 else if (attr == &veth_size_attr)
1247 return sprintf(buf, "%d\n", pool->buff_size);
1248 return 0;
1249}
1250
1251static ssize_t veth_pool_store(struct kobject * kobj, struct attribute * attr,
1252const char * buf, size_t count)
1253{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001254 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001255 struct ibmveth_buff_pool,
1256 kobj);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001257 struct net_device *netdev =
Santiago Leon860f2422006-04-25 11:19:59 -05001258 container_of(kobj->parent, struct device, kobj)->driver_data;
1259 struct ibmveth_adapter *adapter = netdev->priv;
1260 long value = simple_strtol(buf, NULL, 10);
1261 long rc;
1262
1263 if (attr == &veth_active_attr) {
1264 if (value && !pool->active) {
Brian King4aa9c932007-06-08 14:05:16 -05001265 if (netif_running(netdev)) {
1266 if(ibmveth_alloc_buffer_pool(pool)) {
1267 ibmveth_error_printk("unable to alloc pool\n");
1268 return -ENOMEM;
1269 }
1270 pool->active = 1;
1271 adapter->pool_config = 1;
1272 ibmveth_close(netdev);
1273 adapter->pool_config = 0;
1274 if ((rc = ibmveth_open(netdev)))
1275 return rc;
1276 } else
1277 pool->active = 1;
Santiago Leon860f2422006-04-25 11:19:59 -05001278 } else if (!value && pool->active) {
1279 int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1280 int i;
1281 /* Make sure there is a buffer pool with buffers that
1282 can hold a packet of the size of the MTU */
1283 for(i = 0; i<IbmVethNumBufferPools; i++) {
1284 if (pool == &adapter->rx_buff_pool[i])
1285 continue;
1286 if (!adapter->rx_buff_pool[i].active)
1287 continue;
1288 if (mtu < adapter->rx_buff_pool[i].buff_size) {
1289 pool->active = 0;
1290 h_free_logical_lan_buffer(adapter->
1291 vdev->
1292 unit_address,
1293 pool->
1294 buff_size);
1295 }
1296 }
1297 if (pool->active) {
1298 ibmveth_error_printk("no active pool >= MTU\n");
1299 return -EPERM;
1300 }
1301 }
1302 } else if (attr == &veth_num_attr) {
1303 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT)
1304 return -EINVAL;
1305 else {
Brian King4aa9c932007-06-08 14:05:16 -05001306 if (netif_running(netdev)) {
1307 adapter->pool_config = 1;
1308 ibmveth_close(netdev);
1309 adapter->pool_config = 0;
1310 pool->size = value;
1311 if ((rc = ibmveth_open(netdev)))
1312 return rc;
1313 } else
1314 pool->size = value;
Santiago Leon860f2422006-04-25 11:19:59 -05001315 }
1316 } else if (attr == &veth_size_attr) {
1317 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE)
1318 return -EINVAL;
1319 else {
Brian King4aa9c932007-06-08 14:05:16 -05001320 if (netif_running(netdev)) {
1321 adapter->pool_config = 1;
1322 ibmveth_close(netdev);
1323 adapter->pool_config = 0;
1324 pool->buff_size = value;
1325 if ((rc = ibmveth_open(netdev)))
1326 return rc;
1327 } else
1328 pool->buff_size = value;
Santiago Leon860f2422006-04-25 11:19:59 -05001329 }
1330 }
1331
1332 /* kick the interrupt handler to allocate/deallocate pools */
David Howells7d12e782006-10-05 14:55:46 +01001333 ibmveth_interrupt(netdev->irq, netdev);
Santiago Leon860f2422006-04-25 11:19:59 -05001334 return count;
1335}
1336
1337
1338#define ATTR(_name, _mode) \
1339 struct attribute veth_##_name##_attr = { \
Tejun Heo7b595752007-06-14 03:45:17 +09001340 .name = __stringify(_name), .mode = _mode, \
Santiago Leon860f2422006-04-25 11:19:59 -05001341 };
1342
1343static ATTR(active, 0644);
1344static ATTR(num, 0644);
1345static ATTR(size, 0644);
1346
1347static struct attribute * veth_pool_attrs[] = {
1348 &veth_active_attr,
1349 &veth_num_attr,
1350 &veth_size_attr,
1351 NULL,
1352};
1353
1354static struct sysfs_ops veth_pool_ops = {
1355 .show = veth_pool_show,
1356 .store = veth_pool_store,
1357};
1358
1359static struct kobj_type ktype_veth_pool = {
1360 .release = NULL,
1361 .sysfs_ops = &veth_pool_ops,
1362 .default_attrs = veth_pool_attrs,
1363};
1364
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366static struct vio_device_id ibmveth_device_table[] __devinitdata= {
1367 { "network", "IBM,l-lan"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +10001368 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1371
1372static struct vio_driver ibmveth_driver = {
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001373 .id_table = ibmveth_device_table,
1374 .probe = ibmveth_probe,
1375 .remove = ibmveth_remove,
1376 .driver = {
1377 .name = ibmveth_driver_name,
Stephen Rothwell915124d2005-10-24 15:12:22 +10001378 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380};
1381
1382static int __init ibmveth_module_init(void)
1383{
1384 ibmveth_printk("%s: %s %s\n", ibmveth_driver_name, ibmveth_driver_string, ibmveth_driver_version);
1385
1386 ibmveth_proc_register_driver();
1387
1388 return vio_register_driver(&ibmveth_driver);
1389}
1390
1391static void __exit ibmveth_module_exit(void)
1392{
1393 vio_unregister_driver(&ibmveth_driver);
1394 ibmveth_proc_unregister_driver();
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001395}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397module_init(ibmveth_module_init);
1398module_exit(ibmveth_module_exit);