hwchannel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. *
  3. * Author Karsten Keil <kkeil@novell.com>
  4. *
  5. * Copyright 2008 by Karsten Keil <kkeil@novell.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/gfp.h>
  18. #include <linux/module.h>
  19. #include <linux/mISDNhw.h>
  20. static void
  21. dchannel_bh(struct work_struct *ws)
  22. {
  23. struct dchannel *dch = container_of(ws, struct dchannel, workq);
  24. struct sk_buff *skb;
  25. int err;
  26. if (test_and_clear_bit(FLG_RECVQUEUE, &dch->Flags)) {
  27. while ((skb = skb_dequeue(&dch->rqueue))) {
  28. if (likely(dch->dev.D.peer)) {
  29. err = dch->dev.D.recv(dch->dev.D.peer, skb);
  30. if (err)
  31. dev_kfree_skb(skb);
  32. } else
  33. dev_kfree_skb(skb);
  34. }
  35. }
  36. if (test_and_clear_bit(FLG_PHCHANGE, &dch->Flags)) {
  37. if (dch->phfunc)
  38. dch->phfunc(dch);
  39. }
  40. }
  41. static void
  42. bchannel_bh(struct work_struct *ws)
  43. {
  44. struct bchannel *bch = container_of(ws, struct bchannel, workq);
  45. struct sk_buff *skb;
  46. int err;
  47. if (test_and_clear_bit(FLG_RECVQUEUE, &bch->Flags)) {
  48. while ((skb = skb_dequeue(&bch->rqueue))) {
  49. bch->rcount--;
  50. if (likely(bch->ch.peer)) {
  51. err = bch->ch.recv(bch->ch.peer, skb);
  52. if (err)
  53. dev_kfree_skb(skb);
  54. } else
  55. dev_kfree_skb(skb);
  56. }
  57. }
  58. }
  59. int
  60. mISDN_initdchannel(struct dchannel *ch, int maxlen, void *phf)
  61. {
  62. test_and_set_bit(FLG_HDLC, &ch->Flags);
  63. ch->maxlen = maxlen;
  64. ch->hw = NULL;
  65. ch->rx_skb = NULL;
  66. ch->tx_skb = NULL;
  67. ch->tx_idx = 0;
  68. ch->phfunc = phf;
  69. skb_queue_head_init(&ch->squeue);
  70. skb_queue_head_init(&ch->rqueue);
  71. INIT_LIST_HEAD(&ch->dev.bchannels);
  72. INIT_WORK(&ch->workq, dchannel_bh);
  73. return 0;
  74. }
  75. EXPORT_SYMBOL(mISDN_initdchannel);
  76. int
  77. mISDN_initbchannel(struct bchannel *ch, unsigned short maxlen,
  78. unsigned short minlen)
  79. {
  80. ch->Flags = 0;
  81. ch->minlen = minlen;
  82. ch->next_minlen = minlen;
  83. ch->init_minlen = minlen;
  84. ch->maxlen = maxlen;
  85. ch->next_maxlen = maxlen;
  86. ch->init_maxlen = maxlen;
  87. ch->hw = NULL;
  88. ch->rx_skb = NULL;
  89. ch->tx_skb = NULL;
  90. ch->tx_idx = 0;
  91. skb_queue_head_init(&ch->rqueue);
  92. ch->rcount = 0;
  93. ch->next_skb = NULL;
  94. INIT_WORK(&ch->workq, bchannel_bh);
  95. return 0;
  96. }
  97. EXPORT_SYMBOL(mISDN_initbchannel);
  98. int
  99. mISDN_freedchannel(struct dchannel *ch)
  100. {
  101. if (ch->tx_skb) {
  102. dev_kfree_skb(ch->tx_skb);
  103. ch->tx_skb = NULL;
  104. }
  105. if (ch->rx_skb) {
  106. dev_kfree_skb(ch->rx_skb);
  107. ch->rx_skb = NULL;
  108. }
  109. skb_queue_purge(&ch->squeue);
  110. skb_queue_purge(&ch->rqueue);
  111. flush_work_sync(&ch->workq);
  112. return 0;
  113. }
  114. EXPORT_SYMBOL(mISDN_freedchannel);
  115. void
  116. mISDN_clear_bchannel(struct bchannel *ch)
  117. {
  118. if (ch->tx_skb) {
  119. dev_kfree_skb(ch->tx_skb);
  120. ch->tx_skb = NULL;
  121. }
  122. ch->tx_idx = 0;
  123. if (ch->rx_skb) {
  124. dev_kfree_skb(ch->rx_skb);
  125. ch->rx_skb = NULL;
  126. }
  127. if (ch->next_skb) {
  128. dev_kfree_skb(ch->next_skb);
  129. ch->next_skb = NULL;
  130. }
  131. test_and_clear_bit(FLG_TX_BUSY, &ch->Flags);
  132. test_and_clear_bit(FLG_TX_NEXT, &ch->Flags);
  133. test_and_clear_bit(FLG_ACTIVE, &ch->Flags);
  134. test_and_clear_bit(FLG_FILLEMPTY, &ch->Flags);
  135. test_and_clear_bit(FLG_TX_EMPTY, &ch->Flags);
  136. test_and_clear_bit(FLG_RX_OFF, &ch->Flags);
  137. ch->dropcnt = 0;
  138. ch->minlen = ch->init_minlen;
  139. ch->next_minlen = ch->init_minlen;
  140. ch->maxlen = ch->init_maxlen;
  141. ch->next_maxlen = ch->init_maxlen;
  142. }
  143. EXPORT_SYMBOL(mISDN_clear_bchannel);
  144. int
  145. mISDN_freebchannel(struct bchannel *ch)
  146. {
  147. mISDN_clear_bchannel(ch);
  148. skb_queue_purge(&ch->rqueue);
  149. ch->rcount = 0;
  150. flush_work_sync(&ch->workq);
  151. return 0;
  152. }
  153. EXPORT_SYMBOL(mISDN_freebchannel);
  154. int
  155. mISDN_ctrl_bchannel(struct bchannel *bch, struct mISDN_ctrl_req *cq)
  156. {
  157. int ret = 0;
  158. switch (cq->op) {
  159. case MISDN_CTRL_GETOP:
  160. cq->op = MISDN_CTRL_RX_BUFFER | MISDN_CTRL_FILL_EMPTY |
  161. MISDN_CTRL_RX_OFF;
  162. break;
  163. case MISDN_CTRL_FILL_EMPTY:
  164. if (cq->p1) {
  165. memset(bch->fill, cq->p2 & 0xff, MISDN_BCH_FILL_SIZE);
  166. test_and_set_bit(FLG_FILLEMPTY, &bch->Flags);
  167. } else {
  168. test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);
  169. }
  170. break;
  171. case MISDN_CTRL_RX_OFF:
  172. /* read back dropped byte count */
  173. cq->p2 = bch->dropcnt;
  174. if (cq->p1)
  175. test_and_set_bit(FLG_RX_OFF, &bch->Flags);
  176. else
  177. test_and_clear_bit(FLG_RX_OFF, &bch->Flags);
  178. bch->dropcnt = 0;
  179. break;
  180. case MISDN_CTRL_RX_BUFFER:
  181. if (cq->p2 > MISDN_CTRL_RX_SIZE_IGNORE)
  182. bch->next_maxlen = cq->p2;
  183. if (cq->p1 > MISDN_CTRL_RX_SIZE_IGNORE)
  184. bch->next_minlen = cq->p1;
  185. /* we return the old values */
  186. cq->p1 = bch->minlen;
  187. cq->p2 = bch->maxlen;
  188. break;
  189. default:
  190. pr_info("mISDN unhandled control %x operation\n", cq->op);
  191. ret = -EINVAL;
  192. break;
  193. }
  194. return ret;
  195. }
  196. EXPORT_SYMBOL(mISDN_ctrl_bchannel);
  197. static inline u_int
  198. get_sapi_tei(u_char *p)
  199. {
  200. u_int sapi, tei;
  201. sapi = *p >> 2;
  202. tei = p[1] >> 1;
  203. return sapi | (tei << 8);
  204. }
  205. void
  206. recv_Dchannel(struct dchannel *dch)
  207. {
  208. struct mISDNhead *hh;
  209. if (dch->rx_skb->len < 2) { /* at least 2 for sapi / tei */
  210. dev_kfree_skb(dch->rx_skb);
  211. dch->rx_skb = NULL;
  212. return;
  213. }
  214. hh = mISDN_HEAD_P(dch->rx_skb);
  215. hh->prim = PH_DATA_IND;
  216. hh->id = get_sapi_tei(dch->rx_skb->data);
  217. skb_queue_tail(&dch->rqueue, dch->rx_skb);
  218. dch->rx_skb = NULL;
  219. schedule_event(dch, FLG_RECVQUEUE);
  220. }
  221. EXPORT_SYMBOL(recv_Dchannel);
  222. void
  223. recv_Echannel(struct dchannel *ech, struct dchannel *dch)
  224. {
  225. struct mISDNhead *hh;
  226. if (ech->rx_skb->len < 2) { /* at least 2 for sapi / tei */
  227. dev_kfree_skb(ech->rx_skb);
  228. ech->rx_skb = NULL;
  229. return;
  230. }
  231. hh = mISDN_HEAD_P(ech->rx_skb);
  232. hh->prim = PH_DATA_E_IND;
  233. hh->id = get_sapi_tei(ech->rx_skb->data);
  234. skb_queue_tail(&dch->rqueue, ech->rx_skb);
  235. ech->rx_skb = NULL;
  236. schedule_event(dch, FLG_RECVQUEUE);
  237. }
  238. EXPORT_SYMBOL(recv_Echannel);
  239. void
  240. recv_Bchannel(struct bchannel *bch, unsigned int id, bool force)
  241. {
  242. struct mISDNhead *hh;
  243. /* if allocation did fail upper functions still may call us */
  244. if (unlikely(!bch->rx_skb))
  245. return;
  246. if (unlikely(!bch->rx_skb->len)) {
  247. /* we have no data to send - this may happen after recovery
  248. * from overflow or too small allocation.
  249. * We need to free the buffer here */
  250. dev_kfree_skb(bch->rx_skb);
  251. bch->rx_skb = NULL;
  252. } else {
  253. if (test_bit(FLG_TRANSPARENT, &bch->Flags) &&
  254. (bch->rx_skb->len < bch->minlen) && !force)
  255. return;
  256. hh = mISDN_HEAD_P(bch->rx_skb);
  257. hh->prim = PH_DATA_IND;
  258. hh->id = id;
  259. if (bch->rcount >= 64) {
  260. printk(KERN_WARNING
  261. "B%d receive queue overflow - flushing!\n",
  262. bch->nr);
  263. skb_queue_purge(&bch->rqueue);
  264. }
  265. bch->rcount++;
  266. skb_queue_tail(&bch->rqueue, bch->rx_skb);
  267. bch->rx_skb = NULL;
  268. schedule_event(bch, FLG_RECVQUEUE);
  269. }
  270. }
  271. EXPORT_SYMBOL(recv_Bchannel);
  272. void
  273. recv_Dchannel_skb(struct dchannel *dch, struct sk_buff *skb)
  274. {
  275. skb_queue_tail(&dch->rqueue, skb);
  276. schedule_event(dch, FLG_RECVQUEUE);
  277. }
  278. EXPORT_SYMBOL(recv_Dchannel_skb);
  279. void
  280. recv_Bchannel_skb(struct bchannel *bch, struct sk_buff *skb)
  281. {
  282. if (bch->rcount >= 64) {
  283. printk(KERN_WARNING "B-channel %p receive queue overflow, "
  284. "flushing!\n", bch);
  285. skb_queue_purge(&bch->rqueue);
  286. bch->rcount = 0;
  287. }
  288. bch->rcount++;
  289. skb_queue_tail(&bch->rqueue, skb);
  290. schedule_event(bch, FLG_RECVQUEUE);
  291. }
  292. EXPORT_SYMBOL(recv_Bchannel_skb);
  293. static void
  294. confirm_Dsend(struct dchannel *dch)
  295. {
  296. struct sk_buff *skb;
  297. skb = _alloc_mISDN_skb(PH_DATA_CNF, mISDN_HEAD_ID(dch->tx_skb),
  298. 0, NULL, GFP_ATOMIC);
  299. if (!skb) {
  300. printk(KERN_ERR "%s: no skb id %x\n", __func__,
  301. mISDN_HEAD_ID(dch->tx_skb));
  302. return;
  303. }
  304. skb_queue_tail(&dch->rqueue, skb);
  305. schedule_event(dch, FLG_RECVQUEUE);
  306. }
  307. int
  308. get_next_dframe(struct dchannel *dch)
  309. {
  310. dch->tx_idx = 0;
  311. dch->tx_skb = skb_dequeue(&dch->squeue);
  312. if (dch->tx_skb) {
  313. confirm_Dsend(dch);
  314. return 1;
  315. }
  316. dch->tx_skb = NULL;
  317. test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
  318. return 0;
  319. }
  320. EXPORT_SYMBOL(get_next_dframe);
  321. static void
  322. confirm_Bsend(struct bchannel *bch)
  323. {
  324. struct sk_buff *skb;
  325. if (bch->rcount >= 64) {
  326. printk(KERN_WARNING "B-channel %p receive queue overflow, "
  327. "flushing!\n", bch);
  328. skb_queue_purge(&bch->rqueue);
  329. bch->rcount = 0;
  330. }
  331. skb = _alloc_mISDN_skb(PH_DATA_CNF, mISDN_HEAD_ID(bch->tx_skb),
  332. 0, NULL, GFP_ATOMIC);
  333. if (!skb) {
  334. printk(KERN_ERR "%s: no skb id %x\n", __func__,
  335. mISDN_HEAD_ID(bch->tx_skb));
  336. return;
  337. }
  338. bch->rcount++;
  339. skb_queue_tail(&bch->rqueue, skb);
  340. schedule_event(bch, FLG_RECVQUEUE);
  341. }
  342. int
  343. get_next_bframe(struct bchannel *bch)
  344. {
  345. bch->tx_idx = 0;
  346. if (test_bit(FLG_TX_NEXT, &bch->Flags)) {
  347. bch->tx_skb = bch->next_skb;
  348. if (bch->tx_skb) {
  349. bch->next_skb = NULL;
  350. test_and_clear_bit(FLG_TX_NEXT, &bch->Flags);
  351. /* confirm imediately to allow next data */
  352. confirm_Bsend(bch);
  353. return 1;
  354. } else {
  355. test_and_clear_bit(FLG_TX_NEXT, &bch->Flags);
  356. printk(KERN_WARNING "B TX_NEXT without skb\n");
  357. }
  358. }
  359. bch->tx_skb = NULL;
  360. test_and_clear_bit(FLG_TX_BUSY, &bch->Flags);
  361. return 0;
  362. }
  363. EXPORT_SYMBOL(get_next_bframe);
  364. void
  365. queue_ch_frame(struct mISDNchannel *ch, u_int pr, int id, struct sk_buff *skb)
  366. {
  367. struct mISDNhead *hh;
  368. if (!skb) {
  369. _queue_data(ch, pr, id, 0, NULL, GFP_ATOMIC);
  370. } else {
  371. if (ch->peer) {
  372. hh = mISDN_HEAD_P(skb);
  373. hh->prim = pr;
  374. hh->id = id;
  375. if (!ch->recv(ch->peer, skb))
  376. return;
  377. }
  378. dev_kfree_skb(skb);
  379. }
  380. }
  381. EXPORT_SYMBOL(queue_ch_frame);
  382. int
  383. dchannel_senddata(struct dchannel *ch, struct sk_buff *skb)
  384. {
  385. /* check oversize */
  386. if (skb->len <= 0) {
  387. printk(KERN_WARNING "%s: skb too small\n", __func__);
  388. return -EINVAL;
  389. }
  390. if (skb->len > ch->maxlen) {
  391. printk(KERN_WARNING "%s: skb too large(%d/%d)\n",
  392. __func__, skb->len, ch->maxlen);
  393. return -EINVAL;
  394. }
  395. /* HW lock must be obtained */
  396. if (test_and_set_bit(FLG_TX_BUSY, &ch->Flags)) {
  397. skb_queue_tail(&ch->squeue, skb);
  398. return 0;
  399. } else {
  400. /* write to fifo */
  401. ch->tx_skb = skb;
  402. ch->tx_idx = 0;
  403. return 1;
  404. }
  405. }
  406. EXPORT_SYMBOL(dchannel_senddata);
  407. int
  408. bchannel_senddata(struct bchannel *ch, struct sk_buff *skb)
  409. {
  410. /* check oversize */
  411. if (skb->len <= 0) {
  412. printk(KERN_WARNING "%s: skb too small\n", __func__);
  413. return -EINVAL;
  414. }
  415. if (skb->len > ch->maxlen) {
  416. printk(KERN_WARNING "%s: skb too large(%d/%d)\n",
  417. __func__, skb->len, ch->maxlen);
  418. return -EINVAL;
  419. }
  420. /* HW lock must be obtained */
  421. /* check for pending next_skb */
  422. if (ch->next_skb) {
  423. printk(KERN_WARNING
  424. "%s: next_skb exist ERROR (skb->len=%d next_skb->len=%d)\n",
  425. __func__, skb->len, ch->next_skb->len);
  426. return -EBUSY;
  427. }
  428. if (test_and_set_bit(FLG_TX_BUSY, &ch->Flags)) {
  429. test_and_set_bit(FLG_TX_NEXT, &ch->Flags);
  430. ch->next_skb = skb;
  431. return 0;
  432. } else {
  433. /* write to fifo */
  434. ch->tx_skb = skb;
  435. ch->tx_idx = 0;
  436. confirm_Bsend(ch);
  437. return 1;
  438. }
  439. }
  440. EXPORT_SYMBOL(bchannel_senddata);
  441. /* The function allocates a new receive skb on demand with a size for the
  442. * requirements of the current protocol. It returns the tailroom of the
  443. * receive skb or an error.
  444. */
  445. int
  446. bchannel_get_rxbuf(struct bchannel *bch, int reqlen)
  447. {
  448. int len;
  449. if (bch->rx_skb) {
  450. len = skb_tailroom(bch->rx_skb);
  451. if (len < reqlen) {
  452. pr_warning("B%d no space for %d (only %d) bytes\n",
  453. bch->nr, reqlen, len);
  454. if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
  455. /* send what we have now and try a new buffer */
  456. recv_Bchannel(bch, 0, true);
  457. } else {
  458. /* on HDLC we have to drop too big frames */
  459. return -EMSGSIZE;
  460. }
  461. } else {
  462. return len;
  463. }
  464. }
  465. /* update current min/max length first */
  466. if (unlikely(bch->maxlen != bch->next_maxlen))
  467. bch->maxlen = bch->next_maxlen;
  468. if (unlikely(bch->minlen != bch->next_minlen))
  469. bch->minlen = bch->next_minlen;
  470. if (unlikely(reqlen > bch->maxlen))
  471. return -EMSGSIZE;
  472. if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
  473. if (reqlen >= bch->minlen) {
  474. len = reqlen;
  475. } else {
  476. len = 2 * bch->minlen;
  477. if (len > bch->maxlen)
  478. len = bch->maxlen;
  479. }
  480. } else {
  481. /* with HDLC we do not know the length yet */
  482. len = bch->maxlen;
  483. }
  484. bch->rx_skb = mI_alloc_skb(len, GFP_ATOMIC);
  485. if (!bch->rx_skb) {
  486. pr_warning("B%d receive no memory for %d bytes\n",
  487. bch->nr, len);
  488. len = -ENOMEM;
  489. }
  490. return len;
  491. }
  492. EXPORT_SYMBOL(bchannel_get_rxbuf);