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(&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. skb_queue_purge(&ch->rqueue);
  143. ch->rcount = 0;
  144. }
  145. EXPORT_SYMBOL(mISDN_clear_bchannel);
  146. void
  147. mISDN_freebchannel(struct bchannel *ch)
  148. {
  149. cancel_work_sync(&ch->workq);
  150. mISDN_clear_bchannel(ch);
  151. }
  152. EXPORT_SYMBOL(mISDN_freebchannel);
  153. int
  154. mISDN_ctrl_bchannel(struct bchannel *bch, struct mISDN_ctrl_req *cq)
  155. {
  156. int ret = 0;
  157. switch (cq->op) {
  158. case MISDN_CTRL_GETOP:
  159. cq->op = MISDN_CTRL_RX_BUFFER | MISDN_CTRL_FILL_EMPTY |
  160. MISDN_CTRL_RX_OFF;
  161. break;
  162. case MISDN_CTRL_FILL_EMPTY:
  163. if (cq->p1) {
  164. memset(bch->fill, cq->p2 & 0xff, MISDN_BCH_FILL_SIZE);
  165. test_and_set_bit(FLG_FILLEMPTY, &bch->Flags);
  166. } else {
  167. test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);
  168. }
  169. break;
  170. case MISDN_CTRL_RX_OFF:
  171. /* read back dropped byte count */
  172. cq->p2 = bch->dropcnt;
  173. if (cq->p1)
  174. test_and_set_bit(FLG_RX_OFF, &bch->Flags);
  175. else
  176. test_and_clear_bit(FLG_RX_OFF, &bch->Flags);
  177. bch->dropcnt = 0;
  178. break;
  179. case MISDN_CTRL_RX_BUFFER:
  180. if (cq->p2 > MISDN_CTRL_RX_SIZE_IGNORE)
  181. bch->next_maxlen = cq->p2;
  182. if (cq->p1 > MISDN_CTRL_RX_SIZE_IGNORE)
  183. bch->next_minlen = cq->p1;
  184. /* we return the old values */
  185. cq->p1 = bch->minlen;
  186. cq->p2 = bch->maxlen;
  187. break;
  188. default:
  189. pr_info("mISDN unhandled control %x operation\n", cq->op);
  190. ret = -EINVAL;
  191. break;
  192. }
  193. return ret;
  194. }
  195. EXPORT_SYMBOL(mISDN_ctrl_bchannel);
  196. static inline u_int
  197. get_sapi_tei(u_char *p)
  198. {
  199. u_int sapi, tei;
  200. sapi = *p >> 2;
  201. tei = p[1] >> 1;
  202. return sapi | (tei << 8);
  203. }
  204. void
  205. recv_Dchannel(struct dchannel *dch)
  206. {
  207. struct mISDNhead *hh;
  208. if (dch->rx_skb->len < 2) { /* at least 2 for sapi / tei */
  209. dev_kfree_skb(dch->rx_skb);
  210. dch->rx_skb = NULL;
  211. return;
  212. }
  213. hh = mISDN_HEAD_P(dch->rx_skb);
  214. hh->prim = PH_DATA_IND;
  215. hh->id = get_sapi_tei(dch->rx_skb->data);
  216. skb_queue_tail(&dch->rqueue, dch->rx_skb);
  217. dch->rx_skb = NULL;
  218. schedule_event(dch, FLG_RECVQUEUE);
  219. }
  220. EXPORT_SYMBOL(recv_Dchannel);
  221. void
  222. recv_Echannel(struct dchannel *ech, struct dchannel *dch)
  223. {
  224. struct mISDNhead *hh;
  225. if (ech->rx_skb->len < 2) { /* at least 2 for sapi / tei */
  226. dev_kfree_skb(ech->rx_skb);
  227. ech->rx_skb = NULL;
  228. return;
  229. }
  230. hh = mISDN_HEAD_P(ech->rx_skb);
  231. hh->prim = PH_DATA_E_IND;
  232. hh->id = get_sapi_tei(ech->rx_skb->data);
  233. skb_queue_tail(&dch->rqueue, ech->rx_skb);
  234. ech->rx_skb = NULL;
  235. schedule_event(dch, FLG_RECVQUEUE);
  236. }
  237. EXPORT_SYMBOL(recv_Echannel);
  238. void
  239. recv_Bchannel(struct bchannel *bch, unsigned int id, bool force)
  240. {
  241. struct mISDNhead *hh;
  242. /* if allocation did fail upper functions still may call us */
  243. if (unlikely(!bch->rx_skb))
  244. return;
  245. if (unlikely(!bch->rx_skb->len)) {
  246. /* we have no data to send - this may happen after recovery
  247. * from overflow or too small allocation.
  248. * We need to free the buffer here */
  249. dev_kfree_skb(bch->rx_skb);
  250. bch->rx_skb = NULL;
  251. } else {
  252. if (test_bit(FLG_TRANSPARENT, &bch->Flags) &&
  253. (bch->rx_skb->len < bch->minlen) && !force)
  254. return;
  255. hh = mISDN_HEAD_P(bch->rx_skb);
  256. hh->prim = PH_DATA_IND;
  257. hh->id = id;
  258. if (bch->rcount >= 64) {
  259. printk(KERN_WARNING
  260. "B%d receive queue overflow - flushing!\n",
  261. bch->nr);
  262. skb_queue_purge(&bch->rqueue);
  263. }
  264. bch->rcount++;
  265. skb_queue_tail(&bch->rqueue, bch->rx_skb);
  266. bch->rx_skb = NULL;
  267. schedule_event(bch, FLG_RECVQUEUE);
  268. }
  269. }
  270. EXPORT_SYMBOL(recv_Bchannel);
  271. void
  272. recv_Dchannel_skb(struct dchannel *dch, struct sk_buff *skb)
  273. {
  274. skb_queue_tail(&dch->rqueue, skb);
  275. schedule_event(dch, FLG_RECVQUEUE);
  276. }
  277. EXPORT_SYMBOL(recv_Dchannel_skb);
  278. void
  279. recv_Bchannel_skb(struct bchannel *bch, struct sk_buff *skb)
  280. {
  281. if (bch->rcount >= 64) {
  282. printk(KERN_WARNING "B-channel %p receive queue overflow, "
  283. "flushing!\n", bch);
  284. skb_queue_purge(&bch->rqueue);
  285. bch->rcount = 0;
  286. }
  287. bch->rcount++;
  288. skb_queue_tail(&bch->rqueue, skb);
  289. schedule_event(bch, FLG_RECVQUEUE);
  290. }
  291. EXPORT_SYMBOL(recv_Bchannel_skb);
  292. static void
  293. confirm_Dsend(struct dchannel *dch)
  294. {
  295. struct sk_buff *skb;
  296. skb = _alloc_mISDN_skb(PH_DATA_CNF, mISDN_HEAD_ID(dch->tx_skb),
  297. 0, NULL, GFP_ATOMIC);
  298. if (!skb) {
  299. printk(KERN_ERR "%s: no skb id %x\n", __func__,
  300. mISDN_HEAD_ID(dch->tx_skb));
  301. return;
  302. }
  303. skb_queue_tail(&dch->rqueue, skb);
  304. schedule_event(dch, FLG_RECVQUEUE);
  305. }
  306. int
  307. get_next_dframe(struct dchannel *dch)
  308. {
  309. dch->tx_idx = 0;
  310. dch->tx_skb = skb_dequeue(&dch->squeue);
  311. if (dch->tx_skb) {
  312. confirm_Dsend(dch);
  313. return 1;
  314. }
  315. dch->tx_skb = NULL;
  316. test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
  317. return 0;
  318. }
  319. EXPORT_SYMBOL(get_next_dframe);
  320. static void
  321. confirm_Bsend(struct bchannel *bch)
  322. {
  323. struct sk_buff *skb;
  324. if (bch->rcount >= 64) {
  325. printk(KERN_WARNING "B-channel %p receive queue overflow, "
  326. "flushing!\n", bch);
  327. skb_queue_purge(&bch->rqueue);
  328. bch->rcount = 0;
  329. }
  330. skb = _alloc_mISDN_skb(PH_DATA_CNF, mISDN_HEAD_ID(bch->tx_skb),
  331. 0, NULL, GFP_ATOMIC);
  332. if (!skb) {
  333. printk(KERN_ERR "%s: no skb id %x\n", __func__,
  334. mISDN_HEAD_ID(bch->tx_skb));
  335. return;
  336. }
  337. bch->rcount++;
  338. skb_queue_tail(&bch->rqueue, skb);
  339. schedule_event(bch, FLG_RECVQUEUE);
  340. }
  341. int
  342. get_next_bframe(struct bchannel *bch)
  343. {
  344. bch->tx_idx = 0;
  345. if (test_bit(FLG_TX_NEXT, &bch->Flags)) {
  346. bch->tx_skb = bch->next_skb;
  347. if (bch->tx_skb) {
  348. bch->next_skb = NULL;
  349. test_and_clear_bit(FLG_TX_NEXT, &bch->Flags);
  350. /* confirm imediately to allow next data */
  351. confirm_Bsend(bch);
  352. return 1;
  353. } else {
  354. test_and_clear_bit(FLG_TX_NEXT, &bch->Flags);
  355. printk(KERN_WARNING "B TX_NEXT without skb\n");
  356. }
  357. }
  358. bch->tx_skb = NULL;
  359. test_and_clear_bit(FLG_TX_BUSY, &bch->Flags);
  360. return 0;
  361. }
  362. EXPORT_SYMBOL(get_next_bframe);
  363. void
  364. queue_ch_frame(struct mISDNchannel *ch, u_int pr, int id, struct sk_buff *skb)
  365. {
  366. struct mISDNhead *hh;
  367. if (!skb) {
  368. _queue_data(ch, pr, id, 0, NULL, GFP_ATOMIC);
  369. } else {
  370. if (ch->peer) {
  371. hh = mISDN_HEAD_P(skb);
  372. hh->prim = pr;
  373. hh->id = id;
  374. if (!ch->recv(ch->peer, skb))
  375. return;
  376. }
  377. dev_kfree_skb(skb);
  378. }
  379. }
  380. EXPORT_SYMBOL(queue_ch_frame);
  381. int
  382. dchannel_senddata(struct dchannel *ch, struct sk_buff *skb)
  383. {
  384. /* check oversize */
  385. if (skb->len <= 0) {
  386. printk(KERN_WARNING "%s: skb too small\n", __func__);
  387. return -EINVAL;
  388. }
  389. if (skb->len > ch->maxlen) {
  390. printk(KERN_WARNING "%s: skb too large(%d/%d)\n",
  391. __func__, skb->len, ch->maxlen);
  392. return -EINVAL;
  393. }
  394. /* HW lock must be obtained */
  395. if (test_and_set_bit(FLG_TX_BUSY, &ch->Flags)) {
  396. skb_queue_tail(&ch->squeue, skb);
  397. return 0;
  398. } else {
  399. /* write to fifo */
  400. ch->tx_skb = skb;
  401. ch->tx_idx = 0;
  402. return 1;
  403. }
  404. }
  405. EXPORT_SYMBOL(dchannel_senddata);
  406. int
  407. bchannel_senddata(struct bchannel *ch, struct sk_buff *skb)
  408. {
  409. /* check oversize */
  410. if (skb->len <= 0) {
  411. printk(KERN_WARNING "%s: skb too small\n", __func__);
  412. return -EINVAL;
  413. }
  414. if (skb->len > ch->maxlen) {
  415. printk(KERN_WARNING "%s: skb too large(%d/%d)\n",
  416. __func__, skb->len, ch->maxlen);
  417. return -EINVAL;
  418. }
  419. /* HW lock must be obtained */
  420. /* check for pending next_skb */
  421. if (ch->next_skb) {
  422. printk(KERN_WARNING
  423. "%s: next_skb exist ERROR (skb->len=%d next_skb->len=%d)\n",
  424. __func__, skb->len, ch->next_skb->len);
  425. return -EBUSY;
  426. }
  427. if (test_and_set_bit(FLG_TX_BUSY, &ch->Flags)) {
  428. test_and_set_bit(FLG_TX_NEXT, &ch->Flags);
  429. ch->next_skb = skb;
  430. return 0;
  431. } else {
  432. /* write to fifo */
  433. ch->tx_skb = skb;
  434. ch->tx_idx = 0;
  435. confirm_Bsend(ch);
  436. return 1;
  437. }
  438. }
  439. EXPORT_SYMBOL(bchannel_senddata);
  440. /* The function allocates a new receive skb on demand with a size for the
  441. * requirements of the current protocol. It returns the tailroom of the
  442. * receive skb or an error.
  443. */
  444. int
  445. bchannel_get_rxbuf(struct bchannel *bch, int reqlen)
  446. {
  447. int len;
  448. if (bch->rx_skb) {
  449. len = skb_tailroom(bch->rx_skb);
  450. if (len < reqlen) {
  451. pr_warning("B%d no space for %d (only %d) bytes\n",
  452. bch->nr, reqlen, len);
  453. if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
  454. /* send what we have now and try a new buffer */
  455. recv_Bchannel(bch, 0, true);
  456. } else {
  457. /* on HDLC we have to drop too big frames */
  458. return -EMSGSIZE;
  459. }
  460. } else {
  461. return len;
  462. }
  463. }
  464. /* update current min/max length first */
  465. if (unlikely(bch->maxlen != bch->next_maxlen))
  466. bch->maxlen = bch->next_maxlen;
  467. if (unlikely(bch->minlen != bch->next_minlen))
  468. bch->minlen = bch->next_minlen;
  469. if (unlikely(reqlen > bch->maxlen))
  470. return -EMSGSIZE;
  471. if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
  472. if (reqlen >= bch->minlen) {
  473. len = reqlen;
  474. } else {
  475. len = 2 * bch->minlen;
  476. if (len > bch->maxlen)
  477. len = bch->maxlen;
  478. }
  479. } else {
  480. /* with HDLC we do not know the length yet */
  481. len = bch->maxlen;
  482. }
  483. bch->rx_skb = mI_alloc_skb(len, GFP_ATOMIC);
  484. if (!bch->rx_skb) {
  485. pr_warning("B%d receive no memory for %d bytes\n",
  486. bch->nr, len);
  487. len = -ENOMEM;
  488. }
  489. return len;
  490. }
  491. EXPORT_SYMBOL(bchannel_get_rxbuf);