2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright 2010-2016 Freescale Semiconductor Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * * Neither the name of the above-listed copyright holders nor the
18 * names of any contributors may be used to endorse or promote products
19 * derived from this software without specific prior written permission.
23 * ALTERNATIVELY, this software may be distributed under the terms of the
24 * GNU General Public License ("GPL") as published by the Free Software
25 * Foundation, either version 2 of that License or (at your option) any
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/types.h>
42 #include <sys/ioctl.h>
45 /* This header declares the driver interface we implement */
48 #include <rte_dpaa_logs.h>
49 #include <rte_string_fns.h>
51 #define QMI_PORT_REGS_OFFSET 0x400
53 /* CCSR map address to access ccsr based register */
55 /* fman version info */
58 u32 fman_dealloc_bufs_mask_hi;
59 u32 fman_dealloc_bufs_mask_lo;
61 int fman_ccsr_map_fd = -1;
62 static COMPAT_LIST_HEAD(__ifs);
64 /* This is the (const) global variable that callers have read-only access to.
65 * Internally, we have read-write access directly to __ifs.
67 const struct list_head *fman_if_list = &__ifs;
70 if_destructor(struct __fman_if *__if)
72 struct fman_if_bpool *bp, *tmpbp;
77 if (__if->__if.mac_type == fman_offline)
80 list_for_each_entry_safe(bp, tmpbp, &__if->__if.bpool_list, node) {
89 fman_get_ip_rev(const struct device_node *fman_node)
91 const uint32_t *fman_addr;
97 fman_addr = of_get_address(fman_node, 0, ®s_size, NULL);
99 pr_err("of_get_address cannot return fman address\n");
102 phys_addr = of_translate_address(fman_node, fman_addr);
104 pr_err("of_translate_address failed\n");
107 fman_ccsr_map = mmap(NULL, regs_size, PROT_READ | PROT_WRITE,
108 MAP_SHARED, fman_ccsr_map_fd, phys_addr);
109 if (fman_ccsr_map == MAP_FAILED) {
110 pr_err("Can not map FMan ccsr base");
114 ip_rev_1 = in_be32(fman_ccsr_map + FMAN_IP_REV_1);
115 fman_ip_rev = (ip_rev_1 & FMAN_IP_REV_1_MAJOR_MASK) >>
116 FMAN_IP_REV_1_MAJOR_SHIFT;
118 _errno = munmap(fman_ccsr_map, regs_size);
120 pr_err("munmap() of FMan ccsr failed");
126 fman_get_mac_index(uint64_t regs_addr_host, uint8_t *mac_idx)
142 switch (regs_addr_host) {
181 fman_if_init(const struct device_node *dpa_node)
183 const char *rprop, *mprop;
185 struct __fman_if *__if;
186 struct fman_if_bpool *bpool;
188 const phandle *mac_phandle, *ports_phandle, *pools_phandle;
189 const phandle *tx_channel_id = NULL, *mac_addr, *cell_idx;
190 const phandle *rx_phandle, *tx_phandle;
191 uint64_t tx_phandle_host[4] = {0};
192 uint64_t rx_phandle_host[4] = {0};
193 uint64_t regs_addr_host = 0;
194 uint64_t cell_idx_host = 0;
196 const struct device_node *mac_node = NULL, *tx_node;
197 const struct device_node *pool_node, *fman_node, *rx_node;
198 const uint32_t *regs_addr = NULL;
199 const char *mname, *fname;
200 const char *dname = dpa_node->full_name;
203 const char *char_prop;
206 if (of_device_is_available(dpa_node) == false)
209 rprop = "fsl,qman-frame-queues-rx";
210 mprop = "fsl,fman-mac";
212 /* Allocate an object for this network interface */
213 __if = malloc(sizeof(*__if));
215 FMAN_ERR(-ENOMEM, "malloc(%zu)\n", sizeof(*__if));
218 memset(__if, 0, sizeof(*__if));
219 INIT_LIST_HEAD(&__if->__if.bpool_list);
220 strlcpy(__if->node_path, dpa_node->full_name, PATH_MAX - 1);
221 __if->node_path[PATH_MAX - 1] = '\0';
223 /* Obtain the MAC node used by this interface except macless */
224 mac_phandle = of_get_property(dpa_node, mprop, &lenp);
226 FMAN_ERR(-EINVAL, "%s: no %s\n", dname, mprop);
229 assert(lenp == sizeof(phandle));
230 mac_node = of_find_node_by_phandle(*mac_phandle);
232 FMAN_ERR(-ENXIO, "%s: bad 'fsl,fman-mac\n", dname);
235 mname = mac_node->full_name;
237 /* Map the CCSR regs for the MAC node */
238 regs_addr = of_get_address(mac_node, 0, &__if->regs_size, NULL);
240 FMAN_ERR(-EINVAL, "of_get_address(%s)\n", mname);
243 phys_addr = of_translate_address(mac_node, regs_addr);
245 FMAN_ERR(-EINVAL, "of_translate_address(%s, %p)\n",
249 __if->ccsr_map = mmap(NULL, __if->regs_size,
250 PROT_READ | PROT_WRITE, MAP_SHARED,
251 fman_ccsr_map_fd, phys_addr);
252 if (__if->ccsr_map == MAP_FAILED) {
253 FMAN_ERR(-errno, "mmap(0x%"PRIx64")\n", phys_addr);
256 na = of_n_addr_cells(mac_node);
257 /* Get rid of endianness (issues). Convert to host byte order */
258 regs_addr_host = of_read_number(regs_addr, na);
261 /* Get the index of the Fman this i/f belongs to */
262 fman_node = of_get_parent(mac_node);
263 na = of_n_addr_cells(mac_node);
265 FMAN_ERR(-ENXIO, "of_get_parent(%s)\n", mname);
268 fname = fman_node->full_name;
269 cell_idx = of_get_property(fman_node, "cell-index", &lenp);
271 FMAN_ERR(-ENXIO, "%s: no cell-index)\n", fname);
274 assert(lenp == sizeof(*cell_idx));
275 cell_idx_host = of_read_number(cell_idx, lenp / sizeof(phandle));
276 __if->__if.fman_idx = cell_idx_host;
278 _errno = fman_get_ip_rev(fman_node);
280 FMAN_ERR(-ENXIO, "%s: ip_rev is not available\n",
286 if (fman_ip_rev >= FMAN_V3) {
288 * Set A2V, OVOM, EBD bits in contextA to allow external
289 * buffer deallocation by fman.
291 fman_dealloc_bufs_mask_hi = FMAN_V3_CONTEXTA_EN_A2V |
292 FMAN_V3_CONTEXTA_EN_OVOM;
293 fman_dealloc_bufs_mask_lo = FMAN_V3_CONTEXTA_EN_EBD;
295 fman_dealloc_bufs_mask_hi = 0;
296 fman_dealloc_bufs_mask_lo = 0;
298 /* Is the MAC node 1G, 10G? */
299 __if->__if.is_memac = 0;
301 if (of_device_is_compatible(mac_node, "fsl,fman-1g-mac"))
302 __if->__if.mac_type = fman_mac_1g;
303 else if (of_device_is_compatible(mac_node, "fsl,fman-10g-mac"))
304 __if->__if.mac_type = fman_mac_10g;
305 else if (of_device_is_compatible(mac_node, "fsl,fman-memac")) {
306 __if->__if.is_memac = 1;
307 char_prop = of_get_property(mac_node, "phy-connection-type",
310 printf("memac: unknown MII type assuming 1G\n");
311 /* Right now forcing memac to 1g in case of error*/
312 __if->__if.mac_type = fman_mac_1g;
314 if (strstr(char_prop, "sgmii"))
315 __if->__if.mac_type = fman_mac_1g;
316 else if (strstr(char_prop, "rgmii")) {
317 __if->__if.mac_type = fman_mac_1g;
318 __if->__if.is_rgmii = 1;
319 } else if (strstr(char_prop, "xgmii"))
320 __if->__if.mac_type = fman_mac_10g;
323 FMAN_ERR(-EINVAL, "%s: unknown MAC type\n", mname);
328 * For MAC ports, we cannot rely on cell-index. In
329 * T2080, two of the 10G ports on single FMAN have same
330 * duplicate cell-indexes as the other two 10G ports on
331 * same FMAN. Hence, we now rely upon addresses of the
332 * ports from device tree to deduce the index.
335 _errno = fman_get_mac_index(regs_addr_host, &__if->__if.mac_idx);
337 FMAN_ERR(-EINVAL, "Invalid register address: %lu",
342 /* Extract the MAC address for private and shared interfaces */
343 mac_addr = of_get_property(mac_node, "local-mac-address",
346 FMAN_ERR(-EINVAL, "%s: no local-mac-address\n",
350 memcpy(&__if->__if.mac_addr, mac_addr, ETHER_ADDR_LEN);
352 /* Extract the Tx port (it's the second of the two port handles)
353 * and get its channel ID
355 ports_phandle = of_get_property(mac_node, "fsl,port-handles",
358 ports_phandle = of_get_property(mac_node, "fsl,fman-ports",
360 if (!ports_phandle) {
361 FMAN_ERR(-EINVAL, "%s: no fsl,port-handles\n",
365 assert(lenp == (2 * sizeof(phandle)));
366 tx_node = of_find_node_by_phandle(ports_phandle[1]);
368 FMAN_ERR(-ENXIO, "%s: bad fsl,port-handle[1]\n", mname);
371 /* Extract the channel ID (from tx-port-handle) */
372 tx_channel_id = of_get_property(tx_node, "fsl,qman-channel-id",
374 if (!tx_channel_id) {
375 FMAN_ERR(-EINVAL, "%s: no fsl-qman-channel-id\n",
380 rx_node = of_find_node_by_phandle(ports_phandle[0]);
382 FMAN_ERR(-ENXIO, "%s: bad fsl,port-handle[0]\n", mname);
385 regs_addr = of_get_address(rx_node, 0, &__if->regs_size, NULL);
387 FMAN_ERR(-EINVAL, "of_get_address(%s)\n", mname);
390 phys_addr = of_translate_address(rx_node, regs_addr);
392 FMAN_ERR(-EINVAL, "of_translate_address(%s, %p)\n",
396 __if->bmi_map = mmap(NULL, __if->regs_size,
397 PROT_READ | PROT_WRITE, MAP_SHARED,
398 fman_ccsr_map_fd, phys_addr);
399 if (__if->bmi_map == MAP_FAILED) {
400 FMAN_ERR(-errno, "mmap(0x%"PRIx64")\n", phys_addr);
404 /* No channel ID for MAC-less */
405 assert(lenp == sizeof(*tx_channel_id));
406 na = of_n_addr_cells(mac_node);
407 __if->__if.tx_channel_id = of_read_number(tx_channel_id, na);
409 /* Extract the Rx FQIDs. (Note, the device representation is silly,
410 * there are "counts" that must always be 1.)
412 rx_phandle = of_get_property(dpa_node, rprop, &lenp);
414 FMAN_ERR(-EINVAL, "%s: no fsl,qman-frame-queues-rx\n", dname);
418 assert(lenp == (4 * sizeof(phandle)));
420 na = of_n_addr_cells(mac_node);
421 /* Get rid of endianness (issues). Convert to host byte order */
422 rx_phandle_host[0] = of_read_number(&rx_phandle[0], na);
423 rx_phandle_host[1] = of_read_number(&rx_phandle[1], na);
424 rx_phandle_host[2] = of_read_number(&rx_phandle[2], na);
425 rx_phandle_host[3] = of_read_number(&rx_phandle[3], na);
427 assert((rx_phandle_host[1] == 1) && (rx_phandle_host[3] == 1));
428 __if->__if.fqid_rx_err = rx_phandle_host[0];
429 __if->__if.fqid_rx_def = rx_phandle_host[2];
431 /* Extract the Tx FQIDs */
432 tx_phandle = of_get_property(dpa_node,
433 "fsl,qman-frame-queues-tx", &lenp);
435 FMAN_ERR(-EINVAL, "%s: no fsl,qman-frame-queues-tx\n", dname);
439 assert(lenp == (4 * sizeof(phandle)));
440 /*TODO: Fix for other cases also */
441 na = of_n_addr_cells(mac_node);
442 /* Get rid of endianness (issues). Convert to host byte order */
443 tx_phandle_host[0] = of_read_number(&tx_phandle[0], na);
444 tx_phandle_host[1] = of_read_number(&tx_phandle[1], na);
445 tx_phandle_host[2] = of_read_number(&tx_phandle[2], na);
446 tx_phandle_host[3] = of_read_number(&tx_phandle[3], na);
447 assert((tx_phandle_host[1] == 1) && (tx_phandle_host[3] == 1));
448 __if->__if.fqid_tx_err = tx_phandle_host[0];
449 __if->__if.fqid_tx_confirm = tx_phandle_host[2];
451 /* Obtain the buffer pool nodes used by this interface */
452 pools_phandle = of_get_property(dpa_node, "fsl,bman-buffer-pools",
454 if (!pools_phandle) {
455 FMAN_ERR(-EINVAL, "%s: no fsl,bman-buffer-pools\n", dname);
458 /* For each pool, parse the corresponding node and add a pool object
459 * to the interface's "bpool_list"
461 assert(lenp && !(lenp % sizeof(phandle)));
465 uint64_t bpid_host = 0;
466 uint64_t bpool_host[6] = {0};
468 /* Allocate an object for the pool */
469 bpool = malloc(sizeof(*bpool));
471 FMAN_ERR(-ENOMEM, "malloc(%zu)\n", sizeof(*bpool));
474 /* Find the pool node */
475 pool_node = of_find_node_by_phandle(*pools_phandle);
477 FMAN_ERR(-ENXIO, "%s: bad fsl,bman-buffer-pools\n",
482 pname = pool_node->full_name;
483 /* Extract the BPID property */
484 prop = of_get_property(pool_node, "fsl,bpid", &proplen);
486 FMAN_ERR(-EINVAL, "%s: no fsl,bpid\n", pname);
490 assert(proplen == sizeof(*prop));
491 na = of_n_addr_cells(mac_node);
492 /* Get rid of endianness (issues).
493 * Convert to host byte-order
495 bpid_host = of_read_number(prop, na);
496 bpool->bpid = bpid_host;
497 /* Extract the cfg property (count/size/addr). "fsl,bpool-cfg"
498 * indicates for the Bman driver to seed the pool.
499 * "fsl,bpool-ethernet-cfg" is used by the network driver. The
500 * two are mutually exclusive, so check for either of them.
502 prop = of_get_property(pool_node, "fsl,bpool-cfg",
505 prop = of_get_property(pool_node,
506 "fsl,bpool-ethernet-cfg",
509 /* It's OK for there to be no bpool-cfg */
510 bpool->count = bpool->size = bpool->addr = 0;
512 assert(proplen == (6 * sizeof(*prop)));
513 na = of_n_addr_cells(mac_node);
514 /* Get rid of endianness (issues).
515 * Convert to host byte order
517 bpool_host[0] = of_read_number(&prop[0], na);
518 bpool_host[1] = of_read_number(&prop[1], na);
519 bpool_host[2] = of_read_number(&prop[2], na);
520 bpool_host[3] = of_read_number(&prop[3], na);
521 bpool_host[4] = of_read_number(&prop[4], na);
522 bpool_host[5] = of_read_number(&prop[5], na);
524 bpool->count = ((uint64_t)bpool_host[0] << 32) |
526 bpool->size = ((uint64_t)bpool_host[2] << 32) |
528 bpool->addr = ((uint64_t)bpool_host[4] << 32) |
531 /* Parsing of the pool is complete, add it to the interface
534 list_add_tail(&bpool->node, &__if->__if.bpool_list);
535 lenp -= sizeof(phandle);
539 /* Parsing of the network interface is complete, add it to the list */
540 DPAA_BUS_LOG(DEBUG, "Found %s, Tx Channel = %x, FMAN = %x,"
542 dname, __if->__if.tx_channel_id, __if->__if.fman_idx,
545 list_add_tail(&__if->__if.node, &__ifs);
555 const struct device_node *dpa_node;
558 /* If multiple dependencies try to initialise the Fman driver, don't
561 if (fman_ccsr_map_fd != -1)
564 fman_ccsr_map_fd = open(FMAN_DEVICE_PATH, O_RDWR);
565 if (unlikely(fman_ccsr_map_fd < 0)) {
566 DPAA_BUS_LOG(ERR, "Unable to open (/dev/mem)");
567 return fman_ccsr_map_fd;
570 for_each_compatible_node(dpa_node, NULL, "fsl,dpa-ethernet-init") {
571 _errno = fman_if_init(dpa_node);
573 FMAN_ERR(_errno, "if_init(%s)\n", dpa_node->full_name);
587 struct __fman_if *__if, *tmpif;
589 assert(fman_ccsr_map_fd != -1);
591 list_for_each_entry_safe(__if, tmpif, &__ifs, __if.node) {
594 /* disable Rx and Tx */
595 if ((__if->__if.mac_type == fman_mac_1g) &&
596 (!__if->__if.is_memac))
597 out_be32(__if->ccsr_map + 0x100,
598 in_be32(__if->ccsr_map + 0x100) & ~(u32)0x5);
600 out_be32(__if->ccsr_map + 8,
601 in_be32(__if->ccsr_map + 8) & ~(u32)3);
602 /* release the mapping */
603 _errno = munmap(__if->ccsr_map, __if->regs_size);
604 if (unlikely(_errno < 0))
605 fprintf(stderr, "%s:%hu:%s(): munmap() = %d (%s)\n",
606 __FILE__, __LINE__, __func__,
607 -errno, strerror(errno));
608 printf("Tearing down %s\n", __if->node_path);
609 list_del(&__if->__if.node);
613 close(fman_ccsr_map_fd);
614 fman_ccsr_map_fd = -1;