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.
9 #define pr_fmt(fmt) KBUILD_MODNAME fmt
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/device.h>
14 #include <linux/netdevice.h>
15 #include <linux/string.h>
16 #include <linux/list.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/sched.h>
20 #include <linux/if_arp.h>
21 #include <linux/timer.h>
22 #include <net/rtnetlink.h>
23 #include <linux/pkt_sched.h>
24 #include <net/caif/caif_layer.h>
25 #include <net/caif/caif_hsi.h>
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Daniel Martensson<daniel.martensson@stericsson.com>");
29 MODULE_DESCRIPTION("CAIF HSI driver");
31 /* Returns the number of padding bytes for alignment. */
32 #define PAD_POW2(x, pow) ((((x)&((pow)-1)) == 0) ? 0 :\
33 (((pow)-((x)&((pow)-1)))))
35 static int inactivity_timeout = 1000;
36 module_param(inactivity_timeout, int, S_IRUGO | S_IWUSR);
37 MODULE_PARM_DESC(inactivity_timeout, "Inactivity timeout on HSI, ms.");
39 static int aggregation_timeout = 1;
40 module_param(aggregation_timeout, int, S_IRUGO | S_IWUSR);
41 MODULE_PARM_DESC(aggregation_timeout, "Aggregation timeout on HSI, ms.");
44 * HSI padding options.
45 * Warning: must be a base of 2 (& operation used) and can not be zero !
47 static int hsi_head_align = 4;
48 module_param(hsi_head_align, int, S_IRUGO);
49 MODULE_PARM_DESC(hsi_head_align, "HSI head alignment.");
51 static int hsi_tail_align = 4;
52 module_param(hsi_tail_align, int, S_IRUGO);
53 MODULE_PARM_DESC(hsi_tail_align, "HSI tail alignment.");
56 * HSI link layer flowcontrol thresholds.
57 * Warning: A high threshold value migth increase throughput but it will at
58 * the same time prevent channel prioritization and increase the risk of
59 * flooding the modem. The high threshold should be above the low.
61 static int hsi_high_threshold = 100;
62 module_param(hsi_high_threshold, int, S_IRUGO);
63 MODULE_PARM_DESC(hsi_high_threshold, "HSI high threshold (FLOW OFF).");
65 static int hsi_low_threshold = 50;
66 module_param(hsi_low_threshold, int, S_IRUGO);
67 MODULE_PARM_DESC(hsi_low_threshold, "HSI high threshold (FLOW ON).");
73 * Threshold values for the HSI packet queue. Flowcontrol will be asserted
74 * when the number of packets exceeds HIGH_WATER_MARK. It will not be
75 * de-asserted before the number of packets drops below LOW_WATER_MARK.
77 #define LOW_WATER_MARK hsi_low_threshold
78 #define HIGH_WATER_MARK hsi_high_threshold
80 static LIST_HEAD(cfhsi_list);
82 static void cfhsi_inactivity_tout(unsigned long arg)
84 struct cfhsi *cfhsi = (struct cfhsi *)arg;
86 netdev_dbg(cfhsi->ndev, "%s.\n",
89 /* Schedule power down work queue. */
90 if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
91 queue_work(cfhsi->wq, &cfhsi->wake_down_work);
94 static void cfhsi_update_aggregation_stats(struct cfhsi *cfhsi,
95 const struct sk_buff *skb,
98 struct caif_payload_info *info;
101 info = (struct caif_payload_info *)&skb->cb;
102 hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
103 tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);
104 len = skb->len + hpad + tpad;
107 cfhsi->aggregation_len += len;
108 else if (direction < 0)
109 cfhsi->aggregation_len -= len;
112 static bool cfhsi_can_send_aggregate(struct cfhsi *cfhsi)
116 if (cfhsi->aggregation_timeout == 0)
119 for (i = 0; i < CFHSI_PRIO_BEBK; ++i) {
120 if (cfhsi->qhead[i].qlen)
124 /* TODO: Use aggregation_len instead */
125 if (cfhsi->qhead[CFHSI_PRIO_BEBK].qlen >= CFHSI_MAX_PKTS)
131 static struct sk_buff *cfhsi_dequeue(struct cfhsi *cfhsi)
136 for (i = 0; i < CFHSI_PRIO_LAST; ++i) {
137 skb = skb_dequeue(&cfhsi->qhead[i]);
145 static int cfhsi_tx_queue_len(struct cfhsi *cfhsi)
148 for (i = 0; i < CFHSI_PRIO_LAST; ++i)
149 len += skb_queue_len(&cfhsi->qhead[i]);
153 static void cfhsi_abort_tx(struct cfhsi *cfhsi)
158 spin_lock_bh(&cfhsi->lock);
159 skb = cfhsi_dequeue(cfhsi);
163 cfhsi->ndev->stats.tx_errors++;
164 cfhsi->ndev->stats.tx_dropped++;
165 cfhsi_update_aggregation_stats(cfhsi, skb, -1);
166 spin_unlock_bh(&cfhsi->lock);
169 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
170 if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
171 mod_timer(&cfhsi->inactivity_timer,
172 jiffies + cfhsi->inactivity_timeout);
173 spin_unlock_bh(&cfhsi->lock);
176 static int cfhsi_flush_fifo(struct cfhsi *cfhsi)
178 char buffer[32]; /* Any reasonable value */
179 size_t fifo_occupancy;
182 netdev_dbg(cfhsi->ndev, "%s.\n",
186 ret = cfhsi->ops->cfhsi_fifo_occupancy(cfhsi->ops,
189 netdev_warn(cfhsi->ndev,
190 "%s: can't get FIFO occupancy: %d.\n",
193 } else if (!fifo_occupancy)
194 /* No more data, exitting normally */
197 fifo_occupancy = min(sizeof(buffer), fifo_occupancy);
198 set_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
199 ret = cfhsi->ops->cfhsi_rx(buffer, fifo_occupancy,
202 clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
203 netdev_warn(cfhsi->ndev,
204 "%s: can't read data: %d.\n",
210 ret = wait_event_interruptible_timeout(cfhsi->flush_fifo_wait,
211 !test_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits), ret);
214 netdev_warn(cfhsi->ndev,
215 "%s: can't wait for flush complete: %d.\n",
220 netdev_warn(cfhsi->ndev,
221 "%s: timeout waiting for flush complete.\n",
230 static int cfhsi_tx_frm(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
235 u8 *pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
237 skb = cfhsi_dequeue(cfhsi);
244 /* Check if we can embed a CAIF frame. */
245 if (skb->len < CFHSI_MAX_EMB_FRM_SZ) {
246 struct caif_payload_info *info;
250 /* Calculate needed head alignment and tail alignment. */
251 info = (struct caif_payload_info *)&skb->cb;
253 hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
254 tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);
256 /* Check if frame still fits with added alignment. */
257 if ((skb->len + hpad + tpad) <= CFHSI_MAX_EMB_FRM_SZ) {
258 u8 *pemb = desc->emb_frm;
259 desc->offset = CFHSI_DESC_SHORT_SZ;
260 *pemb = (u8)(hpad - 1);
263 /* Update network statistics. */
264 spin_lock_bh(&cfhsi->lock);
265 cfhsi->ndev->stats.tx_packets++;
266 cfhsi->ndev->stats.tx_bytes += skb->len;
267 cfhsi_update_aggregation_stats(cfhsi, skb, -1);
268 spin_unlock_bh(&cfhsi->lock);
270 /* Copy in embedded CAIF frame. */
271 skb_copy_bits(skb, 0, pemb, skb->len);
273 /* Consume the SKB */
279 /* Create payload CAIF frames. */
280 pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
281 while (nfrms < CFHSI_MAX_PKTS) {
282 struct caif_payload_info *info;
287 skb = cfhsi_dequeue(cfhsi);
292 /* Calculate needed head alignment and tail alignment. */
293 info = (struct caif_payload_info *)&skb->cb;
295 hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
296 tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);
298 /* Fill in CAIF frame length in descriptor. */
299 desc->cffrm_len[nfrms] = hpad + skb->len + tpad;
301 /* Fill head padding information. */
302 *pfrm = (u8)(hpad - 1);
305 /* Update network statistics. */
306 spin_lock_bh(&cfhsi->lock);
307 cfhsi->ndev->stats.tx_packets++;
308 cfhsi->ndev->stats.tx_bytes += skb->len;
309 cfhsi_update_aggregation_stats(cfhsi, skb, -1);
310 spin_unlock_bh(&cfhsi->lock);
312 /* Copy in CAIF frame. */
313 skb_copy_bits(skb, 0, pfrm, skb->len);
315 /* Update payload length. */
316 pld_len += desc->cffrm_len[nfrms];
318 /* Update frame pointer. */
319 pfrm += skb->len + tpad;
321 /* Consume the SKB */
325 /* Update number of frames. */
329 /* Unused length fields should be zero-filled (according to SPEC). */
330 while (nfrms < CFHSI_MAX_PKTS) {
331 desc->cffrm_len[nfrms] = 0x0000;
335 /* Check if we can piggy-back another descriptor. */
336 if (cfhsi_can_send_aggregate(cfhsi))
337 desc->header |= CFHSI_PIGGY_DESC;
339 desc->header &= ~CFHSI_PIGGY_DESC;
341 return CFHSI_DESC_SZ + pld_len;
344 static void cfhsi_start_tx(struct cfhsi *cfhsi)
346 struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
349 netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
351 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
355 /* Create HSI frame. */
356 len = cfhsi_tx_frm(desc, cfhsi);
358 spin_lock_bh(&cfhsi->lock);
359 if (unlikely(cfhsi_tx_queue_len(cfhsi))) {
360 spin_unlock_bh(&cfhsi->lock);
364 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
365 /* Start inactivity timer. */
366 mod_timer(&cfhsi->inactivity_timer,
367 jiffies + cfhsi->inactivity_timeout);
368 spin_unlock_bh(&cfhsi->lock);
372 /* Set up new transfer. */
373 res = cfhsi->ops->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->ops);
374 if (WARN_ON(res < 0))
375 netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
380 static void cfhsi_tx_done(struct cfhsi *cfhsi)
382 netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
384 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
388 * Send flow on if flow off has been previously signalled
389 * and number of packets is below low water mark.
391 spin_lock_bh(&cfhsi->lock);
392 if (cfhsi->flow_off_sent &&
393 cfhsi_tx_queue_len(cfhsi) <= cfhsi->q_low_mark &&
394 cfhsi->cfdev.flowctrl) {
396 cfhsi->flow_off_sent = 0;
397 cfhsi->cfdev.flowctrl(cfhsi->ndev, ON);
400 if (cfhsi_can_send_aggregate(cfhsi)) {
401 spin_unlock_bh(&cfhsi->lock);
402 cfhsi_start_tx(cfhsi);
404 mod_timer(&cfhsi->aggregation_timer,
405 jiffies + cfhsi->aggregation_timeout);
406 spin_unlock_bh(&cfhsi->lock);
412 static void cfhsi_tx_done_cb(struct cfhsi_cb_ops *cb_ops)
416 cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
417 netdev_dbg(cfhsi->ndev, "%s.\n",
420 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
422 cfhsi_tx_done(cfhsi);
425 static int cfhsi_rx_desc(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
432 if ((desc->header & ~CFHSI_PIGGY_DESC) ||
433 (desc->offset > CFHSI_MAX_EMB_FRM_SZ)) {
434 netdev_err(cfhsi->ndev, "%s: Invalid descriptor.\n",
439 /* Check for embedded CAIF frame. */
444 pfrm = ((u8 *)desc) + desc->offset;
446 /* Remove offset padding. */
449 /* Read length of CAIF frame (little endian). */
451 len |= ((*(pfrm+1)) << 8) & 0xFF00;
452 len += 2; /* Add FCS fields. */
454 /* Sanity check length of CAIF frame. */
455 if (unlikely(len > CFHSI_MAX_CAIF_FRAME_SZ)) {
456 netdev_err(cfhsi->ndev, "%s: Invalid length.\n",
461 /* Allocate SKB (OK even in IRQ context). */
462 skb = alloc_skb(len + 1, GFP_ATOMIC);
464 netdev_err(cfhsi->ndev, "%s: Out of memory !\n",
468 caif_assert(skb != NULL);
470 dst = skb_put(skb, len);
471 memcpy(dst, pfrm, len);
473 skb->protocol = htons(ETH_P_CAIF);
474 skb_reset_mac_header(skb);
475 skb->dev = cfhsi->ndev;
478 * We are in a callback handler and
479 * unfortunately we don't know what context we're
487 /* Update network statistics. */
488 cfhsi->ndev->stats.rx_packets++;
489 cfhsi->ndev->stats.rx_bytes += len;
492 /* Calculate transfer length. */
493 plen = desc->cffrm_len;
494 while (nfrms < CFHSI_MAX_PKTS && *plen) {
500 /* Check for piggy-backed descriptor. */
501 if (desc->header & CFHSI_PIGGY_DESC)
502 xfer_sz += CFHSI_DESC_SZ;
504 if ((xfer_sz % 4) || (xfer_sz > (CFHSI_BUF_SZ_RX - CFHSI_DESC_SZ))) {
505 netdev_err(cfhsi->ndev,
506 "%s: Invalid payload len: %d, ignored.\n",
513 static int cfhsi_rx_desc_len(struct cfhsi_desc *desc)
519 if ((desc->header & ~CFHSI_PIGGY_DESC) ||
520 (desc->offset > CFHSI_MAX_EMB_FRM_SZ)) {
522 pr_err("Invalid descriptor. %x %x\n", desc->header,
527 /* Calculate transfer length. */
528 plen = desc->cffrm_len;
529 while (nfrms < CFHSI_MAX_PKTS && *plen) {
536 pr_err("Invalid payload len: %d, ignored.\n", xfer_sz);
542 static int cfhsi_rx_pld(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
549 /* Sanity check header and offset. */
550 if (WARN_ON((desc->header & ~CFHSI_PIGGY_DESC) ||
551 (desc->offset > CFHSI_MAX_EMB_FRM_SZ))) {
552 netdev_err(cfhsi->ndev, "%s: Invalid descriptor.\n",
557 /* Set frame pointer to start of payload. */
558 pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
559 plen = desc->cffrm_len;
561 /* Skip already processed frames. */
562 while (nfrms < cfhsi->rx_state.nfrms) {
570 while (nfrms < CFHSI_MAX_PKTS && *plen) {
576 /* CAIF frame starts after head padding. */
577 pcffrm = pfrm + *pfrm + 1;
579 /* Read length of CAIF frame (little endian). */
581 len |= ((*(pcffrm + 1)) << 8) & 0xFF00;
582 len += 2; /* Add FCS fields. */
584 /* Sanity check length of CAIF frames. */
585 if (unlikely(len > CFHSI_MAX_CAIF_FRAME_SZ)) {
586 netdev_err(cfhsi->ndev, "%s: Invalid length.\n",
591 /* Allocate SKB (OK even in IRQ context). */
592 skb = alloc_skb(len + 1, GFP_ATOMIC);
594 netdev_err(cfhsi->ndev, "%s: Out of memory !\n",
596 cfhsi->rx_state.nfrms = nfrms;
599 caif_assert(skb != NULL);
601 dst = skb_put(skb, len);
602 memcpy(dst, pcffrm, len);
604 skb->protocol = htons(ETH_P_CAIF);
605 skb_reset_mac_header(skb);
606 skb->dev = cfhsi->ndev;
609 * We're called in callback from HSI
610 * and don't know the context we're running in.
617 /* Update network statistics. */
618 cfhsi->ndev->stats.rx_packets++;
619 cfhsi->ndev->stats.rx_bytes += len;
630 static void cfhsi_rx_done(struct cfhsi *cfhsi)
633 int desc_pld_len = 0, rx_len, rx_state;
634 struct cfhsi_desc *desc = NULL;
636 struct cfhsi_desc *piggy_desc = NULL;
638 desc = (struct cfhsi_desc *)cfhsi->rx_buf;
640 netdev_dbg(cfhsi->ndev, "%s\n", __func__);
642 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
645 /* Update inactivity timer if pending. */
646 spin_lock_bh(&cfhsi->lock);
647 mod_timer_pending(&cfhsi->inactivity_timer,
648 jiffies + cfhsi->inactivity_timeout);
649 spin_unlock_bh(&cfhsi->lock);
651 if (cfhsi->rx_state.state == CFHSI_RX_STATE_DESC) {
652 desc_pld_len = cfhsi_rx_desc_len(desc);
654 if (desc_pld_len < 0)
657 rx_buf = cfhsi->rx_buf;
658 rx_len = desc_pld_len;
659 if (desc_pld_len > 0 && (desc->header & CFHSI_PIGGY_DESC))
660 rx_len += CFHSI_DESC_SZ;
661 if (desc_pld_len == 0)
662 rx_buf = cfhsi->rx_flip_buf;
664 rx_buf = cfhsi->rx_flip_buf;
666 rx_len = CFHSI_DESC_SZ;
667 if (cfhsi->rx_state.pld_len > 0 &&
668 (desc->header & CFHSI_PIGGY_DESC)) {
670 piggy_desc = (struct cfhsi_desc *)
671 (desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ +
672 cfhsi->rx_state.pld_len);
674 cfhsi->rx_state.piggy_desc = true;
676 /* Extract payload len from piggy-backed descriptor. */
677 desc_pld_len = cfhsi_rx_desc_len(piggy_desc);
678 if (desc_pld_len < 0)
681 if (desc_pld_len > 0) {
682 rx_len = desc_pld_len;
683 if (piggy_desc->header & CFHSI_PIGGY_DESC)
684 rx_len += CFHSI_DESC_SZ;
688 * Copy needed information from the piggy-backed
689 * descriptor to the descriptor in the start.
691 memcpy(rx_buf, (u8 *)piggy_desc,
692 CFHSI_DESC_SHORT_SZ);
693 /* Mark no embedded frame here */
694 piggy_desc->offset = 0;
699 rx_state = CFHSI_RX_STATE_PAYLOAD;
700 rx_ptr = rx_buf + CFHSI_DESC_SZ;
702 rx_state = CFHSI_RX_STATE_DESC;
704 rx_len = CFHSI_DESC_SZ;
707 /* Initiate next read */
708 if (test_bit(CFHSI_AWAKE, &cfhsi->bits)) {
709 /* Set up new transfer. */
710 netdev_dbg(cfhsi->ndev, "%s: Start RX.\n",
713 res = cfhsi->ops->cfhsi_rx(rx_ptr, rx_len,
715 if (WARN_ON(res < 0)) {
716 netdev_err(cfhsi->ndev, "%s: RX error %d.\n",
718 cfhsi->ndev->stats.rx_errors++;
719 cfhsi->ndev->stats.rx_dropped++;
723 if (cfhsi->rx_state.state == CFHSI_RX_STATE_DESC) {
724 /* Extract payload from descriptor */
725 if (cfhsi_rx_desc(desc, cfhsi) < 0)
728 /* Extract payload */
729 if (cfhsi_rx_pld(desc, cfhsi) < 0)
732 /* Extract any payload in piggyback descriptor. */
733 if (cfhsi_rx_desc(piggy_desc, cfhsi) < 0)
738 /* Update state info */
739 memset(&cfhsi->rx_state, 0, sizeof(cfhsi->rx_state));
740 cfhsi->rx_state.state = rx_state;
741 cfhsi->rx_ptr = rx_ptr;
742 cfhsi->rx_len = rx_len;
743 cfhsi->rx_state.pld_len = desc_pld_len;
744 cfhsi->rx_state.piggy_desc = desc->header & CFHSI_PIGGY_DESC;
746 if (rx_buf != cfhsi->rx_buf)
747 swap(cfhsi->rx_buf, cfhsi->rx_flip_buf);
751 netdev_err(cfhsi->ndev, "%s: Out of sync.\n", __func__);
752 print_hex_dump_bytes("--> ", DUMP_PREFIX_NONE,
753 cfhsi->rx_buf, CFHSI_DESC_SZ);
754 schedule_work(&cfhsi->out_of_sync_work);
757 static void cfhsi_rx_slowpath(unsigned long arg)
759 struct cfhsi *cfhsi = (struct cfhsi *)arg;
761 netdev_dbg(cfhsi->ndev, "%s.\n",
764 cfhsi_rx_done(cfhsi);
767 static void cfhsi_rx_done_cb(struct cfhsi_cb_ops *cb_ops)
771 cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
772 netdev_dbg(cfhsi->ndev, "%s.\n",
775 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
778 if (test_and_clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits))
779 wake_up_interruptible(&cfhsi->flush_fifo_wait);
781 cfhsi_rx_done(cfhsi);
784 static void cfhsi_wake_up(struct work_struct *work)
786 struct cfhsi *cfhsi = NULL;
791 cfhsi = container_of(work, struct cfhsi, wake_up_work);
793 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
796 if (unlikely(test_bit(CFHSI_AWAKE, &cfhsi->bits))) {
797 /* It happenes when wakeup is requested by
798 * both ends at the same time. */
799 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
800 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
804 /* Activate wake line. */
805 cfhsi->ops->cfhsi_wake_up(cfhsi->ops);
807 netdev_dbg(cfhsi->ndev, "%s: Start waiting.\n",
810 /* Wait for acknowledge. */
811 ret = CFHSI_WAKE_TOUT;
812 ret = wait_event_interruptible_timeout(cfhsi->wake_up_wait,
813 test_and_clear_bit(CFHSI_WAKE_UP_ACK,
815 if (unlikely(ret < 0)) {
816 /* Interrupted by signal. */
817 netdev_err(cfhsi->ndev, "%s: Signalled: %ld.\n",
820 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
821 cfhsi->ops->cfhsi_wake_down(cfhsi->ops);
824 bool ca_wake = false;
825 size_t fifo_occupancy = 0;
828 netdev_dbg(cfhsi->ndev, "%s: Timeout.\n",
831 /* Check FIFO to check if modem has sent something. */
832 WARN_ON(cfhsi->ops->cfhsi_fifo_occupancy(cfhsi->ops,
835 netdev_dbg(cfhsi->ndev, "%s: Bytes in FIFO: %u.\n",
836 __func__, (unsigned) fifo_occupancy);
838 /* Check if we misssed the interrupt. */
839 WARN_ON(cfhsi->ops->cfhsi_get_peer_wake(cfhsi->ops,
843 netdev_err(cfhsi->ndev, "%s: CA Wake missed !.\n",
846 /* Clear the CFHSI_WAKE_UP_ACK bit to prevent race. */
847 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
849 /* Continue execution. */
853 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
854 cfhsi->ops->cfhsi_wake_down(cfhsi->ops);
858 netdev_dbg(cfhsi->ndev, "%s: Woken.\n",
861 /* Clear power up bit. */
862 set_bit(CFHSI_AWAKE, &cfhsi->bits);
863 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
865 /* Resume read operation. */
866 netdev_dbg(cfhsi->ndev, "%s: Start RX.\n", __func__);
867 res = cfhsi->ops->cfhsi_rx(cfhsi->rx_ptr, cfhsi->rx_len, cfhsi->ops);
869 if (WARN_ON(res < 0))
870 netdev_err(cfhsi->ndev, "%s: RX err %d.\n", __func__, res);
872 /* Clear power up acknowledment. */
873 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
875 spin_lock_bh(&cfhsi->lock);
877 /* Resume transmit if queues are not empty. */
878 if (!cfhsi_tx_queue_len(cfhsi)) {
879 netdev_dbg(cfhsi->ndev, "%s: Peer wake, start timer.\n",
881 /* Start inactivity timer. */
882 mod_timer(&cfhsi->inactivity_timer,
883 jiffies + cfhsi->inactivity_timeout);
884 spin_unlock_bh(&cfhsi->lock);
888 netdev_dbg(cfhsi->ndev, "%s: Host wake.\n",
891 spin_unlock_bh(&cfhsi->lock);
893 /* Create HSI frame. */
894 len = cfhsi_tx_frm((struct cfhsi_desc *)cfhsi->tx_buf, cfhsi);
896 if (likely(len > 0)) {
897 /* Set up new transfer. */
898 res = cfhsi->ops->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->ops);
899 if (WARN_ON(res < 0)) {
900 netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
902 cfhsi_abort_tx(cfhsi);
905 netdev_err(cfhsi->ndev,
906 "%s: Failed to create HSI frame: %d.\n",
911 static void cfhsi_wake_down(struct work_struct *work)
914 struct cfhsi *cfhsi = NULL;
915 size_t fifo_occupancy = 0;
916 int retry = CFHSI_WAKE_TOUT;
918 cfhsi = container_of(work, struct cfhsi, wake_down_work);
919 netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
921 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
924 /* Deactivate wake line. */
925 cfhsi->ops->cfhsi_wake_down(cfhsi->ops);
927 /* Wait for acknowledge. */
928 ret = CFHSI_WAKE_TOUT;
929 ret = wait_event_interruptible_timeout(cfhsi->wake_down_wait,
930 test_and_clear_bit(CFHSI_WAKE_DOWN_ACK,
933 /* Interrupted by signal. */
934 netdev_err(cfhsi->ndev, "%s: Signalled: %ld.\n",
941 netdev_err(cfhsi->ndev, "%s: Timeout.\n", __func__);
943 /* Check if we misssed the interrupt. */
944 WARN_ON(cfhsi->ops->cfhsi_get_peer_wake(cfhsi->ops,
947 netdev_err(cfhsi->ndev, "%s: CA Wake missed !.\n",
951 /* Check FIFO occupancy. */
953 WARN_ON(cfhsi->ops->cfhsi_fifo_occupancy(cfhsi->ops,
959 set_current_state(TASK_INTERRUPTIBLE);
965 netdev_err(cfhsi->ndev, "%s: FIFO Timeout.\n", __func__);
967 /* Clear AWAKE condition. */
968 clear_bit(CFHSI_AWAKE, &cfhsi->bits);
970 /* Cancel pending RX requests. */
971 cfhsi->ops->cfhsi_rx_cancel(cfhsi->ops);
974 static void cfhsi_out_of_sync(struct work_struct *work)
976 struct cfhsi *cfhsi = NULL;
978 cfhsi = container_of(work, struct cfhsi, out_of_sync_work);
981 dev_close(cfhsi->ndev);
985 static void cfhsi_wake_up_cb(struct cfhsi_cb_ops *cb_ops)
987 struct cfhsi *cfhsi = NULL;
989 cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
990 netdev_dbg(cfhsi->ndev, "%s.\n",
993 set_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
994 wake_up_interruptible(&cfhsi->wake_up_wait);
996 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
999 /* Schedule wake up work queue if the peer initiates. */
1000 if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
1001 queue_work(cfhsi->wq, &cfhsi->wake_up_work);
1004 static void cfhsi_wake_down_cb(struct cfhsi_cb_ops *cb_ops)
1006 struct cfhsi *cfhsi = NULL;
1008 cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
1009 netdev_dbg(cfhsi->ndev, "%s.\n",
1012 /* Initiating low power is only permitted by the host (us). */
1013 set_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
1014 wake_up_interruptible(&cfhsi->wake_down_wait);
1017 static void cfhsi_aggregation_tout(unsigned long arg)
1019 struct cfhsi *cfhsi = (struct cfhsi *)arg;
1021 netdev_dbg(cfhsi->ndev, "%s.\n",
1024 cfhsi_start_tx(cfhsi);
1027 static int cfhsi_xmit(struct sk_buff *skb, struct net_device *dev)
1029 struct cfhsi *cfhsi = NULL;
1037 cfhsi = netdev_priv(dev);
1039 switch (skb->priority) {
1040 case TC_PRIO_BESTEFFORT:
1041 case TC_PRIO_FILLER:
1043 prio = CFHSI_PRIO_BEBK;
1045 case TC_PRIO_INTERACTIVE_BULK:
1046 prio = CFHSI_PRIO_VI;
1048 case TC_PRIO_INTERACTIVE:
1049 prio = CFHSI_PRIO_VO;
1051 case TC_PRIO_CONTROL:
1053 prio = CFHSI_PRIO_CTL;
1057 spin_lock_bh(&cfhsi->lock);
1059 /* Update aggregation statistics */
1060 cfhsi_update_aggregation_stats(cfhsi, skb, 1);
1063 skb_queue_tail(&cfhsi->qhead[prio], skb);
1065 /* Sanity check; xmit should not be called after unregister_netdev */
1066 if (WARN_ON(test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))) {
1067 spin_unlock_bh(&cfhsi->lock);
1068 cfhsi_abort_tx(cfhsi);
1072 /* Send flow off if number of packets is above high water mark. */
1073 if (!cfhsi->flow_off_sent &&
1074 cfhsi_tx_queue_len(cfhsi) > cfhsi->q_high_mark &&
1075 cfhsi->cfdev.flowctrl) {
1076 cfhsi->flow_off_sent = 1;
1077 cfhsi->cfdev.flowctrl(cfhsi->ndev, OFF);
1080 if (cfhsi->tx_state == CFHSI_TX_STATE_IDLE) {
1081 cfhsi->tx_state = CFHSI_TX_STATE_XFER;
1086 /* Send aggregate if it is possible */
1087 bool aggregate_ready =
1088 cfhsi_can_send_aggregate(cfhsi) &&
1089 del_timer(&cfhsi->aggregation_timer) > 0;
1090 spin_unlock_bh(&cfhsi->lock);
1091 if (aggregate_ready)
1092 cfhsi_start_tx(cfhsi);
1096 /* Delete inactivity timer if started. */
1097 timer_active = del_timer_sync(&cfhsi->inactivity_timer);
1099 spin_unlock_bh(&cfhsi->lock);
1102 struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
1106 /* Create HSI frame. */
1107 len = cfhsi_tx_frm(desc, cfhsi);
1110 /* Set up new transfer. */
1111 res = cfhsi->ops->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->ops);
1112 if (WARN_ON(res < 0)) {
1113 netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
1115 cfhsi_abort_tx(cfhsi);
1118 /* Schedule wake up work queue if the we initiate. */
1119 if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
1120 queue_work(cfhsi->wq, &cfhsi->wake_up_work);
1126 static const struct net_device_ops cfhsi_netdevops;
1128 static void cfhsi_setup(struct net_device *dev)
1131 struct cfhsi *cfhsi = netdev_priv(dev);
1133 dev->type = ARPHRD_CAIF;
1134 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
1135 dev->mtu = CFHSI_MAX_CAIF_FRAME_SZ;
1136 dev->tx_queue_len = 0;
1137 dev->destructor = free_netdev;
1138 dev->netdev_ops = &cfhsi_netdevops;
1139 for (i = 0; i < CFHSI_PRIO_LAST; ++i)
1140 skb_queue_head_init(&cfhsi->qhead[i]);
1141 cfhsi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
1142 cfhsi->cfdev.use_frag = false;
1143 cfhsi->cfdev.use_stx = false;
1144 cfhsi->cfdev.use_fcs = false;
1148 static int cfhsi_open(struct net_device *ndev)
1150 struct cfhsi *cfhsi = netdev_priv(ndev);
1153 clear_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
1155 /* Initialize state vaiables. */
1156 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
1157 cfhsi->rx_state.state = CFHSI_RX_STATE_DESC;
1160 cfhsi->flow_off_sent = 0;
1161 cfhsi->q_low_mark = LOW_WATER_MARK;
1162 cfhsi->q_high_mark = HIGH_WATER_MARK;
1166 * Allocate a TX buffer with the size of a HSI packet descriptors
1167 * and the necessary room for CAIF payload frames.
1169 cfhsi->tx_buf = kzalloc(CFHSI_BUF_SZ_TX, GFP_KERNEL);
1170 if (!cfhsi->tx_buf) {
1176 * Allocate a RX buffer with the size of two HSI packet descriptors and
1177 * the necessary room for CAIF payload frames.
1179 cfhsi->rx_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
1180 if (!cfhsi->rx_buf) {
1185 cfhsi->rx_flip_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
1186 if (!cfhsi->rx_flip_buf) {
1188 goto err_alloc_rx_flip;
1191 /* Pre-calculate inactivity timeout. */
1192 if (inactivity_timeout != -1) {
1193 cfhsi->inactivity_timeout =
1194 inactivity_timeout * HZ / 1000;
1195 if (!cfhsi->inactivity_timeout)
1196 cfhsi->inactivity_timeout = 1;
1197 else if (cfhsi->inactivity_timeout > NEXT_TIMER_MAX_DELTA)
1198 cfhsi->inactivity_timeout = NEXT_TIMER_MAX_DELTA;
1200 cfhsi->inactivity_timeout = NEXT_TIMER_MAX_DELTA;
1203 /* Initialize aggregation timeout */
1204 cfhsi->aggregation_timeout = aggregation_timeout;
1206 /* Initialize recieve vaiables. */
1207 cfhsi->rx_ptr = cfhsi->rx_buf;
1208 cfhsi->rx_len = CFHSI_DESC_SZ;
1210 /* Initialize spin locks. */
1211 spin_lock_init(&cfhsi->lock);
1213 /* Set up the driver. */
1214 cfhsi->cb_ops.tx_done_cb = cfhsi_tx_done_cb;
1215 cfhsi->cb_ops.rx_done_cb = cfhsi_rx_done_cb;
1216 cfhsi->cb_ops.wake_up_cb = cfhsi_wake_up_cb;
1217 cfhsi->cb_ops.wake_down_cb = cfhsi_wake_down_cb;
1219 /* Initialize the work queues. */
1220 INIT_WORK(&cfhsi->wake_up_work, cfhsi_wake_up);
1221 INIT_WORK(&cfhsi->wake_down_work, cfhsi_wake_down);
1222 INIT_WORK(&cfhsi->out_of_sync_work, cfhsi_out_of_sync);
1224 /* Clear all bit fields. */
1225 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
1226 clear_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
1227 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
1228 clear_bit(CFHSI_AWAKE, &cfhsi->bits);
1230 /* Create work thread. */
1231 cfhsi->wq = create_singlethread_workqueue(cfhsi->ndev->name);
1233 netdev_err(cfhsi->ndev, "%s: Failed to create work queue.\n",
1239 /* Initialize wait queues. */
1240 init_waitqueue_head(&cfhsi->wake_up_wait);
1241 init_waitqueue_head(&cfhsi->wake_down_wait);
1242 init_waitqueue_head(&cfhsi->flush_fifo_wait);
1244 /* Setup the inactivity timer. */
1245 init_timer(&cfhsi->inactivity_timer);
1246 cfhsi->inactivity_timer.data = (unsigned long)cfhsi;
1247 cfhsi->inactivity_timer.function = cfhsi_inactivity_tout;
1248 /* Setup the slowpath RX timer. */
1249 init_timer(&cfhsi->rx_slowpath_timer);
1250 cfhsi->rx_slowpath_timer.data = (unsigned long)cfhsi;
1251 cfhsi->rx_slowpath_timer.function = cfhsi_rx_slowpath;
1252 /* Setup the aggregation timer. */
1253 init_timer(&cfhsi->aggregation_timer);
1254 cfhsi->aggregation_timer.data = (unsigned long)cfhsi;
1255 cfhsi->aggregation_timer.function = cfhsi_aggregation_tout;
1257 /* Activate HSI interface. */
1258 res = cfhsi->ops->cfhsi_up(cfhsi->ops);
1260 netdev_err(cfhsi->ndev,
1261 "%s: can't activate HSI interface: %d.\n",
1267 res = cfhsi_flush_fifo(cfhsi);
1269 netdev_err(cfhsi->ndev, "%s: Can't flush FIFO: %d.\n",
1276 cfhsi->ops->cfhsi_down(cfhsi->ops);
1278 destroy_workqueue(cfhsi->wq);
1280 kfree(cfhsi->rx_flip_buf);
1282 kfree(cfhsi->rx_buf);
1284 kfree(cfhsi->tx_buf);
1289 static int cfhsi_close(struct net_device *ndev)
1291 struct cfhsi *cfhsi = netdev_priv(ndev);
1292 u8 *tx_buf, *rx_buf, *flip_buf;
1294 /* going to shutdown driver */
1295 set_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
1297 /* Flush workqueue */
1298 flush_workqueue(cfhsi->wq);
1300 /* Delete timers if pending */
1301 del_timer_sync(&cfhsi->inactivity_timer);
1302 del_timer_sync(&cfhsi->rx_slowpath_timer);
1303 del_timer_sync(&cfhsi->aggregation_timer);
1305 /* Cancel pending RX request (if any) */
1306 cfhsi->ops->cfhsi_rx_cancel(cfhsi->ops);
1308 /* Destroy workqueue */
1309 destroy_workqueue(cfhsi->wq);
1311 /* Store bufferes: will be freed later. */
1312 tx_buf = cfhsi->tx_buf;
1313 rx_buf = cfhsi->rx_buf;
1314 flip_buf = cfhsi->rx_flip_buf;
1315 /* Flush transmit queues. */
1316 cfhsi_abort_tx(cfhsi);
1318 /* Deactivate interface */
1319 cfhsi->ops->cfhsi_down(cfhsi->ops);
1328 static void cfhsi_uninit(struct net_device *dev)
1330 struct cfhsi *cfhsi = netdev_priv(dev);
1332 symbol_put(cfhsi_get_device);
1333 list_del(&cfhsi->list);
1336 static const struct net_device_ops cfhsi_netdevops = {
1337 .ndo_uninit = cfhsi_uninit,
1338 .ndo_open = cfhsi_open,
1339 .ndo_stop = cfhsi_close,
1340 .ndo_start_xmit = cfhsi_xmit
1343 static void cfhsi_netlink_parms(struct nlattr *data[], struct cfhsi *cfhsi)
1348 pr_debug("no params data found\n");
1352 i = __IFLA_CAIF_HSI_INACTIVITY_TOUT;
1354 inactivity_timeout = nla_get_u32(data[i]);
1356 i = __IFLA_CAIF_HSI_AGGREGATION_TOUT;
1358 aggregation_timeout = nla_get_u32(data[i]);
1360 i = __IFLA_CAIF_HSI_HEAD_ALIGN;
1362 hsi_head_align = nla_get_u32(data[i]);
1364 i = __IFLA_CAIF_HSI_TAIL_ALIGN;
1366 hsi_tail_align = nla_get_u32(data[i]);
1368 i = __IFLA_CAIF_HSI_QHIGH_WATERMARK;
1370 hsi_high_threshold = nla_get_u32(data[i]);
1373 static int caif_hsi_changelink(struct net_device *dev, struct nlattr *tb[],
1374 struct nlattr *data[])
1376 cfhsi_netlink_parms(data, netdev_priv(dev));
1377 netdev_state_change(dev);
1381 static const struct nla_policy caif_hsi_policy[__IFLA_CAIF_HSI_MAX + 1] = {
1382 [__IFLA_CAIF_HSI_INACTIVITY_TOUT] = { .type = NLA_U32, .len = 4 },
1383 [__IFLA_CAIF_HSI_AGGREGATION_TOUT] = { .type = NLA_U32, .len = 4 },
1384 [__IFLA_CAIF_HSI_HEAD_ALIGN] = { .type = NLA_U32, .len = 4 },
1385 [__IFLA_CAIF_HSI_TAIL_ALIGN] = { .type = NLA_U32, .len = 4 },
1386 [__IFLA_CAIF_HSI_QHIGH_WATERMARK] = { .type = NLA_U32, .len = 4 },
1387 [__IFLA_CAIF_HSI_QLOW_WATERMARK] = { .type = NLA_U32, .len = 4 },
1390 static size_t caif_hsi_get_size(const struct net_device *dev)
1394 for (i = __IFLA_CAIF_HSI_UNSPEC + 1; i < __IFLA_CAIF_HSI_MAX; i++)
1395 s += nla_total_size(caif_hsi_policy[i].len);
1399 static int caif_hsi_fill_info(struct sk_buff *skb, const struct net_device *dev)
1401 if (nla_put_u32(skb, __IFLA_CAIF_HSI_INACTIVITY_TOUT,
1402 inactivity_timeout) ||
1403 nla_put_u32(skb, __IFLA_CAIF_HSI_AGGREGATION_TOUT,
1404 aggregation_timeout) ||
1405 nla_put_u32(skb, __IFLA_CAIF_HSI_HEAD_ALIGN, hsi_head_align) ||
1406 nla_put_u32(skb, __IFLA_CAIF_HSI_TAIL_ALIGN, hsi_tail_align) ||
1407 nla_put_u32(skb, __IFLA_CAIF_HSI_QHIGH_WATERMARK,
1408 hsi_high_threshold) ||
1409 nla_put_u32(skb, __IFLA_CAIF_HSI_QLOW_WATERMARK,
1416 static int caif_hsi_newlink(struct net *src_net, struct net_device *dev,
1417 struct nlattr *tb[], struct nlattr *data[])
1419 struct cfhsi *cfhsi = NULL;
1420 struct cfhsi_ops *(*get_ops)(void);
1424 cfhsi = netdev_priv(dev);
1425 cfhsi_netlink_parms(data, cfhsi);
1426 dev_net_set(cfhsi->ndev, src_net);
1428 get_ops = symbol_get(cfhsi_get_ops);
1430 pr_err("%s: failed to get the cfhsi_ops\n", __func__);
1434 /* Assign the HSI device. */
1435 cfhsi->ops = (*get_ops)();
1437 pr_err("%s: failed to get the cfhsi_ops\n", __func__);
1441 /* Assign the driver to this HSI device. */
1442 cfhsi->ops->cb_ops = &cfhsi->cb_ops;
1443 if (register_netdevice(dev)) {
1444 pr_warn("%s: caif_hsi device registration failed\n", __func__);
1447 /* Add CAIF HSI device to list. */
1448 list_add_tail(&cfhsi->list, &cfhsi_list);
1452 symbol_put(cfhsi_get_ops);
1456 static struct rtnl_link_ops caif_hsi_link_ops __read_mostly = {
1458 .priv_size = sizeof(struct cfhsi),
1459 .setup = cfhsi_setup,
1460 .maxtype = __IFLA_CAIF_HSI_MAX,
1461 .policy = caif_hsi_policy,
1462 .newlink = caif_hsi_newlink,
1463 .changelink = caif_hsi_changelink,
1464 .get_size = caif_hsi_get_size,
1465 .fill_info = caif_hsi_fill_info,
1468 static void __exit cfhsi_exit_module(void)
1470 struct list_head *list_node;
1471 struct list_head *n;
1472 struct cfhsi *cfhsi;
1474 rtnl_link_unregister(&caif_hsi_link_ops);
1477 list_for_each_safe(list_node, n, &cfhsi_list) {
1478 cfhsi = list_entry(list_node, struct cfhsi, list);
1479 unregister_netdev(cfhsi->ndev);
1484 static int __init cfhsi_init_module(void)
1486 return rtnl_link_register(&caif_hsi_link_ops);
1489 module_init(cfhsi_init_module);
1490 module_exit(cfhsi_exit_module);