caif_hsi.c 34 KB

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