1 /**************************************************************************/
2 /* */
3 /* IBM eServer i/pSeries Virtual Ethernet Device Driver */
4 /* Copyright (C) 2003 Dave Larson (larson1@us.ibm.com), IBM Corp. */
5 /* */
6 /* This program is free software; you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation; either version 2 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* This program is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU General Public License */
17 /* along with this program; if not, write to the Free Software */
18 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 */
19 /* USA */
20 /* */
21 /* This module contains the implementation of a virtual ethernet device */
22 /* for use with IBM i/pSeries LPAR Linux. It utilizes the logical LAN */
23 /* option of the RS/6000 Platform Architechture to interface with virtual */
24 /* ethernet NICs that are presented to the partition by the hypervisor. */
25 /* */
26 /**************************************************************************/
27 /*
28 TODO:
29 - remove frag processing code - no longer needed
30 - add support for sysfs
31 - possibly remove procfs support
32 */
33
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/version.h>
37 #include <linux/types.h>
38 #include <linux/errno.h>
39 #include <linux/ioport.h>
40 #include <linux/pci.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/pci_dma.h>
54 #include <asm/vio.h>
55 #include <asm/uaccess.h>
56 #include <linux/proc_fs.h>
57 #include <linux/seq_file.h>
58
59 #include "ibmveth.h"
60
61 #define DEBUG 1
62
63 #define ibmveth_printk(fmt, args...) \
64 printk(KERN_INFO "%s: " fmt, __FILE__, ## args)
65
66 #define ibmveth_error_printk(fmt, args...) \
67 printk(KERN_ERR "(%s:%3.3d ua:%lx) ERROR: " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
68
69 #ifdef DEBUG
70 #define ibmveth_debug_printk_no_adapter(fmt, args...) \
71 printk(KERN_DEBUG "(%s:%3.3d): " fmt, __FILE__, __LINE__ , ## args)
72 #define ibmveth_debug_printk(fmt, args...) \
73 printk(KERN_DEBUG "(%s:%3.3d ua:%lx): " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
74 #define ibmveth_assert(expr) \
75 if(!(expr)) { \
76 printk(KERN_DEBUG "assertion failed (%s:%3.3d ua:%lx): %s\n", __FILE__, __LINE__, adapter->vdev->unit_address, #expr); \
77 BUG(); \
78 }
79 #else
80 #define ibmveth_debug_printk_no_adapter(fmt, args...)
81 #define ibmveth_debug_printk(fmt, args...)
82 #define ibmveth_assert(expr)
83 #endif
84
85 static int ibmveth_open(struct net_device *dev);
86 static int ibmveth_close(struct net_device *dev);
87 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
88 static int ibmveth_poll(struct net_device *dev, int *budget);
89 static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *dev);
90 static void ibmveth_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
91 static struct net_device_stats *ibmveth_get_stats(struct net_device *dev);
92 static void ibmveth_set_multicast_list(struct net_device *dev);
93 static void ibmveth_proc_register_driver(void);
94 static void ibmveth_proc_unregister_driver(void);
95 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter);
96 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter);
97
98 #define IBMVETH_PROC_DIR "ibmveth"
99 static struct proc_dir_entry *ibmveth_proc_dir;
100
101 static const char ibmveth_driver_name[] = "ibmveth";
102 static const char ibmveth_driver_string[] = "IBM i/pSeries Virtual Ethernet Driver";
103 static const char ibmveth_driver_version[] = "1.0";
104
105 MODULE_AUTHOR("Dave Larson <larson1@us.ibm.com>");
106 MODULE_DESCRIPTION("IBM i/pSeries Virtual Ethernet Driver");
107 MODULE_LICENSE("GPL");
108
109 /* simple methods of getting data from the current rxq entry */
ibmveth_rxq_pending_buffer(struct ibmveth_adapter * adapter)110 static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
111 {
112 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].toggle == adapter->rx_queue.toggle);
113 }
114
ibmveth_rxq_buffer_valid(struct ibmveth_adapter * adapter)115 static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
116 {
117 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].valid);
118 }
119
ibmveth_rxq_frame_offset(struct ibmveth_adapter * adapter)120 static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
121 {
122 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].offset);
123 }
124
ibmveth_rxq_frame_length(struct ibmveth_adapter * adapter)125 static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
126 {
127 return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
128 }
129
130 /* setup the initial settings for a buffer pool */
ibmveth_init_buffer_pool(struct ibmveth_buff_pool * pool,u32 pool_index,u32 pool_size,u32 buff_size)131 static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool, u32 pool_index, u32 pool_size, u32 buff_size)
132 {
133 pool->size = pool_size;
134 pool->index = pool_index;
135 pool->buff_size = buff_size;
136 pool->threshold = pool_size / 2;
137 }
138
139 /* allocate and setup an buffer pool - called during open */
ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool * pool)140 static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
141 {
142 int i;
143
144 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
145
146 if(!pool->free_map) {
147 return -1;
148 }
149
150 pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
151 if(!pool->dma_addr) {
152 kfree(pool->free_map);
153 pool->free_map = NULL;
154 return -1;
155 }
156
157 pool->skbuff = kmalloc(sizeof(void*) * pool->size, GFP_KERNEL);
158
159 if(!pool->skbuff) {
160 kfree(pool->dma_addr);
161 pool->dma_addr = NULL;
162
163 kfree(pool->free_map);
164 pool->free_map = NULL;
165 return -1;
166 }
167
168 memset(pool->skbuff, 0, sizeof(void*) * pool->size);
169 memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
170
171 for(i = 0; i < pool->size; ++i) {
172 pool->free_map[i] = i;
173 }
174
175 atomic_set(&pool->available, 0);
176 pool->producer_index = 0;
177 pool->consumer_index = 0;
178
179 return 0;
180 }
181
182 /* replenish the buffers for a pool. note that we don't need to
183 * skb_reserve these since they are used for incoming...
184 */
ibmveth_replenish_buffer_pool(struct ibmveth_adapter * adapter,struct ibmveth_buff_pool * pool)185 static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
186 {
187 u32 i;
188 u32 count = pool->size - atomic_read(&pool->available);
189 u32 buffers_added = 0;
190
191 mb();
192
193 for(i = 0; i < count; ++i) {
194 struct sk_buff *skb;
195 unsigned int free_index, index;
196 u64 correlator;
197 union ibmveth_buf_desc desc;
198 unsigned long lpar_rc;
199 dma_addr_t dma_addr;
200
201 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
202
203 if(!skb) {
204 ibmveth_debug_printk("replenish: unable to allocate skb\n");
205 adapter->replenish_no_mem++;
206 break;
207 }
208
209 free_index = pool->consumer_index++ % pool->size;
210 index = pool->free_map[free_index];
211
212 ibmveth_assert(index != 0xffff);
213 ibmveth_assert(pool->skbuff[index] == NULL);
214
215 dma_addr = vio_map_single(adapter->vdev, skb->data, pool->buff_size, PCI_DMA_FROMDEVICE);
216
217 pool->dma_addr[index] = dma_addr;
218 pool->skbuff[index] = skb;
219
220 correlator = ((u64)pool->index << 32) | index;
221 *(u64*)skb->data = correlator;
222
223 desc.desc = 0;
224 desc.fields.valid = 1;
225 desc.fields.length = pool->buff_size;
226 desc.fields.address = dma_addr;
227
228 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
229
230 if(lpar_rc != H_Success) {
231 pool->skbuff[index] = NULL;
232 pool->consumer_index--;
233 vio_unmap_single(adapter->vdev, pool->dma_addr[index], pool->buff_size, PCI_DMA_FROMDEVICE);
234 dev_kfree_skb_any(skb);
235 adapter->replenish_add_buff_failure++;
236 break;
237 } else {
238 pool->free_map[free_index] = 0xffff;
239 buffers_added++;
240 adapter->replenish_add_buff_success++;
241 }
242 }
243
244 atomic_add(buffers_added, &(pool->available));
245 }
246
247 /* check if replenishing is needed. */
ibmveth_is_replenishing_needed(struct ibmveth_adapter * adapter)248 static inline int ibmveth_is_replenishing_needed(struct ibmveth_adapter *adapter)
249 {
250 return ((atomic_read(&adapter->rx_buff_pool[0].available) < adapter->rx_buff_pool[0].threshold) ||
251 (atomic_read(&adapter->rx_buff_pool[1].available) < adapter->rx_buff_pool[1].threshold) ||
252 (atomic_read(&adapter->rx_buff_pool[2].available) < adapter->rx_buff_pool[2].threshold));
253 }
254
255 /* replenish tasklet routine */
ibmveth_replenish_task(struct ibmveth_adapter * adapter)256 static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
257 {
258 adapter->replenish_task_cycles++;
259
260 ibmveth_replenish_buffer_pool(adapter, &adapter->rx_buff_pool[0]);
261 ibmveth_replenish_buffer_pool(adapter, &adapter->rx_buff_pool[1]);
262 ibmveth_replenish_buffer_pool(adapter, &adapter->rx_buff_pool[2]);
263
264 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
265 }
266
267 /* kick the replenish tasklet if we need replenishing and it isn't already running */
ibmveth_schedule_replenishing(struct ibmveth_adapter * adapter)268 static inline void ibmveth_schedule_replenishing(struct ibmveth_adapter *adapter)
269 {
270 if(ibmveth_is_replenishing_needed(adapter)) {
271 tasklet_schedule(&adapter->replenish_task);
272 }
273 }
274
275 /* empty and free ana buffer pool - also used to do cleanup in error paths */
ibmveth_free_buffer_pool(struct ibmveth_adapter * adapter,struct ibmveth_buff_pool * pool)276 static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
277 {
278 int i;
279
280 if(pool->free_map) {
281 kfree(pool->free_map);
282 pool->free_map = NULL;
283 }
284
285 if(pool->skbuff && pool->dma_addr) {
286 for(i = 0; i < pool->size; ++i) {
287 struct sk_buff *skb = pool->skbuff[i];
288 if(skb) {
289 vio_unmap_single(adapter->vdev,
290 pool->dma_addr[i],
291 pool->buff_size,
292 PCI_DMA_FROMDEVICE);
293 dev_kfree_skb_any(skb);
294 pool->skbuff[i] = NULL;
295 }
296 }
297 }
298
299 if(pool->dma_addr) {
300 kfree(pool->dma_addr);
301 pool->dma_addr = NULL;
302 }
303
304 if(pool->skbuff) {
305 kfree(pool->skbuff);
306 pool->skbuff = NULL;
307 }
308 }
309
310 /* remove a buffer from a pool */
ibmveth_remove_buffer_from_pool(struct ibmveth_adapter * adapter,u64 correlator)311 static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter, u64 correlator)
312 {
313 unsigned int pool = correlator >> 32;
314 unsigned int index = correlator & 0xffffffffUL;
315 unsigned int free_index;
316 struct sk_buff *skb;
317
318 ibmveth_assert(pool < IbmVethNumBufferPools);
319 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
320
321 skb = adapter->rx_buff_pool[pool].skbuff[index];
322
323 ibmveth_assert(skb != NULL);
324
325 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
326
327 vio_unmap_single(adapter->vdev,
328 adapter->rx_buff_pool[pool].dma_addr[index],
329 adapter->rx_buff_pool[pool].buff_size,
330 PCI_DMA_FROMDEVICE);
331
332 free_index = adapter->rx_buff_pool[pool].producer_index++ % adapter->rx_buff_pool[pool].size;
333 adapter->rx_buff_pool[pool].free_map[free_index] = index;
334
335 mb();
336
337 atomic_dec(&(adapter->rx_buff_pool[pool].available));
338 }
339
340 /* get the current buffer on the rx queue */
ibmveth_rxq_get_buffer(struct ibmveth_adapter * adapter)341 static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
342 {
343 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
344 unsigned int pool = correlator >> 32;
345 unsigned int index = correlator & 0xffffffffUL;
346
347 ibmveth_assert(pool < IbmVethNumBufferPools);
348 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
349
350 return adapter->rx_buff_pool[pool].skbuff[index];
351 }
352
353 /* recycle the current buffer on the rx queue */
ibmveth_rxq_recycle_buffer(struct ibmveth_adapter * adapter)354 static void ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
355 {
356 u32 q_index = adapter->rx_queue.index;
357 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
358 unsigned int pool = correlator >> 32;
359 unsigned int index = correlator & 0xffffffffUL;
360 union ibmveth_buf_desc desc;
361 unsigned long lpar_rc;
362
363 ibmveth_assert(pool < IbmVethNumBufferPools);
364 ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
365
366 desc.desc = 0;
367 desc.fields.valid = 1;
368 desc.fields.length = adapter->rx_buff_pool[pool].buff_size;
369 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
370
371 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
372
373 if(lpar_rc != H_Success) {
374 ibmveth_debug_printk("h_add_logical_lan_buffer failed during recycle rc=%ld", lpar_rc);
375 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
376 }
377
378 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
379 adapter->rx_queue.index = 0;
380 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
381 }
382 }
383
ibmveth_rxq_harvest_buffer(struct ibmveth_adapter * adapter)384 static inline void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
385 {
386 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
387
388 if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
389 adapter->rx_queue.index = 0;
390 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
391 }
392 }
393
ibmveth_cleanup(struct ibmveth_adapter * adapter)394 static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
395 {
396 if(adapter->buffer_list_addr != NULL) {
397 if(adapter->buffer_list_dma != NO_TCE) {
398 vio_unmap_single(adapter->vdev, adapter->buffer_list_dma, 4096, PCI_DMA_BIDIRECTIONAL);
399 adapter->buffer_list_dma = NO_TCE;
400 }
401 free_page((unsigned long)adapter->buffer_list_addr);
402 adapter->buffer_list_addr = NULL;
403 }
404
405 if(adapter->filter_list_addr != NULL) {
406 if(adapter->filter_list_dma != NO_TCE) {
407 vio_unmap_single(adapter->vdev, adapter->filter_list_dma, 4096, PCI_DMA_BIDIRECTIONAL);
408 adapter->filter_list_dma = NO_TCE;
409 }
410 free_page((unsigned long)adapter->filter_list_addr);
411 adapter->filter_list_addr = NULL;
412 }
413
414 if(adapter->rx_queue.queue_addr != NULL) {
415 if(adapter->rx_queue.queue_dma != NO_TCE) {
416 vio_unmap_single(adapter->vdev, adapter->rx_queue.queue_dma, adapter->rx_queue.queue_len, PCI_DMA_BIDIRECTIONAL);
417 adapter->rx_queue.queue_dma = NO_TCE;
418 }
419 kfree(adapter->rx_queue.queue_addr);
420 adapter->rx_queue.queue_addr = NULL;
421 }
422
423 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[0]);
424 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[1]);
425 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[2]);
426 }
427
ibmveth_open(struct net_device * netdev)428 static int ibmveth_open(struct net_device *netdev)
429 {
430 struct ibmveth_adapter *adapter = netdev->priv;
431 u64 mac_address = 0;
432 int rxq_entries;
433 unsigned long lpar_rc;
434 int rc;
435 union ibmveth_buf_desc rxq_desc;
436
437 ibmveth_debug_printk("open starting\n");
438
439 rxq_entries =
440 adapter->rx_buff_pool[0].size +
441 adapter->rx_buff_pool[1].size +
442 adapter->rx_buff_pool[2].size + 1;
443
444 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
445 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
446
447 if(!adapter->buffer_list_addr || !adapter->filter_list_addr) {
448 ibmveth_error_printk("unable to allocate filter or buffer list pages\n");
449 ibmveth_cleanup(adapter);
450 return -ENOMEM;
451 }
452
453 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) * rxq_entries;
454 adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len, GFP_KERNEL);
455
456 if(!adapter->rx_queue.queue_addr) {
457 ibmveth_error_printk("unable to allocate rx queue pages\n");
458 ibmveth_cleanup(adapter);
459 return -ENOMEM;
460 }
461
462 adapter->buffer_list_dma = vio_map_single(adapter->vdev, adapter->buffer_list_addr, 4096, PCI_DMA_BIDIRECTIONAL);
463 adapter->filter_list_dma = vio_map_single(adapter->vdev, adapter->filter_list_addr, 4096, PCI_DMA_BIDIRECTIONAL);
464 adapter->rx_queue.queue_dma = vio_map_single(adapter->vdev, adapter->rx_queue.queue_addr, adapter->rx_queue.queue_len, PCI_DMA_BIDIRECTIONAL);
465
466 if((adapter->buffer_list_dma == NO_TCE) ||
467 (adapter->filter_list_dma == NO_TCE) ||
468 (adapter->rx_queue.queue_dma == NO_TCE)) {
469 ibmveth_error_printk("unable to map filter or buffer list pages\n");
470 ibmveth_cleanup(adapter);
471 return -ENOMEM;
472 }
473
474 adapter->rx_queue.index = 0;
475 adapter->rx_queue.num_slots = rxq_entries;
476 adapter->rx_queue.toggle = 1;
477
478 if(ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[0]) ||
479 ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[1]) ||
480 ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[2]))
481 {
482 ibmveth_error_printk("unable to allocate buffer pools\n");
483 ibmveth_cleanup(adapter);
484 return -ENOMEM;
485 }
486
487 memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
488 mac_address = mac_address >> 16;
489
490 rxq_desc.desc = 0;
491 rxq_desc.fields.valid = 1;
492 rxq_desc.fields.length = adapter->rx_queue.queue_len;
493 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
494
495 ibmveth_debug_printk("buffer list @ 0x%p\n", adapter->buffer_list_addr);
496 ibmveth_debug_printk("filter list @ 0x%p\n", adapter->filter_list_addr);
497 ibmveth_debug_printk("receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
498
499
500 lpar_rc = h_register_logical_lan(adapter->vdev->unit_address,
501 adapter->buffer_list_dma,
502 rxq_desc.desc,
503 adapter->filter_list_dma,
504 mac_address);
505
506 if(lpar_rc != H_Success) {
507 ibmveth_error_printk("h_register_logical_lan failed with %ld\n", lpar_rc);
508 ibmveth_error_printk("buffer TCE:0x%x filter TCE:0x%x rxq desc:0x%lx MAC:0x%lx\n",
509 adapter->buffer_list_dma,
510 adapter->filter_list_dma,
511 rxq_desc.desc,
512 mac_address);
513 ibmveth_cleanup(adapter);
514 return -ENONET;
515 }
516
517 ibmveth_debug_printk("registering irq 0x%x\n", netdev->irq);
518 if((rc = request_irq(netdev->irq, &ibmveth_interrupt, 0, netdev->name, netdev)) != 0) {
519 ibmveth_error_printk("unable to request irq 0x%x, rc %d\n", netdev->irq, rc);
520 h_free_logical_lan(adapter->vdev->unit_address);
521 ibmveth_cleanup(adapter);
522 return rc;
523 }
524
525 netif_start_queue(netdev);
526
527 ibmveth_debug_printk("scheduling initial replenish cycle\n");
528 ibmveth_schedule_replenishing(adapter);
529
530 ibmveth_debug_printk("open complete\n");
531
532 return 0;
533 }
534
ibmveth_close(struct net_device * netdev)535 static int ibmveth_close(struct net_device *netdev)
536 {
537 struct ibmveth_adapter *adapter = netdev->priv;
538 long lpar_rc;
539
540 ibmveth_debug_printk("close starting\n");
541
542 netif_stop_queue(netdev);
543
544 free_irq(netdev->irq, netdev);
545
546 tasklet_kill(&adapter->replenish_task);
547
548 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
549
550 if(lpar_rc != H_Success)
551 {
552 ibmveth_error_printk("h_free_logical_lan failed with %lx, continuing with close\n",
553 lpar_rc);
554 }
555
556 adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
557
558 ibmveth_cleanup(adapter);
559
560 ibmveth_debug_printk("close complete\n");
561
562 return 0;
563 }
564
netdev_get_settings(struct net_device * dev,struct ethtool_cmd * cmd)565 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
566 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
567 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | ADVERTISED_FIBRE);
568 cmd->speed = SPEED_1000;
569 cmd->duplex = DUPLEX_FULL;
570 cmd->port = PORT_FIBRE;
571 cmd->phy_address = 0;
572 cmd->transceiver = XCVR_INTERNAL;
573 cmd->autoneg = AUTONEG_ENABLE;
574 cmd->maxtxpkt = 0;
575 cmd->maxrxpkt = 1;
576 return 0;
577 }
578
netdev_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)579 static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info) {
580 strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
581 strncpy(info->version, ibmveth_driver_version, sizeof(info->version) - 1);
582 }
583
netdev_get_link(struct net_device * dev)584 static u32 netdev_get_link(struct net_device *dev) {
585 return 0;
586 }
587
588 static struct ethtool_ops netdev_ethtool_ops = {
589 .get_drvinfo = netdev_get_drvinfo,
590 .get_settings = netdev_get_settings,
591 .get_link = netdev_get_link,
592 .get_sg = ethtool_op_get_sg,
593 .get_tx_csum = ethtool_op_get_tx_csum,
594 };
595
ibmveth_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)596 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
597 {
598 return -EOPNOTSUPP;
599 }
600
601 #define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
602
ibmveth_start_xmit(struct sk_buff * skb,struct net_device * netdev)603 static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *netdev)
604 {
605 struct ibmveth_adapter *adapter = netdev->priv;
606 union ibmveth_buf_desc desc[IbmVethMaxSendFrags];
607 unsigned long lpar_rc;
608 int nfrags = 0, curfrag;
609
610 if ((skb_shinfo(skb)->nr_frags + 1) > IbmVethMaxSendFrags) {
611 adapter->stats.tx_dropped++;
612 dev_kfree_skb(skb);
613 return 0;
614 }
615
616 memset(&desc, 0, sizeof(desc));
617
618 /* nfrags = number of frags after the initial fragment */
619 nfrags = skb_shinfo(skb)->nr_frags;
620
621 if(nfrags)
622 adapter->tx_multidesc_send++;
623
624 /* map the initial fragment */
625 desc[0].fields.length = nfrags ? skb->len - skb->data_len : skb->len;
626 desc[0].fields.address = vio_map_single(adapter->vdev, skb->data, desc[0].fields.length, PCI_DMA_TODEVICE);
627 desc[0].fields.valid = 1;
628
629 if(desc[0].fields.address == NO_TCE) {
630 ibmveth_error_printk("tx: unable to map initial fragment\n");
631 adapter->tx_map_failed++;
632 adapter->stats.tx_dropped++;
633 dev_kfree_skb(skb);
634 return 0;
635 }
636
637 curfrag = nfrags;
638
639 /* map fragments past the initial portion if there are any */
640 while(curfrag--) {
641 skb_frag_t *frag = &skb_shinfo(skb)->frags[curfrag];
642 desc[curfrag+1].fields.address = vio_map_single(adapter->vdev,
643 page_address(frag->page) + frag->page_offset,
644 frag->size, PCI_DMA_TODEVICE);
645 desc[curfrag+1].fields.length = frag->size;
646 desc[curfrag+1].fields.valid = 1;
647
648 if(desc[curfrag+1].fields.address == NO_TCE) {
649 ibmveth_error_printk("tx: unable to map fragment %d\n", curfrag);
650 adapter->tx_map_failed++;
651 adapter->stats.tx_dropped++;
652 /* Free all the mappings we just created */
653 while(curfrag < nfrags) {
654 vio_unmap_single(adapter->vdev,
655 desc[curfrag+1].fields.address,
656 desc[curfrag+1].fields.length,
657 PCI_DMA_TODEVICE);
658 curfrag++;
659 }
660 dev_kfree_skb(skb);
661 return 0;
662 }
663 }
664
665 /* send the frame. Arbitrarily set retrycount to 1024 */
666 unsigned long correlator = 0;
667 unsigned int retry_count = 1024;
668 do {
669 lpar_rc = h_send_logical_lan(adapter->vdev->unit_address,
670 desc[0].desc,
671 desc[1].desc,
672 desc[2].desc,
673 desc[3].desc,
674 desc[4].desc,
675 desc[5].desc,
676 correlator);
677 } while ((lpar_rc == H_Busy) && (retry_count--));
678
679 if(lpar_rc != H_Success && lpar_rc != H_Dropped) {
680 int i;
681 ibmveth_error_printk("tx: h_send_logical_lan failed with rc=%ld\n", lpar_rc);
682 for(i = 0; i < 6; i++) {
683 ibmveth_error_printk("tx: desc[%i] valid=%d, len=%d, address=0x%d\n", i,
684 desc[i].fields.valid, desc[i].fields.length, desc[i].fields.address);
685 }
686 adapter->tx_send_failed++;
687 adapter->stats.tx_dropped++;
688 } else {
689 adapter->stats.tx_packets++;
690 adapter->stats.tx_bytes += skb->len;
691 }
692
693 do {
694 vio_unmap_single(adapter->vdev, desc[nfrags].fields.address, desc[nfrags].fields.length, PCI_DMA_TODEVICE);
695 } while(--nfrags >= 0);
696
697 dev_kfree_skb(skb);
698 return 0;
699 }
700
ibmveth_poll(struct net_device * netdev,int * budget)701 static int ibmveth_poll(struct net_device *netdev, int *budget)
702 {
703 struct ibmveth_adapter *adapter = netdev->priv;
704 int max_frames_to_process = netdev->quota;
705 int frames_processed = 0;
706 int more_work = 1;
707 unsigned long lpar_rc;
708
709 restart_poll:
710 do {
711 struct net_device *netdev = adapter->netdev;
712
713 if(ibmveth_rxq_pending_buffer(adapter)) {
714 struct sk_buff *skb;
715
716 if(!ibmveth_rxq_buffer_valid(adapter)) {
717 wmb(); /* suggested by larson1 */
718 adapter->rx_invalid_buffer++;
719 ibmveth_debug_printk("recycling invalid buffer\n");
720 ibmveth_rxq_recycle_buffer(adapter);
721 } else {
722 int length = ibmveth_rxq_frame_length(adapter);
723 int offset = ibmveth_rxq_frame_offset(adapter);
724 skb = ibmveth_rxq_get_buffer(adapter);
725
726 ibmveth_rxq_harvest_buffer(adapter);
727
728 skb_reserve(skb, offset);
729 skb_put(skb, length);
730 skb->dev = netdev;
731 skb->protocol = eth_type_trans(skb, netdev);
732
733 netif_receive_skb(skb); /* send it up */
734
735 adapter->stats.rx_packets++;
736 adapter->stats.rx_bytes += length;
737 frames_processed++;
738 }
739 } else {
740 more_work = 0;
741 }
742 } while(more_work && (frames_processed < max_frames_to_process));
743
744 ibmveth_schedule_replenishing(adapter);
745
746 if(more_work) {
747 /* more work to do - return that we are not done yet */
748 netdev->quota -= frames_processed;
749 *budget -= frames_processed;
750 return 1;
751 }
752
753 /* we think we are done - reenable interrupts, then check once more to make sure we are done */
754 lpar_rc = h_vio_signal(adapter->vdev->unit_address, IbmVethIntsEnabled);
755 ibmveth_assert(lpar_rc == H_Success);
756
757 netif_rx_complete(netdev);
758
759 if(ibmveth_rxq_pending_buffer(adapter) && netif_rx_reschedule(netdev, frames_processed))
760 {
761 lpar_rc = h_vio_signal(adapter->vdev->unit_address, IbmVethIntsDisabled);
762 ibmveth_assert(lpar_rc == H_Success);
763 more_work = 1;
764 goto restart_poll;
765 }
766
767 netdev->quota -= frames_processed;
768 *budget -= frames_processed;
769
770 /* we really are done */
771 return 0;
772 }
773
ibmveth_interrupt(int irq,void * dev_instance,struct pt_regs * regs)774 static void ibmveth_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
775 {
776 struct net_device *netdev = dev_instance;
777 struct ibmveth_adapter *adapter = netdev->priv;
778 unsigned long lpar_rc;
779
780 if(netif_rx_schedule_prep(netdev)) {
781 lpar_rc = h_vio_signal(adapter->vdev->unit_address, IbmVethIntsDisabled);
782 ibmveth_assert(lpar_rc == H_Success);
783 __netif_rx_schedule(netdev);
784 }
785 }
786
ibmveth_get_stats(struct net_device * dev)787 static struct net_device_stats *ibmveth_get_stats(struct net_device *dev)
788 {
789 struct ibmveth_adapter *adapter = dev->priv;
790 return &adapter->stats;
791 }
792
ibmveth_set_multicast_list(struct net_device * netdev)793 static void ibmveth_set_multicast_list(struct net_device *netdev)
794 {
795 struct ibmveth_adapter *adapter = netdev->priv;
796 unsigned long lpar_rc;
797
798 if((netdev->flags & IFF_PROMISC) || (netdev->mc_count > adapter->mcastFilterSize)) {
799 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
800 IbmVethMcastEnableRecv |
801 IbmVethMcastDisableFiltering,
802 0);
803 if(lpar_rc != H_Success) {
804 ibmveth_error_printk("h_multicast_ctrl rc=%ld when entering promisc mode\n", lpar_rc);
805 }
806 } else {
807 struct dev_mc_list *mclist = netdev->mc_list;
808 int i;
809 /* clear the filter table & disable filtering */
810 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
811 IbmVethMcastEnableRecv |
812 IbmVethMcastDisableFiltering |
813 IbmVethMcastClearFilterTable,
814 0);
815 if(lpar_rc != H_Success) {
816 ibmveth_error_printk("h_multicast_ctrl rc=%ld when attempting to clear filter table\n", lpar_rc);
817 }
818 /* add the addresses to the filter table */
819 for(i = 0; i < netdev->mc_count; ++i, mclist = mclist->next) {
820 // add the multicast address to the filter table
821 unsigned long mcast_addr = 0;
822 memcpy(((char *)&mcast_addr)+2, mclist->dmi_addr, 6);
823 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
824 IbmVethMcastAddFilter,
825 mcast_addr);
826 if(lpar_rc != H_Success) {
827 ibmveth_error_printk("h_multicast_ctrl rc=%ld when adding an entry to the filter table\n", lpar_rc);
828 }
829 }
830
831 /* re-enable filtering */
832 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
833 IbmVethMcastEnableFiltering,
834 0);
835 if(lpar_rc != H_Success) {
836 ibmveth_error_printk("h_multicast_ctrl rc=%ld when enabling filtering\n", lpar_rc);
837 }
838 }
839 }
840
ibmveth_probe(struct vio_dev * dev,const struct vio_device_id * id)841 static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
842 {
843 int rc;
844 struct net_device *netdev;
845 struct ibmveth_adapter *adapter;
846
847 unsigned int *mac_addr_p;
848 unsigned int *mcastFilterSize_p;
849
850
851 ibmveth_debug_printk_no_adapter("entering ibmveth_probe for UA 0x%lx\n",
852 dev->unit_address);
853
854 mac_addr_p = (unsigned int *) vio_get_attribute(dev, VETH_MAC_ADDR, 0);
855 if(!mac_addr_p) {
856 ibmveth_error_printk("Can't find VETH_MAC_ADDR attribute\n");
857 return 0;
858 }
859
860 mcastFilterSize_p= (unsigned int *) vio_get_attribute(dev, VETH_MCAST_FILTER_SIZE, 0);
861 if(!mcastFilterSize_p) {
862 ibmveth_error_printk("Can't find VETH_MCAST_FILTER_SIZE attribute\n");
863 return 0;
864 }
865
866 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
867
868 if(!netdev)
869 return -ENOMEM;
870
871 SET_MODULE_OWNER(netdev);
872
873 adapter = netdev->priv;
874 memset(adapter, 0, sizeof(adapter));
875 dev->driver_data = netdev;
876
877 adapter->vdev = dev;
878 adapter->netdev = netdev;
879 adapter->mcastFilterSize= *mcastFilterSize_p;
880
881 /* Some older boxes running PHYP non-natively have an OF that
882 returns a 8-byte local-mac-address field (and the first
883 2 bytes have to be ignored) while newer boxes' OF return
884 a 6-byte field. Note that IEEE 1275 specifies that
885 local-mac-address must be a 6-byte field.
886 The RPA doc specifies that the first byte must be 10b, so
887 we'll just look for it to solve this 8 vs. 6 byte field issue */
888
889 while (*((char*)mac_addr_p) != (char)(0x02))
890 ((char*)mac_addr_p)++;
891
892 adapter->mac_addr = 0;
893 memcpy(&adapter->mac_addr, mac_addr_p, 6);
894
895 adapter->liobn = dev->tce_table->index;
896
897 netdev->irq = dev->irq;
898 netdev->open = ibmveth_open;
899 netdev->poll = ibmveth_poll;
900 netdev->weight = 16;
901 netdev->stop = ibmveth_close;
902 netdev->hard_start_xmit = ibmveth_start_xmit;
903 netdev->get_stats = ibmveth_get_stats;
904 netdev->set_multicast_list = ibmveth_set_multicast_list;
905 netdev->do_ioctl = ibmveth_ioctl;
906 netdev->ethtool_ops = &netdev_ethtool_ops;
907
908 memcpy(&netdev->dev_addr, &adapter->mac_addr, netdev->addr_len);
909
910 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[0], 0, IbmVethPool0DftCnt, IbmVethPool0DftSize);
911 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[1], 1, IbmVethPool1DftCnt, IbmVethPool1DftSize);
912 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[2], 2, IbmVethPool2DftCnt, IbmVethPool2DftSize);
913
914 ibmveth_debug_printk("adapter @ 0x%p\n", adapter);
915
916 tasklet_init(&adapter->replenish_task, (void*)ibmveth_replenish_task, (unsigned long)adapter);
917
918 adapter->buffer_list_dma = NO_TCE;
919 adapter->filter_list_dma = NO_TCE;
920 adapter->rx_queue.queue_dma = NO_TCE;
921
922 ibmveth_debug_printk("registering netdev...\n");
923
924 rc = register_netdev(netdev);
925
926 if(rc) {
927 ibmveth_debug_printk("failed to register netdev rc=%d\n", rc);
928 free_netdev(netdev);
929 return rc;
930 }
931
932 ibmveth_debug_printk("registered\n");
933
934 ibmveth_proc_register_adapter(adapter);
935
936 return 0;
937 }
938
ibmveth_remove(struct vio_dev * dev)939 static void __devexit ibmveth_remove(struct vio_dev *dev)
940 {
941 struct net_device *netdev = dev->driver_data;
942 struct ibmveth_adapter *adapter = netdev->priv;
943
944 unregister_netdev(netdev);
945
946 ibmveth_proc_unregister_adapter(adapter);
947
948 free_netdev(netdev);
949 return;
950 }
951
952 #ifdef CONFIG_PROC_FS
ibmveth_proc_register_driver(void)953 static void ibmveth_proc_register_driver(void)
954 {
955 ibmveth_proc_dir = create_proc_entry(IBMVETH_PROC_DIR, S_IFDIR, proc_net);
956 if (ibmveth_proc_dir) {
957 SET_MODULE_OWNER(ibmveth_proc_dir);
958 }
959 }
960
ibmveth_proc_unregister_driver(void)961 static void ibmveth_proc_unregister_driver(void)
962 {
963 remove_proc_entry(IBMVETH_PROC_DIR, proc_net);
964 }
965
ibmveth_seq_start(struct seq_file * seq,loff_t * pos)966 static void *ibmveth_seq_start(struct seq_file *seq, loff_t *pos)
967 {
968 if (*pos == 0) {
969 return (void *)1;
970 } else {
971 return NULL;
972 }
973 }
974
ibmveth_seq_next(struct seq_file * seq,void * v,loff_t * pos)975 static void *ibmveth_seq_next(struct seq_file *seq, void *v, loff_t *pos)
976 {
977 ++*pos;
978 return NULL;
979 }
980
ibmveth_seq_stop(struct seq_file * seq,void * v)981 static void ibmveth_seq_stop(struct seq_file *seq, void *v)
982 {
983 }
984
ibmveth_seq_show(struct seq_file * seq,void * v)985 static int ibmveth_seq_show(struct seq_file *seq, void *v)
986 {
987 struct ibmveth_adapter *adapter = seq->private;
988 char *current_mac = ((char*) &adapter->netdev->dev_addr);
989 char *firmware_mac = ((char*) &adapter->mac_addr) ;
990
991 seq_printf(seq, "%s %s\n\n", ibmveth_driver_string, ibmveth_driver_version);
992
993 seq_printf(seq, "Unit Address: 0x%lx\n", adapter->vdev->unit_address);
994 seq_printf(seq, "LIOBN: 0x%lx\n", adapter->liobn);
995 seq_printf(seq, "Current MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
996 current_mac[0], current_mac[1], current_mac[2],
997 current_mac[3], current_mac[4], current_mac[5]);
998 seq_printf(seq, "Firmware MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
999 firmware_mac[0], firmware_mac[1], firmware_mac[2],
1000 firmware_mac[3], firmware_mac[4], firmware_mac[5]);
1001
1002 seq_printf(seq, "\nAdapter Statistics:\n");
1003 seq_printf(seq, " TX: skbuffs linearized: %ld\n", adapter->tx_linearized);
1004 seq_printf(seq, " multi-descriptor sends: %ld\n", adapter->tx_multidesc_send);
1005 seq_printf(seq, " skb_linearize failures: %ld\n", adapter->tx_linearize_failed);
1006 seq_printf(seq, " vio_map_single failres: %ld\n", adapter->tx_map_failed);
1007 seq_printf(seq, " send failures: %ld\n", adapter->tx_send_failed);
1008 seq_printf(seq, " RX: replenish task cycles: %ld\n", adapter->replenish_task_cycles);
1009 seq_printf(seq, " alloc_skb_failures: %ld\n", adapter->replenish_no_mem);
1010 seq_printf(seq, " add buffer failures: %ld\n", adapter->replenish_add_buff_failure);
1011 seq_printf(seq, " invalid buffers: %ld\n", adapter->rx_invalid_buffer);
1012 seq_printf(seq, " no buffers: %ld\n", adapter->rx_no_buffer);
1013
1014 return 0;
1015 }
1016 static struct seq_operations ibmveth_seq_ops = {
1017 .start = ibmveth_seq_start,
1018 .next = ibmveth_seq_next,
1019 .stop = ibmveth_seq_stop,
1020 .show = ibmveth_seq_show,
1021 };
1022
ibmveth_proc_open(struct inode * inode,struct file * file)1023 static int ibmveth_proc_open(struct inode *inode, struct file *file)
1024 {
1025 struct seq_file *seq;
1026 struct proc_dir_entry *proc;
1027 int rc;
1028
1029 rc = seq_open(file, &ibmveth_seq_ops);
1030 if (!rc) {
1031 /* recover the pointer buried in proc_dir_entry data */
1032 seq = file->private_data;
1033 proc = PDE(inode);
1034 seq->private = proc->data;
1035 }
1036 return rc;
1037 }
1038
1039 static struct file_operations ibmveth_proc_fops = {
1040 .owner = THIS_MODULE,
1041 .open = ibmveth_proc_open,
1042 .read = seq_read,
1043 .llseek = seq_lseek,
1044 .release = seq_release,
1045 };
1046
ibmveth_proc_register_adapter(struct ibmveth_adapter * adapter)1047 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1048 {
1049 struct proc_dir_entry *entry;
1050 if (ibmveth_proc_dir) {
1051 entry = create_proc_entry(adapter->netdev->name, S_IFREG, ibmveth_proc_dir);
1052 if (!entry) {
1053 ibmveth_error_printk("Cannot create adapter proc entry");
1054 } else {
1055 entry->data = (void *) adapter;
1056 entry->proc_fops = &ibmveth_proc_fops;
1057 SET_MODULE_OWNER(entry);
1058 }
1059 }
1060 return;
1061 }
1062
ibmveth_proc_unregister_adapter(struct ibmveth_adapter * adapter)1063 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1064 {
1065 if (ibmveth_proc_dir) {
1066 remove_proc_entry(adapter->netdev->name, ibmveth_proc_dir);
1067 }
1068 }
1069
1070 #else /* CONFIG_PROC_FS */
ibmveth_proc_register_adapter(struct ibmveth_adapter * adapter)1071 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1072 {
1073 }
1074
ibmveth_proc_unregister_adapter(struct ibmveth_adapter * adapter)1075 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1076 {
1077 }
ibmveth_proc_register_driver(void)1078 static void ibmveth_proc_register_driver(void)
1079 {
1080 }
1081
ibmveth_proc_unregister_driver(void)1082 static void ibmveth_proc_unregister_driver(void)
1083 {
1084 }
1085 #endif /* CONFIG_PROC_FS */
1086
1087 static struct vio_device_id ibmveth_device_table[] __devinitdata= {
1088 { "network", "IBM,l-lan"},
1089 { 0,}
1090 };
1091
1092 MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1093
1094 static struct vio_driver ibmveth_driver = {
1095 .name = (char *)ibmveth_driver_name,
1096 .id_table = ibmveth_device_table,
1097 .probe = ibmveth_probe,
1098 .remove = ibmveth_remove
1099 };
1100
ibmveth_module_init(void)1101 static int __init ibmveth_module_init(void)
1102 {
1103 int rc;
1104
1105 ibmveth_printk("%s: %s %s\n", ibmveth_driver_name, ibmveth_driver_string, ibmveth_driver_version);
1106
1107 ibmveth_proc_register_driver();
1108
1109 rc = vio_module_init(&ibmveth_driver);
1110
1111 return rc;
1112 }
1113
ibmveth_module_exit(void)1114 static void __exit ibmveth_module_exit(void)
1115 {
1116 vio_unregister_driver(&ibmveth_driver);
1117 ibmveth_proc_unregister_driver();
1118 }
1119
1120 module_init(ibmveth_module_init);
1121 module_exit(ibmveth_module_exit);
1122