blob: 5424fb341613fe578b237f3498dc83a5237b2ede [file] [log] [blame]
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001/*
2 * drivers/net/ethernet/rocker/rocker.c - Rocker switch device driver
Jiri Pirko11ce2ba2016-02-16 15:14:41 +01003 * Copyright (c) 2014-2016 Jiri Pirko <jiri@mellanox.com>
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004 * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/pci.h>
15#include <linux/interrupt.h>
16#include <linux/sched.h>
17#include <linux/wait.h>
18#include <linux/spinlock.h>
Jiri Pirko4b8ac962014-11-28 14:34:26 +010019#include <linux/sort.h>
20#include <linux/random.h>
21#include <linux/netdevice.h>
Jiri Pirko4b8ac962014-11-28 14:34:26 +010022#include <linux/skbuff.h>
23#include <linux/socket.h>
24#include <linux/etherdevice.h>
25#include <linux/ethtool.h>
26#include <linux/if_ether.h>
27#include <linux/if_vlan.h>
Scott Feldman6c707942014-11-28 14:34:28 +010028#include <linux/if_bridge.h>
Scott Feldman9f6bbf72014-11-28 14:34:27 +010029#include <linux/bitops.h>
David Aherndb191702015-03-17 20:23:16 -060030#include <linux/ctype.h>
Jiri Pirko4b8ac962014-11-28 14:34:26 +010031#include <net/switchdev.h>
32#include <net/rtnetlink.h>
Scott Feldmanc1beeef2015-03-05 21:21:20 -080033#include <net/netevent.h>
34#include <net/arp.h>
Christoph Hellwig2f8e2c82015-08-28 09:27:14 +020035#include <linux/io-64-nonatomic-lo-hi.h>
Jiri Pirko4b8ac962014-11-28 14:34:26 +010036#include <generated/utsrelease.h>
37
Jiri Pirko0fe685f2016-02-16 15:14:40 +010038#include "rocker_hw.h"
Jiri Pirkode152192016-02-16 15:14:42 +010039#include "rocker.h"
40#include "rocker_tlv.h"
Jiri Pirko4b8ac962014-11-28 14:34:26 +010041
42static const char rocker_driver_name[] = "rocker";
43
44static const struct pci_device_id rocker_pci_id_table[] = {
45 {PCI_VDEVICE(REDHAT, PCI_DEVICE_ID_REDHAT_ROCKER), 0},
46 {0, }
47};
48
Jiri Pirko4b8ac962014-11-28 14:34:26 +010049struct rocker_wait {
50 wait_queue_head_t wait;
51 bool done;
Scott Feldman179f9a22015-06-12 21:35:46 -070052 bool nowait;
Jiri Pirko4b8ac962014-11-28 14:34:26 +010053};
54
55static void rocker_wait_reset(struct rocker_wait *wait)
56{
57 wait->done = false;
Scott Feldman179f9a22015-06-12 21:35:46 -070058 wait->nowait = false;
Jiri Pirko4b8ac962014-11-28 14:34:26 +010059}
60
61static void rocker_wait_init(struct rocker_wait *wait)
62{
63 init_waitqueue_head(&wait->wait);
64 rocker_wait_reset(wait);
65}
66
Jiri Pirkoca0a5f22016-02-16 15:14:47 +010067static struct rocker_wait *rocker_wait_create(void)
Jiri Pirko4b8ac962014-11-28 14:34:26 +010068{
69 struct rocker_wait *wait;
70
Jiri Pirkoca0a5f22016-02-16 15:14:47 +010071 wait = kzalloc(sizeof(*wait), GFP_KERNEL);
Jiri Pirko4b8ac962014-11-28 14:34:26 +010072 if (!wait)
73 return NULL;
Jiri Pirko4b8ac962014-11-28 14:34:26 +010074 return wait;
75}
76
Jiri Pirkoca0a5f22016-02-16 15:14:47 +010077static void rocker_wait_destroy(struct rocker_wait *wait)
Jiri Pirko4b8ac962014-11-28 14:34:26 +010078{
Jiri Pirkoca0a5f22016-02-16 15:14:47 +010079 kfree(wait);
Jiri Pirko4b8ac962014-11-28 14:34:26 +010080}
81
82static bool rocker_wait_event_timeout(struct rocker_wait *wait,
83 unsigned long timeout)
84{
85 wait_event_timeout(wait->wait, wait->done, HZ / 10);
86 if (!wait->done)
87 return false;
88 return true;
89}
90
91static void rocker_wait_wake_up(struct rocker_wait *wait)
92{
93 wait->done = true;
94 wake_up(&wait->wait);
95}
96
Simon Hormane5054642015-05-25 14:28:36 +090097static u32 rocker_msix_vector(const struct rocker *rocker, unsigned int vector)
Jiri Pirko4b8ac962014-11-28 14:34:26 +010098{
99 return rocker->msix_entries[vector].vector;
100}
101
Simon Hormane5054642015-05-25 14:28:36 +0900102static u32 rocker_msix_tx_vector(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100103{
104 return rocker_msix_vector(rocker_port->rocker,
105 ROCKER_MSIX_VEC_TX(rocker_port->port_number));
106}
107
Simon Hormane5054642015-05-25 14:28:36 +0900108static u32 rocker_msix_rx_vector(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100109{
110 return rocker_msix_vector(rocker_port->rocker,
111 ROCKER_MSIX_VEC_RX(rocker_port->port_number));
112}
113
114#define rocker_write32(rocker, reg, val) \
115 writel((val), (rocker)->hw_addr + (ROCKER_ ## reg))
116#define rocker_read32(rocker, reg) \
117 readl((rocker)->hw_addr + (ROCKER_ ## reg))
118#define rocker_write64(rocker, reg, val) \
119 writeq((val), (rocker)->hw_addr + (ROCKER_ ## reg))
120#define rocker_read64(rocker, reg) \
121 readq((rocker)->hw_addr + (ROCKER_ ## reg))
122
123/*****************************
124 * HW basic testing functions
125 *****************************/
126
Simon Hormane5054642015-05-25 14:28:36 +0900127static int rocker_reg_test(const struct rocker *rocker)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100128{
Simon Hormane5054642015-05-25 14:28:36 +0900129 const struct pci_dev *pdev = rocker->pdev;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100130 u64 test_reg;
131 u64 rnd;
132
133 rnd = prandom_u32();
134 rnd >>= 1;
135 rocker_write32(rocker, TEST_REG, rnd);
136 test_reg = rocker_read32(rocker, TEST_REG);
137 if (test_reg != rnd * 2) {
138 dev_err(&pdev->dev, "unexpected 32bit register value %08llx, expected %08llx\n",
139 test_reg, rnd * 2);
140 return -EIO;
141 }
142
143 rnd = prandom_u32();
144 rnd <<= 31;
145 rnd |= prandom_u32();
146 rocker_write64(rocker, TEST_REG64, rnd);
147 test_reg = rocker_read64(rocker, TEST_REG64);
148 if (test_reg != rnd * 2) {
149 dev_err(&pdev->dev, "unexpected 64bit register value %16llx, expected %16llx\n",
150 test_reg, rnd * 2);
151 return -EIO;
152 }
153
154 return 0;
155}
156
Simon Hormane5054642015-05-25 14:28:36 +0900157static int rocker_dma_test_one(const struct rocker *rocker,
158 struct rocker_wait *wait, u32 test_type,
159 dma_addr_t dma_handle, const unsigned char *buf,
160 const unsigned char *expect, size_t size)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100161{
Simon Hormane5054642015-05-25 14:28:36 +0900162 const struct pci_dev *pdev = rocker->pdev;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100163 int i;
164
165 rocker_wait_reset(wait);
166 rocker_write32(rocker, TEST_DMA_CTRL, test_type);
167
168 if (!rocker_wait_event_timeout(wait, HZ / 10)) {
169 dev_err(&pdev->dev, "no interrupt received within a timeout\n");
170 return -EIO;
171 }
172
173 for (i = 0; i < size; i++) {
174 if (buf[i] != expect[i]) {
175 dev_err(&pdev->dev, "unexpected memory content %02x at byte %x\n, %02x expected",
176 buf[i], i, expect[i]);
177 return -EIO;
178 }
179 }
180 return 0;
181}
182
183#define ROCKER_TEST_DMA_BUF_SIZE (PAGE_SIZE * 4)
184#define ROCKER_TEST_DMA_FILL_PATTERN 0x96
185
Simon Hormane5054642015-05-25 14:28:36 +0900186static int rocker_dma_test_offset(const struct rocker *rocker,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100187 struct rocker_wait *wait, int offset)
188{
189 struct pci_dev *pdev = rocker->pdev;
190 unsigned char *alloc;
191 unsigned char *buf;
192 unsigned char *expect;
193 dma_addr_t dma_handle;
194 int i;
195 int err;
196
197 alloc = kzalloc(ROCKER_TEST_DMA_BUF_SIZE * 2 + offset,
198 GFP_KERNEL | GFP_DMA);
199 if (!alloc)
200 return -ENOMEM;
201 buf = alloc + offset;
202 expect = buf + ROCKER_TEST_DMA_BUF_SIZE;
203
204 dma_handle = pci_map_single(pdev, buf, ROCKER_TEST_DMA_BUF_SIZE,
205 PCI_DMA_BIDIRECTIONAL);
206 if (pci_dma_mapping_error(pdev, dma_handle)) {
207 err = -EIO;
208 goto free_alloc;
209 }
210
211 rocker_write64(rocker, TEST_DMA_ADDR, dma_handle);
212 rocker_write32(rocker, TEST_DMA_SIZE, ROCKER_TEST_DMA_BUF_SIZE);
213
214 memset(expect, ROCKER_TEST_DMA_FILL_PATTERN, ROCKER_TEST_DMA_BUF_SIZE);
215 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_FILL,
216 dma_handle, buf, expect,
217 ROCKER_TEST_DMA_BUF_SIZE);
218 if (err)
219 goto unmap;
220
221 memset(expect, 0, ROCKER_TEST_DMA_BUF_SIZE);
222 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_CLEAR,
223 dma_handle, buf, expect,
224 ROCKER_TEST_DMA_BUF_SIZE);
225 if (err)
226 goto unmap;
227
228 prandom_bytes(buf, ROCKER_TEST_DMA_BUF_SIZE);
229 for (i = 0; i < ROCKER_TEST_DMA_BUF_SIZE; i++)
230 expect[i] = ~buf[i];
231 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_INVERT,
232 dma_handle, buf, expect,
233 ROCKER_TEST_DMA_BUF_SIZE);
234 if (err)
235 goto unmap;
236
237unmap:
238 pci_unmap_single(pdev, dma_handle, ROCKER_TEST_DMA_BUF_SIZE,
239 PCI_DMA_BIDIRECTIONAL);
240free_alloc:
241 kfree(alloc);
242
243 return err;
244}
245
Simon Hormane5054642015-05-25 14:28:36 +0900246static int rocker_dma_test(const struct rocker *rocker,
247 struct rocker_wait *wait)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100248{
249 int i;
250 int err;
251
252 for (i = 0; i < 8; i++) {
253 err = rocker_dma_test_offset(rocker, wait, i);
254 if (err)
255 return err;
256 }
257 return 0;
258}
259
260static irqreturn_t rocker_test_irq_handler(int irq, void *dev_id)
261{
262 struct rocker_wait *wait = dev_id;
263
264 rocker_wait_wake_up(wait);
265
266 return IRQ_HANDLED;
267}
268
Simon Hormane5054642015-05-25 14:28:36 +0900269static int rocker_basic_hw_test(const struct rocker *rocker)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100270{
Simon Hormane5054642015-05-25 14:28:36 +0900271 const struct pci_dev *pdev = rocker->pdev;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100272 struct rocker_wait wait;
273 int err;
274
275 err = rocker_reg_test(rocker);
276 if (err) {
277 dev_err(&pdev->dev, "reg test failed\n");
278 return err;
279 }
280
281 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST),
282 rocker_test_irq_handler, 0,
283 rocker_driver_name, &wait);
284 if (err) {
285 dev_err(&pdev->dev, "cannot assign test irq\n");
286 return err;
287 }
288
289 rocker_wait_init(&wait);
290 rocker_write32(rocker, TEST_IRQ, ROCKER_MSIX_VEC_TEST);
291
292 if (!rocker_wait_event_timeout(&wait, HZ / 10)) {
293 dev_err(&pdev->dev, "no interrupt received within a timeout\n");
294 err = -EIO;
295 goto free_irq;
296 }
297
298 err = rocker_dma_test(rocker, &wait);
299 if (err)
300 dev_err(&pdev->dev, "dma test failed\n");
301
302free_irq:
303 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST), &wait);
304 return err;
305}
306
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100307/******************************************
308 * DMA rings and descriptors manipulations
309 ******************************************/
310
311static u32 __pos_inc(u32 pos, size_t limit)
312{
313 return ++pos == limit ? 0 : pos;
314}
315
Simon Hormane5054642015-05-25 14:28:36 +0900316static int rocker_desc_err(const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100317{
Scott Feldman7eb344f2015-02-25 20:15:36 -0800318 int err = desc_info->desc->comp_err & ~ROCKER_DMA_DESC_COMP_ERR_GEN;
319
320 switch (err) {
321 case ROCKER_OK:
322 return 0;
323 case -ROCKER_ENOENT:
324 return -ENOENT;
325 case -ROCKER_ENXIO:
326 return -ENXIO;
327 case -ROCKER_ENOMEM:
328 return -ENOMEM;
329 case -ROCKER_EEXIST:
330 return -EEXIST;
331 case -ROCKER_EINVAL:
332 return -EINVAL;
333 case -ROCKER_EMSGSIZE:
334 return -EMSGSIZE;
335 case -ROCKER_ENOTSUP:
336 return -EOPNOTSUPP;
337 case -ROCKER_ENOBUFS:
338 return -ENOBUFS;
339 }
340
341 return -EINVAL;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100342}
343
Simon Hormane5054642015-05-25 14:28:36 +0900344static void rocker_desc_gen_clear(const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100345{
346 desc_info->desc->comp_err &= ~ROCKER_DMA_DESC_COMP_ERR_GEN;
347}
348
Simon Hormane5054642015-05-25 14:28:36 +0900349static bool rocker_desc_gen(const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100350{
351 u32 comp_err = desc_info->desc->comp_err;
352
353 return comp_err & ROCKER_DMA_DESC_COMP_ERR_GEN ? true : false;
354}
355
Jiri Pirko11ce2ba2016-02-16 15:14:41 +0100356static void *
357rocker_desc_cookie_ptr_get(const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100358{
Arnd Bergmannadedf372015-01-13 15:23:52 +0100359 return (void *)(uintptr_t)desc_info->desc->cookie;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100360}
361
Simon Hormane5054642015-05-25 14:28:36 +0900362static void rocker_desc_cookie_ptr_set(const struct rocker_desc_info *desc_info,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100363 void *ptr)
364{
Arnd Bergmannadedf372015-01-13 15:23:52 +0100365 desc_info->desc->cookie = (uintptr_t) ptr;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100366}
367
368static struct rocker_desc_info *
Simon Hormane5054642015-05-25 14:28:36 +0900369rocker_desc_head_get(const struct rocker_dma_ring_info *info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100370{
371 static struct rocker_desc_info *desc_info;
372 u32 head = __pos_inc(info->head, info->size);
373
374 desc_info = &info->desc_info[info->head];
375 if (head == info->tail)
376 return NULL; /* ring full */
377 desc_info->tlv_size = 0;
378 return desc_info;
379}
380
Simon Hormane5054642015-05-25 14:28:36 +0900381static void rocker_desc_commit(const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100382{
383 desc_info->desc->buf_size = desc_info->data_size;
384 desc_info->desc->tlv_size = desc_info->tlv_size;
385}
386
Simon Hormane5054642015-05-25 14:28:36 +0900387static void rocker_desc_head_set(const struct rocker *rocker,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100388 struct rocker_dma_ring_info *info,
Simon Hormane5054642015-05-25 14:28:36 +0900389 const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100390{
391 u32 head = __pos_inc(info->head, info->size);
392
393 BUG_ON(head == info->tail);
394 rocker_desc_commit(desc_info);
395 info->head = head;
396 rocker_write32(rocker, DMA_DESC_HEAD(info->type), head);
397}
398
399static struct rocker_desc_info *
400rocker_desc_tail_get(struct rocker_dma_ring_info *info)
401{
402 static struct rocker_desc_info *desc_info;
403
404 if (info->tail == info->head)
405 return NULL; /* nothing to be done between head and tail */
406 desc_info = &info->desc_info[info->tail];
407 if (!rocker_desc_gen(desc_info))
408 return NULL; /* gen bit not set, desc is not ready yet */
409 info->tail = __pos_inc(info->tail, info->size);
410 desc_info->tlv_size = desc_info->desc->tlv_size;
411 return desc_info;
412}
413
Simon Hormane5054642015-05-25 14:28:36 +0900414static void rocker_dma_ring_credits_set(const struct rocker *rocker,
415 const struct rocker_dma_ring_info *info,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100416 u32 credits)
417{
418 if (credits)
419 rocker_write32(rocker, DMA_DESC_CREDITS(info->type), credits);
420}
421
422static unsigned long rocker_dma_ring_size_fix(size_t size)
423{
424 return max(ROCKER_DMA_SIZE_MIN,
425 min(roundup_pow_of_two(size), ROCKER_DMA_SIZE_MAX));
426}
427
Simon Hormane5054642015-05-25 14:28:36 +0900428static int rocker_dma_ring_create(const struct rocker *rocker,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100429 unsigned int type,
430 size_t size,
431 struct rocker_dma_ring_info *info)
432{
433 int i;
434
435 BUG_ON(size != rocker_dma_ring_size_fix(size));
436 info->size = size;
437 info->type = type;
438 info->head = 0;
439 info->tail = 0;
440 info->desc_info = kcalloc(info->size, sizeof(*info->desc_info),
441 GFP_KERNEL);
442 if (!info->desc_info)
443 return -ENOMEM;
444
445 info->desc = pci_alloc_consistent(rocker->pdev,
446 info->size * sizeof(*info->desc),
447 &info->mapaddr);
448 if (!info->desc) {
449 kfree(info->desc_info);
450 return -ENOMEM;
451 }
452
453 for (i = 0; i < info->size; i++)
454 info->desc_info[i].desc = &info->desc[i];
455
456 rocker_write32(rocker, DMA_DESC_CTRL(info->type),
457 ROCKER_DMA_DESC_CTRL_RESET);
458 rocker_write64(rocker, DMA_DESC_ADDR(info->type), info->mapaddr);
459 rocker_write32(rocker, DMA_DESC_SIZE(info->type), info->size);
460
461 return 0;
462}
463
Simon Hormane5054642015-05-25 14:28:36 +0900464static void rocker_dma_ring_destroy(const struct rocker *rocker,
465 const struct rocker_dma_ring_info *info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100466{
467 rocker_write64(rocker, DMA_DESC_ADDR(info->type), 0);
468
469 pci_free_consistent(rocker->pdev,
470 info->size * sizeof(struct rocker_desc),
471 info->desc, info->mapaddr);
472 kfree(info->desc_info);
473}
474
Simon Hormane5054642015-05-25 14:28:36 +0900475static void rocker_dma_ring_pass_to_producer(const struct rocker *rocker,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100476 struct rocker_dma_ring_info *info)
477{
478 int i;
479
480 BUG_ON(info->head || info->tail);
481
482 /* When ring is consumer, we need to advance head for each desc.
483 * That tells hw that the desc is ready to be used by it.
484 */
485 for (i = 0; i < info->size - 1; i++)
486 rocker_desc_head_set(rocker, info, &info->desc_info[i]);
487 rocker_desc_commit(&info->desc_info[i]);
488}
489
Simon Hormane5054642015-05-25 14:28:36 +0900490static int rocker_dma_ring_bufs_alloc(const struct rocker *rocker,
491 const struct rocker_dma_ring_info *info,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100492 int direction, size_t buf_size)
493{
494 struct pci_dev *pdev = rocker->pdev;
495 int i;
496 int err;
497
498 for (i = 0; i < info->size; i++) {
499 struct rocker_desc_info *desc_info = &info->desc_info[i];
500 struct rocker_desc *desc = &info->desc[i];
501 dma_addr_t dma_handle;
502 char *buf;
503
504 buf = kzalloc(buf_size, GFP_KERNEL | GFP_DMA);
505 if (!buf) {
506 err = -ENOMEM;
507 goto rollback;
508 }
509
510 dma_handle = pci_map_single(pdev, buf, buf_size, direction);
511 if (pci_dma_mapping_error(pdev, 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
526rollback:
527 for (i--; i >= 0; i--) {
Simon Hormane5054642015-05-25 14:28:36 +0900528 const struct rocker_desc_info *desc_info = &info->desc_info[i];
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100529
530 pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
531 desc_info->data_size, direction);
532 kfree(desc_info->data);
533 }
534 return err;
535}
536
Simon Hormane5054642015-05-25 14:28:36 +0900537static void rocker_dma_ring_bufs_free(const struct rocker *rocker,
538 const struct rocker_dma_ring_info *info,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100539 int direction)
540{
541 struct pci_dev *pdev = rocker->pdev;
542 int i;
543
544 for (i = 0; i < info->size; i++) {
Simon Hormane5054642015-05-25 14:28:36 +0900545 const struct rocker_desc_info *desc_info = &info->desc_info[i];
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100546 struct rocker_desc *desc = &info->desc[i];
547
548 desc->buf_addr = 0;
549 desc->buf_size = 0;
550 pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
551 desc_info->data_size, direction);
552 kfree(desc_info->data);
553 }
554}
555
Jiri Pirkoca0a5f22016-02-16 15:14:47 +0100556static int rocker_dma_cmd_ring_wait_alloc(struct rocker_desc_info *desc_info)
557{
558 struct rocker_wait *wait;
559
560 wait = rocker_wait_create();
561 if (!wait)
562 return -ENOMEM;
563 rocker_desc_cookie_ptr_set(desc_info, wait);
564 return 0;
565}
566
567static void
568rocker_dma_cmd_ring_wait_free(const struct rocker_desc_info *desc_info)
569{
570 struct rocker_wait *wait = rocker_desc_cookie_ptr_get(desc_info);
571
572 rocker_wait_destroy(wait);
573}
574
575static int rocker_dma_cmd_ring_waits_alloc(const struct rocker *rocker)
576{
577 const struct rocker_dma_ring_info *cmd_ring = &rocker->cmd_ring;
578 int i;
579 int err;
580
581 for (i = 0; i < cmd_ring->size; i++) {
582 err = rocker_dma_cmd_ring_wait_alloc(&cmd_ring->desc_info[i]);
583 if (err)
584 goto rollback;
585 }
586 return 0;
587
588rollback:
589 for (i--; i >= 0; i--)
590 rocker_dma_cmd_ring_wait_free(&cmd_ring->desc_info[i]);
591 return err;
592}
593
594static void rocker_dma_cmd_ring_waits_free(const struct rocker *rocker)
595{
596 const struct rocker_dma_ring_info *cmd_ring = &rocker->cmd_ring;
597 int i;
598
599 for (i = 0; i < cmd_ring->size; i++)
600 rocker_dma_cmd_ring_wait_free(&cmd_ring->desc_info[i]);
601}
602
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100603static int rocker_dma_rings_init(struct rocker *rocker)
604{
Simon Hormane5054642015-05-25 14:28:36 +0900605 const struct pci_dev *pdev = rocker->pdev;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100606 int err;
607
608 err = rocker_dma_ring_create(rocker, ROCKER_DMA_CMD,
609 ROCKER_DMA_CMD_DEFAULT_SIZE,
610 &rocker->cmd_ring);
611 if (err) {
612 dev_err(&pdev->dev, "failed to create command dma ring\n");
613 return err;
614 }
615
616 spin_lock_init(&rocker->cmd_ring_lock);
617
618 err = rocker_dma_ring_bufs_alloc(rocker, &rocker->cmd_ring,
619 PCI_DMA_BIDIRECTIONAL, PAGE_SIZE);
620 if (err) {
621 dev_err(&pdev->dev, "failed to alloc command dma ring buffers\n");
622 goto err_dma_cmd_ring_bufs_alloc;
623 }
624
Jiri Pirkoca0a5f22016-02-16 15:14:47 +0100625 err = rocker_dma_cmd_ring_waits_alloc(rocker);
626 if (err) {
627 dev_err(&pdev->dev, "failed to alloc command dma ring waits\n");
628 goto err_dma_cmd_ring_waits_alloc;
629 }
630
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100631 err = rocker_dma_ring_create(rocker, ROCKER_DMA_EVENT,
632 ROCKER_DMA_EVENT_DEFAULT_SIZE,
633 &rocker->event_ring);
634 if (err) {
635 dev_err(&pdev->dev, "failed to create event dma ring\n");
636 goto err_dma_event_ring_create;
637 }
638
639 err = rocker_dma_ring_bufs_alloc(rocker, &rocker->event_ring,
640 PCI_DMA_FROMDEVICE, PAGE_SIZE);
641 if (err) {
642 dev_err(&pdev->dev, "failed to alloc event dma ring buffers\n");
643 goto err_dma_event_ring_bufs_alloc;
644 }
645 rocker_dma_ring_pass_to_producer(rocker, &rocker->event_ring);
646 return 0;
647
648err_dma_event_ring_bufs_alloc:
649 rocker_dma_ring_destroy(rocker, &rocker->event_ring);
650err_dma_event_ring_create:
651 rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
652 PCI_DMA_BIDIRECTIONAL);
Jiri Pirkoca0a5f22016-02-16 15:14:47 +0100653err_dma_cmd_ring_waits_alloc:
654 rocker_dma_cmd_ring_waits_free(rocker);
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100655err_dma_cmd_ring_bufs_alloc:
656 rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
657 return err;
658}
659
660static void rocker_dma_rings_fini(struct rocker *rocker)
661{
662 rocker_dma_ring_bufs_free(rocker, &rocker->event_ring,
663 PCI_DMA_BIDIRECTIONAL);
664 rocker_dma_ring_destroy(rocker, &rocker->event_ring);
Jiri Pirkoca0a5f22016-02-16 15:14:47 +0100665 rocker_dma_cmd_ring_waits_free(rocker);
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100666 rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
667 PCI_DMA_BIDIRECTIONAL);
668 rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
669}
670
Simon Horman534ba6a2015-06-01 13:25:04 +0900671static int rocker_dma_rx_ring_skb_map(const struct rocker_port *rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100672 struct rocker_desc_info *desc_info,
673 struct sk_buff *skb, size_t buf_len)
674{
Simon Horman534ba6a2015-06-01 13:25:04 +0900675 const struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100676 struct pci_dev *pdev = rocker->pdev;
677 dma_addr_t dma_handle;
678
679 dma_handle = pci_map_single(pdev, skb->data, buf_len,
680 PCI_DMA_FROMDEVICE);
681 if (pci_dma_mapping_error(pdev, dma_handle))
682 return -EIO;
683 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_RX_FRAG_ADDR, dma_handle))
684 goto tlv_put_failure;
685 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_RX_FRAG_MAX_LEN, buf_len))
686 goto tlv_put_failure;
687 return 0;
688
689tlv_put_failure:
690 pci_unmap_single(pdev, dma_handle, buf_len, PCI_DMA_FROMDEVICE);
691 desc_info->tlv_size = 0;
692 return -EMSGSIZE;
693}
694
Simon Hormane5054642015-05-25 14:28:36 +0900695static size_t rocker_port_rx_buf_len(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100696{
697 return rocker_port->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
698}
699
Simon Horman534ba6a2015-06-01 13:25:04 +0900700static int rocker_dma_rx_ring_skb_alloc(const struct rocker_port *rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100701 struct rocker_desc_info *desc_info)
702{
703 struct net_device *dev = rocker_port->dev;
704 struct sk_buff *skb;
705 size_t buf_len = rocker_port_rx_buf_len(rocker_port);
706 int err;
707
708 /* Ensure that hw will see tlv_size zero in case of an error.
709 * That tells hw to use another descriptor.
710 */
711 rocker_desc_cookie_ptr_set(desc_info, NULL);
712 desc_info->tlv_size = 0;
713
714 skb = netdev_alloc_skb_ip_align(dev, buf_len);
715 if (!skb)
716 return -ENOMEM;
Simon Horman534ba6a2015-06-01 13:25:04 +0900717 err = rocker_dma_rx_ring_skb_map(rocker_port, desc_info, skb, buf_len);
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100718 if (err) {
719 dev_kfree_skb_any(skb);
720 return err;
721 }
722 rocker_desc_cookie_ptr_set(desc_info, skb);
723 return 0;
724}
725
Simon Hormane5054642015-05-25 14:28:36 +0900726static void rocker_dma_rx_ring_skb_unmap(const struct rocker *rocker,
727 const struct rocker_tlv **attrs)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100728{
729 struct pci_dev *pdev = rocker->pdev;
730 dma_addr_t dma_handle;
731 size_t len;
732
733 if (!attrs[ROCKER_TLV_RX_FRAG_ADDR] ||
734 !attrs[ROCKER_TLV_RX_FRAG_MAX_LEN])
735 return;
736 dma_handle = rocker_tlv_get_u64(attrs[ROCKER_TLV_RX_FRAG_ADDR]);
737 len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_MAX_LEN]);
738 pci_unmap_single(pdev, dma_handle, len, PCI_DMA_FROMDEVICE);
739}
740
Simon Hormane5054642015-05-25 14:28:36 +0900741static void rocker_dma_rx_ring_skb_free(const struct rocker *rocker,
742 const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100743{
Simon Hormane5054642015-05-25 14:28:36 +0900744 const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100745 struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
746
747 if (!skb)
748 return;
749 rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
750 rocker_dma_rx_ring_skb_unmap(rocker, attrs);
751 dev_kfree_skb_any(skb);
752}
753
Simon Horman534ba6a2015-06-01 13:25:04 +0900754static int rocker_dma_rx_ring_skbs_alloc(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100755{
Simon Hormane5054642015-05-25 14:28:36 +0900756 const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
Simon Horman534ba6a2015-06-01 13:25:04 +0900757 const struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100758 int i;
759 int err;
760
761 for (i = 0; i < rx_ring->size; i++) {
Simon Horman534ba6a2015-06-01 13:25:04 +0900762 err = rocker_dma_rx_ring_skb_alloc(rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100763 &rx_ring->desc_info[i]);
764 if (err)
765 goto rollback;
766 }
767 return 0;
768
769rollback:
770 for (i--; i >= 0; i--)
771 rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
772 return err;
773}
774
Simon Horman534ba6a2015-06-01 13:25:04 +0900775static void rocker_dma_rx_ring_skbs_free(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100776{
Simon Hormane5054642015-05-25 14:28:36 +0900777 const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
Simon Horman534ba6a2015-06-01 13:25:04 +0900778 const struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100779 int i;
780
781 for (i = 0; i < rx_ring->size; i++)
782 rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
783}
784
785static int rocker_port_dma_rings_init(struct rocker_port *rocker_port)
786{
787 struct rocker *rocker = rocker_port->rocker;
788 int err;
789
790 err = rocker_dma_ring_create(rocker,
791 ROCKER_DMA_TX(rocker_port->port_number),
792 ROCKER_DMA_TX_DEFAULT_SIZE,
793 &rocker_port->tx_ring);
794 if (err) {
795 netdev_err(rocker_port->dev, "failed to create tx dma ring\n");
796 return err;
797 }
798
799 err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->tx_ring,
800 PCI_DMA_TODEVICE,
801 ROCKER_DMA_TX_DESC_SIZE);
802 if (err) {
803 netdev_err(rocker_port->dev, "failed to alloc tx dma ring buffers\n");
804 goto err_dma_tx_ring_bufs_alloc;
805 }
806
807 err = rocker_dma_ring_create(rocker,
808 ROCKER_DMA_RX(rocker_port->port_number),
809 ROCKER_DMA_RX_DEFAULT_SIZE,
810 &rocker_port->rx_ring);
811 if (err) {
812 netdev_err(rocker_port->dev, "failed to create rx dma ring\n");
813 goto err_dma_rx_ring_create;
814 }
815
816 err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->rx_ring,
817 PCI_DMA_BIDIRECTIONAL,
818 ROCKER_DMA_RX_DESC_SIZE);
819 if (err) {
820 netdev_err(rocker_port->dev, "failed to alloc rx dma ring buffers\n");
821 goto err_dma_rx_ring_bufs_alloc;
822 }
823
Simon Horman534ba6a2015-06-01 13:25:04 +0900824 err = rocker_dma_rx_ring_skbs_alloc(rocker_port);
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100825 if (err) {
826 netdev_err(rocker_port->dev, "failed to alloc rx dma ring skbs\n");
827 goto err_dma_rx_ring_skbs_alloc;
828 }
829 rocker_dma_ring_pass_to_producer(rocker, &rocker_port->rx_ring);
830
831 return 0;
832
833err_dma_rx_ring_skbs_alloc:
834 rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
835 PCI_DMA_BIDIRECTIONAL);
836err_dma_rx_ring_bufs_alloc:
837 rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
838err_dma_rx_ring_create:
839 rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
840 PCI_DMA_TODEVICE);
841err_dma_tx_ring_bufs_alloc:
842 rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
843 return err;
844}
845
846static void rocker_port_dma_rings_fini(struct rocker_port *rocker_port)
847{
848 struct rocker *rocker = rocker_port->rocker;
849
Simon Horman534ba6a2015-06-01 13:25:04 +0900850 rocker_dma_rx_ring_skbs_free(rocker_port);
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100851 rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
852 PCI_DMA_BIDIRECTIONAL);
853 rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
854 rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
855 PCI_DMA_TODEVICE);
856 rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
857}
858
Simon Hormane5054642015-05-25 14:28:36 +0900859static void rocker_port_set_enable(const struct rocker_port *rocker_port,
860 bool enable)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100861{
862 u64 val = rocker_read64(rocker_port->rocker, PORT_PHYS_ENABLE);
863
864 if (enable)
David S. Miller71a83a62015-03-03 21:16:48 -0500865 val |= 1ULL << rocker_port->pport;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100866 else
David S. Miller71a83a62015-03-03 21:16:48 -0500867 val &= ~(1ULL << rocker_port->pport);
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100868 rocker_write64(rocker_port->rocker, PORT_PHYS_ENABLE, val);
869}
870
871/********************************
872 * Interrupt handler and helpers
873 ********************************/
874
875static irqreturn_t rocker_cmd_irq_handler(int irq, void *dev_id)
876{
877 struct rocker *rocker = dev_id;
Simon Hormane5054642015-05-25 14:28:36 +0900878 const struct rocker_desc_info *desc_info;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100879 struct rocker_wait *wait;
880 u32 credits = 0;
881
882 spin_lock(&rocker->cmd_ring_lock);
883 while ((desc_info = rocker_desc_tail_get(&rocker->cmd_ring))) {
884 wait = rocker_desc_cookie_ptr_get(desc_info);
Scott Feldman179f9a22015-06-12 21:35:46 -0700885 if (wait->nowait) {
886 rocker_desc_gen_clear(desc_info);
Scott Feldman179f9a22015-06-12 21:35:46 -0700887 } else {
888 rocker_wait_wake_up(wait);
889 }
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100890 credits++;
891 }
892 spin_unlock(&rocker->cmd_ring_lock);
893 rocker_dma_ring_credits_set(rocker, &rocker->cmd_ring, credits);
894
895 return IRQ_HANDLED;
896}
897
Simon Hormane5054642015-05-25 14:28:36 +0900898static void rocker_port_link_up(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100899{
900 netif_carrier_on(rocker_port->dev);
901 netdev_info(rocker_port->dev, "Link is up\n");
902}
903
Simon Hormane5054642015-05-25 14:28:36 +0900904static void rocker_port_link_down(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100905{
906 netif_carrier_off(rocker_port->dev);
907 netdev_info(rocker_port->dev, "Link is down\n");
908}
909
Simon Hormane5054642015-05-25 14:28:36 +0900910static int rocker_event_link_change(const struct rocker *rocker,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100911 const struct rocker_tlv *info)
912{
Simon Hormane5054642015-05-25 14:28:36 +0900913 const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_LINK_CHANGED_MAX + 1];
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100914 unsigned int port_number;
915 bool link_up;
916 struct rocker_port *rocker_port;
917
918 rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_LINK_CHANGED_MAX, info);
Scott Feldman4a6bb6d2015-02-25 20:15:37 -0800919 if (!attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT] ||
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100920 !attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP])
921 return -EIO;
922 port_number =
Scott Feldman4a6bb6d2015-02-25 20:15:37 -0800923 rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT]) - 1;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100924 link_up = rocker_tlv_get_u8(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP]);
925
926 if (port_number >= rocker->port_count)
927 return -EINVAL;
928
929 rocker_port = rocker->ports[port_number];
930 if (netif_carrier_ok(rocker_port->dev) != link_up) {
931 if (link_up)
932 rocker_port_link_up(rocker_port);
933 else
934 rocker_port_link_down(rocker_port);
935 }
936
937 return 0;
938}
939
Jiri Pirkoe4201142016-02-16 15:14:45 +0100940static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port *rocker_port,
941 const unsigned char *addr,
942 __be16 vlan_id);
Scott Feldman6c707942014-11-28 14:34:28 +0100943
Simon Hormane5054642015-05-25 14:28:36 +0900944static int rocker_event_mac_vlan_seen(const struct rocker *rocker,
Scott Feldman6c707942014-11-28 14:34:28 +0100945 const struct rocker_tlv *info)
946{
Simon Hormane5054642015-05-25 14:28:36 +0900947 const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAX + 1];
Scott Feldman6c707942014-11-28 14:34:28 +0100948 unsigned int port_number;
949 struct rocker_port *rocker_port;
Simon Hormane5054642015-05-25 14:28:36 +0900950 const unsigned char *addr;
Scott Feldman6c707942014-11-28 14:34:28 +0100951 __be16 vlan_id;
952
953 rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_MAC_VLAN_MAX, info);
Scott Feldman4a6bb6d2015-02-25 20:15:37 -0800954 if (!attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT] ||
Scott Feldman6c707942014-11-28 14:34:28 +0100955 !attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC] ||
956 !attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID])
957 return -EIO;
958 port_number =
Scott Feldman4a6bb6d2015-02-25 20:15:37 -0800959 rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT]) - 1;
Scott Feldman6c707942014-11-28 14:34:28 +0100960 addr = rocker_tlv_data(attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC]);
Jiri Pirko9b03c712014-12-03 14:14:53 +0100961 vlan_id = rocker_tlv_get_be16(attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID]);
Scott Feldman6c707942014-11-28 14:34:28 +0100962
963 if (port_number >= rocker->port_count)
964 return -EINVAL;
965
966 rocker_port = rocker->ports[port_number];
Jiri Pirko3fbcdbf2016-02-16 15:14:50 +0100967 return rocker_world_port_ev_mac_vlan_seen(rocker_port, addr, vlan_id);
Scott Feldman6c707942014-11-28 14:34:28 +0100968}
Scott Feldman9f6bbf72014-11-28 14:34:27 +0100969
Simon Hormane5054642015-05-25 14:28:36 +0900970static int rocker_event_process(const struct rocker *rocker,
971 const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100972{
Simon Hormane5054642015-05-25 14:28:36 +0900973 const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAX + 1];
974 const struct rocker_tlv *info;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100975 u16 type;
976
977 rocker_tlv_parse_desc(attrs, ROCKER_TLV_EVENT_MAX, desc_info);
978 if (!attrs[ROCKER_TLV_EVENT_TYPE] ||
979 !attrs[ROCKER_TLV_EVENT_INFO])
980 return -EIO;
981
982 type = rocker_tlv_get_u16(attrs[ROCKER_TLV_EVENT_TYPE]);
983 info = attrs[ROCKER_TLV_EVENT_INFO];
984
985 switch (type) {
986 case ROCKER_TLV_EVENT_TYPE_LINK_CHANGED:
987 return rocker_event_link_change(rocker, info);
Scott Feldman6c707942014-11-28 14:34:28 +0100988 case ROCKER_TLV_EVENT_TYPE_MAC_VLAN_SEEN:
989 return rocker_event_mac_vlan_seen(rocker, info);
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100990 }
991
992 return -EOPNOTSUPP;
993}
994
995static irqreturn_t rocker_event_irq_handler(int irq, void *dev_id)
996{
997 struct rocker *rocker = dev_id;
Simon Hormane5054642015-05-25 14:28:36 +0900998 const struct pci_dev *pdev = rocker->pdev;
999 const struct rocker_desc_info *desc_info;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001000 u32 credits = 0;
1001 int err;
1002
1003 while ((desc_info = rocker_desc_tail_get(&rocker->event_ring))) {
1004 err = rocker_desc_err(desc_info);
1005 if (err) {
1006 dev_err(&pdev->dev, "event desc received with err %d\n",
1007 err);
1008 } else {
1009 err = rocker_event_process(rocker, desc_info);
1010 if (err)
1011 dev_err(&pdev->dev, "event processing failed with err %d\n",
1012 err);
1013 }
1014 rocker_desc_gen_clear(desc_info);
1015 rocker_desc_head_set(rocker, &rocker->event_ring, desc_info);
1016 credits++;
1017 }
1018 rocker_dma_ring_credits_set(rocker, &rocker->event_ring, credits);
1019
1020 return IRQ_HANDLED;
1021}
1022
1023static irqreturn_t rocker_tx_irq_handler(int irq, void *dev_id)
1024{
1025 struct rocker_port *rocker_port = dev_id;
1026
1027 napi_schedule(&rocker_port->napi_tx);
1028 return IRQ_HANDLED;
1029}
1030
1031static irqreturn_t rocker_rx_irq_handler(int irq, void *dev_id)
1032{
1033 struct rocker_port *rocker_port = dev_id;
1034
1035 napi_schedule(&rocker_port->napi_rx);
1036 return IRQ_HANDLED;
1037}
1038
1039/********************
1040 * Command interface
1041 ********************/
1042
Jiri Pirko3fbcdbf2016-02-16 15:14:50 +01001043int rocker_cmd_exec(struct rocker_port *rocker_port, bool nowait,
1044 rocker_cmd_prep_cb_t prepare, void *prepare_priv,
1045 rocker_cmd_proc_cb_t process, void *process_priv)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001046{
Simon Horman534ba6a2015-06-01 13:25:04 +09001047 struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001048 struct rocker_desc_info *desc_info;
1049 struct rocker_wait *wait;
Scott Feldman179f9a22015-06-12 21:35:46 -07001050 unsigned long lock_flags;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001051 int err;
1052
Scott Feldman179f9a22015-06-12 21:35:46 -07001053 spin_lock_irqsave(&rocker->cmd_ring_lock, lock_flags);
Scott Feldmanc4f20322015-05-10 09:47:50 -07001054
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001055 desc_info = rocker_desc_head_get(&rocker->cmd_ring);
1056 if (!desc_info) {
Scott Feldman179f9a22015-06-12 21:35:46 -07001057 spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
Jiri Pirkoca0a5f22016-02-16 15:14:47 +01001058 return -EAGAIN;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001059 }
Scott Feldmanc4f20322015-05-10 09:47:50 -07001060
Jiri Pirkoca0a5f22016-02-16 15:14:47 +01001061 wait = rocker_desc_cookie_ptr_get(desc_info);
1062 rocker_wait_init(wait);
1063 wait->nowait = nowait;
1064
Simon Horman534ba6a2015-06-01 13:25:04 +09001065 err = prepare(rocker_port, desc_info, prepare_priv);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001066 if (err) {
Scott Feldman179f9a22015-06-12 21:35:46 -07001067 spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
Jiri Pirkoca0a5f22016-02-16 15:14:47 +01001068 return err;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001069 }
Scott Feldmanc4f20322015-05-10 09:47:50 -07001070
Jiri Pirkoae3907e2016-02-16 15:14:48 +01001071 rocker_desc_head_set(rocker, &rocker->cmd_ring, desc_info);
Scott Feldmanc4f20322015-05-10 09:47:50 -07001072
Scott Feldman179f9a22015-06-12 21:35:46 -07001073 spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
1074
1075 if (nowait)
1076 return 0;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001077
Jiri Pirkoae3907e2016-02-16 15:14:48 +01001078 if (!rocker_wait_event_timeout(wait, HZ / 10))
1079 return -EIO;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001080
1081 err = rocker_desc_err(desc_info);
1082 if (err)
1083 return err;
1084
1085 if (process)
Simon Horman534ba6a2015-06-01 13:25:04 +09001086 err = process(rocker_port, desc_info, process_priv);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001087
1088 rocker_desc_gen_clear(desc_info);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001089 return err;
1090}
1091
1092static int
Simon Horman534ba6a2015-06-01 13:25:04 +09001093rocker_cmd_get_port_settings_prep(const struct rocker_port *rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001094 struct rocker_desc_info *desc_info,
1095 void *priv)
1096{
1097 struct rocker_tlv *cmd_info;
1098
1099 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1100 ROCKER_TLV_CMD_TYPE_GET_PORT_SETTINGS))
1101 return -EMSGSIZE;
1102 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1103 if (!cmd_info)
1104 return -EMSGSIZE;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001105 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1106 rocker_port->pport))
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001107 return -EMSGSIZE;
1108 rocker_tlv_nest_end(desc_info, cmd_info);
1109 return 0;
1110}
1111
1112static int
Simon Horman534ba6a2015-06-01 13:25:04 +09001113rocker_cmd_get_port_settings_ethtool_proc(const struct rocker_port *rocker_port,
Simon Hormane5054642015-05-25 14:28:36 +09001114 const struct rocker_desc_info *desc_info,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001115 void *priv)
1116{
1117 struct ethtool_cmd *ecmd = priv;
Simon Hormane5054642015-05-25 14:28:36 +09001118 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1119 const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001120 u32 speed;
1121 u8 duplex;
1122 u8 autoneg;
1123
1124 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1125 if (!attrs[ROCKER_TLV_CMD_INFO])
1126 return -EIO;
1127
1128 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1129 attrs[ROCKER_TLV_CMD_INFO]);
1130 if (!info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED] ||
1131 !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX] ||
1132 !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG])
1133 return -EIO;
1134
1135 speed = rocker_tlv_get_u32(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED]);
1136 duplex = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX]);
1137 autoneg = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG]);
1138
1139 ecmd->transceiver = XCVR_INTERNAL;
1140 ecmd->supported = SUPPORTED_TP;
1141 ecmd->phy_address = 0xff;
1142 ecmd->port = PORT_TP;
1143 ethtool_cmd_speed_set(ecmd, speed);
1144 ecmd->duplex = duplex ? DUPLEX_FULL : DUPLEX_HALF;
1145 ecmd->autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
1146
1147 return 0;
1148}
1149
1150static int
Simon Horman534ba6a2015-06-01 13:25:04 +09001151rocker_cmd_get_port_settings_macaddr_proc(const struct rocker_port *rocker_port,
Simon Hormane5054642015-05-25 14:28:36 +09001152 const struct rocker_desc_info *desc_info,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001153 void *priv)
1154{
1155 unsigned char *macaddr = priv;
Simon Hormane5054642015-05-25 14:28:36 +09001156 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1157 const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1158 const struct rocker_tlv *attr;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001159
1160 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1161 if (!attrs[ROCKER_TLV_CMD_INFO])
1162 return -EIO;
1163
1164 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1165 attrs[ROCKER_TLV_CMD_INFO]);
1166 attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR];
1167 if (!attr)
1168 return -EIO;
1169
1170 if (rocker_tlv_len(attr) != ETH_ALEN)
1171 return -EINVAL;
1172
1173 ether_addr_copy(macaddr, rocker_tlv_data(attr));
1174 return 0;
1175}
1176
Jiri Pirkoe1ba3de2016-02-16 15:14:43 +01001177static int
1178rocker_cmd_get_port_settings_mode_proc(const struct rocker_port *rocker_port,
1179 const struct rocker_desc_info *desc_info,
1180 void *priv)
1181{
1182 u8 *p_mode = priv;
1183 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1184 const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1185 const struct rocker_tlv *attr;
1186
1187 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1188 if (!attrs[ROCKER_TLV_CMD_INFO])
1189 return -EIO;
1190
1191 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1192 attrs[ROCKER_TLV_CMD_INFO]);
1193 attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MODE];
1194 if (!attr)
1195 return -EIO;
1196
1197 *p_mode = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MODE]);
1198 return 0;
1199}
1200
David Aherndb191702015-03-17 20:23:16 -06001201struct port_name {
1202 char *buf;
1203 size_t len;
1204};
1205
1206static int
Simon Horman534ba6a2015-06-01 13:25:04 +09001207rocker_cmd_get_port_settings_phys_name_proc(const struct rocker_port *rocker_port,
Simon Hormane5054642015-05-25 14:28:36 +09001208 const struct rocker_desc_info *desc_info,
David Aherndb191702015-03-17 20:23:16 -06001209 void *priv)
1210{
Simon Hormane5054642015-05-25 14:28:36 +09001211 const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1212 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
David Aherndb191702015-03-17 20:23:16 -06001213 struct port_name *name = priv;
Simon Hormane5054642015-05-25 14:28:36 +09001214 const struct rocker_tlv *attr;
David Aherndb191702015-03-17 20:23:16 -06001215 size_t i, j, len;
Simon Hormane5054642015-05-25 14:28:36 +09001216 const char *str;
David Aherndb191702015-03-17 20:23:16 -06001217
1218 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1219 if (!attrs[ROCKER_TLV_CMD_INFO])
1220 return -EIO;
1221
1222 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1223 attrs[ROCKER_TLV_CMD_INFO]);
1224 attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_PHYS_NAME];
1225 if (!attr)
1226 return -EIO;
1227
1228 len = min_t(size_t, rocker_tlv_len(attr), name->len);
1229 str = rocker_tlv_data(attr);
1230
1231 /* make sure name only contains alphanumeric characters */
1232 for (i = j = 0; i < len; ++i) {
1233 if (isalnum(str[i])) {
1234 name->buf[j] = str[i];
1235 j++;
1236 }
1237 }
1238
1239 if (j == 0)
1240 return -EIO;
1241
1242 name->buf[j] = '\0';
1243
1244 return 0;
1245}
1246
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001247static int
Simon Horman534ba6a2015-06-01 13:25:04 +09001248rocker_cmd_set_port_settings_ethtool_prep(const struct rocker_port *rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001249 struct rocker_desc_info *desc_info,
1250 void *priv)
1251{
1252 struct ethtool_cmd *ecmd = priv;
1253 struct rocker_tlv *cmd_info;
1254
1255 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1256 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1257 return -EMSGSIZE;
1258 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1259 if (!cmd_info)
1260 return -EMSGSIZE;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001261 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1262 rocker_port->pport))
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001263 return -EMSGSIZE;
1264 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_SPEED,
1265 ethtool_cmd_speed(ecmd)))
1266 return -EMSGSIZE;
1267 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX,
1268 ecmd->duplex))
1269 return -EMSGSIZE;
1270 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG,
1271 ecmd->autoneg))
1272 return -EMSGSIZE;
1273 rocker_tlv_nest_end(desc_info, cmd_info);
1274 return 0;
1275}
1276
1277static int
Simon Horman534ba6a2015-06-01 13:25:04 +09001278rocker_cmd_set_port_settings_macaddr_prep(const struct rocker_port *rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001279 struct rocker_desc_info *desc_info,
1280 void *priv)
1281{
Simon Hormane5054642015-05-25 14:28:36 +09001282 const unsigned char *macaddr = priv;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001283 struct rocker_tlv *cmd_info;
1284
1285 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1286 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1287 return -EMSGSIZE;
1288 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1289 if (!cmd_info)
1290 return -EMSGSIZE;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001291 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1292 rocker_port->pport))
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001293 return -EMSGSIZE;
1294 if (rocker_tlv_put(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR,
1295 ETH_ALEN, macaddr))
1296 return -EMSGSIZE;
1297 rocker_tlv_nest_end(desc_info, cmd_info);
1298 return 0;
1299}
1300
Scott Feldman5111f802014-11-28 14:34:30 +01001301static int
Scott Feldman77a58c72015-07-08 16:06:47 -07001302rocker_cmd_set_port_settings_mtu_prep(const struct rocker_port *rocker_port,
1303 struct rocker_desc_info *desc_info,
1304 void *priv)
1305{
1306 int mtu = *(int *)priv;
1307 struct rocker_tlv *cmd_info;
1308
1309 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1310 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1311 return -EMSGSIZE;
1312 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1313 if (!cmd_info)
1314 return -EMSGSIZE;
1315 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1316 rocker_port->pport))
1317 return -EMSGSIZE;
1318 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MTU,
1319 mtu))
1320 return -EMSGSIZE;
1321 rocker_tlv_nest_end(desc_info, cmd_info);
1322 return 0;
1323}
1324
1325static int
Simon Horman534ba6a2015-06-01 13:25:04 +09001326rocker_cmd_set_port_learning_prep(const struct rocker_port *rocker_port,
Scott Feldman5111f802014-11-28 14:34:30 +01001327 struct rocker_desc_info *desc_info,
1328 void *priv)
1329{
Jiri Pirkoc1fe9222016-02-16 15:14:46 +01001330 bool learning = *(bool *)priv;
Scott Feldman5111f802014-11-28 14:34:30 +01001331 struct rocker_tlv *cmd_info;
1332
1333 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1334 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1335 return -EMSGSIZE;
1336 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1337 if (!cmd_info)
1338 return -EMSGSIZE;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001339 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1340 rocker_port->pport))
Scott Feldman5111f802014-11-28 14:34:30 +01001341 return -EMSGSIZE;
1342 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_LEARNING,
Jiri Pirkoc1fe9222016-02-16 15:14:46 +01001343 learning))
Scott Feldman5111f802014-11-28 14:34:30 +01001344 return -EMSGSIZE;
1345 rocker_tlv_nest_end(desc_info, cmd_info);
1346 return 0;
1347}
1348
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001349static int rocker_cmd_get_port_settings_ethtool(struct rocker_port *rocker_port,
1350 struct ethtool_cmd *ecmd)
1351{
Jiri Pirko53901cc2016-02-16 15:14:49 +01001352 return rocker_cmd_exec(rocker_port, false,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001353 rocker_cmd_get_port_settings_prep, NULL,
1354 rocker_cmd_get_port_settings_ethtool_proc,
Scott Feldmanc4f20322015-05-10 09:47:50 -07001355 ecmd);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001356}
1357
1358static int rocker_cmd_get_port_settings_macaddr(struct rocker_port *rocker_port,
1359 unsigned char *macaddr)
1360{
Jiri Pirko53901cc2016-02-16 15:14:49 +01001361 return rocker_cmd_exec(rocker_port, false,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001362 rocker_cmd_get_port_settings_prep, NULL,
1363 rocker_cmd_get_port_settings_macaddr_proc,
Scott Feldmanc4f20322015-05-10 09:47:50 -07001364 macaddr);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001365}
1366
Jiri Pirkoe1ba3de2016-02-16 15:14:43 +01001367static int rocker_cmd_get_port_settings_mode(struct rocker_port *rocker_port,
1368 u8 *p_mode)
1369{
Jiri Pirko53901cc2016-02-16 15:14:49 +01001370 return rocker_cmd_exec(rocker_port, false,
Jiri Pirkoe1ba3de2016-02-16 15:14:43 +01001371 rocker_cmd_get_port_settings_prep, NULL,
1372 rocker_cmd_get_port_settings_mode_proc, p_mode);
1373}
1374
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001375static int rocker_cmd_set_port_settings_ethtool(struct rocker_port *rocker_port,
1376 struct ethtool_cmd *ecmd)
1377{
Jiri Pirko53901cc2016-02-16 15:14:49 +01001378 return rocker_cmd_exec(rocker_port, false,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001379 rocker_cmd_set_port_settings_ethtool_prep,
Scott Feldmanc4f20322015-05-10 09:47:50 -07001380 ecmd, NULL, NULL);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001381}
1382
1383static int rocker_cmd_set_port_settings_macaddr(struct rocker_port *rocker_port,
1384 unsigned char *macaddr)
1385{
Jiri Pirko53901cc2016-02-16 15:14:49 +01001386 return rocker_cmd_exec(rocker_port, false,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001387 rocker_cmd_set_port_settings_macaddr_prep,
Scott Feldmanc4f20322015-05-10 09:47:50 -07001388 macaddr, NULL, NULL);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001389}
1390
Scott Feldman77a58c72015-07-08 16:06:47 -07001391static int rocker_cmd_set_port_settings_mtu(struct rocker_port *rocker_port,
1392 int mtu)
1393{
Jiri Pirko53901cc2016-02-16 15:14:49 +01001394 return rocker_cmd_exec(rocker_port, false,
Scott Feldman77a58c72015-07-08 16:06:47 -07001395 rocker_cmd_set_port_settings_mtu_prep,
1396 &mtu, NULL, NULL);
1397}
1398
Jiri Pirko3fbcdbf2016-02-16 15:14:50 +01001399int rocker_port_set_learning(struct rocker_port *rocker_port,
1400 bool learning)
Scott Feldman5111f802014-11-28 14:34:30 +01001401{
Jiri Pirko53901cc2016-02-16 15:14:49 +01001402 return rocker_cmd_exec(rocker_port, false,
Scott Feldman5111f802014-11-28 14:34:30 +01001403 rocker_cmd_set_port_learning_prep,
Jiri Pirkoc1fe9222016-02-16 15:14:46 +01001404 &learning, NULL, NULL);
Scott Feldman5111f802014-11-28 14:34:30 +01001405}
1406
Jiri Pirkoe4201142016-02-16 15:14:45 +01001407/**********************
1408 * Worlds manipulation
1409 **********************/
1410
1411static struct rocker_world_ops *rocker_world_ops[] = {
1412 &rocker_ofdpa_ops,
1413};
1414
1415#define ROCKER_WORLD_OPS_LEN ARRAY_SIZE(rocker_world_ops)
1416
1417static struct rocker_world_ops *rocker_world_ops_find(u8 mode)
1418{
1419 int i;
1420
1421 for (i = 0; i < ROCKER_WORLD_OPS_LEN; i++)
1422 if (rocker_world_ops[i]->mode == mode)
1423 return rocker_world_ops[i];
1424 return NULL;
1425}
1426
1427static int rocker_world_init(struct rocker *rocker, u8 mode)
1428{
1429 struct rocker_world_ops *wops;
1430 int err;
1431
1432 wops = rocker_world_ops_find(mode);
1433 if (!wops) {
1434 dev_err(&rocker->pdev->dev, "port mode \"%d\" is not supported\n",
1435 mode);
1436 return -EINVAL;
1437 }
1438 rocker->wops = wops;
1439 rocker->wpriv = kzalloc(wops->priv_size, GFP_KERNEL);
1440 if (!rocker->wpriv)
1441 return -ENOMEM;
1442 if (!wops->init)
1443 return 0;
1444 err = wops->init(rocker);
1445 if (err)
1446 kfree(rocker->wpriv);
1447 return err;
1448}
1449
1450static void rocker_world_fini(struct rocker *rocker)
1451{
1452 struct rocker_world_ops *wops = rocker->wops;
1453
1454 if (!wops || !wops->fini)
1455 return;
1456 wops->fini(rocker);
1457 kfree(rocker->wpriv);
1458}
1459
1460static int rocker_world_check_init(struct rocker_port *rocker_port)
1461{
1462 struct rocker *rocker = rocker_port->rocker;
1463 u8 mode;
1464 int err;
1465
1466 err = rocker_cmd_get_port_settings_mode(rocker_port, &mode);
1467 if (err) {
1468 dev_err(&rocker->pdev->dev, "failed to get port mode\n");
1469 return err;
1470 }
1471 if (rocker->wops) {
1472 if (rocker->wops->mode != mode) {
1473 dev_err(&rocker->pdev->dev, "hardware has ports in different worlds, which is not supported\n");
1474 return err;
1475 }
1476 return 0;
1477 }
1478 return rocker_world_init(rocker, mode);
1479}
1480
1481static int rocker_world_port_pre_init(struct rocker_port *rocker_port)
1482{
1483 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1484 int err;
1485
1486 rocker_port->wpriv = kzalloc(wops->port_priv_size, GFP_KERNEL);
1487 if (!rocker_port->wpriv)
1488 return -ENOMEM;
1489 if (!wops->port_pre_init)
1490 return 0;
1491 err = wops->port_pre_init(rocker_port);
1492 if (err)
1493 kfree(rocker_port->wpriv);
1494 return 0;
1495}
1496
1497static int rocker_world_port_init(struct rocker_port *rocker_port)
1498{
1499 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1500
1501 if (!wops->port_init)
1502 return 0;
1503 return wops->port_init(rocker_port);
1504}
1505
1506static void rocker_world_port_fini(struct rocker_port *rocker_port)
1507{
1508 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1509
1510 if (!wops->port_fini)
1511 return;
1512 wops->port_fini(rocker_port);
1513}
1514
1515static void rocker_world_port_post_fini(struct rocker_port *rocker_port)
1516{
1517 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1518
1519 if (!wops->port_post_fini)
1520 return;
1521 wops->port_post_fini(rocker_port);
1522 kfree(rocker_port->wpriv);
1523}
1524
1525static int rocker_world_port_open(struct rocker_port *rocker_port)
1526{
1527 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1528
1529 if (!wops->port_open)
1530 return 0;
1531 return wops->port_open(rocker_port);
1532}
1533
1534static void rocker_world_port_stop(struct rocker_port *rocker_port)
1535{
1536 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1537
1538 if (!wops->port_stop)
1539 return;
1540 wops->port_stop(rocker_port);
1541}
1542
1543static int rocker_world_port_attr_stp_state_set(struct rocker_port *rocker_port,
1544 u8 state,
1545 struct switchdev_trans *trans)
1546{
1547 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1548
1549 if (!wops->port_attr_stp_state_set)
Jiri Pirkofccd84d2016-02-16 15:14:51 +01001550 return -EOPNOTSUPP;
Jiri Pirkoe4201142016-02-16 15:14:45 +01001551 return wops->port_attr_stp_state_set(rocker_port, state, trans);
1552}
1553
1554static int
1555rocker_world_port_attr_bridge_flags_set(struct rocker_port *rocker_port,
1556 unsigned long brport_flags,
1557 struct switchdev_trans *trans)
1558{
1559 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1560
1561 if (!wops->port_attr_bridge_flags_set)
Jiri Pirkofccd84d2016-02-16 15:14:51 +01001562 return -EOPNOTSUPP;
Jiri Pirkoe4201142016-02-16 15:14:45 +01001563 return wops->port_attr_bridge_flags_set(rocker_port, brport_flags,
1564 trans);
1565}
1566
1567static int
1568rocker_world_port_attr_bridge_flags_get(const struct rocker_port *rocker_port,
1569 unsigned long *p_brport_flags)
1570{
1571 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1572
1573 if (!wops->port_attr_bridge_flags_get)
Jiri Pirkofccd84d2016-02-16 15:14:51 +01001574 return -EOPNOTSUPP;
Jiri Pirkoe4201142016-02-16 15:14:45 +01001575 return wops->port_attr_bridge_flags_get(rocker_port, p_brport_flags);
1576}
1577
1578static int
1579rocker_world_port_attr_bridge_ageing_time_set(struct rocker_port *rocker_port,
1580 u32 ageing_time,
1581 struct switchdev_trans *trans)
1582
1583{
1584 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1585
1586 if (!wops->port_attr_bridge_ageing_time_set)
Jiri Pirkofccd84d2016-02-16 15:14:51 +01001587 return -EOPNOTSUPP;
Jiri Pirkoe4201142016-02-16 15:14:45 +01001588 return wops->port_attr_bridge_ageing_time_set(rocker_port, ageing_time,
1589 trans);
1590}
1591
1592static int
1593rocker_world_port_obj_vlan_add(struct rocker_port *rocker_port,
1594 const struct switchdev_obj_port_vlan *vlan,
1595 struct switchdev_trans *trans)
1596{
1597 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1598
1599 if (!wops->port_obj_vlan_add)
Jiri Pirkofccd84d2016-02-16 15:14:51 +01001600 return -EOPNOTSUPP;
Jiri Pirkoe4201142016-02-16 15:14:45 +01001601 return wops->port_obj_vlan_add(rocker_port, vlan, trans);
1602}
1603
1604static int
1605rocker_world_port_obj_vlan_del(struct rocker_port *rocker_port,
1606 const struct switchdev_obj_port_vlan *vlan)
1607{
1608 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1609
1610 if (!wops->port_obj_vlan_del)
Jiri Pirkofccd84d2016-02-16 15:14:51 +01001611 return -EOPNOTSUPP;
Jiri Pirkoe4201142016-02-16 15:14:45 +01001612 return wops->port_obj_vlan_del(rocker_port, vlan);
1613}
1614
1615static int
1616rocker_world_port_obj_vlan_dump(const struct rocker_port *rocker_port,
1617 struct switchdev_obj_port_vlan *vlan,
1618 switchdev_obj_dump_cb_t *cb)
1619{
1620 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1621
1622 if (!wops->port_obj_vlan_dump)
Jiri Pirkofccd84d2016-02-16 15:14:51 +01001623 return -EOPNOTSUPP;
Jiri Pirkoe4201142016-02-16 15:14:45 +01001624 return wops->port_obj_vlan_dump(rocker_port, vlan, cb);
1625}
1626
1627static int
Jiri Pirkoe4201142016-02-16 15:14:45 +01001628rocker_world_port_obj_fdb_add(struct rocker_port *rocker_port,
1629 const struct switchdev_obj_port_fdb *fdb,
1630 struct switchdev_trans *trans)
1631{
1632 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1633
1634 if (!wops->port_obj_fdb_add)
Jiri Pirkofccd84d2016-02-16 15:14:51 +01001635 return -EOPNOTSUPP;
Jiri Pirkoe4201142016-02-16 15:14:45 +01001636 return wops->port_obj_fdb_add(rocker_port, fdb, trans);
1637}
1638
1639static int
1640rocker_world_port_obj_fdb_del(struct rocker_port *rocker_port,
1641 const struct switchdev_obj_port_fdb *fdb)
1642{
1643 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1644
1645 if (!wops->port_obj_fdb_del)
Jiri Pirkofccd84d2016-02-16 15:14:51 +01001646 return -EOPNOTSUPP;
Jiri Pirkoe4201142016-02-16 15:14:45 +01001647 return wops->port_obj_fdb_del(rocker_port, fdb);
1648}
1649
1650static int
1651rocker_world_port_obj_fdb_dump(const struct rocker_port *rocker_port,
1652 struct switchdev_obj_port_fdb *fdb,
1653 switchdev_obj_dump_cb_t *cb)
1654{
1655 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1656
1657 if (!wops->port_obj_fdb_dump)
Jiri Pirkofccd84d2016-02-16 15:14:51 +01001658 return -EOPNOTSUPP;
Jiri Pirkoe4201142016-02-16 15:14:45 +01001659 return wops->port_obj_fdb_dump(rocker_port, fdb, cb);
1660}
1661
1662static int rocker_world_port_master_linked(struct rocker_port *rocker_port,
1663 struct net_device *master)
1664{
1665 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1666
1667 if (!wops->port_master_linked)
Jiri Pirkofccd84d2016-02-16 15:14:51 +01001668 return -EOPNOTSUPP;
Jiri Pirkoe4201142016-02-16 15:14:45 +01001669 return wops->port_master_linked(rocker_port, master);
1670}
1671
1672static int rocker_world_port_master_unlinked(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_unlinked)
Jiri Pirkofccd84d2016-02-16 15:14:51 +01001678 return -EOPNOTSUPP;
Jiri Pirkoe4201142016-02-16 15:14:45 +01001679 return wops->port_master_unlinked(rocker_port, master);
1680}
1681
1682static int rocker_world_port_neigh_update(struct rocker_port *rocker_port,
1683 struct neighbour *n)
1684{
1685 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1686
1687 if (!wops->port_neigh_update)
Jiri Pirkofccd84d2016-02-16 15:14:51 +01001688 return -EOPNOTSUPP;
Jiri Pirkoe4201142016-02-16 15:14:45 +01001689 return wops->port_neigh_update(rocker_port, n);
1690}
1691
1692static int rocker_world_port_neigh_destroy(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_destroy)
Jiri Pirkofccd84d2016-02-16 15:14:51 +01001698 return -EOPNOTSUPP;
Jiri Pirkoe4201142016-02-16 15:14:45 +01001699 return wops->port_neigh_destroy(rocker_port, n);
1700}
1701
1702static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port *rocker_port,
1703 const unsigned char *addr,
1704 __be16 vlan_id)
1705{
1706 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1707
1708 if (!wops->port_ev_mac_vlan_seen)
Jiri Pirkofccd84d2016-02-16 15:14:51 +01001709 return -EOPNOTSUPP;
Jiri Pirkoe4201142016-02-16 15:14:45 +01001710 return wops->port_ev_mac_vlan_seen(rocker_port, addr, vlan_id);
1711}
1712
Jiri Pirko936bd482016-09-26 12:52:32 +02001713static int rocker_world_fib4_add(struct rocker *rocker,
1714 const struct fib_entry_notifier_info *fen_info)
1715{
1716 struct rocker_world_ops *wops = rocker->wops;
1717
1718 if (!wops->fib4_add)
1719 return 0;
1720 return wops->fib4_add(rocker, fen_info);
1721}
1722
1723static int rocker_world_fib4_del(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_del)
1729 return 0;
1730 return wops->fib4_del(rocker, fen_info);
1731}
1732
1733static void rocker_world_fib4_abort(struct rocker *rocker)
1734{
1735 struct rocker_world_ops *wops = rocker->wops;
1736
1737 if (wops->fib4_abort)
1738 wops->fib4_abort(rocker);
1739}
1740
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001741/*****************
1742 * Net device ops
1743 *****************/
1744
1745static int rocker_port_open(struct net_device *dev)
1746{
1747 struct rocker_port *rocker_port = netdev_priv(dev);
1748 int err;
1749
1750 err = rocker_port_dma_rings_init(rocker_port);
1751 if (err)
1752 return err;
1753
1754 err = request_irq(rocker_msix_tx_vector(rocker_port),
1755 rocker_tx_irq_handler, 0,
1756 rocker_driver_name, rocker_port);
1757 if (err) {
1758 netdev_err(rocker_port->dev, "cannot assign tx irq\n");
1759 goto err_request_tx_irq;
1760 }
1761
1762 err = request_irq(rocker_msix_rx_vector(rocker_port),
1763 rocker_rx_irq_handler, 0,
1764 rocker_driver_name, rocker_port);
1765 if (err) {
1766 netdev_err(rocker_port->dev, "cannot assign rx irq\n");
1767 goto err_request_rx_irq;
1768 }
1769
Jiri Pirkoe4201142016-02-16 15:14:45 +01001770 err = rocker_world_port_open(rocker_port);
1771 if (err) {
1772 netdev_err(rocker_port->dev, "cannot open port in world\n");
1773 goto err_world_port_open;
1774 }
1775
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001776 napi_enable(&rocker_port->napi_tx);
1777 napi_enable(&rocker_port->napi_rx);
Anuradha Karuppiahc3055242015-07-14 13:43:21 -07001778 if (!dev->proto_down)
1779 rocker_port_set_enable(rocker_port, true);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001780 netif_start_queue(dev);
1781 return 0;
1782
Jiri Pirkoe4201142016-02-16 15:14:45 +01001783err_world_port_open:
Scott Feldman6c707942014-11-28 14:34:28 +01001784 free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001785err_request_rx_irq:
1786 free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
1787err_request_tx_irq:
1788 rocker_port_dma_rings_fini(rocker_port);
1789 return err;
1790}
1791
1792static int rocker_port_stop(struct net_device *dev)
1793{
1794 struct rocker_port *rocker_port = netdev_priv(dev);
1795
1796 netif_stop_queue(dev);
1797 rocker_port_set_enable(rocker_port, false);
1798 napi_disable(&rocker_port->napi_rx);
1799 napi_disable(&rocker_port->napi_tx);
Jiri Pirkoe4201142016-02-16 15:14:45 +01001800 rocker_world_port_stop(rocker_port);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001801 free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
1802 free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
1803 rocker_port_dma_rings_fini(rocker_port);
1804
1805 return 0;
1806}
1807
Simon Hormane5054642015-05-25 14:28:36 +09001808static void rocker_tx_desc_frags_unmap(const struct rocker_port *rocker_port,
1809 const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001810{
Simon Hormane5054642015-05-25 14:28:36 +09001811 const struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001812 struct pci_dev *pdev = rocker->pdev;
Simon Hormane5054642015-05-25 14:28:36 +09001813 const struct rocker_tlv *attrs[ROCKER_TLV_TX_MAX + 1];
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001814 struct rocker_tlv *attr;
1815 int rem;
1816
1817 rocker_tlv_parse_desc(attrs, ROCKER_TLV_TX_MAX, desc_info);
1818 if (!attrs[ROCKER_TLV_TX_FRAGS])
1819 return;
1820 rocker_tlv_for_each_nested(attr, attrs[ROCKER_TLV_TX_FRAGS], rem) {
Simon Hormane5054642015-05-25 14:28:36 +09001821 const struct rocker_tlv *frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_MAX + 1];
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001822 dma_addr_t dma_handle;
1823 size_t len;
1824
1825 if (rocker_tlv_type(attr) != ROCKER_TLV_TX_FRAG)
1826 continue;
1827 rocker_tlv_parse_nested(frag_attrs, ROCKER_TLV_TX_FRAG_ATTR_MAX,
1828 attr);
1829 if (!frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR] ||
1830 !frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN])
1831 continue;
1832 dma_handle = rocker_tlv_get_u64(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR]);
1833 len = rocker_tlv_get_u16(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN]);
1834 pci_unmap_single(pdev, dma_handle, len, DMA_TO_DEVICE);
1835 }
1836}
1837
Simon Hormane5054642015-05-25 14:28:36 +09001838static int rocker_tx_desc_frag_map_put(const struct rocker_port *rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001839 struct rocker_desc_info *desc_info,
1840 char *buf, size_t buf_len)
1841{
Simon Hormane5054642015-05-25 14:28:36 +09001842 const struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001843 struct pci_dev *pdev = rocker->pdev;
1844 dma_addr_t dma_handle;
1845 struct rocker_tlv *frag;
1846
1847 dma_handle = pci_map_single(pdev, buf, buf_len, DMA_TO_DEVICE);
1848 if (unlikely(pci_dma_mapping_error(pdev, dma_handle))) {
1849 if (net_ratelimit())
1850 netdev_err(rocker_port->dev, "failed to dma map tx frag\n");
1851 return -EIO;
1852 }
1853 frag = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAG);
1854 if (!frag)
1855 goto unmap_frag;
1856 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_TX_FRAG_ATTR_ADDR,
1857 dma_handle))
1858 goto nest_cancel;
1859 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_TX_FRAG_ATTR_LEN,
1860 buf_len))
1861 goto nest_cancel;
1862 rocker_tlv_nest_end(desc_info, frag);
1863 return 0;
1864
1865nest_cancel:
1866 rocker_tlv_nest_cancel(desc_info, frag);
1867unmap_frag:
1868 pci_unmap_single(pdev, dma_handle, buf_len, DMA_TO_DEVICE);
1869 return -EMSGSIZE;
1870}
1871
1872static netdev_tx_t rocker_port_xmit(struct sk_buff *skb, struct net_device *dev)
1873{
1874 struct rocker_port *rocker_port = netdev_priv(dev);
1875 struct rocker *rocker = rocker_port->rocker;
1876 struct rocker_desc_info *desc_info;
1877 struct rocker_tlv *frags;
1878 int i;
1879 int err;
1880
1881 desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
1882 if (unlikely(!desc_info)) {
1883 if (net_ratelimit())
1884 netdev_err(dev, "tx ring full when queue awake\n");
1885 return NETDEV_TX_BUSY;
1886 }
1887
1888 rocker_desc_cookie_ptr_set(desc_info, skb);
1889
1890 frags = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAGS);
1891 if (!frags)
1892 goto out;
1893 err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
1894 skb->data, skb_headlen(skb));
1895 if (err)
1896 goto nest_cancel;
Jiri Pirko95b9be62015-08-02 20:56:38 +02001897 if (skb_shinfo(skb)->nr_frags > ROCKER_TX_FRAGS_MAX) {
1898 err = skb_linearize(skb);
1899 if (err)
1900 goto unmap_frags;
1901 }
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001902
1903 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1904 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1905
1906 err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
1907 skb_frag_address(frag),
1908 skb_frag_size(frag));
1909 if (err)
1910 goto unmap_frags;
1911 }
1912 rocker_tlv_nest_end(desc_info, frags);
1913
1914 rocker_desc_gen_clear(desc_info);
1915 rocker_desc_head_set(rocker, &rocker_port->tx_ring, desc_info);
1916
1917 desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
1918 if (!desc_info)
1919 netif_stop_queue(dev);
1920
1921 return NETDEV_TX_OK;
1922
1923unmap_frags:
1924 rocker_tx_desc_frags_unmap(rocker_port, desc_info);
1925nest_cancel:
1926 rocker_tlv_nest_cancel(desc_info, frags);
1927out:
1928 dev_kfree_skb(skb);
David Ahernf2bbca52015-01-16 14:22:29 -07001929 dev->stats.tx_dropped++;
1930
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001931 return NETDEV_TX_OK;
1932}
1933
1934static int rocker_port_set_mac_address(struct net_device *dev, void *p)
1935{
1936 struct sockaddr *addr = p;
1937 struct rocker_port *rocker_port = netdev_priv(dev);
1938 int err;
1939
1940 if (!is_valid_ether_addr(addr->sa_data))
1941 return -EADDRNOTAVAIL;
1942
1943 err = rocker_cmd_set_port_settings_macaddr(rocker_port, addr->sa_data);
1944 if (err)
1945 return err;
1946 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1947 return 0;
1948}
1949
Scott Feldman77a58c72015-07-08 16:06:47 -07001950static int rocker_port_change_mtu(struct net_device *dev, int new_mtu)
1951{
1952 struct rocker_port *rocker_port = netdev_priv(dev);
1953 int running = netif_running(dev);
1954 int err;
1955
1956#define ROCKER_PORT_MIN_MTU 68
1957#define ROCKER_PORT_MAX_MTU 9000
1958
1959 if (new_mtu < ROCKER_PORT_MIN_MTU || new_mtu > ROCKER_PORT_MAX_MTU)
1960 return -EINVAL;
1961
1962 if (running)
1963 rocker_port_stop(dev);
1964
1965 netdev_info(dev, "MTU change from %d to %d\n", dev->mtu, new_mtu);
1966 dev->mtu = new_mtu;
1967
1968 err = rocker_cmd_set_port_settings_mtu(rocker_port, new_mtu);
1969 if (err)
1970 return err;
1971
1972 if (running)
1973 err = rocker_port_open(dev);
1974
1975 return err;
1976}
1977
David Aherndb191702015-03-17 20:23:16 -06001978static int rocker_port_get_phys_port_name(struct net_device *dev,
1979 char *buf, size_t len)
1980{
1981 struct rocker_port *rocker_port = netdev_priv(dev);
1982 struct port_name name = { .buf = buf, .len = len };
1983 int err;
1984
Jiri Pirko53901cc2016-02-16 15:14:49 +01001985 err = rocker_cmd_exec(rocker_port, false,
David Aherndb191702015-03-17 20:23:16 -06001986 rocker_cmd_get_port_settings_prep, NULL,
1987 rocker_cmd_get_port_settings_phys_name_proc,
Scott Feldmanc4f20322015-05-10 09:47:50 -07001988 &name);
David Aherndb191702015-03-17 20:23:16 -06001989
1990 return err ? -EOPNOTSUPP : 0;
1991}
1992
Anuradha Karuppiahc3055242015-07-14 13:43:21 -07001993static int rocker_port_change_proto_down(struct net_device *dev,
1994 bool proto_down)
1995{
1996 struct rocker_port *rocker_port = netdev_priv(dev);
1997
1998 if (rocker_port->dev->flags & IFF_UP)
1999 rocker_port_set_enable(rocker_port, !proto_down);
2000 rocker_port->dev->proto_down = proto_down;
2001 return 0;
2002}
2003
Jiri Pirko503eebc2016-07-05 11:27:37 +02002004static void rocker_port_neigh_destroy(struct net_device *dev,
2005 struct neighbour *n)
Scott Feldmandd19f832015-08-12 18:45:25 -07002006{
2007 struct rocker_port *rocker_port = netdev_priv(n->dev);
Jiri Pirkoe4201142016-02-16 15:14:45 +01002008 int err;
Scott Feldmandd19f832015-08-12 18:45:25 -07002009
Jiri Pirkoe4201142016-02-16 15:14:45 +01002010 err = rocker_world_port_neigh_destroy(rocker_port, n);
2011 if (err)
2012 netdev_warn(rocker_port->dev, "failed to handle neigh destroy (err %d)\n",
2013 err);
Scott Feldmandd19f832015-08-12 18:45:25 -07002014}
2015
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002016static const struct net_device_ops rocker_port_netdev_ops = {
2017 .ndo_open = rocker_port_open,
2018 .ndo_stop = rocker_port_stop,
2019 .ndo_start_xmit = rocker_port_xmit,
2020 .ndo_set_mac_address = rocker_port_set_mac_address,
Scott Feldman77a58c72015-07-08 16:06:47 -07002021 .ndo_change_mtu = rocker_port_change_mtu,
Scott Feldman85fdb952015-05-10 09:48:05 -07002022 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
Scott Feldmanfc8f40d2015-05-10 09:47:57 -07002023 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
Scott Feldman54ba5a02015-05-10 09:48:01 -07002024 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07002025 .ndo_fdb_add = switchdev_port_fdb_add,
2026 .ndo_fdb_del = switchdev_port_fdb_del,
2027 .ndo_fdb_dump = switchdev_port_fdb_dump,
David Aherndb191702015-03-17 20:23:16 -06002028 .ndo_get_phys_port_name = rocker_port_get_phys_port_name,
Anuradha Karuppiahc3055242015-07-14 13:43:21 -07002029 .ndo_change_proto_down = rocker_port_change_proto_down,
Scott Feldmandd19f832015-08-12 18:45:25 -07002030 .ndo_neigh_destroy = rocker_port_neigh_destroy,
Scott Feldman98237d42015-03-15 21:07:15 -07002031};
2032
2033/********************
2034 * swdev interface
2035 ********************/
2036
Scott Feldmanf8e20a92015-05-10 09:47:49 -07002037static int rocker_port_attr_get(struct net_device *dev,
2038 struct switchdev_attr *attr)
Scott Feldman98237d42015-03-15 21:07:15 -07002039{
Simon Hormane5054642015-05-25 14:28:36 +09002040 const struct rocker_port *rocker_port = netdev_priv(dev);
2041 const struct rocker *rocker = rocker_port->rocker;
Jiri Pirkoe4201142016-02-16 15:14:45 +01002042 int err = 0;
Scott Feldman98237d42015-03-15 21:07:15 -07002043
Scott Feldmanf8e20a92015-05-10 09:47:49 -07002044 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +02002045 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -07002046 attr->u.ppid.id_len = sizeof(rocker->hw.id);
2047 memcpy(&attr->u.ppid.id, &rocker->hw.id, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -07002048 break;
Jiri Pirko1f868392015-10-01 11:03:42 +02002049 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
Jiri Pirkoe4201142016-02-16 15:14:45 +01002050 err = rocker_world_port_attr_bridge_flags_get(rocker_port,
2051 &attr->u.brport_flags);
Scott Feldman6004c862015-05-10 09:47:55 -07002052 break;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07002053 default:
2054 return -EOPNOTSUPP;
2055 }
2056
Jiri Pirkoe4201142016-02-16 15:14:45 +01002057 return err;
Scott Feldman98237d42015-03-15 21:07:15 -07002058}
2059
Scott Feldmanc4f20322015-05-10 09:47:50 -07002060static int rocker_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +02002061 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +02002062 struct switchdev_trans *trans)
Scott Feldmanc4f20322015-05-10 09:47:50 -07002063{
2064 struct rocker_port *rocker_port = netdev_priv(dev);
2065 int err = 0;
2066
Scott Feldmanc4f20322015-05-10 09:47:50 -07002067 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +02002068 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Jiri Pirkoe4201142016-02-16 15:14:45 +01002069 err = rocker_world_port_attr_stp_state_set(rocker_port,
2070 attr->u.stp_state,
2071 trans);
Scott Feldman35636062015-05-10 09:47:51 -07002072 break;
Jiri Pirko1f868392015-10-01 11:03:42 +02002073 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
Jiri Pirkoe4201142016-02-16 15:14:45 +01002074 err = rocker_world_port_attr_bridge_flags_set(rocker_port,
2075 attr->u.brport_flags,
2076 trans);
Scott Feldman6004c862015-05-10 09:47:55 -07002077 break;
Scott Feldmand0cf57f2015-10-08 19:23:20 -07002078 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
Jiri Pirkoe4201142016-02-16 15:14:45 +01002079 err = rocker_world_port_attr_bridge_ageing_time_set(rocker_port,
2080 attr->u.ageing_time,
2081 trans);
Scott Feldmand0cf57f2015-10-08 19:23:20 -07002082 break;
Scott Feldmanc4f20322015-05-10 09:47:50 -07002083 default:
2084 err = -EOPNOTSUPP;
2085 break;
2086 }
2087
2088 return err;
Scott Feldman98237d42015-03-15 21:07:15 -07002089}
2090
Scott Feldman9228ad22015-05-10 09:47:54 -07002091static int rocker_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +02002092 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +02002093 struct switchdev_trans *trans)
Scott Feldman9228ad22015-05-10 09:47:54 -07002094{
2095 struct rocker_port *rocker_port = netdev_priv(dev);
2096 int err = 0;
2097
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02002098 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +02002099 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirkoe4201142016-02-16 15:14:45 +01002100 err = rocker_world_port_obj_vlan_add(rocker_port,
2101 SWITCHDEV_OBJ_PORT_VLAN(obj),
2102 trans);
Scott Feldman9228ad22015-05-10 09:47:54 -07002103 break;
Jiri Pirko57d80832015-10-01 11:03:41 +02002104 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirkoe4201142016-02-16 15:14:45 +01002105 err = rocker_world_port_obj_fdb_add(rocker_port,
2106 SWITCHDEV_OBJ_PORT_FDB(obj),
2107 trans);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07002108 break;
Scott Feldman9228ad22015-05-10 09:47:54 -07002109 default:
2110 err = -EOPNOTSUPP;
2111 break;
2112 }
2113
2114 return err;
2115}
2116
Scott Feldman9228ad22015-05-10 09:47:54 -07002117static int rocker_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +02002118 const struct switchdev_obj *obj)
Scott Feldman9228ad22015-05-10 09:47:54 -07002119{
2120 struct rocker_port *rocker_port = netdev_priv(dev);
2121 int err = 0;
2122
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02002123 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +02002124 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirkoe4201142016-02-16 15:14:45 +01002125 err = rocker_world_port_obj_vlan_del(rocker_port,
2126 SWITCHDEV_OBJ_PORT_VLAN(obj));
Scott Feldman9228ad22015-05-10 09:47:54 -07002127 break;
Jiri Pirko57d80832015-10-01 11:03:41 +02002128 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirkoe4201142016-02-16 15:14:45 +01002129 err = rocker_world_port_obj_fdb_del(rocker_port,
2130 SWITCHDEV_OBJ_PORT_FDB(obj));
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07002131 break;
2132 default:
2133 err = -EOPNOTSUPP;
2134 break;
2135 }
2136
2137 return err;
2138}
2139
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07002140static int rocker_port_obj_dump(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +02002141 struct switchdev_obj *obj,
2142 switchdev_obj_dump_cb_t *cb)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07002143{
Simon Hormane5054642015-05-25 14:28:36 +09002144 const struct rocker_port *rocker_port = netdev_priv(dev);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07002145 int err = 0;
2146
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02002147 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +02002148 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirkoe4201142016-02-16 15:14:45 +01002149 err = rocker_world_port_obj_fdb_dump(rocker_port,
2150 SWITCHDEV_OBJ_PORT_FDB(obj),
2151 cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07002152 break;
Jiri Pirko57d80832015-10-01 11:03:41 +02002153 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirkoe4201142016-02-16 15:14:45 +01002154 err = rocker_world_port_obj_vlan_dump(rocker_port,
2155 SWITCHDEV_OBJ_PORT_VLAN(obj),
2156 cb);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07002157 break;
Scott Feldman9228ad22015-05-10 09:47:54 -07002158 default:
2159 err = -EOPNOTSUPP;
2160 break;
2161 }
2162
2163 return err;
2164}
2165
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07002166static const struct switchdev_ops rocker_port_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -07002167 .switchdev_port_attr_get = rocker_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -07002168 .switchdev_port_attr_set = rocker_port_attr_set,
Scott Feldman9228ad22015-05-10 09:47:54 -07002169 .switchdev_port_obj_add = rocker_port_obj_add,
2170 .switchdev_port_obj_del = rocker_port_obj_del,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07002171 .switchdev_port_obj_dump = rocker_port_obj_dump,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002172};
2173
Jiri Pirko936bd482016-09-26 12:52:32 +02002174static int rocker_router_fib_event(struct notifier_block *nb,
2175 unsigned long event, void *ptr)
2176{
2177 struct rocker *rocker = container_of(nb, struct rocker, fib_nb);
2178 struct fib_entry_notifier_info *fen_info = ptr;
2179 int err;
2180
2181 switch (event) {
2182 case FIB_EVENT_ENTRY_ADD:
2183 err = rocker_world_fib4_add(rocker, fen_info);
2184 if (err)
2185 rocker_world_fib4_abort(rocker);
2186 else
2187 break;
2188 case FIB_EVENT_ENTRY_DEL:
2189 rocker_world_fib4_del(rocker, fen_info);
2190 break;
2191 case FIB_EVENT_RULE_ADD: /* fall through */
2192 case FIB_EVENT_RULE_DEL:
2193 rocker_world_fib4_abort(rocker);
2194 break;
2195 }
2196 return NOTIFY_DONE;
2197}
2198
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002199/********************
2200 * ethtool interface
2201 ********************/
2202
2203static int rocker_port_get_settings(struct net_device *dev,
2204 struct ethtool_cmd *ecmd)
2205{
2206 struct rocker_port *rocker_port = netdev_priv(dev);
2207
2208 return rocker_cmd_get_port_settings_ethtool(rocker_port, ecmd);
2209}
2210
2211static int rocker_port_set_settings(struct net_device *dev,
2212 struct ethtool_cmd *ecmd)
2213{
2214 struct rocker_port *rocker_port = netdev_priv(dev);
2215
2216 return rocker_cmd_set_port_settings_ethtool(rocker_port, ecmd);
2217}
2218
2219static void rocker_port_get_drvinfo(struct net_device *dev,
2220 struct ethtool_drvinfo *drvinfo)
2221{
2222 strlcpy(drvinfo->driver, rocker_driver_name, sizeof(drvinfo->driver));
2223 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
2224}
2225
David Ahern9766e972015-01-29 20:59:33 -07002226static struct rocker_port_stats {
2227 char str[ETH_GSTRING_LEN];
2228 int type;
2229} rocker_port_stats[] = {
2230 { "rx_packets", ROCKER_TLV_CMD_PORT_STATS_RX_PKTS, },
2231 { "rx_bytes", ROCKER_TLV_CMD_PORT_STATS_RX_BYTES, },
2232 { "rx_dropped", ROCKER_TLV_CMD_PORT_STATS_RX_DROPPED, },
2233 { "rx_errors", ROCKER_TLV_CMD_PORT_STATS_RX_ERRORS, },
2234
2235 { "tx_packets", ROCKER_TLV_CMD_PORT_STATS_TX_PKTS, },
2236 { "tx_bytes", ROCKER_TLV_CMD_PORT_STATS_TX_BYTES, },
2237 { "tx_dropped", ROCKER_TLV_CMD_PORT_STATS_TX_DROPPED, },
2238 { "tx_errors", ROCKER_TLV_CMD_PORT_STATS_TX_ERRORS, },
2239};
2240
2241#define ROCKER_PORT_STATS_LEN ARRAY_SIZE(rocker_port_stats)
2242
2243static void rocker_port_get_strings(struct net_device *netdev, u32 stringset,
2244 u8 *data)
2245{
2246 u8 *p = data;
2247 int i;
2248
2249 switch (stringset) {
2250 case ETH_SS_STATS:
2251 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
2252 memcpy(p, rocker_port_stats[i].str, ETH_GSTRING_LEN);
2253 p += ETH_GSTRING_LEN;
2254 }
2255 break;
2256 }
2257}
2258
2259static int
Simon Horman534ba6a2015-06-01 13:25:04 +09002260rocker_cmd_get_port_stats_prep(const struct rocker_port *rocker_port,
David Ahern9766e972015-01-29 20:59:33 -07002261 struct rocker_desc_info *desc_info,
2262 void *priv)
2263{
2264 struct rocker_tlv *cmd_stats;
2265
2266 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
2267 ROCKER_TLV_CMD_TYPE_GET_PORT_STATS))
2268 return -EMSGSIZE;
2269
2270 cmd_stats = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2271 if (!cmd_stats)
2272 return -EMSGSIZE;
2273
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08002274 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_STATS_PPORT,
2275 rocker_port->pport))
David Ahern9766e972015-01-29 20:59:33 -07002276 return -EMSGSIZE;
2277
2278 rocker_tlv_nest_end(desc_info, cmd_stats);
2279
2280 return 0;
2281}
2282
2283static int
Simon Horman534ba6a2015-06-01 13:25:04 +09002284rocker_cmd_get_port_stats_ethtool_proc(const struct rocker_port *rocker_port,
Simon Hormane5054642015-05-25 14:28:36 +09002285 const struct rocker_desc_info *desc_info,
David Ahern9766e972015-01-29 20:59:33 -07002286 void *priv)
2287{
Simon Hormane5054642015-05-25 14:28:36 +09002288 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
2289 const struct rocker_tlv *stats_attrs[ROCKER_TLV_CMD_PORT_STATS_MAX + 1];
2290 const struct rocker_tlv *pattr;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08002291 u32 pport;
David Ahern9766e972015-01-29 20:59:33 -07002292 u64 *data = priv;
2293 int i;
2294
2295 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
2296
2297 if (!attrs[ROCKER_TLV_CMD_INFO])
2298 return -EIO;
2299
2300 rocker_tlv_parse_nested(stats_attrs, ROCKER_TLV_CMD_PORT_STATS_MAX,
2301 attrs[ROCKER_TLV_CMD_INFO]);
2302
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08002303 if (!stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT])
David Ahern9766e972015-01-29 20:59:33 -07002304 return -EIO;
2305
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08002306 pport = rocker_tlv_get_u32(stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]);
2307 if (pport != rocker_port->pport)
David Ahern9766e972015-01-29 20:59:33 -07002308 return -EIO;
2309
2310 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
2311 pattr = stats_attrs[rocker_port_stats[i].type];
2312 if (!pattr)
2313 continue;
2314
2315 data[i] = rocker_tlv_get_u64(pattr);
2316 }
2317
2318 return 0;
2319}
2320
2321static int rocker_cmd_get_port_stats_ethtool(struct rocker_port *rocker_port,
2322 void *priv)
2323{
Jiri Pirko53901cc2016-02-16 15:14:49 +01002324 return rocker_cmd_exec(rocker_port, false,
David Ahern9766e972015-01-29 20:59:33 -07002325 rocker_cmd_get_port_stats_prep, NULL,
2326 rocker_cmd_get_port_stats_ethtool_proc,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002327 priv);
David Ahern9766e972015-01-29 20:59:33 -07002328}
2329
2330static void rocker_port_get_stats(struct net_device *dev,
2331 struct ethtool_stats *stats, u64 *data)
2332{
2333 struct rocker_port *rocker_port = netdev_priv(dev);
2334
2335 if (rocker_cmd_get_port_stats_ethtool(rocker_port, data) != 0) {
2336 int i;
2337
2338 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); ++i)
2339 data[i] = 0;
2340 }
David Ahern9766e972015-01-29 20:59:33 -07002341}
2342
2343static int rocker_port_get_sset_count(struct net_device *netdev, int sset)
2344{
2345 switch (sset) {
2346 case ETH_SS_STATS:
2347 return ROCKER_PORT_STATS_LEN;
2348 default:
2349 return -EOPNOTSUPP;
2350 }
2351}
2352
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002353static const struct ethtool_ops rocker_port_ethtool_ops = {
2354 .get_settings = rocker_port_get_settings,
2355 .set_settings = rocker_port_set_settings,
2356 .get_drvinfo = rocker_port_get_drvinfo,
2357 .get_link = ethtool_op_get_link,
David Ahern9766e972015-01-29 20:59:33 -07002358 .get_strings = rocker_port_get_strings,
2359 .get_ethtool_stats = rocker_port_get_stats,
2360 .get_sset_count = rocker_port_get_sset_count,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002361};
2362
2363/*****************
2364 * NAPI interface
2365 *****************/
2366
2367static struct rocker_port *rocker_port_napi_tx_get(struct napi_struct *napi)
2368{
2369 return container_of(napi, struct rocker_port, napi_tx);
2370}
2371
2372static int rocker_port_poll_tx(struct napi_struct *napi, int budget)
2373{
2374 struct rocker_port *rocker_port = rocker_port_napi_tx_get(napi);
Simon Hormane5054642015-05-25 14:28:36 +09002375 const struct rocker *rocker = rocker_port->rocker;
2376 const struct rocker_desc_info *desc_info;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002377 u32 credits = 0;
2378 int err;
2379
2380 /* Cleanup tx descriptors */
2381 while ((desc_info = rocker_desc_tail_get(&rocker_port->tx_ring))) {
David Ahernf2bbca52015-01-16 14:22:29 -07002382 struct sk_buff *skb;
2383
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002384 err = rocker_desc_err(desc_info);
2385 if (err && net_ratelimit())
2386 netdev_err(rocker_port->dev, "tx desc received with err %d\n",
2387 err);
2388 rocker_tx_desc_frags_unmap(rocker_port, desc_info);
David Ahernf2bbca52015-01-16 14:22:29 -07002389
2390 skb = rocker_desc_cookie_ptr_get(desc_info);
2391 if (err == 0) {
2392 rocker_port->dev->stats.tx_packets++;
2393 rocker_port->dev->stats.tx_bytes += skb->len;
Scott Feldman4725ceb2015-05-10 09:48:08 -07002394 } else {
David Ahernf2bbca52015-01-16 14:22:29 -07002395 rocker_port->dev->stats.tx_errors++;
Scott Feldman4725ceb2015-05-10 09:48:08 -07002396 }
David Ahernf2bbca52015-01-16 14:22:29 -07002397
2398 dev_kfree_skb_any(skb);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002399 credits++;
2400 }
2401
2402 if (credits && netif_queue_stopped(rocker_port->dev))
2403 netif_wake_queue(rocker_port->dev);
2404
2405 napi_complete(napi);
2406 rocker_dma_ring_credits_set(rocker, &rocker_port->tx_ring, credits);
2407
2408 return 0;
2409}
2410
Simon Hormane5054642015-05-25 14:28:36 +09002411static int rocker_port_rx_proc(const struct rocker *rocker,
2412 const struct rocker_port *rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002413 struct rocker_desc_info *desc_info)
2414{
Simon Hormane5054642015-05-25 14:28:36 +09002415 const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002416 struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
2417 size_t rx_len;
Scott Feldman3f98a8e2015-07-18 18:24:51 -07002418 u16 rx_flags = 0;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002419
2420 if (!skb)
2421 return -ENOENT;
2422
2423 rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
2424 if (!attrs[ROCKER_TLV_RX_FRAG_LEN])
2425 return -EINVAL;
Scott Feldman3f98a8e2015-07-18 18:24:51 -07002426 if (attrs[ROCKER_TLV_RX_FLAGS])
2427 rx_flags = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FLAGS]);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002428
2429 rocker_dma_rx_ring_skb_unmap(rocker, attrs);
2430
2431 rx_len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_LEN]);
2432 skb_put(skb, rx_len);
2433 skb->protocol = eth_type_trans(skb, rocker_port->dev);
David Ahernf2bbca52015-01-16 14:22:29 -07002434
Scott Feldman3f98a8e2015-07-18 18:24:51 -07002435 if (rx_flags & ROCKER_RX_FLAGS_FWD_OFFLOAD)
Ido Schimmel6bc506b2016-08-25 18:42:37 +02002436 skb->offload_fwd_mark = 1;
Scott Feldman3f98a8e2015-07-18 18:24:51 -07002437
David Ahernf2bbca52015-01-16 14:22:29 -07002438 rocker_port->dev->stats.rx_packets++;
2439 rocker_port->dev->stats.rx_bytes += skb->len;
2440
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002441 netif_receive_skb(skb);
2442
Simon Horman534ba6a2015-06-01 13:25:04 +09002443 return rocker_dma_rx_ring_skb_alloc(rocker_port, desc_info);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002444}
2445
2446static struct rocker_port *rocker_port_napi_rx_get(struct napi_struct *napi)
2447{
2448 return container_of(napi, struct rocker_port, napi_rx);
2449}
2450
2451static int rocker_port_poll_rx(struct napi_struct *napi, int budget)
2452{
2453 struct rocker_port *rocker_port = rocker_port_napi_rx_get(napi);
Simon Hormane5054642015-05-25 14:28:36 +09002454 const struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002455 struct rocker_desc_info *desc_info;
2456 u32 credits = 0;
2457 int err;
2458
2459 /* Process rx descriptors */
2460 while (credits < budget &&
2461 (desc_info = rocker_desc_tail_get(&rocker_port->rx_ring))) {
2462 err = rocker_desc_err(desc_info);
2463 if (err) {
2464 if (net_ratelimit())
2465 netdev_err(rocker_port->dev, "rx desc received with err %d\n",
2466 err);
2467 } else {
2468 err = rocker_port_rx_proc(rocker, rocker_port,
2469 desc_info);
2470 if (err && net_ratelimit())
2471 netdev_err(rocker_port->dev, "rx processing failed with err %d\n",
2472 err);
2473 }
David Ahernf2bbca52015-01-16 14:22:29 -07002474 if (err)
2475 rocker_port->dev->stats.rx_errors++;
2476
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002477 rocker_desc_gen_clear(desc_info);
2478 rocker_desc_head_set(rocker, &rocker_port->rx_ring, desc_info);
2479 credits++;
2480 }
2481
2482 if (credits < budget)
2483 napi_complete(napi);
2484
2485 rocker_dma_ring_credits_set(rocker, &rocker_port->rx_ring, credits);
2486
2487 return credits;
2488}
2489
2490/*****************
2491 * PCI driver ops
2492 *****************/
2493
Simon Hormane5054642015-05-25 14:28:36 +09002494static void rocker_carrier_init(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002495{
Simon Hormane5054642015-05-25 14:28:36 +09002496 const struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002497 u64 link_status = rocker_read64(rocker, PORT_PHYS_LINK_STATUS);
2498 bool link_up;
2499
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08002500 link_up = link_status & (1 << rocker_port->pport);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002501 if (link_up)
2502 netif_carrier_on(rocker_port->dev);
2503 else
2504 netif_carrier_off(rocker_port->dev);
2505}
2506
Jiri Pirkoe4201142016-02-16 15:14:45 +01002507static void rocker_remove_ports(struct rocker *rocker)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002508{
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002509 struct rocker_port *rocker_port;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002510 int i;
2511
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002512 for (i = 0; i < rocker->port_count; i++) {
2513 rocker_port = rocker->ports[i];
Scott Feldmana0720312015-06-12 21:09:44 -07002514 if (!rocker_port)
2515 continue;
Jiri Pirkoe4201142016-02-16 15:14:45 +01002516 rocker_world_port_fini(rocker_port);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002517 unregister_netdev(rocker_port->dev);
Jiri Pirkoe4201142016-02-16 15:14:45 +01002518 rocker_world_port_post_fini(rocker_port);
Ido Schimmel1ebd47e2015-08-02 19:29:16 +02002519 free_netdev(rocker_port->dev);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002520 }
Jiri Pirkoe4201142016-02-16 15:14:45 +01002521 rocker_world_fini(rocker);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002522 kfree(rocker->ports);
2523}
2524
Simon Horman534ba6a2015-06-01 13:25:04 +09002525static void rocker_port_dev_addr_init(struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002526{
Simon Horman534ba6a2015-06-01 13:25:04 +09002527 const struct rocker *rocker = rocker_port->rocker;
Simon Hormane5054642015-05-25 14:28:36 +09002528 const struct pci_dev *pdev = rocker->pdev;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002529 int err;
2530
2531 err = rocker_cmd_get_port_settings_macaddr(rocker_port,
2532 rocker_port->dev->dev_addr);
2533 if (err) {
2534 dev_warn(&pdev->dev, "failed to get mac address, using random\n");
2535 eth_hw_addr_random(rocker_port->dev);
2536 }
2537}
2538
2539static int rocker_probe_port(struct rocker *rocker, unsigned int port_number)
2540{
Simon Hormane5054642015-05-25 14:28:36 +09002541 const struct pci_dev *pdev = rocker->pdev;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002542 struct rocker_port *rocker_port;
2543 struct net_device *dev;
2544 int err;
2545
2546 dev = alloc_etherdev(sizeof(struct rocker_port));
2547 if (!dev)
2548 return -ENOMEM;
2549 rocker_port = netdev_priv(dev);
2550 rocker_port->dev = dev;
2551 rocker_port->rocker = rocker;
2552 rocker_port->port_number = port_number;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08002553 rocker_port->pport = port_number + 1;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002554
Jiri Pirkoe4201142016-02-16 15:14:45 +01002555 err = rocker_world_check_init(rocker_port);
2556 if (err) {
2557 dev_err(&pdev->dev, "world init failed\n");
2558 goto err_world_check_init;
2559 }
2560
Simon Horman534ba6a2015-06-01 13:25:04 +09002561 rocker_port_dev_addr_init(rocker_port);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002562 dev->netdev_ops = &rocker_port_netdev_ops;
2563 dev->ethtool_ops = &rocker_port_ethtool_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07002564 dev->switchdev_ops = &rocker_port_switchdev_ops;
Eric Dumazetd64b5e82015-11-18 06:31:00 -08002565 netif_tx_napi_add(dev, &rocker_port->napi_tx, rocker_port_poll_tx,
Jiri Pirko11ce2ba2016-02-16 15:14:41 +01002566 NAPI_POLL_WEIGHT);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002567 netif_napi_add(dev, &rocker_port->napi_rx, rocker_port_poll_rx,
2568 NAPI_POLL_WEIGHT);
2569 rocker_carrier_init(rocker_port);
2570
Ido Schimmel21518a62015-08-02 20:56:37 +02002571 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_SG;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002572
Jiri Pirkoe4201142016-02-16 15:14:45 +01002573 err = rocker_world_port_pre_init(rocker_port);
2574 if (err) {
2575 dev_err(&pdev->dev, "port world pre-init failed\n");
2576 goto err_world_port_pre_init;
2577 }
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002578 err = register_netdev(dev);
2579 if (err) {
2580 dev_err(&pdev->dev, "register_netdev failed\n");
2581 goto err_register_netdev;
2582 }
2583 rocker->ports[port_number] = rocker_port;
2584
Jiri Pirkoe4201142016-02-16 15:14:45 +01002585 err = rocker_world_port_init(rocker_port);
2586 if (err) {
2587 dev_err(&pdev->dev, "port world init failed\n");
2588 goto err_world_port_init;
2589 }
2590
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002591 return 0;
2592
Jiri Pirkoe4201142016-02-16 15:14:45 +01002593err_world_port_init:
Scott Feldman6c4f7782015-08-03 22:31:17 -07002594 rocker->ports[port_number] = NULL;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002595 unregister_netdev(dev);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002596err_register_netdev:
Jiri Pirkoe4201142016-02-16 15:14:45 +01002597 rocker_world_port_post_fini(rocker_port);
2598err_world_port_pre_init:
2599err_world_check_init:
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002600 free_netdev(dev);
2601 return err;
2602}
2603
2604static int rocker_probe_ports(struct rocker *rocker)
2605{
2606 int i;
2607 size_t alloc_size;
2608 int err;
2609
2610 alloc_size = sizeof(struct rocker_port *) * rocker->port_count;
Scott Feldman27b808c2015-06-01 11:39:02 -07002611 rocker->ports = kzalloc(alloc_size, GFP_KERNEL);
Dan Carpentere65ad3b2015-02-25 16:35:32 +03002612 if (!rocker->ports)
2613 return -ENOMEM;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002614 for (i = 0; i < rocker->port_count; i++) {
2615 err = rocker_probe_port(rocker, i);
2616 if (err)
2617 goto remove_ports;
2618 }
2619 return 0;
2620
2621remove_ports:
2622 rocker_remove_ports(rocker);
2623 return err;
2624}
2625
2626static int rocker_msix_init(struct rocker *rocker)
2627{
2628 struct pci_dev *pdev = rocker->pdev;
2629 int msix_entries;
2630 int i;
2631 int err;
2632
2633 msix_entries = pci_msix_vec_count(pdev);
2634 if (msix_entries < 0)
2635 return msix_entries;
2636
2637 if (msix_entries != ROCKER_MSIX_VEC_COUNT(rocker->port_count))
2638 return -EINVAL;
2639
2640 rocker->msix_entries = kmalloc_array(msix_entries,
2641 sizeof(struct msix_entry),
2642 GFP_KERNEL);
2643 if (!rocker->msix_entries)
2644 return -ENOMEM;
2645
2646 for (i = 0; i < msix_entries; i++)
2647 rocker->msix_entries[i].entry = i;
2648
2649 err = pci_enable_msix_exact(pdev, rocker->msix_entries, msix_entries);
2650 if (err < 0)
2651 goto err_enable_msix;
2652
2653 return 0;
2654
2655err_enable_msix:
2656 kfree(rocker->msix_entries);
2657 return err;
2658}
2659
Simon Hormane5054642015-05-25 14:28:36 +09002660static void rocker_msix_fini(const struct rocker *rocker)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002661{
2662 pci_disable_msix(rocker->pdev);
2663 kfree(rocker->msix_entries);
2664}
2665
2666static int rocker_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2667{
2668 struct rocker *rocker;
2669 int err;
2670
2671 rocker = kzalloc(sizeof(*rocker), GFP_KERNEL);
2672 if (!rocker)
2673 return -ENOMEM;
2674
2675 err = pci_enable_device(pdev);
2676 if (err) {
2677 dev_err(&pdev->dev, "pci_enable_device failed\n");
2678 goto err_pci_enable_device;
2679 }
2680
2681 err = pci_request_regions(pdev, rocker_driver_name);
2682 if (err) {
2683 dev_err(&pdev->dev, "pci_request_regions failed\n");
2684 goto err_pci_request_regions;
2685 }
2686
2687 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
2688 if (!err) {
2689 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2690 if (err) {
2691 dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n");
2692 goto err_pci_set_dma_mask;
2693 }
2694 } else {
2695 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2696 if (err) {
2697 dev_err(&pdev->dev, "pci_set_dma_mask failed\n");
2698 goto err_pci_set_dma_mask;
2699 }
2700 }
2701
2702 if (pci_resource_len(pdev, 0) < ROCKER_PCI_BAR0_SIZE) {
2703 dev_err(&pdev->dev, "invalid PCI region size\n");
Wei Yongjun3122a922015-04-16 20:21:02 +08002704 err = -EINVAL;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002705 goto err_pci_resource_len_check;
2706 }
2707
2708 rocker->hw_addr = ioremap(pci_resource_start(pdev, 0),
2709 pci_resource_len(pdev, 0));
2710 if (!rocker->hw_addr) {
2711 dev_err(&pdev->dev, "ioremap failed\n");
2712 err = -EIO;
2713 goto err_ioremap;
2714 }
2715 pci_set_master(pdev);
2716
2717 rocker->pdev = pdev;
2718 pci_set_drvdata(pdev, rocker);
2719
2720 rocker->port_count = rocker_read32(rocker, PORT_PHYS_COUNT);
2721
2722 err = rocker_msix_init(rocker);
2723 if (err) {
2724 dev_err(&pdev->dev, "MSI-X init failed\n");
2725 goto err_msix_init;
2726 }
2727
2728 err = rocker_basic_hw_test(rocker);
2729 if (err) {
2730 dev_err(&pdev->dev, "basic hw test failed\n");
2731 goto err_basic_hw_test;
2732 }
2733
2734 rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
2735
2736 err = rocker_dma_rings_init(rocker);
2737 if (err)
2738 goto err_dma_rings_init;
2739
2740 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD),
2741 rocker_cmd_irq_handler, 0,
2742 rocker_driver_name, rocker);
2743 if (err) {
2744 dev_err(&pdev->dev, "cannot assign cmd irq\n");
2745 goto err_request_cmd_irq;
2746 }
2747
2748 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT),
2749 rocker_event_irq_handler, 0,
2750 rocker_driver_name, rocker);
2751 if (err) {
2752 dev_err(&pdev->dev, "cannot assign event irq\n");
2753 goto err_request_event_irq;
2754 }
2755
2756 rocker->hw.id = rocker_read64(rocker, SWITCH_ID);
2757
2758 err = rocker_probe_ports(rocker);
2759 if (err) {
2760 dev_err(&pdev->dev, "failed to probe ports\n");
2761 goto err_probe_ports;
2762 }
2763
Jiri Pirko936bd482016-09-26 12:52:32 +02002764 rocker->fib_nb.notifier_call = rocker_router_fib_event;
2765 register_fib_notifier(&rocker->fib_nb);
2766
Scott Feldmanc8beb5b2015-08-12 18:44:13 -07002767 dev_info(&pdev->dev, "Rocker switch with id %*phN\n",
2768 (int)sizeof(rocker->hw.id), &rocker->hw.id);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002769
2770 return 0;
2771
2772err_probe_ports:
2773 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
2774err_request_event_irq:
2775 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
2776err_request_cmd_irq:
2777 rocker_dma_rings_fini(rocker);
2778err_dma_rings_init:
2779err_basic_hw_test:
2780 rocker_msix_fini(rocker);
2781err_msix_init:
2782 iounmap(rocker->hw_addr);
2783err_ioremap:
2784err_pci_resource_len_check:
2785err_pci_set_dma_mask:
2786 pci_release_regions(pdev);
2787err_pci_request_regions:
2788 pci_disable_device(pdev);
2789err_pci_enable_device:
2790 kfree(rocker);
2791 return err;
2792}
2793
2794static void rocker_remove(struct pci_dev *pdev)
2795{
2796 struct rocker *rocker = pci_get_drvdata(pdev);
2797
Jiri Pirko936bd482016-09-26 12:52:32 +02002798 unregister_fib_notifier(&rocker->fib_nb);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002799 rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
2800 rocker_remove_ports(rocker);
2801 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
2802 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
2803 rocker_dma_rings_fini(rocker);
2804 rocker_msix_fini(rocker);
2805 iounmap(rocker->hw_addr);
2806 pci_release_regions(rocker->pdev);
2807 pci_disable_device(rocker->pdev);
2808 kfree(rocker);
2809}
2810
2811static struct pci_driver rocker_pci_driver = {
2812 .name = rocker_driver_name,
2813 .id_table = rocker_pci_id_table,
2814 .probe = rocker_probe,
2815 .remove = rocker_remove,
2816};
2817
Scott Feldman6c707942014-11-28 14:34:28 +01002818/************************************
2819 * Net device notifier event handler
2820 ************************************/
2821
Simon Hormane5054642015-05-25 14:28:36 +09002822static bool rocker_port_dev_check(const struct net_device *dev)
Scott Feldman6c707942014-11-28 14:34:28 +01002823{
2824 return dev->netdev_ops == &rocker_port_netdev_ops;
2825}
2826
Jiri Pirko936bd482016-09-26 12:52:32 +02002827static bool rocker_port_dev_check_under(const struct net_device *dev,
2828 struct rocker *rocker)
2829{
2830 struct rocker_port *rocker_port;
2831
2832 if (!rocker_port_dev_check(dev))
2833 return false;
2834
2835 rocker_port = netdev_priv(dev);
2836 if (rocker_port->rocker != rocker)
2837 return false;
2838
2839 return true;
2840}
2841
2842struct rocker_port *rocker_port_dev_lower_find(struct net_device *dev,
2843 struct rocker *rocker)
2844{
2845 struct net_device *lower_dev;
2846 struct list_head *iter;
2847
2848 if (rocker_port_dev_check_under(dev, rocker))
2849 return netdev_priv(dev);
2850
2851 netdev_for_each_all_lower_dev(dev, lower_dev, iter) {
2852 if (rocker_port_dev_check_under(lower_dev, rocker))
2853 return netdev_priv(lower_dev);
2854 }
2855 return NULL;
2856}
2857
Scott Feldman6c707942014-11-28 14:34:28 +01002858static int rocker_netdevice_event(struct notifier_block *unused,
2859 unsigned long event, void *ptr)
2860{
Jiri Pirko686ed302015-08-27 09:31:23 +02002861 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2862 struct netdev_notifier_changeupper_info *info;
2863 struct rocker_port *rocker_port;
Scott Feldman6c707942014-11-28 14:34:28 +01002864 int err;
2865
Jiri Pirko686ed302015-08-27 09:31:23 +02002866 if (!rocker_port_dev_check(dev))
2867 return NOTIFY_DONE;
2868
Scott Feldman6c707942014-11-28 14:34:28 +01002869 switch (event) {
2870 case NETDEV_CHANGEUPPER:
Jiri Pirko686ed302015-08-27 09:31:23 +02002871 info = ptr;
2872 if (!info->master)
2873 goto out;
2874 rocker_port = netdev_priv(dev);
2875 if (info->linking) {
Jiri Pirkoe4201142016-02-16 15:14:45 +01002876 err = rocker_world_port_master_linked(rocker_port,
2877 info->upper_dev);
2878 if (err)
2879 netdev_warn(dev, "failed to reflect master linked (err %d)\n",
2880 err);
Jiri Pirko686ed302015-08-27 09:31:23 +02002881 } else {
Jiri Pirkoe4201142016-02-16 15:14:45 +01002882 err = rocker_world_port_master_unlinked(rocker_port,
2883 info->upper_dev);
2884 if (err)
2885 netdev_warn(dev, "failed to reflect master unlinked (err %d)\n",
2886 err);
Jiri Pirko686ed302015-08-27 09:31:23 +02002887 }
Scott Feldman6c707942014-11-28 14:34:28 +01002888 }
Jiri Pirko686ed302015-08-27 09:31:23 +02002889out:
Scott Feldman6c707942014-11-28 14:34:28 +01002890 return NOTIFY_DONE;
2891}
2892
2893static struct notifier_block rocker_netdevice_nb __read_mostly = {
2894 .notifier_call = rocker_netdevice_event,
2895};
2896
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002897/************************************
2898 * Net event notifier event handler
2899 ************************************/
2900
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002901static int rocker_netevent_event(struct notifier_block *unused,
2902 unsigned long event, void *ptr)
2903{
Jiri Pirkoe4201142016-02-16 15:14:45 +01002904 struct rocker_port *rocker_port;
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002905 struct net_device *dev;
2906 struct neighbour *n = ptr;
2907 int err;
2908
2909 switch (event) {
2910 case NETEVENT_NEIGH_UPDATE:
2911 if (n->tbl != &arp_tbl)
2912 return NOTIFY_DONE;
2913 dev = n->dev;
2914 if (!rocker_port_dev_check(dev))
2915 return NOTIFY_DONE;
Jiri Pirkoe4201142016-02-16 15:14:45 +01002916 rocker_port = netdev_priv(dev);
2917 err = rocker_world_port_neigh_update(rocker_port, n);
2918 if (err)
2919 netdev_warn(dev, "failed to handle neigh update (err %d)\n",
2920 err);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002921 break;
2922 }
2923
2924 return NOTIFY_DONE;
2925}
2926
2927static struct notifier_block rocker_netevent_nb __read_mostly = {
2928 .notifier_call = rocker_netevent_event,
2929};
2930
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002931/***********************
2932 * Module init and exit
2933 ***********************/
2934
2935static int __init rocker_module_init(void)
2936{
Scott Feldman6c707942014-11-28 14:34:28 +01002937 int err;
2938
2939 register_netdevice_notifier(&rocker_netdevice_nb);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002940 register_netevent_notifier(&rocker_netevent_nb);
Scott Feldman6c707942014-11-28 14:34:28 +01002941 err = pci_register_driver(&rocker_pci_driver);
2942 if (err)
2943 goto err_pci_register_driver;
2944 return 0;
2945
2946err_pci_register_driver:
Gilad Ben-Yossefa076e6b2015-06-23 10:52:10 +03002947 unregister_netevent_notifier(&rocker_netevent_nb);
Scott Feldman6c707942014-11-28 14:34:28 +01002948 unregister_netdevice_notifier(&rocker_netdevice_nb);
2949 return err;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002950}
2951
2952static void __exit rocker_module_exit(void)
2953{
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002954 unregister_netevent_notifier(&rocker_netevent_nb);
Scott Feldman6c707942014-11-28 14:34:28 +01002955 unregister_netdevice_notifier(&rocker_netdevice_nb);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01002956 pci_unregister_driver(&rocker_pci_driver);
2957}
2958
2959module_init(rocker_module_init);
2960module_exit(rocker_module_exit);
2961
2962MODULE_LICENSE("GPL v2");
2963MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
2964MODULE_AUTHOR("Scott Feldman <sfeldma@gmail.com>");
2965MODULE_DESCRIPTION("Rocker switch device driver");
2966MODULE_DEVICE_TABLE(pci, rocker_pci_id_table);