tx.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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. * 16 byte aligned MAX_MTU + 4 byte payload prefix.
  289. */
  290. I2400M_MAX_MTU_ALIGN = 16,
  291. I2400M_TX_PDU_SIZE = I2400M_MAX_MTU % I2400M_MAX_MTU_ALIGN
  292. + I2400M_MAX_MTU + sizeof(struct i2400m_pl_data_hdr),
  293. /*
  294. * 256 byte aligned toal size of 12 PDUs including msg header,
  295. */
  296. I2400M_TX_PDU_ALIGN = 256,
  297. I2400M_TX_PDU_TOTAL_SIZE = ((I2400M_TX_PDU_SIZE * I2400M_TX_PLD_MAX
  298. + sizeof(struct i2400m_msg_hdr))/I2400M_TX_PDU_ALIGN + 1)
  299. * I2400M_TX_PDU_ALIGN * 2,
  300. };
  301. #define TAIL_FULL ((void *)~(unsigned long)NULL)
  302. /*
  303. * Calculate how much tail room is available
  304. *
  305. * Note the trick here. This path is ONLY caleed for Case A (see
  306. * i2400m_tx_fifo_push() below), where we have:
  307. *
  308. * Case A
  309. * N ___________
  310. * | tail room |
  311. * | |
  312. * |<- IN ->|
  313. * | |
  314. * | data |
  315. * | |
  316. * |<- OUT ->|
  317. * | |
  318. * | head room |
  319. * 0 -----------
  320. *
  321. * When calculating the tail_room, tx_in might get to be zero if
  322. * i2400m->tx_in is right at the end of the buffer (really full
  323. * buffer) if there is no head room. In this case, tail_room would be
  324. * I2400M_TX_BUF_SIZE, although it is actually zero. Hence the final
  325. * mod (%) operation. However, when doing this kind of optimization,
  326. * i2400m->tx_in being zero would fail, so we treat is an a special
  327. * case.
  328. */
  329. static inline
  330. size_t __i2400m_tx_tail_room(struct i2400m *i2400m)
  331. {
  332. size_t tail_room;
  333. size_t tx_in;
  334. if (unlikely(i2400m->tx_in == 0))
  335. return I2400M_TX_BUF_SIZE;
  336. tx_in = i2400m->tx_in % I2400M_TX_BUF_SIZE;
  337. tail_room = I2400M_TX_BUF_SIZE - tx_in;
  338. tail_room %= I2400M_TX_BUF_SIZE;
  339. return tail_room;
  340. }
  341. /*
  342. * Allocate @size bytes in the TX fifo, return a pointer to it
  343. *
  344. * @i2400m: device descriptor
  345. * @size: size of the buffer we need to allocate
  346. * @padding: ensure that there is at least this many bytes of free
  347. * contiguous space in the fifo. This is needed because later on
  348. * we might need to add padding.
  349. *
  350. * Returns:
  351. *
  352. * Pointer to the allocated space. NULL if there is no
  353. * space. TAIL_FULL if there is no space at the tail but there is at
  354. * the head (Case B below).
  355. *
  356. * These are the two basic cases we need to keep an eye for -- it is
  357. * much better explained in linux/kernel/kfifo.c, but this code
  358. * basically does the same. No rocket science here.
  359. *
  360. * Case A Case B
  361. * N ___________ ___________
  362. * | tail room | | data |
  363. * | | | |
  364. * |<- IN ->| |<- OUT ->|
  365. * | | | |
  366. * | data | | room |
  367. * | | | |
  368. * |<- OUT ->| |<- IN ->|
  369. * | | | |
  370. * | head room | | data |
  371. * 0 ----------- -----------
  372. *
  373. * We allocate only *contiguous* space.
  374. *
  375. * We can allocate only from 'room'. In Case B, it is simple; in case
  376. * A, we only try from the tail room; if it is not enough, we just
  377. * fail and return TAIL_FULL and let the caller figure out if we wants to
  378. * skip the tail room and try to allocate from the head.
  379. *
  380. * Note:
  381. *
  382. * Assumes i2400m->tx_lock is taken, and we use that as a barrier
  383. *
  384. * The indexes keep increasing and we reset them to zero when we
  385. * pop data off the queue
  386. */
  387. static
  388. void *i2400m_tx_fifo_push(struct i2400m *i2400m, size_t size, size_t padding)
  389. {
  390. struct device *dev = i2400m_dev(i2400m);
  391. size_t room, tail_room, needed_size;
  392. void *ptr;
  393. needed_size = size + padding;
  394. room = I2400M_TX_BUF_SIZE - (i2400m->tx_in - i2400m->tx_out);
  395. if (room < needed_size) { /* this takes care of Case B */
  396. d_printf(2, dev, "fifo push %zu/%zu: no space\n",
  397. size, padding);
  398. return NULL;
  399. }
  400. /* Is there space at the tail? */
  401. tail_room = __i2400m_tx_tail_room(i2400m);
  402. if (tail_room < needed_size) {
  403. if (i2400m->tx_out % I2400M_TX_BUF_SIZE
  404. < i2400m->tx_in % I2400M_TX_BUF_SIZE) {
  405. d_printf(2, dev, "fifo push %zu/%zu: tail full\n",
  406. size, padding);
  407. return TAIL_FULL; /* There might be head space */
  408. } else {
  409. d_printf(2, dev, "fifo push %zu/%zu: no head space\n",
  410. size, padding);
  411. return NULL; /* There is no space */
  412. }
  413. }
  414. ptr = i2400m->tx_buf + i2400m->tx_in % I2400M_TX_BUF_SIZE;
  415. d_printf(2, dev, "fifo push %zu/%zu: at @%zu\n", size, padding,
  416. i2400m->tx_in % I2400M_TX_BUF_SIZE);
  417. i2400m->tx_in += size;
  418. return ptr;
  419. }
  420. /*
  421. * Mark the tail of the FIFO buffer as 'to-skip'
  422. *
  423. * We should never hit the BUG_ON() because all the sizes we push to
  424. * the FIFO are padded to be a multiple of 16 -- the size of *msg
  425. * (I2400M_PL_PAD for the payloads, I2400M_TX_PLD_SIZE for the
  426. * header).
  427. *
  428. * Tail room can get to be zero if a message was opened when there was
  429. * space only for a header. _tx_close() will mark it as to-skip (as it
  430. * will have no payloads) and there will be no more space to flush, so
  431. * nothing has to be done here. This is probably cheaper than ensuring
  432. * in _tx_new() that there is some space for payloads...as we could
  433. * always possibly hit the same problem if the payload wouldn't fit.
  434. *
  435. * Note:
  436. *
  437. * Assumes i2400m->tx_lock is taken, and we use that as a barrier
  438. *
  439. * This path is only taken for Case A FIFO situations [see
  440. * i2400m_tx_fifo_push()]
  441. */
  442. static
  443. void i2400m_tx_skip_tail(struct i2400m *i2400m)
  444. {
  445. struct device *dev = i2400m_dev(i2400m);
  446. size_t tx_in = i2400m->tx_in % I2400M_TX_BUF_SIZE;
  447. size_t tail_room = __i2400m_tx_tail_room(i2400m);
  448. struct i2400m_msg_hdr *msg = i2400m->tx_buf + tx_in;
  449. if (unlikely(tail_room == 0))
  450. return;
  451. BUG_ON(tail_room < sizeof(*msg));
  452. msg->size = tail_room | I2400M_TX_SKIP;
  453. d_printf(2, dev, "skip tail: skipping %zu bytes @%zu\n",
  454. tail_room, tx_in);
  455. i2400m->tx_in += tail_room;
  456. }
  457. /*
  458. * Check if a skb will fit in the TX queue's current active TX
  459. * message (if there are still descriptors left unused).
  460. *
  461. * Returns:
  462. * 0 if the message won't fit, 1 if it will.
  463. *
  464. * Note:
  465. *
  466. * Assumes a TX message is active (i2400m->tx_msg).
  467. *
  468. * Assumes i2400m->tx_lock is taken, and we use that as a barrier
  469. */
  470. static
  471. unsigned i2400m_tx_fits(struct i2400m *i2400m)
  472. {
  473. struct i2400m_msg_hdr *msg_hdr = i2400m->tx_msg;
  474. return le16_to_cpu(msg_hdr->num_pls) < I2400M_TX_PLD_MAX;
  475. }
  476. /*
  477. * Start a new TX message header in the queue.
  478. *
  479. * Reserve memory from the base FIFO engine and then just initialize
  480. * the message header.
  481. *
  482. * We allocate the biggest TX message header we might need (one that'd
  483. * fit I2400M_TX_PLD_MAX payloads) -- when it is closed it will be
  484. * 'ironed it out' and the unneeded parts removed.
  485. *
  486. * NOTE:
  487. *
  488. * Assumes that the previous message is CLOSED (eg: either
  489. * there was none or 'i2400m_tx_close()' was called on it).
  490. *
  491. * Assumes i2400m->tx_lock is taken, and we use that as a barrier
  492. */
  493. static
  494. void i2400m_tx_new(struct i2400m *i2400m)
  495. {
  496. struct device *dev = i2400m_dev(i2400m);
  497. struct i2400m_msg_hdr *tx_msg;
  498. BUG_ON(i2400m->tx_msg != NULL);
  499. try_head:
  500. tx_msg = i2400m_tx_fifo_push(i2400m, I2400M_TX_PLD_SIZE, 0);
  501. if (tx_msg == NULL)
  502. goto out;
  503. else if (tx_msg == TAIL_FULL) {
  504. i2400m_tx_skip_tail(i2400m);
  505. d_printf(2, dev, "new TX message: tail full, trying head\n");
  506. goto try_head;
  507. }
  508. memset(tx_msg, 0, I2400M_TX_PLD_SIZE);
  509. tx_msg->size = I2400M_TX_PLD_SIZE;
  510. out:
  511. i2400m->tx_msg = tx_msg;
  512. d_printf(2, dev, "new TX message: %p @%zu\n",
  513. tx_msg, (void *) tx_msg - i2400m->tx_buf);
  514. }
  515. /*
  516. * Finalize the current TX message header
  517. *
  518. * Sets the message header to be at the proper location depending on
  519. * how many descriptors we have (check documentation at the file's
  520. * header for more info on that).
  521. *
  522. * Appends padding bytes to make sure the whole TX message (counting
  523. * from the 'relocated' message header) is aligned to
  524. * tx_block_size. We assume the _append() code has left enough space
  525. * in the FIFO for that. If there are no payloads, just pass, as it
  526. * won't be transferred.
  527. *
  528. * The amount of padding bytes depends on how many payloads are in the
  529. * TX message, as the "msg header and payload descriptors" will be
  530. * shifted up in the buffer.
  531. */
  532. static
  533. void i2400m_tx_close(struct i2400m *i2400m)
  534. {
  535. struct device *dev = i2400m_dev(i2400m);
  536. struct i2400m_msg_hdr *tx_msg = i2400m->tx_msg;
  537. struct i2400m_msg_hdr *tx_msg_moved;
  538. size_t aligned_size, padding, hdr_size;
  539. void *pad_buf;
  540. unsigned num_pls;
  541. if (tx_msg->size & I2400M_TX_SKIP) /* a skipper? nothing to do */
  542. goto out;
  543. num_pls = le16_to_cpu(tx_msg->num_pls);
  544. /* We can get this situation when a new message was started
  545. * and there was no space to add payloads before hitting the
  546. tail (and taking padding into consideration). */
  547. if (num_pls == 0) {
  548. tx_msg->size |= I2400M_TX_SKIP;
  549. goto out;
  550. }
  551. /* Relocate the message header
  552. *
  553. * Find the current header size, align it to 16 and if we need
  554. * to move it so the tail is next to the payloads, move it and
  555. * set the offset.
  556. *
  557. * If it moved, this header is good only for transmission; the
  558. * original one (it is kept if we moved) is still used to
  559. * figure out where the next TX message starts (and where the
  560. * offset to the moved header is).
  561. */
  562. hdr_size = sizeof(*tx_msg)
  563. + le16_to_cpu(tx_msg->num_pls) * sizeof(tx_msg->pld[0]);
  564. hdr_size = ALIGN(hdr_size, I2400M_PL_ALIGN);
  565. tx_msg->offset = I2400M_TX_PLD_SIZE - hdr_size;
  566. tx_msg_moved = (void *) tx_msg + tx_msg->offset;
  567. memmove(tx_msg_moved, tx_msg, hdr_size);
  568. tx_msg_moved->size -= tx_msg->offset;
  569. /*
  570. * Now figure out how much we have to add to the (moved!)
  571. * message so the size is a multiple of i2400m->bus_tx_block_size.
  572. */
  573. aligned_size = ALIGN(tx_msg_moved->size, i2400m->bus_tx_block_size);
  574. padding = aligned_size - tx_msg_moved->size;
  575. if (padding > 0) {
  576. pad_buf = i2400m_tx_fifo_push(i2400m, padding, 0);
  577. if (unlikely(WARN_ON(pad_buf == NULL
  578. || pad_buf == TAIL_FULL))) {
  579. /* This should not happen -- append should verify
  580. * there is always space left at least to append
  581. * tx_block_size */
  582. dev_err(dev,
  583. "SW BUG! Possible data leakage from memory the "
  584. "device should not read for padding - "
  585. "size %lu aligned_size %zu tx_buf %p in "
  586. "%zu out %zu\n",
  587. (unsigned long) tx_msg_moved->size,
  588. aligned_size, i2400m->tx_buf, i2400m->tx_in,
  589. i2400m->tx_out);
  590. } else
  591. memset(pad_buf, 0xad, padding);
  592. }
  593. tx_msg_moved->padding = cpu_to_le16(padding);
  594. tx_msg_moved->size += padding;
  595. if (tx_msg != tx_msg_moved)
  596. tx_msg->size += padding;
  597. out:
  598. i2400m->tx_msg = NULL;
  599. }
  600. /**
  601. * i2400m_tx - send the data in a buffer to the device
  602. *
  603. * @buf: pointer to the buffer to transmit
  604. *
  605. * @buf_len: buffer size
  606. *
  607. * @pl_type: type of the payload we are sending.
  608. *
  609. * Returns:
  610. * 0 if ok, < 0 errno code on error (-ENOSPC, if there is no more
  611. * room for the message in the queue).
  612. *
  613. * Appends the buffer to the TX FIFO and notifies the bus-specific
  614. * part of the driver that there is new data ready to transmit.
  615. * Once this function returns, the buffer has been copied, so it can
  616. * be reused.
  617. *
  618. * The steps followed to append are explained in detail in the file
  619. * header.
  620. *
  621. * Whenever we write to a message, we increase msg->size, so it
  622. * reflects exactly how big the message is. This is needed so that if
  623. * we concatenate two messages before they can be sent, the code that
  624. * sends the messages can find the boundaries (and it will replace the
  625. * size with the real barker before sending).
  626. *
  627. * Note:
  628. *
  629. * Cold and warm reset payloads need to be sent as a single
  630. * payload, so we handle that.
  631. */
  632. int i2400m_tx(struct i2400m *i2400m, const void *buf, size_t buf_len,
  633. enum i2400m_pt pl_type)
  634. {
  635. int result = -ENOSPC;
  636. struct device *dev = i2400m_dev(i2400m);
  637. unsigned long flags;
  638. size_t padded_len;
  639. void *ptr;
  640. unsigned is_singleton = pl_type == I2400M_PT_RESET_WARM
  641. || pl_type == I2400M_PT_RESET_COLD;
  642. d_fnstart(3, dev, "(i2400m %p skb %p [%zu bytes] pt %u)\n",
  643. i2400m, buf, buf_len, pl_type);
  644. padded_len = ALIGN(buf_len, I2400M_PL_ALIGN);
  645. d_printf(5, dev, "padded_len %zd buf_len %zd\n", padded_len, buf_len);
  646. /* If there is no current TX message, create one; if the
  647. * current one is out of payload slots or we have a singleton,
  648. * close it and start a new one */
  649. spin_lock_irqsave(&i2400m->tx_lock, flags);
  650. /* If tx_buf is NULL, device is shutdown */
  651. if (i2400m->tx_buf == NULL) {
  652. result = -ESHUTDOWN;
  653. goto error_tx_new;
  654. }
  655. try_new:
  656. if (unlikely(i2400m->tx_msg == NULL))
  657. i2400m_tx_new(i2400m);
  658. else if (unlikely(!i2400m_tx_fits(i2400m)
  659. || (is_singleton && i2400m->tx_msg->num_pls != 0))) {
  660. d_printf(2, dev, "closing TX message (fits %u singleton "
  661. "%u num_pls %u)\n", i2400m_tx_fits(i2400m),
  662. is_singleton, i2400m->tx_msg->num_pls);
  663. i2400m_tx_close(i2400m);
  664. i2400m_tx_new(i2400m);
  665. }
  666. if (i2400m->tx_msg == NULL)
  667. goto error_tx_new;
  668. /*
  669. * Check if this skb will fit in the TX queue's current active
  670. * TX message. The total message size must not exceed the maximum
  671. * size of each message I2400M_TX_MSG_SIZE. If it exceeds,
  672. * close the current message and push this skb into the new message.
  673. */
  674. if (i2400m->tx_msg->size + padded_len > I2400M_TX_MSG_SIZE) {
  675. d_printf(2, dev, "TX: message too big, going new\n");
  676. i2400m_tx_close(i2400m);
  677. i2400m_tx_new(i2400m);
  678. }
  679. if (i2400m->tx_msg == NULL)
  680. goto error_tx_new;
  681. /* So we have a current message header; now append space for
  682. * the message -- if there is not enough, try the head */
  683. ptr = i2400m_tx_fifo_push(i2400m, padded_len,
  684. i2400m->bus_tx_block_size);
  685. if (ptr == TAIL_FULL) { /* Tail is full, try head */
  686. d_printf(2, dev, "pl append: tail full\n");
  687. i2400m_tx_close(i2400m);
  688. i2400m_tx_skip_tail(i2400m);
  689. goto try_new;
  690. } else if (ptr == NULL) { /* All full */
  691. result = -ENOSPC;
  692. d_printf(2, dev, "pl append: all full\n");
  693. } else { /* Got space, copy it, set padding */
  694. struct i2400m_msg_hdr *tx_msg = i2400m->tx_msg;
  695. unsigned num_pls = le16_to_cpu(tx_msg->num_pls);
  696. memcpy(ptr, buf, buf_len);
  697. memset(ptr + buf_len, 0xad, padded_len - buf_len);
  698. i2400m_pld_set(&tx_msg->pld[num_pls], buf_len, pl_type);
  699. d_printf(3, dev, "pld 0x%08x (type 0x%1x len 0x%04zx\n",
  700. le32_to_cpu(tx_msg->pld[num_pls].val),
  701. pl_type, buf_len);
  702. tx_msg->num_pls = le16_to_cpu(num_pls+1);
  703. tx_msg->size += padded_len;
  704. d_printf(2, dev, "TX: appended %zu b (up to %u b) pl #%u\n",
  705. padded_len, tx_msg->size, num_pls+1);
  706. d_printf(2, dev,
  707. "TX: appended hdr @%zu %zu b pl #%u @%zu %zu/%zu b\n",
  708. (void *)tx_msg - i2400m->tx_buf, (size_t)tx_msg->size,
  709. num_pls+1, ptr - i2400m->tx_buf, buf_len, padded_len);
  710. result = 0;
  711. if (is_singleton)
  712. i2400m_tx_close(i2400m);
  713. }
  714. error_tx_new:
  715. spin_unlock_irqrestore(&i2400m->tx_lock, flags);
  716. /* kick in most cases, except when the TX subsys is down, as
  717. * it might free space */
  718. if (likely(result != -ESHUTDOWN))
  719. i2400m->bus_tx_kick(i2400m);
  720. d_fnend(3, dev, "(i2400m %p skb %p [%zu bytes] pt %u) = %d\n",
  721. i2400m, buf, buf_len, pl_type, result);
  722. return result;
  723. }
  724. EXPORT_SYMBOL_GPL(i2400m_tx);
  725. /**
  726. * i2400m_tx_msg_get - Get the first TX message in the FIFO to start sending it
  727. *
  728. * @i2400m: device descriptors
  729. * @bus_size: where to place the size of the TX message
  730. *
  731. * Called by the bus-specific driver to get the first TX message at
  732. * the FIF that is ready for transmission.
  733. *
  734. * It sets the state in @i2400m to indicate the bus-specific driver is
  735. * transfering that message (i2400m->tx_msg_size).
  736. *
  737. * Once the transfer is completed, call i2400m_tx_msg_sent().
  738. *
  739. * Notes:
  740. *
  741. * The size of the TX message to be transmitted might be smaller than
  742. * that of the TX message in the FIFO (in case the header was
  743. * shorter). Hence, we copy it in @bus_size, for the bus layer to
  744. * use. We keep the message's size in i2400m->tx_msg_size so that
  745. * when the bus later is done transferring we know how much to
  746. * advance the fifo.
  747. *
  748. * We collect statistics here as all the data is available and we
  749. * assume it is going to work [see i2400m_tx_msg_sent()].
  750. */
  751. struct i2400m_msg_hdr *i2400m_tx_msg_get(struct i2400m *i2400m,
  752. size_t *bus_size)
  753. {
  754. struct device *dev = i2400m_dev(i2400m);
  755. struct i2400m_msg_hdr *tx_msg, *tx_msg_moved;
  756. unsigned long flags, pls;
  757. d_fnstart(3, dev, "(i2400m %p bus_size %p)\n", i2400m, bus_size);
  758. spin_lock_irqsave(&i2400m->tx_lock, flags);
  759. tx_msg_moved = NULL;
  760. if (i2400m->tx_buf == NULL)
  761. goto out_unlock;
  762. skip:
  763. tx_msg_moved = NULL;
  764. if (i2400m->tx_in == i2400m->tx_out) { /* Empty FIFO? */
  765. i2400m->tx_in = 0;
  766. i2400m->tx_out = 0;
  767. d_printf(2, dev, "TX: FIFO empty: resetting\n");
  768. goto out_unlock;
  769. }
  770. tx_msg = i2400m->tx_buf + i2400m->tx_out % I2400M_TX_BUF_SIZE;
  771. if (tx_msg->size & I2400M_TX_SKIP) { /* skip? */
  772. d_printf(2, dev, "TX: skip: msg @%zu (%zu b)\n",
  773. i2400m->tx_out % I2400M_TX_BUF_SIZE,
  774. (size_t) tx_msg->size & ~I2400M_TX_SKIP);
  775. i2400m->tx_out += tx_msg->size & ~I2400M_TX_SKIP;
  776. goto skip;
  777. }
  778. if (tx_msg->num_pls == 0) { /* No payloads? */
  779. if (tx_msg == i2400m->tx_msg) { /* open, we are done */
  780. d_printf(2, dev,
  781. "TX: FIFO empty: open msg w/o payloads @%zu\n",
  782. (void *) tx_msg - i2400m->tx_buf);
  783. tx_msg = NULL;
  784. goto out_unlock;
  785. } else { /* closed, skip it */
  786. d_printf(2, dev,
  787. "TX: skip msg w/o payloads @%zu (%zu b)\n",
  788. (void *) tx_msg - i2400m->tx_buf,
  789. (size_t) tx_msg->size);
  790. i2400m->tx_out += tx_msg->size & ~I2400M_TX_SKIP;
  791. goto skip;
  792. }
  793. }
  794. if (tx_msg == i2400m->tx_msg) /* open msg? */
  795. i2400m_tx_close(i2400m);
  796. /* Now we have a valid TX message (with payloads) to TX */
  797. tx_msg_moved = (void *) tx_msg + tx_msg->offset;
  798. i2400m->tx_msg_size = tx_msg->size;
  799. *bus_size = tx_msg_moved->size;
  800. d_printf(2, dev, "TX: pid %d msg hdr at @%zu offset +@%zu "
  801. "size %zu bus_size %zu\n",
  802. current->pid, (void *) tx_msg - i2400m->tx_buf,
  803. (size_t) tx_msg->offset, (size_t) tx_msg->size,
  804. (size_t) tx_msg_moved->size);
  805. tx_msg_moved->barker = le32_to_cpu(I2400M_H2D_PREVIEW_BARKER);
  806. tx_msg_moved->sequence = le32_to_cpu(i2400m->tx_sequence++);
  807. pls = le32_to_cpu(tx_msg_moved->num_pls);
  808. i2400m->tx_pl_num += pls; /* Update stats */
  809. if (pls > i2400m->tx_pl_max)
  810. i2400m->tx_pl_max = pls;
  811. if (pls < i2400m->tx_pl_min)
  812. i2400m->tx_pl_min = pls;
  813. i2400m->tx_num++;
  814. i2400m->tx_size_acc += *bus_size;
  815. if (*bus_size < i2400m->tx_size_min)
  816. i2400m->tx_size_min = *bus_size;
  817. if (*bus_size > i2400m->tx_size_max)
  818. i2400m->tx_size_max = *bus_size;
  819. out_unlock:
  820. spin_unlock_irqrestore(&i2400m->tx_lock, flags);
  821. d_fnstart(3, dev, "(i2400m %p bus_size %p [%zu]) = %p\n",
  822. i2400m, bus_size, *bus_size, tx_msg_moved);
  823. return tx_msg_moved;
  824. }
  825. EXPORT_SYMBOL_GPL(i2400m_tx_msg_get);
  826. /**
  827. * i2400m_tx_msg_sent - indicate the transmission of a TX message
  828. *
  829. * @i2400m: device descriptor
  830. *
  831. * Called by the bus-specific driver when a message has been sent;
  832. * this pops it from the FIFO; and as there is space, start the queue
  833. * in case it was stopped.
  834. *
  835. * Should be called even if the message send failed and we are
  836. * dropping this TX message.
  837. */
  838. void i2400m_tx_msg_sent(struct i2400m *i2400m)
  839. {
  840. unsigned n;
  841. unsigned long flags;
  842. struct device *dev = i2400m_dev(i2400m);
  843. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  844. spin_lock_irqsave(&i2400m->tx_lock, flags);
  845. if (i2400m->tx_buf == NULL)
  846. goto out_unlock;
  847. i2400m->tx_out += i2400m->tx_msg_size;
  848. d_printf(2, dev, "TX: sent %zu b\n", (size_t) i2400m->tx_msg_size);
  849. i2400m->tx_msg_size = 0;
  850. BUG_ON(i2400m->tx_out > i2400m->tx_in);
  851. /* level them FIFO markers off */
  852. n = i2400m->tx_out / I2400M_TX_BUF_SIZE;
  853. i2400m->tx_out %= I2400M_TX_BUF_SIZE;
  854. i2400m->tx_in -= n * I2400M_TX_BUF_SIZE;
  855. out_unlock:
  856. spin_unlock_irqrestore(&i2400m->tx_lock, flags);
  857. d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
  858. }
  859. EXPORT_SYMBOL_GPL(i2400m_tx_msg_sent);
  860. /**
  861. * i2400m_tx_setup - Initialize the TX queue and infrastructure
  862. *
  863. * Make sure we reset the TX sequence to zero, as when this function
  864. * is called, the firmware has been just restarted. Same rational
  865. * for tx_in, tx_out, tx_msg_size and tx_msg. We reset them since
  866. * the memory for TX queue is reallocated.
  867. */
  868. int i2400m_tx_setup(struct i2400m *i2400m)
  869. {
  870. int result = 0;
  871. void *tx_buf;
  872. unsigned long flags;
  873. /* Do this here only once -- can't do on
  874. * i2400m_hard_start_xmit() as we'll cause race conditions if
  875. * the WS was scheduled on another CPU */
  876. INIT_WORK(&i2400m->wake_tx_ws, i2400m_wake_tx_work);
  877. tx_buf = kmalloc(I2400M_TX_BUF_SIZE, GFP_ATOMIC);
  878. if (tx_buf == NULL) {
  879. result = -ENOMEM;
  880. goto error_kmalloc;
  881. }
  882. /* Warn if the calculated buffer size exceeds I2400M_TX_BUF_SIZE. */
  883. BUILD_BUG_ON(I2400M_TX_PDU_TOTAL_SIZE > I2400M_TX_BUF_SIZE);
  884. spin_lock_irqsave(&i2400m->tx_lock, flags);
  885. i2400m->tx_sequence = 0;
  886. i2400m->tx_in = 0;
  887. i2400m->tx_out = 0;
  888. i2400m->tx_msg_size = 0;
  889. i2400m->tx_msg = NULL;
  890. i2400m->tx_buf = tx_buf;
  891. spin_unlock_irqrestore(&i2400m->tx_lock, flags);
  892. /* Huh? the bus layer has to define this... */
  893. BUG_ON(i2400m->bus_tx_block_size == 0);
  894. error_kmalloc:
  895. return result;
  896. }
  897. /**
  898. * i2400m_tx_release - Tear down the TX queue and infrastructure
  899. */
  900. void i2400m_tx_release(struct i2400m *i2400m)
  901. {
  902. unsigned long flags;
  903. spin_lock_irqsave(&i2400m->tx_lock, flags);
  904. kfree(i2400m->tx_buf);
  905. i2400m->tx_buf = NULL;
  906. spin_unlock_irqrestore(&i2400m->tx_lock, flags);
  907. }