blob: 767203d35bc2eb7b3088ccdf69053204518000b1 [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>
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 Blanchard6af37fa2005-11-11 14:02:04 +110060#undef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#define ibmveth_printk(fmt, args...) \
Olof Johanssonf98baff2006-04-12 15:22:29 -050063 printk(KERN_DEBUG "%s: " fmt, __FILE__, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
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 Garzikd7fbeba2006-05-24 01:31:14 -040081#define ibmveth_assert(expr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif
83
84static int ibmveth_open(struct net_device *dev);
85static int ibmveth_close(struct net_device *dev);
86static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
87static int ibmveth_poll(struct net_device *dev, int *budget);
88static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *dev);
89static struct net_device_stats *ibmveth_get_stats(struct net_device *dev);
90static void ibmveth_set_multicast_list(struct net_device *dev);
91static int ibmveth_change_mtu(struct net_device *dev, int new_mtu);
92static void ibmveth_proc_register_driver(void);
93static void ibmveth_proc_unregister_driver(void);
94static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter);
95static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter);
96static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
Santiago Leonb6d35182005-10-26 10:47:01 -060097static inline void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
Santiago Leon860f2422006-04-25 11:19:59 -050098static struct kobj_type ktype_veth_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#ifdef CONFIG_PROC_FS
Al Viro66600222005-09-28 22:32:57 +0100101#define IBMVETH_PROC_DIR "net/ibmveth"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static struct proc_dir_entry *ibmveth_proc_dir;
103#endif
104
105static const char ibmveth_driver_name[] = "ibmveth";
106static const char ibmveth_driver_string[] = "IBM i/pSeries Virtual Ethernet Driver";
107#define ibmveth_driver_version "1.03"
108
109MODULE_AUTHOR("Santiago Leon <santil@us.ibm.com>");
110MODULE_DESCRIPTION("IBM i/pSeries Virtual Ethernet Driver");
111MODULE_LICENSE("GPL");
112MODULE_VERSION(ibmveth_driver_version);
113
114/* simple methods of getting data from the current rxq entry */
115static 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
120static 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
125static 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
130static 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 Leon860f2422006-04-25 11:19:59 -0500136static 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 -0700137{
138 pool->size = pool_size;
139 pool->index = pool_index;
140 pool->buff_size = buff_size;
141 pool->threshold = pool_size / 2;
Santiago Leon860f2422006-04-25 11:19:59 -0500142 pool->active = pool_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145/* allocate and setup an buffer pool - called during open */
146static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
147{
148 int i;
149
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400150 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 if(!pool->free_map) {
153 return -1;
154 }
155
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400156 pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 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 */
191static 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 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;
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 Garzikd7fbeba2006-05-24 01:31:14 -0400253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 mb();
255 atomic_add(buffers_added, &(pool->available));
256}
257
Santiago Leone2adbcb2005-10-26 10:47:08 -0600258/* replenish routine */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400259static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Santiago Leonb6d35182005-10-26 10:47:01 -0600261 int i;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 adapter->replenish_task_cycles++;
264
Santiago Leonb6d35182005-10-26 10:47:01 -0600265 for(i = 0; i < IbmVethNumBufferPools; i++)
266 if(adapter->rx_buff_pool[i].active)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400267 ibmveth_replenish_buffer_pool(adapter,
Santiago Leonb6d35182005-10-26 10:47:01 -0600268 &adapter->rx_buff_pool[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/* empty and free ana buffer pool - also used to do cleanup in error paths */
274static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
275{
276 int i;
277
Jesper Juhlb4558ea2005-10-28 16:53:13 -0400278 kfree(pool->free_map);
279 pool->free_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
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 */
307static 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 */
337static 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 */
350static 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 Leonb6d35182005-10-26 10:47:01 -0600362 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 Torvalds1da177e2005-04-16 15:20:36 -0700368 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 Garzikd7fbeba2006-05-24 01:31:14 -0400374
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200375 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 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
386static 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
396static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
397{
Santiago Leonb6d35182005-10-26 10:47:01 -0600398 int i;
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 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 Garzikd7fbeba2006-05-24 01:31:14 -0400409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
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 Leonb6d35182005-10-26 10:47:01 -0600434 for(i = 0; i<IbmVethNumBufferPools; i++)
Santiago Leon860f2422006-04-25 11:19:59 -0500435 if (adapter->rx_buff_pool[i].active)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400436 ibmveth_free_buffer_pool(adapter,
Santiago Leon860f2422006-04-25 11:19:59 -0500437 &adapter->rx_buff_pool[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440static int ibmveth_open(struct net_device *netdev)
441{
442 struct ibmveth_adapter *adapter = netdev->priv;
443 u64 mac_address = 0;
Santiago Leonb6d35182005-10-26 10:47:01 -0600444 int rxq_entries = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 unsigned long lpar_rc;
446 int rc;
447 union ibmveth_buf_desc rxq_desc;
Santiago Leonb6d35182005-10-26 10:47:01 -0600448 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 ibmveth_debug_printk("open starting\n");
451
Santiago Leonb6d35182005-10-26 10:47:01 -0600452 for(i = 0; i<IbmVethNumBufferPools; i++)
453 rxq_entries += adapter->rx_buff_pool[i].size;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
456 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if(!adapter->buffer_list_addr || !adapter->filter_list_addr) {
459 ibmveth_error_printk("unable to allocate filter or buffer list pages\n");
460 ibmveth_cleanup(adapter);
461 return -ENOMEM;
462 }
463
464 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) * rxq_entries;
465 adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len, GFP_KERNEL);
466
467 if(!adapter->rx_queue.queue_addr) {
468 ibmveth_error_printk("unable to allocate rx queue pages\n");
469 ibmveth_cleanup(adapter);
470 return -ENOMEM;
471 }
472
473 adapter->buffer_list_dma = dma_map_single(&adapter->vdev->dev,
474 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
475 adapter->filter_list_dma = dma_map_single(&adapter->vdev->dev,
476 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
477 adapter->rx_queue.queue_dma = dma_map_single(&adapter->vdev->dev,
478 adapter->rx_queue.queue_addr,
479 adapter->rx_queue.queue_len, DMA_BIDIRECTIONAL);
480
481 if((dma_mapping_error(adapter->buffer_list_dma) ) ||
482 (dma_mapping_error(adapter->filter_list_dma)) ||
483 (dma_mapping_error(adapter->rx_queue.queue_dma))) {
484 ibmveth_error_printk("unable to map filter or buffer list pages\n");
485 ibmveth_cleanup(adapter);
486 return -ENOMEM;
487 }
488
489 adapter->rx_queue.index = 0;
490 adapter->rx_queue.num_slots = rxq_entries;
491 adapter->rx_queue.toggle = 1;
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
494 mac_address = mac_address >> 16;
495
496 rxq_desc.desc = 0;
497 rxq_desc.fields.valid = 1;
498 rxq_desc.fields.length = adapter->rx_queue.queue_len;
499 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
500
501 ibmveth_debug_printk("buffer list @ 0x%p\n", adapter->buffer_list_addr);
502 ibmveth_debug_printk("filter list @ 0x%p\n", adapter->filter_list_addr);
503 ibmveth_debug_printk("receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
504
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 lpar_rc = h_register_logical_lan(adapter->vdev->unit_address,
507 adapter->buffer_list_dma,
508 rxq_desc.desc,
509 adapter->filter_list_dma,
510 mac_address);
511
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200512 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 ibmveth_error_printk("h_register_logical_lan failed with %ld\n", lpar_rc);
Stephen Rothwell8168f902005-10-25 16:56:43 +1000514 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 -0700515 adapter->buffer_list_dma,
516 adapter->filter_list_dma,
517 rxq_desc.desc,
518 mac_address);
519 ibmveth_cleanup(adapter);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400520 return -ENONET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522
Santiago Leon860f2422006-04-25 11:19:59 -0500523 for(i = 0; i<IbmVethNumBufferPools; i++) {
524 if(!adapter->rx_buff_pool[i].active)
525 continue;
526 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
527 ibmveth_error_printk("unable to alloc pool\n");
528 adapter->rx_buff_pool[i].active = 0;
529 ibmveth_cleanup(adapter);
530 return -ENOMEM ;
531 }
532 }
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 ibmveth_debug_printk("registering irq 0x%x\n", netdev->irq);
535 if((rc = request_irq(netdev->irq, &ibmveth_interrupt, 0, netdev->name, netdev)) != 0) {
536 ibmveth_error_printk("unable to request irq 0x%x, rc %d\n", netdev->irq, rc);
537 do {
538 rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200539 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 ibmveth_cleanup(adapter);
542 return rc;
543 }
544
Santiago Leone2adbcb2005-10-26 10:47:08 -0600545 ibmveth_debug_printk("initial replenish cycle\n");
Santiago Leon6c2af712005-11-01 14:15:09 -0500546 ibmveth_interrupt(netdev->irq, netdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Santiago Leone2adbcb2005-10-26 10:47:08 -0600548 netif_start_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 ibmveth_debug_printk("open complete\n");
551
552 return 0;
553}
554
555static int ibmveth_close(struct net_device *netdev)
556{
557 struct ibmveth_adapter *adapter = netdev->priv;
558 long lpar_rc;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 ibmveth_debug_printk("close starting\n");
561
Santiago Leon860f2422006-04-25 11:19:59 -0500562 if (!adapter->pool_config)
563 netif_stop_queue(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 free_irq(netdev->irq, netdev);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 do {
568 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200569 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200571 if(lpar_rc != H_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 {
573 ibmveth_error_printk("h_free_logical_lan failed with %lx, continuing with close\n",
574 lpar_rc);
575 }
576
577 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
578
579 ibmveth_cleanup(adapter);
580
581 ibmveth_debug_printk("close complete\n");
582
583 return 0;
584}
585
586static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
587 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
588 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | ADVERTISED_FIBRE);
589 cmd->speed = SPEED_1000;
590 cmd->duplex = DUPLEX_FULL;
591 cmd->port = PORT_FIBRE;
592 cmd->phy_address = 0;
593 cmd->transceiver = XCVR_INTERNAL;
594 cmd->autoneg = AUTONEG_ENABLE;
595 cmd->maxtxpkt = 0;
596 cmd->maxrxpkt = 1;
597 return 0;
598}
599
600static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info) {
601 strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
602 strncpy(info->version, ibmveth_driver_version, sizeof(info->version) - 1);
603}
604
605static u32 netdev_get_link(struct net_device *dev) {
606 return 1;
607}
608
Jeff Garzik7282d492006-09-13 14:30:00 -0400609static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 .get_drvinfo = netdev_get_drvinfo,
611 .get_settings = netdev_get_settings,
612 .get_link = netdev_get_link,
613 .get_sg = ethtool_op_get_sg,
614 .get_tx_csum = ethtool_op_get_tx_csum,
615};
616
617static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
618{
619 return -EOPNOTSUPP;
620}
621
622#define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
623
624static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *netdev)
625{
626 struct ibmveth_adapter *adapter = netdev->priv;
627 union ibmveth_buf_desc desc[IbmVethMaxSendFrags];
628 unsigned long lpar_rc;
629 int nfrags = 0, curfrag;
630 unsigned long correlator;
Santiago Leon60296d92005-10-26 10:47:16 -0600631 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 unsigned int retry_count;
Santiago Leon60296d92005-10-26 10:47:16 -0600633 unsigned int tx_dropped = 0;
634 unsigned int tx_bytes = 0;
635 unsigned int tx_packets = 0;
636 unsigned int tx_send_failed = 0;
637 unsigned int tx_map_failed = 0;
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 if ((skb_shinfo(skb)->nr_frags + 1) > IbmVethMaxSendFrags) {
Santiago Leon60296d92005-10-26 10:47:16 -0600641 tx_dropped++;
642 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
645 memset(&desc, 0, sizeof(desc));
646
647 /* nfrags = number of frags after the initial fragment */
648 nfrags = skb_shinfo(skb)->nr_frags;
649
650 if(nfrags)
651 adapter->tx_multidesc_send++;
652
653 /* map the initial fragment */
654 desc[0].fields.length = nfrags ? skb->len - skb->data_len : skb->len;
655 desc[0].fields.address = dma_map_single(&adapter->vdev->dev, skb->data,
656 desc[0].fields.length, DMA_TO_DEVICE);
657 desc[0].fields.valid = 1;
658
659 if(dma_mapping_error(desc[0].fields.address)) {
660 ibmveth_error_printk("tx: unable to map initial fragment\n");
Santiago Leon60296d92005-10-26 10:47:16 -0600661 tx_map_failed++;
662 tx_dropped++;
663 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
666 curfrag = nfrags;
667
668 /* map fragments past the initial portion if there are any */
669 while(curfrag--) {
670 skb_frag_t *frag = &skb_shinfo(skb)->frags[curfrag];
671 desc[curfrag+1].fields.address
672 = dma_map_single(&adapter->vdev->dev,
673 page_address(frag->page) + frag->page_offset,
674 frag->size, DMA_TO_DEVICE);
675 desc[curfrag+1].fields.length = frag->size;
676 desc[curfrag+1].fields.valid = 1;
677
678 if(dma_mapping_error(desc[curfrag+1].fields.address)) {
679 ibmveth_error_printk("tx: unable to map fragment %d\n", curfrag);
Santiago Leon60296d92005-10-26 10:47:16 -0600680 tx_map_failed++;
681 tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* Free all the mappings we just created */
683 while(curfrag < nfrags) {
684 dma_unmap_single(&adapter->vdev->dev,
685 desc[curfrag+1].fields.address,
686 desc[curfrag+1].fields.length,
687 DMA_TO_DEVICE);
688 curfrag++;
689 }
Santiago Leon60296d92005-10-26 10:47:16 -0600690 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692 }
693
694 /* send the frame. Arbitrarily set retrycount to 1024 */
695 correlator = 0;
696 retry_count = 1024;
697 do {
698 lpar_rc = h_send_logical_lan(adapter->vdev->unit_address,
699 desc[0].desc,
700 desc[1].desc,
701 desc[2].desc,
702 desc[3].desc,
703 desc[4].desc,
704 desc[5].desc,
Anton Blanchardb9377ff2006-07-19 08:01:28 +1000705 correlator,
706 &correlator);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200707 } while ((lpar_rc == H_BUSY) && (retry_count--));
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400708
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200709 if(lpar_rc != H_SUCCESS && lpar_rc != H_DROPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 int i;
711 ibmveth_error_printk("tx: h_send_logical_lan failed with rc=%ld\n", lpar_rc);
712 for(i = 0; i < 6; i++) {
713 ibmveth_error_printk("tx: desc[%i] valid=%d, len=%d, address=0x%d\n", i,
714 desc[i].fields.valid, desc[i].fields.length, desc[i].fields.address);
715 }
Santiago Leon60296d92005-10-26 10:47:16 -0600716 tx_send_failed++;
717 tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 } else {
Santiago Leon60296d92005-10-26 10:47:16 -0600719 tx_packets++;
720 tx_bytes += skb->len;
Santiago Leon0abe7912005-10-26 10:46:53 -0600721 netdev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723
724 do {
725 dma_unmap_single(&adapter->vdev->dev,
726 desc[nfrags].fields.address,
727 desc[nfrags].fields.length, DMA_TO_DEVICE);
728 } while(--nfrags >= 0);
729
Santiago Leon60296d92005-10-26 10:47:16 -0600730out: spin_lock_irqsave(&adapter->stats_lock, flags);
731 adapter->stats.tx_dropped += tx_dropped;
732 adapter->stats.tx_bytes += tx_bytes;
733 adapter->stats.tx_packets += tx_packets;
734 adapter->tx_send_failed += tx_send_failed;
735 adapter->tx_map_failed += tx_map_failed;
736 spin_unlock_irqrestore(&adapter->stats_lock, flags);
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 dev_kfree_skb(skb);
739 return 0;
740}
741
742static int ibmveth_poll(struct net_device *netdev, int *budget)
743{
744 struct ibmveth_adapter *adapter = netdev->priv;
745 int max_frames_to_process = netdev->quota;
746 int frames_processed = 0;
747 int more_work = 1;
748 unsigned long lpar_rc;
749
750 restart_poll:
751 do {
752 struct net_device *netdev = adapter->netdev;
753
754 if(ibmveth_rxq_pending_buffer(adapter)) {
755 struct sk_buff *skb;
756
757 rmb();
758
759 if(!ibmveth_rxq_buffer_valid(adapter)) {
760 wmb(); /* suggested by larson1 */
761 adapter->rx_invalid_buffer++;
762 ibmveth_debug_printk("recycling invalid buffer\n");
763 ibmveth_rxq_recycle_buffer(adapter);
764 } else {
765 int length = ibmveth_rxq_frame_length(adapter);
766 int offset = ibmveth_rxq_frame_offset(adapter);
767 skb = ibmveth_rxq_get_buffer(adapter);
768
769 ibmveth_rxq_harvest_buffer(adapter);
770
771 skb_reserve(skb, offset);
772 skb_put(skb, length);
773 skb->dev = netdev;
774 skb->protocol = eth_type_trans(skb, netdev);
775
776 netif_receive_skb(skb); /* send it up */
777
778 adapter->stats.rx_packets++;
779 adapter->stats.rx_bytes += length;
780 frames_processed++;
Santiago Leon0abe7912005-10-26 10:46:53 -0600781 netdev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783 } else {
784 more_work = 0;
785 }
786 } while(more_work && (frames_processed < max_frames_to_process));
787
Santiago Leone2adbcb2005-10-26 10:47:08 -0600788 ibmveth_replenish_task(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if(more_work) {
791 /* more work to do - return that we are not done yet */
792 netdev->quota -= frames_processed;
793 *budget -= frames_processed;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400794 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
797 /* we think we are done - reenable interrupts, then check once more to make sure we are done */
798 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_ENABLE);
799
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200800 ibmveth_assert(lpar_rc == H_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 netif_rx_complete(netdev);
803
804 if(ibmveth_rxq_pending_buffer(adapter) && netif_rx_reschedule(netdev, frames_processed))
805 {
806 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200807 ibmveth_assert(lpar_rc == H_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 more_work = 1;
809 goto restart_poll;
810 }
811
812 netdev->quota -= frames_processed;
813 *budget -= frames_processed;
814
815 /* we really are done */
816 return 0;
817}
818
819static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400820{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 struct net_device *netdev = dev_instance;
822 struct ibmveth_adapter *adapter = netdev->priv;
823 unsigned long lpar_rc;
824
825 if(netif_rx_schedule_prep(netdev)) {
826 lpar_rc = h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200827 ibmveth_assert(lpar_rc == H_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 __netif_rx_schedule(netdev);
829 }
830 return IRQ_HANDLED;
831}
832
833static struct net_device_stats *ibmveth_get_stats(struct net_device *dev)
834{
835 struct ibmveth_adapter *adapter = dev->priv;
836 return &adapter->stats;
837}
838
839static void ibmveth_set_multicast_list(struct net_device *netdev)
840{
841 struct ibmveth_adapter *adapter = netdev->priv;
842 unsigned long lpar_rc;
843
844 if((netdev->flags & IFF_PROMISC) || (netdev->mc_count > adapter->mcastFilterSize)) {
845 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
846 IbmVethMcastEnableRecv |
847 IbmVethMcastDisableFiltering,
848 0);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200849 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 ibmveth_error_printk("h_multicast_ctrl rc=%ld when entering promisc mode\n", lpar_rc);
851 }
852 } else {
853 struct dev_mc_list *mclist = netdev->mc_list;
854 int i;
855 /* clear the filter table & disable filtering */
856 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
857 IbmVethMcastEnableRecv |
858 IbmVethMcastDisableFiltering |
859 IbmVethMcastClearFilterTable,
860 0);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200861 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 ibmveth_error_printk("h_multicast_ctrl rc=%ld when attempting to clear filter table\n", lpar_rc);
863 }
864 /* add the addresses to the filter table */
865 for(i = 0; i < netdev->mc_count; ++i, mclist = mclist->next) {
866 // add the multicast address to the filter table
867 unsigned long mcast_addr = 0;
868 memcpy(((char *)&mcast_addr)+2, mclist->dmi_addr, 6);
869 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
870 IbmVethMcastAddFilter,
871 mcast_addr);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200872 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 ibmveth_error_printk("h_multicast_ctrl rc=%ld when adding an entry to the filter table\n", lpar_rc);
874 }
875 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 /* re-enable filtering */
878 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
879 IbmVethMcastEnableFiltering,
880 0);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200881 if(lpar_rc != H_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 ibmveth_error_printk("h_multicast_ctrl rc=%ld when enabling filtering\n", lpar_rc);
883 }
884 }
885}
886
887static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
888{
Santiago Leonb6d35182005-10-26 10:47:01 -0600889 struct ibmveth_adapter *adapter = dev->priv;
Santiago Leon860f2422006-04-25 11:19:59 -0500890 int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
Santiago Leonb6d35182005-10-26 10:47:01 -0600891 int i;
Santiago Leonb6d35182005-10-26 10:47:01 -0600892
Santiago Leon860f2422006-04-25 11:19:59 -0500893 if (new_mtu < IBMVETH_MAX_MTU)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return -EINVAL;
Santiago Leonb6d35182005-10-26 10:47:01 -0600895
Santiago Leon860f2422006-04-25 11:19:59 -0500896 /* Look for an active buffer pool that can hold the new MTU */
Santiago Leonb6d35182005-10-26 10:47:01 -0600897 for(i = 0; i<IbmVethNumBufferPools; i++) {
Santiago Leon860f2422006-04-25 11:19:59 -0500898 if (!adapter->rx_buff_pool[i].active)
899 continue;
900 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size) {
901 dev->mtu = new_mtu;
902 return 0;
Santiago Leonb6d35182005-10-26 10:47:01 -0600903 }
Santiago Leonb6d35182005-10-26 10:47:01 -0600904 }
Santiago Leon860f2422006-04-25 11:19:59 -0500905 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
909{
Santiago Leonb6d35182005-10-26 10:47:01 -0600910 int rc, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct net_device *netdev;
Santiago Leonb6d35182005-10-26 10:47:01 -0600912 struct ibmveth_adapter *adapter = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 unsigned char *mac_addr_p;
915 unsigned int *mcastFilterSize_p;
916
917
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400918 ibmveth_debug_printk_no_adapter("entering ibmveth_probe for UA 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 dev->unit_address);
920
921 mac_addr_p = (unsigned char *) vio_get_attribute(dev, VETH_MAC_ADDR, 0);
922 if(!mac_addr_p) {
923 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find VETH_MAC_ADDR "
924 "attribute\n", __FILE__, __LINE__);
925 return 0;
926 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 mcastFilterSize_p= (unsigned int *) vio_get_attribute(dev, VETH_MCAST_FILTER_SIZE, 0);
929 if(!mcastFilterSize_p) {
930 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find "
931 "VETH_MCAST_FILTER_SIZE attribute\n",
932 __FILE__, __LINE__);
933 return 0;
934 }
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
937
938 if(!netdev)
939 return -ENOMEM;
940
941 SET_MODULE_OWNER(netdev);
942
943 adapter = netdev->priv;
944 memset(adapter, 0, sizeof(adapter));
945 dev->dev.driver_data = netdev;
946
947 adapter->vdev = dev;
948 adapter->netdev = netdev;
949 adapter->mcastFilterSize= *mcastFilterSize_p;
Santiago Leon860f2422006-04-25 11:19:59 -0500950 adapter->pool_config = 0;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /* Some older boxes running PHYP non-natively have an OF that
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400953 returns a 8-byte local-mac-address field (and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 2 bytes have to be ignored) while newer boxes' OF return
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400955 a 6-byte field. Note that IEEE 1275 specifies that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 local-mac-address must be a 6-byte field.
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400957 The RPA doc specifies that the first byte must be 10b, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 we'll just look for it to solve this 8 vs. 6 byte field issue */
959
960 if ((*mac_addr_p & 0x3) != 0x02)
961 mac_addr_p += 2;
962
963 adapter->mac_addr = 0;
964 memcpy(&adapter->mac_addr, mac_addr_p, 6);
965
966 adapter->liobn = dev->iommu_table->it_index;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 netdev->irq = dev->irq;
969 netdev->open = ibmveth_open;
970 netdev->poll = ibmveth_poll;
971 netdev->weight = 16;
972 netdev->stop = ibmveth_close;
973 netdev->hard_start_xmit = ibmveth_start_xmit;
974 netdev->get_stats = ibmveth_get_stats;
975 netdev->set_multicast_list = ibmveth_set_multicast_list;
976 netdev->do_ioctl = ibmveth_ioctl;
977 netdev->ethtool_ops = &netdev_ethtool_ops;
978 netdev->change_mtu = ibmveth_change_mtu;
979 SET_NETDEV_DEV(netdev, &dev->dev);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400980 netdev->features |= NETIF_F_LLTX;
Santiago Leon60296d92005-10-26 10:47:16 -0600981 spin_lock_init(&adapter->stats_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 memcpy(&netdev->dev_addr, &adapter->mac_addr, netdev->addr_len);
984
Santiago Leon860f2422006-04-25 11:19:59 -0500985 for(i = 0; i<IbmVethNumBufferPools; i++) {
986 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
Jeff Garzikd7fbeba2006-05-24 01:31:14 -0400987 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
988 pool_count[i], pool_size[i],
Santiago Leon860f2422006-04-25 11:19:59 -0500989 pool_active[i]);
990 kobj->parent = &dev->dev.kobj;
991 sprintf(kobj->name, "pool%d", i);
992 kobj->ktype = &ktype_veth_pool;
993 kobject_register(kobj);
994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 ibmveth_debug_printk("adapter @ 0x%p\n", adapter);
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 adapter->buffer_list_dma = DMA_ERROR_CODE;
999 adapter->filter_list_dma = DMA_ERROR_CODE;
1000 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 ibmveth_debug_printk("registering netdev...\n");
1003
1004 rc = register_netdev(netdev);
1005
1006 if(rc) {
1007 ibmveth_debug_printk("failed to register netdev rc=%d\n", rc);
1008 free_netdev(netdev);
1009 return rc;
1010 }
1011
1012 ibmveth_debug_printk("registered\n");
1013
1014 ibmveth_proc_register_adapter(adapter);
1015
1016 return 0;
1017}
1018
1019static int __devexit ibmveth_remove(struct vio_dev *dev)
1020{
1021 struct net_device *netdev = dev->dev.driver_data;
1022 struct ibmveth_adapter *adapter = netdev->priv;
Santiago Leon860f2422006-04-25 11:19:59 -05001023 int i;
1024
1025 for(i = 0; i<IbmVethNumBufferPools; i++)
1026 kobject_unregister(&adapter->rx_buff_pool[i].kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 unregister_netdev(netdev);
1029
1030 ibmveth_proc_unregister_adapter(adapter);
1031
1032 free_netdev(netdev);
1033 return 0;
1034}
1035
1036#ifdef CONFIG_PROC_FS
1037static void ibmveth_proc_register_driver(void)
1038{
Al Viro66600222005-09-28 22:32:57 +01001039 ibmveth_proc_dir = proc_mkdir(IBMVETH_PROC_DIR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (ibmveth_proc_dir) {
1041 SET_MODULE_OWNER(ibmveth_proc_dir);
1042 }
1043}
1044
1045static void ibmveth_proc_unregister_driver(void)
1046{
Al Viro66600222005-09-28 22:32:57 +01001047 remove_proc_entry(IBMVETH_PROC_DIR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001050static void *ibmveth_seq_start(struct seq_file *seq, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 if (*pos == 0) {
1053 return (void *)1;
1054 } else {
1055 return NULL;
1056 }
1057}
1058
1059static void *ibmveth_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1060{
1061 ++*pos;
1062 return NULL;
1063}
1064
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001065static void ibmveth_seq_stop(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067}
1068
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001069static int ibmveth_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 struct ibmveth_adapter *adapter = seq->private;
1072 char *current_mac = ((char*) &adapter->netdev->dev_addr);
1073 char *firmware_mac = ((char*) &adapter->mac_addr) ;
1074
1075 seq_printf(seq, "%s %s\n\n", ibmveth_driver_string, ibmveth_driver_version);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 seq_printf(seq, "Unit Address: 0x%x\n", adapter->vdev->unit_address);
1078 seq_printf(seq, "LIOBN: 0x%lx\n", adapter->liobn);
1079 seq_printf(seq, "Current MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
1080 current_mac[0], current_mac[1], current_mac[2],
1081 current_mac[3], current_mac[4], current_mac[5]);
1082 seq_printf(seq, "Firmware MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
1083 firmware_mac[0], firmware_mac[1], firmware_mac[2],
1084 firmware_mac[3], firmware_mac[4], firmware_mac[5]);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 seq_printf(seq, "\nAdapter Statistics:\n");
1087 seq_printf(seq, " TX: skbuffs linearized: %ld\n", adapter->tx_linearized);
1088 seq_printf(seq, " multi-descriptor sends: %ld\n", adapter->tx_multidesc_send);
1089 seq_printf(seq, " skb_linearize failures: %ld\n", adapter->tx_linearize_failed);
1090 seq_printf(seq, " vio_map_single failres: %ld\n", adapter->tx_map_failed);
1091 seq_printf(seq, " send failures: %ld\n", adapter->tx_send_failed);
1092 seq_printf(seq, " RX: replenish task cycles: %ld\n", adapter->replenish_task_cycles);
1093 seq_printf(seq, " alloc_skb_failures: %ld\n", adapter->replenish_no_mem);
1094 seq_printf(seq, " add buffer failures: %ld\n", adapter->replenish_add_buff_failure);
1095 seq_printf(seq, " invalid buffers: %ld\n", adapter->rx_invalid_buffer);
1096 seq_printf(seq, " no buffers: %ld\n", adapter->rx_no_buffer);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return 0;
1099}
1100static struct seq_operations ibmveth_seq_ops = {
1101 .start = ibmveth_seq_start,
1102 .next = ibmveth_seq_next,
1103 .stop = ibmveth_seq_stop,
1104 .show = ibmveth_seq_show,
1105};
1106
1107static int ibmveth_proc_open(struct inode *inode, struct file *file)
1108{
1109 struct seq_file *seq;
1110 struct proc_dir_entry *proc;
1111 int rc;
1112
1113 rc = seq_open(file, &ibmveth_seq_ops);
1114 if (!rc) {
1115 /* recover the pointer buried in proc_dir_entry data */
1116 seq = file->private_data;
1117 proc = PDE(inode);
1118 seq->private = proc->data;
1119 }
1120 return rc;
1121}
1122
1123static struct file_operations ibmveth_proc_fops = {
1124 .owner = THIS_MODULE,
1125 .open = ibmveth_proc_open,
1126 .read = seq_read,
1127 .llseek = seq_lseek,
1128 .release = seq_release,
1129};
1130
1131static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1132{
1133 struct proc_dir_entry *entry;
1134 if (ibmveth_proc_dir) {
1135 entry = create_proc_entry(adapter->netdev->name, S_IFREG, ibmveth_proc_dir);
1136 if (!entry) {
1137 ibmveth_error_printk("Cannot create adapter proc entry");
1138 } else {
1139 entry->data = (void *) adapter;
1140 entry->proc_fops = &ibmveth_proc_fops;
1141 SET_MODULE_OWNER(entry);
1142 }
1143 }
1144 return;
1145}
1146
1147static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1148{
1149 if (ibmveth_proc_dir) {
1150 remove_proc_entry(adapter->netdev->name, ibmveth_proc_dir);
1151 }
1152}
1153
1154#else /* CONFIG_PROC_FS */
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001155static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157}
1158
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001159static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161}
1162static void ibmveth_proc_register_driver(void)
1163{
1164}
1165
1166static void ibmveth_proc_unregister_driver(void)
1167{
1168}
1169#endif /* CONFIG_PROC_FS */
1170
Santiago Leon860f2422006-04-25 11:19:59 -05001171static struct attribute veth_active_attr;
1172static struct attribute veth_num_attr;
1173static struct attribute veth_size_attr;
1174
1175static ssize_t veth_pool_show(struct kobject * kobj,
1176 struct attribute * attr, char * buf)
1177{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001178 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001179 struct ibmveth_buff_pool,
1180 kobj);
1181
1182 if (attr == &veth_active_attr)
1183 return sprintf(buf, "%d\n", pool->active);
1184 else if (attr == &veth_num_attr)
1185 return sprintf(buf, "%d\n", pool->size);
1186 else if (attr == &veth_size_attr)
1187 return sprintf(buf, "%d\n", pool->buff_size);
1188 return 0;
1189}
1190
1191static ssize_t veth_pool_store(struct kobject * kobj, struct attribute * attr,
1192const char * buf, size_t count)
1193{
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001194 struct ibmveth_buff_pool *pool = container_of(kobj,
Santiago Leon860f2422006-04-25 11:19:59 -05001195 struct ibmveth_buff_pool,
1196 kobj);
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001197 struct net_device *netdev =
Santiago Leon860f2422006-04-25 11:19:59 -05001198 container_of(kobj->parent, struct device, kobj)->driver_data;
1199 struct ibmveth_adapter *adapter = netdev->priv;
1200 long value = simple_strtol(buf, NULL, 10);
1201 long rc;
1202
1203 if (attr == &veth_active_attr) {
1204 if (value && !pool->active) {
1205 if(ibmveth_alloc_buffer_pool(pool)) {
1206 ibmveth_error_printk("unable to alloc pool\n");
1207 return -ENOMEM;
1208 }
1209 pool->active = 1;
1210 adapter->pool_config = 1;
1211 ibmveth_close(netdev);
1212 adapter->pool_config = 0;
1213 if ((rc = ibmveth_open(netdev)))
1214 return rc;
1215 } else if (!value && pool->active) {
1216 int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1217 int i;
1218 /* Make sure there is a buffer pool with buffers that
1219 can hold a packet of the size of the MTU */
1220 for(i = 0; i<IbmVethNumBufferPools; i++) {
1221 if (pool == &adapter->rx_buff_pool[i])
1222 continue;
1223 if (!adapter->rx_buff_pool[i].active)
1224 continue;
1225 if (mtu < adapter->rx_buff_pool[i].buff_size) {
1226 pool->active = 0;
1227 h_free_logical_lan_buffer(adapter->
1228 vdev->
1229 unit_address,
1230 pool->
1231 buff_size);
1232 }
1233 }
1234 if (pool->active) {
1235 ibmveth_error_printk("no active pool >= MTU\n");
1236 return -EPERM;
1237 }
1238 }
1239 } else if (attr == &veth_num_attr) {
1240 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT)
1241 return -EINVAL;
1242 else {
1243 adapter->pool_config = 1;
1244 ibmveth_close(netdev);
1245 adapter->pool_config = 0;
1246 pool->size = value;
1247 if ((rc = ibmveth_open(netdev)))
1248 return rc;
1249 }
1250 } else if (attr == &veth_size_attr) {
1251 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE)
1252 return -EINVAL;
1253 else {
1254 adapter->pool_config = 1;
1255 ibmveth_close(netdev);
1256 adapter->pool_config = 0;
1257 pool->buff_size = value;
1258 if ((rc = ibmveth_open(netdev)))
1259 return rc;
1260 }
1261 }
1262
1263 /* kick the interrupt handler to allocate/deallocate pools */
1264 ibmveth_interrupt(netdev->irq, netdev, NULL);
1265 return count;
1266}
1267
1268
1269#define ATTR(_name, _mode) \
1270 struct attribute veth_##_name##_attr = { \
1271 .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE \
1272 };
1273
1274static ATTR(active, 0644);
1275static ATTR(num, 0644);
1276static ATTR(size, 0644);
1277
1278static struct attribute * veth_pool_attrs[] = {
1279 &veth_active_attr,
1280 &veth_num_attr,
1281 &veth_size_attr,
1282 NULL,
1283};
1284
1285static struct sysfs_ops veth_pool_ops = {
1286 .show = veth_pool_show,
1287 .store = veth_pool_store,
1288};
1289
1290static struct kobj_type ktype_veth_pool = {
1291 .release = NULL,
1292 .sysfs_ops = &veth_pool_ops,
1293 .default_attrs = veth_pool_attrs,
1294};
1295
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297static struct vio_device_id ibmveth_device_table[] __devinitdata= {
1298 { "network", "IBM,l-lan"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +10001299 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1302
1303static struct vio_driver ibmveth_driver = {
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001304 .id_table = ibmveth_device_table,
1305 .probe = ibmveth_probe,
1306 .remove = ibmveth_remove,
1307 .driver = {
1308 .name = ibmveth_driver_name,
Stephen Rothwell915124d2005-10-24 15:12:22 +10001309 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +10001310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311};
1312
1313static int __init ibmveth_module_init(void)
1314{
1315 ibmveth_printk("%s: %s %s\n", ibmveth_driver_name, ibmveth_driver_string, ibmveth_driver_version);
1316
1317 ibmveth_proc_register_driver();
1318
1319 return vio_register_driver(&ibmveth_driver);
1320}
1321
1322static void __exit ibmveth_module_exit(void)
1323{
1324 vio_unregister_driver(&ibmveth_driver);
1325 ibmveth_proc_unregister_driver();
Jeff Garzikd7fbeba2006-05-24 01:31:14 -04001326}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328module_init(ibmveth_module_init);
1329module_exit(ibmveth_module_exit);