caif_hsi.c 35 KB

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