Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  * This file is provided under a dual BSD/GPLv2 license.  When using or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *   redistributing this file, you may do so under either license.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *   GPL LICENSE SUMMARY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *   Copyright (C) 2017 T-Platforms All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *   This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *   it under the terms of version 2 of the GNU General Public License as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *   published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *   This program is distributed in the hope that it will be useful, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *   WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  *   General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *   BSD LICENSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  *   Copyright (C) 2017 T-Platforms All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  *   Redistribution and use in source and binary forms, with or without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  *   modification, are permitted provided that the following conditions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  *   are met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  *     * Redistributions of source code must retain the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  *       notice, this list of conditions and the following disclaimer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  *     * Redistributions in binary form must reproduce the above copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  *       notice, this list of conditions and the following disclaimer in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  *       the documentation and/or other materials provided with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  *       distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  *     * Neither the name of Intel Corporation nor the names of its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  *       contributors may be used to endorse or promote products derived
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  *       from this software without specific prior written permission.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  * PCIe NTB Debugging Tool Linux driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * How to use this tool, by example.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  * Assuming $DBG_DIR is something like:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  * '/sys/kernel/debug/ntb_tool/0000:00:03.0'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * Suppose aside from local device there is at least one remote device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * connected to NTB with index 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  *-----------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  * Eg: check local/peer device information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  * # Get local device port number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  * root@self# cat $DBG_DIR/port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66)  * # Check local device functionality
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  * root@self# ls $DBG_DIR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68)  * db            msg1          msg_sts     peer4/        port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * db_event      msg2          peer0/      peer5/        spad0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * db_mask       msg3          peer1/      peer_db       spad1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * link          msg_event     peer2/      peer_db_mask  spad2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  * msg0          msg_mask      peer3/      peer_spad     spad3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  * # As one can see it supports:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  * # 1) four inbound message registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  * # 2) four inbound scratchpads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  * # 3) up to six peer devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  * # Check peer device port number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  * root@self# cat $DBG_DIR/peer0/port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)  * # Check peer device(s) functionality to be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  * root@self# ls $DBG_DIR/peer0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  * link             mw_trans0       mw_trans6        port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  * link_event       mw_trans1       mw_trans7        spad0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  * msg0             mw_trans2       peer_mw_trans0   spad1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  * msg1             mw_trans3       peer_mw_trans1   spad2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  * msg2             mw_trans4       peer_mw_trans2   spad3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  * msg3             mw_trans5       peer_mw_trans3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  * # As one can see we got:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  * # 1) four outbound message registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * # 2) four outbound scratchpads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  * # 3) eight inbound memory windows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  * # 4) four outbound memory windows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  *-----------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95)  * Eg: NTB link tests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97)  * # Set local link up/down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98)  * root@self# echo Y > $DBG_DIR/link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  * root@self# echo N > $DBG_DIR/link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  * # Check if link with peer device is up/down:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  * root@self# cat $DBG_DIR/peer0/link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104)  * # Block until the link is up/down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105)  * root@self# echo Y > $DBG_DIR/peer0/link_event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  * root@self# echo N > $DBG_DIR/peer0/link_event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107)  *-----------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108)  * Eg: Doorbell registers tests (some functionality might be absent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110)  * # Set/clear/get local doorbell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111)  * root@self# echo 's 1' > $DBG_DIR/db
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112)  * root@self# echo 'c 1' > $DBG_DIR/db
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113)  * root@self# cat  $DBG_DIR/db
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  * # Set/clear/get local doorbell mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116)  * root@self# echo 's 1' > $DBG_DIR/db_mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117)  * root@self# echo 'c 1' > $DBG_DIR/db_mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  * root@self# cat $DBG_DIR/db_mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120)  * # Ring/clear/get peer doorbell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121)  * root@peer# echo 's 1' > $DBG_DIR/peer_db
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122)  * root@peer# echo 'c 1' > $DBG_DIR/peer_db
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123)  * root@peer# cat $DBG_DIR/peer_db
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125)  * # Set/clear/get peer doorbell mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126)  * root@self# echo 's 1' > $DBG_DIR/peer_db_mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127)  * root@self# echo 'c 1' > $DBG_DIR/peer_db_mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128)  * root@self# cat $DBG_DIR/peer_db_mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130)  * # Block until local doorbell is set with specified value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131)  * root@self# echo 1 > $DBG_DIR/db_event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132)  *-----------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133)  * Eg: Message registers tests (functionality might be absent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135)  * # Set/clear/get in/out message registers status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136)  * root@self# echo 's 1' > $DBG_DIR/msg_sts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137)  * root@self# echo 'c 1' > $DBG_DIR/msg_sts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138)  * root@self# cat $DBG_DIR/msg_sts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140)  * # Set/clear in/out message registers mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141)  * root@self# echo 's 1' > $DBG_DIR/msg_mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142)  * root@self# echo 'c 1' > $DBG_DIR/msg_mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144)  * # Get inbound message register #0 value and source of port index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145)  * root@self# cat  $DBG_DIR/msg0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147)  * # Send some data to peer over outbound message register #0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148)  * root@self# echo 0x01020304 > $DBG_DIR/peer0/msg0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149)  *-----------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150)  * Eg: Scratchpad registers tests (functionality might be absent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152)  * # Write/read to/from local scratchpad register #0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153)  * root@peer# echo 0x01020304 > $DBG_DIR/spad0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154)  * root@peer# cat $DBG_DIR/spad0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156)  * # Write/read to/from peer scratchpad register #0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157)  * root@peer# echo 0x01020304 > $DBG_DIR/peer0/spad0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158)  * root@peer# cat $DBG_DIR/peer0/spad0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159)  *-----------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160)  * Eg: Memory windows tests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162)  * # Create inbound memory window buffer of specified size/get its base address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163)  * root@peer# echo 16384 > $DBG_DIR/peer0/mw_trans0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164)  * root@peer# cat $DBG_DIR/peer0/mw_trans0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  * # Write/read data to/from inbound memory window
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  * root@peer# echo Hello > $DBG_DIR/peer0/mw0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168)  * root@peer# head -c 7 $DBG_DIR/peer0/mw0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170)  * # Map outbound memory window/check it settings (on peer device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171)  * root@peer# echo 0xADD0BA5E:16384 > $DBG_DIR/peer0/peer_mw_trans0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172)  * root@peer# cat $DBG_DIR/peer0/peer_mw_trans0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174)  * # Write/read data to/from outbound memory window (on peer device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175)  * root@peer# echo olleH > $DBG_DIR/peer0/peer_mw0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176)  * root@peer# head -c 7 $DBG_DIR/peer0/peer_mw0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) #include <linux/ntb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) #define DRIVER_NAME		"ntb_tool"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) #define DRIVER_VERSION		"2.0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) MODULE_LICENSE("Dual BSD/GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) MODULE_VERSION(DRIVER_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) MODULE_AUTHOR("Allen Hubbe <Allen.Hubbe@emc.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) MODULE_DESCRIPTION("PCIe NTB Debugging Tool");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200)  * Inbound and outbound memory windows descriptor. Union members selection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201)  * depends on the MW type the structure describes. mm_base/dma_base are the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202)  * virtual and DMA address of an inbound MW. io_base/tr_base are the MMIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203)  * mapped virtual and xlat addresses of an outbound MW respectively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) struct tool_mw {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	int widx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	int pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	struct tool_ctx *tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		u8 *mm_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		u8 __iomem *io_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		dma_addr_t dma_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		u64 tr_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	resource_size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	struct dentry *dbgfs_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222)  * Wrapper structure is used to distinguish the outbound MW peers reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223)  * within the corresponding DebugFS directory IO operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) struct tool_mw_wrap {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	int pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	struct tool_mw *mw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) struct tool_msg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	int midx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	int pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	struct tool_ctx *tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) struct tool_spad {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	int sidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	int pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	struct tool_ctx *tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) struct tool_peer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	int pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	struct tool_ctx *tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	int inmw_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	struct tool_mw *inmws;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	int outmw_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	struct tool_mw_wrap *outmws;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	int outmsg_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	struct tool_msg *outmsgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	int outspad_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	struct tool_spad *outspads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	struct dentry *dbgfs_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) struct tool_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	struct ntb_dev *ntb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	wait_queue_head_t link_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	wait_queue_head_t db_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	wait_queue_head_t msg_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	int outmw_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	struct tool_mw *outmws;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	int peer_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	struct tool_peer *peers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	int inmsg_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	struct tool_msg *inmsgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	int inspad_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	struct tool_spad *inspads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	struct dentry *dbgfs_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) #define TOOL_FOPS_RDWR(__name, __read, __write) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	const struct file_operations __name = {	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		.owner = THIS_MODULE,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		.open = simple_open,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		.read = __read,			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		.write = __write,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) #define TOOL_BUF_LEN 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) static struct dentry *tool_dbgfs_topdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) /*==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285)  *                               NTB events handlers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286)  *==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) static void tool_link_event(void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	struct tool_ctx *tc = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	enum ntb_speed speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	enum ntb_width width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	int up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	up = ntb_link_is_up(tc->ntb, &speed, &width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	dev_dbg(&tc->ntb->dev, "link is %s speed %d width %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		up ? "up" : "down", speed, width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	wake_up(&tc->link_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) static void tool_db_event(void *ctx, int vec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	struct tool_ctx *tc = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	u64 db_bits, db_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	db_mask = ntb_db_vector_mask(tc->ntb, vec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	db_bits = ntb_db_read(tc->ntb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	dev_dbg(&tc->ntb->dev, "doorbell vec %d mask %#llx bits %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		vec, db_mask, db_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	wake_up(&tc->db_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) static void tool_msg_event(void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	struct tool_ctx *tc = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	u64 msg_sts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	msg_sts = ntb_msg_read_sts(tc->ntb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	dev_dbg(&tc->ntb->dev, "message bits %#llx\n", msg_sts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	wake_up(&tc->msg_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) static const struct ntb_ctx_ops tool_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	.link_event = tool_link_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	.db_event = tool_db_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	.msg_event = tool_msg_event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) /*==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337)  *                        Common read/write methods
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338)  *==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) static ssize_t tool_fn_read(struct tool_ctx *tc, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 			    size_t size, loff_t *offp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 			    u64 (*fn_read)(struct ntb_dev *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	size_t buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	char buf[TOOL_BUF_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	ssize_t pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	if (!fn_read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	buf_size = min(size, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	pos = scnprintf(buf, buf_size, "%#llx\n", fn_read(tc->ntb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	return simple_read_from_buffer(ubuf, size, offp, buf, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) static ssize_t tool_fn_write(struct tool_ctx *tc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 			     const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 			     size_t size, loff_t *offp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 			     int (*fn_set)(struct ntb_dev *, u64),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 			     int (*fn_clear)(struct ntb_dev *, u64))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	char *buf, cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	u64 bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	buf = kmalloc(size + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	ret = simple_write_to_buffer(buf, size, offp, ubuf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	buf[size] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	n = sscanf(buf, "%c %lli", &cmd, &bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	if (n != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	} else if (cmd == 's') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		if (!fn_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 			ret = fn_set(tc->ntb, bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	} else if (cmd == 'c') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		if (!fn_clear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 			ret = fn_clear(tc->ntb, bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	return ret ? : size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) /*==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406)  *                            Port read/write methods
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407)  *==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) static ssize_t tool_port_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 			      size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	char buf[TOOL_BUF_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	int pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	pos = scnprintf(buf, sizeof(buf), "%d\n", ntb_port_number(tc->ntb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	return simple_read_from_buffer(ubuf, size, offp, buf, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) static TOOL_FOPS_RDWR(tool_port_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		      tool_port_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		      NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) static ssize_t tool_peer_port_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 				   size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	struct tool_peer *peer = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	struct tool_ctx *tc = peer->tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	char buf[TOOL_BUF_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	int pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	pos = scnprintf(buf, sizeof(buf), "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		ntb_peer_port_number(tc->ntb, peer->pidx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	return simple_read_from_buffer(ubuf, size, offp, buf, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) static TOOL_FOPS_RDWR(tool_peer_port_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		      tool_peer_port_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		      NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) static int tool_init_peers(struct tool_ctx *tc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	int pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	tc->peer_cnt = ntb_peer_port_count(tc->ntb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	tc->peers = devm_kcalloc(&tc->ntb->dev, tc->peer_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 				 sizeof(*tc->peers), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	if (tc->peers == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		tc->peers[pidx].pidx = pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		tc->peers[pidx].tc = tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) /*==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463)  *                       Link state read/write methods
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464)  *==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) static ssize_t tool_link_write(struct file *filep, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 			       size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	bool val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	ret = kstrtobool_from_user(ubuf, size, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		ret = ntb_link_enable(tc->ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		ret = ntb_link_disable(tc->ntb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) static TOOL_FOPS_RDWR(tool_link_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 		      NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		      tool_link_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) static ssize_t tool_peer_link_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 				   size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	struct tool_peer *peer = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	struct tool_ctx *tc = peer->tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	char buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	if (ntb_link_is_up(tc->ntb, NULL, NULL) & BIT(peer->pidx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		buf[0] = 'Y';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		buf[0] = 'N';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	buf[1] = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	buf[2] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	return simple_read_from_buffer(ubuf, size, offp, buf, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) static TOOL_FOPS_RDWR(tool_peer_link_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		      tool_peer_link_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		      NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) static ssize_t tool_peer_link_event_write(struct file *filep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 					  const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 					  size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	struct tool_peer *peer = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	struct tool_ctx *tc = peer->tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	u64 link_msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	bool val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	ret = kstrtobool_from_user(ubuf, size, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	link_msk = BIT_ULL_MASK(peer->pidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	if (wait_event_interruptible(tc->link_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		!!(ntb_link_is_up(tc->ntb, NULL, NULL) & link_msk) == val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		return -ERESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) static TOOL_FOPS_RDWR(tool_peer_link_event_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		      NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		      tool_peer_link_event_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) /*==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542)  *                  Memory windows read/write/setting methods
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543)  *==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) static ssize_t tool_mw_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 			    size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	struct tool_mw *inmw = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	if (inmw->mm_base == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	return simple_read_from_buffer(ubuf, size, offp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 				       inmw->mm_base, inmw->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) static ssize_t tool_mw_write(struct file *filep, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 			     size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	struct tool_mw *inmw = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	if (inmw->mm_base == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	return simple_write_to_buffer(inmw->mm_base, inmw->size, offp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 				      ubuf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) static TOOL_FOPS_RDWR(tool_mw_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		      tool_mw_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		      tool_mw_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) static int tool_setup_mw(struct tool_ctx *tc, int pidx, int widx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 			 size_t req_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	resource_size_t size, addr_align, size_align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	struct tool_mw *inmw = &tc->peers[pidx].inmws[widx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	char buf[TOOL_BUF_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	if (inmw->mm_base != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	ret = ntb_mw_get_align(tc->ntb, pidx, widx, &addr_align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 				&size_align, &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	inmw->size = min_t(resource_size_t, req_size, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	inmw->size = round_up(inmw->size, addr_align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	inmw->size = round_up(inmw->size, size_align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	inmw->mm_base = dma_alloc_coherent(&tc->ntb->pdev->dev, inmw->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 					   &inmw->dma_base, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	if (!inmw->mm_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	if (!IS_ALIGNED(inmw->dma_base, addr_align)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		goto err_free_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	ret = ntb_mw_set_trans(tc->ntb, pidx, widx, inmw->dma_base, inmw->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		goto err_free_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	snprintf(buf, sizeof(buf), "mw%d", widx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	inmw->dbgfs_file = debugfs_create_file(buf, 0600,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 					       tc->peers[pidx].dbgfs_dir, inmw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 					       &tool_mw_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) err_free_dma:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	dma_free_coherent(&tc->ntb->pdev->dev, inmw->size, inmw->mm_base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 			  inmw->dma_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	inmw->mm_base = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	inmw->dma_base = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	inmw->size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) static void tool_free_mw(struct tool_ctx *tc, int pidx, int widx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	struct tool_mw *inmw = &tc->peers[pidx].inmws[widx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	debugfs_remove(inmw->dbgfs_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	if (inmw->mm_base != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		ntb_mw_clear_trans(tc->ntb, pidx, widx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		dma_free_coherent(&tc->ntb->pdev->dev, inmw->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 				  inmw->mm_base, inmw->dma_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	inmw->mm_base = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	inmw->dma_base = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	inmw->size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	inmw->dbgfs_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) static ssize_t tool_mw_trans_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 				  size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	struct tool_mw *inmw = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	resource_size_t addr_align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	resource_size_t size_align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	resource_size_t size_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	ssize_t ret, off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	size_t buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	buf_size = min_t(size_t, size, 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	buf = kmalloc(buf_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	ret = ntb_mw_get_align(inmw->tc->ntb, inmw->pidx, inmw->widx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 			       &addr_align, &size_align, &size_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 			 "Inbound MW     \t%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 			 inmw->widx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 			 "Port           \t%d (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 			 ntb_peer_port_number(inmw->tc->ntb, inmw->pidx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 			 inmw->pidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 			 "Window Address \t0x%pK\n", inmw->mm_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 			 "DMA Address    \t%pad\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 			 &inmw->dma_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 			 "Window Size    \t%pap\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 			 &inmw->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 			 "Alignment      \t%pap\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 			 &addr_align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 			 "Size Alignment \t%pap\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 			 &size_align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 			 "Size Max       \t%pap\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 			 &size_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	ret = simple_read_from_buffer(ubuf, size, offp, buf, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) static ssize_t tool_mw_trans_write(struct file *filep, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 				   size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	struct tool_mw *inmw = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	ret = kstrtouint_from_user(ubuf, size, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	tool_free_mw(inmw->tc, inmw->pidx, inmw->widx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	if (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		ret = tool_setup_mw(inmw->tc, inmw->pidx, inmw->widx, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) static TOOL_FOPS_RDWR(tool_mw_trans_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		      tool_mw_trans_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		      tool_mw_trans_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) static ssize_t tool_peer_mw_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 				 size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	struct tool_mw *outmw = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	loff_t pos = *offp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	if (outmw->io_base == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	if (pos >= outmw->size || !size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	if (size > outmw->size - pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		size = outmw->size - pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	buf = kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	memcpy_fromio(buf, outmw->io_base + pos, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	ret = copy_to_user(ubuf, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	if (ret == size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	size -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	*offp = pos + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	ret = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) err_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) static ssize_t tool_peer_mw_write(struct file *filep, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 				  size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	struct tool_mw *outmw = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	loff_t pos = *offp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	if (outmw->io_base == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	if (pos >= outmw->size || !size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	if (size > outmw->size - pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		size = outmw->size - pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	buf = kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	ret = copy_from_user(buf, ubuf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	if (ret == size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	size -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	*offp = pos + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	ret = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	memcpy_toio(outmw->io_base + pos, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) err_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) static TOOL_FOPS_RDWR(tool_peer_mw_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		      tool_peer_mw_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		      tool_peer_mw_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) static int tool_setup_peer_mw(struct tool_ctx *tc, int pidx, int widx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 			      u64 req_addr, size_t req_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	struct tool_mw *outmw = &tc->outmws[widx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	resource_size_t map_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	phys_addr_t map_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	char buf[TOOL_BUF_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	if (outmw->io_base != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	ret = ntb_peer_mw_get_addr(tc->ntb, widx, &map_base, &map_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	ret = ntb_peer_mw_set_trans(tc->ntb, pidx, widx, req_addr, req_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	outmw->io_base = ioremap_wc(map_base, map_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	if (outmw->io_base == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		goto err_clear_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	outmw->tr_base = req_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	outmw->size = req_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	outmw->pidx = pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	snprintf(buf, sizeof(buf), "peer_mw%d", widx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	outmw->dbgfs_file = debugfs_create_file(buf, 0600,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 					       tc->peers[pidx].dbgfs_dir, outmw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 					       &tool_peer_mw_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) err_clear_trans:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	ntb_peer_mw_clear_trans(tc->ntb, pidx, widx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) static void tool_free_peer_mw(struct tool_ctx *tc, int widx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	struct tool_mw *outmw = &tc->outmws[widx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	debugfs_remove(outmw->dbgfs_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	if (outmw->io_base != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		iounmap(tc->outmws[widx].io_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		ntb_peer_mw_clear_trans(tc->ntb, outmw->pidx, widx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	outmw->io_base = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	outmw->tr_base = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	outmw->size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	outmw->pidx = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	outmw->dbgfs_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) static ssize_t tool_peer_mw_trans_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 					size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	struct tool_mw_wrap *outmw_wrap = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	struct tool_mw *outmw = outmw_wrap->mw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	resource_size_t map_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	phys_addr_t map_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	ssize_t off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	size_t buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	ret = ntb_peer_mw_get_addr(outmw->tc->ntb, outmw->widx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 				  &map_base, &map_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	buf_size = min_t(size_t, size, 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	buf = kmalloc(buf_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 			 "Outbound MW:        \t%d\n", outmw->widx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	if (outmw->io_base != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 			"Port attached       \t%d (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 			ntb_peer_port_number(outmw->tc->ntb, outmw->pidx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 			outmw->pidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 				 "Port attached       \t-1 (-1)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 			 "Virtual address     \t0x%pK\n", outmw->io_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 			 "Phys Address        \t%pap\n", &map_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 			 "Mapping Size        \t%pap\n", &map_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 			 "Translation Address \t0x%016llx\n", outmw->tr_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	off += scnprintf(buf + off, buf_size - off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			 "Window Size         \t%pap\n", &outmw->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	ret = simple_read_from_buffer(ubuf, size, offp, buf, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) static ssize_t tool_peer_mw_trans_write(struct file *filep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 					const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 					size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	struct tool_mw_wrap *outmw_wrap = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	struct tool_mw *outmw = outmw_wrap->mw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	size_t buf_size, wsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	char buf[TOOL_BUF_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	int ret, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	u64 addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	buf_size = min(size, (sizeof(buf) - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	if (copy_from_user(buf, ubuf, buf_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	buf[buf_size] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	n = sscanf(buf, "%lli:%zi", &addr, &wsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	if (n != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	tool_free_peer_mw(outmw->tc, outmw->widx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	if (wsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		ret = tool_setup_peer_mw(outmw->tc, outmw_wrap->pidx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 					 outmw->widx, addr, wsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) static TOOL_FOPS_RDWR(tool_peer_mw_trans_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		      tool_peer_mw_trans_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		      tool_peer_mw_trans_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) static int tool_init_mws(struct tool_ctx *tc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	int widx, pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	/* Initialize outbound memory windows */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	tc->outmw_cnt = ntb_peer_mw_count(tc->ntb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	tc->outmws = devm_kcalloc(&tc->ntb->dev, tc->outmw_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 				  sizeof(*tc->outmws), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	if (tc->outmws == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	for (widx = 0; widx < tc->outmw_cnt; widx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		tc->outmws[widx].widx = widx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		tc->outmws[widx].pidx = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		tc->outmws[widx].tc = tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	/* Initialize inbound memory windows and outbound MWs wrapper */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		tc->peers[pidx].inmw_cnt = ntb_mw_count(tc->ntb, pidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		tc->peers[pidx].inmws =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].inmw_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 				    sizeof(*tc->peers[pidx].inmws), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		if (tc->peers[pidx].inmws == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 			tc->peers[pidx].inmws[widx].widx = widx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 			tc->peers[pidx].inmws[widx].pidx = pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 			tc->peers[pidx].inmws[widx].tc = tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		tc->peers[pidx].outmw_cnt = ntb_peer_mw_count(tc->ntb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		tc->peers[pidx].outmws =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 			devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].outmw_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 				   sizeof(*tc->peers[pidx].outmws), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		for (widx = 0; widx < tc->peers[pidx].outmw_cnt; widx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 			tc->peers[pidx].outmws[widx].pidx = pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 			tc->peers[pidx].outmws[widx].mw = &tc->outmws[widx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) static void tool_clear_mws(struct tool_ctx *tc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	int widx, pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	/* Free outbound memory windows */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	for (widx = 0; widx < tc->outmw_cnt; widx++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		tool_free_peer_mw(tc, widx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	/* Free outbound memory windows */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	for (pidx = 0; pidx < tc->peer_cnt; pidx++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 			tool_free_mw(tc, pidx, widx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) /*==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)  *                       Doorbell read/write methods
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)  *==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) static ssize_t tool_db_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 			    size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->db_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) static ssize_t tool_db_write(struct file *filep, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 			     size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	return tool_fn_write(tc, ubuf, size, offp, tc->ntb->ops->db_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 			     tc->ntb->ops->db_clear);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) static TOOL_FOPS_RDWR(tool_db_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		      tool_db_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		      tool_db_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) static ssize_t tool_db_valid_mask_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 				       size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->db_valid_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) static TOOL_FOPS_RDWR(tool_db_valid_mask_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		      tool_db_valid_mask_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		      NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) static ssize_t tool_db_mask_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 				 size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->db_read_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) static ssize_t tool_db_mask_write(struct file *filep, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 			       size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	return tool_fn_write(tc, ubuf, size, offp, tc->ntb->ops->db_set_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 			     tc->ntb->ops->db_clear_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) static TOOL_FOPS_RDWR(tool_db_mask_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		      tool_db_mask_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		      tool_db_mask_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) static ssize_t tool_peer_db_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 				 size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->peer_db_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) static ssize_t tool_peer_db_write(struct file *filep, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 				  size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	return tool_fn_write(tc, ubuf, size, offp, tc->ntb->ops->peer_db_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 			     tc->ntb->ops->peer_db_clear);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) static TOOL_FOPS_RDWR(tool_peer_db_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		      tool_peer_db_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		      tool_peer_db_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) static ssize_t tool_peer_db_mask_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 				   size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	return tool_fn_read(tc, ubuf, size, offp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 			    tc->ntb->ops->peer_db_read_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) static ssize_t tool_peer_db_mask_write(struct file *filep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 				       const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 				       size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	return tool_fn_write(tc, ubuf, size, offp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 			     tc->ntb->ops->peer_db_set_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 			     tc->ntb->ops->peer_db_clear_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) static TOOL_FOPS_RDWR(tool_peer_db_mask_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		      tool_peer_db_mask_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		      tool_peer_db_mask_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) static ssize_t tool_db_event_write(struct file *filep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 				   const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 				   size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	ret = kstrtou64_from_user(ubuf, size, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	if (wait_event_interruptible(tc->db_wq, ntb_db_read(tc->ntb) == val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		return -ERESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) static TOOL_FOPS_RDWR(tool_db_event_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		      NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		      tool_db_event_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) /*==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150)  *                       Scratchpads read/write methods
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151)  *==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) static ssize_t tool_spad_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 			      size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	struct tool_spad *spad = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	char buf[TOOL_BUF_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	ssize_t pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	if (!spad->tc->ntb->ops->spad_read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	pos = scnprintf(buf, sizeof(buf), "%#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		ntb_spad_read(spad->tc->ntb, spad->sidx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	return simple_read_from_buffer(ubuf, size, offp, buf, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) static ssize_t tool_spad_write(struct file *filep, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 			       size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	struct tool_spad *spad = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	if (!spad->tc->ntb->ops->spad_write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		dev_dbg(&spad->tc->ntb->dev, "no spad write fn\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	ret = kstrtou32_from_user(ubuf, size, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	ret = ntb_spad_write(spad->tc->ntb, spad->sidx, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	return ret ?: size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) static TOOL_FOPS_RDWR(tool_spad_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		      tool_spad_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		      tool_spad_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) static ssize_t tool_peer_spad_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 				   size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	struct tool_spad *spad = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	char buf[TOOL_BUF_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	ssize_t pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	if (!spad->tc->ntb->ops->peer_spad_read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	pos = scnprintf(buf, sizeof(buf), "%#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		ntb_peer_spad_read(spad->tc->ntb, spad->pidx, spad->sidx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	return simple_read_from_buffer(ubuf, size, offp, buf, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) static ssize_t tool_peer_spad_write(struct file *filep, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 				    size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	struct tool_spad *spad = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	if (!spad->tc->ntb->ops->peer_spad_write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		dev_dbg(&spad->tc->ntb->dev, "no spad write fn\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	ret = kstrtou32_from_user(ubuf, size, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	ret = ntb_peer_spad_write(spad->tc->ntb, spad->pidx, spad->sidx, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	return ret ?: size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) static TOOL_FOPS_RDWR(tool_peer_spad_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		      tool_peer_spad_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		      tool_peer_spad_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) static int tool_init_spads(struct tool_ctx *tc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	int sidx, pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	/* Initialize inbound scratchpad structures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	tc->inspad_cnt = ntb_spad_count(tc->ntb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	tc->inspads = devm_kcalloc(&tc->ntb->dev, tc->inspad_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 				   sizeof(*tc->inspads), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	if (tc->inspads == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	for (sidx = 0; sidx < tc->inspad_cnt; sidx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		tc->inspads[sidx].sidx = sidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		tc->inspads[sidx].pidx = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 		tc->inspads[sidx].tc = tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	/* Initialize outbound scratchpad structures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		tc->peers[pidx].outspad_cnt = ntb_spad_count(tc->ntb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 		tc->peers[pidx].outspads =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 			devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].outspad_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 				sizeof(*tc->peers[pidx].outspads), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		if (tc->peers[pidx].outspads == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		for (sidx = 0; sidx < tc->peers[pidx].outspad_cnt; sidx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			tc->peers[pidx].outspads[sidx].sidx = sidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 			tc->peers[pidx].outspads[sidx].pidx = pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 			tc->peers[pidx].outspads[sidx].tc = tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) /*==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)  *                       Messages read/write methods
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)  *==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) static ssize_t tool_inmsg_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 			       size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	struct tool_msg *msg = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	char buf[TOOL_BUF_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	ssize_t pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	u32 data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	int pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	data = ntb_msg_read(msg->tc->ntb, &pidx, msg->midx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	pos = scnprintf(buf, sizeof(buf), "0x%08x<-%d\n", data, pidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	return simple_read_from_buffer(ubuf, size, offp, buf, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) static TOOL_FOPS_RDWR(tool_inmsg_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		      tool_inmsg_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		      NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) static ssize_t tool_outmsg_write(struct file *filep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 				 const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 				 size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	struct tool_msg *msg = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	ret = kstrtou32_from_user(ubuf, size, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	ret = ntb_peer_msg_write(msg->tc->ntb, msg->pidx, msg->midx, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	return ret ? : size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) static TOOL_FOPS_RDWR(tool_outmsg_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 		      NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		      tool_outmsg_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) static ssize_t tool_msg_sts_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 				 size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->msg_read_sts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) static ssize_t tool_msg_sts_write(struct file *filep, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 				  size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	return tool_fn_write(tc, ubuf, size, offp, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 			     tc->ntb->ops->msg_clear_sts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) static TOOL_FOPS_RDWR(tool_msg_sts_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		      tool_msg_sts_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		      tool_msg_sts_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) static ssize_t tool_msg_inbits_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 				    size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->msg_inbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) static TOOL_FOPS_RDWR(tool_msg_inbits_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		      tool_msg_inbits_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		      NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) static ssize_t tool_msg_outbits_read(struct file *filep, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 				     size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->msg_outbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) static TOOL_FOPS_RDWR(tool_msg_outbits_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		      tool_msg_outbits_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 		      NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) static ssize_t tool_msg_mask_write(struct file *filep, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 				   size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	return tool_fn_write(tc, ubuf, size, offp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			     tc->ntb->ops->msg_set_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 			     tc->ntb->ops->msg_clear_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) static TOOL_FOPS_RDWR(tool_msg_mask_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		      NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		      tool_msg_mask_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) static ssize_t tool_msg_event_write(struct file *filep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 				    const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 				    size_t size, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	struct tool_ctx *tc = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	ret = kstrtou64_from_user(ubuf, size, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	if (wait_event_interruptible(tc->msg_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		ntb_msg_read_sts(tc->ntb) == val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		return -ERESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) static TOOL_FOPS_RDWR(tool_msg_event_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		      NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		      tool_msg_event_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) static int tool_init_msgs(struct tool_ctx *tc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	int midx, pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	/* Initialize inbound message structures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	tc->inmsg_cnt = ntb_msg_count(tc->ntb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	tc->inmsgs = devm_kcalloc(&tc->ntb->dev, tc->inmsg_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 				   sizeof(*tc->inmsgs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	if (tc->inmsgs == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	for (midx = 0; midx < tc->inmsg_cnt; midx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 		tc->inmsgs[midx].midx = midx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 		tc->inmsgs[midx].pidx = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		tc->inmsgs[midx].tc = tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	/* Initialize outbound message structures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		tc->peers[pidx].outmsg_cnt = ntb_msg_count(tc->ntb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 		tc->peers[pidx].outmsgs =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 			devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].outmsg_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 				sizeof(*tc->peers[pidx].outmsgs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		if (tc->peers[pidx].outmsgs == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		for (midx = 0; midx < tc->peers[pidx].outmsg_cnt; midx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 			tc->peers[pidx].outmsgs[midx].midx = midx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 			tc->peers[pidx].outmsgs[midx].pidx = pidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 			tc->peers[pidx].outmsgs[midx].tc = tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) /*==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)  *                          Initialization methods
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438)  *==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) static struct tool_ctx *tool_create_data(struct ntb_dev *ntb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	struct tool_ctx *tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	tc = devm_kzalloc(&ntb->dev, sizeof(*tc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	if (tc == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	tc->ntb = ntb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	init_waitqueue_head(&tc->link_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	init_waitqueue_head(&tc->db_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	init_waitqueue_head(&tc->msg_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	if (ntb_db_is_unsafe(ntb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 		dev_dbg(&ntb->dev, "doorbell is unsafe\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	if (ntb_spad_is_unsafe(ntb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		dev_dbg(&ntb->dev, "scratchpad is unsafe\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	return tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) static void tool_clear_data(struct tool_ctx *tc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	wake_up(&tc->link_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	wake_up(&tc->db_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	wake_up(&tc->msg_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) static int tool_init_ntb(struct tool_ctx *tc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	return ntb_set_ctx(tc->ntb, tc, &tool_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) static void tool_clear_ntb(struct tool_ctx *tc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	ntb_clear_ctx(tc->ntb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	ntb_link_disable(tc->ntb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) static void tool_setup_dbgfs(struct tool_ctx *tc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	int pidx, widx, sidx, midx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	char buf[TOOL_BUF_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	/* This modules is useless without dbgfs... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	if (!tool_dbgfs_topdir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		tc->dbgfs_dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	tc->dbgfs_dir = debugfs_create_dir(dev_name(&tc->ntb->dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 					   tool_dbgfs_topdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	if (!tc->dbgfs_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	debugfs_create_file("port", 0600, tc->dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 			    tc, &tool_port_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	debugfs_create_file("link", 0600, tc->dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 			    tc, &tool_link_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	debugfs_create_file("db", 0600, tc->dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 			    tc, &tool_db_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	debugfs_create_file("db_valid_mask", 0600, tc->dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 			    tc, &tool_db_valid_mask_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	debugfs_create_file("db_mask", 0600, tc->dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 			    tc, &tool_db_mask_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	debugfs_create_file("db_event", 0600, tc->dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 			    tc, &tool_db_event_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	debugfs_create_file("peer_db", 0600, tc->dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 			    tc, &tool_peer_db_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	debugfs_create_file("peer_db_mask", 0600, tc->dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 			    tc, &tool_peer_db_mask_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	if (tc->inspad_cnt != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		for (sidx = 0; sidx < tc->inspad_cnt; sidx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 			snprintf(buf, sizeof(buf), "spad%d", sidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 			debugfs_create_file(buf, 0600, tc->dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 					   &tc->inspads[sidx], &tool_spad_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	if (tc->inmsg_cnt != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		for (midx = 0; midx < tc->inmsg_cnt; midx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 			snprintf(buf, sizeof(buf), "msg%d", midx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 			debugfs_create_file(buf, 0600, tc->dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 					   &tc->inmsgs[midx], &tool_inmsg_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 		debugfs_create_file("msg_sts", 0600, tc->dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 				    tc, &tool_msg_sts_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		debugfs_create_file("msg_inbits", 0600, tc->dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 				    tc, &tool_msg_inbits_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 		debugfs_create_file("msg_outbits", 0600, tc->dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 				    tc, &tool_msg_outbits_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		debugfs_create_file("msg_mask", 0600, tc->dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 				    tc, &tool_msg_mask_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		debugfs_create_file("msg_event", 0600, tc->dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 				    tc, &tool_msg_event_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		snprintf(buf, sizeof(buf), "peer%d", pidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		tc->peers[pidx].dbgfs_dir =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 			debugfs_create_dir(buf, tc->dbgfs_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 		debugfs_create_file("port", 0600,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 				    tc->peers[pidx].dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 				    &tc->peers[pidx], &tool_peer_port_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		debugfs_create_file("link", 0200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 				    tc->peers[pidx].dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 				    &tc->peers[pidx], &tool_peer_link_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		debugfs_create_file("link_event", 0200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 				  tc->peers[pidx].dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 				  &tc->peers[pidx], &tool_peer_link_event_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 			snprintf(buf, sizeof(buf), "mw_trans%d", widx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 			debugfs_create_file(buf, 0600,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 					    tc->peers[pidx].dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 					    &tc->peers[pidx].inmws[widx],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 					    &tool_mw_trans_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 		for (widx = 0; widx < tc->peers[pidx].outmw_cnt; widx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 			snprintf(buf, sizeof(buf), "peer_mw_trans%d", widx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 			debugfs_create_file(buf, 0600,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 					    tc->peers[pidx].dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 					    &tc->peers[pidx].outmws[widx],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 					    &tool_peer_mw_trans_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 		for (sidx = 0; sidx < tc->peers[pidx].outspad_cnt; sidx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 			snprintf(buf, sizeof(buf), "spad%d", sidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 			debugfs_create_file(buf, 0600,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 					    tc->peers[pidx].dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 					    &tc->peers[pidx].outspads[sidx],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 					    &tool_peer_spad_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 		for (midx = 0; midx < tc->peers[pidx].outmsg_cnt; midx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 			snprintf(buf, sizeof(buf), "msg%d", midx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 			debugfs_create_file(buf, 0600,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 					    tc->peers[pidx].dbgfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 					    &tc->peers[pidx].outmsgs[midx],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 					    &tool_outmsg_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) static void tool_clear_dbgfs(struct tool_ctx *tc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	debugfs_remove_recursive(tc->dbgfs_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) static int tool_probe(struct ntb_client *self, struct ntb_dev *ntb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	struct tool_ctx *tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	tc = tool_create_data(ntb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	if (IS_ERR(tc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 		return PTR_ERR(tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	ret = tool_init_peers(tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		goto err_clear_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	ret = tool_init_mws(tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		goto err_clear_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	ret = tool_init_spads(tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		goto err_clear_mws;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	ret = tool_init_msgs(tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		goto err_clear_mws;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	ret = tool_init_ntb(tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 		goto err_clear_mws;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	tool_setup_dbgfs(tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) err_clear_mws:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	tool_clear_mws(tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) err_clear_data:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	tool_clear_data(tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) static void tool_remove(struct ntb_client *self, struct ntb_dev *ntb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	struct tool_ctx *tc = ntb->ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	tool_clear_dbgfs(tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	tool_clear_ntb(tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	tool_clear_mws(tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	tool_clear_data(tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) static struct ntb_client tool_client = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	.ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 		.probe = tool_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 		.remove = tool_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) static int __init tool_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	if (debugfs_initialized())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		tool_dbgfs_topdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	ret = ntb_register_client(&tool_client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 		debugfs_remove_recursive(tool_dbgfs_topdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) module_init(tool_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) static void __exit tool_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	ntb_unregister_client(&tool_client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	debugfs_remove_recursive(tool_dbgfs_topdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) module_exit(tool_exit);