diff options
| author | Tzafrir Cohen <tzafrir@debian.org> | 2022-08-17 12:12:52 +0300 |
|---|---|---|
| committer | git-ubuntu importer <ubuntu-devel-discuss@lists.ubuntu.com> | 2022-08-17 16:34:12 +0000 |
| commit | 889a33c97f3d650be0b72dd3080feedb7fc061c1 (patch) | |
| tree | 9b0ac4fb540d8bece7d6f765e29d17ec4dfc3c78 /mtcr_py | |
| parent | 21849d048cf5242368eda7bb64f4073723f3eea2 (diff) | |
4.21.0+1-1 (patches unapplied)import/4.21.0+1-1ubuntu/manticubuntu/lunar-develubuntu/lunarubuntu/kinetic-proposedubuntu/kinetic-develubuntu/kineticdebian/bookworm
Imported using git-ubuntu import.
Notes
Notes: * New upstream release: - 0003-Fix-man-page-errors.patch removed: merged upstream - malloc_delete_free.patch removed: merged upstream * Add man-proper-header-line.patch * Add typos.patch
Diffstat (limited to 'mtcr_py')
| -rw-r--r-- | mtcr_py/Makefile.am | 1 | ||||
| -rw-r--r-- | mtcr_py/Makefile.in | 5 | ||||
| -rw-r--r-- | mtcr_py/cmtcr.c | 165 | ||||
| -rw-r--r-- | mtcr_py/mtcr.py | 63 | ||||
| -rwxr-xr-x | mtcr_py/test.py | 3 |
5 files changed, 137 insertions, 100 deletions
diff --git a/mtcr_py/Makefile.am b/mtcr_py/Makefile.am index 351eb81..934b803 100644 --- a/mtcr_py/Makefile.am +++ b/mtcr_py/Makefile.am @@ -1,4 +1,5 @@ # Copyright (c) 2004-2010 Mellanox Technologies LTD. All rights reserved. +# Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # # This software is available to you under a choice of one of two # licenses. You may choose to be licensed under the terms of the GNU diff --git a/mtcr_py/Makefile.in b/mtcr_py/Makefile.in index 705a7af..bef80d1 100644 --- a/mtcr_py/Makefile.in +++ b/mtcr_py/Makefile.in @@ -1,7 +1,7 @@ -# Makefile.in generated by automake 1.16.3 from Makefile.am. +# Makefile.in generated by automake 1.16.1 from Makefile.am. # @configure_input@ -# Copyright (C) 1994-2020 Free Software Foundation, Inc. +# Copyright (C) 1994-2018 Free Software Foundation, Inc. # This Makefile.in is free software; the Free Software Foundation # gives unlimited permission to copy and/or distribute it, @@ -15,6 +15,7 @@ @SET_MAKE@ # Copyright (c) 2004-2010 Mellanox Technologies LTD. All rights reserved. +# Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # # This software is available to you under a choice of one of two # licenses. You may choose to be licensed under the terms of the GNU diff --git a/mtcr_py/cmtcr.c b/mtcr_py/cmtcr.c index 499a045..85c1121 100644 --- a/mtcr_py/cmtcr.c +++ b/mtcr_py/cmtcr.c @@ -1,6 +1,6 @@ /* - * Copyright (C) Jan 2013 Mellanox Technologies Ltd. All rights reserved. + * Copyright (c) 2013-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -38,33 +38,34 @@ #include <errno.h> #define CMTCR_UNUSED(var) ((void)(var)) -static PyObject *mtcrModule = 0; -static PyObject *mtcrExceptionType = 0; +static PyObject* mtcrModule = 0; +static PyObject* mtcrExceptionType = 0; /********************************************** * mtcr_init * @return *********************************************/ -PyObject* mtcr_init(PyObject *self, PyObject *args) +PyObject* mtcr_init(PyObject* self, PyObject* args) { - PyObject *mfileObj; - const char *dev; - mfile *mf; - + PyObject* mfileObj; + const char* dev; + mfile* mf; CMTCR_UNUSED(self); // Parse args - if (!PyArg_ParseTuple(args, "Os", &mfileObj, &dev)) { + if (!PyArg_ParseTuple(args, "Os", &mfileObj, &dev)) + { return NULL; } mf = mopen(dev); - if (!mf) { + if (!mf) + { PyErr_SetString(mtcrExceptionType, strerror(errno)); return NULL; } - PyObject *mfLong = PyLong_FromVoidPtr(mf); + PyObject* mfLong = PyLong_FromVoidPtr(mf); PyObject_SetAttrString(mfileObj, "mf", mfLong); Py_DecRef(mfLong); @@ -74,21 +75,23 @@ PyObject* mtcr_init(PyObject *self, PyObject *args) /********************************************** * mtcr_close *********************************************/ -PyObject* mtcr_close(PyObject *self, PyObject *args) +PyObject* mtcr_close(PyObject* self, PyObject* args) { - PyObject *mfileObj; - const char *dev; - mfile *mf; + PyObject* mfileObj; + const char* dev; + mfile* mf; CMTCR_UNUSED(self); // Parse args - if (!PyArg_ParseTuple(args, "Os", &mfileObj, &dev)) { + if (!PyArg_ParseTuple(args, "Os", &mfileObj, &dev)) + { return NULL; } - PyObject *mfLong = PyObject_GetAttrString(mfileObj, "mf"); - mf = (mfile*) PyLong_AsVoidPtr(mfLong); - if (mf) { + PyObject* mfLong = PyObject_GetAttrString(mfileObj, "mf"); + mf = (mfile*)PyLong_AsVoidPtr(mfLong); + if (mf) + { mclose(mf); } Py_DecRef(mfLong); @@ -99,19 +102,20 @@ PyObject* mtcr_close(PyObject *self, PyObject *args) /********************************************** * mtcr_del *********************************************/ -PyObject* mtcr_del(PyObject *self, PyObject *args) +PyObject* mtcr_del(PyObject* self, PyObject* args) { - PyObject *mfileObj; - const char *dev; + PyObject* mfileObj; + const char* dev; CMTCR_UNUSED(self); // Parse args - if (!PyArg_ParseTuple(args, "Os", &mfileObj, &dev)) { + if (!PyArg_ParseTuple(args, "Os", &mfileObj, &dev)) + { return NULL; } mtcr_close(self, args); - PyObject *mfLong = PyLong_FromLong(0); + PyObject* mfLong = PyLong_FromLong(0); PyObject_SetAttrString(mfLong, "mf", mfLong); Py_DecRef(mfLong); @@ -121,25 +125,27 @@ PyObject* mtcr_del(PyObject *self, PyObject *args) /********************************************** * mtcr_read4 *********************************************/ -PyObject* mtcr_read4(PyObject *self, PyObject *args) +PyObject* mtcr_read4(PyObject* self, PyObject* args) { - PyObject *mfileObj; + PyObject* mfileObj; int addr; - mfile *mf; + mfile* mf; u_int32_t value; - PyObject *valueObj; + PyObject* valueObj; CMTCR_UNUSED(self); // Parse args - if (!PyArg_ParseTuple(args, "Oi", &mfileObj, &addr)) { + if (!PyArg_ParseTuple(args, "Oi", &mfileObj, &addr)) + { return NULL; } - PyObject *mfLong = PyObject_GetAttrString(mfileObj, "mf"); - mf = (mfile*) PyLong_AsVoidPtr(mfLong); + PyObject* mfLong = PyObject_GetAttrString(mfileObj, "mf"); + mf = (mfile*)PyLong_AsVoidPtr(mfLong); Py_DecRef(mfLong); - if (mread4(mf, addr, &value) != 4) { + if (mread4(mf, addr, &value) != 4) + { PyErr_SetString(mtcrExceptionType, strerror(errno)); return NULL; } @@ -151,40 +157,44 @@ PyObject* mtcr_read4(PyObject *self, PyObject *args) /********************************************** * mtcr_read4 *********************************************/ -PyObject* mtcr_read4block(PyObject *self, PyObject *args) +PyObject* mtcr_read4block(PyObject* self, PyObject* args) { - PyObject *mfileObj; + PyObject* mfileObj; int addr; int i; - mfile *mf; + mfile* mf; int dword_len; - PyObject *data_list; - u_int32_t *data_buf; - PyObject *mfLong; + PyObject* data_list; + u_int32_t* data_buf; + PyObject* mfLong; CMTCR_UNUSED(self); // Parse args - if (!PyArg_ParseTuple(args, "Oii", &mfileObj, &addr, &dword_len)) { + if (!PyArg_ParseTuple(args, "Oii", &mfileObj, &addr, &dword_len)) + { return NULL; } mfLong = PyObject_GetAttrString(mfileObj, "mf"); - mf = (mfile*) PyLong_AsVoidPtr(mfLong); + mf = (mfile*)PyLong_AsVoidPtr(mfLong); Py_DecRef(mfLong); data_buf = malloc(sizeof(u_int32_t) * dword_len); - if (!data_buf) { + if (!data_buf) + { PyErr_SetString(mtcrExceptionType, "Out read of memory"); return NULL; } - if (mread4_block(mf, addr, data_buf, dword_len * 4) != (dword_len * 4)) { + if (mread4_block(mf, addr, data_buf, dword_len * 4) != (dword_len * 4)) + { PyErr_SetString(mtcrExceptionType, strerror(errno)); return NULL; } data_list = PyList_New(dword_len); - for (i = 0; i < dword_len; i++) { + for (i = 0; i < dword_len; i++) + { PyList_SET_ITEM(data_list, i, PyInt_FromLong((long)data_buf[i])); } @@ -194,24 +204,26 @@ PyObject* mtcr_read4block(PyObject *self, PyObject *args) /********************************************** * mtcr_write4 *********************************************/ -PyObject* mtcr_write4(PyObject *self, PyObject *args) +PyObject* mtcr_write4(PyObject* self, PyObject* args) { - PyObject *mfileObj; + PyObject* mfileObj; int addr; - mfile *mf; + mfile* mf; u_int32_t value; CMTCR_UNUSED(self); // Parse args - if (!PyArg_ParseTuple(args, "Oii", &mfileObj, &addr, &value)) { + if (!PyArg_ParseTuple(args, "Oii", &mfileObj, &addr, &value)) + { return NULL; } - PyObject *mfLong = PyObject_GetAttrString(mfileObj, "mf"); - mf = (mfile*) PyLong_AsVoidPtr(mfLong); + PyObject* mfLong = PyObject_GetAttrString(mfileObj, "mf"); + mf = (mfile*)PyLong_AsVoidPtr(mfLong); Py_DecRef(mfLong); - if (mwrite4(mf, addr, value) != 4) { + if (mwrite4(mf, addr, value) != 4) + { PyErr_SetString(mtcrExceptionType, strerror(errno)); return NULL; } @@ -222,39 +234,43 @@ PyObject* mtcr_write4(PyObject *self, PyObject *args) /********************************************** * mtcr_write4block *********************************************/ -PyObject* mtcr_write4block(PyObject *self, PyObject *args) +PyObject* mtcr_write4block(PyObject* self, PyObject* args) { - PyObject *mfileObj; + PyObject* mfileObj; int addr; int i; - mfile *mf; + mfile* mf; int dword_len; - PyObject *data_list; - u_int32_t *data_buf; - PyObject *mfLong; + PyObject* data_list; + u_int32_t* data_buf; + PyObject* mfLong; CMTCR_UNUSED(self); // Parse args - if (!PyArg_ParseTuple(args, "OiO", &mfileObj, &addr, &data_list)) { + if (!PyArg_ParseTuple(args, "OiO", &mfileObj, &addr, &data_list)) + { return NULL; } mfLong = PyObject_GetAttrString(mfileObj, "mf"); - mf = (mfile*) PyLong_AsVoidPtr(mfLong); + mf = (mfile*)PyLong_AsVoidPtr(mfLong); Py_DecRef(mfLong); dword_len = PyList_Size(data_list); data_buf = malloc(sizeof(u_int32_t) * dword_len); - if (!data_buf) { + if (!data_buf) + { PyErr_SetString(mtcrExceptionType, "Write Out of memory"); return NULL; } - for (i = 0; i < dword_len; i++) { + for (i = 0; i < dword_len; i++) + { data_buf[i] = PyLong_AsUnsignedLong(PyList_GET_ITEM(data_list, i)); } - if (mwrite4_block(mf, addr, data_buf, dword_len * 4) != (dword_len * 4)) { + if (mwrite4_block(mf, addr, data_buf, dword_len * 4) != (dword_len * 4)) + { PyErr_SetString(mtcrExceptionType, strerror(errno)); return NULL; } @@ -265,14 +281,15 @@ PyObject* mtcr_write4block(PyObject *self, PyObject *args) /********************************************** * mtcr_funcs static db *********************************************/ -static PyMethodDef mtcr_funcs [] = { {"mtcr_init", (PyCFunction)mtcr_init, METH_VARARGS, "Init mfile object"}, - {"mtcr_close", (PyCFunction)mtcr_close, METH_VARARGS, "Close internal mfile"}, - {"mtcr_del", (PyCFunction)mtcr_del, METH_VARARGS, "Free resources"}, - {"mtcr_read4", (PyCFunction)mtcr_read4, METH_VARARGS, "Read dword"}, - {"mtcr_write4", (PyCFunction)mtcr_write4, METH_VARARGS, "Write dword"}, - {"mtcr_read4block", (PyCFunction)mtcr_read4block, METH_VARARGS, "Read block of dwords"}, - {"mtcr_write4block", (PyCFunction)mtcr_write4block, METH_VARARGS, "Write block of dwords"}, - {NULL, NULL, 0, NULL} }; +static PyMethodDef mtcr_funcs[] = { + {"mtcr_init", (PyCFunction)mtcr_init, METH_VARARGS, "Init mfile object"}, + {"mtcr_close", (PyCFunction)mtcr_close, METH_VARARGS, "Close internal mfile"}, + {"mtcr_del", (PyCFunction)mtcr_del, METH_VARARGS, "Free resources"}, + {"mtcr_read4", (PyCFunction)mtcr_read4, METH_VARARGS, "Read dword"}, + {"mtcr_write4", (PyCFunction)mtcr_write4, METH_VARARGS, "Write dword"}, + {"mtcr_read4block", (PyCFunction)mtcr_read4block, METH_VARARGS, "Read block of dwords"}, + {"mtcr_write4block", (PyCFunction)mtcr_write4block, METH_VARARGS, "Write block of dwords"}, + {NULL, NULL, 0, NULL}}; #define STR_EXPAND(tok) #tok #define STR(tok) STR_EXPAND(tok) @@ -287,20 +304,22 @@ static PyMethodDef mtcr_funcs [] = { {"mtcr_init", (PyCFunction)mtcr_init, METH_ * initcmtcr * @return *********************************************/ -#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ +#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ #define PyMODINIT_FUNC void #endif PyMODINIT_FUNC INIT_FUNC() { Py_InitModule(MODULE_NAME, mtcr_funcs); mtcrModule = PyImport_ImportModule("mtcr"); - if (!mtcrModule) { + if (!mtcrModule) + { PyErr_SetString(mtcrExceptionType, "Failed to import module"); return; } - mtcrExceptionType = PyObject_GetAttrString(mtcrModule, "MtcrException"); - if (!mtcrExceptionType) { + mtcrExceptionType = PyObject_GetAttrString(mtcrModule, "MtcrException"); + if (!mtcrExceptionType) + { PyErr_SetString(PyExc_ImportError, "Failed to import MtcrException class"); return; } diff --git a/mtcr_py/mtcr.py b/mtcr_py/mtcr.py index dfcca99..4fdcc45 100644 --- a/mtcr_py/mtcr.py +++ b/mtcr_py/mtcr.py @@ -1,5 +1,6 @@ # Copyright (c) 2004-2010 Mellanox Technologies LTD. All rights reserved. +# Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # # This software is available to you under a choice of one of two # licenses. You may choose to be licensed under the terms of the GNU @@ -28,41 +29,51 @@ # 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. -#-- +# -- import os import sys import platform import ctypes +import struct + def ones(n): return (1 << n) - 1 + def extractField(val, start, size): - return (val & (ones(size)<<start)) >> start + return (val & (ones(size) << start)) >> start + def insertField(val1, start1, val2, start2, size): - return val1 & ~(ones(size)<<start1) | (extractField(val2, start2, size) << start1) + return val1 & ~(ones(size) << start1) | (extractField(val2, start2, size) << start1) + class MtcrException(Exception): pass + class CmdIfException(Exception): pass + ########################## CMTCR = None try: from ctypes import * ctypes.CDLL._func_restype_ = ctypes.c_ulonglong if platform.system() == "Windows" or os.name == "nt": - CMTCR = CDLL(".\\libmtcr-1.dll", use_errno=True) + try: + CMTCR = CDLL("libmtcr-1.dll", use_errno=True) + except BaseException: + CMTCR = CDLL(os.path.join(os.path.dirname(os.path.realpath(__file__)), "libmtcr-1.dll"), use_errno=True) else: try: CMTCR = CDLL("cmtcr.so", use_errno=True) - except: + except BaseException: CMTCR = CDLL(os.path.join(os.path.dirname(os.path.realpath(__file__)), "cmtcr.so"), use_errno=True) -except: +except BaseException: CMTCR = None if CMTCR: @@ -93,13 +104,13 @@ if CMTCR: def is_cable(self): return '_cable' in str(self.mdev) - + def is_linkx(self): return '_lx' in str(self.mdev) def open(self): if self.is_cable() or self.is_linkx(): - self.mf = ctypes.c_void_p(self.mopenFuncAdv(self.mdev,0xffffffff)) + self.mf = ctypes.c_void_p(self.mopenFuncAdv(self.mdev, 0xffffffff)) else: self.mf = ctypes.c_void_p(self.mopenFunc(self.mdev)) @@ -107,8 +118,6 @@ if CMTCR: error = os.strerror(ctypes.get_errno()) raise MtcrException("Failed to re-open device (%s): %s" % (self.mdev.decode("utf-8"), error)) - - ########################## def __del__(self): self.close() @@ -139,9 +148,9 @@ if CMTCR: self.write4(addr, newVal) ########################## - def read4Block(self, addr, size): # size in dwords + def read4Block(self, addr, size): # size in dwords dataArr = (c_uint32 * size)() - if self.mread4BlockFunc(self.mf, addr, cast(dataArr, POINTER(c_uint32)), size*4) != size*4: + if self.mread4BlockFunc(self.mf, addr, cast(dataArr, POINTER(c_uint32)), size * 4) != size * 4: raise MtcrException("Failed to read block from mst device from address 0x%x of size %d bytes" % (addr, size)) return list(dataArr) @@ -149,33 +158,39 @@ if CMTCR: def write4Block(self, addr, dataList): size = len(dataList) dataArr = (c_uint32 * size)(*dataList) - if self.mwrite4BlockFunc(self.mf, addr, cast(dataArr, POINTER(c_uint32)), size*4) != size*4: + if self.mwrite4BlockFunc(self.mf, addr, cast(dataArr, POINTER(c_uint32)), size * 4) != size * 4: raise MtcrException("Failed to write block to mst device to address 0x%x of size %d bytes" % (addr, size)) - + ########################## def icmdSendCmd(self, opcode, data, skipWrite): dataArr = (c_uint8 * len(data))(*data) rc = self.icmdSendCommandFunc(self.mf, opcode, cast(dataArr, POINTER(c_uint8)), len(data), skipWrite) + + # data can also be used as output so need to convert back from dataArr. + for i, x in enumerate(dataArr): + data[i] = dataArr[i] if rc: raise CmdIfException("Failed to send command") ########################## def mHcaReset(self, dbdf_list): - + bus_array_size = len(dbdf_list) bus_array = (ctypes.c_ulong * bus_array_size)() - + # Extract the bus value from the dbdf list. - for idx,dbdf in enumerate(dbdf_list): + for idx, dbdf in enumerate(dbdf_list): bus_array[idx] = int(dbdf[5:7], 16) if self.mHcaResetFunc(self.mf, bus_array, bus_array_size) != 0: raise MtcrException("Failed to reset device") + ########################## + else: import subprocess import string - + def getstatusoutput(cmd): pipe = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) cout = pipe.stdout @@ -228,22 +243,22 @@ else: self.write4(addr, newVal) ########################## - def read4Block(self, addr, size): # size in dwords + def read4Block(self, addr, size): # size in dwords l = [] - for add in range(addr, addr + size*4, 4): + for add in range(addr, addr + size * 4, 4): l.append(self.read4(add)) return l ########################## def write4Block(self, addr, dataList): size = len(dataList) - for i, current_addr in enumerate(range(addr, addr + size*4, 4)): + for i, current_addr in enumerate(range(addr, addr + size * 4, 4)): self.write4(current_addr, dataList[i]) - - ########################## + + ########################## def icmdSendCmd(self, opcode, data, skipWrite): raise MtcrException("icmd isn't supported in MCRA mode") - ########################## + ########################## def mHcaReset(self, dbdf_list): raise MtcrException("mswReset isn't supported in MCRA mode") diff --git a/mtcr_py/test.py b/mtcr_py/test.py index 4b8cf3a..71d4232 100755 --- a/mtcr_py/test.py +++ b/mtcr_py/test.py @@ -1,4 +1,5 @@ # Copyright (c) 2004-2010 Mellanox Technologies LTD. All rights reserved. +# Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # # This software is available to you under a choice of one of two # licenses. You may choose to be licensed under the terms of the GNU @@ -27,7 +28,7 @@ # 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. -#-- +# -- from __future__ import print_function |
