caif_hsi.c 36 KB

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