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 <linux/rtnetlink.h>
22 #include <net/caif/caif_layer.h>
23 #include <net/caif/caif_hsi.h>
25 MODULE_LICENSE("GPL");
26 MODULE_AUTHOR("Daniel Martensson<daniel.martensson@stericsson.com>");
27 MODULE_DESCRIPTION("CAIF HSI driver");
29 /* Returns the number of padding bytes for alignment. */
30 #define PAD_POW2(x, pow) ((((x)&((pow)-1)) == 0) ? 0 :\
31 (((pow)-((x)&((pow)-1)))))
33 static int inactivity_timeout
= 1000;
34 module_param(inactivity_timeout
, int, S_IRUGO
| S_IWUSR
);
35 MODULE_PARM_DESC(inactivity_timeout
, "Inactivity timeout on HSI, ms.");
38 * HSI padding options.
39 * Warning: must be a base of 2 (& operation used) and can not be zero !
41 static int hsi_head_align
= 4;
42 module_param(hsi_head_align
, int, S_IRUGO
);
43 MODULE_PARM_DESC(hsi_head_align
, "HSI head alignment.");
45 static int hsi_tail_align
= 4;
46 module_param(hsi_tail_align
, int, S_IRUGO
);
47 MODULE_PARM_DESC(hsi_tail_align
, "HSI tail alignment.");
50 * HSI link layer flowcontrol thresholds.
51 * Warning: A high threshold value migth increase throughput but it will at
52 * the same time prevent channel prioritization and increase the risk of
53 * flooding the modem. The high threshold should be above the low.
55 static int hsi_high_threshold
= 100;
56 module_param(hsi_high_threshold
, int, S_IRUGO
);
57 MODULE_PARM_DESC(hsi_high_threshold
, "HSI high threshold (FLOW OFF).");
59 static int hsi_low_threshold
= 50;
60 module_param(hsi_low_threshold
, int, S_IRUGO
);
61 MODULE_PARM_DESC(hsi_low_threshold
, "HSI high threshold (FLOW ON).");
67 * Threshold values for the HSI packet queue. Flowcontrol will be asserted
68 * when the number of packets exceeds HIGH_WATER_MARK. It will not be
69 * de-asserted before the number of packets drops below LOW_WATER_MARK.
71 #define LOW_WATER_MARK hsi_low_threshold
72 #define HIGH_WATER_MARK hsi_high_threshold
74 static LIST_HEAD(cfhsi_list
);
75 static spinlock_t cfhsi_list_lock
;
77 static void cfhsi_inactivity_tout(unsigned long arg
)
79 struct cfhsi
*cfhsi
= (struct cfhsi
*)arg
;
81 dev_dbg(&cfhsi
->ndev
->dev
, "%s.\n",
84 /* Schedule power down work queue. */
85 if (!test_bit(CFHSI_SHUTDOWN
, &cfhsi
->bits
))
86 queue_work(cfhsi
->wq
, &cfhsi
->wake_down_work
);
89 static void cfhsi_abort_tx(struct cfhsi
*cfhsi
)
94 spin_lock_bh(&cfhsi
->lock
);
95 skb
= skb_dequeue(&cfhsi
->qhead
);
99 cfhsi
->ndev
->stats
.tx_errors
++;
100 cfhsi
->ndev
->stats
.tx_dropped
++;
101 spin_unlock_bh(&cfhsi
->lock
);
104 cfhsi
->tx_state
= CFHSI_TX_STATE_IDLE
;
105 if (!test_bit(CFHSI_SHUTDOWN
, &cfhsi
->bits
))
106 mod_timer(&cfhsi
->timer
,
107 jiffies
+ cfhsi
->inactivity_timeout
);
108 spin_unlock_bh(&cfhsi
->lock
);
111 static int cfhsi_flush_fifo(struct cfhsi
*cfhsi
)
113 char buffer
[32]; /* Any reasonable value */
114 size_t fifo_occupancy
;
117 dev_dbg(&cfhsi
->ndev
->dev
, "%s.\n",
121 ret
= cfhsi
->dev
->cfhsi_fifo_occupancy(cfhsi
->dev
,
124 dev_warn(&cfhsi
->ndev
->dev
,
125 "%s: can't get FIFO occupancy: %d.\n",
128 } else if (!fifo_occupancy
)
129 /* No more data, exitting normally */
132 fifo_occupancy
= min(sizeof(buffer
), fifo_occupancy
);
133 set_bit(CFHSI_FLUSH_FIFO
, &cfhsi
->bits
);
134 ret
= cfhsi
->dev
->cfhsi_rx(buffer
, fifo_occupancy
,
137 clear_bit(CFHSI_FLUSH_FIFO
, &cfhsi
->bits
);
138 dev_warn(&cfhsi
->ndev
->dev
,
139 "%s: can't read data: %d.\n",
145 ret
= wait_event_interruptible_timeout(cfhsi
->flush_fifo_wait
,
146 !test_bit(CFHSI_FLUSH_FIFO
, &cfhsi
->bits
), ret
);
149 dev_warn(&cfhsi
->ndev
->dev
,
150 "%s: can't wait for flush complete: %d.\n",
155 dev_warn(&cfhsi
->ndev
->dev
,
156 "%s: timeout waiting for flush complete.\n",
165 static int cfhsi_tx_frm(struct cfhsi_desc
*desc
, struct cfhsi
*cfhsi
)
170 u8
*pfrm
= desc
->emb_frm
+ CFHSI_MAX_EMB_FRM_SZ
;
172 skb
= skb_dequeue(&cfhsi
->qhead
);
179 /* Check if we can embed a CAIF frame. */
180 if (skb
->len
< CFHSI_MAX_EMB_FRM_SZ
) {
181 struct caif_payload_info
*info
;
185 /* Calculate needed head alignment and tail alignment. */
186 info
= (struct caif_payload_info
*)&skb
->cb
;
188 hpad
= 1 + PAD_POW2((info
->hdr_len
+ 1), hsi_head_align
);
189 tpad
= PAD_POW2((skb
->len
+ hpad
), hsi_tail_align
);
191 /* Check if frame still fits with added alignment. */
192 if ((skb
->len
+ hpad
+ tpad
) <= CFHSI_MAX_EMB_FRM_SZ
) {
193 u8
*pemb
= desc
->emb_frm
;
194 desc
->offset
= CFHSI_DESC_SHORT_SZ
;
195 *pemb
= (u8
)(hpad
- 1);
198 /* Update network statistics. */
199 cfhsi
->ndev
->stats
.tx_packets
++;
200 cfhsi
->ndev
->stats
.tx_bytes
+= skb
->len
;
202 /* Copy in embedded CAIF frame. */
203 skb_copy_bits(skb
, 0, pemb
, skb
->len
);
209 /* Create payload CAIF frames. */
210 pfrm
= desc
->emb_frm
+ CFHSI_MAX_EMB_FRM_SZ
;
211 while (nfrms
< CFHSI_MAX_PKTS
) {
212 struct caif_payload_info
*info
;
217 skb
= skb_dequeue(&cfhsi
->qhead
);
222 /* Calculate needed head alignment and tail alignment. */
223 info
= (struct caif_payload_info
*)&skb
->cb
;
225 hpad
= 1 + PAD_POW2((info
->hdr_len
+ 1), hsi_head_align
);
226 tpad
= PAD_POW2((skb
->len
+ hpad
), hsi_tail_align
);
228 /* Fill in CAIF frame length in descriptor. */
229 desc
->cffrm_len
[nfrms
] = hpad
+ skb
->len
+ tpad
;
231 /* Fill head padding information. */
232 *pfrm
= (u8
)(hpad
- 1);
235 /* Update network statistics. */
236 cfhsi
->ndev
->stats
.tx_packets
++;
237 cfhsi
->ndev
->stats
.tx_bytes
+= skb
->len
;
239 /* Copy in CAIF frame. */
240 skb_copy_bits(skb
, 0, pfrm
, skb
->len
);
242 /* Update payload length. */
243 pld_len
+= desc
->cffrm_len
[nfrms
];
245 /* Update frame pointer. */
246 pfrm
+= skb
->len
+ tpad
;
250 /* Update number of frames. */
254 /* Unused length fields should be zero-filled (according to SPEC). */
255 while (nfrms
< CFHSI_MAX_PKTS
) {
256 desc
->cffrm_len
[nfrms
] = 0x0000;
260 /* Check if we can piggy-back another descriptor. */
261 skb
= skb_peek(&cfhsi
->qhead
);
263 desc
->header
|= CFHSI_PIGGY_DESC
;
265 desc
->header
&= ~CFHSI_PIGGY_DESC
;
267 return CFHSI_DESC_SZ
+ pld_len
;
270 static void cfhsi_tx_done(struct cfhsi
*cfhsi
)
272 struct cfhsi_desc
*desc
= NULL
;
276 dev_dbg(&cfhsi
->ndev
->dev
, "%s.\n", __func__
);
278 if (test_bit(CFHSI_SHUTDOWN
, &cfhsi
->bits
))
281 desc
= (struct cfhsi_desc
*)cfhsi
->tx_buf
;
285 * Send flow on if flow off has been previously signalled
286 * and number of packets is below low water mark.
288 spin_lock_bh(&cfhsi
->lock
);
289 if (cfhsi
->flow_off_sent
&&
290 cfhsi
->qhead
.qlen
<= cfhsi
->q_low_mark
&&
291 cfhsi
->cfdev
.flowctrl
) {
293 cfhsi
->flow_off_sent
= 0;
294 cfhsi
->cfdev
.flowctrl(cfhsi
->ndev
, ON
);
296 spin_unlock_bh(&cfhsi
->lock
);
298 /* Create HSI frame. */
300 len
= cfhsi_tx_frm(desc
, cfhsi
);
302 spin_lock_bh(&cfhsi
->lock
);
303 if (unlikely(skb_peek(&cfhsi
->qhead
))) {
304 spin_unlock_bh(&cfhsi
->lock
);
307 cfhsi
->tx_state
= CFHSI_TX_STATE_IDLE
;
308 /* Start inactivity timer. */
309 mod_timer(&cfhsi
->timer
,
310 jiffies
+ cfhsi
->inactivity_timeout
);
311 spin_unlock_bh(&cfhsi
->lock
);
316 /* Set up new transfer. */
317 res
= cfhsi
->dev
->cfhsi_tx(cfhsi
->tx_buf
, len
, cfhsi
->dev
);
318 if (WARN_ON(res
< 0)) {
319 dev_err(&cfhsi
->ndev
->dev
, "%s: TX error %d.\n",
328 static void cfhsi_tx_done_cb(struct cfhsi_drv
*drv
)
332 cfhsi
= container_of(drv
, struct cfhsi
, drv
);
333 dev_dbg(&cfhsi
->ndev
->dev
, "%s.\n",
336 if (test_bit(CFHSI_SHUTDOWN
, &cfhsi
->bits
))
338 cfhsi_tx_done(cfhsi
);
341 static int cfhsi_rx_desc(struct cfhsi_desc
*desc
, struct cfhsi
*cfhsi
)
348 if ((desc
->header
& ~CFHSI_PIGGY_DESC
) ||
349 (desc
->offset
> CFHSI_MAX_EMB_FRM_SZ
)) {
350 dev_err(&cfhsi
->ndev
->dev
, "%s: Invalid descriptor.\n",
355 /* Check for embedded CAIF frame. */
360 pfrm
= ((u8
*)desc
) + desc
->offset
;
362 /* Remove offset padding. */
365 /* Read length of CAIF frame (little endian). */
367 len
|= ((*(pfrm
+1)) << 8) & 0xFF00;
368 len
+= 2; /* Add FCS fields. */
370 /* Sanity check length of CAIF frame. */
371 if (unlikely(len
> CFHSI_MAX_CAIF_FRAME_SZ
)) {
372 dev_err(&cfhsi
->ndev
->dev
, "%s: Invalid length.\n",
377 /* Allocate SKB (OK even in IRQ context). */
378 skb
= alloc_skb(len
+ 1, GFP_ATOMIC
);
380 dev_err(&cfhsi
->ndev
->dev
, "%s: Out of memory !\n",
384 caif_assert(skb
!= NULL
);
386 dst
= skb_put(skb
, len
);
387 memcpy(dst
, pfrm
, len
);
389 skb
->protocol
= htons(ETH_P_CAIF
);
390 skb_reset_mac_header(skb
);
391 skb
->dev
= cfhsi
->ndev
;
394 * We are called from a arch specific platform device.
395 * Unfortunately we don't know what context we're
403 /* Update network statistics. */
404 cfhsi
->ndev
->stats
.rx_packets
++;
405 cfhsi
->ndev
->stats
.rx_bytes
+= len
;
408 /* Calculate transfer length. */
409 plen
= desc
->cffrm_len
;
410 while (nfrms
< CFHSI_MAX_PKTS
&& *plen
) {
416 /* Check for piggy-backed descriptor. */
417 if (desc
->header
& CFHSI_PIGGY_DESC
)
418 xfer_sz
+= CFHSI_DESC_SZ
;
420 if ((xfer_sz
% 4) || (xfer_sz
> (CFHSI_BUF_SZ_RX
- CFHSI_DESC_SZ
))) {
421 dev_err(&cfhsi
->ndev
->dev
,
422 "%s: Invalid payload len: %d, ignored.\n",
429 static int cfhsi_rx_pld(struct cfhsi_desc
*desc
, struct cfhsi
*cfhsi
)
436 /* Sanity check header and offset. */
437 if (WARN_ON((desc
->header
& ~CFHSI_PIGGY_DESC
) ||
438 (desc
->offset
> CFHSI_MAX_EMB_FRM_SZ
))) {
439 dev_err(&cfhsi
->ndev
->dev
, "%s: Invalid descriptor.\n",
444 /* Set frame pointer to start of payload. */
445 pfrm
= desc
->emb_frm
+ CFHSI_MAX_EMB_FRM_SZ
;
446 plen
= desc
->cffrm_len
;
448 /* Skip already processed frames. */
449 while (nfrms
< cfhsi
->rx_state
.nfrms
) {
457 while (nfrms
< CFHSI_MAX_PKTS
&& *plen
) {
463 /* CAIF frame starts after head padding. */
464 pcffrm
= pfrm
+ *pfrm
+ 1;
466 /* Read length of CAIF frame (little endian). */
468 len
|= ((*(pcffrm
+ 1)) << 8) & 0xFF00;
469 len
+= 2; /* Add FCS fields. */
471 /* Sanity check length of CAIF frames. */
472 if (unlikely(len
> CFHSI_MAX_CAIF_FRAME_SZ
)) {
473 dev_err(&cfhsi
->ndev
->dev
, "%s: Invalid length.\n",
478 /* Allocate SKB (OK even in IRQ context). */
479 skb
= alloc_skb(len
+ 1, GFP_ATOMIC
);
481 dev_err(&cfhsi
->ndev
->dev
, "%s: Out of memory !\n",
483 cfhsi
->rx_state
.nfrms
= nfrms
;
486 caif_assert(skb
!= NULL
);
488 dst
= skb_put(skb
, len
);
489 memcpy(dst
, pcffrm
, len
);
491 skb
->protocol
= htons(ETH_P_CAIF
);
492 skb_reset_mac_header(skb
);
493 skb
->dev
= cfhsi
->ndev
;
496 * We're called from a platform device,
497 * and don't know the context we're running in.
504 /* Update network statistics. */
505 cfhsi
->ndev
->stats
.rx_packets
++;
506 cfhsi
->ndev
->stats
.rx_bytes
+= len
;
517 static void cfhsi_rx_done(struct cfhsi
*cfhsi
)
520 int desc_pld_len
= 0;
521 struct cfhsi_desc
*desc
= NULL
;
523 desc
= (struct cfhsi_desc
*)cfhsi
->rx_buf
;
525 dev_dbg(&cfhsi
->ndev
->dev
, "%s\n", __func__
);
527 if (test_bit(CFHSI_SHUTDOWN
, &cfhsi
->bits
))
530 /* Update inactivity timer if pending. */
531 spin_lock_bh(&cfhsi
->lock
);
532 mod_timer_pending(&cfhsi
->timer
,
533 jiffies
+ cfhsi
->inactivity_timeout
);
534 spin_unlock_bh(&cfhsi
->lock
);
536 if (cfhsi
->rx_state
.state
== CFHSI_RX_STATE_DESC
) {
537 desc_pld_len
= cfhsi_rx_desc(desc
, cfhsi
);
538 if (desc_pld_len
== -ENOMEM
)
540 if (desc_pld_len
== -EPROTO
)
545 if (!cfhsi
->rx_state
.piggy_desc
) {
546 pld_len
= cfhsi_rx_pld(desc
, cfhsi
);
547 if (pld_len
== -ENOMEM
)
549 if (pld_len
== -EPROTO
)
551 cfhsi
->rx_state
.pld_len
= pld_len
;
553 pld_len
= cfhsi
->rx_state
.pld_len
;
556 if ((pld_len
> 0) && (desc
->header
& CFHSI_PIGGY_DESC
)) {
557 struct cfhsi_desc
*piggy_desc
;
558 piggy_desc
= (struct cfhsi_desc
*)
559 (desc
->emb_frm
+ CFHSI_MAX_EMB_FRM_SZ
+
561 cfhsi
->rx_state
.piggy_desc
= true;
563 /* Extract piggy-backed descriptor. */
564 desc_pld_len
= cfhsi_rx_desc(piggy_desc
, cfhsi
);
565 if (desc_pld_len
== -ENOMEM
)
569 * Copy needed information from the piggy-backed
570 * descriptor to the descriptor in the start.
572 memcpy((u8
*)desc
, (u8
*)piggy_desc
,
573 CFHSI_DESC_SHORT_SZ
);
575 if (desc_pld_len
== -EPROTO
)
580 memset(&cfhsi
->rx_state
, 0, sizeof(cfhsi
->rx_state
));
582 cfhsi
->rx_state
.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
.state
= CFHSI_RX_STATE_DESC
;
587 cfhsi
->rx_ptr
= cfhsi
->rx_buf
;
588 cfhsi
->rx_len
= CFHSI_DESC_SZ
;
591 if (test_bit(CFHSI_AWAKE
, &cfhsi
->bits
)) {
592 /* Set up new transfer. */
593 dev_dbg(&cfhsi
->ndev
->dev
, "%s: Start RX.\n",
595 res
= cfhsi
->dev
->cfhsi_rx(cfhsi
->rx_ptr
, cfhsi
->rx_len
,
597 if (WARN_ON(res
< 0)) {
598 dev_err(&cfhsi
->ndev
->dev
, "%s: RX error %d.\n",
600 cfhsi
->ndev
->stats
.rx_errors
++;
601 cfhsi
->ndev
->stats
.rx_dropped
++;
607 if (++cfhsi
->rx_state
.retries
> CFHSI_MAX_RX_RETRIES
) {
608 dev_err(&cfhsi
->ndev
->dev
, "%s: No memory available "
609 "in %d iterations.\n",
610 __func__
, CFHSI_MAX_RX_RETRIES
);
613 mod_timer(&cfhsi
->rx_slowpath_timer
, jiffies
+ 1);
617 dev_err(&cfhsi
->ndev
->dev
, "%s: Out of sync.\n", __func__
);
618 print_hex_dump_bytes("--> ", DUMP_PREFIX_NONE
,
619 cfhsi
->rx_buf
, CFHSI_DESC_SZ
);
620 schedule_work(&cfhsi
->out_of_sync_work
);
623 static void cfhsi_rx_slowpath(unsigned long arg
)
625 struct cfhsi
*cfhsi
= (struct cfhsi
*)arg
;
627 dev_dbg(&cfhsi
->ndev
->dev
, "%s.\n",
630 cfhsi_rx_done(cfhsi
);
633 static void cfhsi_rx_done_cb(struct cfhsi_drv
*drv
)
637 cfhsi
= container_of(drv
, struct cfhsi
, drv
);
638 dev_dbg(&cfhsi
->ndev
->dev
, "%s.\n",
641 if (test_bit(CFHSI_SHUTDOWN
, &cfhsi
->bits
))
644 if (test_and_clear_bit(CFHSI_FLUSH_FIFO
, &cfhsi
->bits
))
645 wake_up_interruptible(&cfhsi
->flush_fifo_wait
);
647 cfhsi_rx_done(cfhsi
);
650 static void cfhsi_wake_up(struct work_struct
*work
)
652 struct cfhsi
*cfhsi
= NULL
;
657 cfhsi
= container_of(work
, struct cfhsi
, wake_up_work
);
659 if (test_bit(CFHSI_SHUTDOWN
, &cfhsi
->bits
))
662 if (unlikely(test_bit(CFHSI_AWAKE
, &cfhsi
->bits
))) {
663 /* It happenes when wakeup is requested by
664 * both ends at the same time. */
665 clear_bit(CFHSI_WAKE_UP
, &cfhsi
->bits
);
666 clear_bit(CFHSI_WAKE_UP_ACK
, &cfhsi
->bits
);
670 /* Activate wake line. */
671 cfhsi
->dev
->cfhsi_wake_up(cfhsi
->dev
);
673 dev_dbg(&cfhsi
->ndev
->dev
, "%s: Start waiting.\n",
676 /* Wait for acknowledge. */
677 ret
= CFHSI_WAKE_TOUT
;
678 ret
= wait_event_interruptible_timeout(cfhsi
->wake_up_wait
,
679 test_and_clear_bit(CFHSI_WAKE_UP_ACK
,
681 if (unlikely(ret
< 0)) {
682 /* Interrupted by signal. */
683 dev_err(&cfhsi
->ndev
->dev
, "%s: Signalled: %ld.\n",
686 clear_bit(CFHSI_WAKE_UP
, &cfhsi
->bits
);
687 cfhsi
->dev
->cfhsi_wake_down(cfhsi
->dev
);
690 bool ca_wake
= false;
691 size_t fifo_occupancy
= 0;
694 dev_err(&cfhsi
->ndev
->dev
, "%s: Timeout.\n",
697 /* Check FIFO to check if modem has sent something. */
698 WARN_ON(cfhsi
->dev
->cfhsi_fifo_occupancy(cfhsi
->dev
,
701 dev_err(&cfhsi
->ndev
->dev
, "%s: Bytes in FIFO: %u.\n",
702 __func__
, (unsigned) fifo_occupancy
);
704 /* Check if we misssed the interrupt. */
705 WARN_ON(cfhsi
->dev
->cfhsi_get_peer_wake(cfhsi
->dev
,
709 dev_err(&cfhsi
->ndev
->dev
, "%s: CA Wake missed !.\n",
712 /* Clear the CFHSI_WAKE_UP_ACK bit to prevent race. */
713 clear_bit(CFHSI_WAKE_UP_ACK
, &cfhsi
->bits
);
715 /* Continue execution. */
719 clear_bit(CFHSI_WAKE_UP
, &cfhsi
->bits
);
720 cfhsi
->dev
->cfhsi_wake_down(cfhsi
->dev
);
724 dev_dbg(&cfhsi
->ndev
->dev
, "%s: Woken.\n",
727 /* Clear power up bit. */
728 set_bit(CFHSI_AWAKE
, &cfhsi
->bits
);
729 clear_bit(CFHSI_WAKE_UP
, &cfhsi
->bits
);
731 /* Resume read operation. */
732 dev_dbg(&cfhsi
->ndev
->dev
, "%s: Start RX.\n", __func__
);
733 res
= cfhsi
->dev
->cfhsi_rx(cfhsi
->rx_ptr
, cfhsi
->rx_len
, cfhsi
->dev
);
735 if (WARN_ON(res
< 0))
736 dev_err(&cfhsi
->ndev
->dev
, "%s: RX err %d.\n", __func__
, res
);
738 /* Clear power up acknowledment. */
739 clear_bit(CFHSI_WAKE_UP_ACK
, &cfhsi
->bits
);
741 spin_lock_bh(&cfhsi
->lock
);
743 /* Resume transmit if queue is not empty. */
744 if (!skb_peek(&cfhsi
->qhead
)) {
745 dev_dbg(&cfhsi
->ndev
->dev
, "%s: Peer wake, start timer.\n",
747 /* Start inactivity timer. */
748 mod_timer(&cfhsi
->timer
,
749 jiffies
+ cfhsi
->inactivity_timeout
);
750 spin_unlock_bh(&cfhsi
->lock
);
754 dev_dbg(&cfhsi
->ndev
->dev
, "%s: Host wake.\n",
757 spin_unlock_bh(&cfhsi
->lock
);
759 /* Create HSI frame. */
760 len
= cfhsi_tx_frm((struct cfhsi_desc
*)cfhsi
->tx_buf
, cfhsi
);
762 if (likely(len
> 0)) {
763 /* Set up new transfer. */
764 res
= cfhsi
->dev
->cfhsi_tx(cfhsi
->tx_buf
, len
, cfhsi
->dev
);
765 if (WARN_ON(res
< 0)) {
766 dev_err(&cfhsi
->ndev
->dev
, "%s: TX error %d.\n",
768 cfhsi_abort_tx(cfhsi
);
771 dev_err(&cfhsi
->ndev
->dev
,
772 "%s: Failed to create HSI frame: %d.\n",
777 static void cfhsi_wake_down(struct work_struct
*work
)
780 struct cfhsi
*cfhsi
= NULL
;
781 size_t fifo_occupancy
= 0;
782 int retry
= CFHSI_WAKE_TOUT
;
784 cfhsi
= container_of(work
, struct cfhsi
, wake_down_work
);
785 dev_dbg(&cfhsi
->ndev
->dev
, "%s.\n", __func__
);
787 if (test_bit(CFHSI_SHUTDOWN
, &cfhsi
->bits
))
790 /* Deactivate wake line. */
791 cfhsi
->dev
->cfhsi_wake_down(cfhsi
->dev
);
793 /* Wait for acknowledge. */
794 ret
= CFHSI_WAKE_TOUT
;
795 ret
= wait_event_interruptible_timeout(cfhsi
->wake_down_wait
,
796 test_and_clear_bit(CFHSI_WAKE_DOWN_ACK
,
799 /* Interrupted by signal. */
800 dev_err(&cfhsi
->ndev
->dev
, "%s: Signalled: %ld.\n",
807 dev_err(&cfhsi
->ndev
->dev
, "%s: Timeout.\n", __func__
);
809 /* Check if we misssed the interrupt. */
810 WARN_ON(cfhsi
->dev
->cfhsi_get_peer_wake(cfhsi
->dev
,
813 dev_err(&cfhsi
->ndev
->dev
, "%s: CA Wake missed !.\n",
817 /* Check FIFO occupancy. */
819 WARN_ON(cfhsi
->dev
->cfhsi_fifo_occupancy(cfhsi
->dev
,
825 set_current_state(TASK_INTERRUPTIBLE
);
831 dev_err(&cfhsi
->ndev
->dev
, "%s: FIFO Timeout.\n", __func__
);
833 /* Clear AWAKE condition. */
834 clear_bit(CFHSI_AWAKE
, &cfhsi
->bits
);
836 /* Cancel pending RX requests. */
837 cfhsi
->dev
->cfhsi_rx_cancel(cfhsi
->dev
);
841 static void cfhsi_out_of_sync(struct work_struct
*work
)
843 struct cfhsi
*cfhsi
= NULL
;
845 cfhsi
= container_of(work
, struct cfhsi
, out_of_sync_work
);
848 dev_close(cfhsi
->ndev
);
852 static void cfhsi_wake_up_cb(struct cfhsi_drv
*drv
)
854 struct cfhsi
*cfhsi
= NULL
;
856 cfhsi
= container_of(drv
, struct cfhsi
, drv
);
857 dev_dbg(&cfhsi
->ndev
->dev
, "%s.\n",
860 set_bit(CFHSI_WAKE_UP_ACK
, &cfhsi
->bits
);
861 wake_up_interruptible(&cfhsi
->wake_up_wait
);
863 if (test_bit(CFHSI_SHUTDOWN
, &cfhsi
->bits
))
866 /* Schedule wake up work queue if the peer initiates. */
867 if (!test_and_set_bit(CFHSI_WAKE_UP
, &cfhsi
->bits
))
868 queue_work(cfhsi
->wq
, &cfhsi
->wake_up_work
);
871 static void cfhsi_wake_down_cb(struct cfhsi_drv
*drv
)
873 struct cfhsi
*cfhsi
= NULL
;
875 cfhsi
= container_of(drv
, struct cfhsi
, drv
);
876 dev_dbg(&cfhsi
->ndev
->dev
, "%s.\n",
879 /* Initiating low power is only permitted by the host (us). */
880 set_bit(CFHSI_WAKE_DOWN_ACK
, &cfhsi
->bits
);
881 wake_up_interruptible(&cfhsi
->wake_down_wait
);
884 static int cfhsi_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
886 struct cfhsi
*cfhsi
= NULL
;
893 cfhsi
= netdev_priv(dev
);
895 spin_lock_bh(&cfhsi
->lock
);
897 skb_queue_tail(&cfhsi
->qhead
, skb
);
899 /* Sanity check; xmit should not be called after unregister_netdev */
900 if (WARN_ON(test_bit(CFHSI_SHUTDOWN
, &cfhsi
->bits
))) {
901 spin_unlock_bh(&cfhsi
->lock
);
902 cfhsi_abort_tx(cfhsi
);
906 /* Send flow off if number of packets is above high water mark. */
907 if (!cfhsi
->flow_off_sent
&&
908 cfhsi
->qhead
.qlen
> cfhsi
->q_high_mark
&&
909 cfhsi
->cfdev
.flowctrl
) {
910 cfhsi
->flow_off_sent
= 1;
911 cfhsi
->cfdev
.flowctrl(cfhsi
->ndev
, OFF
);
914 if (cfhsi
->tx_state
== CFHSI_TX_STATE_IDLE
) {
915 cfhsi
->tx_state
= CFHSI_TX_STATE_XFER
;
920 spin_unlock_bh(&cfhsi
->lock
);
924 /* Delete inactivity timer if started. */
925 timer_active
= del_timer_sync(&cfhsi
->timer
);
927 spin_unlock_bh(&cfhsi
->lock
);
930 struct cfhsi_desc
*desc
= (struct cfhsi_desc
*)cfhsi
->tx_buf
;
934 /* Create HSI frame. */
935 len
= cfhsi_tx_frm(desc
, cfhsi
);
938 /* Set up new transfer. */
939 res
= cfhsi
->dev
->cfhsi_tx(cfhsi
->tx_buf
, len
, cfhsi
->dev
);
940 if (WARN_ON(res
< 0)) {
941 dev_err(&cfhsi
->ndev
->dev
, "%s: TX error %d.\n",
943 cfhsi_abort_tx(cfhsi
);
946 /* Schedule wake up work queue if the we initiate. */
947 if (!test_and_set_bit(CFHSI_WAKE_UP
, &cfhsi
->bits
))
948 queue_work(cfhsi
->wq
, &cfhsi
->wake_up_work
);
954 static int cfhsi_open(struct net_device
*dev
)
956 netif_wake_queue(dev
);
961 static int cfhsi_close(struct net_device
*dev
)
963 netif_stop_queue(dev
);
968 static const struct net_device_ops cfhsi_ops
= {
969 .ndo_open
= cfhsi_open
,
970 .ndo_stop
= cfhsi_close
,
971 .ndo_start_xmit
= cfhsi_xmit
974 static void cfhsi_setup(struct net_device
*dev
)
976 struct cfhsi
*cfhsi
= netdev_priv(dev
);
978 dev
->netdev_ops
= &cfhsi_ops
;
979 dev
->type
= ARPHRD_CAIF
;
980 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;
981 dev
->mtu
= CFHSI_MAX_CAIF_FRAME_SZ
;
982 dev
->tx_queue_len
= 0;
983 dev
->destructor
= free_netdev
;
984 skb_queue_head_init(&cfhsi
->qhead
);
985 cfhsi
->cfdev
.link_select
= CAIF_LINK_HIGH_BANDW
;
986 cfhsi
->cfdev
.use_frag
= false;
987 cfhsi
->cfdev
.use_stx
= false;
988 cfhsi
->cfdev
.use_fcs
= false;
992 int cfhsi_probe(struct platform_device
*pdev
)
994 struct cfhsi
*cfhsi
= NULL
;
995 struct net_device
*ndev
;
996 struct cfhsi_dev
*dev
;
999 ndev
= alloc_netdev(sizeof(struct cfhsi
), "cfhsi%d", cfhsi_setup
);
1003 cfhsi
= netdev_priv(ndev
);
1007 /* Initialize state vaiables. */
1008 cfhsi
->tx_state
= CFHSI_TX_STATE_IDLE
;
1009 cfhsi
->rx_state
.state
= CFHSI_RX_STATE_DESC
;
1012 cfhsi
->flow_off_sent
= 0;
1013 cfhsi
->q_low_mark
= LOW_WATER_MARK
;
1014 cfhsi
->q_high_mark
= HIGH_WATER_MARK
;
1016 /* Assign the HSI device. */
1017 dev
= (struct cfhsi_dev
*)pdev
->dev
.platform_data
;
1020 /* Assign the driver to this HSI device. */
1021 dev
->drv
= &cfhsi
->drv
;
1024 * Allocate a TX buffer with the size of a HSI packet descriptors
1025 * and the necessary room for CAIF payload frames.
1027 cfhsi
->tx_buf
= kzalloc(CFHSI_BUF_SZ_TX
, GFP_KERNEL
);
1028 if (!cfhsi
->tx_buf
) {
1034 * Allocate a RX buffer with the size of two HSI packet descriptors and
1035 * the necessary room for CAIF payload frames.
1037 cfhsi
->rx_buf
= kzalloc(CFHSI_BUF_SZ_RX
, GFP_KERNEL
);
1038 if (!cfhsi
->rx_buf
) {
1043 /* Pre-calculate inactivity timeout. */
1044 if (inactivity_timeout
!= -1) {
1045 cfhsi
->inactivity_timeout
=
1046 inactivity_timeout
* HZ
/ 1000;
1047 if (!cfhsi
->inactivity_timeout
)
1048 cfhsi
->inactivity_timeout
= 1;
1049 else if (cfhsi
->inactivity_timeout
> NEXT_TIMER_MAX_DELTA
)
1050 cfhsi
->inactivity_timeout
= NEXT_TIMER_MAX_DELTA
;
1052 cfhsi
->inactivity_timeout
= NEXT_TIMER_MAX_DELTA
;
1055 /* Initialize recieve vaiables. */
1056 cfhsi
->rx_ptr
= cfhsi
->rx_buf
;
1057 cfhsi
->rx_len
= CFHSI_DESC_SZ
;
1059 /* Initialize spin locks. */
1060 spin_lock_init(&cfhsi
->lock
);
1062 /* Set up the driver. */
1063 cfhsi
->drv
.tx_done_cb
= cfhsi_tx_done_cb
;
1064 cfhsi
->drv
.rx_done_cb
= cfhsi_rx_done_cb
;
1065 cfhsi
->drv
.wake_up_cb
= cfhsi_wake_up_cb
;
1066 cfhsi
->drv
.wake_down_cb
= cfhsi_wake_down_cb
;
1068 /* Initialize the work queues. */
1069 INIT_WORK(&cfhsi
->wake_up_work
, cfhsi_wake_up
);
1070 INIT_WORK(&cfhsi
->wake_down_work
, cfhsi_wake_down
);
1071 INIT_WORK(&cfhsi
->out_of_sync_work
, cfhsi_out_of_sync
);
1073 /* Clear all bit fields. */
1074 clear_bit(CFHSI_WAKE_UP_ACK
, &cfhsi
->bits
);
1075 clear_bit(CFHSI_WAKE_DOWN_ACK
, &cfhsi
->bits
);
1076 clear_bit(CFHSI_WAKE_UP
, &cfhsi
->bits
);
1077 clear_bit(CFHSI_AWAKE
, &cfhsi
->bits
);
1079 /* Create work thread. */
1080 cfhsi
->wq
= create_singlethread_workqueue(pdev
->name
);
1082 dev_err(&ndev
->dev
, "%s: Failed to create work queue.\n",
1088 /* Initialize wait queues. */
1089 init_waitqueue_head(&cfhsi
->wake_up_wait
);
1090 init_waitqueue_head(&cfhsi
->wake_down_wait
);
1091 init_waitqueue_head(&cfhsi
->flush_fifo_wait
);
1093 /* Setup the inactivity timer. */
1094 init_timer(&cfhsi
->timer
);
1095 cfhsi
->timer
.data
= (unsigned long)cfhsi
;
1096 cfhsi
->timer
.function
= cfhsi_inactivity_tout
;
1097 /* Setup the slowpath RX timer. */
1098 init_timer(&cfhsi
->rx_slowpath_timer
);
1099 cfhsi
->rx_slowpath_timer
.data
= (unsigned long)cfhsi
;
1100 cfhsi
->rx_slowpath_timer
.function
= cfhsi_rx_slowpath
;
1102 /* Add CAIF HSI device to list. */
1103 spin_lock(&cfhsi_list_lock
);
1104 list_add_tail(&cfhsi
->list
, &cfhsi_list
);
1105 spin_unlock(&cfhsi_list_lock
);
1107 /* Activate HSI interface. */
1108 res
= cfhsi
->dev
->cfhsi_up(cfhsi
->dev
);
1110 dev_err(&cfhsi
->ndev
->dev
,
1111 "%s: can't activate HSI interface: %d.\n",
1117 res
= cfhsi_flush_fifo(cfhsi
);
1119 dev_err(&ndev
->dev
, "%s: Can't flush FIFO: %d.\n",
1124 /* Register network device. */
1125 res
= register_netdev(ndev
);
1127 dev_err(&ndev
->dev
, "%s: Registration error: %d.\n",
1132 netif_stop_queue(ndev
);
1137 cfhsi
->dev
->cfhsi_down(cfhsi
->dev
);
1139 destroy_workqueue(cfhsi
->wq
);
1141 kfree(cfhsi
->rx_buf
);
1143 kfree(cfhsi
->tx_buf
);
1150 static void cfhsi_shutdown(struct cfhsi
*cfhsi
)
1152 u8
*tx_buf
, *rx_buf
;
1155 netif_tx_stop_all_queues(cfhsi
->ndev
);
1157 /* going to shutdown driver */
1158 set_bit(CFHSI_SHUTDOWN
, &cfhsi
->bits
);
1160 /* Flush workqueue */
1161 flush_workqueue(cfhsi
->wq
);
1163 /* Delete timers if pending */
1164 del_timer_sync(&cfhsi
->timer
);
1165 del_timer_sync(&cfhsi
->rx_slowpath_timer
);
1167 /* Cancel pending RX request (if any) */
1168 cfhsi
->dev
->cfhsi_rx_cancel(cfhsi
->dev
);
1170 /* Destroy workqueue */
1171 destroy_workqueue(cfhsi
->wq
);
1173 /* Store bufferes: will be freed later. */
1174 tx_buf
= cfhsi
->tx_buf
;
1175 rx_buf
= cfhsi
->rx_buf
;
1177 /* Flush transmit queues. */
1178 cfhsi_abort_tx(cfhsi
);
1180 /* Deactivate interface */
1181 cfhsi
->dev
->cfhsi_down(cfhsi
->dev
);
1183 /* Finally unregister the network device. */
1184 unregister_netdev(cfhsi
->ndev
);
1191 int cfhsi_remove(struct platform_device
*pdev
)
1193 struct list_head
*list_node
;
1194 struct list_head
*n
;
1195 struct cfhsi
*cfhsi
= NULL
;
1196 struct cfhsi_dev
*dev
;
1198 dev
= (struct cfhsi_dev
*)pdev
->dev
.platform_data
;
1199 spin_lock(&cfhsi_list_lock
);
1200 list_for_each_safe(list_node
, n
, &cfhsi_list
) {
1201 cfhsi
= list_entry(list_node
, struct cfhsi
, list
);
1202 /* Find the corresponding device. */
1203 if (cfhsi
->dev
== dev
) {
1204 /* Remove from list. */
1205 list_del(list_node
);
1206 spin_unlock(&cfhsi_list_lock
);
1208 /* Shutdown driver. */
1209 cfhsi_shutdown(cfhsi
);
1214 spin_unlock(&cfhsi_list_lock
);
1218 struct platform_driver cfhsi_plat_drv
= {
1219 .probe
= cfhsi_probe
,
1220 .remove
= cfhsi_remove
,
1223 .owner
= THIS_MODULE
,
1227 static void __exit
cfhsi_exit_module(void)
1229 struct list_head
*list_node
;
1230 struct list_head
*n
;
1231 struct cfhsi
*cfhsi
= NULL
;
1233 spin_lock(&cfhsi_list_lock
);
1234 list_for_each_safe(list_node
, n
, &cfhsi_list
) {
1235 cfhsi
= list_entry(list_node
, struct cfhsi
, list
);
1237 /* Remove from list. */
1238 list_del(list_node
);
1239 spin_unlock(&cfhsi_list_lock
);
1241 /* Shutdown driver. */
1242 cfhsi_shutdown(cfhsi
);
1244 spin_lock(&cfhsi_list_lock
);
1246 spin_unlock(&cfhsi_list_lock
);
1248 /* Unregister platform driver. */
1249 platform_driver_unregister(&cfhsi_plat_drv
);
1252 static int __init
cfhsi_init_module(void)
1256 /* Initialize spin lock. */
1257 spin_lock_init(&cfhsi_list_lock
);
1259 /* Register platform driver. */
1260 result
= platform_driver_register(&cfhsi_plat_drv
);
1262 printk(KERN_ERR
"Could not register platform HSI driver: %d.\n",
1264 goto err_dev_register
;
1273 module_init(cfhsi_init_module
);
1274 module_exit(cfhsi_exit_module
);