/*************************************************************************\ * Copyright (c) 2010 Brookhaven Science Associates, as Operator of * Brookhaven National Laboratory. * devLib2 is distributed subject to a Software License Agreement found * in file LICENSE that is included with this distribution. \*************************************************************************/ /* * Author: Michael Davidsaver */ #include #include #include #include #include #include #include #include #include static const epicsPCIDevice *diagdev; static volatile void *diagbase; static epicsUInt32 diaglen; struct bdf { unsigned int b,d,f; const epicsPCIDevice *dev; }; static int matchbdf(void* raw,const epicsPCIDevice* dev) { struct bdf *loc=raw; if(dev->bus!=loc->b || dev->device!=loc->d || dev->function!=loc->f) return 0; loc->dev=dev; return 1; } void pcidiagset(int b, int d, int f, int bar, int vendor, int device, int exact) { epicsUInt32 len=0; struct bdf loc; epicsPCIID ids[] = { DEVPCI_DEVICE_VENDOR(device,vendor), DEVPCI_END }; diagbase = NULL; diagdev = NULL; diaglen = 0; printf("Looking for %x:%x.%x\n", b, d, f); if(vendor==0 && !exact) ids[0].vendor=DEVPCI_ANY_VENDOR; if(device==0 && !exact) ids[0].device=DEVPCI_ANY_DEVICE; loc.b=b; loc.d=d; loc.f=f; loc.dev=0; if(devPCIFindCB(ids, &matchbdf, (void*)&loc, 0)) { printf("Error searching\n"); return; } if(!loc.dev) { printf("No such device\n"); return; } printf("Mapping %x:%x.%x\n", loc.dev->bus, loc.dev->device, loc.dev->function); #if defined(linux) if(devPCIBarLen(loc.dev, bar, &len)) { printf("Failed to get BAR length\n"); len=0; } #endif if(devPCIToLocalAddr(loc.dev, bar, &diagbase, 0)) { printf("Failed to map BAR\n"); return; } diagdev = loc.dev; diaglen=len; #if defined(__linux__) printf("BAR %d from %p for %u bytes\n",bar, (void*)diagbase, (unsigned)diaglen); #else printf("BAR %d from %p\n",bar, (void*)diagbase); #endif } static int check_args(int dmod, unsigned int offset, unsigned int count) { switch(dmod){ case 8: case 16: case 32: break; default: printf("Invalid data width %d\n",dmod); return 1; } #if defined(__linux__) if(offset>=diaglen || offset+count>diaglen) { printf("Invalid offset and/or count\n"); return 1; } #endif return 0; } void pciwrite(int dmod, int offset, int value) { epicsUInt32 tval = value; volatile char* dptr = offset + (volatile char*)diagbase; if(!diagbase) { printf("Run pcidiagset first\n"); return; } if(check_args(dmod, offset, 1)) return; switch(dmod){ case 8: iowrite8(dptr, tval); break; case 16: nat_iowrite16(dptr, tval); break; case 32: nat_iowrite32(dptr, tval); break; } } void pciread(int dmod, int offset, int count) { epicsUInt32 tval; volatile char* dptr; short dbytes; int i; if(!diagbase) { printf("Run pcidiagset first\n"); return; } if(check_args(dmod, offset, count)) return; dbytes=dmod/8; count/=dbytes; if(count==0) count=1; for(i=0, dptr=offset+(volatile char*)diagbase; i