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