caif_hsi.c 35 KB

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