xref: /dpdk/drivers/net/ionic/ionic_ethdev.c (revision ec15c66b9be0f6b293ad2b2b80f2a6c911114770)
1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2  * Copyright(c) 2018-2019 Pensando Systems, Inc. All rights reserved.
3  */
4 
5 #include <rte_pci.h>
6 #include <rte_bus_pci.h>
7 #include <rte_ethdev.h>
8 #include <rte_ethdev_driver.h>
9 #include <rte_malloc.h>
10 #include <rte_ethdev_pci.h>
11 
12 #include "ionic_logs.h"
13 #include "ionic.h"
14 #include "ionic_dev.h"
15 #include "ionic_mac_api.h"
16 #include "ionic_lif.h"
17 #include "ionic_ethdev.h"
18 
19 static int  eth_ionic_dev_init(struct rte_eth_dev *eth_dev, void *init_params);
20 static int  eth_ionic_dev_uninit(struct rte_eth_dev *eth_dev);
21 static int  ionic_dev_info_get(struct rte_eth_dev *eth_dev,
22 	struct rte_eth_dev_info *dev_info);
23 static int  ionic_dev_configure(struct rte_eth_dev *dev);
24 static int  ionic_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
25 static int  ionic_dev_start(struct rte_eth_dev *dev);
26 static void ionic_dev_stop(struct rte_eth_dev *dev);
27 static void ionic_dev_close(struct rte_eth_dev *dev);
28 static int  ionic_dev_set_link_up(struct rte_eth_dev *dev);
29 static int  ionic_dev_set_link_down(struct rte_eth_dev *dev);
30 static int  ionic_dev_link_update(struct rte_eth_dev *eth_dev,
31 	int wait_to_complete);
32 static int  ionic_flow_ctrl_get(struct rte_eth_dev *eth_dev,
33 	struct rte_eth_fc_conf *fc_conf);
34 static int  ionic_flow_ctrl_set(struct rte_eth_dev *eth_dev,
35 	struct rte_eth_fc_conf *fc_conf);
36 
37 int ionic_logtype;
38 
39 static const struct rte_pci_id pci_id_ionic_map[] = {
40 	{ RTE_PCI_DEVICE(IONIC_PENSANDO_VENDOR_ID, IONIC_DEV_ID_ETH_PF) },
41 	{ RTE_PCI_DEVICE(IONIC_PENSANDO_VENDOR_ID, IONIC_DEV_ID_ETH_VF) },
42 	{ RTE_PCI_DEVICE(IONIC_PENSANDO_VENDOR_ID, IONIC_DEV_ID_ETH_MGMT) },
43 	{ .vendor_id = 0, /* sentinel */ },
44 };
45 
46 static const struct eth_dev_ops ionic_eth_dev_ops = {
47 	.dev_infos_get          = ionic_dev_info_get,
48 	.dev_configure          = ionic_dev_configure,
49 	.mtu_set                = ionic_dev_mtu_set,
50 	.dev_start              = ionic_dev_start,
51 	.dev_stop               = ionic_dev_stop,
52 	.dev_close              = ionic_dev_close,
53 	.link_update            = ionic_dev_link_update,
54 	.dev_set_link_up        = ionic_dev_set_link_up,
55 	.dev_set_link_down      = ionic_dev_set_link_down,
56 	.mac_addr_add           = ionic_dev_add_mac,
57 	.mac_addr_remove        = ionic_dev_remove_mac,
58 	.mac_addr_set           = ionic_dev_set_mac,
59 	.vlan_filter_set        = ionic_dev_vlan_filter_set,
60 	.promiscuous_enable     = ionic_dev_promiscuous_enable,
61 	.promiscuous_disable    = ionic_dev_promiscuous_disable,
62 	.allmulticast_enable    = ionic_dev_allmulticast_enable,
63 	.allmulticast_disable   = ionic_dev_allmulticast_disable,
64 	.flow_ctrl_get          = ionic_flow_ctrl_get,
65 	.flow_ctrl_set          = ionic_flow_ctrl_set,
66 };
67 
68 /*
69  * Set device link up, enable tx.
70  */
71 static int
72 ionic_dev_set_link_up(struct rte_eth_dev *eth_dev)
73 {
74 	struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
75 	struct ionic_adapter *adapter = lif->adapter;
76 	struct ionic_dev *idev = &adapter->idev;
77 	int err;
78 
79 	IONIC_PRINT_CALL();
80 
81 	ionic_dev_cmd_port_state(idev, IONIC_PORT_ADMIN_STATE_UP);
82 
83 	err = ionic_dev_cmd_wait_check(idev, IONIC_DEVCMD_TIMEOUT);
84 	if (err) {
85 		IONIC_PRINT(WARNING, "Failed to bring port UP");
86 		return err;
87 	}
88 
89 	return 0;
90 }
91 
92 /*
93  * Set device link down, disable tx.
94  */
95 static int
96 ionic_dev_set_link_down(struct rte_eth_dev *eth_dev)
97 {
98 	struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
99 	struct ionic_adapter *adapter = lif->adapter;
100 	struct ionic_dev *idev = &adapter->idev;
101 	int err;
102 
103 	IONIC_PRINT_CALL();
104 
105 	ionic_dev_cmd_port_state(idev, IONIC_PORT_ADMIN_STATE_DOWN);
106 
107 	err = ionic_dev_cmd_wait_check(idev, IONIC_DEVCMD_TIMEOUT);
108 	if (err) {
109 		IONIC_PRINT(WARNING, "Failed to bring port DOWN");
110 		return err;
111 	}
112 
113 	return 0;
114 }
115 
116 static int
117 ionic_dev_link_update(struct rte_eth_dev *eth_dev,
118 		int wait_to_complete __rte_unused)
119 {
120 	struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
121 	struct ionic_adapter *adapter = lif->adapter;
122 	struct rte_eth_link link;
123 
124 	IONIC_PRINT_CALL();
125 
126 	/* Initialize */
127 	memset(&link, 0, sizeof(link));
128 	link.link_autoneg = ETH_LINK_AUTONEG;
129 
130 	if (!adapter->link_up) {
131 		/* Interface is down */
132 		link.link_status = ETH_LINK_DOWN;
133 		link.link_duplex = ETH_LINK_HALF_DUPLEX;
134 		link.link_speed = ETH_SPEED_NUM_NONE;
135 	} else {
136 		/* Interface is up */
137 		link.link_status = ETH_LINK_UP;
138 		link.link_duplex = ETH_LINK_FULL_DUPLEX;
139 		switch (adapter->link_speed) {
140 		case  10000:
141 			link.link_speed = ETH_SPEED_NUM_10G;
142 			break;
143 		case  25000:
144 			link.link_speed = ETH_SPEED_NUM_25G;
145 			break;
146 		case  40000:
147 			link.link_speed = ETH_SPEED_NUM_40G;
148 			break;
149 		case  50000:
150 			link.link_speed = ETH_SPEED_NUM_50G;
151 			break;
152 		case 100000:
153 			link.link_speed = ETH_SPEED_NUM_100G;
154 			break;
155 		default:
156 			link.link_speed = ETH_SPEED_NUM_NONE;
157 			break;
158 		}
159 	}
160 
161 	return rte_eth_linkstatus_set(eth_dev, &link);
162 }
163 
164 /**
165  * Interrupt handler triggered by NIC for handling
166  * specific interrupt.
167  *
168  * @param param
169  *  The address of parameter registered before.
170  *
171  * @return
172  *  void
173  */
174 static void
175 ionic_dev_interrupt_handler(void *param)
176 {
177 	struct ionic_adapter *adapter = (struct ionic_adapter *)param;
178 	uint32_t i;
179 
180 	IONIC_PRINT(DEBUG, "->");
181 
182 	for (i = 0; i < adapter->nlifs; i++) {
183 		if (adapter->lifs[i])
184 			ionic_notifyq_handler(adapter->lifs[i], -1);
185 	}
186 }
187 
188 static int
189 ionic_dev_mtu_set(struct rte_eth_dev *eth_dev, uint16_t mtu)
190 {
191 	struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
192 	uint32_t max_frame_size;
193 	int err;
194 
195 	IONIC_PRINT_CALL();
196 
197 	/*
198 	 * Note: mtu check against IONIC_MIN_MTU, IONIC_MAX_MTU
199 	 * is done by the the API.
200 	 */
201 
202 	/*
203 	 * Max frame size is MTU + Ethernet header + VLAN + QinQ
204 	 * (plus ETHER_CRC_LEN if the adapter is able to keep CRC)
205 	 */
206 	max_frame_size = mtu + RTE_ETHER_HDR_LEN + 4 + 4;
207 
208 	if (eth_dev->data->dev_conf.rxmode.max_rx_pkt_len < max_frame_size)
209 		return -EINVAL;
210 
211 	err = ionic_lif_change_mtu(lif, mtu);
212 	if (err)
213 		return err;
214 
215 	return 0;
216 }
217 
218 static int
219 ionic_dev_info_get(struct rte_eth_dev *eth_dev,
220 		struct rte_eth_dev_info *dev_info)
221 {
222 	struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
223 	struct ionic_adapter *adapter = lif->adapter;
224 	struct ionic_identity *ident = &adapter->ident;
225 
226 	IONIC_PRINT_CALL();
227 
228 	dev_info->max_rx_queues = (uint16_t)
229 		ident->lif.eth.config.queue_count[IONIC_QTYPE_RXQ];
230 	dev_info->max_tx_queues = (uint16_t)
231 		ident->lif.eth.config.queue_count[IONIC_QTYPE_TXQ];
232 	/* Also add ETHER_CRC_LEN if the adapter is able to keep CRC */
233 	dev_info->min_rx_bufsize = IONIC_MIN_MTU + RTE_ETHER_HDR_LEN;
234 	dev_info->max_rx_pktlen = IONIC_MAX_MTU + RTE_ETHER_HDR_LEN;
235 	dev_info->max_mac_addrs = adapter->max_mac_addrs;
236 	dev_info->min_mtu = IONIC_MIN_MTU;
237 	dev_info->max_mtu = IONIC_MAX_MTU;
238 
239 	dev_info->speed_capa =
240 		ETH_LINK_SPEED_10G |
241 		ETH_LINK_SPEED_25G |
242 		ETH_LINK_SPEED_40G |
243 		ETH_LINK_SPEED_50G |
244 		ETH_LINK_SPEED_100G;
245 
246 	return 0;
247 }
248 
249 static int
250 ionic_flow_ctrl_get(struct rte_eth_dev *eth_dev,
251 		struct rte_eth_fc_conf *fc_conf)
252 {
253 	struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
254 	struct ionic_adapter *adapter = lif->adapter;
255 	struct ionic_dev *idev = &adapter->idev;
256 
257 	if (idev->port_info) {
258 		fc_conf->autoneg = idev->port_info->config.an_enable;
259 
260 		if (idev->port_info->config.pause_type)
261 			fc_conf->mode = RTE_FC_FULL;
262 		else
263 			fc_conf->mode = RTE_FC_NONE;
264 	}
265 
266 	return 0;
267 }
268 
269 static int
270 ionic_flow_ctrl_set(struct rte_eth_dev *eth_dev,
271 		struct rte_eth_fc_conf *fc_conf)
272 {
273 	struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
274 	struct ionic_adapter *adapter = lif->adapter;
275 	struct ionic_dev *idev = &adapter->idev;
276 	uint8_t pause_type = IONIC_PORT_PAUSE_TYPE_NONE;
277 	uint8_t an_enable;
278 
279 	switch (fc_conf->mode) {
280 	case RTE_FC_NONE:
281 		pause_type = IONIC_PORT_PAUSE_TYPE_NONE;
282 		break;
283 	case RTE_FC_FULL:
284 		pause_type = IONIC_PORT_PAUSE_TYPE_LINK;
285 		break;
286 	case RTE_FC_RX_PAUSE:
287 	case RTE_FC_TX_PAUSE:
288 		return -ENOTSUP;
289 	}
290 
291 	an_enable = fc_conf->autoneg;
292 
293 	ionic_dev_cmd_port_pause(idev, pause_type);
294 	ionic_dev_cmd_port_autoneg(idev, an_enable);
295 
296 	return 0;
297 }
298 
299 static int
300 ionic_dev_configure(struct rte_eth_dev *eth_dev)
301 {
302 	struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
303 	int err;
304 
305 	IONIC_PRINT_CALL();
306 
307 	err = ionic_lif_configure(lif);
308 	if (err) {
309 		IONIC_PRINT(ERR, "Cannot configure LIF: %d", err);
310 		return err;
311 	}
312 
313 	return 0;
314 }
315 
316 static inline uint32_t
317 ionic_parse_link_speeds(uint16_t link_speeds)
318 {
319 	if (link_speeds & ETH_LINK_SPEED_100G)
320 		return 100000;
321 	else if (link_speeds & ETH_LINK_SPEED_50G)
322 		return 50000;
323 	else if (link_speeds & ETH_LINK_SPEED_40G)
324 		return 40000;
325 	else if (link_speeds & ETH_LINK_SPEED_25G)
326 		return 25000;
327 	else if (link_speeds & ETH_LINK_SPEED_10G)
328 		return 10000;
329 	else
330 		return 0;
331 }
332 
333 /*
334  * Configure device link speed and setup link.
335  * It returns 0 on success.
336  */
337 static int
338 ionic_dev_start(struct rte_eth_dev *eth_dev)
339 {
340 	struct rte_eth_conf *dev_conf = &eth_dev->data->dev_conf;
341 	struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
342 	struct ionic_adapter *adapter = lif->adapter;
343 	struct ionic_dev *idev = &adapter->idev;
344 	uint32_t allowed_speeds;
345 	int err;
346 
347 	IONIC_PRINT_CALL();
348 
349 	allowed_speeds =
350 		ETH_LINK_SPEED_FIXED |
351 		ETH_LINK_SPEED_10G |
352 		ETH_LINK_SPEED_25G |
353 		ETH_LINK_SPEED_40G |
354 		ETH_LINK_SPEED_50G |
355 		ETH_LINK_SPEED_100G;
356 
357 	if (dev_conf->link_speeds & ~allowed_speeds) {
358 		IONIC_PRINT(ERR, "Invalid link setting");
359 		return -EINVAL;
360 	}
361 
362 	err = ionic_lif_start(lif);
363 	if (err) {
364 		IONIC_PRINT(ERR, "Cannot start LIF: %d", err);
365 		return err;
366 	}
367 
368 	if (eth_dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED) {
369 		uint32_t speed = ionic_parse_link_speeds(dev_conf->link_speeds);
370 
371 		if (speed)
372 			ionic_dev_cmd_port_speed(idev, speed);
373 	}
374 
375 	ionic_dev_link_update(eth_dev, 0);
376 
377 	return 0;
378 }
379 
380 /*
381  * Stop device: disable rx and tx functions to allow for reconfiguring.
382  */
383 static void
384 ionic_dev_stop(struct rte_eth_dev *eth_dev)
385 {
386 	struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
387 	int err;
388 
389 	IONIC_PRINT_CALL();
390 
391 	err = ionic_lif_stop(lif);
392 	if (err)
393 		IONIC_PRINT(ERR, "Cannot stop LIF: %d", err);
394 }
395 
396 /*
397  * Reset and stop device.
398  */
399 static void
400 ionic_dev_close(struct rte_eth_dev *eth_dev)
401 {
402 	struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
403 	int err;
404 
405 	IONIC_PRINT_CALL();
406 
407 	err = ionic_lif_stop(lif);
408 	if (err) {
409 		IONIC_PRINT(ERR, "Cannot stop LIF: %d", err);
410 		return;
411 	}
412 
413 	err = eth_ionic_dev_uninit(eth_dev);
414 	if (err) {
415 		IONIC_PRINT(ERR, "Cannot destroy LIF: %d", err);
416 		return;
417 	}
418 }
419 
420 static int
421 eth_ionic_dev_init(struct rte_eth_dev *eth_dev, void *init_params)
422 {
423 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
424 	struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
425 	struct ionic_adapter *adapter = (struct ionic_adapter *)init_params;
426 	int err;
427 
428 	IONIC_PRINT_CALL();
429 
430 	eth_dev->dev_ops = &ionic_eth_dev_ops;
431 
432 	/* Multi-process not supported, primary does initialization anyway */
433 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
434 		return 0;
435 
436 	rte_eth_copy_pci_info(eth_dev, pci_dev);
437 
438 	lif->index = adapter->nlifs;
439 	lif->eth_dev = eth_dev;
440 	lif->adapter = adapter;
441 	adapter->lifs[adapter->nlifs] = lif;
442 
443 	IONIC_PRINT(DEBUG, "Up to %u MAC addresses supported",
444 		adapter->max_mac_addrs);
445 
446 	/* Allocate memory for storing MAC addresses */
447 	eth_dev->data->mac_addrs = rte_zmalloc("ionic",
448 		RTE_ETHER_ADDR_LEN * adapter->max_mac_addrs, 0);
449 
450 	if (eth_dev->data->mac_addrs == NULL) {
451 		IONIC_PRINT(ERR, "Failed to allocate %u bytes needed to "
452 			"store MAC addresses",
453 			RTE_ETHER_ADDR_LEN * adapter->max_mac_addrs);
454 		err = -ENOMEM;
455 		goto err;
456 	}
457 
458 	err = ionic_lif_alloc(lif);
459 	if (err) {
460 		IONIC_PRINT(ERR, "Cannot allocate LIFs: %d, aborting",
461 			err);
462 		goto err;
463 	}
464 
465 	err = ionic_lif_init(lif);
466 	if (err) {
467 		IONIC_PRINT(ERR, "Cannot init LIFs: %d, aborting", err);
468 		goto err_free_lif;
469 	}
470 
471 	/* Copy the MAC address */
472 	rte_ether_addr_copy((struct rte_ether_addr *)lif->mac_addr,
473 		&eth_dev->data->mac_addrs[0]);
474 
475 	IONIC_PRINT(DEBUG, "Port %u initialized", eth_dev->data->port_id);
476 
477 	return 0;
478 
479 err_free_lif:
480 	ionic_lif_free(lif);
481 err:
482 	return err;
483 }
484 
485 static int
486 eth_ionic_dev_uninit(struct rte_eth_dev *eth_dev)
487 {
488 	struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
489 	struct ionic_adapter *adapter = lif->adapter;
490 
491 	IONIC_PRINT_CALL();
492 
493 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
494 		return 0;
495 
496 	adapter->lifs[lif->index] = NULL;
497 
498 	ionic_lif_deinit(lif);
499 	ionic_lif_free(lif);
500 
501 	eth_dev->dev_ops = NULL;
502 
503 	return 0;
504 }
505 
506 static int
507 ionic_configure_intr(struct ionic_adapter *adapter)
508 {
509 	struct rte_pci_device *pci_dev = adapter->pci_dev;
510 	struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
511 	int err;
512 
513 	IONIC_PRINT(DEBUG, "Configuring %u intrs", adapter->nintrs);
514 
515 	if (rte_intr_efd_enable(intr_handle, adapter->nintrs)) {
516 		IONIC_PRINT(ERR, "Fail to create eventfd");
517 		return -1;
518 	}
519 
520 	if (rte_intr_dp_is_en(intr_handle))
521 		IONIC_PRINT(DEBUG,
522 			"Packet I/O interrupt on datapath is enabled");
523 
524 	if (!intr_handle->intr_vec) {
525 		intr_handle->intr_vec = rte_zmalloc("intr_vec",
526 			adapter->nintrs * sizeof(int), 0);
527 
528 		if (!intr_handle->intr_vec) {
529 			IONIC_PRINT(ERR, "Failed to allocate %u vectors",
530 				adapter->nintrs);
531 			return -ENOMEM;
532 		}
533 	}
534 
535 	err = rte_intr_callback_register(intr_handle,
536 		ionic_dev_interrupt_handler,
537 		adapter);
538 
539 	if (err) {
540 		IONIC_PRINT(ERR,
541 			"Failure registering interrupts handler (%d)",
542 			err);
543 		return err;
544 	}
545 
546 	/* enable intr mapping */
547 	err = rte_intr_enable(intr_handle);
548 
549 	if (err) {
550 		IONIC_PRINT(ERR, "Failure enabling interrupts (%d)", err);
551 		return err;
552 	}
553 
554 	return 0;
555 }
556 
557 static void
558 ionic_unconfigure_intr(struct ionic_adapter *adapter)
559 {
560 	struct rte_pci_device *pci_dev = adapter->pci_dev;
561 	struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
562 
563 	rte_intr_disable(intr_handle);
564 
565 	rte_intr_callback_unregister(intr_handle,
566 		ionic_dev_interrupt_handler,
567 		adapter);
568 }
569 
570 static int
571 eth_ionic_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
572 		struct rte_pci_device *pci_dev)
573 {
574 	char name[RTE_ETH_NAME_MAX_LEN];
575 	struct rte_mem_resource *resource;
576 	struct ionic_adapter *adapter;
577 	struct ionic_hw *hw;
578 	unsigned long i;
579 	int err;
580 
581 	/* Check structs (trigger error at compilation time) */
582 	ionic_struct_size_checks();
583 
584 	/* Multi-process not supported */
585 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
586 		err = -EPERM;
587 		goto err;
588 	}
589 
590 	IONIC_PRINT(DEBUG, "Initializing device %s",
591 		pci_dev->device.name);
592 
593 	adapter = rte_zmalloc("ionic", sizeof(*adapter), 0);
594 	if (!adapter) {
595 		IONIC_PRINT(ERR, "OOM");
596 		err = -ENOMEM;
597 		goto err;
598 	}
599 
600 	adapter->pci_dev = pci_dev;
601 	hw = &adapter->hw;
602 
603 	hw->device_id = pci_dev->id.device_id;
604 	hw->vendor_id = pci_dev->id.vendor_id;
605 
606 	err = ionic_init_mac(hw);
607 	if (err != 0) {
608 		IONIC_PRINT(ERR, "Mac init failed: %d", err);
609 		err = -EIO;
610 		goto err_free_adapter;
611 	}
612 
613 	adapter->is_mgmt_nic = (pci_dev->id.device_id == IONIC_DEV_ID_ETH_MGMT);
614 
615 	adapter->num_bars = 0;
616 	for (i = 0; i < PCI_MAX_RESOURCE && i < IONIC_BARS_MAX; i++) {
617 		resource = &pci_dev->mem_resource[i];
618 		if (resource->phys_addr == 0 || resource->len == 0)
619 			continue;
620 		adapter->bars[adapter->num_bars].vaddr = resource->addr;
621 		adapter->bars[adapter->num_bars].bus_addr = resource->phys_addr;
622 		adapter->bars[adapter->num_bars].len = resource->len;
623 		adapter->num_bars++;
624 	}
625 
626 	/* Discover ionic dev resources */
627 
628 	err = ionic_setup(adapter);
629 	if (err) {
630 		IONIC_PRINT(ERR, "Cannot setup device: %d, aborting", err);
631 		goto err_free_adapter;
632 	}
633 
634 	err = ionic_identify(adapter);
635 	if (err) {
636 		IONIC_PRINT(ERR, "Cannot identify device: %d, aborting",
637 			err);
638 		goto err_free_adapter;
639 	}
640 
641 	err = ionic_init(adapter);
642 	if (err) {
643 		IONIC_PRINT(ERR, "Cannot init device: %d, aborting", err);
644 		goto err_free_adapter;
645 	}
646 
647 	/* Configure the ports */
648 	err = ionic_port_identify(adapter);
649 	if (err) {
650 		IONIC_PRINT(ERR, "Cannot identify port: %d, aborting",
651 			err);
652 		goto err_free_adapter;
653 	}
654 
655 	err = ionic_port_init(adapter);
656 	if (err) {
657 		IONIC_PRINT(ERR, "Cannot init port: %d, aborting", err);
658 		goto err_free_adapter;
659 	}
660 
661 	/* Configure LIFs */
662 	err = ionic_lif_identify(adapter);
663 	if (err) {
664 		IONIC_PRINT(ERR, "Cannot identify lif: %d, aborting", err);
665 		goto err_free_adapter;
666 	}
667 
668 	/* Allocate and init LIFs */
669 	err = ionic_lifs_size(adapter);
670 	if (err) {
671 		IONIC_PRINT(ERR, "Cannot size LIFs: %d, aborting", err);
672 		goto err_free_adapter;
673 	}
674 
675 	adapter->max_mac_addrs = adapter->ident.lif.eth.max_ucast_filters;
676 
677 	adapter->nlifs = 0;
678 	for (i = 0; i < adapter->ident.dev.nlifs; i++) {
679 		snprintf(name, sizeof(name), "net_%s_lif_%lu",
680 			pci_dev->device.name, i);
681 
682 		err = rte_eth_dev_create(&pci_dev->device, name,
683 			sizeof(struct ionic_lif),
684 			NULL, NULL,
685 			eth_ionic_dev_init, adapter);
686 		if (err) {
687 			IONIC_PRINT(ERR, "Cannot create eth device for "
688 				"ionic lif %s", name);
689 			break;
690 		}
691 
692 		adapter->nlifs++;
693 	}
694 
695 	err = ionic_configure_intr(adapter);
696 
697 	if (err) {
698 		IONIC_PRINT(ERR, "Failed to configure interrupts");
699 		goto err_free_adapter;
700 	}
701 
702 	return 0;
703 
704 err_free_adapter:
705 	rte_free(adapter);
706 err:
707 	return err;
708 }
709 
710 static int
711 eth_ionic_pci_remove(struct rte_pci_device *pci_dev __rte_unused)
712 {
713 	char name[RTE_ETH_NAME_MAX_LEN];
714 	struct ionic_adapter *adapter = NULL;
715 	struct rte_eth_dev *eth_dev;
716 	struct ionic_lif *lif;
717 	uint32_t i;
718 
719 	/* Adapter lookup is using (the first) eth_dev name */
720 	snprintf(name, sizeof(name), "net_%s_lif_0",
721 		pci_dev->device.name);
722 
723 	eth_dev = rte_eth_dev_allocated(name);
724 	if (eth_dev) {
725 		lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
726 		adapter = lif->adapter;
727 	}
728 
729 	if (adapter) {
730 		ionic_unconfigure_intr(adapter);
731 
732 		for (i = 0; i < adapter->nlifs; i++) {
733 			lif = adapter->lifs[i];
734 			rte_eth_dev_destroy(lif->eth_dev, eth_ionic_dev_uninit);
735 		}
736 
737 		rte_free(adapter);
738 	}
739 
740 	return 0;
741 }
742 
743 static struct rte_pci_driver rte_ionic_pmd = {
744 	.id_table = pci_id_ionic_map,
745 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
746 	.probe = eth_ionic_pci_probe,
747 	.remove = eth_ionic_pci_remove,
748 };
749 
750 RTE_PMD_REGISTER_PCI(net_ionic, rte_ionic_pmd);
751 RTE_PMD_REGISTER_PCI_TABLE(net_ionic, pci_id_ionic_map);
752 RTE_PMD_REGISTER_KMOD_DEP(net_ionic, "* igb_uio | uio_pci_generic | vfio-pci");
753 
754 RTE_INIT(ionic_init_log)
755 {
756 	ionic_logtype = rte_log_register("pmd.net.ionic");
757 	if (ionic_logtype >= 0)
758 		rte_log_set_level(ionic_logtype, RTE_LOG_NOTICE);
759 }
760