rx.c 38 KB

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