xref: /dpdk/drivers/net/bnx2x/bnx2x_ethdev.c (revision 0d09cbc7)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2013-2015 Brocade Communications Systems, Inc.
3  * Copyright (c) 2015-2018 Cavium Inc.
4  * All rights reserved.
5  * www.cavium.com
6  */
7 
8 #include "bnx2x.h"
9 #include "bnx2x_rxtx.h"
10 
11 #include <rte_string_fns.h>
12 #include <rte_dev.h>
13 #include <rte_ethdev_pci.h>
14 #include <rte_alarm.h>
15 
16 /*
17  * The set of PCI devices this driver supports
18  */
19 #define BROADCOM_PCI_VENDOR_ID 0x14E4
20 static const struct rte_pci_id pci_id_bnx2x_map[] = {
21 	{ RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57800) },
22 	{ RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57711) },
23 	{ RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57810) },
24 	{ RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57811) },
25 	{ RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57840_OBS) },
26 	{ RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57840_4_10) },
27 	{ RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57840_2_20) },
28 #ifdef RTE_LIBRTE_BNX2X_MF_SUPPORT
29 	{ RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57810_MF) },
30 	{ RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57811_MF) },
31 	{ RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57840_MF) },
32 #endif
33 	{ .vendor_id = 0, }
34 };
35 
36 static const struct rte_pci_id pci_id_bnx2xvf_map[] = {
37 	{ RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57800_VF) },
38 	{ RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57810_VF) },
39 	{ RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57811_VF) },
40 	{ RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57840_VF) },
41 	{ .vendor_id = 0, }
42 };
43 
44 struct rte_bnx2x_xstats_name_off {
45 	char name[RTE_ETH_XSTATS_NAME_SIZE];
46 	uint32_t offset_hi;
47 	uint32_t offset_lo;
48 };
49 
50 static const struct rte_bnx2x_xstats_name_off bnx2x_xstats_strings[] = {
51 	{"rx_buffer_drops",
52 		offsetof(struct bnx2x_eth_stats, brb_drop_hi),
53 		offsetof(struct bnx2x_eth_stats, brb_drop_lo)},
54 	{"rx_buffer_truncates",
55 		offsetof(struct bnx2x_eth_stats, brb_truncate_hi),
56 		offsetof(struct bnx2x_eth_stats, brb_truncate_lo)},
57 	{"rx_buffer_truncate_discard",
58 		offsetof(struct bnx2x_eth_stats, brb_truncate_discard),
59 		offsetof(struct bnx2x_eth_stats, brb_truncate_discard)},
60 	{"mac_filter_discard",
61 		offsetof(struct bnx2x_eth_stats, mac_filter_discard),
62 		offsetof(struct bnx2x_eth_stats, mac_filter_discard)},
63 	{"no_match_vlan_tag_discard",
64 		offsetof(struct bnx2x_eth_stats, mf_tag_discard),
65 		offsetof(struct bnx2x_eth_stats, mf_tag_discard)},
66 	{"tx_pause",
67 		offsetof(struct bnx2x_eth_stats, pause_frames_sent_hi),
68 		offsetof(struct bnx2x_eth_stats, pause_frames_sent_lo)},
69 	{"rx_pause",
70 		offsetof(struct bnx2x_eth_stats, pause_frames_received_hi),
71 		offsetof(struct bnx2x_eth_stats, pause_frames_received_lo)},
72 	{"tx_priority_flow_control",
73 		offsetof(struct bnx2x_eth_stats, pfc_frames_sent_hi),
74 		offsetof(struct bnx2x_eth_stats, pfc_frames_sent_lo)},
75 	{"rx_priority_flow_control",
76 		offsetof(struct bnx2x_eth_stats, pfc_frames_received_hi),
77 		offsetof(struct bnx2x_eth_stats, pfc_frames_received_lo)}
78 };
79 
80 static int
81 bnx2x_link_update(struct rte_eth_dev *dev)
82 {
83 	struct bnx2x_softc *sc = dev->data->dev_private;
84 	struct rte_eth_link link;
85 
86 	PMD_INIT_FUNC_TRACE(sc);
87 
88 	memset(&link, 0, sizeof(link));
89 	mb();
90 	link.link_speed = sc->link_vars.line_speed;
91 	switch (sc->link_vars.duplex) {
92 		case DUPLEX_FULL:
93 			link.link_duplex = ETH_LINK_FULL_DUPLEX;
94 			break;
95 		case DUPLEX_HALF:
96 			link.link_duplex = ETH_LINK_HALF_DUPLEX;
97 			break;
98 	}
99 	link.link_autoneg = !(dev->data->dev_conf.link_speeds &
100 			ETH_LINK_SPEED_FIXED);
101 	link.link_status = sc->link_vars.link_up;
102 
103 	return rte_eth_linkstatus_set(dev, &link);
104 }
105 
106 static void
107 bnx2x_interrupt_action(struct rte_eth_dev *dev, int intr_cxt)
108 {
109 	struct bnx2x_softc *sc = dev->data->dev_private;
110 	uint32_t link_status;
111 
112 	bnx2x_intr_legacy(sc);
113 
114 	if ((atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO) &&
115 	    !intr_cxt)
116 		bnx2x_periodic_callout(sc);
117 	link_status = REG_RD(sc, sc->link_params.shmem_base +
118 			offsetof(struct shmem_region,
119 				port_mb[sc->link_params.port].link_status));
120 	if ((link_status & LINK_STATUS_LINK_UP) != dev->data->dev_link.link_status)
121 		bnx2x_link_update(dev);
122 }
123 
124 static void
125 bnx2x_interrupt_handler(void *param)
126 {
127 	struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
128 	struct bnx2x_softc *sc = dev->data->dev_private;
129 
130 	PMD_DEBUG_PERIODIC_LOG(INFO, sc, "Interrupt handled");
131 
132 	bnx2x_interrupt_action(dev, 1);
133 	rte_intr_ack(&sc->pci_dev->intr_handle);
134 }
135 
136 static void bnx2x_periodic_start(void *param)
137 {
138 	struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
139 	struct bnx2x_softc *sc = dev->data->dev_private;
140 	int ret = 0;
141 
142 	atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
143 	bnx2x_interrupt_action(dev, 0);
144 	if (IS_PF(sc)) {
145 		ret = rte_eal_alarm_set(BNX2X_SP_TIMER_PERIOD,
146 					bnx2x_periodic_start, (void *)dev);
147 		if (ret) {
148 			PMD_DRV_LOG(ERR, sc, "Unable to start periodic"
149 					     " timer rc %d", ret);
150 		}
151 	}
152 }
153 
154 void bnx2x_periodic_stop(void *param)
155 {
156 	struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
157 	struct bnx2x_softc *sc = dev->data->dev_private;
158 
159 	atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
160 
161 	rte_eal_alarm_cancel(bnx2x_periodic_start, (void *)dev);
162 
163 	PMD_DRV_LOG(DEBUG, sc, "Periodic poll stopped");
164 }
165 
166 /*
167  * Devops - helper functions can be called from user application
168  */
169 
170 static int
171 bnx2x_dev_configure(struct rte_eth_dev *dev)
172 {
173 	struct bnx2x_softc *sc = dev->data->dev_private;
174 	struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
175 
176 	int mp_ncpus = sysconf(_SC_NPROCESSORS_CONF);
177 
178 	PMD_INIT_FUNC_TRACE(sc);
179 
180 	if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
181 		sc->mtu = dev->data->dev_conf.rxmode.max_rx_pkt_len;
182 		dev->data->mtu = sc->mtu;
183 	}
184 
185 	if (dev->data->nb_tx_queues > dev->data->nb_rx_queues) {
186 		PMD_DRV_LOG(ERR, sc, "The number of TX queues is greater than number of RX queues");
187 		return -EINVAL;
188 	}
189 
190 	sc->num_queues = MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
191 	if (sc->num_queues > mp_ncpus) {
192 		PMD_DRV_LOG(ERR, sc, "The number of queues is more than number of CPUs");
193 		return -EINVAL;
194 	}
195 
196 	PMD_DRV_LOG(DEBUG, sc, "num_queues=%d, mtu=%d",
197 		       sc->num_queues, sc->mtu);
198 
199 	/* allocate ilt */
200 	if (bnx2x_alloc_ilt_mem(sc) != 0) {
201 		PMD_DRV_LOG(ERR, sc, "bnx2x_alloc_ilt_mem was failed");
202 		return -ENXIO;
203 	}
204 
205 	bnx2x_dev_rxtx_init_dummy(dev);
206 	return 0;
207 }
208 
209 static int
210 bnx2x_dev_start(struct rte_eth_dev *dev)
211 {
212 	struct bnx2x_softc *sc = dev->data->dev_private;
213 	int ret = 0;
214 
215 	PMD_INIT_FUNC_TRACE(sc);
216 
217 	/* start the periodic callout */
218 	if (IS_PF(sc)) {
219 		if (atomic_load_acq_long(&sc->periodic_flags) ==
220 		    PERIODIC_STOP) {
221 			bnx2x_periodic_start(dev);
222 			PMD_DRV_LOG(DEBUG, sc, "Periodic poll re-started");
223 		}
224 	}
225 
226 	ret = bnx2x_init(sc);
227 	if (ret) {
228 		PMD_DRV_LOG(DEBUG, sc, "bnx2x_init failed (%d)", ret);
229 		return -1;
230 	}
231 
232 	if (IS_PF(sc)) {
233 		rte_intr_callback_register(&sc->pci_dev->intr_handle,
234 				bnx2x_interrupt_handler, (void *)dev);
235 
236 		if (rte_intr_enable(&sc->pci_dev->intr_handle))
237 			PMD_DRV_LOG(ERR, sc, "rte_intr_enable failed");
238 	}
239 
240 	/* Configure the previously stored Multicast address list */
241 	if (IS_VF(sc))
242 		bnx2x_vfpf_set_mcast(sc, sc->mc_addrs, sc->mc_addrs_num);
243 	bnx2x_dev_rxtx_init(dev);
244 
245 	bnx2x_print_device_info(sc);
246 
247 	return ret;
248 }
249 
250 static void
251 bnx2x_dev_stop(struct rte_eth_dev *dev)
252 {
253 	struct bnx2x_softc *sc = dev->data->dev_private;
254 	int ret = 0;
255 
256 	PMD_INIT_FUNC_TRACE(sc);
257 
258 	bnx2x_dev_rxtx_init_dummy(dev);
259 
260 	if (IS_PF(sc)) {
261 		rte_intr_disable(&sc->pci_dev->intr_handle);
262 		rte_intr_callback_unregister(&sc->pci_dev->intr_handle,
263 				bnx2x_interrupt_handler, (void *)dev);
264 
265 		/* stop the periodic callout */
266 		bnx2x_periodic_stop(dev);
267 	}
268 	/* Remove the configured Multicast list
269 	 * Sending NULL for the list of address and the
270 	 * Number is set to 0 denoting DEL_CMD
271 	 */
272 	if (IS_VF(sc))
273 		bnx2x_vfpf_set_mcast(sc, NULL, 0);
274 	ret = bnx2x_nic_unload(sc, UNLOAD_NORMAL, FALSE);
275 	if (ret) {
276 		PMD_DRV_LOG(DEBUG, sc, "bnx2x_nic_unload failed (%d)", ret);
277 		return;
278 	}
279 
280 	return;
281 }
282 
283 static void
284 bnx2x_dev_close(struct rte_eth_dev *dev)
285 {
286 	struct bnx2x_softc *sc = dev->data->dev_private;
287 
288 	PMD_INIT_FUNC_TRACE(sc);
289 
290 	if (IS_VF(sc))
291 		bnx2x_vf_close(sc);
292 
293 	bnx2x_dev_clear_queues(dev);
294 	memset(&(dev->data->dev_link), 0 , sizeof(struct rte_eth_link));
295 
296 	/* free ilt */
297 	bnx2x_free_ilt_mem(sc);
298 }
299 
300 static int
301 bnx2x_promisc_enable(struct rte_eth_dev *dev)
302 {
303 	struct bnx2x_softc *sc = dev->data->dev_private;
304 
305 	PMD_INIT_FUNC_TRACE(sc);
306 	sc->rx_mode = BNX2X_RX_MODE_PROMISC;
307 	if (rte_eth_allmulticast_get(dev->data->port_id) == 1)
308 		sc->rx_mode = BNX2X_RX_MODE_ALLMULTI_PROMISC;
309 	bnx2x_set_rx_mode(sc);
310 
311 	return 0;
312 }
313 
314 static int
315 bnx2x_promisc_disable(struct rte_eth_dev *dev)
316 {
317 	struct bnx2x_softc *sc = dev->data->dev_private;
318 
319 	PMD_INIT_FUNC_TRACE(sc);
320 	sc->rx_mode = BNX2X_RX_MODE_NORMAL;
321 	if (rte_eth_allmulticast_get(dev->data->port_id) == 1)
322 		sc->rx_mode = BNX2X_RX_MODE_ALLMULTI;
323 	bnx2x_set_rx_mode(sc);
324 
325 	return 0;
326 }
327 
328 static int
329 bnx2x_dev_allmulticast_enable(struct rte_eth_dev *dev)
330 {
331 	struct bnx2x_softc *sc = dev->data->dev_private;
332 
333 	PMD_INIT_FUNC_TRACE(sc);
334 	sc->rx_mode = BNX2X_RX_MODE_ALLMULTI;
335 	if (rte_eth_promiscuous_get(dev->data->port_id) == 1)
336 		sc->rx_mode = BNX2X_RX_MODE_ALLMULTI_PROMISC;
337 	bnx2x_set_rx_mode(sc);
338 
339 	return 0;
340 }
341 
342 static int
343 bnx2x_dev_allmulticast_disable(struct rte_eth_dev *dev)
344 {
345 	struct bnx2x_softc *sc = dev->data->dev_private;
346 
347 	PMD_INIT_FUNC_TRACE(sc);
348 	sc->rx_mode = BNX2X_RX_MODE_NORMAL;
349 	if (rte_eth_promiscuous_get(dev->data->port_id) == 1)
350 		sc->rx_mode = BNX2X_RX_MODE_PROMISC;
351 	bnx2x_set_rx_mode(sc);
352 
353 	return 0;
354 }
355 
356 static int
357 bnx2x_dev_set_mc_addr_list(struct rte_eth_dev *dev,
358 		struct rte_ether_addr *mc_addrs, uint32_t mc_addrs_num)
359 {
360 	struct bnx2x_softc *sc = dev->data->dev_private;
361 	int err;
362 	PMD_INIT_FUNC_TRACE(sc);
363 	/* flush previous addresses */
364 	err = bnx2x_vfpf_set_mcast(sc, NULL, 0);
365 	if (err)
366 		return err;
367 	sc->mc_addrs_num = 0;
368 
369 	/* Add new ones */
370 	err = bnx2x_vfpf_set_mcast(sc, mc_addrs, mc_addrs_num);
371 	if (err)
372 		return err;
373 
374 	sc->mc_addrs_num = mc_addrs_num;
375 	memcpy(sc->mc_addrs, mc_addrs, mc_addrs_num * sizeof(*mc_addrs));
376 
377 	return 0;
378 }
379 
380 static int
381 bnx2x_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
382 {
383 	struct bnx2x_softc *sc = dev->data->dev_private;
384 
385 	PMD_INIT_FUNC_TRACE(sc);
386 
387 	return bnx2x_link_update(dev);
388 }
389 
390 static int
391 bnx2xvf_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
392 {
393 	struct bnx2x_softc *sc = dev->data->dev_private;
394 	int ret = 0;
395 
396 	ret = bnx2x_link_update(dev);
397 
398 	bnx2x_check_bull(sc);
399 	if (sc->old_bulletin.valid_bitmap & (1 << CHANNEL_DOWN)) {
400 		PMD_DRV_LOG(ERR, sc, "PF indicated channel is down."
401 				"VF device is no longer operational");
402 		dev->data->dev_link.link_status = ETH_LINK_DOWN;
403 	}
404 
405 	return ret;
406 }
407 
408 static int
409 bnx2x_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
410 {
411 	struct bnx2x_softc *sc = dev->data->dev_private;
412 	uint32_t brb_truncate_discard;
413 	uint64_t brb_drops;
414 	uint64_t brb_truncates;
415 
416 	PMD_INIT_FUNC_TRACE(sc);
417 
418 	bnx2x_stats_handle(sc, STATS_EVENT_UPDATE);
419 
420 	memset(stats, 0, sizeof (struct rte_eth_stats));
421 
422 	stats->ipackets =
423 		HILO_U64(sc->eth_stats.total_unicast_packets_received_hi,
424 				sc->eth_stats.total_unicast_packets_received_lo) +
425 		HILO_U64(sc->eth_stats.total_multicast_packets_received_hi,
426 				sc->eth_stats.total_multicast_packets_received_lo) +
427 		HILO_U64(sc->eth_stats.total_broadcast_packets_received_hi,
428 				sc->eth_stats.total_broadcast_packets_received_lo);
429 
430 	stats->opackets =
431 		HILO_U64(sc->eth_stats.total_unicast_packets_transmitted_hi,
432 				sc->eth_stats.total_unicast_packets_transmitted_lo) +
433 		HILO_U64(sc->eth_stats.total_multicast_packets_transmitted_hi,
434 				sc->eth_stats.total_multicast_packets_transmitted_lo) +
435 		HILO_U64(sc->eth_stats.total_broadcast_packets_transmitted_hi,
436 				sc->eth_stats.total_broadcast_packets_transmitted_lo);
437 
438 	stats->ibytes =
439 		HILO_U64(sc->eth_stats.total_bytes_received_hi,
440 				sc->eth_stats.total_bytes_received_lo);
441 
442 	stats->obytes =
443 		HILO_U64(sc->eth_stats.total_bytes_transmitted_hi,
444 				sc->eth_stats.total_bytes_transmitted_lo);
445 
446 	stats->ierrors =
447 		HILO_U64(sc->eth_stats.error_bytes_received_hi,
448 				sc->eth_stats.error_bytes_received_lo);
449 
450 	stats->oerrors = 0;
451 
452 	stats->rx_nombuf =
453 		HILO_U64(sc->eth_stats.no_buff_discard_hi,
454 				sc->eth_stats.no_buff_discard_lo);
455 
456 	brb_drops =
457 		HILO_U64(sc->eth_stats.brb_drop_hi,
458 			 sc->eth_stats.brb_drop_lo);
459 
460 	brb_truncates =
461 		HILO_U64(sc->eth_stats.brb_truncate_hi,
462 			 sc->eth_stats.brb_truncate_lo);
463 
464 	brb_truncate_discard = sc->eth_stats.brb_truncate_discard;
465 
466 	stats->imissed = brb_drops + brb_truncates +
467 			 brb_truncate_discard + stats->rx_nombuf;
468 
469 	return 0;
470 }
471 
472 static int
473 bnx2x_get_xstats_names(__rte_unused struct rte_eth_dev *dev,
474 		       struct rte_eth_xstat_name *xstats_names,
475 		       __rte_unused unsigned limit)
476 {
477 	unsigned int i, stat_cnt = RTE_DIM(bnx2x_xstats_strings);
478 
479 	if (xstats_names != NULL)
480 		for (i = 0; i < stat_cnt; i++)
481 			strlcpy(xstats_names[i].name,
482 				bnx2x_xstats_strings[i].name,
483 				sizeof(xstats_names[i].name));
484 
485 	return stat_cnt;
486 }
487 
488 static int
489 bnx2x_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
490 		     unsigned int n)
491 {
492 	struct bnx2x_softc *sc = dev->data->dev_private;
493 	unsigned int num = RTE_DIM(bnx2x_xstats_strings);
494 
495 	if (n < num)
496 		return num;
497 
498 	bnx2x_stats_handle(sc, STATS_EVENT_UPDATE);
499 
500 	for (num = 0; num < n; num++) {
501 		if (bnx2x_xstats_strings[num].offset_hi !=
502 		    bnx2x_xstats_strings[num].offset_lo)
503 			xstats[num].value = HILO_U64(
504 					  *(uint32_t *)((char *)&sc->eth_stats +
505 					  bnx2x_xstats_strings[num].offset_hi),
506 					  *(uint32_t *)((char *)&sc->eth_stats +
507 					  bnx2x_xstats_strings[num].offset_lo));
508 		else
509 			xstats[num].value =
510 					  *(uint64_t *)((char *)&sc->eth_stats +
511 					  bnx2x_xstats_strings[num].offset_lo);
512 		xstats[num].id = num;
513 	}
514 
515 	return num;
516 }
517 
518 static int
519 bnx2x_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
520 {
521 	struct bnx2x_softc *sc = dev->data->dev_private;
522 
523 	dev_info->max_rx_queues  = sc->max_rx_queues;
524 	dev_info->max_tx_queues  = sc->max_tx_queues;
525 	dev_info->min_rx_bufsize = BNX2X_MIN_RX_BUF_SIZE;
526 	dev_info->max_rx_pktlen  = BNX2X_MAX_RX_PKT_LEN;
527 	dev_info->max_mac_addrs  = BNX2X_MAX_MAC_ADDRS;
528 	dev_info->speed_capa = ETH_LINK_SPEED_10G | ETH_LINK_SPEED_20G;
529 	dev_info->rx_offload_capa = DEV_RX_OFFLOAD_JUMBO_FRAME;
530 
531 	dev_info->rx_desc_lim.nb_max = MAX_RX_AVAIL;
532 	dev_info->rx_desc_lim.nb_min = MIN_RX_SIZE_NONTPA;
533 	dev_info->rx_desc_lim.nb_mtu_seg_max = 1;
534 	dev_info->tx_desc_lim.nb_max = MAX_TX_AVAIL;
535 
536 	return 0;
537 }
538 
539 static int
540 bnx2x_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
541 		uint32_t index, uint32_t pool)
542 {
543 	struct bnx2x_softc *sc = dev->data->dev_private;
544 
545 	if (sc->mac_ops.mac_addr_add) {
546 		sc->mac_ops.mac_addr_add(dev, mac_addr, index, pool);
547 		return 0;
548 	}
549 	return -ENOTSUP;
550 }
551 
552 static void
553 bnx2x_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
554 {
555 	struct bnx2x_softc *sc = dev->data->dev_private;
556 
557 	if (sc->mac_ops.mac_addr_remove)
558 		sc->mac_ops.mac_addr_remove(dev, index);
559 }
560 
561 static const struct eth_dev_ops bnx2x_eth_dev_ops = {
562 	.dev_configure                = bnx2x_dev_configure,
563 	.dev_start                    = bnx2x_dev_start,
564 	.dev_stop                     = bnx2x_dev_stop,
565 	.dev_close                    = bnx2x_dev_close,
566 	.promiscuous_enable           = bnx2x_promisc_enable,
567 	.promiscuous_disable          = bnx2x_promisc_disable,
568 	.allmulticast_enable          = bnx2x_dev_allmulticast_enable,
569 	.allmulticast_disable         = bnx2x_dev_allmulticast_disable,
570 	.link_update                  = bnx2x_dev_link_update,
571 	.stats_get                    = bnx2x_dev_stats_get,
572 	.xstats_get                   = bnx2x_dev_xstats_get,
573 	.xstats_get_names             = bnx2x_get_xstats_names,
574 	.dev_infos_get                = bnx2x_dev_infos_get,
575 	.rx_queue_setup               = bnx2x_dev_rx_queue_setup,
576 	.rx_queue_release             = bnx2x_dev_rx_queue_release,
577 	.tx_queue_setup               = bnx2x_dev_tx_queue_setup,
578 	.tx_queue_release             = bnx2x_dev_tx_queue_release,
579 	.mac_addr_add                 = bnx2x_mac_addr_add,
580 	.mac_addr_remove              = bnx2x_mac_addr_remove,
581 };
582 
583 /*
584  * dev_ops for virtual function
585  */
586 static const struct eth_dev_ops bnx2xvf_eth_dev_ops = {
587 	.dev_configure                = bnx2x_dev_configure,
588 	.dev_start                    = bnx2x_dev_start,
589 	.dev_stop                     = bnx2x_dev_stop,
590 	.dev_close                    = bnx2x_dev_close,
591 	.promiscuous_enable           = bnx2x_promisc_enable,
592 	.promiscuous_disable          = bnx2x_promisc_disable,
593 	.allmulticast_enable          = bnx2x_dev_allmulticast_enable,
594 	.allmulticast_disable         = bnx2x_dev_allmulticast_disable,
595 	.set_mc_addr_list             = bnx2x_dev_set_mc_addr_list,
596 	.link_update                  = bnx2xvf_dev_link_update,
597 	.stats_get                    = bnx2x_dev_stats_get,
598 	.xstats_get                   = bnx2x_dev_xstats_get,
599 	.xstats_get_names             = bnx2x_get_xstats_names,
600 	.dev_infos_get                = bnx2x_dev_infos_get,
601 	.rx_queue_setup               = bnx2x_dev_rx_queue_setup,
602 	.rx_queue_release             = bnx2x_dev_rx_queue_release,
603 	.tx_queue_setup               = bnx2x_dev_tx_queue_setup,
604 	.tx_queue_release             = bnx2x_dev_tx_queue_release,
605 	.mac_addr_add                 = bnx2x_mac_addr_add,
606 	.mac_addr_remove              = bnx2x_mac_addr_remove,
607 };
608 
609 
610 static int
611 bnx2x_common_dev_init(struct rte_eth_dev *eth_dev, int is_vf)
612 {
613 	int ret = 0;
614 	struct rte_pci_device *pci_dev;
615 	struct rte_pci_addr pci_addr;
616 	struct bnx2x_softc *sc;
617 	static bool adapter_info = true;
618 
619 	/* Extract key data structures */
620 	sc = eth_dev->data->dev_private;
621 	pci_dev = RTE_DEV_TO_PCI(eth_dev->device);
622 	pci_addr = pci_dev->addr;
623 
624 	snprintf(sc->devinfo.name, NAME_SIZE, PCI_SHORT_PRI_FMT ":dpdk-port-%u",
625 		 pci_addr.bus, pci_addr.devid, pci_addr.function,
626 		 eth_dev->data->port_id);
627 
628 	PMD_INIT_FUNC_TRACE(sc);
629 
630 	eth_dev->dev_ops = is_vf ? &bnx2xvf_eth_dev_ops : &bnx2x_eth_dev_ops;
631 
632 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
633 		PMD_DRV_LOG(ERR, sc, "Skipping device init from secondary process");
634 		return 0;
635 	}
636 
637 	rte_eth_copy_pci_info(eth_dev, pci_dev);
638 
639 	sc->pcie_bus    = pci_dev->addr.bus;
640 	sc->pcie_device = pci_dev->addr.devid;
641 
642 	sc->devinfo.vendor_id    = pci_dev->id.vendor_id;
643 	sc->devinfo.device_id    = pci_dev->id.device_id;
644 	sc->devinfo.subvendor_id = pci_dev->id.subsystem_vendor_id;
645 	sc->devinfo.subdevice_id = pci_dev->id.subsystem_device_id;
646 
647 	if (is_vf)
648 		sc->flags = BNX2X_IS_VF_FLAG;
649 
650 	sc->pcie_func = pci_dev->addr.function;
651 	sc->bar[BAR0].base_addr = (void *)pci_dev->mem_resource[0].addr;
652 	if (is_vf)
653 		sc->bar[BAR1].base_addr = (void *)
654 			((uintptr_t)pci_dev->mem_resource[0].addr + PXP_VF_ADDR_DB_START);
655 	else
656 		sc->bar[BAR1].base_addr = pci_dev->mem_resource[2].addr;
657 
658 	assert(sc->bar[BAR0].base_addr);
659 	assert(sc->bar[BAR1].base_addr);
660 
661 	bnx2x_load_firmware(sc);
662 	assert(sc->firmware);
663 
664 	if (eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf & ETH_RSS_NONFRAG_IPV4_UDP)
665 		sc->udp_rss = 1;
666 
667 	sc->rx_budget = BNX2X_RX_BUDGET;
668 	sc->hc_rx_ticks = BNX2X_RX_TICKS;
669 	sc->hc_tx_ticks = BNX2X_TX_TICKS;
670 
671 	sc->interrupt_mode = INTR_MODE_SINGLE_MSIX;
672 	sc->rx_mode = BNX2X_RX_MODE_NORMAL;
673 
674 	sc->pci_dev = pci_dev;
675 	ret = bnx2x_attach(sc);
676 	if (ret) {
677 		PMD_DRV_LOG(ERR, sc, "bnx2x_attach failed (%d)", ret);
678 		return ret;
679 	}
680 
681 	/* Print important adapter info for the user. */
682 	if (adapter_info) {
683 		bnx2x_print_adapter_info(sc);
684 		adapter_info = false;
685 	}
686 
687 	/* schedule periodic poll for slowpath link events */
688 	if (IS_PF(sc)) {
689 		PMD_DRV_LOG(DEBUG, sc, "Scheduling periodic poll for slowpath link events");
690 		ret = rte_eal_alarm_set(BNX2X_SP_TIMER_PERIOD,
691 					bnx2x_periodic_start, (void *)eth_dev);
692 		if (ret) {
693 			PMD_DRV_LOG(ERR, sc, "Unable to start periodic"
694 					     " timer rc %d", ret);
695 			return -EINVAL;
696 		}
697 	}
698 
699 	eth_dev->data->mac_addrs =
700 		(struct rte_ether_addr *)sc->link_params.mac_addr;
701 
702 	if (IS_VF(sc)) {
703 		rte_spinlock_init(&sc->vf2pf_lock);
704 
705 		ret = bnx2x_dma_alloc(sc, sizeof(struct bnx2x_vf_mbx_msg),
706 				      &sc->vf2pf_mbox_mapping, "vf2pf_mbox",
707 				      RTE_CACHE_LINE_SIZE);
708 		if (ret)
709 			goto out;
710 
711 		sc->vf2pf_mbox = (struct bnx2x_vf_mbx_msg *)
712 					 sc->vf2pf_mbox_mapping.vaddr;
713 
714 		ret = bnx2x_dma_alloc(sc, sizeof(struct bnx2x_vf_bulletin),
715 				      &sc->pf2vf_bulletin_mapping, "vf2pf_bull",
716 				      RTE_CACHE_LINE_SIZE);
717 		if (ret)
718 			goto out;
719 
720 		sc->pf2vf_bulletin = (struct bnx2x_vf_bulletin *)
721 					     sc->pf2vf_bulletin_mapping.vaddr;
722 
723 		ret = bnx2x_vf_get_resources(sc, sc->max_tx_queues,
724 					     sc->max_rx_queues);
725 		if (ret)
726 			goto out;
727 	}
728 
729 	return 0;
730 
731 out:
732 	if (IS_PF(sc))
733 		bnx2x_periodic_stop(eth_dev);
734 
735 	return ret;
736 }
737 
738 static int
739 eth_bnx2x_dev_init(struct rte_eth_dev *eth_dev)
740 {
741 	struct bnx2x_softc *sc = eth_dev->data->dev_private;
742 	PMD_INIT_FUNC_TRACE(sc);
743 	return bnx2x_common_dev_init(eth_dev, 0);
744 }
745 
746 static int
747 eth_bnx2xvf_dev_init(struct rte_eth_dev *eth_dev)
748 {
749 	struct bnx2x_softc *sc = eth_dev->data->dev_private;
750 	PMD_INIT_FUNC_TRACE(sc);
751 	return bnx2x_common_dev_init(eth_dev, 1);
752 }
753 
754 static int eth_bnx2x_dev_uninit(struct rte_eth_dev *eth_dev)
755 {
756 	/* mac_addrs must not be freed alone because part of dev_private */
757 	eth_dev->data->mac_addrs = NULL;
758 	return 0;
759 }
760 
761 static struct rte_pci_driver rte_bnx2x_pmd;
762 static struct rte_pci_driver rte_bnx2xvf_pmd;
763 
764 static int eth_bnx2x_pci_probe(struct rte_pci_driver *pci_drv,
765 	struct rte_pci_device *pci_dev)
766 {
767 	if (pci_drv == &rte_bnx2x_pmd)
768 		return rte_eth_dev_pci_generic_probe(pci_dev,
769 				sizeof(struct bnx2x_softc), eth_bnx2x_dev_init);
770 	else if (pci_drv == &rte_bnx2xvf_pmd)
771 		return rte_eth_dev_pci_generic_probe(pci_dev,
772 				sizeof(struct bnx2x_softc), eth_bnx2xvf_dev_init);
773 	else
774 		return -EINVAL;
775 }
776 
777 static int eth_bnx2x_pci_remove(struct rte_pci_device *pci_dev)
778 {
779 	return rte_eth_dev_pci_generic_remove(pci_dev, eth_bnx2x_dev_uninit);
780 }
781 
782 static struct rte_pci_driver rte_bnx2x_pmd = {
783 	.id_table = pci_id_bnx2x_map,
784 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
785 	.probe = eth_bnx2x_pci_probe,
786 	.remove = eth_bnx2x_pci_remove,
787 };
788 
789 /*
790  * virtual function driver struct
791  */
792 static struct rte_pci_driver rte_bnx2xvf_pmd = {
793 	.id_table = pci_id_bnx2xvf_map,
794 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING,
795 	.probe = eth_bnx2x_pci_probe,
796 	.remove = eth_bnx2x_pci_remove,
797 };
798 
799 RTE_PMD_REGISTER_PCI(net_bnx2x, rte_bnx2x_pmd);
800 RTE_PMD_REGISTER_PCI_TABLE(net_bnx2x, pci_id_bnx2x_map);
801 RTE_PMD_REGISTER_KMOD_DEP(net_bnx2x, "* igb_uio | uio_pci_generic | vfio-pci");
802 RTE_PMD_REGISTER_PCI(net_bnx2xvf, rte_bnx2xvf_pmd);
803 RTE_PMD_REGISTER_PCI_TABLE(net_bnx2xvf, pci_id_bnx2xvf_map);
804 RTE_PMD_REGISTER_KMOD_DEP(net_bnx2xvf, "* igb_uio | vfio-pci");
805 RTE_LOG_REGISTER(bnx2x_logtype_init, pmd.net.bnx2x.init, NOTICE);
806 RTE_LOG_REGISTER(bnx2x_logtype_driver, pmd.net.bnx2x.driver, NOTICE);
807