ibmveth: Add ethtool driver stats hooks
[linux-2.6.git] / drivers / net / ibmveth.c
1 /**************************************************************************/
2 /*                                                                        */
3 /* IBM eServer i/pSeries Virtual Ethernet Device Driver                   */
4 /* Copyright (C) 2003 IBM Corp.                                           */
5 /*  Originally written by Dave Larson (larson1@us.ibm.com)                */
6 /*  Maintained by Santiago Leon (santil@us.ibm.com)                       */
7 /*                                                                        */
8 /*  This program is free software; you can redistribute it and/or modify  */
9 /*  it under the terms of the GNU General Public License as published by  */
10 /*  the Free Software Foundation; either version 2 of the License, or     */
11 /*  (at your option) any later version.                                   */
12 /*                                                                        */
13 /*  This program is distributed in the hope that it will be useful,       */
14 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of        */
15 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
16 /*  GNU General Public License for more details.                          */
17 /*                                                                        */
18 /*  You should have received a copy of the GNU General Public License     */
19 /*  along with this program; if not, write to the Free Software           */
20 /*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  */
21 /*                                                                   USA  */
22 /*                                                                        */
23 /* This module contains the implementation of a virtual ethernet device   */
24 /* for use with IBM i/pSeries LPAR Linux.  It utilizes the logical LAN    */
25 /* option of the RS/6000 Platform Architechture to interface with virtual */
26 /* ethernet NICs that are presented to the partition by the hypervisor.   */
27 /*                                                                        */
28 /**************************************************************************/
29 /*
30   TODO:
31   - remove frag processing code - no longer needed
32   - add support for sysfs
33   - possibly remove procfs support
34 */
35
36 #include <linux/module.h>
37 #include <linux/types.h>
38 #include <linux/errno.h>
39 #include <linux/ioport.h>
40 #include <linux/dma-mapping.h>
41 #include <linux/kernel.h>
42 #include <linux/netdevice.h>
43 #include <linux/etherdevice.h>
44 #include <linux/skbuff.h>
45 #include <linux/init.h>
46 #include <linux/delay.h>
47 #include <linux/mm.h>
48 #include <linux/ethtool.h>
49 #include <linux/proc_fs.h>
50 #include <linux/in.h>
51 #include <linux/ip.h>
52 #include <net/net_namespace.h>
53 #include <asm/semaphore.h>
54 #include <asm/hvcall.h>
55 #include <asm/atomic.h>
56 #include <asm/vio.h>
57 #include <asm/uaccess.h>
58 #include <linux/seq_file.h>
59
60 #include "ibmveth.h"
61
62 #undef DEBUG
63
64 #define ibmveth_printk(fmt, args...) \
65   printk(KERN_DEBUG "%s: " fmt, __FILE__, ## args)
66
67 #define ibmveth_error_printk(fmt, args...) \
68   printk(KERN_ERR "(%s:%3.3d ua:%x) ERROR: " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
69
70 #ifdef DEBUG
71 #define ibmveth_debug_printk_no_adapter(fmt, args...) \
72   printk(KERN_DEBUG "(%s:%3.3d): " fmt, __FILE__, __LINE__ , ## args)
73 #define ibmveth_debug_printk(fmt, args...) \
74   printk(KERN_DEBUG "(%s:%3.3d ua:%x): " fmt, __FILE__, __LINE__ , adapter->vdev->unit_address, ## args)
75 #define ibmveth_assert(expr) \
76   if(!(expr)) {                                   \
77     printk(KERN_DEBUG "assertion failed (%s:%3.3d ua:%x): %s\n", __FILE__, __LINE__, adapter->vdev->unit_address, #expr); \
78     BUG(); \
79   }
80 #else
81 #define ibmveth_debug_printk_no_adapter(fmt, args...)
82 #define ibmveth_debug_printk(fmt, args...)
83 #define ibmveth_assert(expr)
84 #endif
85
86 static int ibmveth_open(struct net_device *dev);
87 static int ibmveth_close(struct net_device *dev);
88 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
89 static int ibmveth_poll(struct napi_struct *napi, int budget);
90 static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *dev);
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 int ibmveth_change_mtu(struct net_device *dev, int new_mtu);
94 static void ibmveth_proc_register_driver(void);
95 static void ibmveth_proc_unregister_driver(void);
96 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter);
97 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter);
98 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
99 static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
100 static struct kobj_type ktype_veth_pool;
101
102 #ifdef CONFIG_PROC_FS
103 #define IBMVETH_PROC_DIR "ibmveth"
104 static struct proc_dir_entry *ibmveth_proc_dir;
105 #endif
106
107 static const char ibmveth_driver_name[] = "ibmveth";
108 static const char ibmveth_driver_string[] = "IBM i/pSeries Virtual Ethernet Driver";
109 #define ibmveth_driver_version "1.03"
110
111 MODULE_AUTHOR("Santiago Leon <santil@us.ibm.com>");
112 MODULE_DESCRIPTION("IBM i/pSeries Virtual Ethernet Driver");
113 MODULE_LICENSE("GPL");
114 MODULE_VERSION(ibmveth_driver_version);
115
116 struct ibmveth_stat {
117         char name[ETH_GSTRING_LEN];
118         int offset;
119 };
120
121 #define IBMVETH_STAT_OFF(stat) offsetof(struct ibmveth_adapter, stat)
122 #define IBMVETH_GET_STAT(a, off) *((u64 *)(((unsigned long)(a)) + off))
123
124 struct ibmveth_stat ibmveth_stats[] = {
125         { "replenish_task_cycles", IBMVETH_STAT_OFF(replenish_task_cycles) },
126         { "replenish_no_mem", IBMVETH_STAT_OFF(replenish_no_mem) },
127         { "replenish_add_buff_failure", IBMVETH_STAT_OFF(replenish_add_buff_failure) },
128         { "replenish_add_buff_success", IBMVETH_STAT_OFF(replenish_add_buff_success) },
129         { "rx_invalid_buffer", IBMVETH_STAT_OFF(rx_invalid_buffer) },
130         { "rx_no_buffer", IBMVETH_STAT_OFF(rx_no_buffer) },
131         { "tx_multidesc_send", IBMVETH_STAT_OFF(tx_multidesc_send) },
132         { "tx_linearized", IBMVETH_STAT_OFF(tx_linearized) },
133         { "tx_linearize_failed", IBMVETH_STAT_OFF(tx_linearize_failed) },
134         { "tx_map_failed", IBMVETH_STAT_OFF(tx_map_failed) },
135         { "tx_send_failed", IBMVETH_STAT_OFF(tx_send_failed) },
136 };
137
138 /* simple methods of getting data from the current rxq entry */
139 static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
140 {
141         return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].toggle == adapter->rx_queue.toggle);
142 }
143
144 static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
145 {
146         return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].valid);
147 }
148
149 static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
150 {
151         return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].offset);
152 }
153
154 static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
155 {
156         return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
157 }
158
159 static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
160 {
161         return (adapter->rx_queue.queue_addr[adapter->rx_queue.index].csum_good);
162 }
163
164 /* setup the initial settings for a buffer pool */
165 static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool, u32 pool_index, u32 pool_size, u32 buff_size, u32 pool_active)
166 {
167         pool->size = pool_size;
168         pool->index = pool_index;
169         pool->buff_size = buff_size;
170         pool->threshold = pool_size / 2;
171         pool->active = pool_active;
172 }
173
174 /* allocate and setup an buffer pool - called during open */
175 static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
176 {
177         int i;
178
179         pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
180
181         if(!pool->free_map) {
182                 return -1;
183         }
184
185         pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
186         if(!pool->dma_addr) {
187                 kfree(pool->free_map);
188                 pool->free_map = NULL;
189                 return -1;
190         }
191
192         pool->skbuff = kmalloc(sizeof(void*) * pool->size, GFP_KERNEL);
193
194         if(!pool->skbuff) {
195                 kfree(pool->dma_addr);
196                 pool->dma_addr = NULL;
197
198                 kfree(pool->free_map);
199                 pool->free_map = NULL;
200                 return -1;
201         }
202
203         memset(pool->skbuff, 0, sizeof(void*) * pool->size);
204         memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
205
206         for(i = 0; i < pool->size; ++i) {
207                 pool->free_map[i] = i;
208         }
209
210         atomic_set(&pool->available, 0);
211         pool->producer_index = 0;
212         pool->consumer_index = 0;
213
214         return 0;
215 }
216
217 /* replenish the buffers for a pool.  note that we don't need to
218  * skb_reserve these since they are used for incoming...
219  */
220 static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
221 {
222         u32 i;
223         u32 count = pool->size - atomic_read(&pool->available);
224         u32 buffers_added = 0;
225
226         mb();
227
228         for(i = 0; i < count; ++i) {
229                 struct sk_buff *skb;
230                 unsigned int free_index, index;
231                 u64 correlator;
232                 union ibmveth_buf_desc desc;
233                 unsigned long lpar_rc;
234                 dma_addr_t dma_addr;
235
236                 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
237
238                 if(!skb) {
239                         ibmveth_debug_printk("replenish: unable to allocate skb\n");
240                         adapter->replenish_no_mem++;
241                         break;
242                 }
243
244                 free_index = pool->consumer_index;
245                 pool->consumer_index = (pool->consumer_index + 1) % pool->size;
246                 index = pool->free_map[free_index];
247
248                 ibmveth_assert(index != IBM_VETH_INVALID_MAP);
249                 ibmveth_assert(pool->skbuff[index] == NULL);
250
251                 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
252                                 pool->buff_size, DMA_FROM_DEVICE);
253
254                 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
255                 pool->dma_addr[index] = dma_addr;
256                 pool->skbuff[index] = skb;
257
258                 correlator = ((u64)pool->index << 32) | index;
259                 *(u64*)skb->data = correlator;
260
261                 desc.desc = 0;
262                 desc.fields.valid = 1;
263                 desc.fields.length = pool->buff_size;
264                 desc.fields.address = dma_addr;
265
266                 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
267
268                 if(lpar_rc != H_SUCCESS) {
269                         pool->free_map[free_index] = index;
270                         pool->skbuff[index] = NULL;
271                         if (pool->consumer_index == 0)
272                                 pool->consumer_index = pool->size - 1;
273                         else
274                                 pool->consumer_index--;
275                         dma_unmap_single(&adapter->vdev->dev,
276                                         pool->dma_addr[index], pool->buff_size,
277                                         DMA_FROM_DEVICE);
278                         dev_kfree_skb_any(skb);
279                         adapter->replenish_add_buff_failure++;
280                         break;
281                 } else {
282                         buffers_added++;
283                         adapter->replenish_add_buff_success++;
284                 }
285         }
286
287         mb();
288         atomic_add(buffers_added, &(pool->available));
289 }
290
291 /* replenish routine */
292 static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
293 {
294         int i;
295
296         adapter->replenish_task_cycles++;
297
298         for(i = 0; i < IbmVethNumBufferPools; i++)
299                 if(adapter->rx_buff_pool[i].active)
300                         ibmveth_replenish_buffer_pool(adapter,
301                                                      &adapter->rx_buff_pool[i]);
302
303         adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
304 }
305
306 /* empty and free ana buffer pool - also used to do cleanup in error paths */
307 static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter, struct ibmveth_buff_pool *pool)
308 {
309         int i;
310
311         kfree(pool->free_map);
312         pool->free_map = NULL;
313
314         if(pool->skbuff && pool->dma_addr) {
315                 for(i = 0; i < pool->size; ++i) {
316                         struct sk_buff *skb = pool->skbuff[i];
317                         if(skb) {
318                                 dma_unmap_single(&adapter->vdev->dev,
319                                                  pool->dma_addr[i],
320                                                  pool->buff_size,
321                                                  DMA_FROM_DEVICE);
322                                 dev_kfree_skb_any(skb);
323                                 pool->skbuff[i] = NULL;
324                         }
325                 }
326         }
327
328         if(pool->dma_addr) {
329                 kfree(pool->dma_addr);
330                 pool->dma_addr = NULL;
331         }
332
333         if(pool->skbuff) {
334                 kfree(pool->skbuff);
335                 pool->skbuff = NULL;
336         }
337 }
338
339 /* remove a buffer from a pool */
340 static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter, u64 correlator)
341 {
342         unsigned int pool  = correlator >> 32;
343         unsigned int index = correlator & 0xffffffffUL;
344         unsigned int free_index;
345         struct sk_buff *skb;
346
347         ibmveth_assert(pool < IbmVethNumBufferPools);
348         ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
349
350         skb = adapter->rx_buff_pool[pool].skbuff[index];
351
352         ibmveth_assert(skb != NULL);
353
354         adapter->rx_buff_pool[pool].skbuff[index] = NULL;
355
356         dma_unmap_single(&adapter->vdev->dev,
357                          adapter->rx_buff_pool[pool].dma_addr[index],
358                          adapter->rx_buff_pool[pool].buff_size,
359                          DMA_FROM_DEVICE);
360
361         free_index = adapter->rx_buff_pool[pool].producer_index;
362         adapter->rx_buff_pool[pool].producer_index
363                 = (adapter->rx_buff_pool[pool].producer_index + 1)
364                 % adapter->rx_buff_pool[pool].size;
365         adapter->rx_buff_pool[pool].free_map[free_index] = index;
366
367         mb();
368
369         atomic_dec(&(adapter->rx_buff_pool[pool].available));
370 }
371
372 /* get the current buffer on the rx queue */
373 static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
374 {
375         u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
376         unsigned int pool = correlator >> 32;
377         unsigned int index = correlator & 0xffffffffUL;
378
379         ibmveth_assert(pool < IbmVethNumBufferPools);
380         ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
381
382         return adapter->rx_buff_pool[pool].skbuff[index];
383 }
384
385 /* recycle the current buffer on the rx queue */
386 static void ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
387 {
388         u32 q_index = adapter->rx_queue.index;
389         u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
390         unsigned int pool = correlator >> 32;
391         unsigned int index = correlator & 0xffffffffUL;
392         union ibmveth_buf_desc desc;
393         unsigned long lpar_rc;
394
395         ibmveth_assert(pool < IbmVethNumBufferPools);
396         ibmveth_assert(index < adapter->rx_buff_pool[pool].size);
397
398         if(!adapter->rx_buff_pool[pool].active) {
399                 ibmveth_rxq_harvest_buffer(adapter);
400                 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
401                 return;
402         }
403
404         desc.desc = 0;
405         desc.fields.valid = 1;
406         desc.fields.length = adapter->rx_buff_pool[pool].buff_size;
407         desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
408
409         lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
410
411         if(lpar_rc != H_SUCCESS) {
412                 ibmveth_debug_printk("h_add_logical_lan_buffer failed during recycle rc=%ld", lpar_rc);
413                 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
414         }
415
416         if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
417                 adapter->rx_queue.index = 0;
418                 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
419         }
420 }
421
422 static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
423 {
424         ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
425
426         if(++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
427                 adapter->rx_queue.index = 0;
428                 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
429         }
430 }
431
432 static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
433 {
434         int i;
435
436         if(adapter->buffer_list_addr != NULL) {
437                 if(!dma_mapping_error(adapter->buffer_list_dma)) {
438                         dma_unmap_single(&adapter->vdev->dev,
439                                         adapter->buffer_list_dma, 4096,
440                                         DMA_BIDIRECTIONAL);
441                         adapter->buffer_list_dma = DMA_ERROR_CODE;
442                 }
443                 free_page((unsigned long)adapter->buffer_list_addr);
444                 adapter->buffer_list_addr = NULL;
445         }
446
447         if(adapter->filter_list_addr != NULL) {
448                 if(!dma_mapping_error(adapter->filter_list_dma)) {
449                         dma_unmap_single(&adapter->vdev->dev,
450                                         adapter->filter_list_dma, 4096,
451                                         DMA_BIDIRECTIONAL);
452                         adapter->filter_list_dma = DMA_ERROR_CODE;
453                 }
454                 free_page((unsigned long)adapter->filter_list_addr);
455                 adapter->filter_list_addr = NULL;
456         }
457
458         if(adapter->rx_queue.queue_addr != NULL) {
459                 if(!dma_mapping_error(adapter->rx_queue.queue_dma)) {
460                         dma_unmap_single(&adapter->vdev->dev,
461                                         adapter->rx_queue.queue_dma,
462                                         adapter->rx_queue.queue_len,
463                                         DMA_BIDIRECTIONAL);
464                         adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
465                 }
466                 kfree(adapter->rx_queue.queue_addr);
467                 adapter->rx_queue.queue_addr = NULL;
468         }
469
470         for(i = 0; i<IbmVethNumBufferPools; i++)
471                 if (adapter->rx_buff_pool[i].active)
472                         ibmveth_free_buffer_pool(adapter,
473                                                  &adapter->rx_buff_pool[i]);
474 }
475
476 static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
477         union ibmveth_buf_desc rxq_desc, u64 mac_address)
478 {
479         int rc, try_again = 1;
480
481         /* After a kexec the adapter will still be open, so our attempt to
482         * open it will fail. So if we get a failure we free the adapter and
483         * try again, but only once. */
484 retry:
485         rc = h_register_logical_lan(adapter->vdev->unit_address,
486                                     adapter->buffer_list_dma, rxq_desc.desc,
487                                     adapter->filter_list_dma, mac_address);
488
489         if (rc != H_SUCCESS && try_again) {
490                 do {
491                         rc = h_free_logical_lan(adapter->vdev->unit_address);
492                 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
493
494                 try_again = 0;
495                 goto retry;
496         }
497
498         return rc;
499 }
500
501 static int ibmveth_open(struct net_device *netdev)
502 {
503         struct ibmveth_adapter *adapter = netdev->priv;
504         u64 mac_address = 0;
505         int rxq_entries = 1;
506         unsigned long lpar_rc;
507         int rc;
508         union ibmveth_buf_desc rxq_desc;
509         int i;
510
511         ibmveth_debug_printk("open starting\n");
512
513         napi_enable(&adapter->napi);
514
515         for(i = 0; i<IbmVethNumBufferPools; i++)
516                 rxq_entries += adapter->rx_buff_pool[i].size;
517
518         adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
519         adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
520
521         if(!adapter->buffer_list_addr || !adapter->filter_list_addr) {
522                 ibmveth_error_printk("unable to allocate filter or buffer list pages\n");
523                 ibmveth_cleanup(adapter);
524                 napi_disable(&adapter->napi);
525                 return -ENOMEM;
526         }
527
528         adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) * rxq_entries;
529         adapter->rx_queue.queue_addr = kmalloc(adapter->rx_queue.queue_len, GFP_KERNEL);
530
531         if(!adapter->rx_queue.queue_addr) {
532                 ibmveth_error_printk("unable to allocate rx queue pages\n");
533                 ibmveth_cleanup(adapter);
534                 napi_disable(&adapter->napi);
535                 return -ENOMEM;
536         }
537
538         adapter->buffer_list_dma = dma_map_single(&adapter->vdev->dev,
539                         adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
540         adapter->filter_list_dma = dma_map_single(&adapter->vdev->dev,
541                         adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
542         adapter->rx_queue.queue_dma = dma_map_single(&adapter->vdev->dev,
543                         adapter->rx_queue.queue_addr,
544                         adapter->rx_queue.queue_len, DMA_BIDIRECTIONAL);
545
546         if((dma_mapping_error(adapter->buffer_list_dma) ) ||
547            (dma_mapping_error(adapter->filter_list_dma)) ||
548            (dma_mapping_error(adapter->rx_queue.queue_dma))) {
549                 ibmveth_error_printk("unable to map filter or buffer list pages\n");
550                 ibmveth_cleanup(adapter);
551                 napi_disable(&adapter->napi);
552                 return -ENOMEM;
553         }
554
555         adapter->rx_queue.index = 0;
556         adapter->rx_queue.num_slots = rxq_entries;
557         adapter->rx_queue.toggle = 1;
558
559         memcpy(&mac_address, netdev->dev_addr, netdev->addr_len);
560         mac_address = mac_address >> 16;
561
562         rxq_desc.desc = 0;
563         rxq_desc.fields.valid = 1;
564         rxq_desc.fields.length = adapter->rx_queue.queue_len;
565         rxq_desc.fields.address = adapter->rx_queue.queue_dma;
566
567         ibmveth_debug_printk("buffer list @ 0x%p\n", adapter->buffer_list_addr);
568         ibmveth_debug_printk("filter list @ 0x%p\n", adapter->filter_list_addr);
569         ibmveth_debug_printk("receive q   @ 0x%p\n", adapter->rx_queue.queue_addr);
570
571         h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
572
573         lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
574
575         if(lpar_rc != H_SUCCESS) {
576                 ibmveth_error_printk("h_register_logical_lan failed with %ld\n", lpar_rc);
577                 ibmveth_error_printk("buffer TCE:0x%lx filter TCE:0x%lx rxq desc:0x%lx MAC:0x%lx\n",
578                                      adapter->buffer_list_dma,
579                                      adapter->filter_list_dma,
580                                      rxq_desc.desc,
581                                      mac_address);
582                 ibmveth_cleanup(adapter);
583                 napi_disable(&adapter->napi);
584                 return -ENONET;
585         }
586
587         for(i = 0; i<IbmVethNumBufferPools; i++) {
588                 if(!adapter->rx_buff_pool[i].active)
589                         continue;
590                 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
591                         ibmveth_error_printk("unable to alloc pool\n");
592                         adapter->rx_buff_pool[i].active = 0;
593                         ibmveth_cleanup(adapter);
594                         napi_disable(&adapter->napi);
595                         return -ENOMEM ;
596                 }
597         }
598
599         ibmveth_debug_printk("registering irq 0x%x\n", netdev->irq);
600         if((rc = request_irq(netdev->irq, &ibmveth_interrupt, 0, netdev->name, netdev)) != 0) {
601                 ibmveth_error_printk("unable to request irq 0x%x, rc %d\n", netdev->irq, rc);
602                 do {
603                         rc = h_free_logical_lan(adapter->vdev->unit_address);
604                 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
605
606                 ibmveth_cleanup(adapter);
607                 napi_disable(&adapter->napi);
608                 return rc;
609         }
610
611         ibmveth_debug_printk("initial replenish cycle\n");
612         ibmveth_interrupt(netdev->irq, netdev);
613
614         netif_start_queue(netdev);
615
616         ibmveth_debug_printk("open complete\n");
617
618         return 0;
619 }
620
621 static int ibmveth_close(struct net_device *netdev)
622 {
623         struct ibmveth_adapter *adapter = netdev->priv;
624         long lpar_rc;
625
626         ibmveth_debug_printk("close starting\n");
627
628         napi_disable(&adapter->napi);
629
630         if (!adapter->pool_config)
631                 netif_stop_queue(netdev);
632
633         free_irq(netdev->irq, netdev);
634
635         do {
636                 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
637         } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
638
639         if(lpar_rc != H_SUCCESS)
640         {
641                 ibmveth_error_printk("h_free_logical_lan failed with %lx, continuing with close\n",
642                                      lpar_rc);
643         }
644
645         adapter->rx_no_buffer = *(u64*)(((char*)adapter->buffer_list_addr) + 4096 - 8);
646
647         ibmveth_cleanup(adapter);
648
649         ibmveth_debug_printk("close complete\n");
650
651         return 0;
652 }
653
654 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) {
655         cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
656         cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg | ADVERTISED_FIBRE);
657         cmd->speed = SPEED_1000;
658         cmd->duplex = DUPLEX_FULL;
659         cmd->port = PORT_FIBRE;
660         cmd->phy_address = 0;
661         cmd->transceiver = XCVR_INTERNAL;
662         cmd->autoneg = AUTONEG_ENABLE;
663         cmd->maxtxpkt = 0;
664         cmd->maxrxpkt = 1;
665         return 0;
666 }
667
668 static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info) {
669         strncpy(info->driver, ibmveth_driver_name, sizeof(info->driver) - 1);
670         strncpy(info->version, ibmveth_driver_version, sizeof(info->version) - 1);
671 }
672
673 static u32 netdev_get_link(struct net_device *dev) {
674         return 1;
675 }
676
677 static void ibmveth_set_rx_csum_flags(struct net_device *dev, u32 data)
678 {
679         struct ibmveth_adapter *adapter = dev->priv;
680
681         if (data)
682                 adapter->rx_csum = 1;
683         else {
684                 /*
685                  * Since the ibmveth firmware interface does not have the concept of
686                  * separate tx/rx checksum offload enable, if rx checksum is disabled
687                  * we also have to disable tx checksum offload. Once we disable rx
688                  * checksum offload, we are no longer allowed to send tx buffers that
689                  * are not properly checksummed.
690                  */
691                 adapter->rx_csum = 0;
692                 dev->features &= ~NETIF_F_IP_CSUM;
693         }
694 }
695
696 static void ibmveth_set_tx_csum_flags(struct net_device *dev, u32 data)
697 {
698         struct ibmveth_adapter *adapter = dev->priv;
699
700         if (data) {
701                 dev->features |= NETIF_F_IP_CSUM;
702                 adapter->rx_csum = 1;
703         } else
704                 dev->features &= ~NETIF_F_IP_CSUM;
705 }
706
707 static int ibmveth_set_csum_offload(struct net_device *dev, u32 data,
708                                     void (*done) (struct net_device *, u32))
709 {
710         struct ibmveth_adapter *adapter = dev->priv;
711         union ibmveth_illan_attributes set_attr, clr_attr, ret_attr;
712         long ret;
713         int rc1 = 0, rc2 = 0;
714         int restart = 0;
715
716         if (netif_running(dev)) {
717                 restart = 1;
718                 adapter->pool_config = 1;
719                 ibmveth_close(dev);
720                 adapter->pool_config = 0;
721         }
722
723         set_attr.desc = 0;
724         clr_attr.desc = 0;
725
726         if (data)
727                 set_attr.fields.tcp_csum_offload_ipv4 = 1;
728         else
729                 clr_attr.fields.tcp_csum_offload_ipv4 = 1;
730
731         ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr.desc);
732
733         if (ret == H_SUCCESS && !ret_attr.fields.active_trunk &&
734             !ret_attr.fields.trunk_priority &&
735             ret_attr.fields.csum_offload_padded_pkt_support) {
736                 ret = h_illan_attributes(adapter->vdev->unit_address, clr_attr.desc,
737                                          set_attr.desc, &ret_attr.desc);
738
739                 if (ret != H_SUCCESS) {
740                         rc1 = -EIO;
741                         ibmveth_error_printk("unable to change checksum offload settings."
742                                              " %d rc=%ld\n", data, ret);
743
744                         ret = h_illan_attributes(adapter->vdev->unit_address,
745                                                  set_attr.desc, clr_attr.desc, &ret_attr.desc);
746                 } else
747                         done(dev, data);
748         } else {
749                 rc1 = -EIO;
750                 ibmveth_error_printk("unable to change checksum offload settings."
751                                      " %d rc=%ld ret_attr=%lx\n", data, ret, ret_attr.desc);
752         }
753
754         if (restart)
755                 rc2 = ibmveth_open(dev);
756
757         return rc1 ? rc1 : rc2;
758 }
759
760 static int ibmveth_set_rx_csum(struct net_device *dev, u32 data)
761 {
762         struct ibmveth_adapter *adapter = dev->priv;
763
764         if ((data && adapter->rx_csum) || (!data && !adapter->rx_csum))
765                 return 0;
766
767         return ibmveth_set_csum_offload(dev, data, ibmveth_set_rx_csum_flags);
768 }
769
770 static int ibmveth_set_tx_csum(struct net_device *dev, u32 data)
771 {
772         struct ibmveth_adapter *adapter = dev->priv;
773         int rc = 0;
774
775         if (data && (dev->features & NETIF_F_IP_CSUM))
776                 return 0;
777         if (!data && !(dev->features & NETIF_F_IP_CSUM))
778                 return 0;
779
780         if (data && !adapter->rx_csum)
781                 rc = ibmveth_set_csum_offload(dev, data, ibmveth_set_tx_csum_flags);
782         else
783                 ibmveth_set_tx_csum_flags(dev, data);
784
785         return rc;
786 }
787
788 static u32 ibmveth_get_rx_csum(struct net_device *dev)
789 {
790         struct ibmveth_adapter *adapter = dev->priv;
791         return adapter->rx_csum;
792 }
793
794 static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
795 {
796         int i;
797
798         if (stringset != ETH_SS_STATS)
799                 return;
800
801         for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
802                 memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
803 }
804
805 static int ibmveth_get_stats_count(struct net_device *dev)
806 {
807         return ARRAY_SIZE(ibmveth_stats);
808 }
809
810 static void ibmveth_get_ethtool_stats(struct net_device *dev,
811                                       struct ethtool_stats *stats, u64 *data)
812 {
813         int i;
814         struct ibmveth_adapter *adapter = dev->priv;
815
816         for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
817                 data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
818 }
819
820 static const struct ethtool_ops netdev_ethtool_ops = {
821         .get_drvinfo            = netdev_get_drvinfo,
822         .get_settings           = netdev_get_settings,
823         .get_link               = netdev_get_link,
824         .get_sg                 = ethtool_op_get_sg,
825         .get_tx_csum            = ethtool_op_get_tx_csum,
826         .set_tx_csum            = ibmveth_set_tx_csum,
827         .get_rx_csum            = ibmveth_get_rx_csum,
828         .set_rx_csum            = ibmveth_set_rx_csum,
829         .get_tso                        = ethtool_op_get_tso,
830         .get_ufo                        = ethtool_op_get_ufo,
831         .get_strings            = ibmveth_get_strings,
832         .get_stats_count                = ibmveth_get_stats_count,
833         .get_ethtool_stats      = ibmveth_get_ethtool_stats,
834 };
835
836 static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
837 {
838         return -EOPNOTSUPP;
839 }
840
841 #define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
842
843 static int ibmveth_start_xmit(struct sk_buff *skb, struct net_device *netdev)
844 {
845         struct ibmveth_adapter *adapter = netdev->priv;
846         union ibmveth_buf_desc desc[IbmVethMaxSendFrags];
847         unsigned long lpar_rc;
848         int nfrags = 0, curfrag;
849         unsigned long correlator;
850         unsigned long flags;
851         unsigned int retry_count;
852         unsigned int tx_dropped = 0;
853         unsigned int tx_bytes = 0;
854         unsigned int tx_packets = 0;
855         unsigned int tx_send_failed = 0;
856         unsigned int tx_map_failed = 0;
857
858
859         if ((skb_shinfo(skb)->nr_frags + 1) > IbmVethMaxSendFrags) {
860                 tx_dropped++;
861                 goto out;
862         }
863
864         memset(&desc, 0, sizeof(desc));
865
866         /* nfrags = number of frags after the initial fragment */
867         nfrags = skb_shinfo(skb)->nr_frags;
868
869         if(nfrags)
870                 adapter->tx_multidesc_send++;
871
872         /* map the initial fragment */
873         desc[0].fields.length  = nfrags ? skb->len - skb->data_len : skb->len;
874         desc[0].fields.address = dma_map_single(&adapter->vdev->dev, skb->data,
875                                         desc[0].fields.length, DMA_TO_DEVICE);
876         desc[0].fields.valid   = 1;
877
878         if (skb->ip_summed == CHECKSUM_PARTIAL &&
879             ip_hdr(skb)->protocol != IPPROTO_TCP && skb_checksum_help(skb)) {
880                 ibmveth_error_printk("tx: failed to checksum packet\n");
881                 tx_dropped++;
882                 goto out;
883         }
884
885         if (skb->ip_summed == CHECKSUM_PARTIAL) {
886                 unsigned char *buf = skb_transport_header(skb) + skb->csum_offset;
887
888                 desc[0].fields.no_csum = 1;
889                 desc[0].fields.csum_good = 1;
890
891                 /* Need to zero out the checksum */
892                 buf[0] = 0;
893                 buf[1] = 0;
894         }
895
896         if(dma_mapping_error(desc[0].fields.address)) {
897                 ibmveth_error_printk("tx: unable to map initial fragment\n");
898                 tx_map_failed++;
899                 tx_dropped++;
900                 goto out;
901         }
902
903         curfrag = nfrags;
904
905         /* map fragments past the initial portion if there are any */
906         while(curfrag--) {
907                 skb_frag_t *frag = &skb_shinfo(skb)->frags[curfrag];
908                 desc[curfrag+1].fields.address
909                         = dma_map_single(&adapter->vdev->dev,
910                                 page_address(frag->page) + frag->page_offset,
911                                 frag->size, DMA_TO_DEVICE);
912                 desc[curfrag+1].fields.length = frag->size;
913                 desc[curfrag+1].fields.valid  = 1;
914                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
915                         desc[curfrag+1].fields.no_csum = 1;
916                         desc[curfrag+1].fields.csum_good = 1;
917                 }
918
919                 if(dma_mapping_error(desc[curfrag+1].fields.address)) {
920                         ibmveth_error_printk("tx: unable to map fragment %d\n", curfrag);
921                         tx_map_failed++;
922                         tx_dropped++;
923                         /* Free all the mappings we just created */
924                         while(curfrag < nfrags) {
925                                 dma_unmap_single(&adapter->vdev->dev,
926                                                  desc[curfrag+1].fields.address,
927                                                  desc[curfrag+1].fields.length,
928                                                  DMA_TO_DEVICE);
929                                 curfrag++;
930                         }
931                         goto out;
932                 }
933         }
934
935         /* send the frame. Arbitrarily set retrycount to 1024 */
936         correlator = 0;
937         retry_count = 1024;
938         do {
939                 lpar_rc = h_send_logical_lan(adapter->vdev->unit_address,
940                                              desc[0].desc,
941                                              desc[1].desc,
942                                              desc[2].desc,
943                                              desc[3].desc,
944                                              desc[4].desc,
945                                              desc[5].desc,
946                                              correlator,
947                                              &correlator);
948         } while ((lpar_rc == H_BUSY) && (retry_count--));
949
950         if(lpar_rc != H_SUCCESS && lpar_rc != H_DROPPED) {
951                 int i;
952                 ibmveth_error_printk("tx: h_send_logical_lan failed with rc=%ld\n", lpar_rc);
953                 for(i = 0; i < 6; i++) {
954                         ibmveth_error_printk("tx: desc[%i] valid=%d, len=%d, address=0x%d\n", i,
955                                              desc[i].fields.valid, desc[i].fields.length, desc[i].fields.address);
956                 }
957                 tx_send_failed++;
958                 tx_dropped++;
959         } else {
960                 tx_packets++;
961                 tx_bytes += skb->len;
962                 netdev->trans_start = jiffies;
963         }
964
965         do {
966                 dma_unmap_single(&adapter->vdev->dev,
967                                 desc[nfrags].fields.address,
968                                 desc[nfrags].fields.length, DMA_TO_DEVICE);
969         } while(--nfrags >= 0);
970
971 out:    spin_lock_irqsave(&adapter->stats_lock, flags);
972         adapter->stats.tx_dropped += tx_dropped;
973         adapter->stats.tx_bytes += tx_bytes;
974         adapter->stats.tx_packets += tx_packets;
975         adapter->tx_send_failed += tx_send_failed;
976         adapter->tx_map_failed += tx_map_failed;
977         spin_unlock_irqrestore(&adapter->stats_lock, flags);
978
979         dev_kfree_skb(skb);
980         return 0;
981 }
982
983 static int ibmveth_poll(struct napi_struct *napi, int budget)
984 {
985         struct ibmveth_adapter *adapter = container_of(napi, struct ibmveth_adapter, napi);
986         struct net_device *netdev = adapter->netdev;
987         int frames_processed = 0;
988         unsigned long lpar_rc;
989
990  restart_poll:
991         do {
992                 struct sk_buff *skb;
993
994                 if (!ibmveth_rxq_pending_buffer(adapter))
995                         break;
996
997                 rmb();
998                 if (!ibmveth_rxq_buffer_valid(adapter)) {
999                         wmb(); /* suggested by larson1 */
1000                         adapter->rx_invalid_buffer++;
1001                         ibmveth_debug_printk("recycling invalid buffer\n");
1002                         ibmveth_rxq_recycle_buffer(adapter);
1003                 } else {
1004                         int length = ibmveth_rxq_frame_length(adapter);
1005                         int offset = ibmveth_rxq_frame_offset(adapter);
1006                         int csum_good = ibmveth_rxq_csum_good(adapter);
1007
1008                         skb = ibmveth_rxq_get_buffer(adapter);
1009                         if (csum_good)
1010                                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1011
1012                         ibmveth_rxq_harvest_buffer(adapter);
1013
1014                         skb_reserve(skb, offset);
1015                         skb_put(skb, length);
1016                         skb->protocol = eth_type_trans(skb, netdev);
1017
1018                         netif_receive_skb(skb); /* send it up */
1019
1020                         adapter->stats.rx_packets++;
1021                         adapter->stats.rx_bytes += length;
1022                         frames_processed++;
1023                         netdev->last_rx = jiffies;
1024                 }
1025         } while (frames_processed < budget);
1026
1027         ibmveth_replenish_task(adapter);
1028
1029         if (frames_processed < budget) {
1030                 /* We think we are done - reenable interrupts,
1031                  * then check once more to make sure we are done.
1032                  */
1033                 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1034                                        VIO_IRQ_ENABLE);
1035
1036                 ibmveth_assert(lpar_rc == H_SUCCESS);
1037
1038                 netif_rx_complete(netdev, napi);
1039
1040                 if (ibmveth_rxq_pending_buffer(adapter) &&
1041                     netif_rx_reschedule(netdev, napi)) {
1042                         lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1043                                                VIO_IRQ_DISABLE);
1044                         goto restart_poll;
1045                 }
1046         }
1047
1048         return frames_processed;
1049 }
1050
1051 static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
1052 {
1053         struct net_device *netdev = dev_instance;
1054         struct ibmveth_adapter *adapter = netdev->priv;
1055         unsigned long lpar_rc;
1056
1057         if (netif_rx_schedule_prep(netdev, &adapter->napi)) {
1058                 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1059                                        VIO_IRQ_DISABLE);
1060                 ibmveth_assert(lpar_rc == H_SUCCESS);
1061                 __netif_rx_schedule(netdev, &adapter->napi);
1062         }
1063         return IRQ_HANDLED;
1064 }
1065
1066 static struct net_device_stats *ibmveth_get_stats(struct net_device *dev)
1067 {
1068         struct ibmveth_adapter *adapter = dev->priv;
1069         return &adapter->stats;
1070 }
1071
1072 static void ibmveth_set_multicast_list(struct net_device *netdev)
1073 {
1074         struct ibmveth_adapter *adapter = netdev->priv;
1075         unsigned long lpar_rc;
1076
1077         if((netdev->flags & IFF_PROMISC) || (netdev->mc_count > adapter->mcastFilterSize)) {
1078                 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1079                                            IbmVethMcastEnableRecv |
1080                                            IbmVethMcastDisableFiltering,
1081                                            0);
1082                 if(lpar_rc != H_SUCCESS) {
1083                         ibmveth_error_printk("h_multicast_ctrl rc=%ld when entering promisc mode\n", lpar_rc);
1084                 }
1085         } else {
1086                 struct dev_mc_list *mclist = netdev->mc_list;
1087                 int i;
1088                 /* clear the filter table & disable filtering */
1089                 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1090                                            IbmVethMcastEnableRecv |
1091                                            IbmVethMcastDisableFiltering |
1092                                            IbmVethMcastClearFilterTable,
1093                                            0);
1094                 if(lpar_rc != H_SUCCESS) {
1095                         ibmveth_error_printk("h_multicast_ctrl rc=%ld when attempting to clear filter table\n", lpar_rc);
1096                 }
1097                 /* add the addresses to the filter table */
1098                 for(i = 0; i < netdev->mc_count; ++i, mclist = mclist->next) {
1099                         // add the multicast address to the filter table
1100                         unsigned long mcast_addr = 0;
1101                         memcpy(((char *)&mcast_addr)+2, mclist->dmi_addr, 6);
1102                         lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1103                                                    IbmVethMcastAddFilter,
1104                                                    mcast_addr);
1105                         if(lpar_rc != H_SUCCESS) {
1106                                 ibmveth_error_printk("h_multicast_ctrl rc=%ld when adding an entry to the filter table\n", lpar_rc);
1107                         }
1108                 }
1109
1110                 /* re-enable filtering */
1111                 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1112                                            IbmVethMcastEnableFiltering,
1113                                            0);
1114                 if(lpar_rc != H_SUCCESS) {
1115                         ibmveth_error_printk("h_multicast_ctrl rc=%ld when enabling filtering\n", lpar_rc);
1116                 }
1117         }
1118 }
1119
1120 static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
1121 {
1122         struct ibmveth_adapter *adapter = dev->priv;
1123         int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
1124         int reinit = 0;
1125         int i, rc;
1126
1127         if (new_mtu < IBMVETH_MAX_MTU)
1128                 return -EINVAL;
1129
1130         for (i = 0; i < IbmVethNumBufferPools; i++)
1131                 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size)
1132                         break;
1133
1134         if (i == IbmVethNumBufferPools)
1135                 return -EINVAL;
1136
1137         /* Look for an active buffer pool that can hold the new MTU */
1138         for(i = 0; i<IbmVethNumBufferPools; i++) {
1139                 if (!adapter->rx_buff_pool[i].active) {
1140                         adapter->rx_buff_pool[i].active = 1;
1141                         reinit = 1;
1142                 }
1143
1144                 if (new_mtu_oh < adapter->rx_buff_pool[i].buff_size) {
1145                         if (reinit && netif_running(adapter->netdev)) {
1146                                 adapter->pool_config = 1;
1147                                 ibmveth_close(adapter->netdev);
1148                                 adapter->pool_config = 0;
1149                                 dev->mtu = new_mtu;
1150                                 if ((rc = ibmveth_open(adapter->netdev)))
1151                                         return rc;
1152                         } else
1153                                 dev->mtu = new_mtu;
1154                         return 0;
1155                 }
1156         }
1157         return -EINVAL;
1158 }
1159
1160 #ifdef CONFIG_NET_POLL_CONTROLLER
1161 static void ibmveth_poll_controller(struct net_device *dev)
1162 {
1163         ibmveth_replenish_task(dev->priv);
1164         ibmveth_interrupt(dev->irq, dev);
1165 }
1166 #endif
1167
1168 static int __devinit ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
1169 {
1170         int rc, i;
1171         long ret;
1172         struct net_device *netdev;
1173         struct ibmveth_adapter *adapter;
1174         union ibmveth_illan_attributes set_attr, ret_attr;
1175
1176         unsigned char *mac_addr_p;
1177         unsigned int *mcastFilterSize_p;
1178
1179
1180         ibmveth_debug_printk_no_adapter("entering ibmveth_probe for UA 0x%x\n",
1181                                         dev->unit_address);
1182
1183         mac_addr_p = (unsigned char *) vio_get_attribute(dev,
1184                                                 VETH_MAC_ADDR, NULL);
1185         if(!mac_addr_p) {
1186                 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find VETH_MAC_ADDR "
1187                                 "attribute\n", __FILE__, __LINE__);
1188                 return 0;
1189         }
1190
1191         mcastFilterSize_p = (unsigned int *) vio_get_attribute(dev,
1192                                                 VETH_MCAST_FILTER_SIZE, NULL);
1193         if(!mcastFilterSize_p) {
1194                 printk(KERN_ERR "(%s:%3.3d) ERROR: Can't find "
1195                                 "VETH_MCAST_FILTER_SIZE attribute\n",
1196                                 __FILE__, __LINE__);
1197                 return 0;
1198         }
1199
1200         netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
1201
1202         if(!netdev)
1203                 return -ENOMEM;
1204
1205         SET_MODULE_OWNER(netdev);
1206
1207         adapter = netdev->priv;
1208         dev->dev.driver_data = netdev;
1209
1210         adapter->vdev = dev;
1211         adapter->netdev = netdev;
1212         adapter->mcastFilterSize= *mcastFilterSize_p;
1213         adapter->pool_config = 0;
1214
1215         netif_napi_add(netdev, &adapter->napi, ibmveth_poll, 16);
1216
1217         /*      Some older boxes running PHYP non-natively have an OF that
1218                 returns a 8-byte local-mac-address field (and the first
1219                 2 bytes have to be ignored) while newer boxes' OF return
1220                 a 6-byte field. Note that IEEE 1275 specifies that
1221                 local-mac-address must be a 6-byte field.
1222                 The RPA doc specifies that the first byte must be 10b, so
1223                 we'll just look for it to solve this 8 vs. 6 byte field issue */
1224
1225         if ((*mac_addr_p & 0x3) != 0x02)
1226                 mac_addr_p += 2;
1227
1228         adapter->mac_addr = 0;
1229         memcpy(&adapter->mac_addr, mac_addr_p, 6);
1230
1231         netdev->irq = dev->irq;
1232         netdev->open               = ibmveth_open;
1233         netdev->stop               = ibmveth_close;
1234         netdev->hard_start_xmit    = ibmveth_start_xmit;
1235         netdev->get_stats          = ibmveth_get_stats;
1236         netdev->set_multicast_list = ibmveth_set_multicast_list;
1237         netdev->do_ioctl           = ibmveth_ioctl;
1238         netdev->ethtool_ops           = &netdev_ethtool_ops;
1239         netdev->change_mtu         = ibmveth_change_mtu;
1240         SET_NETDEV_DEV(netdev, &dev->dev);
1241 #ifdef CONFIG_NET_POLL_CONTROLLER
1242         netdev->poll_controller = ibmveth_poll_controller;
1243 #endif
1244         netdev->features |= NETIF_F_LLTX;
1245         spin_lock_init(&adapter->stats_lock);
1246
1247         memcpy(&netdev->dev_addr, &adapter->mac_addr, netdev->addr_len);
1248
1249         for(i = 0; i<IbmVethNumBufferPools; i++) {
1250                 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
1251                 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1252                                          pool_count[i], pool_size[i],
1253                                          pool_active[i]);
1254                 kobj->parent = &dev->dev.kobj;
1255                 sprintf(kobj->name, "pool%d", i);
1256                 kobj->ktype = &ktype_veth_pool;
1257                 kobject_register(kobj);
1258         }
1259
1260         ibmveth_debug_printk("adapter @ 0x%p\n", adapter);
1261
1262         adapter->buffer_list_dma = DMA_ERROR_CODE;
1263         adapter->filter_list_dma = DMA_ERROR_CODE;
1264         adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1265
1266         ibmveth_debug_printk("registering netdev...\n");
1267
1268         ret = h_illan_attributes(dev->unit_address, 0, 0, &ret_attr.desc);
1269
1270         if (ret == H_SUCCESS && !ret_attr.fields.active_trunk &&
1271             !ret_attr.fields.trunk_priority &&
1272             ret_attr.fields.csum_offload_padded_pkt_support) {
1273                 set_attr.desc = 0;
1274                 set_attr.fields.tcp_csum_offload_ipv4 = 1;
1275
1276                 ret = h_illan_attributes(dev->unit_address, 0, set_attr.desc,
1277                                          &ret_attr.desc);
1278
1279                 if (ret == H_SUCCESS) {
1280                         adapter->rx_csum = 1;
1281                         netdev->features |= NETIF_F_IP_CSUM;
1282                 } else
1283                         ret = h_illan_attributes(dev->unit_address, set_attr.desc,
1284                                                  0, &ret_attr.desc);
1285         }
1286
1287         rc = register_netdev(netdev);
1288
1289         if(rc) {
1290                 ibmveth_debug_printk("failed to register netdev rc=%d\n", rc);
1291                 free_netdev(netdev);
1292                 return rc;
1293         }
1294
1295         ibmveth_debug_printk("registered\n");
1296
1297         ibmveth_proc_register_adapter(adapter);
1298
1299         return 0;
1300 }
1301
1302 static int __devexit ibmveth_remove(struct vio_dev *dev)
1303 {
1304         struct net_device *netdev = dev->dev.driver_data;
1305         struct ibmveth_adapter *adapter = netdev->priv;
1306         int i;
1307
1308         for(i = 0; i<IbmVethNumBufferPools; i++)
1309                 kobject_unregister(&adapter->rx_buff_pool[i].kobj);
1310
1311         unregister_netdev(netdev);
1312
1313         ibmveth_proc_unregister_adapter(adapter);
1314
1315         free_netdev(netdev);
1316         return 0;
1317 }
1318
1319 #ifdef CONFIG_PROC_FS
1320 static void ibmveth_proc_register_driver(void)
1321 {
1322         ibmveth_proc_dir = proc_mkdir(IBMVETH_PROC_DIR, init_net.proc_net);
1323         if (ibmveth_proc_dir) {
1324                 SET_MODULE_OWNER(ibmveth_proc_dir);
1325         }
1326 }
1327
1328 static void ibmveth_proc_unregister_driver(void)
1329 {
1330         remove_proc_entry(IBMVETH_PROC_DIR, init_net.proc_net);
1331 }
1332
1333 static void *ibmveth_seq_start(struct seq_file *seq, loff_t *pos)
1334 {
1335         if (*pos == 0) {
1336                 return (void *)1;
1337         } else {
1338                 return NULL;
1339         }
1340 }
1341
1342 static void *ibmveth_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1343 {
1344         ++*pos;
1345         return NULL;
1346 }
1347
1348 static void ibmveth_seq_stop(struct seq_file *seq, void *v)
1349 {
1350 }
1351
1352 static int ibmveth_seq_show(struct seq_file *seq, void *v)
1353 {
1354         struct ibmveth_adapter *adapter = seq->private;
1355         char *current_mac = ((char*) &adapter->netdev->dev_addr);
1356         char *firmware_mac = ((char*) &adapter->mac_addr) ;
1357
1358         seq_printf(seq, "%s %s\n\n", ibmveth_driver_string, ibmveth_driver_version);
1359
1360         seq_printf(seq, "Unit Address:    0x%x\n", adapter->vdev->unit_address);
1361         seq_printf(seq, "Current MAC:     %02X:%02X:%02X:%02X:%02X:%02X\n",
1362                    current_mac[0], current_mac[1], current_mac[2],
1363                    current_mac[3], current_mac[4], current_mac[5]);
1364         seq_printf(seq, "Firmware MAC:    %02X:%02X:%02X:%02X:%02X:%02X\n",
1365                    firmware_mac[0], firmware_mac[1], firmware_mac[2],
1366                    firmware_mac[3], firmware_mac[4], firmware_mac[5]);
1367
1368         seq_printf(seq, "\nAdapter Statistics:\n");
1369         seq_printf(seq, "  TX:  skbuffs linearized:          %ld\n", adapter->tx_linearized);
1370         seq_printf(seq, "       multi-descriptor sends:      %ld\n", adapter->tx_multidesc_send);
1371         seq_printf(seq, "       skb_linearize failures:      %ld\n", adapter->tx_linearize_failed);
1372         seq_printf(seq, "       vio_map_single failres:      %ld\n", adapter->tx_map_failed);
1373         seq_printf(seq, "       send failures:               %ld\n", adapter->tx_send_failed);
1374         seq_printf(seq, "  RX:  replenish task cycles:       %ld\n", adapter->replenish_task_cycles);
1375         seq_printf(seq, "       alloc_skb_failures:          %ld\n", adapter->replenish_no_mem);
1376         seq_printf(seq, "       add buffer failures:         %ld\n", adapter->replenish_add_buff_failure);
1377         seq_printf(seq, "       invalid buffers:             %ld\n", adapter->rx_invalid_buffer);
1378         seq_printf(seq, "       no buffers:                  %ld\n", adapter->rx_no_buffer);
1379
1380         return 0;
1381 }
1382 static struct seq_operations ibmveth_seq_ops = {
1383         .start = ibmveth_seq_start,
1384         .next  = ibmveth_seq_next,
1385         .stop  = ibmveth_seq_stop,
1386         .show  = ibmveth_seq_show,
1387 };
1388
1389 static int ibmveth_proc_open(struct inode *inode, struct file *file)
1390 {
1391         struct seq_file *seq;
1392         struct proc_dir_entry *proc;
1393         int rc;
1394
1395         rc = seq_open(file, &ibmveth_seq_ops);
1396         if (!rc) {
1397                 /* recover the pointer buried in proc_dir_entry data */
1398                 seq = file->private_data;
1399                 proc = PDE(inode);
1400                 seq->private = proc->data;
1401         }
1402         return rc;
1403 }
1404
1405 static const struct file_operations ibmveth_proc_fops = {
1406         .owner   = THIS_MODULE,
1407         .open    = ibmveth_proc_open,
1408         .read    = seq_read,
1409         .llseek  = seq_lseek,
1410         .release = seq_release,
1411 };
1412
1413 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1414 {
1415         struct proc_dir_entry *entry;
1416         if (ibmveth_proc_dir) {
1417                 char u_addr[10];
1418                 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1419                 entry = create_proc_entry(u_addr, S_IFREG, ibmveth_proc_dir);
1420                 if (!entry) {
1421                         ibmveth_error_printk("Cannot create adapter proc entry");
1422                 } else {
1423                         entry->data = (void *) adapter;
1424                         entry->proc_fops = &ibmveth_proc_fops;
1425                         SET_MODULE_OWNER(entry);
1426                 }
1427         }
1428         return;
1429 }
1430
1431 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1432 {
1433         if (ibmveth_proc_dir) {
1434                 char u_addr[10];
1435                 sprintf(u_addr, "%x", adapter->vdev->unit_address);
1436                 remove_proc_entry(u_addr, ibmveth_proc_dir);
1437         }
1438 }
1439
1440 #else /* CONFIG_PROC_FS */
1441 static void ibmveth_proc_register_adapter(struct ibmveth_adapter *adapter)
1442 {
1443 }
1444
1445 static void ibmveth_proc_unregister_adapter(struct ibmveth_adapter *adapter)
1446 {
1447 }
1448 static void ibmveth_proc_register_driver(void)
1449 {
1450 }
1451
1452 static void ibmveth_proc_unregister_driver(void)
1453 {
1454 }
1455 #endif /* CONFIG_PROC_FS */
1456
1457 static struct attribute veth_active_attr;
1458 static struct attribute veth_num_attr;
1459 static struct attribute veth_size_attr;
1460
1461 static ssize_t veth_pool_show(struct kobject * kobj,
1462                               struct attribute * attr, char * buf)
1463 {
1464         struct ibmveth_buff_pool *pool = container_of(kobj,
1465                                                       struct ibmveth_buff_pool,
1466                                                       kobj);
1467
1468         if (attr == &veth_active_attr)
1469                 return sprintf(buf, "%d\n", pool->active);
1470         else if (attr == &veth_num_attr)
1471                 return sprintf(buf, "%d\n", pool->size);
1472         else if (attr == &veth_size_attr)
1473                 return sprintf(buf, "%d\n", pool->buff_size);
1474         return 0;
1475 }
1476
1477 static ssize_t veth_pool_store(struct kobject * kobj, struct attribute * attr,
1478 const char * buf, size_t count)
1479 {
1480         struct ibmveth_buff_pool *pool = container_of(kobj,
1481                                                       struct ibmveth_buff_pool,
1482                                                       kobj);
1483         struct net_device *netdev =
1484             container_of(kobj->parent, struct device, kobj)->driver_data;
1485         struct ibmveth_adapter *adapter = netdev->priv;
1486         long value = simple_strtol(buf, NULL, 10);
1487         long rc;
1488
1489         if (attr == &veth_active_attr) {
1490                 if (value && !pool->active) {
1491                         if (netif_running(netdev)) {
1492                                 if(ibmveth_alloc_buffer_pool(pool)) {
1493                                         ibmveth_error_printk("unable to alloc pool\n");
1494                                         return -ENOMEM;
1495                                 }
1496                                 pool->active = 1;
1497                                 adapter->pool_config = 1;
1498                                 ibmveth_close(netdev);
1499                                 adapter->pool_config = 0;
1500                                 if ((rc = ibmveth_open(netdev)))
1501                                         return rc;
1502                         } else
1503                                 pool->active = 1;
1504                 } else if (!value && pool->active) {
1505                         int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1506                         int i;
1507                         /* Make sure there is a buffer pool with buffers that
1508                            can hold a packet of the size of the MTU */
1509                         for (i = 0; i < IbmVethNumBufferPools; i++) {
1510                                 if (pool == &adapter->rx_buff_pool[i])
1511                                         continue;
1512                                 if (!adapter->rx_buff_pool[i].active)
1513                                         continue;
1514                                 if (mtu <= adapter->rx_buff_pool[i].buff_size)
1515                                         break;
1516                         }
1517
1518                         if (i == IbmVethNumBufferPools) {
1519                                 ibmveth_error_printk("no active pool >= MTU\n");
1520                                 return -EPERM;
1521                         }
1522
1523                         pool->active = 0;
1524                         if (netif_running(netdev)) {
1525                                 adapter->pool_config = 1;
1526                                 ibmveth_close(netdev);
1527                                 adapter->pool_config = 0;
1528                                 if ((rc = ibmveth_open(netdev)))
1529                                         return rc;
1530                         }
1531                 }
1532         } else if (attr == &veth_num_attr) {
1533                 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT)
1534                         return -EINVAL;
1535                 else {
1536                         if (netif_running(netdev)) {
1537                                 adapter->pool_config = 1;
1538                                 ibmveth_close(netdev);
1539                                 adapter->pool_config = 0;
1540                                 pool->size = value;
1541                                 if ((rc = ibmveth_open(netdev)))
1542                                         return rc;
1543                         } else
1544                                 pool->size = value;
1545                 }
1546         } else if (attr == &veth_size_attr) {
1547                 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE)
1548                         return -EINVAL;
1549                 else {
1550                         if (netif_running(netdev)) {
1551                                 adapter->pool_config = 1;
1552                                 ibmveth_close(netdev);
1553                                 adapter->pool_config = 0;
1554                                 pool->buff_size = value;
1555                                 if ((rc = ibmveth_open(netdev)))
1556                                         return rc;
1557                         } else
1558                                 pool->buff_size = value;
1559                 }
1560         }
1561
1562         /* kick the interrupt handler to allocate/deallocate pools */
1563         ibmveth_interrupt(netdev->irq, netdev);
1564         return count;
1565 }
1566
1567
1568 #define ATTR(_name, _mode)      \
1569         struct attribute veth_##_name##_attr = {               \
1570         .name = __stringify(_name), .mode = _mode, \
1571         };
1572
1573 static ATTR(active, 0644);
1574 static ATTR(num, 0644);
1575 static ATTR(size, 0644);
1576
1577 static struct attribute * veth_pool_attrs[] = {
1578         &veth_active_attr,
1579         &veth_num_attr,
1580         &veth_size_attr,
1581         NULL,
1582 };
1583
1584 static struct sysfs_ops veth_pool_ops = {
1585         .show   = veth_pool_show,
1586         .store  = veth_pool_store,
1587 };
1588
1589 static struct kobj_type ktype_veth_pool = {
1590         .release        = NULL,
1591         .sysfs_ops      = &veth_pool_ops,
1592         .default_attrs  = veth_pool_attrs,
1593 };
1594
1595
1596 static struct vio_device_id ibmveth_device_table[] __devinitdata= {
1597         { "network", "IBM,l-lan"},
1598         { "", "" }
1599 };
1600 MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1601
1602 static struct vio_driver ibmveth_driver = {
1603         .id_table       = ibmveth_device_table,
1604         .probe          = ibmveth_probe,
1605         .remove         = ibmveth_remove,
1606         .driver         = {
1607                 .name   = ibmveth_driver_name,
1608                 .owner  = THIS_MODULE,
1609         }
1610 };
1611
1612 static int __init ibmveth_module_init(void)
1613 {
1614         ibmveth_printk("%s: %s %s\n", ibmveth_driver_name, ibmveth_driver_string, ibmveth_driver_version);
1615
1616         ibmveth_proc_register_driver();
1617
1618         return vio_register_driver(&ibmveth_driver);
1619 }
1620
1621 static void __exit ibmveth_module_exit(void)
1622 {
1623         vio_unregister_driver(&ibmveth_driver);
1624         ibmveth_proc_unregister_driver();
1625 }
1626
1627 module_init(ibmveth_module_init);
1628 module_exit(ibmveth_module_exit);