rx.c 41 KB

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