]> mj.ucw.cz Git - pciutils.git/blob - setpci.c
Use PCI_CAP_xxx internally in setpci.
[pciutils.git] / setpci.c
1 /*
2  *      The PCI Utilities -- Manipulate PCI Configuration Registers
3  *
4  *      Copyright (c) 1998--2008 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <unistd.h>
14 #include <errno.h>
15
16 #define PCIUTILS_SETPCI
17 #include "pciutils.h"
18
19 static int force;                       /* Don't complain if no devices match */
20 static int verbose;                     /* Verbosity level */
21 static int demo_mode;                   /* Only show */
22
23 const char program_name[] = "setpci";
24
25 static struct pci_access *pacc;
26
27 struct value {
28   unsigned int value;
29   unsigned int mask;
30 };
31
32 struct op {
33   struct op *next;
34   struct pci_dev **dev_vector;
35   u16 cap_type;                         /* PCI_CAP_xxx or 0 */
36   u16 cap_id;
37   unsigned int addr;
38   unsigned int width;                   /* Byte width of the access */
39   unsigned int num_values;              /* Number of values to write; 0=read */
40   struct value values[0];
41 };
42
43 static struct op *first_op, **last_op = &first_op;
44 static unsigned int max_values[] = { 0, 0xff, 0xffff, 0, 0xffffffff };
45
46 static struct pci_dev **
47 select_devices(struct pci_filter *filt)
48 {
49   struct pci_dev *z, **a, **b;
50   int cnt = 1;
51
52   for (z=pacc->devices; z; z=z->next)
53     if (pci_filter_match(filt, z))
54       cnt++;
55   a = b = xmalloc(sizeof(struct device *) * cnt);
56   for (z=pacc->devices; z; z=z->next)
57     if (pci_filter_match(filt, z))
58       *a++ = z;
59   *a = NULL;
60   return b;
61 }
62
63 static void
64 exec_op(struct op *op, struct pci_dev *dev)
65 {
66   char *formats[] = { NULL, "%02x", "%04x", NULL, "%08x" };
67   char *mask_formats[] = { NULL, "%02x->(%02x:%02x)->%02x", "%04x->(%04x:%04x)->%04x", NULL, "%08x->(%08x:%08x)->%08x" };
68   unsigned int i, x, y;
69   int addr = 0;
70   int width = op->width;
71
72   if (verbose)
73     printf("%02x:%02x.%x", dev->bus, dev->dev, dev->func);
74   if (op->cap_type)
75     {
76       struct pci_cap *cap;
77       if (verbose)
78         printf(((op->cap_type == PCI_CAP_NORMAL) ? "(cap %02x)" : "(ecap %04x)"), op->cap_id);
79       cap = pci_find_cap(dev, op->cap_id, op->cap_type);
80       if (cap)
81         addr = cap->addr;
82       else
83         {
84           /* FIXME: Report the error properly */
85           die("Capability %04x not found", op->cap_id);
86         }
87     }
88   addr += op->addr;
89   if (verbose)
90     printf(":%02x", addr);
91   if (op->num_values)
92     {
93       for (i=0; i<op->num_values; i++)
94         {
95           if (addr + width > 0x1000)
96             die("Out of range");        /* FIXME */
97           if ((op->values[i].mask & max_values[width]) == max_values[width])
98             {
99               x = op->values[i].value;
100               if (verbose)
101                 {
102                   putchar(' ');
103                   printf(formats[width], op->values[i].value);
104                 }
105             }
106           else
107             {
108               switch (width)
109                 {
110                 case 1:
111                   y = pci_read_byte(dev, addr);
112                   break;
113                 case 2:
114                   y = pci_read_word(dev, addr);
115                   break;
116                 default:
117                   y = pci_read_long(dev, addr);
118                   break;
119                 }
120               x = (y & ~op->values[i].mask) | op->values[i].value;
121               if (verbose)
122                 {
123                   putchar(' ');
124                   printf(mask_formats[width], y, op->values[i].value, op->values[i].mask, x);
125                 }
126             }
127           if (!demo_mode)
128             {
129               switch (width)
130                 {
131                 case 1:
132                   pci_write_byte(dev, addr, x);
133                   break;
134                 case 2:
135                   pci_write_word(dev, addr, x);
136                   break;
137                 default:
138                   pci_write_long(dev, addr, x);
139                   break;
140                 }
141             }
142           addr += width;
143         }
144       if (verbose)
145         putchar('\n');
146     }
147   else
148     {
149       if (verbose)
150         printf(" = ");
151       if (addr + width > 0x1000)
152         die("Out of range");    /* FIXME */
153       switch (width)
154         {
155         case 1:
156           x = pci_read_byte(dev, addr);
157           break;
158         case 2:
159           x = pci_read_word(dev, addr);
160           break;
161         default:
162           x = pci_read_long(dev, addr);
163           break;
164         }
165       printf(formats[width], x);
166       putchar('\n');
167     }
168 }
169
170 static void
171 execute(struct op *op)
172 {
173   struct pci_dev **vec = NULL;
174   struct pci_dev **pdev, *dev;
175   struct op *oops;
176
177   while (op)
178     {
179       pdev = vec = op->dev_vector;
180       while (dev = *pdev++)
181         for (oops=op; oops && oops->dev_vector == vec; oops=oops->next)
182           exec_op(oops, dev);
183       while (op && op->dev_vector == vec)
184         op = op->next;
185     }
186 }
187
188 static void
189 scan_ops(struct op *op)
190 {
191   while (op)
192     {
193       if (op->num_values)
194         pacc->writeable = 1;
195       op = op->next;
196     }
197 }
198
199 struct reg_name {
200   unsigned int cap;
201   unsigned int offset;
202   unsigned int width;
203   const char *name;
204 };
205
206 static const struct reg_name pci_reg_names[] = {
207   {       0, 0x00, 2, "VENDOR_ID" },
208   {       0, 0x02, 2, "DEVICE_ID" },
209   {       0, 0x04, 2, "COMMAND" },
210   {       0, 0x06, 2, "STATUS" },
211   {       0, 0x08, 1, "REVISION" },
212   {       0, 0x09, 1, "CLASS_PROG" },
213   {       0, 0x0a, 2, "CLASS_DEVICE" },
214   {       0, 0x0c, 1, "CACHE_LINE_SIZE" },
215   {       0, 0x0d, 1, "LATENCY_TIMER" },
216   {       0, 0x0e, 1, "HEADER_TYPE" },
217   {       0, 0x0f, 1, "BIST" },
218   {       0, 0x10, 4, "BASE_ADDRESS_0" },
219   {       0, 0x14, 4, "BASE_ADDRESS_1" },
220   {       0, 0x18, 4, "BASE_ADDRESS_2" },
221   {       0, 0x1c, 4, "BASE_ADDRESS_3" },
222   {       0, 0x20, 4, "BASE_ADDRESS_4" },
223   {       0, 0x24, 4, "BASE_ADDRESS_5" },
224   {       0, 0x28, 4, "CARDBUS_CIS" },
225   {       0, 0x2c, 4, "SUBSYSTEM_VENDOR_ID" },
226   {       0, 0x2e, 2, "SUBSYSTEM_ID" },
227   {       0, 0x30, 4, "ROM_ADDRESS" },
228   {       0, 0x3c, 1, "INTERRUPT_LINE" },
229   {       0, 0x3d, 1, "INTERRUPT_PIN" },
230   {       0, 0x3e, 1, "MIN_GNT" },
231   {       0, 0x3f, 1, "MAX_LAT" },
232   {       0, 0x18, 1, "PRIMARY_BUS" },
233   {       0, 0x19, 1, "SECONDARY_BUS" },
234   {       0, 0x1a, 1, "SUBORDINATE_BUS" },
235   {       0, 0x1b, 1, "SEC_LATENCY_TIMER" },
236   {       0, 0x1c, 1, "IO_BASE" },
237   {       0, 0x1d, 1, "IO_LIMIT" },
238   {       0, 0x1e, 2, "SEC_STATUS" },
239   {       0, 0x20, 2, "MEMORY_BASE" },
240   {       0, 0x22, 2, "MEMORY_LIMIT" },
241   {       0, 0x24, 2, "PREF_MEMORY_BASE" },
242   {       0, 0x26, 2, "PREF_MEMORY_LIMIT" },
243   {       0, 0x28, 4, "PREF_BASE_UPPER32" },
244   {       0, 0x2c, 4, "PREF_LIMIT_UPPER32" },
245   {       0, 0x30, 2, "IO_BASE_UPPER16" },
246   {       0, 0x32, 2, "IO_LIMIT_UPPER16" },
247   {       0, 0x38, 4, "BRIDGE_ROM_ADDRESS" },
248   {       0, 0x3e, 2, "BRIDGE_CONTROL" },
249   {       0, 0x10, 4, "CB_CARDBUS_BASE" },
250   {       0, 0x14, 2, "CB_CAPABILITIES" },
251   {       0, 0x16, 2, "CB_SEC_STATUS" },
252   {       0, 0x18, 1, "CB_BUS_NUMBER" },
253   {       0, 0x19, 1, "CB_CARDBUS_NUMBER" },
254   {       0, 0x1a, 1, "CB_SUBORDINATE_BUS" },
255   {       0, 0x1b, 1, "CB_CARDBUS_LATENCY" },
256   {       0, 0x1c, 4, "CB_MEMORY_BASE_0" },
257   {       0, 0x20, 4, "CB_MEMORY_LIMIT_0" },
258   {       0, 0x24, 4, "CB_MEMORY_BASE_1" },
259   {       0, 0x28, 4, "CB_MEMORY_LIMIT_1" },
260   {       0, 0x2c, 2, "CB_IO_BASE_0" },
261   {       0, 0x2e, 2, "CB_IO_BASE_0_HI" },
262   {       0, 0x30, 2, "CB_IO_LIMIT_0" },
263   {       0, 0x32, 2, "CB_IO_LIMIT_0_HI" },
264   {       0, 0x34, 2, "CB_IO_BASE_1" },
265   {       0, 0x36, 2, "CB_IO_BASE_1_HI" },
266   {       0, 0x38, 2, "CB_IO_LIMIT_1" },
267   {       0, 0x3a, 2, "CB_IO_LIMIT_1_HI" },
268   {       0, 0x40, 2, "CB_SUBSYSTEM_VENDOR_ID" },
269   {       0, 0x42, 2, "CB_SUBSYSTEM_ID" },
270   {       0, 0x44, 4, "CB_LEGACY_MODE_BASE" },
271   { 0x10001,    0, 0, "CAP_PM" },
272   { 0x10002,    0, 0, "CAP_AGP" },
273   { 0x10003,    0, 0, "CAP_VPD" },
274   { 0x10004,    0, 0, "CAP_SLOTID" },
275   { 0x10005,    0, 0, "CAP_MSI" },
276   { 0x10006,    0, 0, "CAP_CHSWP" },
277   { 0x10007,    0, 0, "CAP_PCIX" },
278   { 0x10008,    0, 0, "CAP_HT" },
279   { 0x10009,    0, 0, "CAP_VNDR" },
280   { 0x1000a,    0, 0, "CAP_DBG" },
281   { 0x1000b,    0, 0, "CAP_CCRC" },
282   { 0x1000c,    0, 0, "CAP_HOTPLUG" },
283   { 0x1000d,    0, 0, "CAP_SSVID" },
284   { 0x1000e,    0, 0, "CAP_AGP3" },
285   { 0x1000f,    0, 0, "CAP_SECURE" },
286   { 0x10010,    0, 0, "CAP_EXP" },
287   { 0x10011,    0, 0, "CAP_MSIX" },
288   { 0x10012,    0, 0, "CAP_SATA" },
289   { 0x10013,    0, 0, "CAP_AF" },
290   { 0x20001,    0, 0, "ECAP_AER" },
291   { 0x20002,    0, 0, "ECAP_VC" },
292   { 0x20003,    0, 0, "ECAP_DSN" },
293   { 0x20004,    0, 0, "ECAP_PB" },
294   { 0x20005,    0, 0, "ECAP_RCLINK" },
295   { 0x20006,    0, 0, "ECAP_RCILINK" },
296   { 0x20007,    0, 0, "ECAP_RCECOLL" },
297   { 0x20008,    0, 0, "ECAP_MFVC" },
298   { 0x2000a,    0, 0, "ECAP_RBCB" },
299   { 0x2000b,    0, 0, "ECAP_VNDR" },
300   { 0x2000d,    0, 0, "ECAP_ACS" },
301   { 0x2000e,    0, 0, "ECAP_ARI" },
302   { 0x2000f,    0, 0, "ECAP_ATS" },
303   { 0x20010,    0, 0, "ECAP_SRIOV" },
304   {       0,    0, 0, NULL }
305 };
306
307 static void
308 dump_registers(void)
309 {
310   const struct reg_name *r;
311
312   printf("cap pos w name\n");
313   for (r = pci_reg_names; r->name; r++)
314     {
315       if (r->cap >= 0x20000)
316         printf("%04x", r->cap - 0x20000);
317       else if (r->cap)
318         printf("  %02x", r->cap - 0x10000);
319       else
320         printf("    ");
321       printf(" %02x %c %s\n", r->offset, "-BW?L"[r->width], r->name);
322     }
323 }
324
325 static void NONRET PCI_PRINTF(1,2)
326 usage(char *msg, ...)
327 {
328   va_list args;
329   va_start(args, msg);
330   if (msg)
331     {
332       fprintf(stderr, "setpci: ");
333       vfprintf(stderr, msg, args);
334       fprintf(stderr, "\n\n");
335     }
336   fprintf(stderr,
337 "Usage: setpci [<options>] (<device>+ <reg>[=<values>]*)*\n"
338 "\n"
339 "General options:\n"
340 "-f\t\tDon't complain if there's nothing to do\n"
341 "-v\t\tBe verbose\n"
342 "-D\t\tList changes, don't commit them\n"
343 "--dumpregs\tDump all known register names and exit\n"
344 "\n"
345 "PCI access options:\n"
346 GENERIC_HELP
347 "\n"
348 "Setting commands:\n"
349 "<device>:\t-s [[[<domain>]:][<bus>]:][<slot>][.[<func>]]\n"
350 "\t\t-d [<vendor>]:[<device>]\n"
351 "<reg>:\t\t<base>[+<offset>][.(B|W|L)]\n"
352 "<base>:\t\t<address>\n"
353 "\t\t<named-register>\n"
354 "\t\t[E]CAP_<capability-name>\n"
355 "\t\t[E]CAP<capability-number>\n"
356 "<values>:\t<value>[,<value>...]\n"
357 "<value>:\t<hex>\n"
358 "\t\t<hex>:<mask>\n");
359   exit(1);
360 }
361
362 static int
363 parse_options(int argc, char **argv)
364 {
365   char *opts = GENERIC_OPTIONS;
366   int i=1;
367
368   if (argc == 2 && !strcmp(argv[1], "--version"))
369     {
370       puts("setpci version " PCIUTILS_VERSION);
371       exit(0);
372     }
373   if (argc == 2 && !strcmp(argv[1], "--dumpregs"))
374     {
375       dump_registers();
376       exit(0);
377     }
378
379   while (i < argc && argv[i][0] == '-')
380     {
381       char *c = argv[i++] + 1;
382       char *d = c;
383       char *e;
384       while (*c)
385         switch (*c)
386           {
387           case 0:
388             break;
389           case 'v':
390             verbose++;
391             c++;
392             break;
393           case 'f':
394             force++;
395             c++;
396             break;
397           case 'D':
398             demo_mode++;
399             c++;
400             break;
401           default:
402             if (e = strchr(opts, *c))
403               {
404                 char *arg;
405                 c++;
406                 if (e[1] == ':')
407                   {
408                     if (*c)
409                       arg = c;
410                     else if (i < argc)
411                       arg = argv[i++];
412                     else
413                       usage(NULL);
414                     c = "";
415                   }
416                 else
417                   arg = NULL;
418                 if (!parse_generic_option(*e, pacc, arg))
419                   usage(NULL);
420               }
421             else
422               {
423                 if (c != d)
424                   usage(NULL);
425                 return i-1;
426               }
427           }
428     }
429
430   return i;
431 }
432
433 static int parse_filter(int argc, char **argv, int i, struct pci_filter *filter)
434 {
435   char *c = argv[i++];
436   char *d;
437
438   if (!c[1] || !strchr("sd", c[1]))
439     usage(NULL);
440   if (c[2])
441     d = (c[2] == '=') ? c+3 : c+2;
442   else if (i < argc)
443     d = argv[i++];
444   else
445     usage(NULL);
446   switch (c[1])
447     {
448     case 's':
449       if (d = pci_filter_parse_slot(filter, d))
450         die("-s: %s", d);
451       break;
452     case 'd':
453       if (d = pci_filter_parse_id(filter, d))
454         die("-d: %s", d);
455       break;
456     default:
457       usage(NULL);
458     }
459
460   return i;
461 }
462
463 static const struct reg_name *parse_reg_name(char *name)
464 {
465   const struct reg_name *r;
466
467   for (r = pci_reg_names; r->name; r++)
468     if (!strcasecmp(r->name, name))
469       return r;
470   return NULL;
471 }
472
473 static int parse_x32(char *c, char **stopp, unsigned int *resp)
474 {
475   char *stop;
476
477   if (!*c)
478     return -1;
479   errno = 0;
480   unsigned long int l = strtoul(c, &stop, 16);
481   if (errno)
482     return -1;
483   if ((l & ~0U) != l)
484     return -1;
485   *resp = l;
486   if (*stop)
487     {
488       if (stopp)
489         *stopp = stop;
490       return 0;
491     }
492   else
493     return 1;
494 }
495
496 static void parse_register(struct op *op, char *base)
497 {
498   const struct reg_name *r;
499   unsigned int cap;
500
501   op->cap_type = op->cap_id = 0;
502   if (parse_x32(base, NULL, &op->addr) > 0)
503     return;
504   else if (r = parse_reg_name(base))
505     {
506       switch (r->cap & 0xff0000)
507         {
508         case 0x10000:
509           op->cap_type = PCI_CAP_NORMAL;
510           break;
511         case 0x20000:
512           op->cap_type = PCI_CAP_EXTENDED;
513           break;
514         }
515       op->cap_id = r->cap & 0xffff;
516       op->addr = r->offset;
517       if (r->width && !op->width)
518         op->width = r->width;
519       return;
520     }
521   else if (!strncasecmp(base, "CAP", 3))
522     {
523       if (parse_x32(base+3, NULL, &cap) > 0 && cap < 0x100)
524         {
525           op->cap_type = PCI_CAP_NORMAL;
526           op->cap_id = cap;
527           op->addr = 0;
528           return;
529         }
530     }
531   else if (!strncasecmp(base, "ECAP", 4))
532     {
533       if (parse_x32(base+4, NULL, &cap) > 0 && cap < 0x1000)
534         {
535           op->cap_type = PCI_CAP_EXTENDED;
536           op->cap_id = cap;
537           op->addr = 0;
538           return;
539         }
540     }
541   usage("Unknown register \"%s\"", base);
542 }
543
544 static void parse_op(char *c, struct pci_dev **selected_devices)
545 {
546   char *base, *offset, *width, *value;
547   char *e, *f;
548   int n, j;
549   struct op *op;
550
551   /* Split the argument */
552   base = xstrdup(c);
553   if (value = strchr(base, '='))
554     *value++ = 0;
555   if (width = strchr(base, '.'))
556     *width++ = 0;
557   if (offset = strchr(base, '+'))
558     *offset++ = 0;
559
560   /* Look for setting of values and count how many */
561   n = 0;
562   if (value)
563     {
564       if (!*value)
565         usage("Missing value");
566       n++;
567       for (e=value; *e; e++)
568         if (*e == ',')
569           n++;
570     }
571
572   /* Allocate the operation */
573   op = xmalloc(sizeof(struct op) + n*sizeof(struct value));
574   op->dev_vector = selected_devices;
575   op->num_values = n;
576
577   /* What is the width suffix? */
578   if (width)
579     {
580       if (width[1])
581         usage("Invalid width \"%s\"", width);
582       switch (*width & 0xdf)
583         {
584         case 'B':
585           op->width = 1; break;
586         case 'W':
587           op->width = 2; break;
588         case 'L':
589           op->width = 4; break;
590         default:
591           usage("Invalid width \"%c\"", *width);
592         }
593     }
594   else
595     op->width = 0;
596
597   /* Find the register */
598   parse_register(op, base);
599   if (!op->width)
600     usage("Missing width");
601
602   /* Add offset */
603   if (offset)
604     {
605       unsigned int off;
606       if (parse_x32(offset, NULL, &off) <= 0 || off >= 0x1000)
607         die("Invalid offset \"%s\"", offset);
608       op->addr += off;
609     }
610
611   /* Check range */
612   if (op->addr >= 0x1000 || op->addr + op->width*(n ? n : 1) > 0x1000)
613     die("Register number out of range!");
614   if (op->addr & (op->width - 1))
615     die("Unaligned register address!");
616
617   /* Parse the values */
618   for (j=0; j<n; j++)
619     {
620       unsigned int ll, lim;
621       e = strchr(value, ',');
622       if (e)
623         *e++ = 0;
624       if (parse_x32(value, &f, &ll) < 0 || *f && *f != ':')
625         usage("Invalid value \"%s\"", value);
626       lim = max_values[op->width];
627       if (ll > lim && ll < ~0UL - lim)
628         usage("Value \"%s\" is out of range", value);
629       op->values[j].value = ll;
630       if (*f == ':')
631         {
632           if (parse_x32(f+1, NULL, &ll) <= 0)
633             usage("Invalid mask \"%s\"", f+1);
634           if (ll > lim && ll < ~0UL - lim)
635             usage("Mask \"%s\" is out of range", f+1);
636           op->values[j].mask = ll;
637           op->values[j].value &= ll;
638         }
639       else
640         op->values[j].mask = ~0U;
641       value = e;
642     }
643
644   *last_op = op;
645   last_op = &op->next;
646   op->next = NULL;
647 }
648
649 static void parse_ops(int argc, char **argv, int i)
650 {
651   enum { STATE_INIT, STATE_GOT_FILTER, STATE_GOT_OP } state = STATE_INIT;
652   struct pci_filter filter;
653   struct pci_dev **selected_devices = NULL;
654
655   while (i < argc)
656     {
657       char *c = argv[i++];
658
659       if (*c == '-')
660         {
661           if (state != STATE_GOT_FILTER)
662             pci_filter_init(pacc, &filter);
663           i = parse_filter(argc, argv, i-1, &filter);
664           state = STATE_GOT_FILTER;
665         }
666       else
667         {
668           if (state == STATE_INIT)
669             usage(NULL);
670           if (state == STATE_GOT_FILTER)
671             selected_devices = select_devices(&filter);
672           if (!selected_devices[0] && !force)
673             fprintf(stderr, "setpci: Warning: No devices selected for `%s'.\n", c);
674           parse_op(c, selected_devices);
675           state = STATE_GOT_OP;
676         }
677     }
678   if (state == STATE_INIT)
679     usage("No operation specified");
680 }
681
682 int
683 main(int argc, char **argv)
684 {
685   int i;
686
687   pacc = pci_alloc();
688   pacc->error = die;
689   i = parse_options(argc, argv);
690
691   pci_init(pacc);
692   pci_scan_bus(pacc);
693
694   parse_ops(argc, argv, i);
695   scan_ops(first_op);
696   execute(first_op);
697
698   return 0;
699 }