summaryrefslogtreecommitdiff
path: root/com32/lib/pci/scan.c
blob: 3bd98ceb7c2e563e4f2efd483841fe729956f63d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2006-2007 Erwan Velu - All Rights Reserved
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------- */

/*
 * pci.c
 *
 * A module to extract pci informations
 */

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <console.h>
#include <sys/pci.h>
#include <com32.h>
#include <stdbool.h>

#ifdef DEBUG
# define dprintf printf
#else
# define dprintf(...) ((void)0)
#endif

#define MAX_LINE 512

/* searching the next char that is not a space */
static char *skipspace(char *p)
{
  while (*p && *p <= ' ')
    p++;

  return p;
}

/* removing any \n found in a string */
void remove_eol(char *string)
{
 int j = strlen(string);
 int i = 0;
 for(i = 0; i < j; i++) if(string[i] == '\n') string[i] = 0;
}

/* converting a hexa string into its numerical value*/
int hex_to_int(char *hexa)
{
 int i;
 sscanf(hexa,"%x",&i);
 return i;
}

/* Try to match any pci device to the appropriate kernel module */
/* it uses the modules.pcimap from the boot device*/
void get_module_name_from_pci_ids(struct pci_device_list *pci_device_list)
{
  char line[MAX_LINE];
  char module_name[21]; // the module name field is 21 char long
  char delims[]=" ";    // colums are separated by spaces
  char vendor_id[16];
  char product_id[16];
  char sub_vendor_id[16];
  char sub_product_id[16];
  FILE *f;
  int pci_dev;

  /* Intializing the linux_kernel_module for each pci device to "unknow" */
  /* adding a pci_dev_info member if needed*/
  for (pci_dev=0; pci_dev < pci_device_list->count; pci_dev++) {
      struct pci_device *pci_device = &(pci_device_list->pci_device[pci_dev]);

      /* initialize the pci_dev_info structure if it doesn't exist yet. */
      if (! pci_device->pci_dev_info) {
	pci_device->pci_dev_info=calloc(1,sizeof (struct pci_device));
	if (pci_device->pci_dev_info == NULL) {
		printf("Can't allocate memory\n");
		return;
	}
      }
      pci_device->pci_dev_info->linux_kernel_module=strdup("unknown");
      //printf("%04x:%04x %s %s\n",pci_device->vendor,pci_device->product,pci_device->pci_dev_info->vendor_name,pci_device->pci_dev_info->vendor_name);

  }

  /* Opening the modules.pcimap (ofa linux kernel) from the boot device*/
  f=fopen("modules.pcimap","r");
  if (!f)
        return;

  strcpy(vendor_id,"0000");
  strcpy(product_id,"0000");
  strcpy(sub_product_id,"0000");
  strcpy(sub_vendor_id,"0000");

  /* for each line we found in the modules.pcimap*/
  while ( fgets(line, sizeof line, f) ) {
	  /*skipping unecessary lines */
    if ((line[0] == '#') || (line[0] == ' ') || (line[0] == 10))
        continue;

    char *result = NULL;
    int field=0;

    /* looking for the next field */
    result = strtok(line, delims);
    while( result != NULL ) {
       /* if the column is larger than 1 char */
       /* multiple spaces generates some empty fields*/
       if (strlen(result)>1) {
	       switch (field) {
		       case 0:strcpy(module_name,result); break;
		       case 1:strcpy(vendor_id,result); break;
		       case 2:strcpy(product_id,result); break;
		       case 3:strcpy(sub_vendor_id,result); break;
		       case 4:strcpy(sub_product_id,result); break;
	       }
	       field++;
       }
       /* Searching the next field*/
       result = strtok( NULL, delims );
   }
    /* if a pci_device match an entry, fill the linux_kernel_module with the appropriate kernel module */
    for (pci_dev=0; pci_dev < pci_device_list->count; pci_dev++) {
        struct pci_device *pci_device = &pci_device_list->pci_device[pci_dev];
        if ((hex_to_int(vendor_id)==pci_device->vendor) && (hex_to_int(product_id)==pci_device->product) &&\
		      ((hex_to_int(sub_product_id) & pci_device->sub_product)==pci_device->sub_product) &&\
		      ((hex_to_int(sub_vendor_id) & pci_device->sub_vendor)==pci_device->sub_vendor)) {
//			printf("module=%s#%s#\n",module_name,pci_device->pci_dev_info->product_name);
		strcpy(pci_device->pci_dev_info->linux_kernel_module,module_name);
	}
   }
  }
 fclose(f);
}

