26
26
#include "compat_ioport.h"
27
27
#include <linux/pci.h>
29
# include <linux/bios32.h>
32
#if KERNEL_VERSION(2, 6, 6) <= LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
34
# define DMA_BIT_MASK(n) DMA_##n##BIT_MASK
39
/* 2.0.x has useless struct pci_dev; remap it to our own */
41
#define pci_dev vmw_pci_driver_instance
45
/* 2.0/2.2 does not have pci driver API */
46
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0)
47
struct vmw_pci_driver_instance {
48
struct vmw_pci_driver_instance *next;
50
struct pci_driver *pcidrv;
52
struct pci_dev *pcidev;
62
/* 2.0 has pcibios_* calls only... We have to provide pci_* compatible wrappers. */
65
pci_read_config_byte(struct pci_dev *pdev, // IN: PCI slot
66
unsigned char where, // IN: Byte to read
67
u8 *value) // OUT: Value read
69
return pcibios_read_config_byte(pdev->bus, pdev->devfn, where, value);
73
pci_read_config_dword(struct pci_dev *pdev, // IN: PCI slot
74
unsigned char where, // IN: Dword to read
75
u32 *value) // OUT: Value read
77
return pcibios_read_config_dword(pdev->bus, pdev->devfn, where, value);
81
pci_write_config_dword(struct pci_dev *pdev, // IN: PCI slot
82
unsigned char where, // IN: Dword to write
83
u32 value) // IN: Value to write
85
return pcibios_write_config_dword(pdev->bus, pdev->devfn, where, value);
91
*-----------------------------------------------------------------------------
95
* Return human readable PCI slot name. Note that some implementations
96
* return a pointer to the static storage, so returned value may be
97
* overwritten by subsequent calls to this function.
100
* Returns pointer to the string with slot name.
105
*-----------------------------------------------------------------------------
108
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
109
#define compat_pci_name(pdev) pci_name(pdev)
110
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 0)
111
#define compat_pci_name(pdev) (pdev)->slot_name
112
#elif defined(KERNEL_2_1)
113
static inline const char*
114
compat_pci_name(struct pci_dev* pdev)
116
static char slot_name[12];
117
sprintf(slot_name, "%02X:%02X.%X", pdev->bus->number,
118
PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
122
static inline const char*
123
compat_pci_name(struct pci_dev* pdev)
125
static char slot_name[12];
126
sprintf(slot_name, "%02X:%02X.%X", pdev->bus,
127
PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
133
/* pci_resource_start comes in 4 flavors - 2.0, 2.2, early 2.3, 2.4+ */
135
static inline unsigned long
136
compat_pci_resource_start(struct pci_dev *pdev,
141
if (pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0 + index * 4, &addr)) {
142
printk(KERN_ERR "Unable to read base address %u from PCI slot %s!\n",
143
index, compat_pci_name(pdev));
146
if (addr & PCI_BASE_ADDRESS_SPACE) {
147
return addr & PCI_BASE_ADDRESS_IO_MASK;
149
return addr & PCI_BASE_ADDRESS_MEM_MASK;
152
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 1)
153
# define compat_pci_resource_start(dev, index) \
154
(((dev)->base_address[index] & PCI_BASE_ADDRESS_SPACE) \
155
? ((dev)->base_address[index] & PCI_BASE_ADDRESS_IO_MASK) \
156
: ((dev)->base_address[index] & PCI_BASE_ADDRESS_MEM_MASK))
157
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 43)
158
# define compat_pci_resource_start(dev, index) \
159
((dev)->resource[index].start)
161
# define compat_pci_resource_start(dev, index) \
162
pci_resource_start(dev, index)
165
/* since 2.3.15, a new set of s/w res flags IORESOURCE_ is introduced,
166
* we fake them by returning either IORESOURCE_{IO, MEM} prior to 2.3.15 since
167
* this is what compat_pci_request_region uses
170
static inline unsigned long
171
compat_pci_resource_flags(struct pci_dev *pdev,
176
if (pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0 + index * 4, &addr)) {
177
printk(KERN_ERR "Unable to read base address %u from PCI slot %s!\n",
178
index, compat_pci_name(pdev));
181
if (addr & PCI_BASE_ADDRESS_SPACE) {
182
return IORESOURCE_IO;
184
return IORESOURCE_MEM;
187
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 1)
188
# define compat_pci_resource_flags(dev, index) \
189
(((dev)->base_address[index] & PCI_BASE_ADDRESS_SPACE) \
190
? IORESOURCE_IO: IORESOURCE_MEM)
191
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 15)
192
/* IORESOURCE_xxx appeared in 2.3.15 and is set in resource[].flags */
193
# define compat_pci_resource_flags(dev, index) ((dev)->resource[index].flags)
195
# define compat_pci_resource_flags(dev, index) pci_resource_flags(dev, index)
198
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 2, 18)
199
static inline unsigned long
200
compat_pci_resource_len(struct pci_dev *pdev, // IN
201
unsigned int index) // IN
204
unsigned char reg = PCI_BASE_ADDRESS_0 + index * 4;
206
if (pci_read_config_dword(pdev, reg, &addr) || addr == 0xFFFFFFFF) {
210
pci_write_config_dword(pdev, reg, 0xFFFFFFFF);
211
pci_read_config_dword(pdev, reg, &mask);
212
pci_write_config_dword(pdev, reg, addr);
214
if (mask == 0 || mask == 0xFFFFFFFF) {
217
if (addr & PCI_BASE_ADDRESS_SPACE) {
218
return 65536 - (mask & PCI_BASE_ADDRESS_IO_MASK & 0xFFFF);
220
return -(mask & PCI_BASE_ADDRESS_MEM_MASK);
224
#define compat_pci_resource_len(dev, index) pci_resource_len(dev, index)
227
/* pci_request_region appears in 2.4.20 */
228
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 20)
230
compat_pci_request_region(struct pci_dev *pdev, int bar, char *name)
232
if (compat_pci_resource_len(pdev, bar) == 0) {
236
if (compat_pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
237
if (!compat_request_region(compat_pci_resource_start(pdev, bar),
238
compat_pci_resource_len(pdev, bar),
242
} else if (compat_pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
243
if (!compat_request_mem_region(compat_pci_resource_start(pdev, bar),
244
compat_pci_resource_len(pdev, bar),
254
compat_pci_release_region(struct pci_dev *pdev, int bar)
256
if (compat_pci_resource_len(pdev, bar) != 0) {
257
if (compat_pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
258
release_region(compat_pci_resource_start(pdev, bar),
259
compat_pci_resource_len(pdev, bar));
260
} else if (compat_pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
261
compat_release_mem_region(compat_pci_resource_start(pdev, bar),
262
compat_pci_resource_len(pdev, bar));
267
#define compat_pci_request_region(pdev, bar, name) pci_request_region(pdev, bar, name)
268
#define compat_pci_release_region(pdev, bar) pci_release_region(pdev, bar)
271
/* pci_request_regions appeears in 2.4.3 */
272
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 3)
274
compat_pci_request_regions(struct pci_dev *pdev, char *name)
278
for (i = 0; i < 6; i++) {
279
if (compat_pci_request_region(pdev, i, name)) {
287
compat_pci_release_region(pdev, i);
292
compat_pci_release_regions(struct pci_dev *pdev)
296
for (i = 0; i < 6; i++) {
297
compat_pci_release_region(pdev, i);
301
#define compat_pci_request_regions(pdev, name) pci_request_regions(pdev, name)
302
#define compat_pci_release_regions(pdev) pci_release_regions(pdev)
305
/* pci_enable_device is available since 2.4.0 */
306
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0)
307
#define compat_pci_enable_device(pdev) (0)
309
#define compat_pci_enable_device(pdev) pci_enable_device(pdev)
313
/* pci_set_master is available since 2.2.0 */
314
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 2, 0)
315
#define compat_pci_set_master(pdev) (0)
317
#define compat_pci_set_master(pdev) pci_set_master(pdev)
321
/* pci_disable_device is available since 2.4.4 */
322
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 4)
323
#define compat_pci_disable_device(pdev) do {} while (0)
325
#define compat_pci_disable_device(pdev) pci_disable_device(pdev)
329
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0)
331
* Devices supported by particular pci driver. While 2.4+ kernels
332
* can do match on subsystem and class too, we support match on
333
* vendor/device IDs only.
335
struct pci_device_id {
336
unsigned int vendor, device;
337
unsigned long driver_data;
339
#define PCI_DEVICE(vend, dev) .vendor = (vend), .device = (dev)
344
const struct pci_device_id *id_table;
345
int (*probe)(struct pci_dev* dev, const struct pci_device_id* id);
346
void (*remove)(struct pci_dev* dev);
351
* Note that this is static variable. Maybe everything below should be in
352
* separate compat_pci.c file, but currently only user of this file is vmxnet,
353
* and vmxnet has only one file, so it is fine. Also with vmxnet all
354
* functions below are called just once, so difference between 'inline' and
355
* separate compat_pci.c should be very small.
358
static struct vmw_pci_driver_instance *pci_driver_instances = NULL;
361
#define vmw_pci_device(instance) (instance)->pcidev
363
#define vmw_pci_device(instance) (instance)
368
*-----------------------------------------------------------------------------
370
* pci_register_driver --
372
* Create driver instances for all matching PCI devices in the box.
375
* Returns 0 for success, negative error value for failure.
380
*-----------------------------------------------------------------------------
384
pci_register_driver(struct pci_driver *drv)
386
const struct pci_device_id *chipID;
388
for (chipID = drv->id_table; chipID->vendor; chipID++) {
390
struct pci_dev *pdev;
393
(pdev = pci_find_device(chipID->vendor, chipID->device, pdev)) != NULL; ) {
396
unsigned char bus, devfn, irq;
399
pcibios_find_device(chipID->vendor, chipID->device, adapter,
403
struct vmw_pci_driver_instance *pdi;
406
pdi = kmalloc(sizeof *pdi, GFP_KERNEL);
408
printk(KERN_ERR "Not enough memory.\n");
417
if (pci_read_config_byte(pdi, PCI_INTERRUPT_LINE, &irq)) {
423
pdi->driver_data = NULL;
424
pdi->next = pci_driver_instances;
425
pci_driver_instances = pdi;
426
err = drv->probe(vmw_pci_device(pdi), chipID);
428
pci_driver_instances = pdi->next;
438
*-----------------------------------------------------------------------------
440
* compat_pci_unregister_driver --
442
* Shut down PCI driver - unbind all device instances from driver.
450
*-----------------------------------------------------------------------------
454
pci_unregister_driver(struct pci_driver *drv)
456
struct vmw_pci_driver_instance **ppdi;
458
ppdi = &pci_driver_instances;
460
struct vmw_pci_driver_instance *pdi = *ppdi;
465
if (pdi->pcidrv == drv) {
466
drv->remove(vmw_pci_device(pdi));
475
/* provide PCI_DEVICE for early 2.4.x kernels */
477
#define PCI_DEVICE(vend, dev) .vendor = (vend), .device = (dev), \
478
.subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID
483
/* provide dummy MODULE_DEVICE_TABLE for 2.0/2.2 */
484
#ifndef MODULE_DEVICE_TABLE
485
#define MODULE_DEVICE_TABLE(bus, devices)
490
*-----------------------------------------------------------------------------
494
* Set per-device driver's private data.
502
*-----------------------------------------------------------------------------
506
*-----------------------------------------------------------------------------
510
* Retrieve per-device driver's private data.
513
* per-device driver's data previously set by pci_set_drvdata,
514
* or NULL on failure.
519
*-----------------------------------------------------------------------------
523
/* 2.0.x is simple, we have driver_data directly in pci_dev */
524
#define pci_set_drvdata(pdev, data) do { (pdev)->driver_data = (data); } while (0)
525
#define pci_get_drvdata(pdev) (pdev)->driver_data
526
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0)
527
/* 2.2.x is trickier, we have to find driver instance first */
529
pci_set_drvdata(struct pci_dev *pdev, void* data)
531
struct vmw_pci_driver_instance *pdi;
533
for (pdi = pci_driver_instances; pdi; pdi = pdi->next) {
534
if (pdi->pcidev == pdev) {
535
pdi->driver_data = data;
539
printk(KERN_ERR "pci_set_drvdata issued for unknown device %p\n", pdev);
543
pci_get_drvdata(struct pci_dev *pdev)
545
struct vmw_pci_driver_instance *pdi;
547
for (pdi = pci_driver_instances; pdi; pdi = pdi->next) {
548
if (pdi->pcidev == pdev) {
549
return pdi->driver_data;
552
printk(KERN_ERR "pci_get_drvdata issued for unknown device %p\n", pdev);
557
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,48)
558
# define PCI_DMA_BIDIRECTIONAL 0
559
# define PCI_DMA_TODEVICE 1
560
# define PCI_DMA_FROMDEVICE 2
561
# define PCI_DMA_NONE 3
30
# define DMA_BIT_MASK(n) DMA_##n##BIT_MASK