caif_hsi.c 35 KB

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