1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
3 * Copyright 2010-2016 Freescale Semiconductor Inc.
12 /* This header declares the driver interface we implement */
15 #include <rte_dpaa_logs.h>
17 #define QMI_PORT_REGS_OFFSET 0x400
19 /* CCSR map address to access ccsr based register */
21 /* fman version info */
24 u32 fman_dealloc_bufs_mask_hi;
25 u32 fman_dealloc_bufs_mask_lo;
27 int fman_ccsr_map_fd = -1;
28 static COMPAT_LIST_HEAD(__ifs);
30 /* This is the (const) global variable that callers have read-only access to.
31 * Internally, we have read-write access directly to __ifs.
33 const struct list_head *fman_if_list = &__ifs;
36 if_destructor(struct __fman_if *__if)
38 struct fman_if_bpool *bp, *tmpbp;
43 if (__if->__if.mac_type == fman_offline)
46 list_for_each_entry_safe(bp, tmpbp, &__if->__if.bpool_list, node) {
55 fman_get_ip_rev(const struct device_node *fman_node)
57 const uint32_t *fman_addr;
63 fman_addr = of_get_address(fman_node, 0, ®s_size, NULL);
65 pr_err("of_get_address cannot return fman address\n");
68 phys_addr = of_translate_address(fman_node, fman_addr);
70 pr_err("of_translate_address failed\n");
73 fman_ccsr_map = mmap(NULL, regs_size, PROT_READ | PROT_WRITE,
74 MAP_SHARED, fman_ccsr_map_fd, phys_addr);
75 if (fman_ccsr_map == MAP_FAILED) {
76 pr_err("Can not map FMan ccsr base");
80 ip_rev_1 = in_be32(fman_ccsr_map + FMAN_IP_REV_1);
81 fman_ip_rev = (ip_rev_1 & FMAN_IP_REV_1_MAJOR_MASK) >>
82 FMAN_IP_REV_1_MAJOR_SHIFT;
84 _errno = munmap(fman_ccsr_map, regs_size);
86 pr_err("munmap() of FMan ccsr failed");
92 fman_get_mac_index(uint64_t regs_addr_host, uint8_t *mac_idx)
108 switch (regs_addr_host) {
147 fman_if_init(const struct device_node *dpa_node)
149 const char *rprop, *mprop;
151 struct __fman_if *__if;
152 struct fman_if_bpool *bpool;
154 const phandle *mac_phandle, *ports_phandle, *pools_phandle;
155 const phandle *tx_channel_id = NULL, *mac_addr, *cell_idx;
156 const phandle *rx_phandle, *tx_phandle;
157 uint64_t tx_phandle_host[4] = {0};
158 uint64_t rx_phandle_host[4] = {0};
159 uint64_t regs_addr_host = 0;
160 uint64_t cell_idx_host = 0;
162 const struct device_node *mac_node = NULL, *tx_node;
163 const struct device_node *pool_node, *fman_node, *rx_node;
164 const uint32_t *regs_addr = NULL;
165 const char *mname, *fname;
166 const char *dname = dpa_node->full_name;
169 const char *char_prop;
172 if (of_device_is_available(dpa_node) == false)
175 rprop = "fsl,qman-frame-queues-rx";
176 mprop = "fsl,fman-mac";
178 /* Allocate an object for this network interface */
179 __if = malloc(sizeof(*__if));
181 FMAN_ERR(-ENOMEM, "malloc(%zu)\n", sizeof(*__if));
184 memset(__if, 0, sizeof(*__if));
185 INIT_LIST_HEAD(&__if->__if.bpool_list);
186 strncpy(__if->node_path, dpa_node->full_name, PATH_MAX - 1);
187 __if->node_path[PATH_MAX - 1] = '\0';
189 /* Obtain the MAC node used by this interface except macless */
190 mac_phandle = of_get_property(dpa_node, mprop, &lenp);
192 FMAN_ERR(-EINVAL, "%s: no %s\n", dname, mprop);
195 assert(lenp == sizeof(phandle));
196 mac_node = of_find_node_by_phandle(*mac_phandle);
198 FMAN_ERR(-ENXIO, "%s: bad 'fsl,fman-mac\n", dname);
201 mname = mac_node->full_name;
203 /* Map the CCSR regs for the MAC node */
204 regs_addr = of_get_address(mac_node, 0, &__if->regs_size, NULL);
206 FMAN_ERR(-EINVAL, "of_get_address(%s)\n", mname);
209 phys_addr = of_translate_address(mac_node, regs_addr);
211 FMAN_ERR(-EINVAL, "of_translate_address(%s, %p)\n",
215 __if->ccsr_map = mmap(NULL, __if->regs_size,
216 PROT_READ | PROT_WRITE, MAP_SHARED,
217 fman_ccsr_map_fd, phys_addr);
218 if (__if->ccsr_map == MAP_FAILED) {
219 FMAN_ERR(-errno, "mmap(0x%"PRIx64")\n", phys_addr);
222 na = of_n_addr_cells(mac_node);
223 /* Get rid of endianness (issues). Convert to host byte order */
224 regs_addr_host = of_read_number(regs_addr, na);
227 /* Get the index of the Fman this i/f belongs to */
228 fman_node = of_get_parent(mac_node);
229 na = of_n_addr_cells(mac_node);
231 FMAN_ERR(-ENXIO, "of_get_parent(%s)\n", mname);
234 fname = fman_node->full_name;
235 cell_idx = of_get_property(fman_node, "cell-index", &lenp);
237 FMAN_ERR(-ENXIO, "%s: no cell-index)\n", fname);
240 assert(lenp == sizeof(*cell_idx));
241 cell_idx_host = of_read_number(cell_idx, lenp / sizeof(phandle));
242 __if->__if.fman_idx = cell_idx_host;
244 _errno = fman_get_ip_rev(fman_node);
246 FMAN_ERR(-ENXIO, "%s: ip_rev is not available\n",
252 if (fman_ip_rev >= FMAN_V3) {
254 * Set A2V, OVOM, EBD bits in contextA to allow external
255 * buffer deallocation by fman.
257 fman_dealloc_bufs_mask_hi = FMAN_V3_CONTEXTA_EN_A2V |
258 FMAN_V3_CONTEXTA_EN_OVOM;
259 fman_dealloc_bufs_mask_lo = FMAN_V3_CONTEXTA_EN_EBD;
261 fman_dealloc_bufs_mask_hi = 0;
262 fman_dealloc_bufs_mask_lo = 0;
264 /* Is the MAC node 1G, 10G? */
265 __if->__if.is_memac = 0;
267 if (of_device_is_compatible(mac_node, "fsl,fman-1g-mac"))
268 __if->__if.mac_type = fman_mac_1g;
269 else if (of_device_is_compatible(mac_node, "fsl,fman-10g-mac"))
270 __if->__if.mac_type = fman_mac_10g;
271 else if (of_device_is_compatible(mac_node, "fsl,fman-memac")) {
272 __if->__if.is_memac = 1;
273 char_prop = of_get_property(mac_node, "phy-connection-type",
276 printf("memac: unknown MII type assuming 1G\n");
277 /* Right now forcing memac to 1g in case of error*/
278 __if->__if.mac_type = fman_mac_1g;
280 if (strstr(char_prop, "sgmii"))
281 __if->__if.mac_type = fman_mac_1g;
282 else if (strstr(char_prop, "rgmii")) {
283 __if->__if.mac_type = fman_mac_1g;
284 __if->__if.is_rgmii = 1;
285 } else if (strstr(char_prop, "xgmii"))
286 __if->__if.mac_type = fman_mac_10g;
289 FMAN_ERR(-EINVAL, "%s: unknown MAC type\n", mname);
294 * For MAC ports, we cannot rely on cell-index. In
295 * T2080, two of the 10G ports on single FMAN have same
296 * duplicate cell-indexes as the other two 10G ports on
297 * same FMAN. Hence, we now rely upon addresses of the
298 * ports from device tree to deduce the index.
301 _errno = fman_get_mac_index(regs_addr_host, &__if->__if.mac_idx);
303 FMAN_ERR(-EINVAL, "Invalid register address: %lu",
308 /* Extract the MAC address for private and shared interfaces */
309 mac_addr = of_get_property(mac_node, "local-mac-address",
312 FMAN_ERR(-EINVAL, "%s: no local-mac-address\n",
316 memcpy(&__if->__if.mac_addr, mac_addr, ETHER_ADDR_LEN);
318 /* Extract the Tx port (it's the second of the two port handles)
319 * and get its channel ID
321 ports_phandle = of_get_property(mac_node, "fsl,port-handles",
324 ports_phandle = of_get_property(mac_node, "fsl,fman-ports",
326 if (!ports_phandle) {
327 FMAN_ERR(-EINVAL, "%s: no fsl,port-handles\n",
331 assert(lenp == (2 * sizeof(phandle)));
332 tx_node = of_find_node_by_phandle(ports_phandle[1]);
334 FMAN_ERR(-ENXIO, "%s: bad fsl,port-handle[1]\n", mname);
337 /* Extract the channel ID (from tx-port-handle) */
338 tx_channel_id = of_get_property(tx_node, "fsl,qman-channel-id",
340 if (!tx_channel_id) {
341 FMAN_ERR(-EINVAL, "%s: no fsl-qman-channel-id\n",
346 rx_node = of_find_node_by_phandle(ports_phandle[0]);
348 FMAN_ERR(-ENXIO, "%s: bad fsl,port-handle[0]\n", mname);
351 regs_addr = of_get_address(rx_node, 0, &__if->regs_size, NULL);
353 FMAN_ERR(-EINVAL, "of_get_address(%s)\n", mname);
356 phys_addr = of_translate_address(rx_node, regs_addr);
358 FMAN_ERR(-EINVAL, "of_translate_address(%s, %p)\n",
362 __if->bmi_map = mmap(NULL, __if->regs_size,
363 PROT_READ | PROT_WRITE, MAP_SHARED,
364 fman_ccsr_map_fd, phys_addr);
365 if (__if->bmi_map == MAP_FAILED) {
366 FMAN_ERR(-errno, "mmap(0x%"PRIx64")\n", phys_addr);
370 /* No channel ID for MAC-less */
371 assert(lenp == sizeof(*tx_channel_id));
372 na = of_n_addr_cells(mac_node);
373 __if->__if.tx_channel_id = of_read_number(tx_channel_id, na);
375 /* Extract the Rx FQIDs. (Note, the device representation is silly,
376 * there are "counts" that must always be 1.)
378 rx_phandle = of_get_property(dpa_node, rprop, &lenp);
380 FMAN_ERR(-EINVAL, "%s: no fsl,qman-frame-queues-rx\n", dname);
384 assert(lenp == (4 * sizeof(phandle)));
386 na = of_n_addr_cells(mac_node);
387 /* Get rid of endianness (issues). Convert to host byte order */
388 rx_phandle_host[0] = of_read_number(&rx_phandle[0], na);
389 rx_phandle_host[1] = of_read_number(&rx_phandle[1], na);
390 rx_phandle_host[2] = of_read_number(&rx_phandle[2], na);
391 rx_phandle_host[3] = of_read_number(&rx_phandle[3], na);
393 assert((rx_phandle_host[1] == 1) && (rx_phandle_host[3] == 1));
394 __if->__if.fqid_rx_err = rx_phandle_host[0];
395 __if->__if.fqid_rx_def = rx_phandle_host[2];
397 /* Extract the Tx FQIDs */
398 tx_phandle = of_get_property(dpa_node,
399 "fsl,qman-frame-queues-tx", &lenp);
401 FMAN_ERR(-EINVAL, "%s: no fsl,qman-frame-queues-tx\n", dname);
405 assert(lenp == (4 * sizeof(phandle)));
406 /*TODO: Fix for other cases also */
407 na = of_n_addr_cells(mac_node);
408 /* Get rid of endianness (issues). Convert to host byte order */
409 tx_phandle_host[0] = of_read_number(&tx_phandle[0], na);
410 tx_phandle_host[1] = of_read_number(&tx_phandle[1], na);
411 tx_phandle_host[2] = of_read_number(&tx_phandle[2], na);
412 tx_phandle_host[3] = of_read_number(&tx_phandle[3], na);
413 assert((tx_phandle_host[1] == 1) && (tx_phandle_host[3] == 1));
414 __if->__if.fqid_tx_err = tx_phandle_host[0];
415 __if->__if.fqid_tx_confirm = tx_phandle_host[2];
417 /* Obtain the buffer pool nodes used by this interface */
418 pools_phandle = of_get_property(dpa_node, "fsl,bman-buffer-pools",
420 if (!pools_phandle) {
421 FMAN_ERR(-EINVAL, "%s: no fsl,bman-buffer-pools\n", dname);
424 /* For each pool, parse the corresponding node and add a pool object
425 * to the interface's "bpool_list"
427 assert(lenp && !(lenp % sizeof(phandle)));
431 uint64_t bpid_host = 0;
432 uint64_t bpool_host[6] = {0};
434 /* Allocate an object for the pool */
435 bpool = malloc(sizeof(*bpool));
437 FMAN_ERR(-ENOMEM, "malloc(%zu)\n", sizeof(*bpool));
440 /* Find the pool node */
441 pool_node = of_find_node_by_phandle(*pools_phandle);
443 FMAN_ERR(-ENXIO, "%s: bad fsl,bman-buffer-pools\n",
447 pname = pool_node->full_name;
448 /* Extract the BPID property */
449 prop = of_get_property(pool_node, "fsl,bpid", &proplen);
451 FMAN_ERR(-EINVAL, "%s: no fsl,bpid\n", pname);
454 assert(proplen == sizeof(*prop));
455 na = of_n_addr_cells(mac_node);
456 /* Get rid of endianness (issues).
457 * Convert to host byte-order
459 bpid_host = of_read_number(prop, na);
460 bpool->bpid = bpid_host;
461 /* Extract the cfg property (count/size/addr). "fsl,bpool-cfg"
462 * indicates for the Bman driver to seed the pool.
463 * "fsl,bpool-ethernet-cfg" is used by the network driver. The
464 * two are mutually exclusive, so check for either of them.
466 prop = of_get_property(pool_node, "fsl,bpool-cfg",
469 prop = of_get_property(pool_node,
470 "fsl,bpool-ethernet-cfg",
473 /* It's OK for there to be no bpool-cfg */
474 bpool->count = bpool->size = bpool->addr = 0;
476 assert(proplen == (6 * sizeof(*prop)));
477 na = of_n_addr_cells(mac_node);
478 /* Get rid of endianness (issues).
479 * Convert to host byte order
481 bpool_host[0] = of_read_number(&prop[0], na);
482 bpool_host[1] = of_read_number(&prop[1], na);
483 bpool_host[2] = of_read_number(&prop[2], na);
484 bpool_host[3] = of_read_number(&prop[3], na);
485 bpool_host[4] = of_read_number(&prop[4], na);
486 bpool_host[5] = of_read_number(&prop[5], na);
488 bpool->count = ((uint64_t)bpool_host[0] << 32) |
490 bpool->size = ((uint64_t)bpool_host[2] << 32) |
492 bpool->addr = ((uint64_t)bpool_host[4] << 32) |
495 /* Parsing of the pool is complete, add it to the interface
498 list_add_tail(&bpool->node, &__if->__if.bpool_list);
499 lenp -= sizeof(phandle);
503 /* Parsing of the network interface is complete, add it to the list */
504 DPAA_BUS_LOG(DEBUG, "Found %s, Tx Channel = %x, FMAN = %x,"
506 dname, __if->__if.tx_channel_id, __if->__if.fman_idx,
509 list_add_tail(&__if->__if.node, &__ifs);
519 const struct device_node *dpa_node;
522 /* If multiple dependencies try to initialise the Fman driver, don't
525 if (fman_ccsr_map_fd != -1)
528 fman_ccsr_map_fd = open(FMAN_DEVICE_PATH, O_RDWR);
529 if (unlikely(fman_ccsr_map_fd < 0)) {
530 DPAA_BUS_LOG(ERR, "Unable to open (/dev/mem)");
531 return fman_ccsr_map_fd;
534 for_each_compatible_node(dpa_node, NULL, "fsl,dpa-ethernet-init") {
535 _errno = fman_if_init(dpa_node);
537 FMAN_ERR(_errno, "if_init(%s)\n", dpa_node->full_name);
551 struct __fman_if *__if, *tmpif;
553 assert(fman_ccsr_map_fd != -1);
555 list_for_each_entry_safe(__if, tmpif, &__ifs, __if.node) {
558 /* disable Rx and Tx */
559 if ((__if->__if.mac_type == fman_mac_1g) &&
560 (!__if->__if.is_memac))
561 out_be32(__if->ccsr_map + 0x100,
562 in_be32(__if->ccsr_map + 0x100) & ~(u32)0x5);
564 out_be32(__if->ccsr_map + 8,
565 in_be32(__if->ccsr_map + 8) & ~(u32)3);
566 /* release the mapping */
567 _errno = munmap(__if->ccsr_map, __if->regs_size);
568 if (unlikely(_errno < 0))
569 fprintf(stderr, "%s:%d:%s(): munmap() = %d (%s)\n",
570 __FILE__, __LINE__, __func__,
571 -errno, strerror(errno));
572 printf("Tearing down %s\n", __if->node_path);
573 list_del(&__if->__if.node);
577 close(fman_ccsr_map_fd);
578 fman_ccsr_map_fd = -1;