1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Intel Wireless WiMAX Connection 2400m
3*4882a593Smuzhiyun * Generic (non-bus specific) TX handling
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
9*4882a593Smuzhiyun * modification, are permitted provided that the following conditions
10*4882a593Smuzhiyun * are met:
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * * Redistributions of source code must retain the above copyright
13*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
14*4882a593Smuzhiyun * * Redistributions in binary form must reproduce the above copyright
15*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in
16*4882a593Smuzhiyun * the documentation and/or other materials provided with the
17*4882a593Smuzhiyun * distribution.
18*4882a593Smuzhiyun * * Neither the name of Intel Corporation nor the names of its
19*4882a593Smuzhiyun * contributors may be used to endorse or promote products derived
20*4882a593Smuzhiyun * from this software without specific prior written permission.
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23*4882a593Smuzhiyun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24*4882a593Smuzhiyun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25*4882a593Smuzhiyun * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26*4882a593Smuzhiyun * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27*4882a593Smuzhiyun * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28*4882a593Smuzhiyun * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29*4882a593Smuzhiyun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30*4882a593Smuzhiyun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32*4882a593Smuzhiyun * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * Intel Corporation <linux-wimax@intel.com>
36*4882a593Smuzhiyun * Yanir Lubetkin <yanirx.lubetkin@intel.com>
37*4882a593Smuzhiyun * - Initial implementation
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun * Intel Corporation <linux-wimax@intel.com>
40*4882a593Smuzhiyun * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
41*4882a593Smuzhiyun * - Rewritten to use a single FIFO to lower the memory allocation
42*4882a593Smuzhiyun * pressure and optimize cache hits when copying to the queue, as
43*4882a593Smuzhiyun * well as splitting out bus-specific code.
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * Implements data transmission to the device; this is done through a
47*4882a593Smuzhiyun * software FIFO, as data/control frames can be coalesced (while the
48*4882a593Smuzhiyun * device is reading the previous tx transaction, others accumulate).
49*4882a593Smuzhiyun *
50*4882a593Smuzhiyun * A FIFO is used because at the end it is resource-cheaper that trying
51*4882a593Smuzhiyun * to implement scatter/gather over USB. As well, most traffic is going
52*4882a593Smuzhiyun * to be download (vs upload).
53*4882a593Smuzhiyun *
54*4882a593Smuzhiyun * The format for sending/receiving data to/from the i2400m is
55*4882a593Smuzhiyun * described in detail in rx.c:PROTOCOL FORMAT. In here we implement
56*4882a593Smuzhiyun * the transmission of that. This is split between a bus-independent
57*4882a593Smuzhiyun * part that just prepares everything and a bus-specific part that
58*4882a593Smuzhiyun * does the actual transmission over the bus to the device (in the
59*4882a593Smuzhiyun * bus-specific driver).
60*4882a593Smuzhiyun *
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * The general format of a device-host transaction is MSG-HDR, PLD1,
63*4882a593Smuzhiyun * PLD2...PLDN, PL1, PL2,...PLN, PADDING.
64*4882a593Smuzhiyun *
65*4882a593Smuzhiyun * Because we need the send payload descriptors and then payloads and
66*4882a593Smuzhiyun * because it is kind of expensive to do scatterlists in USB (one URB
67*4882a593Smuzhiyun * per node), it becomes cheaper to append all the data to a FIFO
68*4882a593Smuzhiyun * (copying to a FIFO potentially in cache is cheaper).
69*4882a593Smuzhiyun *
70*4882a593Smuzhiyun * Then the bus-specific code takes the parts of that FIFO that are
71*4882a593Smuzhiyun * written and passes them to the device.
72*4882a593Smuzhiyun *
73*4882a593Smuzhiyun * So the concepts to keep in mind there are:
74*4882a593Smuzhiyun *
75*4882a593Smuzhiyun * We use a FIFO to queue the data in a linear buffer. We first append
76*4882a593Smuzhiyun * a MSG-HDR, space for I2400M_TX_PLD_MAX payload descriptors and then
77*4882a593Smuzhiyun * go appending payloads until we run out of space or of payload
78*4882a593Smuzhiyun * descriptors. Then we append padding to make the whole transaction a
79*4882a593Smuzhiyun * multiple of i2400m->bus_tx_block_size (as defined by the bus layer).
80*4882a593Smuzhiyun *
81*4882a593Smuzhiyun * - A TX message: a combination of a message header, payload
82*4882a593Smuzhiyun * descriptors and payloads.
83*4882a593Smuzhiyun *
84*4882a593Smuzhiyun * Open: it is marked as active (i2400m->tx_msg is valid) and we
85*4882a593Smuzhiyun * can keep adding payloads to it.
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * Closed: we are not appending more payloads to this TX message
88*4882a593Smuzhiyun * (exahusted space in the queue, too many payloads or
89*4882a593Smuzhiyun * whichever). We have appended padding so the whole message
90*4882a593Smuzhiyun * length is aligned to i2400m->bus_tx_block_size (as set by the
91*4882a593Smuzhiyun * bus/transport layer).
92*4882a593Smuzhiyun *
93*4882a593Smuzhiyun * - Most of the time we keep a TX message open to which we append
94*4882a593Smuzhiyun * payloads.
95*4882a593Smuzhiyun *
96*4882a593Smuzhiyun * - If we are going to append and there is no more space (we are at
97*4882a593Smuzhiyun * the end of the FIFO), we close the message, mark the rest of the
98*4882a593Smuzhiyun * FIFO space unusable (skip_tail), create a new message at the
99*4882a593Smuzhiyun * beginning of the FIFO (if there is space) and append the message
100*4882a593Smuzhiyun * there.
101*4882a593Smuzhiyun *
102*4882a593Smuzhiyun * This is because we need to give linear TX messages to the bus
103*4882a593Smuzhiyun * engine. So we don't write a message to the remaining FIFO space
104*4882a593Smuzhiyun * until the tail and continue at the head of it.
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * - We overload one of the fields in the message header to use it as
107*4882a593Smuzhiyun * 'size' of the TX message, so we can iterate over them. It also
108*4882a593Smuzhiyun * contains a flag that indicates if we have to skip it or not.
109*4882a593Smuzhiyun * When we send the buffer, we update that to its real on-the-wire
110*4882a593Smuzhiyun * value.
111*4882a593Smuzhiyun *
112*4882a593Smuzhiyun * - The MSG-HDR PLD1...PLD2 stuff has to be a size multiple of 16.
113*4882a593Smuzhiyun *
114*4882a593Smuzhiyun * It follows that if MSG-HDR says we have N messages, the whole
115*4882a593Smuzhiyun * header + descriptors is 16 + 4*N; for those to be a multiple of
116*4882a593Smuzhiyun * 16, it follows that N can be 4, 8, 12, ... (32, 48, 64, 80...
117*4882a593Smuzhiyun * bytes).
118*4882a593Smuzhiyun *
119*4882a593Smuzhiyun * So if we have only 1 payload, we have to submit a header that in
120*4882a593Smuzhiyun * all truth has space for 4.
121*4882a593Smuzhiyun *
122*4882a593Smuzhiyun * The implication is that we reserve space for 12 (64 bytes); but
123*4882a593Smuzhiyun * if we fill up only (eg) 2, our header becomes 32 bytes only. So
124*4882a593Smuzhiyun * the TX engine has to shift those 32 bytes of msg header and 2
125*4882a593Smuzhiyun * payloads and padding so that right after it the payloads start
126*4882a593Smuzhiyun * and the TX engine has to know about that.
127*4882a593Smuzhiyun *
128*4882a593Smuzhiyun * It is cheaper to move the header up than the whole payloads down.
129*4882a593Smuzhiyun *
130*4882a593Smuzhiyun * We do this in i2400m_tx_close(). See 'i2400m_msg_hdr->offset'.
131*4882a593Smuzhiyun *
132*4882a593Smuzhiyun * - Each payload has to be size-padded to 16 bytes; before appending
133*4882a593Smuzhiyun * it, we just do it.
134*4882a593Smuzhiyun *
135*4882a593Smuzhiyun * - The whole message has to be padded to i2400m->bus_tx_block_size;
136*4882a593Smuzhiyun * we do this at close time. Thus, when reserving space for the
137*4882a593Smuzhiyun * payload, we always make sure there is also free space for this
138*4882a593Smuzhiyun * padding that sooner or later will happen.
139*4882a593Smuzhiyun *
140*4882a593Smuzhiyun * When we append a message, we tell the bus specific code to kick in
141*4882a593Smuzhiyun * TXs. It will TX (in parallel) until the buffer is exhausted--hence
142*4882a593Smuzhiyun * the lockin we do. The TX code will only send a TX message at the
143*4882a593Smuzhiyun * time (which remember, might contain more than one payload). Of
144*4882a593Smuzhiyun * course, when the bus-specific driver attempts to TX a message that
145*4882a593Smuzhiyun * is still open, it gets closed first.
146*4882a593Smuzhiyun *
147*4882a593Smuzhiyun * Gee, this is messy; well a picture. In the example below we have a
148*4882a593Smuzhiyun * partially full FIFO, with a closed message ready to be delivered
149*4882a593Smuzhiyun * (with a moved message header to make sure it is size-aligned to
150*4882a593Smuzhiyun * 16), TAIL room that was unusable (and thus is marked with a message
151*4882a593Smuzhiyun * header that says 'skip this') and at the head of the buffer, an
152*4882a593Smuzhiyun * incomplete message with a couple of payloads.
153*4882a593Smuzhiyun *
154*4882a593Smuzhiyun * N ___________________________________________________
155*4882a593Smuzhiyun * | |
156*4882a593Smuzhiyun * | TAIL room |
157*4882a593Smuzhiyun * | |
158*4882a593Smuzhiyun * | msg_hdr to skip (size |= 0x80000) |
159*4882a593Smuzhiyun * |---------------------------------------------------|-------
160*4882a593Smuzhiyun * | | /|\
161*4882a593Smuzhiyun * | | |
162*4882a593Smuzhiyun * | TX message padding | |
163*4882a593Smuzhiyun * | | |
164*4882a593Smuzhiyun * | | |
165*4882a593Smuzhiyun * |- - - - - - - - - - - - - - - - - - - - - - - - - -| |
166*4882a593Smuzhiyun * | | |
167*4882a593Smuzhiyun * | payload 1 | |
168*4882a593Smuzhiyun * | | N * tx_block_size
169*4882a593Smuzhiyun * | | |
170*4882a593Smuzhiyun * |- - - - - - - - - - - - - - - - - - - - - - - - - -| |
171*4882a593Smuzhiyun * | | |
172*4882a593Smuzhiyun * | payload 1 | |
173*4882a593Smuzhiyun * | | |
174*4882a593Smuzhiyun * | | |
175*4882a593Smuzhiyun * |- - - - - - - - - - - - - - - - - - - - - - - - - -|- -|- - - -
176*4882a593Smuzhiyun * | padding 3 /|\ | | /|\
177*4882a593Smuzhiyun * | padding 2 | | | |
178*4882a593Smuzhiyun * | pld 1 32 bytes (2 * 16) | | |
179*4882a593Smuzhiyun * | pld 0 | | | |
180*4882a593Smuzhiyun * | moved msg_hdr \|/ | \|/ |
181*4882a593Smuzhiyun * |- - - - - - - - - - - - - - - - - - - - - - - - - -|- - - |
182*4882a593Smuzhiyun * | | _PLD_SIZE
183*4882a593Smuzhiyun * | unused | |
184*4882a593Smuzhiyun * | | |
185*4882a593Smuzhiyun * |- - - - - - - - - - - - - - - - - - - - - - - - - -| |
186*4882a593Smuzhiyun * | msg_hdr (size X) [this message is closed] | \|/
187*4882a593Smuzhiyun * |===================================================|========== <=== OUT
188*4882a593Smuzhiyun * | |
189*4882a593Smuzhiyun * | |
190*4882a593Smuzhiyun * | |
191*4882a593Smuzhiyun * | Free rooom |
192*4882a593Smuzhiyun * | |
193*4882a593Smuzhiyun * | |
194*4882a593Smuzhiyun * | |
195*4882a593Smuzhiyun * | |
196*4882a593Smuzhiyun * | |
197*4882a593Smuzhiyun * | |
198*4882a593Smuzhiyun * | |
199*4882a593Smuzhiyun * | |
200*4882a593Smuzhiyun * | |
201*4882a593Smuzhiyun * |===================================================|========== <=== IN
202*4882a593Smuzhiyun * | |
203*4882a593Smuzhiyun * | |
204*4882a593Smuzhiyun * | |
205*4882a593Smuzhiyun * | |
206*4882a593Smuzhiyun * | payload 1 |
207*4882a593Smuzhiyun * | |
208*4882a593Smuzhiyun * | |
209*4882a593Smuzhiyun * |- - - - - - - - - - - - - - - - - - - - - - - - - -|
210*4882a593Smuzhiyun * | |
211*4882a593Smuzhiyun * | payload 0 |
212*4882a593Smuzhiyun * | |
213*4882a593Smuzhiyun * | |
214*4882a593Smuzhiyun * |- - - - - - - - - - - - - - - - - - - - - - - - - -|
215*4882a593Smuzhiyun * | pld 11 /|\ |
216*4882a593Smuzhiyun * | ... | |
217*4882a593Smuzhiyun * | pld 1 64 bytes (2 * 16) |
218*4882a593Smuzhiyun * | pld 0 | |
219*4882a593Smuzhiyun * | msg_hdr (size X) \|/ [message is open] |
220*4882a593Smuzhiyun * 0 ---------------------------------------------------
221*4882a593Smuzhiyun *
222*4882a593Smuzhiyun *
223*4882a593Smuzhiyun * ROADMAP
224*4882a593Smuzhiyun *
225*4882a593Smuzhiyun * i2400m_tx_setup() Called by i2400m_setup
226*4882a593Smuzhiyun * i2400m_tx_release() Called by i2400m_release()
227*4882a593Smuzhiyun *
228*4882a593Smuzhiyun * i2400m_tx() Called to send data or control frames
229*4882a593Smuzhiyun * i2400m_tx_fifo_push() Allocates append-space in the FIFO
230*4882a593Smuzhiyun * i2400m_tx_new() Opens a new message in the FIFO
231*4882a593Smuzhiyun * i2400m_tx_fits() Checks if a new payload fits in the message
232*4882a593Smuzhiyun * i2400m_tx_close() Closes an open message in the FIFO
233*4882a593Smuzhiyun * i2400m_tx_skip_tail() Marks unusable FIFO tail space
234*4882a593Smuzhiyun * i2400m->bus_tx_kick()
235*4882a593Smuzhiyun *
236*4882a593Smuzhiyun * Now i2400m->bus_tx_kick() is the the bus-specific driver backend
237*4882a593Smuzhiyun * implementation; that would do:
238*4882a593Smuzhiyun *
239*4882a593Smuzhiyun * i2400m->bus_tx_kick()
240*4882a593Smuzhiyun * i2400m_tx_msg_get() Gets first message ready to go
241*4882a593Smuzhiyun * ...sends it...
242*4882a593Smuzhiyun * i2400m_tx_msg_sent() Ack the message is sent; repeat from
243*4882a593Smuzhiyun * _tx_msg_get() until it returns NULL
244*4882a593Smuzhiyun * (FIFO empty).
245*4882a593Smuzhiyun */
246*4882a593Smuzhiyun #include <linux/netdevice.h>
247*4882a593Smuzhiyun #include <linux/slab.h>
248*4882a593Smuzhiyun #include <linux/export.h>
249*4882a593Smuzhiyun #include "i2400m.h"
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun #define D_SUBMODULE tx
253*4882a593Smuzhiyun #include "debug-levels.h"
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun enum {
256*4882a593Smuzhiyun /**
257*4882a593Smuzhiyun * TX Buffer size
258*4882a593Smuzhiyun *
259*4882a593Smuzhiyun * Doc says maximum transaction is 16KiB. If we had 16KiB en
260*4882a593Smuzhiyun * route and 16KiB being queued, it boils down to needing
261*4882a593Smuzhiyun * 32KiB.
262*4882a593Smuzhiyun * 32KiB is insufficient for 1400 MTU, hence increasing
263*4882a593Smuzhiyun * tx buffer size to 64KiB.
264*4882a593Smuzhiyun */
265*4882a593Smuzhiyun I2400M_TX_BUF_SIZE = 65536,
266*4882a593Smuzhiyun /**
267*4882a593Smuzhiyun * Message header and payload descriptors have to be 16
268*4882a593Smuzhiyun * aligned (16 + 4 * N = 16 * M). If we take that average sent
269*4882a593Smuzhiyun * packets are MTU size (~1400-~1500) it follows that we could
270*4882a593Smuzhiyun * fit at most 10-11 payloads in one transaction. To meet the
271*4882a593Smuzhiyun * alignment requirement, that means we need to leave space
272*4882a593Smuzhiyun * for 12 (64 bytes). To simplify, we leave space for that. If
273*4882a593Smuzhiyun * at the end there are less, we pad up to the nearest
274*4882a593Smuzhiyun * multiple of 16.
275*4882a593Smuzhiyun */
276*4882a593Smuzhiyun /*
277*4882a593Smuzhiyun * According to Intel Wimax i3200, i5x50 and i6x50 specification
278*4882a593Smuzhiyun * documents, the maximum number of payloads per message can be
279*4882a593Smuzhiyun * up to 60. Increasing the number of payloads to 60 per message
280*4882a593Smuzhiyun * helps to accommodate smaller payloads in a single transaction.
281*4882a593Smuzhiyun */
282*4882a593Smuzhiyun I2400M_TX_PLD_MAX = 60,
283*4882a593Smuzhiyun I2400M_TX_PLD_SIZE = sizeof(struct i2400m_msg_hdr)
284*4882a593Smuzhiyun + I2400M_TX_PLD_MAX * sizeof(struct i2400m_pld),
285*4882a593Smuzhiyun I2400M_TX_SKIP = 0x80000000,
286*4882a593Smuzhiyun /*
287*4882a593Smuzhiyun * According to Intel Wimax i3200, i5x50 and i6x50 specification
288*4882a593Smuzhiyun * documents, the maximum size of each message can be up to 16KiB.
289*4882a593Smuzhiyun */
290*4882a593Smuzhiyun I2400M_TX_MSG_SIZE = 16384,
291*4882a593Smuzhiyun };
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun #define TAIL_FULL ((void *)~(unsigned long)NULL)
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /*
296*4882a593Smuzhiyun * Calculate how much tail room is available
297*4882a593Smuzhiyun *
298*4882a593Smuzhiyun * Note the trick here. This path is ONLY caleed for Case A (see
299*4882a593Smuzhiyun * i2400m_tx_fifo_push() below), where we have:
300*4882a593Smuzhiyun *
301*4882a593Smuzhiyun * Case A
302*4882a593Smuzhiyun * N ___________
303*4882a593Smuzhiyun * | tail room |
304*4882a593Smuzhiyun * | |
305*4882a593Smuzhiyun * |<- IN ->|
306*4882a593Smuzhiyun * | |
307*4882a593Smuzhiyun * | data |
308*4882a593Smuzhiyun * | |
309*4882a593Smuzhiyun * |<- OUT ->|
310*4882a593Smuzhiyun * | |
311*4882a593Smuzhiyun * | head room |
312*4882a593Smuzhiyun * 0 -----------
313*4882a593Smuzhiyun *
314*4882a593Smuzhiyun * When calculating the tail_room, tx_in might get to be zero if
315*4882a593Smuzhiyun * i2400m->tx_in is right at the end of the buffer (really full
316*4882a593Smuzhiyun * buffer) if there is no head room. In this case, tail_room would be
317*4882a593Smuzhiyun * I2400M_TX_BUF_SIZE, although it is actually zero. Hence the final
318*4882a593Smuzhiyun * mod (%) operation. However, when doing this kind of optimization,
319*4882a593Smuzhiyun * i2400m->tx_in being zero would fail, so we treat is an a special
320*4882a593Smuzhiyun * case.
321*4882a593Smuzhiyun */
322*4882a593Smuzhiyun static inline
__i2400m_tx_tail_room(struct i2400m * i2400m)323*4882a593Smuzhiyun size_t __i2400m_tx_tail_room(struct i2400m *i2400m)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun size_t tail_room;
326*4882a593Smuzhiyun size_t tx_in;
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun if (unlikely(i2400m->tx_in == 0))
329*4882a593Smuzhiyun return I2400M_TX_BUF_SIZE;
330*4882a593Smuzhiyun tx_in = i2400m->tx_in % I2400M_TX_BUF_SIZE;
331*4882a593Smuzhiyun tail_room = I2400M_TX_BUF_SIZE - tx_in;
332*4882a593Smuzhiyun tail_room %= I2400M_TX_BUF_SIZE;
333*4882a593Smuzhiyun return tail_room;
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun /*
338*4882a593Smuzhiyun * Allocate @size bytes in the TX fifo, return a pointer to it
339*4882a593Smuzhiyun *
340*4882a593Smuzhiyun * @i2400m: device descriptor
341*4882a593Smuzhiyun * @size: size of the buffer we need to allocate
342*4882a593Smuzhiyun * @padding: ensure that there is at least this many bytes of free
343*4882a593Smuzhiyun * contiguous space in the fifo. This is needed because later on
344*4882a593Smuzhiyun * we might need to add padding.
345*4882a593Smuzhiyun * @try_head: specify either to allocate head room or tail room space
346*4882a593Smuzhiyun * in the TX FIFO. This boolean is required to avoids a system hang
347*4882a593Smuzhiyun * due to an infinite loop caused by i2400m_tx_fifo_push().
348*4882a593Smuzhiyun * The caller must always try to allocate tail room space first by
349*4882a593Smuzhiyun * calling this routine with try_head = 0. In case if there
350*4882a593Smuzhiyun * is not enough tail room space but there is enough head room space,
351*4882a593Smuzhiyun * (i2400m_tx_fifo_push() returns TAIL_FULL) try to allocate head
352*4882a593Smuzhiyun * room space, by calling this routine again with try_head = 1.
353*4882a593Smuzhiyun *
354*4882a593Smuzhiyun * Returns:
355*4882a593Smuzhiyun *
356*4882a593Smuzhiyun * Pointer to the allocated space. NULL if there is no
357*4882a593Smuzhiyun * space. TAIL_FULL if there is no space at the tail but there is at
358*4882a593Smuzhiyun * the head (Case B below).
359*4882a593Smuzhiyun *
360*4882a593Smuzhiyun * These are the two basic cases we need to keep an eye for -- it is
361*4882a593Smuzhiyun * much better explained in linux/kernel/kfifo.c, but this code
362*4882a593Smuzhiyun * basically does the same. No rocket science here.
363*4882a593Smuzhiyun *
364*4882a593Smuzhiyun * Case A Case B
365*4882a593Smuzhiyun * N ___________ ___________
366*4882a593Smuzhiyun * | tail room | | data |
367*4882a593Smuzhiyun * | | | |
368*4882a593Smuzhiyun * |<- IN ->| |<- OUT ->|
369*4882a593Smuzhiyun * | | | |
370*4882a593Smuzhiyun * | data | | room |
371*4882a593Smuzhiyun * | | | |
372*4882a593Smuzhiyun * |<- OUT ->| |<- IN ->|
373*4882a593Smuzhiyun * | | | |
374*4882a593Smuzhiyun * | head room | | data |
375*4882a593Smuzhiyun * 0 ----------- -----------
376*4882a593Smuzhiyun *
377*4882a593Smuzhiyun * We allocate only *contiguous* space.
378*4882a593Smuzhiyun *
379*4882a593Smuzhiyun * We can allocate only from 'room'. In Case B, it is simple; in case
380*4882a593Smuzhiyun * A, we only try from the tail room; if it is not enough, we just
381*4882a593Smuzhiyun * fail and return TAIL_FULL and let the caller figure out if we wants to
382*4882a593Smuzhiyun * skip the tail room and try to allocate from the head.
383*4882a593Smuzhiyun *
384*4882a593Smuzhiyun * There is a corner case, wherein i2400m_tx_new() can get into
385*4882a593Smuzhiyun * an infinite loop calling i2400m_tx_fifo_push().
386*4882a593Smuzhiyun * In certain situations, tx_in would have reached on the top of TX FIFO
387*4882a593Smuzhiyun * and i2400m_tx_tail_room() returns 0, as described below:
388*4882a593Smuzhiyun *
389*4882a593Smuzhiyun * N ___________ tail room is zero
390*4882a593Smuzhiyun * |<- IN ->|
391*4882a593Smuzhiyun * | |
392*4882a593Smuzhiyun * | |
393*4882a593Smuzhiyun * | |
394*4882a593Smuzhiyun * | data |
395*4882a593Smuzhiyun * |<- OUT ->|
396*4882a593Smuzhiyun * | |
397*4882a593Smuzhiyun * | |
398*4882a593Smuzhiyun * | head room |
399*4882a593Smuzhiyun * 0 -----------
400*4882a593Smuzhiyun * During such a time, where tail room is zero in the TX FIFO and if there
401*4882a593Smuzhiyun * is a request to add a payload to TX FIFO, which calls:
402*4882a593Smuzhiyun * i2400m_tx()
403*4882a593Smuzhiyun * ->calls i2400m_tx_close()
404*4882a593Smuzhiyun * ->calls i2400m_tx_skip_tail()
405*4882a593Smuzhiyun * goto try_new;
406*4882a593Smuzhiyun * ->calls i2400m_tx_new()
407*4882a593Smuzhiyun * |----> [try_head:]
408*4882a593Smuzhiyun * infinite loop | ->calls i2400m_tx_fifo_push()
409*4882a593Smuzhiyun * | if (tail_room < needed)
410*4882a593Smuzhiyun * | if (head_room => needed)
411*4882a593Smuzhiyun * | return TAIL_FULL;
412*4882a593Smuzhiyun * |<---- goto try_head;
413*4882a593Smuzhiyun *
414*4882a593Smuzhiyun * i2400m_tx() calls i2400m_tx_close() to close the message, since there
415*4882a593Smuzhiyun * is no tail room to accommodate the payload and calls
416*4882a593Smuzhiyun * i2400m_tx_skip_tail() to skip the tail space. Now i2400m_tx() calls
417*4882a593Smuzhiyun * i2400m_tx_new() to allocate space for new message header calling
418*4882a593Smuzhiyun * i2400m_tx_fifo_push() that returns TAIL_FULL, since there is no tail space
419*4882a593Smuzhiyun * to accommodate the message header, but there is enough head space.
420*4882a593Smuzhiyun * The i2400m_tx_new() keeps re-retrying by calling i2400m_tx_fifo_push()
421*4882a593Smuzhiyun * ending up in a loop causing system freeze.
422*4882a593Smuzhiyun *
423*4882a593Smuzhiyun * This corner case is avoided by using a try_head boolean,
424*4882a593Smuzhiyun * as an argument to i2400m_tx_fifo_push().
425*4882a593Smuzhiyun *
426*4882a593Smuzhiyun * Note:
427*4882a593Smuzhiyun *
428*4882a593Smuzhiyun * Assumes i2400m->tx_lock is taken, and we use that as a barrier
429*4882a593Smuzhiyun *
430*4882a593Smuzhiyun * The indexes keep increasing and we reset them to zero when we
431*4882a593Smuzhiyun * pop data off the queue
432*4882a593Smuzhiyun */
433*4882a593Smuzhiyun static
i2400m_tx_fifo_push(struct i2400m * i2400m,size_t size,size_t padding,bool try_head)434*4882a593Smuzhiyun void *i2400m_tx_fifo_push(struct i2400m *i2400m, size_t size,
435*4882a593Smuzhiyun size_t padding, bool try_head)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
438*4882a593Smuzhiyun size_t room, tail_room, needed_size;
439*4882a593Smuzhiyun void *ptr;
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun needed_size = size + padding;
442*4882a593Smuzhiyun room = I2400M_TX_BUF_SIZE - (i2400m->tx_in - i2400m->tx_out);
443*4882a593Smuzhiyun if (room < needed_size) { /* this takes care of Case B */
444*4882a593Smuzhiyun d_printf(2, dev, "fifo push %zu/%zu: no space\n",
445*4882a593Smuzhiyun size, padding);
446*4882a593Smuzhiyun return NULL;
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun /* Is there space at the tail? */
449*4882a593Smuzhiyun tail_room = __i2400m_tx_tail_room(i2400m);
450*4882a593Smuzhiyun if (!try_head && tail_room < needed_size) {
451*4882a593Smuzhiyun /*
452*4882a593Smuzhiyun * If the tail room space is not enough to push the message
453*4882a593Smuzhiyun * in the TX FIFO, then there are two possibilities:
454*4882a593Smuzhiyun * 1. There is enough head room space to accommodate
455*4882a593Smuzhiyun * this message in the TX FIFO.
456*4882a593Smuzhiyun * 2. There is not enough space in the head room and
457*4882a593Smuzhiyun * in tail room of the TX FIFO to accommodate the message.
458*4882a593Smuzhiyun * In the case (1), return TAIL_FULL so that the caller
459*4882a593Smuzhiyun * can figure out, if the caller wants to push the message
460*4882a593Smuzhiyun * into the head room space.
461*4882a593Smuzhiyun * In the case (2), return NULL, indicating that the TX FIFO
462*4882a593Smuzhiyun * cannot accommodate the message.
463*4882a593Smuzhiyun */
464*4882a593Smuzhiyun if (room - tail_room >= needed_size) {
465*4882a593Smuzhiyun d_printf(2, dev, "fifo push %zu/%zu: tail full\n",
466*4882a593Smuzhiyun size, padding);
467*4882a593Smuzhiyun return TAIL_FULL; /* There might be head space */
468*4882a593Smuzhiyun } else {
469*4882a593Smuzhiyun d_printf(2, dev, "fifo push %zu/%zu: no head space\n",
470*4882a593Smuzhiyun size, padding);
471*4882a593Smuzhiyun return NULL; /* There is no space */
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun ptr = i2400m->tx_buf + i2400m->tx_in % I2400M_TX_BUF_SIZE;
475*4882a593Smuzhiyun d_printf(2, dev, "fifo push %zu/%zu: at @%zu\n", size, padding,
476*4882a593Smuzhiyun i2400m->tx_in % I2400M_TX_BUF_SIZE);
477*4882a593Smuzhiyun i2400m->tx_in += size;
478*4882a593Smuzhiyun return ptr;
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun /*
483*4882a593Smuzhiyun * Mark the tail of the FIFO buffer as 'to-skip'
484*4882a593Smuzhiyun *
485*4882a593Smuzhiyun * We should never hit the BUG_ON() because all the sizes we push to
486*4882a593Smuzhiyun * the FIFO are padded to be a multiple of 16 -- the size of *msg
487*4882a593Smuzhiyun * (I2400M_PL_PAD for the payloads, I2400M_TX_PLD_SIZE for the
488*4882a593Smuzhiyun * header).
489*4882a593Smuzhiyun *
490*4882a593Smuzhiyun * Tail room can get to be zero if a message was opened when there was
491*4882a593Smuzhiyun * space only for a header. _tx_close() will mark it as to-skip (as it
492*4882a593Smuzhiyun * will have no payloads) and there will be no more space to flush, so
493*4882a593Smuzhiyun * nothing has to be done here. This is probably cheaper than ensuring
494*4882a593Smuzhiyun * in _tx_new() that there is some space for payloads...as we could
495*4882a593Smuzhiyun * always possibly hit the same problem if the payload wouldn't fit.
496*4882a593Smuzhiyun *
497*4882a593Smuzhiyun * Note:
498*4882a593Smuzhiyun *
499*4882a593Smuzhiyun * Assumes i2400m->tx_lock is taken, and we use that as a barrier
500*4882a593Smuzhiyun *
501*4882a593Smuzhiyun * This path is only taken for Case A FIFO situations [see
502*4882a593Smuzhiyun * i2400m_tx_fifo_push()]
503*4882a593Smuzhiyun */
504*4882a593Smuzhiyun static
i2400m_tx_skip_tail(struct i2400m * i2400m)505*4882a593Smuzhiyun void i2400m_tx_skip_tail(struct i2400m *i2400m)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
508*4882a593Smuzhiyun size_t tx_in = i2400m->tx_in % I2400M_TX_BUF_SIZE;
509*4882a593Smuzhiyun size_t tail_room = __i2400m_tx_tail_room(i2400m);
510*4882a593Smuzhiyun struct i2400m_msg_hdr *msg = i2400m->tx_buf + tx_in;
511*4882a593Smuzhiyun if (unlikely(tail_room == 0))
512*4882a593Smuzhiyun return;
513*4882a593Smuzhiyun BUG_ON(tail_room < sizeof(*msg));
514*4882a593Smuzhiyun msg->size = tail_room | I2400M_TX_SKIP;
515*4882a593Smuzhiyun d_printf(2, dev, "skip tail: skipping %zu bytes @%zu\n",
516*4882a593Smuzhiyun tail_room, tx_in);
517*4882a593Smuzhiyun i2400m->tx_in += tail_room;
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun /*
522*4882a593Smuzhiyun * Check if a skb will fit in the TX queue's current active TX
523*4882a593Smuzhiyun * message (if there are still descriptors left unused).
524*4882a593Smuzhiyun *
525*4882a593Smuzhiyun * Returns:
526*4882a593Smuzhiyun * 0 if the message won't fit, 1 if it will.
527*4882a593Smuzhiyun *
528*4882a593Smuzhiyun * Note:
529*4882a593Smuzhiyun *
530*4882a593Smuzhiyun * Assumes a TX message is active (i2400m->tx_msg).
531*4882a593Smuzhiyun *
532*4882a593Smuzhiyun * Assumes i2400m->tx_lock is taken, and we use that as a barrier
533*4882a593Smuzhiyun */
534*4882a593Smuzhiyun static
i2400m_tx_fits(struct i2400m * i2400m)535*4882a593Smuzhiyun unsigned i2400m_tx_fits(struct i2400m *i2400m)
536*4882a593Smuzhiyun {
537*4882a593Smuzhiyun struct i2400m_msg_hdr *msg_hdr = i2400m->tx_msg;
538*4882a593Smuzhiyun return le16_to_cpu(msg_hdr->num_pls) < I2400M_TX_PLD_MAX;
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun /*
544*4882a593Smuzhiyun * Start a new TX message header in the queue.
545*4882a593Smuzhiyun *
546*4882a593Smuzhiyun * Reserve memory from the base FIFO engine and then just initialize
547*4882a593Smuzhiyun * the message header.
548*4882a593Smuzhiyun *
549*4882a593Smuzhiyun * We allocate the biggest TX message header we might need (one that'd
550*4882a593Smuzhiyun * fit I2400M_TX_PLD_MAX payloads) -- when it is closed it will be
551*4882a593Smuzhiyun * 'ironed it out' and the unneeded parts removed.
552*4882a593Smuzhiyun *
553*4882a593Smuzhiyun * NOTE:
554*4882a593Smuzhiyun *
555*4882a593Smuzhiyun * Assumes that the previous message is CLOSED (eg: either
556*4882a593Smuzhiyun * there was none or 'i2400m_tx_close()' was called on it).
557*4882a593Smuzhiyun *
558*4882a593Smuzhiyun * Assumes i2400m->tx_lock is taken, and we use that as a barrier
559*4882a593Smuzhiyun */
560*4882a593Smuzhiyun static
i2400m_tx_new(struct i2400m * i2400m)561*4882a593Smuzhiyun void i2400m_tx_new(struct i2400m *i2400m)
562*4882a593Smuzhiyun {
563*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
564*4882a593Smuzhiyun struct i2400m_msg_hdr *tx_msg;
565*4882a593Smuzhiyun bool try_head = false;
566*4882a593Smuzhiyun BUG_ON(i2400m->tx_msg != NULL);
567*4882a593Smuzhiyun /*
568*4882a593Smuzhiyun * In certain situations, TX queue might have enough space to
569*4882a593Smuzhiyun * accommodate the new message header I2400M_TX_PLD_SIZE, but
570*4882a593Smuzhiyun * might not have enough space to accommodate the payloads.
571*4882a593Smuzhiyun * Adding bus_tx_room_min padding while allocating a new TX message
572*4882a593Smuzhiyun * increases the possibilities of including at least one payload of the
573*4882a593Smuzhiyun * size <= bus_tx_room_min.
574*4882a593Smuzhiyun */
575*4882a593Smuzhiyun try_head:
576*4882a593Smuzhiyun tx_msg = i2400m_tx_fifo_push(i2400m, I2400M_TX_PLD_SIZE,
577*4882a593Smuzhiyun i2400m->bus_tx_room_min, try_head);
578*4882a593Smuzhiyun if (tx_msg == NULL)
579*4882a593Smuzhiyun goto out;
580*4882a593Smuzhiyun else if (tx_msg == TAIL_FULL) {
581*4882a593Smuzhiyun i2400m_tx_skip_tail(i2400m);
582*4882a593Smuzhiyun d_printf(2, dev, "new TX message: tail full, trying head\n");
583*4882a593Smuzhiyun try_head = true;
584*4882a593Smuzhiyun goto try_head;
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun memset(tx_msg, 0, I2400M_TX_PLD_SIZE);
587*4882a593Smuzhiyun tx_msg->size = I2400M_TX_PLD_SIZE;
588*4882a593Smuzhiyun out:
589*4882a593Smuzhiyun i2400m->tx_msg = tx_msg;
590*4882a593Smuzhiyun d_printf(2, dev, "new TX message: %p @%zu\n",
591*4882a593Smuzhiyun tx_msg, (void *) tx_msg - i2400m->tx_buf);
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun /*
596*4882a593Smuzhiyun * Finalize the current TX message header
597*4882a593Smuzhiyun *
598*4882a593Smuzhiyun * Sets the message header to be at the proper location depending on
599*4882a593Smuzhiyun * how many descriptors we have (check documentation at the file's
600*4882a593Smuzhiyun * header for more info on that).
601*4882a593Smuzhiyun *
602*4882a593Smuzhiyun * Appends padding bytes to make sure the whole TX message (counting
603*4882a593Smuzhiyun * from the 'relocated' message header) is aligned to
604*4882a593Smuzhiyun * tx_block_size. We assume the _append() code has left enough space
605*4882a593Smuzhiyun * in the FIFO for that. If there are no payloads, just pass, as it
606*4882a593Smuzhiyun * won't be transferred.
607*4882a593Smuzhiyun *
608*4882a593Smuzhiyun * The amount of padding bytes depends on how many payloads are in the
609*4882a593Smuzhiyun * TX message, as the "msg header and payload descriptors" will be
610*4882a593Smuzhiyun * shifted up in the buffer.
611*4882a593Smuzhiyun */
612*4882a593Smuzhiyun static
i2400m_tx_close(struct i2400m * i2400m)613*4882a593Smuzhiyun void i2400m_tx_close(struct i2400m *i2400m)
614*4882a593Smuzhiyun {
615*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
616*4882a593Smuzhiyun struct i2400m_msg_hdr *tx_msg = i2400m->tx_msg;
617*4882a593Smuzhiyun struct i2400m_msg_hdr *tx_msg_moved;
618*4882a593Smuzhiyun size_t aligned_size, padding, hdr_size;
619*4882a593Smuzhiyun void *pad_buf;
620*4882a593Smuzhiyun unsigned num_pls;
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun if (tx_msg->size & I2400M_TX_SKIP) /* a skipper? nothing to do */
623*4882a593Smuzhiyun goto out;
624*4882a593Smuzhiyun num_pls = le16_to_cpu(tx_msg->num_pls);
625*4882a593Smuzhiyun /* We can get this situation when a new message was started
626*4882a593Smuzhiyun * and there was no space to add payloads before hitting the
627*4882a593Smuzhiyun tail (and taking padding into consideration). */
628*4882a593Smuzhiyun if (num_pls == 0) {
629*4882a593Smuzhiyun tx_msg->size |= I2400M_TX_SKIP;
630*4882a593Smuzhiyun goto out;
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun /* Relocate the message header
633*4882a593Smuzhiyun *
634*4882a593Smuzhiyun * Find the current header size, align it to 16 and if we need
635*4882a593Smuzhiyun * to move it so the tail is next to the payloads, move it and
636*4882a593Smuzhiyun * set the offset.
637*4882a593Smuzhiyun *
638*4882a593Smuzhiyun * If it moved, this header is good only for transmission; the
639*4882a593Smuzhiyun * original one (it is kept if we moved) is still used to
640*4882a593Smuzhiyun * figure out where the next TX message starts (and where the
641*4882a593Smuzhiyun * offset to the moved header is).
642*4882a593Smuzhiyun */
643*4882a593Smuzhiyun hdr_size = struct_size(tx_msg, pld, le16_to_cpu(tx_msg->num_pls));
644*4882a593Smuzhiyun hdr_size = ALIGN(hdr_size, I2400M_PL_ALIGN);
645*4882a593Smuzhiyun tx_msg->offset = I2400M_TX_PLD_SIZE - hdr_size;
646*4882a593Smuzhiyun tx_msg_moved = (void *) tx_msg + tx_msg->offset;
647*4882a593Smuzhiyun memmove(tx_msg_moved, tx_msg, hdr_size);
648*4882a593Smuzhiyun tx_msg_moved->size -= tx_msg->offset;
649*4882a593Smuzhiyun /*
650*4882a593Smuzhiyun * Now figure out how much we have to add to the (moved!)
651*4882a593Smuzhiyun * message so the size is a multiple of i2400m->bus_tx_block_size.
652*4882a593Smuzhiyun */
653*4882a593Smuzhiyun aligned_size = ALIGN(tx_msg_moved->size, i2400m->bus_tx_block_size);
654*4882a593Smuzhiyun padding = aligned_size - tx_msg_moved->size;
655*4882a593Smuzhiyun if (padding > 0) {
656*4882a593Smuzhiyun pad_buf = i2400m_tx_fifo_push(i2400m, padding, 0, 0);
657*4882a593Smuzhiyun if (WARN_ON(pad_buf == NULL || pad_buf == TAIL_FULL)) {
658*4882a593Smuzhiyun /* This should not happen -- append should verify
659*4882a593Smuzhiyun * there is always space left at least to append
660*4882a593Smuzhiyun * tx_block_size */
661*4882a593Smuzhiyun dev_err(dev,
662*4882a593Smuzhiyun "SW BUG! Possible data leakage from memory the "
663*4882a593Smuzhiyun "device should not read for padding - "
664*4882a593Smuzhiyun "size %lu aligned_size %zu tx_buf %p in "
665*4882a593Smuzhiyun "%zu out %zu\n",
666*4882a593Smuzhiyun (unsigned long) tx_msg_moved->size,
667*4882a593Smuzhiyun aligned_size, i2400m->tx_buf, i2400m->tx_in,
668*4882a593Smuzhiyun i2400m->tx_out);
669*4882a593Smuzhiyun } else
670*4882a593Smuzhiyun memset(pad_buf, 0xad, padding);
671*4882a593Smuzhiyun }
672*4882a593Smuzhiyun tx_msg_moved->padding = cpu_to_le16(padding);
673*4882a593Smuzhiyun tx_msg_moved->size += padding;
674*4882a593Smuzhiyun if (tx_msg != tx_msg_moved)
675*4882a593Smuzhiyun tx_msg->size += padding;
676*4882a593Smuzhiyun out:
677*4882a593Smuzhiyun i2400m->tx_msg = NULL;
678*4882a593Smuzhiyun }
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun /**
682*4882a593Smuzhiyun * i2400m_tx - send the data in a buffer to the device
683*4882a593Smuzhiyun *
684*4882a593Smuzhiyun * @buf: pointer to the buffer to transmit
685*4882a593Smuzhiyun *
686*4882a593Smuzhiyun * @buf_len: buffer size
687*4882a593Smuzhiyun *
688*4882a593Smuzhiyun * @pl_type: type of the payload we are sending.
689*4882a593Smuzhiyun *
690*4882a593Smuzhiyun * Returns:
691*4882a593Smuzhiyun * 0 if ok, < 0 errno code on error (-ENOSPC, if there is no more
692*4882a593Smuzhiyun * room for the message in the queue).
693*4882a593Smuzhiyun *
694*4882a593Smuzhiyun * Appends the buffer to the TX FIFO and notifies the bus-specific
695*4882a593Smuzhiyun * part of the driver that there is new data ready to transmit.
696*4882a593Smuzhiyun * Once this function returns, the buffer has been copied, so it can
697*4882a593Smuzhiyun * be reused.
698*4882a593Smuzhiyun *
699*4882a593Smuzhiyun * The steps followed to append are explained in detail in the file
700*4882a593Smuzhiyun * header.
701*4882a593Smuzhiyun *
702*4882a593Smuzhiyun * Whenever we write to a message, we increase msg->size, so it
703*4882a593Smuzhiyun * reflects exactly how big the message is. This is needed so that if
704*4882a593Smuzhiyun * we concatenate two messages before they can be sent, the code that
705*4882a593Smuzhiyun * sends the messages can find the boundaries (and it will replace the
706*4882a593Smuzhiyun * size with the real barker before sending).
707*4882a593Smuzhiyun *
708*4882a593Smuzhiyun * Note:
709*4882a593Smuzhiyun *
710*4882a593Smuzhiyun * Cold and warm reset payloads need to be sent as a single
711*4882a593Smuzhiyun * payload, so we handle that.
712*4882a593Smuzhiyun */
i2400m_tx(struct i2400m * i2400m,const void * buf,size_t buf_len,enum i2400m_pt pl_type)713*4882a593Smuzhiyun int i2400m_tx(struct i2400m *i2400m, const void *buf, size_t buf_len,
714*4882a593Smuzhiyun enum i2400m_pt pl_type)
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun int result = -ENOSPC;
717*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
718*4882a593Smuzhiyun unsigned long flags;
719*4882a593Smuzhiyun size_t padded_len;
720*4882a593Smuzhiyun void *ptr;
721*4882a593Smuzhiyun bool try_head = false;
722*4882a593Smuzhiyun unsigned is_singleton = pl_type == I2400M_PT_RESET_WARM
723*4882a593Smuzhiyun || pl_type == I2400M_PT_RESET_COLD;
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun d_fnstart(3, dev, "(i2400m %p skb %p [%zu bytes] pt %u)\n",
726*4882a593Smuzhiyun i2400m, buf, buf_len, pl_type);
727*4882a593Smuzhiyun padded_len = ALIGN(buf_len, I2400M_PL_ALIGN);
728*4882a593Smuzhiyun d_printf(5, dev, "padded_len %zd buf_len %zd\n", padded_len, buf_len);
729*4882a593Smuzhiyun /* If there is no current TX message, create one; if the
730*4882a593Smuzhiyun * current one is out of payload slots or we have a singleton,
731*4882a593Smuzhiyun * close it and start a new one */
732*4882a593Smuzhiyun spin_lock_irqsave(&i2400m->tx_lock, flags);
733*4882a593Smuzhiyun /* If tx_buf is NULL, device is shutdown */
734*4882a593Smuzhiyun if (i2400m->tx_buf == NULL) {
735*4882a593Smuzhiyun result = -ESHUTDOWN;
736*4882a593Smuzhiyun goto error_tx_new;
737*4882a593Smuzhiyun }
738*4882a593Smuzhiyun try_new:
739*4882a593Smuzhiyun if (unlikely(i2400m->tx_msg == NULL))
740*4882a593Smuzhiyun i2400m_tx_new(i2400m);
741*4882a593Smuzhiyun else if (unlikely(!i2400m_tx_fits(i2400m)
742*4882a593Smuzhiyun || (is_singleton && i2400m->tx_msg->num_pls != 0))) {
743*4882a593Smuzhiyun d_printf(2, dev, "closing TX message (fits %u singleton "
744*4882a593Smuzhiyun "%u num_pls %u)\n", i2400m_tx_fits(i2400m),
745*4882a593Smuzhiyun is_singleton, i2400m->tx_msg->num_pls);
746*4882a593Smuzhiyun i2400m_tx_close(i2400m);
747*4882a593Smuzhiyun i2400m_tx_new(i2400m);
748*4882a593Smuzhiyun }
749*4882a593Smuzhiyun if (i2400m->tx_msg == NULL)
750*4882a593Smuzhiyun goto error_tx_new;
751*4882a593Smuzhiyun /*
752*4882a593Smuzhiyun * Check if this skb will fit in the TX queue's current active
753*4882a593Smuzhiyun * TX message. The total message size must not exceed the maximum
754*4882a593Smuzhiyun * size of each message I2400M_TX_MSG_SIZE. If it exceeds,
755*4882a593Smuzhiyun * close the current message and push this skb into the new message.
756*4882a593Smuzhiyun */
757*4882a593Smuzhiyun if (i2400m->tx_msg->size + padded_len > I2400M_TX_MSG_SIZE) {
758*4882a593Smuzhiyun d_printf(2, dev, "TX: message too big, going new\n");
759*4882a593Smuzhiyun i2400m_tx_close(i2400m);
760*4882a593Smuzhiyun i2400m_tx_new(i2400m);
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun if (i2400m->tx_msg == NULL)
763*4882a593Smuzhiyun goto error_tx_new;
764*4882a593Smuzhiyun /* So we have a current message header; now append space for
765*4882a593Smuzhiyun * the message -- if there is not enough, try the head */
766*4882a593Smuzhiyun ptr = i2400m_tx_fifo_push(i2400m, padded_len,
767*4882a593Smuzhiyun i2400m->bus_tx_block_size, try_head);
768*4882a593Smuzhiyun if (ptr == TAIL_FULL) { /* Tail is full, try head */
769*4882a593Smuzhiyun d_printf(2, dev, "pl append: tail full\n");
770*4882a593Smuzhiyun i2400m_tx_close(i2400m);
771*4882a593Smuzhiyun i2400m_tx_skip_tail(i2400m);
772*4882a593Smuzhiyun try_head = true;
773*4882a593Smuzhiyun goto try_new;
774*4882a593Smuzhiyun } else if (ptr == NULL) { /* All full */
775*4882a593Smuzhiyun result = -ENOSPC;
776*4882a593Smuzhiyun d_printf(2, dev, "pl append: all full\n");
777*4882a593Smuzhiyun } else { /* Got space, copy it, set padding */
778*4882a593Smuzhiyun struct i2400m_msg_hdr *tx_msg = i2400m->tx_msg;
779*4882a593Smuzhiyun unsigned num_pls = le16_to_cpu(tx_msg->num_pls);
780*4882a593Smuzhiyun memcpy(ptr, buf, buf_len);
781*4882a593Smuzhiyun memset(ptr + buf_len, 0xad, padded_len - buf_len);
782*4882a593Smuzhiyun i2400m_pld_set(&tx_msg->pld[num_pls], buf_len, pl_type);
783*4882a593Smuzhiyun d_printf(3, dev, "pld 0x%08x (type 0x%1x len 0x%04zx\n",
784*4882a593Smuzhiyun le32_to_cpu(tx_msg->pld[num_pls].val),
785*4882a593Smuzhiyun pl_type, buf_len);
786*4882a593Smuzhiyun tx_msg->num_pls = le16_to_cpu(num_pls+1);
787*4882a593Smuzhiyun tx_msg->size += padded_len;
788*4882a593Smuzhiyun d_printf(2, dev, "TX: appended %zu b (up to %u b) pl #%u\n",
789*4882a593Smuzhiyun padded_len, tx_msg->size, num_pls+1);
790*4882a593Smuzhiyun d_printf(2, dev,
791*4882a593Smuzhiyun "TX: appended hdr @%zu %zu b pl #%u @%zu %zu/%zu b\n",
792*4882a593Smuzhiyun (void *)tx_msg - i2400m->tx_buf, (size_t)tx_msg->size,
793*4882a593Smuzhiyun num_pls+1, ptr - i2400m->tx_buf, buf_len, padded_len);
794*4882a593Smuzhiyun result = 0;
795*4882a593Smuzhiyun if (is_singleton)
796*4882a593Smuzhiyun i2400m_tx_close(i2400m);
797*4882a593Smuzhiyun }
798*4882a593Smuzhiyun error_tx_new:
799*4882a593Smuzhiyun spin_unlock_irqrestore(&i2400m->tx_lock, flags);
800*4882a593Smuzhiyun /* kick in most cases, except when the TX subsys is down, as
801*4882a593Smuzhiyun * it might free space */
802*4882a593Smuzhiyun if (likely(result != -ESHUTDOWN))
803*4882a593Smuzhiyun i2400m->bus_tx_kick(i2400m);
804*4882a593Smuzhiyun d_fnend(3, dev, "(i2400m %p skb %p [%zu bytes] pt %u) = %d\n",
805*4882a593Smuzhiyun i2400m, buf, buf_len, pl_type, result);
806*4882a593Smuzhiyun return result;
807*4882a593Smuzhiyun }
808*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2400m_tx);
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun
811*4882a593Smuzhiyun /**
812*4882a593Smuzhiyun * i2400m_tx_msg_get - Get the first TX message in the FIFO to start sending it
813*4882a593Smuzhiyun *
814*4882a593Smuzhiyun * @i2400m: device descriptors
815*4882a593Smuzhiyun * @bus_size: where to place the size of the TX message
816*4882a593Smuzhiyun *
817*4882a593Smuzhiyun * Called by the bus-specific driver to get the first TX message at
818*4882a593Smuzhiyun * the FIF that is ready for transmission.
819*4882a593Smuzhiyun *
820*4882a593Smuzhiyun * It sets the state in @i2400m to indicate the bus-specific driver is
821*4882a593Smuzhiyun * transferring that message (i2400m->tx_msg_size).
822*4882a593Smuzhiyun *
823*4882a593Smuzhiyun * Once the transfer is completed, call i2400m_tx_msg_sent().
824*4882a593Smuzhiyun *
825*4882a593Smuzhiyun * Notes:
826*4882a593Smuzhiyun *
827*4882a593Smuzhiyun * The size of the TX message to be transmitted might be smaller than
828*4882a593Smuzhiyun * that of the TX message in the FIFO (in case the header was
829*4882a593Smuzhiyun * shorter). Hence, we copy it in @bus_size, for the bus layer to
830*4882a593Smuzhiyun * use. We keep the message's size in i2400m->tx_msg_size so that
831*4882a593Smuzhiyun * when the bus later is done transferring we know how much to
832*4882a593Smuzhiyun * advance the fifo.
833*4882a593Smuzhiyun *
834*4882a593Smuzhiyun * We collect statistics here as all the data is available and we
835*4882a593Smuzhiyun * assume it is going to work [see i2400m_tx_msg_sent()].
836*4882a593Smuzhiyun */
i2400m_tx_msg_get(struct i2400m * i2400m,size_t * bus_size)837*4882a593Smuzhiyun struct i2400m_msg_hdr *i2400m_tx_msg_get(struct i2400m *i2400m,
838*4882a593Smuzhiyun size_t *bus_size)
839*4882a593Smuzhiyun {
840*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
841*4882a593Smuzhiyun struct i2400m_msg_hdr *tx_msg, *tx_msg_moved;
842*4882a593Smuzhiyun unsigned long flags, pls;
843*4882a593Smuzhiyun
844*4882a593Smuzhiyun d_fnstart(3, dev, "(i2400m %p bus_size %p)\n", i2400m, bus_size);
845*4882a593Smuzhiyun spin_lock_irqsave(&i2400m->tx_lock, flags);
846*4882a593Smuzhiyun tx_msg_moved = NULL;
847*4882a593Smuzhiyun if (i2400m->tx_buf == NULL)
848*4882a593Smuzhiyun goto out_unlock;
849*4882a593Smuzhiyun skip:
850*4882a593Smuzhiyun tx_msg_moved = NULL;
851*4882a593Smuzhiyun if (i2400m->tx_in == i2400m->tx_out) { /* Empty FIFO? */
852*4882a593Smuzhiyun i2400m->tx_in = 0;
853*4882a593Smuzhiyun i2400m->tx_out = 0;
854*4882a593Smuzhiyun d_printf(2, dev, "TX: FIFO empty: resetting\n");
855*4882a593Smuzhiyun goto out_unlock;
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun tx_msg = i2400m->tx_buf + i2400m->tx_out % I2400M_TX_BUF_SIZE;
858*4882a593Smuzhiyun if (tx_msg->size & I2400M_TX_SKIP) { /* skip? */
859*4882a593Smuzhiyun d_printf(2, dev, "TX: skip: msg @%zu (%zu b)\n",
860*4882a593Smuzhiyun i2400m->tx_out % I2400M_TX_BUF_SIZE,
861*4882a593Smuzhiyun (size_t) tx_msg->size & ~I2400M_TX_SKIP);
862*4882a593Smuzhiyun i2400m->tx_out += tx_msg->size & ~I2400M_TX_SKIP;
863*4882a593Smuzhiyun goto skip;
864*4882a593Smuzhiyun }
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun if (tx_msg->num_pls == 0) { /* No payloads? */
867*4882a593Smuzhiyun if (tx_msg == i2400m->tx_msg) { /* open, we are done */
868*4882a593Smuzhiyun d_printf(2, dev,
869*4882a593Smuzhiyun "TX: FIFO empty: open msg w/o payloads @%zu\n",
870*4882a593Smuzhiyun (void *) tx_msg - i2400m->tx_buf);
871*4882a593Smuzhiyun tx_msg = NULL;
872*4882a593Smuzhiyun goto out_unlock;
873*4882a593Smuzhiyun } else { /* closed, skip it */
874*4882a593Smuzhiyun d_printf(2, dev,
875*4882a593Smuzhiyun "TX: skip msg w/o payloads @%zu (%zu b)\n",
876*4882a593Smuzhiyun (void *) tx_msg - i2400m->tx_buf,
877*4882a593Smuzhiyun (size_t) tx_msg->size);
878*4882a593Smuzhiyun i2400m->tx_out += tx_msg->size & ~I2400M_TX_SKIP;
879*4882a593Smuzhiyun goto skip;
880*4882a593Smuzhiyun }
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun if (tx_msg == i2400m->tx_msg) /* open msg? */
883*4882a593Smuzhiyun i2400m_tx_close(i2400m);
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun /* Now we have a valid TX message (with payloads) to TX */
886*4882a593Smuzhiyun tx_msg_moved = (void *) tx_msg + tx_msg->offset;
887*4882a593Smuzhiyun i2400m->tx_msg_size = tx_msg->size;
888*4882a593Smuzhiyun *bus_size = tx_msg_moved->size;
889*4882a593Smuzhiyun d_printf(2, dev, "TX: pid %d msg hdr at @%zu offset +@%zu "
890*4882a593Smuzhiyun "size %zu bus_size %zu\n",
891*4882a593Smuzhiyun current->pid, (void *) tx_msg - i2400m->tx_buf,
892*4882a593Smuzhiyun (size_t) tx_msg->offset, (size_t) tx_msg->size,
893*4882a593Smuzhiyun (size_t) tx_msg_moved->size);
894*4882a593Smuzhiyun tx_msg_moved->barker = le32_to_cpu(I2400M_H2D_PREVIEW_BARKER);
895*4882a593Smuzhiyun tx_msg_moved->sequence = le32_to_cpu(i2400m->tx_sequence++);
896*4882a593Smuzhiyun
897*4882a593Smuzhiyun pls = le32_to_cpu(tx_msg_moved->num_pls);
898*4882a593Smuzhiyun i2400m->tx_pl_num += pls; /* Update stats */
899*4882a593Smuzhiyun if (pls > i2400m->tx_pl_max)
900*4882a593Smuzhiyun i2400m->tx_pl_max = pls;
901*4882a593Smuzhiyun if (pls < i2400m->tx_pl_min)
902*4882a593Smuzhiyun i2400m->tx_pl_min = pls;
903*4882a593Smuzhiyun i2400m->tx_num++;
904*4882a593Smuzhiyun i2400m->tx_size_acc += *bus_size;
905*4882a593Smuzhiyun if (*bus_size < i2400m->tx_size_min)
906*4882a593Smuzhiyun i2400m->tx_size_min = *bus_size;
907*4882a593Smuzhiyun if (*bus_size > i2400m->tx_size_max)
908*4882a593Smuzhiyun i2400m->tx_size_max = *bus_size;
909*4882a593Smuzhiyun out_unlock:
910*4882a593Smuzhiyun spin_unlock_irqrestore(&i2400m->tx_lock, flags);
911*4882a593Smuzhiyun d_fnstart(3, dev, "(i2400m %p bus_size %p [%zu]) = %p\n",
912*4882a593Smuzhiyun i2400m, bus_size, *bus_size, tx_msg_moved);
913*4882a593Smuzhiyun return tx_msg_moved;
914*4882a593Smuzhiyun }
915*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2400m_tx_msg_get);
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun
918*4882a593Smuzhiyun /**
919*4882a593Smuzhiyun * i2400m_tx_msg_sent - indicate the transmission of a TX message
920*4882a593Smuzhiyun *
921*4882a593Smuzhiyun * @i2400m: device descriptor
922*4882a593Smuzhiyun *
923*4882a593Smuzhiyun * Called by the bus-specific driver when a message has been sent;
924*4882a593Smuzhiyun * this pops it from the FIFO; and as there is space, start the queue
925*4882a593Smuzhiyun * in case it was stopped.
926*4882a593Smuzhiyun *
927*4882a593Smuzhiyun * Should be called even if the message send failed and we are
928*4882a593Smuzhiyun * dropping this TX message.
929*4882a593Smuzhiyun */
i2400m_tx_msg_sent(struct i2400m * i2400m)930*4882a593Smuzhiyun void i2400m_tx_msg_sent(struct i2400m *i2400m)
931*4882a593Smuzhiyun {
932*4882a593Smuzhiyun unsigned n;
933*4882a593Smuzhiyun unsigned long flags;
934*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
935*4882a593Smuzhiyun
936*4882a593Smuzhiyun d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
937*4882a593Smuzhiyun spin_lock_irqsave(&i2400m->tx_lock, flags);
938*4882a593Smuzhiyun if (i2400m->tx_buf == NULL)
939*4882a593Smuzhiyun goto out_unlock;
940*4882a593Smuzhiyun i2400m->tx_out += i2400m->tx_msg_size;
941*4882a593Smuzhiyun d_printf(2, dev, "TX: sent %zu b\n", (size_t) i2400m->tx_msg_size);
942*4882a593Smuzhiyun i2400m->tx_msg_size = 0;
943*4882a593Smuzhiyun BUG_ON(i2400m->tx_out > i2400m->tx_in);
944*4882a593Smuzhiyun /* level them FIFO markers off */
945*4882a593Smuzhiyun n = i2400m->tx_out / I2400M_TX_BUF_SIZE;
946*4882a593Smuzhiyun i2400m->tx_out %= I2400M_TX_BUF_SIZE;
947*4882a593Smuzhiyun i2400m->tx_in -= n * I2400M_TX_BUF_SIZE;
948*4882a593Smuzhiyun out_unlock:
949*4882a593Smuzhiyun spin_unlock_irqrestore(&i2400m->tx_lock, flags);
950*4882a593Smuzhiyun d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
951*4882a593Smuzhiyun }
952*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2400m_tx_msg_sent);
953*4882a593Smuzhiyun
954*4882a593Smuzhiyun
955*4882a593Smuzhiyun /**
956*4882a593Smuzhiyun * i2400m_tx_setup - Initialize the TX queue and infrastructure
957*4882a593Smuzhiyun *
958*4882a593Smuzhiyun * Make sure we reset the TX sequence to zero, as when this function
959*4882a593Smuzhiyun * is called, the firmware has been just restarted. Same rational
960*4882a593Smuzhiyun * for tx_in, tx_out, tx_msg_size and tx_msg. We reset them since
961*4882a593Smuzhiyun * the memory for TX queue is reallocated.
962*4882a593Smuzhiyun */
i2400m_tx_setup(struct i2400m * i2400m)963*4882a593Smuzhiyun int i2400m_tx_setup(struct i2400m *i2400m)
964*4882a593Smuzhiyun {
965*4882a593Smuzhiyun int result = 0;
966*4882a593Smuzhiyun void *tx_buf;
967*4882a593Smuzhiyun unsigned long flags;
968*4882a593Smuzhiyun
969*4882a593Smuzhiyun /* Do this here only once -- can't do on
970*4882a593Smuzhiyun * i2400m_hard_start_xmit() as we'll cause race conditions if
971*4882a593Smuzhiyun * the WS was scheduled on another CPU */
972*4882a593Smuzhiyun INIT_WORK(&i2400m->wake_tx_ws, i2400m_wake_tx_work);
973*4882a593Smuzhiyun
974*4882a593Smuzhiyun tx_buf = kmalloc(I2400M_TX_BUF_SIZE, GFP_ATOMIC);
975*4882a593Smuzhiyun if (tx_buf == NULL) {
976*4882a593Smuzhiyun result = -ENOMEM;
977*4882a593Smuzhiyun goto error_kmalloc;
978*4882a593Smuzhiyun }
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun /*
981*4882a593Smuzhiyun * Fail the build if we can't fit at least two maximum size messages
982*4882a593Smuzhiyun * on the TX FIFO [one being delivered while one is constructed].
983*4882a593Smuzhiyun */
984*4882a593Smuzhiyun BUILD_BUG_ON(2 * I2400M_TX_MSG_SIZE > I2400M_TX_BUF_SIZE);
985*4882a593Smuzhiyun spin_lock_irqsave(&i2400m->tx_lock, flags);
986*4882a593Smuzhiyun i2400m->tx_sequence = 0;
987*4882a593Smuzhiyun i2400m->tx_in = 0;
988*4882a593Smuzhiyun i2400m->tx_out = 0;
989*4882a593Smuzhiyun i2400m->tx_msg_size = 0;
990*4882a593Smuzhiyun i2400m->tx_msg = NULL;
991*4882a593Smuzhiyun i2400m->tx_buf = tx_buf;
992*4882a593Smuzhiyun spin_unlock_irqrestore(&i2400m->tx_lock, flags);
993*4882a593Smuzhiyun /* Huh? the bus layer has to define this... */
994*4882a593Smuzhiyun BUG_ON(i2400m->bus_tx_block_size == 0);
995*4882a593Smuzhiyun error_kmalloc:
996*4882a593Smuzhiyun return result;
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun }
999*4882a593Smuzhiyun
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun /**
1002*4882a593Smuzhiyun * i2400m_tx_release - Tear down the TX queue and infrastructure
1003*4882a593Smuzhiyun */
i2400m_tx_release(struct i2400m * i2400m)1004*4882a593Smuzhiyun void i2400m_tx_release(struct i2400m *i2400m)
1005*4882a593Smuzhiyun {
1006*4882a593Smuzhiyun unsigned long flags;
1007*4882a593Smuzhiyun spin_lock_irqsave(&i2400m->tx_lock, flags);
1008*4882a593Smuzhiyun kfree(i2400m->tx_buf);
1009*4882a593Smuzhiyun i2400m->tx_buf = NULL;
1010*4882a593Smuzhiyun spin_unlock_irqrestore(&i2400m->tx_lock, flags);
1011*4882a593Smuzhiyun }
1012