rx.c 41 KB

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