caif_hsi.c 31 KB

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