tx.c 37 KB

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