replace snprintf with strlcpy
[dpdk.git] / drivers / bus / dpaa / base / fman / of.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 <of.h>
9 #include <rte_string_fns.h>
10 #include <rte_dpaa_logs.h>
11
12 static int alive;
13 static struct dt_dir root_dir;
14 static const char *base_dir;
15 static COMPAT_LIST_HEAD(linear);
16
17 static int
18 of_open_dir(const char *relative_path, struct dirent ***d)
19 {
20         int ret;
21         char full_path[PATH_MAX];
22
23         snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
24         ret = scandir(full_path, d, 0, versionsort);
25         if (ret < 0)
26                 DPAA_BUS_LOG(ERR, "Failed to open directory %s",
27                              full_path);
28         return ret;
29 }
30
31 static void
32 of_close_dir(struct dirent **d, int num)
33 {
34         while (num--)
35                 free(d[num]);
36         free(d);
37 }
38
39 static int
40 of_open_file(const char *relative_path)
41 {
42         int ret;
43         char full_path[PATH_MAX];
44
45         snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
46         ret = open(full_path, O_RDONLY);
47         if (ret < 0)
48                 DPAA_BUS_LOG(ERR, "Failed to open directory %s",
49                              full_path);
50         return ret;
51 }
52
53 static void
54 process_file(struct dirent *dent, struct dt_dir *parent)
55 {
56         int fd;
57         struct dt_file *f = malloc(sizeof(*f));
58
59         if (!f) {
60                 DPAA_BUS_LOG(DEBUG, "Unable to allocate memory for file node");
61                 return;
62         }
63         f->node.is_file = 1;
64         strlcpy(f->node.node.name, dent->d_name, NAME_MAX);
65         snprintf(f->node.node.full_name, PATH_MAX, "%s/%s",
66                  parent->node.node.full_name, dent->d_name);
67         f->parent = parent;
68         fd = of_open_file(f->node.node.full_name);
69         if (fd < 0) {
70                 DPAA_BUS_LOG(DEBUG, "Unable to open file node");
71                 free(f);
72                 return;
73         }
74         f->len = read(fd, f->buf, OF_FILE_BUF_MAX);
75         close(fd);
76         if (f->len < 0) {
77                 DPAA_BUS_LOG(DEBUG, "Unable to read file node");
78                 free(f);
79                 return;
80         }
81         list_add_tail(&f->node.list, &parent->files);
82 }
83
84 static const struct dt_dir *
85 node2dir(const struct device_node *n)
86 {
87         struct dt_node *dn = container_of((struct device_node *)n,
88                                           struct dt_node, node);
89         const struct dt_dir *d = container_of(dn, struct dt_dir, node);
90
91         assert(!dn->is_file);
92         return d;
93 }
94
95 /* process_dir() calls iterate_dir(), but the latter will also call the former
96  * when recursing into sub-directories, so a predeclaration is needed.
97  */
98 static int process_dir(const char *relative_path, struct dt_dir *dt);
99
100 static int
101 iterate_dir(struct dirent **d, int num, struct dt_dir *dt)
102 {
103         int loop;
104         /* Iterate the directory contents */
105         for (loop = 0; loop < num; loop++) {
106                 struct dt_dir *subdir;
107                 int ret;
108                 /* Ignore dot files of all types (especially "..") */
109                 if (d[loop]->d_name[0] == '.')
110                         continue;
111                 switch (d[loop]->d_type) {
112                 case DT_REG:
113                         process_file(d[loop], dt);
114                         break;
115                 case DT_DIR:
116                         subdir = malloc(sizeof(*subdir));
117                         if (!subdir) {
118                                 perror("malloc");
119                                 return -ENOMEM;
120                         }
121                         strlcpy(subdir->node.node.name, d[loop]->d_name,
122                                 NAME_MAX);
123                         snprintf(subdir->node.node.full_name, PATH_MAX,
124                                  "%s/%s", dt->node.node.full_name,
125                                  d[loop]->d_name);
126                         subdir->parent = dt;
127                         ret = process_dir(subdir->node.node.full_name, subdir);
128                         if (ret)
129                                 return ret;
130                         list_add_tail(&subdir->node.list, &dt->subdirs);
131                         break;
132                 default:
133                         DPAA_BUS_LOG(DEBUG, "Ignoring invalid dt entry %s/%s",
134                                      dt->node.node.full_name, d[loop]->d_name);
135                 }
136         }
137         return 0;
138 }
139
140 static int
141 process_dir(const char *relative_path, struct dt_dir *dt)
142 {
143         struct dirent **d;
144         int ret, num;
145
146         dt->node.is_file = 0;
147         INIT_LIST_HEAD(&dt->subdirs);
148         INIT_LIST_HEAD(&dt->files);
149         ret = of_open_dir(relative_path, &d);
150         if (ret < 0)
151                 return ret;
152         num = ret;
153         ret = iterate_dir(d, num, dt);
154         of_close_dir(d, num);
155         return (ret < 0) ? ret : 0;
156 }
157
158 static void
159 linear_dir(struct dt_dir *d)
160 {
161         struct dt_file *f;
162         struct dt_dir *dd;
163
164         d->compatible = NULL;
165         d->status = NULL;
166         d->lphandle = NULL;
167         d->a_cells = NULL;
168         d->s_cells = NULL;
169         d->reg = NULL;
170         list_for_each_entry(f, &d->files, node.list) {
171                 if (!strcmp(f->node.node.name, "compatible")) {
172                         if (d->compatible)
173                                 DPAA_BUS_LOG(DEBUG, "Duplicate compatible in"
174                                              " %s", d->node.node.full_name);
175                         d->compatible = f;
176                 } else if (!strcmp(f->node.node.name, "status")) {
177                         if (d->status)
178                                 DPAA_BUS_LOG(DEBUG, "Duplicate status in %s",
179                                              d->node.node.full_name);
180                         d->status = f;
181                 } else if (!strcmp(f->node.node.name, "linux,phandle")) {
182                         if (d->lphandle)
183                                 DPAA_BUS_LOG(DEBUG, "Duplicate lphandle in %s",
184                                              d->node.node.full_name);
185                         d->lphandle = f;
186                 } else if (!strcmp(f->node.node.name, "phandle")) {
187                         if (d->lphandle)
188                                 DPAA_BUS_LOG(DEBUG, "Duplicate lphandle in %s",
189                                              d->node.node.full_name);
190                         d->lphandle = f;
191                 } else if (!strcmp(f->node.node.name, "#address-cells")) {
192                         if (d->a_cells)
193                                 DPAA_BUS_LOG(DEBUG, "Duplicate a_cells in %s",
194                                              d->node.node.full_name);
195                         d->a_cells = f;
196                 } else if (!strcmp(f->node.node.name, "#size-cells")) {
197                         if (d->s_cells)
198                                 DPAA_BUS_LOG(DEBUG, "Duplicate s_cells in %s",
199                                              d->node.node.full_name);
200                         d->s_cells = f;
201                 } else if (!strcmp(f->node.node.name, "reg")) {
202                         if (d->reg)
203                                 DPAA_BUS_LOG(DEBUG, "Duplicate reg in %s",
204                                              d->node.node.full_name);
205                         d->reg = f;
206                 }
207         }
208
209         list_for_each_entry(dd, &d->subdirs, node.list) {
210                 list_add_tail(&dd->linear, &linear);
211                 linear_dir(dd);
212         }
213 }
214
215 int
216 of_init_path(const char *dt_path)
217 {
218         int ret;
219
220         base_dir = dt_path;
221
222         /* This needs to be singleton initialization */
223         DPAA_BUS_HWWARN(alive, "Double-init of device-tree driver!");
224
225         /* Prepare root node (the remaining fields are set in process_dir()) */
226         root_dir.node.node.name[0] = '\0';
227         root_dir.node.node.full_name[0] = '\0';
228         INIT_LIST_HEAD(&root_dir.node.list);
229         root_dir.parent = NULL;
230
231         /* Kick things off... */
232         ret = process_dir("", &root_dir);
233         if (ret) {
234                 DPAA_BUS_LOG(ERR, "Unable to parse device tree");
235                 return ret;
236         }
237
238         /* Now make a flat, linear list of directories */
239         linear_dir(&root_dir);
240         alive = 1;
241         return 0;
242 }
243
244 static void
245 destroy_dir(struct dt_dir *d)
246 {
247         struct dt_file *f, *tmpf;
248         struct dt_dir *dd, *tmpd;
249
250         list_for_each_entry_safe(f, tmpf, &d->files, node.list) {
251                 list_del(&f->node.list);
252                 free(f);
253         }
254         list_for_each_entry_safe(dd, tmpd, &d->subdirs, node.list) {
255                 destroy_dir(dd);
256                 list_del(&dd->node.list);
257                 free(dd);
258         }
259 }
260
261 void
262 of_finish(void)
263 {
264         DPAA_BUS_HWWARN(!alive, "Double-finish of device-tree driver!");
265
266         destroy_dir(&root_dir);
267         INIT_LIST_HEAD(&linear);
268         alive = 0;
269 }
270
271 static const struct dt_dir *
272 next_linear(const struct dt_dir *f)
273 {
274         if (f->linear.next == &linear)
275                 return NULL;
276         return list_entry(f->linear.next, struct dt_dir, linear);
277 }
278
279 static int
280 check_compatible(const struct dt_file *f, const char *compatible)
281 {
282         const char *c = (char *)f->buf;
283         unsigned int len, remains = f->len;
284
285         while (remains) {
286                 len = strlen(c);
287                 if (!strcmp(c, compatible))
288                         return 1;
289
290                 if (remains < len + 1)
291                         break;
292
293                 c += (len + 1);
294                 remains -= (len + 1);
295         }
296         return 0;
297 }
298
299 const struct device_node *
300 of_find_compatible_node(const struct device_node *from,
301                         const char *type __always_unused,
302                         const char *compatible)
303 {
304         const struct dt_dir *d;
305
306         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
307
308         if (list_empty(&linear))
309                 return NULL;
310         if (!from)
311                 d = list_entry(linear.next, struct dt_dir, linear);
312         else
313                 d = node2dir(from);
314         for (d = next_linear(d); d && (!d->compatible ||
315                                        !check_compatible(d->compatible,
316                                        compatible));
317                         d = next_linear(d))
318                 ;
319         if (d)
320                 return &d->node.node;
321         return NULL;
322 }
323
324 const void *
325 of_get_property(const struct device_node *from, const char *name,
326                 size_t *lenp)
327 {
328         const struct dt_dir *d;
329         const struct dt_file *f;
330
331         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
332
333         d = node2dir(from);
334         list_for_each_entry(f, &d->files, node.list)
335                 if (!strcmp(f->node.node.name, name)) {
336                         if (lenp)
337                                 *lenp = f->len;
338                         return f->buf;
339                 }
340         return NULL;
341 }
342
343 bool
344 of_device_is_available(const struct device_node *dev_node)
345 {
346         const struct dt_dir *d;
347
348         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
349         d = node2dir(dev_node);
350         if (!d->status)
351                 return true;
352         if (!strcmp((char *)d->status->buf, "okay"))
353                 return true;
354         if (!strcmp((char *)d->status->buf, "ok"))
355                 return true;
356         return false;
357 }
358
359 const struct device_node *
360 of_find_node_by_phandle(phandle ph)
361 {
362         const struct dt_dir *d;
363
364         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
365         list_for_each_entry(d, &linear, linear)
366                 if (d->lphandle && (d->lphandle->len == 4) &&
367                     !memcmp(d->lphandle->buf, &ph, 4))
368                         return &d->node.node;
369         return NULL;
370 }
371
372 const struct device_node *
373 of_get_parent(const struct device_node *dev_node)
374 {
375         const struct dt_dir *d;
376
377         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
378
379         if (!dev_node)
380                 return NULL;
381         d = node2dir(dev_node);
382         if (!d->parent)
383                 return NULL;
384         return &d->parent->node.node;
385 }
386
387 const struct device_node *
388 of_get_next_child(const struct device_node *dev_node,
389                   const struct device_node *prev)
390 {
391         const struct dt_dir *p, *c;
392
393         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
394
395         if (!dev_node)
396                 return NULL;
397         p = node2dir(dev_node);
398         if (prev) {
399                 c = node2dir(prev);
400                 DPAA_BUS_HWWARN((c->parent != p), "Parent/child mismatch");
401                 if (c->parent != p)
402                         return NULL;
403                 if (c->node.list.next == &p->subdirs)
404                         /* prev was the last child */
405                         return NULL;
406                 c = list_entry(c->node.list.next, struct dt_dir, node.list);
407                 return &c->node.node;
408         }
409         /* Return first child */
410         if (list_empty(&p->subdirs))
411                 return NULL;
412         c = list_entry(p->subdirs.next, struct dt_dir, node.list);
413         return &c->node.node;
414 }
415
416 uint32_t
417 of_n_addr_cells(const struct device_node *dev_node)
418 {
419         const struct dt_dir *d;
420
421         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised");
422         if (!dev_node)
423                 return OF_DEFAULT_NA;
424         d = node2dir(dev_node);
425         while ((d = d->parent))
426                 if (d->a_cells) {
427                         unsigned char *buf =
428                                 (unsigned char *)&d->a_cells->buf[0];
429                         assert(d->a_cells->len == 4);
430                         return ((uint32_t)buf[0] << 24) |
431                                 ((uint32_t)buf[1] << 16) |
432                                 ((uint32_t)buf[2] << 8) |
433                                 (uint32_t)buf[3];
434                 }
435         return OF_DEFAULT_NA;
436 }
437
438 uint32_t
439 of_n_size_cells(const struct device_node *dev_node)
440 {
441         const struct dt_dir *d;
442
443         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
444         if (!dev_node)
445                 return OF_DEFAULT_NA;
446         d = node2dir(dev_node);
447         while ((d = d->parent))
448                 if (d->s_cells) {
449                         unsigned char *buf =
450                                 (unsigned char *)&d->s_cells->buf[0];
451                         assert(d->s_cells->len == 4);
452                         return ((uint32_t)buf[0] << 24) |
453                                 ((uint32_t)buf[1] << 16) |
454                                 ((uint32_t)buf[2] << 8) |
455                                 (uint32_t)buf[3];
456                 }
457         return OF_DEFAULT_NS;
458 }
459
460 const uint32_t *
461 of_get_address(const struct device_node *dev_node, size_t idx,
462                uint64_t *size, uint32_t *flags __rte_unused)
463 {
464         const struct dt_dir *d;
465         const unsigned char *buf;
466         uint32_t na = of_n_addr_cells(dev_node);
467         uint32_t ns = of_n_size_cells(dev_node);
468
469         if (!dev_node)
470                 d = &root_dir;
471         else
472                 d = node2dir(dev_node);
473         if (!d->reg)
474                 return NULL;
475         assert(d->reg->len % ((na + ns) * 4) == 0);
476         assert(d->reg->len / ((na + ns) * 4) > (unsigned int) idx);
477         buf = (const unsigned char *)&d->reg->buf[0];
478         buf += (na + ns) * idx * 4;
479         if (size)
480                 for (*size = 0; ns > 0; ns--, na++)
481                         *size = (*size << 32) +
482                                 (((uint32_t)buf[4 * na] << 24) |
483                                 ((uint32_t)buf[4 * na + 1] << 16) |
484                                 ((uint32_t)buf[4 * na + 2] << 8) |
485                                 (uint32_t)buf[4 * na + 3]);
486         return (const uint32_t *)buf;
487 }
488
489 uint64_t
490 of_translate_address(const struct device_node *dev_node,
491                      const uint32_t *addr)
492 {
493         uint64_t phys_addr, tmp_addr;
494         const struct device_node *parent;
495         const uint32_t *ranges;
496         size_t rlen;
497         uint32_t na, pna;
498
499         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
500         assert(dev_node != NULL);
501
502         na = of_n_addr_cells(dev_node);
503         phys_addr = of_read_number(addr, na);
504
505         dev_node = of_get_parent(dev_node);
506         if (!dev_node)
507                 return 0;
508         else if (node2dir(dev_node) == &root_dir)
509                 return phys_addr;
510
511         do {
512                 pna = of_n_addr_cells(dev_node);
513                 parent = of_get_parent(dev_node);
514                 if (!parent)
515                         return 0;
516
517                 ranges = of_get_property(dev_node, "ranges", &rlen);
518                 /* "ranges" property is missing. Translation breaks */
519                 if (!ranges)
520                         return 0;
521                 /* "ranges" property is empty. Do 1:1 translation */
522                 else if (rlen == 0)
523                         continue;
524                 else
525                         tmp_addr = of_read_number(ranges + na, pna);
526
527                 na = pna;
528                 dev_node = parent;
529                 phys_addr += tmp_addr;
530         } while (node2dir(parent) != &root_dir);
531
532         return phys_addr;
533 }
534
535 bool
536 of_device_is_compatible(const struct device_node *dev_node,
537                         const char *compatible)
538 {
539         const struct dt_dir *d;
540
541         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
542         if (!dev_node)
543                 d = &root_dir;
544         else
545                 d = node2dir(dev_node);
546         if (d->compatible && check_compatible(d->compatible, compatible))
547                 return true;
548         return false;
549 }
550
551 static const void *of_get_mac_addr(const struct device_node *np,
552                 const char *name)
553 {
554         return of_get_property(np, name, NULL);
555 }
556
557 /**
558  * Search the device tree for the best MAC address to use.  'mac-address' is
559  * checked first, because that is supposed to contain to "most recent" MAC
560  * address. If that isn't set, then 'local-mac-address' is checked next,
561  * because that is the default address.  If that isn't set, then the obsolete
562  * 'address' is checked, just in case we're using an old device tree.
563  *
564  * Note that the 'address' property is supposed to contain a virtual address of
565  * the register set, but some DTS files have redefined that property to be the
566  * MAC address.
567  *
568  * All-zero MAC addresses are rejected, because those could be properties that
569  * exist in the device tree, but were not set by U-Boot.  For example, the
570  * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
571  * addresses.  Some older U-Boots only initialized 'local-mac-address'.  In
572  * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
573  * but is all zeros.
574  */
575 const void *of_get_mac_address(const struct device_node *np)
576 {
577         const void *addr;
578
579         addr = of_get_mac_addr(np, "mac-address");
580         if (addr)
581                 return addr;
582
583         addr = of_get_mac_addr(np, "local-mac-address");
584         if (addr)
585                 return addr;
586
587         return of_get_mac_addr(np, "address");
588 }