caif_hsi.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. /*
  2. * Copyright (C) ST-Ericsson AB 2010
  3. * Author: Daniel Martensson
  4. * Dmitry.Tarnyagin / dmitry.tarnyagin@lockless.no
  5. * License terms: GNU General Public License (GPL) version 2.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME fmt
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/device.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/string.h>
  13. #include <linux/list.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/delay.h>
  16. #include <linux/sched.h>
  17. #include <linux/if_arp.h>
  18. #include <linux/timer.h>
  19. #include <net/rtnetlink.h>
  20. #include <linux/pkt_sched.h>
  21. #include <net/caif/caif_layer.h>
  22. #include <net/caif/caif_hsi.h>
  23. MODULE_LICENSE("GPL");
  24. MODULE_AUTHOR("Daniel Martensson");
  25. MODULE_DESCRIPTION("CAIF HSI driver");
  26. /* Returns the number of padding bytes for alignment. */
  27. #define PAD_POW2(x, pow) ((((x)&((pow)-1)) == 0) ? 0 :\
  28. (((pow)-((x)&((pow)-1)))))
  29. static const struct cfhsi_config hsi_default_config = {
  30. /* Inactivity timeout on HSI, ms */
  31. .inactivity_timeout = HZ,
  32. /* Aggregation timeout (ms) of zero means no aggregation is done*/
  33. .aggregation_timeout = 1,
  34. /*
  35. * HSI link layer flow-control thresholds.
  36. * Threshold values for the HSI packet queue. Flow-control will be
  37. * asserted when the number of packets exceeds q_high_mark. It will
  38. * not be de-asserted before the number of packets drops below
  39. * q_low_mark.
  40. * Warning: A high threshold value might increase throughput but it
  41. * will at the same time prevent channel prioritization and increase
  42. * the risk of flooding the modem. The high threshold should be above
  43. * the low.
  44. */
  45. .q_high_mark = 100,
  46. .q_low_mark = 50,
  47. /*
  48. * HSI padding options.
  49. * Warning: must be a base of 2 (& operation used) and can not be zero !
  50. */
  51. .head_align = 4,
  52. .tail_align = 4,
  53. };
  54. #define ON 1
  55. #define OFF 0
  56. static LIST_HEAD(cfhsi_list);
  57. static void cfhsi_inactivity_tout(unsigned long arg)
  58. {
  59. struct cfhsi *cfhsi = (struct cfhsi *)arg;
  60. netdev_dbg(cfhsi->ndev, "%s.\n",
  61. __func__);
  62. /* Schedule power down work queue. */
  63. if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  64. queue_work(cfhsi->wq, &cfhsi->wake_down_work);
  65. }
  66. static void cfhsi_update_aggregation_stats(struct cfhsi *cfhsi,
  67. const struct sk_buff *skb,
  68. int direction)
  69. {
  70. struct caif_payload_info *info;
  71. int hpad, tpad, len;
  72. info = (struct caif_payload_info *)&skb->cb;
  73. hpad = 1 + PAD_POW2((info->hdr_len + 1), cfhsi->cfg.head_align);
  74. tpad = PAD_POW2((skb->len + hpad), cfhsi->cfg.tail_align);
  75. len = skb->len + hpad + tpad;
  76. if (direction > 0)
  77. cfhsi->aggregation_len += len;
  78. else if (direction < 0)
  79. cfhsi->aggregation_len -= len;
  80. }
  81. static bool cfhsi_can_send_aggregate(struct cfhsi *cfhsi)
  82. {
  83. int i;
  84. if (cfhsi->cfg.aggregation_timeout == 0)
  85. return true;
  86. for (i = 0; i < CFHSI_PRIO_BEBK; ++i) {
  87. if (cfhsi->qhead[i].qlen)
  88. return true;
  89. }
  90. /* TODO: Use aggregation_len instead */
  91. if (cfhsi->qhead[CFHSI_PRIO_BEBK].qlen >= CFHSI_MAX_PKTS)
  92. return true;
  93. return false;
  94. }
  95. static struct sk_buff *cfhsi_dequeue(struct cfhsi *cfhsi)
  96. {
  97. struct sk_buff *skb;
  98. int i;
  99. for (i = 0; i < CFHSI_PRIO_LAST; ++i) {
  100. skb = skb_dequeue(&cfhsi->qhead[i]);
  101. if (skb)
  102. break;
  103. }
  104. return skb;
  105. }
  106. static int cfhsi_tx_queue_len(struct cfhsi *cfhsi)
  107. {
  108. int i, len = 0;
  109. for (i = 0; i < CFHSI_PRIO_LAST; ++i)
  110. len += skb_queue_len(&cfhsi->qhead[i]);
  111. return len;
  112. }
  113. static void cfhsi_abort_tx(struct cfhsi *cfhsi)
  114. {
  115. struct sk_buff *skb;
  116. for (;;) {
  117. spin_lock_bh(&cfhsi->lock);
  118. skb = cfhsi_dequeue(cfhsi);
  119. if (!skb)
  120. break;
  121. cfhsi->ndev->stats.tx_errors++;
  122. cfhsi->ndev->stats.tx_dropped++;
  123. cfhsi_update_aggregation_stats(cfhsi, skb, -1);
  124. spin_unlock_bh(&cfhsi->lock);
  125. kfree_skb(skb);
  126. }
  127. cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
  128. if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  129. mod_timer(&cfhsi->inactivity_timer,
  130. jiffies + cfhsi->cfg.inactivity_timeout);
  131. spin_unlock_bh(&cfhsi->lock);
  132. }
  133. static int cfhsi_flush_fifo(struct cfhsi *cfhsi)
  134. {
  135. char buffer[32]; /* Any reasonable value */
  136. size_t fifo_occupancy;
  137. int ret;
  138. netdev_dbg(cfhsi->ndev, "%s.\n",
  139. __func__);
  140. do {
  141. ret = cfhsi->ops->cfhsi_fifo_occupancy(cfhsi->ops,
  142. &fifo_occupancy);
  143. if (ret) {
  144. netdev_warn(cfhsi->ndev,
  145. "%s: can't get FIFO occupancy: %d.\n",
  146. __func__, ret);
  147. break;
  148. } else if (!fifo_occupancy)
  149. /* No more data, exitting normally */
  150. break;
  151. fifo_occupancy = min(sizeof(buffer), fifo_occupancy);
  152. set_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
  153. ret = cfhsi->ops->cfhsi_rx(buffer, fifo_occupancy,
  154. cfhsi->ops);
  155. if (ret) {
  156. clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
  157. netdev_warn(cfhsi->ndev,
  158. "%s: can't read data: %d.\n",
  159. __func__, ret);
  160. break;
  161. }
  162. ret = 5 * HZ;
  163. ret = wait_event_interruptible_timeout(cfhsi->flush_fifo_wait,
  164. !test_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits), ret);
  165. if (ret < 0) {
  166. netdev_warn(cfhsi->ndev,
  167. "%s: can't wait for flush complete: %d.\n",
  168. __func__, ret);
  169. break;
  170. } else if (!ret) {
  171. ret = -ETIMEDOUT;
  172. netdev_warn(cfhsi->ndev,
  173. "%s: timeout waiting for flush complete.\n",
  174. __func__);
  175. break;
  176. }
  177. } while (1);
  178. return ret;
  179. }
  180. static int cfhsi_tx_frm(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
  181. {
  182. int nfrms = 0;
  183. int pld_len = 0;
  184. struct sk_buff *skb;
  185. u8 *pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
  186. skb = cfhsi_dequeue(cfhsi);
  187. if (!skb)
  188. return 0;
  189. /* Clear offset. */
  190. desc->offset = 0;
  191. /* Check if we can embed a CAIF frame. */
  192. if (skb->len < CFHSI_MAX_EMB_FRM_SZ) {
  193. struct caif_payload_info *info;
  194. int hpad;
  195. int tpad;
  196. /* Calculate needed head alignment and tail alignment. */
  197. info = (struct caif_payload_info *)&skb->cb;
  198. hpad = 1 + PAD_POW2((info->hdr_len + 1), cfhsi->cfg.head_align);
  199. tpad = PAD_POW2((skb->len + hpad), cfhsi->cfg.tail_align);
  200. /* Check if frame still fits with added alignment. */
  201. if ((skb->len + hpad + tpad) <= CFHSI_MAX_EMB_FRM_SZ) {
  202. u8 *pemb = desc->emb_frm;
  203. desc->offset = CFHSI_DESC_SHORT_SZ;
  204. *pemb = (u8)(hpad - 1);
  205. pemb += hpad;
  206. /* Update network statistics. */
  207. spin_lock_bh(&cfhsi->lock);
  208. cfhsi->ndev->stats.tx_packets++;
  209. cfhsi->ndev->stats.tx_bytes += skb->len;
  210. cfhsi_update_aggregation_stats(cfhsi, skb, -1);
  211. spin_unlock_bh(&cfhsi->lock);
  212. /* Copy in embedded CAIF frame. */
  213. skb_copy_bits(skb, 0, pemb, skb->len);
  214. /* Consume the SKB */
  215. consume_skb(skb);
  216. skb = NULL;
  217. }
  218. }
  219. /* Create payload CAIF frames. */
  220. pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
  221. while (nfrms < CFHSI_MAX_PKTS) {
  222. struct caif_payload_info *info;
  223. int hpad;
  224. int tpad;
  225. if (!skb)
  226. skb = cfhsi_dequeue(cfhsi);
  227. if (!skb)
  228. break;
  229. /* Calculate needed head alignment and tail alignment. */
  230. info = (struct caif_payload_info *)&skb->cb;
  231. hpad = 1 + PAD_POW2((info->hdr_len + 1), cfhsi->cfg.head_align);
  232. tpad = PAD_POW2((skb->len + hpad), cfhsi->cfg.tail_align);
  233. /* Fill in CAIF frame length in descriptor. */
  234. desc->cffrm_len[nfrms] = hpad + skb->len + tpad;
  235. /* Fill head padding information. */
  236. *pfrm = (u8)(hpad - 1);
  237. pfrm += hpad;
  238. /* Update network statistics. */
  239. spin_lock_bh(&cfhsi->lock);
  240. cfhsi->ndev->stats.tx_packets++;
  241. cfhsi->ndev->stats.tx_bytes += skb->len;
  242. cfhsi_update_aggregation_stats(cfhsi, skb, -1);
  243. spin_unlock_bh(&cfhsi->lock);
  244. /* Copy in CAIF frame. */
  245. skb_copy_bits(skb, 0, pfrm, skb->len);
  246. /* Update payload length. */
  247. pld_len += desc->cffrm_len[nfrms];
  248. /* Update frame pointer. */
  249. pfrm += skb->len + tpad;
  250. /* Consume the SKB */
  251. consume_skb(skb);
  252. skb = NULL;
  253. /* Update number of frames. */
  254. nfrms++;
  255. }
  256. /* Unused length fields should be zero-filled (according to SPEC). */
  257. while (nfrms < CFHSI_MAX_PKTS) {
  258. desc->cffrm_len[nfrms] = 0x0000;
  259. nfrms++;
  260. }
  261. /* Check if we can piggy-back another descriptor. */
  262. if (cfhsi_can_send_aggregate(cfhsi))
  263. desc->header |= CFHSI_PIGGY_DESC;
  264. else
  265. desc->header &= ~CFHSI_PIGGY_DESC;
  266. return CFHSI_DESC_SZ + pld_len;
  267. }
  268. static void cfhsi_start_tx(struct cfhsi *cfhsi)
  269. {
  270. struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
  271. int len, res;
  272. netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
  273. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  274. return;
  275. do {
  276. /* Create HSI frame. */
  277. len = cfhsi_tx_frm(desc, cfhsi);
  278. if (!len) {
  279. spin_lock_bh(&cfhsi->lock);
  280. if (unlikely(cfhsi_tx_queue_len(cfhsi))) {
  281. spin_unlock_bh(&cfhsi->lock);
  282. res = -EAGAIN;
  283. continue;
  284. }
  285. cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
  286. /* Start inactivity timer. */
  287. mod_timer(&cfhsi->inactivity_timer,
  288. jiffies + cfhsi->cfg.inactivity_timeout);
  289. spin_unlock_bh(&cfhsi->lock);
  290. break;
  291. }
  292. /* Set up new transfer. */
  293. res = cfhsi->ops->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->ops);
  294. if (WARN_ON(res < 0))
  295. netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
  296. __func__, res);
  297. } while (res < 0);
  298. }
  299. static void cfhsi_tx_done(struct cfhsi *cfhsi)
  300. {
  301. netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
  302. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  303. return;
  304. /*
  305. * Send flow on if flow off has been previously signalled
  306. * and number of packets is below low water mark.
  307. */
  308. spin_lock_bh(&cfhsi->lock);
  309. if (cfhsi->flow_off_sent &&
  310. cfhsi_tx_queue_len(cfhsi) <= cfhsi->cfg.q_low_mark &&
  311. cfhsi->cfdev.flowctrl) {
  312. cfhsi->flow_off_sent = 0;
  313. cfhsi->cfdev.flowctrl(cfhsi->ndev, ON);
  314. }
  315. if (cfhsi_can_send_aggregate(cfhsi)) {
  316. spin_unlock_bh(&cfhsi->lock);
  317. cfhsi_start_tx(cfhsi);
  318. } else {
  319. mod_timer(&cfhsi->aggregation_timer,
  320. jiffies + cfhsi->cfg.aggregation_timeout);
  321. spin_unlock_bh(&cfhsi->lock);
  322. }
  323. return;
  324. }
  325. static void cfhsi_tx_done_cb(struct cfhsi_cb_ops *cb_ops)
  326. {
  327. struct cfhsi *cfhsi;
  328. cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
  329. netdev_dbg(cfhsi->ndev, "%s.\n",
  330. __func__);
  331. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  332. return;
  333. cfhsi_tx_done(cfhsi);
  334. }
  335. static int cfhsi_rx_desc(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
  336. {
  337. int xfer_sz = 0;
  338. int nfrms = 0;
  339. u16 *plen = NULL;
  340. u8 *pfrm = NULL;
  341. if ((desc->header & ~CFHSI_PIGGY_DESC) ||
  342. (desc->offset > CFHSI_MAX_EMB_FRM_SZ)) {
  343. netdev_err(cfhsi->ndev, "%s: Invalid descriptor.\n",
  344. __func__);
  345. return -EPROTO;
  346. }
  347. /* Check for embedded CAIF frame. */
  348. if (desc->offset) {
  349. struct sk_buff *skb;
  350. u8 *dst = NULL;
  351. int len = 0;
  352. pfrm = ((u8 *)desc) + desc->offset;
  353. /* Remove offset padding. */
  354. pfrm += *pfrm + 1;
  355. /* Read length of CAIF frame (little endian). */
  356. len = *pfrm;
  357. len |= ((*(pfrm+1)) << 8) & 0xFF00;
  358. len += 2; /* Add FCS fields. */
  359. /* Sanity check length of CAIF frame. */
  360. if (unlikely(len > CFHSI_MAX_CAIF_FRAME_SZ)) {
  361. netdev_err(cfhsi->ndev, "%s: Invalid length.\n",
  362. __func__);
  363. return -EPROTO;
  364. }
  365. /* Allocate SKB (OK even in IRQ context). */
  366. skb = alloc_skb(len + 1, GFP_ATOMIC);
  367. if (!skb) {
  368. netdev_err(cfhsi->ndev, "%s: Out of memory !\n",
  369. __func__);
  370. return -ENOMEM;
  371. }
  372. caif_assert(skb != NULL);
  373. dst = skb_put(skb, len);
  374. memcpy(dst, pfrm, len);
  375. skb->protocol = htons(ETH_P_CAIF);
  376. skb_reset_mac_header(skb);
  377. skb->dev = cfhsi->ndev;
  378. /*
  379. * We are in a callback handler and
  380. * unfortunately we don't know what context we're
  381. * running in.
  382. */
  383. if (in_interrupt())
  384. netif_rx(skb);
  385. else
  386. netif_rx_ni(skb);
  387. /* Update network statistics. */
  388. cfhsi->ndev->stats.rx_packets++;
  389. cfhsi->ndev->stats.rx_bytes += len;
  390. }
  391. /* Calculate transfer length. */
  392. plen = desc->cffrm_len;
  393. while (nfrms < CFHSI_MAX_PKTS && *plen) {
  394. xfer_sz += *plen;
  395. plen++;
  396. nfrms++;
  397. }
  398. /* Check for piggy-backed descriptor. */
  399. if (desc->header & CFHSI_PIGGY_DESC)
  400. xfer_sz += CFHSI_DESC_SZ;
  401. if ((xfer_sz % 4) || (xfer_sz > (CFHSI_BUF_SZ_RX - CFHSI_DESC_SZ))) {
  402. netdev_err(cfhsi->ndev,
  403. "%s: Invalid payload len: %d, ignored.\n",
  404. __func__, xfer_sz);
  405. return -EPROTO;
  406. }
  407. return xfer_sz;
  408. }
  409. static int cfhsi_rx_desc_len(struct cfhsi_desc *desc)
  410. {
  411. int xfer_sz = 0;
  412. int nfrms = 0;
  413. u16 *plen;
  414. if ((desc->header & ~CFHSI_PIGGY_DESC) ||
  415. (desc->offset > CFHSI_MAX_EMB_FRM_SZ)) {
  416. pr_err("Invalid descriptor. %x %x\n", desc->header,
  417. desc->offset);
  418. return -EPROTO;
  419. }
  420. /* Calculate transfer length. */
  421. plen = desc->cffrm_len;
  422. while (nfrms < CFHSI_MAX_PKTS && *plen) {
  423. xfer_sz += *plen;
  424. plen++;
  425. nfrms++;
  426. }
  427. if (xfer_sz % 4) {
  428. pr_err("Invalid payload len: %d, ignored.\n", xfer_sz);
  429. return -EPROTO;
  430. }
  431. return xfer_sz;
  432. }
  433. static int cfhsi_rx_pld(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
  434. {
  435. int rx_sz = 0;
  436. int nfrms = 0;
  437. u16 *plen = NULL;
  438. u8 *pfrm = NULL;
  439. /* Sanity check header and offset. */
  440. if (WARN_ON((desc->header & ~CFHSI_PIGGY_DESC) ||
  441. (desc->offset > CFHSI_MAX_EMB_FRM_SZ))) {
  442. netdev_err(cfhsi->ndev, "%s: Invalid descriptor.\n",
  443. __func__);
  444. return -EPROTO;
  445. }
  446. /* Set frame pointer to start of payload. */
  447. pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
  448. plen = desc->cffrm_len;
  449. /* Skip already processed frames. */
  450. while (nfrms < cfhsi->rx_state.nfrms) {
  451. pfrm += *plen;
  452. rx_sz += *plen;
  453. plen++;
  454. nfrms++;
  455. }
  456. /* Parse payload. */
  457. while (nfrms < CFHSI_MAX_PKTS && *plen) {
  458. struct sk_buff *skb;
  459. u8 *dst = NULL;
  460. u8 *pcffrm = NULL;
  461. int len;
  462. /* CAIF frame starts after head padding. */
  463. pcffrm = pfrm + *pfrm + 1;
  464. /* Read length of CAIF frame (little endian). */
  465. len = *pcffrm;
  466. len |= ((*(pcffrm + 1)) << 8) & 0xFF00;
  467. len += 2; /* Add FCS fields. */
  468. /* Sanity check length of CAIF frames. */
  469. if (unlikely(len > CFHSI_MAX_CAIF_FRAME_SZ)) {
  470. netdev_err(cfhsi->ndev, "%s: Invalid length.\n",
  471. __func__);
  472. return -EPROTO;
  473. }
  474. /* Allocate SKB (OK even in IRQ context). */
  475. skb = alloc_skb(len + 1, GFP_ATOMIC);
  476. if (!skb) {
  477. netdev_err(cfhsi->ndev, "%s: Out of memory !\n",
  478. __func__);
  479. cfhsi->rx_state.nfrms = nfrms;
  480. return -ENOMEM;
  481. }
  482. caif_assert(skb != NULL);
  483. dst = skb_put(skb, len);
  484. memcpy(dst, pcffrm, len);
  485. skb->protocol = htons(ETH_P_CAIF);
  486. skb_reset_mac_header(skb);
  487. skb->dev = cfhsi->ndev;
  488. /*
  489. * We're called in callback from HSI
  490. * and don't know the context we're running in.
  491. */
  492. if (in_interrupt())
  493. netif_rx(skb);
  494. else
  495. netif_rx_ni(skb);
  496. /* Update network statistics. */
  497. cfhsi->ndev->stats.rx_packets++;
  498. cfhsi->ndev->stats.rx_bytes += len;
  499. pfrm += *plen;
  500. rx_sz += *plen;
  501. plen++;
  502. nfrms++;
  503. }
  504. return rx_sz;
  505. }
  506. static void cfhsi_rx_done(struct cfhsi *cfhsi)
  507. {
  508. int res;
  509. int desc_pld_len = 0, rx_len, rx_state;
  510. struct cfhsi_desc *desc = NULL;
  511. u8 *rx_ptr, *rx_buf;
  512. struct cfhsi_desc *piggy_desc = NULL;
  513. desc = (struct cfhsi_desc *)cfhsi->rx_buf;
  514. netdev_dbg(cfhsi->ndev, "%s\n", __func__);
  515. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  516. return;
  517. /* Update inactivity timer if pending. */
  518. spin_lock_bh(&cfhsi->lock);
  519. mod_timer_pending(&cfhsi->inactivity_timer,
  520. jiffies + cfhsi->cfg.inactivity_timeout);
  521. spin_unlock_bh(&cfhsi->lock);
  522. if (cfhsi->rx_state.state == CFHSI_RX_STATE_DESC) {
  523. desc_pld_len = cfhsi_rx_desc_len(desc);
  524. if (desc_pld_len < 0)
  525. goto out_of_sync;
  526. rx_buf = cfhsi->rx_buf;
  527. rx_len = desc_pld_len;
  528. if (desc_pld_len > 0 && (desc->header & CFHSI_PIGGY_DESC))
  529. rx_len += CFHSI_DESC_SZ;
  530. if (desc_pld_len == 0)
  531. rx_buf = cfhsi->rx_flip_buf;
  532. } else {
  533. rx_buf = cfhsi->rx_flip_buf;
  534. rx_len = CFHSI_DESC_SZ;
  535. if (cfhsi->rx_state.pld_len > 0 &&
  536. (desc->header & CFHSI_PIGGY_DESC)) {
  537. piggy_desc = (struct cfhsi_desc *)
  538. (desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ +
  539. cfhsi->rx_state.pld_len);
  540. cfhsi->rx_state.piggy_desc = true;
  541. /* Extract payload len from piggy-backed descriptor. */
  542. desc_pld_len = cfhsi_rx_desc_len(piggy_desc);
  543. if (desc_pld_len < 0)
  544. goto out_of_sync;
  545. if (desc_pld_len > 0) {
  546. rx_len = desc_pld_len;
  547. if (piggy_desc->header & CFHSI_PIGGY_DESC)
  548. rx_len += CFHSI_DESC_SZ;
  549. }
  550. /*
  551. * Copy needed information from the piggy-backed
  552. * descriptor to the descriptor in the start.
  553. */
  554. memcpy(rx_buf, (u8 *)piggy_desc,
  555. CFHSI_DESC_SHORT_SZ);
  556. }
  557. }
  558. if (desc_pld_len) {
  559. rx_state = CFHSI_RX_STATE_PAYLOAD;
  560. rx_ptr = rx_buf + CFHSI_DESC_SZ;
  561. } else {
  562. rx_state = CFHSI_RX_STATE_DESC;
  563. rx_ptr = rx_buf;
  564. rx_len = CFHSI_DESC_SZ;
  565. }
  566. /* Initiate next read */
  567. if (test_bit(CFHSI_AWAKE, &cfhsi->bits)) {
  568. /* Set up new transfer. */
  569. netdev_dbg(cfhsi->ndev, "%s: Start RX.\n",
  570. __func__);
  571. res = cfhsi->ops->cfhsi_rx(rx_ptr, rx_len,
  572. cfhsi->ops);
  573. if (WARN_ON(res < 0)) {
  574. netdev_err(cfhsi->ndev, "%s: RX error %d.\n",
  575. __func__, res);
  576. cfhsi->ndev->stats.rx_errors++;
  577. cfhsi->ndev->stats.rx_dropped++;
  578. }
  579. }
  580. if (cfhsi->rx_state.state == CFHSI_RX_STATE_DESC) {
  581. /* Extract payload from descriptor */
  582. if (cfhsi_rx_desc(desc, cfhsi) < 0)
  583. goto out_of_sync;
  584. } else {
  585. /* Extract payload */
  586. if (cfhsi_rx_pld(desc, cfhsi) < 0)
  587. goto out_of_sync;
  588. if (piggy_desc) {
  589. /* Extract any payload in piggyback descriptor. */
  590. if (cfhsi_rx_desc(piggy_desc, cfhsi) < 0)
  591. goto out_of_sync;
  592. /* Mark no embedded frame after extracting it */
  593. piggy_desc->offset = 0;
  594. }
  595. }
  596. /* Update state info */
  597. memset(&cfhsi->rx_state, 0, sizeof(cfhsi->rx_state));
  598. cfhsi->rx_state.state = rx_state;
  599. cfhsi->rx_ptr = rx_ptr;
  600. cfhsi->rx_len = rx_len;
  601. cfhsi->rx_state.pld_len = desc_pld_len;
  602. cfhsi->rx_state.piggy_desc = desc->header & CFHSI_PIGGY_DESC;
  603. if (rx_buf != cfhsi->rx_buf)
  604. swap(cfhsi->rx_buf, cfhsi->rx_flip_buf);
  605. return;
  606. out_of_sync:
  607. netdev_err(cfhsi->ndev, "%s: Out of sync.\n", __func__);
  608. print_hex_dump_bytes("--> ", DUMP_PREFIX_NONE,
  609. cfhsi->rx_buf, CFHSI_DESC_SZ);
  610. schedule_work(&cfhsi->out_of_sync_work);
  611. }
  612. static void cfhsi_rx_slowpath(unsigned long arg)
  613. {
  614. struct cfhsi *cfhsi = (struct cfhsi *)arg;
  615. netdev_dbg(cfhsi->ndev, "%s.\n",
  616. __func__);
  617. cfhsi_rx_done(cfhsi);
  618. }
  619. static void cfhsi_rx_done_cb(struct cfhsi_cb_ops *cb_ops)
  620. {
  621. struct cfhsi *cfhsi;
  622. cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
  623. netdev_dbg(cfhsi->ndev, "%s.\n",
  624. __func__);
  625. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  626. return;
  627. if (test_and_clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits))
  628. wake_up_interruptible(&cfhsi->flush_fifo_wait);
  629. else
  630. cfhsi_rx_done(cfhsi);
  631. }
  632. static void cfhsi_wake_up(struct work_struct *work)
  633. {
  634. struct cfhsi *cfhsi = NULL;
  635. int res;
  636. int len;
  637. long ret;
  638. cfhsi = container_of(work, struct cfhsi, wake_up_work);
  639. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  640. return;
  641. if (unlikely(test_bit(CFHSI_AWAKE, &cfhsi->bits))) {
  642. /* It happenes when wakeup is requested by
  643. * both ends at the same time. */
  644. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  645. clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  646. return;
  647. }
  648. /* Activate wake line. */
  649. cfhsi->ops->cfhsi_wake_up(cfhsi->ops);
  650. netdev_dbg(cfhsi->ndev, "%s: Start waiting.\n",
  651. __func__);
  652. /* Wait for acknowledge. */
  653. ret = CFHSI_WAKE_TOUT;
  654. ret = wait_event_interruptible_timeout(cfhsi->wake_up_wait,
  655. test_and_clear_bit(CFHSI_WAKE_UP_ACK,
  656. &cfhsi->bits), ret);
  657. if (unlikely(ret < 0)) {
  658. /* Interrupted by signal. */
  659. netdev_err(cfhsi->ndev, "%s: Signalled: %ld.\n",
  660. __func__, ret);
  661. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  662. cfhsi->ops->cfhsi_wake_down(cfhsi->ops);
  663. return;
  664. } else if (!ret) {
  665. bool ca_wake = false;
  666. size_t fifo_occupancy = 0;
  667. /* Wakeup timeout */
  668. netdev_dbg(cfhsi->ndev, "%s: Timeout.\n",
  669. __func__);
  670. /* Check FIFO to check if modem has sent something. */
  671. WARN_ON(cfhsi->ops->cfhsi_fifo_occupancy(cfhsi->ops,
  672. &fifo_occupancy));
  673. netdev_dbg(cfhsi->ndev, "%s: Bytes in FIFO: %u.\n",
  674. __func__, (unsigned) fifo_occupancy);
  675. /* Check if we misssed the interrupt. */
  676. WARN_ON(cfhsi->ops->cfhsi_get_peer_wake(cfhsi->ops,
  677. &ca_wake));
  678. if (ca_wake) {
  679. netdev_err(cfhsi->ndev, "%s: CA Wake missed !.\n",
  680. __func__);
  681. /* Clear the CFHSI_WAKE_UP_ACK bit to prevent race. */
  682. clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  683. /* Continue execution. */
  684. goto wake_ack;
  685. }
  686. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  687. cfhsi->ops->cfhsi_wake_down(cfhsi->ops);
  688. return;
  689. }
  690. wake_ack:
  691. netdev_dbg(cfhsi->ndev, "%s: Woken.\n",
  692. __func__);
  693. /* Clear power up bit. */
  694. set_bit(CFHSI_AWAKE, &cfhsi->bits);
  695. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  696. /* Resume read operation. */
  697. netdev_dbg(cfhsi->ndev, "%s: Start RX.\n", __func__);
  698. res = cfhsi->ops->cfhsi_rx(cfhsi->rx_ptr, cfhsi->rx_len, cfhsi->ops);
  699. if (WARN_ON(res < 0))
  700. netdev_err(cfhsi->ndev, "%s: RX err %d.\n", __func__, res);
  701. /* Clear power up acknowledment. */
  702. clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  703. spin_lock_bh(&cfhsi->lock);
  704. /* Resume transmit if queues are not empty. */
  705. if (!cfhsi_tx_queue_len(cfhsi)) {
  706. netdev_dbg(cfhsi->ndev, "%s: Peer wake, start timer.\n",
  707. __func__);
  708. /* Start inactivity timer. */
  709. mod_timer(&cfhsi->inactivity_timer,
  710. jiffies + cfhsi->cfg.inactivity_timeout);
  711. spin_unlock_bh(&cfhsi->lock);
  712. return;
  713. }
  714. netdev_dbg(cfhsi->ndev, "%s: Host wake.\n",
  715. __func__);
  716. spin_unlock_bh(&cfhsi->lock);
  717. /* Create HSI frame. */
  718. len = cfhsi_tx_frm((struct cfhsi_desc *)cfhsi->tx_buf, cfhsi);
  719. if (likely(len > 0)) {
  720. /* Set up new transfer. */
  721. res = cfhsi->ops->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->ops);
  722. if (WARN_ON(res < 0)) {
  723. netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
  724. __func__, res);
  725. cfhsi_abort_tx(cfhsi);
  726. }
  727. } else {
  728. netdev_err(cfhsi->ndev,
  729. "%s: Failed to create HSI frame: %d.\n",
  730. __func__, len);
  731. }
  732. }
  733. static void cfhsi_wake_down(struct work_struct *work)
  734. {
  735. long ret;
  736. struct cfhsi *cfhsi = NULL;
  737. size_t fifo_occupancy = 0;
  738. int retry = CFHSI_WAKE_TOUT;
  739. cfhsi = container_of(work, struct cfhsi, wake_down_work);
  740. netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
  741. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  742. return;
  743. /* Deactivate wake line. */
  744. cfhsi->ops->cfhsi_wake_down(cfhsi->ops);
  745. /* Wait for acknowledge. */
  746. ret = CFHSI_WAKE_TOUT;
  747. ret = wait_event_interruptible_timeout(cfhsi->wake_down_wait,
  748. test_and_clear_bit(CFHSI_WAKE_DOWN_ACK,
  749. &cfhsi->bits), ret);
  750. if (ret < 0) {
  751. /* Interrupted by signal. */
  752. netdev_err(cfhsi->ndev, "%s: Signalled: %ld.\n",
  753. __func__, ret);
  754. return;
  755. } else if (!ret) {
  756. bool ca_wake = true;
  757. /* Timeout */
  758. netdev_err(cfhsi->ndev, "%s: Timeout.\n", __func__);
  759. /* Check if we misssed the interrupt. */
  760. WARN_ON(cfhsi->ops->cfhsi_get_peer_wake(cfhsi->ops,
  761. &ca_wake));
  762. if (!ca_wake)
  763. netdev_err(cfhsi->ndev, "%s: CA Wake missed !.\n",
  764. __func__);
  765. }
  766. /* Check FIFO occupancy. */
  767. while (retry) {
  768. WARN_ON(cfhsi->ops->cfhsi_fifo_occupancy(cfhsi->ops,
  769. &fifo_occupancy));
  770. if (!fifo_occupancy)
  771. break;
  772. set_current_state(TASK_INTERRUPTIBLE);
  773. schedule_timeout(1);
  774. retry--;
  775. }
  776. if (!retry)
  777. netdev_err(cfhsi->ndev, "%s: FIFO Timeout.\n", __func__);
  778. /* Clear AWAKE condition. */
  779. clear_bit(CFHSI_AWAKE, &cfhsi->bits);
  780. /* Cancel pending RX requests. */
  781. cfhsi->ops->cfhsi_rx_cancel(cfhsi->ops);
  782. }
  783. static void cfhsi_out_of_sync(struct work_struct *work)
  784. {
  785. struct cfhsi *cfhsi = NULL;
  786. cfhsi = container_of(work, struct cfhsi, out_of_sync_work);
  787. rtnl_lock();
  788. dev_close(cfhsi->ndev);
  789. rtnl_unlock();
  790. }
  791. static void cfhsi_wake_up_cb(struct cfhsi_cb_ops *cb_ops)
  792. {
  793. struct cfhsi *cfhsi = NULL;
  794. cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
  795. netdev_dbg(cfhsi->ndev, "%s.\n",
  796. __func__);
  797. set_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  798. wake_up_interruptible(&cfhsi->wake_up_wait);
  799. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  800. return;
  801. /* Schedule wake up work queue if the peer initiates. */
  802. if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
  803. queue_work(cfhsi->wq, &cfhsi->wake_up_work);
  804. }
  805. static void cfhsi_wake_down_cb(struct cfhsi_cb_ops *cb_ops)
  806. {
  807. struct cfhsi *cfhsi = NULL;
  808. cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
  809. netdev_dbg(cfhsi->ndev, "%s.\n",
  810. __func__);
  811. /* Initiating low power is only permitted by the host (us). */
  812. set_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
  813. wake_up_interruptible(&cfhsi->wake_down_wait);
  814. }
  815. static void cfhsi_aggregation_tout(unsigned long arg)
  816. {
  817. struct cfhsi *cfhsi = (struct cfhsi *)arg;
  818. netdev_dbg(cfhsi->ndev, "%s.\n",
  819. __func__);
  820. cfhsi_start_tx(cfhsi);
  821. }
  822. static int cfhsi_xmit(struct sk_buff *skb, struct net_device *dev)
  823. {
  824. struct cfhsi *cfhsi = NULL;
  825. int start_xfer = 0;
  826. int timer_active;
  827. int prio;
  828. if (!dev)
  829. return -EINVAL;
  830. cfhsi = netdev_priv(dev);
  831. switch (skb->priority) {
  832. case TC_PRIO_BESTEFFORT:
  833. case TC_PRIO_FILLER:
  834. case TC_PRIO_BULK:
  835. prio = CFHSI_PRIO_BEBK;
  836. break;
  837. case TC_PRIO_INTERACTIVE_BULK:
  838. prio = CFHSI_PRIO_VI;
  839. break;
  840. case TC_PRIO_INTERACTIVE:
  841. prio = CFHSI_PRIO_VO;
  842. break;
  843. case TC_PRIO_CONTROL:
  844. default:
  845. prio = CFHSI_PRIO_CTL;
  846. break;
  847. }
  848. spin_lock_bh(&cfhsi->lock);
  849. /* Update aggregation statistics */
  850. cfhsi_update_aggregation_stats(cfhsi, skb, 1);
  851. /* Queue the SKB */
  852. skb_queue_tail(&cfhsi->qhead[prio], skb);
  853. /* Sanity check; xmit should not be called after unregister_netdev */
  854. if (WARN_ON(test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))) {
  855. spin_unlock_bh(&cfhsi->lock);
  856. cfhsi_abort_tx(cfhsi);
  857. return -EINVAL;
  858. }
  859. /* Send flow off if number of packets is above high water mark. */
  860. if (!cfhsi->flow_off_sent &&
  861. cfhsi_tx_queue_len(cfhsi) > cfhsi->cfg.q_high_mark &&
  862. cfhsi->cfdev.flowctrl) {
  863. cfhsi->flow_off_sent = 1;
  864. cfhsi->cfdev.flowctrl(cfhsi->ndev, OFF);
  865. }
  866. if (cfhsi->tx_state == CFHSI_TX_STATE_IDLE) {
  867. cfhsi->tx_state = CFHSI_TX_STATE_XFER;
  868. start_xfer = 1;
  869. }
  870. if (!start_xfer) {
  871. /* Send aggregate if it is possible */
  872. bool aggregate_ready =
  873. cfhsi_can_send_aggregate(cfhsi) &&
  874. del_timer(&cfhsi->aggregation_timer) > 0;
  875. spin_unlock_bh(&cfhsi->lock);
  876. if (aggregate_ready)
  877. cfhsi_start_tx(cfhsi);
  878. return 0;
  879. }
  880. /* Delete inactivity timer if started. */
  881. timer_active = del_timer_sync(&cfhsi->inactivity_timer);
  882. spin_unlock_bh(&cfhsi->lock);
  883. if (timer_active) {
  884. struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
  885. int len;
  886. int res;
  887. /* Create HSI frame. */
  888. len = cfhsi_tx_frm(desc, cfhsi);
  889. WARN_ON(!len);
  890. /* Set up new transfer. */
  891. res = cfhsi->ops->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->ops);
  892. if (WARN_ON(res < 0)) {
  893. netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
  894. __func__, res);
  895. cfhsi_abort_tx(cfhsi);
  896. }
  897. } else {
  898. /* Schedule wake up work queue if the we initiate. */
  899. if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
  900. queue_work(cfhsi->wq, &cfhsi->wake_up_work);
  901. }
  902. return 0;
  903. }
  904. static const struct net_device_ops cfhsi_netdevops;
  905. static void cfhsi_setup(struct net_device *dev)
  906. {
  907. int i;
  908. struct cfhsi *cfhsi = netdev_priv(dev);
  909. dev->features = 0;
  910. dev->type = ARPHRD_CAIF;
  911. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  912. dev->mtu = CFHSI_MAX_CAIF_FRAME_SZ;
  913. dev->priv_flags |= IFF_NO_QUEUE;
  914. dev->destructor = free_netdev;
  915. dev->netdev_ops = &cfhsi_netdevops;
  916. for (i = 0; i < CFHSI_PRIO_LAST; ++i)
  917. skb_queue_head_init(&cfhsi->qhead[i]);
  918. cfhsi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
  919. cfhsi->cfdev.use_frag = false;
  920. cfhsi->cfdev.use_stx = false;
  921. cfhsi->cfdev.use_fcs = false;
  922. cfhsi->ndev = dev;
  923. cfhsi->cfg = hsi_default_config;
  924. }
  925. static int cfhsi_open(struct net_device *ndev)
  926. {
  927. struct cfhsi *cfhsi = netdev_priv(ndev);
  928. int res;
  929. clear_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
  930. /* Initialize state vaiables. */
  931. cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
  932. cfhsi->rx_state.state = CFHSI_RX_STATE_DESC;
  933. /* Set flow info */
  934. cfhsi->flow_off_sent = 0;
  935. /*
  936. * Allocate a TX buffer with the size of a HSI packet descriptors
  937. * and the necessary room for CAIF payload frames.
  938. */
  939. cfhsi->tx_buf = kzalloc(CFHSI_BUF_SZ_TX, GFP_KERNEL);
  940. if (!cfhsi->tx_buf) {
  941. res = -ENODEV;
  942. goto err_alloc_tx;
  943. }
  944. /*
  945. * Allocate a RX buffer with the size of two HSI packet descriptors and
  946. * the necessary room for CAIF payload frames.
  947. */
  948. cfhsi->rx_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
  949. if (!cfhsi->rx_buf) {
  950. res = -ENODEV;
  951. goto err_alloc_rx;
  952. }
  953. cfhsi->rx_flip_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
  954. if (!cfhsi->rx_flip_buf) {
  955. res = -ENODEV;
  956. goto err_alloc_rx_flip;
  957. }
  958. /* Initialize aggregation timeout */
  959. cfhsi->cfg.aggregation_timeout = hsi_default_config.aggregation_timeout;
  960. /* Initialize recieve vaiables. */
  961. cfhsi->rx_ptr = cfhsi->rx_buf;
  962. cfhsi->rx_len = CFHSI_DESC_SZ;
  963. /* Initialize spin locks. */
  964. spin_lock_init(&cfhsi->lock);
  965. /* Set up the driver. */
  966. cfhsi->cb_ops.tx_done_cb = cfhsi_tx_done_cb;
  967. cfhsi->cb_ops.rx_done_cb = cfhsi_rx_done_cb;
  968. cfhsi->cb_ops.wake_up_cb = cfhsi_wake_up_cb;
  969. cfhsi->cb_ops.wake_down_cb = cfhsi_wake_down_cb;
  970. /* Initialize the work queues. */
  971. INIT_WORK(&cfhsi->wake_up_work, cfhsi_wake_up);
  972. INIT_WORK(&cfhsi->wake_down_work, cfhsi_wake_down);
  973. INIT_WORK(&cfhsi->out_of_sync_work, cfhsi_out_of_sync);
  974. /* Clear all bit fields. */
  975. clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  976. clear_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
  977. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  978. clear_bit(CFHSI_AWAKE, &cfhsi->bits);
  979. /* Create work thread. */
  980. cfhsi->wq = create_singlethread_workqueue(cfhsi->ndev->name);
  981. if (!cfhsi->wq) {
  982. netdev_err(cfhsi->ndev, "%s: Failed to create work queue.\n",
  983. __func__);
  984. res = -ENODEV;
  985. goto err_create_wq;
  986. }
  987. /* Initialize wait queues. */
  988. init_waitqueue_head(&cfhsi->wake_up_wait);
  989. init_waitqueue_head(&cfhsi->wake_down_wait);
  990. init_waitqueue_head(&cfhsi->flush_fifo_wait);
  991. /* Setup the inactivity timer. */
  992. init_timer(&cfhsi->inactivity_timer);
  993. cfhsi->inactivity_timer.data = (unsigned long)cfhsi;
  994. cfhsi->inactivity_timer.function = cfhsi_inactivity_tout;
  995. /* Setup the slowpath RX timer. */
  996. init_timer(&cfhsi->rx_slowpath_timer);
  997. cfhsi->rx_slowpath_timer.data = (unsigned long)cfhsi;
  998. cfhsi->rx_slowpath_timer.function = cfhsi_rx_slowpath;
  999. /* Setup the aggregation timer. */
  1000. init_timer(&cfhsi->aggregation_timer);
  1001. cfhsi->aggregation_timer.data = (unsigned long)cfhsi;
  1002. cfhsi->aggregation_timer.function = cfhsi_aggregation_tout;
  1003. /* Activate HSI interface. */
  1004. res = cfhsi->ops->cfhsi_up(cfhsi->ops);
  1005. if (res) {
  1006. netdev_err(cfhsi->ndev,
  1007. "%s: can't activate HSI interface: %d.\n",
  1008. __func__, res);
  1009. goto err_activate;
  1010. }
  1011. /* Flush FIFO */
  1012. res = cfhsi_flush_fifo(cfhsi);
  1013. if (res) {
  1014. netdev_err(cfhsi->ndev, "%s: Can't flush FIFO: %d.\n",
  1015. __func__, res);
  1016. goto err_net_reg;
  1017. }
  1018. return res;
  1019. err_net_reg:
  1020. cfhsi->ops->cfhsi_down(cfhsi->ops);
  1021. err_activate:
  1022. destroy_workqueue(cfhsi->wq);
  1023. err_create_wq:
  1024. kfree(cfhsi->rx_flip_buf);
  1025. err_alloc_rx_flip:
  1026. kfree(cfhsi->rx_buf);
  1027. err_alloc_rx:
  1028. kfree(cfhsi->tx_buf);
  1029. err_alloc_tx:
  1030. return res;
  1031. }
  1032. static int cfhsi_close(struct net_device *ndev)
  1033. {
  1034. struct cfhsi *cfhsi = netdev_priv(ndev);
  1035. u8 *tx_buf, *rx_buf, *flip_buf;
  1036. /* going to shutdown driver */
  1037. set_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
  1038. /* Flush workqueue */
  1039. flush_workqueue(cfhsi->wq);
  1040. /* Delete timers if pending */
  1041. del_timer_sync(&cfhsi->inactivity_timer);
  1042. del_timer_sync(&cfhsi->rx_slowpath_timer);
  1043. del_timer_sync(&cfhsi->aggregation_timer);
  1044. /* Cancel pending RX request (if any) */
  1045. cfhsi->ops->cfhsi_rx_cancel(cfhsi->ops);
  1046. /* Destroy workqueue */
  1047. destroy_workqueue(cfhsi->wq);
  1048. /* Store bufferes: will be freed later. */
  1049. tx_buf = cfhsi->tx_buf;
  1050. rx_buf = cfhsi->rx_buf;
  1051. flip_buf = cfhsi->rx_flip_buf;
  1052. /* Flush transmit queues. */
  1053. cfhsi_abort_tx(cfhsi);
  1054. /* Deactivate interface */
  1055. cfhsi->ops->cfhsi_down(cfhsi->ops);
  1056. /* Free buffers. */
  1057. kfree(tx_buf);
  1058. kfree(rx_buf);
  1059. kfree(flip_buf);
  1060. return 0;
  1061. }
  1062. static void cfhsi_uninit(struct net_device *dev)
  1063. {
  1064. struct cfhsi *cfhsi = netdev_priv(dev);
  1065. ASSERT_RTNL();
  1066. symbol_put(cfhsi_get_device);
  1067. list_del(&cfhsi->list);
  1068. }
  1069. static const struct net_device_ops cfhsi_netdevops = {
  1070. .ndo_uninit = cfhsi_uninit,
  1071. .ndo_open = cfhsi_open,
  1072. .ndo_stop = cfhsi_close,
  1073. .ndo_start_xmit = cfhsi_xmit
  1074. };
  1075. static void cfhsi_netlink_parms(struct nlattr *data[], struct cfhsi *cfhsi)
  1076. {
  1077. int i;
  1078. if (!data) {
  1079. pr_debug("no params data found\n");
  1080. return;
  1081. }
  1082. i = __IFLA_CAIF_HSI_INACTIVITY_TOUT;
  1083. /*
  1084. * Inactivity timeout in millisecs. Lowest possible value is 1,
  1085. * and highest possible is NEXT_TIMER_MAX_DELTA.
  1086. */
  1087. if (data[i]) {
  1088. u32 inactivity_timeout = nla_get_u32(data[i]);
  1089. /* Pre-calculate inactivity timeout. */
  1090. cfhsi->cfg.inactivity_timeout = inactivity_timeout * HZ / 1000;
  1091. if (cfhsi->cfg.inactivity_timeout == 0)
  1092. cfhsi->cfg.inactivity_timeout = 1;
  1093. else if (cfhsi->cfg.inactivity_timeout > NEXT_TIMER_MAX_DELTA)
  1094. cfhsi->cfg.inactivity_timeout = NEXT_TIMER_MAX_DELTA;
  1095. }
  1096. i = __IFLA_CAIF_HSI_AGGREGATION_TOUT;
  1097. if (data[i])
  1098. cfhsi->cfg.aggregation_timeout = nla_get_u32(data[i]);
  1099. i = __IFLA_CAIF_HSI_HEAD_ALIGN;
  1100. if (data[i])
  1101. cfhsi->cfg.head_align = nla_get_u32(data[i]);
  1102. i = __IFLA_CAIF_HSI_TAIL_ALIGN;
  1103. if (data[i])
  1104. cfhsi->cfg.tail_align = nla_get_u32(data[i]);
  1105. i = __IFLA_CAIF_HSI_QHIGH_WATERMARK;
  1106. if (data[i])
  1107. cfhsi->cfg.q_high_mark = nla_get_u32(data[i]);
  1108. i = __IFLA_CAIF_HSI_QLOW_WATERMARK;
  1109. if (data[i])
  1110. cfhsi->cfg.q_low_mark = nla_get_u32(data[i]);
  1111. }
  1112. static int caif_hsi_changelink(struct net_device *dev, struct nlattr *tb[],
  1113. struct nlattr *data[])
  1114. {
  1115. cfhsi_netlink_parms(data, netdev_priv(dev));
  1116. netdev_state_change(dev);
  1117. return 0;
  1118. }
  1119. static const struct nla_policy caif_hsi_policy[__IFLA_CAIF_HSI_MAX + 1] = {
  1120. [__IFLA_CAIF_HSI_INACTIVITY_TOUT] = { .type = NLA_U32, .len = 4 },
  1121. [__IFLA_CAIF_HSI_AGGREGATION_TOUT] = { .type = NLA_U32, .len = 4 },
  1122. [__IFLA_CAIF_HSI_HEAD_ALIGN] = { .type = NLA_U32, .len = 4 },
  1123. [__IFLA_CAIF_HSI_TAIL_ALIGN] = { .type = NLA_U32, .len = 4 },
  1124. [__IFLA_CAIF_HSI_QHIGH_WATERMARK] = { .type = NLA_U32, .len = 4 },
  1125. [__IFLA_CAIF_HSI_QLOW_WATERMARK] = { .type = NLA_U32, .len = 4 },
  1126. };
  1127. static size_t caif_hsi_get_size(const struct net_device *dev)
  1128. {
  1129. int i;
  1130. size_t s = 0;
  1131. for (i = __IFLA_CAIF_HSI_UNSPEC + 1; i < __IFLA_CAIF_HSI_MAX; i++)
  1132. s += nla_total_size(caif_hsi_policy[i].len);
  1133. return s;
  1134. }
  1135. static int caif_hsi_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1136. {
  1137. struct cfhsi *cfhsi = netdev_priv(dev);
  1138. if (nla_put_u32(skb, __IFLA_CAIF_HSI_INACTIVITY_TOUT,
  1139. cfhsi->cfg.inactivity_timeout) ||
  1140. nla_put_u32(skb, __IFLA_CAIF_HSI_AGGREGATION_TOUT,
  1141. cfhsi->cfg.aggregation_timeout) ||
  1142. nla_put_u32(skb, __IFLA_CAIF_HSI_HEAD_ALIGN,
  1143. cfhsi->cfg.head_align) ||
  1144. nla_put_u32(skb, __IFLA_CAIF_HSI_TAIL_ALIGN,
  1145. cfhsi->cfg.tail_align) ||
  1146. nla_put_u32(skb, __IFLA_CAIF_HSI_QHIGH_WATERMARK,
  1147. cfhsi->cfg.q_high_mark) ||
  1148. nla_put_u32(skb, __IFLA_CAIF_HSI_QLOW_WATERMARK,
  1149. cfhsi->cfg.q_low_mark))
  1150. return -EMSGSIZE;
  1151. return 0;
  1152. }
  1153. static int caif_hsi_newlink(struct net *src_net, struct net_device *dev,
  1154. struct nlattr *tb[], struct nlattr *data[])
  1155. {
  1156. struct cfhsi *cfhsi = NULL;
  1157. struct cfhsi_ops *(*get_ops)(void);
  1158. ASSERT_RTNL();
  1159. cfhsi = netdev_priv(dev);
  1160. cfhsi_netlink_parms(data, cfhsi);
  1161. get_ops = symbol_get(cfhsi_get_ops);
  1162. if (!get_ops) {
  1163. pr_err("%s: failed to get the cfhsi_ops\n", __func__);
  1164. return -ENODEV;
  1165. }
  1166. /* Assign the HSI device. */
  1167. cfhsi->ops = (*get_ops)();
  1168. if (!cfhsi->ops) {
  1169. pr_err("%s: failed to get the cfhsi_ops\n", __func__);
  1170. goto err;
  1171. }
  1172. /* Assign the driver to this HSI device. */
  1173. cfhsi->ops->cb_ops = &cfhsi->cb_ops;
  1174. if (register_netdevice(dev)) {
  1175. pr_warn("%s: caif_hsi device registration failed\n", __func__);
  1176. goto err;
  1177. }
  1178. /* Add CAIF HSI device to list. */
  1179. list_add_tail(&cfhsi->list, &cfhsi_list);
  1180. return 0;
  1181. err:
  1182. symbol_put(cfhsi_get_ops);
  1183. return -ENODEV;
  1184. }
  1185. static struct rtnl_link_ops caif_hsi_link_ops __read_mostly = {
  1186. .kind = "cfhsi",
  1187. .priv_size = sizeof(struct cfhsi),
  1188. .setup = cfhsi_setup,
  1189. .maxtype = __IFLA_CAIF_HSI_MAX,
  1190. .policy = caif_hsi_policy,
  1191. .newlink = caif_hsi_newlink,
  1192. .changelink = caif_hsi_changelink,
  1193. .get_size = caif_hsi_get_size,
  1194. .fill_info = caif_hsi_fill_info,
  1195. };
  1196. static void __exit cfhsi_exit_module(void)
  1197. {
  1198. struct list_head *list_node;
  1199. struct list_head *n;
  1200. struct cfhsi *cfhsi;
  1201. rtnl_link_unregister(&caif_hsi_link_ops);
  1202. rtnl_lock();
  1203. list_for_each_safe(list_node, n, &cfhsi_list) {
  1204. cfhsi = list_entry(list_node, struct cfhsi, list);
  1205. unregister_netdev(cfhsi->ndev);
  1206. }
  1207. rtnl_unlock();
  1208. }
  1209. static int __init cfhsi_init_module(void)
  1210. {
  1211. return rtnl_link_register(&caif_hsi_link_ops);
  1212. }
  1213. module_init(cfhsi_init_module);
  1214. module_exit(cfhsi_exit_module);