1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * drivers/net/ethernet/rocker/rocker.c - Rocker switch device driver
4  * Copyright (c) 2014-2016 Jiri Pirko <jiri@mellanox.com>
5  * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com>
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/interrupt.h>
12 #include <linux/sched.h>
13 #include <linux/wait.h>
14 #include <linux/spinlock.h>
15 #include <linux/sort.h>
16 #include <linux/random.h>
17 #include <linux/netdevice.h>
18 #include <linux/skbuff.h>
19 #include <linux/socket.h>
20 #include <linux/etherdevice.h>
21 #include <linux/ethtool.h>
22 #include <linux/if_ether.h>
23 #include <linux/if_vlan.h>
24 #include <linux/if_bridge.h>
25 #include <linux/bitops.h>
26 #include <linux/ctype.h>
27 #include <linux/workqueue.h>
28 #include <net/switchdev.h>
29 #include <net/rtnetlink.h>
30 #include <net/netevent.h>
31 #include <net/arp.h>
32 #include <net/fib_rules.h>
33 #include <net/fib_notifier.h>
34 #include <linux/io-64-nonatomic-lo-hi.h>
35 #include <generated/utsrelease.h>
36 
37 #include "rocker_hw.h"
38 #include "rocker.h"
39 #include "rocker_tlv.h"
40 
41 static const char rocker_driver_name[] = "rocker";
42 
43 static const struct pci_device_id rocker_pci_id_table[] = {
44 	{PCI_VDEVICE(REDHAT, PCI_DEVICE_ID_REDHAT_ROCKER), 0},
45 	{0, }
46 };
47 
48 struct rocker_wait {
49 	wait_queue_head_t wait;
50 	bool done;
51 	bool nowait;
52 };
53 
54 static void rocker_wait_reset(struct rocker_wait *wait)
55 {
56 	wait->done = false;
57 	wait->nowait = false;
58 }
59 
60 static void rocker_wait_init(struct rocker_wait *wait)
61 {
62 	init_waitqueue_head(&wait->wait);
63 	rocker_wait_reset(wait);
64 }
65 
66 static struct rocker_wait *rocker_wait_create(void)
67 {
68 	struct rocker_wait *wait;
69 
70 	wait = kzalloc(sizeof(*wait), GFP_KERNEL);
71 	if (!wait)
72 		return NULL;
73 	return wait;
74 }
75 
76 static void rocker_wait_destroy(struct rocker_wait *wait)
77 {
78 	kfree(wait);
79 }
80 
81 static bool rocker_wait_event_timeout(struct rocker_wait *wait,
82 				      unsigned long timeout)
83 {
84 	wait_event_timeout(wait->wait, wait->done, HZ / 10);
85 	if (!wait->done)
86 		return false;
87 	return true;
88 }
89 
90 static void rocker_wait_wake_up(struct rocker_wait *wait)
91 {
92 	wait->done = true;
93 	wake_up(&wait->wait);
94 }
95 
96 static u32 rocker_msix_vector(const struct rocker *rocker, unsigned int vector)
97 {
98 	return rocker->msix_entries[vector].vector;
99 }
100 
101 static u32 rocker_msix_tx_vector(const struct rocker_port *rocker_port)
102 {
103 	return rocker_msix_vector(rocker_port->rocker,
104 				  ROCKER_MSIX_VEC_TX(rocker_port->port_number));
105 }
106 
107 static u32 rocker_msix_rx_vector(const struct rocker_port *rocker_port)
108 {
109 	return rocker_msix_vector(rocker_port->rocker,
110 				  ROCKER_MSIX_VEC_RX(rocker_port->port_number));
111 }
112 
113 #define rocker_write32(rocker, reg, val)	\
114 	writel((val), (rocker)->hw_addr + (ROCKER_ ## reg))
115 #define rocker_read32(rocker, reg)	\
116 	readl((rocker)->hw_addr + (ROCKER_ ## reg))
117 #define rocker_write64(rocker, reg, val)	\
118 	writeq((val), (rocker)->hw_addr + (ROCKER_ ## reg))
119 #define rocker_read64(rocker, reg)	\
120 	readq((rocker)->hw_addr + (ROCKER_ ## reg))
121 
122 /*****************************
123  * HW basic testing functions
124  *****************************/
125 
126 static int rocker_reg_test(const struct rocker *rocker)
127 {
128 	const struct pci_dev *pdev = rocker->pdev;
129 	u64 test_reg;
130 	u64 rnd;
131 
132 	rnd = prandom_u32();
133 	rnd >>= 1;
134 	rocker_write32(rocker, TEST_REG, rnd);
135 	test_reg = rocker_read32(rocker, TEST_REG);
136 	if (test_reg != rnd * 2) {
137 		dev_err(&pdev->dev, "unexpected 32bit register value %08llx, expected %08llx\n",
138 			test_reg, rnd * 2);
139 		return -EIO;
140 	}
141 
142 	rnd = prandom_u32();
143 	rnd <<= 31;
144 	rnd |= prandom_u32();
145 	rocker_write64(rocker, TEST_REG64, rnd);
146 	test_reg = rocker_read64(rocker, TEST_REG64);
147 	if (test_reg != rnd * 2) {
148 		dev_err(&pdev->dev, "unexpected 64bit register value %16llx, expected %16llx\n",
149 			test_reg, rnd * 2);
150 		return -EIO;
151 	}
152 
153 	return 0;
154 }
155 
156 static int rocker_dma_test_one(const struct rocker *rocker,
157 			       struct rocker_wait *wait, u32 test_type,
158 			       dma_addr_t dma_handle, const unsigned char *buf,
159 			       const unsigned char *expect, size_t size)
160 {
161 	const struct pci_dev *pdev = rocker->pdev;
162 	int i;
163 
164 	rocker_wait_reset(wait);
165 	rocker_write32(rocker, TEST_DMA_CTRL, test_type);
166 
167 	if (!rocker_wait_event_timeout(wait, HZ / 10)) {
168 		dev_err(&pdev->dev, "no interrupt received within a timeout\n");
169 		return -EIO;
170 	}
171 
172 	for (i = 0; i < size; i++) {
173 		if (buf[i] != expect[i]) {
174 			dev_err(&pdev->dev, "unexpected memory content %02x at byte %x\n, %02x expected",
175 				buf[i], i, expect[i]);
176 			return -EIO;
177 		}
178 	}
179 	return 0;
180 }
181 
182 #define ROCKER_TEST_DMA_BUF_SIZE (PAGE_SIZE * 4)
183 #define ROCKER_TEST_DMA_FILL_PATTERN 0x96
184 
185 static int rocker_dma_test_offset(const struct rocker *rocker,
186 				  struct rocker_wait *wait, int offset)
187 {
188 	struct pci_dev *pdev = rocker->pdev;
189 	unsigned char *alloc;
190 	unsigned char *buf;
191 	unsigned char *expect;
192 	dma_addr_t dma_handle;
193 	int i;
194 	int err;
195 
196 	alloc = kzalloc(ROCKER_TEST_DMA_BUF_SIZE * 2 + offset,
197 			GFP_KERNEL | GFP_DMA);
198 	if (!alloc)
199 		return -ENOMEM;
200 	buf = alloc + offset;
201 	expect = buf + ROCKER_TEST_DMA_BUF_SIZE;
202 
203 	dma_handle = pci_map_single(pdev, buf, ROCKER_TEST_DMA_BUF_SIZE,
204 				    PCI_DMA_BIDIRECTIONAL);
205 	if (pci_dma_mapping_error(pdev, dma_handle)) {
206 		err = -EIO;
207 		goto free_alloc;
208 	}
209 
210 	rocker_write64(rocker, TEST_DMA_ADDR, dma_handle);
211 	rocker_write32(rocker, TEST_DMA_SIZE, ROCKER_TEST_DMA_BUF_SIZE);
212 
213 	memset(expect, ROCKER_TEST_DMA_FILL_PATTERN, ROCKER_TEST_DMA_BUF_SIZE);
214 	err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_FILL,
215 				  dma_handle, buf, expect,
216 				  ROCKER_TEST_DMA_BUF_SIZE);
217 	if (err)
218 		goto unmap;
219 
220 	memset(expect, 0, ROCKER_TEST_DMA_BUF_SIZE);
221 	err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_CLEAR,
222 				  dma_handle, buf, expect,
223 				  ROCKER_TEST_DMA_BUF_SIZE);
224 	if (err)
225 		goto unmap;
226 
227 	prandom_bytes(buf, ROCKER_TEST_DMA_BUF_SIZE);
228 	for (i = 0; i < ROCKER_TEST_DMA_BUF_SIZE; i++)
229 		expect[i] = ~buf[i];
230 	err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_INVERT,
231 				  dma_handle, buf, expect,
232 				  ROCKER_TEST_DMA_BUF_SIZE);
233 	if (err)
234 		goto unmap;
235 
236 unmap:
237 	pci_unmap_single(pdev, dma_handle, ROCKER_TEST_DMA_BUF_SIZE,
238 			 PCI_DMA_BIDIRECTIONAL);
239 free_alloc:
240 	kfree(alloc);
241 
242 	return err;
243 }
244 
245 static int rocker_dma_test(const struct rocker *rocker,
246 			   struct rocker_wait *wait)
247 {
248 	int i;
249 	int err;
250 
251 	for (i = 0; i < 8; i++) {
252 		err = rocker_dma_test_offset(rocker, wait, i);
253 		if (err)
254 			return err;
255 	}
256 	return 0;
257 }
258 
259 static irqreturn_t rocker_test_irq_handler(int irq, void *dev_id)
260 {
261 	struct rocker_wait *wait = dev_id;
262 
263 	rocker_wait_wake_up(wait);
264 
265 	return IRQ_HANDLED;
266 }
267 
268 static int rocker_basic_hw_test(const struct rocker *rocker)
269 {
270 	const struct pci_dev *pdev = rocker->pdev;
271 	struct rocker_wait wait;
272 	int err;
273 
274 	err = rocker_reg_test(rocker);
275 	if (err) {
276 		dev_err(&pdev->dev, "reg test failed\n");
277 		return err;
278 	}
279 
280 	err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST),
281 			  rocker_test_irq_handler, 0,
282 			  rocker_driver_name, &wait);
283 	if (err) {
284 		dev_err(&pdev->dev, "cannot assign test irq\n");
285 		return err;
286 	}
287 
288 	rocker_wait_init(&wait);
289 	rocker_write32(rocker, TEST_IRQ, ROCKER_MSIX_VEC_TEST);
290 
291 	if (!rocker_wait_event_timeout(&wait, HZ / 10)) {
292 		dev_err(&pdev->dev, "no interrupt received within a timeout\n");
293 		err = -EIO;
294 		goto free_irq;
295 	}
296 
297 	err = rocker_dma_test(rocker, &wait);
298 	if (err)
299 		dev_err(&pdev->dev, "dma test failed\n");
300 
301 free_irq:
302 	free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST), &wait);
303 	return err;
304 }
305 
306 /******************************************
307  * DMA rings and descriptors manipulations
308  ******************************************/
309 
310 static u32 __pos_inc(u32 pos, size_t limit)
311 {
312 	return ++pos == limit ? 0 : pos;
313 }
314 
315 static int rocker_desc_err(const struct rocker_desc_info *desc_info)
316 {
317 	int err = desc_info->desc->comp_err & ~ROCKER_DMA_DESC_COMP_ERR_GEN;
318 
319 	switch (err) {
320 	case ROCKER_OK:
321 		return 0;
322 	case -ROCKER_ENOENT:
323 		return -ENOENT;
324 	case -ROCKER_ENXIO:
325 		return -ENXIO;
326 	case -ROCKER_ENOMEM:
327 		return -ENOMEM;
328 	case -ROCKER_EEXIST:
329 		return -EEXIST;
330 	case -ROCKER_EINVAL:
331 		return -EINVAL;
332 	case -ROCKER_EMSGSIZE:
333 		return -EMSGSIZE;
334 	case -ROCKER_ENOTSUP:
335 		return -EOPNOTSUPP;
336 	case -ROCKER_ENOBUFS:
337 		return -ENOBUFS;
338 	}
339 
340 	return -EINVAL;
341 }
342 
343 static void rocker_desc_gen_clear(const struct rocker_desc_info *desc_info)
344 {
345 	desc_info->desc->comp_err &= ~ROCKER_DMA_DESC_COMP_ERR_GEN;
346 }
347 
348 static bool rocker_desc_gen(const struct rocker_desc_info *desc_info)
349 {
350 	u32 comp_err = desc_info->desc->comp_err;
351 
352 	return comp_err & ROCKER_DMA_DESC_COMP_ERR_GEN ? true : false;
353 }
354 
355 static void *
356 rocker_desc_cookie_ptr_get(const struct rocker_desc_info *desc_info)
357 {
358 	return (void *)(uintptr_t)desc_info->desc->cookie;
359 }
360 
361 static void rocker_desc_cookie_ptr_set(const struct rocker_desc_info *desc_info,
362 				       void *ptr)
363 {
364 	desc_info->desc->cookie = (uintptr_t) ptr;
365 }
366 
367 static struct rocker_desc_info *
368 rocker_desc_head_get(const struct rocker_dma_ring_info *info)
369 {
370 	struct rocker_desc_info *desc_info;
371 	u32 head = __pos_inc(info->head, info->size);
372 
373 	desc_info = &info->desc_info[info->head];
374 	if (head == info->tail)
375 		return NULL; /* ring full */
376 	desc_info->tlv_size = 0;
377 	return desc_info;
378 }
379 
380 static void rocker_desc_commit(const struct rocker_desc_info *desc_info)
381 {
382 	desc_info->desc->buf_size = desc_info->data_size;
383 	desc_info->desc->tlv_size = desc_info->tlv_size;
384 }
385 
386 static void rocker_desc_head_set(const struct rocker *rocker,
387 				 struct rocker_dma_ring_info *info,
388 				 const struct rocker_desc_info *desc_info)
389 {
390 	u32 head = __pos_inc(info->head, info->size);
391 
392 	BUG_ON(head == info->tail);
393 	rocker_desc_commit(desc_info);
394 	info->head = head;
395 	rocker_write32(rocker, DMA_DESC_HEAD(info->type), head);
396 }
397 
398 static struct rocker_desc_info *
399 rocker_desc_tail_get(struct rocker_dma_ring_info *info)
400 {
401 	struct rocker_desc_info *desc_info;
402 
403 	if (info->tail == info->head)
404 		return NULL; /* nothing to be done between head and tail */
405 	desc_info = &info->desc_info[info->tail];
406 	if (!rocker_desc_gen(desc_info))
407 		return NULL; /* gen bit not set, desc is not ready yet */
408 	info->tail = __pos_inc(info->tail, info->size);
409 	desc_info->tlv_size = desc_info->desc->tlv_size;
410 	return desc_info;
411 }
412 
413 static void rocker_dma_ring_credits_set(const struct rocker *rocker,
414 					const struct rocker_dma_ring_info *info,
415 					u32 credits)
416 {
417 	if (credits)
418 		rocker_write32(rocker, DMA_DESC_CREDITS(info->type), credits);
419 }
420 
421 static unsigned long rocker_dma_ring_size_fix(size_t size)
422 {
423 	return max(ROCKER_DMA_SIZE_MIN,
424 		   min(roundup_pow_of_two(size), ROCKER_DMA_SIZE_MAX));
425 }
426 
427 static int rocker_dma_ring_create(const struct rocker *rocker,
428 				  unsigned int type,
429 				  size_t size,
430 				  struct rocker_dma_ring_info *info)
431 {
432 	int i;
433 
434 	BUG_ON(size != rocker_dma_ring_size_fix(size));
435 	info->size = size;
436 	info->type = type;
437 	info->head = 0;
438 	info->tail = 0;
439 	info->desc_info = kcalloc(info->size, sizeof(*info->desc_info),
440 				  GFP_KERNEL);
441 	if (!info->desc_info)
442 		return -ENOMEM;
443 
444 	info->desc = pci_alloc_consistent(rocker->pdev,
445 					  info->size * sizeof(*info->desc),
446 					  &info->mapaddr);
447 	if (!info->desc) {
448 		kfree(info->desc_info);
449 		return -ENOMEM;
450 	}
451 
452 	for (i = 0; i < info->size; i++)
453 		info->desc_info[i].desc = &info->desc[i];
454 
455 	rocker_write32(rocker, DMA_DESC_CTRL(info->type),
456 		       ROCKER_DMA_DESC_CTRL_RESET);
457 	rocker_write64(rocker, DMA_DESC_ADDR(info->type), info->mapaddr);
458 	rocker_write32(rocker, DMA_DESC_SIZE(info->type), info->size);
459 
460 	return 0;
461 }
462 
463 static void rocker_dma_ring_destroy(const struct rocker *rocker,
464 				    const struct rocker_dma_ring_info *info)
465 {
466 	rocker_write64(rocker, DMA_DESC_ADDR(info->type), 0);
467 
468 	pci_free_consistent(rocker->pdev,
469 			    info->size * sizeof(struct rocker_desc),
470 			    info->desc, info->mapaddr);
471 	kfree(info->desc_info);
472 }
473 
474 static void rocker_dma_ring_pass_to_producer(const struct rocker *rocker,
475 					     struct rocker_dma_ring_info *info)
476 {
477 	int i;
478 
479 	BUG_ON(info->head || info->tail);
480 
481 	/* When ring is consumer, we need to advance head for each desc.
482 	 * That tells hw that the desc is ready to be used by it.
483 	 */
484 	for (i = 0; i < info->size - 1; i++)
485 		rocker_desc_head_set(rocker, info, &info->desc_info[i]);
486 	rocker_desc_commit(&info->desc_info[i]);
487 }
488 
489 static int rocker_dma_ring_bufs_alloc(const struct rocker *rocker,
490 				      const struct rocker_dma_ring_info *info,
491 				      int direction, size_t buf_size)
492 {
493 	struct pci_dev *pdev = rocker->pdev;
494 	int i;
495 	int err;
496 
497 	for (i = 0; i < info->size; i++) {
498 		struct rocker_desc_info *desc_info = &info->desc_info[i];
499 		struct rocker_desc *desc = &info->desc[i];
500 		dma_addr_t dma_handle;
501 		char *buf;
502 
503 		buf = kzalloc(buf_size, GFP_KERNEL | GFP_DMA);
504 		if (!buf) {
505 			err = -ENOMEM;
506 			goto rollback;
507 		}
508 
509 		dma_handle = pci_map_single(pdev, buf, buf_size, direction);
510 		if (pci_dma_mapping_error(pdev, dma_handle)) {
511 			kfree(buf);
512 			err = -EIO;
513 			goto rollback;
514 		}
515 
516 		desc_info->data = buf;
517 		desc_info->data_size = buf_size;
518 		dma_unmap_addr_set(desc_info, mapaddr, dma_handle);
519 
520 		desc->buf_addr = dma_handle;
521 		desc->buf_size = buf_size;
522 	}
523 	return 0;
524 
525 rollback:
526 	for (i--; i >= 0; i--) {
527 		const struct rocker_desc_info *desc_info = &info->desc_info[i];
528 
529 		pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
530 				 desc_info->data_size, direction);
531 		kfree(desc_info->data);
532 	}
533 	return err;
534 }
535 
536 static void rocker_dma_ring_bufs_free(const struct rocker *rocker,
537 				      const struct rocker_dma_ring_info *info,
538 				      int direction)
539 {
540 	struct pci_dev *pdev = rocker->pdev;
541 	int i;
542 
543 	for (i = 0; i < info->size; i++) {
544 		const struct rocker_desc_info *desc_info = &info->desc_info[i];
545 		struct rocker_desc *desc = &info->desc[i];
546 
547 		desc->buf_addr = 0;
548 		desc->buf_size = 0;
549 		pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
550 				 desc_info->data_size, direction);
551 		kfree(desc_info->data);
552 	}
553 }
554 
555 static int rocker_dma_cmd_ring_wait_alloc(struct rocker_desc_info *desc_info)
556 {
557 	struct rocker_wait *wait;
558 
559 	wait = rocker_wait_create();
560 	if (!wait)
561 		return -ENOMEM;
562 	rocker_desc_cookie_ptr_set(desc_info, wait);
563 	return 0;
564 }
565 
566 static void
567 rocker_dma_cmd_ring_wait_free(const struct rocker_desc_info *desc_info)
568 {
569 	struct rocker_wait *wait = rocker_desc_cookie_ptr_get(desc_info);
570 
571 	rocker_wait_destroy(wait);
572 }
573 
574 static int rocker_dma_cmd_ring_waits_alloc(const struct rocker *rocker)
575 {
576 	const struct rocker_dma_ring_info *cmd_ring = &rocker->cmd_ring;
577 	int i;
578 	int err;
579 
580 	for (i = 0; i < cmd_ring->size; i++) {
581 		err = rocker_dma_cmd_ring_wait_alloc(&cmd_ring->desc_info[i]);
582 		if (err)
583 			goto rollback;
584 	}
585 	return 0;
586 
587 rollback:
588 	for (i--; i >= 0; i--)
589 		rocker_dma_cmd_ring_wait_free(&cmd_ring->desc_info[i]);
590 	return err;
591 }
592 
593 static void rocker_dma_cmd_ring_waits_free(const struct rocker *rocker)
594 {
595 	const struct rocker_dma_ring_info *cmd_ring = &rocker->cmd_ring;
596 	int i;
597 
598 	for (i = 0; i < cmd_ring->size; i++)
599 		rocker_dma_cmd_ring_wait_free(&cmd_ring->desc_info[i]);
600 }
601 
602 static int rocker_dma_rings_init(struct rocker *rocker)
603 {
604 	const struct pci_dev *pdev = rocker->pdev;
605 	int err;
606 
607 	err = rocker_dma_ring_create(rocker, ROCKER_DMA_CMD,
608 				     ROCKER_DMA_CMD_DEFAULT_SIZE,
609 				     &rocker->cmd_ring);
610 	if (err) {
611 		dev_err(&pdev->dev, "failed to create command dma ring\n");
612 		return err;
613 	}
614 
615 	spin_lock_init(&rocker->cmd_ring_lock);
616 
617 	err = rocker_dma_ring_bufs_alloc(rocker, &rocker->cmd_ring,
618 					 PCI_DMA_BIDIRECTIONAL, PAGE_SIZE);
619 	if (err) {
620 		dev_err(&pdev->dev, "failed to alloc command dma ring buffers\n");
621 		goto err_dma_cmd_ring_bufs_alloc;
622 	}
623 
624 	err = rocker_dma_cmd_ring_waits_alloc(rocker);
625 	if (err) {
626 		dev_err(&pdev->dev, "failed to alloc command dma ring waits\n");
627 		goto err_dma_cmd_ring_waits_alloc;
628 	}
629 
630 	err = rocker_dma_ring_create(rocker, ROCKER_DMA_EVENT,
631 				     ROCKER_DMA_EVENT_DEFAULT_SIZE,
632 				     &rocker->event_ring);
633 	if (err) {
634 		dev_err(&pdev->dev, "failed to create event dma ring\n");
635 		goto err_dma_event_ring_create;
636 	}
637 
638 	err = rocker_dma_ring_bufs_alloc(rocker, &rocker->event_ring,
639 					 PCI_DMA_FROMDEVICE, PAGE_SIZE);
640 	if (err) {
641 		dev_err(&pdev->dev, "failed to alloc event dma ring buffers\n");
642 		goto err_dma_event_ring_bufs_alloc;
643 	}
644 	rocker_dma_ring_pass_to_producer(rocker, &rocker->event_ring);
645 	return 0;
646 
647 err_dma_event_ring_bufs_alloc:
648 	rocker_dma_ring_destroy(rocker, &rocker->event_ring);
649 err_dma_event_ring_create:
650 	rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
651 				  PCI_DMA_BIDIRECTIONAL);
652 err_dma_cmd_ring_waits_alloc:
653 	rocker_dma_cmd_ring_waits_free(rocker);
654 err_dma_cmd_ring_bufs_alloc:
655 	rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
656 	return err;
657 }
658 
659 static void rocker_dma_rings_fini(struct rocker *rocker)
660 {
661 	rocker_dma_ring_bufs_free(rocker, &rocker->event_ring,
662 				  PCI_DMA_BIDIRECTIONAL);
663 	rocker_dma_ring_destroy(rocker, &rocker->event_ring);
664 	rocker_dma_cmd_ring_waits_free(rocker);
665 	rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
666 				  PCI_DMA_BIDIRECTIONAL);
667 	rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
668 }
669 
670 static int rocker_dma_rx_ring_skb_map(const struct rocker_port *rocker_port,
671 				      struct rocker_desc_info *desc_info,
672 				      struct sk_buff *skb, size_t buf_len)
673 {
674 	const struct rocker *rocker = rocker_port->rocker;
675 	struct pci_dev *pdev = rocker->pdev;
676 	dma_addr_t dma_handle;
677 
678 	dma_handle = pci_map_single(pdev, skb->data, buf_len,
679 				    PCI_DMA_FROMDEVICE);
680 	if (pci_dma_mapping_error(pdev, dma_handle))
681 		return -EIO;
682 	if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_RX_FRAG_ADDR, dma_handle))
683 		goto tlv_put_failure;
684 	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_RX_FRAG_MAX_LEN, buf_len))
685 		goto tlv_put_failure;
686 	return 0;
687 
688 tlv_put_failure:
689 	pci_unmap_single(pdev, dma_handle, buf_len, PCI_DMA_FROMDEVICE);
690 	desc_info->tlv_size = 0;
691 	return -EMSGSIZE;
692 }
693 
694 static size_t rocker_port_rx_buf_len(const struct rocker_port *rocker_port)
695 {
696 	return rocker_port->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
697 }
698 
699 static int rocker_dma_rx_ring_skb_alloc(const struct rocker_port *rocker_port,
700 					struct rocker_desc_info *desc_info)
701 {
702 	struct net_device *dev = rocker_port->dev;
703 	struct sk_buff *skb;
704 	size_t buf_len = rocker_port_rx_buf_len(rocker_port);
705 	int err;
706 
707 	/* Ensure that hw will see tlv_size zero in case of an error.
708 	 * That tells hw to use another descriptor.
709 	 */
710 	rocker_desc_cookie_ptr_set(desc_info, NULL);
711 	desc_info->tlv_size = 0;
712 
713 	skb = netdev_alloc_skb_ip_align(dev, buf_len);
714 	if (!skb)
715 		return -ENOMEM;
716 	err = rocker_dma_rx_ring_skb_map(rocker_port, desc_info, skb, buf_len);
717 	if (err) {
718 		dev_kfree_skb_any(skb);
719 		return err;
720 	}
721 	rocker_desc_cookie_ptr_set(desc_info, skb);
722 	return 0;
723 }
724 
725 static void rocker_dma_rx_ring_skb_unmap(const struct rocker *rocker,
726 					 const struct rocker_tlv **attrs)
727 {
728 	struct pci_dev *pdev = rocker->pdev;
729 	dma_addr_t dma_handle;
730 	size_t len;
731 
732 	if (!attrs[ROCKER_TLV_RX_FRAG_ADDR] ||
733 	    !attrs[ROCKER_TLV_RX_FRAG_MAX_LEN])
734 		return;
735 	dma_handle = rocker_tlv_get_u64(attrs[ROCKER_TLV_RX_FRAG_ADDR]);
736 	len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_MAX_LEN]);
737 	pci_unmap_single(pdev, dma_handle, len, PCI_DMA_FROMDEVICE);
738 }
739 
740 static void rocker_dma_rx_ring_skb_free(const struct rocker *rocker,
741 					const struct rocker_desc_info *desc_info)
742 {
743 	const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
744 	struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
745 
746 	if (!skb)
747 		return;
748 	rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
749 	rocker_dma_rx_ring_skb_unmap(rocker, attrs);
750 	dev_kfree_skb_any(skb);
751 }
752 
753 static int rocker_dma_rx_ring_skbs_alloc(const struct rocker_port *rocker_port)
754 {
755 	const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
756 	const struct rocker *rocker = rocker_port->rocker;
757 	int i;
758 	int err;
759 
760 	for (i = 0; i < rx_ring->size; i++) {
761 		err = rocker_dma_rx_ring_skb_alloc(rocker_port,
762 						   &rx_ring->desc_info[i]);
763 		if (err)
764 			goto rollback;
765 	}
766 	return 0;
767 
768 rollback:
769 	for (i--; i >= 0; i--)
770 		rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
771 	return err;
772 }
773 
774 static void rocker_dma_rx_ring_skbs_free(const struct rocker_port *rocker_port)
775 {
776 	const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
777 	const struct rocker *rocker = rocker_port->rocker;
778 	int i;
779 
780 	for (i = 0; i < rx_ring->size; i++)
781 		rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
782 }
783 
784 static int rocker_port_dma_rings_init(struct rocker_port *rocker_port)
785 {
786 	struct rocker *rocker = rocker_port->rocker;
787 	int err;
788 
789 	err = rocker_dma_ring_create(rocker,
790 				     ROCKER_DMA_TX(rocker_port->port_number),
791 				     ROCKER_DMA_TX_DEFAULT_SIZE,
792 				     &rocker_port->tx_ring);
793 	if (err) {
794 		netdev_err(rocker_port->dev, "failed to create tx dma ring\n");
795 		return err;
796 	}
797 
798 	err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->tx_ring,
799 					 PCI_DMA_TODEVICE,
800 					 ROCKER_DMA_TX_DESC_SIZE);
801 	if (err) {
802 		netdev_err(rocker_port->dev, "failed to alloc tx dma ring buffers\n");
803 		goto err_dma_tx_ring_bufs_alloc;
804 	}
805 
806 	err = rocker_dma_ring_create(rocker,
807 				     ROCKER_DMA_RX(rocker_port->port_number),
808 				     ROCKER_DMA_RX_DEFAULT_SIZE,
809 				     &rocker_port->rx_ring);
810 	if (err) {
811 		netdev_err(rocker_port->dev, "failed to create rx dma ring\n");
812 		goto err_dma_rx_ring_create;
813 	}
814 
815 	err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->rx_ring,
816 					 PCI_DMA_BIDIRECTIONAL,
817 					 ROCKER_DMA_RX_DESC_SIZE);
818 	if (err) {
819 		netdev_err(rocker_port->dev, "failed to alloc rx dma ring buffers\n");
820 		goto err_dma_rx_ring_bufs_alloc;
821 	}
822 
823 	err = rocker_dma_rx_ring_skbs_alloc(rocker_port);
824 	if (err) {
825 		netdev_err(rocker_port->dev, "failed to alloc rx dma ring skbs\n");
826 		goto err_dma_rx_ring_skbs_alloc;
827 	}
828 	rocker_dma_ring_pass_to_producer(rocker, &rocker_port->rx_ring);
829 
830 	return 0;
831 
832 err_dma_rx_ring_skbs_alloc:
833 	rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
834 				  PCI_DMA_BIDIRECTIONAL);
835 err_dma_rx_ring_bufs_alloc:
836 	rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
837 err_dma_rx_ring_create:
838 	rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
839 				  PCI_DMA_TODEVICE);
840 err_dma_tx_ring_bufs_alloc:
841 	rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
842 	return err;
843 }
844 
845 static void rocker_port_dma_rings_fini(struct rocker_port *rocker_port)
846 {
847 	struct rocker *rocker = rocker_port->rocker;
848 
849 	rocker_dma_rx_ring_skbs_free(rocker_port);
850 	rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
851 				  PCI_DMA_BIDIRECTIONAL);
852 	rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
853 	rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
854 				  PCI_DMA_TODEVICE);
855 	rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
856 }
857 
858 static void rocker_port_set_enable(const struct rocker_port *rocker_port,
859 				   bool enable)
860 {
861 	u64 val = rocker_read64(rocker_port->rocker, PORT_PHYS_ENABLE);
862 
863 	if (enable)
864 		val |= 1ULL << rocker_port->pport;
865 	else
866 		val &= ~(1ULL << rocker_port->pport);
867 	rocker_write64(rocker_port->rocker, PORT_PHYS_ENABLE, val);
868 }
869 
870 /********************************
871  * Interrupt handler and helpers
872  ********************************/
873 
874 static irqreturn_t rocker_cmd_irq_handler(int irq, void *dev_id)
875 {
876 	struct rocker *rocker = dev_id;
877 	const struct rocker_desc_info *desc_info;
878 	struct rocker_wait *wait;
879 	u32 credits = 0;
880 
881 	spin_lock(&rocker->cmd_ring_lock);
882 	while ((desc_info = rocker_desc_tail_get(&rocker->cmd_ring))) {
883 		wait = rocker_desc_cookie_ptr_get(desc_info);
884 		if (wait->nowait) {
885 			rocker_desc_gen_clear(desc_info);
886 		} else {
887 			rocker_wait_wake_up(wait);
888 		}
889 		credits++;
890 	}
891 	spin_unlock(&rocker->cmd_ring_lock);
892 	rocker_dma_ring_credits_set(rocker, &rocker->cmd_ring, credits);
893 
894 	return IRQ_HANDLED;
895 }
896 
897 static void rocker_port_link_up(const struct rocker_port *rocker_port)
898 {
899 	netif_carrier_on(rocker_port->dev);
900 	netdev_info(rocker_port->dev, "Link is up\n");
901 }
902 
903 static void rocker_port_link_down(const struct rocker_port *rocker_port)
904 {
905 	netif_carrier_off(rocker_port->dev);
906 	netdev_info(rocker_port->dev, "Link is down\n");
907 }
908 
909 static int rocker_event_link_change(const struct rocker *rocker,
910 				    const struct rocker_tlv *info)
911 {
912 	const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_LINK_CHANGED_MAX + 1];
913 	unsigned int port_number;
914 	bool link_up;
915 	struct rocker_port *rocker_port;
916 
917 	rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_LINK_CHANGED_MAX, info);
918 	if (!attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT] ||
919 	    !attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP])
920 		return -EIO;
921 	port_number =
922 		rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT]) - 1;
923 	link_up = rocker_tlv_get_u8(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP]);
924 
925 	if (port_number >= rocker->port_count)
926 		return -EINVAL;
927 
928 	rocker_port = rocker->ports[port_number];
929 	if (netif_carrier_ok(rocker_port->dev) != link_up) {
930 		if (link_up)
931 			rocker_port_link_up(rocker_port);
932 		else
933 			rocker_port_link_down(rocker_port);
934 	}
935 
936 	return 0;
937 }
938 
939 static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port *rocker_port,
940 					      const unsigned char *addr,
941 					      __be16 vlan_id);
942 
943 static int rocker_event_mac_vlan_seen(const struct rocker *rocker,
944 				      const struct rocker_tlv *info)
945 {
946 	const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAX + 1];
947 	unsigned int port_number;
948 	struct rocker_port *rocker_port;
949 	const unsigned char *addr;
950 	__be16 vlan_id;
951 
952 	rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_MAC_VLAN_MAX, info);
953 	if (!attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT] ||
954 	    !attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC] ||
955 	    !attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID])
956 		return -EIO;
957 	port_number =
958 		rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT]) - 1;
959 	addr = rocker_tlv_data(attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC]);
960 	vlan_id = rocker_tlv_get_be16(attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID]);
961 
962 	if (port_number >= rocker->port_count)
963 		return -EINVAL;
964 
965 	rocker_port = rocker->ports[port_number];
966 	return rocker_world_port_ev_mac_vlan_seen(rocker_port, addr, vlan_id);
967 }
968 
969 static int rocker_event_process(const struct rocker *rocker,
970 				const struct rocker_desc_info *desc_info)
971 {
972 	const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAX + 1];
973 	const struct rocker_tlv *info;
974 	u16 type;
975 
976 	rocker_tlv_parse_desc(attrs, ROCKER_TLV_EVENT_MAX, desc_info);
977 	if (!attrs[ROCKER_TLV_EVENT_TYPE] ||
978 	    !attrs[ROCKER_TLV_EVENT_INFO])
979 		return -EIO;
980 
981 	type = rocker_tlv_get_u16(attrs[ROCKER_TLV_EVENT_TYPE]);
982 	info = attrs[ROCKER_TLV_EVENT_INFO];
983 
984 	switch (type) {
985 	case ROCKER_TLV_EVENT_TYPE_LINK_CHANGED:
986 		return rocker_event_link_change(rocker, info);
987 	case ROCKER_TLV_EVENT_TYPE_MAC_VLAN_SEEN:
988 		return rocker_event_mac_vlan_seen(rocker, info);
989 	}
990 
991 	return -EOPNOTSUPP;
992 }
993 
994 static irqreturn_t rocker_event_irq_handler(int irq, void *dev_id)
995 {
996 	struct rocker *rocker = dev_id;
997 	const struct pci_dev *pdev = rocker->pdev;
998 	const struct rocker_desc_info *desc_info;
999 	u32 credits = 0;
1000 	int err;
1001 
1002 	while ((desc_info = rocker_desc_tail_get(&rocker->event_ring))) {
1003 		err = rocker_desc_err(desc_info);
1004 		if (err) {
1005 			dev_err(&pdev->dev, "event desc received with err %d\n",
1006 				err);
1007 		} else {
1008 			err = rocker_event_process(rocker, desc_info);
1009 			if (err)
1010 				dev_err(&pdev->dev, "event processing failed with err %d\n",
1011 					err);
1012 		}
1013 		rocker_desc_gen_clear(desc_info);
1014 		rocker_desc_head_set(rocker, &rocker->event_ring, desc_info);
1015 		credits++;
1016 	}
1017 	rocker_dma_ring_credits_set(rocker, &rocker->event_ring, credits);
1018 
1019 	return IRQ_HANDLED;
1020 }
1021 
1022 static irqreturn_t rocker_tx_irq_handler(int irq, void *dev_id)
1023 {
1024 	struct rocker_port *rocker_port = dev_id;
1025 
1026 	napi_schedule(&rocker_port->napi_tx);
1027 	return IRQ_HANDLED;
1028 }
1029 
1030 static irqreturn_t rocker_rx_irq_handler(int irq, void *dev_id)
1031 {
1032 	struct rocker_port *rocker_port = dev_id;
1033 
1034 	napi_schedule(&rocker_port->napi_rx);
1035 	return IRQ_HANDLED;
1036 }
1037 
1038 /********************
1039  * Command interface
1040  ********************/
1041 
1042 int rocker_cmd_exec(struct rocker_port *rocker_port, bool nowait,
1043 		    rocker_cmd_prep_cb_t prepare, void *prepare_priv,
1044 		    rocker_cmd_proc_cb_t process, void *process_priv)
1045 {
1046 	struct rocker *rocker = rocker_port->rocker;
1047 	struct rocker_desc_info *desc_info;
1048 	struct rocker_wait *wait;
1049 	unsigned long lock_flags;
1050 	int err;
1051 
1052 	spin_lock_irqsave(&rocker->cmd_ring_lock, lock_flags);
1053 
1054 	desc_info = rocker_desc_head_get(&rocker->cmd_ring);
1055 	if (!desc_info) {
1056 		spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
1057 		return -EAGAIN;
1058 	}
1059 
1060 	wait = rocker_desc_cookie_ptr_get(desc_info);
1061 	rocker_wait_init(wait);
1062 	wait->nowait = nowait;
1063 
1064 	err = prepare(rocker_port, desc_info, prepare_priv);
1065 	if (err) {
1066 		spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
1067 		return err;
1068 	}
1069 
1070 	rocker_desc_head_set(rocker, &rocker->cmd_ring, desc_info);
1071 
1072 	spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
1073 
1074 	if (nowait)
1075 		return 0;
1076 
1077 	if (!rocker_wait_event_timeout(wait, HZ / 10))
1078 		return -EIO;
1079 
1080 	err = rocker_desc_err(desc_info);
1081 	if (err)
1082 		return err;
1083 
1084 	if (process)
1085 		err = process(rocker_port, desc_info, process_priv);
1086 
1087 	rocker_desc_gen_clear(desc_info);
1088 	return err;
1089 }
1090 
1091 static int
1092 rocker_cmd_get_port_settings_prep(const struct rocker_port *rocker_port,
1093 				  struct rocker_desc_info *desc_info,
1094 				  void *priv)
1095 {
1096 	struct rocker_tlv *cmd_info;
1097 
1098 	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1099 			       ROCKER_TLV_CMD_TYPE_GET_PORT_SETTINGS))
1100 		return -EMSGSIZE;
1101 	cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1102 	if (!cmd_info)
1103 		return -EMSGSIZE;
1104 	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1105 			       rocker_port->pport))
1106 		return -EMSGSIZE;
1107 	rocker_tlv_nest_end(desc_info, cmd_info);
1108 	return 0;
1109 }
1110 
1111 static int
1112 rocker_cmd_get_port_settings_ethtool_proc(const struct rocker_port *rocker_port,
1113 					  const struct rocker_desc_info *desc_info,
1114 					  void *priv)
1115 {
1116 	struct ethtool_link_ksettings *ecmd = priv;
1117 	const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1118 	const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1119 	u32 speed;
1120 	u8 duplex;
1121 	u8 autoneg;
1122 
1123 	rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1124 	if (!attrs[ROCKER_TLV_CMD_INFO])
1125 		return -EIO;
1126 
1127 	rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1128 				attrs[ROCKER_TLV_CMD_INFO]);
1129 	if (!info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED] ||
1130 	    !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX] ||
1131 	    !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG])
1132 		return -EIO;
1133 
1134 	speed = rocker_tlv_get_u32(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED]);
1135 	duplex = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX]);
1136 	autoneg = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG]);
1137 
1138 	ethtool_link_ksettings_zero_link_mode(ecmd, supported);
1139 	ethtool_link_ksettings_add_link_mode(ecmd, supported, TP);
1140 
1141 	ecmd->base.phy_address = 0xff;
1142 	ecmd->base.port = PORT_TP;
1143 	ecmd->base.speed = speed;
1144 	ecmd->base.duplex = duplex ? DUPLEX_FULL : DUPLEX_HALF;
1145 	ecmd->base.autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
1146 
1147 	return 0;
1148 }
1149 
1150 static int
1151 rocker_cmd_get_port_settings_macaddr_proc(const struct rocker_port *rocker_port,
1152 					  const struct rocker_desc_info *desc_info,
1153 					  void *priv)
1154 {
1155 	unsigned char *macaddr = priv;
1156 	const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1157 	const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1158 	const struct rocker_tlv *attr;
1159 
1160 	rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1161 	if (!attrs[ROCKER_TLV_CMD_INFO])
1162 		return -EIO;
1163 
1164 	rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1165 				attrs[ROCKER_TLV_CMD_INFO]);
1166 	attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR];
1167 	if (!attr)
1168 		return -EIO;
1169 
1170 	if (rocker_tlv_len(attr) != ETH_ALEN)
1171 		return -EINVAL;
1172 
1173 	ether_addr_copy(macaddr, rocker_tlv_data(attr));
1174 	return 0;
1175 }
1176 
1177 static int
1178 rocker_cmd_get_port_settings_mode_proc(const struct rocker_port *rocker_port,
1179 				       const struct rocker_desc_info *desc_info,
1180 				       void *priv)
1181 {
1182 	u8 *p_mode = priv;
1183 	const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1184 	const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1185 	const struct rocker_tlv *attr;
1186 
1187 	rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1188 	if (!attrs[ROCKER_TLV_CMD_INFO])
1189 		return -EIO;
1190 
1191 	rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1192 				attrs[ROCKER_TLV_CMD_INFO]);
1193 	attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MODE];
1194 	if (!attr)
1195 		return -EIO;
1196 
1197 	*p_mode = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MODE]);
1198 	return 0;
1199 }
1200 
1201 struct port_name {
1202 	char *buf;
1203 	size_t len;
1204 };
1205 
1206 static int
1207 rocker_cmd_get_port_settings_phys_name_proc(const struct rocker_port *rocker_port,
1208 					    const struct rocker_desc_info *desc_info,
1209 					    void *priv)
1210 {
1211 	const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1212 	const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1213 	struct port_name *name = priv;
1214 	const struct rocker_tlv *attr;
1215 	size_t i, j, len;
1216 	const char *str;
1217 
1218 	rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1219 	if (!attrs[ROCKER_TLV_CMD_INFO])
1220 		return -EIO;
1221 
1222 	rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1223 				attrs[ROCKER_TLV_CMD_INFO]);
1224 	attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_PHYS_NAME];
1225 	if (!attr)
1226 		return -EIO;
1227 
1228 	len = min_t(size_t, rocker_tlv_len(attr), name->len);
1229 	str = rocker_tlv_data(attr);
1230 
1231 	/* make sure name only contains alphanumeric characters */
1232 	for (i = j = 0; i < len; ++i) {
1233 		if (isalnum(str[i])) {
1234 			name->buf[j] = str[i];
1235 			j++;
1236 		}
1237 	}
1238 
1239 	if (j == 0)
1240 		return -EIO;
1241 
1242 	name->buf[j] = '\0';
1243 
1244 	return 0;
1245 }
1246 
1247 static int
1248 rocker_cmd_set_port_settings_ethtool_prep(const struct rocker_port *rocker_port,
1249 					  struct rocker_desc_info *desc_info,
1250 					  void *priv)
1251 {
1252 	struct ethtool_link_ksettings *ecmd = priv;
1253 	struct rocker_tlv *cmd_info;
1254 
1255 	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1256 			       ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1257 		return -EMSGSIZE;
1258 	cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1259 	if (!cmd_info)
1260 		return -EMSGSIZE;
1261 	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1262 			       rocker_port->pport))
1263 		return -EMSGSIZE;
1264 	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_SPEED,
1265 			       ecmd->base.speed))
1266 		return -EMSGSIZE;
1267 	if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX,
1268 			      ecmd->base.duplex))
1269 		return -EMSGSIZE;
1270 	if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG,
1271 			      ecmd->base.autoneg))
1272 		return -EMSGSIZE;
1273 	rocker_tlv_nest_end(desc_info, cmd_info);
1274 	return 0;
1275 }
1276 
1277 static int
1278 rocker_cmd_set_port_settings_macaddr_prep(const struct rocker_port *rocker_port,
1279 					  struct rocker_desc_info *desc_info,
1280 					  void *priv)
1281 {
1282 	const unsigned char *macaddr = priv;
1283 	struct rocker_tlv *cmd_info;
1284 
1285 	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1286 			       ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1287 		return -EMSGSIZE;
1288 	cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1289 	if (!cmd_info)
1290 		return -EMSGSIZE;
1291 	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1292 			       rocker_port->pport))
1293 		return -EMSGSIZE;
1294 	if (rocker_tlv_put(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR,
1295 			   ETH_ALEN, macaddr))
1296 		return -EMSGSIZE;
1297 	rocker_tlv_nest_end(desc_info, cmd_info);
1298 	return 0;
1299 }
1300 
1301 static int
1302 rocker_cmd_set_port_settings_mtu_prep(const struct rocker_port *rocker_port,
1303 				      struct rocker_desc_info *desc_info,
1304 				      void *priv)
1305 {
1306 	int mtu = *(int *)priv;
1307 	struct rocker_tlv *cmd_info;
1308 
1309 	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1310 			       ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1311 		return -EMSGSIZE;
1312 	cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1313 	if (!cmd_info)
1314 		return -EMSGSIZE;
1315 	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1316 			       rocker_port->pport))
1317 		return -EMSGSIZE;
1318 	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MTU,
1319 			       mtu))
1320 		return -EMSGSIZE;
1321 	rocker_tlv_nest_end(desc_info, cmd_info);
1322 	return 0;
1323 }
1324 
1325 static int
1326 rocker_cmd_set_port_learning_prep(const struct rocker_port *rocker_port,
1327 				  struct rocker_desc_info *desc_info,
1328 				  void *priv)
1329 {
1330 	bool learning = *(bool *)priv;
1331 	struct rocker_tlv *cmd_info;
1332 
1333 	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1334 			       ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1335 		return -EMSGSIZE;
1336 	cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1337 	if (!cmd_info)
1338 		return -EMSGSIZE;
1339 	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1340 			       rocker_port->pport))
1341 		return -EMSGSIZE;
1342 	if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_LEARNING,
1343 			      learning))
1344 		return -EMSGSIZE;
1345 	rocker_tlv_nest_end(desc_info, cmd_info);
1346 	return 0;
1347 }
1348 
1349 static int
1350 rocker_cmd_get_port_settings_ethtool(struct rocker_port *rocker_port,
1351 				     struct ethtool_link_ksettings *ecmd)
1352 {
1353 	return rocker_cmd_exec(rocker_port, false,
1354 			       rocker_cmd_get_port_settings_prep, NULL,
1355 			       rocker_cmd_get_port_settings_ethtool_proc,
1356 			       ecmd);
1357 }
1358 
1359 static int rocker_cmd_get_port_settings_macaddr(struct rocker_port *rocker_port,
1360 						unsigned char *macaddr)
1361 {
1362 	return rocker_cmd_exec(rocker_port, false,
1363 			       rocker_cmd_get_port_settings_prep, NULL,
1364 			       rocker_cmd_get_port_settings_macaddr_proc,
1365 			       macaddr);
1366 }
1367 
1368 static int rocker_cmd_get_port_settings_mode(struct rocker_port *rocker_port,
1369 					     u8 *p_mode)
1370 {
1371 	return rocker_cmd_exec(rocker_port, false,
1372 			       rocker_cmd_get_port_settings_prep, NULL,
1373 			       rocker_cmd_get_port_settings_mode_proc, p_mode);
1374 }
1375 
1376 static int
1377 rocker_cmd_set_port_settings_ethtool(struct rocker_port *rocker_port,
1378 				     const struct ethtool_link_ksettings *ecmd)
1379 {
1380 	struct ethtool_link_ksettings copy_ecmd;
1381 
1382 	memcpy(&copy_ecmd, ecmd, sizeof(copy_ecmd));
1383 
1384 	return rocker_cmd_exec(rocker_port, false,
1385 			       rocker_cmd_set_port_settings_ethtool_prep,
1386 			       &copy_ecmd, NULL, NULL);
1387 }
1388 
1389 static int rocker_cmd_set_port_settings_macaddr(struct rocker_port *rocker_port,
1390 						unsigned char *macaddr)
1391 {
1392 	return rocker_cmd_exec(rocker_port, false,
1393 			       rocker_cmd_set_port_settings_macaddr_prep,
1394 			       macaddr, NULL, NULL);
1395 }
1396 
1397 static int rocker_cmd_set_port_settings_mtu(struct rocker_port *rocker_port,
1398 					    int mtu)
1399 {
1400 	return rocker_cmd_exec(rocker_port, false,
1401 			       rocker_cmd_set_port_settings_mtu_prep,
1402 			       &mtu, NULL, NULL);
1403 }
1404 
1405 int rocker_port_set_learning(struct rocker_port *rocker_port,
1406 			     bool learning)
1407 {
1408 	return rocker_cmd_exec(rocker_port, false,
1409 			       rocker_cmd_set_port_learning_prep,
1410 			       &learning, NULL, NULL);
1411 }
1412 
1413 /**********************
1414  * Worlds manipulation
1415  **********************/
1416 
1417 static struct rocker_world_ops *rocker_world_ops[] = {
1418 	&rocker_ofdpa_ops,
1419 };
1420 
1421 #define ROCKER_WORLD_OPS_LEN ARRAY_SIZE(rocker_world_ops)
1422 
1423 static struct rocker_world_ops *rocker_world_ops_find(u8 mode)
1424 {
1425 	int i;
1426 
1427 	for (i = 0; i < ROCKER_WORLD_OPS_LEN; i++)
1428 		if (rocker_world_ops[i]->mode == mode)
1429 			return rocker_world_ops[i];
1430 	return NULL;
1431 }
1432 
1433 static int rocker_world_init(struct rocker *rocker, u8 mode)
1434 {
1435 	struct rocker_world_ops *wops;
1436 	int err;
1437 
1438 	wops = rocker_world_ops_find(mode);
1439 	if (!wops) {
1440 		dev_err(&rocker->pdev->dev, "port mode \"%d\" is not supported\n",
1441 			mode);
1442 		return -EINVAL;
1443 	}
1444 	rocker->wops = wops;
1445 	rocker->wpriv = kzalloc(wops->priv_size, GFP_KERNEL);
1446 	if (!rocker->wpriv)
1447 		return -ENOMEM;
1448 	if (!wops->init)
1449 		return 0;
1450 	err = wops->init(rocker);
1451 	if (err)
1452 		kfree(rocker->wpriv);
1453 	return err;
1454 }
1455 
1456 static void rocker_world_fini(struct rocker *rocker)
1457 {
1458 	struct rocker_world_ops *wops = rocker->wops;
1459 
1460 	if (!wops || !wops->fini)
1461 		return;
1462 	wops->fini(rocker);
1463 	kfree(rocker->wpriv);
1464 }
1465 
1466 static int rocker_world_check_init(struct rocker_port *rocker_port)
1467 {
1468 	struct rocker *rocker = rocker_port->rocker;
1469 	u8 mode;
1470 	int err;
1471 
1472 	err = rocker_cmd_get_port_settings_mode(rocker_port, &mode);
1473 	if (err) {
1474 		dev_err(&rocker->pdev->dev, "failed to get port mode\n");
1475 		return err;
1476 	}
1477 	if (rocker->wops) {
1478 		if (rocker->wops->mode != mode) {
1479 			dev_err(&rocker->pdev->dev, "hardware has ports in different worlds, which is not supported\n");
1480 			return -EINVAL;
1481 		}
1482 		return 0;
1483 	}
1484 	return rocker_world_init(rocker, mode);
1485 }
1486 
1487 static int rocker_world_port_pre_init(struct rocker_port *rocker_port)
1488 {
1489 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1490 	int err;
1491 
1492 	rocker_port->wpriv = kzalloc(wops->port_priv_size, GFP_KERNEL);
1493 	if (!rocker_port->wpriv)
1494 		return -ENOMEM;
1495 	if (!wops->port_pre_init)
1496 		return 0;
1497 	err = wops->port_pre_init(rocker_port);
1498 	if (err)
1499 		kfree(rocker_port->wpriv);
1500 	return 0;
1501 }
1502 
1503 static int rocker_world_port_init(struct rocker_port *rocker_port)
1504 {
1505 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1506 
1507 	if (!wops->port_init)
1508 		return 0;
1509 	return wops->port_init(rocker_port);
1510 }
1511 
1512 static void rocker_world_port_fini(struct rocker_port *rocker_port)
1513 {
1514 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1515 
1516 	if (!wops->port_fini)
1517 		return;
1518 	wops->port_fini(rocker_port);
1519 }
1520 
1521 static void rocker_world_port_post_fini(struct rocker_port *rocker_port)
1522 {
1523 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1524 
1525 	if (!wops->port_post_fini)
1526 		return;
1527 	wops->port_post_fini(rocker_port);
1528 	kfree(rocker_port->wpriv);
1529 }
1530 
1531 static int rocker_world_port_open(struct rocker_port *rocker_port)
1532 {
1533 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1534 
1535 	if (!wops->port_open)
1536 		return 0;
1537 	return wops->port_open(rocker_port);
1538 }
1539 
1540 static void rocker_world_port_stop(struct rocker_port *rocker_port)
1541 {
1542 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1543 
1544 	if (!wops->port_stop)
1545 		return;
1546 	wops->port_stop(rocker_port);
1547 }
1548 
1549 static int rocker_world_port_attr_stp_state_set(struct rocker_port *rocker_port,
1550 						u8 state,
1551 						struct switchdev_trans *trans)
1552 {
1553 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1554 
1555 	if (!wops->port_attr_stp_state_set)
1556 		return -EOPNOTSUPP;
1557 
1558 	if (switchdev_trans_ph_prepare(trans))
1559 		return 0;
1560 
1561 	return wops->port_attr_stp_state_set(rocker_port, state);
1562 }
1563 
1564 static int
1565 rocker_world_port_attr_bridge_flags_support_get(const struct rocker_port *
1566 						rocker_port,
1567 						unsigned long *
1568 						p_brport_flags_support)
1569 {
1570 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1571 
1572 	if (!wops->port_attr_bridge_flags_support_get)
1573 		return -EOPNOTSUPP;
1574 	return wops->port_attr_bridge_flags_support_get(rocker_port,
1575 							p_brport_flags_support);
1576 }
1577 
1578 static int
1579 rocker_world_port_attr_pre_bridge_flags_set(struct rocker_port *rocker_port,
1580 					    unsigned long brport_flags,
1581 					    struct switchdev_trans *trans)
1582 {
1583 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1584 	unsigned long brport_flags_s;
1585 	int err;
1586 
1587 	if (!wops->port_attr_bridge_flags_set)
1588 		return -EOPNOTSUPP;
1589 
1590 	err = rocker_world_port_attr_bridge_flags_support_get(rocker_port,
1591 							      &brport_flags_s);
1592 	if (err)
1593 		return err;
1594 
1595 	if (brport_flags & ~brport_flags_s)
1596 		return -EINVAL;
1597 
1598 	return 0;
1599 }
1600 
1601 static int
1602 rocker_world_port_attr_bridge_flags_set(struct rocker_port *rocker_port,
1603 					unsigned long brport_flags,
1604 					struct switchdev_trans *trans)
1605 {
1606 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1607 
1608 	if (!wops->port_attr_bridge_flags_set)
1609 		return -EOPNOTSUPP;
1610 
1611 	if (switchdev_trans_ph_prepare(trans))
1612 		return 0;
1613 
1614 	return wops->port_attr_bridge_flags_set(rocker_port, brport_flags,
1615 						trans);
1616 }
1617 
1618 static int
1619 rocker_world_port_attr_bridge_ageing_time_set(struct rocker_port *rocker_port,
1620 					      u32 ageing_time,
1621 					      struct switchdev_trans *trans)
1622 
1623 {
1624 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1625 
1626 	if (!wops->port_attr_bridge_ageing_time_set)
1627 		return -EOPNOTSUPP;
1628 
1629 	if (switchdev_trans_ph_prepare(trans))
1630 		return 0;
1631 
1632 	return wops->port_attr_bridge_ageing_time_set(rocker_port, ageing_time,
1633 						      trans);
1634 }
1635 
1636 static int
1637 rocker_world_port_obj_vlan_add(struct rocker_port *rocker_port,
1638 			       const struct switchdev_obj_port_vlan *vlan,
1639 			       struct switchdev_trans *trans)
1640 {
1641 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1642 
1643 	if (!wops->port_obj_vlan_add)
1644 		return -EOPNOTSUPP;
1645 
1646 	if (switchdev_trans_ph_prepare(trans))
1647 		return 0;
1648 
1649 	return wops->port_obj_vlan_add(rocker_port, vlan);
1650 }
1651 
1652 static int
1653 rocker_world_port_obj_vlan_del(struct rocker_port *rocker_port,
1654 			       const struct switchdev_obj_port_vlan *vlan)
1655 {
1656 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1657 
1658 	if (netif_is_bridge_master(vlan->obj.orig_dev))
1659 		return -EOPNOTSUPP;
1660 
1661 	if (!wops->port_obj_vlan_del)
1662 		return -EOPNOTSUPP;
1663 	return wops->port_obj_vlan_del(rocker_port, vlan);
1664 }
1665 
1666 static int
1667 rocker_world_port_fdb_add(struct rocker_port *rocker_port,
1668 			  struct switchdev_notifier_fdb_info *info)
1669 {
1670 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1671 
1672 	if (!wops->port_obj_fdb_add)
1673 		return -EOPNOTSUPP;
1674 
1675 	return wops->port_obj_fdb_add(rocker_port, info->vid, info->addr);
1676 }
1677 
1678 static int
1679 rocker_world_port_fdb_del(struct rocker_port *rocker_port,
1680 			  struct switchdev_notifier_fdb_info *info)
1681 {
1682 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1683 
1684 	if (!wops->port_obj_fdb_del)
1685 		return -EOPNOTSUPP;
1686 	return wops->port_obj_fdb_del(rocker_port, info->vid, info->addr);
1687 }
1688 
1689 static int rocker_world_port_master_linked(struct rocker_port *rocker_port,
1690 					   struct net_device *master)
1691 {
1692 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1693 
1694 	if (!wops->port_master_linked)
1695 		return -EOPNOTSUPP;
1696 	return wops->port_master_linked(rocker_port, master);
1697 }
1698 
1699 static int rocker_world_port_master_unlinked(struct rocker_port *rocker_port,
1700 					     struct net_device *master)
1701 {
1702 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1703 
1704 	if (!wops->port_master_unlinked)
1705 		return -EOPNOTSUPP;
1706 	return wops->port_master_unlinked(rocker_port, master);
1707 }
1708 
1709 static int rocker_world_port_neigh_update(struct rocker_port *rocker_port,
1710 					  struct neighbour *n)
1711 {
1712 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1713 
1714 	if (!wops->port_neigh_update)
1715 		return -EOPNOTSUPP;
1716 	return wops->port_neigh_update(rocker_port, n);
1717 }
1718 
1719 static int rocker_world_port_neigh_destroy(struct rocker_port *rocker_port,
1720 					   struct neighbour *n)
1721 {
1722 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1723 
1724 	if (!wops->port_neigh_destroy)
1725 		return -EOPNOTSUPP;
1726 	return wops->port_neigh_destroy(rocker_port, n);
1727 }
1728 
1729 static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port *rocker_port,
1730 					      const unsigned char *addr,
1731 					      __be16 vlan_id)
1732 {
1733 	struct rocker_world_ops *wops = rocker_port->rocker->wops;
1734 
1735 	if (!wops->port_ev_mac_vlan_seen)
1736 		return -EOPNOTSUPP;
1737 	return wops->port_ev_mac_vlan_seen(rocker_port, addr, vlan_id);
1738 }
1739 
1740 static int rocker_world_fib4_add(struct rocker *rocker,
1741 				 const struct fib_entry_notifier_info *fen_info)
1742 {
1743 	struct rocker_world_ops *wops = rocker->wops;
1744 
1745 	if (!wops->fib4_add)
1746 		return 0;
1747 	return wops->fib4_add(rocker, fen_info);
1748 }
1749 
1750 static int rocker_world_fib4_del(struct rocker *rocker,
1751 				 const struct fib_entry_notifier_info *fen_info)
1752 {
1753 	struct rocker_world_ops *wops = rocker->wops;
1754 
1755 	if (!wops->fib4_del)
1756 		return 0;
1757 	return wops->fib4_del(rocker, fen_info);
1758 }
1759 
1760 static void rocker_world_fib4_abort(struct rocker *rocker)
1761 {
1762 	struct rocker_world_ops *wops = rocker->wops;
1763 
1764 	if (wops->fib4_abort)
1765 		wops->fib4_abort(rocker);
1766 }
1767 
1768 /*****************
1769  * Net device ops
1770  *****************/
1771 
1772 static int rocker_port_open(struct net_device *dev)
1773 {
1774 	struct rocker_port *rocker_port = netdev_priv(dev);
1775 	int err;
1776 
1777 	err = rocker_port_dma_rings_init(rocker_port);
1778 	if (err)
1779 		return err;
1780 
1781 	err = request_irq(rocker_msix_tx_vector(rocker_port),
1782 			  rocker_tx_irq_handler, 0,
1783 			  rocker_driver_name, rocker_port);
1784 	if (err) {
1785 		netdev_err(rocker_port->dev, "cannot assign tx irq\n");
1786 		goto err_request_tx_irq;
1787 	}
1788 
1789 	err = request_irq(rocker_msix_rx_vector(rocker_port),
1790 			  rocker_rx_irq_handler, 0,
1791 			  rocker_driver_name, rocker_port);
1792 	if (err) {
1793 		netdev_err(rocker_port->dev, "cannot assign rx irq\n");
1794 		goto err_request_rx_irq;
1795 	}
1796 
1797 	err = rocker_world_port_open(rocker_port);
1798 	if (err) {
1799 		netdev_err(rocker_port->dev, "cannot open port in world\n");
1800 		goto err_world_port_open;
1801 	}
1802 
1803 	napi_enable(&rocker_port->napi_tx);
1804 	napi_enable(&rocker_port->napi_rx);
1805 	if (!dev->proto_down)
1806 		rocker_port_set_enable(rocker_port, true);
1807 	netif_start_queue(dev);
1808 	return 0;
1809 
1810 err_world_port_open:
1811 	free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
1812 err_request_rx_irq:
1813 	free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
1814 err_request_tx_irq:
1815 	rocker_port_dma_rings_fini(rocker_port);
1816 	return err;
1817 }
1818 
1819 static int rocker_port_stop(struct net_device *dev)
1820 {
1821 	struct rocker_port *rocker_port = netdev_priv(dev);
1822 
1823 	netif_stop_queue(dev);
1824 	rocker_port_set_enable(rocker_port, false);
1825 	napi_disable(&rocker_port->napi_rx);
1826 	napi_disable(&rocker_port->napi_tx);
1827 	rocker_world_port_stop(rocker_port);
1828 	free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
1829 	free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
1830 	rocker_port_dma_rings_fini(rocker_port);
1831 
1832 	return 0;
1833 }
1834 
1835 static void rocker_tx_desc_frags_unmap(const struct rocker_port *rocker_port,
1836 				       const struct rocker_desc_info *desc_info)
1837 {
1838 	const struct rocker *rocker = rocker_port->rocker;
1839 	struct pci_dev *pdev = rocker->pdev;
1840 	const struct rocker_tlv *attrs[ROCKER_TLV_TX_MAX + 1];
1841 	struct rocker_tlv *attr;
1842 	int rem;
1843 
1844 	rocker_tlv_parse_desc(attrs, ROCKER_TLV_TX_MAX, desc_info);
1845 	if (!attrs[ROCKER_TLV_TX_FRAGS])
1846 		return;
1847 	rocker_tlv_for_each_nested(attr, attrs[ROCKER_TLV_TX_FRAGS], rem) {
1848 		const struct rocker_tlv *frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_MAX + 1];
1849 		dma_addr_t dma_handle;
1850 		size_t len;
1851 
1852 		if (rocker_tlv_type(attr) != ROCKER_TLV_TX_FRAG)
1853 			continue;
1854 		rocker_tlv_parse_nested(frag_attrs, ROCKER_TLV_TX_FRAG_ATTR_MAX,
1855 					attr);
1856 		if (!frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR] ||
1857 		    !frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN])
1858 			continue;
1859 		dma_handle = rocker_tlv_get_u64(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR]);
1860 		len = rocker_tlv_get_u16(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN]);
1861 		pci_unmap_single(pdev, dma_handle, len, DMA_TO_DEVICE);
1862 	}
1863 }
1864 
1865 static int rocker_tx_desc_frag_map_put(const struct rocker_port *rocker_port,
1866 				       struct rocker_desc_info *desc_info,
1867 				       char *buf, size_t buf_len)
1868 {
1869 	const struct rocker *rocker = rocker_port->rocker;
1870 	struct pci_dev *pdev = rocker->pdev;
1871 	dma_addr_t dma_handle;
1872 	struct rocker_tlv *frag;
1873 
1874 	dma_handle = pci_map_single(pdev, buf, buf_len, DMA_TO_DEVICE);
1875 	if (unlikely(pci_dma_mapping_error(pdev, dma_handle))) {
1876 		if (net_ratelimit())
1877 			netdev_err(rocker_port->dev, "failed to dma map tx frag\n");
1878 		return -EIO;
1879 	}
1880 	frag = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAG);
1881 	if (!frag)
1882 		goto unmap_frag;
1883 	if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_TX_FRAG_ATTR_ADDR,
1884 			       dma_handle))
1885 		goto nest_cancel;
1886 	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_TX_FRAG_ATTR_LEN,
1887 			       buf_len))
1888 		goto nest_cancel;
1889 	rocker_tlv_nest_end(desc_info, frag);
1890 	return 0;
1891 
1892 nest_cancel:
1893 	rocker_tlv_nest_cancel(desc_info, frag);
1894 unmap_frag:
1895 	pci_unmap_single(pdev, dma_handle, buf_len, DMA_TO_DEVICE);
1896 	return -EMSGSIZE;
1897 }
1898 
1899 static netdev_tx_t rocker_port_xmit(struct sk_buff *skb, struct net_device *dev)
1900 {
1901 	struct rocker_port *rocker_port = netdev_priv(dev);
1902 	struct rocker *rocker = rocker_port->rocker;
1903 	struct rocker_desc_info *desc_info;
1904 	struct rocker_tlv *frags;
1905 	int i;
1906 	int err;
1907 
1908 	desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
1909 	if (unlikely(!desc_info)) {
1910 		if (net_ratelimit())
1911 			netdev_err(dev, "tx ring full when queue awake\n");
1912 		return NETDEV_TX_BUSY;
1913 	}
1914 
1915 	rocker_desc_cookie_ptr_set(desc_info, skb);
1916 
1917 	frags = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAGS);
1918 	if (!frags)
1919 		goto out;
1920 	err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
1921 					  skb->data, skb_headlen(skb));
1922 	if (err)
1923 		goto nest_cancel;
1924 	if (skb_shinfo(skb)->nr_frags > ROCKER_TX_FRAGS_MAX) {
1925 		err = skb_linearize(skb);
1926 		if (err)
1927 			goto unmap_frags;
1928 	}
1929 
1930 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1931 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1932 
1933 		err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
1934 						  skb_frag_address(frag),
1935 						  skb_frag_size(frag));
1936 		if (err)
1937 			goto unmap_frags;
1938 	}
1939 	rocker_tlv_nest_end(desc_info, frags);
1940 
1941 	rocker_desc_gen_clear(desc_info);
1942 	rocker_desc_head_set(rocker, &rocker_port->tx_ring, desc_info);
1943 
1944 	desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
1945 	if (!desc_info)
1946 		netif_stop_queue(dev);
1947 
1948 	return NETDEV_TX_OK;
1949 
1950 unmap_frags:
1951 	rocker_tx_desc_frags_unmap(rocker_port, desc_info);
1952 nest_cancel:
1953 	rocker_tlv_nest_cancel(desc_info, frags);
1954 out:
1955 	dev_kfree_skb(skb);
1956 	dev->stats.tx_dropped++;
1957 
1958 	return NETDEV_TX_OK;
1959 }
1960 
1961 static int rocker_port_set_mac_address(struct net_device *dev, void *p)
1962 {
1963 	struct sockaddr *addr = p;
1964 	struct rocker_port *rocker_port = netdev_priv(dev);
1965 	int err;
1966 
1967 	if (!is_valid_ether_addr(addr->sa_data))
1968 		return -EADDRNOTAVAIL;
1969 
1970 	err = rocker_cmd_set_port_settings_macaddr(rocker_port, addr->sa_data);
1971 	if (err)
1972 		return err;
1973 	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1974 	return 0;
1975 }
1976 
1977 static int rocker_port_change_mtu(struct net_device *dev, int new_mtu)
1978 {
1979 	struct rocker_port *rocker_port = netdev_priv(dev);
1980 	int running = netif_running(dev);
1981 	int err;
1982 
1983 	if (running)
1984 		rocker_port_stop(dev);
1985 
1986 	netdev_info(dev, "MTU change from %d to %d\n", dev->mtu, new_mtu);
1987 	dev->mtu = new_mtu;
1988 
1989 	err = rocker_cmd_set_port_settings_mtu(rocker_port, new_mtu);
1990 	if (err)
1991 		return err;
1992 
1993 	if (running)
1994 		err = rocker_port_open(dev);
1995 
1996 	return err;
1997 }
1998 
1999 static int rocker_port_get_phys_port_name(struct net_device *dev,
2000 					  char *buf, size_t len)
2001 {
2002 	struct rocker_port *rocker_port = netdev_priv(dev);
2003 	struct port_name name = { .buf = buf, .len = len };
2004 	int err;
2005 
2006 	err = rocker_cmd_exec(rocker_port, false,
2007 			      rocker_cmd_get_port_settings_prep, NULL,
2008 			      rocker_cmd_get_port_settings_phys_name_proc,
2009 			      &name);
2010 
2011 	return err ? -EOPNOTSUPP : 0;
2012 }
2013 
2014 static int rocker_port_change_proto_down(struct net_device *dev,
2015 					 bool proto_down)
2016 {
2017 	struct rocker_port *rocker_port = netdev_priv(dev);
2018 
2019 	if (rocker_port->dev->flags & IFF_UP)
2020 		rocker_port_set_enable(rocker_port, !proto_down);
2021 	rocker_port->dev->proto_down = proto_down;
2022 	return 0;
2023 }
2024 
2025 static void rocker_port_neigh_destroy(struct net_device *dev,
2026 				      struct neighbour *n)
2027 {
2028 	struct rocker_port *rocker_port = netdev_priv(n->dev);
2029 	int err;
2030 
2031 	err = rocker_world_port_neigh_destroy(rocker_port, n);
2032 	if (err)
2033 		netdev_warn(rocker_port->dev, "failed to handle neigh destroy (err %d)\n",
2034 			    err);
2035 }
2036 
2037 static int rocker_port_get_port_parent_id(struct net_device *dev,
2038 					  struct netdev_phys_item_id *ppid)
2039 {
2040 	const struct rocker_port *rocker_port = netdev_priv(dev);
2041 	const struct rocker *rocker = rocker_port->rocker;
2042 
2043 	ppid->id_len = sizeof(rocker->hw.id);
2044 	memcpy(&ppid->id, &rocker->hw.id, ppid->id_len);
2045 
2046 	return 0;
2047 }
2048 
2049 static const struct net_device_ops rocker_port_netdev_ops = {
2050 	.ndo_open			= rocker_port_open,
2051 	.ndo_stop			= rocker_port_stop,
2052 	.ndo_start_xmit			= rocker_port_xmit,
2053 	.ndo_set_mac_address		= rocker_port_set_mac_address,
2054 	.ndo_change_mtu			= rocker_port_change_mtu,
2055 	.ndo_get_phys_port_name		= rocker_port_get_phys_port_name,
2056 	.ndo_change_proto_down		= rocker_port_change_proto_down,
2057 	.ndo_neigh_destroy		= rocker_port_neigh_destroy,
2058 	.ndo_get_port_parent_id		= rocker_port_get_port_parent_id,
2059 };
2060 
2061 /********************
2062  * swdev interface
2063  ********************/
2064 
2065 static int rocker_port_attr_set(struct net_device *dev,
2066 				const struct switchdev_attr *attr,
2067 				struct switchdev_trans *trans)
2068 {
2069 	struct rocker_port *rocker_port = netdev_priv(dev);
2070 	int err = 0;
2071 
2072 	switch (attr->id) {
2073 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
2074 		err = rocker_world_port_attr_stp_state_set(rocker_port,
2075 							   attr->u.stp_state,
2076 							   trans);
2077 		break;
2078 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
2079 		err = rocker_world_port_attr_pre_bridge_flags_set(rocker_port,
2080 							      attr->u.brport_flags,
2081 							      trans);
2082 		break;
2083 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
2084 		err = rocker_world_port_attr_bridge_flags_set(rocker_port,
2085 							      attr->u.brport_flags,
2086 							      trans);
2087 		break;
2088 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
2089 		err = rocker_world_port_attr_bridge_ageing_time_set(rocker_port,
2090 								    attr->u.ageing_time,
2091 								    trans);
2092 		break;
2093 	default:
2094 		err = -EOPNOTSUPP;
2095 		break;
2096 	}
2097 
2098 	return err;
2099 }
2100 
2101 static int rocker_port_obj_add(struct net_device *dev,
2102 			       const struct switchdev_obj *obj,
2103 			       struct switchdev_trans *trans)
2104 {
2105 	struct rocker_port *rocker_port = netdev_priv(dev);
2106 	int err = 0;
2107 
2108 	switch (obj->id) {
2109 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
2110 		err = rocker_world_port_obj_vlan_add(rocker_port,
2111 						     SWITCHDEV_OBJ_PORT_VLAN(obj),
2112 						     trans);
2113 		break;
2114 	default:
2115 		err = -EOPNOTSUPP;
2116 		break;
2117 	}
2118 
2119 	return err;
2120 }
2121 
2122 static int rocker_port_obj_del(struct net_device *dev,
2123 			       const struct switchdev_obj *obj)
2124 {
2125 	struct rocker_port *rocker_port = netdev_priv(dev);
2126 	int err = 0;
2127 
2128 	switch (obj->id) {
2129 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
2130 		err = rocker_world_port_obj_vlan_del(rocker_port,
2131 						     SWITCHDEV_OBJ_PORT_VLAN(obj));
2132 		break;
2133 	default:
2134 		err = -EOPNOTSUPP;
2135 		break;
2136 	}
2137 
2138 	return err;
2139 }
2140 
2141 struct rocker_fib_event_work {
2142 	struct work_struct work;
2143 	union {
2144 		struct fib_entry_notifier_info fen_info;
2145 		struct fib_rule_notifier_info fr_info;
2146 	};
2147 	struct rocker *rocker;
2148 	unsigned long event;
2149 };
2150 
2151 static void rocker_router_fib_event_work(struct work_struct *work)
2152 {
2153 	struct rocker_fib_event_work *fib_work =
2154 		container_of(work, struct rocker_fib_event_work, work);
2155 	struct rocker *rocker = fib_work->rocker;
2156 	struct fib_rule *rule;
2157 	int err;
2158 
2159 	/* Protect internal structures from changes */
2160 	rtnl_lock();
2161 	switch (fib_work->event) {
2162 	case FIB_EVENT_ENTRY_ADD:
2163 		err = rocker_world_fib4_add(rocker, &fib_work->fen_info);
2164 		if (err)
2165 			rocker_world_fib4_abort(rocker);
2166 		fib_info_put(fib_work->fen_info.fi);
2167 		break;
2168 	case FIB_EVENT_ENTRY_DEL:
2169 		rocker_world_fib4_del(rocker, &fib_work->fen_info);
2170 		fib_info_put(fib_work->fen_info.fi);
2171 		break;
2172 	case FIB_EVENT_RULE_ADD: /* fall through */
2173 	case FIB_EVENT_RULE_DEL:
2174 		rule = fib_work->fr_info.rule;
2175 		if (!fib4_rule_default(rule))
2176 			rocker_world_fib4_abort(rocker);
2177 		fib_rule_put(rule);
2178 		break;
2179 	}
2180 	rtnl_unlock();
2181 	kfree(fib_work);
2182 }
2183 
2184 /* Called with rcu_read_lock() */
2185 static int rocker_router_fib_event(struct notifier_block *nb,
2186 				   unsigned long event, void *ptr)
2187 {
2188 	struct rocker *rocker = container_of(nb, struct rocker, fib_nb);
2189 	struct rocker_fib_event_work *fib_work;
2190 	struct fib_notifier_info *info = ptr;
2191 
2192 	if (info->family != AF_INET)
2193 		return NOTIFY_DONE;
2194 
2195 	fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
2196 	if (WARN_ON(!fib_work))
2197 		return NOTIFY_BAD;
2198 
2199 	INIT_WORK(&fib_work->work, rocker_router_fib_event_work);
2200 	fib_work->rocker = rocker;
2201 	fib_work->event = event;
2202 
2203 	switch (event) {
2204 	case FIB_EVENT_ENTRY_ADD: /* fall through */
2205 	case FIB_EVENT_ENTRY_DEL:
2206 		if (info->family == AF_INET) {
2207 			struct fib_entry_notifier_info *fen_info = ptr;
2208 
2209 			if (fen_info->fi->fib_nh_is_v6) {
2210 				NL_SET_ERR_MSG_MOD(info->extack, "IPv6 gateway with IPv4 route is not supported");
2211 				return notifier_from_errno(-EINVAL);
2212 			}
2213 			if (fen_info->fi->nh) {
2214 				NL_SET_ERR_MSG_MOD(info->extack, "IPv4 route with nexthop objects is not supported");
2215 				return notifier_from_errno(-EINVAL);
2216 			}
2217 		}
2218 
2219 		memcpy(&fib_work->fen_info, ptr, sizeof(fib_work->fen_info));
2220 		/* Take referece on fib_info to prevent it from being
2221 		 * freed while work is queued. Release it afterwards.
2222 		 */
2223 		fib_info_hold(fib_work->fen_info.fi);
2224 		break;
2225 	case FIB_EVENT_RULE_ADD: /* fall through */
2226 	case FIB_EVENT_RULE_DEL:
2227 		memcpy(&fib_work->fr_info, ptr, sizeof(fib_work->fr_info));
2228 		fib_rule_get(fib_work->fr_info.rule);
2229 		break;
2230 	}
2231 
2232 	queue_work(rocker->rocker_owq, &fib_work->work);
2233 
2234 	return NOTIFY_DONE;
2235 }
2236 
2237 /********************
2238  * ethtool interface
2239  ********************/
2240 
2241 static int
2242 rocker_port_get_link_ksettings(struct net_device *dev,
2243 			       struct ethtool_link_ksettings *ecmd)
2244 {
2245 	struct rocker_port *rocker_port = netdev_priv(dev);
2246 
2247 	return rocker_cmd_get_port_settings_ethtool(rocker_port, ecmd);
2248 }
2249 
2250 static int
2251 rocker_port_set_link_ksettings(struct net_device *dev,
2252 			       const struct ethtool_link_ksettings *ecmd)
2253 {
2254 	struct rocker_port *rocker_port = netdev_priv(dev);
2255 
2256 	return rocker_cmd_set_port_settings_ethtool(rocker_port, ecmd);
2257 }
2258 
2259 static void rocker_port_get_drvinfo(struct net_device *dev,
2260 				    struct ethtool_drvinfo *drvinfo)
2261 {
2262 	strlcpy(drvinfo->driver, rocker_driver_name, sizeof(drvinfo->driver));
2263 	strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
2264 }
2265 
2266 static struct rocker_port_stats {
2267 	char str[ETH_GSTRING_LEN];
2268 	int type;
2269 } rocker_port_stats[] = {
2270 	{ "rx_packets", ROCKER_TLV_CMD_PORT_STATS_RX_PKTS,    },
2271 	{ "rx_bytes",   ROCKER_TLV_CMD_PORT_STATS_RX_BYTES,   },
2272 	{ "rx_dropped", ROCKER_TLV_CMD_PORT_STATS_RX_DROPPED, },
2273 	{ "rx_errors",  ROCKER_TLV_CMD_PORT_STATS_RX_ERRORS,  },
2274 
2275 	{ "tx_packets", ROCKER_TLV_CMD_PORT_STATS_TX_PKTS,    },
2276 	{ "tx_bytes",   ROCKER_TLV_CMD_PORT_STATS_TX_BYTES,   },
2277 	{ "tx_dropped", ROCKER_TLV_CMD_PORT_STATS_TX_DROPPED, },
2278 	{ "tx_errors",  ROCKER_TLV_CMD_PORT_STATS_TX_ERRORS,  },
2279 };
2280 
2281 #define ROCKER_PORT_STATS_LEN  ARRAY_SIZE(rocker_port_stats)
2282 
2283 static void rocker_port_get_strings(struct net_device *netdev, u32 stringset,
2284 				    u8 *data)
2285 {
2286 	u8 *p = data;
2287 	int i;
2288 
2289 	switch (stringset) {
2290 	case ETH_SS_STATS:
2291 		for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
2292 			memcpy(p, rocker_port_stats[i].str, ETH_GSTRING_LEN);
2293 			p += ETH_GSTRING_LEN;
2294 		}
2295 		break;
2296 	}
2297 }
2298 
2299 static int
2300 rocker_cmd_get_port_stats_prep(const struct rocker_port *rocker_port,
2301 			       struct rocker_desc_info *desc_info,
2302 			       void *priv)
2303 {
2304 	struct rocker_tlv *cmd_stats;
2305 
2306 	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
2307 			       ROCKER_TLV_CMD_TYPE_GET_PORT_STATS))
2308 		return -EMSGSIZE;
2309 
2310 	cmd_stats = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2311 	if (!cmd_stats)
2312 		return -EMSGSIZE;
2313 
2314 	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_STATS_PPORT,
2315 			       rocker_port->pport))
2316 		return -EMSGSIZE;
2317 
2318 	rocker_tlv_nest_end(desc_info, cmd_stats);
2319 
2320 	return 0;
2321 }
2322 
2323 static int
2324 rocker_cmd_get_port_stats_ethtool_proc(const struct rocker_port *rocker_port,
2325 				       const struct rocker_desc_info *desc_info,
2326 				       void *priv)
2327 {
2328 	const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
2329 	const struct rocker_tlv *stats_attrs[ROCKER_TLV_CMD_PORT_STATS_MAX + 1];
2330 	const struct rocker_tlv *pattr;
2331 	u32 pport;
2332 	u64 *data = priv;
2333 	int i;
2334 
2335 	rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
2336 
2337 	if (!attrs[ROCKER_TLV_CMD_INFO])
2338 		return -EIO;
2339 
2340 	rocker_tlv_parse_nested(stats_attrs, ROCKER_TLV_CMD_PORT_STATS_MAX,
2341 				attrs[ROCKER_TLV_CMD_INFO]);
2342 
2343 	if (!stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT])
2344 		return -EIO;
2345 
2346 	pport = rocker_tlv_get_u32(stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]);
2347 	if (pport != rocker_port->pport)
2348 		return -EIO;
2349 
2350 	for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
2351 		pattr = stats_attrs[rocker_port_stats[i].type];
2352 		if (!pattr)
2353 			continue;
2354 
2355 		data[i] = rocker_tlv_get_u64(pattr);
2356 	}
2357 
2358 	return 0;
2359 }
2360 
2361 static int rocker_cmd_get_port_stats_ethtool(struct rocker_port *rocker_port,
2362 					     void *priv)
2363 {
2364 	return rocker_cmd_exec(rocker_port, false,
2365 			       rocker_cmd_get_port_stats_prep, NULL,
2366 			       rocker_cmd_get_port_stats_ethtool_proc,
2367 			       priv);
2368 }
2369 
2370 static void rocker_port_get_stats(struct net_device *dev,
2371 				  struct ethtool_stats *stats, u64 *data)
2372 {
2373 	struct rocker_port *rocker_port = netdev_priv(dev);
2374 
2375 	if (rocker_cmd_get_port_stats_ethtool(rocker_port, data) != 0) {
2376 		int i;
2377 
2378 		for (i = 0; i < ARRAY_SIZE(rocker_port_stats); ++i)
2379 			data[i] = 0;
2380 	}
2381 }
2382 
2383 static int rocker_port_get_sset_count(struct net_device *netdev, int sset)
2384 {
2385 	switch (sset) {
2386 	case ETH_SS_STATS:
2387 		return ROCKER_PORT_STATS_LEN;
2388 	default:
2389 		return -EOPNOTSUPP;
2390 	}
2391 }
2392 
2393 static const struct ethtool_ops rocker_port_ethtool_ops = {
2394 	.get_drvinfo		= rocker_port_get_drvinfo,
2395 	.get_link		= ethtool_op_get_link,
2396 	.get_strings		= rocker_port_get_strings,
2397 	.get_ethtool_stats	= rocker_port_get_stats,
2398 	.get_sset_count		= rocker_port_get_sset_count,
2399 	.get_link_ksettings	= rocker_port_get_link_ksettings,
2400 	.set_link_ksettings	= rocker_port_set_link_ksettings,
2401 };
2402 
2403 /*****************
2404  * NAPI interface
2405  *****************/
2406 
2407 static struct rocker_port *rocker_port_napi_tx_get(struct napi_struct *napi)
2408 {
2409 	return container_of(napi, struct rocker_port, napi_tx);
2410 }
2411 
2412 static int rocker_port_poll_tx(struct napi_struct *napi, int budget)
2413 {
2414 	struct rocker_port *rocker_port = rocker_port_napi_tx_get(napi);
2415 	const struct rocker *rocker = rocker_port->rocker;
2416 	const struct rocker_desc_info *desc_info;
2417 	u32 credits = 0;
2418 	int err;
2419 
2420 	/* Cleanup tx descriptors */
2421 	while ((desc_info = rocker_desc_tail_get(&rocker_port->tx_ring))) {
2422 		struct sk_buff *skb;
2423 
2424 		err = rocker_desc_err(desc_info);
2425 		if (err && net_ratelimit())
2426 			netdev_err(rocker_port->dev, "tx desc received with err %d\n",
2427 				   err);
2428 		rocker_tx_desc_frags_unmap(rocker_port, desc_info);
2429 
2430 		skb = rocker_desc_cookie_ptr_get(desc_info);
2431 		if (err == 0) {
2432 			rocker_port->dev->stats.tx_packets++;
2433 			rocker_port->dev->stats.tx_bytes += skb->len;
2434 		} else {
2435 			rocker_port->dev->stats.tx_errors++;
2436 		}
2437 
2438 		dev_kfree_skb_any(skb);
2439 		credits++;
2440 	}
2441 
2442 	if (credits && netif_queue_stopped(rocker_port->dev))
2443 		netif_wake_queue(rocker_port->dev);
2444 
2445 	napi_complete(napi);
2446 	rocker_dma_ring_credits_set(rocker, &rocker_port->tx_ring, credits);
2447 
2448 	return 0;
2449 }
2450 
2451 static int rocker_port_rx_proc(const struct rocker *rocker,
2452 			       const struct rocker_port *rocker_port,
2453 			       struct rocker_desc_info *desc_info)
2454 {
2455 	const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
2456 	struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
2457 	size_t rx_len;
2458 	u16 rx_flags = 0;
2459 
2460 	if (!skb)
2461 		return -ENOENT;
2462 
2463 	rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
2464 	if (!attrs[ROCKER_TLV_RX_FRAG_LEN])
2465 		return -EINVAL;
2466 	if (attrs[ROCKER_TLV_RX_FLAGS])
2467 		rx_flags = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FLAGS]);
2468 
2469 	rocker_dma_rx_ring_skb_unmap(rocker, attrs);
2470 
2471 	rx_len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_LEN]);
2472 	skb_put(skb, rx_len);
2473 	skb->protocol = eth_type_trans(skb, rocker_port->dev);
2474 
2475 	if (rx_flags & ROCKER_RX_FLAGS_FWD_OFFLOAD)
2476 		skb->offload_fwd_mark = 1;
2477 
2478 	rocker_port->dev->stats.rx_packets++;
2479 	rocker_port->dev->stats.rx_bytes += skb->len;
2480 
2481 	netif_receive_skb(skb);
2482 
2483 	return rocker_dma_rx_ring_skb_alloc(rocker_port, desc_info);
2484 }
2485 
2486 static struct rocker_port *rocker_port_napi_rx_get(struct napi_struct *napi)
2487 {
2488 	return container_of(napi, struct rocker_port, napi_rx);
2489 }
2490 
2491 static int rocker_port_poll_rx(struct napi_struct *napi, int budget)
2492 {
2493 	struct rocker_port *rocker_port = rocker_port_napi_rx_get(napi);
2494 	const struct rocker *rocker = rocker_port->rocker;
2495 	struct rocker_desc_info *desc_info;
2496 	u32 credits = 0;
2497 	int err;
2498 
2499 	/* Process rx descriptors */
2500 	while (credits < budget &&
2501 	       (desc_info = rocker_desc_tail_get(&rocker_port->rx_ring))) {
2502 		err = rocker_desc_err(desc_info);
2503 		if (err) {
2504 			if (net_ratelimit())
2505 				netdev_err(rocker_port->dev, "rx desc received with err %d\n",
2506 					   err);
2507 		} else {
2508 			err = rocker_port_rx_proc(rocker, rocker_port,
2509 						  desc_info);
2510 			if (err && net_ratelimit())
2511 				netdev_err(rocker_port->dev, "rx processing failed with err %d\n",
2512 					   err);
2513 		}
2514 		if (err)
2515 			rocker_port->dev->stats.rx_errors++;
2516 
2517 		rocker_desc_gen_clear(desc_info);
2518 		rocker_desc_head_set(rocker, &rocker_port->rx_ring, desc_info);
2519 		credits++;
2520 	}
2521 
2522 	if (credits < budget)
2523 		napi_complete_done(napi, credits);
2524 
2525 	rocker_dma_ring_credits_set(rocker, &rocker_port->rx_ring, credits);
2526 
2527 	return credits;
2528 }
2529 
2530 /*****************
2531  * PCI driver ops
2532  *****************/
2533 
2534 static void rocker_carrier_init(const struct rocker_port *rocker_port)
2535 {
2536 	const struct rocker *rocker = rocker_port->rocker;
2537 	u64 link_status = rocker_read64(rocker, PORT_PHYS_LINK_STATUS);
2538 	bool link_up;
2539 
2540 	link_up = link_status & (1 << rocker_port->pport);
2541 	if (link_up)
2542 		netif_carrier_on(rocker_port->dev);
2543 	else
2544 		netif_carrier_off(rocker_port->dev);
2545 }
2546 
2547 static void rocker_remove_ports(struct rocker *rocker)
2548 {
2549 	struct rocker_port *rocker_port;
2550 	int i;
2551 
2552 	for (i = 0; i < rocker->port_count; i++) {
2553 		rocker_port = rocker->ports[i];
2554 		if (!rocker_port)
2555 			continue;
2556 		rocker_world_port_fini(rocker_port);
2557 		unregister_netdev(rocker_port->dev);
2558 		rocker_world_port_post_fini(rocker_port);
2559 		free_netdev(rocker_port->dev);
2560 	}
2561 	rocker_world_fini(rocker);
2562 	kfree(rocker->ports);
2563 }
2564 
2565 static void rocker_port_dev_addr_init(struct rocker_port *rocker_port)
2566 {
2567 	const struct rocker *rocker = rocker_port->rocker;
2568 	const struct pci_dev *pdev = rocker->pdev;
2569 	int err;
2570 
2571 	err = rocker_cmd_get_port_settings_macaddr(rocker_port,
2572 						   rocker_port->dev->dev_addr);
2573 	if (err) {
2574 		dev_warn(&pdev->dev, "failed to get mac address, using random\n");
2575 		eth_hw_addr_random(rocker_port->dev);
2576 	}
2577 }
2578 
2579 #define ROCKER_PORT_MIN_MTU	ETH_MIN_MTU
2580 #define ROCKER_PORT_MAX_MTU	9000
2581 static int rocker_probe_port(struct rocker *rocker, unsigned int port_number)
2582 {
2583 	struct pci_dev *pdev = rocker->pdev;
2584 	struct rocker_port *rocker_port;
2585 	struct net_device *dev;
2586 	int err;
2587 
2588 	dev = alloc_etherdev(sizeof(struct rocker_port));
2589 	if (!dev)
2590 		return -ENOMEM;
2591 	SET_NETDEV_DEV(dev, &pdev->dev);
2592 	rocker_port = netdev_priv(dev);
2593 	rocker_port->dev = dev;
2594 	rocker_port->rocker = rocker;
2595 	rocker_port->port_number = port_number;
2596 	rocker_port->pport = port_number + 1;
2597 
2598 	err = rocker_world_check_init(rocker_port);
2599 	if (err) {
2600 		dev_err(&pdev->dev, "world init failed\n");
2601 		goto err_world_check_init;
2602 	}
2603 
2604 	rocker_port_dev_addr_init(rocker_port);
2605 	dev->netdev_ops = &rocker_port_netdev_ops;
2606 	dev->ethtool_ops = &rocker_port_ethtool_ops;
2607 	netif_tx_napi_add(dev, &rocker_port->napi_tx, rocker_port_poll_tx,
2608 			  NAPI_POLL_WEIGHT);
2609 	netif_napi_add(dev, &rocker_port->napi_rx, rocker_port_poll_rx,
2610 		       NAPI_POLL_WEIGHT);
2611 	rocker_carrier_init(rocker_port);
2612 
2613 	dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_SG;
2614 
2615 	/* MTU range: 68 - 9000 */
2616 	dev->min_mtu = ROCKER_PORT_MIN_MTU;
2617 	dev->max_mtu = ROCKER_PORT_MAX_MTU;
2618 
2619 	err = rocker_world_port_pre_init(rocker_port);
2620 	if (err) {
2621 		dev_err(&pdev->dev, "port world pre-init failed\n");
2622 		goto err_world_port_pre_init;
2623 	}
2624 	err = register_netdev(dev);
2625 	if (err) {
2626 		dev_err(&pdev->dev, "register_netdev failed\n");
2627 		goto err_register_netdev;
2628 	}
2629 	rocker->ports[port_number] = rocker_port;
2630 
2631 	err = rocker_world_port_init(rocker_port);
2632 	if (err) {
2633 		dev_err(&pdev->dev, "port world init failed\n");
2634 		goto err_world_port_init;
2635 	}
2636 
2637 	return 0;
2638 
2639 err_world_port_init:
2640 	rocker->ports[port_number] = NULL;
2641 	unregister_netdev(dev);
2642 err_register_netdev:
2643 	rocker_world_port_post_fini(rocker_port);
2644 err_world_port_pre_init:
2645 err_world_check_init:
2646 	free_netdev(dev);
2647 	return err;
2648 }
2649 
2650 static int rocker_probe_ports(struct rocker *rocker)
2651 {
2652 	int i;
2653 	size_t alloc_size;
2654 	int err;
2655 
2656 	alloc_size = sizeof(struct rocker_port *) * rocker->port_count;
2657 	rocker->ports = kzalloc(alloc_size, GFP_KERNEL);
2658 	if (!rocker->ports)
2659 		return -ENOMEM;
2660 	for (i = 0; i < rocker->port_count; i++) {
2661 		err = rocker_probe_port(rocker, i);
2662 		if (err)
2663 			goto remove_ports;
2664 	}
2665 	return 0;
2666 
2667 remove_ports:
2668 	rocker_remove_ports(rocker);
2669 	return err;
2670 }
2671 
2672 static int rocker_msix_init(struct rocker *rocker)
2673 {
2674 	struct pci_dev *pdev = rocker->pdev;
2675 	int msix_entries;
2676 	int i;
2677 	int err;
2678 
2679 	msix_entries = pci_msix_vec_count(pdev);
2680 	if (msix_entries < 0)
2681 		return msix_entries;
2682 
2683 	if (msix_entries != ROCKER_MSIX_VEC_COUNT(rocker->port_count))
2684 		return -EINVAL;
2685 
2686 	rocker->msix_entries = kmalloc_array(msix_entries,
2687 					     sizeof(struct msix_entry),
2688 					     GFP_KERNEL);
2689 	if (!rocker->msix_entries)
2690 		return -ENOMEM;
2691 
2692 	for (i = 0; i < msix_entries; i++)
2693 		rocker->msix_entries[i].entry = i;
2694 
2695 	err = pci_enable_msix_exact(pdev, rocker->msix_entries, msix_entries);
2696 	if (err < 0)
2697 		goto err_enable_msix;
2698 
2699 	return 0;
2700 
2701 err_enable_msix:
2702 	kfree(rocker->msix_entries);
2703 	return err;
2704 }
2705 
2706 static void rocker_msix_fini(const struct rocker *rocker)
2707 {
2708 	pci_disable_msix(rocker->pdev);
2709 	kfree(rocker->msix_entries);
2710 }
2711 
2712 static bool rocker_port_dev_check(const struct net_device *dev)
2713 {
2714 	return dev->netdev_ops == &rocker_port_netdev_ops;
2715 }
2716 
2717 static int
2718 rocker_switchdev_port_attr_set_event(struct net_device *netdev,
2719 		struct switchdev_notifier_port_attr_info *port_attr_info)
2720 {
2721 	int err;
2722 
2723 	err = rocker_port_attr_set(netdev, port_attr_info->attr,
2724 				   port_attr_info->trans);
2725 
2726 	port_attr_info->handled = true;
2727 	return notifier_from_errno(err);
2728 }
2729 
2730 struct rocker_switchdev_event_work {
2731 	struct work_struct work;
2732 	struct switchdev_notifier_fdb_info fdb_info;
2733 	struct rocker_port *rocker_port;
2734 	unsigned long event;
2735 };
2736 
2737 static void
2738 rocker_fdb_offload_notify(struct rocker_port *rocker_port,
2739 			  struct switchdev_notifier_fdb_info *recv_info)
2740 {
2741 	struct switchdev_notifier_fdb_info info;
2742 
2743 	info.addr = recv_info->addr;
2744 	info.vid = recv_info->vid;
2745 	info.offloaded = true;
2746 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2747 				 rocker_port->dev, &info.info, NULL);
2748 }
2749 
2750 static void rocker_switchdev_event_work(struct work_struct *work)
2751 {
2752 	struct rocker_switchdev_event_work *switchdev_work =
2753 		container_of(work, struct rocker_switchdev_event_work, work);
2754 	struct rocker_port *rocker_port = switchdev_work->rocker_port;
2755 	struct switchdev_notifier_fdb_info *fdb_info;
2756 	int err;
2757 
2758 	rtnl_lock();
2759 	switch (switchdev_work->event) {
2760 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2761 		fdb_info = &switchdev_work->fdb_info;
2762 		if (!fdb_info->added_by_user)
2763 			break;
2764 		err = rocker_world_port_fdb_add(rocker_port, fdb_info);
2765 		if (err) {
2766 			netdev_dbg(rocker_port->dev, "fdb add failed err=%d\n", err);
2767 			break;
2768 		}
2769 		rocker_fdb_offload_notify(rocker_port, fdb_info);
2770 		break;
2771 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2772 		fdb_info = &switchdev_work->fdb_info;
2773 		if (!fdb_info->added_by_user)
2774 			break;
2775 		err = rocker_world_port_fdb_del(rocker_port, fdb_info);
2776 		if (err)
2777 			netdev_dbg(rocker_port->dev, "fdb add failed err=%d\n", err);
2778 		break;
2779 	}
2780 	rtnl_unlock();
2781 
2782 	kfree(switchdev_work->fdb_info.addr);
2783 	kfree(switchdev_work);
2784 	dev_put(rocker_port->dev);
2785 }
2786 
2787 /* called under rcu_read_lock() */
2788 static int rocker_switchdev_event(struct notifier_block *unused,
2789 				  unsigned long event, void *ptr)
2790 {
2791 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2792 	struct rocker_switchdev_event_work *switchdev_work;
2793 	struct switchdev_notifier_fdb_info *fdb_info = ptr;
2794 	struct rocker_port *rocker_port;
2795 
2796 	if (!rocker_port_dev_check(dev))
2797 		return NOTIFY_DONE;
2798 
2799 	if (event == SWITCHDEV_PORT_ATTR_SET)
2800 		return rocker_switchdev_port_attr_set_event(dev, ptr);
2801 
2802 	rocker_port = netdev_priv(dev);
2803 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2804 	if (WARN_ON(!switchdev_work))
2805 		return NOTIFY_BAD;
2806 
2807 	INIT_WORK(&switchdev_work->work, rocker_switchdev_event_work);
2808 	switchdev_work->rocker_port = rocker_port;
2809 	switchdev_work->event = event;
2810 
2811 	switch (event) {
2812 	case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
2813 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2814 		memcpy(&switchdev_work->fdb_info, ptr,
2815 		       sizeof(switchdev_work->fdb_info));
2816 		switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2817 		if (unlikely(!switchdev_work->fdb_info.addr)) {
2818 			kfree(switchdev_work);
2819 			return NOTIFY_BAD;
2820 		}
2821 
2822 		ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2823 				fdb_info->addr);
2824 		/* Take a reference on the rocker device */
2825 		dev_hold(dev);
2826 		break;
2827 	default:
2828 		kfree(switchdev_work);
2829 		return NOTIFY_DONE;
2830 	}
2831 
2832 	queue_work(rocker_port->rocker->rocker_owq,
2833 		   &switchdev_work->work);
2834 	return NOTIFY_DONE;
2835 }
2836 
2837 static int
2838 rocker_switchdev_port_obj_event(unsigned long event, struct net_device *netdev,
2839 			struct switchdev_notifier_port_obj_info *port_obj_info)
2840 {
2841 	int err = -EOPNOTSUPP;
2842 
2843 	switch (event) {
2844 	case SWITCHDEV_PORT_OBJ_ADD:
2845 		err = rocker_port_obj_add(netdev, port_obj_info->obj,
2846 					  port_obj_info->trans);
2847 		break;
2848 	case SWITCHDEV_PORT_OBJ_DEL:
2849 		err = rocker_port_obj_del(netdev, port_obj_info->obj);
2850 		break;
2851 	}
2852 
2853 	port_obj_info->handled = true;
2854 	return notifier_from_errno(err);
2855 }
2856 
2857 static int rocker_switchdev_blocking_event(struct notifier_block *unused,
2858 					   unsigned long event, void *ptr)
2859 {
2860 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2861 
2862 	if (!rocker_port_dev_check(dev))
2863 		return NOTIFY_DONE;
2864 
2865 	switch (event) {
2866 	case SWITCHDEV_PORT_OBJ_ADD:
2867 	case SWITCHDEV_PORT_OBJ_DEL:
2868 		return rocker_switchdev_port_obj_event(event, dev, ptr);
2869 	case SWITCHDEV_PORT_ATTR_SET:
2870 		return rocker_switchdev_port_attr_set_event(dev, ptr);
2871 	}
2872 
2873 	return NOTIFY_DONE;
2874 }
2875 
2876 static struct notifier_block rocker_switchdev_notifier = {
2877 	.notifier_call = rocker_switchdev_event,
2878 };
2879 
2880 static struct notifier_block rocker_switchdev_blocking_notifier = {
2881 	.notifier_call = rocker_switchdev_blocking_event,
2882 };
2883 
2884 static int rocker_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2885 {
2886 	struct notifier_block *nb;
2887 	struct rocker *rocker;
2888 	int err;
2889 
2890 	rocker = kzalloc(sizeof(*rocker), GFP_KERNEL);
2891 	if (!rocker)
2892 		return -ENOMEM;
2893 
2894 	err = pci_enable_device(pdev);
2895 	if (err) {
2896 		dev_err(&pdev->dev, "pci_enable_device failed\n");
2897 		goto err_pci_enable_device;
2898 	}
2899 
2900 	err = pci_request_regions(pdev, rocker_driver_name);
2901 	if (err) {
2902 		dev_err(&pdev->dev, "pci_request_regions failed\n");
2903 		goto err_pci_request_regions;
2904 	}
2905 
2906 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
2907 	if (!err) {
2908 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2909 		if (err) {
2910 			dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n");
2911 			goto err_pci_set_dma_mask;
2912 		}
2913 	} else {
2914 		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2915 		if (err) {
2916 			dev_err(&pdev->dev, "pci_set_dma_mask failed\n");
2917 			goto err_pci_set_dma_mask;
2918 		}
2919 	}
2920 
2921 	if (pci_resource_len(pdev, 0) < ROCKER_PCI_BAR0_SIZE) {
2922 		dev_err(&pdev->dev, "invalid PCI region size\n");
2923 		err = -EINVAL;
2924 		goto err_pci_resource_len_check;
2925 	}
2926 
2927 	rocker->hw_addr = ioremap(pci_resource_start(pdev, 0),
2928 				  pci_resource_len(pdev, 0));
2929 	if (!rocker->hw_addr) {
2930 		dev_err(&pdev->dev, "ioremap failed\n");
2931 		err = -EIO;
2932 		goto err_ioremap;
2933 	}
2934 	pci_set_master(pdev);
2935 
2936 	rocker->pdev = pdev;
2937 	pci_set_drvdata(pdev, rocker);
2938 
2939 	rocker->port_count = rocker_read32(rocker, PORT_PHYS_COUNT);
2940 
2941 	err = rocker_msix_init(rocker);
2942 	if (err) {
2943 		dev_err(&pdev->dev, "MSI-X init failed\n");
2944 		goto err_msix_init;
2945 	}
2946 
2947 	err = rocker_basic_hw_test(rocker);
2948 	if (err) {
2949 		dev_err(&pdev->dev, "basic hw test failed\n");
2950 		goto err_basic_hw_test;
2951 	}
2952 
2953 	rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
2954 
2955 	err = rocker_dma_rings_init(rocker);
2956 	if (err)
2957 		goto err_dma_rings_init;
2958 
2959 	err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD),
2960 			  rocker_cmd_irq_handler, 0,
2961 			  rocker_driver_name, rocker);
2962 	if (err) {
2963 		dev_err(&pdev->dev, "cannot assign cmd irq\n");
2964 		goto err_request_cmd_irq;
2965 	}
2966 
2967 	err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT),
2968 			  rocker_event_irq_handler, 0,
2969 			  rocker_driver_name, rocker);
2970 	if (err) {
2971 		dev_err(&pdev->dev, "cannot assign event irq\n");
2972 		goto err_request_event_irq;
2973 	}
2974 
2975 	rocker->rocker_owq = alloc_ordered_workqueue(rocker_driver_name,
2976 						     WQ_MEM_RECLAIM);
2977 	if (!rocker->rocker_owq) {
2978 		err = -ENOMEM;
2979 		goto err_alloc_ordered_workqueue;
2980 	}
2981 
2982 	err = rocker_probe_ports(rocker);
2983 	if (err) {
2984 		dev_err(&pdev->dev, "failed to probe ports\n");
2985 		goto err_probe_ports;
2986 	}
2987 
2988 	/* Only FIBs pointing to our own netdevs are programmed into
2989 	 * the device, so no need to pass a callback.
2990 	 */
2991 	rocker->fib_nb.notifier_call = rocker_router_fib_event;
2992 	err = register_fib_notifier(&rocker->fib_nb, NULL);
2993 	if (err)
2994 		goto err_register_fib_notifier;
2995 
2996 	err = register_switchdev_notifier(&rocker_switchdev_notifier);
2997 	if (err) {
2998 		dev_err(&pdev->dev, "Failed to register switchdev notifier\n");
2999 		goto err_register_switchdev_notifier;
3000 	}
3001 
3002 	nb = &rocker_switchdev_blocking_notifier;
3003 	err = register_switchdev_blocking_notifier(nb);
3004 	if (err) {
3005 		dev_err(&pdev->dev, "Failed to register switchdev blocking notifier\n");
3006 		goto err_register_switchdev_blocking_notifier;
3007 	}
3008 
3009 	rocker->hw.id = rocker_read64(rocker, SWITCH_ID);
3010 
3011 	dev_info(&pdev->dev, "Rocker switch with id %*phN\n",
3012 		 (int)sizeof(rocker->hw.id), &rocker->hw.id);
3013 
3014 	return 0;
3015 
3016 err_register_switchdev_blocking_notifier:
3017 	unregister_switchdev_notifier(&rocker_switchdev_notifier);
3018 err_register_switchdev_notifier:
3019 	unregister_fib_notifier(&rocker->fib_nb);
3020 err_register_fib_notifier:
3021 	rocker_remove_ports(rocker);
3022 err_probe_ports:
3023 	destroy_workqueue(rocker->rocker_owq);
3024 err_alloc_ordered_workqueue:
3025 	free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
3026 err_request_event_irq:
3027 	free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
3028 err_request_cmd_irq:
3029 	rocker_dma_rings_fini(rocker);
3030 err_dma_rings_init:
3031 err_basic_hw_test:
3032 	rocker_msix_fini(rocker);
3033 err_msix_init:
3034 	iounmap(rocker->hw_addr);
3035 err_ioremap:
3036 err_pci_resource_len_check:
3037 err_pci_set_dma_mask:
3038 	pci_release_regions(pdev);
3039 err_pci_request_regions:
3040 	pci_disable_device(pdev);
3041 err_pci_enable_device:
3042 	kfree(rocker);
3043 	return err;
3044 }
3045 
3046 static void rocker_remove(struct pci_dev *pdev)
3047 {
3048 	struct rocker *rocker = pci_get_drvdata(pdev);
3049 	struct notifier_block *nb;
3050 
3051 	nb = &rocker_switchdev_blocking_notifier;
3052 	unregister_switchdev_blocking_notifier(nb);
3053 
3054 	unregister_switchdev_notifier(&rocker_switchdev_notifier);
3055 	unregister_fib_notifier(&rocker->fib_nb);
3056 	rocker_remove_ports(rocker);
3057 	rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
3058 	destroy_workqueue(rocker->rocker_owq);
3059 	free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
3060 	free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
3061 	rocker_dma_rings_fini(rocker);
3062 	rocker_msix_fini(rocker);
3063 	iounmap(rocker->hw_addr);
3064 	pci_release_regions(rocker->pdev);
3065 	pci_disable_device(rocker->pdev);
3066 	kfree(rocker);
3067 }
3068 
3069 static struct pci_driver rocker_pci_driver = {
3070 	.name		= rocker_driver_name,
3071 	.id_table	= rocker_pci_id_table,
3072 	.probe		= rocker_probe,
3073 	.remove		= rocker_remove,
3074 };
3075 
3076 /************************************
3077  * Net device notifier event handler
3078  ************************************/
3079 
3080 static bool rocker_port_dev_check_under(const struct net_device *dev,
3081 					struct rocker *rocker)
3082 {
3083 	struct rocker_port *rocker_port;
3084 
3085 	if (!rocker_port_dev_check(dev))
3086 		return false;
3087 
3088 	rocker_port = netdev_priv(dev);
3089 	if (rocker_port->rocker != rocker)
3090 		return false;
3091 
3092 	return true;
3093 }
3094 
3095 struct rocker_walk_data {
3096 	struct rocker *rocker;
3097 	struct rocker_port *port;
3098 };
3099 
3100 static int rocker_lower_dev_walk(struct net_device *lower_dev, void *_data)
3101 {
3102 	struct rocker_walk_data *data = _data;
3103 	int ret = 0;
3104 
3105 	if (rocker_port_dev_check_under(lower_dev, data->rocker)) {
3106 		data->port = netdev_priv(lower_dev);
3107 		ret = 1;
3108 	}
3109 
3110 	return ret;
3111 }
3112 
3113 struct rocker_port *rocker_port_dev_lower_find(struct net_device *dev,
3114 					       struct rocker *rocker)
3115 {
3116 	struct rocker_walk_data data;
3117 
3118 	if (rocker_port_dev_check_under(dev, rocker))
3119 		return netdev_priv(dev);
3120 
3121 	data.rocker = rocker;
3122 	data.port = NULL;
3123 	netdev_walk_all_lower_dev(dev, rocker_lower_dev_walk, &data);
3124 
3125 	return data.port;
3126 }
3127 
3128 static int rocker_netdevice_event(struct notifier_block *unused,
3129 				  unsigned long event, void *ptr)
3130 {
3131 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3132 	struct netdev_notifier_changeupper_info *info;
3133 	struct rocker_port *rocker_port;
3134 	int err;
3135 
3136 	if (!rocker_port_dev_check(dev))
3137 		return NOTIFY_DONE;
3138 
3139 	switch (event) {
3140 	case NETDEV_CHANGEUPPER:
3141 		info = ptr;
3142 		if (!info->master)
3143 			goto out;
3144 		rocker_port = netdev_priv(dev);
3145 		if (info->linking) {
3146 			err = rocker_world_port_master_linked(rocker_port,
3147 							      info->upper_dev);
3148 			if (err)
3149 				netdev_warn(dev, "failed to reflect master linked (err %d)\n",
3150 					    err);
3151 		} else {
3152 			err = rocker_world_port_master_unlinked(rocker_port,
3153 								info->upper_dev);
3154 			if (err)
3155 				netdev_warn(dev, "failed to reflect master unlinked (err %d)\n",
3156 					    err);
3157 		}
3158 	}
3159 out:
3160 	return NOTIFY_DONE;
3161 }
3162 
3163 static struct notifier_block rocker_netdevice_nb __read_mostly = {
3164 	.notifier_call = rocker_netdevice_event,
3165 };
3166 
3167 /************************************
3168  * Net event notifier event handler
3169  ************************************/
3170 
3171 static int rocker_netevent_event(struct notifier_block *unused,
3172 				 unsigned long event, void *ptr)
3173 {
3174 	struct rocker_port *rocker_port;
3175 	struct net_device *dev;
3176 	struct neighbour *n = ptr;
3177 	int err;
3178 
3179 	switch (event) {
3180 	case NETEVENT_NEIGH_UPDATE:
3181 		if (n->tbl != &arp_tbl)
3182 			return NOTIFY_DONE;
3183 		dev = n->dev;
3184 		if (!rocker_port_dev_check(dev))
3185 			return NOTIFY_DONE;
3186 		rocker_port = netdev_priv(dev);
3187 		err = rocker_world_port_neigh_update(rocker_port, n);
3188 		if (err)
3189 			netdev_warn(dev, "failed to handle neigh update (err %d)\n",
3190 				    err);
3191 		break;
3192 	}
3193 
3194 	return NOTIFY_DONE;
3195 }
3196 
3197 static struct notifier_block rocker_netevent_nb __read_mostly = {
3198 	.notifier_call = rocker_netevent_event,
3199 };
3200 
3201 /***********************
3202  * Module init and exit
3203  ***********************/
3204 
3205 static int __init rocker_module_init(void)
3206 {
3207 	int err;
3208 
3209 	register_netdevice_notifier(&rocker_netdevice_nb);
3210 	register_netevent_notifier(&rocker_netevent_nb);
3211 	err = pci_register_driver(&rocker_pci_driver);
3212 	if (err)
3213 		goto err_pci_register_driver;
3214 	return 0;
3215 
3216 err_pci_register_driver:
3217 	unregister_netevent_notifier(&rocker_netevent_nb);
3218 	unregister_netdevice_notifier(&rocker_netdevice_nb);
3219 	return err;
3220 }
3221 
3222 static void __exit rocker_module_exit(void)
3223 {
3224 	unregister_netevent_notifier(&rocker_netevent_nb);
3225 	unregister_netdevice_notifier(&rocker_netdevice_nb);
3226 	pci_unregister_driver(&rocker_pci_driver);
3227 }
3228 
3229 module_init(rocker_module_init);
3230 module_exit(rocker_module_exit);
3231 
3232 MODULE_LICENSE("GPL v2");
3233 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
3234 MODULE_AUTHOR("Scott Feldman <sfeldma@gmail.com>");
3235 MODULE_DESCRIPTION("Rocker switch device driver");
3236 MODULE_DEVICE_TABLE(pci, rocker_pci_id_table);
3237