1 // SPDX-License-Identifier: GPL-2.0-only
5 * Implementation of the SSI McSAAB improved protocol.
7 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
8 * Copyright (C) 2013 Sebastian Reichel <sre@kernel.org>
10 * Contact: Carlos Chinea <carlos.chinea@nokia.com>
13 #include <linux/atomic.h>
14 #include <linux/clk.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/gpio.h>
18 #include <linux/if_ether.h>
19 #include <linux/if_arp.h>
20 #include <linux/if_phonet.h>
21 #include <linux/init.h>
22 #include <linux/irq.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/netdevice.h>
26 #include <linux/notifier.h>
27 #include <linux/scatterlist.h>
28 #include <linux/skbuff.h>
29 #include <linux/slab.h>
30 #include <linux/spinlock.h>
31 #include <linux/timer.h>
32 #include <linux/hsi/hsi.h>
33 #include <linux/hsi/ssi_protocol.h>
35 void ssi_waketest(struct hsi_client
*cl
, unsigned int enable
);
37 #define SSIP_TXQUEUE_LEN 100
38 #define SSIP_MAX_MTU 65535
39 #define SSIP_DEFAULT_MTU 4000
40 #define PN_MEDIA_SOS 21
41 #define SSIP_MIN_PN_HDR 6 /* FIXME: Revisit */
42 #define SSIP_WDTOUT 2000 /* FIXME: has to be 500 msecs */
43 #define SSIP_KATOUT 15 /* 15 msecs */
44 #define SSIP_MAX_CMDS 5 /* Number of pre-allocated commands buffers */
45 #define SSIP_BYTES_TO_FRAMES(x) ((((x) - 1) >> 2) + 1)
46 #define SSIP_CMT_LOADER_SYNC 0x11223344
48 * SSI protocol command definitions
50 #define SSIP_COMMAND(data) ((data) >> 28)
51 #define SSIP_PAYLOAD(data) ((data) & 0xfffffff)
53 #define SSIP_SW_BREAK 0
54 #define SSIP_BOOTINFO_REQ 1
55 #define SSIP_BOOTINFO_RESP 2
56 #define SSIP_WAKETEST_RESULT 3
57 #define SSIP_START_TRANS 4
60 #define SSIP_DATA_VERSION(data) ((data) & 0xff)
61 #define SSIP_LOCAL_VERID 1
62 #define SSIP_WAKETEST_OK 0
63 #define SSIP_WAKETEST_FAILED 1
64 #define SSIP_PDU_LENGTH(data) (((data) >> 8) & 0xffff)
65 #define SSIP_MSG_ID(data) ((data) & 0xff)
67 #define SSIP_CMD(cmd, payload) (((cmd) << 28) | ((payload) & 0xfffffff))
68 /* Commands for the control channel */
69 #define SSIP_BOOTINFO_REQ_CMD(ver) \
70 SSIP_CMD(SSIP_BOOTINFO_REQ, SSIP_DATA_VERSION(ver))
71 #define SSIP_BOOTINFO_RESP_CMD(ver) \
72 SSIP_CMD(SSIP_BOOTINFO_RESP, SSIP_DATA_VERSION(ver))
73 #define SSIP_START_TRANS_CMD(pdulen, id) \
74 SSIP_CMD(SSIP_START_TRANS, (((pdulen) << 8) | SSIP_MSG_ID(id)))
75 #define SSIP_READY_CMD SSIP_CMD(SSIP_READY, 0)
76 #define SSIP_SWBREAK_CMD SSIP_CMD(SSIP_SW_BREAK, 0)
78 #define SSIP_WAKETEST_FLAG 0
80 /* Main state machine states */
87 /* Send state machine states */
96 /* Receive state machine states */
104 * struct ssi_protocol - SSI protocol (McSAAB) data
105 * @main_state: Main state machine
106 * @send_state: TX state machine
107 * @recv_state: RX state machine
108 * @flags: Flags, currently only used to follow wake line test
111 * @txqueue_len: TX queue length
112 * @tx_wd: TX watchdog
113 * @rx_wd: RX watchdog
114 * @keep_alive: Workaround for SSI HW bug
115 * @lock: To serialize access to this struct
116 * @netdev: Phonet network device
117 * @txqueue: TX data queue
118 * @cmdqueue: Queue of free commands
119 * @cl: HSI client own reference
120 * @link: Link for ssip_list
121 * @tx_usecount: Refcount to keep track the slaves that use the wake line
122 * @channel_id_cmd: HSI channel id for command stream
123 * @channel_id_data: HSI channel id for data stream
125 struct ssi_protocol
{
126 unsigned int main_state
;
127 unsigned int send_state
;
128 unsigned int recv_state
;
132 unsigned int txqueue_len
;
133 struct timer_list tx_wd
;
134 struct timer_list rx_wd
;
135 struct timer_list keep_alive
; /* wake-up workaround */
137 struct net_device
*netdev
;
138 struct list_head txqueue
;
139 struct list_head cmdqueue
;
140 struct work_struct work
;
141 struct hsi_client
*cl
;
142 struct list_head link
;
148 /* List of ssi protocol instances */
149 static LIST_HEAD(ssip_list
);
151 static void ssip_rxcmd_complete(struct hsi_msg
*msg
);
153 static inline void ssip_set_cmd(struct hsi_msg
*msg
, u32 cmd
)
157 data
= sg_virt(msg
->sgt
.sgl
);
161 static inline u32
ssip_get_cmd(struct hsi_msg
*msg
)
165 data
= sg_virt(msg
->sgt
.sgl
);
170 static void ssip_skb_to_msg(struct sk_buff
*skb
, struct hsi_msg
*msg
)
173 struct scatterlist
*sg
;
176 BUG_ON(msg
->sgt
.nents
!= (unsigned int)(skb_shinfo(skb
)->nr_frags
+ 1));
179 sg_set_buf(sg
, skb
->data
, skb_headlen(skb
));
180 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
183 frag
= &skb_shinfo(skb
)->frags
[i
];
184 sg_set_page(sg
, skb_frag_page(frag
), skb_frag_size(frag
),
189 static void ssip_free_data(struct hsi_msg
*msg
)
194 pr_debug("free data: msg %p context %p skb %p\n", msg
, msg
->context
,
196 msg
->destructor
= NULL
;
201 static struct hsi_msg
*ssip_alloc_data(struct ssi_protocol
*ssi
,
202 struct sk_buff
*skb
, gfp_t flags
)
206 msg
= hsi_alloc_msg(skb_shinfo(skb
)->nr_frags
+ 1, flags
);
209 ssip_skb_to_msg(skb
, msg
);
210 msg
->destructor
= ssip_free_data
;
211 msg
->channel
= ssi
->channel_id_data
;
217 static inline void ssip_release_cmd(struct hsi_msg
*msg
)
219 struct ssi_protocol
*ssi
= hsi_client_drvdata(msg
->cl
);
221 dev_dbg(&msg
->cl
->device
, "Release cmd 0x%08x\n", ssip_get_cmd(msg
));
222 spin_lock_bh(&ssi
->lock
);
223 list_add_tail(&msg
->link
, &ssi
->cmdqueue
);
224 spin_unlock_bh(&ssi
->lock
);
227 static struct hsi_msg
*ssip_claim_cmd(struct ssi_protocol
*ssi
)
231 BUG_ON(list_empty(&ssi
->cmdqueue
));
233 spin_lock_bh(&ssi
->lock
);
234 msg
= list_first_entry(&ssi
->cmdqueue
, struct hsi_msg
, link
);
235 list_del(&msg
->link
);
236 spin_unlock_bh(&ssi
->lock
);
237 msg
->destructor
= ssip_release_cmd
;
242 static void ssip_free_cmds(struct ssi_protocol
*ssi
)
244 struct hsi_msg
*msg
, *tmp
;
246 list_for_each_entry_safe(msg
, tmp
, &ssi
->cmdqueue
, link
) {
247 list_del(&msg
->link
);
248 msg
->destructor
= NULL
;
249 kfree(sg_virt(msg
->sgt
.sgl
));
254 static int ssip_alloc_cmds(struct ssi_protocol
*ssi
)
260 for (i
= 0; i
< SSIP_MAX_CMDS
; i
++) {
261 msg
= hsi_alloc_msg(1, GFP_KERNEL
);
264 buf
= kmalloc(sizeof(*buf
), GFP_KERNEL
);
269 sg_init_one(msg
->sgt
.sgl
, buf
, sizeof(*buf
));
270 msg
->channel
= ssi
->channel_id_cmd
;
271 list_add_tail(&msg
->link
, &ssi
->cmdqueue
);
281 static void ssip_set_rxstate(struct ssi_protocol
*ssi
, unsigned int state
)
283 ssi
->recv_state
= state
;
286 del_timer(&ssi
->rx_wd
);
287 if (ssi
->send_state
== SEND_IDLE
)
288 del_timer(&ssi
->keep_alive
);
291 /* CMT speech workaround */
292 if (atomic_read(&ssi
->tx_usecnt
))
296 mod_timer(&ssi
->keep_alive
, jiffies
+
297 msecs_to_jiffies(SSIP_KATOUT
));
298 mod_timer(&ssi
->rx_wd
, jiffies
+ msecs_to_jiffies(SSIP_WDTOUT
));
305 static void ssip_set_txstate(struct ssi_protocol
*ssi
, unsigned int state
)
307 ssi
->send_state
= state
;
311 del_timer(&ssi
->tx_wd
);
312 if (ssi
->recv_state
== RECV_IDLE
)
313 del_timer(&ssi
->keep_alive
);
317 case SENDING_SWBREAK
:
318 mod_timer(&ssi
->keep_alive
,
319 jiffies
+ msecs_to_jiffies(SSIP_KATOUT
));
320 mod_timer(&ssi
->tx_wd
, jiffies
+ msecs_to_jiffies(SSIP_WDTOUT
));
327 struct hsi_client
*ssip_slave_get_master(struct hsi_client
*slave
)
329 struct hsi_client
*master
= ERR_PTR(-ENODEV
);
330 struct ssi_protocol
*ssi
;
332 list_for_each_entry(ssi
, &ssip_list
, link
)
333 if (slave
->device
.parent
== ssi
->cl
->device
.parent
) {
340 EXPORT_SYMBOL_GPL(ssip_slave_get_master
);
342 int ssip_slave_start_tx(struct hsi_client
*master
)
344 struct ssi_protocol
*ssi
= hsi_client_drvdata(master
);
346 dev_dbg(&master
->device
, "start TX %d\n", atomic_read(&ssi
->tx_usecnt
));
347 spin_lock_bh(&ssi
->lock
);
348 if (ssi
->send_state
== SEND_IDLE
) {
349 ssip_set_txstate(ssi
, WAIT4READY
);
350 hsi_start_tx(master
);
352 spin_unlock_bh(&ssi
->lock
);
353 atomic_inc(&ssi
->tx_usecnt
);
357 EXPORT_SYMBOL_GPL(ssip_slave_start_tx
);
359 int ssip_slave_stop_tx(struct hsi_client
*master
)
361 struct ssi_protocol
*ssi
= hsi_client_drvdata(master
);
363 WARN_ON_ONCE(atomic_read(&ssi
->tx_usecnt
) == 0);
365 if (atomic_dec_and_test(&ssi
->tx_usecnt
)) {
366 spin_lock_bh(&ssi
->lock
);
367 if ((ssi
->send_state
== SEND_READY
) ||
368 (ssi
->send_state
== WAIT4READY
)) {
369 ssip_set_txstate(ssi
, SEND_IDLE
);
372 spin_unlock_bh(&ssi
->lock
);
374 dev_dbg(&master
->device
, "stop TX %d\n", atomic_read(&ssi
->tx_usecnt
));
378 EXPORT_SYMBOL_GPL(ssip_slave_stop_tx
);
380 int ssip_slave_running(struct hsi_client
*master
)
382 struct ssi_protocol
*ssi
= hsi_client_drvdata(master
);
383 return netif_running(ssi
->netdev
);
385 EXPORT_SYMBOL_GPL(ssip_slave_running
);
387 static void ssip_reset(struct hsi_client
*cl
)
389 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
390 struct list_head
*head
, *tmp
;
393 if (netif_running(ssi
->netdev
))
394 netif_carrier_off(ssi
->netdev
);
396 spin_lock_bh(&ssi
->lock
);
397 if (ssi
->send_state
!= SEND_IDLE
)
399 spin_unlock_bh(&ssi
->lock
);
400 if (test_and_clear_bit(SSIP_WAKETEST_FLAG
, &ssi
->flags
))
401 ssi_waketest(cl
, 0); /* FIXME: To be removed */
402 spin_lock_bh(&ssi
->lock
);
403 del_timer(&ssi
->rx_wd
);
404 del_timer(&ssi
->tx_wd
);
405 del_timer(&ssi
->keep_alive
);
412 list_for_each_safe(head
, tmp
, &ssi
->txqueue
) {
413 msg
= list_entry(head
, struct hsi_msg
, link
);
414 dev_dbg(&cl
->device
, "Pending TX data\n");
418 ssi
->txqueue_len
= 0;
419 spin_unlock_bh(&ssi
->lock
);
422 static void ssip_dump_state(struct hsi_client
*cl
)
424 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
427 spin_lock_bh(&ssi
->lock
);
428 dev_err(&cl
->device
, "Main state: %d\n", ssi
->main_state
);
429 dev_err(&cl
->device
, "Recv state: %d\n", ssi
->recv_state
);
430 dev_err(&cl
->device
, "Send state: %d\n", ssi
->send_state
);
431 dev_err(&cl
->device
, "CMT %s\n", (ssi
->main_state
== ACTIVE
) ?
432 "Online" : "Offline");
433 dev_err(&cl
->device
, "Wake test %d\n",
434 test_bit(SSIP_WAKETEST_FLAG
, &ssi
->flags
));
435 dev_err(&cl
->device
, "Data RX id: %d\n", ssi
->rxid
);
436 dev_err(&cl
->device
, "Data TX id: %d\n", ssi
->txid
);
438 list_for_each_entry(msg
, &ssi
->txqueue
, link
)
439 dev_err(&cl
->device
, "pending TX data (%p)\n", msg
);
440 spin_unlock_bh(&ssi
->lock
);
443 static void ssip_error(struct hsi_client
*cl
)
445 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
450 msg
= ssip_claim_cmd(ssi
);
451 msg
->complete
= ssip_rxcmd_complete
;
452 hsi_async_read(cl
, msg
);
455 static void ssip_keep_alive(struct timer_list
*t
)
457 struct ssi_protocol
*ssi
= from_timer(ssi
, t
, keep_alive
);
458 struct hsi_client
*cl
= ssi
->cl
;
460 dev_dbg(&cl
->device
, "Keep alive kick in: m(%d) r(%d) s(%d)\n",
461 ssi
->main_state
, ssi
->recv_state
, ssi
->send_state
);
463 spin_lock(&ssi
->lock
);
464 if (ssi
->recv_state
== RECV_IDLE
)
465 switch (ssi
->send_state
) {
467 if (atomic_read(&ssi
->tx_usecnt
) == 0)
471 * Workaround for cmt-speech in that case
472 * we relay on audio timers.
475 spin_unlock(&ssi
->lock
);
478 mod_timer(&ssi
->keep_alive
, jiffies
+ msecs_to_jiffies(SSIP_KATOUT
));
479 spin_unlock(&ssi
->lock
);
482 static void ssip_rx_wd(struct timer_list
*t
)
484 struct ssi_protocol
*ssi
= from_timer(ssi
, t
, rx_wd
);
485 struct hsi_client
*cl
= ssi
->cl
;
487 dev_err(&cl
->device
, "Watchdog triggered\n");
491 static void ssip_tx_wd(struct timer_list
*t
)
493 struct ssi_protocol
*ssi
= from_timer(ssi
, t
, tx_wd
);
494 struct hsi_client
*cl
= ssi
->cl
;
496 dev_err(&cl
->device
, "Watchdog triggered\n");
500 static void ssip_send_bootinfo_req_cmd(struct hsi_client
*cl
)
502 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
505 dev_dbg(&cl
->device
, "Issuing BOOT INFO REQ command\n");
506 msg
= ssip_claim_cmd(ssi
);
507 ssip_set_cmd(msg
, SSIP_BOOTINFO_REQ_CMD(SSIP_LOCAL_VERID
));
508 msg
->complete
= ssip_release_cmd
;
509 hsi_async_write(cl
, msg
);
510 dev_dbg(&cl
->device
, "Issuing RX command\n");
511 msg
= ssip_claim_cmd(ssi
);
512 msg
->complete
= ssip_rxcmd_complete
;
513 hsi_async_read(cl
, msg
);
516 static void ssip_start_rx(struct hsi_client
*cl
)
518 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
521 dev_dbg(&cl
->device
, "RX start M(%d) R(%d)\n", ssi
->main_state
,
523 spin_lock_bh(&ssi
->lock
);
525 * We can have two UP events in a row due to a short low
526 * high transition. Therefore we need to ignore the sencond UP event.
528 if ((ssi
->main_state
!= ACTIVE
) || (ssi
->recv_state
== RECV_READY
)) {
529 spin_unlock_bh(&ssi
->lock
);
532 ssip_set_rxstate(ssi
, RECV_READY
);
533 spin_unlock_bh(&ssi
->lock
);
535 msg
= ssip_claim_cmd(ssi
);
536 ssip_set_cmd(msg
, SSIP_READY_CMD
);
537 msg
->complete
= ssip_release_cmd
;
538 dev_dbg(&cl
->device
, "Send READY\n");
539 hsi_async_write(cl
, msg
);
542 static void ssip_stop_rx(struct hsi_client
*cl
)
544 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
546 dev_dbg(&cl
->device
, "RX stop M(%d)\n", ssi
->main_state
);
547 spin_lock_bh(&ssi
->lock
);
548 if (likely(ssi
->main_state
== ACTIVE
))
549 ssip_set_rxstate(ssi
, RECV_IDLE
);
550 spin_unlock_bh(&ssi
->lock
);
553 static void ssip_free_strans(struct hsi_msg
*msg
)
555 ssip_free_data(msg
->context
);
556 ssip_release_cmd(msg
);
559 static void ssip_strans_complete(struct hsi_msg
*msg
)
561 struct hsi_client
*cl
= msg
->cl
;
562 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
563 struct hsi_msg
*data
;
566 ssip_release_cmd(msg
);
567 spin_lock_bh(&ssi
->lock
);
568 ssip_set_txstate(ssi
, SENDING
);
569 spin_unlock_bh(&ssi
->lock
);
570 hsi_async_write(cl
, data
);
573 static int ssip_xmit(struct hsi_client
*cl
)
575 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
576 struct hsi_msg
*msg
, *dmsg
;
579 spin_lock_bh(&ssi
->lock
);
580 if (list_empty(&ssi
->txqueue
)) {
581 spin_unlock_bh(&ssi
->lock
);
584 dmsg
= list_first_entry(&ssi
->txqueue
, struct hsi_msg
, link
);
585 list_del(&dmsg
->link
);
587 spin_unlock_bh(&ssi
->lock
);
589 msg
= ssip_claim_cmd(ssi
);
592 msg
->complete
= ssip_strans_complete
;
593 msg
->destructor
= ssip_free_strans
;
595 spin_lock_bh(&ssi
->lock
);
596 ssip_set_cmd(msg
, SSIP_START_TRANS_CMD(SSIP_BYTES_TO_FRAMES(skb
->len
),
599 ssip_set_txstate(ssi
, SENDING
);
600 spin_unlock_bh(&ssi
->lock
);
602 dev_dbg(&cl
->device
, "Send STRANS (%d frames)\n",
603 SSIP_BYTES_TO_FRAMES(skb
->len
));
605 return hsi_async_write(cl
, msg
);
608 /* In soft IRQ context */
609 static void ssip_pn_rx(struct sk_buff
*skb
)
611 struct net_device
*dev
= skb
->dev
;
613 if (unlikely(!netif_running(dev
))) {
614 dev_dbg(&dev
->dev
, "Drop RX packet\n");
615 dev
->stats
.rx_dropped
++;
619 if (unlikely(!pskb_may_pull(skb
, SSIP_MIN_PN_HDR
))) {
620 dev_dbg(&dev
->dev
, "Error drop RX packet\n");
621 dev
->stats
.rx_errors
++;
622 dev
->stats
.rx_length_errors
++;
626 dev
->stats
.rx_packets
++;
627 dev
->stats
.rx_bytes
+= skb
->len
;
629 /* length field is exchanged in network byte order */
630 ((u16
*)skb
->data
)[2] = ntohs(((u16
*)skb
->data
)[2]);
631 dev_dbg(&dev
->dev
, "RX length fixed (%04x -> %u)\n",
632 ((u16
*)skb
->data
)[2], ntohs(((u16
*)skb
->data
)[2]));
634 skb
->protocol
= htons(ETH_P_PHONET
);
635 skb_reset_mac_header(skb
);
640 static void ssip_rx_data_complete(struct hsi_msg
*msg
)
642 struct hsi_client
*cl
= msg
->cl
;
643 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
646 if (msg
->status
== HSI_STATUS_ERROR
) {
647 dev_err(&cl
->device
, "RX data error\n");
652 del_timer(&ssi
->rx_wd
); /* FIXME: Revisit */
658 static void ssip_rx_bootinforeq(struct hsi_client
*cl
, u32 cmd
)
660 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
663 /* Workaroud: Ignore CMT Loader message leftover */
664 if (cmd
== SSIP_CMT_LOADER_SYNC
)
667 switch (ssi
->main_state
) {
669 dev_err(&cl
->device
, "Boot info req on active state\n");
674 spin_lock_bh(&ssi
->lock
);
675 ssi
->main_state
= HANDSHAKE
;
676 spin_unlock_bh(&ssi
->lock
);
678 if (!test_and_set_bit(SSIP_WAKETEST_FLAG
, &ssi
->flags
))
679 ssi_waketest(cl
, 1); /* FIXME: To be removed */
681 spin_lock_bh(&ssi
->lock
);
682 /* Start boot handshake watchdog */
683 mod_timer(&ssi
->tx_wd
, jiffies
+ msecs_to_jiffies(SSIP_WDTOUT
));
684 spin_unlock_bh(&ssi
->lock
);
685 dev_dbg(&cl
->device
, "Send BOOTINFO_RESP\n");
686 if (SSIP_DATA_VERSION(cmd
) != SSIP_LOCAL_VERID
)
687 dev_warn(&cl
->device
, "boot info req verid mismatch\n");
688 msg
= ssip_claim_cmd(ssi
);
689 ssip_set_cmd(msg
, SSIP_BOOTINFO_RESP_CMD(SSIP_LOCAL_VERID
));
690 msg
->complete
= ssip_release_cmd
;
691 hsi_async_write(cl
, msg
);
694 dev_dbg(&cl
->device
, "Wrong state M(%d)\n", ssi
->main_state
);
699 static void ssip_rx_bootinforesp(struct hsi_client
*cl
, u32 cmd
)
701 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
703 if (SSIP_DATA_VERSION(cmd
) != SSIP_LOCAL_VERID
)
704 dev_warn(&cl
->device
, "boot info resp verid mismatch\n");
706 spin_lock_bh(&ssi
->lock
);
707 if (ssi
->main_state
!= ACTIVE
)
708 /* Use tx_wd as a boot watchdog in non ACTIVE state */
709 mod_timer(&ssi
->tx_wd
, jiffies
+ msecs_to_jiffies(SSIP_WDTOUT
));
711 dev_dbg(&cl
->device
, "boot info resp ignored M(%d)\n",
713 spin_unlock_bh(&ssi
->lock
);
716 static void ssip_rx_waketest(struct hsi_client
*cl
, u32 cmd
)
718 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
719 unsigned int wkres
= SSIP_PAYLOAD(cmd
);
721 spin_lock_bh(&ssi
->lock
);
722 if (ssi
->main_state
!= HANDSHAKE
) {
723 dev_dbg(&cl
->device
, "wake lines test ignored M(%d)\n",
725 spin_unlock_bh(&ssi
->lock
);
728 spin_unlock_bh(&ssi
->lock
);
730 if (test_and_clear_bit(SSIP_WAKETEST_FLAG
, &ssi
->flags
))
731 ssi_waketest(cl
, 0); /* FIXME: To be removed */
733 spin_lock_bh(&ssi
->lock
);
734 ssi
->main_state
= ACTIVE
;
735 del_timer(&ssi
->tx_wd
); /* Stop boot handshake timer */
736 spin_unlock_bh(&ssi
->lock
);
738 dev_notice(&cl
->device
, "WAKELINES TEST %s\n",
739 wkres
& SSIP_WAKETEST_FAILED
? "FAILED" : "OK");
740 if (wkres
& SSIP_WAKETEST_FAILED
) {
744 dev_dbg(&cl
->device
, "CMT is ONLINE\n");
745 netif_wake_queue(ssi
->netdev
);
746 netif_carrier_on(ssi
->netdev
);
749 static void ssip_rx_ready(struct hsi_client
*cl
)
751 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
753 spin_lock_bh(&ssi
->lock
);
754 if (unlikely(ssi
->main_state
!= ACTIVE
)) {
755 dev_dbg(&cl
->device
, "READY on wrong state: S(%d) M(%d)\n",
756 ssi
->send_state
, ssi
->main_state
);
757 spin_unlock_bh(&ssi
->lock
);
760 if (ssi
->send_state
!= WAIT4READY
) {
761 dev_dbg(&cl
->device
, "Ignore spurious READY command\n");
762 spin_unlock_bh(&ssi
->lock
);
765 ssip_set_txstate(ssi
, SEND_READY
);
766 spin_unlock_bh(&ssi
->lock
);
770 static void ssip_rx_strans(struct hsi_client
*cl
, u32 cmd
)
772 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
775 int len
= SSIP_PDU_LENGTH(cmd
);
777 dev_dbg(&cl
->device
, "RX strans: %d frames\n", len
);
778 spin_lock_bh(&ssi
->lock
);
779 if (unlikely(ssi
->main_state
!= ACTIVE
)) {
780 dev_err(&cl
->device
, "START TRANS wrong state: S(%d) M(%d)\n",
781 ssi
->send_state
, ssi
->main_state
);
782 spin_unlock_bh(&ssi
->lock
);
785 ssip_set_rxstate(ssi
, RECEIVING
);
786 if (unlikely(SSIP_MSG_ID(cmd
) != ssi
->rxid
)) {
787 dev_err(&cl
->device
, "START TRANS id %d expected %d\n",
788 SSIP_MSG_ID(cmd
), ssi
->rxid
);
789 spin_unlock_bh(&ssi
->lock
);
793 spin_unlock_bh(&ssi
->lock
);
794 skb
= netdev_alloc_skb(ssi
->netdev
, len
* 4);
795 if (unlikely(!skb
)) {
796 dev_err(&cl
->device
, "No memory for rx skb\n");
799 skb
->dev
= ssi
->netdev
;
800 skb_put(skb
, len
* 4);
801 msg
= ssip_alloc_data(ssi
, skb
, GFP_ATOMIC
);
802 if (unlikely(!msg
)) {
803 dev_err(&cl
->device
, "No memory for RX data msg\n");
806 msg
->complete
= ssip_rx_data_complete
;
807 hsi_async_read(cl
, msg
);
816 static void ssip_rxcmd_complete(struct hsi_msg
*msg
)
818 struct hsi_client
*cl
= msg
->cl
;
819 u32 cmd
= ssip_get_cmd(msg
);
820 unsigned int cmdid
= SSIP_COMMAND(cmd
);
822 if (msg
->status
== HSI_STATUS_ERROR
) {
823 dev_err(&cl
->device
, "RX error detected\n");
824 ssip_release_cmd(msg
);
828 hsi_async_read(cl
, msg
);
829 dev_dbg(&cl
->device
, "RX cmd: 0x%08x\n", cmd
);
834 case SSIP_BOOTINFO_REQ
:
835 ssip_rx_bootinforeq(cl
, cmd
);
837 case SSIP_BOOTINFO_RESP
:
838 ssip_rx_bootinforesp(cl
, cmd
);
840 case SSIP_WAKETEST_RESULT
:
841 ssip_rx_waketest(cl
, cmd
);
843 case SSIP_START_TRANS
:
844 ssip_rx_strans(cl
, cmd
);
850 dev_warn(&cl
->device
, "command 0x%08x not supported\n", cmd
);
855 static void ssip_swbreak_complete(struct hsi_msg
*msg
)
857 struct hsi_client
*cl
= msg
->cl
;
858 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
860 ssip_release_cmd(msg
);
861 spin_lock_bh(&ssi
->lock
);
862 if (list_empty(&ssi
->txqueue
)) {
863 if (atomic_read(&ssi
->tx_usecnt
)) {
864 ssip_set_txstate(ssi
, SEND_READY
);
866 ssip_set_txstate(ssi
, SEND_IDLE
);
869 spin_unlock_bh(&ssi
->lock
);
871 spin_unlock_bh(&ssi
->lock
);
874 netif_wake_queue(ssi
->netdev
);
877 static void ssip_tx_data_complete(struct hsi_msg
*msg
)
879 struct hsi_client
*cl
= msg
->cl
;
880 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
881 struct hsi_msg
*cmsg
;
883 if (msg
->status
== HSI_STATUS_ERROR
) {
884 dev_err(&cl
->device
, "TX data error\n");
888 spin_lock_bh(&ssi
->lock
);
889 if (list_empty(&ssi
->txqueue
)) {
890 ssip_set_txstate(ssi
, SENDING_SWBREAK
);
891 spin_unlock_bh(&ssi
->lock
);
892 cmsg
= ssip_claim_cmd(ssi
);
893 ssip_set_cmd(cmsg
, SSIP_SWBREAK_CMD
);
894 cmsg
->complete
= ssip_swbreak_complete
;
895 dev_dbg(&cl
->device
, "Send SWBREAK\n");
896 hsi_async_write(cl
, cmsg
);
898 spin_unlock_bh(&ssi
->lock
);
905 static void ssip_port_event(struct hsi_client
*cl
, unsigned long event
)
908 case HSI_EVENT_START_RX
:
911 case HSI_EVENT_STOP_RX
:
919 static int ssip_pn_open(struct net_device
*dev
)
921 struct hsi_client
*cl
= to_hsi_client(dev
->dev
.parent
);
922 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
925 err
= hsi_claim_port(cl
, 1);
927 dev_err(&cl
->device
, "SSI port already claimed\n");
930 err
= hsi_register_port_event(cl
, ssip_port_event
);
932 dev_err(&cl
->device
, "Register HSI port event failed (%d)\n",
936 dev_dbg(&cl
->device
, "Configuring SSI port\n");
939 if (!test_and_set_bit(SSIP_WAKETEST_FLAG
, &ssi
->flags
))
940 ssi_waketest(cl
, 1); /* FIXME: To be removed */
942 spin_lock_bh(&ssi
->lock
);
943 ssi
->main_state
= HANDSHAKE
;
944 spin_unlock_bh(&ssi
->lock
);
946 ssip_send_bootinfo_req_cmd(cl
);
951 static int ssip_pn_stop(struct net_device
*dev
)
953 struct hsi_client
*cl
= to_hsi_client(dev
->dev
.parent
);
956 hsi_unregister_port_event(cl
);
957 hsi_release_port(cl
);
962 static void ssip_xmit_work(struct work_struct
*work
)
964 struct ssi_protocol
*ssi
=
965 container_of(work
, struct ssi_protocol
, work
);
966 struct hsi_client
*cl
= ssi
->cl
;
971 static int ssip_pn_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
973 struct hsi_client
*cl
= to_hsi_client(dev
->dev
.parent
);
974 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
977 if ((skb
->protocol
!= htons(ETH_P_PHONET
)) ||
978 (skb
->len
< SSIP_MIN_PN_HDR
))
980 /* Pad to 32-bits - FIXME: Revisit*/
981 if ((skb
->len
& 3) && skb_pad(skb
, 4 - (skb
->len
& 3)))
985 * Modem sends Phonet messages over SSI with its own endianness.
986 * Assume that modem has the same endianness as we do.
988 if (skb_cow_head(skb
, 0))
991 /* length field is exchanged in network byte order */
992 ((u16
*)skb
->data
)[2] = htons(((u16
*)skb
->data
)[2]);
994 msg
= ssip_alloc_data(ssi
, skb
, GFP_ATOMIC
);
996 dev_dbg(&cl
->device
, "Dropping tx data: No memory\n");
999 msg
->complete
= ssip_tx_data_complete
;
1001 spin_lock_bh(&ssi
->lock
);
1002 if (unlikely(ssi
->main_state
!= ACTIVE
)) {
1003 spin_unlock_bh(&ssi
->lock
);
1004 dev_dbg(&cl
->device
, "Dropping tx data: CMT is OFFLINE\n");
1007 list_add_tail(&msg
->link
, &ssi
->txqueue
);
1009 if (dev
->tx_queue_len
< ssi
->txqueue_len
) {
1010 dev_info(&cl
->device
, "TX queue full %d\n", ssi
->txqueue_len
);
1011 netif_stop_queue(dev
);
1013 if (ssi
->send_state
== SEND_IDLE
) {
1014 ssip_set_txstate(ssi
, WAIT4READY
);
1015 spin_unlock_bh(&ssi
->lock
);
1016 dev_dbg(&cl
->device
, "Start TX qlen %d\n", ssi
->txqueue_len
);
1018 } else if (ssi
->send_state
== SEND_READY
) {
1019 /* Needed for cmt-speech workaround */
1020 dev_dbg(&cl
->device
, "Start TX on SEND READY qlen %d\n",
1022 spin_unlock_bh(&ssi
->lock
);
1023 schedule_work(&ssi
->work
);
1025 spin_unlock_bh(&ssi
->lock
);
1027 dev
->stats
.tx_packets
++;
1028 dev
->stats
.tx_bytes
+= skb
->len
;
1036 dev
->stats
.tx_dropped
++;
1041 /* CMT reset event handler */
1042 void ssip_reset_event(struct hsi_client
*master
)
1044 struct ssi_protocol
*ssi
= hsi_client_drvdata(master
);
1045 dev_err(&ssi
->cl
->device
, "CMT reset detected!\n");
1046 ssip_error(ssi
->cl
);
1048 EXPORT_SYMBOL_GPL(ssip_reset_event
);
1050 static const struct net_device_ops ssip_pn_ops
= {
1051 .ndo_open
= ssip_pn_open
,
1052 .ndo_stop
= ssip_pn_stop
,
1053 .ndo_start_xmit
= ssip_pn_xmit
,
1056 static void ssip_pn_setup(struct net_device
*dev
)
1059 dev
->netdev_ops
= &ssip_pn_ops
;
1060 dev
->type
= ARPHRD_PHONET
;
1061 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;
1062 dev
->mtu
= SSIP_DEFAULT_MTU
;
1063 dev
->hard_header_len
= 1;
1064 dev
->dev_addr
[0] = PN_MEDIA_SOS
;
1066 dev
->tx_queue_len
= SSIP_TXQUEUE_LEN
;
1068 dev
->needs_free_netdev
= true;
1069 dev
->header_ops
= &phonet_header_ops
;
1072 static int ssi_protocol_probe(struct device
*dev
)
1074 static const char ifname
[] = "phonet%d";
1075 struct hsi_client
*cl
= to_hsi_client(dev
);
1076 struct ssi_protocol
*ssi
;
1079 ssi
= kzalloc(sizeof(*ssi
), GFP_KERNEL
);
1083 spin_lock_init(&ssi
->lock
);
1084 timer_setup(&ssi
->rx_wd
, ssip_rx_wd
, TIMER_DEFERRABLE
);
1085 timer_setup(&ssi
->tx_wd
, ssip_tx_wd
, TIMER_DEFERRABLE
);
1086 timer_setup(&ssi
->keep_alive
, ssip_keep_alive
, 0);
1087 INIT_LIST_HEAD(&ssi
->txqueue
);
1088 INIT_LIST_HEAD(&ssi
->cmdqueue
);
1089 atomic_set(&ssi
->tx_usecnt
, 0);
1090 hsi_client_set_drvdata(cl
, ssi
);
1092 INIT_WORK(&ssi
->work
, ssip_xmit_work
);
1094 ssi
->channel_id_cmd
= hsi_get_channel_id_by_name(cl
, "mcsaab-control");
1095 if (ssi
->channel_id_cmd
< 0) {
1096 err
= ssi
->channel_id_cmd
;
1097 dev_err(dev
, "Could not get cmd channel (%d)\n", err
);
1101 ssi
->channel_id_data
= hsi_get_channel_id_by_name(cl
, "mcsaab-data");
1102 if (ssi
->channel_id_data
< 0) {
1103 err
= ssi
->channel_id_data
;
1104 dev_err(dev
, "Could not get data channel (%d)\n", err
);
1108 err
= ssip_alloc_cmds(ssi
);
1110 dev_err(dev
, "No memory for commands\n");
1114 ssi
->netdev
= alloc_netdev(0, ifname
, NET_NAME_UNKNOWN
, ssip_pn_setup
);
1116 dev_err(dev
, "No memory for netdev\n");
1121 /* MTU range: 6 - 65535 */
1122 ssi
->netdev
->min_mtu
= PHONET_MIN_MTU
;
1123 ssi
->netdev
->max_mtu
= SSIP_MAX_MTU
;
1125 SET_NETDEV_DEV(ssi
->netdev
, dev
);
1126 netif_carrier_off(ssi
->netdev
);
1127 err
= register_netdev(ssi
->netdev
);
1129 dev_err(dev
, "Register netdev failed (%d)\n", err
);
1133 list_add(&ssi
->link
, &ssip_list
);
1135 dev_dbg(dev
, "channel configuration: cmd=%d, data=%d\n",
1136 ssi
->channel_id_cmd
, ssi
->channel_id_data
);
1140 free_netdev(ssi
->netdev
);
1142 ssip_free_cmds(ssi
);
1149 static int ssi_protocol_remove(struct device
*dev
)
1151 struct hsi_client
*cl
= to_hsi_client(dev
);
1152 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
1154 list_del(&ssi
->link
);
1155 unregister_netdev(ssi
->netdev
);
1156 ssip_free_cmds(ssi
);
1157 hsi_client_set_drvdata(cl
, NULL
);
1163 static struct hsi_client_driver ssip_driver
= {
1165 .name
= "ssi-protocol",
1166 .owner
= THIS_MODULE
,
1167 .probe
= ssi_protocol_probe
,
1168 .remove
= ssi_protocol_remove
,
1172 static int __init
ssip_init(void)
1174 pr_info("SSI protocol aka McSAAB added\n");
1176 return hsi_register_client_driver(&ssip_driver
);
1178 module_init(ssip_init
);
1180 static void __exit
ssip_exit(void)
1182 hsi_unregister_client_driver(&ssip_driver
);
1183 pr_info("SSI protocol driver removed\n");
1185 module_exit(ssip_exit
);
1187 MODULE_ALIAS("hsi:ssi-protocol");
1188 MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>");
1189 MODULE_AUTHOR("Remi Denis-Courmont <remi.denis-courmont@nokia.com>");
1190 MODULE_DESCRIPTION("SSI protocol improved aka McSAAB");
1191 MODULE_LICENSE("GPL");