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