3b771e3768679e65e2e52d1066316c7dd028df22
[akaros.git] / kern / kfs / bin / pci
1 #!/bin/rc
2 # pci [-bv] - dump pci configuration
3 rfork e
4 fn verbose {
5         if (! test -f /lib/pci)
6                 echo $0: no /lib/pci >[1=2]
7         awk '
8
9         function lower(s) {
10                 gsub(/A/, "a", s)
11                 gsub(/B/, "b", s)
12                 gsub(/C/, "c", s)
13                 gsub(/D/, "d", s)
14                 gsub(/E/, "e", s)
15                 gsub(/F/, "f", s)
16                 return s
17         }
18         BEGIN{
19                 file="/lib/pci"
20                 FS="\t"
21                 while(getline <file > 0){
22                         if(/^;/) continue
23                         if(/^[0-9a-fA-F]/){
24                                 vid=lower($1)
25                                 vendor[vid] = $2
26                         }
27                         if(/^   [0-9a-fA-F]/){
28                                 did=lower($2)
29                                 id[vid "/" did] = $3
30                         }
31                 }
32                 FS = " "
33         }
34
35         {
36                 print $0
37                 vid = $4
38                 sub(/\/.*/, "", vid)
39                 if(vid in vendor){
40                         s = vendor[vid]
41                         if($4 in id)
42                                 s = s " " id[$4]
43                         print "\t" s
44                 }
45         }
46 '
47 }
48 fn usage {
49         echo usage: $1 '[-bv]' >[1=2]
50         exit usage
51 }
52
53 filter=cat
54 bridges=yes
55 done=0
56 while (~ $done 0 && ! ~ $#* 0 && ~ $1 -*) {
57         if (~ $1 -*b*)
58                 bridges=no
59         if (~ $1 -*v*)
60                 filter=verbose
61         switch ($1) {
62         case --
63                 done = 1                # no break in rc, alas
64         case -*[~bv]*
65                 usage $0
66         }
67         shift
68 }
69 if (! ~ $#* 0)
70         usage $0
71
72 # DMG 06/02/2016 Make pci(8) recognize virtio devices
73 # source: http://git.qemu.org/?p=qemu.git;a=blob;f=include/hw/pci/pci.h
74 # devices with vendor code 0x1af4 are QEMU own virtio devices.
75 # useful device codes below:
76 # 0x1000: virtio network card
77 # 0x1001: virtio block device
78 # 0x1002: virtio balloon
79 # 0x1003: virtio console
80 # 0x1004: virtio SCSI
81 # 0x1005: virtio RNG (random numbers generator)
82 # 0x1009: virtio 9P transport
83 # based on this information, the translation table below is amended
84 # to show these devices in the pci (8) output.
85
86 builtin cd '#$/pci' && grep . *ctl | {
87         if (~ $bridges no)
88                 sed /:06/d
89         if not
90                 cat
91         } |
92         sed '
93         s/ctl:/:        /
94         t noop
95 : noop
96         s/:     (02\..*\ 1af4\/1000)/:  virtio-net     \1/
97         s/:     (01\..*\ 1af4\/1001)/:  virtio-disk    \1/
98         s/:     (00\..*\ 1af4\/1002)/:  virtio-balloon \1/
99         s/:     (07\..*\ 1af4\/1003)/:  virtio-console \1/
100         s/:     (01\..*\ 1af4\/1004)/:  virtio-scsi    \1/
101         s/:     (00\..*\ 1af4\/1005)/:  virtio-rng     \1/
102         s/:     (00\..*\ 1af4\/1009)/:  virtio-9p      \1/
103         s/:     01/:    disk  01/
104         s/:     02/:    net   02/
105         s/:     03/:    vid   03/
106         s/:     04/:    aud   04/
107         s/:     05/:    mem   05/
108         s/:     06/:    brg   06/
109         s/:     07/:    ser   07/
110         s/:     0c\.03/:        usb   0c.03/
111         s/:     0c\.05/:        smb   0c.05/
112         s/:     0d/:    rad   0d/
113         s/:     10/:    cryp  10/
114         t
115         s/      /       ---  /
116 ' | $filter