1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson AB 2010
4 * Author: Daniel Martensson
5 * Dmitry.Tarnyagin / dmitry.tarnyagin@lockless.no
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;
461 netif_rx_any_context(skb);
463 /* Update network statistics. */
464 cfhsi->ndev->stats.rx_packets++;
465 cfhsi->ndev->stats.rx_bytes += len;
468 /* Calculate transfer length. */
469 plen = desc->cffrm_len;
470 while (nfrms < CFHSI_MAX_PKTS && *plen) {
476 /* Check for piggy-backed descriptor. */
477 if (desc->header & CFHSI_PIGGY_DESC)
478 xfer_sz += CFHSI_DESC_SZ;
480 if ((xfer_sz % 4) || (xfer_sz > (CFHSI_BUF_SZ_RX - CFHSI_DESC_SZ))) {
481 netdev_err(cfhsi->ndev,
482 "%s: Invalid payload len: %d, ignored.\n",
489 static int cfhsi_rx_desc_len(struct cfhsi_desc *desc)
495 if ((desc->header & ~CFHSI_PIGGY_DESC) ||
496 (desc->offset > CFHSI_MAX_EMB_FRM_SZ)) {
498 pr_err("Invalid descriptor. %x %x\n", desc->header,
503 /* Calculate transfer length. */
504 plen = desc->cffrm_len;
505 while (nfrms < CFHSI_MAX_PKTS && *plen) {
512 pr_err("Invalid payload len: %d, ignored.\n", xfer_sz);
518 static int cfhsi_rx_pld(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
525 /* Sanity check header and offset. */
526 if (WARN_ON((desc->header & ~CFHSI_PIGGY_DESC) ||
527 (desc->offset > CFHSI_MAX_EMB_FRM_SZ))) {
528 netdev_err(cfhsi->ndev, "%s: Invalid descriptor.\n",
533 /* Set frame pointer to start of payload. */
534 pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
535 plen = desc->cffrm_len;
537 /* Skip already processed frames. */
538 while (nfrms < cfhsi->rx_state.nfrms) {
546 while (nfrms < CFHSI_MAX_PKTS && *plen) {
551 /* CAIF frame starts after head padding. */
552 pcffrm = pfrm + *pfrm + 1;
554 /* Read length of CAIF frame (little endian). */
556 len |= ((*(pcffrm + 1)) << 8) & 0xFF00;
557 len += 2; /* Add FCS fields. */
559 /* Sanity check length of CAIF frames. */
560 if (unlikely(len > CFHSI_MAX_CAIF_FRAME_SZ)) {
561 netdev_err(cfhsi->ndev, "%s: Invalid length.\n",
566 /* Allocate SKB (OK even in IRQ context). */
567 skb = alloc_skb(len + 1, GFP_ATOMIC);
569 netdev_err(cfhsi->ndev, "%s: Out of memory !\n",
571 cfhsi->rx_state.nfrms = nfrms;
574 caif_assert(skb != NULL);
576 skb_put_data(skb, pcffrm, len);
578 skb->protocol = htons(ETH_P_CAIF);
579 skb_reset_mac_header(skb);
580 skb->dev = cfhsi->ndev;
582 netif_rx_any_context(skb);
584 /* Update network statistics. */
585 cfhsi->ndev->stats.rx_packets++;
586 cfhsi->ndev->stats.rx_bytes += len;
597 static void cfhsi_rx_done(struct cfhsi *cfhsi)
600 int desc_pld_len = 0, rx_len, rx_state;
601 struct cfhsi_desc *desc = NULL;
603 struct cfhsi_desc *piggy_desc = NULL;
605 desc = (struct cfhsi_desc *)cfhsi->rx_buf;
607 netdev_dbg(cfhsi->ndev, "%s\n", __func__);
609 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
612 /* Update inactivity timer if pending. */
613 spin_lock_bh(&cfhsi->lock);
614 mod_timer_pending(&cfhsi->inactivity_timer,
615 jiffies + cfhsi->cfg.inactivity_timeout);
616 spin_unlock_bh(&cfhsi->lock);
618 if (cfhsi->rx_state.state == CFHSI_RX_STATE_DESC) {
619 desc_pld_len = cfhsi_rx_desc_len(desc);
621 if (desc_pld_len < 0)
624 rx_buf = cfhsi->rx_buf;
625 rx_len = desc_pld_len;
626 if (desc_pld_len > 0 && (desc->header & CFHSI_PIGGY_DESC))
627 rx_len += CFHSI_DESC_SZ;
628 if (desc_pld_len == 0)
629 rx_buf = cfhsi->rx_flip_buf;
631 rx_buf = cfhsi->rx_flip_buf;
633 rx_len = CFHSI_DESC_SZ;
634 if (cfhsi->rx_state.pld_len > 0 &&
635 (desc->header & CFHSI_PIGGY_DESC)) {
637 piggy_desc = (struct cfhsi_desc *)
638 (desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ +
639 cfhsi->rx_state.pld_len);
641 cfhsi->rx_state.piggy_desc = true;
643 /* Extract payload len from piggy-backed descriptor. */
644 desc_pld_len = cfhsi_rx_desc_len(piggy_desc);
645 if (desc_pld_len < 0)
648 if (desc_pld_len > 0) {
649 rx_len = desc_pld_len;
650 if (piggy_desc->header & CFHSI_PIGGY_DESC)
651 rx_len += CFHSI_DESC_SZ;
655 * Copy needed information from the piggy-backed
656 * descriptor to the descriptor in the start.
658 memcpy(rx_buf, (u8 *)piggy_desc,
659 CFHSI_DESC_SHORT_SZ);
664 rx_state = CFHSI_RX_STATE_PAYLOAD;
665 rx_ptr = rx_buf + CFHSI_DESC_SZ;
667 rx_state = CFHSI_RX_STATE_DESC;
669 rx_len = CFHSI_DESC_SZ;
672 /* Initiate next read */
673 if (test_bit(CFHSI_AWAKE, &cfhsi->bits)) {
674 /* Set up new transfer. */
675 netdev_dbg(cfhsi->ndev, "%s: Start RX.\n",
678 res = cfhsi->ops->cfhsi_rx(rx_ptr, rx_len,
680 if (WARN_ON(res < 0)) {
681 netdev_err(cfhsi->ndev, "%s: RX error %d.\n",
683 cfhsi->ndev->stats.rx_errors++;
684 cfhsi->ndev->stats.rx_dropped++;
688 if (cfhsi->rx_state.state == CFHSI_RX_STATE_DESC) {
689 /* Extract payload from descriptor */
690 if (cfhsi_rx_desc(desc, cfhsi) < 0)
693 /* Extract payload */
694 if (cfhsi_rx_pld(desc, cfhsi) < 0)
697 /* Extract any payload in piggyback descriptor. */
698 if (cfhsi_rx_desc(piggy_desc, cfhsi) < 0)
700 /* Mark no embedded frame after extracting it */
701 piggy_desc->offset = 0;
705 /* Update state info */
706 memset(&cfhsi->rx_state, 0, sizeof(cfhsi->rx_state));
707 cfhsi->rx_state.state = rx_state;
708 cfhsi->rx_ptr = rx_ptr;
709 cfhsi->rx_len = rx_len;
710 cfhsi->rx_state.pld_len = desc_pld_len;
711 cfhsi->rx_state.piggy_desc = desc->header & CFHSI_PIGGY_DESC;
713 if (rx_buf != cfhsi->rx_buf)
714 swap(cfhsi->rx_buf, cfhsi->rx_flip_buf);
718 netdev_err(cfhsi->ndev, "%s: Out of sync.\n", __func__);
719 print_hex_dump_bytes("--> ", DUMP_PREFIX_NONE,
720 cfhsi->rx_buf, CFHSI_DESC_SZ);
721 schedule_work(&cfhsi->out_of_sync_work);
724 static void cfhsi_rx_slowpath(struct timer_list *t)
726 struct cfhsi *cfhsi = from_timer(cfhsi, t, rx_slowpath_timer);
728 netdev_dbg(cfhsi->ndev, "%s.\n",
731 cfhsi_rx_done(cfhsi);
734 static void cfhsi_rx_done_cb(struct cfhsi_cb_ops *cb_ops)
738 cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
739 netdev_dbg(cfhsi->ndev, "%s.\n",
742 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
745 if (test_and_clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits))
746 wake_up_interruptible(&cfhsi->flush_fifo_wait);
748 cfhsi_rx_done(cfhsi);
751 static void cfhsi_wake_up(struct work_struct *work)
753 struct cfhsi *cfhsi = NULL;
758 cfhsi = container_of(work, struct cfhsi, wake_up_work);
760 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
763 if (unlikely(test_bit(CFHSI_AWAKE, &cfhsi->bits))) {
764 /* It happenes when wakeup is requested by
765 * both ends at the same time. */
766 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
767 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
771 /* Activate wake line. */
772 cfhsi->ops->cfhsi_wake_up(cfhsi->ops);
774 netdev_dbg(cfhsi->ndev, "%s: Start waiting.\n",
777 /* Wait for acknowledge. */
778 ret = CFHSI_WAKE_TOUT;
779 ret = wait_event_interruptible_timeout(cfhsi->wake_up_wait,
780 test_and_clear_bit(CFHSI_WAKE_UP_ACK,
782 if (unlikely(ret < 0)) {
783 /* Interrupted by signal. */
784 netdev_err(cfhsi->ndev, "%s: Signalled: %ld.\n",
787 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
788 cfhsi->ops->cfhsi_wake_down(cfhsi->ops);
791 bool ca_wake = false;
792 size_t fifo_occupancy = 0;
795 netdev_dbg(cfhsi->ndev, "%s: Timeout.\n",
798 /* Check FIFO to check if modem has sent something. */
799 WARN_ON(cfhsi->ops->cfhsi_fifo_occupancy(cfhsi->ops,
802 netdev_dbg(cfhsi->ndev, "%s: Bytes in FIFO: %u.\n",
803 __func__, (unsigned) fifo_occupancy);
805 /* Check if we misssed the interrupt. */
806 WARN_ON(cfhsi->ops->cfhsi_get_peer_wake(cfhsi->ops,
810 netdev_err(cfhsi->ndev, "%s: CA Wake missed !.\n",
813 /* Clear the CFHSI_WAKE_UP_ACK bit to prevent race. */
814 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
816 /* Continue execution. */
820 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
821 cfhsi->ops->cfhsi_wake_down(cfhsi->ops);
825 netdev_dbg(cfhsi->ndev, "%s: Woken.\n",
828 /* Clear power up bit. */
829 set_bit(CFHSI_AWAKE, &cfhsi->bits);
830 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
832 /* Resume read operation. */
833 netdev_dbg(cfhsi->ndev, "%s: Start RX.\n", __func__);
834 res = cfhsi->ops->cfhsi_rx(cfhsi->rx_ptr, cfhsi->rx_len, cfhsi->ops);
836 if (WARN_ON(res < 0))
837 netdev_err(cfhsi->ndev, "%s: RX err %d.\n", __func__, res);
839 /* Clear power up acknowledment. */
840 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
842 spin_lock_bh(&cfhsi->lock);
844 /* Resume transmit if queues are not empty. */
845 if (!cfhsi_tx_queue_len(cfhsi)) {
846 netdev_dbg(cfhsi->ndev, "%s: Peer wake, start timer.\n",
848 /* Start inactivity timer. */
849 mod_timer(&cfhsi->inactivity_timer,
850 jiffies + cfhsi->cfg.inactivity_timeout);
851 spin_unlock_bh(&cfhsi->lock);
855 netdev_dbg(cfhsi->ndev, "%s: Host wake.\n",
858 spin_unlock_bh(&cfhsi->lock);
860 /* Create HSI frame. */
861 len = cfhsi_tx_frm((struct cfhsi_desc *)cfhsi->tx_buf, cfhsi);
863 if (likely(len > 0)) {
864 /* Set up new transfer. */
865 res = cfhsi->ops->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->ops);
866 if (WARN_ON(res < 0)) {
867 netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
869 cfhsi_abort_tx(cfhsi);
872 netdev_err(cfhsi->ndev,
873 "%s: Failed to create HSI frame: %d.\n",
878 static void cfhsi_wake_down(struct work_struct *work)
881 struct cfhsi *cfhsi = NULL;
882 size_t fifo_occupancy = 0;
883 int retry = CFHSI_WAKE_TOUT;
885 cfhsi = container_of(work, struct cfhsi, wake_down_work);
886 netdev_dbg(cfhsi->ndev, "%s.\n", __func__);
888 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
891 /* Deactivate wake line. */
892 cfhsi->ops->cfhsi_wake_down(cfhsi->ops);
894 /* Wait for acknowledge. */
895 ret = CFHSI_WAKE_TOUT;
896 ret = wait_event_interruptible_timeout(cfhsi->wake_down_wait,
897 test_and_clear_bit(CFHSI_WAKE_DOWN_ACK,
900 /* Interrupted by signal. */
901 netdev_err(cfhsi->ndev, "%s: Signalled: %ld.\n",
908 netdev_err(cfhsi->ndev, "%s: Timeout.\n", __func__);
910 /* Check if we misssed the interrupt. */
911 WARN_ON(cfhsi->ops->cfhsi_get_peer_wake(cfhsi->ops,
914 netdev_err(cfhsi->ndev, "%s: CA Wake missed !.\n",
918 /* Check FIFO occupancy. */
920 WARN_ON(cfhsi->ops->cfhsi_fifo_occupancy(cfhsi->ops,
926 set_current_state(TASK_INTERRUPTIBLE);
932 netdev_err(cfhsi->ndev, "%s: FIFO Timeout.\n", __func__);
934 /* Clear AWAKE condition. */
935 clear_bit(CFHSI_AWAKE, &cfhsi->bits);
937 /* Cancel pending RX requests. */
938 cfhsi->ops->cfhsi_rx_cancel(cfhsi->ops);
941 static void cfhsi_out_of_sync(struct work_struct *work)
943 struct cfhsi *cfhsi = NULL;
945 cfhsi = container_of(work, struct cfhsi, out_of_sync_work);
948 dev_close(cfhsi->ndev);
952 static void cfhsi_wake_up_cb(struct cfhsi_cb_ops *cb_ops)
954 struct cfhsi *cfhsi = NULL;
956 cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
957 netdev_dbg(cfhsi->ndev, "%s.\n",
960 set_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
961 wake_up_interruptible(&cfhsi->wake_up_wait);
963 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
966 /* Schedule wake up work queue if the peer initiates. */
967 if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
968 queue_work(cfhsi->wq, &cfhsi->wake_up_work);
971 static void cfhsi_wake_down_cb(struct cfhsi_cb_ops *cb_ops)
973 struct cfhsi *cfhsi = NULL;
975 cfhsi = container_of(cb_ops, struct cfhsi, cb_ops);
976 netdev_dbg(cfhsi->ndev, "%s.\n",
979 /* Initiating low power is only permitted by the host (us). */
980 set_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
981 wake_up_interruptible(&cfhsi->wake_down_wait);
984 static void cfhsi_aggregation_tout(struct timer_list *t)
986 struct cfhsi *cfhsi = from_timer(cfhsi, t, aggregation_timer);
988 netdev_dbg(cfhsi->ndev, "%s.\n",
991 cfhsi_start_tx(cfhsi);
994 static netdev_tx_t cfhsi_xmit(struct sk_buff *skb, struct net_device *dev)
996 struct cfhsi *cfhsi = NULL;
1004 cfhsi = netdev_priv(dev);
1006 switch (skb->priority) {
1007 case TC_PRIO_BESTEFFORT:
1008 case TC_PRIO_FILLER:
1010 prio = CFHSI_PRIO_BEBK;
1012 case TC_PRIO_INTERACTIVE_BULK:
1013 prio = CFHSI_PRIO_VI;
1015 case TC_PRIO_INTERACTIVE:
1016 prio = CFHSI_PRIO_VO;
1018 case TC_PRIO_CONTROL:
1020 prio = CFHSI_PRIO_CTL;
1024 spin_lock_bh(&cfhsi->lock);
1026 /* Update aggregation statistics */
1027 cfhsi_update_aggregation_stats(cfhsi, skb, 1);
1030 skb_queue_tail(&cfhsi->qhead[prio], skb);
1032 /* Sanity check; xmit should not be called after unregister_netdev */
1033 if (WARN_ON(test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))) {
1034 spin_unlock_bh(&cfhsi->lock);
1035 cfhsi_abort_tx(cfhsi);
1039 /* Send flow off if number of packets is above high water mark. */
1040 if (!cfhsi->flow_off_sent &&
1041 cfhsi_tx_queue_len(cfhsi) > cfhsi->cfg.q_high_mark &&
1042 cfhsi->cfdev.flowctrl) {
1043 cfhsi->flow_off_sent = 1;
1044 cfhsi->cfdev.flowctrl(cfhsi->ndev, OFF);
1047 if (cfhsi->tx_state == CFHSI_TX_STATE_IDLE) {
1048 cfhsi->tx_state = CFHSI_TX_STATE_XFER;
1053 /* Send aggregate if it is possible */
1054 bool aggregate_ready =
1055 cfhsi_can_send_aggregate(cfhsi) &&
1056 del_timer(&cfhsi->aggregation_timer) > 0;
1057 spin_unlock_bh(&cfhsi->lock);
1058 if (aggregate_ready)
1059 cfhsi_start_tx(cfhsi);
1060 return NETDEV_TX_OK;
1063 /* Delete inactivity timer if started. */
1064 timer_active = del_timer_sync(&cfhsi->inactivity_timer);
1066 spin_unlock_bh(&cfhsi->lock);
1069 struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
1073 /* Create HSI frame. */
1074 len = cfhsi_tx_frm(desc, cfhsi);
1077 /* Set up new transfer. */
1078 res = cfhsi->ops->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->ops);
1079 if (WARN_ON(res < 0)) {
1080 netdev_err(cfhsi->ndev, "%s: TX error %d.\n",
1082 cfhsi_abort_tx(cfhsi);
1085 /* Schedule wake up work queue if the we initiate. */
1086 if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
1087 queue_work(cfhsi->wq, &cfhsi->wake_up_work);
1090 return NETDEV_TX_OK;
1093 static const struct net_device_ops cfhsi_netdevops;
1095 static void cfhsi_setup(struct net_device *dev)
1098 struct cfhsi *cfhsi = netdev_priv(dev);
1100 dev->type = ARPHRD_CAIF;
1101 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
1102 dev->mtu = CFHSI_MAX_CAIF_FRAME_SZ;
1103 dev->priv_flags |= IFF_NO_QUEUE;
1104 dev->needs_free_netdev = true;
1105 dev->netdev_ops = &cfhsi_netdevops;
1106 for (i = 0; i < CFHSI_PRIO_LAST; ++i)
1107 skb_queue_head_init(&cfhsi->qhead[i]);
1108 cfhsi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
1109 cfhsi->cfdev.use_frag = false;
1110 cfhsi->cfdev.use_stx = false;
1111 cfhsi->cfdev.use_fcs = false;
1113 cfhsi->cfg = hsi_default_config;
1116 static int cfhsi_open(struct net_device *ndev)
1118 struct cfhsi *cfhsi = netdev_priv(ndev);
1121 clear_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
1123 /* Initialize state vaiables. */
1124 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
1125 cfhsi->rx_state.state = CFHSI_RX_STATE_DESC;
1128 cfhsi->flow_off_sent = 0;
1131 * Allocate a TX buffer with the size of a HSI packet descriptors
1132 * and the necessary room for CAIF payload frames.
1134 cfhsi->tx_buf = kzalloc(CFHSI_BUF_SZ_TX, GFP_KERNEL);
1135 if (!cfhsi->tx_buf) {
1141 * Allocate a RX buffer with the size of two HSI packet descriptors and
1142 * the necessary room for CAIF payload frames.
1144 cfhsi->rx_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
1145 if (!cfhsi->rx_buf) {
1150 cfhsi->rx_flip_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
1151 if (!cfhsi->rx_flip_buf) {
1153 goto err_alloc_rx_flip;
1156 /* Initialize aggregation timeout */
1157 cfhsi->cfg.aggregation_timeout = hsi_default_config.aggregation_timeout;
1159 /* Initialize recieve vaiables. */
1160 cfhsi->rx_ptr = cfhsi->rx_buf;
1161 cfhsi->rx_len = CFHSI_DESC_SZ;
1163 /* Initialize spin locks. */
1164 spin_lock_init(&cfhsi->lock);
1166 /* Set up the driver. */
1167 cfhsi->cb_ops.tx_done_cb = cfhsi_tx_done_cb;
1168 cfhsi->cb_ops.rx_done_cb = cfhsi_rx_done_cb;
1169 cfhsi->cb_ops.wake_up_cb = cfhsi_wake_up_cb;
1170 cfhsi->cb_ops.wake_down_cb = cfhsi_wake_down_cb;
1172 /* Initialize the work queues. */
1173 INIT_WORK(&cfhsi->wake_up_work, cfhsi_wake_up);
1174 INIT_WORK(&cfhsi->wake_down_work, cfhsi_wake_down);
1175 INIT_WORK(&cfhsi->out_of_sync_work, cfhsi_out_of_sync);
1177 /* Clear all bit fields. */
1178 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
1179 clear_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
1180 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
1181 clear_bit(CFHSI_AWAKE, &cfhsi->bits);
1183 /* Create work thread. */
1184 cfhsi->wq = alloc_ordered_workqueue(cfhsi->ndev->name, WQ_MEM_RECLAIM);
1186 netdev_err(cfhsi->ndev, "%s: Failed to create work queue.\n",
1192 /* Initialize wait queues. */
1193 init_waitqueue_head(&cfhsi->wake_up_wait);
1194 init_waitqueue_head(&cfhsi->wake_down_wait);
1195 init_waitqueue_head(&cfhsi->flush_fifo_wait);
1197 /* Setup the inactivity timer. */
1198 timer_setup(&cfhsi->inactivity_timer, cfhsi_inactivity_tout, 0);
1199 /* Setup the slowpath RX timer. */
1200 timer_setup(&cfhsi->rx_slowpath_timer, cfhsi_rx_slowpath, 0);
1201 /* Setup the aggregation timer. */
1202 timer_setup(&cfhsi->aggregation_timer, cfhsi_aggregation_tout, 0);
1204 /* Activate HSI interface. */
1205 res = cfhsi->ops->cfhsi_up(cfhsi->ops);
1207 netdev_err(cfhsi->ndev,
1208 "%s: can't activate HSI interface: %d.\n",
1214 res = cfhsi_flush_fifo(cfhsi);
1216 netdev_err(cfhsi->ndev, "%s: Can't flush FIFO: %d.\n",
1223 cfhsi->ops->cfhsi_down(cfhsi->ops);
1225 destroy_workqueue(cfhsi->wq);
1227 kfree(cfhsi->rx_flip_buf);
1229 kfree(cfhsi->rx_buf);
1231 kfree(cfhsi->tx_buf);
1236 static int cfhsi_close(struct net_device *ndev)
1238 struct cfhsi *cfhsi = netdev_priv(ndev);
1239 u8 *tx_buf, *rx_buf, *flip_buf;
1241 /* going to shutdown driver */
1242 set_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
1244 /* Delete timers if pending */
1245 del_timer_sync(&cfhsi->inactivity_timer);
1246 del_timer_sync(&cfhsi->rx_slowpath_timer);
1247 del_timer_sync(&cfhsi->aggregation_timer);
1249 /* Cancel pending RX request (if any) */
1250 cfhsi->ops->cfhsi_rx_cancel(cfhsi->ops);
1252 /* Destroy workqueue */
1253 destroy_workqueue(cfhsi->wq);
1255 /* Store bufferes: will be freed later. */
1256 tx_buf = cfhsi->tx_buf;
1257 rx_buf = cfhsi->rx_buf;
1258 flip_buf = cfhsi->rx_flip_buf;
1259 /* Flush transmit queues. */
1260 cfhsi_abort_tx(cfhsi);
1262 /* Deactivate interface */
1263 cfhsi->ops->cfhsi_down(cfhsi->ops);
1272 static void cfhsi_uninit(struct net_device *dev)
1274 struct cfhsi *cfhsi = netdev_priv(dev);
1276 symbol_put(cfhsi_get_device);
1277 list_del(&cfhsi->list);
1280 static const struct net_device_ops cfhsi_netdevops = {
1281 .ndo_uninit = cfhsi_uninit,
1282 .ndo_open = cfhsi_open,
1283 .ndo_stop = cfhsi_close,
1284 .ndo_start_xmit = cfhsi_xmit
1287 static void cfhsi_netlink_parms(struct nlattr *data[], struct cfhsi *cfhsi)
1292 pr_debug("no params data found\n");
1296 i = __IFLA_CAIF_HSI_INACTIVITY_TOUT;
1298 * Inactivity timeout in millisecs. Lowest possible value is 1,
1299 * and highest possible is NEXT_TIMER_MAX_DELTA.
1302 u32 inactivity_timeout = nla_get_u32(data[i]);
1303 /* Pre-calculate inactivity timeout. */
1304 cfhsi->cfg.inactivity_timeout = inactivity_timeout * HZ / 1000;
1305 if (cfhsi->cfg.inactivity_timeout == 0)
1306 cfhsi->cfg.inactivity_timeout = 1;
1307 else if (cfhsi->cfg.inactivity_timeout > NEXT_TIMER_MAX_DELTA)
1308 cfhsi->cfg.inactivity_timeout = NEXT_TIMER_MAX_DELTA;
1311 i = __IFLA_CAIF_HSI_AGGREGATION_TOUT;
1313 cfhsi->cfg.aggregation_timeout = nla_get_u32(data[i]);
1315 i = __IFLA_CAIF_HSI_HEAD_ALIGN;
1317 cfhsi->cfg.head_align = nla_get_u32(data[i]);
1319 i = __IFLA_CAIF_HSI_TAIL_ALIGN;
1321 cfhsi->cfg.tail_align = nla_get_u32(data[i]);
1323 i = __IFLA_CAIF_HSI_QHIGH_WATERMARK;
1325 cfhsi->cfg.q_high_mark = nla_get_u32(data[i]);
1327 i = __IFLA_CAIF_HSI_QLOW_WATERMARK;
1329 cfhsi->cfg.q_low_mark = nla_get_u32(data[i]);
1332 static int caif_hsi_changelink(struct net_device *dev, struct nlattr *tb[],
1333 struct nlattr *data[],
1334 struct netlink_ext_ack *extack)
1336 cfhsi_netlink_parms(data, netdev_priv(dev));
1337 netdev_state_change(dev);
1341 static const struct nla_policy caif_hsi_policy[__IFLA_CAIF_HSI_MAX + 1] = {
1342 [__IFLA_CAIF_HSI_INACTIVITY_TOUT] = { .type = NLA_U32, .len = 4 },
1343 [__IFLA_CAIF_HSI_AGGREGATION_TOUT] = { .type = NLA_U32, .len = 4 },
1344 [__IFLA_CAIF_HSI_HEAD_ALIGN] = { .type = NLA_U32, .len = 4 },
1345 [__IFLA_CAIF_HSI_TAIL_ALIGN] = { .type = NLA_U32, .len = 4 },
1346 [__IFLA_CAIF_HSI_QHIGH_WATERMARK] = { .type = NLA_U32, .len = 4 },
1347 [__IFLA_CAIF_HSI_QLOW_WATERMARK] = { .type = NLA_U32, .len = 4 },
1350 static size_t caif_hsi_get_size(const struct net_device *dev)
1354 for (i = __IFLA_CAIF_HSI_UNSPEC + 1; i < __IFLA_CAIF_HSI_MAX; i++)
1355 s += nla_total_size(caif_hsi_policy[i].len);
1359 static int caif_hsi_fill_info(struct sk_buff *skb, const struct net_device *dev)
1361 struct cfhsi *cfhsi = netdev_priv(dev);
1363 if (nla_put_u32(skb, __IFLA_CAIF_HSI_INACTIVITY_TOUT,
1364 cfhsi->cfg.inactivity_timeout) ||
1365 nla_put_u32(skb, __IFLA_CAIF_HSI_AGGREGATION_TOUT,
1366 cfhsi->cfg.aggregation_timeout) ||
1367 nla_put_u32(skb, __IFLA_CAIF_HSI_HEAD_ALIGN,
1368 cfhsi->cfg.head_align) ||
1369 nla_put_u32(skb, __IFLA_CAIF_HSI_TAIL_ALIGN,
1370 cfhsi->cfg.tail_align) ||
1371 nla_put_u32(skb, __IFLA_CAIF_HSI_QHIGH_WATERMARK,
1372 cfhsi->cfg.q_high_mark) ||
1373 nla_put_u32(skb, __IFLA_CAIF_HSI_QLOW_WATERMARK,
1374 cfhsi->cfg.q_low_mark))
1380 static int caif_hsi_newlink(struct net *src_net, struct net_device *dev,
1381 struct nlattr *tb[], struct nlattr *data[],
1382 struct netlink_ext_ack *extack)
1384 struct cfhsi *cfhsi = NULL;
1385 struct cfhsi_ops *(*get_ops)(void);
1389 cfhsi = netdev_priv(dev);
1390 cfhsi_netlink_parms(data, cfhsi);
1392 get_ops = symbol_get(cfhsi_get_ops);
1394 pr_err("%s: failed to get the cfhsi_ops\n", __func__);
1398 /* Assign the HSI device. */
1399 cfhsi->ops = (*get_ops)();
1401 pr_err("%s: failed to get the cfhsi_ops\n", __func__);
1405 /* Assign the driver to this HSI device. */
1406 cfhsi->ops->cb_ops = &cfhsi->cb_ops;
1407 if (register_netdevice(dev)) {
1408 pr_warn("%s: caif_hsi device registration failed\n", __func__);
1411 /* Add CAIF HSI device to list. */
1412 list_add_tail(&cfhsi->list, &cfhsi_list);
1416 symbol_put(cfhsi_get_ops);
1420 static struct rtnl_link_ops caif_hsi_link_ops __read_mostly = {
1422 .priv_size = sizeof(struct cfhsi),
1423 .setup = cfhsi_setup,
1424 .maxtype = __IFLA_CAIF_HSI_MAX,
1425 .policy = caif_hsi_policy,
1426 .newlink = caif_hsi_newlink,
1427 .changelink = caif_hsi_changelink,
1428 .get_size = caif_hsi_get_size,
1429 .fill_info = caif_hsi_fill_info,
1432 static void __exit cfhsi_exit_module(void)
1434 struct list_head *list_node;
1435 struct list_head *n;
1436 struct cfhsi *cfhsi;
1438 rtnl_link_unregister(&caif_hsi_link_ops);
1441 list_for_each_safe(list_node, n, &cfhsi_list) {
1442 cfhsi = list_entry(list_node, struct cfhsi, list);
1443 unregister_netdevice(cfhsi->ndev);
1448 static int __init cfhsi_init_module(void)
1450 return rtnl_link_register(&caif_hsi_link_ops);
1453 module_init(cfhsi_init_module);
1454 module_exit(cfhsi_exit_module);