|
1 |
| -echo 1 > /sys/devices/pci0000:00/0000:00:02.2/0000:02:00.0/mux |
| 1 | +#!/usr/bin/python |
| 2 | +from __future__ import print_function |
| 3 | + |
| 4 | +import mmap |
| 5 | +import os |
| 6 | +import sys |
| 7 | +from struct import pack, unpack |
| 8 | + |
| 9 | +class MmapResource( object ): |
| 10 | + """Resource implementation for a directly-mapped memory region.""" |
| 11 | + def __init__( self, path ): |
| 12 | + try: |
| 13 | + fd = os.open( path, os.O_RDWR ) |
| 14 | + except EnvironmentError: |
| 15 | + print( "FAIL can not open scd memory-map resource file" ) |
| 16 | + print( "FAIL are you running on the proper platform?" ) |
| 17 | + sys.exit( 1 ) |
| 18 | + try: |
| 19 | + size = os.fstat( fd ).st_size |
| 20 | + except EnvironmentError: |
| 21 | + print( "FAIL can not fstat scd memory-map resource file" ) |
| 22 | + print( "FAIL are you running on the proper platform?" ) |
| 23 | + sys.exit( 1 ) |
| 24 | + try: |
| 25 | + self.mmap_ = mmap.mmap( fd, size, mmap.MAP_SHARED, |
| 26 | + mmap.PROT_READ | mmap.PROT_WRITE ) |
| 27 | + except EnvironmentError: |
| 28 | + print( "FAIL can not map scd memory-map file" ) |
| 29 | + print( "FAIL are you running on the proper platform?" ) |
| 30 | + sys.exit( 1 ) |
| 31 | + finally: |
| 32 | + try: |
| 33 | + # Note that closing the file descriptor has no effect on the memory map |
| 34 | + os.close( fd ) |
| 35 | + except EnvironmentError: |
| 36 | + print( "FAIL failed to close scd memory-map file" ) |
| 37 | + sys.exit( 1 ) |
| 38 | + def read32( self, addr ): |
| 39 | + return unpack( '<L', self.mmap_[ addr : addr + 4 ] )[ 0 ] |
| 40 | + def write32( self, addr, value ): |
| 41 | + self.mmap_[ addr: addr + 4 ] = pack( '<L', value ) |
| 42 | + |
| 43 | +m = MmapResource( '/sys/bus/pci/devices/0000:02:00.0/resource0' ) |
| 44 | +for addr in range( 0x5210, 0x5250, 0x10 ): |
| 45 | + v = m.read32( addr ) |
| 46 | + m.write32( addr, v & ~( 1 << 6 ) ) |
| 47 | + print( "orig=%04x new=%04x" % ( v, m.read32( addr ) ) ) |
| 48 | + |
| 49 | +with open( "/sys/devices/pci0000:00/0000:00:02.2/0000:02:00.0/mux_sfp_qsfp/direction", "w" ) as fdir: |
| 50 | + fdir.write( "out" ) |
| 51 | +with open( "/sys/devices/pci0000:00/0000:00:02.2/0000:02:00.0/mux_sfp_qsfp/value", "w" ) as fval: |
| 52 | + fval.write( "1" ) |
0 commit comments