source: rtems/cpukit/libmisc/shell/main_pci.c @ 1f22b26

5
Last change on this file since 1f22b26 was 1f22b26, checked in by Sebastian Huber <sebastian.huber@…>, on 08/25/17 at 08:59:52

Include missing <limits.h>

Update #2132.

  • Property mode set to 100644
File size: 11.6 KB
Line 
1/*  LIBPCI Command Implementation
2 *
3 *  COPYRIGHT (c) 2010.
4 *  Cobham Gaisler AB.
5 *
6 *  The license and distribution terms for this file may be
7 *  found in the file LICENSE in this distribution or at
8 *  http://www.rtems.com/license/LICENSE.
9 */
10
11#ifdef HAVE_CONFIG_H
12#include "config.h"
13#endif
14
15#include <limits.h>
16#include <stdlib.h>
17#include <stdio.h>
18#include <string.h>
19#include <errno.h>
20#include <pci.h>
21#include <pci/cfg.h>
22#include <pci/access.h>
23#include <sys/endian.h>
24#include <bsp.h> /* For PCI endianness config */
25
26#include <rtems.h>
27#include <rtems/shell.h>
28#include "internal.h"
29
30static void usage(void);
31
32struct shell_pci_modifier {
33  char *name;
34  int (*func)(int argc, char *argv[], struct shell_pci_modifier *mod);
35  int data;
36};
37
38static unsigned long get_pciid_from_string(char *arg)
39{
40  unsigned long pciid;
41  char *bus_str, *dev_str, *fun_str;
42  unsigned long busno, devno, funno;
43
44  dev_str = strstr(arg, ":");
45  if (dev_str == NULL) {
46    /* PCIID */
47    pciid = strtoul(arg, NULL, 16);
48    if (pciid == ULONG_MAX)
49      return ~0;
50  } else {
51    /* bus:dev:fun */
52    bus_str = arg;
53    *dev_str = '\0';
54    dev_str++;
55    fun_str = strstr(dev_str, ":");
56    if (fun_str == NULL)
57      return ~0;
58    *fun_str = '\0';
59    fun_str++;
60
61    busno = strtoul(bus_str, NULL, 16);
62    if (busno == ULONG_MAX)
63      return ~0;
64    devno = strtoul(dev_str, NULL, 16);
65    if (devno == ULONG_MAX)
66      return ~0;
67    funno = strtoul(fun_str, NULL, 16);
68    if (funno == ULONG_MAX)
69      return ~0;
70    pciid = PCI_DEV(busno, devno, funno);
71  }
72
73  return pciid;
74}
75
76/* Print current PCI configuration that can be used in a static/peripheral PCI
77 * configuration setup.
78 */
79static int shell_pci_pcfg(
80        int argc,
81        char *argv[],
82        struct shell_pci_modifier *mod)
83{
84  if (argc != 2)
85    return -1;
86
87  pci_cfg_print();
88
89  return 0;
90}
91
92static int shell_pci_ls(
93        int argc,
94        char *argv[],
95        struct shell_pci_modifier *mod)
96{
97  unsigned long pciid;
98
99  if (argc == 2) {
100    /* List all devices */
101    pci_print();
102  } else if (argc > 3) {
103    return -1;
104  } else {
105    pciid = get_pciid_from_string(argv[2]);
106    if ((pciid & 0xffff0000) != 0)
107      return -1;
108
109    pci_print_dev((pci_dev_t)pciid);
110  }
111  return 0;
112}
113
114static int shell_pci_rX(
115        unsigned long pciid,
116        int offset,
117        int size)
118{
119  uint8_t data8;
120  uint16_t data16;
121  uint32_t data32;
122  int result;
123
124  switch(size) {
125    case 1:
126      result = pci_cfg_r8(pciid, offset, &data8);
127      if (result == PCISTS_OK)
128        printf(" r08[0x%02x]: 0x%02x  DEC=%d\n", offset, data8, data8);
129      break;
130
131    case 2:
132      result = pci_cfg_r16(pciid, offset, &data16);
133      if (result == PCISTS_OK)
134        printf(" r16[0x%02x]: 0x%04x  DEC=%d\n", offset, data16, data16);
135      break;
136
137    case 4:
138      result = pci_cfg_r32(pciid, offset, &data32);
139      if (result == PCISTS_OK)
140        printf(" r32[0x%02x]: 0x%08lx  DEC=%lu\n", offset, data32, data32);
141      break;
142
143    default:
144      return PCISTS_EINVAL;
145  }
146  return result;
147}
148
149static int shell_pci_wX(
150        unsigned long pciid,
151        int offset,
152        uint32_t data,
153        int size)
154{
155  uint8_t data8;
156  uint16_t data16;
157  int result;
158
159  switch(size) {
160    case 1:
161      if (data > 0xff)
162        return PCISTS_EINVAL;
163      data8 = data & 0xff;
164      result = pci_cfg_w8(pciid, offset, data8);
165      if (result == PCISTS_OK)
166        printf(" w08[0x%02x]: 0x%02x  DEC=%d\n", offset, data8, data8);
167      break;
168
169    case 2:
170      if (data > 0xffff)
171        return PCISTS_EINVAL;
172      data16 = data & 0xffff;
173      result = pci_cfg_w16(pciid, offset, data16);
174      if (result == PCISTS_OK)
175        printf(" w16[0x%02x]: 0x%04x  DEC=%d\n", offset, data16, data16);
176      break;
177
178    case 4:
179      result = pci_cfg_w32(pciid, offset, data);
180      if (result == PCISTS_OK)
181        printf(" w32[0x%02x]: 0x%08lx  DEC=%lu\n", offset, data, data);
182      break;
183
184    default:
185      return PCISTS_EINVAL;
186  }
187  return result;
188}
189
190static int shell_pci_read(
191        int argc,
192        char *argv[],
193        struct shell_pci_modifier *mod)
194{
195  unsigned long pciid, offset;
196  int result, size;
197
198  if (argc != 4)
199    return -1;
200
201  pciid = get_pciid_from_string(argv[2]);
202  if ((pciid & 0xffff0000) != 0)
203    return -1;
204
205  offset = strtoul(argv[3], NULL, 0);
206  if (offset > 256)
207    return -1;
208
209  size = mod->data;
210  result = shell_pci_rX(pciid, offset, size);
211  switch (result) {
212    default:
213    case PCISTS_OK:
214      break;
215
216    case PCISTS_ERR:
217    case PCISTS_EINVAL:
218      puts(" Bad input argument\n");
219      return PCISTS_EINVAL;
220
221    case PCISTS_MSTABRT:
222      puts(" Master abort while reading configuration space");
223      return PCISTS_MSTABRT;
224  }
225
226  return 0;
227}
228
229static int shell_pci_write(
230        int argc,
231        char *argv[],
232        struct shell_pci_modifier *mod)
233{
234  unsigned long pciid, offset;
235  int result, size;
236  uint32_t data;
237
238  if (argc != 5)
239    return -1;
240
241  pciid = get_pciid_from_string(argv[2]);
242  if ((pciid & 0xffff0000) != 0)
243    return -1;
244
245  offset = strtoul(argv[3], NULL, 0);
246  if (offset > 256)
247    return -1;
248
249  data = strtoul(argv[4], NULL, 0);
250  if (data == ULONG_MAX && errno == ERANGE)
251    return -1;
252
253  size = mod->data;
254  result = shell_pci_wX(pciid, offset, data, size);
255  switch (result) {
256    default:
257    case PCISTS_OK:
258      break;
259
260    case PCISTS_ERR:
261    case PCISTS_EINVAL:
262      puts(" Bad input argument\n");
263      return PCISTS_EINVAL;
264
265    case PCISTS_MSTABRT:
266      puts(" Master abort while reading configuration space");
267      return PCISTS_MSTABRT;
268  }
269  return 0;
270}
271
272static int shell_pci_pciid(
273        int argc,
274        char *argv[],
275        struct shell_pci_modifier *mod)
276{
277  unsigned long pciid;
278
279  if (argc != 3)
280    return -1;
281
282  pciid = get_pciid_from_string(argv[2]);
283  if ((pciid & 0xffff0000) != 0)
284    return -1;
285
286  printf(" PCIID: 0x%lx [%lx:%lx:%lx]\n", pciid, PCI_DEV_EXPAND(pciid));
287  return 0;
288}
289
290static int shell_pci_getdev(
291        int argc,
292        char *argv[],
293        struct shell_pci_modifier *mod)
294{
295  unsigned long pciid;
296  struct pci_dev *dev;
297
298  if (argc != 3)
299    return -1;
300
301  pciid = get_pciid_from_string(argv[2]);
302  if ((pciid & 0xffff0000) != 0)
303    return -1;
304
305  if (pci_get_dev(pciid, &dev)) {
306    printf(" GETDEV: no device on [%lx:%lx:%lx]\n", PCI_DEV_EXPAND(pciid));
307    return 0;
308  }
309
310  printf(" PCI RAM DEVICE: %p\n", dev);
311  return 0;
312}
313
314static int shell_pci_infodev(
315        int argc,
316        char *argv[],
317        struct shell_pci_modifier *mod)
318{
319  unsigned long arg;
320  struct pci_dev *dev;
321  struct pci_bus *bus;
322  struct pci_res *res;
323  char *type_str, *str1, *res_types[3] = {" IO16", "MEMIO", "MEM"};
324  int i, res_avail;
325
326  if (argc != 3)
327    return -1;
328
329  arg = strtoul(argv[2], NULL, 0);
330  if (arg == ULONG_MAX && errno == ERANGE)
331    return -1;
332
333  dev = (struct pci_dev *)arg;
334  if (!dev) {
335    printf(" INFODEV: invalid device\n");
336    return 0;
337  }
338
339  if (dev->flags & PCI_DEV_BRIDGE) {
340    type_str = "PCI-to-PCI BRIDGE";
341    if (!dev->bus)
342      type_str = "PCI HOST BRIDGE";
343  } else
344    type_str = "PCI DEVICE";
345  printf(" %s at [%x:%x:%x]\n", type_str, PCI_DEV_EXPAND(dev->busdevfun));
346
347  bus = (struct pci_bus *)dev;
348  if (bus) {
349    printf(" PRIMARY:       BUS 0x%x\n", bus->pri);
350    printf(" SECONDARY:     BUS 0x%x\n", bus->num);
351    printf(" SUB ORDINATE:  BUS 0x%x\n", bus->sord);
352  }
353
354  printf(" PCIID:         0x%04x\n", dev->busdevfun);
355  bus = dev->bus;
356  if (!bus) {
357    printf(" AT BUS:        0x%x via Host Bridge\n", bus->num);
358  } else {
359    printf(" AT BUS:        0x%x via Bridge at [%x:%x:%x]\n", bus->num,
360           PCI_DEV_EXPAND(bus->dev.busdevfun));
361  }
362  printf(" VENDOR:        0x%04x\n", dev->vendor);
363  printf(" DEVICE:        0x%04x\n", dev->device);
364  printf(" SUB VENDOR:    0x%04x\n", dev->subvendor);
365  printf(" SUB DEVICE:    0x%04x\n", dev->subdevice);
366  printf(" CLASS:         0x%06lx\n", dev->classrev >> 8);
367  printf(" REVISION:      0x%02lx\n", dev->classrev & 0xff);
368  printf(" IRQ:           %d\n", dev->sysirq);
369
370  res_avail = 0;
371  for (i = 0; i < DEV_RES_CNT; i++) {
372    res = &dev->resources[i];
373
374    if ((res->flags & PCI_RES_TYPE_MASK) == 0)
375      continue;
376
377    str1 = res_types[(res->flags & PCI_RES_TYPE_MASK) - 1];
378    if (res->flags & PCI_RES_IO32)
379      str1 = " IO32";
380
381    if (res_avail == 0) {
382      puts(" RESOURCES:");
383      res_avail = 1;
384    }
385
386    if (res->flags & PCI_RES_FAIL) {
387      printf("  %s[%d]:  NOT ASSIGNED", str1, i);
388      continue;
389    }
390
391    printf("  %s[%d]:  %08lx-%08lx\n", str1, i, res->start, res->end - 1);
392  }
393
394  if (res_avail == 0)
395    puts(" NO CONFIGURED RESOURCES AVAILABLE");
396
397  return 0;
398}
399
400static int pci_summary(void)
401{
402        char *str;
403        char *cfglib_strs[5] = {"NONE", "AUTO", "STATIC", "READ", "PERIPHERAL"};
404
405        if (pci_system_type == PCI_SYSTEM_HOST)
406                str = "HOST";
407        else if (pci_system_type == PCI_SYSTEM_PERIPHERAL)
408                str = "PERIPHERAL";
409        else
410                str = "UNKNOWN / UNINITIALIZED";
411        printf(" SYSTEM:            %s\n", str);
412
413        if (pci_config_lib_type > PCI_CONFIG_LIB_PERIPHERAL) {
414                puts(" Bad configuration library");
415                return 1;
416        }
417        printf(" CFG LIBRARY:       %s\n", cfglib_strs[pci_config_lib_type]);
418        printf(" NO. PCI BUSES:     %d buses\n", pci_bus_count());
419        printf(" PCI ENDIAN:        %s\n", pci_endian ? "Big" : "Little");
420#if BYTE_ORDER == LITTLE_ENDIAN
421        puts(" MACHINE ENDIAN:    Little");
422#else
423        puts(" MACHINE ENDIAN:    Big");
424#endif
425
426        return 0;
427}
428
429static const char pci_usage_str[] =
430 " usage:\n"
431 "  pci ls [bus:dev:fun|PCIID]         List one or all devices\n"
432 "  pci r{8|16|32} bus:dev:fun OFS     Configuration space read\n"
433 "  pci r{8|16|32} PCIID OFS           Configuration space read\n"
434 "                                     access by PCIID\n"
435 "  pci w{8|16|32} bus:dev:fun OFS D   Configuration space write\n"
436 "  pci w{8|16|32} PCIID OFS D         Configuration space write\n"
437 "                                     access by PCIID\n"
438 "  pci pciid bus:dev:fun              Print PCIID for bus:dev:fun\n"
439 "  pci pciid PCIID                    Print bus:dev:fun for PCIID\n"
440 "  pci pcfg                           Print current PCI config for\n"
441 "                                     static configuration library\n"
442 "  pci getdev {PCIID|bus:dev:fun}     Get PCI Device from RAM tree\n"
443 "  pci infodev DEV_ADR                Info about a PCI RAM Device\n"
444 "  pci --help\n";
445
446static void usage(void)
447{
448  puts(pci_usage_str);
449}
450
451static int shell_pci_usage(
452        int argc,
453        char *argv[],
454        struct shell_pci_modifier *mod)
455{
456  usage();
457  return 0;
458}
459
460#define MODIFIER_NUM 12
461static struct shell_pci_modifier shell_pci_modifiers[MODIFIER_NUM] =
462{
463  {"ls", shell_pci_ls, 0},
464  {"r8", shell_pci_read, 1},
465  {"r16", shell_pci_read, 2},
466  {"r32", shell_pci_read, 4},
467  {"w8", shell_pci_write, 1},
468  {"w16", shell_pci_write, 2},
469  {"w32", shell_pci_write, 4},
470  {"pciid", shell_pci_pciid, 0},
471  {"pcfg", shell_pci_pcfg, 0},
472  {"getdev", shell_pci_getdev, 0},
473  {"infodev", shell_pci_infodev, 0},
474  {"--help", shell_pci_usage},
475};
476
477static struct shell_pci_modifier *shell_pci_find_modifier(char *name)
478{
479  struct shell_pci_modifier *mod;
480  int i;
481
482  if (name == NULL)
483    return NULL;
484
485  for (i=0, mod=&shell_pci_modifiers[0]; i<MODIFIER_NUM; i++, mod++) {
486    if (strcmp(name, mod->name) == 0)
487      return mod;
488  }
489
490  return NULL;
491}
492
493static int rtems_shell_main_pci(
494  int   argc,
495  char *argv[]
496)
497{
498  struct shell_pci_modifier *mod;
499  int rc;
500
501  if (argc < 2) {
502    /* without arguments */
503    pci_summary();
504    rc = 0;
505  } else if ((mod=shell_pci_find_modifier(argv[1])) != NULL) {
506    rc = mod->func(argc, argv, mod);
507  } else {
508    rc = -1;
509  }
510
511  if (rc < 0) {
512    printf(" invalid argument\n");
513    usage();
514  }
515
516  return rc;
517}
518
519rtems_shell_cmd_t rtems_shell_PCI_Command = {
520  "pci",                         /* name */
521  pci_usage_str,                 /* usage */
522  "system",                      /* topic */
523  rtems_shell_main_pci,          /* command */
524  NULL,                          /* alias */
525  NULL                           /* next */
526};
Note: See TracBrowser for help on using the repository browser.