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>
50 #define QMI_PORT_REGS_OFFSET 0x400
52 /* CCSR map address to access ccsr based register */
54 /* fman version info */
57 u32 fman_dealloc_bufs_mask_hi;
58 u32 fman_dealloc_bufs_mask_lo;
60 int fman_ccsr_map_fd = -1;
61 static COMPAT_LIST_HEAD(__ifs);
63 /* This is the (const) global variable that callers have read-only access to.
64 * Internally, we have read-write access directly to __ifs.
66 const struct list_head *fman_if_list = &__ifs;
69 if_destructor(struct __fman_if *__if)
71 struct fman_if_bpool *bp, *tmpbp;
76 if (__if->__if.mac_type == fman_offline)
79 list_for_each_entry_safe(bp, tmpbp, &__if->__if.bpool_list, node) {
88 fman_get_ip_rev(const struct device_node *fman_node)
90 const uint32_t *fman_addr;
96 fman_addr = of_get_address(fman_node, 0, ®s_size, NULL);
98 pr_err("of_get_address cannot return fman address\n");
101 phys_addr = of_translate_address(fman_node, fman_addr);
103 pr_err("of_translate_address failed\n");
106 fman_ccsr_map = mmap(NULL, regs_size, PROT_READ | PROT_WRITE,
107 MAP_SHARED, fman_ccsr_map_fd, phys_addr);
108 if (fman_ccsr_map == MAP_FAILED) {
109 pr_err("Can not map FMan ccsr base");
113 ip_rev_1 = in_be32(fman_ccsr_map + FMAN_IP_REV_1);
114 fman_ip_rev = (ip_rev_1 & FMAN_IP_REV_1_MAJOR_MASK) >>
115 FMAN_IP_REV_1_MAJOR_SHIFT;
117 _errno = munmap(fman_ccsr_map, regs_size);
119 pr_err("munmap() of FMan ccsr failed");
125 fman_get_mac_index(uint64_t regs_addr_host, uint8_t *mac_idx)
141 switch (regs_addr_host) {
180 fman_if_init(const struct device_node *dpa_node)
182 const char *rprop, *mprop;
184 struct __fman_if *__if;
185 struct fman_if_bpool *bpool;
187 const phandle *mac_phandle, *ports_phandle, *pools_phandle;
188 const phandle *tx_channel_id = NULL, *mac_addr, *cell_idx;
189 const phandle *rx_phandle, *tx_phandle;
190 uint64_t tx_phandle_host[4] = {0};
191 uint64_t rx_phandle_host[4] = {0};
192 uint64_t regs_addr_host = 0;
193 uint64_t cell_idx_host = 0;
195 const struct device_node *mac_node = NULL, *tx_node;
196 const struct device_node *pool_node, *fman_node, *rx_node;
197 const uint32_t *regs_addr = NULL;
198 const char *mname, *fname;
199 const char *dname = dpa_node->full_name;
202 const char *char_prop;
205 if (of_device_is_available(dpa_node) == false)
208 rprop = "fsl,qman-frame-queues-rx";
209 mprop = "fsl,fman-mac";
211 /* Allocate an object for this network interface */
212 __if = malloc(sizeof(*__if));
214 FMAN_ERR(-ENOMEM, "malloc(%zu)\n", sizeof(*__if));
217 memset(__if, 0, sizeof(*__if));
218 INIT_LIST_HEAD(&__if->__if.bpool_list);
219 strncpy(__if->node_path, dpa_node->full_name, PATH_MAX - 1);
220 __if->node_path[PATH_MAX - 1] = '\0';
222 /* Obtain the MAC node used by this interface except macless */
223 mac_phandle = of_get_property(dpa_node, mprop, &lenp);
225 FMAN_ERR(-EINVAL, "%s: no %s\n", dname, mprop);
228 assert(lenp == sizeof(phandle));
229 mac_node = of_find_node_by_phandle(*mac_phandle);
231 FMAN_ERR(-ENXIO, "%s: bad 'fsl,fman-mac\n", dname);
234 mname = mac_node->full_name;
236 /* Map the CCSR regs for the MAC node */
237 regs_addr = of_get_address(mac_node, 0, &__if->regs_size, NULL);
239 FMAN_ERR(-EINVAL, "of_get_address(%s)\n", mname);
242 phys_addr = of_translate_address(mac_node, regs_addr);
244 FMAN_ERR(-EINVAL, "of_translate_address(%s, %p)\n",
248 __if->ccsr_map = mmap(NULL, __if->regs_size,
249 PROT_READ | PROT_WRITE, MAP_SHARED,
250 fman_ccsr_map_fd, phys_addr);
251 if (__if->ccsr_map == MAP_FAILED) {
252 FMAN_ERR(-errno, "mmap(0x%"PRIx64")\n", phys_addr);
255 na = of_n_addr_cells(mac_node);
256 /* Get rid of endianness (issues). Convert to host byte order */
257 regs_addr_host = of_read_number(regs_addr, na);
260 /* Get the index of the Fman this i/f belongs to */
261 fman_node = of_get_parent(mac_node);
262 na = of_n_addr_cells(mac_node);
264 FMAN_ERR(-ENXIO, "of_get_parent(%s)\n", mname);
267 fname = fman_node->full_name;
268 cell_idx = of_get_property(fman_node, "cell-index", &lenp);
270 FMAN_ERR(-ENXIO, "%s: no cell-index)\n", fname);
273 assert(lenp == sizeof(*cell_idx));
274 cell_idx_host = of_read_number(cell_idx, lenp / sizeof(phandle));
275 __if->__if.fman_idx = cell_idx_host;
277 _errno = fman_get_ip_rev(fman_node);
279 FMAN_ERR(-ENXIO, "%s: ip_rev is not available\n",
285 if (fman_ip_rev >= FMAN_V3) {
287 * Set A2V, OVOM, EBD bits in contextA to allow external
288 * buffer deallocation by fman.
290 fman_dealloc_bufs_mask_hi = FMAN_V3_CONTEXTA_EN_A2V |
291 FMAN_V3_CONTEXTA_EN_OVOM;
292 fman_dealloc_bufs_mask_lo = FMAN_V3_CONTEXTA_EN_EBD;
294 fman_dealloc_bufs_mask_hi = 0;
295 fman_dealloc_bufs_mask_lo = 0;
297 /* Is the MAC node 1G, 10G? */
298 __if->__if.is_memac = 0;
300 if (of_device_is_compatible(mac_node, "fsl,fman-1g-mac"))
301 __if->__if.mac_type = fman_mac_1g;
302 else if (of_device_is_compatible(mac_node, "fsl,fman-10g-mac"))
303 __if->__if.mac_type = fman_mac_10g;
304 else if (of_device_is_compatible(mac_node, "fsl,fman-memac")) {
305 __if->__if.is_memac = 1;
306 char_prop = of_get_property(mac_node, "phy-connection-type",
309 printf("memac: unknown MII type assuming 1G\n");
310 /* Right now forcing memac to 1g in case of error*/
311 __if->__if.mac_type = fman_mac_1g;
313 if (strstr(char_prop, "sgmii"))
314 __if->__if.mac_type = fman_mac_1g;
315 else if (strstr(char_prop, "rgmii")) {
316 __if->__if.mac_type = fman_mac_1g;
317 __if->__if.is_rgmii = 1;
318 } else if (strstr(char_prop, "xgmii"))
319 __if->__if.mac_type = fman_mac_10g;
322 FMAN_ERR(-EINVAL, "%s: unknown MAC type\n", mname);
327 * For MAC ports, we cannot rely on cell-index. In
328 * T2080, two of the 10G ports on single FMAN have same
329 * duplicate cell-indexes as the other two 10G ports on
330 * same FMAN. Hence, we now rely upon addresses of the
331 * ports from device tree to deduce the index.
334 _errno = fman_get_mac_index(regs_addr_host, &__if->__if.mac_idx);
336 FMAN_ERR(-EINVAL, "Invalid register address: %lu",
341 /* Extract the MAC address for private and shared interfaces */
342 mac_addr = of_get_property(mac_node, "local-mac-address",
345 FMAN_ERR(-EINVAL, "%s: no local-mac-address\n",
349 memcpy(&__if->__if.mac_addr, mac_addr, ETHER_ADDR_LEN);
351 /* Extract the Tx port (it's the second of the two port handles)
352 * and get its channel ID
354 ports_phandle = of_get_property(mac_node, "fsl,port-handles",
357 ports_phandle = of_get_property(mac_node, "fsl,fman-ports",
359 if (!ports_phandle) {
360 FMAN_ERR(-EINVAL, "%s: no fsl,port-handles\n",
364 assert(lenp == (2 * sizeof(phandle)));
365 tx_node = of_find_node_by_phandle(ports_phandle[1]);
367 FMAN_ERR(-ENXIO, "%s: bad fsl,port-handle[1]\n", mname);
370 /* Extract the channel ID (from tx-port-handle) */
371 tx_channel_id = of_get_property(tx_node, "fsl,qman-channel-id",
373 if (!tx_channel_id) {
374 FMAN_ERR(-EINVAL, "%s: no fsl-qman-channel-id\n",
379 rx_node = of_find_node_by_phandle(ports_phandle[0]);
381 FMAN_ERR(-ENXIO, "%s: bad fsl,port-handle[0]\n", mname);
384 regs_addr = of_get_address(rx_node, 0, &__if->regs_size, NULL);
386 FMAN_ERR(-EINVAL, "of_get_address(%s)\n", mname);
389 phys_addr = of_translate_address(rx_node, regs_addr);
391 FMAN_ERR(-EINVAL, "of_translate_address(%s, %p)\n",
395 __if->bmi_map = mmap(NULL, __if->regs_size,
396 PROT_READ | PROT_WRITE, MAP_SHARED,
397 fman_ccsr_map_fd, phys_addr);
398 if (__if->bmi_map == MAP_FAILED) {
399 FMAN_ERR(-errno, "mmap(0x%"PRIx64")\n", phys_addr);
403 /* No channel ID for MAC-less */
404 assert(lenp == sizeof(*tx_channel_id));
405 na = of_n_addr_cells(mac_node);
406 __if->__if.tx_channel_id = of_read_number(tx_channel_id, na);
408 /* Extract the Rx FQIDs. (Note, the device representation is silly,
409 * there are "counts" that must always be 1.)
411 rx_phandle = of_get_property(dpa_node, rprop, &lenp);
413 FMAN_ERR(-EINVAL, "%s: no fsl,qman-frame-queues-rx\n", dname);
417 assert(lenp == (4 * sizeof(phandle)));
419 na = of_n_addr_cells(mac_node);
420 /* Get rid of endianness (issues). Convert to host byte order */
421 rx_phandle_host[0] = of_read_number(&rx_phandle[0], na);
422 rx_phandle_host[1] = of_read_number(&rx_phandle[1], na);
423 rx_phandle_host[2] = of_read_number(&rx_phandle[2], na);
424 rx_phandle_host[3] = of_read_number(&rx_phandle[3], na);
426 assert((rx_phandle_host[1] == 1) && (rx_phandle_host[3] == 1));
427 __if->__if.fqid_rx_err = rx_phandle_host[0];
428 __if->__if.fqid_rx_def = rx_phandle_host[2];
430 /* Extract the Tx FQIDs */
431 tx_phandle = of_get_property(dpa_node,
432 "fsl,qman-frame-queues-tx", &lenp);
434 FMAN_ERR(-EINVAL, "%s: no fsl,qman-frame-queues-tx\n", dname);
438 assert(lenp == (4 * sizeof(phandle)));
439 /*TODO: Fix for other cases also */
440 na = of_n_addr_cells(mac_node);
441 /* Get rid of endianness (issues). Convert to host byte order */
442 tx_phandle_host[0] = of_read_number(&tx_phandle[0], na);
443 tx_phandle_host[1] = of_read_number(&tx_phandle[1], na);
444 tx_phandle_host[2] = of_read_number(&tx_phandle[2], na);
445 tx_phandle_host[3] = of_read_number(&tx_phandle[3], na);
446 assert((tx_phandle_host[1] == 1) && (tx_phandle_host[3] == 1));
447 __if->__if.fqid_tx_err = tx_phandle_host[0];
448 __if->__if.fqid_tx_confirm = tx_phandle_host[2];
450 /* Obtain the buffer pool nodes used by this interface */
451 pools_phandle = of_get_property(dpa_node, "fsl,bman-buffer-pools",
453 if (!pools_phandle) {
454 FMAN_ERR(-EINVAL, "%s: no fsl,bman-buffer-pools\n", dname);
457 /* For each pool, parse the corresponding node and add a pool object
458 * to the interface's "bpool_list"
460 assert(lenp && !(lenp % sizeof(phandle)));
464 uint64_t bpid_host = 0;
465 uint64_t bpool_host[6] = {0};
467 /* Allocate an object for the pool */
468 bpool = malloc(sizeof(*bpool));
470 FMAN_ERR(-ENOMEM, "malloc(%zu)\n", sizeof(*bpool));
473 /* Find the pool node */
474 pool_node = of_find_node_by_phandle(*pools_phandle);
476 FMAN_ERR(-ENXIO, "%s: bad fsl,bman-buffer-pools\n",
481 pname = pool_node->full_name;
482 /* Extract the BPID property */
483 prop = of_get_property(pool_node, "fsl,bpid", &proplen);
485 FMAN_ERR(-EINVAL, "%s: no fsl,bpid\n", pname);
489 assert(proplen == sizeof(*prop));
490 na = of_n_addr_cells(mac_node);
491 /* Get rid of endianness (issues).
492 * Convert to host byte-order
494 bpid_host = of_read_number(prop, na);
495 bpool->bpid = bpid_host;
496 /* Extract the cfg property (count/size/addr). "fsl,bpool-cfg"
497 * indicates for the Bman driver to seed the pool.
498 * "fsl,bpool-ethernet-cfg" is used by the network driver. The
499 * two are mutually exclusive, so check for either of them.
501 prop = of_get_property(pool_node, "fsl,bpool-cfg",
504 prop = of_get_property(pool_node,
505 "fsl,bpool-ethernet-cfg",
508 /* It's OK for there to be no bpool-cfg */
509 bpool->count = bpool->size = bpool->addr = 0;
511 assert(proplen == (6 * sizeof(*prop)));
512 na = of_n_addr_cells(mac_node);
513 /* Get rid of endianness (issues).
514 * Convert to host byte order
516 bpool_host[0] = of_read_number(&prop[0], na);
517 bpool_host[1] = of_read_number(&prop[1], na);
518 bpool_host[2] = of_read_number(&prop[2], na);
519 bpool_host[3] = of_read_number(&prop[3], na);
520 bpool_host[4] = of_read_number(&prop[4], na);
521 bpool_host[5] = of_read_number(&prop[5], na);
523 bpool->count = ((uint64_t)bpool_host[0] << 32) |
525 bpool->size = ((uint64_t)bpool_host[2] << 32) |
527 bpool->addr = ((uint64_t)bpool_host[4] << 32) |
530 /* Parsing of the pool is complete, add it to the interface
533 list_add_tail(&bpool->node, &__if->__if.bpool_list);
534 lenp -= sizeof(phandle);
538 /* Parsing of the network interface is complete, add it to the list */
539 DPAA_BUS_LOG(DEBUG, "Found %s, Tx Channel = %x, FMAN = %x,"
541 dname, __if->__if.tx_channel_id, __if->__if.fman_idx,
544 list_add_tail(&__if->__if.node, &__ifs);
554 const struct device_node *dpa_node;
557 /* If multiple dependencies try to initialise the Fman driver, don't
560 if (fman_ccsr_map_fd != -1)
563 fman_ccsr_map_fd = open(FMAN_DEVICE_PATH, O_RDWR);
564 if (unlikely(fman_ccsr_map_fd < 0)) {
565 DPAA_BUS_LOG(ERR, "Unable to open (/dev/mem)");
566 return fman_ccsr_map_fd;
569 for_each_compatible_node(dpa_node, NULL, "fsl,dpa-ethernet-init") {
570 _errno = fman_if_init(dpa_node);
572 FMAN_ERR(_errno, "if_init(%s)\n", dpa_node->full_name);
586 struct __fman_if *__if, *tmpif;
588 assert(fman_ccsr_map_fd != -1);
590 list_for_each_entry_safe(__if, tmpif, &__ifs, __if.node) {
593 /* disable Rx and Tx */
594 if ((__if->__if.mac_type == fman_mac_1g) &&
595 (!__if->__if.is_memac))
596 out_be32(__if->ccsr_map + 0x100,
597 in_be32(__if->ccsr_map + 0x100) & ~(u32)0x5);
599 out_be32(__if->ccsr_map + 8,
600 in_be32(__if->ccsr_map + 8) & ~(u32)3);
601 /* release the mapping */
602 _errno = munmap(__if->ccsr_map, __if->regs_size);
603 if (unlikely(_errno < 0))
604 fprintf(stderr, "%s:%hu:%s(): munmap() = %d (%s)\n",
605 __FILE__, __LINE__, __func__,
606 -errno, strerror(errno));
607 printf("Tearing down %s\n", __if->node_path);
608 list_del(&__if->__if.node);
612 close(fman_ccsr_map_fd);
613 fman_ccsr_map_fd = -1;