rx.c 38 KB

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