3ae29bf0653884d853f2c19eb9e39559dba0e2dd
[dpdk.git] / drivers / bus / dpaa / base / fman / fman.c
1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2  *
3  * Copyright 2010-2016 Freescale Semiconductor Inc.
4  * Copyright 2017-2020 NXP
5  *
6  */
7
8 #include <sys/types.h>
9 #include <sys/ioctl.h>
10 #include <ifaddrs.h>
11
12 /* This header declares the driver interface we implement */
13 #include <fman.h>
14 #include <dpaa_of.h>
15 #include <rte_malloc.h>
16 #include <rte_dpaa_logs.h>
17 #include <rte_string_fns.h>
18
19 #define QMI_PORT_REGS_OFFSET            0x400
20
21 /* CCSR map address to access ccsr based register */
22 void *fman_ccsr_map;
23 /* fman version info */
24 u16 fman_ip_rev;
25 static int get_once;
26 u32 fman_dealloc_bufs_mask_hi;
27 u32 fman_dealloc_bufs_mask_lo;
28
29 int fman_ccsr_map_fd = -1;
30 static COMPAT_LIST_HEAD(__ifs);
31
32 /* This is the (const) global variable that callers have read-only access to.
33  * Internally, we have read-write access directly to __ifs.
34  */
35 const struct list_head *fman_if_list = &__ifs;
36
37 static void
38 if_destructor(struct __fman_if *__if)
39 {
40         struct fman_if_bpool *bp, *tmpbp;
41
42         if (!__if)
43                 return;
44
45         if (__if->__if.mac_type == fman_offline)
46                 goto cleanup;
47
48         list_for_each_entry_safe(bp, tmpbp, &__if->__if.bpool_list, node) {
49                 list_del(&bp->node);
50                 free(bp);
51         }
52 cleanup:
53         free(__if);
54 }
55
56 static int
57 fman_get_ip_rev(const struct device_node *fman_node)
58 {
59         const uint32_t *fman_addr;
60         uint64_t phys_addr;
61         uint64_t regs_size;
62         uint32_t ip_rev_1;
63         int _errno;
64
65         fman_addr = of_get_address(fman_node, 0, &regs_size, NULL);
66         if (!fman_addr) {
67                 pr_err("of_get_address cannot return fman address\n");
68                 return -EINVAL;
69         }
70         phys_addr = of_translate_address(fman_node, fman_addr);
71         if (!phys_addr) {
72                 pr_err("of_translate_address failed\n");
73                 return -EINVAL;
74         }
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");
79                 return -EINVAL;
80         }
81
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;
85
86         _errno = munmap(fman_ccsr_map, regs_size);
87         if (_errno)
88                 pr_err("munmap() of FMan ccsr failed");
89
90         return 0;
91 }
92
93 static int
94 fman_get_mac_index(uint64_t regs_addr_host, uint8_t *mac_idx)
95 {
96         int ret = 0;
97
98         /*
99          * MAC1 : E_0000h
100          * MAC2 : E_2000h
101          * MAC3 : E_4000h
102          * MAC4 : E_6000h
103          * MAC5 : E_8000h
104          * MAC6 : E_A000h
105          * MAC7 : E_C000h
106          * MAC8 : E_E000h
107          * MAC9 : F_0000h
108          * MAC10: F_2000h
109          */
110         switch (regs_addr_host) {
111         case 0xE0000:
112                 *mac_idx = 1;
113                 break;
114         case 0xE2000:
115                 *mac_idx = 2;
116                 break;
117         case 0xE4000:
118                 *mac_idx = 3;
119                 break;
120         case 0xE6000:
121                 *mac_idx = 4;
122                 break;
123         case 0xE8000:
124                 *mac_idx = 5;
125                 break;
126         case 0xEA000:
127                 *mac_idx = 6;
128                 break;
129         case 0xEC000:
130                 *mac_idx = 7;
131                 break;
132         case 0xEE000:
133                 *mac_idx = 8;
134                 break;
135         case 0xF0000:
136                 *mac_idx = 9;
137                 break;
138         case 0xF2000:
139                 *mac_idx = 10;
140                 break;
141         default:
142                 ret = -EINVAL;
143         }
144
145         return ret;
146 }
147
148 static int
149 fman_if_init(const struct device_node *dpa_node)
150 {
151         const char *rprop, *mprop;
152         uint64_t phys_addr;
153         struct __fman_if *__if;
154         struct fman_if_bpool *bpool;
155
156         const phandle *mac_phandle, *ports_phandle, *pools_phandle;
157         const phandle *tx_channel_id = NULL, *mac_addr, *cell_idx;
158         const phandle *rx_phandle, *tx_phandle;
159         uint64_t tx_phandle_host[4] = {0};
160         uint64_t rx_phandle_host[4] = {0};
161         uint64_t regs_addr_host = 0;
162         uint64_t cell_idx_host = 0;
163
164         const struct device_node *mac_node = NULL, *tx_node;
165         const struct device_node *pool_node, *fman_node, *rx_node;
166         const uint32_t *regs_addr = NULL;
167         const char *mname, *fname;
168         const char *dname = dpa_node->full_name;
169         size_t lenp;
170         int _errno, is_shared = 0;
171         const char *char_prop;
172         uint32_t na;
173
174         if (of_device_is_available(dpa_node) == false)
175                 return 0;
176
177         if (!of_device_is_compatible(dpa_node, "fsl,dpa-ethernet-init") &&
178                 !of_device_is_compatible(dpa_node, "fsl,dpa-ethernet-shared")) {
179                 return 0;
180         }
181
182         if (of_device_is_compatible(dpa_node, "fsl,dpa-ethernet-shared"))
183                 is_shared = 1;
184
185         rprop = "fsl,qman-frame-queues-rx";
186         mprop = "fsl,fman-mac";
187
188         /* Allocate an object for this network interface */
189         __if = rte_malloc(NULL, sizeof(*__if), RTE_CACHE_LINE_SIZE);
190         if (!__if) {
191                 FMAN_ERR(-ENOMEM, "malloc(%zu)\n", sizeof(*__if));
192                 goto err;
193         }
194         memset(__if, 0, sizeof(*__if));
195         INIT_LIST_HEAD(&__if->__if.bpool_list);
196         strlcpy(__if->node_name, dpa_node->name, IF_NAME_MAX_LEN - 1);
197         __if->node_name[IF_NAME_MAX_LEN - 1] = '\0';
198         strlcpy(__if->node_path, dpa_node->full_name, PATH_MAX - 1);
199         __if->node_path[PATH_MAX - 1] = '\0';
200
201         /* Obtain the MAC node used by this interface except macless */
202         mac_phandle = of_get_property(dpa_node, mprop, &lenp);
203         if (!mac_phandle) {
204                 FMAN_ERR(-EINVAL, "%s: no %s\n", dname, mprop);
205                 goto err;
206         }
207         assert(lenp == sizeof(phandle));
208         mac_node = of_find_node_by_phandle(*mac_phandle);
209         if (!mac_node) {
210                 FMAN_ERR(-ENXIO, "%s: bad 'fsl,fman-mac\n", dname);
211                 goto err;
212         }
213         mname = mac_node->full_name;
214
215         /* Map the CCSR regs for the MAC node */
216         regs_addr = of_get_address(mac_node, 0, &__if->regs_size, NULL);
217         if (!regs_addr) {
218                 FMAN_ERR(-EINVAL, "of_get_address(%s)\n", mname);
219                 goto err;
220         }
221         phys_addr = of_translate_address(mac_node, regs_addr);
222         if (!phys_addr) {
223                 FMAN_ERR(-EINVAL, "of_translate_address(%s, %p)\n",
224                          mname, regs_addr);
225                 goto err;
226         }
227         __if->ccsr_map = mmap(NULL, __if->regs_size,
228                               PROT_READ | PROT_WRITE, MAP_SHARED,
229                               fman_ccsr_map_fd, phys_addr);
230         if (__if->ccsr_map == MAP_FAILED) {
231                 FMAN_ERR(-errno, "mmap(0x%"PRIx64")\n", phys_addr);
232                 goto err;
233         }
234         na = of_n_addr_cells(mac_node);
235         /* Get rid of endianness (issues). Convert to host byte order */
236         regs_addr_host = of_read_number(regs_addr, na);
237
238
239         /* Get the index of the Fman this i/f belongs to */
240         fman_node = of_get_parent(mac_node);
241         na = of_n_addr_cells(mac_node);
242         if (!fman_node) {
243                 FMAN_ERR(-ENXIO, "of_get_parent(%s)\n", mname);
244                 goto err;
245         }
246         fname = fman_node->full_name;
247         cell_idx = of_get_property(fman_node, "cell-index", &lenp);
248         if (!cell_idx) {
249                 FMAN_ERR(-ENXIO, "%s: no cell-index)\n", fname);
250                 goto err;
251         }
252         assert(lenp == sizeof(*cell_idx));
253         cell_idx_host = of_read_number(cell_idx, lenp / sizeof(phandle));
254         __if->__if.fman_idx = cell_idx_host;
255         if (!get_once) {
256                 _errno = fman_get_ip_rev(fman_node);
257                 if (_errno) {
258                         FMAN_ERR(-ENXIO, "%s: ip_rev is not available\n",
259                                  fname);
260                         goto err;
261                 }
262         }
263
264         if (fman_ip_rev >= FMAN_V3) {
265                 /*
266                  * Set A2V, OVOM, EBD bits in contextA to allow external
267                  * buffer deallocation by fman.
268                  */
269                 fman_dealloc_bufs_mask_hi = FMAN_V3_CONTEXTA_EN_A2V |
270                                                 FMAN_V3_CONTEXTA_EN_OVOM;
271                 fman_dealloc_bufs_mask_lo = FMAN_V3_CONTEXTA_EN_EBD;
272         } else {
273                 fman_dealloc_bufs_mask_hi = 0;
274                 fman_dealloc_bufs_mask_lo = 0;
275         }
276         /* Is the MAC node 1G, 2.5G, 10G? */
277         __if->__if.is_memac = 0;
278
279         if (of_device_is_compatible(mac_node, "fsl,fman-1g-mac"))
280                 __if->__if.mac_type = fman_mac_1g;
281         else if (of_device_is_compatible(mac_node, "fsl,fman-10g-mac"))
282                 __if->__if.mac_type = fman_mac_10g;
283         else if (of_device_is_compatible(mac_node, "fsl,fman-memac")) {
284                 __if->__if.is_memac = 1;
285                 char_prop = of_get_property(mac_node, "phy-connection-type",
286                                             NULL);
287                 if (!char_prop) {
288                         printf("memac: unknown MII type assuming 1G\n");
289                         /* Right now forcing memac to 1g in case of error*/
290                         __if->__if.mac_type = fman_mac_1g;
291                 } else {
292                         if (strstr(char_prop, "sgmii-2500"))
293                                 __if->__if.mac_type = fman_mac_2_5g;
294                         else if (strstr(char_prop, "sgmii"))
295                                 __if->__if.mac_type = fman_mac_1g;
296                         else if (strstr(char_prop, "rgmii")) {
297                                 __if->__if.mac_type = fman_mac_1g;
298                                 __if->__if.is_rgmii = 1;
299                         } else if (strstr(char_prop, "xgmii"))
300                                 __if->__if.mac_type = fman_mac_10g;
301                 }
302         } else {
303                 FMAN_ERR(-EINVAL, "%s: unknown MAC type\n", mname);
304                 goto err;
305         }
306
307         /*
308          * For MAC ports, we cannot rely on cell-index. In
309          * T2080, two of the 10G ports on single FMAN have same
310          * duplicate cell-indexes as the other two 10G ports on
311          * same FMAN. Hence, we now rely upon addresses of the
312          * ports from device tree to deduce the index.
313          */
314
315         _errno = fman_get_mac_index(regs_addr_host, &__if->__if.mac_idx);
316         if (_errno) {
317                 FMAN_ERR(-EINVAL, "Invalid register address: %" PRIx64,
318                          regs_addr_host);
319                 goto err;
320         }
321
322         /* Extract the MAC address for private and shared interfaces */
323         mac_addr = of_get_property(mac_node, "local-mac-address",
324                                    &lenp);
325         if (!mac_addr) {
326                 FMAN_ERR(-EINVAL, "%s: no local-mac-address\n",
327                          mname);
328                 goto err;
329         }
330         memcpy(&__if->__if.mac_addr, mac_addr, ETHER_ADDR_LEN);
331
332         /* Extract the Tx port (it's the second of the two port handles)
333          * and get its channel ID
334          */
335         ports_phandle = of_get_property(mac_node, "fsl,port-handles",
336                                         &lenp);
337         if (!ports_phandle)
338                 ports_phandle = of_get_property(mac_node, "fsl,fman-ports",
339                                                 &lenp);
340         if (!ports_phandle) {
341                 FMAN_ERR(-EINVAL, "%s: no fsl,port-handles\n",
342                          mname);
343                 goto err;
344         }
345         assert(lenp == (2 * sizeof(phandle)));
346         tx_node = of_find_node_by_phandle(ports_phandle[1]);
347         if (!tx_node) {
348                 FMAN_ERR(-ENXIO, "%s: bad fsl,port-handle[1]\n", mname);
349                 goto err;
350         }
351         /* Extract the channel ID (from tx-port-handle) */
352         tx_channel_id = of_get_property(tx_node, "fsl,qman-channel-id",
353                                         &lenp);
354         if (!tx_channel_id) {
355                 FMAN_ERR(-EINVAL, "%s: no fsl-qman-channel-id\n",
356                          tx_node->full_name);
357                 goto err;
358         }
359
360         rx_node = of_find_node_by_phandle(ports_phandle[0]);
361         if (!rx_node) {
362                 FMAN_ERR(-ENXIO, "%s: bad fsl,port-handle[0]\n", mname);
363                 goto err;
364         }
365         regs_addr = of_get_address(rx_node, 0, &__if->regs_size, NULL);
366         if (!regs_addr) {
367                 FMAN_ERR(-EINVAL, "of_get_address(%s)\n", mname);
368                 goto err;
369         }
370         phys_addr = of_translate_address(rx_node, regs_addr);
371         if (!phys_addr) {
372                 FMAN_ERR(-EINVAL, "of_translate_address(%s, %p)\n",
373                          mname, regs_addr);
374                 goto err;
375         }
376         __if->bmi_map = mmap(NULL, __if->regs_size,
377                                  PROT_READ | PROT_WRITE, MAP_SHARED,
378                                  fman_ccsr_map_fd, phys_addr);
379         if (__if->bmi_map == MAP_FAILED) {
380                 FMAN_ERR(-errno, "mmap(0x%"PRIx64")\n", phys_addr);
381                 goto err;
382         }
383
384         /* No channel ID for MAC-less */
385         assert(lenp == sizeof(*tx_channel_id));
386         na = of_n_addr_cells(mac_node);
387         __if->__if.tx_channel_id = of_read_number(tx_channel_id, na);
388
389         /* Extract the Rx FQIDs. (Note, the device representation is silly,
390          * there are "counts" that must always be 1.)
391          */
392         rx_phandle = of_get_property(dpa_node, rprop, &lenp);
393         if (!rx_phandle) {
394                 FMAN_ERR(-EINVAL, "%s: no fsl,qman-frame-queues-rx\n", dname);
395                 goto err;
396         }
397
398         assert(lenp >= (4 * sizeof(phandle)));
399
400         na = of_n_addr_cells(mac_node);
401         /* Get rid of endianness (issues). Convert to host byte order */
402         rx_phandle_host[0] = of_read_number(&rx_phandle[0], na);
403         rx_phandle_host[1] = of_read_number(&rx_phandle[1], na);
404         rx_phandle_host[2] = of_read_number(&rx_phandle[2], na);
405         rx_phandle_host[3] = of_read_number(&rx_phandle[3], na);
406
407         assert((rx_phandle_host[1] == 1) && (rx_phandle_host[3] == 1));
408         __if->__if.fqid_rx_err = rx_phandle_host[0];
409         __if->__if.fqid_rx_def = rx_phandle_host[2];
410
411         /* Extract the Tx FQIDs */
412         tx_phandle = of_get_property(dpa_node,
413                                      "fsl,qman-frame-queues-tx", &lenp);
414         if (!tx_phandle) {
415                 FMAN_ERR(-EINVAL, "%s: no fsl,qman-frame-queues-tx\n", dname);
416                 goto err;
417         }
418
419         assert(lenp >= (4 * sizeof(phandle)));
420         /*TODO: Fix for other cases also */
421         na = of_n_addr_cells(mac_node);
422         /* Get rid of endianness (issues). Convert to host byte order */
423         tx_phandle_host[0] = of_read_number(&tx_phandle[0], na);
424         tx_phandle_host[1] = of_read_number(&tx_phandle[1], na);
425         tx_phandle_host[2] = of_read_number(&tx_phandle[2], na);
426         tx_phandle_host[3] = of_read_number(&tx_phandle[3], na);
427         assert((tx_phandle_host[1] == 1) && (tx_phandle_host[3] == 1));
428         __if->__if.fqid_tx_err = tx_phandle_host[0];
429         __if->__if.fqid_tx_confirm = tx_phandle_host[2];
430
431         /* Obtain the buffer pool nodes used by this interface */
432         pools_phandle = of_get_property(dpa_node, "fsl,bman-buffer-pools",
433                                         &lenp);
434         if (!pools_phandle) {
435                 FMAN_ERR(-EINVAL, "%s: no fsl,bman-buffer-pools\n", dname);
436                 goto err;
437         }
438         /* For each pool, parse the corresponding node and add a pool object
439          * to the interface's "bpool_list"
440          */
441         assert(lenp && !(lenp % sizeof(phandle)));
442         while (lenp) {
443                 size_t proplen;
444                 const phandle *prop;
445                 uint64_t bpid_host = 0;
446                 uint64_t bpool_host[6] = {0};
447                 const char *pname;
448                 /* Allocate an object for the pool */
449                 bpool = rte_malloc(NULL, sizeof(*bpool), RTE_CACHE_LINE_SIZE);
450                 if (!bpool) {
451                         FMAN_ERR(-ENOMEM, "malloc(%zu)\n", sizeof(*bpool));
452                         goto err;
453                 }
454                 /* Find the pool node */
455                 pool_node = of_find_node_by_phandle(*pools_phandle);
456                 if (!pool_node) {
457                         FMAN_ERR(-ENXIO, "%s: bad fsl,bman-buffer-pools\n",
458                                  dname);
459                         rte_free(bpool);
460                         goto err;
461                 }
462                 pname = pool_node->full_name;
463                 /* Extract the BPID property */
464                 prop = of_get_property(pool_node, "fsl,bpid", &proplen);
465                 if (!prop) {
466                         FMAN_ERR(-EINVAL, "%s: no fsl,bpid\n", pname);
467                         rte_free(bpool);
468                         goto err;
469                 }
470                 assert(proplen == sizeof(*prop));
471                 na = of_n_addr_cells(mac_node);
472                 /* Get rid of endianness (issues).
473                  * Convert to host byte-order
474                  */
475                 bpid_host = of_read_number(prop, na);
476                 bpool->bpid = bpid_host;
477                 /* Extract the cfg property (count/size/addr). "fsl,bpool-cfg"
478                  * indicates for the Bman driver to seed the pool.
479                  * "fsl,bpool-ethernet-cfg" is used by the network driver. The
480                  * two are mutually exclusive, so check for either of them.
481                  */
482                 prop = of_get_property(pool_node, "fsl,bpool-cfg",
483                                        &proplen);
484                 if (!prop)
485                         prop = of_get_property(pool_node,
486                                                "fsl,bpool-ethernet-cfg",
487                                                &proplen);
488                 if (!prop) {
489                         /* It's OK for there to be no bpool-cfg */
490                         bpool->count = bpool->size = bpool->addr = 0;
491                 } else {
492                         assert(proplen == (6 * sizeof(*prop)));
493                         na = of_n_addr_cells(mac_node);
494                         /* Get rid of endianness (issues).
495                          * Convert to host byte order
496                          */
497                         bpool_host[0] = of_read_number(&prop[0], na);
498                         bpool_host[1] = of_read_number(&prop[1], na);
499                         bpool_host[2] = of_read_number(&prop[2], na);
500                         bpool_host[3] = of_read_number(&prop[3], na);
501                         bpool_host[4] = of_read_number(&prop[4], na);
502                         bpool_host[5] = of_read_number(&prop[5], na);
503
504                         bpool->count = ((uint64_t)bpool_host[0] << 32) |
505                                         bpool_host[1];
506                         bpool->size = ((uint64_t)bpool_host[2] << 32) |
507                                         bpool_host[3];
508                         bpool->addr = ((uint64_t)bpool_host[4] << 32) |
509                                         bpool_host[5];
510                 }
511                 /* Parsing of the pool is complete, add it to the interface
512                  * list.
513                  */
514                 list_add_tail(&bpool->node, &__if->__if.bpool_list);
515                 lenp -= sizeof(phandle);
516                 pools_phandle++;
517         }
518
519         if (is_shared)
520                 __if->__if.is_shared_mac = 1;
521
522         /* Parsing of the network interface is complete, add it to the list */
523         DPAA_BUS_LOG(DEBUG, "Found %s, Tx Channel = %x, FMAN = %x,"
524                     "Port ID = %x",
525                     dname, __if->__if.tx_channel_id, __if->__if.fman_idx,
526                     __if->__if.mac_idx);
527
528         list_add_tail(&__if->__if.node, &__ifs);
529         return 0;
530 err:
531         if_destructor(__if);
532         return _errno;
533 }
534
535 int
536 fman_init(void)
537 {
538         const struct device_node *dpa_node, *parent_node;
539         int _errno;
540
541         /* If multiple dependencies try to initialise the Fman driver, don't
542          * panic.
543          */
544         if (fman_ccsr_map_fd != -1)
545                 return 0;
546
547         fman_ccsr_map_fd = open(FMAN_DEVICE_PATH, O_RDWR);
548         if (unlikely(fman_ccsr_map_fd < 0)) {
549                 DPAA_BUS_LOG(ERR, "Unable to open (/dev/mem)");
550                 return fman_ccsr_map_fd;
551         }
552
553         parent_node = of_find_compatible_node(NULL, NULL, "fsl,dpaa");
554         if (!parent_node) {
555                 DPAA_BUS_LOG(ERR, "Unable to find fsl,dpaa node");
556                 return -ENODEV;
557         }
558
559         for_each_child_node(parent_node, dpa_node) {
560                 _errno = fman_if_init(dpa_node);
561                 if (_errno) {
562                         FMAN_ERR(_errno, "if_init(%s)\n", dpa_node->full_name);
563                         goto err;
564                 }
565         }
566
567         return 0;
568 err:
569         fman_finish();
570         return _errno;
571 }
572
573 void
574 fman_finish(void)
575 {
576         struct __fman_if *__if, *tmpif;
577
578         assert(fman_ccsr_map_fd != -1);
579
580         list_for_each_entry_safe(__if, tmpif, &__ifs, __if.node) {
581                 int _errno;
582
583                 /* disable Rx and Tx */
584                 if ((__if->__if.mac_type == fman_mac_1g) &&
585                     (!__if->__if.is_memac))
586                         out_be32(__if->ccsr_map + 0x100,
587                                  in_be32(__if->ccsr_map + 0x100) & ~(u32)0x5);
588                 else
589                         out_be32(__if->ccsr_map + 8,
590                                  in_be32(__if->ccsr_map + 8) & ~(u32)3);
591                 /* release the mapping */
592                 _errno = munmap(__if->ccsr_map, __if->regs_size);
593                 if (unlikely(_errno < 0))
594                         fprintf(stderr, "%s:%d:%s(): munmap() = %d (%s)\n",
595                                 __FILE__, __LINE__, __func__,
596                                 -errno, strerror(errno));
597                 printf("Tearing down %s\n", __if->node_path);
598                 list_del(&__if->__if.node);
599                 rte_free(__if);
600         }
601
602         close(fman_ccsr_map_fd);
603         fman_ccsr_map_fd = -1;
604 }