caif_hsi.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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 <linux/rtnetlink.h>
  21. #include <linux/pkt_sched.h>
  22. #include <net/caif/caif_layer.h>
  23. #include <net/caif/caif_hsi.h>
  24. MODULE_LICENSE("GPL");
  25. MODULE_AUTHOR("Daniel Martensson<daniel.martensson@stericsson.com>");
  26. MODULE_DESCRIPTION("CAIF HSI driver");
  27. /* Returns the number of padding bytes for alignment. */
  28. #define PAD_POW2(x, pow) ((((x)&((pow)-1)) == 0) ? 0 :\
  29. (((pow)-((x)&((pow)-1)))))
  30. static int inactivity_timeout = 1000;
  31. module_param(inactivity_timeout, int, S_IRUGO | S_IWUSR);
  32. MODULE_PARM_DESC(inactivity_timeout, "Inactivity timeout on HSI, ms.");
  33. static int aggregation_timeout = 1;
  34. module_param(aggregation_timeout, int, S_IRUGO | S_IWUSR);
  35. MODULE_PARM_DESC(aggregation_timeout, "Aggregation timeout on HSI, ms.");
  36. /*
  37. * HSI padding options.
  38. * Warning: must be a base of 2 (& operation used) and can not be zero !
  39. */
  40. static int hsi_head_align = 4;
  41. module_param(hsi_head_align, int, S_IRUGO);
  42. MODULE_PARM_DESC(hsi_head_align, "HSI head alignment.");
  43. static int hsi_tail_align = 4;
  44. module_param(hsi_tail_align, int, S_IRUGO);
  45. MODULE_PARM_DESC(hsi_tail_align, "HSI tail alignment.");
  46. /*
  47. * HSI link layer flowcontrol thresholds.
  48. * Warning: A high threshold value migth increase throughput but it will at
  49. * the same time prevent channel prioritization and increase the risk of
  50. * flooding the modem. The high threshold should be above the low.
  51. */
  52. static int hsi_high_threshold = 100;
  53. module_param(hsi_high_threshold, int, S_IRUGO);
  54. MODULE_PARM_DESC(hsi_high_threshold, "HSI high threshold (FLOW OFF).");
  55. static int hsi_low_threshold = 50;
  56. module_param(hsi_low_threshold, int, S_IRUGO);
  57. MODULE_PARM_DESC(hsi_low_threshold, "HSI high threshold (FLOW ON).");
  58. #define ON 1
  59. #define OFF 0
  60. /*
  61. * Threshold values for the HSI packet queue. Flowcontrol will be asserted
  62. * when the number of packets exceeds HIGH_WATER_MARK. It will not be
  63. * de-asserted before the number of packets drops below LOW_WATER_MARK.
  64. */
  65. #define LOW_WATER_MARK hsi_low_threshold
  66. #define HIGH_WATER_MARK hsi_high_threshold
  67. static LIST_HEAD(cfhsi_list);
  68. static spinlock_t cfhsi_list_lock;
  69. static void cfhsi_inactivity_tout(unsigned long arg)
  70. {
  71. struct cfhsi *cfhsi = (struct cfhsi *)arg;
  72. dev_dbg(&cfhsi->ndev->dev, "%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. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  151. __func__);
  152. do {
  153. ret = cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
  154. &fifo_occupancy);
  155. if (ret) {
  156. dev_warn(&cfhsi->ndev->dev,
  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. dev_warn(&cfhsi->ndev->dev,
  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. dev_warn(&cfhsi->ndev->dev,
  179. "%s: can't wait for flush complete: %d.\n",
  180. __func__, ret);
  181. break;
  182. } else if (!ret) {
  183. ret = -ETIMEDOUT;
  184. dev_warn(&cfhsi->ndev->dev,
  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 = 0;
  207. int tpad = 0;
  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 = 0;
  236. int tpad = 0;
  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. dev_dbg(&cfhsi->ndev->dev, "%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. dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
  308. __func__, res);
  309. } while (res < 0);
  310. }
  311. static void cfhsi_tx_done(struct cfhsi *cfhsi)
  312. {
  313. dev_dbg(&cfhsi->ndev->dev, "%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. dev_dbg(&cfhsi->ndev->dev, "%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. dev_err(&cfhsi->ndev->dev, "%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. dev_err(&cfhsi->ndev->dev, "%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. dev_err(&cfhsi->ndev->dev, "%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. dev_err(&cfhsi->ndev->dev,
  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. dev_err(&cfhsi->ndev->dev, "%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 = 0;
  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. dev_err(&cfhsi->ndev->dev, "%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. dev_err(&cfhsi->ndev->dev, "%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. dev_dbg(&cfhsi->ndev->dev, "%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 (desc_pld_len > 0 &&
  560. (piggy_desc->header & CFHSI_PIGGY_DESC))
  561. rx_len += CFHSI_DESC_SZ;
  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. if (desc_pld_len == -EPROTO)
  571. goto out_of_sync;
  572. }
  573. }
  574. if (desc_pld_len) {
  575. rx_state = CFHSI_RX_STATE_PAYLOAD;
  576. rx_ptr = rx_buf + CFHSI_DESC_SZ;
  577. } else {
  578. rx_state = CFHSI_RX_STATE_DESC;
  579. rx_ptr = rx_buf;
  580. rx_len = CFHSI_DESC_SZ;
  581. }
  582. /* Initiate next read */
  583. if (test_bit(CFHSI_AWAKE, &cfhsi->bits)) {
  584. /* Set up new transfer. */
  585. dev_dbg(&cfhsi->ndev->dev, "%s: Start RX.\n",
  586. __func__);
  587. res = cfhsi->dev->cfhsi_rx(rx_ptr, rx_len,
  588. cfhsi->dev);
  589. if (WARN_ON(res < 0)) {
  590. dev_err(&cfhsi->ndev->dev, "%s: RX error %d.\n",
  591. __func__, res);
  592. cfhsi->ndev->stats.rx_errors++;
  593. cfhsi->ndev->stats.rx_dropped++;
  594. }
  595. }
  596. if (cfhsi->rx_state.state == CFHSI_RX_STATE_DESC) {
  597. /* Extract payload from descriptor */
  598. if (cfhsi_rx_desc(desc, cfhsi) < 0)
  599. goto out_of_sync;
  600. } else {
  601. /* Extract payload */
  602. if (cfhsi_rx_pld(desc, cfhsi) < 0)
  603. goto out_of_sync;
  604. if (piggy_desc) {
  605. /* Extract any payload in piggyback descriptor. */
  606. if (cfhsi_rx_desc(piggy_desc, cfhsi) < 0)
  607. goto out_of_sync;
  608. }
  609. }
  610. /* Update state info */
  611. memset(&cfhsi->rx_state, 0, sizeof(cfhsi->rx_state));
  612. cfhsi->rx_state.state = rx_state;
  613. cfhsi->rx_ptr = rx_ptr;
  614. cfhsi->rx_len = rx_len;
  615. cfhsi->rx_state.pld_len = desc_pld_len;
  616. cfhsi->rx_state.piggy_desc = desc->header & CFHSI_PIGGY_DESC;
  617. if (rx_buf != cfhsi->rx_buf)
  618. swap(cfhsi->rx_buf, cfhsi->rx_flip_buf);
  619. return;
  620. out_of_sync:
  621. dev_err(&cfhsi->ndev->dev, "%s: Out of sync.\n", __func__);
  622. print_hex_dump_bytes("--> ", DUMP_PREFIX_NONE,
  623. cfhsi->rx_buf, CFHSI_DESC_SZ);
  624. schedule_work(&cfhsi->out_of_sync_work);
  625. }
  626. static void cfhsi_rx_slowpath(unsigned long arg)
  627. {
  628. struct cfhsi *cfhsi = (struct cfhsi *)arg;
  629. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  630. __func__);
  631. cfhsi_rx_done(cfhsi);
  632. }
  633. static void cfhsi_rx_done_cb(struct cfhsi_drv *drv)
  634. {
  635. struct cfhsi *cfhsi;
  636. cfhsi = container_of(drv, struct cfhsi, drv);
  637. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  638. __func__);
  639. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  640. return;
  641. if (test_and_clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits))
  642. wake_up_interruptible(&cfhsi->flush_fifo_wait);
  643. else
  644. cfhsi_rx_done(cfhsi);
  645. }
  646. static void cfhsi_wake_up(struct work_struct *work)
  647. {
  648. struct cfhsi *cfhsi = NULL;
  649. int res;
  650. int len;
  651. long ret;
  652. cfhsi = container_of(work, struct cfhsi, wake_up_work);
  653. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  654. return;
  655. if (unlikely(test_bit(CFHSI_AWAKE, &cfhsi->bits))) {
  656. /* It happenes when wakeup is requested by
  657. * both ends at the same time. */
  658. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  659. clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  660. return;
  661. }
  662. /* Activate wake line. */
  663. cfhsi->dev->cfhsi_wake_up(cfhsi->dev);
  664. dev_dbg(&cfhsi->ndev->dev, "%s: Start waiting.\n",
  665. __func__);
  666. /* Wait for acknowledge. */
  667. ret = CFHSI_WAKE_TOUT;
  668. ret = wait_event_interruptible_timeout(cfhsi->wake_up_wait,
  669. test_and_clear_bit(CFHSI_WAKE_UP_ACK,
  670. &cfhsi->bits), ret);
  671. if (unlikely(ret < 0)) {
  672. /* Interrupted by signal. */
  673. dev_err(&cfhsi->ndev->dev, "%s: Signalled: %ld.\n",
  674. __func__, ret);
  675. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  676. cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
  677. return;
  678. } else if (!ret) {
  679. bool ca_wake = false;
  680. size_t fifo_occupancy = 0;
  681. /* Wakeup timeout */
  682. dev_dbg(&cfhsi->ndev->dev, "%s: Timeout.\n",
  683. __func__);
  684. /* Check FIFO to check if modem has sent something. */
  685. WARN_ON(cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
  686. &fifo_occupancy));
  687. dev_dbg(&cfhsi->ndev->dev, "%s: Bytes in FIFO: %u.\n",
  688. __func__, (unsigned) fifo_occupancy);
  689. /* Check if we misssed the interrupt. */
  690. WARN_ON(cfhsi->dev->cfhsi_get_peer_wake(cfhsi->dev,
  691. &ca_wake));
  692. if (ca_wake) {
  693. dev_err(&cfhsi->ndev->dev, "%s: CA Wake missed !.\n",
  694. __func__);
  695. /* Clear the CFHSI_WAKE_UP_ACK bit to prevent race. */
  696. clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  697. /* Continue execution. */
  698. goto wake_ack;
  699. }
  700. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  701. cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
  702. return;
  703. }
  704. wake_ack:
  705. dev_dbg(&cfhsi->ndev->dev, "%s: Woken.\n",
  706. __func__);
  707. /* Clear power up bit. */
  708. set_bit(CFHSI_AWAKE, &cfhsi->bits);
  709. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  710. /* Resume read operation. */
  711. dev_dbg(&cfhsi->ndev->dev, "%s: Start RX.\n", __func__);
  712. res = cfhsi->dev->cfhsi_rx(cfhsi->rx_ptr, cfhsi->rx_len, cfhsi->dev);
  713. if (WARN_ON(res < 0))
  714. dev_err(&cfhsi->ndev->dev, "%s: RX err %d.\n", __func__, res);
  715. /* Clear power up acknowledment. */
  716. clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  717. spin_lock_bh(&cfhsi->lock);
  718. /* Resume transmit if queues are not empty. */
  719. if (!cfhsi_tx_queue_len(cfhsi)) {
  720. dev_dbg(&cfhsi->ndev->dev, "%s: Peer wake, start timer.\n",
  721. __func__);
  722. /* Start inactivity timer. */
  723. mod_timer(&cfhsi->inactivity_timer,
  724. jiffies + cfhsi->inactivity_timeout);
  725. spin_unlock_bh(&cfhsi->lock);
  726. return;
  727. }
  728. dev_dbg(&cfhsi->ndev->dev, "%s: Host wake.\n",
  729. __func__);
  730. spin_unlock_bh(&cfhsi->lock);
  731. /* Create HSI frame. */
  732. len = cfhsi_tx_frm((struct cfhsi_desc *)cfhsi->tx_buf, cfhsi);
  733. if (likely(len > 0)) {
  734. /* Set up new transfer. */
  735. res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
  736. if (WARN_ON(res < 0)) {
  737. dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
  738. __func__, res);
  739. cfhsi_abort_tx(cfhsi);
  740. }
  741. } else {
  742. dev_err(&cfhsi->ndev->dev,
  743. "%s: Failed to create HSI frame: %d.\n",
  744. __func__, len);
  745. }
  746. }
  747. static void cfhsi_wake_down(struct work_struct *work)
  748. {
  749. long ret;
  750. struct cfhsi *cfhsi = NULL;
  751. size_t fifo_occupancy = 0;
  752. int retry = CFHSI_WAKE_TOUT;
  753. cfhsi = container_of(work, struct cfhsi, wake_down_work);
  754. dev_dbg(&cfhsi->ndev->dev, "%s.\n", __func__);
  755. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  756. return;
  757. /* Deactivate wake line. */
  758. cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
  759. /* Wait for acknowledge. */
  760. ret = CFHSI_WAKE_TOUT;
  761. ret = wait_event_interruptible_timeout(cfhsi->wake_down_wait,
  762. test_and_clear_bit(CFHSI_WAKE_DOWN_ACK,
  763. &cfhsi->bits), ret);
  764. if (ret < 0) {
  765. /* Interrupted by signal. */
  766. dev_err(&cfhsi->ndev->dev, "%s: Signalled: %ld.\n",
  767. __func__, ret);
  768. return;
  769. } else if (!ret) {
  770. bool ca_wake = true;
  771. /* Timeout */
  772. dev_err(&cfhsi->ndev->dev, "%s: Timeout.\n", __func__);
  773. /* Check if we misssed the interrupt. */
  774. WARN_ON(cfhsi->dev->cfhsi_get_peer_wake(cfhsi->dev,
  775. &ca_wake));
  776. if (!ca_wake)
  777. dev_err(&cfhsi->ndev->dev, "%s: CA Wake missed !.\n",
  778. __func__);
  779. }
  780. /* Check FIFO occupancy. */
  781. while (retry) {
  782. WARN_ON(cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
  783. &fifo_occupancy));
  784. if (!fifo_occupancy)
  785. break;
  786. set_current_state(TASK_INTERRUPTIBLE);
  787. schedule_timeout(1);
  788. retry--;
  789. }
  790. if (!retry)
  791. dev_err(&cfhsi->ndev->dev, "%s: FIFO Timeout.\n", __func__);
  792. /* Clear AWAKE condition. */
  793. clear_bit(CFHSI_AWAKE, &cfhsi->bits);
  794. /* Cancel pending RX requests. */
  795. cfhsi->dev->cfhsi_rx_cancel(cfhsi->dev);
  796. }
  797. static void cfhsi_out_of_sync(struct work_struct *work)
  798. {
  799. struct cfhsi *cfhsi = NULL;
  800. cfhsi = container_of(work, struct cfhsi, out_of_sync_work);
  801. rtnl_lock();
  802. dev_close(cfhsi->ndev);
  803. rtnl_unlock();
  804. }
  805. static void cfhsi_wake_up_cb(struct cfhsi_drv *drv)
  806. {
  807. struct cfhsi *cfhsi = NULL;
  808. cfhsi = container_of(drv, struct cfhsi, drv);
  809. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  810. __func__);
  811. set_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  812. wake_up_interruptible(&cfhsi->wake_up_wait);
  813. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  814. return;
  815. /* Schedule wake up work queue if the peer initiates. */
  816. if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
  817. queue_work(cfhsi->wq, &cfhsi->wake_up_work);
  818. }
  819. static void cfhsi_wake_down_cb(struct cfhsi_drv *drv)
  820. {
  821. struct cfhsi *cfhsi = NULL;
  822. cfhsi = container_of(drv, struct cfhsi, drv);
  823. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  824. __func__);
  825. /* Initiating low power is only permitted by the host (us). */
  826. set_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
  827. wake_up_interruptible(&cfhsi->wake_down_wait);
  828. }
  829. static void cfhsi_aggregation_tout(unsigned long arg)
  830. {
  831. struct cfhsi *cfhsi = (struct cfhsi *)arg;
  832. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  833. __func__);
  834. cfhsi_start_tx(cfhsi);
  835. }
  836. static int cfhsi_xmit(struct sk_buff *skb, struct net_device *dev)
  837. {
  838. struct cfhsi *cfhsi = NULL;
  839. int start_xfer = 0;
  840. int timer_active;
  841. int prio;
  842. if (!dev)
  843. return -EINVAL;
  844. cfhsi = netdev_priv(dev);
  845. switch (skb->priority) {
  846. case TC_PRIO_BESTEFFORT:
  847. case TC_PRIO_FILLER:
  848. case TC_PRIO_BULK:
  849. prio = CFHSI_PRIO_BEBK;
  850. break;
  851. case TC_PRIO_INTERACTIVE_BULK:
  852. prio = CFHSI_PRIO_VI;
  853. break;
  854. case TC_PRIO_INTERACTIVE:
  855. prio = CFHSI_PRIO_VO;
  856. break;
  857. case TC_PRIO_CONTROL:
  858. default:
  859. prio = CFHSI_PRIO_CTL;
  860. break;
  861. }
  862. spin_lock_bh(&cfhsi->lock);
  863. /* Update aggregation statistics */
  864. cfhsi_update_aggregation_stats(cfhsi, skb, 1);
  865. /* Queue the SKB */
  866. skb_queue_tail(&cfhsi->qhead[prio], skb);
  867. /* Sanity check; xmit should not be called after unregister_netdev */
  868. if (WARN_ON(test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))) {
  869. spin_unlock_bh(&cfhsi->lock);
  870. cfhsi_abort_tx(cfhsi);
  871. return -EINVAL;
  872. }
  873. /* Send flow off if number of packets is above high water mark. */
  874. if (!cfhsi->flow_off_sent &&
  875. cfhsi_tx_queue_len(cfhsi) > cfhsi->q_high_mark &&
  876. cfhsi->cfdev.flowctrl) {
  877. cfhsi->flow_off_sent = 1;
  878. cfhsi->cfdev.flowctrl(cfhsi->ndev, OFF);
  879. }
  880. if (cfhsi->tx_state == CFHSI_TX_STATE_IDLE) {
  881. cfhsi->tx_state = CFHSI_TX_STATE_XFER;
  882. start_xfer = 1;
  883. }
  884. if (!start_xfer) {
  885. /* Send aggregate if it is possible */
  886. bool aggregate_ready =
  887. cfhsi_can_send_aggregate(cfhsi) &&
  888. del_timer(&cfhsi->aggregation_timer) > 0;
  889. spin_unlock_bh(&cfhsi->lock);
  890. if (aggregate_ready)
  891. cfhsi_start_tx(cfhsi);
  892. return 0;
  893. }
  894. /* Delete inactivity timer if started. */
  895. timer_active = del_timer_sync(&cfhsi->inactivity_timer);
  896. spin_unlock_bh(&cfhsi->lock);
  897. if (timer_active) {
  898. struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
  899. int len;
  900. int res;
  901. /* Create HSI frame. */
  902. len = cfhsi_tx_frm(desc, cfhsi);
  903. WARN_ON(!len);
  904. /* Set up new transfer. */
  905. res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
  906. if (WARN_ON(res < 0)) {
  907. dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
  908. __func__, res);
  909. cfhsi_abort_tx(cfhsi);
  910. }
  911. } else {
  912. /* Schedule wake up work queue if the we initiate. */
  913. if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
  914. queue_work(cfhsi->wq, &cfhsi->wake_up_work);
  915. }
  916. return 0;
  917. }
  918. static int cfhsi_open(struct net_device *dev)
  919. {
  920. return 0;
  921. }
  922. static int cfhsi_close(struct net_device *dev)
  923. {
  924. return 0;
  925. }
  926. static const struct net_device_ops cfhsi_ops = {
  927. .ndo_open = cfhsi_open,
  928. .ndo_stop = cfhsi_close,
  929. .ndo_start_xmit = cfhsi_xmit
  930. };
  931. static void cfhsi_setup(struct net_device *dev)
  932. {
  933. int i;
  934. struct cfhsi *cfhsi = netdev_priv(dev);
  935. dev->features = 0;
  936. dev->netdev_ops = &cfhsi_ops;
  937. dev->type = ARPHRD_CAIF;
  938. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  939. dev->mtu = CFHSI_MAX_CAIF_FRAME_SZ;
  940. dev->tx_queue_len = 0;
  941. dev->destructor = free_netdev;
  942. for (i = 0; i < CFHSI_PRIO_LAST; ++i)
  943. skb_queue_head_init(&cfhsi->qhead[i]);
  944. cfhsi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
  945. cfhsi->cfdev.use_frag = false;
  946. cfhsi->cfdev.use_stx = false;
  947. cfhsi->cfdev.use_fcs = false;
  948. cfhsi->ndev = dev;
  949. }
  950. int cfhsi_probe(struct platform_device *pdev)
  951. {
  952. struct cfhsi *cfhsi = NULL;
  953. struct net_device *ndev;
  954. struct cfhsi_dev *dev;
  955. int res;
  956. ndev = alloc_netdev(sizeof(struct cfhsi), "cfhsi%d", cfhsi_setup);
  957. if (!ndev)
  958. return -ENODEV;
  959. cfhsi = netdev_priv(ndev);
  960. cfhsi->ndev = ndev;
  961. cfhsi->pdev = pdev;
  962. /* Initialize state vaiables. */
  963. cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
  964. cfhsi->rx_state.state = CFHSI_RX_STATE_DESC;
  965. /* Set flow info */
  966. cfhsi->flow_off_sent = 0;
  967. cfhsi->q_low_mark = LOW_WATER_MARK;
  968. cfhsi->q_high_mark = HIGH_WATER_MARK;
  969. /* Assign the HSI device. */
  970. dev = (struct cfhsi_dev *)pdev->dev.platform_data;
  971. cfhsi->dev = dev;
  972. /* Assign the driver to this HSI device. */
  973. dev->drv = &cfhsi->drv;
  974. /*
  975. * Allocate a TX buffer with the size of a HSI packet descriptors
  976. * and the necessary room for CAIF payload frames.
  977. */
  978. cfhsi->tx_buf = kzalloc(CFHSI_BUF_SZ_TX, GFP_KERNEL);
  979. if (!cfhsi->tx_buf) {
  980. res = -ENODEV;
  981. goto err_alloc_tx;
  982. }
  983. /*
  984. * Allocate a RX buffer with the size of two HSI packet descriptors and
  985. * the necessary room for CAIF payload frames.
  986. */
  987. cfhsi->rx_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
  988. if (!cfhsi->rx_buf) {
  989. res = -ENODEV;
  990. goto err_alloc_rx;
  991. }
  992. cfhsi->rx_flip_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
  993. if (!cfhsi->rx_flip_buf) {
  994. res = -ENODEV;
  995. goto err_alloc_rx_flip;
  996. }
  997. /* Pre-calculate inactivity timeout. */
  998. if (inactivity_timeout != -1) {
  999. cfhsi->inactivity_timeout =
  1000. inactivity_timeout * HZ / 1000;
  1001. if (!cfhsi->inactivity_timeout)
  1002. cfhsi->inactivity_timeout = 1;
  1003. else if (cfhsi->inactivity_timeout > NEXT_TIMER_MAX_DELTA)
  1004. cfhsi->inactivity_timeout = NEXT_TIMER_MAX_DELTA;
  1005. } else {
  1006. cfhsi->inactivity_timeout = NEXT_TIMER_MAX_DELTA;
  1007. }
  1008. /* Initialize aggregation timeout */
  1009. cfhsi->aggregation_timeout = aggregation_timeout;
  1010. /* Initialize recieve vaiables. */
  1011. cfhsi->rx_ptr = cfhsi->rx_buf;
  1012. cfhsi->rx_len = CFHSI_DESC_SZ;
  1013. /* Initialize spin locks. */
  1014. spin_lock_init(&cfhsi->lock);
  1015. /* Set up the driver. */
  1016. cfhsi->drv.tx_done_cb = cfhsi_tx_done_cb;
  1017. cfhsi->drv.rx_done_cb = cfhsi_rx_done_cb;
  1018. cfhsi->drv.wake_up_cb = cfhsi_wake_up_cb;
  1019. cfhsi->drv.wake_down_cb = cfhsi_wake_down_cb;
  1020. /* Initialize the work queues. */
  1021. INIT_WORK(&cfhsi->wake_up_work, cfhsi_wake_up);
  1022. INIT_WORK(&cfhsi->wake_down_work, cfhsi_wake_down);
  1023. INIT_WORK(&cfhsi->out_of_sync_work, cfhsi_out_of_sync);
  1024. /* Clear all bit fields. */
  1025. clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  1026. clear_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
  1027. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  1028. clear_bit(CFHSI_AWAKE, &cfhsi->bits);
  1029. /* Create work thread. */
  1030. cfhsi->wq = create_singlethread_workqueue(pdev->name);
  1031. if (!cfhsi->wq) {
  1032. dev_err(&ndev->dev, "%s: Failed to create work queue.\n",
  1033. __func__);
  1034. res = -ENODEV;
  1035. goto err_create_wq;
  1036. }
  1037. /* Initialize wait queues. */
  1038. init_waitqueue_head(&cfhsi->wake_up_wait);
  1039. init_waitqueue_head(&cfhsi->wake_down_wait);
  1040. init_waitqueue_head(&cfhsi->flush_fifo_wait);
  1041. /* Setup the inactivity timer. */
  1042. init_timer(&cfhsi->inactivity_timer);
  1043. cfhsi->inactivity_timer.data = (unsigned long)cfhsi;
  1044. cfhsi->inactivity_timer.function = cfhsi_inactivity_tout;
  1045. /* Setup the slowpath RX timer. */
  1046. init_timer(&cfhsi->rx_slowpath_timer);
  1047. cfhsi->rx_slowpath_timer.data = (unsigned long)cfhsi;
  1048. cfhsi->rx_slowpath_timer.function = cfhsi_rx_slowpath;
  1049. /* Setup the aggregation timer. */
  1050. init_timer(&cfhsi->aggregation_timer);
  1051. cfhsi->aggregation_timer.data = (unsigned long)cfhsi;
  1052. cfhsi->aggregation_timer.function = cfhsi_aggregation_tout;
  1053. /* Add CAIF HSI device to list. */
  1054. spin_lock(&cfhsi_list_lock);
  1055. list_add_tail(&cfhsi->list, &cfhsi_list);
  1056. spin_unlock(&cfhsi_list_lock);
  1057. /* Activate HSI interface. */
  1058. res = cfhsi->dev->cfhsi_up(cfhsi->dev);
  1059. if (res) {
  1060. dev_err(&cfhsi->ndev->dev,
  1061. "%s: can't activate HSI interface: %d.\n",
  1062. __func__, res);
  1063. goto err_activate;
  1064. }
  1065. /* Flush FIFO */
  1066. res = cfhsi_flush_fifo(cfhsi);
  1067. if (res) {
  1068. dev_err(&ndev->dev, "%s: Can't flush FIFO: %d.\n",
  1069. __func__, res);
  1070. goto err_net_reg;
  1071. }
  1072. /* Register network device. */
  1073. res = register_netdev(ndev);
  1074. if (res) {
  1075. dev_err(&ndev->dev, "%s: Registration error: %d.\n",
  1076. __func__, res);
  1077. goto err_net_reg;
  1078. }
  1079. return res;
  1080. err_net_reg:
  1081. cfhsi->dev->cfhsi_down(cfhsi->dev);
  1082. err_activate:
  1083. destroy_workqueue(cfhsi->wq);
  1084. err_create_wq:
  1085. kfree(cfhsi->rx_flip_buf);
  1086. err_alloc_rx_flip:
  1087. kfree(cfhsi->rx_buf);
  1088. err_alloc_rx:
  1089. kfree(cfhsi->tx_buf);
  1090. err_alloc_tx:
  1091. free_netdev(ndev);
  1092. return res;
  1093. }
  1094. static void cfhsi_shutdown(struct cfhsi *cfhsi)
  1095. {
  1096. u8 *tx_buf, *rx_buf, *flip_buf;
  1097. /* going to shutdown driver */
  1098. set_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
  1099. /* Flush workqueue */
  1100. flush_workqueue(cfhsi->wq);
  1101. /* Delete timers if pending */
  1102. del_timer_sync(&cfhsi->inactivity_timer);
  1103. del_timer_sync(&cfhsi->rx_slowpath_timer);
  1104. del_timer_sync(&cfhsi->aggregation_timer);
  1105. /* Cancel pending RX request (if any) */
  1106. cfhsi->dev->cfhsi_rx_cancel(cfhsi->dev);
  1107. /* Destroy workqueue */
  1108. destroy_workqueue(cfhsi->wq);
  1109. /* Store bufferes: will be freed later. */
  1110. tx_buf = cfhsi->tx_buf;
  1111. rx_buf = cfhsi->rx_buf;
  1112. flip_buf = cfhsi->rx_flip_buf;
  1113. /* Flush transmit queues. */
  1114. cfhsi_abort_tx(cfhsi);
  1115. /* Deactivate interface */
  1116. cfhsi->dev->cfhsi_down(cfhsi->dev);
  1117. /* Finally unregister the network device. */
  1118. unregister_netdev(cfhsi->ndev);
  1119. /* Free buffers. */
  1120. kfree(tx_buf);
  1121. kfree(rx_buf);
  1122. kfree(flip_buf);
  1123. }
  1124. int cfhsi_remove(struct platform_device *pdev)
  1125. {
  1126. struct list_head *list_node;
  1127. struct list_head *n;
  1128. struct cfhsi *cfhsi = NULL;
  1129. struct cfhsi_dev *dev;
  1130. dev = (struct cfhsi_dev *)pdev->dev.platform_data;
  1131. spin_lock(&cfhsi_list_lock);
  1132. list_for_each_safe(list_node, n, &cfhsi_list) {
  1133. cfhsi = list_entry(list_node, struct cfhsi, list);
  1134. /* Find the corresponding device. */
  1135. if (cfhsi->dev == dev) {
  1136. /* Remove from list. */
  1137. list_del(list_node);
  1138. spin_unlock(&cfhsi_list_lock);
  1139. /* Shutdown driver. */
  1140. cfhsi_shutdown(cfhsi);
  1141. return 0;
  1142. }
  1143. }
  1144. spin_unlock(&cfhsi_list_lock);
  1145. return -ENODEV;
  1146. }
  1147. struct platform_driver cfhsi_plat_drv = {
  1148. .probe = cfhsi_probe,
  1149. .remove = cfhsi_remove,
  1150. .driver = {
  1151. .name = "cfhsi",
  1152. .owner = THIS_MODULE,
  1153. },
  1154. };
  1155. static void __exit cfhsi_exit_module(void)
  1156. {
  1157. struct list_head *list_node;
  1158. struct list_head *n;
  1159. struct cfhsi *cfhsi = NULL;
  1160. spin_lock(&cfhsi_list_lock);
  1161. list_for_each_safe(list_node, n, &cfhsi_list) {
  1162. cfhsi = list_entry(list_node, struct cfhsi, list);
  1163. /* Remove from list. */
  1164. list_del(list_node);
  1165. spin_unlock(&cfhsi_list_lock);
  1166. /* Shutdown driver. */
  1167. cfhsi_shutdown(cfhsi);
  1168. spin_lock(&cfhsi_list_lock);
  1169. }
  1170. spin_unlock(&cfhsi_list_lock);
  1171. /* Unregister platform driver. */
  1172. platform_driver_unregister(&cfhsi_plat_drv);
  1173. }
  1174. static int __init cfhsi_init_module(void)
  1175. {
  1176. int result;
  1177. /* Initialize spin lock. */
  1178. spin_lock_init(&cfhsi_list_lock);
  1179. /* Register platform driver. */
  1180. result = platform_driver_register(&cfhsi_plat_drv);
  1181. if (result) {
  1182. printk(KERN_ERR "Could not register platform HSI driver: %d.\n",
  1183. result);
  1184. goto err_dev_register;
  1185. }
  1186. return result;
  1187. err_dev_register:
  1188. return result;
  1189. }
  1190. module_init(cfhsi_init_module);
  1191. module_exit(cfhsi_exit_module);