/* Try to match any pci device to the appropriate vendor and product name */
/* it uses the pci.ids from the boot device*/
void get_name_from_pci_ids(struct pci_device_list *pci_device_list)
{
  char line[MAX_LINE];
  char *vendor=NULL;
  char vendor_id[5];
  char *product=NULL;
  char product_id[5];
  char sub_product_id[5];
  char sub_vendor_id[5];
  FILE *f;
  int pci_dev;

 /* Intializing the vendor/product name for each pci device to "unknow" */
 /* adding a pci_dev_info member if needed*/
 for (pci_dev=0; pci_dev < pci_device_list->count; pci_dev++) {
    struct pci_device *pci_device = &(pci_device_list->pci_device[pci_dev]);

    /* initialize the pci_dev_info structure if it doesn't exist yet. */
    if (! pci_device->pci_dev_info) {
	pci_device->pci_dev_info=calloc(1,sizeof (struct pci_device));
	if (pci_device->pci_dev_info == NULL) {
		printf("Can't allocate memory\n");
		return;
	}
    }

    pci_device->pci_dev_info->vendor_name=strdup("unknown");
    pci_device->pci_dev_info->product_name=strdup("unknown");
  }

  /* Opening the pci.ids from the boot device*/
  f=fopen("pci.ids","r");
  if (!f)
        return;

  strcpy(vendor_id,"0000");
  strcpy(product_id,"0000");
  strcpy(sub_product_id,"0000");
  strcpy(sub_vendor_id,"0000");


  /* for each line we found in the pci.ids*/
  while ( fgets(line, sizeof line, f) ) {

    /* Skipping uncessary lines */
    if ((line[0] == '#') || (line[0] == ' ') || (line[0] == 'C') || (line[0] == 10))
        continue;
    /* If the line doesn't start with a tab, it means that's a vendor id */
    if (line[0] != '\t') {

	/* the 4th first chars are the vendor_id */
        strncpy(vendor_id,line,4);

	/* the vendor name is the next field*/
        vendor_id[4]=0;
        vendor=strdup(skipspace(strstr(line," ")));
        remove_eol(vendor);

	/* init product_id, sub_product and sub_vendor */
        strcpy(product_id,"0000");
        strcpy(sub_product_id,"0000");
        strcpy(sub_vendor_id,"0000");

	/* ffff is an invalid vendor id */
	if (strstr(vendor_id,"ffff")) break;

	/* assign the vendor_name to any matching pci device*/
	for (pci_dev=0; pci_dev < pci_device_list->count; pci_dev++) {
		struct pci_device *pci_device = &pci_device_list->pci_device[pci_dev];
	        if (hex_to_int(vendor_id)==pci_device->vendor) {
			pci_device->pci_dev_info->vendor_name=strdup(vendor);
		}
	}
    /* if we have a tab + a char, it means this is a product id */
    } else if ((line[0] == '\t') && (line[1] != '\t')) {

	/* the product name the second field */
        product=strdup(skipspace(strstr(line," ")));
        remove_eol(product);

	/* the product id is first field */
	strncpy(product_id,&line[1],4);
        product_id[4]=0;

	/* init sub_product and sub_vendor */
        strcpy(sub_product_id,"0000");
        strcpy(sub_vendor_id,"0000");

	/* assign the product_name to any matching pci device*/
	for (pci_dev=0; pci_dev < pci_device_list->count; pci_dev++) {
		struct pci_device *pci_device = &pci_device_list->pci_device[pci_dev];
		if ((hex_to_int(vendor_id)==pci_device->vendor) && (hex_to_int(product_id)==pci_device->product))  {
			pci_device->pci_dev_info->product_name=strdup(product);
		}
	}

    /* if we have two tabs, it means this is a sub product */
    } else if ((line[0] == '\t') && (line[1] == '\t')) {

	/* the product name is last field */
        product=skipspace(strstr(line," "));
        product=strdup(skipspace(strstr(product," ")));
        remove_eol(product);

	/* the sub_vendor id is first field */
	strncpy(sub_vendor_id,&line[2],4);
        sub_vendor_id[4]=0;

	/* the sub_vendor id is second field */
	strncpy(sub_product_id,&line[7],4);
        sub_product_id[4]=0;

	/* assign the product_name to any matching pci device*/
	for (pci_dev=0; pci_dev < pci_device_list->count; pci_dev++) {
		struct pci_device *pci_device = &pci_device_list->pci_device[pci_dev];
		if ((hex_to_int(vendor_id)==pci_device->vendor) && (hex_to_int(product_id)==pci_device->product) &&
			 (hex_to_int(sub_product_id)==pci_device->sub_product) &&
			 (hex_to_int(sub_vendor_id)==pci_device->sub_vendor)) {
			pci_device->pci_dev_info->product_name=strdup(product);
		}
	}
      }
 }
 fclose(f);
}

