caif_hsi.c 36 KB

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