rx.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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 "i2400m.h"
  154. #define D_SUBMODULE rx
  155. #include "debug-levels.h"
  156. static int i2400m_rx_reorder_disabled; /* 0 (rx reorder enabled) by default */
  157. module_param_named(rx_reorder_disabled, i2400m_rx_reorder_disabled, int, 0644);
  158. MODULE_PARM_DESC(rx_reorder_disabled,
  159. "If true, RX reordering will be disabled.");
  160. struct i2400m_report_hook_args {
  161. struct sk_buff *skb_rx;
  162. const struct i2400m_l3l4_hdr *l3l4_hdr;
  163. size_t size;
  164. struct list_head list_node;
  165. };
  166. /*
  167. * Execute i2400m_report_hook in a workqueue
  168. *
  169. * Goes over the list of queued reports in i2400m->rx_reports and
  170. * processes them.
  171. *
  172. * NOTE: refcounts on i2400m are not needed because we flush the
  173. * workqueue this runs on (i2400m->work_queue) before destroying
  174. * i2400m.
  175. */
  176. void i2400m_report_hook_work(struct work_struct *ws)
  177. {
  178. struct i2400m *i2400m = container_of(ws, struct i2400m, rx_report_ws);
  179. struct device *dev = i2400m_dev(i2400m);
  180. struct i2400m_report_hook_args *args, *args_next;
  181. LIST_HEAD(list);
  182. unsigned long flags;
  183. while (1) {
  184. spin_lock_irqsave(&i2400m->rx_lock, flags);
  185. list_splice_init(&i2400m->rx_reports, &list);
  186. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  187. if (list_empty(&list))
  188. break;
  189. else
  190. d_printf(1, dev, "processing queued reports\n");
  191. list_for_each_entry_safe(args, args_next, &list, list_node) {
  192. d_printf(2, dev, "processing queued report %p\n", args);
  193. i2400m_report_hook(i2400m, args->l3l4_hdr, args->size);
  194. kfree_skb(args->skb_rx);
  195. list_del(&args->list_node);
  196. kfree(args);
  197. }
  198. }
  199. }
  200. /*
  201. * Flush the list of queued reports
  202. */
  203. static
  204. void i2400m_report_hook_flush(struct i2400m *i2400m)
  205. {
  206. struct device *dev = i2400m_dev(i2400m);
  207. struct i2400m_report_hook_args *args, *args_next;
  208. LIST_HEAD(list);
  209. unsigned long flags;
  210. d_printf(1, dev, "flushing queued reports\n");
  211. spin_lock_irqsave(&i2400m->rx_lock, flags);
  212. list_splice_init(&i2400m->rx_reports, &list);
  213. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  214. list_for_each_entry_safe(args, args_next, &list, list_node) {
  215. d_printf(2, dev, "flushing queued report %p\n", args);
  216. kfree_skb(args->skb_rx);
  217. list_del(&args->list_node);
  218. kfree(args);
  219. }
  220. }
  221. /*
  222. * Queue a report for later processing
  223. *
  224. * @i2400m: device descriptor
  225. * @skb_rx: skb that contains the payload (for reference counting)
  226. * @l3l4_hdr: pointer to the control
  227. * @size: size of the message
  228. */
  229. static
  230. void i2400m_report_hook_queue(struct i2400m *i2400m, struct sk_buff *skb_rx,
  231. const void *l3l4_hdr, size_t size)
  232. {
  233. struct device *dev = i2400m_dev(i2400m);
  234. unsigned long flags;
  235. struct i2400m_report_hook_args *args;
  236. args = kzalloc(sizeof(*args), GFP_NOIO);
  237. if (args) {
  238. args->skb_rx = skb_get(skb_rx);
  239. args->l3l4_hdr = l3l4_hdr;
  240. args->size = size;
  241. spin_lock_irqsave(&i2400m->rx_lock, flags);
  242. list_add_tail(&args->list_node, &i2400m->rx_reports);
  243. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  244. d_printf(2, dev, "queued report %p\n", args);
  245. rmb(); /* see i2400m->ready's documentation */
  246. if (likely(i2400m->ready)) /* only send if up */
  247. queue_work(i2400m->work_queue, &i2400m->rx_report_ws);
  248. } else {
  249. if (printk_ratelimit())
  250. dev_err(dev, "%s:%u: Can't allocate %zu B\n",
  251. __func__, __LINE__, sizeof(*args));
  252. }
  253. }
  254. /*
  255. * Process an ack to a command
  256. *
  257. * @i2400m: device descriptor
  258. * @payload: pointer to message
  259. * @size: size of the message
  260. *
  261. * Pass the acknodledgment (in an skb) to the thread that is waiting
  262. * for it in i2400m->msg_completion.
  263. *
  264. * We need to coordinate properly with the thread waiting for the
  265. * ack. Check if it is waiting or if it is gone. We loose the spinlock
  266. * to avoid allocating on atomic contexts (yeah, could use GFP_ATOMIC,
  267. * but this is not so speed critical).
  268. */
  269. static
  270. void i2400m_rx_ctl_ack(struct i2400m *i2400m,
  271. const void *payload, size_t size)
  272. {
  273. struct device *dev = i2400m_dev(i2400m);
  274. struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  275. unsigned long flags;
  276. struct sk_buff *ack_skb;
  277. /* Anyone waiting for an answer? */
  278. spin_lock_irqsave(&i2400m->rx_lock, flags);
  279. if (i2400m->ack_skb != ERR_PTR(-EINPROGRESS)) {
  280. dev_err(dev, "Huh? reply to command with no waiters\n");
  281. goto error_no_waiter;
  282. }
  283. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  284. ack_skb = wimax_msg_alloc(wimax_dev, NULL, payload, size, GFP_KERNEL);
  285. /* Check waiter didn't time out waiting for the answer... */
  286. spin_lock_irqsave(&i2400m->rx_lock, flags);
  287. if (i2400m->ack_skb != ERR_PTR(-EINPROGRESS)) {
  288. d_printf(1, dev, "Huh? waiter for command reply cancelled\n");
  289. goto error_waiter_cancelled;
  290. }
  291. if (IS_ERR(ack_skb))
  292. dev_err(dev, "CMD/GET/SET ack: cannot allocate SKB\n");
  293. i2400m->ack_skb = ack_skb;
  294. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  295. complete(&i2400m->msg_completion);
  296. return;
  297. error_waiter_cancelled:
  298. if (!IS_ERR(ack_skb))
  299. kfree_skb(ack_skb);
  300. error_no_waiter:
  301. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  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 preceding 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 preceding 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. }
  672. /*
  673. * Backbone for the update window start operation
  674. *
  675. * @i2400m: device descriptor
  676. * @roq: Reorder queue
  677. * @sn: New sequence number
  678. *
  679. * Updates the window start of a queue; when doing so, it must deliver
  680. * to the networking stack all the queued skb's whose normalized
  681. * sequence number is lower than the new normalized window start.
  682. */
  683. static
  684. unsigned __i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
  685. unsigned sn)
  686. {
  687. struct device *dev = i2400m_dev(i2400m);
  688. struct sk_buff *skb_itr, *tmp_itr;
  689. struct i2400m_roq_data *roq_data_itr;
  690. unsigned new_nws, nsn_itr;
  691. new_nws = __i2400m_roq_nsn(roq, sn);
  692. /*
  693. * For type 2(update_window_start) rx messages, there is no
  694. * need to check if the normalized sequence number is greater 1023.
  695. * Simply insert and deliver all packets to the host up to the
  696. * window start.
  697. */
  698. skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
  699. roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
  700. nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
  701. /* NSN bounds assumed correct (checked when it was queued) */
  702. if (nsn_itr < new_nws) {
  703. d_printf(2, dev, "ERX: roq %p - release skb %p "
  704. "(nsn %u/%u new nws %u)\n",
  705. roq, skb_itr, nsn_itr, roq_data_itr->sn,
  706. new_nws);
  707. __skb_unlink(skb_itr, &roq->queue);
  708. i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
  709. }
  710. else
  711. break; /* rest of packets all nsn_itr > nws */
  712. }
  713. roq->ws = sn;
  714. return new_nws;
  715. }
  716. /*
  717. * Reset a queue
  718. *
  719. * @i2400m: device descriptor
  720. * @cin: Queue Index
  721. *
  722. * Deliver all the packets and reset the window-start to zero. Name is
  723. * kind of misleading.
  724. */
  725. static
  726. void i2400m_roq_reset(struct i2400m *i2400m, struct i2400m_roq *roq)
  727. {
  728. struct device *dev = i2400m_dev(i2400m);
  729. struct sk_buff *skb_itr, *tmp_itr;
  730. struct i2400m_roq_data *roq_data_itr;
  731. d_fnstart(2, dev, "(i2400m %p roq %p)\n", i2400m, roq);
  732. i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_RESET,
  733. roq->ws, skb_queue_len(&roq->queue),
  734. ~0, ~0, 0);
  735. skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
  736. roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
  737. d_printf(2, dev, "ERX: roq %p - release skb %p (sn %u)\n",
  738. roq, skb_itr, roq_data_itr->sn);
  739. __skb_unlink(skb_itr, &roq->queue);
  740. i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
  741. }
  742. roq->ws = 0;
  743. d_fnend(2, dev, "(i2400m %p roq %p) = void\n", i2400m, roq);
  744. }
  745. /*
  746. * Queue a packet
  747. *
  748. * @i2400m: device descriptor
  749. * @cin: Queue Index
  750. * @skb: containing the packet data
  751. * @fbn: First block number of the packet in @skb
  752. * @lbn: Last block number of the packet in @skb
  753. *
  754. * The hardware is asking the driver to queue a packet for later
  755. * delivery to the networking stack.
  756. */
  757. static
  758. void i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
  759. struct sk_buff * skb, unsigned lbn)
  760. {
  761. struct device *dev = i2400m_dev(i2400m);
  762. unsigned nsn, len;
  763. d_fnstart(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
  764. i2400m, roq, skb, lbn);
  765. len = skb_queue_len(&roq->queue);
  766. nsn = __i2400m_roq_nsn(roq, lbn);
  767. if (unlikely(nsn >= 1024)) {
  768. dev_err(dev, "SW BUG? queue nsn %d (lbn %u ws %u)\n",
  769. nsn, lbn, roq->ws);
  770. i2400m_roq_log_dump(i2400m, roq);
  771. i2400m_reset(i2400m, I2400M_RT_WARM);
  772. } else {
  773. __i2400m_roq_queue(i2400m, roq, skb, lbn, nsn);
  774. i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET,
  775. roq->ws, len, lbn, nsn, ~0);
  776. }
  777. d_fnend(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
  778. i2400m, roq, skb, lbn);
  779. }
  780. /*
  781. * Update the window start in a reorder queue and deliver all skbs
  782. * with a lower window start
  783. *
  784. * @i2400m: device descriptor
  785. * @roq: Reorder queue
  786. * @sn: New sequence number
  787. */
  788. static
  789. void i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
  790. unsigned sn)
  791. {
  792. struct device *dev = i2400m_dev(i2400m);
  793. unsigned old_ws, nsn, len;
  794. d_fnstart(2, dev, "(i2400m %p roq %p sn %u)\n", i2400m, roq, sn);
  795. old_ws = roq->ws;
  796. len = skb_queue_len(&roq->queue);
  797. nsn = __i2400m_roq_update_ws(i2400m, roq, sn);
  798. i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_WS,
  799. old_ws, len, sn, nsn, roq->ws);
  800. d_fnstart(2, dev, "(i2400m %p roq %p sn %u) = void\n", i2400m, roq, sn);
  801. }
  802. /*
  803. * Queue a packet and update the window start
  804. *
  805. * @i2400m: device descriptor
  806. * @cin: Queue Index
  807. * @skb: containing the packet data
  808. * @fbn: First block number of the packet in @skb
  809. * @sn: Last block number of the packet in @skb
  810. *
  811. * Note that unlike i2400m_roq_update_ws(), which sets the new window
  812. * start to @sn, in here we'll set it to @sn + 1.
  813. */
  814. static
  815. void i2400m_roq_queue_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
  816. struct sk_buff * skb, unsigned sn)
  817. {
  818. struct device *dev = i2400m_dev(i2400m);
  819. unsigned nsn, old_ws, len;
  820. d_fnstart(2, dev, "(i2400m %p roq %p skb %p sn %u)\n",
  821. i2400m, roq, skb, sn);
  822. len = skb_queue_len(&roq->queue);
  823. nsn = __i2400m_roq_nsn(roq, sn);
  824. /*
  825. * For type 3(queue_update_window_start) rx messages, there is no
  826. * need to check if the normalized sequence number is greater 1023.
  827. * Simply insert and deliver all packets to the host up to the
  828. * window start.
  829. */
  830. old_ws = roq->ws;
  831. /* If the queue is empty, don't bother as we'd queue
  832. * it and immediately unqueue it -- just deliver it.
  833. */
  834. if (len == 0) {
  835. struct i2400m_roq_data *roq_data;
  836. roq_data = (struct i2400m_roq_data *) &skb->cb;
  837. i2400m_net_erx(i2400m, skb, roq_data->cs);
  838. } else
  839. __i2400m_roq_queue(i2400m, roq, skb, sn, nsn);
  840. __i2400m_roq_update_ws(i2400m, roq, sn + 1);
  841. i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET_WS,
  842. old_ws, len, sn, nsn, roq->ws);
  843. d_fnend(2, dev, "(i2400m %p roq %p skb %p sn %u) = void\n",
  844. i2400m, roq, skb, sn);
  845. }
  846. /*
  847. * This routine destroys the memory allocated for rx_roq, when no
  848. * other thread is accessing it. Access to rx_roq is refcounted by
  849. * rx_roq_refcount, hence memory allocated must be destroyed when
  850. * rx_roq_refcount becomes zero. This routine gets executed when
  851. * rx_roq_refcount becomes zero.
  852. */
  853. static void i2400m_rx_roq_destroy(struct kref *ref)
  854. {
  855. unsigned itr;
  856. struct i2400m *i2400m
  857. = container_of(ref, struct i2400m, rx_roq_refcount);
  858. for (itr = 0; itr < I2400M_RO_CIN + 1; itr++)
  859. __skb_queue_purge(&i2400m->rx_roq[itr].queue);
  860. kfree(i2400m->rx_roq[0].log);
  861. kfree(i2400m->rx_roq);
  862. i2400m->rx_roq = NULL;
  863. }
  864. /*
  865. * Receive and send up an extended data packet
  866. *
  867. * @i2400m: device descriptor
  868. * @skb_rx: skb that contains the extended data packet
  869. * @single_last: 1 if the payload is the only one or the last one of
  870. * the skb.
  871. * @payload: pointer to the packet's data inside the skb
  872. * @size: size of the payload
  873. *
  874. * Starting in v1.4 of the i2400m's firmware, the device can send data
  875. * packets to the host in an extended format that; this incudes a 16
  876. * byte header (struct i2400m_pl_edata_hdr). Using this header's space
  877. * we can fake ethernet headers for ethernet device emulation without
  878. * having to copy packets around.
  879. *
  880. * This function handles said path.
  881. *
  882. *
  883. * Receive and send up an extended data packet that requires no reordering
  884. *
  885. * @i2400m: device descriptor
  886. * @skb_rx: skb that contains the extended data packet
  887. * @single_last: 1 if the payload is the only one or the last one of
  888. * the skb.
  889. * @payload: pointer to the packet's data (past the actual extended
  890. * data payload header).
  891. * @size: size of the payload
  892. *
  893. * Pass over to the networking stack a data packet that might have
  894. * reordering requirements.
  895. *
  896. * This needs to the decide if the skb in which the packet is
  897. * contained can be reused or if it needs to be cloned. Then it has to
  898. * be trimmed in the edges so that the beginning is the space for eth
  899. * header and then pass it to i2400m_net_erx() for the stack
  900. *
  901. * Assumes the caller has verified the sanity of the payload (size,
  902. * etc) already.
  903. */
  904. static
  905. void i2400m_rx_edata(struct i2400m *i2400m, struct sk_buff *skb_rx,
  906. unsigned single_last, const void *payload, size_t size)
  907. {
  908. struct device *dev = i2400m_dev(i2400m);
  909. const struct i2400m_pl_edata_hdr *hdr = payload;
  910. struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  911. struct sk_buff *skb;
  912. enum i2400m_cs cs;
  913. u32 reorder;
  914. unsigned ro_needed, ro_type, ro_cin, ro_sn;
  915. struct i2400m_roq *roq;
  916. struct i2400m_roq_data *roq_data;
  917. unsigned long flags;
  918. BUILD_BUG_ON(ETH_HLEN > sizeof(*hdr));
  919. d_fnstart(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
  920. "size %zu)\n", i2400m, skb_rx, single_last, payload, size);
  921. if (size < sizeof(*hdr)) {
  922. dev_err(dev, "ERX: HW BUG? message with short header (%zu "
  923. "vs %zu bytes expected)\n", size, sizeof(*hdr));
  924. goto error;
  925. }
  926. if (single_last) {
  927. skb = skb_get(skb_rx);
  928. d_printf(3, dev, "ERX: skb %p reusing\n", skb);
  929. } else {
  930. skb = skb_clone(skb_rx, GFP_KERNEL);
  931. if (skb == NULL) {
  932. dev_err(dev, "ERX: no memory to clone skb\n");
  933. net_dev->stats.rx_dropped++;
  934. goto error_skb_clone;
  935. }
  936. d_printf(3, dev, "ERX: skb %p cloned from %p\n", skb, skb_rx);
  937. }
  938. /* now we have to pull and trim so that the skb points to the
  939. * beginning of the IP packet; the netdev part will add the
  940. * ethernet header as needed - we know there is enough space
  941. * because we checked in i2400m_rx_edata(). */
  942. skb_pull(skb, payload + sizeof(*hdr) - (void *) skb->data);
  943. skb_trim(skb, (void *) skb_end_pointer(skb) - payload - sizeof(*hdr));
  944. reorder = le32_to_cpu(hdr->reorder);
  945. ro_needed = reorder & I2400M_RO_NEEDED;
  946. cs = hdr->cs;
  947. if (ro_needed) {
  948. ro_type = (reorder >> I2400M_RO_TYPE_SHIFT) & I2400M_RO_TYPE;
  949. ro_cin = (reorder >> I2400M_RO_CIN_SHIFT) & I2400M_RO_CIN;
  950. ro_sn = (reorder >> I2400M_RO_SN_SHIFT) & I2400M_RO_SN;
  951. spin_lock_irqsave(&i2400m->rx_lock, flags);
  952. if (i2400m->rx_roq == NULL) {
  953. kfree_skb(skb); /* rx_roq is already destroyed */
  954. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  955. goto error;
  956. }
  957. roq = &i2400m->rx_roq[ro_cin];
  958. kref_get(&i2400m->rx_roq_refcount);
  959. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  960. roq_data = (struct i2400m_roq_data *) &skb->cb;
  961. roq_data->sn = ro_sn;
  962. roq_data->cs = cs;
  963. d_printf(2, dev, "ERX: reorder needed: "
  964. "type %u cin %u [ws %u] sn %u/%u len %zuB\n",
  965. ro_type, ro_cin, roq->ws, ro_sn,
  966. __i2400m_roq_nsn(roq, ro_sn), size);
  967. d_dump(2, dev, payload, size);
  968. switch(ro_type) {
  969. case I2400M_RO_TYPE_RESET:
  970. i2400m_roq_reset(i2400m, roq);
  971. kfree_skb(skb); /* no data here */
  972. break;
  973. case I2400M_RO_TYPE_PACKET:
  974. i2400m_roq_queue(i2400m, roq, skb, ro_sn);
  975. break;
  976. case I2400M_RO_TYPE_WS:
  977. i2400m_roq_update_ws(i2400m, roq, ro_sn);
  978. kfree_skb(skb); /* no data here */
  979. break;
  980. case I2400M_RO_TYPE_PACKET_WS:
  981. i2400m_roq_queue_update_ws(i2400m, roq, skb, ro_sn);
  982. break;
  983. default:
  984. dev_err(dev, "HW BUG? unknown reorder type %u\n", ro_type);
  985. }
  986. spin_lock_irqsave(&i2400m->rx_lock, flags);
  987. kref_put(&i2400m->rx_roq_refcount, i2400m_rx_roq_destroy);
  988. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  989. }
  990. else
  991. i2400m_net_erx(i2400m, skb, cs);
  992. error_skb_clone:
  993. error:
  994. d_fnend(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
  995. "size %zu) = void\n", i2400m, skb_rx, single_last, payload, size);
  996. }
  997. /*
  998. * Act on a received payload
  999. *
  1000. * @i2400m: device instance
  1001. * @skb_rx: skb where the transaction was received
  1002. * @single_last: 1 this is the only payload or the last one (so the
  1003. * skb can be reused instead of cloned).
  1004. * @pld: payload descriptor
  1005. * @payload: payload data
  1006. *
  1007. * Upon reception of a payload, look at its guts in the payload
  1008. * descriptor and decide what to do with it. If it is a single payload
  1009. * skb or if the last skb is a data packet, the skb will be referenced
  1010. * and modified (so it doesn't have to be cloned).
  1011. */
  1012. static
  1013. void i2400m_rx_payload(struct i2400m *i2400m, struct sk_buff *skb_rx,
  1014. unsigned single_last, const struct i2400m_pld *pld,
  1015. const void *payload)
  1016. {
  1017. struct device *dev = i2400m_dev(i2400m);
  1018. size_t pl_size = i2400m_pld_size(pld);
  1019. enum i2400m_pt pl_type = i2400m_pld_type(pld);
  1020. d_printf(7, dev, "RX: received payload type %u, %zu bytes\n",
  1021. pl_type, pl_size);
  1022. d_dump(8, dev, payload, pl_size);
  1023. switch (pl_type) {
  1024. case I2400M_PT_DATA:
  1025. d_printf(3, dev, "RX: data payload %zu bytes\n", pl_size);
  1026. i2400m_net_rx(i2400m, skb_rx, single_last, payload, pl_size);
  1027. break;
  1028. case I2400M_PT_CTRL:
  1029. i2400m_rx_ctl(i2400m, skb_rx, payload, pl_size);
  1030. break;
  1031. case I2400M_PT_TRACE:
  1032. i2400m_rx_trace(i2400m, payload, pl_size);
  1033. break;
  1034. case I2400M_PT_EDATA:
  1035. d_printf(3, dev, "ERX: data payload %zu bytes\n", pl_size);
  1036. i2400m_rx_edata(i2400m, skb_rx, single_last, payload, pl_size);
  1037. break;
  1038. default: /* Anything else shouldn't come to the host */
  1039. if (printk_ratelimit())
  1040. dev_err(dev, "RX: HW BUG? unexpected payload type %u\n",
  1041. pl_type);
  1042. }
  1043. }
  1044. /*
  1045. * Check a received transaction's message header
  1046. *
  1047. * @i2400m: device descriptor
  1048. * @msg_hdr: message header
  1049. * @buf_size: size of the received buffer
  1050. *
  1051. * Check that the declarations done by a RX buffer message header are
  1052. * sane and consistent with the amount of data that was received.
  1053. */
  1054. static
  1055. int i2400m_rx_msg_hdr_check(struct i2400m *i2400m,
  1056. const struct i2400m_msg_hdr *msg_hdr,
  1057. size_t buf_size)
  1058. {
  1059. int result = -EIO;
  1060. struct device *dev = i2400m_dev(i2400m);
  1061. if (buf_size < sizeof(*msg_hdr)) {
  1062. dev_err(dev, "RX: HW BUG? message with short header (%zu "
  1063. "vs %zu bytes expected)\n", buf_size, sizeof(*msg_hdr));
  1064. goto error;
  1065. }
  1066. if (msg_hdr->barker != cpu_to_le32(I2400M_D2H_MSG_BARKER)) {
  1067. dev_err(dev, "RX: HW BUG? message received with unknown "
  1068. "barker 0x%08x (buf_size %zu bytes)\n",
  1069. le32_to_cpu(msg_hdr->barker), buf_size);
  1070. goto error;
  1071. }
  1072. if (msg_hdr->num_pls == 0) {
  1073. dev_err(dev, "RX: HW BUG? zero payload packets in message\n");
  1074. goto error;
  1075. }
  1076. if (le16_to_cpu(msg_hdr->num_pls) > I2400M_MAX_PLS_IN_MSG) {
  1077. dev_err(dev, "RX: HW BUG? message contains more payload "
  1078. "than maximum; ignoring.\n");
  1079. goto error;
  1080. }
  1081. result = 0;
  1082. error:
  1083. return result;
  1084. }
  1085. /*
  1086. * Check a payload descriptor against the received data
  1087. *
  1088. * @i2400m: device descriptor
  1089. * @pld: payload descriptor
  1090. * @pl_itr: offset (in bytes) in the received buffer the payload is
  1091. * located
  1092. * @buf_size: size of the received buffer
  1093. *
  1094. * Given a payload descriptor (part of a RX buffer), check it is sane
  1095. * and that the data it declares fits in the buffer.
  1096. */
  1097. static
  1098. int i2400m_rx_pl_descr_check(struct i2400m *i2400m,
  1099. const struct i2400m_pld *pld,
  1100. size_t pl_itr, size_t buf_size)
  1101. {
  1102. int result = -EIO;
  1103. struct device *dev = i2400m_dev(i2400m);
  1104. size_t pl_size = i2400m_pld_size(pld);
  1105. enum i2400m_pt pl_type = i2400m_pld_type(pld);
  1106. if (pl_size > i2400m->bus_pl_size_max) {
  1107. dev_err(dev, "RX: HW BUG? payload @%zu: size %zu is "
  1108. "bigger than maximum %zu; ignoring message\n",
  1109. pl_itr, pl_size, i2400m->bus_pl_size_max);
  1110. goto error;
  1111. }
  1112. if (pl_itr + pl_size > buf_size) { /* enough? */
  1113. dev_err(dev, "RX: HW BUG? payload @%zu: size %zu "
  1114. "goes beyond the received buffer "
  1115. "size (%zu bytes); ignoring message\n",
  1116. pl_itr, pl_size, buf_size);
  1117. goto error;
  1118. }
  1119. if (pl_type >= I2400M_PT_ILLEGAL) {
  1120. dev_err(dev, "RX: HW BUG? illegal payload type %u; "
  1121. "ignoring message\n", pl_type);
  1122. goto error;
  1123. }
  1124. result = 0;
  1125. error:
  1126. return result;
  1127. }
  1128. /**
  1129. * i2400m_rx - Receive a buffer of data from the device
  1130. *
  1131. * @i2400m: device descriptor
  1132. * @skb: skbuff where the data has been received
  1133. *
  1134. * Parse in a buffer of data that contains an RX message sent from the
  1135. * device. See the file header for the format. Run all checks on the
  1136. * buffer header, then run over each payload's descriptors, verify
  1137. * their consistency and act on each payload's contents. If
  1138. * everything is successful, update the device's statistics.
  1139. *
  1140. * Note: You need to set the skb to contain only the length of the
  1141. * received buffer; for that, use skb_trim(skb, RECEIVED_SIZE).
  1142. *
  1143. * Returns:
  1144. *
  1145. * 0 if ok, < 0 errno on error
  1146. *
  1147. * If ok, this function owns now the skb and the caller DOESN'T have
  1148. * to run kfree_skb() on it. However, on error, the caller still owns
  1149. * the skb and it is responsible for releasing it.
  1150. */
  1151. int i2400m_rx(struct i2400m *i2400m, struct sk_buff *skb)
  1152. {
  1153. int i, result;
  1154. struct device *dev = i2400m_dev(i2400m);
  1155. const struct i2400m_msg_hdr *msg_hdr;
  1156. size_t pl_itr, pl_size;
  1157. unsigned long flags;
  1158. unsigned num_pls, single_last, skb_len;
  1159. skb_len = skb->len;
  1160. d_fnstart(4, dev, "(i2400m %p skb %p [size %u])\n",
  1161. i2400m, skb, skb_len);
  1162. result = -EIO;
  1163. msg_hdr = (void *) skb->data;
  1164. result = i2400m_rx_msg_hdr_check(i2400m, msg_hdr, skb_len);
  1165. if (result < 0)
  1166. goto error_msg_hdr_check;
  1167. result = -EIO;
  1168. num_pls = le16_to_cpu(msg_hdr->num_pls);
  1169. pl_itr = sizeof(*msg_hdr) + /* Check payload descriptor(s) */
  1170. num_pls * sizeof(msg_hdr->pld[0]);
  1171. pl_itr = ALIGN(pl_itr, I2400M_PL_ALIGN);
  1172. if (pl_itr > skb_len) { /* got all the payload descriptors? */
  1173. dev_err(dev, "RX: HW BUG? message too short (%u bytes) for "
  1174. "%u payload descriptors (%zu each, total %zu)\n",
  1175. skb_len, num_pls, sizeof(msg_hdr->pld[0]), pl_itr);
  1176. goto error_pl_descr_short;
  1177. }
  1178. /* Walk each payload payload--check we really got it */
  1179. for (i = 0; i < num_pls; i++) {
  1180. /* work around old gcc warnings */
  1181. pl_size = i2400m_pld_size(&msg_hdr->pld[i]);
  1182. result = i2400m_rx_pl_descr_check(i2400m, &msg_hdr->pld[i],
  1183. pl_itr, skb_len);
  1184. if (result < 0)
  1185. goto error_pl_descr_check;
  1186. single_last = num_pls == 1 || i == num_pls - 1;
  1187. i2400m_rx_payload(i2400m, skb, single_last, &msg_hdr->pld[i],
  1188. skb->data + pl_itr);
  1189. pl_itr += ALIGN(pl_size, I2400M_PL_ALIGN);
  1190. cond_resched(); /* Don't monopolize */
  1191. }
  1192. kfree_skb(skb);
  1193. /* Update device statistics */
  1194. spin_lock_irqsave(&i2400m->rx_lock, flags);
  1195. i2400m->rx_pl_num += i;
  1196. if (i > i2400m->rx_pl_max)
  1197. i2400m->rx_pl_max = i;
  1198. if (i < i2400m->rx_pl_min)
  1199. i2400m->rx_pl_min = i;
  1200. i2400m->rx_num++;
  1201. i2400m->rx_size_acc += skb_len;
  1202. if (skb_len < i2400m->rx_size_min)
  1203. i2400m->rx_size_min = skb_len;
  1204. if (skb_len > i2400m->rx_size_max)
  1205. i2400m->rx_size_max = skb_len;
  1206. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  1207. error_pl_descr_check:
  1208. error_pl_descr_short:
  1209. error_msg_hdr_check:
  1210. d_fnend(4, dev, "(i2400m %p skb %p [size %u]) = %d\n",
  1211. i2400m, skb, skb_len, result);
  1212. return result;
  1213. }
  1214. EXPORT_SYMBOL_GPL(i2400m_rx);
  1215. void i2400m_unknown_barker(struct i2400m *i2400m,
  1216. const void *buf, size_t size)
  1217. {
  1218. struct device *dev = i2400m_dev(i2400m);
  1219. char prefix[64];
  1220. const __le32 *barker = buf;
  1221. dev_err(dev, "RX: HW BUG? unknown barker %08x, "
  1222. "dropping %zu bytes\n", le32_to_cpu(*barker), size);
  1223. snprintf(prefix, sizeof(prefix), "%s %s: ",
  1224. dev_driver_string(dev), dev_name(dev));
  1225. if (size > 64) {
  1226. print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
  1227. 8, 4, buf, 64, 0);
  1228. printk(KERN_ERR "%s... (only first 64 bytes "
  1229. "dumped)\n", prefix);
  1230. } else
  1231. print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
  1232. 8, 4, buf, size, 0);
  1233. }
  1234. EXPORT_SYMBOL(i2400m_unknown_barker);
  1235. /*
  1236. * Initialize the RX queue and infrastructure
  1237. *
  1238. * This sets up all the RX reordering infrastructures, which will not
  1239. * be used if reordering is not enabled or if the firmware does not
  1240. * support it. The device is told to do reordering in
  1241. * i2400m_dev_initialize(), where it also looks at the value of the
  1242. * i2400m->rx_reorder switch before taking a decission.
  1243. *
  1244. * Note we allocate the roq queues in one chunk and the actual logging
  1245. * support for it (logging) in another one and then we setup the
  1246. * pointers from the first to the last.
  1247. */
  1248. int i2400m_rx_setup(struct i2400m *i2400m)
  1249. {
  1250. int result = 0;
  1251. struct device *dev = i2400m_dev(i2400m);
  1252. i2400m->rx_reorder = i2400m_rx_reorder_disabled? 0 : 1;
  1253. if (i2400m->rx_reorder) {
  1254. unsigned itr;
  1255. size_t size;
  1256. struct i2400m_roq_log *rd;
  1257. result = -ENOMEM;
  1258. size = sizeof(i2400m->rx_roq[0]) * (I2400M_RO_CIN + 1);
  1259. i2400m->rx_roq = kzalloc(size, GFP_KERNEL);
  1260. if (i2400m->rx_roq == NULL) {
  1261. dev_err(dev, "RX: cannot allocate %zu bytes for "
  1262. "reorder queues\n", size);
  1263. goto error_roq_alloc;
  1264. }
  1265. size = sizeof(*i2400m->rx_roq[0].log) * (I2400M_RO_CIN + 1);
  1266. rd = kzalloc(size, GFP_KERNEL);
  1267. if (rd == NULL) {
  1268. dev_err(dev, "RX: cannot allocate %zu bytes for "
  1269. "reorder queues log areas\n", size);
  1270. result = -ENOMEM;
  1271. goto error_roq_log_alloc;
  1272. }
  1273. for(itr = 0; itr < I2400M_RO_CIN + 1; itr++) {
  1274. __i2400m_roq_init(&i2400m->rx_roq[itr]);
  1275. i2400m->rx_roq[itr].log = &rd[itr];
  1276. }
  1277. kref_init(&i2400m->rx_roq_refcount);
  1278. }
  1279. return 0;
  1280. error_roq_log_alloc:
  1281. kfree(i2400m->rx_roq);
  1282. error_roq_alloc:
  1283. return result;
  1284. }
  1285. /* Tear down the RX queue and infrastructure */
  1286. void i2400m_rx_release(struct i2400m *i2400m)
  1287. {
  1288. unsigned long flags;
  1289. if (i2400m->rx_reorder) {
  1290. spin_lock_irqsave(&i2400m->rx_lock, flags);
  1291. kref_put(&i2400m->rx_roq_refcount, i2400m_rx_roq_destroy);
  1292. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  1293. }
  1294. /* at this point, nothing can be received... */
  1295. i2400m_report_hook_flush(i2400m);
  1296. }