Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *
0004  * Author   Karsten Keil <kkeil@novell.com>
0005  *
0006  * Copyright 2008  by Karsten Keil <kkeil@novell.com>
0007  */
0008 
0009 #include <linux/gfp.h>
0010 #include <linux/module.h>
0011 #include <linux/mISDNhw.h>
0012 
0013 static void
0014 dchannel_bh(struct work_struct *ws)
0015 {
0016     struct dchannel *dch  = container_of(ws, struct dchannel, workq);
0017     struct sk_buff  *skb;
0018     int     err;
0019 
0020     if (test_and_clear_bit(FLG_RECVQUEUE, &dch->Flags)) {
0021         while ((skb = skb_dequeue(&dch->rqueue))) {
0022             if (likely(dch->dev.D.peer)) {
0023                 err = dch->dev.D.recv(dch->dev.D.peer, skb);
0024                 if (err)
0025                     dev_kfree_skb(skb);
0026             } else
0027                 dev_kfree_skb(skb);
0028         }
0029     }
0030     if (test_and_clear_bit(FLG_PHCHANGE, &dch->Flags)) {
0031         if (dch->phfunc)
0032             dch->phfunc(dch);
0033     }
0034 }
0035 
0036 static void
0037 bchannel_bh(struct work_struct *ws)
0038 {
0039     struct bchannel *bch  = container_of(ws, struct bchannel, workq);
0040     struct sk_buff  *skb;
0041     int     err;
0042 
0043     if (test_and_clear_bit(FLG_RECVQUEUE, &bch->Flags)) {
0044         while ((skb = skb_dequeue(&bch->rqueue))) {
0045             bch->rcount--;
0046             if (likely(bch->ch.peer)) {
0047                 err = bch->ch.recv(bch->ch.peer, skb);
0048                 if (err)
0049                     dev_kfree_skb(skb);
0050             } else
0051                 dev_kfree_skb(skb);
0052         }
0053     }
0054 }
0055 
0056 int
0057 mISDN_initdchannel(struct dchannel *ch, int maxlen, void *phf)
0058 {
0059     test_and_set_bit(FLG_HDLC, &ch->Flags);
0060     ch->maxlen = maxlen;
0061     ch->hw = NULL;
0062     ch->rx_skb = NULL;
0063     ch->tx_skb = NULL;
0064     ch->tx_idx = 0;
0065     ch->phfunc = phf;
0066     skb_queue_head_init(&ch->squeue);
0067     skb_queue_head_init(&ch->rqueue);
0068     INIT_LIST_HEAD(&ch->dev.bchannels);
0069     INIT_WORK(&ch->workq, dchannel_bh);
0070     return 0;
0071 }
0072 EXPORT_SYMBOL(mISDN_initdchannel);
0073 
0074 int
0075 mISDN_initbchannel(struct bchannel *ch, unsigned short maxlen,
0076            unsigned short minlen)
0077 {
0078     ch->Flags = 0;
0079     ch->minlen = minlen;
0080     ch->next_minlen = minlen;
0081     ch->init_minlen = minlen;
0082     ch->maxlen = maxlen;
0083     ch->next_maxlen = maxlen;
0084     ch->init_maxlen = maxlen;
0085     ch->hw = NULL;
0086     ch->rx_skb = NULL;
0087     ch->tx_skb = NULL;
0088     ch->tx_idx = 0;
0089     skb_queue_head_init(&ch->rqueue);
0090     ch->rcount = 0;
0091     ch->next_skb = NULL;
0092     INIT_WORK(&ch->workq, bchannel_bh);
0093     return 0;
0094 }
0095 EXPORT_SYMBOL(mISDN_initbchannel);
0096 
0097 int
0098 mISDN_freedchannel(struct dchannel *ch)
0099 {
0100     if (ch->tx_skb) {
0101         dev_kfree_skb(ch->tx_skb);
0102         ch->tx_skb = NULL;
0103     }
0104     if (ch->rx_skb) {
0105         dev_kfree_skb(ch->rx_skb);
0106         ch->rx_skb = NULL;
0107     }
0108     skb_queue_purge(&ch->squeue);
0109     skb_queue_purge(&ch->rqueue);
0110     flush_work(&ch->workq);
0111     return 0;
0112 }
0113 EXPORT_SYMBOL(mISDN_freedchannel);
0114 
0115 void
0116 mISDN_clear_bchannel(struct bchannel *ch)
0117 {
0118     if (ch->tx_skb) {
0119         dev_kfree_skb(ch->tx_skb);
0120         ch->tx_skb = NULL;
0121     }
0122     ch->tx_idx = 0;
0123     if (ch->rx_skb) {
0124         dev_kfree_skb(ch->rx_skb);
0125         ch->rx_skb = NULL;
0126     }
0127     if (ch->next_skb) {
0128         dev_kfree_skb(ch->next_skb);
0129         ch->next_skb = NULL;
0130     }
0131     test_and_clear_bit(FLG_TX_BUSY, &ch->Flags);
0132     test_and_clear_bit(FLG_TX_NEXT, &ch->Flags);
0133     test_and_clear_bit(FLG_ACTIVE, &ch->Flags);
0134     test_and_clear_bit(FLG_FILLEMPTY, &ch->Flags);
0135     test_and_clear_bit(FLG_TX_EMPTY, &ch->Flags);
0136     test_and_clear_bit(FLG_RX_OFF, &ch->Flags);
0137     ch->dropcnt = 0;
0138     ch->minlen = ch->init_minlen;
0139     ch->next_minlen = ch->init_minlen;
0140     ch->maxlen = ch->init_maxlen;
0141     ch->next_maxlen = ch->init_maxlen;
0142     skb_queue_purge(&ch->rqueue);
0143     ch->rcount = 0;
0144 }
0145 EXPORT_SYMBOL(mISDN_clear_bchannel);
0146 
0147 void
0148 mISDN_freebchannel(struct bchannel *ch)
0149 {
0150     cancel_work_sync(&ch->workq);
0151     mISDN_clear_bchannel(ch);
0152 }
0153 EXPORT_SYMBOL(mISDN_freebchannel);
0154 
0155 int
0156 mISDN_ctrl_bchannel(struct bchannel *bch, struct mISDN_ctrl_req *cq)
0157 {
0158     int ret = 0;
0159 
0160     switch (cq->op) {
0161     case MISDN_CTRL_GETOP:
0162         cq->op = MISDN_CTRL_RX_BUFFER | MISDN_CTRL_FILL_EMPTY |
0163              MISDN_CTRL_RX_OFF;
0164         break;
0165     case MISDN_CTRL_FILL_EMPTY:
0166         if (cq->p1) {
0167             memset(bch->fill, cq->p2 & 0xff, MISDN_BCH_FILL_SIZE);
0168             test_and_set_bit(FLG_FILLEMPTY, &bch->Flags);
0169         } else {
0170             test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);
0171         }
0172         break;
0173     case MISDN_CTRL_RX_OFF:
0174         /* read back dropped byte count */
0175         cq->p2 = bch->dropcnt;
0176         if (cq->p1)
0177             test_and_set_bit(FLG_RX_OFF, &bch->Flags);
0178         else
0179             test_and_clear_bit(FLG_RX_OFF, &bch->Flags);
0180         bch->dropcnt = 0;
0181         break;
0182     case MISDN_CTRL_RX_BUFFER:
0183         if (cq->p2 > MISDN_CTRL_RX_SIZE_IGNORE)
0184             bch->next_maxlen = cq->p2;
0185         if (cq->p1 > MISDN_CTRL_RX_SIZE_IGNORE)
0186             bch->next_minlen = cq->p1;
0187         /* we return the old values */
0188         cq->p1 = bch->minlen;
0189         cq->p2 = bch->maxlen;
0190         break;
0191     default:
0192         pr_info("mISDN unhandled control %x operation\n", cq->op);
0193         ret = -EINVAL;
0194         break;
0195     }
0196     return ret;
0197 }
0198 EXPORT_SYMBOL(mISDN_ctrl_bchannel);
0199 
0200 static inline u_int
0201 get_sapi_tei(u_char *p)
0202 {
0203     u_int   sapi, tei;
0204 
0205     sapi = *p >> 2;
0206     tei = p[1] >> 1;
0207     return sapi | (tei << 8);
0208 }
0209 
0210 void
0211 recv_Dchannel(struct dchannel *dch)
0212 {
0213     struct mISDNhead *hh;
0214 
0215     if (dch->rx_skb->len < 2) { /* at least 2 for sapi / tei */
0216         dev_kfree_skb(dch->rx_skb);
0217         dch->rx_skb = NULL;
0218         return;
0219     }
0220     hh = mISDN_HEAD_P(dch->rx_skb);
0221     hh->prim = PH_DATA_IND;
0222     hh->id = get_sapi_tei(dch->rx_skb->data);
0223     skb_queue_tail(&dch->rqueue, dch->rx_skb);
0224     dch->rx_skb = NULL;
0225     schedule_event(dch, FLG_RECVQUEUE);
0226 }
0227 EXPORT_SYMBOL(recv_Dchannel);
0228 
0229 void
0230 recv_Echannel(struct dchannel *ech, struct dchannel *dch)
0231 {
0232     struct mISDNhead *hh;
0233 
0234     if (ech->rx_skb->len < 2) { /* at least 2 for sapi / tei */
0235         dev_kfree_skb(ech->rx_skb);
0236         ech->rx_skb = NULL;
0237         return;
0238     }
0239     hh = mISDN_HEAD_P(ech->rx_skb);
0240     hh->prim = PH_DATA_E_IND;
0241     hh->id = get_sapi_tei(ech->rx_skb->data);
0242     skb_queue_tail(&dch->rqueue, ech->rx_skb);
0243     ech->rx_skb = NULL;
0244     schedule_event(dch, FLG_RECVQUEUE);
0245 }
0246 EXPORT_SYMBOL(recv_Echannel);
0247 
0248 void
0249 recv_Bchannel(struct bchannel *bch, unsigned int id, bool force)
0250 {
0251     struct mISDNhead *hh;
0252 
0253     /* if allocation did fail upper functions still may call us */
0254     if (unlikely(!bch->rx_skb))
0255         return;
0256     if (unlikely(!bch->rx_skb->len)) {
0257         /* we have no data to send - this may happen after recovery
0258          * from overflow or too small allocation.
0259          * We need to free the buffer here */
0260         dev_kfree_skb(bch->rx_skb);
0261         bch->rx_skb = NULL;
0262     } else {
0263         if (test_bit(FLG_TRANSPARENT, &bch->Flags) &&
0264             (bch->rx_skb->len < bch->minlen) && !force)
0265                 return;
0266         hh = mISDN_HEAD_P(bch->rx_skb);
0267         hh->prim = PH_DATA_IND;
0268         hh->id = id;
0269         if (bch->rcount >= 64) {
0270             printk(KERN_WARNING
0271                    "B%d receive queue overflow - flushing!\n",
0272                    bch->nr);
0273             skb_queue_purge(&bch->rqueue);
0274         }
0275         bch->rcount++;
0276         skb_queue_tail(&bch->rqueue, bch->rx_skb);
0277         bch->rx_skb = NULL;
0278         schedule_event(bch, FLG_RECVQUEUE);
0279     }
0280 }
0281 EXPORT_SYMBOL(recv_Bchannel);
0282 
0283 void
0284 recv_Dchannel_skb(struct dchannel *dch, struct sk_buff *skb)
0285 {
0286     skb_queue_tail(&dch->rqueue, skb);
0287     schedule_event(dch, FLG_RECVQUEUE);
0288 }
0289 EXPORT_SYMBOL(recv_Dchannel_skb);
0290 
0291 void
0292 recv_Bchannel_skb(struct bchannel *bch, struct sk_buff *skb)
0293 {
0294     if (bch->rcount >= 64) {
0295         printk(KERN_WARNING "B-channel %p receive queue overflow, "
0296                "flushing!\n", bch);
0297         skb_queue_purge(&bch->rqueue);
0298         bch->rcount = 0;
0299     }
0300     bch->rcount++;
0301     skb_queue_tail(&bch->rqueue, skb);
0302     schedule_event(bch, FLG_RECVQUEUE);
0303 }
0304 EXPORT_SYMBOL(recv_Bchannel_skb);
0305 
0306 static void
0307 confirm_Dsend(struct dchannel *dch)
0308 {
0309     struct sk_buff  *skb;
0310 
0311     skb = _alloc_mISDN_skb(PH_DATA_CNF, mISDN_HEAD_ID(dch->tx_skb),
0312                    0, NULL, GFP_ATOMIC);
0313     if (!skb) {
0314         printk(KERN_ERR "%s: no skb id %x\n", __func__,
0315                mISDN_HEAD_ID(dch->tx_skb));
0316         return;
0317     }
0318     skb_queue_tail(&dch->rqueue, skb);
0319     schedule_event(dch, FLG_RECVQUEUE);
0320 }
0321 
0322 int
0323 get_next_dframe(struct dchannel *dch)
0324 {
0325     dch->tx_idx = 0;
0326     dch->tx_skb = skb_dequeue(&dch->squeue);
0327     if (dch->tx_skb) {
0328         confirm_Dsend(dch);
0329         return 1;
0330     }
0331     dch->tx_skb = NULL;
0332     test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
0333     return 0;
0334 }
0335 EXPORT_SYMBOL(get_next_dframe);
0336 
0337 static void
0338 confirm_Bsend(struct bchannel *bch)
0339 {
0340     struct sk_buff  *skb;
0341 
0342     if (bch->rcount >= 64) {
0343         printk(KERN_WARNING "B-channel %p receive queue overflow, "
0344                "flushing!\n", bch);
0345         skb_queue_purge(&bch->rqueue);
0346         bch->rcount = 0;
0347     }
0348     skb = _alloc_mISDN_skb(PH_DATA_CNF, mISDN_HEAD_ID(bch->tx_skb),
0349                    0, NULL, GFP_ATOMIC);
0350     if (!skb) {
0351         printk(KERN_ERR "%s: no skb id %x\n", __func__,
0352                mISDN_HEAD_ID(bch->tx_skb));
0353         return;
0354     }
0355     bch->rcount++;
0356     skb_queue_tail(&bch->rqueue, skb);
0357     schedule_event(bch, FLG_RECVQUEUE);
0358 }
0359 
0360 int
0361 get_next_bframe(struct bchannel *bch)
0362 {
0363     bch->tx_idx = 0;
0364     if (test_bit(FLG_TX_NEXT, &bch->Flags)) {
0365         bch->tx_skb = bch->next_skb;
0366         if (bch->tx_skb) {
0367             bch->next_skb = NULL;
0368             test_and_clear_bit(FLG_TX_NEXT, &bch->Flags);
0369             /* confirm imediately to allow next data */
0370             confirm_Bsend(bch);
0371             return 1;
0372         } else {
0373             test_and_clear_bit(FLG_TX_NEXT, &bch->Flags);
0374             printk(KERN_WARNING "B TX_NEXT without skb\n");
0375         }
0376     }
0377     bch->tx_skb = NULL;
0378     test_and_clear_bit(FLG_TX_BUSY, &bch->Flags);
0379     return 0;
0380 }
0381 EXPORT_SYMBOL(get_next_bframe);
0382 
0383 void
0384 queue_ch_frame(struct mISDNchannel *ch, u_int pr, int id, struct sk_buff *skb)
0385 {
0386     struct mISDNhead *hh;
0387 
0388     if (!skb) {
0389         _queue_data(ch, pr, id, 0, NULL, GFP_ATOMIC);
0390     } else {
0391         if (ch->peer) {
0392             hh = mISDN_HEAD_P(skb);
0393             hh->prim = pr;
0394             hh->id = id;
0395             if (!ch->recv(ch->peer, skb))
0396                 return;
0397         }
0398         dev_kfree_skb(skb);
0399     }
0400 }
0401 EXPORT_SYMBOL(queue_ch_frame);
0402 
0403 int
0404 dchannel_senddata(struct dchannel *ch, struct sk_buff *skb)
0405 {
0406     /* check oversize */
0407     if (skb->len <= 0) {
0408         printk(KERN_WARNING "%s: skb too small\n", __func__);
0409         return -EINVAL;
0410     }
0411     if (skb->len > ch->maxlen) {
0412         printk(KERN_WARNING "%s: skb too large(%d/%d)\n",
0413                __func__, skb->len, ch->maxlen);
0414         return -EINVAL;
0415     }
0416     /* HW lock must be obtained */
0417     if (test_and_set_bit(FLG_TX_BUSY, &ch->Flags)) {
0418         skb_queue_tail(&ch->squeue, skb);
0419         return 0;
0420     } else {
0421         /* write to fifo */
0422         ch->tx_skb = skb;
0423         ch->tx_idx = 0;
0424         return 1;
0425     }
0426 }
0427 EXPORT_SYMBOL(dchannel_senddata);
0428 
0429 int
0430 bchannel_senddata(struct bchannel *ch, struct sk_buff *skb)
0431 {
0432 
0433     /* check oversize */
0434     if (skb->len <= 0) {
0435         printk(KERN_WARNING "%s: skb too small\n", __func__);
0436         return -EINVAL;
0437     }
0438     if (skb->len > ch->maxlen) {
0439         printk(KERN_WARNING "%s: skb too large(%d/%d)\n",
0440                __func__, skb->len, ch->maxlen);
0441         return -EINVAL;
0442     }
0443     /* HW lock must be obtained */
0444     /* check for pending next_skb */
0445     if (ch->next_skb) {
0446         printk(KERN_WARNING
0447                "%s: next_skb exist ERROR (skb->len=%d next_skb->len=%d)\n",
0448                __func__, skb->len, ch->next_skb->len);
0449         return -EBUSY;
0450     }
0451     if (test_and_set_bit(FLG_TX_BUSY, &ch->Flags)) {
0452         test_and_set_bit(FLG_TX_NEXT, &ch->Flags);
0453         ch->next_skb = skb;
0454         return 0;
0455     } else {
0456         /* write to fifo */
0457         ch->tx_skb = skb;
0458         ch->tx_idx = 0;
0459         confirm_Bsend(ch);
0460         return 1;
0461     }
0462 }
0463 EXPORT_SYMBOL(bchannel_senddata);
0464 
0465 /* The function allocates a new receive skb on demand with a size for the
0466  * requirements of the current protocol. It returns the tailroom of the
0467  * receive skb or an error.
0468  */
0469 int
0470 bchannel_get_rxbuf(struct bchannel *bch, int reqlen)
0471 {
0472     int len;
0473 
0474     if (bch->rx_skb) {
0475         len = skb_tailroom(bch->rx_skb);
0476         if (len < reqlen) {
0477             pr_warn("B%d no space for %d (only %d) bytes\n",
0478                 bch->nr, reqlen, len);
0479             if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
0480                 /* send what we have now and try a new buffer */
0481                 recv_Bchannel(bch, 0, true);
0482             } else {
0483                 /* on HDLC we have to drop too big frames */
0484                 return -EMSGSIZE;
0485             }
0486         } else {
0487             return len;
0488         }
0489     }
0490     /* update current min/max length first */
0491     if (unlikely(bch->maxlen != bch->next_maxlen))
0492         bch->maxlen = bch->next_maxlen;
0493     if (unlikely(bch->minlen != bch->next_minlen))
0494         bch->minlen = bch->next_minlen;
0495     if (unlikely(reqlen > bch->maxlen))
0496         return -EMSGSIZE;
0497     if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
0498         if (reqlen >= bch->minlen) {
0499             len = reqlen;
0500         } else {
0501             len = 2 * bch->minlen;
0502             if (len > bch->maxlen)
0503                 len = bch->maxlen;
0504         }
0505     } else {
0506         /* with HDLC we do not know the length yet */
0507         len = bch->maxlen;
0508     }
0509     bch->rx_skb = mI_alloc_skb(len, GFP_ATOMIC);
0510     if (!bch->rx_skb) {
0511         pr_warn("B%d receive no memory for %d bytes\n", bch->nr, len);
0512         len = -ENOMEM;
0513     }
0514     return len;
0515 }
0516 EXPORT_SYMBOL(bchannel_get_rxbuf);