18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Intel Wireless WiMAX Connection 2400m 38c2ecf20Sopenharmony_ci * Generic (non-bus specific) TX handling 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (C) 2007-2008 Intel Corporation. All rights reserved. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 98c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions 108c2ecf20Sopenharmony_ci * are met: 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * * Redistributions of source code must retain the above copyright 138c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 148c2ecf20Sopenharmony_ci * * Redistributions in binary form must reproduce the above copyright 158c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer in 168c2ecf20Sopenharmony_ci * the documentation and/or other materials provided with the 178c2ecf20Sopenharmony_ci * distribution. 188c2ecf20Sopenharmony_ci * * Neither the name of Intel Corporation nor the names of its 198c2ecf20Sopenharmony_ci * contributors may be used to endorse or promote products derived 208c2ecf20Sopenharmony_ci * from this software without specific prior written permission. 218c2ecf20Sopenharmony_ci * 228c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 238c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 248c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 258c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 268c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 278c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 288c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 298c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 308c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 318c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 328c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 338c2ecf20Sopenharmony_ci * 348c2ecf20Sopenharmony_ci * 358c2ecf20Sopenharmony_ci * Intel Corporation <linux-wimax@intel.com> 368c2ecf20Sopenharmony_ci * Yanir Lubetkin <yanirx.lubetkin@intel.com> 378c2ecf20Sopenharmony_ci * - Initial implementation 388c2ecf20Sopenharmony_ci * 398c2ecf20Sopenharmony_ci * Intel Corporation <linux-wimax@intel.com> 408c2ecf20Sopenharmony_ci * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> 418c2ecf20Sopenharmony_ci * - Rewritten to use a single FIFO to lower the memory allocation 428c2ecf20Sopenharmony_ci * pressure and optimize cache hits when copying to the queue, as 438c2ecf20Sopenharmony_ci * well as splitting out bus-specific code. 448c2ecf20Sopenharmony_ci * 458c2ecf20Sopenharmony_ci * 468c2ecf20Sopenharmony_ci * Implements data transmission to the device; this is done through a 478c2ecf20Sopenharmony_ci * software FIFO, as data/control frames can be coalesced (while the 488c2ecf20Sopenharmony_ci * device is reading the previous tx transaction, others accumulate). 498c2ecf20Sopenharmony_ci * 508c2ecf20Sopenharmony_ci * A FIFO is used because at the end it is resource-cheaper that trying 518c2ecf20Sopenharmony_ci * to implement scatter/gather over USB. As well, most traffic is going 528c2ecf20Sopenharmony_ci * to be download (vs upload). 538c2ecf20Sopenharmony_ci * 548c2ecf20Sopenharmony_ci * The format for sending/receiving data to/from the i2400m is 558c2ecf20Sopenharmony_ci * described in detail in rx.c:PROTOCOL FORMAT. In here we implement 568c2ecf20Sopenharmony_ci * the transmission of that. This is split between a bus-independent 578c2ecf20Sopenharmony_ci * part that just prepares everything and a bus-specific part that 588c2ecf20Sopenharmony_ci * does the actual transmission over the bus to the device (in the 598c2ecf20Sopenharmony_ci * bus-specific driver). 608c2ecf20Sopenharmony_ci * 618c2ecf20Sopenharmony_ci * 628c2ecf20Sopenharmony_ci * The general format of a device-host transaction is MSG-HDR, PLD1, 638c2ecf20Sopenharmony_ci * PLD2...PLDN, PL1, PL2,...PLN, PADDING. 648c2ecf20Sopenharmony_ci * 658c2ecf20Sopenharmony_ci * Because we need the send payload descriptors and then payloads and 668c2ecf20Sopenharmony_ci * because it is kind of expensive to do scatterlists in USB (one URB 678c2ecf20Sopenharmony_ci * per node), it becomes cheaper to append all the data to a FIFO 688c2ecf20Sopenharmony_ci * (copying to a FIFO potentially in cache is cheaper). 698c2ecf20Sopenharmony_ci * 708c2ecf20Sopenharmony_ci * Then the bus-specific code takes the parts of that FIFO that are 718c2ecf20Sopenharmony_ci * written and passes them to the device. 728c2ecf20Sopenharmony_ci * 738c2ecf20Sopenharmony_ci * So the concepts to keep in mind there are: 748c2ecf20Sopenharmony_ci * 758c2ecf20Sopenharmony_ci * We use a FIFO to queue the data in a linear buffer. We first append 768c2ecf20Sopenharmony_ci * a MSG-HDR, space for I2400M_TX_PLD_MAX payload descriptors and then 778c2ecf20Sopenharmony_ci * go appending payloads until we run out of space or of payload 788c2ecf20Sopenharmony_ci * descriptors. Then we append padding to make the whole transaction a 798c2ecf20Sopenharmony_ci * multiple of i2400m->bus_tx_block_size (as defined by the bus layer). 808c2ecf20Sopenharmony_ci * 818c2ecf20Sopenharmony_ci * - A TX message: a combination of a message header, payload 828c2ecf20Sopenharmony_ci * descriptors and payloads. 838c2ecf20Sopenharmony_ci * 848c2ecf20Sopenharmony_ci * Open: it is marked as active (i2400m->tx_msg is valid) and we 858c2ecf20Sopenharmony_ci * can keep adding payloads to it. 868c2ecf20Sopenharmony_ci * 878c2ecf20Sopenharmony_ci * Closed: we are not appending more payloads to this TX message 888c2ecf20Sopenharmony_ci * (exahusted space in the queue, too many payloads or 898c2ecf20Sopenharmony_ci * whichever). We have appended padding so the whole message 908c2ecf20Sopenharmony_ci * length is aligned to i2400m->bus_tx_block_size (as set by the 918c2ecf20Sopenharmony_ci * bus/transport layer). 928c2ecf20Sopenharmony_ci * 938c2ecf20Sopenharmony_ci * - Most of the time we keep a TX message open to which we append 948c2ecf20Sopenharmony_ci * payloads. 958c2ecf20Sopenharmony_ci * 968c2ecf20Sopenharmony_ci * - If we are going to append and there is no more space (we are at 978c2ecf20Sopenharmony_ci * the end of the FIFO), we close the message, mark the rest of the 988c2ecf20Sopenharmony_ci * FIFO space unusable (skip_tail), create a new message at the 998c2ecf20Sopenharmony_ci * beginning of the FIFO (if there is space) and append the message 1008c2ecf20Sopenharmony_ci * there. 1018c2ecf20Sopenharmony_ci * 1028c2ecf20Sopenharmony_ci * This is because we need to give linear TX messages to the bus 1038c2ecf20Sopenharmony_ci * engine. So we don't write a message to the remaining FIFO space 1048c2ecf20Sopenharmony_ci * until the tail and continue at the head of it. 1058c2ecf20Sopenharmony_ci * 1068c2ecf20Sopenharmony_ci * - We overload one of the fields in the message header to use it as 1078c2ecf20Sopenharmony_ci * 'size' of the TX message, so we can iterate over them. It also 1088c2ecf20Sopenharmony_ci * contains a flag that indicates if we have to skip it or not. 1098c2ecf20Sopenharmony_ci * When we send the buffer, we update that to its real on-the-wire 1108c2ecf20Sopenharmony_ci * value. 1118c2ecf20Sopenharmony_ci * 1128c2ecf20Sopenharmony_ci * - The MSG-HDR PLD1...PLD2 stuff has to be a size multiple of 16. 1138c2ecf20Sopenharmony_ci * 1148c2ecf20Sopenharmony_ci * It follows that if MSG-HDR says we have N messages, the whole 1158c2ecf20Sopenharmony_ci * header + descriptors is 16 + 4*N; for those to be a multiple of 1168c2ecf20Sopenharmony_ci * 16, it follows that N can be 4, 8, 12, ... (32, 48, 64, 80... 1178c2ecf20Sopenharmony_ci * bytes). 1188c2ecf20Sopenharmony_ci * 1198c2ecf20Sopenharmony_ci * So if we have only 1 payload, we have to submit a header that in 1208c2ecf20Sopenharmony_ci * all truth has space for 4. 1218c2ecf20Sopenharmony_ci * 1228c2ecf20Sopenharmony_ci * The implication is that we reserve space for 12 (64 bytes); but 1238c2ecf20Sopenharmony_ci * if we fill up only (eg) 2, our header becomes 32 bytes only. So 1248c2ecf20Sopenharmony_ci * the TX engine has to shift those 32 bytes of msg header and 2 1258c2ecf20Sopenharmony_ci * payloads and padding so that right after it the payloads start 1268c2ecf20Sopenharmony_ci * and the TX engine has to know about that. 1278c2ecf20Sopenharmony_ci * 1288c2ecf20Sopenharmony_ci * It is cheaper to move the header up than the whole payloads down. 1298c2ecf20Sopenharmony_ci * 1308c2ecf20Sopenharmony_ci * We do this in i2400m_tx_close(). See 'i2400m_msg_hdr->offset'. 1318c2ecf20Sopenharmony_ci * 1328c2ecf20Sopenharmony_ci * - Each payload has to be size-padded to 16 bytes; before appending 1338c2ecf20Sopenharmony_ci * it, we just do it. 1348c2ecf20Sopenharmony_ci * 1358c2ecf20Sopenharmony_ci * - The whole message has to be padded to i2400m->bus_tx_block_size; 1368c2ecf20Sopenharmony_ci * we do this at close time. Thus, when reserving space for the 1378c2ecf20Sopenharmony_ci * payload, we always make sure there is also free space for this 1388c2ecf20Sopenharmony_ci * padding that sooner or later will happen. 1398c2ecf20Sopenharmony_ci * 1408c2ecf20Sopenharmony_ci * When we append a message, we tell the bus specific code to kick in 1418c2ecf20Sopenharmony_ci * TXs. It will TX (in parallel) until the buffer is exhausted--hence 1428c2ecf20Sopenharmony_ci * the lockin we do. The TX code will only send a TX message at the 1438c2ecf20Sopenharmony_ci * time (which remember, might contain more than one payload). Of 1448c2ecf20Sopenharmony_ci * course, when the bus-specific driver attempts to TX a message that 1458c2ecf20Sopenharmony_ci * is still open, it gets closed first. 1468c2ecf20Sopenharmony_ci * 1478c2ecf20Sopenharmony_ci * Gee, this is messy; well a picture. In the example below we have a 1488c2ecf20Sopenharmony_ci * partially full FIFO, with a closed message ready to be delivered 1498c2ecf20Sopenharmony_ci * (with a moved message header to make sure it is size-aligned to 1508c2ecf20Sopenharmony_ci * 16), TAIL room that was unusable (and thus is marked with a message 1518c2ecf20Sopenharmony_ci * header that says 'skip this') and at the head of the buffer, an 1528c2ecf20Sopenharmony_ci * incomplete message with a couple of payloads. 1538c2ecf20Sopenharmony_ci * 1548c2ecf20Sopenharmony_ci * N ___________________________________________________ 1558c2ecf20Sopenharmony_ci * | | 1568c2ecf20Sopenharmony_ci * | TAIL room | 1578c2ecf20Sopenharmony_ci * | | 1588c2ecf20Sopenharmony_ci * | msg_hdr to skip (size |= 0x80000) | 1598c2ecf20Sopenharmony_ci * |---------------------------------------------------|------- 1608c2ecf20Sopenharmony_ci * | | /|\ 1618c2ecf20Sopenharmony_ci * | | | 1628c2ecf20Sopenharmony_ci * | TX message padding | | 1638c2ecf20Sopenharmony_ci * | | | 1648c2ecf20Sopenharmony_ci * | | | 1658c2ecf20Sopenharmony_ci * |- - - - - - - - - - - - - - - - - - - - - - - - - -| | 1668c2ecf20Sopenharmony_ci * | | | 1678c2ecf20Sopenharmony_ci * | payload 1 | | 1688c2ecf20Sopenharmony_ci * | | N * tx_block_size 1698c2ecf20Sopenharmony_ci * | | | 1708c2ecf20Sopenharmony_ci * |- - - - - - - - - - - - - - - - - - - - - - - - - -| | 1718c2ecf20Sopenharmony_ci * | | | 1728c2ecf20Sopenharmony_ci * | payload 1 | | 1738c2ecf20Sopenharmony_ci * | | | 1748c2ecf20Sopenharmony_ci * | | | 1758c2ecf20Sopenharmony_ci * |- - - - - - - - - - - - - - - - - - - - - - - - - -|- -|- - - - 1768c2ecf20Sopenharmony_ci * | padding 3 /|\ | | /|\ 1778c2ecf20Sopenharmony_ci * | padding 2 | | | | 1788c2ecf20Sopenharmony_ci * | pld 1 32 bytes (2 * 16) | | | 1798c2ecf20Sopenharmony_ci * | pld 0 | | | | 1808c2ecf20Sopenharmony_ci * | moved msg_hdr \|/ | \|/ | 1818c2ecf20Sopenharmony_ci * |- - - - - - - - - - - - - - - - - - - - - - - - - -|- - - | 1828c2ecf20Sopenharmony_ci * | | _PLD_SIZE 1838c2ecf20Sopenharmony_ci * | unused | | 1848c2ecf20Sopenharmony_ci * | | | 1858c2ecf20Sopenharmony_ci * |- - - - - - - - - - - - - - - - - - - - - - - - - -| | 1868c2ecf20Sopenharmony_ci * | msg_hdr (size X) [this message is closed] | \|/ 1878c2ecf20Sopenharmony_ci * |===================================================|========== <=== OUT 1888c2ecf20Sopenharmony_ci * | | 1898c2ecf20Sopenharmony_ci * | | 1908c2ecf20Sopenharmony_ci * | | 1918c2ecf20Sopenharmony_ci * | Free rooom | 1928c2ecf20Sopenharmony_ci * | | 1938c2ecf20Sopenharmony_ci * | | 1948c2ecf20Sopenharmony_ci * | | 1958c2ecf20Sopenharmony_ci * | | 1968c2ecf20Sopenharmony_ci * | | 1978c2ecf20Sopenharmony_ci * | | 1988c2ecf20Sopenharmony_ci * | | 1998c2ecf20Sopenharmony_ci * | | 2008c2ecf20Sopenharmony_ci * | | 2018c2ecf20Sopenharmony_ci * |===================================================|========== <=== IN 2028c2ecf20Sopenharmony_ci * | | 2038c2ecf20Sopenharmony_ci * | | 2048c2ecf20Sopenharmony_ci * | | 2058c2ecf20Sopenharmony_ci * | | 2068c2ecf20Sopenharmony_ci * | payload 1 | 2078c2ecf20Sopenharmony_ci * | | 2088c2ecf20Sopenharmony_ci * | | 2098c2ecf20Sopenharmony_ci * |- - - - - - - - - - - - - - - - - - - - - - - - - -| 2108c2ecf20Sopenharmony_ci * | | 2118c2ecf20Sopenharmony_ci * | payload 0 | 2128c2ecf20Sopenharmony_ci * | | 2138c2ecf20Sopenharmony_ci * | | 2148c2ecf20Sopenharmony_ci * |- - - - - - - - - - - - - - - - - - - - - - - - - -| 2158c2ecf20Sopenharmony_ci * | pld 11 /|\ | 2168c2ecf20Sopenharmony_ci * | ... | | 2178c2ecf20Sopenharmony_ci * | pld 1 64 bytes (2 * 16) | 2188c2ecf20Sopenharmony_ci * | pld 0 | | 2198c2ecf20Sopenharmony_ci * | msg_hdr (size X) \|/ [message is open] | 2208c2ecf20Sopenharmony_ci * 0 --------------------------------------------------- 2218c2ecf20Sopenharmony_ci * 2228c2ecf20Sopenharmony_ci * 2238c2ecf20Sopenharmony_ci * ROADMAP 2248c2ecf20Sopenharmony_ci * 2258c2ecf20Sopenharmony_ci * i2400m_tx_setup() Called by i2400m_setup 2268c2ecf20Sopenharmony_ci * i2400m_tx_release() Called by i2400m_release() 2278c2ecf20Sopenharmony_ci * 2288c2ecf20Sopenharmony_ci * i2400m_tx() Called to send data or control frames 2298c2ecf20Sopenharmony_ci * i2400m_tx_fifo_push() Allocates append-space in the FIFO 2308c2ecf20Sopenharmony_ci * i2400m_tx_new() Opens a new message in the FIFO 2318c2ecf20Sopenharmony_ci * i2400m_tx_fits() Checks if a new payload fits in the message 2328c2ecf20Sopenharmony_ci * i2400m_tx_close() Closes an open message in the FIFO 2338c2ecf20Sopenharmony_ci * i2400m_tx_skip_tail() Marks unusable FIFO tail space 2348c2ecf20Sopenharmony_ci * i2400m->bus_tx_kick() 2358c2ecf20Sopenharmony_ci * 2368c2ecf20Sopenharmony_ci * Now i2400m->bus_tx_kick() is the the bus-specific driver backend 2378c2ecf20Sopenharmony_ci * implementation; that would do: 2388c2ecf20Sopenharmony_ci * 2398c2ecf20Sopenharmony_ci * i2400m->bus_tx_kick() 2408c2ecf20Sopenharmony_ci * i2400m_tx_msg_get() Gets first message ready to go 2418c2ecf20Sopenharmony_ci * ...sends it... 2428c2ecf20Sopenharmony_ci * i2400m_tx_msg_sent() Ack the message is sent; repeat from 2438c2ecf20Sopenharmony_ci * _tx_msg_get() until it returns NULL 2448c2ecf20Sopenharmony_ci * (FIFO empty). 2458c2ecf20Sopenharmony_ci */ 2468c2ecf20Sopenharmony_ci#include <linux/netdevice.h> 2478c2ecf20Sopenharmony_ci#include <linux/slab.h> 2488c2ecf20Sopenharmony_ci#include <linux/export.h> 2498c2ecf20Sopenharmony_ci#include "i2400m.h" 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci#define D_SUBMODULE tx 2538c2ecf20Sopenharmony_ci#include "debug-levels.h" 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_cienum { 2568c2ecf20Sopenharmony_ci /** 2578c2ecf20Sopenharmony_ci * TX Buffer size 2588c2ecf20Sopenharmony_ci * 2598c2ecf20Sopenharmony_ci * Doc says maximum transaction is 16KiB. If we had 16KiB en 2608c2ecf20Sopenharmony_ci * route and 16KiB being queued, it boils down to needing 2618c2ecf20Sopenharmony_ci * 32KiB. 2628c2ecf20Sopenharmony_ci * 32KiB is insufficient for 1400 MTU, hence increasing 2638c2ecf20Sopenharmony_ci * tx buffer size to 64KiB. 2648c2ecf20Sopenharmony_ci */ 2658c2ecf20Sopenharmony_ci I2400M_TX_BUF_SIZE = 65536, 2668c2ecf20Sopenharmony_ci /** 2678c2ecf20Sopenharmony_ci * Message header and payload descriptors have to be 16 2688c2ecf20Sopenharmony_ci * aligned (16 + 4 * N = 16 * M). If we take that average sent 2698c2ecf20Sopenharmony_ci * packets are MTU size (~1400-~1500) it follows that we could 2708c2ecf20Sopenharmony_ci * fit at most 10-11 payloads in one transaction. To meet the 2718c2ecf20Sopenharmony_ci * alignment requirement, that means we need to leave space 2728c2ecf20Sopenharmony_ci * for 12 (64 bytes). To simplify, we leave space for that. If 2738c2ecf20Sopenharmony_ci * at the end there are less, we pad up to the nearest 2748c2ecf20Sopenharmony_ci * multiple of 16. 2758c2ecf20Sopenharmony_ci */ 2768c2ecf20Sopenharmony_ci /* 2778c2ecf20Sopenharmony_ci * According to Intel Wimax i3200, i5x50 and i6x50 specification 2788c2ecf20Sopenharmony_ci * documents, the maximum number of payloads per message can be 2798c2ecf20Sopenharmony_ci * up to 60. Increasing the number of payloads to 60 per message 2808c2ecf20Sopenharmony_ci * helps to accommodate smaller payloads in a single transaction. 2818c2ecf20Sopenharmony_ci */ 2828c2ecf20Sopenharmony_ci I2400M_TX_PLD_MAX = 60, 2838c2ecf20Sopenharmony_ci I2400M_TX_PLD_SIZE = sizeof(struct i2400m_msg_hdr) 2848c2ecf20Sopenharmony_ci + I2400M_TX_PLD_MAX * sizeof(struct i2400m_pld), 2858c2ecf20Sopenharmony_ci I2400M_TX_SKIP = 0x80000000, 2868c2ecf20Sopenharmony_ci /* 2878c2ecf20Sopenharmony_ci * According to Intel Wimax i3200, i5x50 and i6x50 specification 2888c2ecf20Sopenharmony_ci * documents, the maximum size of each message can be up to 16KiB. 2898c2ecf20Sopenharmony_ci */ 2908c2ecf20Sopenharmony_ci I2400M_TX_MSG_SIZE = 16384, 2918c2ecf20Sopenharmony_ci}; 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci#define TAIL_FULL ((void *)~(unsigned long)NULL) 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci/* 2968c2ecf20Sopenharmony_ci * Calculate how much tail room is available 2978c2ecf20Sopenharmony_ci * 2988c2ecf20Sopenharmony_ci * Note the trick here. This path is ONLY caleed for Case A (see 2998c2ecf20Sopenharmony_ci * i2400m_tx_fifo_push() below), where we have: 3008c2ecf20Sopenharmony_ci * 3018c2ecf20Sopenharmony_ci * Case A 3028c2ecf20Sopenharmony_ci * N ___________ 3038c2ecf20Sopenharmony_ci * | tail room | 3048c2ecf20Sopenharmony_ci * | | 3058c2ecf20Sopenharmony_ci * |<- IN ->| 3068c2ecf20Sopenharmony_ci * | | 3078c2ecf20Sopenharmony_ci * | data | 3088c2ecf20Sopenharmony_ci * | | 3098c2ecf20Sopenharmony_ci * |<- OUT ->| 3108c2ecf20Sopenharmony_ci * | | 3118c2ecf20Sopenharmony_ci * | head room | 3128c2ecf20Sopenharmony_ci * 0 ----------- 3138c2ecf20Sopenharmony_ci * 3148c2ecf20Sopenharmony_ci * When calculating the tail_room, tx_in might get to be zero if 3158c2ecf20Sopenharmony_ci * i2400m->tx_in is right at the end of the buffer (really full 3168c2ecf20Sopenharmony_ci * buffer) if there is no head room. In this case, tail_room would be 3178c2ecf20Sopenharmony_ci * I2400M_TX_BUF_SIZE, although it is actually zero. Hence the final 3188c2ecf20Sopenharmony_ci * mod (%) operation. However, when doing this kind of optimization, 3198c2ecf20Sopenharmony_ci * i2400m->tx_in being zero would fail, so we treat is an a special 3208c2ecf20Sopenharmony_ci * case. 3218c2ecf20Sopenharmony_ci */ 3228c2ecf20Sopenharmony_cistatic inline 3238c2ecf20Sopenharmony_cisize_t __i2400m_tx_tail_room(struct i2400m *i2400m) 3248c2ecf20Sopenharmony_ci{ 3258c2ecf20Sopenharmony_ci size_t tail_room; 3268c2ecf20Sopenharmony_ci size_t tx_in; 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci if (unlikely(i2400m->tx_in == 0)) 3298c2ecf20Sopenharmony_ci return I2400M_TX_BUF_SIZE; 3308c2ecf20Sopenharmony_ci tx_in = i2400m->tx_in % I2400M_TX_BUF_SIZE; 3318c2ecf20Sopenharmony_ci tail_room = I2400M_TX_BUF_SIZE - tx_in; 3328c2ecf20Sopenharmony_ci tail_room %= I2400M_TX_BUF_SIZE; 3338c2ecf20Sopenharmony_ci return tail_room; 3348c2ecf20Sopenharmony_ci} 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci/* 3388c2ecf20Sopenharmony_ci * Allocate @size bytes in the TX fifo, return a pointer to it 3398c2ecf20Sopenharmony_ci * 3408c2ecf20Sopenharmony_ci * @i2400m: device descriptor 3418c2ecf20Sopenharmony_ci * @size: size of the buffer we need to allocate 3428c2ecf20Sopenharmony_ci * @padding: ensure that there is at least this many bytes of free 3438c2ecf20Sopenharmony_ci * contiguous space in the fifo. This is needed because later on 3448c2ecf20Sopenharmony_ci * we might need to add padding. 3458c2ecf20Sopenharmony_ci * @try_head: specify either to allocate head room or tail room space 3468c2ecf20Sopenharmony_ci * in the TX FIFO. This boolean is required to avoids a system hang 3478c2ecf20Sopenharmony_ci * due to an infinite loop caused by i2400m_tx_fifo_push(). 3488c2ecf20Sopenharmony_ci * The caller must always try to allocate tail room space first by 3498c2ecf20Sopenharmony_ci * calling this routine with try_head = 0. In case if there 3508c2ecf20Sopenharmony_ci * is not enough tail room space but there is enough head room space, 3518c2ecf20Sopenharmony_ci * (i2400m_tx_fifo_push() returns TAIL_FULL) try to allocate head 3528c2ecf20Sopenharmony_ci * room space, by calling this routine again with try_head = 1. 3538c2ecf20Sopenharmony_ci * 3548c2ecf20Sopenharmony_ci * Returns: 3558c2ecf20Sopenharmony_ci * 3568c2ecf20Sopenharmony_ci * Pointer to the allocated space. NULL if there is no 3578c2ecf20Sopenharmony_ci * space. TAIL_FULL if there is no space at the tail but there is at 3588c2ecf20Sopenharmony_ci * the head (Case B below). 3598c2ecf20Sopenharmony_ci * 3608c2ecf20Sopenharmony_ci * These are the two basic cases we need to keep an eye for -- it is 3618c2ecf20Sopenharmony_ci * much better explained in linux/kernel/kfifo.c, but this code 3628c2ecf20Sopenharmony_ci * basically does the same. No rocket science here. 3638c2ecf20Sopenharmony_ci * 3648c2ecf20Sopenharmony_ci * Case A Case B 3658c2ecf20Sopenharmony_ci * N ___________ ___________ 3668c2ecf20Sopenharmony_ci * | tail room | | data | 3678c2ecf20Sopenharmony_ci * | | | | 3688c2ecf20Sopenharmony_ci * |<- IN ->| |<- OUT ->| 3698c2ecf20Sopenharmony_ci * | | | | 3708c2ecf20Sopenharmony_ci * | data | | room | 3718c2ecf20Sopenharmony_ci * | | | | 3728c2ecf20Sopenharmony_ci * |<- OUT ->| |<- IN ->| 3738c2ecf20Sopenharmony_ci * | | | | 3748c2ecf20Sopenharmony_ci * | head room | | data | 3758c2ecf20Sopenharmony_ci * 0 ----------- ----------- 3768c2ecf20Sopenharmony_ci * 3778c2ecf20Sopenharmony_ci * We allocate only *contiguous* space. 3788c2ecf20Sopenharmony_ci * 3798c2ecf20Sopenharmony_ci * We can allocate only from 'room'. In Case B, it is simple; in case 3808c2ecf20Sopenharmony_ci * A, we only try from the tail room; if it is not enough, we just 3818c2ecf20Sopenharmony_ci * fail and return TAIL_FULL and let the caller figure out if we wants to 3828c2ecf20Sopenharmony_ci * skip the tail room and try to allocate from the head. 3838c2ecf20Sopenharmony_ci * 3848c2ecf20Sopenharmony_ci * There is a corner case, wherein i2400m_tx_new() can get into 3858c2ecf20Sopenharmony_ci * an infinite loop calling i2400m_tx_fifo_push(). 3868c2ecf20Sopenharmony_ci * In certain situations, tx_in would have reached on the top of TX FIFO 3878c2ecf20Sopenharmony_ci * and i2400m_tx_tail_room() returns 0, as described below: 3888c2ecf20Sopenharmony_ci * 3898c2ecf20Sopenharmony_ci * N ___________ tail room is zero 3908c2ecf20Sopenharmony_ci * |<- IN ->| 3918c2ecf20Sopenharmony_ci * | | 3928c2ecf20Sopenharmony_ci * | | 3938c2ecf20Sopenharmony_ci * | | 3948c2ecf20Sopenharmony_ci * | data | 3958c2ecf20Sopenharmony_ci * |<- OUT ->| 3968c2ecf20Sopenharmony_ci * | | 3978c2ecf20Sopenharmony_ci * | | 3988c2ecf20Sopenharmony_ci * | head room | 3998c2ecf20Sopenharmony_ci * 0 ----------- 4008c2ecf20Sopenharmony_ci * During such a time, where tail room is zero in the TX FIFO and if there 4018c2ecf20Sopenharmony_ci * is a request to add a payload to TX FIFO, which calls: 4028c2ecf20Sopenharmony_ci * i2400m_tx() 4038c2ecf20Sopenharmony_ci * ->calls i2400m_tx_close() 4048c2ecf20Sopenharmony_ci * ->calls i2400m_tx_skip_tail() 4058c2ecf20Sopenharmony_ci * goto try_new; 4068c2ecf20Sopenharmony_ci * ->calls i2400m_tx_new() 4078c2ecf20Sopenharmony_ci * |----> [try_head:] 4088c2ecf20Sopenharmony_ci * infinite loop | ->calls i2400m_tx_fifo_push() 4098c2ecf20Sopenharmony_ci * | if (tail_room < needed) 4108c2ecf20Sopenharmony_ci * | if (head_room => needed) 4118c2ecf20Sopenharmony_ci * | return TAIL_FULL; 4128c2ecf20Sopenharmony_ci * |<---- goto try_head; 4138c2ecf20Sopenharmony_ci * 4148c2ecf20Sopenharmony_ci * i2400m_tx() calls i2400m_tx_close() to close the message, since there 4158c2ecf20Sopenharmony_ci * is no tail room to accommodate the payload and calls 4168c2ecf20Sopenharmony_ci * i2400m_tx_skip_tail() to skip the tail space. Now i2400m_tx() calls 4178c2ecf20Sopenharmony_ci * i2400m_tx_new() to allocate space for new message header calling 4188c2ecf20Sopenharmony_ci * i2400m_tx_fifo_push() that returns TAIL_FULL, since there is no tail space 4198c2ecf20Sopenharmony_ci * to accommodate the message header, but there is enough head space. 4208c2ecf20Sopenharmony_ci * The i2400m_tx_new() keeps re-retrying by calling i2400m_tx_fifo_push() 4218c2ecf20Sopenharmony_ci * ending up in a loop causing system freeze. 4228c2ecf20Sopenharmony_ci * 4238c2ecf20Sopenharmony_ci * This corner case is avoided by using a try_head boolean, 4248c2ecf20Sopenharmony_ci * as an argument to i2400m_tx_fifo_push(). 4258c2ecf20Sopenharmony_ci * 4268c2ecf20Sopenharmony_ci * Note: 4278c2ecf20Sopenharmony_ci * 4288c2ecf20Sopenharmony_ci * Assumes i2400m->tx_lock is taken, and we use that as a barrier 4298c2ecf20Sopenharmony_ci * 4308c2ecf20Sopenharmony_ci * The indexes keep increasing and we reset them to zero when we 4318c2ecf20Sopenharmony_ci * pop data off the queue 4328c2ecf20Sopenharmony_ci */ 4338c2ecf20Sopenharmony_cistatic 4348c2ecf20Sopenharmony_civoid *i2400m_tx_fifo_push(struct i2400m *i2400m, size_t size, 4358c2ecf20Sopenharmony_ci size_t padding, bool try_head) 4368c2ecf20Sopenharmony_ci{ 4378c2ecf20Sopenharmony_ci struct device *dev = i2400m_dev(i2400m); 4388c2ecf20Sopenharmony_ci size_t room, tail_room, needed_size; 4398c2ecf20Sopenharmony_ci void *ptr; 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_ci needed_size = size + padding; 4428c2ecf20Sopenharmony_ci room = I2400M_TX_BUF_SIZE - (i2400m->tx_in - i2400m->tx_out); 4438c2ecf20Sopenharmony_ci if (room < needed_size) { /* this takes care of Case B */ 4448c2ecf20Sopenharmony_ci d_printf(2, dev, "fifo push %zu/%zu: no space\n", 4458c2ecf20Sopenharmony_ci size, padding); 4468c2ecf20Sopenharmony_ci return NULL; 4478c2ecf20Sopenharmony_ci } 4488c2ecf20Sopenharmony_ci /* Is there space at the tail? */ 4498c2ecf20Sopenharmony_ci tail_room = __i2400m_tx_tail_room(i2400m); 4508c2ecf20Sopenharmony_ci if (!try_head && tail_room < needed_size) { 4518c2ecf20Sopenharmony_ci /* 4528c2ecf20Sopenharmony_ci * If the tail room space is not enough to push the message 4538c2ecf20Sopenharmony_ci * in the TX FIFO, then there are two possibilities: 4548c2ecf20Sopenharmony_ci * 1. There is enough head room space to accommodate 4558c2ecf20Sopenharmony_ci * this message in the TX FIFO. 4568c2ecf20Sopenharmony_ci * 2. There is not enough space in the head room and 4578c2ecf20Sopenharmony_ci * in tail room of the TX FIFO to accommodate the message. 4588c2ecf20Sopenharmony_ci * In the case (1), return TAIL_FULL so that the caller 4598c2ecf20Sopenharmony_ci * can figure out, if the caller wants to push the message 4608c2ecf20Sopenharmony_ci * into the head room space. 4618c2ecf20Sopenharmony_ci * In the case (2), return NULL, indicating that the TX FIFO 4628c2ecf20Sopenharmony_ci * cannot accommodate the message. 4638c2ecf20Sopenharmony_ci */ 4648c2ecf20Sopenharmony_ci if (room - tail_room >= needed_size) { 4658c2ecf20Sopenharmony_ci d_printf(2, dev, "fifo push %zu/%zu: tail full\n", 4668c2ecf20Sopenharmony_ci size, padding); 4678c2ecf20Sopenharmony_ci return TAIL_FULL; /* There might be head space */ 4688c2ecf20Sopenharmony_ci } else { 4698c2ecf20Sopenharmony_ci d_printf(2, dev, "fifo push %zu/%zu: no head space\n", 4708c2ecf20Sopenharmony_ci size, padding); 4718c2ecf20Sopenharmony_ci return NULL; /* There is no space */ 4728c2ecf20Sopenharmony_ci } 4738c2ecf20Sopenharmony_ci } 4748c2ecf20Sopenharmony_ci ptr = i2400m->tx_buf + i2400m->tx_in % I2400M_TX_BUF_SIZE; 4758c2ecf20Sopenharmony_ci d_printf(2, dev, "fifo push %zu/%zu: at @%zu\n", size, padding, 4768c2ecf20Sopenharmony_ci i2400m->tx_in % I2400M_TX_BUF_SIZE); 4778c2ecf20Sopenharmony_ci i2400m->tx_in += size; 4788c2ecf20Sopenharmony_ci return ptr; 4798c2ecf20Sopenharmony_ci} 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_ci/* 4838c2ecf20Sopenharmony_ci * Mark the tail of the FIFO buffer as 'to-skip' 4848c2ecf20Sopenharmony_ci * 4858c2ecf20Sopenharmony_ci * We should never hit the BUG_ON() because all the sizes we push to 4868c2ecf20Sopenharmony_ci * the FIFO are padded to be a multiple of 16 -- the size of *msg 4878c2ecf20Sopenharmony_ci * (I2400M_PL_PAD for the payloads, I2400M_TX_PLD_SIZE for the 4888c2ecf20Sopenharmony_ci * header). 4898c2ecf20Sopenharmony_ci * 4908c2ecf20Sopenharmony_ci * Tail room can get to be zero if a message was opened when there was 4918c2ecf20Sopenharmony_ci * space only for a header. _tx_close() will mark it as to-skip (as it 4928c2ecf20Sopenharmony_ci * will have no payloads) and there will be no more space to flush, so 4938c2ecf20Sopenharmony_ci * nothing has to be done here. This is probably cheaper than ensuring 4948c2ecf20Sopenharmony_ci * in _tx_new() that there is some space for payloads...as we could 4958c2ecf20Sopenharmony_ci * always possibly hit the same problem if the payload wouldn't fit. 4968c2ecf20Sopenharmony_ci * 4978c2ecf20Sopenharmony_ci * Note: 4988c2ecf20Sopenharmony_ci * 4998c2ecf20Sopenharmony_ci * Assumes i2400m->tx_lock is taken, and we use that as a barrier 5008c2ecf20Sopenharmony_ci * 5018c2ecf20Sopenharmony_ci * This path is only taken for Case A FIFO situations [see 5028c2ecf20Sopenharmony_ci * i2400m_tx_fifo_push()] 5038c2ecf20Sopenharmony_ci */ 5048c2ecf20Sopenharmony_cistatic 5058c2ecf20Sopenharmony_civoid i2400m_tx_skip_tail(struct i2400m *i2400m) 5068c2ecf20Sopenharmony_ci{ 5078c2ecf20Sopenharmony_ci struct device *dev = i2400m_dev(i2400m); 5088c2ecf20Sopenharmony_ci size_t tx_in = i2400m->tx_in % I2400M_TX_BUF_SIZE; 5098c2ecf20Sopenharmony_ci size_t tail_room = __i2400m_tx_tail_room(i2400m); 5108c2ecf20Sopenharmony_ci struct i2400m_msg_hdr *msg = i2400m->tx_buf + tx_in; 5118c2ecf20Sopenharmony_ci if (unlikely(tail_room == 0)) 5128c2ecf20Sopenharmony_ci return; 5138c2ecf20Sopenharmony_ci BUG_ON(tail_room < sizeof(*msg)); 5148c2ecf20Sopenharmony_ci msg->size = tail_room | I2400M_TX_SKIP; 5158c2ecf20Sopenharmony_ci d_printf(2, dev, "skip tail: skipping %zu bytes @%zu\n", 5168c2ecf20Sopenharmony_ci tail_room, tx_in); 5178c2ecf20Sopenharmony_ci i2400m->tx_in += tail_room; 5188c2ecf20Sopenharmony_ci} 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci/* 5228c2ecf20Sopenharmony_ci * Check if a skb will fit in the TX queue's current active TX 5238c2ecf20Sopenharmony_ci * message (if there are still descriptors left unused). 5248c2ecf20Sopenharmony_ci * 5258c2ecf20Sopenharmony_ci * Returns: 5268c2ecf20Sopenharmony_ci * 0 if the message won't fit, 1 if it will. 5278c2ecf20Sopenharmony_ci * 5288c2ecf20Sopenharmony_ci * Note: 5298c2ecf20Sopenharmony_ci * 5308c2ecf20Sopenharmony_ci * Assumes a TX message is active (i2400m->tx_msg). 5318c2ecf20Sopenharmony_ci * 5328c2ecf20Sopenharmony_ci * Assumes i2400m->tx_lock is taken, and we use that as a barrier 5338c2ecf20Sopenharmony_ci */ 5348c2ecf20Sopenharmony_cistatic 5358c2ecf20Sopenharmony_ciunsigned i2400m_tx_fits(struct i2400m *i2400m) 5368c2ecf20Sopenharmony_ci{ 5378c2ecf20Sopenharmony_ci struct i2400m_msg_hdr *msg_hdr = i2400m->tx_msg; 5388c2ecf20Sopenharmony_ci return le16_to_cpu(msg_hdr->num_pls) < I2400M_TX_PLD_MAX; 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_ci} 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_ci/* 5448c2ecf20Sopenharmony_ci * Start a new TX message header in the queue. 5458c2ecf20Sopenharmony_ci * 5468c2ecf20Sopenharmony_ci * Reserve memory from the base FIFO engine and then just initialize 5478c2ecf20Sopenharmony_ci * the message header. 5488c2ecf20Sopenharmony_ci * 5498c2ecf20Sopenharmony_ci * We allocate the biggest TX message header we might need (one that'd 5508c2ecf20Sopenharmony_ci * fit I2400M_TX_PLD_MAX payloads) -- when it is closed it will be 5518c2ecf20Sopenharmony_ci * 'ironed it out' and the unneeded parts removed. 5528c2ecf20Sopenharmony_ci * 5538c2ecf20Sopenharmony_ci * NOTE: 5548c2ecf20Sopenharmony_ci * 5558c2ecf20Sopenharmony_ci * Assumes that the previous message is CLOSED (eg: either 5568c2ecf20Sopenharmony_ci * there was none or 'i2400m_tx_close()' was called on it). 5578c2ecf20Sopenharmony_ci * 5588c2ecf20Sopenharmony_ci * Assumes i2400m->tx_lock is taken, and we use that as a barrier 5598c2ecf20Sopenharmony_ci */ 5608c2ecf20Sopenharmony_cistatic 5618c2ecf20Sopenharmony_civoid i2400m_tx_new(struct i2400m *i2400m) 5628c2ecf20Sopenharmony_ci{ 5638c2ecf20Sopenharmony_ci struct device *dev = i2400m_dev(i2400m); 5648c2ecf20Sopenharmony_ci struct i2400m_msg_hdr *tx_msg; 5658c2ecf20Sopenharmony_ci bool try_head = false; 5668c2ecf20Sopenharmony_ci BUG_ON(i2400m->tx_msg != NULL); 5678c2ecf20Sopenharmony_ci /* 5688c2ecf20Sopenharmony_ci * In certain situations, TX queue might have enough space to 5698c2ecf20Sopenharmony_ci * accommodate the new message header I2400M_TX_PLD_SIZE, but 5708c2ecf20Sopenharmony_ci * might not have enough space to accommodate the payloads. 5718c2ecf20Sopenharmony_ci * Adding bus_tx_room_min padding while allocating a new TX message 5728c2ecf20Sopenharmony_ci * increases the possibilities of including at least one payload of the 5738c2ecf20Sopenharmony_ci * size <= bus_tx_room_min. 5748c2ecf20Sopenharmony_ci */ 5758c2ecf20Sopenharmony_citry_head: 5768c2ecf20Sopenharmony_ci tx_msg = i2400m_tx_fifo_push(i2400m, I2400M_TX_PLD_SIZE, 5778c2ecf20Sopenharmony_ci i2400m->bus_tx_room_min, try_head); 5788c2ecf20Sopenharmony_ci if (tx_msg == NULL) 5798c2ecf20Sopenharmony_ci goto out; 5808c2ecf20Sopenharmony_ci else if (tx_msg == TAIL_FULL) { 5818c2ecf20Sopenharmony_ci i2400m_tx_skip_tail(i2400m); 5828c2ecf20Sopenharmony_ci d_printf(2, dev, "new TX message: tail full, trying head\n"); 5838c2ecf20Sopenharmony_ci try_head = true; 5848c2ecf20Sopenharmony_ci goto try_head; 5858c2ecf20Sopenharmony_ci } 5868c2ecf20Sopenharmony_ci memset(tx_msg, 0, I2400M_TX_PLD_SIZE); 5878c2ecf20Sopenharmony_ci tx_msg->size = I2400M_TX_PLD_SIZE; 5888c2ecf20Sopenharmony_ciout: 5898c2ecf20Sopenharmony_ci i2400m->tx_msg = tx_msg; 5908c2ecf20Sopenharmony_ci d_printf(2, dev, "new TX message: %p @%zu\n", 5918c2ecf20Sopenharmony_ci tx_msg, (void *) tx_msg - i2400m->tx_buf); 5928c2ecf20Sopenharmony_ci} 5938c2ecf20Sopenharmony_ci 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci/* 5968c2ecf20Sopenharmony_ci * Finalize the current TX message header 5978c2ecf20Sopenharmony_ci * 5988c2ecf20Sopenharmony_ci * Sets the message header to be at the proper location depending on 5998c2ecf20Sopenharmony_ci * how many descriptors we have (check documentation at the file's 6008c2ecf20Sopenharmony_ci * header for more info on that). 6018c2ecf20Sopenharmony_ci * 6028c2ecf20Sopenharmony_ci * Appends padding bytes to make sure the whole TX message (counting 6038c2ecf20Sopenharmony_ci * from the 'relocated' message header) is aligned to 6048c2ecf20Sopenharmony_ci * tx_block_size. We assume the _append() code has left enough space 6058c2ecf20Sopenharmony_ci * in the FIFO for that. If there are no payloads, just pass, as it 6068c2ecf20Sopenharmony_ci * won't be transferred. 6078c2ecf20Sopenharmony_ci * 6088c2ecf20Sopenharmony_ci * The amount of padding bytes depends on how many payloads are in the 6098c2ecf20Sopenharmony_ci * TX message, as the "msg header and payload descriptors" will be 6108c2ecf20Sopenharmony_ci * shifted up in the buffer. 6118c2ecf20Sopenharmony_ci */ 6128c2ecf20Sopenharmony_cistatic 6138c2ecf20Sopenharmony_civoid i2400m_tx_close(struct i2400m *i2400m) 6148c2ecf20Sopenharmony_ci{ 6158c2ecf20Sopenharmony_ci struct device *dev = i2400m_dev(i2400m); 6168c2ecf20Sopenharmony_ci struct i2400m_msg_hdr *tx_msg = i2400m->tx_msg; 6178c2ecf20Sopenharmony_ci struct i2400m_msg_hdr *tx_msg_moved; 6188c2ecf20Sopenharmony_ci size_t aligned_size, padding, hdr_size; 6198c2ecf20Sopenharmony_ci void *pad_buf; 6208c2ecf20Sopenharmony_ci unsigned num_pls; 6218c2ecf20Sopenharmony_ci 6228c2ecf20Sopenharmony_ci if (tx_msg->size & I2400M_TX_SKIP) /* a skipper? nothing to do */ 6238c2ecf20Sopenharmony_ci goto out; 6248c2ecf20Sopenharmony_ci num_pls = le16_to_cpu(tx_msg->num_pls); 6258c2ecf20Sopenharmony_ci /* We can get this situation when a new message was started 6268c2ecf20Sopenharmony_ci * and there was no space to add payloads before hitting the 6278c2ecf20Sopenharmony_ci tail (and taking padding into consideration). */ 6288c2ecf20Sopenharmony_ci if (num_pls == 0) { 6298c2ecf20Sopenharmony_ci tx_msg->size |= I2400M_TX_SKIP; 6308c2ecf20Sopenharmony_ci goto out; 6318c2ecf20Sopenharmony_ci } 6328c2ecf20Sopenharmony_ci /* Relocate the message header 6338c2ecf20Sopenharmony_ci * 6348c2ecf20Sopenharmony_ci * Find the current header size, align it to 16 and if we need 6358c2ecf20Sopenharmony_ci * to move it so the tail is next to the payloads, move it and 6368c2ecf20Sopenharmony_ci * set the offset. 6378c2ecf20Sopenharmony_ci * 6388c2ecf20Sopenharmony_ci * If it moved, this header is good only for transmission; the 6398c2ecf20Sopenharmony_ci * original one (it is kept if we moved) is still used to 6408c2ecf20Sopenharmony_ci * figure out where the next TX message starts (and where the 6418c2ecf20Sopenharmony_ci * offset to the moved header is). 6428c2ecf20Sopenharmony_ci */ 6438c2ecf20Sopenharmony_ci hdr_size = struct_size(tx_msg, pld, le16_to_cpu(tx_msg->num_pls)); 6448c2ecf20Sopenharmony_ci hdr_size = ALIGN(hdr_size, I2400M_PL_ALIGN); 6458c2ecf20Sopenharmony_ci tx_msg->offset = I2400M_TX_PLD_SIZE - hdr_size; 6468c2ecf20Sopenharmony_ci tx_msg_moved = (void *) tx_msg + tx_msg->offset; 6478c2ecf20Sopenharmony_ci memmove(tx_msg_moved, tx_msg, hdr_size); 6488c2ecf20Sopenharmony_ci tx_msg_moved->size -= tx_msg->offset; 6498c2ecf20Sopenharmony_ci /* 6508c2ecf20Sopenharmony_ci * Now figure out how much we have to add to the (moved!) 6518c2ecf20Sopenharmony_ci * message so the size is a multiple of i2400m->bus_tx_block_size. 6528c2ecf20Sopenharmony_ci */ 6538c2ecf20Sopenharmony_ci aligned_size = ALIGN(tx_msg_moved->size, i2400m->bus_tx_block_size); 6548c2ecf20Sopenharmony_ci padding = aligned_size - tx_msg_moved->size; 6558c2ecf20Sopenharmony_ci if (padding > 0) { 6568c2ecf20Sopenharmony_ci pad_buf = i2400m_tx_fifo_push(i2400m, padding, 0, 0); 6578c2ecf20Sopenharmony_ci if (WARN_ON(pad_buf == NULL || pad_buf == TAIL_FULL)) { 6588c2ecf20Sopenharmony_ci /* This should not happen -- append should verify 6598c2ecf20Sopenharmony_ci * there is always space left at least to append 6608c2ecf20Sopenharmony_ci * tx_block_size */ 6618c2ecf20Sopenharmony_ci dev_err(dev, 6628c2ecf20Sopenharmony_ci "SW BUG! Possible data leakage from memory the " 6638c2ecf20Sopenharmony_ci "device should not read for padding - " 6648c2ecf20Sopenharmony_ci "size %lu aligned_size %zu tx_buf %p in " 6658c2ecf20Sopenharmony_ci "%zu out %zu\n", 6668c2ecf20Sopenharmony_ci (unsigned long) tx_msg_moved->size, 6678c2ecf20Sopenharmony_ci aligned_size, i2400m->tx_buf, i2400m->tx_in, 6688c2ecf20Sopenharmony_ci i2400m->tx_out); 6698c2ecf20Sopenharmony_ci } else 6708c2ecf20Sopenharmony_ci memset(pad_buf, 0xad, padding); 6718c2ecf20Sopenharmony_ci } 6728c2ecf20Sopenharmony_ci tx_msg_moved->padding = cpu_to_le16(padding); 6738c2ecf20Sopenharmony_ci tx_msg_moved->size += padding; 6748c2ecf20Sopenharmony_ci if (tx_msg != tx_msg_moved) 6758c2ecf20Sopenharmony_ci tx_msg->size += padding; 6768c2ecf20Sopenharmony_ciout: 6778c2ecf20Sopenharmony_ci i2400m->tx_msg = NULL; 6788c2ecf20Sopenharmony_ci} 6798c2ecf20Sopenharmony_ci 6808c2ecf20Sopenharmony_ci 6818c2ecf20Sopenharmony_ci/** 6828c2ecf20Sopenharmony_ci * i2400m_tx - send the data in a buffer to the device 6838c2ecf20Sopenharmony_ci * 6848c2ecf20Sopenharmony_ci * @buf: pointer to the buffer to transmit 6858c2ecf20Sopenharmony_ci * 6868c2ecf20Sopenharmony_ci * @buf_len: buffer size 6878c2ecf20Sopenharmony_ci * 6888c2ecf20Sopenharmony_ci * @pl_type: type of the payload we are sending. 6898c2ecf20Sopenharmony_ci * 6908c2ecf20Sopenharmony_ci * Returns: 6918c2ecf20Sopenharmony_ci * 0 if ok, < 0 errno code on error (-ENOSPC, if there is no more 6928c2ecf20Sopenharmony_ci * room for the message in the queue). 6938c2ecf20Sopenharmony_ci * 6948c2ecf20Sopenharmony_ci * Appends the buffer to the TX FIFO and notifies the bus-specific 6958c2ecf20Sopenharmony_ci * part of the driver that there is new data ready to transmit. 6968c2ecf20Sopenharmony_ci * Once this function returns, the buffer has been copied, so it can 6978c2ecf20Sopenharmony_ci * be reused. 6988c2ecf20Sopenharmony_ci * 6998c2ecf20Sopenharmony_ci * The steps followed to append are explained in detail in the file 7008c2ecf20Sopenharmony_ci * header. 7018c2ecf20Sopenharmony_ci * 7028c2ecf20Sopenharmony_ci * Whenever we write to a message, we increase msg->size, so it 7038c2ecf20Sopenharmony_ci * reflects exactly how big the message is. This is needed so that if 7048c2ecf20Sopenharmony_ci * we concatenate two messages before they can be sent, the code that 7058c2ecf20Sopenharmony_ci * sends the messages can find the boundaries (and it will replace the 7068c2ecf20Sopenharmony_ci * size with the real barker before sending). 7078c2ecf20Sopenharmony_ci * 7088c2ecf20Sopenharmony_ci * Note: 7098c2ecf20Sopenharmony_ci * 7108c2ecf20Sopenharmony_ci * Cold and warm reset payloads need to be sent as a single 7118c2ecf20Sopenharmony_ci * payload, so we handle that. 7128c2ecf20Sopenharmony_ci */ 7138c2ecf20Sopenharmony_ciint i2400m_tx(struct i2400m *i2400m, const void *buf, size_t buf_len, 7148c2ecf20Sopenharmony_ci enum i2400m_pt pl_type) 7158c2ecf20Sopenharmony_ci{ 7168c2ecf20Sopenharmony_ci int result = -ENOSPC; 7178c2ecf20Sopenharmony_ci struct device *dev = i2400m_dev(i2400m); 7188c2ecf20Sopenharmony_ci unsigned long flags; 7198c2ecf20Sopenharmony_ci size_t padded_len; 7208c2ecf20Sopenharmony_ci void *ptr; 7218c2ecf20Sopenharmony_ci bool try_head = false; 7228c2ecf20Sopenharmony_ci unsigned is_singleton = pl_type == I2400M_PT_RESET_WARM 7238c2ecf20Sopenharmony_ci || pl_type == I2400M_PT_RESET_COLD; 7248c2ecf20Sopenharmony_ci 7258c2ecf20Sopenharmony_ci d_fnstart(3, dev, "(i2400m %p skb %p [%zu bytes] pt %u)\n", 7268c2ecf20Sopenharmony_ci i2400m, buf, buf_len, pl_type); 7278c2ecf20Sopenharmony_ci padded_len = ALIGN(buf_len, I2400M_PL_ALIGN); 7288c2ecf20Sopenharmony_ci d_printf(5, dev, "padded_len %zd buf_len %zd\n", padded_len, buf_len); 7298c2ecf20Sopenharmony_ci /* If there is no current TX message, create one; if the 7308c2ecf20Sopenharmony_ci * current one is out of payload slots or we have a singleton, 7318c2ecf20Sopenharmony_ci * close it and start a new one */ 7328c2ecf20Sopenharmony_ci spin_lock_irqsave(&i2400m->tx_lock, flags); 7338c2ecf20Sopenharmony_ci /* If tx_buf is NULL, device is shutdown */ 7348c2ecf20Sopenharmony_ci if (i2400m->tx_buf == NULL) { 7358c2ecf20Sopenharmony_ci result = -ESHUTDOWN; 7368c2ecf20Sopenharmony_ci goto error_tx_new; 7378c2ecf20Sopenharmony_ci } 7388c2ecf20Sopenharmony_citry_new: 7398c2ecf20Sopenharmony_ci if (unlikely(i2400m->tx_msg == NULL)) 7408c2ecf20Sopenharmony_ci i2400m_tx_new(i2400m); 7418c2ecf20Sopenharmony_ci else if (unlikely(!i2400m_tx_fits(i2400m) 7428c2ecf20Sopenharmony_ci || (is_singleton && i2400m->tx_msg->num_pls != 0))) { 7438c2ecf20Sopenharmony_ci d_printf(2, dev, "closing TX message (fits %u singleton " 7448c2ecf20Sopenharmony_ci "%u num_pls %u)\n", i2400m_tx_fits(i2400m), 7458c2ecf20Sopenharmony_ci is_singleton, i2400m->tx_msg->num_pls); 7468c2ecf20Sopenharmony_ci i2400m_tx_close(i2400m); 7478c2ecf20Sopenharmony_ci i2400m_tx_new(i2400m); 7488c2ecf20Sopenharmony_ci } 7498c2ecf20Sopenharmony_ci if (i2400m->tx_msg == NULL) 7508c2ecf20Sopenharmony_ci goto error_tx_new; 7518c2ecf20Sopenharmony_ci /* 7528c2ecf20Sopenharmony_ci * Check if this skb will fit in the TX queue's current active 7538c2ecf20Sopenharmony_ci * TX message. The total message size must not exceed the maximum 7548c2ecf20Sopenharmony_ci * size of each message I2400M_TX_MSG_SIZE. If it exceeds, 7558c2ecf20Sopenharmony_ci * close the current message and push this skb into the new message. 7568c2ecf20Sopenharmony_ci */ 7578c2ecf20Sopenharmony_ci if (i2400m->tx_msg->size + padded_len > I2400M_TX_MSG_SIZE) { 7588c2ecf20Sopenharmony_ci d_printf(2, dev, "TX: message too big, going new\n"); 7598c2ecf20Sopenharmony_ci i2400m_tx_close(i2400m); 7608c2ecf20Sopenharmony_ci i2400m_tx_new(i2400m); 7618c2ecf20Sopenharmony_ci } 7628c2ecf20Sopenharmony_ci if (i2400m->tx_msg == NULL) 7638c2ecf20Sopenharmony_ci goto error_tx_new; 7648c2ecf20Sopenharmony_ci /* So we have a current message header; now append space for 7658c2ecf20Sopenharmony_ci * the message -- if there is not enough, try the head */ 7668c2ecf20Sopenharmony_ci ptr = i2400m_tx_fifo_push(i2400m, padded_len, 7678c2ecf20Sopenharmony_ci i2400m->bus_tx_block_size, try_head); 7688c2ecf20Sopenharmony_ci if (ptr == TAIL_FULL) { /* Tail is full, try head */ 7698c2ecf20Sopenharmony_ci d_printf(2, dev, "pl append: tail full\n"); 7708c2ecf20Sopenharmony_ci i2400m_tx_close(i2400m); 7718c2ecf20Sopenharmony_ci i2400m_tx_skip_tail(i2400m); 7728c2ecf20Sopenharmony_ci try_head = true; 7738c2ecf20Sopenharmony_ci goto try_new; 7748c2ecf20Sopenharmony_ci } else if (ptr == NULL) { /* All full */ 7758c2ecf20Sopenharmony_ci result = -ENOSPC; 7768c2ecf20Sopenharmony_ci d_printf(2, dev, "pl append: all full\n"); 7778c2ecf20Sopenharmony_ci } else { /* Got space, copy it, set padding */ 7788c2ecf20Sopenharmony_ci struct i2400m_msg_hdr *tx_msg = i2400m->tx_msg; 7798c2ecf20Sopenharmony_ci unsigned num_pls = le16_to_cpu(tx_msg->num_pls); 7808c2ecf20Sopenharmony_ci memcpy(ptr, buf, buf_len); 7818c2ecf20Sopenharmony_ci memset(ptr + buf_len, 0xad, padded_len - buf_len); 7828c2ecf20Sopenharmony_ci i2400m_pld_set(&tx_msg->pld[num_pls], buf_len, pl_type); 7838c2ecf20Sopenharmony_ci d_printf(3, dev, "pld 0x%08x (type 0x%1x len 0x%04zx\n", 7848c2ecf20Sopenharmony_ci le32_to_cpu(tx_msg->pld[num_pls].val), 7858c2ecf20Sopenharmony_ci pl_type, buf_len); 7868c2ecf20Sopenharmony_ci tx_msg->num_pls = le16_to_cpu(num_pls+1); 7878c2ecf20Sopenharmony_ci tx_msg->size += padded_len; 7888c2ecf20Sopenharmony_ci d_printf(2, dev, "TX: appended %zu b (up to %u b) pl #%u\n", 7898c2ecf20Sopenharmony_ci padded_len, tx_msg->size, num_pls+1); 7908c2ecf20Sopenharmony_ci d_printf(2, dev, 7918c2ecf20Sopenharmony_ci "TX: appended hdr @%zu %zu b pl #%u @%zu %zu/%zu b\n", 7928c2ecf20Sopenharmony_ci (void *)tx_msg - i2400m->tx_buf, (size_t)tx_msg->size, 7938c2ecf20Sopenharmony_ci num_pls+1, ptr - i2400m->tx_buf, buf_len, padded_len); 7948c2ecf20Sopenharmony_ci result = 0; 7958c2ecf20Sopenharmony_ci if (is_singleton) 7968c2ecf20Sopenharmony_ci i2400m_tx_close(i2400m); 7978c2ecf20Sopenharmony_ci } 7988c2ecf20Sopenharmony_cierror_tx_new: 7998c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&i2400m->tx_lock, flags); 8008c2ecf20Sopenharmony_ci /* kick in most cases, except when the TX subsys is down, as 8018c2ecf20Sopenharmony_ci * it might free space */ 8028c2ecf20Sopenharmony_ci if (likely(result != -ESHUTDOWN)) 8038c2ecf20Sopenharmony_ci i2400m->bus_tx_kick(i2400m); 8048c2ecf20Sopenharmony_ci d_fnend(3, dev, "(i2400m %p skb %p [%zu bytes] pt %u) = %d\n", 8058c2ecf20Sopenharmony_ci i2400m, buf, buf_len, pl_type, result); 8068c2ecf20Sopenharmony_ci return result; 8078c2ecf20Sopenharmony_ci} 8088c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2400m_tx); 8098c2ecf20Sopenharmony_ci 8108c2ecf20Sopenharmony_ci 8118c2ecf20Sopenharmony_ci/** 8128c2ecf20Sopenharmony_ci * i2400m_tx_msg_get - Get the first TX message in the FIFO to start sending it 8138c2ecf20Sopenharmony_ci * 8148c2ecf20Sopenharmony_ci * @i2400m: device descriptors 8158c2ecf20Sopenharmony_ci * @bus_size: where to place the size of the TX message 8168c2ecf20Sopenharmony_ci * 8178c2ecf20Sopenharmony_ci * Called by the bus-specific driver to get the first TX message at 8188c2ecf20Sopenharmony_ci * the FIF that is ready for transmission. 8198c2ecf20Sopenharmony_ci * 8208c2ecf20Sopenharmony_ci * It sets the state in @i2400m to indicate the bus-specific driver is 8218c2ecf20Sopenharmony_ci * transferring that message (i2400m->tx_msg_size). 8228c2ecf20Sopenharmony_ci * 8238c2ecf20Sopenharmony_ci * Once the transfer is completed, call i2400m_tx_msg_sent(). 8248c2ecf20Sopenharmony_ci * 8258c2ecf20Sopenharmony_ci * Notes: 8268c2ecf20Sopenharmony_ci * 8278c2ecf20Sopenharmony_ci * The size of the TX message to be transmitted might be smaller than 8288c2ecf20Sopenharmony_ci * that of the TX message in the FIFO (in case the header was 8298c2ecf20Sopenharmony_ci * shorter). Hence, we copy it in @bus_size, for the bus layer to 8308c2ecf20Sopenharmony_ci * use. We keep the message's size in i2400m->tx_msg_size so that 8318c2ecf20Sopenharmony_ci * when the bus later is done transferring we know how much to 8328c2ecf20Sopenharmony_ci * advance the fifo. 8338c2ecf20Sopenharmony_ci * 8348c2ecf20Sopenharmony_ci * We collect statistics here as all the data is available and we 8358c2ecf20Sopenharmony_ci * assume it is going to work [see i2400m_tx_msg_sent()]. 8368c2ecf20Sopenharmony_ci */ 8378c2ecf20Sopenharmony_cistruct i2400m_msg_hdr *i2400m_tx_msg_get(struct i2400m *i2400m, 8388c2ecf20Sopenharmony_ci size_t *bus_size) 8398c2ecf20Sopenharmony_ci{ 8408c2ecf20Sopenharmony_ci struct device *dev = i2400m_dev(i2400m); 8418c2ecf20Sopenharmony_ci struct i2400m_msg_hdr *tx_msg, *tx_msg_moved; 8428c2ecf20Sopenharmony_ci unsigned long flags, pls; 8438c2ecf20Sopenharmony_ci 8448c2ecf20Sopenharmony_ci d_fnstart(3, dev, "(i2400m %p bus_size %p)\n", i2400m, bus_size); 8458c2ecf20Sopenharmony_ci spin_lock_irqsave(&i2400m->tx_lock, flags); 8468c2ecf20Sopenharmony_ci tx_msg_moved = NULL; 8478c2ecf20Sopenharmony_ci if (i2400m->tx_buf == NULL) 8488c2ecf20Sopenharmony_ci goto out_unlock; 8498c2ecf20Sopenharmony_ciskip: 8508c2ecf20Sopenharmony_ci tx_msg_moved = NULL; 8518c2ecf20Sopenharmony_ci if (i2400m->tx_in == i2400m->tx_out) { /* Empty FIFO? */ 8528c2ecf20Sopenharmony_ci i2400m->tx_in = 0; 8538c2ecf20Sopenharmony_ci i2400m->tx_out = 0; 8548c2ecf20Sopenharmony_ci d_printf(2, dev, "TX: FIFO empty: resetting\n"); 8558c2ecf20Sopenharmony_ci goto out_unlock; 8568c2ecf20Sopenharmony_ci } 8578c2ecf20Sopenharmony_ci tx_msg = i2400m->tx_buf + i2400m->tx_out % I2400M_TX_BUF_SIZE; 8588c2ecf20Sopenharmony_ci if (tx_msg->size & I2400M_TX_SKIP) { /* skip? */ 8598c2ecf20Sopenharmony_ci d_printf(2, dev, "TX: skip: msg @%zu (%zu b)\n", 8608c2ecf20Sopenharmony_ci i2400m->tx_out % I2400M_TX_BUF_SIZE, 8618c2ecf20Sopenharmony_ci (size_t) tx_msg->size & ~I2400M_TX_SKIP); 8628c2ecf20Sopenharmony_ci i2400m->tx_out += tx_msg->size & ~I2400M_TX_SKIP; 8638c2ecf20Sopenharmony_ci goto skip; 8648c2ecf20Sopenharmony_ci } 8658c2ecf20Sopenharmony_ci 8668c2ecf20Sopenharmony_ci if (tx_msg->num_pls == 0) { /* No payloads? */ 8678c2ecf20Sopenharmony_ci if (tx_msg == i2400m->tx_msg) { /* open, we are done */ 8688c2ecf20Sopenharmony_ci d_printf(2, dev, 8698c2ecf20Sopenharmony_ci "TX: FIFO empty: open msg w/o payloads @%zu\n", 8708c2ecf20Sopenharmony_ci (void *) tx_msg - i2400m->tx_buf); 8718c2ecf20Sopenharmony_ci tx_msg = NULL; 8728c2ecf20Sopenharmony_ci goto out_unlock; 8738c2ecf20Sopenharmony_ci } else { /* closed, skip it */ 8748c2ecf20Sopenharmony_ci d_printf(2, dev, 8758c2ecf20Sopenharmony_ci "TX: skip msg w/o payloads @%zu (%zu b)\n", 8768c2ecf20Sopenharmony_ci (void *) tx_msg - i2400m->tx_buf, 8778c2ecf20Sopenharmony_ci (size_t) tx_msg->size); 8788c2ecf20Sopenharmony_ci i2400m->tx_out += tx_msg->size & ~I2400M_TX_SKIP; 8798c2ecf20Sopenharmony_ci goto skip; 8808c2ecf20Sopenharmony_ci } 8818c2ecf20Sopenharmony_ci } 8828c2ecf20Sopenharmony_ci if (tx_msg == i2400m->tx_msg) /* open msg? */ 8838c2ecf20Sopenharmony_ci i2400m_tx_close(i2400m); 8848c2ecf20Sopenharmony_ci 8858c2ecf20Sopenharmony_ci /* Now we have a valid TX message (with payloads) to TX */ 8868c2ecf20Sopenharmony_ci tx_msg_moved = (void *) tx_msg + tx_msg->offset; 8878c2ecf20Sopenharmony_ci i2400m->tx_msg_size = tx_msg->size; 8888c2ecf20Sopenharmony_ci *bus_size = tx_msg_moved->size; 8898c2ecf20Sopenharmony_ci d_printf(2, dev, "TX: pid %d msg hdr at @%zu offset +@%zu " 8908c2ecf20Sopenharmony_ci "size %zu bus_size %zu\n", 8918c2ecf20Sopenharmony_ci current->pid, (void *) tx_msg - i2400m->tx_buf, 8928c2ecf20Sopenharmony_ci (size_t) tx_msg->offset, (size_t) tx_msg->size, 8938c2ecf20Sopenharmony_ci (size_t) tx_msg_moved->size); 8948c2ecf20Sopenharmony_ci tx_msg_moved->barker = le32_to_cpu(I2400M_H2D_PREVIEW_BARKER); 8958c2ecf20Sopenharmony_ci tx_msg_moved->sequence = le32_to_cpu(i2400m->tx_sequence++); 8968c2ecf20Sopenharmony_ci 8978c2ecf20Sopenharmony_ci pls = le32_to_cpu(tx_msg_moved->num_pls); 8988c2ecf20Sopenharmony_ci i2400m->tx_pl_num += pls; /* Update stats */ 8998c2ecf20Sopenharmony_ci if (pls > i2400m->tx_pl_max) 9008c2ecf20Sopenharmony_ci i2400m->tx_pl_max = pls; 9018c2ecf20Sopenharmony_ci if (pls < i2400m->tx_pl_min) 9028c2ecf20Sopenharmony_ci i2400m->tx_pl_min = pls; 9038c2ecf20Sopenharmony_ci i2400m->tx_num++; 9048c2ecf20Sopenharmony_ci i2400m->tx_size_acc += *bus_size; 9058c2ecf20Sopenharmony_ci if (*bus_size < i2400m->tx_size_min) 9068c2ecf20Sopenharmony_ci i2400m->tx_size_min = *bus_size; 9078c2ecf20Sopenharmony_ci if (*bus_size > i2400m->tx_size_max) 9088c2ecf20Sopenharmony_ci i2400m->tx_size_max = *bus_size; 9098c2ecf20Sopenharmony_ciout_unlock: 9108c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&i2400m->tx_lock, flags); 9118c2ecf20Sopenharmony_ci d_fnstart(3, dev, "(i2400m %p bus_size %p [%zu]) = %p\n", 9128c2ecf20Sopenharmony_ci i2400m, bus_size, *bus_size, tx_msg_moved); 9138c2ecf20Sopenharmony_ci return tx_msg_moved; 9148c2ecf20Sopenharmony_ci} 9158c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2400m_tx_msg_get); 9168c2ecf20Sopenharmony_ci 9178c2ecf20Sopenharmony_ci 9188c2ecf20Sopenharmony_ci/** 9198c2ecf20Sopenharmony_ci * i2400m_tx_msg_sent - indicate the transmission of a TX message 9208c2ecf20Sopenharmony_ci * 9218c2ecf20Sopenharmony_ci * @i2400m: device descriptor 9228c2ecf20Sopenharmony_ci * 9238c2ecf20Sopenharmony_ci * Called by the bus-specific driver when a message has been sent; 9248c2ecf20Sopenharmony_ci * this pops it from the FIFO; and as there is space, start the queue 9258c2ecf20Sopenharmony_ci * in case it was stopped. 9268c2ecf20Sopenharmony_ci * 9278c2ecf20Sopenharmony_ci * Should be called even if the message send failed and we are 9288c2ecf20Sopenharmony_ci * dropping this TX message. 9298c2ecf20Sopenharmony_ci */ 9308c2ecf20Sopenharmony_civoid i2400m_tx_msg_sent(struct i2400m *i2400m) 9318c2ecf20Sopenharmony_ci{ 9328c2ecf20Sopenharmony_ci unsigned n; 9338c2ecf20Sopenharmony_ci unsigned long flags; 9348c2ecf20Sopenharmony_ci struct device *dev = i2400m_dev(i2400m); 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci d_fnstart(3, dev, "(i2400m %p)\n", i2400m); 9378c2ecf20Sopenharmony_ci spin_lock_irqsave(&i2400m->tx_lock, flags); 9388c2ecf20Sopenharmony_ci if (i2400m->tx_buf == NULL) 9398c2ecf20Sopenharmony_ci goto out_unlock; 9408c2ecf20Sopenharmony_ci i2400m->tx_out += i2400m->tx_msg_size; 9418c2ecf20Sopenharmony_ci d_printf(2, dev, "TX: sent %zu b\n", (size_t) i2400m->tx_msg_size); 9428c2ecf20Sopenharmony_ci i2400m->tx_msg_size = 0; 9438c2ecf20Sopenharmony_ci BUG_ON(i2400m->tx_out > i2400m->tx_in); 9448c2ecf20Sopenharmony_ci /* level them FIFO markers off */ 9458c2ecf20Sopenharmony_ci n = i2400m->tx_out / I2400M_TX_BUF_SIZE; 9468c2ecf20Sopenharmony_ci i2400m->tx_out %= I2400M_TX_BUF_SIZE; 9478c2ecf20Sopenharmony_ci i2400m->tx_in -= n * I2400M_TX_BUF_SIZE; 9488c2ecf20Sopenharmony_ciout_unlock: 9498c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&i2400m->tx_lock, flags); 9508c2ecf20Sopenharmony_ci d_fnend(3, dev, "(i2400m %p) = void\n", i2400m); 9518c2ecf20Sopenharmony_ci} 9528c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2400m_tx_msg_sent); 9538c2ecf20Sopenharmony_ci 9548c2ecf20Sopenharmony_ci 9558c2ecf20Sopenharmony_ci/** 9568c2ecf20Sopenharmony_ci * i2400m_tx_setup - Initialize the TX queue and infrastructure 9578c2ecf20Sopenharmony_ci * 9588c2ecf20Sopenharmony_ci * Make sure we reset the TX sequence to zero, as when this function 9598c2ecf20Sopenharmony_ci * is called, the firmware has been just restarted. Same rational 9608c2ecf20Sopenharmony_ci * for tx_in, tx_out, tx_msg_size and tx_msg. We reset them since 9618c2ecf20Sopenharmony_ci * the memory for TX queue is reallocated. 9628c2ecf20Sopenharmony_ci */ 9638c2ecf20Sopenharmony_ciint i2400m_tx_setup(struct i2400m *i2400m) 9648c2ecf20Sopenharmony_ci{ 9658c2ecf20Sopenharmony_ci int result = 0; 9668c2ecf20Sopenharmony_ci void *tx_buf; 9678c2ecf20Sopenharmony_ci unsigned long flags; 9688c2ecf20Sopenharmony_ci 9698c2ecf20Sopenharmony_ci /* Do this here only once -- can't do on 9708c2ecf20Sopenharmony_ci * i2400m_hard_start_xmit() as we'll cause race conditions if 9718c2ecf20Sopenharmony_ci * the WS was scheduled on another CPU */ 9728c2ecf20Sopenharmony_ci INIT_WORK(&i2400m->wake_tx_ws, i2400m_wake_tx_work); 9738c2ecf20Sopenharmony_ci 9748c2ecf20Sopenharmony_ci tx_buf = kmalloc(I2400M_TX_BUF_SIZE, GFP_ATOMIC); 9758c2ecf20Sopenharmony_ci if (tx_buf == NULL) { 9768c2ecf20Sopenharmony_ci result = -ENOMEM; 9778c2ecf20Sopenharmony_ci goto error_kmalloc; 9788c2ecf20Sopenharmony_ci } 9798c2ecf20Sopenharmony_ci 9808c2ecf20Sopenharmony_ci /* 9818c2ecf20Sopenharmony_ci * Fail the build if we can't fit at least two maximum size messages 9828c2ecf20Sopenharmony_ci * on the TX FIFO [one being delivered while one is constructed]. 9838c2ecf20Sopenharmony_ci */ 9848c2ecf20Sopenharmony_ci BUILD_BUG_ON(2 * I2400M_TX_MSG_SIZE > I2400M_TX_BUF_SIZE); 9858c2ecf20Sopenharmony_ci spin_lock_irqsave(&i2400m->tx_lock, flags); 9868c2ecf20Sopenharmony_ci i2400m->tx_sequence = 0; 9878c2ecf20Sopenharmony_ci i2400m->tx_in = 0; 9888c2ecf20Sopenharmony_ci i2400m->tx_out = 0; 9898c2ecf20Sopenharmony_ci i2400m->tx_msg_size = 0; 9908c2ecf20Sopenharmony_ci i2400m->tx_msg = NULL; 9918c2ecf20Sopenharmony_ci i2400m->tx_buf = tx_buf; 9928c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&i2400m->tx_lock, flags); 9938c2ecf20Sopenharmony_ci /* Huh? the bus layer has to define this... */ 9948c2ecf20Sopenharmony_ci BUG_ON(i2400m->bus_tx_block_size == 0); 9958c2ecf20Sopenharmony_cierror_kmalloc: 9968c2ecf20Sopenharmony_ci return result; 9978c2ecf20Sopenharmony_ci 9988c2ecf20Sopenharmony_ci} 9998c2ecf20Sopenharmony_ci 10008c2ecf20Sopenharmony_ci 10018c2ecf20Sopenharmony_ci/** 10028c2ecf20Sopenharmony_ci * i2400m_tx_release - Tear down the TX queue and infrastructure 10038c2ecf20Sopenharmony_ci */ 10048c2ecf20Sopenharmony_civoid i2400m_tx_release(struct i2400m *i2400m) 10058c2ecf20Sopenharmony_ci{ 10068c2ecf20Sopenharmony_ci unsigned long flags; 10078c2ecf20Sopenharmony_ci spin_lock_irqsave(&i2400m->tx_lock, flags); 10088c2ecf20Sopenharmony_ci kfree(i2400m->tx_buf); 10098c2ecf20Sopenharmony_ci i2400m->tx_buf = NULL; 10108c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&i2400m->tx_lock, flags); 10118c2ecf20Sopenharmony_ci} 1012