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 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/platform_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/caif/caif_layer.h>
22 #include <net/caif/caif_hsi.h>
24 MODULE_LICENSE("GPL");
25 MODULE_AUTHOR("Daniel Martensson<daniel.martensson@stericsson.com>");
26 MODULE_DESCRIPTION("CAIF HSI driver");
28 /* Returns the number of padding bytes for alignment. */
29 #define PAD_POW2(x, pow) ((((x)&((pow)-1)) == 0) ? 0 :\
30 (((pow)-((x)&((pow)-1)))))
33 * HSI padding options.
34 * Warning: must be a base of 2 (& operation used) and can not be zero !
36 static int hsi_head_align = 4;
37 module_param(hsi_head_align, int, S_IRUGO);
38 MODULE_PARM_DESC(hsi_head_align, "HSI head alignment.");
40 static int hsi_tail_align = 4;
41 module_param(hsi_tail_align, int, S_IRUGO);
42 MODULE_PARM_DESC(hsi_tail_align, "HSI tail alignment.");
45 * HSI link layer flowcontrol thresholds.
46 * Warning: A high threshold value migth increase throughput but it will at
47 * the same time prevent channel prioritization and increase the risk of
48 * flooding the modem. The high threshold should be above the low.
50 static int hsi_high_threshold = 100;
51 module_param(hsi_high_threshold, int, S_IRUGO);
52 MODULE_PARM_DESC(hsi_high_threshold, "HSI high threshold (FLOW OFF).");
54 static int hsi_low_threshold = 50;
55 module_param(hsi_low_threshold, int, S_IRUGO);
56 MODULE_PARM_DESC(hsi_low_threshold, "HSI high threshold (FLOW ON).");
62 * Threshold values for the HSI packet queue. Flowcontrol will be asserted
63 * when the number of packets exceeds HIGH_WATER_MARK. It will not be
64 * de-asserted before the number of packets drops below LOW_WATER_MARK.
66 #define LOW_WATER_MARK hsi_low_threshold
67 #define HIGH_WATER_MARK hsi_high_threshold
69 static LIST_HEAD(cfhsi_list);
70 static spinlock_t cfhsi_list_lock;
72 static void cfhsi_inactivity_tout(unsigned long arg)
74 struct cfhsi *cfhsi = (struct cfhsi *)arg;
76 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
79 /* Schedule power down work queue. */
80 if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
81 queue_work(cfhsi->wq, &cfhsi->wake_down_work);
84 static void cfhsi_abort_tx(struct cfhsi *cfhsi)
89 spin_lock_bh(&cfhsi->lock);
90 skb = skb_dequeue(&cfhsi->qhead);
94 cfhsi->ndev->stats.tx_errors++;
95 cfhsi->ndev->stats.tx_dropped++;
96 spin_unlock_bh(&cfhsi->lock);
99 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
100 if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
101 mod_timer(&cfhsi->timer, jiffies + CFHSI_INACTIVITY_TOUT);
102 spin_unlock_bh(&cfhsi->lock);
105 static int cfhsi_flush_fifo(struct cfhsi *cfhsi)
107 char buffer[32]; /* Any reasonable value */
108 size_t fifo_occupancy;
111 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
115 ret = cfhsi->dev->cfhsi_wake_up(cfhsi->dev);
117 dev_warn(&cfhsi->ndev->dev,
118 "%s: can't wake up HSI interface: %d.\n",
124 ret = cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
127 dev_warn(&cfhsi->ndev->dev,
128 "%s: can't get FIFO occupancy: %d.\n",
131 } else if (!fifo_occupancy)
132 /* No more data, exitting normally */
135 fifo_occupancy = min(sizeof(buffer), fifo_occupancy);
136 set_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
137 ret = cfhsi->dev->cfhsi_rx(buffer, fifo_occupancy,
140 clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
141 dev_warn(&cfhsi->ndev->dev,
142 "%s: can't read data: %d.\n",
148 wait_event_interruptible_timeout(cfhsi->flush_fifo_wait,
149 !test_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits), ret);
152 dev_warn(&cfhsi->ndev->dev,
153 "%s: can't wait for flush complete: %d.\n",
158 dev_warn(&cfhsi->ndev->dev,
159 "%s: timeout waiting for flush complete.\n",
165 cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
170 static int cfhsi_tx_frm(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
175 u8 *pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
177 skb = skb_dequeue(&cfhsi->qhead);
184 /* Check if we can embed a CAIF frame. */
185 if (skb->len < CFHSI_MAX_EMB_FRM_SZ) {
186 struct caif_payload_info *info;
190 /* Calculate needed head alignment and tail alignment. */
191 info = (struct caif_payload_info *)&skb->cb;
193 hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
194 tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);
196 /* Check if frame still fits with added alignment. */
197 if ((skb->len + hpad + tpad) <= CFHSI_MAX_EMB_FRM_SZ) {
198 u8 *pemb = desc->emb_frm;
199 desc->offset = CFHSI_DESC_SHORT_SZ;
200 *pemb = (u8)(hpad - 1);
203 /* Update network statistics. */
204 cfhsi->ndev->stats.tx_packets++;
205 cfhsi->ndev->stats.tx_bytes += skb->len;
207 /* Copy in embedded CAIF frame. */
208 skb_copy_bits(skb, 0, pemb, skb->len);
214 /* Create payload CAIF frames. */
215 pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
216 while (nfrms < CFHSI_MAX_PKTS) {
217 struct caif_payload_info *info;
222 skb = skb_dequeue(&cfhsi->qhead);
227 /* Calculate needed head alignment and tail alignment. */
228 info = (struct caif_payload_info *)&skb->cb;
230 hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
231 tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);
233 /* Fill in CAIF frame length in descriptor. */
234 desc->cffrm_len[nfrms] = hpad + skb->len + tpad;
236 /* Fill head padding information. */
237 *pfrm = (u8)(hpad - 1);
240 /* Update network statistics. */
241 cfhsi->ndev->stats.tx_packets++;
242 cfhsi->ndev->stats.tx_bytes += skb->len;
244 /* Copy in CAIF frame. */
245 skb_copy_bits(skb, 0, pfrm, skb->len);
247 /* Update payload length. */
248 pld_len += desc->cffrm_len[nfrms];
250 /* Update frame pointer. */
251 pfrm += skb->len + tpad;
255 /* Update number of frames. */
259 /* Unused length fields should be zero-filled (according to SPEC). */
260 while (nfrms < CFHSI_MAX_PKTS) {
261 desc->cffrm_len[nfrms] = 0x0000;
265 /* Check if we can piggy-back another descriptor. */
266 skb = skb_peek(&cfhsi->qhead);
268 desc->header |= CFHSI_PIGGY_DESC;
270 desc->header &= ~CFHSI_PIGGY_DESC;
272 return CFHSI_DESC_SZ + pld_len;
275 static void cfhsi_tx_done_work(struct work_struct *work)
277 struct cfhsi *cfhsi = NULL;
278 struct cfhsi_desc *desc = NULL;
282 cfhsi = container_of(work, struct cfhsi, tx_done_work);
283 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
286 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
289 desc = (struct cfhsi_desc *)cfhsi->tx_buf;
293 * Send flow on if flow off has been previously signalled
294 * and number of packets is below low water mark.
296 spin_lock_bh(&cfhsi->lock);
297 if (cfhsi->flow_off_sent &&
298 cfhsi->qhead.qlen <= cfhsi->q_low_mark &&
299 cfhsi->cfdev.flowctrl) {
301 cfhsi->flow_off_sent = 0;
302 cfhsi->cfdev.flowctrl(cfhsi->ndev, ON);
304 spin_unlock_bh(&cfhsi->lock);
306 /* Create HSI frame. */
308 len = cfhsi_tx_frm(desc, cfhsi);
310 spin_lock_bh(&cfhsi->lock);
311 if (unlikely(skb_peek(&cfhsi->qhead))) {
312 spin_unlock_bh(&cfhsi->lock);
315 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
316 /* Start inactivity timer. */
317 mod_timer(&cfhsi->timer,
318 jiffies + CFHSI_INACTIVITY_TOUT);
319 spin_unlock_bh(&cfhsi->lock);
324 /* Set up new transfer. */
325 res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
326 if (WARN_ON(res < 0)) {
327 dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
336 static void cfhsi_tx_done_cb(struct cfhsi_drv *drv)
340 cfhsi = container_of(drv, struct cfhsi, drv);
341 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
344 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
347 queue_work(cfhsi->wq, &cfhsi->tx_done_work);
350 static int cfhsi_rx_desc(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
357 if ((desc->header & ~CFHSI_PIGGY_DESC) ||
358 (desc->offset > CFHSI_MAX_EMB_FRM_SZ)) {
359 dev_err(&cfhsi->ndev->dev, "%s: Invalid descriptor.\n",
364 /* Check for embedded CAIF frame. */
368 int len = 0, retries = 0;
369 pfrm = ((u8 *)desc) + desc->offset;
371 /* Remove offset padding. */
374 /* Read length of CAIF frame (little endian). */
376 len |= ((*(pfrm+1)) << 8) & 0xFF00;
377 len += 2; /* Add FCS fields. */
380 /* Allocate SKB (OK even in IRQ context). */
381 skb = alloc_skb(len + 1, GFP_KERNEL);
385 skb = alloc_skb(len + 1, GFP_KERNEL);
387 printk(KERN_WARNING "%s: slept for %u "
388 "before getting memory\n",
393 printk(KERN_ERR "%s: slept for 1HZ and "
394 "did not get memory\n",
396 cfhsi->ndev->stats.rx_dropped++;
400 caif_assert(skb != NULL);
402 dst = skb_put(skb, len);
403 memcpy(dst, pfrm, len);
405 skb->protocol = htons(ETH_P_CAIF);
406 skb_reset_mac_header(skb);
407 skb->dev = cfhsi->ndev;
410 * We are called from a arch specific platform device.
411 * Unfortunately we don't know what context we're
419 /* Update network statistics. */
420 cfhsi->ndev->stats.rx_packets++;
421 cfhsi->ndev->stats.rx_bytes += len;
425 /* Calculate transfer length. */
426 plen = desc->cffrm_len;
427 while (nfrms < CFHSI_MAX_PKTS && *plen) {
433 /* Check for piggy-backed descriptor. */
434 if (desc->header & CFHSI_PIGGY_DESC)
435 xfer_sz += CFHSI_DESC_SZ;
438 dev_err(&cfhsi->ndev->dev,
439 "%s: Invalid payload len: %d, ignored.\n",
447 static int cfhsi_rx_pld(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
454 /* Sanity check header and offset. */
455 if (WARN_ON((desc->header & ~CFHSI_PIGGY_DESC) ||
456 (desc->offset > CFHSI_MAX_EMB_FRM_SZ))) {
457 dev_err(&cfhsi->ndev->dev, "%s: Invalid descriptor.\n",
462 /* Set frame pointer to start of payload. */
463 pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
464 plen = desc->cffrm_len;
465 while (nfrms < CFHSI_MAX_PKTS && *plen) {
469 int len = 0, retries = 0;
471 if (WARN_ON(desc->cffrm_len[nfrms] > CFHSI_MAX_PAYLOAD_SZ)) {
472 dev_err(&cfhsi->ndev->dev, "%s: Invalid payload.\n",
477 /* CAIF frame starts after head padding. */
478 pcffrm = pfrm + *pfrm + 1;
480 /* Read length of CAIF frame (little endian). */
482 len |= ((*(pcffrm + 1)) << 8) & 0xFF00;
483 len += 2; /* Add FCS fields. */
485 /* Allocate SKB (OK even in IRQ context). */
486 skb = alloc_skb(len + 1, GFP_KERNEL);
490 skb = alloc_skb(len + 1, GFP_KERNEL);
492 printk(KERN_WARNING "%s: slept for %u "
493 "before getting memory\n",
498 printk(KERN_ERR "%s: slept for 1HZ "
499 "and did not get memory\n",
501 cfhsi->ndev->stats.rx_dropped++;
505 caif_assert(skb != NULL);
507 dst = skb_put(skb, len);
508 memcpy(dst, pcffrm, len);
510 skb->protocol = htons(ETH_P_CAIF);
511 skb_reset_mac_header(skb);
512 skb->dev = cfhsi->ndev;
515 * We're called from a platform device,
516 * and don't know the context we're running in.
523 /* Update network statistics. */
524 cfhsi->ndev->stats.rx_packets++;
525 cfhsi->ndev->stats.rx_bytes += len;
537 static void cfhsi_rx_done_work(struct work_struct *work)
540 int desc_pld_len = 0;
541 struct cfhsi *cfhsi = NULL;
542 struct cfhsi_desc *desc = NULL;
544 cfhsi = container_of(work, struct cfhsi, rx_done_work);
545 desc = (struct cfhsi_desc *)cfhsi->rx_buf;
547 dev_dbg(&cfhsi->ndev->dev, "%s: Kick timer if pending.\n",
550 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
553 /* Update inactivity timer if pending. */
554 mod_timer_pending(&cfhsi->timer, jiffies + CFHSI_INACTIVITY_TOUT);
556 if (cfhsi->rx_state == CFHSI_RX_STATE_DESC) {
557 desc_pld_len = cfhsi_rx_desc(desc, cfhsi);
561 pld_len = cfhsi_rx_pld(desc, cfhsi);
563 if ((pld_len > 0) && (desc->header & CFHSI_PIGGY_DESC)) {
564 struct cfhsi_desc *piggy_desc;
565 piggy_desc = (struct cfhsi_desc *)
566 (desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ +
569 /* Extract piggy-backed descriptor. */
570 desc_pld_len = cfhsi_rx_desc(piggy_desc, cfhsi);
573 * Copy needed information from the piggy-backed
574 * descriptor to the descriptor in the start.
576 memcpy((u8 *)desc, (u8 *)piggy_desc,
577 CFHSI_DESC_SHORT_SZ);
582 cfhsi->rx_state = CFHSI_RX_STATE_PAYLOAD;
583 cfhsi->rx_ptr = cfhsi->rx_buf + CFHSI_DESC_SZ;
584 cfhsi->rx_len = desc_pld_len;
586 cfhsi->rx_state = CFHSI_RX_STATE_DESC;
587 cfhsi->rx_ptr = cfhsi->rx_buf;
588 cfhsi->rx_len = CFHSI_DESC_SZ;
590 clear_bit(CFHSI_PENDING_RX, &cfhsi->bits);
592 if (test_bit(CFHSI_AWAKE, &cfhsi->bits)) {
593 /* Set up new transfer. */
594 dev_dbg(&cfhsi->ndev->dev, "%s: Start RX.\n",
596 res = cfhsi->dev->cfhsi_rx(cfhsi->rx_ptr, cfhsi->rx_len,
598 if (WARN_ON(res < 0)) {
599 dev_err(&cfhsi->ndev->dev, "%s: RX error %d.\n",
601 cfhsi->ndev->stats.rx_errors++;
602 cfhsi->ndev->stats.rx_dropped++;
607 static void cfhsi_rx_done_cb(struct cfhsi_drv *drv)
611 cfhsi = container_of(drv, struct cfhsi, drv);
612 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
615 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
618 set_bit(CFHSI_PENDING_RX, &cfhsi->bits);
620 if (test_and_clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits))
621 wake_up_interruptible(&cfhsi->flush_fifo_wait);
623 queue_work(cfhsi->wq, &cfhsi->rx_done_work);
626 static void cfhsi_wake_up(struct work_struct *work)
628 struct cfhsi *cfhsi = NULL;
633 cfhsi = container_of(work, struct cfhsi, wake_up_work);
635 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
638 if (unlikely(test_bit(CFHSI_AWAKE, &cfhsi->bits))) {
639 /* It happenes when wakeup is requested by
640 * both ends at the same time. */
641 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
645 /* Activate wake line. */
646 cfhsi->dev->cfhsi_wake_up(cfhsi->dev);
648 dev_dbg(&cfhsi->ndev->dev, "%s: Start waiting.\n",
651 /* Wait for acknowledge. */
652 ret = CFHSI_WAKEUP_TOUT;
653 wait_event_interruptible_timeout(cfhsi->wake_up_wait,
654 test_bit(CFHSI_WAKE_UP_ACK,
656 if (unlikely(ret < 0)) {
657 /* Interrupted by signal. */
658 dev_info(&cfhsi->ndev->dev, "%s: Signalled: %ld.\n",
660 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
661 cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
665 dev_err(&cfhsi->ndev->dev, "%s: Timeout.\n",
667 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
668 cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
671 dev_dbg(&cfhsi->ndev->dev, "%s: Woken.\n",
674 /* Clear power up bit. */
675 set_bit(CFHSI_AWAKE, &cfhsi->bits);
676 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
678 /* Resume read operation. */
679 if (!test_bit(CFHSI_PENDING_RX, &cfhsi->bits)) {
680 dev_dbg(&cfhsi->ndev->dev, "%s: Start RX.\n",
682 res = cfhsi->dev->cfhsi_rx(cfhsi->rx_ptr,
683 cfhsi->rx_len, cfhsi->dev);
684 if (WARN_ON(res < 0)) {
685 dev_err(&cfhsi->ndev->dev, "%s: RX error %d.\n",
690 /* Clear power up acknowledment. */
691 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
693 spin_lock_bh(&cfhsi->lock);
695 /* Resume transmit if queue is not empty. */
696 if (!skb_peek(&cfhsi->qhead)) {
697 dev_dbg(&cfhsi->ndev->dev, "%s: Peer wake, start timer.\n",
699 /* Start inactivity timer. */
700 mod_timer(&cfhsi->timer,
701 jiffies + CFHSI_INACTIVITY_TOUT);
702 spin_unlock_bh(&cfhsi->lock);
706 dev_dbg(&cfhsi->ndev->dev, "%s: Host wake.\n",
709 spin_unlock_bh(&cfhsi->lock);
711 /* Create HSI frame. */
712 len = cfhsi_tx_frm((struct cfhsi_desc *)cfhsi->tx_buf, cfhsi);
714 if (likely(len > 0)) {
715 /* Set up new transfer. */
716 res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
717 if (WARN_ON(res < 0)) {
718 dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
720 cfhsi_abort_tx(cfhsi);
723 dev_err(&cfhsi->ndev->dev,
724 "%s: Failed to create HSI frame: %d.\n",
730 static void cfhsi_wake_down(struct work_struct *work)
733 struct cfhsi *cfhsi = NULL;
734 size_t fifo_occupancy;
736 cfhsi = container_of(work, struct cfhsi, wake_down_work);
737 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
740 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
743 /* Check if there is something in FIFO. */
744 if (WARN_ON(cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
748 if (fifo_occupancy) {
749 dev_dbg(&cfhsi->ndev->dev,
750 "%s: %u words in RX FIFO, restart timer.\n",
751 __func__, (unsigned) fifo_occupancy);
752 spin_lock_bh(&cfhsi->lock);
753 mod_timer(&cfhsi->timer,
754 jiffies + CFHSI_INACTIVITY_TOUT);
755 spin_unlock_bh(&cfhsi->lock);
759 /* Cancel pending RX requests */
760 cfhsi->dev->cfhsi_rx_cancel(cfhsi->dev);
762 /* Deactivate wake line. */
763 cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
765 /* Wait for acknowledge. */
766 ret = CFHSI_WAKEUP_TOUT;
767 ret = wait_event_interruptible_timeout(cfhsi->wake_down_wait,
768 test_bit(CFHSI_WAKE_DOWN_ACK,
772 /* Interrupted by signal. */
773 dev_info(&cfhsi->ndev->dev, "%s: Signalled: %ld.\n",
778 dev_err(&cfhsi->ndev->dev, "%s: Timeout.\n",
782 /* Clear power down acknowledment. */
783 clear_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
784 clear_bit(CFHSI_AWAKE, &cfhsi->bits);
786 /* Check if there is something in FIFO. */
787 if (WARN_ON(cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
791 if (fifo_occupancy) {
792 dev_dbg(&cfhsi->ndev->dev,
793 "%s: %u words in RX FIFO, wakeup forced.\n",
794 __func__, (unsigned) fifo_occupancy);
795 if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
796 queue_work(cfhsi->wq, &cfhsi->wake_up_work);
798 dev_dbg(&cfhsi->ndev->dev, "%s: Done.\n",
802 static void cfhsi_wake_up_cb(struct cfhsi_drv *drv)
804 struct cfhsi *cfhsi = NULL;
806 cfhsi = container_of(drv, struct cfhsi, drv);
807 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
810 set_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
811 wake_up_interruptible(&cfhsi->wake_up_wait);
813 if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
816 /* Schedule wake up work queue if the peer initiates. */
817 if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
818 queue_work(cfhsi->wq, &cfhsi->wake_up_work);
821 static void cfhsi_wake_down_cb(struct cfhsi_drv *drv)
823 struct cfhsi *cfhsi = NULL;
825 cfhsi = container_of(drv, struct cfhsi, drv);
826 dev_dbg(&cfhsi->ndev->dev, "%s.\n",
829 /* Initiating low power is only permitted by the host (us). */
830 set_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
831 wake_up_interruptible(&cfhsi->wake_down_wait);
834 static int cfhsi_xmit(struct sk_buff *skb, struct net_device *dev)
836 struct cfhsi *cfhsi = NULL;
843 cfhsi = netdev_priv(dev);
845 spin_lock_bh(&cfhsi->lock);
847 skb_queue_tail(&cfhsi->qhead, skb);
849 /* Sanity check; xmit should not be called after unregister_netdev */
850 if (WARN_ON(test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))) {
851 spin_unlock_bh(&cfhsi->lock);
852 cfhsi_abort_tx(cfhsi);
856 /* Send flow off if number of packets is above high water mark. */
857 if (!cfhsi->flow_off_sent &&
858 cfhsi->qhead.qlen > cfhsi->q_high_mark &&
859 cfhsi->cfdev.flowctrl) {
860 cfhsi->flow_off_sent = 1;
861 cfhsi->cfdev.flowctrl(cfhsi->ndev, OFF);
864 if (cfhsi->tx_state == CFHSI_TX_STATE_IDLE) {
865 cfhsi->tx_state = CFHSI_TX_STATE_XFER;
869 spin_unlock_bh(&cfhsi->lock);
874 /* Delete inactivity timer if started. */
876 timer_active = del_timer_sync(&cfhsi->timer);
878 timer_active = del_timer(&cfhsi->timer);
879 #endif /* CONFIG_SMP */
882 struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
886 /* Create HSI frame. */
887 len = cfhsi_tx_frm(desc, cfhsi);
890 /* Set up new transfer. */
891 res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
892 if (WARN_ON(res < 0)) {
893 dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
895 cfhsi_abort_tx(cfhsi);
898 /* Schedule wake up work queue if the we initiate. */
899 if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
900 queue_work(cfhsi->wq, &cfhsi->wake_up_work);
906 static int cfhsi_open(struct net_device *dev)
908 netif_wake_queue(dev);
913 static int cfhsi_close(struct net_device *dev)
915 netif_stop_queue(dev);
920 static const struct net_device_ops cfhsi_ops = {
921 .ndo_open = cfhsi_open,
922 .ndo_stop = cfhsi_close,
923 .ndo_start_xmit = cfhsi_xmit
926 static void cfhsi_setup(struct net_device *dev)
928 struct cfhsi *cfhsi = netdev_priv(dev);
930 dev->netdev_ops = &cfhsi_ops;
931 dev->type = ARPHRD_CAIF;
932 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
933 dev->mtu = CFHSI_MAX_PAYLOAD_SZ;
934 dev->tx_queue_len = 0;
935 dev->destructor = free_netdev;
936 skb_queue_head_init(&cfhsi->qhead);
937 cfhsi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
938 cfhsi->cfdev.use_frag = false;
939 cfhsi->cfdev.use_stx = false;
940 cfhsi->cfdev.use_fcs = false;
944 int cfhsi_probe(struct platform_device *pdev)
946 struct cfhsi *cfhsi = NULL;
947 struct net_device *ndev;
948 struct cfhsi_dev *dev;
951 ndev = alloc_netdev(sizeof(struct cfhsi), "cfhsi%d", cfhsi_setup);
955 cfhsi = netdev_priv(ndev);
959 /* Initialize state vaiables. */
960 cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
961 cfhsi->rx_state = CFHSI_RX_STATE_DESC;
964 cfhsi->flow_off_sent = 0;
965 cfhsi->q_low_mark = LOW_WATER_MARK;
966 cfhsi->q_high_mark = HIGH_WATER_MARK;
968 /* Assign the HSI device. */
969 dev = (struct cfhsi_dev *)pdev->dev.platform_data;
972 /* Assign the driver to this HSI device. */
973 dev->drv = &cfhsi->drv;
976 * Allocate a TX buffer with the size of a HSI packet descriptors
977 * and the necessary room for CAIF payload frames.
979 cfhsi->tx_buf = kzalloc(CFHSI_BUF_SZ_TX, GFP_KERNEL);
980 if (!cfhsi->tx_buf) {
986 * Allocate a RX buffer with the size of two HSI packet descriptors and
987 * the necessary room for CAIF payload frames.
989 cfhsi->rx_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
990 if (!cfhsi->rx_buf) {
995 /* Initialize receive variables. */
996 cfhsi->rx_ptr = cfhsi->rx_buf;
997 cfhsi->rx_len = CFHSI_DESC_SZ;
999 /* Initialize spin locks. */
1000 spin_lock_init(&cfhsi->lock);
1002 /* Set up the driver. */
1003 cfhsi->drv.tx_done_cb = cfhsi_tx_done_cb;
1004 cfhsi->drv.rx_done_cb = cfhsi_rx_done_cb;
1005 cfhsi->drv.wake_up_cb = cfhsi_wake_up_cb;
1006 cfhsi->drv.wake_down_cb = cfhsi_wake_down_cb;
1008 /* Initialize the work queues. */
1009 INIT_WORK(&cfhsi->wake_up_work, cfhsi_wake_up);
1010 INIT_WORK(&cfhsi->wake_down_work, cfhsi_wake_down);
1011 INIT_WORK(&cfhsi->rx_done_work, cfhsi_rx_done_work);
1012 INIT_WORK(&cfhsi->tx_done_work, cfhsi_tx_done_work);
1014 /* Clear all bit fields. */
1015 clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
1016 clear_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
1017 clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
1018 clear_bit(CFHSI_AWAKE, &cfhsi->bits);
1019 clear_bit(CFHSI_PENDING_RX, &cfhsi->bits);
1021 /* Create work thread. */
1022 cfhsi->wq = create_singlethread_workqueue(pdev->name);
1024 dev_err(&ndev->dev, "%s: Failed to create work queue.\n",
1030 /* Initialize wait queues. */
1031 init_waitqueue_head(&cfhsi->wake_up_wait);
1032 init_waitqueue_head(&cfhsi->wake_down_wait);
1033 init_waitqueue_head(&cfhsi->flush_fifo_wait);
1035 /* Setup the inactivity timer. */
1036 init_timer(&cfhsi->timer);
1037 cfhsi->timer.data = (unsigned long)cfhsi;
1038 cfhsi->timer.function = cfhsi_inactivity_tout;
1040 /* Add CAIF HSI device to list. */
1041 spin_lock(&cfhsi_list_lock);
1042 list_add_tail(&cfhsi->list, &cfhsi_list);
1043 spin_unlock(&cfhsi_list_lock);
1045 /* Activate HSI interface. */
1046 res = cfhsi->dev->cfhsi_up(cfhsi->dev);
1048 dev_err(&cfhsi->ndev->dev,
1049 "%s: can't activate HSI interface: %d.\n",
1055 res = cfhsi_flush_fifo(cfhsi);
1057 dev_err(&ndev->dev, "%s: Can't flush FIFO: %d.\n",
1062 /* Register network device. */
1063 res = register_netdev(ndev);
1065 dev_err(&ndev->dev, "%s: Registration error: %d.\n",
1070 netif_stop_queue(ndev);
1075 cfhsi->dev->cfhsi_down(cfhsi->dev);
1077 destroy_workqueue(cfhsi->wq);
1079 kfree(cfhsi->rx_buf);
1081 kfree(cfhsi->tx_buf);
1088 static void cfhsi_shutdown(struct cfhsi *cfhsi, bool remove_platform_dev)
1090 u8 *tx_buf, *rx_buf;
1093 netif_tx_stop_all_queues(cfhsi->ndev);
1095 /* going to shutdown driver */
1096 set_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
1098 if (remove_platform_dev) {
1099 /* Flush workqueue */
1100 flush_workqueue(cfhsi->wq);
1102 /* Notify device. */
1103 platform_device_unregister(cfhsi->pdev);
1106 /* Flush workqueue */
1107 flush_workqueue(cfhsi->wq);
1109 /* Delete timer if pending */
1111 del_timer_sync(&cfhsi->timer);
1113 del_timer(&cfhsi->timer);
1114 #endif /* CONFIG_SMP */
1116 /* Cancel pending RX request (if any) */
1117 cfhsi->dev->cfhsi_rx_cancel(cfhsi->dev);
1119 /* Flush again and destroy workqueue */
1120 destroy_workqueue(cfhsi->wq);
1122 /* Store bufferes: will be freed later. */
1123 tx_buf = cfhsi->tx_buf;
1124 rx_buf = cfhsi->rx_buf;
1126 /* Flush transmit queues. */
1127 cfhsi_abort_tx(cfhsi);
1129 /* Deactivate interface */
1130 cfhsi->dev->cfhsi_down(cfhsi->dev);
1132 /* Finally unregister the network device. */
1133 unregister_netdev(cfhsi->ndev);
1140 int cfhsi_remove(struct platform_device *pdev)
1142 struct list_head *list_node;
1143 struct list_head *n;
1144 struct cfhsi *cfhsi = NULL;
1145 struct cfhsi_dev *dev;
1147 dev = (struct cfhsi_dev *)pdev->dev.platform_data;
1148 spin_lock(&cfhsi_list_lock);
1149 list_for_each_safe(list_node, n, &cfhsi_list) {
1150 cfhsi = list_entry(list_node, struct cfhsi, list);
1151 /* Find the corresponding device. */
1152 if (cfhsi->dev == dev) {
1153 /* Remove from list. */
1154 list_del(list_node);
1155 spin_unlock(&cfhsi_list_lock);
1157 /* Shutdown driver. */
1158 cfhsi_shutdown(cfhsi, false);
1163 spin_unlock(&cfhsi_list_lock);
1167 struct platform_driver cfhsi_plat_drv = {
1168 .probe = cfhsi_probe,
1169 .remove = cfhsi_remove,
1172 .owner = THIS_MODULE,
1176 static void __exit cfhsi_exit_module(void)
1178 struct list_head *list_node;
1179 struct list_head *n;
1180 struct cfhsi *cfhsi = NULL;
1182 spin_lock(&cfhsi_list_lock);
1183 list_for_each_safe(list_node, n, &cfhsi_list) {
1184 cfhsi = list_entry(list_node, struct cfhsi, list);
1186 /* Remove from list. */
1187 list_del(list_node);
1188 spin_unlock(&cfhsi_list_lock);
1190 /* Shutdown driver. */
1191 cfhsi_shutdown(cfhsi, true);
1193 spin_lock(&cfhsi_list_lock);
1195 spin_unlock(&cfhsi_list_lock);
1197 /* Unregister platform driver. */
1198 platform_driver_unregister(&cfhsi_plat_drv);
1201 static int __init cfhsi_init_module(void)
1205 /* Initialize spin lock. */
1206 spin_lock_init(&cfhsi_list_lock);
1208 /* Register platform driver. */
1209 result = platform_driver_register(&cfhsi_plat_drv);
1211 printk(KERN_ERR "Could not register platform HSI driver: %d.\n",
1213 goto err_dev_register;
1222 module_init(cfhsi_init_module);
1223 module_exit(cfhsi_exit_module);