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 (!net_eq(info->net, &init_net))
2193 		return NOTIFY_DONE;
2194 
2195 	if (info->family != AF_INET)
2196 		return NOTIFY_DONE;
2197 
2198 	fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
2199 	if (WARN_ON(!fib_work))
2200 		return NOTIFY_BAD;
2201 
2202 	INIT_WORK(&fib_work->work, rocker_router_fib_event_work);
2203 	fib_work->rocker = rocker;
2204 	fib_work->event = event;
2205 
2206 	switch (event) {
2207 	case FIB_EVENT_ENTRY_ADD: /* fall through */
2208 	case FIB_EVENT_ENTRY_DEL:
2209 		if (info->family == AF_INET) {
2210 			struct fib_entry_notifier_info *fen_info = ptr;
2211 
2212 			if (fen_info->fi->fib_nh_is_v6) {
2213 				NL_SET_ERR_MSG_MOD(info->extack, "IPv6 gateway with IPv4 route is not supported");
2214 				kfree(fib_work);
2215 				return notifier_from_errno(-EINVAL);
2216 			}
2217 			if (fen_info->fi->nh) {
2218 				NL_SET_ERR_MSG_MOD(info->extack, "IPv4 route with nexthop objects is not supported");
2219 				kfree(fib_work);
2220 				return notifier_from_errno(-EINVAL);
2221 			}
2222 		}
2223 
2224 		memcpy(&fib_work->fen_info, ptr, sizeof(fib_work->fen_info));
2225 		/* Take referece on fib_info to prevent it from being
2226 		 * freed while work is queued. Release it afterwards.
2227 		 */
2228 		fib_info_hold(fib_work->fen_info.fi);
2229 		break;
2230 	case FIB_EVENT_RULE_ADD: /* fall through */
2231 	case FIB_EVENT_RULE_DEL:
2232 		memcpy(&fib_work->fr_info, ptr, sizeof(fib_work->fr_info));
2233 		fib_rule_get(fib_work->fr_info.rule);
2234 		break;
2235 	}
2236 
2237 	queue_work(rocker->rocker_owq, &fib_work->work);
2238 
2239 	return NOTIFY_DONE;
2240 }
2241 
2242 /********************
2243  * ethtool interface
2244  ********************/
2245 
2246 static int
2247 rocker_port_get_link_ksettings(struct net_device *dev,
2248 			       struct ethtool_link_ksettings *ecmd)
2249 {
2250 	struct rocker_port *rocker_port = netdev_priv(dev);
2251 
2252 	return rocker_cmd_get_port_settings_ethtool(rocker_port, ecmd);
2253 }
2254 
2255 static int
2256 rocker_port_set_link_ksettings(struct net_device *dev,
2257 			       const struct ethtool_link_ksettings *ecmd)
2258 {
2259 	struct rocker_port *rocker_port = netdev_priv(dev);
2260 
2261 	return rocker_cmd_set_port_settings_ethtool(rocker_port, ecmd);
2262 }
2263 
2264 static void rocker_port_get_drvinfo(struct net_device *dev,
2265 				    struct ethtool_drvinfo *drvinfo)
2266 {
2267 	strlcpy(drvinfo->driver, rocker_driver_name, sizeof(drvinfo->driver));
2268 	strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
2269 }
2270 
2271 static struct rocker_port_stats {
2272 	char str[ETH_GSTRING_LEN];
2273 	int type;
2274 } rocker_port_stats[] = {
2275 	{ "rx_packets", ROCKER_TLV_CMD_PORT_STATS_RX_PKTS,    },
2276 	{ "rx_bytes",   ROCKER_TLV_CMD_PORT_STATS_RX_BYTES,   },
2277 	{ "rx_dropped", ROCKER_TLV_CMD_PORT_STATS_RX_DROPPED, },
2278 	{ "rx_errors",  ROCKER_TLV_CMD_PORT_STATS_RX_ERRORS,  },
2279 
2280 	{ "tx_packets", ROCKER_TLV_CMD_PORT_STATS_TX_PKTS,    },
2281 	{ "tx_bytes",   ROCKER_TLV_CMD_PORT_STATS_TX_BYTES,   },
2282 	{ "tx_dropped", ROCKER_TLV_CMD_PORT_STATS_TX_DROPPED, },
2283 	{ "tx_errors",  ROCKER_TLV_CMD_PORT_STATS_TX_ERRORS,  },
2284 };
2285 
2286 #define ROCKER_PORT_STATS_LEN  ARRAY_SIZE(rocker_port_stats)
2287 
2288 static void rocker_port_get_strings(struct net_device *netdev, u32 stringset,
2289 				    u8 *data)
2290 {
2291 	u8 *p = data;
2292 	int i;
2293 
2294 	switch (stringset) {
2295 	case ETH_SS_STATS:
2296 		for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
2297 			memcpy(p, rocker_port_stats[i].str, ETH_GSTRING_LEN);
2298 			p += ETH_GSTRING_LEN;
2299 		}
2300 		break;
2301 	}
2302 }
2303 
2304 static int
2305 rocker_cmd_get_port_stats_prep(const struct rocker_port *rocker_port,
2306 			       struct rocker_desc_info *desc_info,
2307 			       void *priv)
2308 {
2309 	struct rocker_tlv *cmd_stats;
2310 
2311 	if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
2312 			       ROCKER_TLV_CMD_TYPE_GET_PORT_STATS))
2313 		return -EMSGSIZE;
2314 
2315 	cmd_stats = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2316 	if (!cmd_stats)
2317 		return -EMSGSIZE;
2318 
2319 	if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_STATS_PPORT,
2320 			       rocker_port->pport))
2321 		return -EMSGSIZE;
2322 
2323 	rocker_tlv_nest_end(desc_info, cmd_stats);
2324 
2325 	return 0;
2326 }
2327 
2328 static int
2329 rocker_cmd_get_port_stats_ethtool_proc(const struct rocker_port *rocker_port,
2330 				       const struct rocker_desc_info *desc_info,
2331 				       void *priv)
2332 {
2333 	const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
2334 	const struct rocker_tlv *stats_attrs[ROCKER_TLV_CMD_PORT_STATS_MAX + 1];
2335 	const struct rocker_tlv *pattr;
2336 	u32 pport;
2337 	u64 *data = priv;
2338 	int i;
2339 
2340 	rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
2341 
2342 	if (!attrs[ROCKER_TLV_CMD_INFO])
2343 		return -EIO;
2344 
2345 	rocker_tlv_parse_nested(stats_attrs, ROCKER_TLV_CMD_PORT_STATS_MAX,
2346 				attrs[ROCKER_TLV_CMD_INFO]);
2347 
2348 	if (!stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT])
2349 		return -EIO;
2350 
2351 	pport = rocker_tlv_get_u32(stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]);
2352 	if (pport != rocker_port->pport)
2353 		return -EIO;
2354 
2355 	for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
2356 		pattr = stats_attrs[rocker_port_stats[i].type];
2357 		if (!pattr)
2358 			continue;
2359 
2360 		data[i] = rocker_tlv_get_u64(pattr);
2361 	}
2362 
2363 	return 0;
2364 }
2365 
2366 static int rocker_cmd_get_port_stats_ethtool(struct rocker_port *rocker_port,
2367 					     void *priv)
2368 {
2369 	return rocker_cmd_exec(rocker_port, false,
2370 			       rocker_cmd_get_port_stats_prep, NULL,
2371 			       rocker_cmd_get_port_stats_ethtool_proc,
2372 			       priv);
2373 }
2374 
2375 static void rocker_port_get_stats(struct net_device *dev,
2376 				  struct ethtool_stats *stats, u64 *data)
2377 {
2378 	struct rocker_port *rocker_port = netdev_priv(dev);
2379 
2380 	if (rocker_cmd_get_port_stats_ethtool(rocker_port, data) != 0) {
2381 		int i;
2382 
2383 		for (i = 0; i < ARRAY_SIZE(rocker_port_stats); ++i)
2384 			data[i] = 0;
2385 	}
2386 }
2387 
2388 static int rocker_port_get_sset_count(struct net_device *netdev, int sset)
2389 {
2390 	switch (sset) {
2391 	case ETH_SS_STATS:
2392 		return ROCKER_PORT_STATS_LEN;
2393 	default:
2394 		return -EOPNOTSUPP;
2395 	}
2396 }
2397 
2398 static const struct ethtool_ops rocker_port_ethtool_ops = {
2399 	.get_drvinfo		= rocker_port_get_drvinfo,
2400 	.get_link		= ethtool_op_get_link,
2401 	.get_strings		= rocker_port_get_strings,
2402 	.get_ethtool_stats	= rocker_port_get_stats,
2403 	.get_sset_count		= rocker_port_get_sset_count,
2404 	.get_link_ksettings	= rocker_port_get_link_ksettings,
2405 	.set_link_ksettings	= rocker_port_set_link_ksettings,
2406 };
2407 
2408 /*****************
2409  * NAPI interface
2410  *****************/
2411 
2412 static struct rocker_port *rocker_port_napi_tx_get(struct napi_struct *napi)
2413 {
2414 	return container_of(napi, struct rocker_port, napi_tx);
2415 }
2416 
2417 static int rocker_port_poll_tx(struct napi_struct *napi, int budget)
2418 {
2419 	struct rocker_port *rocker_port = rocker_port_napi_tx_get(napi);
2420 	const struct rocker *rocker = rocker_port->rocker;
2421 	const struct rocker_desc_info *desc_info;
2422 	u32 credits = 0;
2423 	int err;
2424 
2425 	/* Cleanup tx descriptors */
2426 	while ((desc_info = rocker_desc_tail_get(&rocker_port->tx_ring))) {
2427 		struct sk_buff *skb;
2428 
2429 		err = rocker_desc_err(desc_info);
2430 		if (err && net_ratelimit())
2431 			netdev_err(rocker_port->dev, "tx desc received with err %d\n",
2432 				   err);
2433 		rocker_tx_desc_frags_unmap(rocker_port, desc_info);
2434 
2435 		skb = rocker_desc_cookie_ptr_get(desc_info);
2436 		if (err == 0) {
2437 			rocker_port->dev->stats.tx_packets++;
2438 			rocker_port->dev->stats.tx_bytes += skb->len;
2439 		} else {
2440 			rocker_port->dev->stats.tx_errors++;
2441 		}
2442 
2443 		dev_kfree_skb_any(skb);
2444 		credits++;
2445 	}
2446 
2447 	if (credits && netif_queue_stopped(rocker_port->dev))
2448 		netif_wake_queue(rocker_port->dev);
2449 
2450 	napi_complete(napi);
2451 	rocker_dma_ring_credits_set(rocker, &rocker_port->tx_ring, credits);
2452 
2453 	return 0;
2454 }
2455 
2456 static int rocker_port_rx_proc(const struct rocker *rocker,
2457 			       const struct rocker_port *rocker_port,
2458 			       struct rocker_desc_info *desc_info)
2459 {
2460 	const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
2461 	struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
2462 	size_t rx_len;
2463 	u16 rx_flags = 0;
2464 
2465 	if (!skb)
2466 		return -ENOENT;
2467 
2468 	rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
2469 	if (!attrs[ROCKER_TLV_RX_FRAG_LEN])
2470 		return -EINVAL;
2471 	if (attrs[ROCKER_TLV_RX_FLAGS])
2472 		rx_flags = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FLAGS]);
2473 
2474 	rocker_dma_rx_ring_skb_unmap(rocker, attrs);
2475 
2476 	rx_len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_LEN]);
2477 	skb_put(skb, rx_len);
2478 	skb->protocol = eth_type_trans(skb, rocker_port->dev);
2479 
2480 	if (rx_flags & ROCKER_RX_FLAGS_FWD_OFFLOAD)
2481 		skb->offload_fwd_mark = 1;
2482 
2483 	rocker_port->dev->stats.rx_packets++;
2484 	rocker_port->dev->stats.rx_bytes += skb->len;
2485 
2486 	netif_receive_skb(skb);
2487 
2488 	return rocker_dma_rx_ring_skb_alloc(rocker_port, desc_info);
2489 }
2490 
2491 static struct rocker_port *rocker_port_napi_rx_get(struct napi_struct *napi)
2492 {
2493 	return container_of(napi, struct rocker_port, napi_rx);
2494 }
2495 
2496 static int rocker_port_poll_rx(struct napi_struct *napi, int budget)
2497 {
2498 	struct rocker_port *rocker_port = rocker_port_napi_rx_get(napi);
2499 	const struct rocker *rocker = rocker_port->rocker;
2500 	struct rocker_desc_info *desc_info;
2501 	u32 credits = 0;
2502 	int err;
2503 
2504 	/* Process rx descriptors */
2505 	while (credits < budget &&
2506 	       (desc_info = rocker_desc_tail_get(&rocker_port->rx_ring))) {
2507 		err = rocker_desc_err(desc_info);
2508 		if (err) {
2509 			if (net_ratelimit())
2510 				netdev_err(rocker_port->dev, "rx desc received with err %d\n",
2511 					   err);
2512 		} else {
2513 			err = rocker_port_rx_proc(rocker, rocker_port,
2514 						  desc_info);
2515 			if (err && net_ratelimit())
2516 				netdev_err(rocker_port->dev, "rx processing failed with err %d\n",
2517 					   err);
2518 		}
2519 		if (err)
2520 			rocker_port->dev->stats.rx_errors++;
2521 
2522 		rocker_desc_gen_clear(desc_info);
2523 		rocker_desc_head_set(rocker, &rocker_port->rx_ring, desc_info);
2524 		credits++;
2525 	}
2526 
2527 	if (credits < budget)
2528 		napi_complete_done(napi, credits);
2529 
2530 	rocker_dma_ring_credits_set(rocker, &rocker_port->rx_ring, credits);
2531 
2532 	return credits;
2533 }
2534 
2535 /*****************
2536  * PCI driver ops
2537  *****************/
2538 
2539 static void rocker_carrier_init(const struct rocker_port *rocker_port)
2540 {
2541 	const struct rocker *rocker = rocker_port->rocker;
2542 	u64 link_status = rocker_read64(rocker, PORT_PHYS_LINK_STATUS);
2543 	bool link_up;
2544 
2545 	link_up = link_status & (1 << rocker_port->pport);
2546 	if (link_up)
2547 		netif_carrier_on(rocker_port->dev);
2548 	else
2549 		netif_carrier_off(rocker_port->dev);
2550 }
2551 
2552 static void rocker_remove_ports(struct rocker *rocker)
2553 {
2554 	struct rocker_port *rocker_port;
2555 	int i;
2556 
2557 	for (i = 0; i < rocker->port_count; i++) {
2558 		rocker_port = rocker->ports[i];
2559 		if (!rocker_port)
2560 			continue;
2561 		rocker_world_port_fini(rocker_port);
2562 		unregister_netdev(rocker_port->dev);
2563 		rocker_world_port_post_fini(rocker_port);
2564 		free_netdev(rocker_port->dev);
2565 	}
2566 	rocker_world_fini(rocker);
2567 	kfree(rocker->ports);
2568 }
2569 
2570 static void rocker_port_dev_addr_init(struct rocker_port *rocker_port)
2571 {
2572 	const struct rocker *rocker = rocker_port->rocker;
2573 	const struct pci_dev *pdev = rocker->pdev;
2574 	int err;
2575 
2576 	err = rocker_cmd_get_port_settings_macaddr(rocker_port,
2577 						   rocker_port->dev->dev_addr);
2578 	if (err) {
2579 		dev_warn(&pdev->dev, "failed to get mac address, using random\n");
2580 		eth_hw_addr_random(rocker_port->dev);
2581 	}
2582 }
2583 
2584 #define ROCKER_PORT_MIN_MTU	ETH_MIN_MTU
2585 #define ROCKER_PORT_MAX_MTU	9000
2586 static int rocker_probe_port(struct rocker *rocker, unsigned int port_number)
2587 {
2588 	struct pci_dev *pdev = rocker->pdev;
2589 	struct rocker_port *rocker_port;
2590 	struct net_device *dev;
2591 	int err;
2592 
2593 	dev = alloc_etherdev(sizeof(struct rocker_port));
2594 	if (!dev)
2595 		return -ENOMEM;
2596 	SET_NETDEV_DEV(dev, &pdev->dev);
2597 	rocker_port = netdev_priv(dev);
2598 	rocker_port->dev = dev;
2599 	rocker_port->rocker = rocker;
2600 	rocker_port->port_number = port_number;
2601 	rocker_port->pport = port_number + 1;
2602 
2603 	err = rocker_world_check_init(rocker_port);
2604 	if (err) {
2605 		dev_err(&pdev->dev, "world init failed\n");
2606 		goto err_world_check_init;
2607 	}
2608 
2609 	rocker_port_dev_addr_init(rocker_port);
2610 	dev->netdev_ops = &rocker_port_netdev_ops;
2611 	dev->ethtool_ops = &rocker_port_ethtool_ops;
2612 	netif_tx_napi_add(dev, &rocker_port->napi_tx, rocker_port_poll_tx,
2613 			  NAPI_POLL_WEIGHT);
2614 	netif_napi_add(dev, &rocker_port->napi_rx, rocker_port_poll_rx,
2615 		       NAPI_POLL_WEIGHT);
2616 	rocker_carrier_init(rocker_port);
2617 
2618 	dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_SG;
2619 
2620 	/* MTU range: 68 - 9000 */
2621 	dev->min_mtu = ROCKER_PORT_MIN_MTU;
2622 	dev->max_mtu = ROCKER_PORT_MAX_MTU;
2623 
2624 	err = rocker_world_port_pre_init(rocker_port);
2625 	if (err) {
2626 		dev_err(&pdev->dev, "port world pre-init failed\n");
2627 		goto err_world_port_pre_init;
2628 	}
2629 	err = register_netdev(dev);
2630 	if (err) {
2631 		dev_err(&pdev->dev, "register_netdev failed\n");
2632 		goto err_register_netdev;
2633 	}
2634 	rocker->ports[port_number] = rocker_port;
2635 
2636 	err = rocker_world_port_init(rocker_port);
2637 	if (err) {
2638 		dev_err(&pdev->dev, "port world init failed\n");
2639 		goto err_world_port_init;
2640 	}
2641 
2642 	return 0;
2643 
2644 err_world_port_init:
2645 	rocker->ports[port_number] = NULL;
2646 	unregister_netdev(dev);
2647 err_register_netdev:
2648 	rocker_world_port_post_fini(rocker_port);
2649 err_world_port_pre_init:
2650 err_world_check_init:
2651 	free_netdev(dev);
2652 	return err;
2653 }
2654 
2655 static int rocker_probe_ports(struct rocker *rocker)
2656 {
2657 	int i;
2658 	size_t alloc_size;
2659 	int err;
2660 
2661 	alloc_size = sizeof(struct rocker_port *) * rocker->port_count;
2662 	rocker->ports = kzalloc(alloc_size, GFP_KERNEL);
2663 	if (!rocker->ports)
2664 		return -ENOMEM;
2665 	for (i = 0; i < rocker->port_count; i++) {
2666 		err = rocker_probe_port(rocker, i);
2667 		if (err)
2668 			goto remove_ports;
2669 	}
2670 	return 0;
2671 
2672 remove_ports:
2673 	rocker_remove_ports(rocker);
2674 	return err;
2675 }
2676 
2677 static int rocker_msix_init(struct rocker *rocker)
2678 {
2679 	struct pci_dev *pdev = rocker->pdev;
2680 	int msix_entries;
2681 	int i;
2682 	int err;
2683 
2684 	msix_entries = pci_msix_vec_count(pdev);
2685 	if (msix_entries < 0)
2686 		return msix_entries;
2687 
2688 	if (msix_entries != ROCKER_MSIX_VEC_COUNT(rocker->port_count))
2689 		return -EINVAL;
2690 
2691 	rocker->msix_entries = kmalloc_array(msix_entries,
2692 					     sizeof(struct msix_entry),
2693 					     GFP_KERNEL);
2694 	if (!rocker->msix_entries)
2695 		return -ENOMEM;
2696 
2697 	for (i = 0; i < msix_entries; i++)
2698 		rocker->msix_entries[i].entry = i;
2699 
2700 	err = pci_enable_msix_exact(pdev, rocker->msix_entries, msix_entries);
2701 	if (err < 0)
2702 		goto err_enable_msix;
2703 
2704 	return 0;
2705 
2706 err_enable_msix:
2707 	kfree(rocker->msix_entries);
2708 	return err;
2709 }
2710 
2711 static void rocker_msix_fini(const struct rocker *rocker)
2712 {
2713 	pci_disable_msix(rocker->pdev);
2714 	kfree(rocker->msix_entries);
2715 }
2716 
2717 static bool rocker_port_dev_check(const struct net_device *dev)
2718 {
2719 	return dev->netdev_ops == &rocker_port_netdev_ops;
2720 }
2721 
2722 static int
2723 rocker_switchdev_port_attr_set_event(struct net_device *netdev,
2724 		struct switchdev_notifier_port_attr_info *port_attr_info)
2725 {
2726 	int err;
2727 
2728 	err = rocker_port_attr_set(netdev, port_attr_info->attr,
2729 				   port_attr_info->trans);
2730 
2731 	port_attr_info->handled = true;
2732 	return notifier_from_errno(err);
2733 }
2734 
2735 struct rocker_switchdev_event_work {
2736 	struct work_struct work;
2737 	struct switchdev_notifier_fdb_info fdb_info;
2738 	struct rocker_port *rocker_port;
2739 	unsigned long event;
2740 };
2741 
2742 static void
2743 rocker_fdb_offload_notify(struct rocker_port *rocker_port,
2744 			  struct switchdev_notifier_fdb_info *recv_info)
2745 {
2746 	struct switchdev_notifier_fdb_info info;
2747 
2748 	info.addr = recv_info->addr;
2749 	info.vid = recv_info->vid;
2750 	info.offloaded = true;
2751 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2752 				 rocker_port->dev, &info.info, NULL);
2753 }
2754 
2755 static void rocker_switchdev_event_work(struct work_struct *work)
2756 {
2757 	struct rocker_switchdev_event_work *switchdev_work =
2758 		container_of(work, struct rocker_switchdev_event_work, work);
2759 	struct rocker_port *rocker_port = switchdev_work->rocker_port;
2760 	struct switchdev_notifier_fdb_info *fdb_info;
2761 	int err;
2762 
2763 	rtnl_lock();
2764 	switch (switchdev_work->event) {
2765 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2766 		fdb_info = &switchdev_work->fdb_info;
2767 		if (!fdb_info->added_by_user)
2768 			break;
2769 		err = rocker_world_port_fdb_add(rocker_port, fdb_info);
2770 		if (err) {
2771 			netdev_dbg(rocker_port->dev, "fdb add failed err=%d\n", err);
2772 			break;
2773 		}
2774 		rocker_fdb_offload_notify(rocker_port, fdb_info);
2775 		break;
2776 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2777 		fdb_info = &switchdev_work->fdb_info;
2778 		if (!fdb_info->added_by_user)
2779 			break;
2780 		err = rocker_world_port_fdb_del(rocker_port, fdb_info);
2781 		if (err)
2782 			netdev_dbg(rocker_port->dev, "fdb add failed err=%d\n", err);
2783 		break;
2784 	}
2785 	rtnl_unlock();
2786 
2787 	kfree(switchdev_work->fdb_info.addr);
2788 	kfree(switchdev_work);
2789 	dev_put(rocker_port->dev);
2790 }
2791 
2792 /* called under rcu_read_lock() */
2793 static int rocker_switchdev_event(struct notifier_block *unused,
2794 				  unsigned long event, void *ptr)
2795 {
2796 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2797 	struct rocker_switchdev_event_work *switchdev_work;
2798 	struct switchdev_notifier_fdb_info *fdb_info = ptr;
2799 	struct rocker_port *rocker_port;
2800 
2801 	if (!rocker_port_dev_check(dev))
2802 		return NOTIFY_DONE;
2803 
2804 	if (event == SWITCHDEV_PORT_ATTR_SET)
2805 		return rocker_switchdev_port_attr_set_event(dev, ptr);
2806 
2807 	rocker_port = netdev_priv(dev);
2808 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2809 	if (WARN_ON(!switchdev_work))
2810 		return NOTIFY_BAD;
2811 
2812 	INIT_WORK(&switchdev_work->work, rocker_switchdev_event_work);
2813 	switchdev_work->rocker_port = rocker_port;
2814 	switchdev_work->event = event;
2815 
2816 	switch (event) {
2817 	case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
2818 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2819 		memcpy(&switchdev_work->fdb_info, ptr,
2820 		       sizeof(switchdev_work->fdb_info));
2821 		switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2822 		if (unlikely(!switchdev_work->fdb_info.addr)) {
2823 			kfree(switchdev_work);
2824 			return NOTIFY_BAD;
2825 		}
2826 
2827 		ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2828 				fdb_info->addr);
2829 		/* Take a reference on the rocker device */
2830 		dev_hold(dev);
2831 		break;
2832 	default:
2833 		kfree(switchdev_work);
2834 		return NOTIFY_DONE;
2835 	}
2836 
2837 	queue_work(rocker_port->rocker->rocker_owq,
2838 		   &switchdev_work->work);
2839 	return NOTIFY_DONE;
2840 }
2841 
2842 static int
2843 rocker_switchdev_port_obj_event(unsigned long event, struct net_device *netdev,
2844 			struct switchdev_notifier_port_obj_info *port_obj_info)
2845 {
2846 	int err = -EOPNOTSUPP;
2847 
2848 	switch (event) {
2849 	case SWITCHDEV_PORT_OBJ_ADD:
2850 		err = rocker_port_obj_add(netdev, port_obj_info->obj,
2851 					  port_obj_info->trans);
2852 		break;
2853 	case SWITCHDEV_PORT_OBJ_DEL:
2854 		err = rocker_port_obj_del(netdev, port_obj_info->obj);
2855 		break;
2856 	}
2857 
2858 	port_obj_info->handled = true;
2859 	return notifier_from_errno(err);
2860 }
2861 
2862 static int rocker_switchdev_blocking_event(struct notifier_block *unused,
2863 					   unsigned long event, void *ptr)
2864 {
2865 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2866 
2867 	if (!rocker_port_dev_check(dev))
2868 		return NOTIFY_DONE;
2869 
2870 	switch (event) {
2871 	case SWITCHDEV_PORT_OBJ_ADD:
2872 	case SWITCHDEV_PORT_OBJ_DEL:
2873 		return rocker_switchdev_port_obj_event(event, dev, ptr);
2874 	case SWITCHDEV_PORT_ATTR_SET:
2875 		return rocker_switchdev_port_attr_set_event(dev, ptr);
2876 	}
2877 
2878 	return NOTIFY_DONE;
2879 }
2880 
2881 static struct notifier_block rocker_switchdev_notifier = {
2882 	.notifier_call = rocker_switchdev_event,
2883 };
2884 
2885 static struct notifier_block rocker_switchdev_blocking_notifier = {
2886 	.notifier_call = rocker_switchdev_blocking_event,
2887 };
2888 
2889 static int rocker_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2890 {
2891 	struct notifier_block *nb;
2892 	struct rocker *rocker;
2893 	int err;
2894 
2895 	rocker = kzalloc(sizeof(*rocker), GFP_KERNEL);
2896 	if (!rocker)
2897 		return -ENOMEM;
2898 
2899 	err = pci_enable_device(pdev);
2900 	if (err) {
2901 		dev_err(&pdev->dev, "pci_enable_device failed\n");
2902 		goto err_pci_enable_device;
2903 	}
2904 
2905 	err = pci_request_regions(pdev, rocker_driver_name);
2906 	if (err) {
2907 		dev_err(&pdev->dev, "pci_request_regions failed\n");
2908 		goto err_pci_request_regions;
2909 	}
2910 
2911 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
2912 	if (!err) {
2913 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2914 		if (err) {
2915 			dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n");
2916 			goto err_pci_set_dma_mask;
2917 		}
2918 	} else {
2919 		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2920 		if (err) {
2921 			dev_err(&pdev->dev, "pci_set_dma_mask failed\n");
2922 			goto err_pci_set_dma_mask;
2923 		}
2924 	}
2925 
2926 	if (pci_resource_len(pdev, 0) < ROCKER_PCI_BAR0_SIZE) {
2927 		dev_err(&pdev->dev, "invalid PCI region size\n");
2928 		err = -EINVAL;
2929 		goto err_pci_resource_len_check;
2930 	}
2931 
2932 	rocker->hw_addr = ioremap(pci_resource_start(pdev, 0),
2933 				  pci_resource_len(pdev, 0));
2934 	if (!rocker->hw_addr) {
2935 		dev_err(&pdev->dev, "ioremap failed\n");
2936 		err = -EIO;
2937 		goto err_ioremap;
2938 	}
2939 	pci_set_master(pdev);
2940 
2941 	rocker->pdev = pdev;
2942 	pci_set_drvdata(pdev, rocker);
2943 
2944 	rocker->port_count = rocker_read32(rocker, PORT_PHYS_COUNT);
2945 
2946 	err = rocker_msix_init(rocker);
2947 	if (err) {
2948 		dev_err(&pdev->dev, "MSI-X init failed\n");
2949 		goto err_msix_init;
2950 	}
2951 
2952 	err = rocker_basic_hw_test(rocker);
2953 	if (err) {
2954 		dev_err(&pdev->dev, "basic hw test failed\n");
2955 		goto err_basic_hw_test;
2956 	}
2957 
2958 	rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
2959 
2960 	err = rocker_dma_rings_init(rocker);
2961 	if (err)
2962 		goto err_dma_rings_init;
2963 
2964 	err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD),
2965 			  rocker_cmd_irq_handler, 0,
2966 			  rocker_driver_name, rocker);
2967 	if (err) {
2968 		dev_err(&pdev->dev, "cannot assign cmd irq\n");
2969 		goto err_request_cmd_irq;
2970 	}
2971 
2972 	err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT),
2973 			  rocker_event_irq_handler, 0,
2974 			  rocker_driver_name, rocker);
2975 	if (err) {
2976 		dev_err(&pdev->dev, "cannot assign event irq\n");
2977 		goto err_request_event_irq;
2978 	}
2979 
2980 	rocker->rocker_owq = alloc_ordered_workqueue(rocker_driver_name,
2981 						     WQ_MEM_RECLAIM);
2982 	if (!rocker->rocker_owq) {
2983 		err = -ENOMEM;
2984 		goto err_alloc_ordered_workqueue;
2985 	}
2986 
2987 	err = rocker_probe_ports(rocker);
2988 	if (err) {
2989 		dev_err(&pdev->dev, "failed to probe ports\n");
2990 		goto err_probe_ports;
2991 	}
2992 
2993 	/* Only FIBs pointing to our own netdevs are programmed into
2994 	 * the device, so no need to pass a callback.
2995 	 */
2996 	rocker->fib_nb.notifier_call = rocker_router_fib_event;
2997 	err = register_fib_notifier(&rocker->fib_nb, NULL);
2998 	if (err)
2999 		goto err_register_fib_notifier;
3000 
3001 	err = register_switchdev_notifier(&rocker_switchdev_notifier);
3002 	if (err) {
3003 		dev_err(&pdev->dev, "Failed to register switchdev notifier\n");
3004 		goto err_register_switchdev_notifier;
3005 	}
3006 
3007 	nb = &rocker_switchdev_blocking_notifier;
3008 	err = register_switchdev_blocking_notifier(nb);
3009 	if (err) {
3010 		dev_err(&pdev->dev, "Failed to register switchdev blocking notifier\n");
3011 		goto err_register_switchdev_blocking_notifier;
3012 	}
3013 
3014 	rocker->hw.id = rocker_read64(rocker, SWITCH_ID);
3015 
3016 	dev_info(&pdev->dev, "Rocker switch with id %*phN\n",
3017 		 (int)sizeof(rocker->hw.id), &rocker->hw.id);
3018 
3019 	return 0;
3020 
3021 err_register_switchdev_blocking_notifier:
3022 	unregister_switchdev_notifier(&rocker_switchdev_notifier);
3023 err_register_switchdev_notifier:
3024 	unregister_fib_notifier(&rocker->fib_nb);
3025 err_register_fib_notifier:
3026 	rocker_remove_ports(rocker);
3027 err_probe_ports:
3028 	destroy_workqueue(rocker->rocker_owq);
3029 err_alloc_ordered_workqueue:
3030 	free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
3031 err_request_event_irq:
3032 	free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
3033 err_request_cmd_irq:
3034 	rocker_dma_rings_fini(rocker);
3035 err_dma_rings_init:
3036 err_basic_hw_test:
3037 	rocker_msix_fini(rocker);
3038 err_msix_init:
3039 	iounmap(rocker->hw_addr);
3040 err_ioremap:
3041 err_pci_resource_len_check:
3042 err_pci_set_dma_mask:
3043 	pci_release_regions(pdev);
3044 err_pci_request_regions:
3045 	pci_disable_device(pdev);
3046 err_pci_enable_device:
3047 	kfree(rocker);
3048 	return err;
3049 }
3050 
3051 static void rocker_remove(struct pci_dev *pdev)
3052 {
3053 	struct rocker *rocker = pci_get_drvdata(pdev);
3054 	struct notifier_block *nb;
3055 
3056 	nb = &rocker_switchdev_blocking_notifier;
3057 	unregister_switchdev_blocking_notifier(nb);
3058 
3059 	unregister_switchdev_notifier(&rocker_switchdev_notifier);
3060 	unregister_fib_notifier(&rocker->fib_nb);
3061 	rocker_remove_ports(rocker);
3062 	rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
3063 	destroy_workqueue(rocker->rocker_owq);
3064 	free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
3065 	free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
3066 	rocker_dma_rings_fini(rocker);
3067 	rocker_msix_fini(rocker);
3068 	iounmap(rocker->hw_addr);
3069 	pci_release_regions(rocker->pdev);
3070 	pci_disable_device(rocker->pdev);
3071 	kfree(rocker);
3072 }
3073 
3074 static struct pci_driver rocker_pci_driver = {
3075 	.name		= rocker_driver_name,
3076 	.id_table	= rocker_pci_id_table,
3077 	.probe		= rocker_probe,
3078 	.remove		= rocker_remove,
3079 };
3080 
3081 /************************************
3082  * Net device notifier event handler
3083  ************************************/
3084 
3085 static bool rocker_port_dev_check_under(const struct net_device *dev,
3086 					struct rocker *rocker)
3087 {
3088 	struct rocker_port *rocker_port;
3089 
3090 	if (!rocker_port_dev_check(dev))
3091 		return false;
3092 
3093 	rocker_port = netdev_priv(dev);
3094 	if (rocker_port->rocker != rocker)
3095 		return false;
3096 
3097 	return true;
3098 }
3099 
3100 struct rocker_walk_data {
3101 	struct rocker *rocker;
3102 	struct rocker_port *port;
3103 };
3104 
3105 static int rocker_lower_dev_walk(struct net_device *lower_dev, void *_data)
3106 {
3107 	struct rocker_walk_data *data = _data;
3108 	int ret = 0;
3109 
3110 	if (rocker_port_dev_check_under(lower_dev, data->rocker)) {
3111 		data->port = netdev_priv(lower_dev);
3112 		ret = 1;
3113 	}
3114 
3115 	return ret;
3116 }
3117 
3118 struct rocker_port *rocker_port_dev_lower_find(struct net_device *dev,
3119 					       struct rocker *rocker)
3120 {
3121 	struct rocker_walk_data data;
3122 
3123 	if (rocker_port_dev_check_under(dev, rocker))
3124 		return netdev_priv(dev);
3125 
3126 	data.rocker = rocker;
3127 	data.port = NULL;
3128 	netdev_walk_all_lower_dev(dev, rocker_lower_dev_walk, &data);
3129 
3130 	return data.port;
3131 }
3132 
3133 static int rocker_netdevice_event(struct notifier_block *unused,
3134 				  unsigned long event, void *ptr)
3135 {
3136 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3137 	struct netdev_notifier_changeupper_info *info;
3138 	struct rocker_port *rocker_port;
3139 	int err;
3140 
3141 	if (!rocker_port_dev_check(dev))
3142 		return NOTIFY_DONE;
3143 
3144 	switch (event) {
3145 	case NETDEV_CHANGEUPPER:
3146 		info = ptr;
3147 		if (!info->master)
3148 			goto out;
3149 		rocker_port = netdev_priv(dev);
3150 		if (info->linking) {
3151 			err = rocker_world_port_master_linked(rocker_port,
3152 							      info->upper_dev);
3153 			if (err)
3154 				netdev_warn(dev, "failed to reflect master linked (err %d)\n",
3155 					    err);
3156 		} else {
3157 			err = rocker_world_port_master_unlinked(rocker_port,
3158 								info->upper_dev);
3159 			if (err)
3160 				netdev_warn(dev, "failed to reflect master unlinked (err %d)\n",
3161 					    err);
3162 		}
3163 	}
3164 out:
3165 	return NOTIFY_DONE;
3166 }
3167 
3168 static struct notifier_block rocker_netdevice_nb __read_mostly = {
3169 	.notifier_call = rocker_netdevice_event,
3170 };
3171 
3172 /************************************
3173  * Net event notifier event handler
3174  ************************************/
3175 
3176 static int rocker_netevent_event(struct notifier_block *unused,
3177 				 unsigned long event, void *ptr)
3178 {
3179 	struct rocker_port *rocker_port;
3180 	struct net_device *dev;
3181 	struct neighbour *n = ptr;
3182 	int err;
3183 
3184 	switch (event) {
3185 	case NETEVENT_NEIGH_UPDATE:
3186 		if (n->tbl != &arp_tbl)
3187 			return NOTIFY_DONE;
3188 		dev = n->dev;
3189 		if (!rocker_port_dev_check(dev))
3190 			return NOTIFY_DONE;
3191 		rocker_port = netdev_priv(dev);
3192 		err = rocker_world_port_neigh_update(rocker_port, n);
3193 		if (err)
3194 			netdev_warn(dev, "failed to handle neigh update (err %d)\n",
3195 				    err);
3196 		break;
3197 	}
3198 
3199 	return NOTIFY_DONE;
3200 }
3201 
3202 static struct notifier_block rocker_netevent_nb __read_mostly = {
3203 	.notifier_call = rocker_netevent_event,
3204 };
3205 
3206 /***********************
3207  * Module init and exit
3208  ***********************/
3209 
3210 static int __init rocker_module_init(void)
3211 {
3212 	int err;
3213 
3214 	register_netdevice_notifier(&rocker_netdevice_nb);
3215 	register_netevent_notifier(&rocker_netevent_nb);
3216 	err = pci_register_driver(&rocker_pci_driver);
3217 	if (err)
3218 		goto err_pci_register_driver;
3219 	return 0;
3220 
3221 err_pci_register_driver:
3222 	unregister_netevent_notifier(&rocker_netevent_nb);
3223 	unregister_netdevice_notifier(&rocker_netdevice_nb);
3224 	return err;
3225 }
3226 
3227 static void __exit rocker_module_exit(void)
3228 {
3229 	unregister_netevent_notifier(&rocker_netevent_nb);
3230 	unregister_netdevice_notifier(&rocker_netdevice_nb);
3231 	pci_unregister_driver(&rocker_pci_driver);
3232 }
3233 
3234 module_init(rocker_module_init);
3235 module_exit(rocker_module_exit);
3236 
3237 MODULE_LICENSE("GPL v2");
3238 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
3239 MODULE_AUTHOR("Scott Feldman <sfeldma@gmail.com>");
3240 MODULE_DESCRIPTION("Rocker switch device driver");
3241 MODULE_DEVICE_TABLE(pci, rocker_pci_id_table);
3242