blob: 2f9b12cf9ee5b780a13acdad16f143a630e2f413 [file] [log] [blame]
Kyle Swenson8d8f6542021-03-15 11:02:55 -06001/*
2 * IBM Power Virtual Ethernet Device Driver
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 * Copyright (C) IBM Corporation, 2003, 2010
18 *
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>
24 */
25
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>
37#include <linux/mm.h>
38#include <linux/pm.h>
39#include <linux/ethtool.h>
40#include <linux/in.h>
41#include <linux/ip.h>
42#include <linux/ipv6.h>
43#include <linux/slab.h>
44#include <asm/hvcall.h>
45#include <linux/atomic.h>
46#include <asm/vio.h>
47#include <asm/iommu.h>
48#include <asm/firmware.h>
49
50#include "ibmveth.h"
51
52static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
53static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
54static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev);
55
56static struct kobj_type ktype_veth_pool;
57
58
59static const char ibmveth_driver_name[] = "ibmveth";
60static const char ibmveth_driver_string[] = "IBM Power Virtual Ethernet Driver";
61#define ibmveth_driver_version "1.06"
62
63MODULE_AUTHOR("Santiago Leon <santil@linux.vnet.ibm.com>");
64MODULE_DESCRIPTION("IBM Power Virtual Ethernet Driver");
65MODULE_LICENSE("GPL");
66MODULE_VERSION(ibmveth_driver_version);
67
68static unsigned int tx_copybreak __read_mostly = 128;
69module_param(tx_copybreak, uint, 0644);
70MODULE_PARM_DESC(tx_copybreak,
71 "Maximum size of packet that is copied to a new buffer on transmit");
72
73static unsigned int rx_copybreak __read_mostly = 128;
74module_param(rx_copybreak, uint, 0644);
75MODULE_PARM_DESC(rx_copybreak,
76 "Maximum size of packet that is copied to a new buffer on receive");
77
78static unsigned int rx_flush __read_mostly = 0;
79module_param(rx_flush, uint, 0644);
80MODULE_PARM_DESC(rx_flush, "Flush receive buffers before use");
81
82static bool old_large_send __read_mostly;
83module_param(old_large_send, bool, S_IRUGO);
84MODULE_PARM_DESC(old_large_send,
85 "Use old large send method on firmware that supports the new method");
86
87struct ibmveth_stat {
88 char name[ETH_GSTRING_LEN];
89 int offset;
90};
91
92#define IBMVETH_STAT_OFF(stat) offsetof(struct ibmveth_adapter, stat)
93#define IBMVETH_GET_STAT(a, off) *((u64 *)(((unsigned long)(a)) + off))
94
95struct 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) }
111};
112
113/* simple methods of getting data from the current rxq entry */
114static inline u32 ibmveth_rxq_flags(struct ibmveth_adapter *adapter)
115{
116 return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].flags_off);
117}
118
119static inline int ibmveth_rxq_toggle(struct ibmveth_adapter *adapter)
120{
121 return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_TOGGLE) >>
122 IBMVETH_RXQ_TOGGLE_SHIFT;
123}
124
125static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
126{
127 return ibmveth_rxq_toggle(adapter) == adapter->rx_queue.toggle;
128}
129
130static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
131{
132 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_VALID;
133}
134
135static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
136{
137 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_OFF_MASK;
138}
139
140static inline int ibmveth_rxq_large_packet(struct ibmveth_adapter *adapter)
141{
142 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_LRG_PKT;
143}
144
145static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
146{
147 return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
148}
149
150static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
151{
152 return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_CSUM_GOOD;
153}
154
155/* setup the initial settings for a buffer pool */
156static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool,
157 u32 pool_index, u32 pool_size,
158 u32 buff_size, u32 pool_active)
159{
160 pool->size = pool_size;
161 pool->index = pool_index;
162 pool->buff_size = buff_size;
163 pool->threshold = pool_size * 7 / 8;
164 pool->active = pool_active;
165}
166
167/* allocate and setup an buffer pool - called during open */
168static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
169{
170 int i;
171
172 pool->free_map = kmalloc(sizeof(u16) * pool->size, GFP_KERNEL);
173
174 if (!pool->free_map)
175 return -1;
176
177 pool->dma_addr = kmalloc(sizeof(dma_addr_t) * pool->size, GFP_KERNEL);
178 if (!pool->dma_addr) {
179 kfree(pool->free_map);
180 pool->free_map = NULL;
181 return -1;
182 }
183
184 pool->skbuff = kcalloc(pool->size, sizeof(void *), GFP_KERNEL);
185
186 if (!pool->skbuff) {
187 kfree(pool->dma_addr);
188 pool->dma_addr = NULL;
189
190 kfree(pool->free_map);
191 pool->free_map = NULL;
192 return -1;
193 }
194
195 memset(pool->dma_addr, 0, sizeof(dma_addr_t) * pool->size);
196
197 for (i = 0; i < pool->size; ++i)
198 pool->free_map[i] = i;
199
200 atomic_set(&pool->available, 0);
201 pool->producer_index = 0;
202 pool->consumer_index = 0;
203
204 return 0;
205}
206
207static inline void ibmveth_flush_buffer(void *addr, unsigned long length)
208{
209 unsigned long offset;
210
211 for (offset = 0; offset < length; offset += SMP_CACHE_BYTES)
212 asm("dcbfl %0,%1" :: "b" (addr), "r" (offset));
213}
214
215/* replenish the buffers for a pool. note that we don't need to
216 * skb_reserve these since they are used for incoming...
217 */
218static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter,
219 struct ibmveth_buff_pool *pool)
220{
221 u32 i;
222 u32 count = pool->size - atomic_read(&pool->available);
223 u32 buffers_added = 0;
224 struct sk_buff *skb;
225 unsigned int free_index, index;
226 u64 correlator;
227 unsigned long lpar_rc;
228 dma_addr_t dma_addr;
229
230 mb();
231
232 for (i = 0; i < count; ++i) {
233 union ibmveth_buf_desc desc;
234
235 skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
236
237 if (!skb) {
238 netdev_dbg(adapter->netdev,
239 "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++;
246 if (pool->consumer_index >= pool->size)
247 pool->consumer_index = 0;
248 index = pool->free_map[free_index];
249
250 BUG_ON(index == IBM_VETH_INVALID_MAP);
251 BUG_ON(pool->skbuff[index] != NULL);
252
253 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
254 pool->buff_size, DMA_FROM_DEVICE);
255
256 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
257 goto failure;
258
259 pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
260 pool->dma_addr[index] = dma_addr;
261 pool->skbuff[index] = skb;
262
263 correlator = ((u64)pool->index << 32) | index;
264 *(u64 *)skb->data = correlator;
265
266 desc.fields.flags_len = IBMVETH_BUF_VALID | pool->buff_size;
267 desc.fields.address = dma_addr;
268
269 if (rx_flush) {
270 unsigned int len = min(pool->buff_size,
271 adapter->netdev->mtu +
272 IBMVETH_BUFF_OH);
273 ibmveth_flush_buffer(skb->data, len);
274 }
275 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address,
276 desc.desc);
277
278 if (lpar_rc != H_SUCCESS) {
279 goto failure;
280 } else {
281 buffers_added++;
282 adapter->replenish_add_buff_success++;
283 }
284 }
285
286 mb();
287 atomic_add(buffers_added, &(pool->available));
288 return;
289
290failure:
291 pool->free_map[free_index] = index;
292 pool->skbuff[index] = NULL;
293 if (pool->consumer_index == 0)
294 pool->consumer_index = pool->size - 1;
295 else
296 pool->consumer_index--;
297 if (!dma_mapping_error(&adapter->vdev->dev, dma_addr))
298 dma_unmap_single(&adapter->vdev->dev,
299 pool->dma_addr[index], pool->buff_size,
300 DMA_FROM_DEVICE);
301 dev_kfree_skb_any(skb);
302 adapter->replenish_add_buff_failure++;
303
304 mb();
305 atomic_add(buffers_added, &(pool->available));
306}
307
308/*
309 * The final 8 bytes of the buffer list is a counter of frames dropped
310 * because there was not a buffer in the buffer list capable of holding
311 * the frame.
312 */
313static void ibmveth_update_rx_no_buffer(struct ibmveth_adapter *adapter)
314{
315 __be64 *p = adapter->buffer_list_addr + 4096 - 8;
316
317 adapter->rx_no_buffer = be64_to_cpup(p);
318}
319
320/* replenish routine */
321static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
322{
323 int i;
324
325 adapter->replenish_task_cycles++;
326
327 for (i = (IBMVETH_NUM_BUFF_POOLS - 1); i >= 0; i--) {
328 struct ibmveth_buff_pool *pool = &adapter->rx_buff_pool[i];
329
330 if (pool->active &&
331 (atomic_read(&pool->available) < pool->threshold))
332 ibmveth_replenish_buffer_pool(adapter, pool);
333 }
334
335 ibmveth_update_rx_no_buffer(adapter);
336}
337
338/* empty and free ana buffer pool - also used to do cleanup in error paths */
339static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter,
340 struct ibmveth_buff_pool *pool)
341{
342 int i;
343
344 kfree(pool->free_map);
345 pool->free_map = NULL;
346
347 if (pool->skbuff && pool->dma_addr) {
348 for (i = 0; i < pool->size; ++i) {
349 struct sk_buff *skb = pool->skbuff[i];
350 if (skb) {
351 dma_unmap_single(&adapter->vdev->dev,
352 pool->dma_addr[i],
353 pool->buff_size,
354 DMA_FROM_DEVICE);
355 dev_kfree_skb_any(skb);
356 pool->skbuff[i] = NULL;
357 }
358 }
359 }
360
361 if (pool->dma_addr) {
362 kfree(pool->dma_addr);
363 pool->dma_addr = NULL;
364 }
365
366 if (pool->skbuff) {
367 kfree(pool->skbuff);
368 pool->skbuff = NULL;
369 }
370}
371
372/* remove a buffer from a pool */
373static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter,
374 u64 correlator)
375{
376 unsigned int pool = correlator >> 32;
377 unsigned int index = correlator & 0xffffffffUL;
378 unsigned int free_index;
379 struct sk_buff *skb;
380
381 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
382 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
383
384 skb = adapter->rx_buff_pool[pool].skbuff[index];
385
386 BUG_ON(skb == NULL);
387
388 adapter->rx_buff_pool[pool].skbuff[index] = NULL;
389
390 dma_unmap_single(&adapter->vdev->dev,
391 adapter->rx_buff_pool[pool].dma_addr[index],
392 adapter->rx_buff_pool[pool].buff_size,
393 DMA_FROM_DEVICE);
394
395 free_index = adapter->rx_buff_pool[pool].producer_index;
396 adapter->rx_buff_pool[pool].producer_index++;
397 if (adapter->rx_buff_pool[pool].producer_index >=
398 adapter->rx_buff_pool[pool].size)
399 adapter->rx_buff_pool[pool].producer_index = 0;
400 adapter->rx_buff_pool[pool].free_map[free_index] = index;
401
402 mb();
403
404 atomic_dec(&(adapter->rx_buff_pool[pool].available));
405}
406
407/* get the current buffer on the rx queue */
408static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
409{
410 u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
411 unsigned int pool = correlator >> 32;
412 unsigned int index = correlator & 0xffffffffUL;
413
414 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
415 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
416
417 return adapter->rx_buff_pool[pool].skbuff[index];
418}
419
420/* recycle the current buffer on the rx queue */
421static int ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
422{
423 u32 q_index = adapter->rx_queue.index;
424 u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
425 unsigned int pool = correlator >> 32;
426 unsigned int index = correlator & 0xffffffffUL;
427 union ibmveth_buf_desc desc;
428 unsigned long lpar_rc;
429 int ret = 1;
430
431 BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
432 BUG_ON(index >= adapter->rx_buff_pool[pool].size);
433
434 if (!adapter->rx_buff_pool[pool].active) {
435 ibmveth_rxq_harvest_buffer(adapter);
436 ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
437 goto out;
438 }
439
440 desc.fields.flags_len = IBMVETH_BUF_VALID |
441 adapter->rx_buff_pool[pool].buff_size;
442 desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
443
444 lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
445
446 if (lpar_rc != H_SUCCESS) {
447 netdev_dbg(adapter->netdev, "h_add_logical_lan_buffer failed "
448 "during recycle rc=%ld", lpar_rc);
449 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
450 ret = 0;
451 }
452
453 if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
454 adapter->rx_queue.index = 0;
455 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
456 }
457
458out:
459 return ret;
460}
461
462static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
463{
464 ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
465
466 if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
467 adapter->rx_queue.index = 0;
468 adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
469 }
470}
471
472static void ibmveth_cleanup(struct ibmveth_adapter *adapter)
473{
474 int i;
475 struct device *dev = &adapter->vdev->dev;
476
477 if (adapter->buffer_list_addr != NULL) {
478 if (!dma_mapping_error(dev, adapter->buffer_list_dma)) {
479 dma_unmap_single(dev, adapter->buffer_list_dma, 4096,
480 DMA_BIDIRECTIONAL);
481 adapter->buffer_list_dma = DMA_ERROR_CODE;
482 }
483 free_page((unsigned long)adapter->buffer_list_addr);
484 adapter->buffer_list_addr = NULL;
485 }
486
487 if (adapter->filter_list_addr != NULL) {
488 if (!dma_mapping_error(dev, adapter->filter_list_dma)) {
489 dma_unmap_single(dev, adapter->filter_list_dma, 4096,
490 DMA_BIDIRECTIONAL);
491 adapter->filter_list_dma = DMA_ERROR_CODE;
492 }
493 free_page((unsigned long)adapter->filter_list_addr);
494 adapter->filter_list_addr = NULL;
495 }
496
497 if (adapter->rx_queue.queue_addr != NULL) {
498 dma_free_coherent(dev, adapter->rx_queue.queue_len,
499 adapter->rx_queue.queue_addr,
500 adapter->rx_queue.queue_dma);
501 adapter->rx_queue.queue_addr = NULL;
502 }
503
504 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
505 if (adapter->rx_buff_pool[i].active)
506 ibmveth_free_buffer_pool(adapter,
507 &adapter->rx_buff_pool[i]);
508
509 if (adapter->bounce_buffer != NULL) {
510 if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
511 dma_unmap_single(&adapter->vdev->dev,
512 adapter->bounce_buffer_dma,
513 adapter->netdev->mtu + IBMVETH_BUFF_OH,
514 DMA_BIDIRECTIONAL);
515 adapter->bounce_buffer_dma = DMA_ERROR_CODE;
516 }
517 kfree(adapter->bounce_buffer);
518 adapter->bounce_buffer = NULL;
519 }
520}
521
522static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
523 union ibmveth_buf_desc rxq_desc, u64 mac_address)
524{
525 int rc, try_again = 1;
526
527 /*
528 * After a kexec the adapter will still be open, so our attempt to
529 * open it will fail. So if we get a failure we free the adapter and
530 * try again, but only once.
531 */
532retry:
533 rc = h_register_logical_lan(adapter->vdev->unit_address,
534 adapter->buffer_list_dma, rxq_desc.desc,
535 adapter->filter_list_dma, mac_address);
536
537 if (rc != H_SUCCESS && try_again) {
538 do {
539 rc = h_free_logical_lan(adapter->vdev->unit_address);
540 } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
541
542 try_again = 0;
543 goto retry;
544 }
545
546 return rc;
547}
548
549static u64 ibmveth_encode_mac_addr(u8 *mac)
550{
551 int i;
552 u64 encoded = 0;
553
554 for (i = 0; i < ETH_ALEN; i++)
555 encoded = (encoded << 8) | mac[i];
556
557 return encoded;
558}
559
560static int ibmveth_open(struct net_device *netdev)
561{
562 struct ibmveth_adapter *adapter = netdev_priv(netdev);
563 u64 mac_address;
564 int rxq_entries = 1;
565 unsigned long lpar_rc;
566 int rc;
567 union ibmveth_buf_desc rxq_desc;
568 int i;
569 struct device *dev;
570
571 netdev_dbg(netdev, "open starting\n");
572
573 napi_enable(&adapter->napi);
574
575 for(i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
576 rxq_entries += adapter->rx_buff_pool[i].size;
577
578 adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
579 adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
580
581 if (!adapter->buffer_list_addr || !adapter->filter_list_addr) {
582 netdev_err(netdev, "unable to allocate filter or buffer list "
583 "pages\n");
584 rc = -ENOMEM;
585 goto err_out;
586 }
587
588 dev = &adapter->vdev->dev;
589
590 adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) *
591 rxq_entries;
592 adapter->rx_queue.queue_addr =
593 dma_alloc_coherent(dev, adapter->rx_queue.queue_len,
594 &adapter->rx_queue.queue_dma, GFP_KERNEL);
595 if (!adapter->rx_queue.queue_addr) {
596 rc = -ENOMEM;
597 goto err_out;
598 }
599
600 adapter->buffer_list_dma = dma_map_single(dev,
601 adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
602 adapter->filter_list_dma = dma_map_single(dev,
603 adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
604
605 if ((dma_mapping_error(dev, adapter->buffer_list_dma)) ||
606 (dma_mapping_error(dev, adapter->filter_list_dma))) {
607 netdev_err(netdev, "unable to map filter or buffer list "
608 "pages\n");
609 rc = -ENOMEM;
610 goto err_out;
611 }
612
613 adapter->rx_queue.index = 0;
614 adapter->rx_queue.num_slots = rxq_entries;
615 adapter->rx_queue.toggle = 1;
616
617 mac_address = ibmveth_encode_mac_addr(netdev->dev_addr);
618
619 rxq_desc.fields.flags_len = IBMVETH_BUF_VALID |
620 adapter->rx_queue.queue_len;
621 rxq_desc.fields.address = adapter->rx_queue.queue_dma;
622
623 netdev_dbg(netdev, "buffer list @ 0x%p\n", adapter->buffer_list_addr);
624 netdev_dbg(netdev, "filter list @ 0x%p\n", adapter->filter_list_addr);
625 netdev_dbg(netdev, "receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
626
627 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
628
629 lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
630
631 if (lpar_rc != H_SUCCESS) {
632 netdev_err(netdev, "h_register_logical_lan failed with %ld\n",
633 lpar_rc);
634 netdev_err(netdev, "buffer TCE:0x%llx filter TCE:0x%llx rxq "
635 "desc:0x%llx MAC:0x%llx\n",
636 adapter->buffer_list_dma,
637 adapter->filter_list_dma,
638 rxq_desc.desc,
639 mac_address);
640 rc = -ENONET;
641 goto err_out;
642 }
643
644 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
645 if (!adapter->rx_buff_pool[i].active)
646 continue;
647 if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
648 netdev_err(netdev, "unable to alloc pool\n");
649 adapter->rx_buff_pool[i].active = 0;
650 rc = -ENOMEM;
651 goto err_out;
652 }
653 }
654
655 netdev_dbg(netdev, "registering irq 0x%x\n", netdev->irq);
656 rc = request_irq(netdev->irq, ibmveth_interrupt, 0, netdev->name,
657 netdev);
658 if (rc != 0) {
659 netdev_err(netdev, "unable to request irq 0x%x, rc %d\n",
660 netdev->irq, rc);
661 do {
662 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
663 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
664
665 goto err_out;
666 }
667
668 adapter->bounce_buffer =
669 kmalloc(netdev->mtu + IBMVETH_BUFF_OH, GFP_KERNEL);
670 if (!adapter->bounce_buffer) {
671 rc = -ENOMEM;
672 goto err_out_free_irq;
673 }
674 adapter->bounce_buffer_dma =
675 dma_map_single(&adapter->vdev->dev, adapter->bounce_buffer,
676 netdev->mtu + IBMVETH_BUFF_OH, DMA_BIDIRECTIONAL);
677 if (dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
678 netdev_err(netdev, "unable to map bounce buffer\n");
679 rc = -ENOMEM;
680 goto err_out_free_irq;
681 }
682
683 netdev_dbg(netdev, "initial replenish cycle\n");
684 ibmveth_interrupt(netdev->irq, netdev);
685
686 netif_start_queue(netdev);
687
688 netdev_dbg(netdev, "open complete\n");
689
690 return 0;
691
692err_out_free_irq:
693 free_irq(netdev->irq, netdev);
694err_out:
695 ibmveth_cleanup(adapter);
696 napi_disable(&adapter->napi);
697 return rc;
698}
699
700static int ibmveth_close(struct net_device *netdev)
701{
702 struct ibmveth_adapter *adapter = netdev_priv(netdev);
703 long lpar_rc;
704
705 netdev_dbg(netdev, "close starting\n");
706
707 napi_disable(&adapter->napi);
708
709 if (!adapter->pool_config)
710 netif_stop_queue(netdev);
711
712 h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
713
714 do {
715 lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
716 } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
717
718 if (lpar_rc != H_SUCCESS) {
719 netdev_err(netdev, "h_free_logical_lan failed with %lx, "
720 "continuing with close\n", lpar_rc);
721 }
722
723 free_irq(netdev->irq, netdev);
724
725 ibmveth_update_rx_no_buffer(adapter);
726
727 ibmveth_cleanup(adapter);
728
729 netdev_dbg(netdev, "close complete\n");
730
731 return 0;
732}
733
734static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
735{
736 cmd->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
737 SUPPORTED_FIBRE);
738 cmd->advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
739 ADVERTISED_FIBRE);
740 ethtool_cmd_speed_set(cmd, SPEED_1000);
741 cmd->duplex = DUPLEX_FULL;
742 cmd->port = PORT_FIBRE;
743 cmd->phy_address = 0;
744 cmd->transceiver = XCVR_INTERNAL;
745 cmd->autoneg = AUTONEG_ENABLE;
746 cmd->maxtxpkt = 0;
747 cmd->maxrxpkt = 1;
748 return 0;
749}
750
751static void netdev_get_drvinfo(struct net_device *dev,
752 struct ethtool_drvinfo *info)
753{
754 strlcpy(info->driver, ibmveth_driver_name, sizeof(info->driver));
755 strlcpy(info->version, ibmveth_driver_version, sizeof(info->version));
756}
757
758static netdev_features_t ibmveth_fix_features(struct net_device *dev,
759 netdev_features_t features)
760{
761 /*
762 * Since the ibmveth firmware interface does not have the
763 * concept of separate tx/rx checksum offload enable, if rx
764 * checksum is disabled we also have to disable tx checksum
765 * offload. Once we disable rx checksum offload, we are no
766 * longer allowed to send tx buffers that are not properly
767 * checksummed.
768 */
769
770 if (!(features & NETIF_F_RXCSUM))
771 features &= ~NETIF_F_ALL_CSUM;
772
773 return features;
774}
775
776static int ibmveth_set_csum_offload(struct net_device *dev, u32 data)
777{
778 struct ibmveth_adapter *adapter = netdev_priv(dev);
779 unsigned long set_attr, clr_attr, ret_attr;
780 unsigned long set_attr6, clr_attr6;
781 long ret, ret4, ret6;
782 int rc1 = 0, rc2 = 0;
783 int restart = 0;
784
785 if (netif_running(dev)) {
786 restart = 1;
787 adapter->pool_config = 1;
788 ibmveth_close(dev);
789 adapter->pool_config = 0;
790 }
791
792 set_attr = 0;
793 clr_attr = 0;
794 set_attr6 = 0;
795 clr_attr6 = 0;
796
797 if (data) {
798 set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
799 set_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
800 } else {
801 clr_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
802 clr_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
803 }
804
805 ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
806
807 if (ret == H_SUCCESS && !(ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK) &&
808 !(ret_attr & IBMVETH_ILLAN_TRUNK_PRI_MASK) &&
809 (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
810 ret4 = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
811 set_attr, &ret_attr);
812
813 if (ret4 != H_SUCCESS) {
814 netdev_err(dev, "unable to change IPv4 checksum "
815 "offload settings. %d rc=%ld\n",
816 data, ret4);
817
818 h_illan_attributes(adapter->vdev->unit_address,
819 set_attr, clr_attr, &ret_attr);
820
821 if (data == 1)
822 dev->features &= ~NETIF_F_IP_CSUM;
823
824 } else {
825 adapter->fw_ipv4_csum_support = data;
826 }
827
828 ret6 = h_illan_attributes(adapter->vdev->unit_address,
829 clr_attr6, set_attr6, &ret_attr);
830
831 if (ret6 != H_SUCCESS) {
832 netdev_err(dev, "unable to change IPv6 checksum "
833 "offload settings. %d rc=%ld\n",
834 data, ret6);
835
836 h_illan_attributes(adapter->vdev->unit_address,
837 set_attr6, clr_attr6, &ret_attr);
838
839 if (data == 1)
840 dev->features &= ~NETIF_F_IPV6_CSUM;
841
842 } else
843 adapter->fw_ipv6_csum_support = data;
844
845 if (ret4 == H_SUCCESS || ret6 == H_SUCCESS)
846 adapter->rx_csum = data;
847 else
848 rc1 = -EIO;
849 } else {
850 rc1 = -EIO;
851 netdev_err(dev, "unable to change checksum offload settings."
852 " %d rc=%ld ret_attr=%lx\n", data, ret,
853 ret_attr);
854 }
855
856 if (restart)
857 rc2 = ibmveth_open(dev);
858
859 return rc1 ? rc1 : rc2;
860}
861
862static int ibmveth_set_tso(struct net_device *dev, u32 data)
863{
864 struct ibmveth_adapter *adapter = netdev_priv(dev);
865 unsigned long set_attr, clr_attr, ret_attr;
866 long ret1, ret2;
867 int rc1 = 0, rc2 = 0;
868 int restart = 0;
869
870 if (netif_running(dev)) {
871 restart = 1;
872 adapter->pool_config = 1;
873 ibmveth_close(dev);
874 adapter->pool_config = 0;
875 }
876
877 set_attr = 0;
878 clr_attr = 0;
879
880 if (data)
881 set_attr = IBMVETH_ILLAN_LRG_SR_ENABLED;
882 else
883 clr_attr = IBMVETH_ILLAN_LRG_SR_ENABLED;
884
885 ret1 = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
886
887 if (ret1 == H_SUCCESS && (ret_attr & IBMVETH_ILLAN_LRG_SND_SUPPORT) &&
888 !old_large_send) {
889 ret2 = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
890 set_attr, &ret_attr);
891
892 if (ret2 != H_SUCCESS) {
893 netdev_err(dev, "unable to change tso settings. %d rc=%ld\n",
894 data, ret2);
895
896 h_illan_attributes(adapter->vdev->unit_address,
897 set_attr, clr_attr, &ret_attr);
898
899 if (data == 1)
900 dev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
901 rc1 = -EIO;
902
903 } else {
904 adapter->fw_large_send_support = data;
905 adapter->large_send = data;
906 }
907 } else {
908 /* Older firmware version of large send offload does not
909 * support tcp6/ipv6
910 */
911 if (data == 1) {
912 dev->features &= ~NETIF_F_TSO6;
913 netdev_info(dev, "TSO feature requires all partitions to have updated driver");
914 }
915 adapter->large_send = data;
916 }
917
918 if (restart)
919 rc2 = ibmveth_open(dev);
920
921 return rc1 ? rc1 : rc2;
922}
923
924static int ibmveth_set_features(struct net_device *dev,
925 netdev_features_t features)
926{
927 struct ibmveth_adapter *adapter = netdev_priv(dev);
928 int rx_csum = !!(features & NETIF_F_RXCSUM);
929 int large_send = !!(features & (NETIF_F_TSO | NETIF_F_TSO6));
930 int rc1 = 0, rc2 = 0;
931
932 if (rx_csum != adapter->rx_csum) {
933 rc1 = ibmveth_set_csum_offload(dev, rx_csum);
934 if (rc1 && !adapter->rx_csum)
935 dev->features =
936 features & ~(NETIF_F_ALL_CSUM | NETIF_F_RXCSUM);
937 }
938
939 if (large_send != adapter->large_send) {
940 rc2 = ibmveth_set_tso(dev, large_send);
941 if (rc2 && !adapter->large_send)
942 dev->features =
943 features & ~(NETIF_F_TSO | NETIF_F_TSO6);
944 }
945
946 return rc1 ? rc1 : rc2;
947}
948
949static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
950{
951 int i;
952
953 if (stringset != ETH_SS_STATS)
954 return;
955
956 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
957 memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
958}
959
960static int ibmveth_get_sset_count(struct net_device *dev, int sset)
961{
962 switch (sset) {
963 case ETH_SS_STATS:
964 return ARRAY_SIZE(ibmveth_stats);
965 default:
966 return -EOPNOTSUPP;
967 }
968}
969
970static void ibmveth_get_ethtool_stats(struct net_device *dev,
971 struct ethtool_stats *stats, u64 *data)
972{
973 int i;
974 struct ibmveth_adapter *adapter = netdev_priv(dev);
975
976 for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
977 data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
978}
979
980static const struct ethtool_ops netdev_ethtool_ops = {
981 .get_drvinfo = netdev_get_drvinfo,
982 .get_settings = netdev_get_settings,
983 .get_link = ethtool_op_get_link,
984 .get_strings = ibmveth_get_strings,
985 .get_sset_count = ibmveth_get_sset_count,
986 .get_ethtool_stats = ibmveth_get_ethtool_stats,
987};
988
989static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
990{
991 return -EOPNOTSUPP;
992}
993
994#define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
995
996static int ibmveth_send(struct ibmveth_adapter *adapter,
997 union ibmveth_buf_desc *descs, unsigned long mss)
998{
999 unsigned long correlator;
1000 unsigned int retry_count;
1001 unsigned long ret;
1002
1003 /*
1004 * The retry count sets a maximum for the number of broadcast and
1005 * multicast destinations within the system.
1006 */
1007 retry_count = 1024;
1008 correlator = 0;
1009 do {
1010 ret = h_send_logical_lan(adapter->vdev->unit_address,
1011 descs[0].desc, descs[1].desc,
1012 descs[2].desc, descs[3].desc,
1013 descs[4].desc, descs[5].desc,
1014 correlator, &correlator, mss,
1015 adapter->fw_large_send_support);
1016 } while ((ret == H_BUSY) && (retry_count--));
1017
1018 if (ret != H_SUCCESS && ret != H_DROPPED) {
1019 netdev_err(adapter->netdev, "tx: h_send_logical_lan failed "
1020 "with rc=%ld\n", ret);
1021 return 1;
1022 }
1023
1024 return 0;
1025}
1026
1027static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb,
1028 struct net_device *netdev)
1029{
1030 struct ibmveth_adapter *adapter = netdev_priv(netdev);
1031 unsigned int desc_flags;
1032 union ibmveth_buf_desc descs[6];
1033 int last, i;
1034 int force_bounce = 0;
1035 dma_addr_t dma_addr;
1036 unsigned long mss = 0;
1037
1038 /*
1039 * veth handles a maximum of 6 segments including the header, so
1040 * we have to linearize the skb if there are more than this.
1041 */
1042 if (skb_shinfo(skb)->nr_frags > 5 && __skb_linearize(skb)) {
1043 netdev->stats.tx_dropped++;
1044 goto out;
1045 }
1046
1047 /* veth can't checksum offload UDP */
1048 if (skb->ip_summed == CHECKSUM_PARTIAL &&
1049 ((skb->protocol == htons(ETH_P_IP) &&
1050 ip_hdr(skb)->protocol != IPPROTO_TCP) ||
1051 (skb->protocol == htons(ETH_P_IPV6) &&
1052 ipv6_hdr(skb)->nexthdr != IPPROTO_TCP)) &&
1053 skb_checksum_help(skb)) {
1054
1055 netdev_err(netdev, "tx: failed to checksum packet\n");
1056 netdev->stats.tx_dropped++;
1057 goto out;
1058 }
1059
1060 desc_flags = IBMVETH_BUF_VALID;
1061
1062 if (skb_is_gso(skb) && adapter->fw_large_send_support)
1063 desc_flags |= IBMVETH_BUF_LRG_SND;
1064
1065 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1066 unsigned char *buf = skb_transport_header(skb) +
1067 skb->csum_offset;
1068
1069 desc_flags |= (IBMVETH_BUF_NO_CSUM | IBMVETH_BUF_CSUM_GOOD);
1070
1071 /* Need to zero out the checksum */
1072 buf[0] = 0;
1073 buf[1] = 0;
1074 }
1075
1076retry_bounce:
1077 memset(descs, 0, sizeof(descs));
1078
1079 /*
1080 * If a linear packet is below the rx threshold then
1081 * copy it into the static bounce buffer. This avoids the
1082 * cost of a TCE insert and remove.
1083 */
1084 if (force_bounce || (!skb_is_nonlinear(skb) &&
1085 (skb->len < tx_copybreak))) {
1086 skb_copy_from_linear_data(skb, adapter->bounce_buffer,
1087 skb->len);
1088
1089 descs[0].fields.flags_len = desc_flags | skb->len;
1090 descs[0].fields.address = adapter->bounce_buffer_dma;
1091
1092 if (ibmveth_send(adapter, descs, 0)) {
1093 adapter->tx_send_failed++;
1094 netdev->stats.tx_dropped++;
1095 } else {
1096 netdev->stats.tx_packets++;
1097 netdev->stats.tx_bytes += skb->len;
1098 }
1099
1100 goto out;
1101 }
1102
1103 /* Map the header */
1104 dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
1105 skb_headlen(skb), DMA_TO_DEVICE);
1106 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
1107 goto map_failed;
1108
1109 descs[0].fields.flags_len = desc_flags | skb_headlen(skb);
1110 descs[0].fields.address = dma_addr;
1111
1112 /* Map the frags */
1113 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1114 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1115
1116 dma_addr = skb_frag_dma_map(&adapter->vdev->dev, frag, 0,
1117 skb_frag_size(frag), DMA_TO_DEVICE);
1118
1119 if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
1120 goto map_failed_frags;
1121
1122 descs[i+1].fields.flags_len = desc_flags | skb_frag_size(frag);
1123 descs[i+1].fields.address = dma_addr;
1124 }
1125
1126 if (skb_is_gso(skb)) {
1127 if (adapter->fw_large_send_support) {
1128 mss = (unsigned long)skb_shinfo(skb)->gso_size;
1129 adapter->tx_large_packets++;
1130 } else if (!skb_is_gso_v6(skb)) {
1131 /* Put -1 in the IP checksum to tell phyp it
1132 * is a largesend packet. Put the mss in
1133 * the TCP checksum.
1134 */
1135 ip_hdr(skb)->check = 0xffff;
1136 tcp_hdr(skb)->check =
1137 cpu_to_be16(skb_shinfo(skb)->gso_size);
1138 adapter->tx_large_packets++;
1139 }
1140 }
1141
1142 if (ibmveth_send(adapter, descs, mss)) {
1143 adapter->tx_send_failed++;
1144 netdev->stats.tx_dropped++;
1145 } else {
1146 netdev->stats.tx_packets++;
1147 netdev->stats.tx_bytes += skb->len;
1148 }
1149
1150 dma_unmap_single(&adapter->vdev->dev,
1151 descs[0].fields.address,
1152 descs[0].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1153 DMA_TO_DEVICE);
1154
1155 for (i = 1; i < skb_shinfo(skb)->nr_frags + 1; i++)
1156 dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
1157 descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1158 DMA_TO_DEVICE);
1159
1160out:
1161 dev_consume_skb_any(skb);
1162 return NETDEV_TX_OK;
1163
1164map_failed_frags:
1165 last = i+1;
1166 for (i = 0; i < last; i++)
1167 dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
1168 descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
1169 DMA_TO_DEVICE);
1170
1171map_failed:
1172 if (!firmware_has_feature(FW_FEATURE_CMO))
1173 netdev_err(netdev, "tx: unable to map xmit buffer\n");
1174 adapter->tx_map_failed++;
1175 skb_linearize(skb);
1176 force_bounce = 1;
1177 goto retry_bounce;
1178}
1179
1180static void ibmveth_rx_mss_helper(struct sk_buff *skb, u16 mss, int lrg_pkt)
1181{
1182 struct tcphdr *tcph;
1183 int offset = 0;
1184 int hdr_len;
1185
1186 /* only TCP packets will be aggregated */
1187 if (skb->protocol == htons(ETH_P_IP)) {
1188 struct iphdr *iph = (struct iphdr *)skb->data;
1189
1190 if (iph->protocol == IPPROTO_TCP) {
1191 offset = iph->ihl * 4;
1192 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1193 } else {
1194 return;
1195 }
1196 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1197 struct ipv6hdr *iph6 = (struct ipv6hdr *)skb->data;
1198
1199 if (iph6->nexthdr == IPPROTO_TCP) {
1200 offset = sizeof(struct ipv6hdr);
1201 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1202 } else {
1203 return;
1204 }
1205 } else {
1206 return;
1207 }
1208 /* if mss is not set through Large Packet bit/mss in rx buffer,
1209 * expect that the mss will be written to the tcp header checksum.
1210 */
1211 tcph = (struct tcphdr *)(skb->data + offset);
1212 if (lrg_pkt) {
1213 skb_shinfo(skb)->gso_size = mss;
1214 } else if (offset) {
1215 skb_shinfo(skb)->gso_size = ntohs(tcph->check);
1216 tcph->check = 0;
1217 }
1218
1219 if (skb_shinfo(skb)->gso_size) {
1220 hdr_len = offset + tcph->doff * 4;
1221 skb_shinfo(skb)->gso_segs =
1222 DIV_ROUND_UP(skb->len - hdr_len,
1223 skb_shinfo(skb)->gso_size);
1224 }
1225}
1226
1227static int ibmveth_poll(struct napi_struct *napi, int budget)
1228{
1229 struct ibmveth_adapter *adapter =
1230 container_of(napi, struct ibmveth_adapter, napi);
1231 struct net_device *netdev = adapter->netdev;
1232 int frames_processed = 0;
1233 unsigned long lpar_rc;
1234 struct iphdr *iph;
1235 u16 mss = 0;
1236
1237restart_poll:
1238 while (frames_processed < budget) {
1239 if (!ibmveth_rxq_pending_buffer(adapter))
1240 break;
1241
1242 smp_rmb();
1243 if (!ibmveth_rxq_buffer_valid(adapter)) {
1244 wmb(); /* suggested by larson1 */
1245 adapter->rx_invalid_buffer++;
1246 netdev_dbg(netdev, "recycling invalid buffer\n");
1247 ibmveth_rxq_recycle_buffer(adapter);
1248 } else {
1249 struct sk_buff *skb, *new_skb;
1250 int length = ibmveth_rxq_frame_length(adapter);
1251 int offset = ibmveth_rxq_frame_offset(adapter);
1252 int csum_good = ibmveth_rxq_csum_good(adapter);
1253 int lrg_pkt = ibmveth_rxq_large_packet(adapter);
1254
1255 skb = ibmveth_rxq_get_buffer(adapter);
1256
1257 /* if the large packet bit is set in the rx queue
1258 * descriptor, the mss will be written by PHYP eight
1259 * bytes from the start of the rx buffer, which is
1260 * skb->data at this stage
1261 */
1262 if (lrg_pkt) {
1263 __be64 *rxmss = (__be64 *)(skb->data + 8);
1264
1265 mss = (u16)be64_to_cpu(*rxmss);
1266 }
1267
1268 new_skb = NULL;
1269 if (length < rx_copybreak)
1270 new_skb = netdev_alloc_skb(netdev, length);
1271
1272 if (new_skb) {
1273 skb_copy_to_linear_data(new_skb,
1274 skb->data + offset,
1275 length);
1276 if (rx_flush)
1277 ibmveth_flush_buffer(skb->data,
1278 length + offset);
1279 if (!ibmveth_rxq_recycle_buffer(adapter))
1280 kfree_skb(skb);
1281 skb = new_skb;
1282 } else {
1283 ibmveth_rxq_harvest_buffer(adapter);
1284 skb_reserve(skb, offset);
1285 }
1286
1287 skb_put(skb, length);
1288 skb->protocol = eth_type_trans(skb, netdev);
1289
1290 if (csum_good) {
1291 skb->ip_summed = CHECKSUM_UNNECESSARY;
1292 if (be16_to_cpu(skb->protocol) == ETH_P_IP) {
1293 iph = (struct iphdr *)skb->data;
1294
1295 /* If the IP checksum is not offloaded and if the packet
1296 * is large send, the checksum must be rebuilt.
1297 */
1298 if (iph->check == 0xffff) {
1299 iph->check = 0;
1300 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1301 }
1302 }
1303 }
1304
1305 if (length > netdev->mtu + ETH_HLEN) {
1306 ibmveth_rx_mss_helper(skb, mss, lrg_pkt);
1307 adapter->rx_large_packets++;
1308 }
1309
1310 napi_gro_receive(napi, skb); /* send it up */
1311
1312 netdev->stats.rx_packets++;
1313 netdev->stats.rx_bytes += length;
1314 frames_processed++;
1315 }
1316 }
1317
1318 ibmveth_replenish_task(adapter);
1319
1320 if (frames_processed < budget) {
1321 napi_complete(napi);
1322
1323 /* We think we are done - reenable interrupts,
1324 * then check once more to make sure we are done.
1325 */
1326 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1327 VIO_IRQ_ENABLE);
1328
1329 BUG_ON(lpar_rc != H_SUCCESS);
1330
1331 if (ibmveth_rxq_pending_buffer(adapter) &&
1332 napi_reschedule(napi)) {
1333 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1334 VIO_IRQ_DISABLE);
1335 goto restart_poll;
1336 }
1337 }
1338
1339 return frames_processed;
1340}
1341
1342static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
1343{
1344 struct net_device *netdev = dev_instance;
1345 struct ibmveth_adapter *adapter = netdev_priv(netdev);
1346 unsigned long lpar_rc;
1347
1348 if (napi_schedule_prep(&adapter->napi)) {
1349 lpar_rc = h_vio_signal(adapter->vdev->unit_address,
1350 VIO_IRQ_DISABLE);
1351 BUG_ON(lpar_rc != H_SUCCESS);
1352 __napi_schedule(&adapter->napi);
1353 }
1354 return IRQ_HANDLED;
1355}
1356
1357static void ibmveth_set_multicast_list(struct net_device *netdev)
1358{
1359 struct ibmveth_adapter *adapter = netdev_priv(netdev);
1360 unsigned long lpar_rc;
1361
1362 if ((netdev->flags & IFF_PROMISC) ||
1363 (netdev_mc_count(netdev) > adapter->mcastFilterSize)) {
1364 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1365 IbmVethMcastEnableRecv |
1366 IbmVethMcastDisableFiltering,
1367 0);
1368 if (lpar_rc != H_SUCCESS) {
1369 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1370 "entering promisc mode\n", lpar_rc);
1371 }
1372 } else {
1373 struct netdev_hw_addr *ha;
1374 /* clear the filter table & disable filtering */
1375 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1376 IbmVethMcastEnableRecv |
1377 IbmVethMcastDisableFiltering |
1378 IbmVethMcastClearFilterTable,
1379 0);
1380 if (lpar_rc != H_SUCCESS) {
1381 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1382 "attempting to clear filter table\n",
1383 lpar_rc);
1384 }
1385 /* add the addresses to the filter table */
1386 netdev_for_each_mc_addr(ha, netdev) {
1387 /* add the multicast address to the filter table */
1388 u64 mcast_addr;
1389 mcast_addr = ibmveth_encode_mac_addr(ha->addr);
1390 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1391 IbmVethMcastAddFilter,
1392 mcast_addr);
1393 if (lpar_rc != H_SUCCESS) {
1394 netdev_err(netdev, "h_multicast_ctrl rc=%ld "
1395 "when adding an entry to the filter "
1396 "table\n", lpar_rc);
1397 }
1398 }
1399
1400 /* re-enable filtering */
1401 lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
1402 IbmVethMcastEnableFiltering,
1403 0);
1404 if (lpar_rc != H_SUCCESS) {
1405 netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
1406 "enabling filtering\n", lpar_rc);
1407 }
1408 }
1409}
1410
1411static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
1412{
1413 struct ibmveth_adapter *adapter = netdev_priv(dev);
1414 struct vio_dev *viodev = adapter->vdev;
1415 int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
1416 int i, rc;
1417 int need_restart = 0;
1418
1419 if (new_mtu < IBMVETH_MIN_MTU)
1420 return -EINVAL;
1421
1422 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
1423 if (new_mtu_oh <= adapter->rx_buff_pool[i].buff_size)
1424 break;
1425
1426 if (i == IBMVETH_NUM_BUFF_POOLS)
1427 return -EINVAL;
1428
1429 /* Deactivate all the buffer pools so that the next loop can activate
1430 only the buffer pools necessary to hold the new MTU */
1431 if (netif_running(adapter->netdev)) {
1432 need_restart = 1;
1433 adapter->pool_config = 1;
1434 ibmveth_close(adapter->netdev);
1435 adapter->pool_config = 0;
1436 }
1437
1438 /* Look for an active buffer pool that can hold the new MTU */
1439 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
1440 adapter->rx_buff_pool[i].active = 1;
1441
1442 if (new_mtu_oh <= adapter->rx_buff_pool[i].buff_size) {
1443 dev->mtu = new_mtu;
1444 vio_cmo_set_dev_desired(viodev,
1445 ibmveth_get_desired_dma
1446 (viodev));
1447 if (need_restart) {
1448 return ibmveth_open(adapter->netdev);
1449 }
1450 return 0;
1451 }
1452 }
1453
1454 if (need_restart && (rc = ibmveth_open(adapter->netdev)))
1455 return rc;
1456
1457 return -EINVAL;
1458}
1459
1460#ifdef CONFIG_NET_POLL_CONTROLLER
1461static void ibmveth_poll_controller(struct net_device *dev)
1462{
1463 ibmveth_replenish_task(netdev_priv(dev));
1464 ibmveth_interrupt(dev->irq, dev);
1465}
1466#endif
1467
1468/**
1469 * ibmveth_get_desired_dma - Calculate IO memory desired by the driver
1470 *
1471 * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
1472 *
1473 * Return value:
1474 * Number of bytes of IO data the driver will need to perform well.
1475 */
1476static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev)
1477{
1478 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
1479 struct ibmveth_adapter *adapter;
1480 struct iommu_table *tbl;
1481 unsigned long ret;
1482 int i;
1483 int rxqentries = 1;
1484
1485 tbl = get_iommu_table_base(&vdev->dev);
1486
1487 /* netdev inits at probe time along with the structures we need below*/
1488 if (netdev == NULL)
1489 return IOMMU_PAGE_ALIGN(IBMVETH_IO_ENTITLEMENT_DEFAULT, tbl);
1490
1491 adapter = netdev_priv(netdev);
1492
1493 ret = IBMVETH_BUFF_LIST_SIZE + IBMVETH_FILT_LIST_SIZE;
1494 ret += IOMMU_PAGE_ALIGN(netdev->mtu, tbl);
1495
1496 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
1497 /* add the size of the active receive buffers */
1498 if (adapter->rx_buff_pool[i].active)
1499 ret +=
1500 adapter->rx_buff_pool[i].size *
1501 IOMMU_PAGE_ALIGN(adapter->rx_buff_pool[i].
1502 buff_size, tbl);
1503 rxqentries += adapter->rx_buff_pool[i].size;
1504 }
1505 /* add the size of the receive queue entries */
1506 ret += IOMMU_PAGE_ALIGN(
1507 rxqentries * sizeof(struct ibmveth_rx_q_entry), tbl);
1508
1509 return ret;
1510}
1511
1512static int ibmveth_set_mac_addr(struct net_device *dev, void *p)
1513{
1514 struct ibmveth_adapter *adapter = netdev_priv(dev);
1515 struct sockaddr *addr = p;
1516 u64 mac_address;
1517 int rc;
1518
1519 if (!is_valid_ether_addr(addr->sa_data))
1520 return -EADDRNOTAVAIL;
1521
1522 mac_address = ibmveth_encode_mac_addr(addr->sa_data);
1523 rc = h_change_logical_lan_mac(adapter->vdev->unit_address, mac_address);
1524 if (rc) {
1525 netdev_err(adapter->netdev, "h_change_logical_lan_mac failed with rc=%d\n", rc);
1526 return rc;
1527 }
1528
1529 ether_addr_copy(dev->dev_addr, addr->sa_data);
1530
1531 return 0;
1532}
1533
1534static const struct net_device_ops ibmveth_netdev_ops = {
1535 .ndo_open = ibmveth_open,
1536 .ndo_stop = ibmveth_close,
1537 .ndo_start_xmit = ibmveth_start_xmit,
1538 .ndo_set_rx_mode = ibmveth_set_multicast_list,
1539 .ndo_do_ioctl = ibmveth_ioctl,
1540 .ndo_change_mtu = ibmveth_change_mtu,
1541 .ndo_fix_features = ibmveth_fix_features,
1542 .ndo_set_features = ibmveth_set_features,
1543 .ndo_validate_addr = eth_validate_addr,
1544 .ndo_set_mac_address = ibmveth_set_mac_addr,
1545#ifdef CONFIG_NET_POLL_CONTROLLER
1546 .ndo_poll_controller = ibmveth_poll_controller,
1547#endif
1548};
1549
1550static int ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
1551{
1552 int rc, i, mac_len;
1553 struct net_device *netdev;
1554 struct ibmveth_adapter *adapter;
1555 unsigned char *mac_addr_p;
1556 unsigned int *mcastFilterSize_p;
1557 long ret;
1558 unsigned long ret_attr;
1559
1560 dev_dbg(&dev->dev, "entering ibmveth_probe for UA 0x%x\n",
1561 dev->unit_address);
1562
1563 mac_addr_p = (unsigned char *)vio_get_attribute(dev, VETH_MAC_ADDR,
1564 &mac_len);
1565 if (!mac_addr_p) {
1566 dev_err(&dev->dev, "Can't find VETH_MAC_ADDR attribute\n");
1567 return -EINVAL;
1568 }
1569 /* Workaround for old/broken pHyp */
1570 if (mac_len == 8)
1571 mac_addr_p += 2;
1572 else if (mac_len != 6) {
1573 dev_err(&dev->dev, "VETH_MAC_ADDR attribute wrong len %d\n",
1574 mac_len);
1575 return -EINVAL;
1576 }
1577
1578 mcastFilterSize_p = (unsigned int *)vio_get_attribute(dev,
1579 VETH_MCAST_FILTER_SIZE, NULL);
1580 if (!mcastFilterSize_p) {
1581 dev_err(&dev->dev, "Can't find VETH_MCAST_FILTER_SIZE "
1582 "attribute\n");
1583 return -EINVAL;
1584 }
1585
1586 netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
1587
1588 if (!netdev)
1589 return -ENOMEM;
1590
1591 adapter = netdev_priv(netdev);
1592 dev_set_drvdata(&dev->dev, netdev);
1593
1594 adapter->vdev = dev;
1595 adapter->netdev = netdev;
1596 adapter->mcastFilterSize = *mcastFilterSize_p;
1597 adapter->pool_config = 0;
1598
1599 netif_napi_add(netdev, &adapter->napi, ibmveth_poll, 16);
1600
1601 netdev->irq = dev->irq;
1602 netdev->netdev_ops = &ibmveth_netdev_ops;
1603 netdev->ethtool_ops = &netdev_ethtool_ops;
1604 SET_NETDEV_DEV(netdev, &dev->dev);
1605 netdev->hw_features = NETIF_F_SG;
1606 if (vio_get_attribute(dev, "ibm,illan-options", NULL) != NULL) {
1607 netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1608 NETIF_F_RXCSUM;
1609 }
1610
1611 netdev->features |= netdev->hw_features;
1612
1613 ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
1614
1615 /* If running older firmware, TSO should not be enabled by default */
1616 if (ret == H_SUCCESS && (ret_attr & IBMVETH_ILLAN_LRG_SND_SUPPORT) &&
1617 !old_large_send) {
1618 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1619 netdev->features |= netdev->hw_features;
1620 } else {
1621 netdev->hw_features |= NETIF_F_TSO;
1622 }
1623
1624 memcpy(netdev->dev_addr, mac_addr_p, ETH_ALEN);
1625
1626 if (firmware_has_feature(FW_FEATURE_CMO))
1627 memcpy(pool_count, pool_count_cmo, sizeof(pool_count));
1628
1629 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
1630 struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
1631 int error;
1632
1633 ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
1634 pool_count[i], pool_size[i],
1635 pool_active[i]);
1636 error = kobject_init_and_add(kobj, &ktype_veth_pool,
1637 &dev->dev.kobj, "pool%d", i);
1638 if (!error)
1639 kobject_uevent(kobj, KOBJ_ADD);
1640 }
1641
1642 netdev_dbg(netdev, "adapter @ 0x%p\n", adapter);
1643
1644 adapter->buffer_list_dma = DMA_ERROR_CODE;
1645 adapter->filter_list_dma = DMA_ERROR_CODE;
1646 adapter->rx_queue.queue_dma = DMA_ERROR_CODE;
1647
1648 netdev_dbg(netdev, "registering netdev...\n");
1649
1650 ibmveth_set_features(netdev, netdev->features);
1651
1652 rc = register_netdev(netdev);
1653
1654 if (rc) {
1655 netdev_dbg(netdev, "failed to register netdev rc=%d\n", rc);
1656 free_netdev(netdev);
1657 return rc;
1658 }
1659
1660 netdev_dbg(netdev, "registered\n");
1661
1662 return 0;
1663}
1664
1665static int ibmveth_remove(struct vio_dev *dev)
1666{
1667 struct net_device *netdev = dev_get_drvdata(&dev->dev);
1668 struct ibmveth_adapter *adapter = netdev_priv(netdev);
1669 int i;
1670
1671 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
1672 kobject_put(&adapter->rx_buff_pool[i].kobj);
1673
1674 unregister_netdev(netdev);
1675
1676 free_netdev(netdev);
1677 dev_set_drvdata(&dev->dev, NULL);
1678
1679 return 0;
1680}
1681
1682static struct attribute veth_active_attr;
1683static struct attribute veth_num_attr;
1684static struct attribute veth_size_attr;
1685
1686static ssize_t veth_pool_show(struct kobject *kobj,
1687 struct attribute *attr, char *buf)
1688{
1689 struct ibmveth_buff_pool *pool = container_of(kobj,
1690 struct ibmveth_buff_pool,
1691 kobj);
1692
1693 if (attr == &veth_active_attr)
1694 return sprintf(buf, "%d\n", pool->active);
1695 else if (attr == &veth_num_attr)
1696 return sprintf(buf, "%d\n", pool->size);
1697 else if (attr == &veth_size_attr)
1698 return sprintf(buf, "%d\n", pool->buff_size);
1699 return 0;
1700}
1701
1702static ssize_t veth_pool_store(struct kobject *kobj, struct attribute *attr,
1703 const char *buf, size_t count)
1704{
1705 struct ibmveth_buff_pool *pool = container_of(kobj,
1706 struct ibmveth_buff_pool,
1707 kobj);
1708 struct net_device *netdev = dev_get_drvdata(
1709 container_of(kobj->parent, struct device, kobj));
1710 struct ibmveth_adapter *adapter = netdev_priv(netdev);
1711 long value = simple_strtol(buf, NULL, 10);
1712 long rc;
1713
1714 if (attr == &veth_active_attr) {
1715 if (value && !pool->active) {
1716 if (netif_running(netdev)) {
1717 if (ibmveth_alloc_buffer_pool(pool)) {
1718 netdev_err(netdev,
1719 "unable to alloc pool\n");
1720 return -ENOMEM;
1721 }
1722 pool->active = 1;
1723 adapter->pool_config = 1;
1724 ibmveth_close(netdev);
1725 adapter->pool_config = 0;
1726 if ((rc = ibmveth_open(netdev)))
1727 return rc;
1728 } else {
1729 pool->active = 1;
1730 }
1731 } else if (!value && pool->active) {
1732 int mtu = netdev->mtu + IBMVETH_BUFF_OH;
1733 int i;
1734 /* Make sure there is a buffer pool with buffers that
1735 can hold a packet of the size of the MTU */
1736 for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
1737 if (pool == &adapter->rx_buff_pool[i])
1738 continue;
1739 if (!adapter->rx_buff_pool[i].active)
1740 continue;
1741 if (mtu <= adapter->rx_buff_pool[i].buff_size)
1742 break;
1743 }
1744
1745 if (i == IBMVETH_NUM_BUFF_POOLS) {
1746 netdev_err(netdev, "no active pool >= MTU\n");
1747 return -EPERM;
1748 }
1749
1750 if (netif_running(netdev)) {
1751 adapter->pool_config = 1;
1752 ibmveth_close(netdev);
1753 pool->active = 0;
1754 adapter->pool_config = 0;
1755 if ((rc = ibmveth_open(netdev)))
1756 return rc;
1757 }
1758 pool->active = 0;
1759 }
1760 } else if (attr == &veth_num_attr) {
1761 if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT) {
1762 return -EINVAL;
1763 } else {
1764 if (netif_running(netdev)) {
1765 adapter->pool_config = 1;
1766 ibmveth_close(netdev);
1767 adapter->pool_config = 0;
1768 pool->size = value;
1769 if ((rc = ibmveth_open(netdev)))
1770 return rc;
1771 } else {
1772 pool->size = value;
1773 }
1774 }
1775 } else if (attr == &veth_size_attr) {
1776 if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE) {
1777 return -EINVAL;
1778 } else {
1779 if (netif_running(netdev)) {
1780 adapter->pool_config = 1;
1781 ibmveth_close(netdev);
1782 adapter->pool_config = 0;
1783 pool->buff_size = value;
1784 if ((rc = ibmveth_open(netdev)))
1785 return rc;
1786 } else {
1787 pool->buff_size = value;
1788 }
1789 }
1790 }
1791
1792 /* kick the interrupt handler to allocate/deallocate pools */
1793 ibmveth_interrupt(netdev->irq, netdev);
1794 return count;
1795}
1796
1797
1798#define ATTR(_name, _mode) \
1799 struct attribute veth_##_name##_attr = { \
1800 .name = __stringify(_name), .mode = _mode, \
1801 };
1802
1803static ATTR(active, 0644);
1804static ATTR(num, 0644);
1805static ATTR(size, 0644);
1806
1807static struct attribute *veth_pool_attrs[] = {
1808 &veth_active_attr,
1809 &veth_num_attr,
1810 &veth_size_attr,
1811 NULL,
1812};
1813
1814static const struct sysfs_ops veth_pool_ops = {
1815 .show = veth_pool_show,
1816 .store = veth_pool_store,
1817};
1818
1819static struct kobj_type ktype_veth_pool = {
1820 .release = NULL,
1821 .sysfs_ops = &veth_pool_ops,
1822 .default_attrs = veth_pool_attrs,
1823};
1824
1825static int ibmveth_resume(struct device *dev)
1826{
1827 struct net_device *netdev = dev_get_drvdata(dev);
1828 ibmveth_interrupt(netdev->irq, netdev);
1829 return 0;
1830}
1831
1832static struct vio_device_id ibmveth_device_table[] = {
1833 { "network", "IBM,l-lan"},
1834 { "", "" }
1835};
1836MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
1837
1838static struct dev_pm_ops ibmveth_pm_ops = {
1839 .resume = ibmveth_resume
1840};
1841
1842static struct vio_driver ibmveth_driver = {
1843 .id_table = ibmveth_device_table,
1844 .probe = ibmveth_probe,
1845 .remove = ibmveth_remove,
1846 .get_desired_dma = ibmveth_get_desired_dma,
1847 .name = ibmveth_driver_name,
1848 .pm = &ibmveth_pm_ops,
1849};
1850
1851static int __init ibmveth_module_init(void)
1852{
1853 printk(KERN_DEBUG "%s: %s %s\n", ibmveth_driver_name,
1854 ibmveth_driver_string, ibmveth_driver_version);
1855
1856 return vio_register_driver(&ibmveth_driver);
1857}
1858
1859static void __exit ibmveth_module_exit(void)
1860{
1861 vio_unregister_driver(&ibmveth_driver);
1862}
1863
1864module_init(ibmveth_module_init);
1865module_exit(ibmveth_module_exit);