caif_hsi.c 30 KB

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