2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Daniel Martensson
5 * License terms: GNU General Public License (GPL) version 2.
8 #define pr_fmt(fmt) KBUILD_MODNAME fmt
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/netdevice.h>
14 #include <linux/string.h>
15 #include <linux/list.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/sched.h>
19 #include <linux/if_arp.h>
20 #include <linux/timer.h>
21 #include <net/rtnetlink.h>
22 #include <linux/pkt_sched.h>
23 #include <net/caif/caif_layer.h>
24 #include <net/caif/caif_hsi.h>
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Daniel Martensson");
28 MODULE_DESCRIPTION("CAIF HSI driver");
30 /* Returns the number of padding bytes for alignment. */
31 #define PAD_POW2(x, pow) ((((x)&((pow)-1)) == 0) ? 0 :\
32 (((pow)-((x)&((pow)-1)))))
34 static const struct cfhsi_config hsi_default_config = {
36 /* Inactivity timeout on HSI, ms */
37 .inactivity_timeout = HZ,
39 /* Aggregation timeout (ms) of zero means no aggregation is done*/
40 .aggregation_timeout = 1,
43 * HSI link layer flow-control thresholds.
44 * Threshold values for the HSI packet queue. Flow-control will be
45 * asserted when the number of packets exceeds q_high_mark. It will
46 * not be de-asserted before the number of packets drops below
48 * Warning: A high threshold value might increase throughput but it
49 * will at the same time prevent channel prioritization and increase
50 * the risk of flooding the modem. The high threshold should be above
57 * HSI padding options.
58 * Warning: must be a base of 2 (& operation used) and can not be zero !
67 static LIST_HEAD(cfhsi_list);
69 static void cfhsi_inactivity_tout(struct timer_list *t)
71 struct cfhsi *cfhsi = from_timer(cfhsi, t, inactivity_timer);
73 netdev_dbg(cfhsi->ndev, "%s.\n",
76 /* Schedule power down work queue. */
77 if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
78 queue_work(cfhsi->wq, &cfhsi->wake_down_work);
81 static void cfhsi_update_aggregation_stats(struct cfhsi *cfhsi,
82 const struct sk_buff *skb,
85 struct caif_payload_info *info;
88 info = (struct caif_payload_info *)&skb->cb;
89 hpad = 1 + PAD_POW2((info->hdr_len + 1), cfhsi->cfg.head_align);
90 tpad = PAD_POW2((skb->len + hpad), cfhsi->cfg.tail_align);
91 len = skb->len + hpad + tpad;
94 cfhsi->aggregation_len += len;
95 else if (direction < 0)
96 cfhsi->aggregation_len -= len;
99 static bool cfhsi_can_send_aggregate(struct cfhsi *cfhsi)
103 if (cfhsi->cfg.aggregation_timeout == 0)
106 for (i = 0; i < CFHSI_PRIO_BEBK; ++i) {
107 if (cfhsi->qhead[i].qlen)
111 /* TODO: Use aggregation_len instead */
112 if (cfhsi->qhead[CFHSI_PRIO_BEBK].qlen >= CFHSI_MAX_PKTS)
118 static struct sk_buff *cfhsi_dequeue(struct cfhsi *cfhsi)
123 for (i = 0; i < CFHSI_PRIO_LAST; ++i) {
124 skb = skb_dequeue(&cfhsi->qhead[i]);
132 static int cfhsi_tx_queue_len(struct cfhsi *cfhsi)
135 for (i = 0; i < CFHSI_PRIO_LAST; ++i)
136 len += skb_queue_len(&cfhsi->qhead[i]);
140 static void cfhsi_abort_tx(struct cfhsi *cfhsi)
145 spin_lock_bh(&cfhsi->lock);
146 skb = cfhsi_dequeue(cfhsi);
150 cfhsi->ndev->stats.tx_errors++;
151 cfhsi->ndev->stats.tx_dropped++;
152 cfhsi_update_aggregation_stats(cfhsi, skb, -1);
153 spin_unlock_bh(&cfhsi->lock);
156 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
157 if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
158 mod_timer(&cfhsi->inactivity_timer,
159 jiffies + cfhsi->cfg.inactivity_timeout);
160 spin_unlock_bh(&cfhsi->lock);
163 static int cfhsi_flush_fifo(struct cfhsi *cfhsi)
165 char buffer[32]; /* Any reasonable value */
166 size_t fifo_occupancy;
169 netdev_dbg(cfhsi->ndev, "%s.\n",
173 ret = cfhsi->ops->cfhsi_fifo_occupancy(cfhsi->ops,
176 netdev_warn(cfhsi->ndev,
177 "%s: can't get FIFO occupancy: %d.\n",
180 } else if (!fifo_occupancy)
181 /* No more data, exitting normally */
184 fifo_occupancy = min(sizeof(buffer), fifo_occupancy);
185 set_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
186 ret = cfhsi->ops->cfhsi_rx(buffer, fifo_occupancy,
189 clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
190 netdev_warn(cfhsi->ndev,
191 "%s: can't read data: %d.\n",
197 ret = wait_event_interruptible_timeout(cfhsi->flush_fifo_wait,
198 !test_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits), ret);
201 netdev_warn(cfhsi->ndev,
202 "%s: can't wait for flush complete: %d.\n",
207 netdev_warn(cfhsi->ndev,
208 "%s: timeout waiting for flush complete.\n",
217 static int cfhsi_tx_frm(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
222 u8 *pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
224 skb = cfhsi_dequeue(cfhsi);
231 /* Check if we can embed a CAIF frame. */
232 if (skb->len < CFHSI_MAX_EMB_FRM_SZ) {
233 struct caif_payload_info *info;
237 /* Calculate needed head alignment and tail alignment. */
238 info = (struct caif_payload_info *)&skb->cb;
240 hpad = 1 + PAD_POW2((info->hdr_len + 1), cfhsi->cfg.head_align);
241 tpad = PAD_POW2((skb->len + hpad), cfhsi->cfg.tail_align);
243 /* Check if frame still fits with added alignment. */
244 if ((skb->len + hpad + tpad) <= CFHSI_MAX_EMB_FRM_SZ) {
245 u8 *pemb = desc->emb_frm;
246 desc->offset = CFHSI_DESC_SHORT_SZ;
247 *pemb = (u8)(hpad - 1);
250 /* Update network statistics. */
251 spin_lock_bh(&cfhsi->lock);
252 cfhsi->ndev->stats.tx_packets++;
253 cfhsi->ndev->stats.tx_bytes += skb->len;
254 cfhsi_update_aggregation_stats(cfhsi, skb, -1);
255 spin_unlock_bh(&cfhsi->lock);
257 /* Copy in embedded CAIF frame. */
258 skb_copy_bits(skb, 0, pemb, skb->len);
260 /* Consume the SKB */
266 /* Create payload CAIF frames. */
267 while (nfrms < CFHSI_MAX_PKTS) {
268 struct caif_payload_info *info;
273 skb = cfhsi_dequeue(cfhsi);
278 /* Calculate needed head alignment and tail alignment. */
279 info = (struct caif_payload_info *)&skb->cb;
281 hpad = 1 + PAD_POW2((info->hdr_len + 1), cfhsi->cfg.head_align);
282 tpad = PAD_POW2((skb->len + hpad), cfhsi->cfg.tail_align);
284 /* Fill in CAIF frame length in descriptor. */
285 desc->cffrm_len[nfrms] = hpad + skb->len + tpad;
287 /* Fill head padding information. */
288 *pfrm = (u8)(hpad - 1);
291 /* Update network statistics. */
292 spin_lock_bh(&cfhsi->lock);
293 cfhsi->ndev->stats.tx_packets++;
294 cfhsi->ndev->stats.tx_bytes += skb->len;
295 cfhsi_update_aggregation_stats(cfhsi, skb, -1);
296 spin_unlock_bh(&cfhsi->lock);
298 /* Copy in CAIF frame. */
299 skb_copy_bits(skb, 0, pfrm, skb->len);
301 /* Update payload length. */
302 pld_len += desc->cffrm_len[nfrms];
304 /* Update frame pointer. */
305 pfrm += skb->len + tpad;
307 /* Consume the SKB */
311 /* Update number of frames. */
315 /* Unused length fields should be zero-filled (according to SPEC). */
316 while (nfrms < CFHSI_MAX_PKTS) {
317 desc->cffrm_len[nfrms] = 0x0000;
321 /* Check if we can piggy-back another descriptor. */
322 if (cfhsi_can_send_aggregate(cfhsi))
323 desc->header |= CFHSI_PIGGY_DESC;
325 desc->header &= ~CFHSI_PIGGY_DESC;
327 return CFHSI_DESC_SZ + pld_len;
330 static void cfhsi_start_tx(struct cfhsi *cfhsi)
332 struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
335 netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
337 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
341 /* Create HSI frame. */
342 len = cfhsi_tx_frm(desc, cfhsi);
344 spin_lock_bh(&cfhsi->lock);
345 if (unlikely(cfhsi_tx_queue_len(cfhsi))) {
346 spin_unlock_bh(&cfhsi->lock);
350 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
351 /* Start inactivity timer. */
352 mod_timer(&cfhsi->inactivity_timer,
353 jiffies + cfhsi->cfg.inactivity_timeout);
354 spin_unlock_bh(&cfhsi->lock);
358 /* Set up new transfer. */
359 res = cfhsi->ops->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->ops);
360 if (WARN_ON(res < 0))
361 netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
366 static void cfhsi_tx_done(struct cfhsi *cfhsi)
368 netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
370 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
374 * Send flow on if flow off has been previously signalled
375 * and number of packets is below low water mark.
377 spin_lock_bh(&cfhsi->lock);
378 if (cfhsi->flow_off_sent &&
379 cfhsi_tx_queue_len(cfhsi) <= cfhsi->cfg.q_low_mark &&
380 cfhsi->cfdev.flowctrl) {
382 cfhsi->flow_off_sent = 0;
383 cfhsi->cfdev.flowctrl(cfhsi->ndev, ON);
386 if (cfhsi_can_send_aggregate(cfhsi)) {
387 spin_unlock_bh(&cfhsi->lock);
388 cfhsi_start_tx(cfhsi);
390 mod_timer(&cfhsi->aggregation_timer,
391 jiffies + cfhsi->cfg.aggregation_timeout);
392 spin_unlock_bh(&cfhsi->lock);
398 static void cfhsi_tx_done_cb(struct cfhsi_cb_ops *cb_ops)
402 cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
403 netdev_dbg(cfhsi->ndev, "%s.\n",
406 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
408 cfhsi_tx_done(cfhsi);
411 static int cfhsi_rx_desc(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
418 if ((desc->header & ~CFHSI_PIGGY_DESC) ||
419 (desc->offset > CFHSI_MAX_EMB_FRM_SZ)) {
420 netdev_err(cfhsi->ndev, "%s: Invalid descriptor.\n",
425 /* Check for embedded CAIF frame. */
429 pfrm = ((u8 *)desc) + desc->offset;
431 /* Remove offset padding. */
434 /* Read length of CAIF frame (little endian). */
436 len |= ((*(pfrm+1)) << 8) & 0xFF00;
437 len += 2; /* Add FCS fields. */
439 /* Sanity check length of CAIF frame. */
440 if (unlikely(len > CFHSI_MAX_CAIF_FRAME_SZ)) {
441 netdev_err(cfhsi->ndev, "%s: Invalid length.\n",
446 /* Allocate SKB (OK even in IRQ context). */
447 skb = alloc_skb(len + 1, GFP_ATOMIC);
449 netdev_err(cfhsi->ndev, "%s: Out of memory !\n",
453 caif_assert(skb != NULL);
455 skb_put_data(skb, pfrm, len);
457 skb->protocol = htons(ETH_P_CAIF);
458 skb_reset_mac_header(skb);
459 skb->dev = cfhsi->ndev;
462 * We are in a callback handler and
463 * unfortunately we don't know what context we're
471 /* Update network statistics. */
472 cfhsi->ndev->stats.rx_packets++;
473 cfhsi->ndev->stats.rx_bytes += len;
476 /* Calculate transfer length. */
477 plen = desc->cffrm_len;
478 while (nfrms < CFHSI_MAX_PKTS && *plen) {
484 /* Check for piggy-backed descriptor. */
485 if (desc->header & CFHSI_PIGGY_DESC)
486 xfer_sz += CFHSI_DESC_SZ;
488 if ((xfer_sz % 4) || (xfer_sz > (CFHSI_BUF_SZ_RX - CFHSI_DESC_SZ))) {
489 netdev_err(cfhsi->ndev,
490 "%s: Invalid payload len: %d, ignored.\n",
497 static int cfhsi_rx_desc_len(struct cfhsi_desc *desc)
503 if ((desc->header & ~CFHSI_PIGGY_DESC) ||
504 (desc->offset > CFHSI_MAX_EMB_FRM_SZ)) {
506 pr_err("Invalid descriptor. %x %x\n", desc->header,
511 /* Calculate transfer length. */
512 plen = desc->cffrm_len;
513 while (nfrms < CFHSI_MAX_PKTS && *plen) {
520 pr_err("Invalid payload len: %d, ignored.\n", xfer_sz);
526 static int cfhsi_rx_pld(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
533 /* Sanity check header and offset. */
534 if (WARN_ON((desc->header & ~CFHSI_PIGGY_DESC) ||
535 (desc->offset > CFHSI_MAX_EMB_FRM_SZ))) {
536 netdev_err(cfhsi->ndev, "%s: Invalid descriptor.\n",
541 /* Set frame pointer to start of payload. */
542 pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
543 plen = desc->cffrm_len;
545 /* Skip already processed frames. */
546 while (nfrms < cfhsi->rx_state.nfrms) {
554 while (nfrms < CFHSI_MAX_PKTS && *plen) {
559 /* CAIF frame starts after head padding. */
560 pcffrm = pfrm + *pfrm + 1;
562 /* Read length of CAIF frame (little endian). */
564 len |= ((*(pcffrm + 1)) << 8) & 0xFF00;
565 len += 2; /* Add FCS fields. */
567 /* Sanity check length of CAIF frames. */
568 if (unlikely(len > CFHSI_MAX_CAIF_FRAME_SZ)) {
569 netdev_err(cfhsi->ndev, "%s: Invalid length.\n",
574 /* Allocate SKB (OK even in IRQ context). */
575 skb = alloc_skb(len + 1, GFP_ATOMIC);
577 netdev_err(cfhsi->ndev, "%s: Out of memory !\n",
579 cfhsi->rx_state.nfrms = nfrms;
582 caif_assert(skb != NULL);
584 skb_put_data(skb, pcffrm, len);
586 skb->protocol = htons(ETH_P_CAIF);
587 skb_reset_mac_header(skb);
588 skb->dev = cfhsi->ndev;
591 * We're called in callback from HSI
592 * and don't know the context we're running in.
599 /* Update network statistics. */
600 cfhsi->ndev->stats.rx_packets++;
601 cfhsi->ndev->stats.rx_bytes += len;
612 static void cfhsi_rx_done(struct cfhsi *cfhsi)
615 int desc_pld_len = 0, rx_len, rx_state;
616 struct cfhsi_desc *desc = NULL;
618 struct cfhsi_desc *piggy_desc = NULL;
620 desc = (struct cfhsi_desc *)cfhsi->rx_buf;
622 netdev_dbg(cfhsi->ndev, "%s\n", __func__);
624 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
627 /* Update inactivity timer if pending. */
628 spin_lock_bh(&cfhsi->lock);
629 mod_timer_pending(&cfhsi->inactivity_timer,
630 jiffies + cfhsi->cfg.inactivity_timeout);
631 spin_unlock_bh(&cfhsi->lock);
633 if (cfhsi->rx_state.state == CFHSI_RX_STATE_DESC) {
634 desc_pld_len = cfhsi_rx_desc_len(desc);
636 if (desc_pld_len < 0)
639 rx_buf = cfhsi->rx_buf;
640 rx_len = desc_pld_len;
641 if (desc_pld_len > 0 && (desc->header & CFHSI_PIGGY_DESC))
642 rx_len += CFHSI_DESC_SZ;
643 if (desc_pld_len == 0)
644 rx_buf = cfhsi->rx_flip_buf;
646 rx_buf = cfhsi->rx_flip_buf;
648 rx_len = CFHSI_DESC_SZ;
649 if (cfhsi->rx_state.pld_len > 0 &&
650 (desc->header & CFHSI_PIGGY_DESC)) {
652 piggy_desc = (struct cfhsi_desc *)
653 (desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ +
654 cfhsi->rx_state.pld_len);
656 cfhsi->rx_state.piggy_desc = true;
658 /* Extract payload len from piggy-backed descriptor. */
659 desc_pld_len = cfhsi_rx_desc_len(piggy_desc);
660 if (desc_pld_len < 0)
663 if (desc_pld_len > 0) {
664 rx_len = desc_pld_len;
665 if (piggy_desc->header & CFHSI_PIGGY_DESC)
666 rx_len += CFHSI_DESC_SZ;
670 * Copy needed information from the piggy-backed
671 * descriptor to the descriptor in the start.
673 memcpy(rx_buf, (u8 *)piggy_desc,
674 CFHSI_DESC_SHORT_SZ);
679 rx_state = CFHSI_RX_STATE_PAYLOAD;
680 rx_ptr = rx_buf + CFHSI_DESC_SZ;
682 rx_state = CFHSI_RX_STATE_DESC;
684 rx_len = CFHSI_DESC_SZ;
687 /* Initiate next read */
688 if (test_bit(CFHSI_AWAKE, &cfhsi->bits)) {
689 /* Set up new transfer. */
690 netdev_dbg(cfhsi->ndev, "%s: Start RX.\n",
693 res = cfhsi->ops->cfhsi_rx(rx_ptr, rx_len,
695 if (WARN_ON(res < 0)) {
696 netdev_err(cfhsi->ndev, "%s: RX error %d.\n",
698 cfhsi->ndev->stats.rx_errors++;
699 cfhsi->ndev->stats.rx_dropped++;
703 if (cfhsi->rx_state.state == CFHSI_RX_STATE_DESC) {
704 /* Extract payload from descriptor */
705 if (cfhsi_rx_desc(desc, cfhsi) < 0)
708 /* Extract payload */
709 if (cfhsi_rx_pld(desc, cfhsi) < 0)
712 /* Extract any payload in piggyback descriptor. */
713 if (cfhsi_rx_desc(piggy_desc, cfhsi) < 0)
715 /* Mark no embedded frame after extracting it */
716 piggy_desc->offset = 0;
720 /* Update state info */
721 memset(&cfhsi->rx_state, 0, sizeof(cfhsi->rx_state));
722 cfhsi->rx_state.state = rx_state;
723 cfhsi->rx_ptr = rx_ptr;
724 cfhsi->rx_len = rx_len;
725 cfhsi->rx_state.pld_len = desc_pld_len;
726 cfhsi->rx_state.piggy_desc = desc->header & CFHSI_PIGGY_DESC;
728 if (rx_buf != cfhsi->rx_buf)
729 swap(cfhsi->rx_buf, cfhsi->rx_flip_buf);
733 netdev_err(cfhsi->ndev, "%s: Out of sync.\n", __func__);
734 print_hex_dump_bytes("--> ", DUMP_PREFIX_NONE,
735 cfhsi->rx_buf, CFHSI_DESC_SZ);
736 schedule_work(&cfhsi->out_of_sync_work);
739 static void cfhsi_rx_slowpath(struct timer_list *t)
741 struct cfhsi *cfhsi = from_timer(cfhsi, t, rx_slowpath_timer);
743 netdev_dbg(cfhsi->ndev, "%s.\n",
746 cfhsi_rx_done(cfhsi);
749 static void cfhsi_rx_done_cb(struct cfhsi_cb_ops *cb_ops)
753 cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
754 netdev_dbg(cfhsi->ndev, "%s.\n",
757 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
760 if (test_and_clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits))
761 wake_up_interruptible(&cfhsi->flush_fifo_wait);
763 cfhsi_rx_done(cfhsi);
766 static void cfhsi_wake_up(struct work_struct *work)
768 struct cfhsi *cfhsi = NULL;
773 cfhsi = container_of(work, struct cfhsi, wake_up_work);
775 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
778 if (unlikely(test_bit(CFHSI_AWAKE, &cfhsi->bits))) {
779 /* It happenes when wakeup is requested by
780 * both ends at the same time. */
781 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
782 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
786 /* Activate wake line. */
787 cfhsi->ops->cfhsi_wake_up(cfhsi->ops);
789 netdev_dbg(cfhsi->ndev, "%s: Start waiting.\n",
792 /* Wait for acknowledge. */
793 ret = CFHSI_WAKE_TOUT;
794 ret = wait_event_interruptible_timeout(cfhsi->wake_up_wait,
795 test_and_clear_bit(CFHSI_WAKE_UP_ACK,
797 if (unlikely(ret < 0)) {
798 /* Interrupted by signal. */
799 netdev_err(cfhsi->ndev, "%s: Signalled: %ld.\n",
802 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
803 cfhsi->ops->cfhsi_wake_down(cfhsi->ops);
806 bool ca_wake = false;
807 size_t fifo_occupancy = 0;
810 netdev_dbg(cfhsi->ndev, "%s: Timeout.\n",
813 /* Check FIFO to check if modem has sent something. */
814 WARN_ON(cfhsi->ops->cfhsi_fifo_occupancy(cfhsi->ops,
817 netdev_dbg(cfhsi->ndev, "%s: Bytes in FIFO: %u.\n",
818 __func__, (unsigned) fifo_occupancy);
820 /* Check if we misssed the interrupt. */
821 WARN_ON(cfhsi->ops->cfhsi_get_peer_wake(cfhsi->ops,
825 netdev_err(cfhsi->ndev, "%s: CA Wake missed !.\n",
828 /* Clear the CFHSI_WAKE_UP_ACK bit to prevent race. */
829 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
831 /* Continue execution. */
835 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
836 cfhsi->ops->cfhsi_wake_down(cfhsi->ops);
840 netdev_dbg(cfhsi->ndev, "%s: Woken.\n",
843 /* Clear power up bit. */
844 set_bit(CFHSI_AWAKE, &cfhsi->bits);
845 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
847 /* Resume read operation. */
848 netdev_dbg(cfhsi->ndev, "%s: Start RX.\n", __func__);
849 res = cfhsi->ops->cfhsi_rx(cfhsi->rx_ptr, cfhsi->rx_len, cfhsi->ops);
851 if (WARN_ON(res < 0))
852 netdev_err(cfhsi->ndev, "%s: RX err %d.\n", __func__, res);
854 /* Clear power up acknowledment. */
855 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
857 spin_lock_bh(&cfhsi->lock);
859 /* Resume transmit if queues are not empty. */
860 if (!cfhsi_tx_queue_len(cfhsi)) {
861 netdev_dbg(cfhsi->ndev, "%s: Peer wake, start timer.\n",
863 /* Start inactivity timer. */
864 mod_timer(&cfhsi->inactivity_timer,
865 jiffies + cfhsi->cfg.inactivity_timeout);
866 spin_unlock_bh(&cfhsi->lock);
870 netdev_dbg(cfhsi->ndev, "%s: Host wake.\n",
873 spin_unlock_bh(&cfhsi->lock);
875 /* Create HSI frame. */
876 len = cfhsi_tx_frm((struct cfhsi_desc *)cfhsi->tx_buf, cfhsi);
878 if (likely(len > 0)) {
879 /* Set up new transfer. */
880 res = cfhsi->ops->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->ops);
881 if (WARN_ON(res < 0)) {
882 netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
884 cfhsi_abort_tx(cfhsi);
887 netdev_err(cfhsi->ndev,
888 "%s: Failed to create HSI frame: %d.\n",
893 static void cfhsi_wake_down(struct work_struct *work)
896 struct cfhsi *cfhsi = NULL;
897 size_t fifo_occupancy = 0;
898 int retry = CFHSI_WAKE_TOUT;
900 cfhsi = container_of(work, struct cfhsi, wake_down_work);
901 netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
903 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
906 /* Deactivate wake line. */
907 cfhsi->ops->cfhsi_wake_down(cfhsi->ops);
909 /* Wait for acknowledge. */
910 ret = CFHSI_WAKE_TOUT;
911 ret = wait_event_interruptible_timeout(cfhsi->wake_down_wait,
912 test_and_clear_bit(CFHSI_WAKE_DOWN_ACK,
915 /* Interrupted by signal. */
916 netdev_err(cfhsi->ndev, "%s: Signalled: %ld.\n",
923 netdev_err(cfhsi->ndev, "%s: Timeout.\n", __func__);
925 /* Check if we misssed the interrupt. */
926 WARN_ON(cfhsi->ops->cfhsi_get_peer_wake(cfhsi->ops,
929 netdev_err(cfhsi->ndev, "%s: CA Wake missed !.\n",
933 /* Check FIFO occupancy. */
935 WARN_ON(cfhsi->ops->cfhsi_fifo_occupancy(cfhsi->ops,
941 set_current_state(TASK_INTERRUPTIBLE);
947 netdev_err(cfhsi->ndev, "%s: FIFO Timeout.\n", __func__);
949 /* Clear AWAKE condition. */
950 clear_bit(CFHSI_AWAKE, &cfhsi->bits);
952 /* Cancel pending RX requests. */
953 cfhsi->ops->cfhsi_rx_cancel(cfhsi->ops);
956 static void cfhsi_out_of_sync(struct work_struct *work)
958 struct cfhsi *cfhsi = NULL;
960 cfhsi = container_of(work, struct cfhsi, out_of_sync_work);
963 dev_close(cfhsi->ndev);
967 static void cfhsi_wake_up_cb(struct cfhsi_cb_ops *cb_ops)
969 struct cfhsi *cfhsi = NULL;
971 cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
972 netdev_dbg(cfhsi->ndev, "%s.\n",
975 set_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
976 wake_up_interruptible(&cfhsi->wake_up_wait);
978 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
981 /* Schedule wake up work queue if the peer initiates. */
982 if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
983 queue_work(cfhsi->wq, &cfhsi->wake_up_work);
986 static void cfhsi_wake_down_cb(struct cfhsi_cb_ops *cb_ops)
988 struct cfhsi *cfhsi = NULL;
990 cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
991 netdev_dbg(cfhsi->ndev, "%s.\n",
994 /* Initiating low power is only permitted by the host (us). */
995 set_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
996 wake_up_interruptible(&cfhsi->wake_down_wait);
999 static void cfhsi_aggregation_tout(struct timer_list *t)
1001 struct cfhsi *cfhsi = from_timer(cfhsi, t, aggregation_timer);
1003 netdev_dbg(cfhsi->ndev, "%s.\n",
1006 cfhsi_start_tx(cfhsi);
1009 static int cfhsi_xmit(struct sk_buff *skb, struct net_device *dev)
1011 struct cfhsi *cfhsi = NULL;
1019 cfhsi = netdev_priv(dev);
1021 switch (skb->priority) {
1022 case TC_PRIO_BESTEFFORT:
1023 case TC_PRIO_FILLER:
1025 prio = CFHSI_PRIO_BEBK;
1027 case TC_PRIO_INTERACTIVE_BULK:
1028 prio = CFHSI_PRIO_VI;
1030 case TC_PRIO_INTERACTIVE:
1031 prio = CFHSI_PRIO_VO;
1033 case TC_PRIO_CONTROL:
1035 prio = CFHSI_PRIO_CTL;
1039 spin_lock_bh(&cfhsi->lock);
1041 /* Update aggregation statistics */
1042 cfhsi_update_aggregation_stats(cfhsi, skb, 1);
1045 skb_queue_tail(&cfhsi->qhead[prio], skb);
1047 /* Sanity check; xmit should not be called after unregister_netdev */
1048 if (WARN_ON(test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))) {
1049 spin_unlock_bh(&cfhsi->lock);
1050 cfhsi_abort_tx(cfhsi);
1054 /* Send flow off if number of packets is above high water mark. */
1055 if (!cfhsi->flow_off_sent &&
1056 cfhsi_tx_queue_len(cfhsi) > cfhsi->cfg.q_high_mark &&
1057 cfhsi->cfdev.flowctrl) {
1058 cfhsi->flow_off_sent = 1;
1059 cfhsi->cfdev.flowctrl(cfhsi->ndev, OFF);
1062 if (cfhsi->tx_state == CFHSI_TX_STATE_IDLE) {
1063 cfhsi->tx_state = CFHSI_TX_STATE_XFER;
1068 /* Send aggregate if it is possible */
1069 bool aggregate_ready =
1070 cfhsi_can_send_aggregate(cfhsi) &&
1071 del_timer(&cfhsi->aggregation_timer) > 0;
1072 spin_unlock_bh(&cfhsi->lock);
1073 if (aggregate_ready)
1074 cfhsi_start_tx(cfhsi);
1078 /* Delete inactivity timer if started. */
1079 timer_active = del_timer_sync(&cfhsi->inactivity_timer);
1081 spin_unlock_bh(&cfhsi->lock);
1084 struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
1088 /* Create HSI frame. */
1089 len = cfhsi_tx_frm(desc, cfhsi);
1092 /* Set up new transfer. */
1093 res = cfhsi->ops->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->ops);
1094 if (WARN_ON(res < 0)) {
1095 netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
1097 cfhsi_abort_tx(cfhsi);
1100 /* Schedule wake up work queue if the we initiate. */
1101 if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
1102 queue_work(cfhsi->wq, &cfhsi->wake_up_work);
1108 static const struct net_device_ops cfhsi_netdevops;
1110 static void cfhsi_setup(struct net_device *dev)
1113 struct cfhsi *cfhsi = netdev_priv(dev);
1115 dev->type = ARPHRD_CAIF;
1116 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
1117 dev->mtu = CFHSI_MAX_CAIF_FRAME_SZ;
1118 dev->priv_flags |= IFF_NO_QUEUE;
1119 dev->needs_free_netdev = true;
1120 dev->netdev_ops = &cfhsi_netdevops;
1121 for (i = 0; i < CFHSI_PRIO_LAST; ++i)
1122 skb_queue_head_init(&cfhsi->qhead[i]);
1123 cfhsi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
1124 cfhsi->cfdev.use_frag = false;
1125 cfhsi->cfdev.use_stx = false;
1126 cfhsi->cfdev.use_fcs = false;
1128 cfhsi->cfg = hsi_default_config;
1131 static int cfhsi_open(struct net_device *ndev)
1133 struct cfhsi *cfhsi = netdev_priv(ndev);
1136 clear_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
1138 /* Initialize state vaiables. */
1139 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
1140 cfhsi->rx_state.state = CFHSI_RX_STATE_DESC;
1143 cfhsi->flow_off_sent = 0;
1146 * Allocate a TX buffer with the size of a HSI packet descriptors
1147 * and the necessary room for CAIF payload frames.
1149 cfhsi->tx_buf = kzalloc(CFHSI_BUF_SZ_TX, GFP_KERNEL);
1150 if (!cfhsi->tx_buf) {
1156 * Allocate a RX buffer with the size of two HSI packet descriptors and
1157 * the necessary room for CAIF payload frames.
1159 cfhsi->rx_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
1160 if (!cfhsi->rx_buf) {
1165 cfhsi->rx_flip_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
1166 if (!cfhsi->rx_flip_buf) {
1168 goto err_alloc_rx_flip;
1171 /* Initialize aggregation timeout */
1172 cfhsi->cfg.aggregation_timeout = hsi_default_config.aggregation_timeout;
1174 /* Initialize recieve vaiables. */
1175 cfhsi->rx_ptr = cfhsi->rx_buf;
1176 cfhsi->rx_len = CFHSI_DESC_SZ;
1178 /* Initialize spin locks. */
1179 spin_lock_init(&cfhsi->lock);
1181 /* Set up the driver. */
1182 cfhsi->cb_ops.tx_done_cb = cfhsi_tx_done_cb;
1183 cfhsi->cb_ops.rx_done_cb = cfhsi_rx_done_cb;
1184 cfhsi->cb_ops.wake_up_cb = cfhsi_wake_up_cb;
1185 cfhsi->cb_ops.wake_down_cb = cfhsi_wake_down_cb;
1187 /* Initialize the work queues. */
1188 INIT_WORK(&cfhsi->wake_up_work, cfhsi_wake_up);
1189 INIT_WORK(&cfhsi->wake_down_work, cfhsi_wake_down);
1190 INIT_WORK(&cfhsi->out_of_sync_work, cfhsi_out_of_sync);
1192 /* Clear all bit fields. */
1193 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
1194 clear_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
1195 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
1196 clear_bit(CFHSI_AWAKE, &cfhsi->bits);
1198 /* Create work thread. */
1199 cfhsi->wq = alloc_ordered_workqueue(cfhsi->ndev->name, WQ_MEM_RECLAIM);
1201 netdev_err(cfhsi->ndev, "%s: Failed to create work queue.\n",
1207 /* Initialize wait queues. */
1208 init_waitqueue_head(&cfhsi->wake_up_wait);
1209 init_waitqueue_head(&cfhsi->wake_down_wait);
1210 init_waitqueue_head(&cfhsi->flush_fifo_wait);
1212 /* Setup the inactivity timer. */
1213 timer_setup(&cfhsi->inactivity_timer, cfhsi_inactivity_tout, 0);
1214 /* Setup the slowpath RX timer. */
1215 timer_setup(&cfhsi->rx_slowpath_timer, cfhsi_rx_slowpath, 0);
1216 /* Setup the aggregation timer. */
1217 timer_setup(&cfhsi->aggregation_timer, cfhsi_aggregation_tout, 0);
1219 /* Activate HSI interface. */
1220 res = cfhsi->ops->cfhsi_up(cfhsi->ops);
1222 netdev_err(cfhsi->ndev,
1223 "%s: can't activate HSI interface: %d.\n",
1229 res = cfhsi_flush_fifo(cfhsi);
1231 netdev_err(cfhsi->ndev, "%s: Can't flush FIFO: %d.\n",
1238 cfhsi->ops->cfhsi_down(cfhsi->ops);
1240 destroy_workqueue(cfhsi->wq);
1242 kfree(cfhsi->rx_flip_buf);
1244 kfree(cfhsi->rx_buf);
1246 kfree(cfhsi->tx_buf);
1251 static int cfhsi_close(struct net_device *ndev)
1253 struct cfhsi *cfhsi = netdev_priv(ndev);
1254 u8 *tx_buf, *rx_buf, *flip_buf;
1256 /* going to shutdown driver */
1257 set_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
1259 /* Delete timers if pending */
1260 del_timer_sync(&cfhsi->inactivity_timer);
1261 del_timer_sync(&cfhsi->rx_slowpath_timer);
1262 del_timer_sync(&cfhsi->aggregation_timer);
1264 /* Cancel pending RX request (if any) */
1265 cfhsi->ops->cfhsi_rx_cancel(cfhsi->ops);
1267 /* Destroy workqueue */
1268 destroy_workqueue(cfhsi->wq);
1270 /* Store bufferes: will be freed later. */
1271 tx_buf = cfhsi->tx_buf;
1272 rx_buf = cfhsi->rx_buf;
1273 flip_buf = cfhsi->rx_flip_buf;
1274 /* Flush transmit queues. */
1275 cfhsi_abort_tx(cfhsi);
1277 /* Deactivate interface */
1278 cfhsi->ops->cfhsi_down(cfhsi->ops);
1287 static void cfhsi_uninit(struct net_device *dev)
1289 struct cfhsi *cfhsi = netdev_priv(dev);
1291 symbol_put(cfhsi_get_device);
1292 list_del(&cfhsi->list);
1295 static const struct net_device_ops cfhsi_netdevops = {
1296 .ndo_uninit = cfhsi_uninit,
1297 .ndo_open = cfhsi_open,
1298 .ndo_stop = cfhsi_close,
1299 .ndo_start_xmit = cfhsi_xmit
1302 static void cfhsi_netlink_parms(struct nlattr *data[], struct cfhsi *cfhsi)
1307 pr_debug("no params data found\n");
1311 i = __IFLA_CAIF_HSI_INACTIVITY_TOUT;
1313 * Inactivity timeout in millisecs. Lowest possible value is 1,
1314 * and highest possible is NEXT_TIMER_MAX_DELTA.
1317 u32 inactivity_timeout = nla_get_u32(data[i]);
1318 /* Pre-calculate inactivity timeout. */
1319 cfhsi->cfg.inactivity_timeout = inactivity_timeout * HZ / 1000;
1320 if (cfhsi->cfg.inactivity_timeout == 0)
1321 cfhsi->cfg.inactivity_timeout = 1;
1322 else if (cfhsi->cfg.inactivity_timeout > NEXT_TIMER_MAX_DELTA)
1323 cfhsi->cfg.inactivity_timeout = NEXT_TIMER_MAX_DELTA;
1326 i = __IFLA_CAIF_HSI_AGGREGATION_TOUT;
1328 cfhsi->cfg.aggregation_timeout = nla_get_u32(data[i]);
1330 i = __IFLA_CAIF_HSI_HEAD_ALIGN;
1332 cfhsi->cfg.head_align = nla_get_u32(data[i]);
1334 i = __IFLA_CAIF_HSI_TAIL_ALIGN;
1336 cfhsi->cfg.tail_align = nla_get_u32(data[i]);
1338 i = __IFLA_CAIF_HSI_QHIGH_WATERMARK;
1340 cfhsi->cfg.q_high_mark = nla_get_u32(data[i]);
1342 i = __IFLA_CAIF_HSI_QLOW_WATERMARK;
1344 cfhsi->cfg.q_low_mark = nla_get_u32(data[i]);
1347 static int caif_hsi_changelink(struct net_device *dev, struct nlattr *tb[],
1348 struct nlattr *data[],
1349 struct netlink_ext_ack *extack)
1351 cfhsi_netlink_parms(data, netdev_priv(dev));
1352 netdev_state_change(dev);
1356 static const struct nla_policy caif_hsi_policy[__IFLA_CAIF_HSI_MAX + 1] = {
1357 [__IFLA_CAIF_HSI_INACTIVITY_TOUT] = { .type = NLA_U32, .len = 4 },
1358 [__IFLA_CAIF_HSI_AGGREGATION_TOUT] = { .type = NLA_U32, .len = 4 },
1359 [__IFLA_CAIF_HSI_HEAD_ALIGN] = { .type = NLA_U32, .len = 4 },
1360 [__IFLA_CAIF_HSI_TAIL_ALIGN] = { .type = NLA_U32, .len = 4 },
1361 [__IFLA_CAIF_HSI_QHIGH_WATERMARK] = { .type = NLA_U32, .len = 4 },
1362 [__IFLA_CAIF_HSI_QLOW_WATERMARK] = { .type = NLA_U32, .len = 4 },
1365 static size_t caif_hsi_get_size(const struct net_device *dev)
1369 for (i = __IFLA_CAIF_HSI_UNSPEC + 1; i < __IFLA_CAIF_HSI_MAX; i++)
1370 s += nla_total_size(caif_hsi_policy[i].len);
1374 static int caif_hsi_fill_info(struct sk_buff *skb, const struct net_device *dev)
1376 struct cfhsi *cfhsi = netdev_priv(dev);
1378 if (nla_put_u32(skb, __IFLA_CAIF_HSI_INACTIVITY_TOUT,
1379 cfhsi->cfg.inactivity_timeout) ||
1380 nla_put_u32(skb, __IFLA_CAIF_HSI_AGGREGATION_TOUT,
1381 cfhsi->cfg.aggregation_timeout) ||
1382 nla_put_u32(skb, __IFLA_CAIF_HSI_HEAD_ALIGN,
1383 cfhsi->cfg.head_align) ||
1384 nla_put_u32(skb, __IFLA_CAIF_HSI_TAIL_ALIGN,
1385 cfhsi->cfg.tail_align) ||
1386 nla_put_u32(skb, __IFLA_CAIF_HSI_QHIGH_WATERMARK,
1387 cfhsi->cfg.q_high_mark) ||
1388 nla_put_u32(skb, __IFLA_CAIF_HSI_QLOW_WATERMARK,
1389 cfhsi->cfg.q_low_mark))
1395 static int caif_hsi_newlink(struct net *src_net, struct net_device *dev,
1396 struct nlattr *tb[], struct nlattr *data[],
1397 struct netlink_ext_ack *extack)
1399 struct cfhsi *cfhsi = NULL;
1400 struct cfhsi_ops *(*get_ops)(void);
1404 cfhsi = netdev_priv(dev);
1405 cfhsi_netlink_parms(data, cfhsi);
1407 get_ops = symbol_get(cfhsi_get_ops);
1409 pr_err("%s: failed to get the cfhsi_ops\n", __func__);
1413 /* Assign the HSI device. */
1414 cfhsi->ops = (*get_ops)();
1416 pr_err("%s: failed to get the cfhsi_ops\n", __func__);
1420 /* Assign the driver to this HSI device. */
1421 cfhsi->ops->cb_ops = &cfhsi->cb_ops;
1422 if (register_netdevice(dev)) {
1423 pr_warn("%s: caif_hsi device registration failed\n", __func__);
1426 /* Add CAIF HSI device to list. */
1427 list_add_tail(&cfhsi->list, &cfhsi_list);
1431 symbol_put(cfhsi_get_ops);
1435 static struct rtnl_link_ops caif_hsi_link_ops __read_mostly = {
1437 .priv_size = sizeof(struct cfhsi),
1438 .setup = cfhsi_setup,
1439 .maxtype = __IFLA_CAIF_HSI_MAX,
1440 .policy = caif_hsi_policy,
1441 .newlink = caif_hsi_newlink,
1442 .changelink = caif_hsi_changelink,
1443 .get_size = caif_hsi_get_size,
1444 .fill_info = caif_hsi_fill_info,
1447 static void __exit cfhsi_exit_module(void)
1449 struct list_head *list_node;
1450 struct list_head *n;
1451 struct cfhsi *cfhsi;
1453 rtnl_link_unregister(&caif_hsi_link_ops);
1456 list_for_each_safe(list_node, n, &cfhsi_list) {
1457 cfhsi = list_entry(list_node, struct cfhsi, list);
1458 unregister_netdev(cfhsi->ndev);
1463 static int __init cfhsi_init_module(void)
1465 return rtnl_link_register(&caif_hsi_link_ops);
1468 module_init(cfhsi_init_module);
1469 module_exit(cfhsi_exit_module);