rx.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. /*
  2. * Intel Wireless WiMAX Connection 2400m
  3. * Handle incoming traffic and deliver it to the control or data planes
  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. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  39. * - Use skb_clone(), break up processing in chunks
  40. * - Split transport/device specific
  41. * - Make buffer size dynamic to exert less memory pressure
  42. * - RX reorder support
  43. *
  44. * This handles the RX path.
  45. *
  46. * We receive an RX message from the bus-specific driver, which
  47. * contains one or more payloads that have potentially different
  48. * destinataries (data or control paths).
  49. *
  50. * So we just take that payload from the transport specific code in
  51. * the form of an skb, break it up in chunks (a cloned skb each in the
  52. * case of network packets) and pass it to netdev or to the
  53. * command/ack handler (and from there to the WiMAX stack).
  54. *
  55. * PROTOCOL FORMAT
  56. *
  57. * The format of the buffer is:
  58. *
  59. * HEADER (struct i2400m_msg_hdr)
  60. * PAYLOAD DESCRIPTOR 0 (struct i2400m_pld)
  61. * PAYLOAD DESCRIPTOR 1
  62. * ...
  63. * PAYLOAD DESCRIPTOR N
  64. * PAYLOAD 0 (raw bytes)
  65. * PAYLOAD 1
  66. * ...
  67. * PAYLOAD N
  68. *
  69. * See tx.c for a deeper description on alignment requirements and
  70. * other fun facts of it.
  71. *
  72. * DATA PACKETS
  73. *
  74. * In firmwares <= v1.3, data packets have no header for RX, but they
  75. * do for TX (currently unused).
  76. *
  77. * In firmware >= 1.4, RX packets have an extended header (16
  78. * bytes). This header conveys information for management of host
  79. * reordering of packets (the device offloads storage of the packets
  80. * for reordering to the host). Read below for more information.
  81. *
  82. * The header is used as dummy space to emulate an ethernet header and
  83. * thus be able to act as an ethernet device without having to reallocate.
  84. *
  85. * DATA RX REORDERING
  86. *
  87. * Starting in firmware v1.4, the device can deliver packets for
  88. * delivery with special reordering information; this allows it to
  89. * more effectively do packet management when some frames were lost in
  90. * the radio traffic.
  91. *
  92. * Thus, for RX packets that come out of order, the device gives the
  93. * driver enough information to queue them properly and then at some
  94. * point, the signal to deliver the whole (or part) of the queued
  95. * packets to the networking stack. There are 16 such queues.
  96. *
  97. * This only happens when a packet comes in with the "need reorder"
  98. * flag set in the RX header. When such bit is set, the following
  99. * operations might be indicated:
  100. *
  101. * - reset queue: send all queued packets to the OS
  102. *
  103. * - queue: queue a packet
  104. *
  105. * - update ws: update the queue's window start and deliver queued
  106. * packets that meet the criteria
  107. *
  108. * - queue & update ws: queue a packet, update the window start and
  109. * deliver queued packets that meet the criteria
  110. *
  111. * (delivery criteria: the packet's [normalized] sequence number is
  112. * lower than the new [normalized] window start).
  113. *
  114. * See the i2400m_roq_*() functions for details.
  115. *
  116. * ROADMAP
  117. *
  118. * i2400m_rx
  119. * i2400m_rx_msg_hdr_check
  120. * i2400m_rx_pl_descr_check
  121. * i2400m_rx_payload
  122. * i2400m_net_rx
  123. * i2400m_rx_edata
  124. * i2400m_net_erx
  125. * i2400m_roq_reset
  126. * i2400m_net_erx
  127. * i2400m_roq_queue
  128. * __i2400m_roq_queue
  129. * i2400m_roq_update_ws
  130. * __i2400m_roq_update_ws
  131. * i2400m_net_erx
  132. * i2400m_roq_queue_update_ws
  133. * __i2400m_roq_queue
  134. * __i2400m_roq_update_ws
  135. * i2400m_net_erx
  136. * i2400m_rx_ctl
  137. * i2400m_msg_size_check
  138. * i2400m_report_hook_work [in a workqueue]
  139. * i2400m_report_hook
  140. * wimax_msg_to_user
  141. * i2400m_rx_ctl_ack
  142. * wimax_msg_to_user_alloc
  143. * i2400m_rx_trace
  144. * i2400m_msg_size_check
  145. * wimax_msg
  146. */
  147. #include <linux/slab.h>
  148. #include <linux/kernel.h>
  149. #include <linux/if_arp.h>
  150. #include <linux/netdevice.h>
  151. #include <linux/workqueue.h>
  152. #include "i2400m.h"
  153. #define D_SUBMODULE rx
  154. #include "debug-levels.h"
  155. static int i2400m_rx_reorder_disabled; /* 0 (rx reorder enabled) by default */
  156. module_param_named(rx_reorder_disabled, i2400m_rx_reorder_disabled, int, 0644);
  157. MODULE_PARM_DESC(rx_reorder_disabled,
  158. "If true, RX reordering will be disabled.");
  159. struct i2400m_report_hook_args {
  160. struct sk_buff *skb_rx;
  161. const struct i2400m_l3l4_hdr *l3l4_hdr;
  162. size_t size;
  163. struct list_head list_node;
  164. };
  165. /*
  166. * Execute i2400m_report_hook in a workqueue
  167. *
  168. * Goes over the list of queued reports in i2400m->rx_reports and
  169. * processes them.
  170. *
  171. * NOTE: refcounts on i2400m are not needed because we flush the
  172. * workqueue this runs on (i2400m->work_queue) before destroying
  173. * i2400m.
  174. */
  175. void i2400m_report_hook_work(struct work_struct *ws)
  176. {
  177. struct i2400m *i2400m = container_of(ws, struct i2400m, rx_report_ws);
  178. struct device *dev = i2400m_dev(i2400m);
  179. struct i2400m_report_hook_args *args, *args_next;
  180. LIST_HEAD(list);
  181. unsigned long flags;
  182. while (1) {
  183. spin_lock_irqsave(&i2400m->rx_lock, flags);
  184. list_splice_init(&i2400m->rx_reports, &list);
  185. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  186. if (list_empty(&list))
  187. break;
  188. else
  189. d_printf(1, dev, "processing queued reports\n");
  190. list_for_each_entry_safe(args, args_next, &list, list_node) {
  191. d_printf(2, dev, "processing queued report %p\n", args);
  192. i2400m_report_hook(i2400m, args->l3l4_hdr, args->size);
  193. kfree_skb(args->skb_rx);
  194. list_del(&args->list_node);
  195. kfree(args);
  196. }
  197. }
  198. }
  199. /*
  200. * Flush the list of queued reports
  201. */
  202. static
  203. void i2400m_report_hook_flush(struct i2400m *i2400m)
  204. {
  205. struct device *dev = i2400m_dev(i2400m);
  206. struct i2400m_report_hook_args *args, *args_next;
  207. LIST_HEAD(list);
  208. unsigned long flags;
  209. d_printf(1, dev, "flushing queued reports\n");
  210. spin_lock_irqsave(&i2400m->rx_lock, flags);
  211. list_splice_init(&i2400m->rx_reports, &list);
  212. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  213. list_for_each_entry_safe(args, args_next, &list, list_node) {
  214. d_printf(2, dev, "flushing queued report %p\n", args);
  215. kfree_skb(args->skb_rx);
  216. list_del(&args->list_node);
  217. kfree(args);
  218. }
  219. }
  220. /*
  221. * Queue a report for later processing
  222. *
  223. * @i2400m: device descriptor
  224. * @skb_rx: skb that contains the payload (for reference counting)
  225. * @l3l4_hdr: pointer to the control
  226. * @size: size of the message
  227. */
  228. static
  229. void i2400m_report_hook_queue(struct i2400m *i2400m, struct sk_buff *skb_rx,
  230. const void *l3l4_hdr, size_t size)
  231. {
  232. struct device *dev = i2400m_dev(i2400m);
  233. unsigned long flags;
  234. struct i2400m_report_hook_args *args;
  235. args = kzalloc(sizeof(*args), GFP_NOIO);
  236. if (args) {
  237. args->skb_rx = skb_get(skb_rx);
  238. args->l3l4_hdr = l3l4_hdr;
  239. args->size = size;
  240. spin_lock_irqsave(&i2400m->rx_lock, flags);
  241. list_add_tail(&args->list_node, &i2400m->rx_reports);
  242. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  243. d_printf(2, dev, "queued report %p\n", args);
  244. rmb(); /* see i2400m->ready's documentation */
  245. if (likely(i2400m->ready)) /* only send if up */
  246. queue_work(i2400m->work_queue, &i2400m->rx_report_ws);
  247. } else {
  248. if (printk_ratelimit())
  249. dev_err(dev, "%s:%u: Can't allocate %zu B\n",
  250. __func__, __LINE__, sizeof(*args));
  251. }
  252. }
  253. /*
  254. * Process an ack to a command
  255. *
  256. * @i2400m: device descriptor
  257. * @payload: pointer to message
  258. * @size: size of the message
  259. *
  260. * Pass the acknodledgment (in an skb) to the thread that is waiting
  261. * for it in i2400m->msg_completion.
  262. *
  263. * We need to coordinate properly with the thread waiting for the
  264. * ack. Check if it is waiting or if it is gone. We loose the spinlock
  265. * to avoid allocating on atomic contexts (yeah, could use GFP_ATOMIC,
  266. * but this is not so speed critical).
  267. */
  268. static
  269. void i2400m_rx_ctl_ack(struct i2400m *i2400m,
  270. const void *payload, size_t size)
  271. {
  272. struct device *dev = i2400m_dev(i2400m);
  273. struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  274. unsigned long flags;
  275. struct sk_buff *ack_skb;
  276. /* Anyone waiting for an answer? */
  277. spin_lock_irqsave(&i2400m->rx_lock, flags);
  278. if (i2400m->ack_skb != ERR_PTR(-EINPROGRESS)) {
  279. dev_err(dev, "Huh? reply to command with no waiters\n");
  280. goto error_no_waiter;
  281. }
  282. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  283. ack_skb = wimax_msg_alloc(wimax_dev, NULL, payload, size, GFP_KERNEL);
  284. /* Check waiter didn't time out waiting for the answer... */
  285. spin_lock_irqsave(&i2400m->rx_lock, flags);
  286. if (i2400m->ack_skb != ERR_PTR(-EINPROGRESS)) {
  287. d_printf(1, dev, "Huh? waiter for command reply cancelled\n");
  288. goto error_waiter_cancelled;
  289. }
  290. if (IS_ERR(ack_skb))
  291. dev_err(dev, "CMD/GET/SET ack: cannot allocate SKB\n");
  292. i2400m->ack_skb = ack_skb;
  293. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  294. complete(&i2400m->msg_completion);
  295. return;
  296. error_waiter_cancelled:
  297. if (!IS_ERR(ack_skb))
  298. kfree_skb(ack_skb);
  299. error_no_waiter:
  300. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  301. }
  302. /*
  303. * Receive and process a control payload
  304. *
  305. * @i2400m: device descriptor
  306. * @skb_rx: skb that contains the payload (for reference counting)
  307. * @payload: pointer to message
  308. * @size: size of the message
  309. *
  310. * There are two types of control RX messages: reports (asynchronous,
  311. * like your every day interrupts) and 'acks' (reponses to a command,
  312. * get or set request).
  313. *
  314. * If it is a report, we run hooks on it (to extract information for
  315. * things we need to do in the driver) and then pass it over to the
  316. * WiMAX stack to send it to user space.
  317. *
  318. * NOTE: report processing is done in a workqueue specific to the
  319. * generic driver, to avoid deadlocks in the system.
  320. *
  321. * If it is not a report, it is an ack to a previously executed
  322. * command, set or get, so wake up whoever is waiting for it from
  323. * i2400m_msg_to_dev(). i2400m_rx_ctl_ack() takes care of that.
  324. *
  325. * Note that the sizes we pass to other functions from here are the
  326. * sizes of the _l3l4_hdr + payload, not full buffer sizes, as we have
  327. * verified in _msg_size_check() that they are congruent.
  328. *
  329. * For reports: We can't clone the original skb where the data is
  330. * because we need to send this up via netlink; netlink has to add
  331. * headers and we can't overwrite what's preceeding the payload...as
  332. * it is another message. So we just dup them.
  333. */
  334. static
  335. void i2400m_rx_ctl(struct i2400m *i2400m, struct sk_buff *skb_rx,
  336. const void *payload, size_t size)
  337. {
  338. int result;
  339. struct device *dev = i2400m_dev(i2400m);
  340. const struct i2400m_l3l4_hdr *l3l4_hdr = payload;
  341. unsigned msg_type;
  342. result = i2400m_msg_size_check(i2400m, l3l4_hdr, size);
  343. if (result < 0) {
  344. dev_err(dev, "HW BUG? device sent a bad message: %d\n",
  345. result);
  346. goto error_check;
  347. }
  348. msg_type = le16_to_cpu(l3l4_hdr->type);
  349. d_printf(1, dev, "%s 0x%04x: %zu bytes\n",
  350. msg_type & I2400M_MT_REPORT_MASK ? "REPORT" : "CMD/SET/GET",
  351. msg_type, size);
  352. d_dump(2, dev, l3l4_hdr, size);
  353. if (msg_type & I2400M_MT_REPORT_MASK) {
  354. /*
  355. * Process each report
  356. *
  357. * - has to be ran serialized as well
  358. *
  359. * - the handling might force the execution of
  360. * commands. That might cause reentrancy issues with
  361. * bus-specific subdrivers and workqueues, so the we
  362. * run it in a separate workqueue.
  363. *
  364. * - when the driver is not yet ready to handle them,
  365. * they are queued and at some point the queue is
  366. * restarted [NOTE: we can't queue SKBs directly, as
  367. * this might be a piece of a SKB, not the whole
  368. * thing, and this is cheaper than cloning the
  369. * SKB].
  370. *
  371. * Note we don't do refcounting for the device
  372. * structure; this is because before destroying
  373. * 'i2400m', we make sure to flush the
  374. * i2400m->work_queue, so there are no issues.
  375. */
  376. i2400m_report_hook_queue(i2400m, skb_rx, l3l4_hdr, size);
  377. if (unlikely(i2400m->trace_msg_from_user))
  378. wimax_msg(&i2400m->wimax_dev, "echo",
  379. l3l4_hdr, size, GFP_KERNEL);
  380. result = wimax_msg(&i2400m->wimax_dev, NULL, l3l4_hdr, size,
  381. GFP_KERNEL);
  382. if (result < 0)
  383. dev_err(dev, "error sending report to userspace: %d\n",
  384. result);
  385. } else /* an ack to a CMD, GET or SET */
  386. i2400m_rx_ctl_ack(i2400m, payload, size);
  387. error_check:
  388. return;
  389. }
  390. /*
  391. * Receive and send up a trace
  392. *
  393. * @i2400m: device descriptor
  394. * @skb_rx: skb that contains the trace (for reference counting)
  395. * @payload: pointer to trace message inside the skb
  396. * @size: size of the message
  397. *
  398. * THe i2400m might produce trace information (diagnostics) and we
  399. * send them through a different kernel-to-user pipe (to avoid
  400. * clogging it).
  401. *
  402. * As in i2400m_rx_ctl(), we can't clone the original skb where the
  403. * data is because we need to send this up via netlink; netlink has to
  404. * add headers and we can't overwrite what's preceeding the
  405. * payload...as it is another message. So we just dup them.
  406. */
  407. static
  408. void i2400m_rx_trace(struct i2400m *i2400m,
  409. const void *payload, size_t size)
  410. {
  411. int result;
  412. struct device *dev = i2400m_dev(i2400m);
  413. struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  414. const struct i2400m_l3l4_hdr *l3l4_hdr = payload;
  415. unsigned msg_type;
  416. result = i2400m_msg_size_check(i2400m, l3l4_hdr, size);
  417. if (result < 0) {
  418. dev_err(dev, "HW BUG? device sent a bad trace message: %d\n",
  419. result);
  420. goto error_check;
  421. }
  422. msg_type = le16_to_cpu(l3l4_hdr->type);
  423. d_printf(1, dev, "Trace %s 0x%04x: %zu bytes\n",
  424. msg_type & I2400M_MT_REPORT_MASK ? "REPORT" : "CMD/SET/GET",
  425. msg_type, size);
  426. d_dump(2, dev, l3l4_hdr, size);
  427. result = wimax_msg(wimax_dev, "trace", l3l4_hdr, size, GFP_KERNEL);
  428. if (result < 0)
  429. dev_err(dev, "error sending trace to userspace: %d\n",
  430. result);
  431. error_check:
  432. return;
  433. }
  434. /*
  435. * Reorder queue data stored on skb->cb while the skb is queued in the
  436. * reorder queues.
  437. */
  438. struct i2400m_roq_data {
  439. unsigned sn; /* Serial number for the skb */
  440. enum i2400m_cs cs; /* packet type for the skb */
  441. };
  442. /*
  443. * ReOrder Queue
  444. *
  445. * @ws: Window Start; sequence number where the current window start
  446. * is for this queue
  447. * @queue: the skb queue itself
  448. * @log: circular ring buffer used to log information about the
  449. * reorder process in this queue that can be displayed in case of
  450. * error to help diagnose it.
  451. *
  452. * This is the head for a list of skbs. In the skb->cb member of the
  453. * skb when queued here contains a 'struct i2400m_roq_data' were we
  454. * store the sequence number (sn) and the cs (packet type) coming from
  455. * the RX payload header from the device.
  456. */
  457. struct i2400m_roq
  458. {
  459. unsigned ws;
  460. struct sk_buff_head queue;
  461. struct i2400m_roq_log *log;
  462. };
  463. static
  464. void __i2400m_roq_init(struct i2400m_roq *roq)
  465. {
  466. roq->ws = 0;
  467. skb_queue_head_init(&roq->queue);
  468. }
  469. static
  470. unsigned __i2400m_roq_index(struct i2400m *i2400m, struct i2400m_roq *roq)
  471. {
  472. return ((unsigned long) roq - (unsigned long) i2400m->rx_roq)
  473. / sizeof(*roq);
  474. }
  475. /*
  476. * Normalize a sequence number based on the queue's window start
  477. *
  478. * nsn = (sn - ws) % 2048
  479. *
  480. * Note that if @sn < @roq->ws, we still need a positive number; %'s
  481. * sign is implementation specific, so we normalize it by adding 2048
  482. * to bring it to be positive.
  483. */
  484. static
  485. unsigned __i2400m_roq_nsn(struct i2400m_roq *roq, unsigned sn)
  486. {
  487. int r;
  488. r = ((int) sn - (int) roq->ws) % 2048;
  489. if (r < 0)
  490. r += 2048;
  491. return r;
  492. }
  493. /*
  494. * Circular buffer to keep the last N reorder operations
  495. *
  496. * In case something fails, dumb then to try to come up with what
  497. * happened.
  498. */
  499. enum {
  500. I2400M_ROQ_LOG_LENGTH = 32,
  501. };
  502. struct i2400m_roq_log {
  503. struct i2400m_roq_log_entry {
  504. enum i2400m_ro_type type;
  505. unsigned ws, count, sn, nsn, new_ws;
  506. } entry[I2400M_ROQ_LOG_LENGTH];
  507. unsigned in, out;
  508. };
  509. /* Print a log entry */
  510. static
  511. void i2400m_roq_log_entry_print(struct i2400m *i2400m, unsigned index,
  512. unsigned e_index,
  513. struct i2400m_roq_log_entry *e)
  514. {
  515. struct device *dev = i2400m_dev(i2400m);
  516. switch(e->type) {
  517. case I2400M_RO_TYPE_RESET:
  518. dev_err(dev, "q#%d reset ws %u cnt %u sn %u/%u"
  519. " - new nws %u\n",
  520. index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
  521. break;
  522. case I2400M_RO_TYPE_PACKET:
  523. dev_err(dev, "q#%d queue ws %u cnt %u sn %u/%u\n",
  524. index, e->ws, e->count, e->sn, e->nsn);
  525. break;
  526. case I2400M_RO_TYPE_WS:
  527. dev_err(dev, "q#%d update_ws ws %u cnt %u sn %u/%u"
  528. " - new nws %u\n",
  529. index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
  530. break;
  531. case I2400M_RO_TYPE_PACKET_WS:
  532. dev_err(dev, "q#%d queue_update_ws ws %u cnt %u sn %u/%u"
  533. " - new nws %u\n",
  534. index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
  535. break;
  536. default:
  537. dev_err(dev, "q#%d BUG? entry %u - unknown type %u\n",
  538. index, e_index, e->type);
  539. break;
  540. }
  541. }
  542. static
  543. void i2400m_roq_log_add(struct i2400m *i2400m,
  544. struct i2400m_roq *roq, enum i2400m_ro_type type,
  545. unsigned ws, unsigned count, unsigned sn,
  546. unsigned nsn, unsigned new_ws)
  547. {
  548. struct i2400m_roq_log_entry *e;
  549. unsigned cnt_idx;
  550. int index = __i2400m_roq_index(i2400m, roq);
  551. /* if we run out of space, we eat from the end */
  552. if (roq->log->in - roq->log->out == I2400M_ROQ_LOG_LENGTH)
  553. roq->log->out++;
  554. cnt_idx = roq->log->in++ % I2400M_ROQ_LOG_LENGTH;
  555. e = &roq->log->entry[cnt_idx];
  556. e->type = type;
  557. e->ws = ws;
  558. e->count = count;
  559. e->sn = sn;
  560. e->nsn = nsn;
  561. e->new_ws = new_ws;
  562. if (d_test(1))
  563. i2400m_roq_log_entry_print(i2400m, index, cnt_idx, e);
  564. }
  565. /* Dump all the entries in the FIFO and reinitialize it */
  566. static
  567. void i2400m_roq_log_dump(struct i2400m *i2400m, struct i2400m_roq *roq)
  568. {
  569. unsigned cnt, cnt_idx;
  570. struct i2400m_roq_log_entry *e;
  571. int index = __i2400m_roq_index(i2400m, roq);
  572. BUG_ON(roq->log->out > roq->log->in);
  573. for (cnt = roq->log->out; cnt < roq->log->in; cnt++) {
  574. cnt_idx = cnt % I2400M_ROQ_LOG_LENGTH;
  575. e = &roq->log->entry[cnt_idx];
  576. i2400m_roq_log_entry_print(i2400m, index, cnt_idx, e);
  577. memset(e, 0, sizeof(*e));
  578. }
  579. roq->log->in = roq->log->out = 0;
  580. }
  581. /*
  582. * Backbone for the queuing of an skb (by normalized sequence number)
  583. *
  584. * @i2400m: device descriptor
  585. * @roq: reorder queue where to add
  586. * @skb: the skb to add
  587. * @sn: the sequence number of the skb
  588. * @nsn: the normalized sequence number of the skb (pre-computed by the
  589. * caller from the @sn and @roq->ws).
  590. *
  591. * We try first a couple of quick cases:
  592. *
  593. * - the queue is empty
  594. * - the skb would be appended to the queue
  595. *
  596. * These will be the most common operations.
  597. *
  598. * If these fail, then we have to do a sorted insertion in the queue,
  599. * which is the slowest path.
  600. *
  601. * We don't have to acquire a reference count as we are going to own it.
  602. */
  603. static
  604. void __i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
  605. struct sk_buff *skb, unsigned sn, unsigned nsn)
  606. {
  607. struct device *dev = i2400m_dev(i2400m);
  608. struct sk_buff *skb_itr;
  609. struct i2400m_roq_data *roq_data_itr, *roq_data;
  610. unsigned nsn_itr;
  611. d_fnstart(4, dev, "(i2400m %p roq %p skb %p sn %u nsn %u)\n",
  612. i2400m, roq, skb, sn, nsn);
  613. roq_data = (struct i2400m_roq_data *) &skb->cb;
  614. BUILD_BUG_ON(sizeof(*roq_data) > sizeof(skb->cb));
  615. roq_data->sn = sn;
  616. d_printf(3, dev, "ERX: roq %p [ws %u] nsn %d sn %u\n",
  617. roq, roq->ws, nsn, roq_data->sn);
  618. /* Queues will be empty on not-so-bad environments, so try
  619. * that first */
  620. if (skb_queue_empty(&roq->queue)) {
  621. d_printf(2, dev, "ERX: roq %p - first one\n", roq);
  622. __skb_queue_head(&roq->queue, skb);
  623. goto out;
  624. }
  625. /* Now try append, as most of the operations will be that */
  626. skb_itr = skb_peek_tail(&roq->queue);
  627. roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
  628. nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
  629. /* NSN bounds assumed correct (checked when it was queued) */
  630. if (nsn >= nsn_itr) {
  631. d_printf(2, dev, "ERX: roq %p - appended after %p (nsn %d sn %u)\n",
  632. roq, skb_itr, nsn_itr, roq_data_itr->sn);
  633. __skb_queue_tail(&roq->queue, skb);
  634. goto out;
  635. }
  636. /* None of the fast paths option worked. Iterate to find the
  637. * right spot where to insert the packet; we know the queue is
  638. * not empty, so we are not the first ones; we also know we
  639. * are not going to be the last ones. The list is sorted, so
  640. * we have to insert before the the first guy with an nsn_itr
  641. * greater that our nsn. */
  642. skb_queue_walk(&roq->queue, skb_itr) {
  643. roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
  644. nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
  645. /* NSN bounds assumed correct (checked when it was queued) */
  646. if (nsn_itr > nsn) {
  647. d_printf(2, dev, "ERX: roq %p - queued before %p "
  648. "(nsn %d sn %u)\n", roq, skb_itr, nsn_itr,
  649. roq_data_itr->sn);
  650. __skb_queue_before(&roq->queue, skb_itr, skb);
  651. goto out;
  652. }
  653. }
  654. /* If we get here, that is VERY bad -- print info to help
  655. * diagnose and crash it */
  656. dev_err(dev, "SW BUG? failed to insert packet\n");
  657. dev_err(dev, "ERX: roq %p [ws %u] skb %p nsn %d sn %u\n",
  658. roq, roq->ws, skb, nsn, roq_data->sn);
  659. skb_queue_walk(&roq->queue, skb_itr) {
  660. roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
  661. nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
  662. /* NSN bounds assumed correct (checked when it was queued) */
  663. dev_err(dev, "ERX: roq %p skb_itr %p nsn %d sn %u\n",
  664. roq, skb_itr, nsn_itr, roq_data_itr->sn);
  665. }
  666. BUG();
  667. out:
  668. d_fnend(4, dev, "(i2400m %p roq %p skb %p sn %u nsn %d) = void\n",
  669. i2400m, roq, skb, sn, nsn);
  670. }
  671. /*
  672. * Backbone for the update window start operation
  673. *
  674. * @i2400m: device descriptor
  675. * @roq: Reorder queue
  676. * @sn: New sequence number
  677. *
  678. * Updates the window start of a queue; when doing so, it must deliver
  679. * to the networking stack all the queued skb's whose normalized
  680. * sequence number is lower than the new normalized window start.
  681. */
  682. static
  683. unsigned __i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
  684. unsigned sn)
  685. {
  686. struct device *dev = i2400m_dev(i2400m);
  687. struct sk_buff *skb_itr, *tmp_itr;
  688. struct i2400m_roq_data *roq_data_itr;
  689. unsigned new_nws, nsn_itr;
  690. new_nws = __i2400m_roq_nsn(roq, sn);
  691. /*
  692. * For type 2(update_window_start) rx messages, there is no
  693. * need to check if the normalized sequence number is greater 1023.
  694. * Simply insert and deliver all packets to the host up to the
  695. * window start.
  696. */
  697. skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
  698. roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
  699. nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
  700. /* NSN bounds assumed correct (checked when it was queued) */
  701. if (nsn_itr < new_nws) {
  702. d_printf(2, dev, "ERX: roq %p - release skb %p "
  703. "(nsn %u/%u new nws %u)\n",
  704. roq, skb_itr, nsn_itr, roq_data_itr->sn,
  705. new_nws);
  706. __skb_unlink(skb_itr, &roq->queue);
  707. i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
  708. }
  709. else
  710. break; /* rest of packets all nsn_itr > nws */
  711. }
  712. roq->ws = sn;
  713. return new_nws;
  714. }
  715. /*
  716. * Reset a queue
  717. *
  718. * @i2400m: device descriptor
  719. * @cin: Queue Index
  720. *
  721. * Deliver all the packets and reset the window-start to zero. Name is
  722. * kind of misleading.
  723. */
  724. static
  725. void i2400m_roq_reset(struct i2400m *i2400m, struct i2400m_roq *roq)
  726. {
  727. struct device *dev = i2400m_dev(i2400m);
  728. struct sk_buff *skb_itr, *tmp_itr;
  729. struct i2400m_roq_data *roq_data_itr;
  730. d_fnstart(2, dev, "(i2400m %p roq %p)\n", i2400m, roq);
  731. i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_RESET,
  732. roq->ws, skb_queue_len(&roq->queue),
  733. ~0, ~0, 0);
  734. skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
  735. roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
  736. d_printf(2, dev, "ERX: roq %p - release skb %p (sn %u)\n",
  737. roq, skb_itr, roq_data_itr->sn);
  738. __skb_unlink(skb_itr, &roq->queue);
  739. i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
  740. }
  741. roq->ws = 0;
  742. d_fnend(2, dev, "(i2400m %p roq %p) = void\n", i2400m, roq);
  743. }
  744. /*
  745. * Queue a packet
  746. *
  747. * @i2400m: device descriptor
  748. * @cin: Queue Index
  749. * @skb: containing the packet data
  750. * @fbn: First block number of the packet in @skb
  751. * @lbn: Last block number of the packet in @skb
  752. *
  753. * The hardware is asking the driver to queue a packet for later
  754. * delivery to the networking stack.
  755. */
  756. static
  757. void i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
  758. struct sk_buff * skb, unsigned lbn)
  759. {
  760. struct device *dev = i2400m_dev(i2400m);
  761. unsigned nsn, len;
  762. d_fnstart(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
  763. i2400m, roq, skb, lbn);
  764. len = skb_queue_len(&roq->queue);
  765. nsn = __i2400m_roq_nsn(roq, lbn);
  766. if (unlikely(nsn >= 1024)) {
  767. dev_err(dev, "SW BUG? queue nsn %d (lbn %u ws %u)\n",
  768. nsn, lbn, roq->ws);
  769. i2400m_roq_log_dump(i2400m, roq);
  770. i2400m_reset(i2400m, I2400M_RT_WARM);
  771. } else {
  772. __i2400m_roq_queue(i2400m, roq, skb, lbn, nsn);
  773. i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET,
  774. roq->ws, len, lbn, nsn, ~0);
  775. }
  776. d_fnend(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
  777. i2400m, roq, skb, lbn);
  778. }
  779. /*
  780. * Update the window start in a reorder queue and deliver all skbs
  781. * with a lower window start
  782. *
  783. * @i2400m: device descriptor
  784. * @roq: Reorder queue
  785. * @sn: New sequence number
  786. */
  787. static
  788. void i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
  789. unsigned sn)
  790. {
  791. struct device *dev = i2400m_dev(i2400m);
  792. unsigned old_ws, nsn, len;
  793. d_fnstart(2, dev, "(i2400m %p roq %p sn %u)\n", i2400m, roq, sn);
  794. old_ws = roq->ws;
  795. len = skb_queue_len(&roq->queue);
  796. nsn = __i2400m_roq_update_ws(i2400m, roq, sn);
  797. i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_WS,
  798. old_ws, len, sn, nsn, roq->ws);
  799. d_fnstart(2, dev, "(i2400m %p roq %p sn %u) = void\n", i2400m, roq, sn);
  800. }
  801. /*
  802. * Queue a packet and update the window start
  803. *
  804. * @i2400m: device descriptor
  805. * @cin: Queue Index
  806. * @skb: containing the packet data
  807. * @fbn: First block number of the packet in @skb
  808. * @sn: Last block number of the packet in @skb
  809. *
  810. * Note that unlike i2400m_roq_update_ws(), which sets the new window
  811. * start to @sn, in here we'll set it to @sn + 1.
  812. */
  813. static
  814. void i2400m_roq_queue_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
  815. struct sk_buff * skb, unsigned sn)
  816. {
  817. struct device *dev = i2400m_dev(i2400m);
  818. unsigned nsn, old_ws, len;
  819. d_fnstart(2, dev, "(i2400m %p roq %p skb %p sn %u)\n",
  820. i2400m, roq, skb, sn);
  821. len = skb_queue_len(&roq->queue);
  822. nsn = __i2400m_roq_nsn(roq, sn);
  823. /*
  824. * For type 3(queue_update_window_start) rx messages, there is no
  825. * need to check if the normalized sequence number is greater 1023.
  826. * Simply insert and deliver all packets to the host up to the
  827. * window start.
  828. */
  829. old_ws = roq->ws;
  830. /* If the queue is empty, don't bother as we'd queue
  831. * it and immediately unqueue it -- just deliver it.
  832. */
  833. if (len == 0) {
  834. struct i2400m_roq_data *roq_data;
  835. roq_data = (struct i2400m_roq_data *) &skb->cb;
  836. i2400m_net_erx(i2400m, skb, roq_data->cs);
  837. } else
  838. __i2400m_roq_queue(i2400m, roq, skb, sn, nsn);
  839. __i2400m_roq_update_ws(i2400m, roq, sn + 1);
  840. i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET_WS,
  841. old_ws, len, sn, nsn, roq->ws);
  842. d_fnend(2, dev, "(i2400m %p roq %p skb %p sn %u) = void\n",
  843. i2400m, roq, skb, sn);
  844. }
  845. /*
  846. * This routine destroys the memory allocated for rx_roq, when no
  847. * other thread is accessing it. Access to rx_roq is refcounted by
  848. * rx_roq_refcount, hence memory allocated must be destroyed when
  849. * rx_roq_refcount becomes zero. This routine gets executed when
  850. * rx_roq_refcount becomes zero.
  851. */
  852. static void i2400m_rx_roq_destroy(struct kref *ref)
  853. {
  854. unsigned itr;
  855. struct i2400m *i2400m
  856. = container_of(ref, struct i2400m, rx_roq_refcount);
  857. for (itr = 0; itr < I2400M_RO_CIN + 1; itr++)
  858. __skb_queue_purge(&i2400m->rx_roq[itr].queue);
  859. kfree(i2400m->rx_roq[0].log);
  860. kfree(i2400m->rx_roq);
  861. i2400m->rx_roq = NULL;
  862. }
  863. /*
  864. * Receive and send up an extended data packet
  865. *
  866. * @i2400m: device descriptor
  867. * @skb_rx: skb that contains the extended data packet
  868. * @single_last: 1 if the payload is the only one or the last one of
  869. * the skb.
  870. * @payload: pointer to the packet's data inside the skb
  871. * @size: size of the payload
  872. *
  873. * Starting in v1.4 of the i2400m's firmware, the device can send data
  874. * packets to the host in an extended format that; this incudes a 16
  875. * byte header (struct i2400m_pl_edata_hdr). Using this header's space
  876. * we can fake ethernet headers for ethernet device emulation without
  877. * having to copy packets around.
  878. *
  879. * This function handles said path.
  880. *
  881. *
  882. * Receive and send up an extended data packet that requires no reordering
  883. *
  884. * @i2400m: device descriptor
  885. * @skb_rx: skb that contains the extended data packet
  886. * @single_last: 1 if the payload is the only one or the last one of
  887. * the skb.
  888. * @payload: pointer to the packet's data (past the actual extended
  889. * data payload header).
  890. * @size: size of the payload
  891. *
  892. * Pass over to the networking stack a data packet that might have
  893. * reordering requirements.
  894. *
  895. * This needs to the decide if the skb in which the packet is
  896. * contained can be reused or if it needs to be cloned. Then it has to
  897. * be trimmed in the edges so that the beginning is the space for eth
  898. * header and then pass it to i2400m_net_erx() for the stack
  899. *
  900. * Assumes the caller has verified the sanity of the payload (size,
  901. * etc) already.
  902. */
  903. static
  904. void i2400m_rx_edata(struct i2400m *i2400m, struct sk_buff *skb_rx,
  905. unsigned single_last, const void *payload, size_t size)
  906. {
  907. struct device *dev = i2400m_dev(i2400m);
  908. const struct i2400m_pl_edata_hdr *hdr = payload;
  909. struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  910. struct sk_buff *skb;
  911. enum i2400m_cs cs;
  912. u32 reorder;
  913. unsigned ro_needed, ro_type, ro_cin, ro_sn;
  914. struct i2400m_roq *roq;
  915. struct i2400m_roq_data *roq_data;
  916. unsigned long flags;
  917. BUILD_BUG_ON(ETH_HLEN > sizeof(*hdr));
  918. d_fnstart(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
  919. "size %zu)\n", i2400m, skb_rx, single_last, payload, size);
  920. if (size < sizeof(*hdr)) {
  921. dev_err(dev, "ERX: HW BUG? message with short header (%zu "
  922. "vs %zu bytes expected)\n", size, sizeof(*hdr));
  923. goto error;
  924. }
  925. if (single_last) {
  926. skb = skb_get(skb_rx);
  927. d_printf(3, dev, "ERX: skb %p reusing\n", skb);
  928. } else {
  929. skb = skb_clone(skb_rx, GFP_KERNEL);
  930. if (skb == NULL) {
  931. dev_err(dev, "ERX: no memory to clone skb\n");
  932. net_dev->stats.rx_dropped++;
  933. goto error_skb_clone;
  934. }
  935. d_printf(3, dev, "ERX: skb %p cloned from %p\n", skb, skb_rx);
  936. }
  937. /* now we have to pull and trim so that the skb points to the
  938. * beginning of the IP packet; the netdev part will add the
  939. * ethernet header as needed - we know there is enough space
  940. * because we checked in i2400m_rx_edata(). */
  941. skb_pull(skb, payload + sizeof(*hdr) - (void *) skb->data);
  942. skb_trim(skb, (void *) skb_end_pointer(skb) - payload - sizeof(*hdr));
  943. reorder = le32_to_cpu(hdr->reorder);
  944. ro_needed = reorder & I2400M_RO_NEEDED;
  945. cs = hdr->cs;
  946. if (ro_needed) {
  947. ro_type = (reorder >> I2400M_RO_TYPE_SHIFT) & I2400M_RO_TYPE;
  948. ro_cin = (reorder >> I2400M_RO_CIN_SHIFT) & I2400M_RO_CIN;
  949. ro_sn = (reorder >> I2400M_RO_SN_SHIFT) & I2400M_RO_SN;
  950. spin_lock_irqsave(&i2400m->rx_lock, flags);
  951. if (i2400m->rx_roq == NULL) {
  952. kfree_skb(skb); /* rx_roq is already destroyed */
  953. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  954. goto error;
  955. }
  956. roq = &i2400m->rx_roq[ro_cin];
  957. kref_get(&i2400m->rx_roq_refcount);
  958. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  959. roq_data = (struct i2400m_roq_data *) &skb->cb;
  960. roq_data->sn = ro_sn;
  961. roq_data->cs = cs;
  962. d_printf(2, dev, "ERX: reorder needed: "
  963. "type %u cin %u [ws %u] sn %u/%u len %zuB\n",
  964. ro_type, ro_cin, roq->ws, ro_sn,
  965. __i2400m_roq_nsn(roq, ro_sn), size);
  966. d_dump(2, dev, payload, size);
  967. switch(ro_type) {
  968. case I2400M_RO_TYPE_RESET:
  969. i2400m_roq_reset(i2400m, roq);
  970. kfree_skb(skb); /* no data here */
  971. break;
  972. case I2400M_RO_TYPE_PACKET:
  973. i2400m_roq_queue(i2400m, roq, skb, ro_sn);
  974. break;
  975. case I2400M_RO_TYPE_WS:
  976. i2400m_roq_update_ws(i2400m, roq, ro_sn);
  977. kfree_skb(skb); /* no data here */
  978. break;
  979. case I2400M_RO_TYPE_PACKET_WS:
  980. i2400m_roq_queue_update_ws(i2400m, roq, skb, ro_sn);
  981. break;
  982. default:
  983. dev_err(dev, "HW BUG? unknown reorder type %u\n", ro_type);
  984. }
  985. spin_lock_irqsave(&i2400m->rx_lock, flags);
  986. kref_put(&i2400m->rx_roq_refcount, i2400m_rx_roq_destroy);
  987. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  988. }
  989. else
  990. i2400m_net_erx(i2400m, skb, cs);
  991. error_skb_clone:
  992. error:
  993. d_fnend(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
  994. "size %zu) = void\n", i2400m, skb_rx, single_last, payload, size);
  995. }
  996. /*
  997. * Act on a received payload
  998. *
  999. * @i2400m: device instance
  1000. * @skb_rx: skb where the transaction was received
  1001. * @single_last: 1 this is the only payload or the last one (so the
  1002. * skb can be reused instead of cloned).
  1003. * @pld: payload descriptor
  1004. * @payload: payload data
  1005. *
  1006. * Upon reception of a payload, look at its guts in the payload
  1007. * descriptor and decide what to do with it. If it is a single payload
  1008. * skb or if the last skb is a data packet, the skb will be referenced
  1009. * and modified (so it doesn't have to be cloned).
  1010. */
  1011. static
  1012. void i2400m_rx_payload(struct i2400m *i2400m, struct sk_buff *skb_rx,
  1013. unsigned single_last, const struct i2400m_pld *pld,
  1014. const void *payload)
  1015. {
  1016. struct device *dev = i2400m_dev(i2400m);
  1017. size_t pl_size = i2400m_pld_size(pld);
  1018. enum i2400m_pt pl_type = i2400m_pld_type(pld);
  1019. d_printf(7, dev, "RX: received payload type %u, %zu bytes\n",
  1020. pl_type, pl_size);
  1021. d_dump(8, dev, payload, pl_size);
  1022. switch (pl_type) {
  1023. case I2400M_PT_DATA:
  1024. d_printf(3, dev, "RX: data payload %zu bytes\n", pl_size);
  1025. i2400m_net_rx(i2400m, skb_rx, single_last, payload, pl_size);
  1026. break;
  1027. case I2400M_PT_CTRL:
  1028. i2400m_rx_ctl(i2400m, skb_rx, payload, pl_size);
  1029. break;
  1030. case I2400M_PT_TRACE:
  1031. i2400m_rx_trace(i2400m, payload, pl_size);
  1032. break;
  1033. case I2400M_PT_EDATA:
  1034. d_printf(3, dev, "ERX: data payload %zu bytes\n", pl_size);
  1035. i2400m_rx_edata(i2400m, skb_rx, single_last, payload, pl_size);
  1036. break;
  1037. default: /* Anything else shouldn't come to the host */
  1038. if (printk_ratelimit())
  1039. dev_err(dev, "RX: HW BUG? unexpected payload type %u\n",
  1040. pl_type);
  1041. }
  1042. }
  1043. /*
  1044. * Check a received transaction's message header
  1045. *
  1046. * @i2400m: device descriptor
  1047. * @msg_hdr: message header
  1048. * @buf_size: size of the received buffer
  1049. *
  1050. * Check that the declarations done by a RX buffer message header are
  1051. * sane and consistent with the amount of data that was received.
  1052. */
  1053. static
  1054. int i2400m_rx_msg_hdr_check(struct i2400m *i2400m,
  1055. const struct i2400m_msg_hdr *msg_hdr,
  1056. size_t buf_size)
  1057. {
  1058. int result = -EIO;
  1059. struct device *dev = i2400m_dev(i2400m);
  1060. if (buf_size < sizeof(*msg_hdr)) {
  1061. dev_err(dev, "RX: HW BUG? message with short header (%zu "
  1062. "vs %zu bytes expected)\n", buf_size, sizeof(*msg_hdr));
  1063. goto error;
  1064. }
  1065. if (msg_hdr->barker != cpu_to_le32(I2400M_D2H_MSG_BARKER)) {
  1066. dev_err(dev, "RX: HW BUG? message received with unknown "
  1067. "barker 0x%08x (buf_size %zu bytes)\n",
  1068. le32_to_cpu(msg_hdr->barker), buf_size);
  1069. goto error;
  1070. }
  1071. if (msg_hdr->num_pls == 0) {
  1072. dev_err(dev, "RX: HW BUG? zero payload packets in message\n");
  1073. goto error;
  1074. }
  1075. if (le16_to_cpu(msg_hdr->num_pls) > I2400M_MAX_PLS_IN_MSG) {
  1076. dev_err(dev, "RX: HW BUG? message contains more payload "
  1077. "than maximum; ignoring.\n");
  1078. goto error;
  1079. }
  1080. result = 0;
  1081. error:
  1082. return result;
  1083. }
  1084. /*
  1085. * Check a payload descriptor against the received data
  1086. *
  1087. * @i2400m: device descriptor
  1088. * @pld: payload descriptor
  1089. * @pl_itr: offset (in bytes) in the received buffer the payload is
  1090. * located
  1091. * @buf_size: size of the received buffer
  1092. *
  1093. * Given a payload descriptor (part of a RX buffer), check it is sane
  1094. * and that the data it declares fits in the buffer.
  1095. */
  1096. static
  1097. int i2400m_rx_pl_descr_check(struct i2400m *i2400m,
  1098. const struct i2400m_pld *pld,
  1099. size_t pl_itr, size_t buf_size)
  1100. {
  1101. int result = -EIO;
  1102. struct device *dev = i2400m_dev(i2400m);
  1103. size_t pl_size = i2400m_pld_size(pld);
  1104. enum i2400m_pt pl_type = i2400m_pld_type(pld);
  1105. if (pl_size > i2400m->bus_pl_size_max) {
  1106. dev_err(dev, "RX: HW BUG? payload @%zu: size %zu is "
  1107. "bigger than maximum %zu; ignoring message\n",
  1108. pl_itr, pl_size, i2400m->bus_pl_size_max);
  1109. goto error;
  1110. }
  1111. if (pl_itr + pl_size > buf_size) { /* enough? */
  1112. dev_err(dev, "RX: HW BUG? payload @%zu: size %zu "
  1113. "goes beyond the received buffer "
  1114. "size (%zu bytes); ignoring message\n",
  1115. pl_itr, pl_size, buf_size);
  1116. goto error;
  1117. }
  1118. if (pl_type >= I2400M_PT_ILLEGAL) {
  1119. dev_err(dev, "RX: HW BUG? illegal payload type %u; "
  1120. "ignoring message\n", pl_type);
  1121. goto error;
  1122. }
  1123. result = 0;
  1124. error:
  1125. return result;
  1126. }
  1127. /**
  1128. * i2400m_rx - Receive a buffer of data from the device
  1129. *
  1130. * @i2400m: device descriptor
  1131. * @skb: skbuff where the data has been received
  1132. *
  1133. * Parse in a buffer of data that contains an RX message sent from the
  1134. * device. See the file header for the format. Run all checks on the
  1135. * buffer header, then run over each payload's descriptors, verify
  1136. * their consistency and act on each payload's contents. If
  1137. * everything is successful, update the device's statistics.
  1138. *
  1139. * Note: You need to set the skb to contain only the length of the
  1140. * received buffer; for that, use skb_trim(skb, RECEIVED_SIZE).
  1141. *
  1142. * Returns:
  1143. *
  1144. * 0 if ok, < 0 errno on error
  1145. *
  1146. * If ok, this function owns now the skb and the caller DOESN'T have
  1147. * to run kfree_skb() on it. However, on error, the caller still owns
  1148. * the skb and it is responsible for releasing it.
  1149. */
  1150. int i2400m_rx(struct i2400m *i2400m, struct sk_buff *skb)
  1151. {
  1152. int i, result;
  1153. struct device *dev = i2400m_dev(i2400m);
  1154. const struct i2400m_msg_hdr *msg_hdr;
  1155. size_t pl_itr, pl_size;
  1156. unsigned long flags;
  1157. unsigned num_pls, single_last, skb_len;
  1158. skb_len = skb->len;
  1159. d_fnstart(4, dev, "(i2400m %p skb %p [size %u])\n",
  1160. i2400m, skb, skb_len);
  1161. result = -EIO;
  1162. msg_hdr = (void *) skb->data;
  1163. result = i2400m_rx_msg_hdr_check(i2400m, msg_hdr, skb_len);
  1164. if (result < 0)
  1165. goto error_msg_hdr_check;
  1166. result = -EIO;
  1167. num_pls = le16_to_cpu(msg_hdr->num_pls);
  1168. pl_itr = sizeof(*msg_hdr) + /* Check payload descriptor(s) */
  1169. num_pls * sizeof(msg_hdr->pld[0]);
  1170. pl_itr = ALIGN(pl_itr, I2400M_PL_ALIGN);
  1171. if (pl_itr > skb_len) { /* got all the payload descriptors? */
  1172. dev_err(dev, "RX: HW BUG? message too short (%u bytes) for "
  1173. "%u payload descriptors (%zu each, total %zu)\n",
  1174. skb_len, num_pls, sizeof(msg_hdr->pld[0]), pl_itr);
  1175. goto error_pl_descr_short;
  1176. }
  1177. /* Walk each payload payload--check we really got it */
  1178. for (i = 0; i < num_pls; i++) {
  1179. /* work around old gcc warnings */
  1180. pl_size = i2400m_pld_size(&msg_hdr->pld[i]);
  1181. result = i2400m_rx_pl_descr_check(i2400m, &msg_hdr->pld[i],
  1182. pl_itr, skb_len);
  1183. if (result < 0)
  1184. goto error_pl_descr_check;
  1185. single_last = num_pls == 1 || i == num_pls - 1;
  1186. i2400m_rx_payload(i2400m, skb, single_last, &msg_hdr->pld[i],
  1187. skb->data + pl_itr);
  1188. pl_itr += ALIGN(pl_size, I2400M_PL_ALIGN);
  1189. cond_resched(); /* Don't monopolize */
  1190. }
  1191. kfree_skb(skb);
  1192. /* Update device statistics */
  1193. spin_lock_irqsave(&i2400m->rx_lock, flags);
  1194. i2400m->rx_pl_num += i;
  1195. if (i > i2400m->rx_pl_max)
  1196. i2400m->rx_pl_max = i;
  1197. if (i < i2400m->rx_pl_min)
  1198. i2400m->rx_pl_min = i;
  1199. i2400m->rx_num++;
  1200. i2400m->rx_size_acc += skb_len;
  1201. if (skb_len < i2400m->rx_size_min)
  1202. i2400m->rx_size_min = skb_len;
  1203. if (skb_len > i2400m->rx_size_max)
  1204. i2400m->rx_size_max = skb_len;
  1205. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  1206. error_pl_descr_check:
  1207. error_pl_descr_short:
  1208. error_msg_hdr_check:
  1209. d_fnend(4, dev, "(i2400m %p skb %p [size %u]) = %d\n",
  1210. i2400m, skb, skb_len, result);
  1211. return result;
  1212. }
  1213. EXPORT_SYMBOL_GPL(i2400m_rx);
  1214. void i2400m_unknown_barker(struct i2400m *i2400m,
  1215. const void *buf, size_t size)
  1216. {
  1217. struct device *dev = i2400m_dev(i2400m);
  1218. char prefix[64];
  1219. const __le32 *barker = buf;
  1220. dev_err(dev, "RX: HW BUG? unknown barker %08x, "
  1221. "dropping %zu bytes\n", le32_to_cpu(*barker), size);
  1222. snprintf(prefix, sizeof(prefix), "%s %s: ",
  1223. dev_driver_string(dev), dev_name(dev));
  1224. if (size > 64) {
  1225. print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
  1226. 8, 4, buf, 64, 0);
  1227. printk(KERN_ERR "%s... (only first 64 bytes "
  1228. "dumped)\n", prefix);
  1229. } else
  1230. print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
  1231. 8, 4, buf, size, 0);
  1232. }
  1233. EXPORT_SYMBOL(i2400m_unknown_barker);
  1234. /*
  1235. * Initialize the RX queue and infrastructure
  1236. *
  1237. * This sets up all the RX reordering infrastructures, which will not
  1238. * be used if reordering is not enabled or if the firmware does not
  1239. * support it. The device is told to do reordering in
  1240. * i2400m_dev_initialize(), where it also looks at the value of the
  1241. * i2400m->rx_reorder switch before taking a decission.
  1242. *
  1243. * Note we allocate the roq queues in one chunk and the actual logging
  1244. * support for it (logging) in another one and then we setup the
  1245. * pointers from the first to the last.
  1246. */
  1247. int i2400m_rx_setup(struct i2400m *i2400m)
  1248. {
  1249. int result = 0;
  1250. struct device *dev = i2400m_dev(i2400m);
  1251. i2400m->rx_reorder = i2400m_rx_reorder_disabled? 0 : 1;
  1252. if (i2400m->rx_reorder) {
  1253. unsigned itr;
  1254. size_t size;
  1255. struct i2400m_roq_log *rd;
  1256. result = -ENOMEM;
  1257. size = sizeof(i2400m->rx_roq[0]) * (I2400M_RO_CIN + 1);
  1258. i2400m->rx_roq = kzalloc(size, GFP_KERNEL);
  1259. if (i2400m->rx_roq == NULL) {
  1260. dev_err(dev, "RX: cannot allocate %zu bytes for "
  1261. "reorder queues\n", size);
  1262. goto error_roq_alloc;
  1263. }
  1264. size = sizeof(*i2400m->rx_roq[0].log) * (I2400M_RO_CIN + 1);
  1265. rd = kzalloc(size, GFP_KERNEL);
  1266. if (rd == NULL) {
  1267. dev_err(dev, "RX: cannot allocate %zu bytes for "
  1268. "reorder queues log areas\n", size);
  1269. result = -ENOMEM;
  1270. goto error_roq_log_alloc;
  1271. }
  1272. for(itr = 0; itr < I2400M_RO_CIN + 1; itr++) {
  1273. __i2400m_roq_init(&i2400m->rx_roq[itr]);
  1274. i2400m->rx_roq[itr].log = &rd[itr];
  1275. }
  1276. kref_init(&i2400m->rx_roq_refcount);
  1277. }
  1278. return 0;
  1279. error_roq_log_alloc:
  1280. kfree(i2400m->rx_roq);
  1281. error_roq_alloc:
  1282. return result;
  1283. }
  1284. /* Tear down the RX queue and infrastructure */
  1285. void i2400m_rx_release(struct i2400m *i2400m)
  1286. {
  1287. unsigned long flags;
  1288. if (i2400m->rx_reorder) {
  1289. spin_lock_irqsave(&i2400m->rx_lock, flags);
  1290. kref_put(&i2400m->rx_roq_refcount, i2400m_rx_roq_destroy);
  1291. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  1292. }
  1293. /* at this point, nothing can be received... */
  1294. i2400m_report_hook_flush(i2400m);
  1295. }