1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
3 * Copyright 2010-2016 Freescale Semiconductor Inc.
4 * Copyright 2017-2020 NXP
12 /* This header declares the driver interface we implement */
15 #include <rte_malloc.h>
16 #include <rte_dpaa_logs.h>
17 #include <rte_string_fns.h>
19 #define QMI_PORT_REGS_OFFSET 0x400
21 /* CCSR map address to access ccsr based register */
23 /* fman version info */
26 u32 fman_dealloc_bufs_mask_hi;
27 u32 fman_dealloc_bufs_mask_lo;
29 int fman_ccsr_map_fd = -1;
30 static COMPAT_LIST_HEAD(__ifs);
32 /* This is the (const) global variable that callers have read-only access to.
33 * Internally, we have read-write access directly to __ifs.
35 const struct list_head *fman_if_list = &__ifs;
38 if_destructor(struct __fman_if *__if)
40 struct fman_if_bpool *bp, *tmpbp;
45 if (__if->__if.mac_type == fman_offline)
48 list_for_each_entry_safe(bp, tmpbp, &__if->__if.bpool_list, node) {
57 fman_get_ip_rev(const struct device_node *fman_node)
59 const uint32_t *fman_addr;
65 fman_addr = of_get_address(fman_node, 0, ®s_size, NULL);
67 pr_err("of_get_address cannot return fman address\n");
70 phys_addr = of_translate_address(fman_node, fman_addr);
72 pr_err("of_translate_address failed\n");
75 fman_ccsr_map = mmap(NULL, regs_size, PROT_READ | PROT_WRITE,
76 MAP_SHARED, fman_ccsr_map_fd, phys_addr);
77 if (fman_ccsr_map == MAP_FAILED) {
78 pr_err("Can not map FMan ccsr base");
82 ip_rev_1 = in_be32(fman_ccsr_map + FMAN_IP_REV_1);
83 fman_ip_rev = (ip_rev_1 & FMAN_IP_REV_1_MAJOR_MASK) >>
84 FMAN_IP_REV_1_MAJOR_SHIFT;
86 _errno = munmap(fman_ccsr_map, regs_size);
88 pr_err("munmap() of FMan ccsr failed");
94 fman_get_mac_index(uint64_t regs_addr_host, uint8_t *mac_idx)
110 switch (regs_addr_host) {
148 static void fman_if_vsp_init(struct __fman_if *__if)
152 const struct device_node *dev;
154 const uint8_t mac_idx[] = {-1, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1};
156 if (__if->__if.mac_type == fman_mac_1g) {
157 for_each_compatible_node(dev, NULL,
158 "fsl,fman-port-1g-rx-extended-args") {
159 prop = of_get_property(dev, "cell-index", &lenp);
161 cell_index = of_read_number(
163 lenp / sizeof(phandle));
164 if (cell_index == mac_idx[__if->__if.mac_idx]) {
165 prop = of_get_property(
167 "vsp-window", &lenp);
169 __if->__if.num_profiles =
172 __if->__if.base_profile_id =
179 } else if (__if->__if.mac_type == fman_mac_10g) {
180 for_each_compatible_node(dev, NULL,
181 "fsl,fman-port-10g-rx-extended-args") {
182 prop = of_get_property(dev, "cell-index", &lenp);
184 cell_index = of_read_number(
185 &prop[0], lenp / sizeof(phandle));
186 if (cell_index == mac_idx[__if->__if.mac_idx]) {
187 prop = of_get_property(
188 dev, "vsp-window", &lenp);
190 __if->__if.num_profiles =
193 __if->__if.base_profile_id =
204 fman_if_init(const struct device_node *dpa_node)
206 const char *rprop, *mprop;
208 struct __fman_if *__if;
209 struct fman_if_bpool *bpool;
211 const phandle *mac_phandle, *ports_phandle, *pools_phandle;
212 const phandle *tx_channel_id = NULL, *mac_addr, *cell_idx;
213 const phandle *rx_phandle, *tx_phandle;
214 uint64_t tx_phandle_host[4] = {0};
215 uint64_t rx_phandle_host[4] = {0};
216 uint64_t regs_addr_host = 0;
217 uint64_t cell_idx_host = 0;
219 const struct device_node *mac_node = NULL, *tx_node;
220 const struct device_node *pool_node, *fman_node, *rx_node;
221 const uint32_t *regs_addr = NULL;
222 const char *mname, *fname;
223 const char *dname = dpa_node->full_name;
225 int _errno, is_shared = 0;
226 const char *char_prop;
229 if (of_device_is_available(dpa_node) == false)
232 if (!of_device_is_compatible(dpa_node, "fsl,dpa-ethernet-init") &&
233 !of_device_is_compatible(dpa_node, "fsl,dpa-ethernet-shared")) {
237 if (of_device_is_compatible(dpa_node, "fsl,dpa-ethernet-shared"))
240 rprop = "fsl,qman-frame-queues-rx";
241 mprop = "fsl,fman-mac";
243 /* Allocate an object for this network interface */
244 __if = rte_malloc(NULL, sizeof(*__if), RTE_CACHE_LINE_SIZE);
246 FMAN_ERR(-ENOMEM, "malloc(%zu)\n", sizeof(*__if));
249 memset(__if, 0, sizeof(*__if));
250 INIT_LIST_HEAD(&__if->__if.bpool_list);
251 strlcpy(__if->node_name, dpa_node->name, IF_NAME_MAX_LEN - 1);
252 __if->node_name[IF_NAME_MAX_LEN - 1] = '\0';
253 strlcpy(__if->node_path, dpa_node->full_name, PATH_MAX - 1);
254 __if->node_path[PATH_MAX - 1] = '\0';
256 /* Obtain the MAC node used by this interface except macless */
257 mac_phandle = of_get_property(dpa_node, mprop, &lenp);
259 FMAN_ERR(-EINVAL, "%s: no %s\n", dname, mprop);
262 assert(lenp == sizeof(phandle));
263 mac_node = of_find_node_by_phandle(*mac_phandle);
265 FMAN_ERR(-ENXIO, "%s: bad 'fsl,fman-mac\n", dname);
268 mname = mac_node->full_name;
270 /* Map the CCSR regs for the MAC node */
271 regs_addr = of_get_address(mac_node, 0, &__if->regs_size, NULL);
273 FMAN_ERR(-EINVAL, "of_get_address(%s)\n", mname);
276 phys_addr = of_translate_address(mac_node, regs_addr);
278 FMAN_ERR(-EINVAL, "of_translate_address(%s, %p)\n",
282 __if->ccsr_map = mmap(NULL, __if->regs_size,
283 PROT_READ | PROT_WRITE, MAP_SHARED,
284 fman_ccsr_map_fd, phys_addr);
285 if (__if->ccsr_map == MAP_FAILED) {
286 FMAN_ERR(-errno, "mmap(0x%"PRIx64")\n", phys_addr);
289 na = of_n_addr_cells(mac_node);
290 /* Get rid of endianness (issues). Convert to host byte order */
291 regs_addr_host = of_read_number(regs_addr, na);
294 /* Get the index of the Fman this i/f belongs to */
295 fman_node = of_get_parent(mac_node);
296 na = of_n_addr_cells(mac_node);
298 FMAN_ERR(-ENXIO, "of_get_parent(%s)\n", mname);
301 fname = fman_node->full_name;
302 cell_idx = of_get_property(fman_node, "cell-index", &lenp);
304 FMAN_ERR(-ENXIO, "%s: no cell-index)\n", fname);
307 assert(lenp == sizeof(*cell_idx));
308 cell_idx_host = of_read_number(cell_idx, lenp / sizeof(phandle));
309 __if->__if.fman_idx = cell_idx_host;
311 _errno = fman_get_ip_rev(fman_node);
313 FMAN_ERR(-ENXIO, "%s: ip_rev is not available\n",
319 if (fman_ip_rev >= FMAN_V3) {
321 * Set A2V, OVOM, EBD bits in contextA to allow external
322 * buffer deallocation by fman.
324 fman_dealloc_bufs_mask_hi = FMAN_V3_CONTEXTA_EN_A2V |
325 FMAN_V3_CONTEXTA_EN_OVOM;
326 fman_dealloc_bufs_mask_lo = FMAN_V3_CONTEXTA_EN_EBD;
328 fman_dealloc_bufs_mask_hi = 0;
329 fman_dealloc_bufs_mask_lo = 0;
331 /* Is the MAC node 1G, 2.5G, 10G? */
332 __if->__if.is_memac = 0;
334 if (of_device_is_compatible(mac_node, "fsl,fman-1g-mac"))
335 __if->__if.mac_type = fman_mac_1g;
336 else if (of_device_is_compatible(mac_node, "fsl,fman-10g-mac"))
337 __if->__if.mac_type = fman_mac_10g;
338 else if (of_device_is_compatible(mac_node, "fsl,fman-memac")) {
339 __if->__if.is_memac = 1;
340 char_prop = of_get_property(mac_node, "phy-connection-type",
343 printf("memac: unknown MII type assuming 1G\n");
344 /* Right now forcing memac to 1g in case of error*/
345 __if->__if.mac_type = fman_mac_1g;
347 if (strstr(char_prop, "sgmii-2500"))
348 __if->__if.mac_type = fman_mac_2_5g;
349 else if (strstr(char_prop, "sgmii"))
350 __if->__if.mac_type = fman_mac_1g;
351 else if (strstr(char_prop, "rgmii")) {
352 __if->__if.mac_type = fman_mac_1g;
353 __if->__if.is_rgmii = 1;
354 } else if (strstr(char_prop, "xgmii"))
355 __if->__if.mac_type = fman_mac_10g;
358 FMAN_ERR(-EINVAL, "%s: unknown MAC type\n", mname);
363 * For MAC ports, we cannot rely on cell-index. In
364 * T2080, two of the 10G ports on single FMAN have same
365 * duplicate cell-indexes as the other two 10G ports on
366 * same FMAN. Hence, we now rely upon addresses of the
367 * ports from device tree to deduce the index.
370 _errno = fman_get_mac_index(regs_addr_host, &__if->__if.mac_idx);
372 FMAN_ERR(-EINVAL, "Invalid register address: %" PRIx64,
377 /* Extract the MAC address for private and shared interfaces */
378 mac_addr = of_get_property(mac_node, "local-mac-address",
381 FMAN_ERR(-EINVAL, "%s: no local-mac-address\n",
385 memcpy(&__if->__if.mac_addr, mac_addr, ETHER_ADDR_LEN);
387 /* Extract the Tx port (it's the second of the two port handles)
388 * and get its channel ID
390 ports_phandle = of_get_property(mac_node, "fsl,port-handles",
393 ports_phandle = of_get_property(mac_node, "fsl,fman-ports",
395 if (!ports_phandle) {
396 FMAN_ERR(-EINVAL, "%s: no fsl,port-handles\n",
400 assert(lenp == (2 * sizeof(phandle)));
401 tx_node = of_find_node_by_phandle(ports_phandle[1]);
403 FMAN_ERR(-ENXIO, "%s: bad fsl,port-handle[1]\n", mname);
406 /* Extract the channel ID (from tx-port-handle) */
407 tx_channel_id = of_get_property(tx_node, "fsl,qman-channel-id",
409 if (!tx_channel_id) {
410 FMAN_ERR(-EINVAL, "%s: no fsl-qman-channel-id\n",
415 rx_node = of_find_node_by_phandle(ports_phandle[0]);
417 FMAN_ERR(-ENXIO, "%s: bad fsl,port-handle[0]\n", mname);
420 regs_addr = of_get_address(rx_node, 0, &__if->regs_size, NULL);
422 FMAN_ERR(-EINVAL, "of_get_address(%s)\n", mname);
425 phys_addr = of_translate_address(rx_node, regs_addr);
427 FMAN_ERR(-EINVAL, "of_translate_address(%s, %p)\n",
431 __if->bmi_map = mmap(NULL, __if->regs_size,
432 PROT_READ | PROT_WRITE, MAP_SHARED,
433 fman_ccsr_map_fd, phys_addr);
434 if (__if->bmi_map == MAP_FAILED) {
435 FMAN_ERR(-errno, "mmap(0x%"PRIx64")\n", phys_addr);
439 /* No channel ID for MAC-less */
440 assert(lenp == sizeof(*tx_channel_id));
441 na = of_n_addr_cells(mac_node);
442 __if->__if.tx_channel_id = of_read_number(tx_channel_id, na);
444 /* Extract the Rx FQIDs. (Note, the device representation is silly,
445 * there are "counts" that must always be 1.)
447 rx_phandle = of_get_property(dpa_node, rprop, &lenp);
449 FMAN_ERR(-EINVAL, "%s: no fsl,qman-frame-queues-rx\n", dname);
453 assert(lenp >= (4 * sizeof(phandle)));
455 na = of_n_addr_cells(mac_node);
456 /* Get rid of endianness (issues). Convert to host byte order */
457 rx_phandle_host[0] = of_read_number(&rx_phandle[0], na);
458 rx_phandle_host[1] = of_read_number(&rx_phandle[1], na);
459 rx_phandle_host[2] = of_read_number(&rx_phandle[2], na);
460 rx_phandle_host[3] = of_read_number(&rx_phandle[3], na);
462 assert((rx_phandle_host[1] == 1) && (rx_phandle_host[3] == 1));
463 __if->__if.fqid_rx_err = rx_phandle_host[0];
464 __if->__if.fqid_rx_def = rx_phandle_host[2];
466 /* Extract the Tx FQIDs */
467 tx_phandle = of_get_property(dpa_node,
468 "fsl,qman-frame-queues-tx", &lenp);
470 FMAN_ERR(-EINVAL, "%s: no fsl,qman-frame-queues-tx\n", dname);
474 assert(lenp >= (4 * sizeof(phandle)));
475 /*TODO: Fix for other cases also */
476 na = of_n_addr_cells(mac_node);
477 /* Get rid of endianness (issues). Convert to host byte order */
478 tx_phandle_host[0] = of_read_number(&tx_phandle[0], na);
479 tx_phandle_host[1] = of_read_number(&tx_phandle[1], na);
480 tx_phandle_host[2] = of_read_number(&tx_phandle[2], na);
481 tx_phandle_host[3] = of_read_number(&tx_phandle[3], na);
482 assert((tx_phandle_host[1] == 1) && (tx_phandle_host[3] == 1));
483 __if->__if.fqid_tx_err = tx_phandle_host[0];
484 __if->__if.fqid_tx_confirm = tx_phandle_host[2];
486 /* Obtain the buffer pool nodes used by this interface */
487 pools_phandle = of_get_property(dpa_node, "fsl,bman-buffer-pools",
489 if (!pools_phandle) {
490 FMAN_ERR(-EINVAL, "%s: no fsl,bman-buffer-pools\n", dname);
493 /* For each pool, parse the corresponding node and add a pool object
494 * to the interface's "bpool_list"
496 assert(lenp && !(lenp % sizeof(phandle)));
500 uint64_t bpid_host = 0;
501 uint64_t bpool_host[6] = {0};
503 /* Allocate an object for the pool */
504 bpool = rte_malloc(NULL, sizeof(*bpool), RTE_CACHE_LINE_SIZE);
506 FMAN_ERR(-ENOMEM, "malloc(%zu)\n", sizeof(*bpool));
509 /* Find the pool node */
510 pool_node = of_find_node_by_phandle(*pools_phandle);
512 FMAN_ERR(-ENXIO, "%s: bad fsl,bman-buffer-pools\n",
517 pname = pool_node->full_name;
518 /* Extract the BPID property */
519 prop = of_get_property(pool_node, "fsl,bpid", &proplen);
521 FMAN_ERR(-EINVAL, "%s: no fsl,bpid\n", pname);
525 assert(proplen == sizeof(*prop));
526 na = of_n_addr_cells(mac_node);
527 /* Get rid of endianness (issues).
528 * Convert to host byte-order
530 bpid_host = of_read_number(prop, na);
531 bpool->bpid = bpid_host;
532 /* Extract the cfg property (count/size/addr). "fsl,bpool-cfg"
533 * indicates for the Bman driver to seed the pool.
534 * "fsl,bpool-ethernet-cfg" is used by the network driver. The
535 * two are mutually exclusive, so check for either of them.
537 prop = of_get_property(pool_node, "fsl,bpool-cfg",
540 prop = of_get_property(pool_node,
541 "fsl,bpool-ethernet-cfg",
544 /* It's OK for there to be no bpool-cfg */
545 bpool->count = bpool->size = bpool->addr = 0;
547 assert(proplen == (6 * sizeof(*prop)));
548 na = of_n_addr_cells(mac_node);
549 /* Get rid of endianness (issues).
550 * Convert to host byte order
552 bpool_host[0] = of_read_number(&prop[0], na);
553 bpool_host[1] = of_read_number(&prop[1], na);
554 bpool_host[2] = of_read_number(&prop[2], na);
555 bpool_host[3] = of_read_number(&prop[3], na);
556 bpool_host[4] = of_read_number(&prop[4], na);
557 bpool_host[5] = of_read_number(&prop[5], na);
559 bpool->count = ((uint64_t)bpool_host[0] << 32) |
561 bpool->size = ((uint64_t)bpool_host[2] << 32) |
563 bpool->addr = ((uint64_t)bpool_host[4] << 32) |
566 /* Parsing of the pool is complete, add it to the interface
569 list_add_tail(&bpool->node, &__if->__if.bpool_list);
570 lenp -= sizeof(phandle);
575 __if->__if.is_shared_mac = 1;
577 fman_if_vsp_init(__if);
579 /* Parsing of the network interface is complete, add it to the list */
580 DPAA_BUS_LOG(DEBUG, "Found %s, Tx Channel = %x, FMAN = %x,"
582 dname, __if->__if.tx_channel_id, __if->__if.fman_idx,
585 list_add_tail(&__if->__if.node, &__ifs);
595 const struct device_node *dpa_node, *parent_node;
598 /* If multiple dependencies try to initialise the Fman driver, don't
601 if (fman_ccsr_map_fd != -1)
604 fman_ccsr_map_fd = open(FMAN_DEVICE_PATH, O_RDWR);
605 if (unlikely(fman_ccsr_map_fd < 0)) {
606 DPAA_BUS_LOG(ERR, "Unable to open (/dev/mem)");
607 return fman_ccsr_map_fd;
610 parent_node = of_find_compatible_node(NULL, NULL, "fsl,dpaa");
612 DPAA_BUS_LOG(ERR, "Unable to find fsl,dpaa node");
616 for_each_child_node(parent_node, dpa_node) {
617 _errno = fman_if_init(dpa_node);
619 FMAN_ERR(_errno, "if_init(%s)\n", dpa_node->full_name);
633 struct __fman_if *__if, *tmpif;
635 assert(fman_ccsr_map_fd != -1);
637 list_for_each_entry_safe(__if, tmpif, &__ifs, __if.node) {
640 /* disable Rx and Tx */
641 if ((__if->__if.mac_type == fman_mac_1g) &&
642 (!__if->__if.is_memac))
643 out_be32(__if->ccsr_map + 0x100,
644 in_be32(__if->ccsr_map + 0x100) & ~(u32)0x5);
646 out_be32(__if->ccsr_map + 8,
647 in_be32(__if->ccsr_map + 8) & ~(u32)3);
648 /* release the mapping */
649 _errno = munmap(__if->ccsr_map, __if->regs_size);
650 if (unlikely(_errno < 0))
651 fprintf(stderr, "%s:%d:%s(): munmap() = %d (%s)\n",
652 __FILE__, __LINE__, __func__,
653 -errno, strerror(errno));
654 printf("Tearing down %s\n", __if->node_path);
655 list_del(&__if->__if.node);
659 close(fman_ccsr_map_fd);
660 fman_ccsr_map_fd = -1;