/* searching if any pcidevice match our query */
struct match *find_pci_device(struct pci_device_list * pci_device_list,
			      struct match *list)
{
  int pci_dev;
  uint32_t did, sid;
  struct match *m;
  /* for all matches we have to search */
  for (m = list; m; m = m->next) {
	  /* for each pci device we know */
    for (pci_dev = 0; pci_dev < pci_device_list->count; pci_dev++) {
      struct pci_device *pci_device =
	&pci_device_list->pci_device[pci_dev];

      /* sid & did are the easiest way to compare devices */
      /* they are made of vendor/product subvendor/subproduct ids */
      sid =
	((pci_device->sub_product) << 16 | (pci_device->
					    sub_vendor));
      did = ((pci_device->product << 16) | (pci_device->vendor));

      /*if the current device match */
      if (((did ^ m->did) & m->did_mask) == 0 &&
	  ((sid ^ m->sid) & m->sid_mask) == 0 &&
	  pci_device->revision >= m->rid_min
	  && pci_device->revision <= m->rid_max) {
	dprintf("PCI Match: Vendor=%04x Product=%04x Sub_vendor=%04x Sub_Product=%04x Release=%02x\n",
		pci_device->vendor, pci_device->product,
		pci_device->sub_vendor,
		pci_device->sub_product,
		pci_device->revision);
	/* returning the matched pci device */
	return m;
      }
    }
  }
  return NULL;
}

/* scanning the pci bus to find pci devices */
int pci_scan(struct pci_bus_list * pci_bus_list, struct pci_device_list * pci_device_list)
{
  unsigned int bus, dev, func, maxfunc;
  uint32_t did, sid;
  uint8_t hdrtype, rid;
  pciaddr_t a;
  int cfgtype;

  pci_device_list->count = 0;

#ifdef DEBUG
  outl(~0, 0xcf8);
  printf("Poking at port CF8 = %#08x\n", inl(0xcf8));
  outl(0, 0xcf8);
#endif

  cfgtype = pci_set_config_type(PCI_CFG_AUTO);
  (void)cfgtype;

  dprintf("PCI configuration type %d\n", cfgtype);
  dprintf("Scanning PCI Buses\n");

  /* We try to detect 255 buses */
  for (bus = 0; bus <= MAX_PCI_BUSES; bus++) {

    dprintf("Probing bus 0x%02x... \n", bus);

    pci_bus_list->pci_bus[bus].id = bus;
    pci_bus_list->pci_bus[bus].pci_device_count = 0;
    pci_bus_list->count = 0;;

    for (dev = 0; dev <= 0x1f; dev++) {
      maxfunc = 0;
      for (func = 0; func <= maxfunc; func++) {
	a = pci_mkaddr(bus, dev, func, 0);

	did = pci_readl(a);

	if (did == 0xffffffff || did == 0xffff0000 ||
	    did == 0x0000ffff || did == 0x00000000)
	  continue;

	hdrtype = pci_readb(a + 0x0e);

	if (hdrtype & 0x80)
	  maxfunc = 7;	/* Multifunction device */

	rid = pci_readb(a + 0x08);
	sid = pci_readl(a + 0x2c);
	struct pci_device *pci_device =
	  &pci_device_list->
	  pci_device[pci_device_list->count];
	pci_device->product = did >> 16;
	pci_device->sub_product = sid >> 16;
	pci_device->vendor = (did << 16) >> 16;
	pci_device->sub_vendor = (sid << 16) >> 16;
	pci_device->revision = rid;
	pci_device_list->count++;
	dprintf
	  ("Scanning: BUS %02x DID %08x (%04x:%04x) SID %08x RID %02x\n",
	   bus, did, did >> 16, (did << 16) >> 16,
	   sid, rid);
	/* Adding the detected pci device to the bus */
	pci_bus_list->pci_bus[bus].
	  pci_device[pci_bus_list->pci_bus[bus].
		     pci_device_count] = pci_device;
	pci_bus_list->pci_bus[bus].pci_device_count++;
      }
    }
  }

  /* Detecting pci buses that have pci devices connected */
  for (bus = 0; bus <= 0xff; bus++) {
    if (pci_bus_list->pci_bus[bus].pci_device_count > 0) {
      pci_bus_list->count++;
    }
  }
  return 0;
}