caif_hsi.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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);