rx.c 43 KB

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