rx.c 38 KB

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