caif_hsi.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/device.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/string.h>
  14. #include <linux/list.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/sched.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/timer.h>
  20. #include <net/caif/caif_layer.h>
  21. #include <net/caif/caif_hsi.h>
  22. MODULE_LICENSE("GPL");
  23. MODULE_AUTHOR("Daniel Martensson<daniel.martensson@stericsson.com>");
  24. MODULE_DESCRIPTION("CAIF HSI driver");
  25. /* Returns the number of padding bytes for alignment. */
  26. #define PAD_POW2(x, pow) ((((x)&((pow)-1)) == 0) ? 0 :\
  27. (((pow)-((x)&((pow)-1)))))
  28. /*
  29. * HSI padding options.
  30. * Warning: must be a base of 2 (& operation used) and can not be zero !
  31. */
  32. static int hsi_head_align = 4;
  33. module_param(hsi_head_align, int, S_IRUGO);
  34. MODULE_PARM_DESC(hsi_head_align, "HSI head alignment.");
  35. static int hsi_tail_align = 4;
  36. module_param(hsi_tail_align, int, S_IRUGO);
  37. MODULE_PARM_DESC(hsi_tail_align, "HSI tail alignment.");
  38. /*
  39. * HSI link layer flowcontrol thresholds.
  40. * Warning: A high threshold value migth increase throughput but it will at
  41. * the same time prevent channel prioritization and increase the risk of
  42. * flooding the modem. The high threshold should be above the low.
  43. */
  44. static int hsi_high_threshold = 100;
  45. module_param(hsi_high_threshold, int, S_IRUGO);
  46. MODULE_PARM_DESC(hsi_high_threshold, "HSI high threshold (FLOW OFF).");
  47. static int hsi_low_threshold = 50;
  48. module_param(hsi_low_threshold, int, S_IRUGO);
  49. MODULE_PARM_DESC(hsi_low_threshold, "HSI high threshold (FLOW ON).");
  50. #define ON 1
  51. #define OFF 0
  52. /*
  53. * Threshold values for the HSI packet queue. Flowcontrol will be asserted
  54. * when the number of packets exceeds HIGH_WATER_MARK. It will not be
  55. * de-asserted before the number of packets drops below LOW_WATER_MARK.
  56. */
  57. #define LOW_WATER_MARK hsi_low_threshold
  58. #define HIGH_WATER_MARK hsi_high_threshold
  59. static LIST_HEAD(cfhsi_list);
  60. static spinlock_t cfhsi_list_lock;
  61. static void cfhsi_inactivity_tout(unsigned long arg)
  62. {
  63. struct cfhsi *cfhsi = (struct cfhsi *)arg;
  64. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  65. __func__);
  66. /* Schedule power down work queue. */
  67. if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  68. queue_work(cfhsi->wq, &cfhsi->wake_down_work);
  69. }
  70. static void cfhsi_abort_tx(struct cfhsi *cfhsi)
  71. {
  72. struct sk_buff *skb;
  73. for (;;) {
  74. spin_lock_bh(&cfhsi->lock);
  75. skb = skb_dequeue(&cfhsi->qhead);
  76. if (!skb)
  77. break;
  78. cfhsi->ndev->stats.tx_errors++;
  79. cfhsi->ndev->stats.tx_dropped++;
  80. spin_unlock_bh(&cfhsi->lock);
  81. kfree_skb(skb);
  82. }
  83. cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
  84. if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  85. mod_timer(&cfhsi->timer, jiffies + CFHSI_INACTIVITY_TOUT);
  86. spin_unlock_bh(&cfhsi->lock);
  87. }
  88. static int cfhsi_flush_fifo(struct cfhsi *cfhsi)
  89. {
  90. char buffer[32]; /* Any reasonable value */
  91. size_t fifo_occupancy;
  92. int ret;
  93. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  94. __func__);
  95. ret = cfhsi->dev->cfhsi_wake_up(cfhsi->dev);
  96. if (ret) {
  97. dev_warn(&cfhsi->ndev->dev,
  98. "%s: can't wake up HSI interface: %d.\n",
  99. __func__, ret);
  100. return ret;
  101. }
  102. do {
  103. ret = cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
  104. &fifo_occupancy);
  105. if (ret) {
  106. dev_warn(&cfhsi->ndev->dev,
  107. "%s: can't get FIFO occupancy: %d.\n",
  108. __func__, ret);
  109. break;
  110. } else if (!fifo_occupancy)
  111. /* No more data, exitting normally */
  112. break;
  113. fifo_occupancy = min(sizeof(buffer), fifo_occupancy);
  114. set_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
  115. ret = cfhsi->dev->cfhsi_rx(buffer, fifo_occupancy,
  116. cfhsi->dev);
  117. if (ret) {
  118. clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
  119. dev_warn(&cfhsi->ndev->dev,
  120. "%s: can't read data: %d.\n",
  121. __func__, ret);
  122. break;
  123. }
  124. ret = 5 * HZ;
  125. wait_event_interruptible_timeout(cfhsi->flush_fifo_wait,
  126. !test_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits), ret);
  127. if (ret < 0) {
  128. dev_warn(&cfhsi->ndev->dev,
  129. "%s: can't wait for flush complete: %d.\n",
  130. __func__, ret);
  131. break;
  132. } else if (!ret) {
  133. ret = -ETIMEDOUT;
  134. dev_warn(&cfhsi->ndev->dev,
  135. "%s: timeout waiting for flush complete.\n",
  136. __func__);
  137. break;
  138. }
  139. } while (1);
  140. cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
  141. return ret;
  142. }
  143. static int cfhsi_tx_frm(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
  144. {
  145. int nfrms = 0;
  146. int pld_len = 0;
  147. struct sk_buff *skb;
  148. u8 *pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
  149. skb = skb_dequeue(&cfhsi->qhead);
  150. if (!skb)
  151. return 0;
  152. /* Clear offset. */
  153. desc->offset = 0;
  154. /* Check if we can embed a CAIF frame. */
  155. if (skb->len < CFHSI_MAX_EMB_FRM_SZ) {
  156. struct caif_payload_info *info;
  157. int hpad = 0;
  158. int tpad = 0;
  159. /* Calculate needed head alignment and tail alignment. */
  160. info = (struct caif_payload_info *)&skb->cb;
  161. hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
  162. tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);
  163. /* Check if frame still fits with added alignment. */
  164. if ((skb->len + hpad + tpad) <= CFHSI_MAX_EMB_FRM_SZ) {
  165. u8 *pemb = desc->emb_frm;
  166. desc->offset = CFHSI_DESC_SHORT_SZ;
  167. *pemb = (u8)(hpad - 1);
  168. pemb += hpad;
  169. /* Update network statistics. */
  170. cfhsi->ndev->stats.tx_packets++;
  171. cfhsi->ndev->stats.tx_bytes += skb->len;
  172. /* Copy in embedded CAIF frame. */
  173. skb_copy_bits(skb, 0, pemb, skb->len);
  174. consume_skb(skb);
  175. skb = NULL;
  176. }
  177. }
  178. /* Create payload CAIF frames. */
  179. pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
  180. while (nfrms < CFHSI_MAX_PKTS) {
  181. struct caif_payload_info *info;
  182. int hpad = 0;
  183. int tpad = 0;
  184. if (!skb)
  185. skb = skb_dequeue(&cfhsi->qhead);
  186. if (!skb)
  187. break;
  188. /* Calculate needed head alignment and tail alignment. */
  189. info = (struct caif_payload_info *)&skb->cb;
  190. hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
  191. tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);
  192. /* Fill in CAIF frame length in descriptor. */
  193. desc->cffrm_len[nfrms] = hpad + skb->len + tpad;
  194. /* Fill head padding information. */
  195. *pfrm = (u8)(hpad - 1);
  196. pfrm += hpad;
  197. /* Update network statistics. */
  198. cfhsi->ndev->stats.tx_packets++;
  199. cfhsi->ndev->stats.tx_bytes += skb->len;
  200. /* Copy in CAIF frame. */
  201. skb_copy_bits(skb, 0, pfrm, skb->len);
  202. /* Update payload length. */
  203. pld_len += desc->cffrm_len[nfrms];
  204. /* Update frame pointer. */
  205. pfrm += skb->len + tpad;
  206. consume_skb(skb);
  207. skb = NULL;
  208. /* Update number of frames. */
  209. nfrms++;
  210. }
  211. /* Unused length fields should be zero-filled (according to SPEC). */
  212. while (nfrms < CFHSI_MAX_PKTS) {
  213. desc->cffrm_len[nfrms] = 0x0000;
  214. nfrms++;
  215. }
  216. /* Check if we can piggy-back another descriptor. */
  217. skb = skb_peek(&cfhsi->qhead);
  218. if (skb)
  219. desc->header |= CFHSI_PIGGY_DESC;
  220. else
  221. desc->header &= ~CFHSI_PIGGY_DESC;
  222. return CFHSI_DESC_SZ + pld_len;
  223. }
  224. static void cfhsi_tx_done_work(struct work_struct *work)
  225. {
  226. struct cfhsi *cfhsi = NULL;
  227. struct cfhsi_desc *desc = NULL;
  228. int len = 0;
  229. int res;
  230. cfhsi = container_of(work, struct cfhsi, tx_done_work);
  231. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  232. __func__);
  233. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  234. return;
  235. desc = (struct cfhsi_desc *)cfhsi->tx_buf;
  236. do {
  237. /*
  238. * Send flow on if flow off has been previously signalled
  239. * and number of packets is below low water mark.
  240. */
  241. spin_lock_bh(&cfhsi->lock);
  242. if (cfhsi->flow_off_sent &&
  243. cfhsi->qhead.qlen <= cfhsi->q_low_mark &&
  244. cfhsi->cfdev.flowctrl) {
  245. cfhsi->flow_off_sent = 0;
  246. cfhsi->cfdev.flowctrl(cfhsi->ndev, ON);
  247. }
  248. spin_unlock_bh(&cfhsi->lock);
  249. /* Create HSI frame. */
  250. len = cfhsi_tx_frm(desc, cfhsi);
  251. if (!len) {
  252. cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
  253. /* Start inactivity timer. */
  254. mod_timer(&cfhsi->timer,
  255. jiffies + CFHSI_INACTIVITY_TOUT);
  256. break;
  257. }
  258. /* Set up new transfer. */
  259. res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
  260. if (WARN_ON(res < 0)) {
  261. dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
  262. __func__, res);
  263. }
  264. } while (res < 0);
  265. }
  266. static void cfhsi_tx_done_cb(struct cfhsi_drv *drv)
  267. {
  268. struct cfhsi *cfhsi;
  269. cfhsi = container_of(drv, struct cfhsi, drv);
  270. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  271. __func__);
  272. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  273. return;
  274. queue_work(cfhsi->wq, &cfhsi->tx_done_work);
  275. }
  276. static int cfhsi_rx_desc(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
  277. {
  278. int xfer_sz = 0;
  279. int nfrms = 0;
  280. u16 *plen = NULL;
  281. u8 *pfrm = NULL;
  282. if ((desc->header & ~CFHSI_PIGGY_DESC) ||
  283. (desc->offset > CFHSI_MAX_EMB_FRM_SZ)) {
  284. dev_err(&cfhsi->ndev->dev, "%s: Invalid descriptor.\n",
  285. __func__);
  286. return 0;
  287. }
  288. /* Check for embedded CAIF frame. */
  289. if (desc->offset) {
  290. struct sk_buff *skb;
  291. u8 *dst = NULL;
  292. int len = 0, retries = 0;
  293. pfrm = ((u8 *)desc) + desc->offset;
  294. /* Remove offset padding. */
  295. pfrm += *pfrm + 1;
  296. /* Read length of CAIF frame (little endian). */
  297. len = *pfrm;
  298. len |= ((*(pfrm+1)) << 8) & 0xFF00;
  299. len += 2; /* Add FCS fields. */
  300. /* Allocate SKB (OK even in IRQ context). */
  301. skb = alloc_skb(len + 1, GFP_KERNEL);
  302. while (!skb) {
  303. retries++;
  304. schedule_timeout(1);
  305. skb = alloc_skb(len + 1, GFP_KERNEL);
  306. if (skb) {
  307. printk(KERN_WARNING "%s: slept for %u "
  308. "before getting memory\n",
  309. __func__, retries);
  310. break;
  311. }
  312. if (retries > HZ) {
  313. printk(KERN_ERR "%s: slept for 1HZ and "
  314. "did not get memory\n",
  315. __func__);
  316. cfhsi->ndev->stats.rx_dropped++;
  317. goto drop_frame;
  318. }
  319. }
  320. caif_assert(skb != NULL);
  321. dst = skb_put(skb, len);
  322. memcpy(dst, pfrm, len);
  323. skb->protocol = htons(ETH_P_CAIF);
  324. skb_reset_mac_header(skb);
  325. skb->dev = cfhsi->ndev;
  326. /*
  327. * We are called from a arch specific platform device.
  328. * Unfortunately we don't know what context we're
  329. * running in.
  330. */
  331. if (in_interrupt())
  332. netif_rx(skb);
  333. else
  334. netif_rx_ni(skb);
  335. /* Update network statistics. */
  336. cfhsi->ndev->stats.rx_packets++;
  337. cfhsi->ndev->stats.rx_bytes += len;
  338. }
  339. drop_frame:
  340. /* Calculate transfer length. */
  341. plen = desc->cffrm_len;
  342. while (nfrms < CFHSI_MAX_PKTS && *plen) {
  343. xfer_sz += *plen;
  344. plen++;
  345. nfrms++;
  346. }
  347. /* Check for piggy-backed descriptor. */
  348. if (desc->header & CFHSI_PIGGY_DESC)
  349. xfer_sz += CFHSI_DESC_SZ;
  350. if (xfer_sz % 4) {
  351. dev_err(&cfhsi->ndev->dev,
  352. "%s: Invalid payload len: %d, ignored.\n",
  353. __func__, xfer_sz);
  354. xfer_sz = 0;
  355. }
  356. return xfer_sz;
  357. }
  358. static int cfhsi_rx_pld(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
  359. {
  360. int rx_sz = 0;
  361. int nfrms = 0;
  362. u16 *plen = NULL;
  363. u8 *pfrm = NULL;
  364. /* Sanity check header and offset. */
  365. if (WARN_ON((desc->header & ~CFHSI_PIGGY_DESC) ||
  366. (desc->offset > CFHSI_MAX_EMB_FRM_SZ))) {
  367. dev_err(&cfhsi->ndev->dev, "%s: Invalid descriptor.\n",
  368. __func__);
  369. return -EINVAL;
  370. }
  371. /* Set frame pointer to start of payload. */
  372. pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
  373. plen = desc->cffrm_len;
  374. while (nfrms < CFHSI_MAX_PKTS && *plen) {
  375. struct sk_buff *skb;
  376. u8 *dst = NULL;
  377. u8 *pcffrm = NULL;
  378. int len = 0, retries = 0;
  379. if (WARN_ON(desc->cffrm_len[nfrms] > CFHSI_MAX_PAYLOAD_SZ)) {
  380. dev_err(&cfhsi->ndev->dev, "%s: Invalid payload.\n",
  381. __func__);
  382. return -EINVAL;
  383. }
  384. /* CAIF frame starts after head padding. */
  385. pcffrm = pfrm + *pfrm + 1;
  386. /* Read length of CAIF frame (little endian). */
  387. len = *pcffrm;
  388. len |= ((*(pcffrm + 1)) << 8) & 0xFF00;
  389. len += 2; /* Add FCS fields. */
  390. /* Allocate SKB (OK even in IRQ context). */
  391. skb = alloc_skb(len + 1, GFP_KERNEL);
  392. while (!skb) {
  393. retries++;
  394. schedule_timeout(1);
  395. skb = alloc_skb(len + 1, GFP_KERNEL);
  396. if (skb) {
  397. printk(KERN_WARNING "%s: slept for %u "
  398. "before getting memory\n",
  399. __func__, retries);
  400. break;
  401. }
  402. if (retries > HZ) {
  403. printk(KERN_ERR "%s: slept for 1HZ "
  404. "and did not get memory\n",
  405. __func__);
  406. cfhsi->ndev->stats.rx_dropped++;
  407. goto drop_frame;
  408. }
  409. }
  410. caif_assert(skb != NULL);
  411. dst = skb_put(skb, len);
  412. memcpy(dst, pcffrm, len);
  413. skb->protocol = htons(ETH_P_CAIF);
  414. skb_reset_mac_header(skb);
  415. skb->dev = cfhsi->ndev;
  416. /*
  417. * We're called from a platform device,
  418. * and don't know the context we're running in.
  419. */
  420. if (in_interrupt())
  421. netif_rx(skb);
  422. else
  423. netif_rx_ni(skb);
  424. /* Update network statistics. */
  425. cfhsi->ndev->stats.rx_packets++;
  426. cfhsi->ndev->stats.rx_bytes += len;
  427. drop_frame:
  428. pfrm += *plen;
  429. rx_sz += *plen;
  430. plen++;
  431. nfrms++;
  432. }
  433. return rx_sz;
  434. }
  435. static void cfhsi_rx_done_work(struct work_struct *work)
  436. {
  437. int res;
  438. int desc_pld_len = 0;
  439. struct cfhsi *cfhsi = NULL;
  440. struct cfhsi_desc *desc = NULL;
  441. cfhsi = container_of(work, struct cfhsi, rx_done_work);
  442. desc = (struct cfhsi_desc *)cfhsi->rx_buf;
  443. dev_dbg(&cfhsi->ndev->dev, "%s: Kick timer if pending.\n",
  444. __func__);
  445. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  446. return;
  447. /* Update inactivity timer if pending. */
  448. mod_timer_pending(&cfhsi->timer, jiffies + CFHSI_INACTIVITY_TOUT);
  449. if (cfhsi->rx_state == CFHSI_RX_STATE_DESC) {
  450. desc_pld_len = cfhsi_rx_desc(desc, cfhsi);
  451. } else {
  452. int pld_len;
  453. pld_len = cfhsi_rx_pld(desc, cfhsi);
  454. if ((pld_len > 0) && (desc->header & CFHSI_PIGGY_DESC)) {
  455. struct cfhsi_desc *piggy_desc;
  456. piggy_desc = (struct cfhsi_desc *)
  457. (desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ +
  458. pld_len);
  459. /* Extract piggy-backed descriptor. */
  460. desc_pld_len = cfhsi_rx_desc(piggy_desc, cfhsi);
  461. /*
  462. * Copy needed information from the piggy-backed
  463. * descriptor to the descriptor in the start.
  464. */
  465. memcpy((u8 *)desc, (u8 *)piggy_desc,
  466. CFHSI_DESC_SHORT_SZ);
  467. }
  468. }
  469. if (desc_pld_len) {
  470. cfhsi->rx_state = CFHSI_RX_STATE_PAYLOAD;
  471. cfhsi->rx_ptr = cfhsi->rx_buf + CFHSI_DESC_SZ;
  472. cfhsi->rx_len = desc_pld_len;
  473. } else {
  474. cfhsi->rx_state = CFHSI_RX_STATE_DESC;
  475. cfhsi->rx_ptr = cfhsi->rx_buf;
  476. cfhsi->rx_len = CFHSI_DESC_SZ;
  477. }
  478. clear_bit(CFHSI_PENDING_RX, &cfhsi->bits);
  479. if (test_bit(CFHSI_AWAKE, &cfhsi->bits)) {
  480. /* Set up new transfer. */
  481. dev_dbg(&cfhsi->ndev->dev, "%s: Start RX.\n",
  482. __func__);
  483. res = cfhsi->dev->cfhsi_rx(cfhsi->rx_ptr, cfhsi->rx_len,
  484. cfhsi->dev);
  485. if (WARN_ON(res < 0)) {
  486. dev_err(&cfhsi->ndev->dev, "%s: RX error %d.\n",
  487. __func__, res);
  488. cfhsi->ndev->stats.rx_errors++;
  489. cfhsi->ndev->stats.rx_dropped++;
  490. }
  491. }
  492. }
  493. static void cfhsi_rx_done_cb(struct cfhsi_drv *drv)
  494. {
  495. struct cfhsi *cfhsi;
  496. cfhsi = container_of(drv, struct cfhsi, drv);
  497. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  498. __func__);
  499. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  500. return;
  501. set_bit(CFHSI_PENDING_RX, &cfhsi->bits);
  502. if (test_and_clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits))
  503. wake_up_interruptible(&cfhsi->flush_fifo_wait);
  504. else
  505. queue_work(cfhsi->wq, &cfhsi->rx_done_work);
  506. }
  507. static void cfhsi_wake_up(struct work_struct *work)
  508. {
  509. struct cfhsi *cfhsi = NULL;
  510. int res;
  511. int len;
  512. long ret;
  513. cfhsi = container_of(work, struct cfhsi, wake_up_work);
  514. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  515. return;
  516. if (unlikely(test_bit(CFHSI_AWAKE, &cfhsi->bits))) {
  517. /* It happenes when wakeup is requested by
  518. * both ends at the same time. */
  519. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  520. return;
  521. }
  522. /* Activate wake line. */
  523. cfhsi->dev->cfhsi_wake_up(cfhsi->dev);
  524. dev_dbg(&cfhsi->ndev->dev, "%s: Start waiting.\n",
  525. __func__);
  526. /* Wait for acknowledge. */
  527. ret = CFHSI_WAKEUP_TOUT;
  528. wait_event_interruptible_timeout(cfhsi->wake_up_wait,
  529. test_bit(CFHSI_WAKE_UP_ACK,
  530. &cfhsi->bits), ret);
  531. if (unlikely(ret < 0)) {
  532. /* Interrupted by signal. */
  533. dev_info(&cfhsi->ndev->dev, "%s: Signalled: %ld.\n",
  534. __func__, ret);
  535. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  536. cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
  537. return;
  538. } else if (!ret) {
  539. /* Wakeup timeout */
  540. dev_err(&cfhsi->ndev->dev, "%s: Timeout.\n",
  541. __func__);
  542. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  543. cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
  544. return;
  545. }
  546. dev_dbg(&cfhsi->ndev->dev, "%s: Woken.\n",
  547. __func__);
  548. /* Clear power up bit. */
  549. set_bit(CFHSI_AWAKE, &cfhsi->bits);
  550. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  551. /* Resume read operation. */
  552. if (!test_bit(CFHSI_PENDING_RX, &cfhsi->bits)) {
  553. dev_dbg(&cfhsi->ndev->dev, "%s: Start RX.\n",
  554. __func__);
  555. res = cfhsi->dev->cfhsi_rx(cfhsi->rx_ptr,
  556. cfhsi->rx_len, cfhsi->dev);
  557. if (WARN_ON(res < 0)) {
  558. dev_err(&cfhsi->ndev->dev, "%s: RX error %d.\n",
  559. __func__, res);
  560. }
  561. }
  562. /* Clear power up acknowledment. */
  563. clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  564. spin_lock_bh(&cfhsi->lock);
  565. /* Resume transmit if queue is not empty. */
  566. if (!skb_peek(&cfhsi->qhead)) {
  567. dev_dbg(&cfhsi->ndev->dev, "%s: Peer wake, start timer.\n",
  568. __func__);
  569. /* Start inactivity timer. */
  570. mod_timer(&cfhsi->timer,
  571. jiffies + CFHSI_INACTIVITY_TOUT);
  572. spin_unlock_bh(&cfhsi->lock);
  573. return;
  574. }
  575. dev_dbg(&cfhsi->ndev->dev, "%s: Host wake.\n",
  576. __func__);
  577. spin_unlock_bh(&cfhsi->lock);
  578. /* Create HSI frame. */
  579. len = cfhsi_tx_frm((struct cfhsi_desc *)cfhsi->tx_buf, cfhsi);
  580. if (likely(len > 0)) {
  581. /* Set up new transfer. */
  582. res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
  583. if (WARN_ON(res < 0)) {
  584. dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
  585. __func__, res);
  586. cfhsi_abort_tx(cfhsi);
  587. }
  588. } else {
  589. dev_err(&cfhsi->ndev->dev,
  590. "%s: Failed to create HSI frame: %d.\n",
  591. __func__, len);
  592. }
  593. }
  594. static void cfhsi_wake_down(struct work_struct *work)
  595. {
  596. long ret;
  597. struct cfhsi *cfhsi = NULL;
  598. size_t fifo_occupancy;
  599. cfhsi = container_of(work, struct cfhsi, wake_down_work);
  600. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  601. __func__);
  602. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  603. return;
  604. /* Check if there is something in FIFO. */
  605. if (WARN_ON(cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
  606. &fifo_occupancy)))
  607. fifo_occupancy = 0;
  608. if (fifo_occupancy) {
  609. dev_dbg(&cfhsi->ndev->dev,
  610. "%s: %u words in RX FIFO, restart timer.\n",
  611. __func__, (unsigned) fifo_occupancy);
  612. spin_lock_bh(&cfhsi->lock);
  613. mod_timer(&cfhsi->timer,
  614. jiffies + CFHSI_INACTIVITY_TOUT);
  615. spin_unlock_bh(&cfhsi->lock);
  616. return;
  617. }
  618. /* Cancel pending RX requests */
  619. cfhsi->dev->cfhsi_rx_cancel(cfhsi->dev);
  620. /* Deactivate wake line. */
  621. cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
  622. /* Wait for acknowledge. */
  623. ret = CFHSI_WAKEUP_TOUT;
  624. ret = wait_event_interruptible_timeout(cfhsi->wake_down_wait,
  625. test_bit(CFHSI_WAKE_DOWN_ACK,
  626. &cfhsi->bits),
  627. ret);
  628. if (ret < 0) {
  629. /* Interrupted by signal. */
  630. dev_info(&cfhsi->ndev->dev, "%s: Signalled: %ld.\n",
  631. __func__, ret);
  632. return;
  633. } else if (!ret) {
  634. /* Timeout */
  635. dev_err(&cfhsi->ndev->dev, "%s: Timeout.\n",
  636. __func__);
  637. }
  638. /* Clear power down acknowledment. */
  639. clear_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
  640. clear_bit(CFHSI_AWAKE, &cfhsi->bits);
  641. /* Check if there is something in FIFO. */
  642. if (WARN_ON(cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
  643. &fifo_occupancy)))
  644. fifo_occupancy = 0;
  645. if (fifo_occupancy) {
  646. dev_dbg(&cfhsi->ndev->dev,
  647. "%s: %u words in RX FIFO, wakeup forced.\n",
  648. __func__, (unsigned) fifo_occupancy);
  649. if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
  650. queue_work(cfhsi->wq, &cfhsi->wake_up_work);
  651. } else
  652. dev_dbg(&cfhsi->ndev->dev, "%s: Done.\n",
  653. __func__);
  654. }
  655. static void cfhsi_wake_up_cb(struct cfhsi_drv *drv)
  656. {
  657. struct cfhsi *cfhsi = NULL;
  658. cfhsi = container_of(drv, struct cfhsi, drv);
  659. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  660. __func__);
  661. set_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  662. wake_up_interruptible(&cfhsi->wake_up_wait);
  663. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  664. return;
  665. /* Schedule wake up work queue if the peer initiates. */
  666. if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
  667. queue_work(cfhsi->wq, &cfhsi->wake_up_work);
  668. }
  669. static void cfhsi_wake_down_cb(struct cfhsi_drv *drv)
  670. {
  671. struct cfhsi *cfhsi = NULL;
  672. cfhsi = container_of(drv, struct cfhsi, drv);
  673. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  674. __func__);
  675. /* Initiating low power is only permitted by the host (us). */
  676. set_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
  677. wake_up_interruptible(&cfhsi->wake_down_wait);
  678. }
  679. static int cfhsi_xmit(struct sk_buff *skb, struct net_device *dev)
  680. {
  681. struct cfhsi *cfhsi = NULL;
  682. int start_xfer = 0;
  683. int timer_active;
  684. if (!dev)
  685. return -EINVAL;
  686. cfhsi = netdev_priv(dev);
  687. spin_lock_bh(&cfhsi->lock);
  688. skb_queue_tail(&cfhsi->qhead, skb);
  689. /* Sanity check; xmit should not be called after unregister_netdev */
  690. if (WARN_ON(test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))) {
  691. spin_unlock_bh(&cfhsi->lock);
  692. cfhsi_abort_tx(cfhsi);
  693. return -EINVAL;
  694. }
  695. /* Send flow off if number of packets is above high water mark. */
  696. if (!cfhsi->flow_off_sent &&
  697. cfhsi->qhead.qlen > cfhsi->q_high_mark &&
  698. cfhsi->cfdev.flowctrl) {
  699. cfhsi->flow_off_sent = 1;
  700. cfhsi->cfdev.flowctrl(cfhsi->ndev, OFF);
  701. }
  702. if (cfhsi->tx_state == CFHSI_TX_STATE_IDLE) {
  703. cfhsi->tx_state = CFHSI_TX_STATE_XFER;
  704. start_xfer = 1;
  705. }
  706. spin_unlock_bh(&cfhsi->lock);
  707. if (!start_xfer)
  708. return 0;
  709. /* Delete inactivity timer if started. */
  710. #ifdef CONFIG_SMP
  711. timer_active = del_timer_sync(&cfhsi->timer);
  712. #else
  713. timer_active = del_timer(&cfhsi->timer);
  714. #endif /* CONFIG_SMP */
  715. if (timer_active) {
  716. struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
  717. int len;
  718. int res;
  719. /* Create HSI frame. */
  720. len = cfhsi_tx_frm(desc, cfhsi);
  721. BUG_ON(!len);
  722. /* Set up new transfer. */
  723. res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
  724. if (WARN_ON(res < 0)) {
  725. dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
  726. __func__, res);
  727. cfhsi_abort_tx(cfhsi);
  728. }
  729. } else {
  730. /* Schedule wake up work queue if the we initiate. */
  731. if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
  732. queue_work(cfhsi->wq, &cfhsi->wake_up_work);
  733. }
  734. return 0;
  735. }
  736. static int cfhsi_open(struct net_device *dev)
  737. {
  738. netif_wake_queue(dev);
  739. return 0;
  740. }
  741. static int cfhsi_close(struct net_device *dev)
  742. {
  743. netif_stop_queue(dev);
  744. return 0;
  745. }
  746. static const struct net_device_ops cfhsi_ops = {
  747. .ndo_open = cfhsi_open,
  748. .ndo_stop = cfhsi_close,
  749. .ndo_start_xmit = cfhsi_xmit
  750. };
  751. static void cfhsi_setup(struct net_device *dev)
  752. {
  753. struct cfhsi *cfhsi = netdev_priv(dev);
  754. dev->features = 0;
  755. dev->netdev_ops = &cfhsi_ops;
  756. dev->type = ARPHRD_CAIF;
  757. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  758. dev->mtu = CFHSI_MAX_PAYLOAD_SZ;
  759. dev->tx_queue_len = 0;
  760. dev->destructor = free_netdev;
  761. skb_queue_head_init(&cfhsi->qhead);
  762. cfhsi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
  763. cfhsi->cfdev.use_frag = false;
  764. cfhsi->cfdev.use_stx = false;
  765. cfhsi->cfdev.use_fcs = false;
  766. cfhsi->ndev = dev;
  767. }
  768. int cfhsi_probe(struct platform_device *pdev)
  769. {
  770. struct cfhsi *cfhsi = NULL;
  771. struct net_device *ndev;
  772. struct cfhsi_dev *dev;
  773. int res;
  774. ndev = alloc_netdev(sizeof(struct cfhsi), "cfhsi%d", cfhsi_setup);
  775. if (!ndev)
  776. return -ENODEV;
  777. cfhsi = netdev_priv(ndev);
  778. cfhsi->ndev = ndev;
  779. cfhsi->pdev = pdev;
  780. /* Initialize state vaiables. */
  781. cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
  782. cfhsi->rx_state = CFHSI_RX_STATE_DESC;
  783. /* Set flow info */
  784. cfhsi->flow_off_sent = 0;
  785. cfhsi->q_low_mark = LOW_WATER_MARK;
  786. cfhsi->q_high_mark = HIGH_WATER_MARK;
  787. /* Assign the HSI device. */
  788. dev = (struct cfhsi_dev *)pdev->dev.platform_data;
  789. cfhsi->dev = dev;
  790. /* Assign the driver to this HSI device. */
  791. dev->drv = &cfhsi->drv;
  792. /*
  793. * Allocate a TX buffer with the size of a HSI packet descriptors
  794. * and the necessary room for CAIF payload frames.
  795. */
  796. cfhsi->tx_buf = kzalloc(CFHSI_BUF_SZ_TX, GFP_KERNEL);
  797. if (!cfhsi->tx_buf) {
  798. res = -ENODEV;
  799. goto err_alloc_tx;
  800. }
  801. /*
  802. * Allocate a RX buffer with the size of two HSI packet descriptors and
  803. * the necessary room for CAIF payload frames.
  804. */
  805. cfhsi->rx_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
  806. if (!cfhsi->rx_buf) {
  807. res = -ENODEV;
  808. goto err_alloc_rx;
  809. }
  810. /* Initialize receive variables. */
  811. cfhsi->rx_ptr = cfhsi->rx_buf;
  812. cfhsi->rx_len = CFHSI_DESC_SZ;
  813. /* Initialize spin locks. */
  814. spin_lock_init(&cfhsi->lock);
  815. /* Set up the driver. */
  816. cfhsi->drv.tx_done_cb = cfhsi_tx_done_cb;
  817. cfhsi->drv.rx_done_cb = cfhsi_rx_done_cb;
  818. cfhsi->drv.wake_up_cb = cfhsi_wake_up_cb;
  819. cfhsi->drv.wake_down_cb = cfhsi_wake_down_cb;
  820. /* Initialize the work queues. */
  821. INIT_WORK(&cfhsi->wake_up_work, cfhsi_wake_up);
  822. INIT_WORK(&cfhsi->wake_down_work, cfhsi_wake_down);
  823. INIT_WORK(&cfhsi->rx_done_work, cfhsi_rx_done_work);
  824. INIT_WORK(&cfhsi->tx_done_work, cfhsi_tx_done_work);
  825. /* Clear all bit fields. */
  826. clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  827. clear_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
  828. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  829. clear_bit(CFHSI_AWAKE, &cfhsi->bits);
  830. clear_bit(CFHSI_PENDING_RX, &cfhsi->bits);
  831. /* Create work thread. */
  832. cfhsi->wq = create_singlethread_workqueue(pdev->name);
  833. if (!cfhsi->wq) {
  834. dev_err(&ndev->dev, "%s: Failed to create work queue.\n",
  835. __func__);
  836. res = -ENODEV;
  837. goto err_create_wq;
  838. }
  839. /* Initialize wait queues. */
  840. init_waitqueue_head(&cfhsi->wake_up_wait);
  841. init_waitqueue_head(&cfhsi->wake_down_wait);
  842. init_waitqueue_head(&cfhsi->flush_fifo_wait);
  843. /* Setup the inactivity timer. */
  844. init_timer(&cfhsi->timer);
  845. cfhsi->timer.data = (unsigned long)cfhsi;
  846. cfhsi->timer.function = cfhsi_inactivity_tout;
  847. /* Add CAIF HSI device to list. */
  848. spin_lock(&cfhsi_list_lock);
  849. list_add_tail(&cfhsi->list, &cfhsi_list);
  850. spin_unlock(&cfhsi_list_lock);
  851. /* Activate HSI interface. */
  852. res = cfhsi->dev->cfhsi_up(cfhsi->dev);
  853. if (res) {
  854. dev_err(&cfhsi->ndev->dev,
  855. "%s: can't activate HSI interface: %d.\n",
  856. __func__, res);
  857. goto err_activate;
  858. }
  859. /* Flush FIFO */
  860. res = cfhsi_flush_fifo(cfhsi);
  861. if (res) {
  862. dev_err(&ndev->dev, "%s: Can't flush FIFO: %d.\n",
  863. __func__, res);
  864. goto err_net_reg;
  865. }
  866. /* Register network device. */
  867. res = register_netdev(ndev);
  868. if (res) {
  869. dev_err(&ndev->dev, "%s: Registration error: %d.\n",
  870. __func__, res);
  871. goto err_net_reg;
  872. }
  873. netif_stop_queue(ndev);
  874. return res;
  875. err_net_reg:
  876. cfhsi->dev->cfhsi_down(cfhsi->dev);
  877. err_activate:
  878. destroy_workqueue(cfhsi->wq);
  879. err_create_wq:
  880. kfree(cfhsi->rx_buf);
  881. err_alloc_rx:
  882. kfree(cfhsi->tx_buf);
  883. err_alloc_tx:
  884. free_netdev(ndev);
  885. return res;
  886. }
  887. static void cfhsi_shutdown(struct cfhsi *cfhsi, bool remove_platform_dev)
  888. {
  889. u8 *tx_buf, *rx_buf;
  890. /* Stop TXing */
  891. netif_tx_stop_all_queues(cfhsi->ndev);
  892. /* going to shutdown driver */
  893. set_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
  894. if (remove_platform_dev) {
  895. /* Flush workqueue */
  896. flush_workqueue(cfhsi->wq);
  897. /* Notify device. */
  898. platform_device_unregister(cfhsi->pdev);
  899. }
  900. /* Flush workqueue */
  901. flush_workqueue(cfhsi->wq);
  902. /* Delete timer if pending */
  903. #ifdef CONFIG_SMP
  904. del_timer_sync(&cfhsi->timer);
  905. #else
  906. del_timer(&cfhsi->timer);
  907. #endif /* CONFIG_SMP */
  908. /* Cancel pending RX request (if any) */
  909. cfhsi->dev->cfhsi_rx_cancel(cfhsi->dev);
  910. /* Flush again and destroy workqueue */
  911. destroy_workqueue(cfhsi->wq);
  912. /* Store bufferes: will be freed later. */
  913. tx_buf = cfhsi->tx_buf;
  914. rx_buf = cfhsi->rx_buf;
  915. /* Flush transmit queues. */
  916. cfhsi_abort_tx(cfhsi);
  917. /* Deactivate interface */
  918. cfhsi->dev->cfhsi_down(cfhsi->dev);
  919. /* Finally unregister the network device. */
  920. unregister_netdev(cfhsi->ndev);
  921. /* Free buffers. */
  922. kfree(tx_buf);
  923. kfree(rx_buf);
  924. }
  925. int cfhsi_remove(struct platform_device *pdev)
  926. {
  927. struct list_head *list_node;
  928. struct list_head *n;
  929. struct cfhsi *cfhsi = NULL;
  930. struct cfhsi_dev *dev;
  931. dev = (struct cfhsi_dev *)pdev->dev.platform_data;
  932. spin_lock(&cfhsi_list_lock);
  933. list_for_each_safe(list_node, n, &cfhsi_list) {
  934. cfhsi = list_entry(list_node, struct cfhsi, list);
  935. /* Find the corresponding device. */
  936. if (cfhsi->dev == dev) {
  937. /* Remove from list. */
  938. list_del(list_node);
  939. spin_unlock(&cfhsi_list_lock);
  940. /* Shutdown driver. */
  941. cfhsi_shutdown(cfhsi, false);
  942. return 0;
  943. }
  944. }
  945. spin_unlock(&cfhsi_list_lock);
  946. return -ENODEV;
  947. }
  948. struct platform_driver cfhsi_plat_drv = {
  949. .probe = cfhsi_probe,
  950. .remove = cfhsi_remove,
  951. .driver = {
  952. .name = "cfhsi",
  953. .owner = THIS_MODULE,
  954. },
  955. };
  956. static void __exit cfhsi_exit_module(void)
  957. {
  958. struct list_head *list_node;
  959. struct list_head *n;
  960. struct cfhsi *cfhsi = NULL;
  961. spin_lock(&cfhsi_list_lock);
  962. list_for_each_safe(list_node, n, &cfhsi_list) {
  963. cfhsi = list_entry(list_node, struct cfhsi, list);
  964. /* Remove from list. */
  965. list_del(list_node);
  966. spin_unlock(&cfhsi_list_lock);
  967. /* Shutdown driver. */
  968. cfhsi_shutdown(cfhsi, true);
  969. spin_lock(&cfhsi_list_lock);
  970. }
  971. spin_unlock(&cfhsi_list_lock);
  972. /* Unregister platform driver. */
  973. platform_driver_unregister(&cfhsi_plat_drv);
  974. }
  975. static int __init cfhsi_init_module(void)
  976. {
  977. int result;
  978. /* Initialize spin lock. */
  979. spin_lock_init(&cfhsi_list_lock);
  980. /* Register platform driver. */
  981. result = platform_driver_register(&cfhsi_plat_drv);
  982. if (result) {
  983. printk(KERN_ERR "Could not register platform HSI driver: %d.\n",
  984. result);
  985. goto err_dev_register;
  986. }
  987. return result;
  988. err_dev_register:
  989. return result;
  990. }
  991. module_init(cfhsi_init_module);
  992. module_exit(cfhsi_exit_module);