rx.c 43 KB

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