caif_hsi.c 35 KB

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