rx.c 43 KB

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