caif_hsi.c 35 KB

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