2 * IBM Power Virtual Ethernet Device Driver
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 * Copyright (C) IBM Corporation, 2003, 2010
19 * Authors: Dave Larson <larson1@us.ibm.com>
20 * Santiago Leon <santil@linux.vnet.ibm.com>
21 * Brian King <brking@linux.vnet.ibm.com>
22 * Robert Jennings <rcj@linux.vnet.ibm.com>
23 * Anton Blanchard <anton@au.ibm.com>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/types.h>
29 #include <linux/errno.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/kernel.h>
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34 #include <linux/skbuff.h>
35 #include <linux/init.h>
36 #include <linux/interrupt.h>
39 #include <linux/ethtool.h>
42 #include <linux/ipv6.h>
43 #include <linux/slab.h>
44 #include <asm/hvcall.h>
45 #include <linux/atomic.h>
47 #include <asm/iommu.h>
48 #include <asm/firmware.h>
52 static irqreturn_t
ibmveth_interrupt(int irq
, void *dev_instance
);
53 static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter
*adapter
);
54 static unsigned long ibmveth_get_desired_dma(struct vio_dev
*vdev
);
56 static struct kobj_type ktype_veth_pool
;
59 static const char ibmveth_driver_name
[] = "ibmveth";
60 static const char ibmveth_driver_string
[] = "IBM Power Virtual Ethernet Driver";
61 #define ibmveth_driver_version "1.05"
63 MODULE_AUTHOR("Santiago Leon <santil@linux.vnet.ibm.com>");
64 MODULE_DESCRIPTION("IBM Power Virtual Ethernet Driver");
65 MODULE_LICENSE("GPL");
66 MODULE_VERSION(ibmveth_driver_version
);
68 static unsigned int tx_copybreak __read_mostly
= 128;
69 module_param(tx_copybreak
, uint
, 0644);
70 MODULE_PARM_DESC(tx_copybreak
,
71 "Maximum size of packet that is copied to a new buffer on transmit");
73 static unsigned int rx_copybreak __read_mostly
= 128;
74 module_param(rx_copybreak
, uint
, 0644);
75 MODULE_PARM_DESC(rx_copybreak
,
76 "Maximum size of packet that is copied to a new buffer on receive");
78 static unsigned int rx_flush __read_mostly
= 0;
79 module_param(rx_flush
, uint
, 0644);
80 MODULE_PARM_DESC(rx_flush
, "Flush receive buffers before use");
82 static bool old_large_send __read_mostly
;
83 module_param(old_large_send
, bool, S_IRUGO
);
84 MODULE_PARM_DESC(old_large_send
,
85 "Use old large send method on firmware that supports the new method");
88 char name
[ETH_GSTRING_LEN
];
92 #define IBMVETH_STAT_OFF(stat) offsetof(struct ibmveth_adapter, stat)
93 #define IBMVETH_GET_STAT(a, off) *((u64 *)(((unsigned long)(a)) + off))
95 struct ibmveth_stat ibmveth_stats
[] = {
96 { "replenish_task_cycles", IBMVETH_STAT_OFF(replenish_task_cycles
) },
97 { "replenish_no_mem", IBMVETH_STAT_OFF(replenish_no_mem
) },
98 { "replenish_add_buff_failure",
99 IBMVETH_STAT_OFF(replenish_add_buff_failure
) },
100 { "replenish_add_buff_success",
101 IBMVETH_STAT_OFF(replenish_add_buff_success
) },
102 { "rx_invalid_buffer", IBMVETH_STAT_OFF(rx_invalid_buffer
) },
103 { "rx_no_buffer", IBMVETH_STAT_OFF(rx_no_buffer
) },
104 { "tx_map_failed", IBMVETH_STAT_OFF(tx_map_failed
) },
105 { "tx_send_failed", IBMVETH_STAT_OFF(tx_send_failed
) },
106 { "fw_enabled_ipv4_csum", IBMVETH_STAT_OFF(fw_ipv4_csum_support
) },
107 { "fw_enabled_ipv6_csum", IBMVETH_STAT_OFF(fw_ipv6_csum_support
) },
108 { "tx_large_packets", IBMVETH_STAT_OFF(tx_large_packets
) },
109 { "rx_large_packets", IBMVETH_STAT_OFF(rx_large_packets
) },
110 { "fw_enabled_large_send", IBMVETH_STAT_OFF(fw_large_send_support
) }
113 /* simple methods of getting data from the current rxq entry */
114 static inline u32
ibmveth_rxq_flags(struct ibmveth_adapter
*adapter
)
116 return be32_to_cpu(adapter
->rx_queue
.queue_addr
[adapter
->rx_queue
.index
].flags_off
);
119 static inline int ibmveth_rxq_toggle(struct ibmveth_adapter
*adapter
)
121 return (ibmveth_rxq_flags(adapter
) & IBMVETH_RXQ_TOGGLE
) >>
122 IBMVETH_RXQ_TOGGLE_SHIFT
;
125 static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter
*adapter
)
127 return ibmveth_rxq_toggle(adapter
) == adapter
->rx_queue
.toggle
;
130 static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter
*adapter
)
132 return ibmveth_rxq_flags(adapter
) & IBMVETH_RXQ_VALID
;
135 static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter
*adapter
)
137 return ibmveth_rxq_flags(adapter
) & IBMVETH_RXQ_OFF_MASK
;
140 static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter
*adapter
)
142 return be32_to_cpu(adapter
->rx_queue
.queue_addr
[adapter
->rx_queue
.index
].length
);
145 static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter
*adapter
)
147 return ibmveth_rxq_flags(adapter
) & IBMVETH_RXQ_CSUM_GOOD
;
150 /* setup the initial settings for a buffer pool */
151 static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool
*pool
,
152 u32 pool_index
, u32 pool_size
,
153 u32 buff_size
, u32 pool_active
)
155 pool
->size
= pool_size
;
156 pool
->index
= pool_index
;
157 pool
->buff_size
= buff_size
;
158 pool
->threshold
= pool_size
* 7 / 8;
159 pool
->active
= pool_active
;
162 /* allocate and setup an buffer pool - called during open */
163 static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool
*pool
)
167 pool
->free_map
= kmalloc(sizeof(u16
) * pool
->size
, GFP_KERNEL
);
172 pool
->dma_addr
= kcalloc(pool
->size
, sizeof(dma_addr_t
), GFP_KERNEL
);
173 if (!pool
->dma_addr
) {
174 kfree(pool
->free_map
);
175 pool
->free_map
= NULL
;
179 pool
->skbuff
= kcalloc(pool
->size
, sizeof(void *), GFP_KERNEL
);
182 kfree(pool
->dma_addr
);
183 pool
->dma_addr
= NULL
;
185 kfree(pool
->free_map
);
186 pool
->free_map
= NULL
;
190 for (i
= 0; i
< pool
->size
; ++i
)
191 pool
->free_map
[i
] = i
;
193 atomic_set(&pool
->available
, 0);
194 pool
->producer_index
= 0;
195 pool
->consumer_index
= 0;
200 static inline void ibmveth_flush_buffer(void *addr
, unsigned long length
)
202 unsigned long offset
;
204 for (offset
= 0; offset
< length
; offset
+= SMP_CACHE_BYTES
)
205 asm("dcbfl %0,%1" :: "b" (addr
), "r" (offset
));
208 /* replenish the buffers for a pool. note that we don't need to
209 * skb_reserve these since they are used for incoming...
211 static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter
*adapter
,
212 struct ibmveth_buff_pool
*pool
)
215 u32 count
= pool
->size
- atomic_read(&pool
->available
);
216 u32 buffers_added
= 0;
218 unsigned int free_index
, index
;
220 unsigned long lpar_rc
;
225 for (i
= 0; i
< count
; ++i
) {
226 union ibmveth_buf_desc desc
;
228 skb
= netdev_alloc_skb(adapter
->netdev
, pool
->buff_size
);
231 netdev_dbg(adapter
->netdev
,
232 "replenish: unable to allocate skb\n");
233 adapter
->replenish_no_mem
++;
237 free_index
= pool
->consumer_index
;
238 pool
->consumer_index
++;
239 if (pool
->consumer_index
>= pool
->size
)
240 pool
->consumer_index
= 0;
241 index
= pool
->free_map
[free_index
];
243 BUG_ON(index
== IBM_VETH_INVALID_MAP
);
244 BUG_ON(pool
->skbuff
[index
] != NULL
);
246 dma_addr
= dma_map_single(&adapter
->vdev
->dev
, skb
->data
,
247 pool
->buff_size
, DMA_FROM_DEVICE
);
249 if (dma_mapping_error(&adapter
->vdev
->dev
, dma_addr
))
252 pool
->free_map
[free_index
] = IBM_VETH_INVALID_MAP
;
253 pool
->dma_addr
[index
] = dma_addr
;
254 pool
->skbuff
[index
] = skb
;
256 correlator
= ((u64
)pool
->index
<< 32) | index
;
257 *(u64
*)skb
->data
= correlator
;
259 desc
.fields
.flags_len
= IBMVETH_BUF_VALID
| pool
->buff_size
;
260 desc
.fields
.address
= dma_addr
;
263 unsigned int len
= min(pool
->buff_size
,
264 adapter
->netdev
->mtu
+
266 ibmveth_flush_buffer(skb
->data
, len
);
268 lpar_rc
= h_add_logical_lan_buffer(adapter
->vdev
->unit_address
,
271 if (lpar_rc
!= H_SUCCESS
) {
275 adapter
->replenish_add_buff_success
++;
280 atomic_add(buffers_added
, &(pool
->available
));
284 pool
->free_map
[free_index
] = index
;
285 pool
->skbuff
[index
] = NULL
;
286 if (pool
->consumer_index
== 0)
287 pool
->consumer_index
= pool
->size
- 1;
289 pool
->consumer_index
--;
290 if (!dma_mapping_error(&adapter
->vdev
->dev
, dma_addr
))
291 dma_unmap_single(&adapter
->vdev
->dev
,
292 pool
->dma_addr
[index
], pool
->buff_size
,
294 dev_kfree_skb_any(skb
);
295 adapter
->replenish_add_buff_failure
++;
298 atomic_add(buffers_added
, &(pool
->available
));
302 * The final 8 bytes of the buffer list is a counter of frames dropped
303 * because there was not a buffer in the buffer list capable of holding
306 static void ibmveth_update_rx_no_buffer(struct ibmveth_adapter
*adapter
)
308 __be64
*p
= adapter
->buffer_list_addr
+ 4096 - 8;
310 adapter
->rx_no_buffer
= be64_to_cpup(p
);
313 /* replenish routine */
314 static void ibmveth_replenish_task(struct ibmveth_adapter
*adapter
)
318 adapter
->replenish_task_cycles
++;
320 for (i
= (IBMVETH_NUM_BUFF_POOLS
- 1); i
>= 0; i
--) {
321 struct ibmveth_buff_pool
*pool
= &adapter
->rx_buff_pool
[i
];
324 (atomic_read(&pool
->available
) < pool
->threshold
))
325 ibmveth_replenish_buffer_pool(adapter
, pool
);
328 ibmveth_update_rx_no_buffer(adapter
);
331 /* empty and free ana buffer pool - also used to do cleanup in error paths */
332 static void ibmveth_free_buffer_pool(struct ibmveth_adapter
*adapter
,
333 struct ibmveth_buff_pool
*pool
)
337 kfree(pool
->free_map
);
338 pool
->free_map
= NULL
;
340 if (pool
->skbuff
&& pool
->dma_addr
) {
341 for (i
= 0; i
< pool
->size
; ++i
) {
342 struct sk_buff
*skb
= pool
->skbuff
[i
];
344 dma_unmap_single(&adapter
->vdev
->dev
,
348 dev_kfree_skb_any(skb
);
349 pool
->skbuff
[i
] = NULL
;
354 if (pool
->dma_addr
) {
355 kfree(pool
->dma_addr
);
356 pool
->dma_addr
= NULL
;
365 /* remove a buffer from a pool */
366 static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter
*adapter
,
369 unsigned int pool
= correlator
>> 32;
370 unsigned int index
= correlator
& 0xffffffffUL
;
371 unsigned int free_index
;
374 BUG_ON(pool
>= IBMVETH_NUM_BUFF_POOLS
);
375 BUG_ON(index
>= adapter
->rx_buff_pool
[pool
].size
);
377 skb
= adapter
->rx_buff_pool
[pool
].skbuff
[index
];
381 adapter
->rx_buff_pool
[pool
].skbuff
[index
] = NULL
;
383 dma_unmap_single(&adapter
->vdev
->dev
,
384 adapter
->rx_buff_pool
[pool
].dma_addr
[index
],
385 adapter
->rx_buff_pool
[pool
].buff_size
,
388 free_index
= adapter
->rx_buff_pool
[pool
].producer_index
;
389 adapter
->rx_buff_pool
[pool
].producer_index
++;
390 if (adapter
->rx_buff_pool
[pool
].producer_index
>=
391 adapter
->rx_buff_pool
[pool
].size
)
392 adapter
->rx_buff_pool
[pool
].producer_index
= 0;
393 adapter
->rx_buff_pool
[pool
].free_map
[free_index
] = index
;
397 atomic_dec(&(adapter
->rx_buff_pool
[pool
].available
));
400 /* get the current buffer on the rx queue */
401 static inline struct sk_buff
*ibmveth_rxq_get_buffer(struct ibmveth_adapter
*adapter
)
403 u64 correlator
= adapter
->rx_queue
.queue_addr
[adapter
->rx_queue
.index
].correlator
;
404 unsigned int pool
= correlator
>> 32;
405 unsigned int index
= correlator
& 0xffffffffUL
;
407 BUG_ON(pool
>= IBMVETH_NUM_BUFF_POOLS
);
408 BUG_ON(index
>= adapter
->rx_buff_pool
[pool
].size
);
410 return adapter
->rx_buff_pool
[pool
].skbuff
[index
];
413 /* recycle the current buffer on the rx queue */
414 static int ibmveth_rxq_recycle_buffer(struct ibmveth_adapter
*adapter
)
416 u32 q_index
= adapter
->rx_queue
.index
;
417 u64 correlator
= adapter
->rx_queue
.queue_addr
[q_index
].correlator
;
418 unsigned int pool
= correlator
>> 32;
419 unsigned int index
= correlator
& 0xffffffffUL
;
420 union ibmveth_buf_desc desc
;
421 unsigned long lpar_rc
;
424 BUG_ON(pool
>= IBMVETH_NUM_BUFF_POOLS
);
425 BUG_ON(index
>= adapter
->rx_buff_pool
[pool
].size
);
427 if (!adapter
->rx_buff_pool
[pool
].active
) {
428 ibmveth_rxq_harvest_buffer(adapter
);
429 ibmveth_free_buffer_pool(adapter
, &adapter
->rx_buff_pool
[pool
]);
433 desc
.fields
.flags_len
= IBMVETH_BUF_VALID
|
434 adapter
->rx_buff_pool
[pool
].buff_size
;
435 desc
.fields
.address
= adapter
->rx_buff_pool
[pool
].dma_addr
[index
];
437 lpar_rc
= h_add_logical_lan_buffer(adapter
->vdev
->unit_address
, desc
.desc
);
439 if (lpar_rc
!= H_SUCCESS
) {
440 netdev_dbg(adapter
->netdev
, "h_add_logical_lan_buffer failed "
441 "during recycle rc=%ld", lpar_rc
);
442 ibmveth_remove_buffer_from_pool(adapter
, adapter
->rx_queue
.queue_addr
[adapter
->rx_queue
.index
].correlator
);
446 if (++adapter
->rx_queue
.index
== adapter
->rx_queue
.num_slots
) {
447 adapter
->rx_queue
.index
= 0;
448 adapter
->rx_queue
.toggle
= !adapter
->rx_queue
.toggle
;
455 static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter
*adapter
)
457 ibmveth_remove_buffer_from_pool(adapter
, adapter
->rx_queue
.queue_addr
[adapter
->rx_queue
.index
].correlator
);
459 if (++adapter
->rx_queue
.index
== adapter
->rx_queue
.num_slots
) {
460 adapter
->rx_queue
.index
= 0;
461 adapter
->rx_queue
.toggle
= !adapter
->rx_queue
.toggle
;
465 static void ibmveth_cleanup(struct ibmveth_adapter
*adapter
)
468 struct device
*dev
= &adapter
->vdev
->dev
;
470 if (adapter
->buffer_list_addr
!= NULL
) {
471 if (!dma_mapping_error(dev
, adapter
->buffer_list_dma
)) {
472 dma_unmap_single(dev
, adapter
->buffer_list_dma
, 4096,
474 adapter
->buffer_list_dma
= DMA_ERROR_CODE
;
476 free_page((unsigned long)adapter
->buffer_list_addr
);
477 adapter
->buffer_list_addr
= NULL
;
480 if (adapter
->filter_list_addr
!= NULL
) {
481 if (!dma_mapping_error(dev
, adapter
->filter_list_dma
)) {
482 dma_unmap_single(dev
, adapter
->filter_list_dma
, 4096,
484 adapter
->filter_list_dma
= DMA_ERROR_CODE
;
486 free_page((unsigned long)adapter
->filter_list_addr
);
487 adapter
->filter_list_addr
= NULL
;
490 if (adapter
->rx_queue
.queue_addr
!= NULL
) {
491 dma_free_coherent(dev
, adapter
->rx_queue
.queue_len
,
492 adapter
->rx_queue
.queue_addr
,
493 adapter
->rx_queue
.queue_dma
);
494 adapter
->rx_queue
.queue_addr
= NULL
;
497 for (i
= 0; i
< IBMVETH_NUM_BUFF_POOLS
; i
++)
498 if (adapter
->rx_buff_pool
[i
].active
)
499 ibmveth_free_buffer_pool(adapter
,
500 &adapter
->rx_buff_pool
[i
]);
502 if (adapter
->bounce_buffer
!= NULL
) {
503 if (!dma_mapping_error(dev
, adapter
->bounce_buffer_dma
)) {
504 dma_unmap_single(&adapter
->vdev
->dev
,
505 adapter
->bounce_buffer_dma
,
506 adapter
->netdev
->mtu
+ IBMVETH_BUFF_OH
,
508 adapter
->bounce_buffer_dma
= DMA_ERROR_CODE
;
510 kfree(adapter
->bounce_buffer
);
511 adapter
->bounce_buffer
= NULL
;
515 static int ibmveth_register_logical_lan(struct ibmveth_adapter
*adapter
,
516 union ibmveth_buf_desc rxq_desc
, u64 mac_address
)
518 int rc
, try_again
= 1;
521 * After a kexec the adapter will still be open, so our attempt to
522 * open it will fail. So if we get a failure we free the adapter and
523 * try again, but only once.
526 rc
= h_register_logical_lan(adapter
->vdev
->unit_address
,
527 adapter
->buffer_list_dma
, rxq_desc
.desc
,
528 adapter
->filter_list_dma
, mac_address
);
530 if (rc
!= H_SUCCESS
&& try_again
) {
532 rc
= h_free_logical_lan(adapter
->vdev
->unit_address
);
533 } while (H_IS_LONG_BUSY(rc
) || (rc
== H_BUSY
));
542 static u64
ibmveth_encode_mac_addr(u8
*mac
)
547 for (i
= 0; i
< ETH_ALEN
; i
++)
548 encoded
= (encoded
<< 8) | mac
[i
];
553 static int ibmveth_open(struct net_device
*netdev
)
555 struct ibmveth_adapter
*adapter
= netdev_priv(netdev
);
558 unsigned long lpar_rc
;
560 union ibmveth_buf_desc rxq_desc
;
564 netdev_dbg(netdev
, "open starting\n");
566 napi_enable(&adapter
->napi
);
568 for(i
= 0; i
< IBMVETH_NUM_BUFF_POOLS
; i
++)
569 rxq_entries
+= adapter
->rx_buff_pool
[i
].size
;
571 adapter
->buffer_list_addr
= (void*) get_zeroed_page(GFP_KERNEL
);
572 adapter
->filter_list_addr
= (void*) get_zeroed_page(GFP_KERNEL
);
574 if (!adapter
->buffer_list_addr
|| !adapter
->filter_list_addr
) {
575 netdev_err(netdev
, "unable to allocate filter or buffer list "
581 dev
= &adapter
->vdev
->dev
;
583 adapter
->rx_queue
.queue_len
= sizeof(struct ibmveth_rx_q_entry
) *
585 adapter
->rx_queue
.queue_addr
=
586 dma_alloc_coherent(dev
, adapter
->rx_queue
.queue_len
,
587 &adapter
->rx_queue
.queue_dma
, GFP_KERNEL
);
588 if (!adapter
->rx_queue
.queue_addr
) {
593 adapter
->buffer_list_dma
= dma_map_single(dev
,
594 adapter
->buffer_list_addr
, 4096, DMA_BIDIRECTIONAL
);
595 adapter
->filter_list_dma
= dma_map_single(dev
,
596 adapter
->filter_list_addr
, 4096, DMA_BIDIRECTIONAL
);
598 if ((dma_mapping_error(dev
, adapter
->buffer_list_dma
)) ||
599 (dma_mapping_error(dev
, adapter
->filter_list_dma
))) {
600 netdev_err(netdev
, "unable to map filter or buffer list "
606 adapter
->rx_queue
.index
= 0;
607 adapter
->rx_queue
.num_slots
= rxq_entries
;
608 adapter
->rx_queue
.toggle
= 1;
610 mac_address
= ibmveth_encode_mac_addr(netdev
->dev_addr
);
612 rxq_desc
.fields
.flags_len
= IBMVETH_BUF_VALID
|
613 adapter
->rx_queue
.queue_len
;
614 rxq_desc
.fields
.address
= adapter
->rx_queue
.queue_dma
;
616 netdev_dbg(netdev
, "buffer list @ 0x%p\n", adapter
->buffer_list_addr
);
617 netdev_dbg(netdev
, "filter list @ 0x%p\n", adapter
->filter_list_addr
);
618 netdev_dbg(netdev
, "receive q @ 0x%p\n", adapter
->rx_queue
.queue_addr
);
620 h_vio_signal(adapter
->vdev
->unit_address
, VIO_IRQ_DISABLE
);
622 lpar_rc
= ibmveth_register_logical_lan(adapter
, rxq_desc
, mac_address
);
624 if (lpar_rc
!= H_SUCCESS
) {
625 netdev_err(netdev
, "h_register_logical_lan failed with %ld\n",
627 netdev_err(netdev
, "buffer TCE:0x%llx filter TCE:0x%llx rxq "
628 "desc:0x%llx MAC:0x%llx\n",
629 adapter
->buffer_list_dma
,
630 adapter
->filter_list_dma
,
637 for (i
= 0; i
< IBMVETH_NUM_BUFF_POOLS
; i
++) {
638 if (!adapter
->rx_buff_pool
[i
].active
)
640 if (ibmveth_alloc_buffer_pool(&adapter
->rx_buff_pool
[i
])) {
641 netdev_err(netdev
, "unable to alloc pool\n");
642 adapter
->rx_buff_pool
[i
].active
= 0;
648 netdev_dbg(netdev
, "registering irq 0x%x\n", netdev
->irq
);
649 rc
= request_irq(netdev
->irq
, ibmveth_interrupt
, 0, netdev
->name
,
652 netdev_err(netdev
, "unable to request irq 0x%x, rc %d\n",
655 lpar_rc
= h_free_logical_lan(adapter
->vdev
->unit_address
);
656 } while (H_IS_LONG_BUSY(lpar_rc
) || (lpar_rc
== H_BUSY
));
661 adapter
->bounce_buffer
=
662 kmalloc(netdev
->mtu
+ IBMVETH_BUFF_OH
, GFP_KERNEL
);
663 if (!adapter
->bounce_buffer
) {
665 goto err_out_free_irq
;
667 adapter
->bounce_buffer_dma
=
668 dma_map_single(&adapter
->vdev
->dev
, adapter
->bounce_buffer
,
669 netdev
->mtu
+ IBMVETH_BUFF_OH
, DMA_BIDIRECTIONAL
);
670 if (dma_mapping_error(dev
, adapter
->bounce_buffer_dma
)) {
671 netdev_err(netdev
, "unable to map bounce buffer\n");
673 goto err_out_free_irq
;
676 netdev_dbg(netdev
, "initial replenish cycle\n");
677 ibmveth_interrupt(netdev
->irq
, netdev
);
679 netif_start_queue(netdev
);
681 netdev_dbg(netdev
, "open complete\n");
686 free_irq(netdev
->irq
, netdev
);
688 ibmveth_cleanup(adapter
);
689 napi_disable(&adapter
->napi
);
693 static int ibmveth_close(struct net_device
*netdev
)
695 struct ibmveth_adapter
*adapter
= netdev_priv(netdev
);
698 netdev_dbg(netdev
, "close starting\n");
700 napi_disable(&adapter
->napi
);
702 if (!adapter
->pool_config
)
703 netif_stop_queue(netdev
);
705 h_vio_signal(adapter
->vdev
->unit_address
, VIO_IRQ_DISABLE
);
708 lpar_rc
= h_free_logical_lan(adapter
->vdev
->unit_address
);
709 } while (H_IS_LONG_BUSY(lpar_rc
) || (lpar_rc
== H_BUSY
));
711 if (lpar_rc
!= H_SUCCESS
) {
712 netdev_err(netdev
, "h_free_logical_lan failed with %lx, "
713 "continuing with close\n", lpar_rc
);
716 free_irq(netdev
->irq
, netdev
);
718 ibmveth_update_rx_no_buffer(adapter
);
720 ibmveth_cleanup(adapter
);
722 netdev_dbg(netdev
, "close complete\n");
727 static int netdev_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
729 cmd
->supported
= (SUPPORTED_1000baseT_Full
| SUPPORTED_Autoneg
|
731 cmd
->advertising
= (ADVERTISED_1000baseT_Full
| ADVERTISED_Autoneg
|
733 ethtool_cmd_speed_set(cmd
, SPEED_1000
);
734 cmd
->duplex
= DUPLEX_FULL
;
735 cmd
->port
= PORT_FIBRE
;
736 cmd
->phy_address
= 0;
737 cmd
->transceiver
= XCVR_INTERNAL
;
738 cmd
->autoneg
= AUTONEG_ENABLE
;
744 static void netdev_get_drvinfo(struct net_device
*dev
,
745 struct ethtool_drvinfo
*info
)
747 strlcpy(info
->driver
, ibmveth_driver_name
, sizeof(info
->driver
));
748 strlcpy(info
->version
, ibmveth_driver_version
, sizeof(info
->version
));
751 static netdev_features_t
ibmveth_fix_features(struct net_device
*dev
,
752 netdev_features_t features
)
755 * Since the ibmveth firmware interface does not have the
756 * concept of separate tx/rx checksum offload enable, if rx
757 * checksum is disabled we also have to disable tx checksum
758 * offload. Once we disable rx checksum offload, we are no
759 * longer allowed to send tx buffers that are not properly
763 if (!(features
& NETIF_F_RXCSUM
))
764 features
&= ~NETIF_F_CSUM_MASK
;
769 static int ibmveth_set_csum_offload(struct net_device
*dev
, u32 data
)
771 struct ibmveth_adapter
*adapter
= netdev_priv(dev
);
772 unsigned long set_attr
, clr_attr
, ret_attr
;
773 unsigned long set_attr6
, clr_attr6
;
774 long ret
, ret4
, ret6
;
775 int rc1
= 0, rc2
= 0;
778 if (netif_running(dev
)) {
780 adapter
->pool_config
= 1;
782 adapter
->pool_config
= 0;
791 set_attr
= IBMVETH_ILLAN_IPV4_TCP_CSUM
;
792 set_attr6
= IBMVETH_ILLAN_IPV6_TCP_CSUM
;
794 clr_attr
= IBMVETH_ILLAN_IPV4_TCP_CSUM
;
795 clr_attr6
= IBMVETH_ILLAN_IPV6_TCP_CSUM
;
798 ret
= h_illan_attributes(adapter
->vdev
->unit_address
, 0, 0, &ret_attr
);
800 if (ret
== H_SUCCESS
&& !(ret_attr
& IBMVETH_ILLAN_ACTIVE_TRUNK
) &&
801 !(ret_attr
& IBMVETH_ILLAN_TRUNK_PRI_MASK
) &&
802 (ret_attr
& IBMVETH_ILLAN_PADDED_PKT_CSUM
)) {
803 ret4
= h_illan_attributes(adapter
->vdev
->unit_address
, clr_attr
,
804 set_attr
, &ret_attr
);
806 if (ret4
!= H_SUCCESS
) {
807 netdev_err(dev
, "unable to change IPv4 checksum "
808 "offload settings. %d rc=%ld\n",
811 h_illan_attributes(adapter
->vdev
->unit_address
,
812 set_attr
, clr_attr
, &ret_attr
);
815 dev
->features
&= ~NETIF_F_IP_CSUM
;
818 adapter
->fw_ipv4_csum_support
= data
;
821 ret6
= h_illan_attributes(adapter
->vdev
->unit_address
,
822 clr_attr6
, set_attr6
, &ret_attr
);
824 if (ret6
!= H_SUCCESS
) {
825 netdev_err(dev
, "unable to change IPv6 checksum "
826 "offload settings. %d rc=%ld\n",
829 h_illan_attributes(adapter
->vdev
->unit_address
,
830 set_attr6
, clr_attr6
, &ret_attr
);
833 dev
->features
&= ~NETIF_F_IPV6_CSUM
;
836 adapter
->fw_ipv6_csum_support
= data
;
838 if (ret4
== H_SUCCESS
|| ret6
== H_SUCCESS
)
839 adapter
->rx_csum
= data
;
844 netdev_err(dev
, "unable to change checksum offload settings."
845 " %d rc=%ld ret_attr=%lx\n", data
, ret
,
850 rc2
= ibmveth_open(dev
);
852 return rc1
? rc1
: rc2
;
855 static int ibmveth_set_tso(struct net_device
*dev
, u32 data
)
857 struct ibmveth_adapter
*adapter
= netdev_priv(dev
);
858 unsigned long set_attr
, clr_attr
, ret_attr
;
860 int rc1
= 0, rc2
= 0;
863 if (netif_running(dev
)) {
865 adapter
->pool_config
= 1;
867 adapter
->pool_config
= 0;
874 set_attr
= IBMVETH_ILLAN_LRG_SR_ENABLED
;
876 clr_attr
= IBMVETH_ILLAN_LRG_SR_ENABLED
;
878 ret1
= h_illan_attributes(adapter
->vdev
->unit_address
, 0, 0, &ret_attr
);
880 if (ret1
== H_SUCCESS
&& (ret_attr
& IBMVETH_ILLAN_LRG_SND_SUPPORT
) &&
882 ret2
= h_illan_attributes(adapter
->vdev
->unit_address
, clr_attr
,
883 set_attr
, &ret_attr
);
885 if (ret2
!= H_SUCCESS
) {
886 netdev_err(dev
, "unable to change tso settings. %d rc=%ld\n",
889 h_illan_attributes(adapter
->vdev
->unit_address
,
890 set_attr
, clr_attr
, &ret_attr
);
893 dev
->features
&= ~(NETIF_F_TSO
| NETIF_F_TSO6
);
897 adapter
->fw_large_send_support
= data
;
898 adapter
->large_send
= data
;
901 /* Older firmware version of large send offload does not
905 dev
->features
&= ~NETIF_F_TSO6
;
906 netdev_info(dev
, "TSO feature requires all partitions to have updated driver");
908 adapter
->large_send
= data
;
912 rc2
= ibmveth_open(dev
);
914 return rc1
? rc1
: rc2
;
917 static int ibmveth_set_features(struct net_device
*dev
,
918 netdev_features_t features
)
920 struct ibmveth_adapter
*adapter
= netdev_priv(dev
);
921 int rx_csum
= !!(features
& NETIF_F_RXCSUM
);
922 int large_send
= !!(features
& (NETIF_F_TSO
| NETIF_F_TSO6
));
923 int rc1
= 0, rc2
= 0;
925 if (rx_csum
!= adapter
->rx_csum
) {
926 rc1
= ibmveth_set_csum_offload(dev
, rx_csum
);
927 if (rc1
&& !adapter
->rx_csum
)
929 features
& ~(NETIF_F_CSUM_MASK
|
933 if (large_send
!= adapter
->large_send
) {
934 rc2
= ibmveth_set_tso(dev
, large_send
);
935 if (rc2
&& !adapter
->large_send
)
937 features
& ~(NETIF_F_TSO
| NETIF_F_TSO6
);
940 return rc1
? rc1
: rc2
;
943 static void ibmveth_get_strings(struct net_device
*dev
, u32 stringset
, u8
*data
)
947 if (stringset
!= ETH_SS_STATS
)
950 for (i
= 0; i
< ARRAY_SIZE(ibmveth_stats
); i
++, data
+= ETH_GSTRING_LEN
)
951 memcpy(data
, ibmveth_stats
[i
].name
, ETH_GSTRING_LEN
);
954 static int ibmveth_get_sset_count(struct net_device
*dev
, int sset
)
958 return ARRAY_SIZE(ibmveth_stats
);
964 static void ibmveth_get_ethtool_stats(struct net_device
*dev
,
965 struct ethtool_stats
*stats
, u64
*data
)
968 struct ibmveth_adapter
*adapter
= netdev_priv(dev
);
970 for (i
= 0; i
< ARRAY_SIZE(ibmveth_stats
); i
++)
971 data
[i
] = IBMVETH_GET_STAT(adapter
, ibmveth_stats
[i
].offset
);
974 static const struct ethtool_ops netdev_ethtool_ops
= {
975 .get_drvinfo
= netdev_get_drvinfo
,
976 .get_settings
= netdev_get_settings
,
977 .get_link
= ethtool_op_get_link
,
978 .get_strings
= ibmveth_get_strings
,
979 .get_sset_count
= ibmveth_get_sset_count
,
980 .get_ethtool_stats
= ibmveth_get_ethtool_stats
,
983 static int ibmveth_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
988 #define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
990 static int ibmveth_send(struct ibmveth_adapter
*adapter
,
991 union ibmveth_buf_desc
*descs
, unsigned long mss
)
993 unsigned long correlator
;
994 unsigned int retry_count
;
998 * The retry count sets a maximum for the number of broadcast and
999 * multicast destinations within the system.
1004 ret
= h_send_logical_lan(adapter
->vdev
->unit_address
,
1005 descs
[0].desc
, descs
[1].desc
,
1006 descs
[2].desc
, descs
[3].desc
,
1007 descs
[4].desc
, descs
[5].desc
,
1008 correlator
, &correlator
, mss
,
1009 adapter
->fw_large_send_support
);
1010 } while ((ret
== H_BUSY
) && (retry_count
--));
1012 if (ret
!= H_SUCCESS
&& ret
!= H_DROPPED
) {
1013 netdev_err(adapter
->netdev
, "tx: h_send_logical_lan failed "
1014 "with rc=%ld\n", ret
);
1021 static netdev_tx_t
ibmveth_start_xmit(struct sk_buff
*skb
,
1022 struct net_device
*netdev
)
1024 struct ibmveth_adapter
*adapter
= netdev_priv(netdev
);
1025 unsigned int desc_flags
;
1026 union ibmveth_buf_desc descs
[6];
1028 int force_bounce
= 0;
1029 dma_addr_t dma_addr
;
1030 unsigned long mss
= 0;
1033 * veth handles a maximum of 6 segments including the header, so
1034 * we have to linearize the skb if there are more than this.
1036 if (skb_shinfo(skb
)->nr_frags
> 5 && __skb_linearize(skb
)) {
1037 netdev
->stats
.tx_dropped
++;
1041 /* veth can't checksum offload UDP */
1042 if (skb
->ip_summed
== CHECKSUM_PARTIAL
&&
1043 ((skb
->protocol
== htons(ETH_P_IP
) &&
1044 ip_hdr(skb
)->protocol
!= IPPROTO_TCP
) ||
1045 (skb
->protocol
== htons(ETH_P_IPV6
) &&
1046 ipv6_hdr(skb
)->nexthdr
!= IPPROTO_TCP
)) &&
1047 skb_checksum_help(skb
)) {
1049 netdev_err(netdev
, "tx: failed to checksum packet\n");
1050 netdev
->stats
.tx_dropped
++;
1054 desc_flags
= IBMVETH_BUF_VALID
;
1056 if (skb_is_gso(skb
) && adapter
->fw_large_send_support
)
1057 desc_flags
|= IBMVETH_BUF_LRG_SND
;
1059 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1060 unsigned char *buf
= skb_transport_header(skb
) +
1063 desc_flags
|= (IBMVETH_BUF_NO_CSUM
| IBMVETH_BUF_CSUM_GOOD
);
1065 /* Need to zero out the checksum */
1071 memset(descs
, 0, sizeof(descs
));
1074 * If a linear packet is below the rx threshold then
1075 * copy it into the static bounce buffer. This avoids the
1076 * cost of a TCE insert and remove.
1078 if (force_bounce
|| (!skb_is_nonlinear(skb
) &&
1079 (skb
->len
< tx_copybreak
))) {
1080 skb_copy_from_linear_data(skb
, adapter
->bounce_buffer
,
1083 descs
[0].fields
.flags_len
= desc_flags
| skb
->len
;
1084 descs
[0].fields
.address
= adapter
->bounce_buffer_dma
;
1086 if (ibmveth_send(adapter
, descs
, 0)) {
1087 adapter
->tx_send_failed
++;
1088 netdev
->stats
.tx_dropped
++;
1090 netdev
->stats
.tx_packets
++;
1091 netdev
->stats
.tx_bytes
+= skb
->len
;
1097 /* Map the header */
1098 dma_addr
= dma_map_single(&adapter
->vdev
->dev
, skb
->data
,
1099 skb_headlen(skb
), DMA_TO_DEVICE
);
1100 if (dma_mapping_error(&adapter
->vdev
->dev
, dma_addr
))
1103 descs
[0].fields
.flags_len
= desc_flags
| skb_headlen(skb
);
1104 descs
[0].fields
.address
= dma_addr
;
1107 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1108 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1110 dma_addr
= skb_frag_dma_map(&adapter
->vdev
->dev
, frag
, 0,
1111 skb_frag_size(frag
), DMA_TO_DEVICE
);
1113 if (dma_mapping_error(&adapter
->vdev
->dev
, dma_addr
))
1114 goto map_failed_frags
;
1116 descs
[i
+1].fields
.flags_len
= desc_flags
| skb_frag_size(frag
);
1117 descs
[i
+1].fields
.address
= dma_addr
;
1120 if (skb_is_gso(skb
)) {
1121 if (adapter
->fw_large_send_support
) {
1122 mss
= (unsigned long)skb_shinfo(skb
)->gso_size
;
1123 adapter
->tx_large_packets
++;
1124 } else if (!skb_is_gso_v6(skb
)) {
1125 /* Put -1 in the IP checksum to tell phyp it
1126 * is a largesend packet. Put the mss in
1129 ip_hdr(skb
)->check
= 0xffff;
1130 tcp_hdr(skb
)->check
=
1131 cpu_to_be16(skb_shinfo(skb
)->gso_size
);
1132 adapter
->tx_large_packets
++;
1136 if (ibmveth_send(adapter
, descs
, mss
)) {
1137 adapter
->tx_send_failed
++;
1138 netdev
->stats
.tx_dropped
++;
1140 netdev
->stats
.tx_packets
++;
1141 netdev
->stats
.tx_bytes
+= skb
->len
;
1144 dma_unmap_single(&adapter
->vdev
->dev
,
1145 descs
[0].fields
.address
,
1146 descs
[0].fields
.flags_len
& IBMVETH_BUF_LEN_MASK
,
1149 for (i
= 1; i
< skb_shinfo(skb
)->nr_frags
+ 1; i
++)
1150 dma_unmap_page(&adapter
->vdev
->dev
, descs
[i
].fields
.address
,
1151 descs
[i
].fields
.flags_len
& IBMVETH_BUF_LEN_MASK
,
1155 dev_consume_skb_any(skb
);
1156 return NETDEV_TX_OK
;
1160 for (i
= 0; i
< last
; i
++)
1161 dma_unmap_page(&adapter
->vdev
->dev
, descs
[i
].fields
.address
,
1162 descs
[i
].fields
.flags_len
& IBMVETH_BUF_LEN_MASK
,
1166 if (!firmware_has_feature(FW_FEATURE_CMO
))
1167 netdev_err(netdev
, "tx: unable to map xmit buffer\n");
1168 adapter
->tx_map_failed
++;
1169 if (skb_linearize(skb
)) {
1170 netdev
->stats
.tx_dropped
++;
1177 static int ibmveth_poll(struct napi_struct
*napi
, int budget
)
1179 struct ibmveth_adapter
*adapter
=
1180 container_of(napi
, struct ibmveth_adapter
, napi
);
1181 struct net_device
*netdev
= adapter
->netdev
;
1182 int frames_processed
= 0;
1183 unsigned long lpar_rc
;
1187 while (frames_processed
< budget
) {
1188 if (!ibmveth_rxq_pending_buffer(adapter
))
1192 if (!ibmveth_rxq_buffer_valid(adapter
)) {
1193 wmb(); /* suggested by larson1 */
1194 adapter
->rx_invalid_buffer
++;
1195 netdev_dbg(netdev
, "recycling invalid buffer\n");
1196 ibmveth_rxq_recycle_buffer(adapter
);
1198 struct sk_buff
*skb
, *new_skb
;
1199 int length
= ibmveth_rxq_frame_length(adapter
);
1200 int offset
= ibmveth_rxq_frame_offset(adapter
);
1201 int csum_good
= ibmveth_rxq_csum_good(adapter
);
1203 skb
= ibmveth_rxq_get_buffer(adapter
);
1206 if (length
< rx_copybreak
)
1207 new_skb
= netdev_alloc_skb(netdev
, length
);
1210 skb_copy_to_linear_data(new_skb
,
1214 ibmveth_flush_buffer(skb
->data
,
1216 if (!ibmveth_rxq_recycle_buffer(adapter
))
1220 ibmveth_rxq_harvest_buffer(adapter
);
1221 skb_reserve(skb
, offset
);
1224 skb_put(skb
, length
);
1225 skb
->protocol
= eth_type_trans(skb
, netdev
);
1228 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1229 if (be16_to_cpu(skb
->protocol
) == ETH_P_IP
) {
1230 iph
= (struct iphdr
*)skb
->data
;
1232 /* If the IP checksum is not offloaded and if the packet
1233 * is large send, the checksum must be rebuilt.
1235 if (iph
->check
== 0xffff) {
1237 iph
->check
= ip_fast_csum((unsigned char *)iph
, iph
->ihl
);
1238 adapter
->rx_large_packets
++;
1243 napi_gro_receive(napi
, skb
); /* send it up */
1245 netdev
->stats
.rx_packets
++;
1246 netdev
->stats
.rx_bytes
+= length
;
1251 ibmveth_replenish_task(adapter
);
1253 if (frames_processed
< budget
) {
1254 napi_complete(napi
);
1256 /* We think we are done - reenable interrupts,
1257 * then check once more to make sure we are done.
1259 lpar_rc
= h_vio_signal(adapter
->vdev
->unit_address
,
1262 BUG_ON(lpar_rc
!= H_SUCCESS
);
1264 if (ibmveth_rxq_pending_buffer(adapter
) &&
1265 napi_reschedule(napi
)) {
1266 lpar_rc
= h_vio_signal(adapter
->vdev
->unit_address
,
1272 return frames_processed
;
1275 static irqreturn_t
ibmveth_interrupt(int irq
, void *dev_instance
)
1277 struct net_device
*netdev
= dev_instance
;
1278 struct ibmveth_adapter
*adapter
= netdev_priv(netdev
);
1279 unsigned long lpar_rc
;
1281 if (napi_schedule_prep(&adapter
->napi
)) {
1282 lpar_rc
= h_vio_signal(adapter
->vdev
->unit_address
,
1284 BUG_ON(lpar_rc
!= H_SUCCESS
);
1285 __napi_schedule(&adapter
->napi
);
1290 static void ibmveth_set_multicast_list(struct net_device
*netdev
)
1292 struct ibmveth_adapter
*adapter
= netdev_priv(netdev
);
1293 unsigned long lpar_rc
;
1295 if ((netdev
->flags
& IFF_PROMISC
) ||
1296 (netdev_mc_count(netdev
) > adapter
->mcastFilterSize
)) {
1297 lpar_rc
= h_multicast_ctrl(adapter
->vdev
->unit_address
,
1298 IbmVethMcastEnableRecv
|
1299 IbmVethMcastDisableFiltering
,
1301 if (lpar_rc
!= H_SUCCESS
) {
1302 netdev_err(netdev
, "h_multicast_ctrl rc=%ld when "
1303 "entering promisc mode\n", lpar_rc
);
1306 struct netdev_hw_addr
*ha
;
1307 /* clear the filter table & disable filtering */
1308 lpar_rc
= h_multicast_ctrl(adapter
->vdev
->unit_address
,
1309 IbmVethMcastEnableRecv
|
1310 IbmVethMcastDisableFiltering
|
1311 IbmVethMcastClearFilterTable
,
1313 if (lpar_rc
!= H_SUCCESS
) {
1314 netdev_err(netdev
, "h_multicast_ctrl rc=%ld when "
1315 "attempting to clear filter table\n",
1318 /* add the addresses to the filter table */
1319 netdev_for_each_mc_addr(ha
, netdev
) {
1320 /* add the multicast address to the filter table */
1322 mcast_addr
= ibmveth_encode_mac_addr(ha
->addr
);
1323 lpar_rc
= h_multicast_ctrl(adapter
->vdev
->unit_address
,
1324 IbmVethMcastAddFilter
,
1326 if (lpar_rc
!= H_SUCCESS
) {
1327 netdev_err(netdev
, "h_multicast_ctrl rc=%ld "
1328 "when adding an entry to the filter "
1329 "table\n", lpar_rc
);
1333 /* re-enable filtering */
1334 lpar_rc
= h_multicast_ctrl(adapter
->vdev
->unit_address
,
1335 IbmVethMcastEnableFiltering
,
1337 if (lpar_rc
!= H_SUCCESS
) {
1338 netdev_err(netdev
, "h_multicast_ctrl rc=%ld when "
1339 "enabling filtering\n", lpar_rc
);
1344 static int ibmveth_change_mtu(struct net_device
*dev
, int new_mtu
)
1346 struct ibmveth_adapter
*adapter
= netdev_priv(dev
);
1347 struct vio_dev
*viodev
= adapter
->vdev
;
1348 int new_mtu_oh
= new_mtu
+ IBMVETH_BUFF_OH
;
1350 int need_restart
= 0;
1352 if (new_mtu
< IBMVETH_MIN_MTU
)
1355 for (i
= 0; i
< IBMVETH_NUM_BUFF_POOLS
; i
++)
1356 if (new_mtu_oh
<= adapter
->rx_buff_pool
[i
].buff_size
)
1359 if (i
== IBMVETH_NUM_BUFF_POOLS
)
1362 /* Deactivate all the buffer pools so that the next loop can activate
1363 only the buffer pools necessary to hold the new MTU */
1364 if (netif_running(adapter
->netdev
)) {
1366 adapter
->pool_config
= 1;
1367 ibmveth_close(adapter
->netdev
);
1368 adapter
->pool_config
= 0;
1371 /* Look for an active buffer pool that can hold the new MTU */
1372 for (i
= 0; i
< IBMVETH_NUM_BUFF_POOLS
; i
++) {
1373 adapter
->rx_buff_pool
[i
].active
= 1;
1375 if (new_mtu_oh
<= adapter
->rx_buff_pool
[i
].buff_size
) {
1377 vio_cmo_set_dev_desired(viodev
,
1378 ibmveth_get_desired_dma
1381 return ibmveth_open(adapter
->netdev
);
1387 if (need_restart
&& (rc
= ibmveth_open(adapter
->netdev
)))
1393 #ifdef CONFIG_NET_POLL_CONTROLLER
1394 static void ibmveth_poll_controller(struct net_device
*dev
)
1396 ibmveth_replenish_task(netdev_priv(dev
));
1397 ibmveth_interrupt(dev
->irq
, dev
);
1402 * ibmveth_get_desired_dma - Calculate IO memory desired by the driver
1404 * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
1407 * Number of bytes of IO data the driver will need to perform well.
1409 static unsigned long ibmveth_get_desired_dma(struct vio_dev
*vdev
)
1411 struct net_device
*netdev
= dev_get_drvdata(&vdev
->dev
);
1412 struct ibmveth_adapter
*adapter
;
1413 struct iommu_table
*tbl
;
1418 tbl
= get_iommu_table_base(&vdev
->dev
);
1420 /* netdev inits at probe time along with the structures we need below*/
1422 return IOMMU_PAGE_ALIGN(IBMVETH_IO_ENTITLEMENT_DEFAULT
, tbl
);
1424 adapter
= netdev_priv(netdev
);
1426 ret
= IBMVETH_BUFF_LIST_SIZE
+ IBMVETH_FILT_LIST_SIZE
;
1427 ret
+= IOMMU_PAGE_ALIGN(netdev
->mtu
, tbl
);
1429 for (i
= 0; i
< IBMVETH_NUM_BUFF_POOLS
; i
++) {
1430 /* add the size of the active receive buffers */
1431 if (adapter
->rx_buff_pool
[i
].active
)
1433 adapter
->rx_buff_pool
[i
].size
*
1434 IOMMU_PAGE_ALIGN(adapter
->rx_buff_pool
[i
].
1436 rxqentries
+= adapter
->rx_buff_pool
[i
].size
;
1438 /* add the size of the receive queue entries */
1439 ret
+= IOMMU_PAGE_ALIGN(
1440 rxqentries
* sizeof(struct ibmveth_rx_q_entry
), tbl
);
1445 static int ibmveth_set_mac_addr(struct net_device
*dev
, void *p
)
1447 struct ibmveth_adapter
*adapter
= netdev_priv(dev
);
1448 struct sockaddr
*addr
= p
;
1452 if (!is_valid_ether_addr(addr
->sa_data
))
1453 return -EADDRNOTAVAIL
;
1455 mac_address
= ibmveth_encode_mac_addr(addr
->sa_data
);
1456 rc
= h_change_logical_lan_mac(adapter
->vdev
->unit_address
, mac_address
);
1458 netdev_err(adapter
->netdev
, "h_change_logical_lan_mac failed with rc=%d\n", rc
);
1462 ether_addr_copy(dev
->dev_addr
, addr
->sa_data
);
1467 static const struct net_device_ops ibmveth_netdev_ops
= {
1468 .ndo_open
= ibmveth_open
,
1469 .ndo_stop
= ibmveth_close
,
1470 .ndo_start_xmit
= ibmveth_start_xmit
,
1471 .ndo_set_rx_mode
= ibmveth_set_multicast_list
,
1472 .ndo_do_ioctl
= ibmveth_ioctl
,
1473 .ndo_change_mtu
= ibmveth_change_mtu
,
1474 .ndo_fix_features
= ibmveth_fix_features
,
1475 .ndo_set_features
= ibmveth_set_features
,
1476 .ndo_validate_addr
= eth_validate_addr
,
1477 .ndo_set_mac_address
= ibmveth_set_mac_addr
,
1478 #ifdef CONFIG_NET_POLL_CONTROLLER
1479 .ndo_poll_controller
= ibmveth_poll_controller
,
1483 static int ibmveth_probe(struct vio_dev
*dev
, const struct vio_device_id
*id
)
1486 struct net_device
*netdev
;
1487 struct ibmveth_adapter
*adapter
;
1488 unsigned char *mac_addr_p
;
1489 unsigned int *mcastFilterSize_p
;
1491 unsigned long ret_attr
;
1493 dev_dbg(&dev
->dev
, "entering ibmveth_probe for UA 0x%x\n",
1496 mac_addr_p
= (unsigned char *)vio_get_attribute(dev
, VETH_MAC_ADDR
,
1499 dev_err(&dev
->dev
, "Can't find VETH_MAC_ADDR attribute\n");
1502 /* Workaround for old/broken pHyp */
1505 else if (mac_len
!= 6) {
1506 dev_err(&dev
->dev
, "VETH_MAC_ADDR attribute wrong len %d\n",
1511 mcastFilterSize_p
= (unsigned int *)vio_get_attribute(dev
,
1512 VETH_MCAST_FILTER_SIZE
, NULL
);
1513 if (!mcastFilterSize_p
) {
1514 dev_err(&dev
->dev
, "Can't find VETH_MCAST_FILTER_SIZE "
1519 netdev
= alloc_etherdev(sizeof(struct ibmveth_adapter
));
1524 adapter
= netdev_priv(netdev
);
1525 dev_set_drvdata(&dev
->dev
, netdev
);
1527 adapter
->vdev
= dev
;
1528 adapter
->netdev
= netdev
;
1529 adapter
->mcastFilterSize
= *mcastFilterSize_p
;
1530 adapter
->pool_config
= 0;
1532 netif_napi_add(netdev
, &adapter
->napi
, ibmveth_poll
, 16);
1534 netdev
->irq
= dev
->irq
;
1535 netdev
->netdev_ops
= &ibmveth_netdev_ops
;
1536 netdev
->ethtool_ops
= &netdev_ethtool_ops
;
1537 SET_NETDEV_DEV(netdev
, &dev
->dev
);
1538 netdev
->hw_features
= NETIF_F_SG
| NETIF_F_RXCSUM
|
1539 NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
;
1541 netdev
->features
|= netdev
->hw_features
;
1543 ret
= h_illan_attributes(adapter
->vdev
->unit_address
, 0, 0, &ret_attr
);
1545 /* If running older firmware, TSO should not be enabled by default */
1546 if (ret
== H_SUCCESS
&& (ret_attr
& IBMVETH_ILLAN_LRG_SND_SUPPORT
) &&
1548 netdev
->hw_features
|= NETIF_F_TSO
| NETIF_F_TSO6
;
1549 netdev
->features
|= netdev
->hw_features
;
1551 netdev
->hw_features
|= NETIF_F_TSO
;
1554 memcpy(netdev
->dev_addr
, mac_addr_p
, ETH_ALEN
);
1556 if (firmware_has_feature(FW_FEATURE_CMO
))
1557 memcpy(pool_count
, pool_count_cmo
, sizeof(pool_count
));
1559 for (i
= 0; i
< IBMVETH_NUM_BUFF_POOLS
; i
++) {
1560 struct kobject
*kobj
= &adapter
->rx_buff_pool
[i
].kobj
;
1563 ibmveth_init_buffer_pool(&adapter
->rx_buff_pool
[i
], i
,
1564 pool_count
[i
], pool_size
[i
],
1566 error
= kobject_init_and_add(kobj
, &ktype_veth_pool
,
1567 &dev
->dev
.kobj
, "pool%d", i
);
1569 kobject_uevent(kobj
, KOBJ_ADD
);
1572 netdev_dbg(netdev
, "adapter @ 0x%p\n", adapter
);
1574 adapter
->buffer_list_dma
= DMA_ERROR_CODE
;
1575 adapter
->filter_list_dma
= DMA_ERROR_CODE
;
1576 adapter
->rx_queue
.queue_dma
= DMA_ERROR_CODE
;
1578 netdev_dbg(netdev
, "registering netdev...\n");
1580 ibmveth_set_features(netdev
, netdev
->features
);
1582 rc
= register_netdev(netdev
);
1585 netdev_dbg(netdev
, "failed to register netdev rc=%d\n", rc
);
1586 free_netdev(netdev
);
1590 netdev_dbg(netdev
, "registered\n");
1595 static int ibmveth_remove(struct vio_dev
*dev
)
1597 struct net_device
*netdev
= dev_get_drvdata(&dev
->dev
);
1598 struct ibmveth_adapter
*adapter
= netdev_priv(netdev
);
1601 for (i
= 0; i
< IBMVETH_NUM_BUFF_POOLS
; i
++)
1602 kobject_put(&adapter
->rx_buff_pool
[i
].kobj
);
1604 unregister_netdev(netdev
);
1606 free_netdev(netdev
);
1607 dev_set_drvdata(&dev
->dev
, NULL
);
1612 static struct attribute veth_active_attr
;
1613 static struct attribute veth_num_attr
;
1614 static struct attribute veth_size_attr
;
1616 static ssize_t
veth_pool_show(struct kobject
*kobj
,
1617 struct attribute
*attr
, char *buf
)
1619 struct ibmveth_buff_pool
*pool
= container_of(kobj
,
1620 struct ibmveth_buff_pool
,
1623 if (attr
== &veth_active_attr
)
1624 return sprintf(buf
, "%d\n", pool
->active
);
1625 else if (attr
== &veth_num_attr
)
1626 return sprintf(buf
, "%d\n", pool
->size
);
1627 else if (attr
== &veth_size_attr
)
1628 return sprintf(buf
, "%d\n", pool
->buff_size
);
1632 static ssize_t
veth_pool_store(struct kobject
*kobj
, struct attribute
*attr
,
1633 const char *buf
, size_t count
)
1635 struct ibmveth_buff_pool
*pool
= container_of(kobj
,
1636 struct ibmveth_buff_pool
,
1638 struct net_device
*netdev
= dev_get_drvdata(
1639 container_of(kobj
->parent
, struct device
, kobj
));
1640 struct ibmveth_adapter
*adapter
= netdev_priv(netdev
);
1641 long value
= simple_strtol(buf
, NULL
, 10);
1644 if (attr
== &veth_active_attr
) {
1645 if (value
&& !pool
->active
) {
1646 if (netif_running(netdev
)) {
1647 if (ibmveth_alloc_buffer_pool(pool
)) {
1649 "unable to alloc pool\n");
1653 adapter
->pool_config
= 1;
1654 ibmveth_close(netdev
);
1655 adapter
->pool_config
= 0;
1656 if ((rc
= ibmveth_open(netdev
)))
1661 } else if (!value
&& pool
->active
) {
1662 int mtu
= netdev
->mtu
+ IBMVETH_BUFF_OH
;
1664 /* Make sure there is a buffer pool with buffers that
1665 can hold a packet of the size of the MTU */
1666 for (i
= 0; i
< IBMVETH_NUM_BUFF_POOLS
; i
++) {
1667 if (pool
== &adapter
->rx_buff_pool
[i
])
1669 if (!adapter
->rx_buff_pool
[i
].active
)
1671 if (mtu
<= adapter
->rx_buff_pool
[i
].buff_size
)
1675 if (i
== IBMVETH_NUM_BUFF_POOLS
) {
1676 netdev_err(netdev
, "no active pool >= MTU\n");
1680 if (netif_running(netdev
)) {
1681 adapter
->pool_config
= 1;
1682 ibmveth_close(netdev
);
1684 adapter
->pool_config
= 0;
1685 if ((rc
= ibmveth_open(netdev
)))
1690 } else if (attr
== &veth_num_attr
) {
1691 if (value
<= 0 || value
> IBMVETH_MAX_POOL_COUNT
) {
1694 if (netif_running(netdev
)) {
1695 adapter
->pool_config
= 1;
1696 ibmveth_close(netdev
);
1697 adapter
->pool_config
= 0;
1699 if ((rc
= ibmveth_open(netdev
)))
1705 } else if (attr
== &veth_size_attr
) {
1706 if (value
<= IBMVETH_BUFF_OH
|| value
> IBMVETH_MAX_BUF_SIZE
) {
1709 if (netif_running(netdev
)) {
1710 adapter
->pool_config
= 1;
1711 ibmveth_close(netdev
);
1712 adapter
->pool_config
= 0;
1713 pool
->buff_size
= value
;
1714 if ((rc
= ibmveth_open(netdev
)))
1717 pool
->buff_size
= value
;
1722 /* kick the interrupt handler to allocate/deallocate pools */
1723 ibmveth_interrupt(netdev
->irq
, netdev
);
1728 #define ATTR(_name, _mode) \
1729 struct attribute veth_##_name##_attr = { \
1730 .name = __stringify(_name), .mode = _mode, \
1733 static ATTR(active
, 0644);
1734 static ATTR(num
, 0644);
1735 static ATTR(size
, 0644);
1737 static struct attribute
*veth_pool_attrs
[] = {
1744 static const struct sysfs_ops veth_pool_ops
= {
1745 .show
= veth_pool_show
,
1746 .store
= veth_pool_store
,
1749 static struct kobj_type ktype_veth_pool
= {
1751 .sysfs_ops
= &veth_pool_ops
,
1752 .default_attrs
= veth_pool_attrs
,
1755 static int ibmveth_resume(struct device
*dev
)
1757 struct net_device
*netdev
= dev_get_drvdata(dev
);
1758 ibmveth_interrupt(netdev
->irq
, netdev
);
1762 static struct vio_device_id ibmveth_device_table
[] = {
1763 { "network", "IBM,l-lan"},
1766 MODULE_DEVICE_TABLE(vio
, ibmveth_device_table
);
1768 static struct dev_pm_ops ibmveth_pm_ops
= {
1769 .resume
= ibmveth_resume
1772 static struct vio_driver ibmveth_driver
= {
1773 .id_table
= ibmveth_device_table
,
1774 .probe
= ibmveth_probe
,
1775 .remove
= ibmveth_remove
,
1776 .get_desired_dma
= ibmveth_get_desired_dma
,
1777 .name
= ibmveth_driver_name
,
1778 .pm
= &ibmveth_pm_ops
,
1781 static int __init
ibmveth_module_init(void)
1783 printk(KERN_DEBUG
"%s: %s %s\n", ibmveth_driver_name
,
1784 ibmveth_driver_string
, ibmveth_driver_version
);
1786 return vio_register_driver(&ibmveth_driver
);
1789 static void __exit
ibmveth_module_exit(void)
1791 vio_unregister_driver(&ibmveth_driver
);
1794 module_init(ibmveth_module_init
);
1795 module_exit(ibmveth_module_exit
);