New upstream version 18.08
[deb_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 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 <of.h>
15 #include <rte_dpaa_logs.h>
16
17 #define QMI_PORT_REGS_OFFSET            0x400
18
19 /* CCSR map address to access ccsr based register */
20 void *fman_ccsr_map;
21 /* fman version info */
22 u16 fman_ip_rev;
23 static int get_once;
24 u32 fman_dealloc_bufs_mask_hi;
25 u32 fman_dealloc_bufs_mask_lo;
26
27 int fman_ccsr_map_fd = -1;
28 static COMPAT_LIST_HEAD(__ifs);
29
30 /* This is the (const) global variable that callers have read-only access to.
31  * Internally, we have read-write access directly to __ifs.
32  */
33 const struct list_head *fman_if_list = &__ifs;
34
35 static void
36 if_destructor(struct __fman_if *__if)
37 {
38         struct fman_if_bpool *bp, *tmpbp;
39
40         if (!__if)
41                 return;
42
43         if (__if->__if.mac_type == fman_offline)
44                 goto cleanup;
45
46         list_for_each_entry_safe(bp, tmpbp, &__if->__if.bpool_list, node) {
47                 list_del(&bp->node);
48                 free(bp);
49         }
50 cleanup:
51         free(__if);
52 }
53
54 static int
55 fman_get_ip_rev(const struct device_node *fman_node)
56 {
57         const uint32_t *fman_addr;
58         uint64_t phys_addr;
59         uint64_t regs_size;
60         uint32_t ip_rev_1;
61         int _errno;
62
63         fman_addr = of_get_address(fman_node, 0, &regs_size, NULL);
64         if (!fman_addr) {
65                 pr_err("of_get_address cannot return fman address\n");
66                 return -EINVAL;
67         }
68         phys_addr = of_translate_address(fman_node, fman_addr);
69         if (!phys_addr) {
70                 pr_err("of_translate_address failed\n");
71                 return -EINVAL;
72         }
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");
77                 return -EINVAL;
78         }
79
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;
83
84         _errno = munmap(fman_ccsr_map, regs_size);
85         if (_errno)
86                 pr_err("munmap() of FMan ccsr failed");
87
88         return 0;
89 }
90
91 static int
92 fman_get_mac_index(uint64_t regs_addr_host, uint8_t *mac_idx)
93 {
94         int ret = 0;
95
96         /*
97          * MAC1 : E_0000h
98          * MAC2 : E_2000h
99          * MAC3 : E_4000h
100          * MAC4 : E_6000h
101          * MAC5 : E_8000h
102          * MAC6 : E_A000h
103          * MAC7 : E_C000h
104          * MAC8 : E_E000h
105          * MAC9 : F_0000h
106          * MAC10: F_2000h
107          */
108         switch (regs_addr_host) {
109         case 0xE0000:
110                 *mac_idx = 1;
111                 break;
112         case 0xE2000:
113                 *mac_idx = 2;
114                 break;
115         case 0xE4000:
116                 *mac_idx = 3;
117                 break;
118         case 0xE6000:
119                 *mac_idx = 4;
120                 break;
121         case 0xE8000:
122                 *mac_idx = 5;
123                 break;
124         case 0xEA000:
125                 *mac_idx = 6;
126                 break;
127         case 0xEC000:
128                 *mac_idx = 7;
129                 break;
130         case 0xEE000:
131                 *mac_idx = 8;
132                 break;
133         case 0xF0000:
134                 *mac_idx = 9;
135                 break;
136         case 0xF2000:
137                 *mac_idx = 10;
138                 break;
139         default:
140                 ret = -EINVAL;
141         }
142
143         return ret;
144 }
145
146 static int
147 fman_if_init(const struct device_node *dpa_node)
148 {
149         const char *rprop, *mprop;
150         uint64_t phys_addr;
151         struct __fman_if *__if;
152         struct fman_if_bpool *bpool;
153
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;
161
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;
167         size_t lenp;
168         int _errno;
169         const char *char_prop;
170         uint32_t na;
171
172         if (of_device_is_available(dpa_node) == false)
173                 return 0;
174
175         rprop = "fsl,qman-frame-queues-rx";
176         mprop = "fsl,fman-mac";
177
178         /* Allocate an object for this network interface */
179         __if = malloc(sizeof(*__if));
180         if (!__if) {
181                 FMAN_ERR(-ENOMEM, "malloc(%zu)\n", sizeof(*__if));
182                 goto err;
183         }
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';
188
189         /* Obtain the MAC node used by this interface except macless */
190         mac_phandle = of_get_property(dpa_node, mprop, &lenp);
191         if (!mac_phandle) {
192                 FMAN_ERR(-EINVAL, "%s: no %s\n", dname, mprop);
193                 goto err;
194         }
195         assert(lenp == sizeof(phandle));
196         mac_node = of_find_node_by_phandle(*mac_phandle);
197         if (!mac_node) {
198                 FMAN_ERR(-ENXIO, "%s: bad 'fsl,fman-mac\n", dname);
199                 goto err;
200         }
201         mname = mac_node->full_name;
202
203         /* Map the CCSR regs for the MAC node */
204         regs_addr = of_get_address(mac_node, 0, &__if->regs_size, NULL);
205         if (!regs_addr) {
206                 FMAN_ERR(-EINVAL, "of_get_address(%s)\n", mname);
207                 goto err;
208         }
209         phys_addr = of_translate_address(mac_node, regs_addr);
210         if (!phys_addr) {
211                 FMAN_ERR(-EINVAL, "of_translate_address(%s, %p)\n",
212                          mname, regs_addr);
213                 goto err;
214         }
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);
220                 goto err;
221         }
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);
225
226
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);
230         if (!fman_node) {
231                 FMAN_ERR(-ENXIO, "of_get_parent(%s)\n", mname);
232                 goto err;
233         }
234         fname = fman_node->full_name;
235         cell_idx = of_get_property(fman_node, "cell-index", &lenp);
236         if (!cell_idx) {
237                 FMAN_ERR(-ENXIO, "%s: no cell-index)\n", fname);
238                 goto err;
239         }
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;
243         if (!get_once) {
244                 _errno = fman_get_ip_rev(fman_node);
245                 if (_errno) {
246                         FMAN_ERR(-ENXIO, "%s: ip_rev is not available\n",
247                                  fname);
248                         goto err;
249                 }
250         }
251
252         if (fman_ip_rev >= FMAN_V3) {
253                 /*
254                  * Set A2V, OVOM, EBD bits in contextA to allow external
255                  * buffer deallocation by fman.
256                  */
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;
260         } else {
261                 fman_dealloc_bufs_mask_hi = 0;
262                 fman_dealloc_bufs_mask_lo = 0;
263         }
264         /* Is the MAC node 1G, 10G? */
265         __if->__if.is_memac = 0;
266
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",
274                                             NULL);
275                 if (!char_prop) {
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;
279                 } else {
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;
287                 }
288         } else {
289                 FMAN_ERR(-EINVAL, "%s: unknown MAC type\n", mname);
290                 goto err;
291         }
292
293         /*
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.
299          */
300
301         _errno = fman_get_mac_index(regs_addr_host, &__if->__if.mac_idx);
302         if (_errno) {
303                 FMAN_ERR(-EINVAL, "Invalid register address: %" PRIx64,
304                          regs_addr_host);
305                 goto err;
306         }
307
308         /* Extract the MAC address for private and shared interfaces */
309         mac_addr = of_get_property(mac_node, "local-mac-address",
310                                    &lenp);
311         if (!mac_addr) {
312                 FMAN_ERR(-EINVAL, "%s: no local-mac-address\n",
313                          mname);
314                 goto err;
315         }
316         memcpy(&__if->__if.mac_addr, mac_addr, ETHER_ADDR_LEN);
317
318         /* Extract the Tx port (it's the second of the two port handles)
319          * and get its channel ID
320          */
321         ports_phandle = of_get_property(mac_node, "fsl,port-handles",
322                                         &lenp);
323         if (!ports_phandle)
324                 ports_phandle = of_get_property(mac_node, "fsl,fman-ports",
325                                                 &lenp);
326         if (!ports_phandle) {
327                 FMAN_ERR(-EINVAL, "%s: no fsl,port-handles\n",
328                          mname);
329                 goto err;
330         }
331         assert(lenp == (2 * sizeof(phandle)));
332         tx_node = of_find_node_by_phandle(ports_phandle[1]);
333         if (!tx_node) {
334                 FMAN_ERR(-ENXIO, "%s: bad fsl,port-handle[1]\n", mname);
335                 goto err;
336         }
337         /* Extract the channel ID (from tx-port-handle) */
338         tx_channel_id = of_get_property(tx_node, "fsl,qman-channel-id",
339                                         &lenp);
340         if (!tx_channel_id) {
341                 FMAN_ERR(-EINVAL, "%s: no fsl-qman-channel-id\n",
342                          tx_node->full_name);
343                 goto err;
344         }
345
346         rx_node = of_find_node_by_phandle(ports_phandle[0]);
347         if (!rx_node) {
348                 FMAN_ERR(-ENXIO, "%s: bad fsl,port-handle[0]\n", mname);
349                 goto err;
350         }
351         regs_addr = of_get_address(rx_node, 0, &__if->regs_size, NULL);
352         if (!regs_addr) {
353                 FMAN_ERR(-EINVAL, "of_get_address(%s)\n", mname);
354                 goto err;
355         }
356         phys_addr = of_translate_address(rx_node, regs_addr);
357         if (!phys_addr) {
358                 FMAN_ERR(-EINVAL, "of_translate_address(%s, %p)\n",
359                          mname, regs_addr);
360                 goto err;
361         }
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);
367                 goto err;
368         }
369
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);
374
375         /* Extract the Rx FQIDs. (Note, the device representation is silly,
376          * there are "counts" that must always be 1.)
377          */
378         rx_phandle = of_get_property(dpa_node, rprop, &lenp);
379         if (!rx_phandle) {
380                 FMAN_ERR(-EINVAL, "%s: no fsl,qman-frame-queues-rx\n", dname);
381                 goto err;
382         }
383
384         assert(lenp == (4 * sizeof(phandle)));
385
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);
392
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];
396
397         /* Extract the Tx FQIDs */
398         tx_phandle = of_get_property(dpa_node,
399                                      "fsl,qman-frame-queues-tx", &lenp);
400         if (!tx_phandle) {
401                 FMAN_ERR(-EINVAL, "%s: no fsl,qman-frame-queues-tx\n", dname);
402                 goto err;
403         }
404
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];
416
417         /* Obtain the buffer pool nodes used by this interface */
418         pools_phandle = of_get_property(dpa_node, "fsl,bman-buffer-pools",
419                                         &lenp);
420         if (!pools_phandle) {
421                 FMAN_ERR(-EINVAL, "%s: no fsl,bman-buffer-pools\n", dname);
422                 goto err;
423         }
424         /* For each pool, parse the corresponding node and add a pool object
425          * to the interface's "bpool_list"
426          */
427         assert(lenp && !(lenp % sizeof(phandle)));
428         while (lenp) {
429                 size_t proplen;
430                 const phandle *prop;
431                 uint64_t bpid_host = 0;
432                 uint64_t bpool_host[6] = {0};
433                 const char *pname;
434                 /* Allocate an object for the pool */
435                 bpool = malloc(sizeof(*bpool));
436                 if (!bpool) {
437                         FMAN_ERR(-ENOMEM, "malloc(%zu)\n", sizeof(*bpool));
438                         goto err;
439                 }
440                 /* Find the pool node */
441                 pool_node = of_find_node_by_phandle(*pools_phandle);
442                 if (!pool_node) {
443                         FMAN_ERR(-ENXIO, "%s: bad fsl,bman-buffer-pools\n",
444                                  dname);
445                         free(bpool);
446                         goto err;
447                 }
448                 pname = pool_node->full_name;
449                 /* Extract the BPID property */
450                 prop = of_get_property(pool_node, "fsl,bpid", &proplen);
451                 if (!prop) {
452                         FMAN_ERR(-EINVAL, "%s: no fsl,bpid\n", pname);
453                         free(bpool);
454                         goto err;
455                 }
456                 assert(proplen == sizeof(*prop));
457                 na = of_n_addr_cells(mac_node);
458                 /* Get rid of endianness (issues).
459                  * Convert to host byte-order
460                  */
461                 bpid_host = of_read_number(prop, na);
462                 bpool->bpid = bpid_host;
463                 /* Extract the cfg property (count/size/addr). "fsl,bpool-cfg"
464                  * indicates for the Bman driver to seed the pool.
465                  * "fsl,bpool-ethernet-cfg" is used by the network driver. The
466                  * two are mutually exclusive, so check for either of them.
467                  */
468                 prop = of_get_property(pool_node, "fsl,bpool-cfg",
469                                        &proplen);
470                 if (!prop)
471                         prop = of_get_property(pool_node,
472                                                "fsl,bpool-ethernet-cfg",
473                                                &proplen);
474                 if (!prop) {
475                         /* It's OK for there to be no bpool-cfg */
476                         bpool->count = bpool->size = bpool->addr = 0;
477                 } else {
478                         assert(proplen == (6 * sizeof(*prop)));
479                         na = of_n_addr_cells(mac_node);
480                         /* Get rid of endianness (issues).
481                          * Convert to host byte order
482                          */
483                         bpool_host[0] = of_read_number(&prop[0], na);
484                         bpool_host[1] = of_read_number(&prop[1], na);
485                         bpool_host[2] = of_read_number(&prop[2], na);
486                         bpool_host[3] = of_read_number(&prop[3], na);
487                         bpool_host[4] = of_read_number(&prop[4], na);
488                         bpool_host[5] = of_read_number(&prop[5], na);
489
490                         bpool->count = ((uint64_t)bpool_host[0] << 32) |
491                                         bpool_host[1];
492                         bpool->size = ((uint64_t)bpool_host[2] << 32) |
493                                         bpool_host[3];
494                         bpool->addr = ((uint64_t)bpool_host[4] << 32) |
495                                         bpool_host[5];
496                 }
497                 /* Parsing of the pool is complete, add it to the interface
498                  * list.
499                  */
500                 list_add_tail(&bpool->node, &__if->__if.bpool_list);
501                 lenp -= sizeof(phandle);
502                 pools_phandle++;
503         }
504
505         /* Parsing of the network interface is complete, add it to the list */
506         DPAA_BUS_LOG(DEBUG, "Found %s, Tx Channel = %x, FMAN = %x,"
507                     "Port ID = %x",
508                     dname, __if->__if.tx_channel_id, __if->__if.fman_idx,
509                     __if->__if.mac_idx);
510
511         list_add_tail(&__if->__if.node, &__ifs);
512         return 0;
513 err:
514         if_destructor(__if);
515         return _errno;
516 }
517
518 int
519 fman_init(void)
520 {
521         const struct device_node *dpa_node;
522         int _errno;
523
524         /* If multiple dependencies try to initialise the Fman driver, don't
525          * panic.
526          */
527         if (fman_ccsr_map_fd != -1)
528                 return 0;
529
530         fman_ccsr_map_fd = open(FMAN_DEVICE_PATH, O_RDWR);
531         if (unlikely(fman_ccsr_map_fd < 0)) {
532                 DPAA_BUS_LOG(ERR, "Unable to open (/dev/mem)");
533                 return fman_ccsr_map_fd;
534         }
535
536         for_each_compatible_node(dpa_node, NULL, "fsl,dpa-ethernet-init") {
537                 _errno = fman_if_init(dpa_node);
538                 if (_errno) {
539                         FMAN_ERR(_errno, "if_init(%s)\n", dpa_node->full_name);
540                         goto err;
541                 }
542         }
543
544         return 0;
545 err:
546         fman_finish();
547         return _errno;
548 }
549
550 void
551 fman_finish(void)
552 {
553         struct __fman_if *__if, *tmpif;
554
555         assert(fman_ccsr_map_fd != -1);
556
557         list_for_each_entry_safe(__if, tmpif, &__ifs, __if.node) {
558                 int _errno;
559
560                 /* disable Rx and Tx */
561                 if ((__if->__if.mac_type == fman_mac_1g) &&
562                     (!__if->__if.is_memac))
563                         out_be32(__if->ccsr_map + 0x100,
564                                  in_be32(__if->ccsr_map + 0x100) & ~(u32)0x5);
565                 else
566                         out_be32(__if->ccsr_map + 8,
567                                  in_be32(__if->ccsr_map + 8) & ~(u32)3);
568                 /* release the mapping */
569                 _errno = munmap(__if->ccsr_map, __if->regs_size);
570                 if (unlikely(_errno < 0))
571                         fprintf(stderr, "%s:%d:%s(): munmap() = %d (%s)\n",
572                                 __FILE__, __LINE__, __func__,
573                                 -errno, strerror(errno));
574                 printf("Tearing down %s\n", __if->node_path);
575                 list_del(&__if->__if.node);
576                 free(__if);
577         }
578
579         close(fman_ccsr_map_fd);
580         fman_ccsr_map_fd = -1;
581 }